annotate src/glslang/glslang.nim @ 0:5daf3f236d87

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