GL_EXT_mesh_shader/SPV_EXT_mesh_shader implementation
[platform/upstream/glslang.git] / SPIRV / CInterface / spirv_c_interface.cpp
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 static_assert(sizeof(glslang_spv_options_t) == sizeof(glslang::SpvOptions), "");
40
41 typedef struct glslang_program_s {
42     glslang::TProgram* program;
43     std::vector<unsigned int> spirv;
44     std::string loggerMessages;
45 } glslang_program_t;
46
47 static EShLanguage c_shader_stage(glslang_stage_t stage)
48 {
49     switch (stage) {
50     case GLSLANG_STAGE_VERTEX:
51         return EShLangVertex;
52     case GLSLANG_STAGE_TESSCONTROL:
53         return EShLangTessControl;
54     case GLSLANG_STAGE_TESSEVALUATION:
55         return EShLangTessEvaluation;
56     case GLSLANG_STAGE_GEOMETRY:
57         return EShLangGeometry;
58     case GLSLANG_STAGE_FRAGMENT:
59         return EShLangFragment;
60     case GLSLANG_STAGE_COMPUTE:
61         return EShLangCompute;
62     case GLSLANG_STAGE_RAYGEN_NV:
63         return EShLangRayGen;
64     case GLSLANG_STAGE_INTERSECT_NV:
65         return EShLangIntersect;
66     case GLSLANG_STAGE_ANYHIT_NV:
67         return EShLangAnyHit;
68     case GLSLANG_STAGE_CLOSESTHIT_NV:
69         return EShLangClosestHit;
70     case GLSLANG_STAGE_MISS_NV:
71         return EShLangMiss;
72     case GLSLANG_STAGE_CALLABLE_NV:
73         return EShLangCallable;
74     case GLSLANG_STAGE_TASK:
75         return EShLangTask;
76     case GLSLANG_STAGE_MESH:
77         return EShLangMesh;
78     default:
79         break;
80     }
81     return EShLangCount;
82 }
83
84 GLSLANG_EXPORT void glslang_program_SPIRV_generate(glslang_program_t* program, glslang_stage_t stage)
85 {
86     glslang_spv_options_t spv_options;
87     spv_options.generate_debug_info = false;
88     spv_options.strip_debug_info = false;
89     spv_options.disable_optimizer = true;
90     spv_options.optimize_size = false;
91     spv_options.disassemble = false;
92     spv_options.validate = true;
93
94     glslang_program_SPIRV_generate_with_options(program, stage, &spv_options);
95 }
96
97 GLSLANG_EXPORT void glslang_program_SPIRV_generate_with_options(glslang_program_t* program, glslang_stage_t stage, glslang_spv_options_t* spv_options) {
98     spv::SpvBuildLogger logger;
99
100     const glslang::TIntermediate* intermediate = program->program->getIntermediate(c_shader_stage(stage));
101
102     glslang::GlslangToSpv(*intermediate, program->spirv, &logger, reinterpret_cast<glslang::SpvOptions*>(spv_options));
103
104     program->loggerMessages = logger.getAllMessages();
105 }
106
107 GLSLANG_EXPORT size_t glslang_program_SPIRV_get_size(glslang_program_t* program) { return program->spirv.size(); }
108
109 GLSLANG_EXPORT void glslang_program_SPIRV_get(glslang_program_t* program, unsigned int* out)
110 {
111     memcpy(out, program->spirv.data(), program->spirv.size() * sizeof(unsigned int));
112 }
113
114 GLSLANG_EXPORT unsigned int* glslang_program_SPIRV_get_ptr(glslang_program_t* program)
115 {
116     return program->spirv.data();
117 }
118
119 GLSLANG_EXPORT const char* glslang_program_SPIRV_get_messages(glslang_program_t* program)
120 {
121     return program->loggerMessages.empty() ? nullptr : program->loggerMessages.c_str();
122 }