2 This code is based on the glslang_c_interface implementation by Viktor Latypov
8 Copyright (c) 2019, Viktor Latypov
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
14 1. Redistributions of source code must retain the above copyright notice, this
15 list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright notice,
18 this list of conditions and the following disclaimer in the documentation
19 and/or other materials provided with the distribution.
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "glslang/Include/glslang_c_interface.h"
36 #include "glslang/Public/ShaderLang.h"
37 #include "SPIRV/GlslangToSpv.h"
38 #include "SPIRV/Logger.h"
39 #include "SPIRV/SpvTools.h"
41 static_assert(sizeof(glslang_spv_options_t
) == sizeof(glslang::SpvOptions
), "");
43 typedef struct glslang_program_s
{
44 glslang::TProgram
* program
;
45 std::vector
<unsigned int> spirv
;
46 std::string loggerMessages
;
49 static EShLanguage
c_shader_stage(glslang_stage_t stage
)
52 case GLSLANG_STAGE_VERTEX
:
54 case GLSLANG_STAGE_TESSCONTROL
:
55 return EShLangTessControl
;
56 case GLSLANG_STAGE_TESSEVALUATION
:
57 return EShLangTessEvaluation
;
58 case GLSLANG_STAGE_GEOMETRY
:
59 return EShLangGeometry
;
60 case GLSLANG_STAGE_FRAGMENT
:
61 return EShLangFragment
;
62 case GLSLANG_STAGE_COMPUTE
:
63 return EShLangCompute
;
64 case GLSLANG_STAGE_RAYGEN
:
66 case GLSLANG_STAGE_INTERSECT
:
67 return EShLangIntersect
;
68 case GLSLANG_STAGE_ANYHIT
:
70 case GLSLANG_STAGE_CLOSESTHIT
:
71 return EShLangClosestHit
;
72 case GLSLANG_STAGE_MISS
:
74 case GLSLANG_STAGE_CALLABLE
:
75 return EShLangCallable
;
76 case GLSLANG_STAGE_TASK
:
78 case GLSLANG_STAGE_MESH
:
86 GLSLANG_EXPORT
void glslang_program_SPIRV_generate(glslang_program_t
* program
, glslang_stage_t stage
)
88 glslang_spv_options_t spv_options
{};
89 spv_options
.disable_optimizer
= true;
90 spv_options
.validate
= true;
92 glslang_program_SPIRV_generate_with_options(program
, stage
, &spv_options
);
95 GLSLANG_EXPORT
void glslang_program_SPIRV_generate_with_options(glslang_program_t
* program
, glslang_stage_t stage
, glslang_spv_options_t
* spv_options
) {
96 spv::SpvBuildLogger logger
;
98 const glslang::TIntermediate
* intermediate
= program
->program
->getIntermediate(c_shader_stage(stage
));
100 program
->spirv
.clear();
102 glslang::GlslangToSpv(*intermediate
, program
->spirv
, &logger
, reinterpret_cast<glslang::SpvOptions
*>(spv_options
));
104 program
->loggerMessages
= logger
.getAllMessages();
107 GLSLANG_EXPORT
size_t glslang_program_SPIRV_get_size(glslang_program_t
* program
) { return program
->spirv
.size(); }
109 GLSLANG_EXPORT
void glslang_program_SPIRV_get(glslang_program_t
* program
, unsigned int* out
)
111 memcpy(out
, program
->spirv
.data(), program
->spirv
.size() * sizeof(unsigned int));
114 GLSLANG_EXPORT
unsigned int* glslang_program_SPIRV_get_ptr(glslang_program_t
* program
)
116 return program
->spirv
.data();
119 GLSLANG_EXPORT
const char* glslang_program_SPIRV_get_messages(glslang_program_t
* program
)
121 return program
->loggerMessages
.empty() ? nullptr : program
->loggerMessages
.c_str();