• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2025 The Khronos Group Inc.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions
7 // are met:
8 //
9 //    Redistributions of source code must retain the above copyright
10 //    notice, this list of conditions and the following disclaimer.
11 //
12 //    Redistributions in binary form must reproduce the above
13 //    copyright notice, this list of conditions and the following
14 //    disclaimer in the documentation and/or other materials provided
15 //    with the distribution.
16 //
17 //    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
18 //    contributors may be used to endorse or promote products derived
19 //    from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 // POSSIBILITY OF SUCH DAMAGE.
33 //
34 
35 #include <stdint.h>
36 #include <stdio.h>
37 
38 #include <glslang/Include/glslang_c_interface.h>
39 
40 // Required for use of glslang_default_resource
41 #include <glslang/Public/resource_limits_c.h>
42 
43 typedef struct SpirVBinary {
44     uint32_t* words; // SPIR-V words
45     int size;        // number of words in SPIR-V binary
46 } SpirVBinary;
47 
compileShaderToSPIRV_Vulkan(glslang_stage_t stage,const char * shaderSource,const char * fileName)48 SpirVBinary compileShaderToSPIRV_Vulkan(glslang_stage_t stage, const char* shaderSource, const char* fileName)
49 {
50     const glslang_input_t input = {
51         .language = GLSLANG_SOURCE_GLSL,
52         .stage = stage,
53         .client = GLSLANG_CLIENT_VULKAN,
54         .client_version = GLSLANG_TARGET_VULKAN_1_2,
55         .target_language = GLSLANG_TARGET_SPV,
56         .target_language_version = GLSLANG_TARGET_SPV_1_5,
57         .code = shaderSource,
58         .default_version = 100,
59         .default_profile = GLSLANG_NO_PROFILE,
60         .force_default_version_and_profile = false,
61         .forward_compatible = false,
62         .messages = GLSLANG_MSG_DEFAULT_BIT,
63         .resource = glslang_default_resource(),
64     };
65 
66     glslang_shader_t* shader = glslang_shader_create(&input);
67 
68     SpirVBinary bin = {
69         .words = NULL,
70         .size = 0,
71     };
72     if (!glslang_shader_preprocess(shader, &input)) {
73         printf("GLSL preprocessing failed %s\n", fileName);
74         printf("%s\n", glslang_shader_get_info_log(shader));
75         printf("%s\n", glslang_shader_get_info_debug_log(shader));
76         printf("%s\n", input.code);
77         glslang_shader_delete(shader);
78         return bin;
79     }
80 
81     if (!glslang_shader_parse(shader, &input)) {
82         printf("GLSL parsing failed %s\n", fileName);
83         printf("%s\n", glslang_shader_get_info_log(shader));
84         printf("%s\n", glslang_shader_get_info_debug_log(shader));
85         printf("%s\n", glslang_shader_get_preprocessed_code(shader));
86         glslang_shader_delete(shader);
87         return bin;
88     }
89 
90     glslang_program_t* program = glslang_program_create();
91     glslang_program_add_shader(program, shader);
92 
93     if (!glslang_program_link(program, GLSLANG_MSG_SPV_RULES_BIT | GLSLANG_MSG_VULKAN_RULES_BIT)) {
94         printf("GLSL linking failed %s\n", fileName);
95         printf("%s\n", glslang_program_get_info_log(program));
96         printf("%s\n", glslang_program_get_info_debug_log(program));
97         glslang_program_delete(program);
98         glslang_shader_delete(shader);
99         return bin;
100     }
101 
102     glslang_program_SPIRV_generate(program, stage);
103 
104     bin.size = glslang_program_SPIRV_get_size(program);
105     bin.words = malloc(bin.size * sizeof(uint32_t));
106     glslang_program_SPIRV_get(program, bin.words);
107 
108     const char* spirv_messages = glslang_program_SPIRV_get_messages(program);
109     if (spirv_messages)
110         printf("(%s) %s\b", fileName, spirv_messages);
111 
112     glslang_program_delete(program);
113     glslang_shader_delete(shader);
114 
115     return bin;
116 }
117 
main()118 int main() { return 0; }
119