1/**
2    This code is based on the glslang_c_interface implementation by Viktor Latypov
3**/
4
5/**
6BSD 2-Clause License
7
8Copyright (c) 2019, Viktor Latypov
9All rights reserved.
10
11Redistribution and use in source and binary forms, with or without
12modification, are permitted provided that the following conditions are met:
13
141. Redistributions of source code must retain the above copyright notice, this
15   list of conditions and the following disclaimer.
16
172. 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
21THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF 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
39static_assert(sizeof(glslang_spv_options_t) == sizeof(glslang::SpvOptions), "");
40
41typedef struct glslang_program_s {
42    glslang::TProgram* program;
43    std::vector<unsigned int> spirv;
44    std::string loggerMessages;
45} glslang_program_t;
46
47static 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:
63        return EShLangRayGen;
64    case GLSLANG_STAGE_INTERSECT:
65        return EShLangIntersect;
66    case GLSLANG_STAGE_ANYHIT:
67        return EShLangAnyHit;
68    case GLSLANG_STAGE_CLOSESTHIT:
69        return EShLangClosestHit;
70    case GLSLANG_STAGE_MISS:
71        return EShLangMiss;
72    case GLSLANG_STAGE_CALLABLE:
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
84GLSLANG_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.disable_optimizer = true;
88    spv_options.validate = true;
89
90    glslang_program_SPIRV_generate_with_options(program, stage, &spv_options);
91}
92
93GLSLANG_EXPORT void glslang_program_SPIRV_generate_with_options(glslang_program_t* program, glslang_stage_t stage, glslang_spv_options_t* spv_options) {
94    spv::SpvBuildLogger logger;
95
96    const glslang::TIntermediate* intermediate = program->program->getIntermediate(c_shader_stage(stage));
97
98    glslang::GlslangToSpv(*intermediate, program->spirv, &logger, reinterpret_cast<glslang::SpvOptions*>(spv_options));
99
100    program->loggerMessages = logger.getAllMessages();
101}
102
103GLSLANG_EXPORT size_t glslang_program_SPIRV_get_size(glslang_program_t* program) { return program->spirv.size(); }
104
105GLSLANG_EXPORT void glslang_program_SPIRV_get(glslang_program_t* program, unsigned int* out)
106{
107    memcpy(out, program->spirv.data(), program->spirv.size() * sizeof(unsigned int));
108}
109
110GLSLANG_EXPORT unsigned int* glslang_program_SPIRV_get_ptr(glslang_program_t* program)
111{
112    return program->spirv.data();
113}
114
115GLSLANG_EXPORT const char* glslang_program_SPIRV_get_messages(glslang_program_t* program)
116{
117    return program->loggerMessages.empty() ? nullptr : program->loggerMessages.c_str();
118}
119