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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
// 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 const char rtEntryPointName[] = "raygenMain";
gfx::StageType translateStage(SlangStage slangStage)
{
switch(slangStage)
{
default:
SLANG_ASSERT(!"unhandled case");
return gfx::StageType::Unknown;
#define CASE(FROM, TO) \
case SLANG_STAGE_##FROM: return gfx::StageType::TO
CASE(VERTEX, Vertex);
CASE(HULL, Hull);
CASE(DOMAIN, Domain);
CASE(GEOMETRY, Geometry);
CASE(FRAGMENT, Fragment);
CASE(COMPUTE, Compute);
CASE(RAY_GENERATION, RayGeneration);
CASE(INTERSECTION, Intersection);
CASE(ANY_HIT, AnyHit);
CASE(CLOSEST_HIT, ClosestHit);
CASE(MISS, Miss);
CASE(CALLABLE, Callable);
#undef CASE
}
}
void ShaderCompilerUtil::Output::set(
PipelineType pipelineType,
slang::IComponentType* inSlangProgram)
{
slangProgram = inSlangProgram;
desc.pipelineType = pipelineType;
desc.slangProgram = inSlangProgram;
}
void ShaderCompilerUtil::Output::reset()
{
{
desc.pipelineType = PipelineType::Unknown;
desc.slangProgram = nullptr;
}
if (m_requestForKernels && session)
{
spDestroyCompileRequest(m_requestForKernels);
}
if (m_extraRequestForReflection && session)
{
spDestroyCompileRequest(m_extraRequestForReflection);
}
session = nullptr;
m_requestForKernels = nullptr;
m_extraRequestForReflection = nullptr;
}
/* static */ SlangResult ShaderCompilerUtil::_compileProgramImpl(SlangSession* session, const Options& options, const Input& input, const ShaderCompileRequest& request, Output& out)
{
out.reset();
SlangCompileRequest* slangRequest = spCreateCompileRequest(session);
out.m_requestForKernels = 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.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;
case SLANG_SOURCE_LANGUAGE_CUDA:
spAddPreprocessorDefine(slangRequest, "__CUDA__", "1");
break;
default:
assert(!"unexpected");
break;
}
if (input.passThrough != SLANG_PASS_THROUGH_NONE)
{
spSetPassThrough(slangRequest, input.passThrough);
}
else
{
spSetCompileFlags(slangRequest, SLANG_COMPILE_FLAG_NO_CODEGEN);
}
// 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));
const auto sourceLanguage = input.sourceLanguage;
int translationUnitIndex = 0;
{
translationUnitIndex = spAddTranslationUnit(slangRequest, sourceLanguage, nullptr);
spAddTranslationUnitSourceString(slangRequest, translationUnitIndex, request.source.path, request.source.dataBegin);
}
const int globalSpecializationArgCount = int(request.globalSpecializationArgs.getCount());
for(int ii = 0; ii < globalSpecializationArgCount; ++ii )
{
spSetTypeNameForGlobalExistentialTypeParam(slangRequest, ii, request.globalSpecializationArgs[ii].getBuffer());
}
const int entryPointSpecializationArgCount = int(request.entryPointSpecializationArgs.getCount());
auto setEntryPointSpecializationArgs = [&](int entryPoint)
{
for( int ii = 0; ii < entryPointSpecializationArgCount; ++ii )
{
spSetTypeNameForEntryPointExistentialTypeParam(slangRequest, entryPoint, ii, request.entryPointSpecializationArgs[ii].getBuffer());
}
};
Index explicitEntryPointCount = request.entryPoints.getCount();
for(Index ee = 0; ee < explicitEntryPointCount; ++ee)
{
if(options.dontAddDefaultEntryPoints)
{
// If default entry points are not to be added, then
// the `request.entryPoints` array should have been
// left empty.
//
SLANG_ASSERT(false);
}
auto& entryPointInfo = request.entryPoints[ee];
int entryPointIndex = spAddEntryPoint(
slangRequest,
translationUnitIndex,
entryPointInfo.name,
entryPointInfo.slangStage);
SLANG_ASSERT(entryPointIndex == ee);
setEntryPointSpecializationArgs(entryPointIndex);
}
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);
ComPtr<slang::IComponentType> linkedSlangProgram;
List<ShaderCompileRequest::EntryPoint> actualEntryPoints;
if(input.passThrough == SLANG_PASS_THROUGH_NONE)
{
// In the case where pass-through compilation is not being used,
// we can use the Slang reflection information to discover what
// the entry points were, and then use those to drive the
// loading of code.
//
auto reflection = slang::ProgramLayout::get(slangRequest);
SLANG_RETURN_ON_FAIL(spCompileRequest_getProgramWithEntryPoints(slangRequest, linkedSlangProgram.writeRef()));
// Get the amount of entry points in reflection
Index entryPointCount = Index(reflection->getEntryPointCount());
// We must have at least one entry point (whether explicit or implicit)
SLANG_ASSERT(entryPointCount);
for(Index ee = 0; ee < entryPointCount; ++ee)
{
auto entryPoint = reflection->getEntryPointByIndex(ee);
const char* entryPointName = entryPoint->getName();
SLANG_ASSERT(entryPointName);
auto slangStage = entryPoint->getStage();
ShaderCompileRequest::EntryPoint entryPointInfo;
entryPointInfo.name = entryPointName;
entryPointInfo.slangStage = slangStage;
actualEntryPoints.add(entryPointInfo);
}
}
else
{
actualEntryPoints = request.entryPoints;
}
out.set(input.pipelineType, linkedSlangProgram);
return SLANG_OK;
}
/* static */ SlangResult ShaderCompilerUtil::compileProgram(SlangSession* session, const Options& options, const Input& input, const ShaderCompileRequest& request, Output& out)
{
if( input.passThrough == SLANG_PASS_THROUGH_NONE )
{
return _compileProgramImpl(session, options, input, request, out);
}
else
{
bool canUseSlangForPrecompile = false;
switch (input.passThrough)
{
case SLANG_PASS_THROUGH_DXC:
case SLANG_PASS_THROUGH_FXC:
canUseSlangForPrecompile = true;
break;
default:
break;
}
// If we are doing a HLSL pass-through compilation, then we can't rely
// on the downstream compiler for the reflection information that
// will drive all of our parameter binding. As such, we will first
// compile with Slang to get reflection information, and then
// compile in another pass using the desired downstream compiler
// so that we can get the refleciton information we need.
//
Output slangOutput;
if (canUseSlangForPrecompile)
{
ShaderCompilerUtil::Input slangInput = input;
slangInput.sourceLanguage = SLANG_SOURCE_LANGUAGE_SLANG;
slangInput.passThrough = SLANG_PASS_THROUGH_NONE;
// TODO: we want to pass along a flag to skip codegen...
SLANG_RETURN_ON_FAIL(_compileProgramImpl(session, options, slangInput, request, slangOutput));
}
// Now we have what we need to be able to do the downstream compile better.
//
// TODO: We should be able to use the output from the Slang compilation
// to fill in the actual entry points to be used for this compilation,
// so that discovery of entry points via `[shader(...)]` attributes will work.
//
SLANG_RETURN_ON_FAIL(_compileProgramImpl(session, options, input, request, out));
out.m_extraRequestForReflection = slangOutput.getRequestForReflection();
out.desc.slangProgram = slangOutput.desc.slangProgram;
slangOutput.m_requestForKernels = nullptr;
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 Options& options, const ShaderCompilerUtil::Input& input, OutputAndLayout& output)
{
String sourcePath = options.sourcePath;
auto& compileArgs = options.compileArgs;
auto shaderType = options.shaderType;
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->getLanguagePrelude(input.sourceLanguage, 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:
case Options::ShaderProgramType::RayTracing:
layout.numRenderTargets = 0;
break;
}
// Deterministic random generator
RefPtr<RandomGenerator> rand = RandomGenerator::create(0x34234);
// Parse the layout
layout.parse(rand, sourceText.getBuffer());
// 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;
// Now we will add the "default" entry point names/stages that
// are appropriate to the pipeline type being targetted, *unless*
// the options specify that we should leave out the default
// entry points and instead rely on the Slang compiler's built-in
// mechanisms for discovering entry points (e.g., `[shader(...)]`
// attributes).
//
if( !options.dontAddDefaultEntryPoints )
{
if (shaderType == Options::ShaderProgramType::Graphics || shaderType == Options::ShaderProgramType::GraphicsCompute)
{
ShaderCompileRequest::EntryPoint vertexEntryPoint;
vertexEntryPoint.name = vertexEntryPointName;
vertexEntryPoint.slangStage = SLANG_STAGE_VERTEX;
compileRequest.entryPoints.add(vertexEntryPoint);
ShaderCompileRequest::EntryPoint fragmentEntryPoint;
fragmentEntryPoint.name = fragmentEntryPointName;
fragmentEntryPoint.slangStage = SLANG_STAGE_FRAGMENT;
compileRequest.entryPoints.add(fragmentEntryPoint);
}
else if( shaderType == Options::ShaderProgramType::RayTracing )
{
// Note: Current GPU ray tracing pipelines allow for an
// almost arbitrary mix of entry points for different stages
// to be used together (e.g., a single "program" might
// have multiple any-hit shaders, multiple miss shaders, etc.)
//
// Rather than try to define a fixed set of entry point
// names and stages that the testing will support, we will
// instead rely on `[shader(...)]` annotations to tell us
// what entry points are present in the input code.
}
else
{
ShaderCompileRequest::EntryPoint computeEntryPoint;
computeEntryPoint.name = computeEntryPointName;
computeEntryPoint.slangStage = SLANG_STAGE_COMPUTE;
compileRequest.entryPoints.add(computeEntryPoint);
}
}
compileRequest.globalSpecializationArgs = layout.globalSpecializationArgs;
compileRequest.entryPointSpecializationArgs = layout.entryPointSpecializationArgs;
return ShaderCompilerUtil::compileProgram(session, options, input, compileRequest, output.output);
}
} // renderer_test
|