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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
// slang-support.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "slang-support.h"
#include "options.h"
#include <assert.h>
#include <stdio.h>
#include "../../source/core/slang-string-util.h"
namespace renderer_test {
using namespace Slang;
// Entry point name to use for vertex/fragment shader
static const char vertexEntryPointName[] = "vertexMain";
static const char fragmentEntryPointName[] = "fragmentMain";
static const char computeEntryPointName[] = "computeMain";
/* static */ SlangResult ShaderCompilerUtil::compileProgram(SlangSession* session, const Input& input, const ShaderCompileRequest& request, Output& out)
{
out.reset();
SlangCompileRequest* slangRequest = spCreateCompileRequest(session);
out.request = slangRequest;
out.session = session;
// Parse all the extra args
if (request.compileArgs.getCount() > 0)
{
List<const char*> args;
for (const auto& arg : request.compileArgs)
{
args.add(arg.value.getBuffer());
}
SLANG_RETURN_ON_FAIL(spProcessCommandLineArguments(slangRequest, args.getBuffer(), int(args.getCount())));
}
spSetCodeGenTarget(slangRequest, input.target);
spSetTargetProfile(slangRequest, 0, spFindProfile(session, input.profile));
// Define a macro so that shader code in a test can detect what language we
// are nominally working with.
char const* langDefine = nullptr;
switch (input.sourceLanguage)
{
case SLANG_SOURCE_LANGUAGE_GLSL:
spAddPreprocessorDefine(slangRequest, "__GLSL__", "1");
break;
case SLANG_SOURCE_LANGUAGE_SLANG:
spAddPreprocessorDefine(slangRequest, "__SLANG__", "1");
// fall through
case SLANG_SOURCE_LANGUAGE_HLSL:
spAddPreprocessorDefine(slangRequest, "__HLSL__", "1");
break;
case SLANG_SOURCE_LANGUAGE_C:
spAddPreprocessorDefine(slangRequest, "__C__", "1");
break;
case SLANG_SOURCE_LANGUAGE_CPP:
spAddPreprocessorDefine(slangRequest, "__CPP__", "1");
break;
default:
assert(!"unexpected");
break;
}
if (input.passThrough != SLANG_PASS_THROUGH_NONE)
{
spSetPassThrough(slangRequest, input.passThrough);
}
// Process any additional command-line options specified for Slang using
// the `-xslang <arg>` option to `render-test`.
SLANG_RETURN_ON_FAIL(spProcessCommandLineArguments(slangRequest, input.args, input.argCount));
int computeTranslationUnit = 0;
int vertexTranslationUnit = 0;
int fragmentTranslationUnit = 0;
char const* vertexEntryPointName = request.vertexShader.name;
char const* fragmentEntryPointName = request.fragmentShader.name;
char const* computeEntryPointName = request.computeShader.name;
const auto sourceLanguage = input.sourceLanguage;
if (sourceLanguage == SLANG_SOURCE_LANGUAGE_GLSL)
{
// GLSL presents unique challenges because, frankly, it got the whole
// compilation model wrong. One aspect of working around this is that
// we will compile the same source file multiple times: once per
// entry point, and we will have different preprocessor definitions
// active in each case.
vertexTranslationUnit = spAddTranslationUnit(slangRequest, sourceLanguage, nullptr);
spAddTranslationUnitSourceString(slangRequest, vertexTranslationUnit, request.source.path, request.source.dataBegin);
spTranslationUnit_addPreprocessorDefine(slangRequest, vertexTranslationUnit, "__GLSL_VERTEX__", "1");
vertexEntryPointName = "main";
fragmentTranslationUnit = spAddTranslationUnit(slangRequest, sourceLanguage, nullptr);
spAddTranslationUnitSourceString(slangRequest, fragmentTranslationUnit, request.source.path, request.source.dataBegin);
spTranslationUnit_addPreprocessorDefine(slangRequest, fragmentTranslationUnit, "__GLSL_FRAGMENT__", "1");
fragmentEntryPointName = "main";
computeTranslationUnit = spAddTranslationUnit(slangRequest, sourceLanguage, nullptr);
spAddTranslationUnitSourceString(slangRequest, computeTranslationUnit, request.source.path, request.source.dataBegin);
spTranslationUnit_addPreprocessorDefine(slangRequest, computeTranslationUnit, "__GLSL_COMPUTE__", "1");
computeEntryPointName = "main";
}
else
{
int translationUnit = spAddTranslationUnit(slangRequest, sourceLanguage, nullptr);
spAddTranslationUnitSourceString(slangRequest, translationUnit, request.source.path, request.source.dataBegin);
vertexTranslationUnit = translationUnit;
fragmentTranslationUnit = translationUnit;
computeTranslationUnit = translationUnit;
}
Slang::List<const char*> rawGlobalTypeNames;
for (auto typeName : request.globalGenericTypeArguments)
rawGlobalTypeNames.add(typeName.getBuffer());
spSetGlobalGenericArgs(
slangRequest,
(int)rawGlobalTypeNames.getCount(),
rawGlobalTypeNames.getBuffer());
Slang::List<const char*> rawEntryPointTypeNames;
for (auto typeName : request.entryPointGenericTypeArguments)
rawEntryPointTypeNames.add(typeName.getBuffer());
const int globalExistentialTypeCount = int(request.globalExistentialTypeArguments.getCount());
for(int ii = 0; ii < globalExistentialTypeCount; ++ii )
{
spSetTypeNameForGlobalExistentialTypeParam(slangRequest, ii, request.globalExistentialTypeArguments[ii].getBuffer());
}
const int entryPointExistentialTypeCount = int(request.entryPointExistentialTypeArguments.getCount());
auto setEntryPointExistentialTypeArgs = [&](int entryPoint)
{
for( int ii = 0; ii < entryPointExistentialTypeCount; ++ii )
{
spSetTypeNameForEntryPointExistentialTypeParam(slangRequest, entryPoint, ii, request.entryPointExistentialTypeArguments[ii].getBuffer());
}
};
if (request.computeShader.name)
{
int computeEntryPoint = 0;
if(!gOptions.dontAddDefaultEntryPoints)
{
computeEntryPoint = spAddEntryPointEx(slangRequest, computeTranslationUnit,
computeEntryPointName,
SLANG_STAGE_COMPUTE,
(int)rawEntryPointTypeNames.getCount(),
rawEntryPointTypeNames.getBuffer());
setEntryPointExistentialTypeArgs(computeEntryPoint);
}
spSetLineDirectiveMode(slangRequest, SLANG_LINE_DIRECTIVE_MODE_NONE);
const SlangResult res = spCompile(slangRequest);
if (auto diagnostics = spGetDiagnosticOutput(slangRequest))
{
fprintf(stderr, "%s", diagnostics);
}
SLANG_RETURN_ON_FAIL(res);
{
size_t codeSize = 0;
char const* code = (char const*) spGetEntryPointCode(slangRequest, computeEntryPoint, &codeSize);
ShaderProgram::KernelDesc kernelDesc;
kernelDesc.stage = StageType::Compute;
kernelDesc.codeBegin = code;
kernelDesc.codeEnd = code + codeSize;
out.set(PipelineType::Compute, &kernelDesc, 1);
}
}
else
{
int vertexEntryPoint = 0;
int fragmentEntryPoint = 1;
if( !gOptions.dontAddDefaultEntryPoints )
{
vertexEntryPoint = spAddEntryPointEx(slangRequest, vertexTranslationUnit, vertexEntryPointName, SLANG_STAGE_VERTEX, (int)rawEntryPointTypeNames.getCount(), rawEntryPointTypeNames.getBuffer());
fragmentEntryPoint = spAddEntryPointEx(slangRequest, fragmentTranslationUnit, fragmentEntryPointName, SLANG_STAGE_FRAGMENT, (int)rawEntryPointTypeNames.getCount(), rawEntryPointTypeNames.getBuffer());
setEntryPointExistentialTypeArgs(vertexEntryPoint);
setEntryPointExistentialTypeArgs(fragmentEntryPoint);
}
const SlangResult res = spCompile(slangRequest);
if (auto diagnostics = spGetDiagnosticOutput(slangRequest))
{
// TODO(tfoley): re-enable when I get a logging solution in place
// OutputDebugStringA(diagnostics);
fprintf(stderr, "%s", diagnostics);
}
SLANG_RETURN_ON_FAIL(res);
{
size_t vertexCodeSize = 0;
char const* vertexCode = (char const*) spGetEntryPointCode(slangRequest, vertexEntryPoint, &vertexCodeSize);
size_t fragmentCodeSize = 0;
char const* fragmentCode = (char const*) spGetEntryPointCode(slangRequest, fragmentEntryPoint, &fragmentCodeSize);
static const int kDescCount = 2;
ShaderProgram::KernelDesc kernelDescs[kDescCount];
kernelDescs[0].stage = StageType::Vertex;
kernelDescs[0].codeBegin = vertexCode;
kernelDescs[0].codeEnd = vertexCode + vertexCodeSize;
kernelDescs[1].stage = StageType::Fragment;
kernelDescs[1].codeBegin = fragmentCode;
kernelDescs[1].codeEnd = fragmentCode + fragmentCodeSize;
out.set(PipelineType::Graphics, kernelDescs, kDescCount);
}
}
return SLANG_OK;
}
/* static */SlangResult ShaderCompilerUtil::readSource(const String& inSourcePath, List<char>& outSourceText)
{
// Read in the source code
FILE* sourceFile = fopen(inSourcePath.getBuffer(), "rb");
if (!sourceFile)
{
fprintf(stderr, "error: failed to open '%s' for reading\n", inSourcePath.getBuffer());
return SLANG_FAIL;
}
fseek(sourceFile, 0, SEEK_END);
size_t sourceSize = ftell(sourceFile);
fseek(sourceFile, 0, SEEK_SET);
outSourceText.setCount(sourceSize + 1);
fread(outSourceText.getBuffer(), sourceSize, 1, sourceFile);
fclose(sourceFile);
outSourceText[sourceSize] = 0;
return SLANG_OK;
}
/* static */SlangResult ShaderCompilerUtil::compileWithLayout(SlangSession* session, const String& sourcePath, const Slang::List<Slang::CommandLine::Arg>& compileArgs, Options::ShaderProgramType shaderType, const ShaderCompilerUtil::Input& input, OutputAndLayout& output)
{
List<char> sourceText;
SLANG_RETURN_ON_FAIL(readSource(sourcePath, sourceText));
if (input.sourceLanguage == SLANG_SOURCE_LANGUAGE_CPP || input.sourceLanguage == SLANG_SOURCE_LANGUAGE_C)
{
// Add an include of the prelude
ComPtr<ISlangBlob> prelude;
session->getDownstreamCompilerPrelude(SLANG_PASS_THROUGH_GENERIC_C_CPP, prelude.writeRef());
String preludeString = StringUtil::getString(prelude);
// Add the prelude
StringBuilder builder;
builder << preludeString << "\n";
builder << UnownedStringSlice(sourceText.getBuffer(), sourceText.getCount());
sourceText.setCount(builder.getLength());
memcpy(sourceText.getBuffer(), builder.getBuffer(), builder.getLength());
}
output.sourcePath = sourcePath;
auto& layout = output.layout;
// Default the amount of renderTargets based on shader type
switch (shaderType)
{
default:
layout.numRenderTargets = 1;
break;
case Options::ShaderProgramType::Compute:
layout.numRenderTargets = 0;
break;
}
// Deterministic random generator
RefPtr<RandomGenerator> rand = RandomGenerator::create(0x34234);
// Parse the layout
layout.parse(rand, sourceText.getBuffer());
layout.updateForTarget(input.target);
// Setup SourceInfo
ShaderCompileRequest::SourceInfo sourceInfo;
sourceInfo.path = sourcePath.getBuffer();
sourceInfo.dataBegin = sourceText.getBuffer();
// Subtract 1 because it's zero terminated
sourceInfo.dataEnd = sourceText.getBuffer() + sourceText.getCount() - 1;
ShaderCompileRequest compileRequest;
compileRequest.compileArgs = compileArgs;
compileRequest.source = sourceInfo;
if (shaderType == Options::ShaderProgramType::Graphics || shaderType == Options::ShaderProgramType::GraphicsCompute)
{
compileRequest.vertexShader.source = sourceInfo;
compileRequest.vertexShader.name = vertexEntryPointName;
compileRequest.fragmentShader.source = sourceInfo;
compileRequest.fragmentShader.name = fragmentEntryPointName;
}
else
{
compileRequest.computeShader.source = sourceInfo;
compileRequest.computeShader.name = computeEntryPointName;
}
compileRequest.globalGenericTypeArguments = layout.globalGenericTypeArguments;
compileRequest.entryPointGenericTypeArguments = layout.entryPointGenericTypeArguments;
compileRequest.globalExistentialTypeArguments = layout.globalExistentialTypeArguments;
compileRequest.entryPointExistentialTypeArguments = layout.entryPointExistentialTypeArguments;
return ShaderCompilerUtil::compileProgram(session, input, compileRequest, output.output);
}
} // renderer_test
|