• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2     This code is based on the glslang_c_interface implementation by Viktor Latypov
3 **/
4 
5 /**
6 BSD 2-Clause License
7 
8 Copyright (c) 2019, Viktor Latypov
9 All rights reserved.
10 
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13 
14 1. Redistributions of source code must retain the above copyright notice, this
15    list of conditions and the following disclaimer.
16 
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.
20 
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.
31 **/
32 
33 #include "glslang/Include/glslang_c_interface.h"
34 
35 #include "SPIRV/GlslangToSpv.h"
36 #include "SPIRV/Logger.h"
37 #include "SPIRV/SpvTools.h"
38 
39 typedef struct glslang_program_s {
40     glslang::TProgram* program;
41     std::vector<unsigned int> spirv;
42     std::string loggerMessages;
43 } glslang_program_t;
44 
c_shader_stage(glslang_stage_t stage)45 static EShLanguage c_shader_stage(glslang_stage_t stage)
46 {
47     switch (stage) {
48     case GLSLANG_STAGE_VERTEX:
49         return EShLangVertex;
50     case GLSLANG_STAGE_TESSCONTROL:
51         return EShLangTessControl;
52     case GLSLANG_STAGE_TESSEVALUATION:
53         return EShLangTessEvaluation;
54     case GLSLANG_STAGE_GEOMETRY:
55         return EShLangGeometry;
56     case GLSLANG_STAGE_FRAGMENT:
57         return EShLangFragment;
58     case GLSLANG_STAGE_COMPUTE:
59         return EShLangCompute;
60     case GLSLANG_STAGE_RAYGEN_NV:
61         return EShLangRayGen;
62     case GLSLANG_STAGE_INTERSECT_NV:
63         return EShLangIntersect;
64     case GLSLANG_STAGE_ANYHIT_NV:
65         return EShLangAnyHit;
66     case GLSLANG_STAGE_CLOSESTHIT_NV:
67         return EShLangClosestHit;
68     case GLSLANG_STAGE_MISS_NV:
69         return EShLangMiss;
70     case GLSLANG_STAGE_CALLABLE_NV:
71         return EShLangCallable;
72     case GLSLANG_STAGE_TASK_NV:
73         return EShLangTaskNV;
74     case GLSLANG_STAGE_MESH_NV:
75         return EShLangMeshNV;
76     default:
77         break;
78     }
79     return EShLangCount;
80 }
81 
glslang_program_SPIRV_generate(glslang_program_t * program,glslang_stage_t stage)82 GLSLANG_EXPORT void glslang_program_SPIRV_generate(glslang_program_t* program, glslang_stage_t stage)
83 {
84     spv::SpvBuildLogger logger;
85     glslang::SpvOptions spvOptions;
86     spvOptions.validate = true;
87 
88     const glslang::TIntermediate* intermediate = program->program->getIntermediate(c_shader_stage(stage));
89 
90     glslang::GlslangToSpv(*intermediate, program->spirv, &logger, &spvOptions);
91 
92     program->loggerMessages = logger.getAllMessages();
93 }
94 
glslang_program_SPIRV_get_size(glslang_program_t * program)95 GLSLANG_EXPORT size_t glslang_program_SPIRV_get_size(glslang_program_t* program) { return program->spirv.size(); }
96 
glslang_program_SPIRV_get(glslang_program_t * program,unsigned int * out)97 GLSLANG_EXPORT void glslang_program_SPIRV_get(glslang_program_t* program, unsigned int* out)
98 {
99     memcpy(out, program->spirv.data(), program->spirv.size() * sizeof(unsigned int));
100 }
101 
glslang_program_SPIRV_get_ptr(glslang_program_t * program)102 GLSLANG_EXPORT unsigned int* glslang_program_SPIRV_get_ptr(glslang_program_t* program)
103 {
104     return program->spirv.data();
105 }
106 
glslang_program_SPIRV_get_messages(glslang_program_t * program)107 GLSLANG_EXPORT const char* glslang_program_SPIRV_get_messages(glslang_program_t* program)
108 {
109     return program->loggerMessages.empty() ? nullptr : program->loggerMessages.c_str();
110 }
111