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
|
#include "core/slang-basic.h"
#include "core/slang-blob.h"
#include "gfx-test-util.h"
#include "gfx-util/shader-cursor.h"
#include "slang-gfx.h"
#include "unit-test/slang-unit-test.h"
using namespace gfx;
namespace gfx_test
{
static Slang::Result loadProgram(
gfx::IDevice* device,
Slang::ComPtr<gfx::IShaderProgram>& outShaderProgram,
const char* mainModuleName,
const char* libModuleName,
const char* entryPointName,
slang::ProgramLayout*& slangReflection)
{
Slang::ComPtr<slang::ISession> slangSession;
SLANG_RETURN_ON_FAIL(device->getSlangSession(slangSession.writeRef()));
Slang::ComPtr<slang::IBlob> diagnosticsBlob;
// Load main module
slang::IModule* mainModule =
slangSession->loadModule(mainModuleName, diagnosticsBlob.writeRef());
diagnoseIfNeeded(diagnosticsBlob);
if (!mainModule)
return SLANG_FAIL;
// Load library module with constants
slang::IModule* libModule = slangSession->loadModule(libModuleName, diagnosticsBlob.writeRef());
diagnoseIfNeeded(diagnosticsBlob);
if (!libModule)
return SLANG_FAIL;
// Find entry point
ComPtr<slang::IEntryPoint> computeEntryPoint;
SLANG_RETURN_ON_FAIL(
mainModule->findEntryPointByName(entryPointName, computeEntryPoint.writeRef()));
// Compose program from modules
Slang::List<slang::IComponentType*> componentTypes;
componentTypes.add(mainModule);
componentTypes.add(libModule);
componentTypes.add(computeEntryPoint);
Slang::ComPtr<slang::IComponentType> composedProgram;
SlangResult result = slangSession->createCompositeComponentType(
componentTypes.getBuffer(),
componentTypes.getCount(),
composedProgram.writeRef(),
diagnosticsBlob.writeRef());
diagnoseIfNeeded(diagnosticsBlob);
SLANG_RETURN_ON_FAIL(result);
// Link program
ComPtr<slang::IComponentType> linkedProgram;
result = composedProgram->link(linkedProgram.writeRef(), diagnosticsBlob.writeRef());
diagnoseIfNeeded(diagnosticsBlob);
SLANG_RETURN_ON_FAIL(result);
composedProgram = linkedProgram;
slangReflection = composedProgram->getLayout();
// Create shader program
gfx::IShaderProgram::Desc programDesc = {};
programDesc.slangGlobalScope = composedProgram.get();
auto shaderProgram = device->createProgram(programDesc);
outShaderProgram = shaderProgram;
return SLANG_OK;
}
// Function to validate the array size in struct S
static void validateArraySizeInStruct(
UnitTestContext* context,
slang::ProgramLayout* slangReflection,
int expectedSize)
{
// Check reflection is available
SLANG_CHECK(slangReflection != nullptr);
// Get the global scope layout
auto globalScope = slangReflection->getGlobalParamsVarLayout();
SLANG_CHECK_MSG(globalScope != nullptr, "Could not get global scope layout");
auto typeLayout = globalScope->getTypeLayout();
SLANG_CHECK_MSG(typeLayout != nullptr, "Global scope has no type layout");
// Check if the global scope is a struct type
auto kind = typeLayout->getKind();
SLANG_CHECK_MSG(
kind == slang::TypeReflection::Kind::Struct,
"Global scope is not a struct type");
// Find the buffer resource 'b'
bool foundBuffer = false;
auto fieldCount = typeLayout->getFieldCount();
for (unsigned int i = 0; i < fieldCount; i++)
{
auto fieldLayout = typeLayout->getFieldByIndex(i);
const char* fieldName = fieldLayout->getName();
if (fieldName && strcmp(fieldName, "b") == 0)
{
foundBuffer = true;
// Get the type layout of the field
auto fieldTypeLayout = fieldLayout->getTypeLayout();
SLANG_CHECK_MSG(fieldTypeLayout != nullptr, "Field has no type layout");
// Get the element type of the structured buffer
auto elementTypeLayout = fieldTypeLayout->getElementTypeLayout();
SLANG_CHECK_MSG(
elementTypeLayout != nullptr,
"Structured buffer has no element type layout");
// Check if it's a struct type
auto elementKind = elementTypeLayout->getKind();
SLANG_CHECK_MSG(
elementKind == slang::TypeReflection::Kind::Struct,
"Buffer element is not a struct type");
// Get the field count of the struct
auto structFieldCount = elementTypeLayout->getFieldCount();
SLANG_CHECK_MSG(structFieldCount >= 1, "Struct has no fields");
// Check for the 'xs' field
bool foundXsField = false;
for (unsigned int j = 0; j < structFieldCount; j++)
{
auto structField = elementTypeLayout->getFieldByIndex(j);
const char* structFieldName = structField->getName();
if (structFieldName && strcmp(structFieldName, "xs") == 0)
{
foundXsField = true;
// Check that it's an array type
auto structFieldTypeLayout = structField->getTypeLayout();
auto structFieldTypeKind = structFieldTypeLayout->getKind();
SLANG_CHECK_MSG(
structFieldTypeKind == slang::TypeReflection::Kind::Array,
"Field 'xs' is not an array type");
// Check the array size
auto arraySize = structFieldTypeLayout->getElementCount();
// 0 becuase we haven't resolved the constant
SLANG_CHECK_MSG(
arraySize == 0,
"Field 'xs' array size does not match expected size");
// 4 because we're resolving it
const auto resolvedArraySize =
structFieldTypeLayout->getElementCount(slangReflection);
SLANG_CHECK_MSG(
resolvedArraySize == expectedSize,
"Field 'xs' array size does not match expected size");
break;
}
}
SLANG_CHECK_MSG(foundXsField, "Could not find field 'xs' in struct S");
break;
}
}
SLANG_CHECK_MSG(foundBuffer, "Could not find buffer 'b' in global scope");
}
void linkTimeConstantArraySizeTestImpl(IDevice* device, UnitTestContext* context)
{
// Create transient heap
Slang::ComPtr<ITransientResourceHeap> transientHeap;
ITransientResourceHeap::Desc transientHeapDesc = {};
transientHeapDesc.constantBufferSize = 4096;
GFX_CHECK_CALL_ABORT(
device->createTransientResourceHeap(transientHeapDesc, transientHeap.writeRef()));
// Load and link program
ComPtr<IShaderProgram> shaderProgram;
slang::ProgramLayout* slangReflection;
GFX_CHECK_CALL_ABORT(loadProgram(
device,
shaderProgram,
"link-time-constant-array-size-main",
"link-time-constant-array-size-lib",
"computeMain",
slangReflection));
// Check array size through reflection
const int N = 4; // This should match the constant in lib.slang
validateArraySizeInStruct(context, slangReflection, N);
// Create compute pipeline
ComputePipelineStateDesc pipelineDesc = {};
pipelineDesc.program = shaderProgram.get();
ComPtr<gfx::IPipelineState> pipelineState;
GFX_CHECK_CALL_ABORT(
device->createComputePipelineState(pipelineDesc, pipelineState.writeRef()));
// Create buffer for struct S with array of size N
int32_t initialData[] = {1, 2, 3, 4};
IBufferResource::Desc bufferDesc = {};
bufferDesc.sizeInBytes = N * sizeof(int32_t);
bufferDesc.format = gfx::Format::Unknown;
bufferDesc.elementSize = sizeof(int32_t);
bufferDesc.allowedStates = ResourceStateSet(
ResourceState::ShaderResource,
ResourceState::UnorderedAccess,
ResourceState::CopyDestination,
ResourceState::CopySource);
bufferDesc.defaultState = ResourceState::UnorderedAccess;
bufferDesc.memoryType = MemoryType::DeviceLocal;
ComPtr<IBufferResource> numbersBuffer;
GFX_CHECK_CALL_ABORT(
device->createBufferResource(bufferDesc, (void*)initialData, numbersBuffer.writeRef()));
ComPtr<IResourceView> bufferView;
IResourceView::Desc viewDesc = {};
viewDesc.type = IResourceView::Type::UnorderedAccess;
viewDesc.format = Format::Unknown;
GFX_CHECK_CALL_ABORT(
device->createBufferView(numbersBuffer, nullptr, viewDesc, bufferView.writeRef()));
// Record and execute command buffer
{
ICommandQueue::Desc queueDesc = {ICommandQueue::QueueType::Graphics};
auto queue = device->createCommandQueue(queueDesc);
auto commandBuffer = transientHeap->createCommandBuffer();
auto encoder = commandBuffer->encodeComputeCommands();
auto rootObject = encoder->bindPipeline(pipelineState);
ShaderCursor rootCursor(rootObject);
rootCursor.getPath("b").setResource(bufferView);
encoder->dispatchCompute(1, 1, 1);
encoder->endEncoding();
commandBuffer->close();
queue->executeCommandBuffer(commandBuffer);
queue->waitOnHost();
}
// Expected results: each element is input * N
// With N=4 and inputs [1,2,3,4], expected output is [4,8,12,16]
compareComputeResult(device, numbersBuffer, Slang::makeArray<int>(4, 8, 12, 16));
}
SLANG_UNIT_TEST(linkTimeConstantArraySizeD3D12)
{
runTestImpl(linkTimeConstantArraySizeTestImpl, unitTestContext, Slang::RenderApiFlag::D3D12);
}
SLANG_UNIT_TEST(linkTimeConstantArraySizeVulkan)
{
runTestImpl(linkTimeConstantArraySizeTestImpl, unitTestContext, Slang::RenderApiFlag::Vulkan);
}
} // namespace gfx_test
|