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