annotate src/glslang/glslang.nim @ 461:59d861a6a5c4

add: initial version
author Sam <sam@basx.dev>
date Wed, 14 Dec 2022 00:49:35 +0700
parents
children bb2a7d3a7003
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
461
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
1 # required to link the GLSL compiler
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
2 {.passl: "-Lthirdparty/glslang/lib/" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
3 {.passl: "-Lthirdparty/spirv-tools/lib/" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
4
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
5 {.passl: "-lglslang" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
6 {.passl: "-lglslang-default-resource-limits" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
7 {.passl: "-lHLSL" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
8 {.passl: "-lMachineIndependent" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
9 {.passl: "-lGenericCodeGen" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
10 {.passl: "-lOSDependent" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
11 {.passl: "-lOGLCompiler" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
12 {.passl: "-lSPIRV" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
13 {.passl: "-lSPIRV-Tools-opt" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
14
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
15 {.passl: "-lSPIRV-Tools" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
16 {.passl: "-lSPIRV-Tools-diff" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
17 {.passl: "-lSPIRV-Tools-fuzz" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
18 {.passl: "-lSPIRV-Tools-link" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
19 {.passl: "-lSPIRV-Tools-lint" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
20 {.passl: "-lSPIRV-Tools-opt" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
21 {.passl: "-lSPIRV-Tools-reduce" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
22
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
23 {.passl: "-lstdc++" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
24 {.passl: "-lm" .}
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
25
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
26 import glslang_c_interface
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
27 import glslang_c_shader_types
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
28
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
29
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
30 proc compileShaderToSPIRV_Vulkan*(stage: glslang_stage_t , shaderSource: string, fileName: string): seq[uint32] =
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
31 var input = glslang_input_t(
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
32 language: GLSLANG_SOURCE_GLSL,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
33 stage: stage,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
34 client: GLSLANG_CLIENT_VULKAN,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
35 client_version: GLSLANG_TARGET_VULKAN_1_2,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
36 target_language: GLSLANG_TARGET_SPV,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
37 target_language_version: GLSLANG_TARGET_SPV_1_5,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
38 code: shaderSource,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
39 default_version: 100,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
40 default_profile: GLSLANG_NO_PROFILE,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
41 force_default_version_and_profile: false.cint,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
42 forward_compatible: false.cint,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
43 messages: GLSLANG_MSG_DEFAULT_BIT,
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
44 resource: glslang_default_resource(),
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
45 )
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
46
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
47 let shader = glslang_shader_create(addr(input))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
48
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
49 if not bool(glslang_shader_preprocess(shader, addr(input))):
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
50 echo "GLSL preprocessing failed " & fileName
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
51 echo glslang_shader_get_info_log(shader)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
52 echo glslang_shader_get_info_debug_log(shader)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
53 echo input.code
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
54 glslang_shader_delete(shader)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
55 return
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
56
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
57 if not bool(glslang_shader_parse(shader, addr(input))):
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
58 echo "GLSL parsing failed " & fileName
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
59 echo glslang_shader_get_info_log(shader)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
60 echo glslang_shader_get_info_debug_log(shader)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
61 echo glslang_shader_get_preprocessed_code(shader)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
62 glslang_shader_delete(shader)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
63 return
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
64
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
65 let program: ptr glslang_program_t = glslang_program_create()
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
66 glslang_program_add_shader(program, shader)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
67
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
68 if not bool(glslang_program_link(program, ord(GLSLANG_MSG_SPV_RULES_BIT) or ord(GLSLANG_MSG_VULKAN_RULES_BIT))):
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
69 echo "GLSL linking failed " & fileName
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
70 echo glslang_program_get_info_log(program)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
71 echo glslang_program_get_info_debug_log(program)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
72 glslang_program_delete(program)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
73 glslang_shader_delete(shader)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
74 return
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
75
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
76 glslang_program_SPIRV_generate(program, stage)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
77
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
78 result = newSeq[uint32](glslang_program_SPIRV_get_size(program))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
79 glslang_program_SPIRV_get(program, addr(result[0]))
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
80
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
81 var spirv_messages: cstring = glslang_program_SPIRV_get_messages(program)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
82 if spirv_messages != nil:
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
83 echo "(%s) %s\b", fileName, spirv_messages
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
84
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
85 glslang_program_delete(program)
59d861a6a5c4 add: initial version
Sam <sam@basx.dev>
parents:
diff changeset
86 glslang_shader_delete(shader)