MCPcopy Create free account
hub / github.com/csyonghe/Spire / initialize

Function initialize

Examples/hello/hello.cpp:78–223  ·  view source on GitHub ↗

At initialization time, we are going to load and compile our Spire shader code, and then create the D3D11 API objects we need for rendering.

Source from the content-addressed store, hash-verified

76// code, and then create the D3D11 API objects we need for rendering.
77//
78HRESULT initialize( ID3D11Device* dxDevice )
79{
80 //
81 // First, we will load and compile our Spire source code.
82 //
83
84 // The argument here is an optional directory where the Spire compiler
85 // can cache files to speed up compilation of many kernels.
86 SpireCompilationContext* spireContext = spCreateCompilationContext(NULL);
87
88 // A diagnostic sink is used to collect output messages from the Spire
89 // compiler, so that we can easily iterate over them if an operation
90 // fails.
91 SpireDiagnosticSink* spireSink = spCreateDiagnosticSink(spireContext);
92
93 // Instruct Spire to generate code as HLSL
94 spSetCodeGenTarget(spireContext, SPIRE_HLSL);
95
96 // Load a file of Spire source code, which defines our modules
97 spLoadModuleLibrary(spireContext, "hello.spire", spireSink);
98
99 // Inspect any error messages that got reported...
100 emitSpireDiagnostics(spireSink);
101
102 //
103 // Once the source Spire has been loaded, we can assemble the modules
104 // there to make one or more shaders. In our case, we really only
105 // have one shader that we will use, so this step is kind of redundant.
106 //
107
108 // Create a shader, which will initially be empty
109 char const* shaderName = "HelloShader";
110 SpireShader* spireShader = spCreateShaderFromSource(spireContext, R"(
111 template shader HelloShader(module0) targets StandardPipeline
112 {
113 using module0;
114 }
115 )");
116
117 SpireModule * helloModule = spFindModule(spireContext, "HelloModule");
118
119 // Compile the constructed shader
120 SpireCompilationResult* spireResult = spCompileShader(spireContext, spireShader, &helloModule, 1, nullptr, spireSink);
121
122 // Inspect any error messages that got reported...
123 emitSpireDiagnostics(spireSink);
124
125 //
126 // Once we've compiled things successfully, we can extract the HLSL kernel
127 // code for our shader, and pass it on to the D3D API.
128 //
129
130 // TODO(tfoley): The implementation should allow `NULL` for the length
131 // output parameter.
132 int sourceCodeLength;
133
134 char const* vertexShaderCode = spGetShaderStageSource(spireResult, nullptr, "vs", &sourceCodeLength);
135 char const* fragmentShaderCode = spGetShaderStageSource(spireResult, nullptr, "fs", &sourceCodeLength);

Callers 1

WinMainFunction · 0.85

Calls 14

spCreateDiagnosticSinkFunction · 0.85
spSetCodeGenTargetFunction · 0.85
spLoadModuleLibraryFunction · 0.85
emitSpireDiagnosticsFunction · 0.85
spCreateShaderFromSourceFunction · 0.85
spFindModuleFunction · 0.85
spCompileShaderFunction · 0.85
spGetShaderStageSourceFunction · 0.85
compileHLSLShaderFunction · 0.85

Tested by

no test coverage detected