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
|
// glslang.cpp
#include "glslang.h"
#include "StandAlone/ResourceLimits.h"
#include "StandAlone/Worklist.h"
#include "glslang/Include/ShHandle.h"
#include "glslang/Include/revision.h"
#include "glslang/Public/ShaderLang.h"
#include "SPIRV/GlslangToSpv.h"
#include "SPIRV/GLSL.std.450.h"
#include "SPIRV/doc.h"
#include "SPIRV/disassemble.h"
#include "../../Slang.h"
#if 0
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <array>
#include <memory>
#include <thread>
#endif
#ifdef _WIN32
#include <Windows.h>
#endif
#include <sstream>
// This is a wrapper to allow us to run the `glslang` compiler
// in a controlled fashion.
#define UNLIMITED 9999
static TBuiltInResource gResources =
{
UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED,
UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED,
UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED,
UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED,
UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED,
UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED,
UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED,
UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED, UNLIMITED,
UNLIMITED, UNLIMITED, UNLIMITED,
{ true, true, true, true, true, true, true, true, true, }
};
static void dump(
std::string const& text,
glslang_OutputFunc outputFunc,
void* outputUserData,
FILE* fallbackStream)
{
if( outputFunc )
{
outputFunc(text.c_str(), outputUserData);
}
else
{
fprintf(fallbackStream, "%s", text.c_str());
// also output it for debug purposes
OutputDebugStringA(text.c_str());
}
}
static void dumpDiagnostics(
glslang_CompileRequest* request,
std::string const& log)
{
dump(log, request->diagnosticFunc, request->diagnosticUserData, stderr);
}
extern "C"
_declspec(dllexport)
int glslang_compile(glslang_CompileRequest* request)
{
glslang::InitializeProcess();
EShLanguage glslangStage;
switch( request->slangStage )
{
#define CASE(SP, GL) case SLANG_STAGE_##SP: glslangStage = EShLang##GL; break
CASE(VERTEX, Vertex);
CASE(FRAGMENT, Fragment);
CASE(GEOMETRY, Geometry);
CASE(HULL, TessControl);
CASE(DOMAIN, TessEvaluation);
CASE(COMPUTE, Compute);
#undef CASE
default:
return 1;
}
// TODO: compute glslang stage to use
glslang::TShader* shader = new glslang::TShader(glslangStage);
auto shaderPtr = std::unique_ptr<glslang::TShader>(shader);
glslang::TProgram* program = new glslang::TProgram();
auto programPtr = std::unique_ptr<glslang::TProgram>(program);
int sourceTextLength = (int) strlen(request->sourceText);
shader->setPreamble("#extension GL_GOOGLE_cpp_style_line_directive : require\n");
shader->setStringsWithLengthsAndNames(
&request->sourceText,
&sourceTextLength,
&request->sourcePath,
1);
// Note: this seems required to get past a bug where
// glslang complains about a declaration of `out gl_PerVertex`
// that it (seemingly) *should* allow according to the GLSL-for-Vulkan
// extension.
shader->setAutoMapLocations(true);
// Let's auto-map the bindings too, just because we can
shader->setAutoMapBindings(true);
EShMessages messages = EShMessages(EShMsgSpvRules | EShMsgVulkanRules);
if( !shader->parse(&gResources, 110, false, messages) )
{
dumpDiagnostics(request, shader->getInfoLog());
return 1;
}
program->addShader(shader);
if( !program->link(messages) )
{
dumpDiagnostics(request, program->getInfoLog());
return 1;
}
if( !program->mapIO() )
{
dumpDiagnostics(request, program->getInfoLog());
return 1;
}
for(int stage = 0; stage < EShLangCount; ++stage)
{
auto stageIntermediate = program->getIntermediate((EShLanguage)stage);
if(!stageIntermediate)
continue;
std::vector<unsigned int> spirv;
std::string warningsErrors;
spv::SpvBuildLogger logger;
glslang::GlslangToSpv(*stageIntermediate, spirv, &logger);
dumpDiagnostics(request, logger.getAllMessages());
std::stringstream spirvAsmStream;
spv::Disassemble(spirvAsmStream, spirv);
dump(spirvAsmStream.str(), request->outputFunc, request->outputUserData, stdout);
}
glslang::FinalizeProcess();
return 0;
}
|