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
|
#ifndef SLANG_PRELUDE_CPP_TYPES_H
#define SLANG_PRELUDE_CPP_TYPES_H
#include "../slang.h"
#ifndef SLANG_PRELUDE_ASSERT
# ifdef _DEBUG
# define SLANG_PRELUDE_ASSERT(VALUE) assert(VALUE)
# else
# define SLANG_PRELUDE_ASSERT(VALUE)
# endif
#endif
#ifdef SLANG_PRELUDE_NAMESPACE
namespace SLANG_PRELUDE_NAMESPACE {
#endif
template <typename T, size_t SIZE>
struct FixedArray
{
const T& operator[](size_t index) const { SLANG_PRELUDE_ASSERT(index < SIZE); return m_data[index]; }
T& operator[](size_t index) { SLANG_PRELUDE_ASSERT(index < SIZE); return m_data[index]; }
T m_data[SIZE];
};
// An array that has no specified size, becomes a 'Array'. This stores the size so it can potentially
// do bounds checking.
template <typename T>
struct Array
{
const T& operator[](size_t index) const { SLANG_PRELUDE_ASSERT(index < count); return data[index]; }
T& operator[](size_t index) { SLANG_PRELUDE_ASSERT(index < count); return data[index]; }
T* data;
size_t count;
};
/* Constant buffers become a pointer to the contained type, so ConstantBuffer<T> becomes T* in C++ code.
*/
template <typename T, int COUNT>
struct Vector;
template <typename T>
struct Vector<T, 1>
{
T x;
};
template <typename T>
struct Vector<T, 2>
{
T x, y;
};
template <typename T>
struct Vector<T, 3>
{
T x, y, z;
};
template <typename T>
struct Vector<T, 4>
{
T x, y, z, w;
};
typedef Vector<float, 2> float2;
typedef Vector<float, 3> float3;
typedef Vector<float, 4> float4;
typedef Vector<int32_t, 2> int2;
typedef Vector<int32_t, 3> int3;
typedef Vector<int32_t, 4> int4;
typedef Vector<uint32_t, 2> uint2;
typedef Vector<uint32_t, 3> uint3;
typedef Vector<uint32_t, 4> uint4;
template <typename T, int ROWS, int COLS>
struct Matrix
{
Vector<T, COLS> rows[ROWS];
};
// We can just map `NonUniformResourceIndex` type directly to the index type on CPU, as CPU does not require
// any special handling around such accesses.
typedef size_t NonUniformResourceIndex;
// ----------------------------- ResourceType -----------------------------------------
// https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-structuredbuffer-getdimensions
// Missing Load(_In_ int Location, _Out_ uint Status);
template <typename T>
struct RWStructuredBuffer
{
SLANG_FORCE_INLINE T& operator[](size_t index) const { SLANG_PRELUDE_ASSERT(index < count); return data[index]; }
const T& Load(size_t index) const { SLANG_PRELUDE_ASSERT(index < count); return data[index]; }
void GetDimensions(uint32_t& outNumStructs, uint32_t& outStride) { outNumStructs = uint32_t(count); outStride = uint32_t(sizeof(T)); }
T* data;
size_t count;
};
template <typename T>
struct StructuredBuffer
{
SLANG_FORCE_INLINE const T& operator[](size_t index) const { SLANG_PRELUDE_ASSERT(index < count); return data[index]; }
const T& Load(size_t index) const { SLANG_PRELUDE_ASSERT(index < count); return data[index]; }
void GetDimensions(uint32_t& outNumStructs, uint32_t& outStride) { outNumStructs = uint32_t(count); outStride = uint32_t(sizeof(T)); }
T* data;
size_t count;
};
// Missing Load(_In_ int Location, _Out_ uint Status);
struct ByteAddressBuffer
{
void GetDimensions(uint32_t& outDim) const { outDim = uint32_t(sizeInBytes); }
uint32_t Load(size_t index) const
{
SLANG_PRELUDE_ASSERT(index + 4 <= sizeInBytes && (index & 3) == 0);
return data[index >> 2];
}
uint2 Load2(size_t index) const
{
SLANG_PRELUDE_ASSERT(index + 8 <= sizeInBytes && (index & 3) == 0);
const size_t dataIdx = index >> 2;
return uint2{data[dataIdx], data[dataIdx + 1]};
}
uint3 Load3(size_t index) const
{
SLANG_PRELUDE_ASSERT(index + 12 <= sizeInBytes && (index & 3) == 0);
const size_t dataIdx = index >> 2;
return uint3{data[dataIdx], data[dataIdx + 1], data[dataIdx + 2]};
}
uint4 Load4(size_t index) const
{
SLANG_PRELUDE_ASSERT(index + 16 <= sizeInBytes && (index & 3) == 0);
const size_t dataIdx = index >> 2;
return uint4{data[dataIdx], data[dataIdx + 1], data[dataIdx + 2], data[dataIdx + 3]};
}
const uint32_t* data;
size_t sizeInBytes; //< Must be multiple of 4
};
// https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-rwbyteaddressbuffer
// Missing support for Atomic operations
// Missing support for Load with status
struct RWByteAddressBuffer
{
void GetDimensions(uint32_t& outDim) const { outDim = uint32_t(sizeInBytes); }
uint32_t Load(size_t index) const
{
SLANG_PRELUDE_ASSERT(index + 4 <= sizeInBytes && (index & 3) == 0);
return data[index >> 2];
}
uint2 Load2(size_t index) const
{
SLANG_PRELUDE_ASSERT(index + 8 <= sizeInBytes && (index & 3) == 0);
const size_t dataIdx = index >> 2;
return uint2{data[dataIdx], data[dataIdx + 1]};
}
uint3 Load3(size_t index) const
{
SLANG_PRELUDE_ASSERT(index + 12 <= sizeInBytes && (index & 3) == 0);
const size_t dataIdx = index >> 2;
return uint3{data[dataIdx], data[dataIdx + 1], data[dataIdx + 2]};
}
uint4 Load4(size_t index) const
{
SLANG_PRELUDE_ASSERT(index + 16 <= sizeInBytes && (index & 3) == 0);
const size_t dataIdx = index >> 2;
return uint4{data[dataIdx], data[dataIdx + 1], data[dataIdx + 2], data[dataIdx + 3]};
}
void Store(size_t index, uint32_t v) const
{
SLANG_PRELUDE_ASSERT(index + 4 <= sizeInBytes && (index & 3) == 0);
data[index >> 2] = v;
}
void Store2(size_t index, uint2 v) const
{
SLANG_PRELUDE_ASSERT(index + 8 <= sizeInBytes && (index & 3) == 0);
const size_t dataIdx = index >> 2;
data[dataIdx + 0] = v.x;
data[dataIdx + 1] = v.y;
}
void Store3(size_t index, uint3 v) const
{
SLANG_PRELUDE_ASSERT(index + 12 <= sizeInBytes && (index & 3) == 0);
const size_t dataIdx = index >> 2;
data[dataIdx + 0] = v.x;
data[dataIdx + 1] = v.y;
data[dataIdx + 2] = v.z;
}
void Store4(size_t index, uint4 v) const
{
SLANG_PRELUDE_ASSERT(index + 16 <= sizeInBytes && (index & 3) == 0);
const size_t dataIdx = index >> 2;
data[dataIdx + 0] = v.x;
data[dataIdx + 1] = v.y;
data[dataIdx + 2] = v.z;
data[dataIdx + 3] = v.w;
}
uint32_t* data;
size_t sizeInBytes; //< Must be multiple of 4
};
struct ISamplerState;
struct ISamplerComparisonState;
struct SamplerState
{
ISamplerState* state;
};
struct SamplerComparisonState
{
ISamplerComparisonState* state;
};
// Texture
struct ITexture1D
{
virtual void Load(const int2& v, void* out) = 0;
virtual void Sample(SamplerState samplerState, float loc, void* out) = 0;
virtual void SampleLevel(SamplerState samplerState, float loc, float level, void* out) = 0;
};
template <typename T>
struct Texture1D
{
T Load(const int2& v) const { T out; texture->Load(v, &out); return out; }
T Sample(SamplerState samplerState, float v) const { T out; texture->Sample(samplerState, v, &out); return out; }
T SampleLevel(SamplerState samplerState, float v, float level) { T out; texture->SampleLevel(samplerState, v, level, &out); return out; }
ITexture1D* texture;
};
struct ITexture2D
{
virtual void Load(const int3& v, void* out) = 0;
virtual void Sample(SamplerState samplerState, const float2& loc, void* out) = 0;
virtual void SampleLevel(SamplerState samplerState, const float2& loc, float level, void* out) = 0;
};
template <typename T>
struct Texture2D
{
T Load(const int3& v) const { T out; texture->Load(v, &out); return out; }
T Sample(SamplerState samplerState, const float2& v) const { T out; texture->Sample(samplerState, v, &out); return out; }
T SampleLevel(SamplerState samplerState, const float2& v, float level) { T out; texture->SampleLevel(samplerState, v, level, &out); return out; }
ITexture2D* texture;
};
struct ITexture3D
{
virtual void Load(const int4& v, void* out) = 0;
virtual void Sample(SamplerState samplerState, const float3& loc, void* out) = 0;
virtual void SampleLevel(SamplerState samplerState, const float3& loc, float level, void* out) = 0;
};
template <typename T>
struct Texture3D
{
T Load(const int4& v) const { T out; texture->Load(v, &out); return out; }
T Sample(SamplerState samplerState, const float3& v) const { T out; texture->Sample(samplerState, v, &out); return out; }
T SampleLevel(SamplerState samplerState, const float3& v, float level) { T out; texture->SampleLevel(samplerState, v, level, &out); return out; }
ITexture3D* texture;
};
struct ITextureCube
{
virtual void Sample(SamplerState samplerState, const float3& loc, void* out) = 0;
virtual void SampleLevel(SamplerState samplerState, const float3& loc, float level, void* out) = 0;
};
template <typename T>
struct TextureCube
{
T Sample(SamplerState samplerState, const float3& v) const { T out; texture->Sample(samplerState, v, &out); return out; }
T SampleLevel(SamplerState samplerState, const float3& v, float level) { T out; texture->SampleLevel(samplerState, v, level, &out); return out; }
ITextureCube* texture;
};
struct ITexture1DArray
{
virtual void Load(const int3& v, void* out) = 0;
virtual void Sample(SamplerState samplerState, const float2& loc, void* out) = 0;
virtual void SampleLevel(SamplerState samplerState, const float2& loc, float level, void* out) = 0;
};
template <typename T>
struct Texture1DArray
{
T Load(const int3& v) const { T out; texture->Load(v, &out); return out; }
T Sample(SamplerState samplerState, const float2& v) const { T out; texture->Sample(samplerState, v, &out); return out; }
T SampleLevel(SamplerState samplerState, const float2& v, float level) { T out; texture->SampleLevel(samplerState, v, level, &out); return out; }
ITexture1DArray* texture;
};
struct ITexture2DArray
{
virtual void Load(const int4& v, void* out) = 0;
virtual void Sample(SamplerState samplerState, const float3& loc, void* out) = 0;
virtual void SampleLevel(SamplerState samplerState, const float3& loc, float level, void* out) = 0;
};
template <typename T>
struct Texture2DArray
{
T Load(const int4& v) const { T out; texture->Load(v, &out); return out; }
T Sample(SamplerState samplerState, const float3& v) const { T out; texture->Sample(samplerState, v, &out); return out; }
T SampleLevel(SamplerState samplerState, const float3& v, float level) { T out; texture->SampleLevel(samplerState, v, level, &out); return out; }
ITexture2DArray* texture;
};
struct ITextureCubeArray
{
virtual void Sample(SamplerState samplerState, const float4& loc, void* out) = 0;
virtual void SampleLevel(SamplerState samplerState, const float4& loc, float level, void* out) = 0;
};
template <typename T>
struct TextureCubeArray
{
T Sample(SamplerState samplerState, const float4& v) const { T out; texture->Sample(samplerState, v, &out); return out; }
T SampleLevel(SamplerState samplerState, const float4& v, float level) { T out; texture->SampleLevel(samplerState, v, level, &out); return out; }
ITextureCubeArray* texture;
};
/* !!!!!!!!!!!!!!!!!!!!!!!!!!! RWTexture !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
struct IRWTexture1D
{
virtual void Load(int32_t loc, void* out) = 0;
};
template <typename T>
struct RWTexture1D
{
T Load(int32_t loc) const { T out; texture->Load(loc, &out); return out; }
IRWTexture1D* texture;
};
/* Varying input for Compute */
/* Used when running a single thread */
struct ComputeThreadVaryingInput
{
uint3 groupID;
uint3 groupThreadID;
};
struct ComputeVaryingInput
{
uint3 startGroupID; ///< start groupID
uint3 endGroupID; ///< Non inclusive end groupID
};
/* Type that defines the uniform entry point params. The actual content of this type is dependent on the entry point parameters, and can be
found via reflection or defined such that it matches the shader appropriately.
*/
struct UniformEntryPointParams;
struct UniformState;
typedef void(*ComputeThreadFunc)(ComputeThreadVaryingInput* varyingInput, UniformEntryPointParams* uniformEntryPointParams, UniformState* uniformState);
typedef void(*ComputeFunc)(ComputeVaryingInput* varyingInput, UniformEntryPointParams* uniformEntryPointParams, UniformState* uniformState);
#ifdef SLANG_PRELUDE_NAMESPACE
}
#endif
#endif
|