forked from shader-slang/slang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslang-cpp-types.h
950 lines (832 loc) · 34.5 KB
/
slang-cpp-types.h
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
#ifndef SLANG_PRELUDE_CPP_TYPES_H
#define SLANG_PRELUDE_CPP_TYPES_H
#ifdef SLANG_PRELUDE_NAMESPACE
namespace SLANG_PRELUDE_NAMESPACE {
#endif
#ifndef SLANG_FORCE_INLINE
# define SLANG_FORCE_INLINE inline
#endif
#include "slang-cpp-types-core.h"
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;
// 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_BOUND_CHECK(index, count); return data[index]; }
const T& Load(size_t index) const { SLANG_BOUND_CHECK(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_BOUND_CHECK(index, count); return data[index]; }
const T& Load(size_t index) const { SLANG_BOUND_CHECK(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 RWBuffer
{
SLANG_FORCE_INLINE T& operator[](size_t index) const { SLANG_BOUND_CHECK(index, count); return data[index]; }
const T& Load(size_t index) const { SLANG_BOUND_CHECK(index, count); return data[index]; }
void GetDimensions(uint32_t* outCount) { *outCount = uint32_t(count); }
T* data;
size_t count;
};
template <typename T>
struct Buffer
{
SLANG_FORCE_INLINE const T& operator[](size_t index) const { SLANG_BOUND_CHECK(index, count); return data[index]; }
const T& Load(size_t index) const { SLANG_BOUND_CHECK(index, count); return data[index]; }
void GetDimensions(uint32_t* outCount) { *outCount = uint32_t(count); }
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_BOUND_CHECK_BYTE_ADDRESS(index, 4, sizeInBytes);
return data[index >> 2];
}
uint2 Load2(size_t index) const
{
SLANG_BOUND_CHECK_BYTE_ADDRESS(index, 8, sizeInBytes);
const size_t dataIdx = index >> 2;
return uint2{data[dataIdx], data[dataIdx + 1]};
}
uint3 Load3(size_t index) const
{
SLANG_BOUND_CHECK_BYTE_ADDRESS(index, 12, sizeInBytes);
const size_t dataIdx = index >> 2;
return uint3{data[dataIdx], data[dataIdx + 1], data[dataIdx + 2]};
}
uint4 Load4(size_t index) const
{
SLANG_BOUND_CHECK_BYTE_ADDRESS(index, 16, sizeInBytes);
const size_t dataIdx = index >> 2;
return uint4{data[dataIdx], data[dataIdx + 1], data[dataIdx + 2], data[dataIdx + 3]};
}
template<typename T>
T Load(size_t index) const
{
SLANG_BOUND_CHECK_BYTE_ADDRESS(index, sizeof(T), sizeInBytes);
return *(const T*)(((const char*)data) + index);
}
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_BOUND_CHECK_BYTE_ADDRESS(index, 4, sizeInBytes);
return data[index >> 2];
}
uint2 Load2(size_t index) const
{
SLANG_BOUND_CHECK_BYTE_ADDRESS(index, 8, sizeInBytes);
const size_t dataIdx = index >> 2;
return uint2{data[dataIdx], data[dataIdx + 1]};
}
uint3 Load3(size_t index) const
{
SLANG_BOUND_CHECK_BYTE_ADDRESS(index, 12, sizeInBytes);
const size_t dataIdx = index >> 2;
return uint3{data[dataIdx], data[dataIdx + 1], data[dataIdx + 2]};
}
uint4 Load4(size_t index) const
{
SLANG_BOUND_CHECK_BYTE_ADDRESS(index, 16, sizeInBytes);
const size_t dataIdx = index >> 2;
return uint4{data[dataIdx], data[dataIdx + 1], data[dataIdx + 2], data[dataIdx + 3]};
}
template<typename T>
T Load(size_t index) const
{
SLANG_BOUND_CHECK_BYTE_ADDRESS(index, sizeof(T), sizeInBytes);
return *(const T*)(((const char*)data) + index);
}
void Store(size_t index, uint32_t v) const
{
SLANG_BOUND_CHECK_BYTE_ADDRESS(index, 4, sizeInBytes);
data[index >> 2] = v;
}
void Store2(size_t index, uint2 v) const
{
SLANG_BOUND_CHECK_BYTE_ADDRESS(index, 8, sizeInBytes);
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_BOUND_CHECK_BYTE_ADDRESS(index, 12, sizeInBytes);
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_BOUND_CHECK_BYTE_ADDRESS(index, 16, sizeInBytes);
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;
}
template<typename T>
void Store(size_t index, T const& value) const
{
SLANG_BOUND_CHECK_BYTE_ADDRESS(index, sizeof(T), sizeInBytes);
*(T*)(((char*)data) + index) = value;
}
uint32_t* data;
size_t sizeInBytes; //< Must be multiple of 4
};
struct ISamplerState;
struct ISamplerComparisonState;
struct SamplerState
{
ISamplerState* state;
};
struct SamplerComparisonState
{
ISamplerComparisonState* state;
};
#ifndef SLANG_RESOURCE_SHAPE
# define SLANG_RESOURCE_SHAPE
typedef unsigned int SlangResourceShape;
enum
{
SLANG_RESOURCE_BASE_SHAPE_MASK = 0x0F,
SLANG_RESOURCE_NONE = 0x00,
SLANG_TEXTURE_1D = 0x01,
SLANG_TEXTURE_2D = 0x02,
SLANG_TEXTURE_3D = 0x03,
SLANG_TEXTURE_CUBE = 0x04,
SLANG_TEXTURE_BUFFER = 0x05,
SLANG_STRUCTURED_BUFFER = 0x06,
SLANG_BYTE_ADDRESS_BUFFER = 0x07,
SLANG_RESOURCE_UNKNOWN = 0x08,
SLANG_ACCELERATION_STRUCTURE = 0x09,
SLANG_RESOURCE_EXT_SHAPE_MASK = 0xF0,
SLANG_TEXTURE_FEEDBACK_FLAG = 0x10,
SLANG_TEXTURE_ARRAY_FLAG = 0x40,
SLANG_TEXTURE_MULTISAMPLE_FLAG = 0x80,
SLANG_TEXTURE_1D_ARRAY = SLANG_TEXTURE_1D | SLANG_TEXTURE_ARRAY_FLAG,
SLANG_TEXTURE_2D_ARRAY = SLANG_TEXTURE_2D | SLANG_TEXTURE_ARRAY_FLAG,
SLANG_TEXTURE_CUBE_ARRAY = SLANG_TEXTURE_CUBE | SLANG_TEXTURE_ARRAY_FLAG,
SLANG_TEXTURE_2D_MULTISAMPLE = SLANG_TEXTURE_2D | SLANG_TEXTURE_MULTISAMPLE_FLAG,
SLANG_TEXTURE_2D_MULTISAMPLE_ARRAY =
SLANG_TEXTURE_2D | SLANG_TEXTURE_MULTISAMPLE_FLAG | SLANG_TEXTURE_ARRAY_FLAG,
};
#endif
//
struct TextureDimensions
{
void reset()
{
shape = 0;
width = height = depth = 0;
numberOfLevels = 0;
arrayElementCount = 0;
}
int getDimSizes(uint32_t outDims[4]) const
{
const auto baseShape = (shape & SLANG_RESOURCE_BASE_SHAPE_MASK);
int count = 0;
switch (baseShape)
{
case SLANG_TEXTURE_1D:
{
outDims[count++] = width;
break;
}
case SLANG_TEXTURE_2D:
{
outDims[count++] = width;
outDims[count++] = height;
break;
}
case SLANG_TEXTURE_3D:
{
outDims[count++] = width;
outDims[count++] = height;
outDims[count++] = depth;
break;
}
case SLANG_TEXTURE_CUBE:
{
outDims[count++] = width;
outDims[count++] = height;
outDims[count++] = 6;
break;
}
}
if (shape & SLANG_TEXTURE_ARRAY_FLAG)
{
outDims[count++] = arrayElementCount;
}
return count;
}
int getMIPDims(int outDims[3]) const
{
const auto baseShape = (shape & SLANG_RESOURCE_BASE_SHAPE_MASK);
int count = 0;
switch (baseShape)
{
case SLANG_TEXTURE_1D:
{
outDims[count++] = width;
break;
}
case SLANG_TEXTURE_CUBE:
case SLANG_TEXTURE_2D:
{
outDims[count++] = width;
outDims[count++] = height;
break;
}
case SLANG_TEXTURE_3D:
{
outDims[count++] = width;
outDims[count++] = height;
outDims[count++] = depth;
break;
}
}
return count;
}
int calcMaxMIPLevels() const
{
int dims[3];
const int dimCount = getMIPDims(dims);
for (int count = 1; true; count++)
{
bool allOne = true;
for (int i = 0; i < dimCount; ++i)
{
if (dims[i] > 1)
{
allOne = false;
dims[i] >>= 1;
}
}
if (allOne)
{
return count;
}
}
}
uint32_t shape;
uint32_t width, height, depth;
uint32_t numberOfLevels;
uint32_t arrayElementCount; ///< For array types, 0 otherwise
};
// Texture
struct ITexture
{
virtual TextureDimensions GetDimensions(int mipLevel = -1) = 0;
virtual void Load(const int32_t* v, void* outData, size_t dataSize) = 0;
virtual void Sample(SamplerState samplerState, const float* loc, void* outData, size_t dataSize) = 0;
virtual void SampleLevel(SamplerState samplerState, const float* loc, float level, void* outData, size_t dataSize) = 0;
};
template <typename T>
struct Texture1D
{
void GetDimensions(uint32_t* outWidth) { *outWidth = texture->GetDimensions().width; }
void GetDimensions(uint32_t mipLevel, uint32_t* outWidth, uint32_t* outNumberOfLevels)
{
auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outNumberOfLevels = dims.numberOfLevels;
}
void GetDimensions(float* outWidth) { *outWidth = texture->GetDimensions().width; }
void GetDimensions(uint32_t mipLevel, float* outWidth, float* outNumberOfLevels)
{
auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outNumberOfLevels = dims.numberOfLevels;
}
T Load(const int2& loc) const { T out; texture->Load(&loc.x, &out, sizeof(out)); return out; }
T Sample(SamplerState samplerState, float loc) const { T out; texture->Sample(samplerState, &loc, &out, sizeof(out)); return out; }
T SampleLevel(SamplerState samplerState, float loc, float level) { T out; texture->SampleLevel(samplerState, &loc, level, &out, sizeof(out)); return out; }
ITexture* texture;
};
template <typename T>
struct Texture2D
{
void GetDimensions(uint32_t* outWidth, uint32_t* outHeight)
{
const auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
}
void GetDimensions(uint32_t mipLevel, uint32_t* outWidth, uint32_t* outHeight, uint32_t* outNumberOfLevels)
{
const auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outNumberOfLevels = dims.numberOfLevels;
}
void GetDimensions(float* outWidth, float* outHeight)
{
const auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
}
void GetDimensions(uint32_t mipLevel, float* outWidth, float* outHeight, float* outNumberOfLevels)
{
const auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outNumberOfLevels = dims.numberOfLevels;
}
T Load(const int3& loc) const { T out; texture->Load(&loc.x, &out, sizeof(out)); return out; }
T Sample(SamplerState samplerState, const float2& loc) const { T out; texture->Sample(samplerState, &loc.x, &out, sizeof(out)); return out; }
T SampleLevel(SamplerState samplerState, const float2& loc, float level) { T out; texture->SampleLevel(samplerState, &loc.x, level, &out, sizeof(out)); return out; }
ITexture* texture;
};
template <typename T>
struct Texture3D
{
void GetDimensions(uint32_t* outWidth, uint32_t* outHeight, uint32_t* outDepth)
{
const auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
*outDepth = dims.depth;
}
void GetDimensions(uint32_t mipLevel, uint32_t* outWidth, uint32_t* outHeight, uint32_t* outDepth, uint32_t* outNumberOfLevels)
{
const auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outDepth = dims.depth;
*outNumberOfLevels = dims.numberOfLevels;
}
void GetDimensions(float* outWidth, float* outHeight, float* outDepth)
{
const auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
*outDepth = dims.depth;
}
void GetDimensions(uint32_t mipLevel, float* outWidth, float* outHeight, float* outDepth, float* outNumberOfLevels)
{
const auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outDepth = dims.depth;
*outNumberOfLevels = dims.numberOfLevels;
}
T Load(const int4& loc) const { T out; texture->Load(&loc.x, &out, sizeof(out)); return out; }
T Sample(SamplerState samplerState, const float3& loc) const { T out; texture->Sample(samplerState, &loc.x, &out, sizeof(out)); return out; }
T SampleLevel(SamplerState samplerState, const float3& loc, float level) { T out; texture->SampleLevel(samplerState, &loc.x, level, &out, sizeof(out)); return out; }
ITexture* texture;
};
template <typename T>
struct TextureCube
{
void GetDimensions(uint32_t* outWidth, uint32_t* outHeight)
{
const auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
}
void GetDimensions(uint32_t mipLevel, uint32_t* outWidth, uint32_t* outHeight, uint32_t* outNumberOfLevels)
{
const auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outNumberOfLevels = dims.numberOfLevels;
}
void GetDimensions(float* outWidth, float* outHeight)
{
const auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
}
void GetDimensions(uint32_t mipLevel, float* outWidth, float* outHeight, float* outNumberOfLevels)
{
const auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outNumberOfLevels = dims.numberOfLevels;
}
T Sample(SamplerState samplerState, const float3& loc) const { T out; texture->Sample(samplerState, &loc.x, &out, sizeof(out)); return out; }
T SampleLevel(SamplerState samplerState, const float3& loc, float level) { T out; texture->SampleLevel(samplerState, &loc.x, level, &out, sizeof(out)); return out; }
ITexture* texture;
};
template <typename T>
struct Texture1DArray
{
void GetDimensions(uint32_t* outWidth, uint32_t* outElements) { auto dims = texture->GetDimensions(); *outWidth = dims.width; *outElements = dims.arrayElementCount; }
void GetDimensions(uint32_t mipLevel, uint32_t* outWidth, uint32_t* outElements, uint32_t* outNumberOfLevels)
{
auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outNumberOfLevels = dims.numberOfLevels;
*outElements = dims.arrayElementCount;
}
void GetDimensions(float* outWidth, float* outElements) { auto dims = texture->GetDimensions(); *outWidth = dims.width; *outElements = dims.arrayElementCount; }
void GetDimensions(uint32_t mipLevel, float* outWidth, float* outElements, float* outNumberOfLevels)
{
auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outNumberOfLevels = dims.numberOfLevels;
*outElements = dims.arrayElementCount;
}
T Load(const int3& loc) const { T out; texture->Load(&loc.x, &out, sizeof(out)); return out; }
T Sample(SamplerState samplerState, const float2& loc) const { T out; texture->Sample(samplerState, &loc.x, &out, sizeof(out)); return out; }
T SampleLevel(SamplerState samplerState, const float2& loc, float level) { T out; texture->SampleLevel(samplerState, &loc.x, level, &out, sizeof(out)); return out; }
ITexture* texture;
};
template <typename T>
struct Texture2DArray
{
void GetDimensions(uint32_t* outWidth, uint32_t* outHeight, uint32_t* outElements)
{
auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
*outElements = dims.arrayElementCount;
}
void GetDimensions(uint32_t mipLevel, uint32_t* outWidth, uint32_t* outHeight, uint32_t* outElements, uint32_t* outNumberOfLevels)
{
auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outElements = dims.arrayElementCount;
*outNumberOfLevels = dims.numberOfLevels;
}
void GetDimensions(uint32_t* outWidth, float* outHeight, float* outElements)
{
auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
*outElements = dims.arrayElementCount;
}
void GetDimensions(uint32_t mipLevel, float* outWidth, float* outHeight, float* outElements, float* outNumberOfLevels)
{
auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outElements = dims.arrayElementCount;
*outNumberOfLevels = dims.numberOfLevels;
}
T Load(const int4& loc) const { T out; texture->Load(&loc.x, &out, sizeof(out)); return out; }
T Sample(SamplerState samplerState, const float3& loc) const { T out; texture->Sample(samplerState, &loc.x, &out, sizeof(out)); return out; }
T SampleLevel(SamplerState samplerState, const float3& loc, float level) { T out; texture->SampleLevel(samplerState, &loc.x, level, &out, sizeof(out)); return out; }
ITexture* texture;
};
template <typename T>
struct TextureCubeArray
{
void GetDimensions(uint32_t* outWidth, uint32_t* outHeight, uint32_t* outElements)
{
auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
*outElements = dims.arrayElementCount;
}
void GetDimensions(uint32_t mipLevel, uint32_t* outWidth, uint32_t* outHeight, uint32_t* outElements, uint32_t* outNumberOfLevels)
{
auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outElements = dims.arrayElementCount;
*outNumberOfLevels = dims.numberOfLevels;
}
void GetDimensions(uint32_t* outWidth, float* outHeight, float* outElements)
{
auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
*outElements = dims.arrayElementCount;
}
void GetDimensions(uint32_t mipLevel, float* outWidth, float* outHeight, float* outElements, float* outNumberOfLevels)
{
auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outElements = dims.arrayElementCount;
*outNumberOfLevels = dims.numberOfLevels;
}
T Sample(SamplerState samplerState, const float4& loc) const { T out; texture->Sample(samplerState, &loc.x, &out, sizeof(out)); return out; }
T SampleLevel(SamplerState samplerState, const float4& loc, float level) { T out; texture->SampleLevel(samplerState, &loc.x, level, &out, sizeof(out)); return out; }
ITexture* texture;
};
/* !!!!!!!!!!!!!!!!!!!!!!!!!!! RWTexture !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
struct IRWTexture : ITexture
{
/// Get the reference to the element at loc.
virtual void* refAt(const uint32_t* loc) = 0;
};
template <typename T>
struct RWTexture1D
{
void GetDimensions(uint32_t* outWidth) { *outWidth = texture->GetDimensions().width; }
void GetDimensions(uint32_t mipLevel, uint32_t* outWidth, uint32_t* outNumberOfLevels) { auto dims = texture->GetDimensions(mipLevel); *outWidth = dims.width; *outNumberOfLevels = dims.numberOfLevels; }
void GetDimensions(float* outWidth) { *outWidth = texture->GetDimensions().width; }
void GetDimensions(uint32_t mipLevel, float* outWidth, float* outNumberOfLevels) { auto dims = texture->GetDimensions(mipLevel); *outWidth = dims.width; *outNumberOfLevels = dims.numberOfLevels; }
T Load(int32_t loc) const { T out; texture->Load(&loc, &out, sizeof(out)); return out; }
T& operator[](uint32_t loc) { return *(T*)texture->refAt(&loc); }
IRWTexture* texture;
};
template <typename T>
struct RWTexture2D
{
void GetDimensions(uint32_t* outWidth, uint32_t* outHeight)
{
const auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
}
void GetDimensions(uint32_t mipLevel, uint32_t* outWidth, uint32_t* outHeight, uint32_t* outNumberOfLevels)
{
const auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outNumberOfLevels = dims.numberOfLevels;
}
void GetDimensions(float* outWidth, float* outHeight)
{
const auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
}
void GetDimensions(uint32_t mipLevel, float* outWidth, float* outHeight, float* outNumberOfLevels)
{
const auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outNumberOfLevels = dims.numberOfLevels;
}
T Load(const int2& loc) const { T out; texture->Load(&loc.x, &out, sizeof(out)); return out; }
T& operator[](const uint2& loc) { return *(T*)texture->refAt(&loc.x); }
IRWTexture* texture;
};
template <typename T>
struct RWTexture3D
{
void GetDimensions(uint32_t* outWidth, uint32_t* outHeight, uint32_t* outDepth)
{
const auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
*outDepth = dims.depth;
}
void GetDimensions(uint32_t mipLevel, uint32_t* outWidth, uint32_t* outHeight, uint32_t* outDepth, uint32_t* outNumberOfLevels)
{
const auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outDepth = dims.depth;
*outNumberOfLevels = dims.numberOfLevels;
}
void GetDimensions(float* outWidth, float* outHeight, float* outDepth)
{
const auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
*outDepth = dims.depth;
}
void GetDimensions(uint32_t mipLevel, float* outWidth, float* outHeight, float* outDepth, float* outNumberOfLevels)
{
const auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outDepth = dims.depth;
*outNumberOfLevels = dims.numberOfLevels;
}
T Load(const int3& loc) const { T out; texture->Load(&loc.x, &out, sizeof(out)); return out; }
T& operator[](const uint3& loc) { return *(T*)texture->refAt(&loc.x); }
IRWTexture* texture;
};
template <typename T>
struct RWTexture1DArray
{
void GetDimensions(uint32_t* outWidth, uint32_t* outElements)
{
auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outElements = dims.arrayElementCount;
}
void GetDimensions(uint32_t mipLevel, uint32_t* outWidth, uint32_t* outElements, uint32_t* outNumberOfLevels)
{
const auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outElements = dims.arrayElementCount;
*outNumberOfLevels = dims.numberOfLevels;
}
void GetDimensions(float* outWidth, float* outElements)
{
auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outElements = dims.arrayElementCount;
}
void GetDimensions(uint32_t mipLevel, float* outWidth, float* outElements, float* outNumberOfLevels)
{
const auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outElements = dims.arrayElementCount;
*outNumberOfLevels = dims.numberOfLevels;
}
T Load(int2 loc) const { T out; texture->Load(&loc.x, &out, sizeof(out)); return out; }
T& operator[](uint2 loc) { return *(T*)texture->refAt(&loc.x); }
IRWTexture* texture;
};
template <typename T>
struct RWTexture2DArray
{
void GetDimensions(uint32_t* outWidth, uint32_t* outHeight, uint32_t* outElements)
{
auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
*outElements = dims.arrayElementCount;
}
void GetDimensions(uint32_t mipLevel, uint32_t* outWidth, uint32_t* outHeight, uint32_t* outElements, uint32_t* outNumberOfLevels)
{
const auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outElements = dims.arrayElementCount;
*outNumberOfLevels = dims.numberOfLevels;
}
void GetDimensions(float* outWidth, float* outHeight, float* outElements)
{
auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
*outElements = dims.arrayElementCount;
}
void GetDimensions(uint32_t mipLevel, float* outWidth, float* outHeight, float* outElements, float* outNumberOfLevels)
{
const auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outElements = dims.arrayElementCount;
*outNumberOfLevels = dims.numberOfLevels;
}
T Load(const int3& loc) const { T out; texture->Load(&loc.x, &out, sizeof(out)); return out; }
T& operator[](const uint3& loc) { return *(T*)texture->refAt(&loc.x); }
IRWTexture* texture;
};
// FeedbackTexture
struct FeedbackType {};
struct SAMPLER_FEEDBACK_MIN_MIP : FeedbackType {};
struct SAMPLER_FEEDBACK_MIP_REGION_USED : FeedbackType {};
struct IFeedbackTexture
{
virtual TextureDimensions GetDimensions(int mipLevel = -1) = 0;
// Note here we pass the optional clamp parameter as a pointer. Passing nullptr means no clamp.
// This was preferred over having two function definitions, and having to differentiate their names
virtual void WriteSamplerFeedback(ITexture* tex, SamplerState samp, const float* location, const float* clamp = nullptr) = 0;
virtual void WriteSamplerFeedbackBias(ITexture* tex, SamplerState samp, const float* location, float bias, const float* clamp = nullptr) = 0;
virtual void WriteSamplerFeedbackGrad(ITexture* tex, SamplerState samp, const float* location, const float* ddx, const float* ddy, const float* clamp = nullptr) = 0;
virtual void WriteSamplerFeedbackLevel(ITexture* tex, SamplerState samp, const float* location, float lod) = 0;
};
template <typename T>
struct FeedbackTexture2D
{
void GetDimensions(uint32_t* outWidth, uint32_t* outHeight)
{
const auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
}
void GetDimensions(uint32_t mipLevel, uint32_t* outWidth, uint32_t* outHeight, uint32_t* outNumberOfLevels)
{
const auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outNumberOfLevels = dims.numberOfLevels;
}
void GetDimensions(float* outWidth, float* outHeight)
{
const auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
}
void GetDimensions(uint32_t mipLevel, float* outWidth, float* outHeight, float* outNumberOfLevels)
{
const auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outNumberOfLevels = dims.numberOfLevels;
}
template <typename S>
void WriteSamplerFeedback(Texture2D<S> tex, SamplerState samp, float2 location, float clamp) { texture->WriteSamplerFeedback(tex.texture, samp, &location.x, &clamp); }
template <typename S>
void WriteSamplerFeedbackBias(Texture2D<S> tex, SamplerState samp, float2 location, float bias, float clamp) { texture->WriteSamplerFeedbackBias(tex.texture, samp, &location.x, bias, &clamp); }
template <typename S>
void WriteSamplerFeedbackGrad(Texture2D<S> tex, SamplerState samp, float2 location, float2 ddx, float2 ddy, float clamp) { texture->WriteSamplerFeedbackGrad(tex.texture, samp, &location.x, &ddx.x, &ddy.x, &clamp); }
// Level
template <typename S>
void WriteSamplerFeedbackLevel(Texture2D<S> tex, SamplerState samp, float2 location, float lod) { texture->WriteSamplerFeedbackLevel(tex.texture, samp, &location.x, lod); }
// Without Clamp
template <typename S>
void WriteSamplerFeedback(Texture2D<S> tex, SamplerState samp, float2 location) { texture->WriteSamplerFeedback(tex.texture, samp, &location.x); }
template <typename S>
void WriteSamplerFeedbackBias(Texture2D<S> tex, SamplerState samp, float2 location, float bias) { texture->WriteSamplerFeedbackBias(tex.texture, samp, &location.x, bias); }
template <typename S>
void WriteSamplerFeedbackGrad(Texture2D<S> tex, SamplerState samp, float2 location, float2 ddx, float2 ddy) { texture->WriteSamplerFeedbackGrad(tex.texture, samp, &location.x, &ddx.x, &ddy.x); }
IFeedbackTexture* texture;
};
template <typename T>
struct FeedbackTexture2DArray
{
void GetDimensions(uint32_t* outWidth, uint32_t* outHeight, uint32_t* outElements)
{
auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
*outElements = dims.arrayElementCount;
}
void GetDimensions(uint32_t mipLevel, uint32_t* outWidth, uint32_t* outHeight, uint32_t* outElements, uint32_t* outNumberOfLevels)
{
const auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outElements = dims.arrayElementCount;
*outNumberOfLevels = dims.numberOfLevels;
}
void GetDimensions(float* outWidth, float* outHeight, float* outElements)
{
auto dims = texture->GetDimensions();
*outWidth = dims.width;
*outHeight = dims.height;
*outElements = dims.arrayElementCount;
}
void GetDimensions(uint32_t mipLevel, float* outWidth, float* outHeight, float* outElements, float* outNumberOfLevels)
{
const auto dims = texture->GetDimensions(mipLevel);
*outWidth = dims.width;
*outHeight = dims.height;
*outElements = dims.arrayElementCount;
*outNumberOfLevels = dims.numberOfLevels;
}
template <typename S>
void WriteSamplerFeedback(Texture2DArray<S> texArray, SamplerState samp, float3 location, float clamp) { texture->WriteSamplerFeedback(texArray.texture, samp, &location.x, &clamp); }
template <typename S>
void WriteSamplerFeedbackBias(Texture2DArray<S> texArray, SamplerState samp, float3 location, float bias, float clamp) { texture->WriteSamplerFeedbackBias(texArray.texture, samp, &location.x, bias, &clamp); }
template <typename S>
void WriteSamplerFeedbackGrad(Texture2DArray<S> texArray, SamplerState samp, float3 location, float3 ddx, float3 ddy, float clamp) { texture->WriteSamplerFeedbackGrad(texArray.texture, samp, &location.x, &ddx.x, &ddy.x, &clamp); }
// Level
template <typename S>
void WriteSamplerFeedbackLevel(Texture2DArray<S> texArray, SamplerState samp, float3 location, float lod) { texture->WriteSamplerFeedbackLevel(texArray.texture, samp, &location.x, lod); }
// Without Clamp
template <typename S>
void WriteSamplerFeedback(Texture2DArray<S> texArray, SamplerState samp, float3 location) { texture->WriteSamplerFeedback(texArray.texture, samp, &location.x); }
template <typename S>
void WriteSamplerFeedbackBias(Texture2DArray<S> texArray, SamplerState samp, float3 location, float bias) { texture->WriteSamplerFeedbackBias(texArray.texture, samp, &location.x, bias); }
template <typename S>
void WriteSamplerFeedbackGrad(Texture2DArray<S> texArray, SamplerState samp, float3 location, float3 ddx, float3 ddy) { texture->WriteSamplerFeedbackGrad(texArray.texture, samp, &location.x, &ddx.x, &ddy.x); }
IFeedbackTexture* 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
};
// The uniformEntryPointParams and uniformState must be set to structures that match layout that the kernel expects.
// This can be determined via reflection for example.
typedef void(*ComputeThreadFunc)(ComputeThreadVaryingInput* varyingInput, void* uniformEntryPointParams, void* uniformState);
typedef void(*ComputeFunc)(ComputeVaryingInput* varyingInput, void* uniformEntryPointParams, void* uniformState);
#ifdef SLANG_PRELUDE_NAMESPACE
}
#endif
#endif