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
|
// unit-test-translation-unit-import.cpp
#include "../../source/core/slang-io.h"
#include "../../source/core/slang-process.h"
#include "slang-com-ptr.h"
#include "slang.h"
#include "unit-test/slang-unit-test.h"
#include <stdio.h>
#include <stdlib.h>
using namespace Slang;
static String getTypeFullName(slang::TypeReflection* type)
{
ComPtr<ISlangBlob> blob;
type->getFullName(blob.writeRef());
return String((const char*)blob->getBufferPointer());
}
// Test that the reflection API provides correct info about modules with link-time types.
SLANG_UNIT_TEST(linkTimeTypeReflection)
{
// Source for a module that contains can be specialized with a link-time type.
const char* userSourceBody = R"(
interface IMaterial { float4 load(); }
extern struct Material : IMaterial;
ConstantBuffer<Material> gMaterial;
RWTexture2D tex;
extern static const int count;
uniform uint4 buffers[count];
[numthreads(1,1,1)]
[shader("compute")]
void computeMain() {
tex[uint2(0, 0)] = gMaterial.load();
}
)";
String moduleName = "linkTimeTypeReflection_Compute";
ComPtr<slang::IGlobalSession> globalSession;
SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
slang::TargetDesc targetDesc = {};
targetDesc.format = SLANG_SPIRV_ASM;
targetDesc.profile = globalSession->findProfile("spirv_1_5");
slang::SessionDesc sessionDesc = {};
sessionDesc.targetCount = 1;
sessionDesc.targets = &targetDesc;
ComPtr<slang::ISession> session;
SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);
ComPtr<slang::IBlob> diagnosticBlob;
auto module = session->loadModuleFromSourceString(
moduleName.getBuffer(),
(moduleName + ".slang").getBuffer(),
userSourceBody,
diagnosticBlob.writeRef());
SLANG_CHECK_ABORT(module != nullptr);
// Source for a module that defines the link-time type.
String configModuleSource = "import " + moduleName + ";\n" + R"(
export struct Material : IMaterial = MyMaterial;
export static const int count = 11;
struct MyMaterial : IMaterial {
int data;
Texture2D diffuse;
float4 load() { return diffuse.Load(uint3(0,0,0)); }
}
)";
auto configModule = session->loadModuleFromSourceString(
"config",
"config.slang",
configModuleSource.getBuffer(),
diagnosticBlob.writeRef());
SLANG_CHECK_ABORT(configModule != nullptr);
slang::IComponentType* components[] = {module, configModule};
ComPtr<slang::IComponentType> compositeProgram;
session->createCompositeComponentType(
components,
2,
compositeProgram.writeRef(),
diagnosticBlob.writeRef());
SLANG_CHECK_ABORT(compositeProgram != nullptr);
ComPtr<slang::IComponentType> linkedProgram;
compositeProgram->link(linkedProgram.writeRef(), diagnosticBlob.writeRef());
SLANG_CHECK_ABORT(linkedProgram != nullptr);
auto programLayout = linkedProgram->getLayout();
auto var0 = programLayout->getParameterByIndex(0);
// `gMaterial`'s binding starts at 1, because there is an implicit global uniform buffer.
SLANG_CHECK(var0->getOffset(slang::ParameterCategory::DescriptorTableSlot) == 1);
SLANG_CHECK(var0->getTypeLayout()->getSize(slang::ParameterCategory::DescriptorTableSlot) == 2);
auto elementLayout = var0->getTypeLayout()->getElementTypeLayout();
SLANG_CHECK_ABORT(elementLayout != nullptr);
SLANG_CHECK(elementLayout->getSize() == 16);
auto var1 = programLayout->getParameterByIndex(1);
SLANG_CHECK(var1->getOffset(slang::ParameterCategory::DescriptorTableSlot) == 3);
auto var2 = programLayout->getParameterByIndex(2);
SLANG_CHECK(var2->getTypeLayout()->getSize() == 11 * 16);
ComPtr<slang::IBlob> codeBlob;
linkedProgram->getTargetCode(0, codeBlob.writeRef(), diagnosticBlob.writeRef());
SLANG_CHECK_ABORT(codeBlob.get());
auto spirvStr = UnownedStringSlice((const char*)codeBlob->getBufferPointer());
SLANG_CHECK(spirvStr.indexOf(toSlice("OpDecorate %tex Binding 3")) != -1);
}
// Test that the reflection API provides correct info about modules using link-time constants in a
// `Conditional` field.
SLANG_UNIT_TEST(linkTimeConditionalReflection)
{
// Source for a module that contains can be specialized with a link-time constant.
const char* userSourceBody = R"(
module LinkTimeConditional;
extern static const bool hasNormal;
extern static const bool hasColor;
struct VertexOut
{
float4 pos : SV_Position;
float someData;
Conditional<float3, hasNormal> normal;
Conditional<float3, hasColor> color;
}
[shader("vertex")]
VertexOut vertexMain()
{
VertexOut v;
v.pos = float4(0,0,0,1);
v.someData = 2.0f;
v.normal.set(float3(1,0,0));
v.color.set(float3(1,1,1));
return v;
}
)";
String moduleName = "LinkTimeConditional";
ComPtr<slang::IGlobalSession> globalSession;
SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
slang::TargetDesc targetDesc = {};
targetDesc.format = SLANG_SPIRV_ASM;
targetDesc.profile = globalSession->findProfile("spirv_1_5");
slang::SessionDesc sessionDesc = {};
sessionDesc.targetCount = 1;
sessionDesc.targets = &targetDesc;
ComPtr<slang::ISession> session;
SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);
ComPtr<slang::IBlob> diagnosticBlob;
auto module = session->loadModuleFromSourceString(
moduleName.getBuffer(),
(moduleName + ".slang").getBuffer(),
userSourceBody,
diagnosticBlob.writeRef());
SLANG_CHECK_ABORT(module != nullptr);
// Source for a module that defines the link-time constants.
String configModuleSource = R"(
export static const bool hasNormal = false;
export static const bool hasColor = true;
)";
auto configModule = session->loadModuleFromSourceString(
"config",
"config.slang",
configModuleSource.getBuffer(),
diagnosticBlob.writeRef());
SLANG_CHECK_ABORT(configModule != nullptr);
ComPtr<slang::IEntryPoint> entryPoint;
module->getDefinedEntryPoint(0, entryPoint.writeRef());
slang::IComponentType* components[] = {module, configModule, entryPoint};
ComPtr<slang::IComponentType> compositeProgram;
session->createCompositeComponentType(
components,
3,
compositeProgram.writeRef(),
diagnosticBlob.writeRef());
SLANG_CHECK_ABORT(compositeProgram != nullptr);
ComPtr<slang::IComponentType> linkedProgram;
compositeProgram->link(linkedProgram.writeRef(), diagnosticBlob.writeRef());
SLANG_CHECK_ABORT(linkedProgram != nullptr);
auto programLayout = linkedProgram->getLayout();
auto entryPointLayout = programLayout->getEntryPointByIndex(0);
auto resultLayout = entryPointLayout->getResultVarLayout();
SLANG_CHECK_ABORT(resultLayout != nullptr);
// Number of varying output is 2, because `pos` is a system value that doesn't count towards
// varying output.
SLANG_CHECK(
resultLayout->getTypeLayout()->getSize(slang::ParameterCategory::VaryingOutput) == 2);
ComPtr<slang::IBlob> codeBlob;
linkedProgram->getTargetCode(0, codeBlob.writeRef(), diagnosticBlob.writeRef());
SLANG_CHECK_ABORT(codeBlob.get());
auto spirvStr = UnownedStringSlice((const char*)codeBlob->getBufferPointer());
// Test that the resulting spirv defines output at location 1, but not at location 2.
SLANG_CHECK(spirvStr.indexOf(toSlice("Location 1")) != -1);
SLANG_CHECK(spirvStr.indexOf(toSlice("Location 2")) == -1);
}
|