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
|
// unit-test-translation-unit-import.cpp
#include "slang.h"
#include <stdio.h>
#include <stdlib.h>
#include "tools/unit-test/slang-unit-test.h"
#include "slang-com-ptr.h"
#include "../../source/core/slang-io.h"
#include "../../source/core/slang-process.h"
using namespace Slang;
static String getTypeFullName(slang::TypeReflection* type)
{
ComPtr<ISlangBlob> blob;
type->getFullName(blob.writeRef());
return String((const char*)blob->getBufferPointer());
}
static void printRefl(slang::DeclReflection* refl, unsigned int level = 0)
{
// Mapping of kind ids to names
std::string names[] = {"Unsupported", "Struct", "Function", "Module", "Generic", "Variable"};
for (unsigned int i = 0; i < level; i++)
{
std::cout << " ";
}
std::cout<< "[" << names[(unsigned int)refl->getKind()] << "] (" << refl->getChildrenCount() << ")" << std::endl;
for (auto* child : refl->getChildren())
{
printRefl(child, level + 1);
}
}
// Test that the reflection API provides correct info about entry point and ordinary functions.
SLANG_UNIT_TEST(declTreeReflection)
{
// Source for a module that contains an undecorated entrypoint.
const char* userSourceBody = R"(
[__AttributeUsage(_AttributeTargets.Function)]
struct MyFuncPropertyAttribute {int v;}
[MyFuncProperty(1024)]
[Differentiable]
float ordinaryFunc(no_diff float x, int y) { return x + y; }
float4 fragMain(float4 pos:SV_Position) : SV_Position
{
return pos;
}
uint f(uint y) { return y; }
struct MyType
{
int x;
float f(float x) { return x; }
}
struct MyGenericType<T : IArithmetic & IFloat>
{
T z;
T g() { return z; }
U h<U>(U x, out T y) { y = z; return x; }
T j<let N : int>(T x, out int o) { o = N; return x; }
}
)";
auto moduleName = "moduleG" + String(Process::getId());
String userSource = "import " + moduleName + ";\n" + userSourceBody;
ComPtr<slang::IGlobalSession> globalSession;
SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
slang::TargetDesc targetDesc = {};
targetDesc.format = SLANG_HLSL;
targetDesc.profile = globalSession->findProfile("sm_5_0");
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("m", "m.slang", userSourceBody, diagnosticBlob.writeRef());
SLANG_CHECK(module != nullptr);
ComPtr<slang::IEntryPoint> entryPoint;
module->findAndCheckEntryPoint("fragMain", SLANG_STAGE_FRAGMENT, entryPoint.writeRef(), diagnosticBlob.writeRef());
SLANG_CHECK(entryPoint != nullptr);
ComPtr<slang::IComponentType> compositeProgram;
slang::IComponentType* components[] = { module, entryPoint.get() };
session->createCompositeComponentType(components, 2, compositeProgram.writeRef(), diagnosticBlob.writeRef());
SLANG_CHECK(compositeProgram != nullptr);
auto moduleDeclReflection = module->getModuleReflection();
SLANG_CHECK(moduleDeclReflection != nullptr);
SLANG_CHECK(moduleDeclReflection->getKind() == slang::DeclReflection::Kind::Module);
SLANG_CHECK(moduleDeclReflection->getChildrenCount() == 7);
// First declaration should be a struct with 1 variable
auto firstDecl = moduleDeclReflection->getChild(0);
SLANG_CHECK(firstDecl->getKind() == slang::DeclReflection::Kind::Struct);
SLANG_CHECK(firstDecl->getChildrenCount() == 1);
{
slang::TypeReflection* type = firstDecl->getType();
SLANG_CHECK(getTypeFullName(type) == "MyFuncPropertyAttribute");
// Check the field of the struct.
SLANG_CHECK(type->getFieldCount() == 1);
auto field = type->getFieldByIndex(0);
SLANG_CHECK(UnownedStringSlice(field->getName()) == "v");
SLANG_CHECK(getTypeFullName(field->getType()) == "int");
}
// Second declaration should be a function
auto secondDecl = moduleDeclReflection->getChild(1);
SLANG_CHECK(secondDecl->getKind() == slang::DeclReflection::Kind::Func);
SLANG_CHECK(secondDecl->getChildrenCount() == 2); // Parameter declarations are children (return type is not)
{
auto funcReflection = secondDecl->asFunction();
SLANG_CHECK(funcReflection->findModifier(slang::Modifier::Differentiable) != nullptr);
SLANG_CHECK(getTypeFullName(funcReflection->getReturnType()) == "float");
SLANG_CHECK(UnownedStringSlice(funcReflection->getName()) == "ordinaryFunc");
SLANG_CHECK(funcReflection->getParameterCount() == 2);
SLANG_CHECK(UnownedStringSlice(funcReflection->getParameterByIndex(0)->getName()) == "x");
SLANG_CHECK(getTypeFullName(funcReflection->getParameterByIndex(0)->getType()) == "float");
SLANG_CHECK(funcReflection->getParameterByIndex(0)->findModifier(slang::Modifier::NoDiff) != nullptr);
SLANG_CHECK(UnownedStringSlice(funcReflection->getParameterByIndex(1)->getName()) == "y");
SLANG_CHECK(getTypeFullName(funcReflection->getParameterByIndex(1)->getType()) == "int");
SLANG_CHECK(funcReflection->getUserAttributeCount() == 1);
auto userAttribute = funcReflection->getUserAttributeByIndex(0);
SLANG_CHECK(UnownedStringSlice(userAttribute->getName()) == "MyFuncProperty");
SLANG_CHECK(userAttribute->getArgumentCount() == 1);
SLANG_CHECK(getTypeFullName(userAttribute->getArgumentType(0)) == "int");
int val = 0;
auto result = userAttribute->getArgumentValueInt(0, &val);
SLANG_CHECK(result == SLANG_OK);
SLANG_CHECK(val == 1024);
SLANG_CHECK(funcReflection->findUserAttributeByName(globalSession.get(), "MyFuncProperty") == userAttribute);
}
// Third declaration should also be a function
auto thirdDecl = moduleDeclReflection->getChild(2);
SLANG_CHECK(thirdDecl->getKind() == slang::DeclReflection::Kind::Func);
SLANG_CHECK(thirdDecl->getChildrenCount() == 1);
{
auto funcReflection = thirdDecl->asFunction();
SLANG_CHECK(getTypeFullName(funcReflection->getReturnType()) == "vector<float,4>");
SLANG_CHECK(UnownedStringSlice(funcReflection->getName()) == "fragMain");
SLANG_CHECK(funcReflection->getParameterCount() == 1);
SLANG_CHECK(UnownedStringSlice(funcReflection->getParameterByIndex(0)->getName()) == "pos");
SLANG_CHECK(getTypeFullName(funcReflection->getParameterByIndex(0)->getType()) == "vector<float,4>");
}
// Sixth declaration should be a generic struct
auto sixthDecl = moduleDeclReflection->getChild(5);
SLANG_CHECK(sixthDecl->getKind() == slang::DeclReflection::Kind::Generic);
auto genericReflection = sixthDecl->asGeneric();
SLANG_CHECK(genericReflection->getTypeParameterCount() == 1);
auto typeParamT = genericReflection->getTypeParameter(0);
SLANG_CHECK(UnownedStringSlice(typeParamT->getName()) == "T");
auto typeParamTConstraintCount = genericReflection->getTypeParameterConstraintCount(typeParamT);
SLANG_CHECK(typeParamTConstraintCount == 2);
auto typeParamTConstraintType1 = genericReflection->getTypeParameterConstraintType(typeParamT, 0);
SLANG_CHECK(getTypeFullName(typeParamTConstraintType1) == "IArithmetic");
auto typeParamTConstraintType2 = genericReflection->getTypeParameterConstraintType(typeParamT, 1);
SLANG_CHECK(getTypeFullName(typeParamTConstraintType2) == "IFloat");
auto innerStruct = genericReflection->getInnerDecl();
SLANG_CHECK(innerStruct->getKind() == slang::DeclReflection::Kind::Struct);
// Check type-lookup-by-name
{
auto type = compositeProgram->getLayout()->findTypeByName("MyType");
SLANG_CHECK(type != nullptr);
//SLANG_CHECK(type->getKind() == slang::DeclReflection::Kind::Struct);
SLANG_CHECK(UnownedStringSlice(type->getName()) == "MyType");
auto funcReflection = compositeProgram->getLayout()->findFunctionByNameInType(type, "f");
SLANG_CHECK(funcReflection != nullptr);
SLANG_CHECK(UnownedStringSlice(funcReflection->getName()) == "f");
SLANG_CHECK(getTypeFullName(funcReflection->getReturnType()) == "float");
SLANG_CHECK(funcReflection->getParameterCount() == 1);
SLANG_CHECK(UnownedStringSlice(funcReflection->getParameterByIndex(0)->getName()) == "x");
SLANG_CHECK(getTypeFullName(funcReflection->getParameterByIndex(0)->getType()) == "float");
}
// Check type-lookup-by-name for generic type
{
auto type = compositeProgram->getLayout()->findTypeByName("MyGenericType<half>");
SLANG_CHECK(type != nullptr);
//SLANG_CHECK(type->getKind() == slang::DeclReflection::Kind::Struct);
SLANG_CHECK(getTypeFullName(type) == "MyGenericType<half>");
auto funcReflection = compositeProgram->getLayout()->findFunctionByNameInType(type, "g");
SLANG_CHECK(funcReflection != nullptr);
SLANG_CHECK(UnownedStringSlice(funcReflection->getName()) == "g");
SLANG_CHECK(getTypeFullName(funcReflection->getReturnType()) == "half");
SLANG_CHECK(funcReflection->getParameterCount() == 0);
auto varReflection = compositeProgram->getLayout()->findVarByNameInType(type, "z");
SLANG_CHECK(varReflection != nullptr);
SLANG_CHECK(UnownedStringSlice(varReflection->getName()) == "z");
SLANG_CHECK(getTypeFullName(varReflection->getType()) == "half");
funcReflection = compositeProgram->getLayout()->findFunctionByNameInType(type, "h<float>");
SLANG_CHECK(funcReflection != nullptr);
SLANG_CHECK(UnownedStringSlice(funcReflection->getName()) == "h");
SLANG_CHECK(getTypeFullName(funcReflection->getReturnType()) == "float");
SLANG_CHECK(funcReflection->getParameterCount() == 2);
SLANG_CHECK(UnownedStringSlice(funcReflection->getParameterByIndex(0)->getName()) == "x");
SLANG_CHECK(getTypeFullName(funcReflection->getParameterByIndex(0)->getType()) == "float");
SLANG_CHECK(UnownedStringSlice(funcReflection->getParameterByIndex(1)->getName()) == "y");
SLANG_CHECK(getTypeFullName(funcReflection->getParameterByIndex(1)->getType()) == "half");
// Access parent generic container from a specialized method.
auto specializationInfo = funcReflection->getGenericContainer();
SLANG_CHECK(specializationInfo != nullptr);
SLANG_CHECK(UnownedStringSlice(specializationInfo->getName()) == "h");
SLANG_CHECK(specializationInfo->asDecl()->getKind() == slang::DeclReflection::Kind::Generic);
// Check type parameters
SLANG_CHECK(specializationInfo->getTypeParameterCount() == 1);
auto typeParam = specializationInfo->getTypeParameter(0);
SLANG_CHECK(UnownedStringSlice(typeParam->getName()) == "U"); // generic name
SLANG_CHECK(getTypeFullName(specializationInfo->getConcreteType(typeParam)) == "float"); // specialized type name under the context in which the generic is obtained
SLANG_CHECK(specializationInfo->getTypeParameterConstraintCount(typeParam) == 0);
// Go up another level to the generic struct
specializationInfo = specializationInfo->getOuterGenericContainer();
SLANG_CHECK(specializationInfo != nullptr);
SLANG_CHECK(UnownedStringSlice(specializationInfo->getName()) == "MyGenericType");
SLANG_CHECK(specializationInfo->asDecl()->getKind() == slang::DeclReflection::Kind::Generic);
// Check type parameters
SLANG_CHECK(specializationInfo->getTypeParameterCount() == 1);
typeParam = specializationInfo->getTypeParameter(0);
SLANG_CHECK(UnownedStringSlice(typeParam->getName()) == "T"); // generic name
SLANG_CHECK(getTypeFullName(specializationInfo->getConcreteType(typeParam)) == "half"); // specialized type name under the context in which the generic is obtained
SLANG_CHECK(specializationInfo->getTypeParameterConstraintCount(typeParam) == 2);
// Query 'j' on the type 'half'
funcReflection = compositeProgram->getLayout()->findFunctionByNameInType(type, "j<10>");
SLANG_CHECK(funcReflection != nullptr);
SLANG_CHECK(UnownedStringSlice(funcReflection->getName()) == "j");
// Check the generic parameters
specializationInfo = funcReflection->getGenericContainer();
SLANG_CHECK(specializationInfo != nullptr);
SLANG_CHECK(UnownedStringSlice(specializationInfo->getName()) == "j");
SLANG_CHECK(specializationInfo->asDecl()->getKind() == slang::DeclReflection::Kind::Generic);
SLANG_CHECK(specializationInfo->getValueParameterCount() == 1);
auto valueParam = specializationInfo->getValueParameter(0);
SLANG_CHECK(UnownedStringSlice(valueParam->getName()) == "N"); // generic name
SLANG_CHECK(specializationInfo->getConcreteIntVal(valueParam) == 10);
}
// Check iterators
{
unsigned int count = 0;
for (auto* child : moduleDeclReflection->getChildren())
{
count++;
}
SLANG_CHECK(count == 7);
count = 0;
for (auto* child : moduleDeclReflection->getChildrenOfKind<slang::DeclReflection::Kind::Func>())
{
count++;
}
SLANG_CHECK(count == 3);
count = 0;
for (auto* child : moduleDeclReflection->getChildrenOfKind<slang::DeclReflection::Kind::Struct>())
{
count++;
}
SLANG_CHECK(count == 2);
count = 0;
for (auto* child : moduleDeclReflection->getChildrenOfKind<slang::DeclReflection::Kind::Generic>())
{
count++;
}
SLANG_CHECK(count == 1);
}
}
|