-
Notifications
You must be signed in to change notification settings - Fork 2
/
glslc.cpp
239 lines (215 loc) · 8.12 KB
/
glslc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <vector>
#include <string>
#include <string.h>
#include <unordered_map>
#include <glslang/Public/ShaderLang.h>
#include <SPIRV/GlslangToSpv.h>
const TBuiltInResource DefaultTBuiltInResource =
{
/* .MaxLights = */ 32,
/* .MaxClipPlanes = */ 6,
/* .MaxTextureUnits = */ 32,
/* .MaxTextureCoords = */ 32,
/* .MaxVertexAttribs = */ 64,
/* .MaxVertexUniformComponents = */ 4096,
/* .MaxVaryingFloats = */ 64,
/* .MaxVertexTextureImageUnits = */ 32,
/* .MaxCombinedTextureImageUnits = */ 80,
/* .MaxTextureImageUnits = */ 32,
/* .MaxFragmentUniformComponents = */ 4096,
/* .MaxDrawBuffers = */ 32,
/* .MaxVertexUniformVectors = */ 128,
/* .MaxVaryingVectors = */ 8,
/* .MaxFragmentUniformVectors = */ 16,
/* .MaxVertexOutputVectors = */ 16,
/* .MaxFragmentInputVectors = */ 15,
/* .MinProgramTexelOffset = */ -8,
/* .MaxProgramTexelOffset = */ 7,
/* .MaxClipDistances = */ 8,
/* .MaxComputeWorkGroupCountX = */ 65535,
/* .MaxComputeWorkGroupCountY = */ 65535,
/* .MaxComputeWorkGroupCountZ = */ 65535,
/* .MaxComputeWorkGroupSizeX = */ 1024,
/* .MaxComputeWorkGroupSizeY = */ 1024,
/* .MaxComputeWorkGroupSizeZ = */ 64,
/* .MaxComputeUniformComponents = */ 1024,
/* .MaxComputeTextureImageUnits = */ 16,
/* .MaxComputeImageUniforms = */ 8,
/* .MaxComputeAtomicCounters = */ 8,
/* .MaxComputeAtomicCounterBuffers = */ 1,
/* .MaxVaryingComponents = */ 60,
/* .MaxVertexOutputComponents = */ 64,
/* .MaxGeometryInputComponents = */ 64,
/* .MaxGeometryOutputComponents = */ 128,
/* .MaxFragmentInputComponents = */ 128,
/* .MaxImageUnits = */ 8,
/* .MaxCombinedImageUnitsAndFragmentOutputs = */ 8,
/* .MaxCombinedShaderOutputResources = */ 8,
/* .MaxImageSamples = */ 0,
/* .MaxVertexImageUniforms = */ 0,
/* .MaxTessControlImageUniforms = */ 0,
/* .MaxTessEvaluationImageUniforms = */ 0,
/* .MaxGeometryImageUniforms = */ 0,
/* .MaxFragmentImageUniforms = */ 8,
/* .MaxCombinedImageUniforms = */ 8,
/* .MaxGeometryTextureImageUnits = */ 16,
/* .MaxGeometryOutputVertices = */ 256,
/* .MaxGeometryTotalOutputComponents = */ 1024,
/* .MaxGeometryUniformComponents = */ 1024,
/* .MaxGeometryVaryingComponents = */ 64,
/* .MaxTessControlInputComponents = */ 128,
/* .MaxTessControlOutputComponents = */ 128,
/* .MaxTessControlTextureImageUnits = */ 16,
/* .MaxTessControlUniformComponents = */ 1024,
/* .MaxTessControlTotalOutputComponents = */ 4096,
/* .MaxTessEvaluationInputComponents = */ 128,
/* .MaxTessEvaluationOutputComponents = */ 128,
/* .MaxTessEvaluationTextureImageUnits = */ 16,
/* .MaxTessEvaluationUniformComponents = */ 1024,
/* .MaxTessPatchComponents = */ 120,
/* .MaxPatchVertices = */ 32,
/* .MaxTessGenLevel = */ 64,
/* .MaxViewports = */ 16,
/* .MaxVertexAtomicCounters = */ 0,
/* .MaxTessControlAtomicCounters = */ 0,
/* .MaxTessEvaluationAtomicCounters = */ 0,
/* .MaxGeometryAtomicCounters = */ 0,
/* .MaxFragmentAtomicCounters = */ 8,
/* .MaxCombinedAtomicCounters = */ 8,
/* .MaxAtomicCounterBindings = */ 1,
/* .MaxVertexAtomicCounterBuffers = */ 0,
/* .MaxTessControlAtomicCounterBuffers = */ 0,
/* .MaxTessEvaluationAtomicCounterBuffers = */ 0,
/* .MaxGeometryAtomicCounterBuffers = */ 0,
/* .MaxFragmentAtomicCounterBuffers = */ 1,
/* .MaxCombinedAtomicCounterBuffers = */ 1,
/* .MaxAtomicCounterBufferSize = */ 16384,
/* .MaxTransformFeedbackBuffers = */ 4,
/* .MaxTransformFeedbackInterleavedComponents = */ 64,
/* .MaxCullDistances = */ 8,
/* .MaxCombinedClipAndCullDistances = */ 8,
/* .MaxSamples = */ 4,
/* .maxMeshOutputVerticesNV = */0,
/* .maxMeshOutputPrimitivesNV = */0,
/* .maxMeshWorkGroupSizeX_NV = */0,
/* .maxMeshWorkGroupSizeY_NV = */0,
/* .maxMeshWorkGroupSizeZ_NV = */0,
/* .maxTaskWorkGroupSizeX_NV = */0,
/* .maxTaskWorkGroupSizeY_NV = */0,
/* .maxTaskWorkGroupSizeZ_NV = */0,
/* .maxMeshViewCountNV = */0,
/* .maxDualSourceDrawBuffersEXT = */ 0,
/* .limits = */
{
/* .nonInductiveForLoops = */ 1,
/* .whileLoops = */ 1,
/* .doWhileLoops = */ 1,
/* .generalUniformIndexing = */ 1,
/* .generalAttributeMatrixVectorIndexing = */ 1,
/* .generalVaryingIndexing = */ 1,
/* .generalSamplerIndexing = */ 1,
/* .generalVariableIndexing = */ 1,
/* .generalConstantMatrixVectorIndexing = */ 1,
}
};
class BuiltInIncluder : public glslang::TShader::Includer
{
const std::unordered_map<std::string, std::string>* m_headers;
public:
BuiltInIncluder(const std::unordered_map<std::string, std::string>* headers = nullptr)
{
m_headers = headers;
}
virtual IncludeResult* includeSystem(const char* headerName, const char* includerName, size_t inclusionDepth)
{
if (m_headers != nullptr)
{
auto iter = m_headers->find(headerName);
if (iter != m_headers->end())
{
IncludeResult* result = new IncludeResult(headerName, iter->second.c_str(), iter->second.length(), nullptr);
return result;
}
}
return nullptr;
}
virtual void releaseInclude(IncludeResult* result) override
{
if (result!=nullptr) delete result;
}
};
bool GLSL2SPV(const char* InputCString, const std::unordered_map<std::string, std::string>* headers, std::vector<unsigned int>& SpirV, EShLanguage ShaderType)
{
glslang::InitializeProcess();
glslang::TShader Shader(ShaderType);
Shader.setStrings(&InputCString, 1);
const int ClientInputSemanticsVersion = 100;
#ifndef _VkInlineEX
glslang::EShTargetClientVersion VulkanClientVersion = glslang::EShTargetVulkan_1_1;
glslang::EShTargetLanguageVersion TargetVersion = glslang::EShTargetSpv_1_0;
#else
glslang::EShTargetClientVersion VulkanClientVersion = glslang::EShTargetVulkan_1_2;
glslang::EShTargetLanguageVersion TargetVersion = glslang::EShTargetSpv_1_4;
#endif
Shader.setEnvInput(glslang::EShSourceGlsl, ShaderType, glslang::EShClientVulkan, ClientInputSemanticsVersion);
Shader.setEnvClient(glslang::EShClientVulkan, VulkanClientVersion);
Shader.setEnvTarget(glslang::EShTargetSpv, TargetVersion);
TBuiltInResource Resources;
Resources = DefaultTBuiltInResource;
EShMessages messages = (EShMessages)(EShMsgSpvRules | EShMsgVulkanRules);
const int DefaultVersion = 110;
BuiltInIncluder Includer(headers);
if (!Shader.parse(&Resources, DefaultVersion, false, messages, Includer))
{
puts("GLSL Parsing Failed for: ");
puts(Shader.getInfoLog());
puts(Shader.getInfoDebugLog());
return false;
}
glslang::TProgram Program;
Program.addShader(&Shader);
if (!Program.link(messages))
{
puts("GLSL Linking Failed for:");
puts(Shader.getInfoLog());
puts(Shader.getInfoDebugLog());
return false;
}
spv::SpvBuildLogger logger;
glslang::SpvOptions spvOptions;
glslang::GlslangToSpv(*Program.getIntermediate(ShaderType), SpirV, &logger, &spvOptions);
if (logger.getAllMessages().length() > 0)
{
puts(logger.getAllMessages().c_str());
return false;
}
return true;
}
bool GLSL2SPV_Compute(const char* InputCString, const std::unordered_map<std::string, std::string>* headers, std::vector<unsigned int>& SpirV)
{
return GLSL2SPV(InputCString, headers, SpirV, EShLangCompute);
}
bool GLSL2SPV_Vertex(const char* InputCString, const std::unordered_map<std::string, std::string>* headers, std::vector<unsigned int>& SpirV)
{
return GLSL2SPV(InputCString, headers, SpirV, EShLangVertex);
}
bool GLSL2SPV_Fragment(const char* InputCString, const std::unordered_map<std::string, std::string>* headers, std::vector<unsigned int>& SpirV)
{
return GLSL2SPV(InputCString, headers, SpirV, EShLangFragment);
}
bool GLSL2SPV_Raygen(const char* InputCString, const std::unordered_map<std::string, std::string>* headers, std::vector<unsigned int>& SpirV)
{
return GLSL2SPV(InputCString, headers, SpirV, EShLangRayGen);
}
bool GLSL2SPV_Miss(const char* InputCString, const std::unordered_map<std::string, std::string>* headers, std::vector<unsigned int>& SpirV)
{
return GLSL2SPV(InputCString, headers, SpirV, EShLangMiss);
}
bool GLSL2SPV_ClosestHit(const char* InputCString, const std::unordered_map<std::string, std::string>* headers, std::vector<unsigned int>& SpirV)
{
return GLSL2SPV(InputCString, headers, SpirV, EShLangClosestHit);
}
bool GLSL2SPV_Intersect(const char* InputCString, const std::unordered_map<std::string, std::string>* headers, std::vector<unsigned int>& SpirV)
{
return GLSL2SPV(InputCString, headers, SpirV, EShLangIntersect);
}