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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
|
// render-test-main.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "options.h"
#include "render.h"
#include "slang-support.h"
#include "surface.h"
#include "png-serialize-util.h"
#include "shader-renderer-util.h"
#include "../source/core/slang-io.h"
#include "../source/core/slang-string-util.h"
#include "core/slang-token-reader.h"
#include "shader-input-layout.h"
#include <stdio.h>
#include <stdlib.h>
#include "window.h"
#include "../../source/core/slang-test-tool-util.h"
#include "cpu-compute-util.h"
#if RENDER_TEST_CUDA
# include "cuda/cuda-compute-util.h"
#endif
namespace renderer_test {
using Slang::Result;
int gWindowWidth = 1024;
int gWindowHeight = 768;
//
// For the purposes of a small example, we will define the vertex data for a
// single triangle directly in the source file. It should be easy to extend
// this example to load data from an external source, if desired.
//
struct Vertex
{
float position[3];
float color[3];
float uv[2];
};
static const Vertex kVertexData[] =
{
{ { 0, 0, 0.5 }, {1, 0, 0} , {0, 0} },
{ { 0, 1, 0.5 }, {0, 0, 1} , {1, 0} },
{ { 1, 0, 0.5 }, {0, 1, 0} , {1, 1} },
};
static const int kVertexCount = SLANG_COUNT_OF(kVertexData);
using namespace Slang;
static void _outputProfileTime(uint64_t startTicks, uint64_t endTicks)
{
WriterHelper out = StdWriters::getOut();
double time = double(endTicks - startTicks) / ProcessUtil::getClockFrequency();
out.print("profile-time=%g\n", time);
}
class RenderTestApp : public WindowListener
{
public:
// WindowListener
virtual Result update(Window* window) SLANG_OVERRIDE;
// At initialization time, we are going to load and compile our Slang shader
// code, and then create the API objects we need for rendering.
Result initialize(SlangSession* session, Renderer* renderer, const Options& options, const ShaderCompilerUtil::Input& input);
void runCompute();
void renderFrame();
void finalize();
BindingStateImpl* getBindingState() const { return m_bindingState; }
Result writeBindingOutput(BindRoot* bindRoot, const char* fileName);
Result writeScreen(const char* filename);
protected:
/// Called in initialize
Result _initializeShaders(SlangSession* session, Renderer* renderer, Options::ShaderProgramType shaderType, const ShaderCompilerUtil::Input& input);
uint64_t m_startTicks;
// variables for state to be used for rendering...
uintptr_t m_constantBufferSize;
RefPtr<Renderer> m_renderer;
RefPtr<BufferResource> m_constantBuffer;
RefPtr<InputLayout> m_inputLayout;
RefPtr<BufferResource> m_vertexBuffer;
RefPtr<ShaderProgram> m_shaderProgram;
RefPtr<PipelineState> m_pipelineState;
RefPtr<BindingStateImpl> m_bindingState;
ShaderCompilerUtil::OutputAndLayout m_compilationOutput;
ShaderInputLayout m_shaderInputLayout; ///< The binding layout
int m_numAddedConstantBuffers; ///< Constant buffers can be added to the binding directly. Will be added at the end.
Options m_options;
};
SlangResult RenderTestApp::initialize(SlangSession* session, Renderer* renderer, const Options& options, const ShaderCompilerUtil::Input& input)
{
m_options = options;
SLANG_RETURN_ON_FAIL(_initializeShaders(session, renderer, options.shaderType, input));
m_numAddedConstantBuffers = 0;
m_renderer = renderer;
// TODO(tfoley): use each API's reflection interface to query the constant-buffer size needed
m_constantBufferSize = 16 * sizeof(float);
BufferResource::Desc constantBufferDesc;
constantBufferDesc.init(m_constantBufferSize);
constantBufferDesc.cpuAccessFlags = Resource::AccessFlag::Write;
m_constantBuffer = renderer->createBufferResource(Resource::Usage::ConstantBuffer, constantBufferDesc);
if (!m_constantBuffer)
return SLANG_FAIL;
//! Hack -> if doing a graphics test, add an extra binding for our dynamic constant buffer
//
// TODO: Should probably be more sophisticated than this - with 'dynamic' constant buffer/s binding always being specified
// in the test file
RefPtr<BufferResource> addedConstantBuffer;
switch(m_options.shaderType)
{
default:
break;
case Options::ShaderProgramType::Graphics:
case Options::ShaderProgramType::GraphicsCompute:
addedConstantBuffer = m_constantBuffer;
m_numAddedConstantBuffers++;
break;
}
BindingStateImpl* bindingState = nullptr;
SLANG_RETURN_ON_FAIL(ShaderRendererUtil::createBindingState(m_shaderInputLayout, m_renderer, addedConstantBuffer, &bindingState));
m_bindingState = bindingState;
// Do other initialization that doesn't depend on the source language.
// Input Assembler (IA)
const InputElementDesc inputElements[] = {
{ "A", 0, Format::RGB_Float32, offsetof(Vertex, position) },
{ "A", 1, Format::RGB_Float32, offsetof(Vertex, color) },
{ "A", 2, Format::RG_Float32, offsetof(Vertex, uv) },
};
m_inputLayout = renderer->createInputLayout(inputElements, SLANG_COUNT_OF(inputElements));
if(!m_inputLayout)
return SLANG_FAIL;
BufferResource::Desc vertexBufferDesc;
vertexBufferDesc.init(kVertexCount * sizeof(Vertex));
m_vertexBuffer = renderer->createBufferResource(Resource::Usage::VertexBuffer, vertexBufferDesc, kVertexData);
if(!m_vertexBuffer)
return SLANG_FAIL;
{
switch(m_options.shaderType)
{
default:
assert(!"unexpected test shader type");
return SLANG_FAIL;
case Options::ShaderProgramType::Compute:
{
ComputePipelineStateDesc desc;
desc.pipelineLayout = m_bindingState->pipelineLayout;
desc.program = m_shaderProgram;
m_pipelineState = renderer->createComputePipelineState(desc);
}
break;
case Options::ShaderProgramType::Graphics:
case Options::ShaderProgramType::GraphicsCompute:
{
GraphicsPipelineStateDesc desc;
desc.pipelineLayout = m_bindingState->pipelineLayout;
desc.program = m_shaderProgram;
desc.inputLayout = m_inputLayout;
desc.renderTargetCount = m_bindingState->m_numRenderTargets;
m_pipelineState = renderer->createGraphicsPipelineState(desc);
}
break;
}
}
// If success must have a pipeline state
return m_pipelineState ? SLANG_OK : SLANG_FAIL;
}
Result RenderTestApp::_initializeShaders(SlangSession* session, Renderer* renderer, Options::ShaderProgramType shaderType, const ShaderCompilerUtil::Input& input)
{
SLANG_RETURN_ON_FAIL(ShaderCompilerUtil::compileWithLayout(session, m_options, input, m_compilationOutput));
m_shaderInputLayout = m_compilationOutput.layout;
m_shaderProgram = renderer->createProgram(m_compilationOutput.output.desc);
return m_shaderProgram ? SLANG_OK : SLANG_FAIL;
}
void RenderTestApp::renderFrame()
{
auto mappedData = m_renderer->map(m_constantBuffer, MapFlavor::WriteDiscard);
if(mappedData)
{
const ProjectionStyle projectionStyle = RendererUtil::getProjectionStyle(m_renderer->getRendererType());
RendererUtil::getIdentityProjection(projectionStyle, (float*)mappedData);
m_renderer->unmap(m_constantBuffer);
}
auto pipelineType = PipelineType::Graphics;
m_renderer->setPipelineState(pipelineType, m_pipelineState);
m_renderer->setPrimitiveTopology(PrimitiveTopology::TriangleList);
m_renderer->setVertexBuffer(0, m_vertexBuffer, sizeof(Vertex));
m_bindingState->apply(m_renderer, pipelineType);
m_renderer->draw(3);
}
void RenderTestApp::runCompute()
{
auto pipelineType = PipelineType::Compute;
m_renderer->setPipelineState(pipelineType, m_pipelineState);
m_bindingState->apply(m_renderer, pipelineType);
m_startTicks = ProcessUtil::getClockTick();
m_renderer->dispatchCompute(m_options.computeDispatchSize[0], m_options.computeDispatchSize[1], m_options.computeDispatchSize[2]);
}
void RenderTestApp::finalize()
{
}
Result RenderTestApp::writeBindingOutput(BindRoot* bindRoot, const char* fileName)
{
// Submit the work
m_renderer->submitGpuWork();
// Wait until everything is complete
m_renderer->waitForGpu();
FILE * f = fopen(fileName, "wb");
if (!f)
{
return SLANG_FAIL;
}
FileWriter writer(f, WriterFlags(0));
for(auto binding : m_bindingState->outputBindings)
{
auto i = binding.entryIndex;
const auto& layoutBinding = m_shaderInputLayout.entries[i];
assert(layoutBinding.isOutput);
if (binding.resource && binding.resource->isBuffer())
{
BufferResource* bufferResource = static_cast<BufferResource*>(binding.resource.Ptr());
const size_t bufferSize = bufferResource->getDesc().sizeInBytes;
unsigned int* ptr = (unsigned int*)m_renderer->map(bufferResource, MapFlavor::HostRead);
if (!ptr)
{
return SLANG_FAIL;
}
const SlangResult res = ShaderInputLayout::writeBinding(bindRoot, m_shaderInputLayout.entries[i], ptr, bufferSize, &writer);
m_renderer->unmap(bufferResource);
SLANG_RETURN_ON_FAIL(res);
}
else
{
printf("invalid output type at %d.\n", int(i));
}
}
return SLANG_OK;
}
Result RenderTestApp::writeScreen(const char* filename)
{
Surface surface;
SLANG_RETURN_ON_FAIL(m_renderer->captureScreenSurface(surface));
return PngSerializeUtil::write(filename, surface);
}
Result RenderTestApp::update(Window* window)
{
// Whenever we don't have Windows events to process, we render a frame.
if (m_options.shaderType == Options::ShaderProgramType::Compute)
{
runCompute();
}
else
{
static const float kClearColor[] = { 0.25, 0.25, 0.25, 1.0 };
m_renderer->setClearColor(kClearColor);
m_renderer->clearFrame();
renderFrame();
}
// If we are in a mode where output is requested, we need to snapshot the back buffer here
if (m_options.outputPath || m_options.performanceProfile)
{
// Submit the work
m_renderer->submitGpuWork();
// Wait until everything is complete
m_renderer->waitForGpu();
if (m_options.performanceProfile)
{
// It might not be enough on some APIs to 'waitForGpu' to mean the computation has completed. Let's lock an output
// buffer to be sure
if (m_bindingState->outputBindings.getCount() > 0)
{
const auto& binding = m_bindingState->outputBindings[0];
auto i = binding.entryIndex;
const auto& layoutBinding = m_shaderInputLayout.entries[i];
assert(layoutBinding.isOutput);
if (binding.resource && binding.resource->isBuffer())
{
BufferResource* bufferResource = static_cast<BufferResource*>(binding.resource.Ptr());
const size_t bufferSize = bufferResource->getDesc().sizeInBytes;
unsigned int* ptr = (unsigned int*)m_renderer->map(bufferResource, MapFlavor::HostRead);
if (!ptr)
{
return SLANG_FAIL;
}
m_renderer->unmap(bufferResource);
}
}
// Note we don't do the same with screen rendering -> as that will do a lot of work, which may swamp any computation
// so can only really profile compute shaders at the moment
const uint64_t endTicks = ProcessUtil::getClockTick();
_outputProfileTime(m_startTicks, endTicks);
}
if (m_options.outputPath)
{
if (m_options.shaderType == Options::ShaderProgramType::Compute || m_options.shaderType == Options::ShaderProgramType::GraphicsCompute)
{
auto request = m_compilationOutput.output.request;
auto slangReflection = (slang::ShaderReflection*) spGetReflection(request);
BindSet bindSet;
GPULikeBindRoot bindRoot;
bindRoot.init(&bindSet, slangReflection, 0);
BindRoot* outputBindRoot = m_options.outputUsingType ? &bindRoot : nullptr;
SLANG_RETURN_ON_FAIL(writeBindingOutput(outputBindRoot, m_options.outputPath));
}
else
{
SlangResult res = writeScreen(m_options.outputPath);
if (SLANG_FAILED(res))
{
fprintf(stderr, "ERROR: failed to write screen capture to file\n");
return res;
}
}
}
// We are done
window->postQuit();
return SLANG_OK;
}
m_renderer->presentFrame();
return SLANG_OK;
}
static SlangResult _setSessionPrelude(const Options& options, const char* exePath, SlangSession* session)
{
// Let's see if we need to set up special prelude for HLSL
if (options.nvapiExtnSlot.getLength())
{
String rootPath;
SLANG_RETURN_ON_FAIL(TestToolUtil::getRootPath(exePath, rootPath));
String includePath;
SLANG_RETURN_ON_FAIL(TestToolUtil::getIncludePath(rootPath, "external/nvapi/nvHLSLExtns.h", includePath));
StringBuilder buf;
// We have to choose a slot that NVAPI will use.
buf << "#define NV_SHADER_EXTN_SLOT " << options.nvapiExtnSlot << "\n";
// Include the NVAPI header
buf << "#include \"" << includePath << "\"\n\n";
session->setLanguagePrelude(SLANG_SOURCE_LANGUAGE_HLSL, buf.getBuffer());
}
else
{
session->setLanguagePrelude(SLANG_SOURCE_LANGUAGE_HLSL, "");
}
return SLANG_OK;
}
} // namespace renderer_test
static SlangResult _innerMain(Slang::StdWriters* stdWriters, SlangSession* session, int argcIn, const char*const* argvIn)
{
using namespace renderer_test;
using namespace Slang;
StdWriters::setSingleton(stdWriters);
Options options;
// Parse command-line options
SLANG_RETURN_ON_FAIL(Options::parse(argcIn, argvIn, StdWriters::getError(), options));
// Declare window pointer before renderer, such that window is released after renderer
RefPtr<renderer_test::Window> window;
ShaderCompilerUtil::Input input;
input.profile = "";
input.target = SLANG_TARGET_NONE;
input.args = &options.slangArgs[0];
input.argCount = options.slangArgCount;
SlangSourceLanguage nativeLanguage = SLANG_SOURCE_LANGUAGE_UNKNOWN;
SlangPassThrough slangPassThrough = SLANG_PASS_THROUGH_NONE;
char const* profileName = "";
switch (options.rendererType)
{
case RendererType::DirectX11:
input.target = SLANG_DXBC;
input.profile = "sm_5_0";
nativeLanguage = SLANG_SOURCE_LANGUAGE_HLSL;
slangPassThrough = SLANG_PASS_THROUGH_FXC;
break;
case RendererType::DirectX12:
input.target = SLANG_DXBC;
input.profile = "sm_5_0";
nativeLanguage = SLANG_SOURCE_LANGUAGE_HLSL;
slangPassThrough = SLANG_PASS_THROUGH_FXC;
if( options.useDXIL )
{
input.target = SLANG_DXIL;
input.profile = "sm_6_0";
slangPassThrough = SLANG_PASS_THROUGH_DXC;
}
break;
case RendererType::OpenGl:
input.target = SLANG_GLSL;
input.profile = "glsl_430";
nativeLanguage = SLANG_SOURCE_LANGUAGE_GLSL;
slangPassThrough = SLANG_PASS_THROUGH_GLSLANG;
break;
case RendererType::Vulkan:
input.target = SLANG_SPIRV;
input.profile = "glsl_430";
nativeLanguage = SLANG_SOURCE_LANGUAGE_GLSL;
slangPassThrough = SLANG_PASS_THROUGH_GLSLANG;
break;
case RendererType::CPU:
input.target = SLANG_HOST_CALLABLE;
input.profile = "";
nativeLanguage = SLANG_SOURCE_LANGUAGE_CPP;
slangPassThrough = SLANG_PASS_THROUGH_GENERIC_C_CPP;
break;
case RendererType::CUDA:
input.target = SLANG_PTX;
input.profile = "";
nativeLanguage = SLANG_SOURCE_LANGUAGE_CUDA;
slangPassThrough = SLANG_PASS_THROUGH_NVRTC;
break;
default:
fprintf(stderr, "error: unexpected\n");
return SLANG_FAIL;
}
switch (options.inputLanguageID)
{
case Options::InputLanguageID::Slang:
input.sourceLanguage = SLANG_SOURCE_LANGUAGE_SLANG;
input.passThrough = SLANG_PASS_THROUGH_NONE;
break;
case Options::InputLanguageID::Native:
input.sourceLanguage = nativeLanguage;
input.passThrough = slangPassThrough;
break;
default:
break;
}
switch( options.shaderType )
{
case Options::ShaderProgramType::Graphics:
case Options::ShaderProgramType::GraphicsCompute:
input.pipelineType = PipelineType::Graphics;
break;
case Options::ShaderProgramType::Compute:
input.pipelineType = PipelineType::Compute;
break;
case Options::ShaderProgramType::RayTracing:
input.pipelineType = PipelineType::RayTracing;
break;
default:
break;
}
if (options.sourceLanguage != SLANG_SOURCE_LANGUAGE_UNKNOWN)
{
input.sourceLanguage = options.sourceLanguage;
if (input.sourceLanguage == SLANG_SOURCE_LANGUAGE_C || input.sourceLanguage == SLANG_SOURCE_LANGUAGE_CPP)
{
input.passThrough = SLANG_PASS_THROUGH_GENERIC_C_CPP;
}
}
// Use the profile name set on options if set
input.profile = options.profileName ? options.profileName : input.profile;
StringBuilder rendererName;
rendererName << "[" << RendererUtil::toText(options.rendererType) << "] ";
if (options.adapter.getLength())
{
rendererName << "'" << options.adapter << "'";
}
if (options.onlyStartup)
{
switch (options.rendererType)
{
case RendererType::CUDA:
{
#if RENDER_TEST_CUDA
return SLANG_SUCCEEDED(spSessionCheckPassThroughSupport(session, SLANG_PASS_THROUGH_NVRTC)) && CUDAComputeUtil::canCreateDevice() ? SLANG_OK : SLANG_FAIL;
#else
return SLANG_FAIL;
#endif
}
case RendererType::CPU:
{
// As long as we have CPU, then this should work
return spSessionCheckPassThroughSupport(session, SLANG_PASS_THROUGH_GENERIC_C_CPP);
}
default: break;
}
}
Index nvapiExtnSlot = -1;
// Let's see if we need to set up special prelude for HLSL
if (options.nvapiExtnSlot.getLength() && options.nvapiExtnSlot[0] == 'u')
{
//
Slang::Int value;
UnownedStringSlice slice = options.nvapiExtnSlot.getUnownedSlice();
UnownedStringSlice indexText(slice.begin() + 1 , slice.end());
if (SLANG_SUCCEEDED(StringUtil::parseInt(indexText, value)))
{
nvapiExtnSlot = Index(value);
}
}
// If can't set up a necessary prelude make not available (which will lead to the test being ignored)
if (SLANG_FAILED(_setSessionPrelude(options, argvIn[0], session)))
{
return SLANG_E_NOT_AVAILABLE;
}
// If it's CPU testing we don't need a window or a renderer
if (options.rendererType == RendererType::CPU)
{
// Check we have all the required features
for (const auto& renderFeature : options.renderFeatures)
{
if (!CPUComputeUtil::hasFeature(renderFeature.getUnownedSlice()))
{
return SLANG_E_NOT_AVAILABLE;
}
}
ShaderCompilerUtil::OutputAndLayout compilationAndLayout;
SLANG_RETURN_ON_FAIL(ShaderCompilerUtil::compileWithLayout(session, options, input, compilationAndLayout));
{
// Get the shared library -> it contains the executable code, we need to keep around if we recompile
ComPtr<ISlangSharedLibrary> sharedLibrary;
SLANG_RETURN_ON_FAIL(spGetEntryPointHostCallable(compilationAndLayout.output.request, 0, 0, sharedLibrary.writeRef()));
// This is a hack to work around, reflection when compiling straight C/C++ code. In that case the code is just passed
// straight through to the C++ compiler so no reflection. In these tests though we should have conditional code
// (performance-profile.slang for example), such that there is both a slang and C++ code, and it is the job
// of the test implementer to *ensure* that the straight C++ code has the same layout as the slang C++ backend.
//
// If we are running c/c++ we still need binding information, so compile again as slang source
if (options.sourceLanguage == SLANG_SOURCE_LANGUAGE_C || input.sourceLanguage == SLANG_SOURCE_LANGUAGE_CPP)
{
ShaderCompilerUtil::Input slangInput = input;
slangInput.sourceLanguage = SLANG_SOURCE_LANGUAGE_SLANG;
slangInput.passThrough = SLANG_PASS_THROUGH_NONE;
// We just want CPP, so we get suitable reflection
slangInput.target = SLANG_CPP_SOURCE;
SLANG_RETURN_ON_FAIL(ShaderCompilerUtil::compileWithLayout(session, options, slangInput, compilationAndLayout));
}
// calculate binding
CPUComputeUtil::Context context;
SLANG_RETURN_ON_FAIL(CPUComputeUtil::createBindlessResources(compilationAndLayout, context));
SLANG_RETURN_ON_FAIL(CPUComputeUtil::fillRuntimeHandleInBuffers(compilationAndLayout, context, sharedLibrary.get()));
SLANG_RETURN_ON_FAIL(CPUComputeUtil::calcBindings(compilationAndLayout, context));
// Get the execution info from the lib
CPUComputeUtil::ExecuteInfo info;
SLANG_RETURN_ON_FAIL(CPUComputeUtil::calcExecuteInfo(CPUComputeUtil::ExecuteStyle::GroupRange, sharedLibrary, options.computeDispatchSize, compilationAndLayout, context, info));
const uint64_t startTicks = ProcessUtil::getClockTick();
SLANG_RETURN_ON_FAIL(CPUComputeUtil::execute(info));
if (options.performanceProfile)
{
const uint64_t endTicks = ProcessUtil::getClockTick();
_outputProfileTime(startTicks, endTicks);
}
if (options.outputPath)
{
BindRoot* outputBindRoot = options.outputUsingType ? &context.m_bindRoot : nullptr;
// Dump everything out that was written
SLANG_RETURN_ON_FAIL(ShaderInputLayout::writeBindings(outputBindRoot, compilationAndLayout.layout, context.m_buffers, options.outputPath));
// Check all execution styles produce the same result
SLANG_RETURN_ON_FAIL(CPUComputeUtil::checkStyleConsistency(sharedLibrary, options.computeDispatchSize, compilationAndLayout));
}
}
return SLANG_OK;
}
if (options.rendererType == RendererType::CUDA)
{
#if RENDER_TEST_CUDA
// Check we have all the required features
for (const auto& renderFeature : options.renderFeatures)
{
if (!CUDAComputeUtil::hasFeature(renderFeature.getUnownedSlice()))
{
return SLANG_E_NOT_AVAILABLE;
}
}
ShaderCompilerUtil::OutputAndLayout compilationAndLayout;
SLANG_RETURN_ON_FAIL(ShaderCompilerUtil::compileWithLayout(session, options, input, compilationAndLayout));
const uint64_t startTicks = ProcessUtil::getClockTick();
CUDAComputeUtil::Context context;
SLANG_RETURN_ON_FAIL(CUDAComputeUtil::execute(compilationAndLayout, options.computeDispatchSize, context));
if (options.performanceProfile)
{
const uint64_t endTicks = ProcessUtil::getClockTick();
_outputProfileTime(startTicks, endTicks);
}
if (options.outputPath)
{
BindRoot* outputBindRoot = options.outputUsingType ? &context.m_bindRoot : nullptr;
// Dump everything out that was written
SLANG_RETURN_ON_FAIL(ShaderInputLayout::writeBindings(outputBindRoot, compilationAndLayout.layout, context.m_buffers, options.outputPath));
}
return SLANG_OK;
#else
return SLANG_FAIL;
#endif
}
Slang::RefPtr<Renderer> renderer;
{
RendererUtil::CreateFunc createFunc = RendererUtil::getCreateFunc(options.rendererType);
if (createFunc)
{
renderer = createFunc();
}
if (!renderer)
{
if (!options.onlyStartup)
{
fprintf(stderr, "Unable to create renderer %s\n", rendererName.getBuffer());
}
return SLANG_FAIL;
}
Renderer::Desc desc;
desc.width = gWindowWidth;
desc.height = gWindowHeight;
desc.adapter = options.adapter;
desc.requiredFeatures = options.renderFeatures;
desc.nvapiExtnSlot = int(nvapiExtnSlot);
window = renderer_test::Window::create();
SLANG_RETURN_ON_FAIL(window->initialize(gWindowWidth, gWindowHeight));
SlangResult res = renderer->initialize(desc, window->getHandle());
if (SLANG_FAILED(res))
{
// Returns E_NOT_AVAILABLE only when specified features are not available.
// Will cause to be ignored.
if (!options.onlyStartup && res != SLANG_E_NOT_AVAILABLE)
{
fprintf(stderr, "Unable to initialize renderer %s\n", rendererName.getBuffer());
}
return res;
}
for (const auto& feature : options.renderFeatures)
{
// If doesn't have required feature... we have to give up
if (!renderer->hasFeature(feature.getUnownedSlice()))
{
return SLANG_E_NOT_AVAILABLE;
}
}
}
// If the only test is we can startup, then we are done
if (options.onlyStartup)
{
return SLANG_OK;
}
{
RefPtr<RenderTestApp> app(new RenderTestApp);
SLANG_RETURN_ON_FAIL(app->initialize(session, renderer, options, input));
window->show();
return window->runLoop(app);
}
}
SLANG_TEST_TOOL_API SlangResult innerMain(Slang::StdWriters* stdWriters, SlangSession* sharedSession, int inArgc, const char*const* inArgv)
{
using namespace Slang;
// Assume we will used the shared session
ComPtr<slang::IGlobalSession> session(sharedSession);
// The sharedSession always has a pre-loaded stdlib.
// This differed test checks if the command line has an option to setup the stdlib.
// If so we *don't* use the sharedSession, and create a new stdlib-less session just for this compilation.
if (TestToolUtil::hasDeferredStdLib(Index(inArgc - 1), inArgv + 1))
{
SLANG_RETURN_ON_FAIL(slang_createGlobalSessionWithoutStdLib(SLANG_API_VERSION, session.writeRef()));
}
SlangResult res = SLANG_FAIL;
try
{
res = _innerMain(stdWriters, session, inArgc, inArgv);
}
catch (const Slang::Exception& exception)
{
stdWriters->getOut().put(exception.Message.getUnownedSlice());
return SLANG_FAIL;
}
catch (...)
{
stdWriters->getOut().put(UnownedStringSlice::fromLiteral("Unhandled exception"));
return SLANG_FAIL;
}
return res;
}
int main(int argc, char** argv)
{
using namespace Slang;
SlangSession* session = spCreateSession(nullptr);
TestToolUtil::setSessionDefaultPreludeFromExePath(argv[0], session);
auto stdWriters = StdWriters::initDefaultSingleton();
SlangResult res = innerMain(stdWriters, session, argc, argv);
spDestroySession(session);
return (int)TestToolUtil::getReturnCode(res);
}
|