0
|
1 import glslang_c_interface
|
|
2 import glslang_c_shader_types
|
|
3
|
1
|
4 export
|
|
5 glslang_stage_t,
|
|
6 glslang_initialize_process,
|
|
7 glslang_finalize_process
|
0
|
8
|
1
|
9 type
|
|
10 ShaderVersion = enum
|
|
11 ES_VERSION = 100
|
|
12 DESKTOP_VERSION = 110
|
0
|
13
|
1
|
14 proc compileGLSLToSPIRV*(stage: glslang_stage_t, shaderSource: string, fileName: string): seq[uint32] =
|
|
15 var input = glslang_input_t(
|
|
16 stage: stage,
|
|
17 language: GLSLANG_SOURCE_GLSL,
|
|
18 client: GLSLANG_CLIENT_VULKAN,
|
|
19 client_version: GLSLANG_TARGET_VULKAN_1_2,
|
|
20 target_language: GLSLANG_TARGET_SPV,
|
|
21 target_language_version: GLSLANG_TARGET_SPV_1_5,
|
|
22 code: cstring(shaderSource),
|
|
23 default_version: ord(DESKTOP_VERSION),
|
|
24 default_profile: GLSLANG_CORE_PROFILE,
|
|
25 force_default_version_and_profile: false.cint,
|
|
26 forward_compatible: false.cint,
|
|
27 messages: GLSLANG_MSG_DEBUG_INFO_BIT,
|
|
28 resource: glslang_default_resource(),
|
|
29 )
|
0
|
30
|
1
|
31 var shader = glslang_shader_create(addr(input))
|
0
|
32
|
1
|
33 if not bool(glslang_shader_preprocess(shader, addr(input))):
|
|
34 echo "GLSL preprocessing failed " & fileName
|
|
35 echo glslang_shader_get_info_log(shader)
|
|
36 echo glslang_shader_get_info_debug_log(shader)
|
|
37 echo input.code
|
|
38 glslang_shader_delete(shader)
|
|
39 return
|
0
|
40
|
1
|
41 if not bool(glslang_shader_parse(shader, addr(input))):
|
|
42 echo "GLSL parsing failed " & fileName
|
|
43 echo glslang_shader_get_info_log(shader)
|
|
44 echo glslang_shader_get_info_debug_log(shader)
|
|
45 echo glslang_shader_get_preprocessed_code(shader)
|
|
46 glslang_shader_delete(shader)
|
|
47 return
|
|
48
|
|
49 var program: ptr glslang_program_t = glslang_program_create()
|
|
50 glslang_program_add_shader(program, shader)
|
0
|
51
|
1
|
52 if not bool(glslang_program_link(program, ord(GLSLANG_MSG_SPV_RULES_BIT) or ord(GLSLANG_MSG_VULKAN_RULES_BIT))):
|
|
53 echo "GLSL linking failed " & fileName
|
|
54 echo glslang_program_get_info_log(program)
|
|
55 echo glslang_program_get_info_debug_log(program)
|
|
56 glslang_program_delete(program)
|
|
57 glslang_shader_delete(shader)
|
|
58 return
|
0
|
59
|
1
|
60 glslang_program_SPIRV_generate(program, stage)
|
0
|
61
|
1
|
62 result = newSeq[uint32](glslang_program_SPIRV_get_size(program))
|
|
63 glslang_program_SPIRV_get(program, addr(result[0]))
|
|
64
|
|
65 var spirv_messages: cstring = glslang_program_SPIRV_get_messages(program)
|
|
66 if spirv_messages != nil:
|
|
67 echo "(%s) %s\b", fileName, spirv_messages
|
0
|
68
|
1
|
69 glslang_program_delete(program)
|
|
70 glslang_shader_delete(shader)
|
0
|
71
|
1
|
72 template checkGlslangResult*(call: untyped) =
|
|
73 let value = call
|
|
74 if value != 1:
|
|
75 raise newException(Exception, "glgslang error: " & astToStr(call) & " returned " & $value)
|