forked from shader-slang/slang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slang-gfx.h
2206 lines (1929 loc) · 67.4 KB
/
slang-gfx.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
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// render.h
#pragma once
#include <float.h>
#include <assert.h>
#include "slang.h"
#include "slang-com-ptr.h"
#if defined(SLANG_GFX_DYNAMIC)
# if defined(_MSC_VER)
# ifdef SLANG_GFX_DYNAMIC_EXPORT
# define SLANG_GFX_API SLANG_DLL_EXPORT
# else
# define SLANG_GFX_API __declspec(dllimport)
# endif
# else
// TODO: need to consider compiler capabilities
//# ifdef SLANG_DYNAMIC_EXPORT
# define SLANG_GFX_API SLANG_DLL_EXPORT
//# endif
# endif
#endif
#ifndef SLANG_GFX_API
# define SLANG_GFX_API
#endif
// Needed for building on cygwin with gcc
#undef Always
#undef None
namespace gfx {
using Slang::ComPtr;
typedef SlangResult Result;
// Had to move here, because Options needs types defined here
typedef SlangInt Int;
typedef SlangUInt UInt;
typedef uint64_t DeviceAddress;
const uint64_t kTimeoutInfinite = 0xFFFFFFFFFFFFFFFF;
// Declare opaque type
class IInputLayout: public ISlangUnknown
{
};
#define SLANG_UUID_IInputLayout \
{ \
0x45223711, 0xa84b, 0x455c, { 0xbe, 0xfa, 0x49, 0x37, 0x42, 0x1e, 0x8e, 0x2e } \
}
enum class StageType
{
Unknown,
Vertex,
Hull,
Domain,
Geometry,
Fragment,
Compute,
RayGeneration,
Intersection,
AnyHit,
ClosestHit,
Miss,
Callable,
Amplification,
Mesh,
CountOf,
};
enum class DeviceType
{
Unknown,
Default,
DirectX11,
DirectX12,
OpenGl,
Vulkan,
CPU,
CUDA,
CountOf,
};
enum class ProjectionStyle
{
Unknown,
OpenGl,
DirectX,
Vulkan,
CountOf,
};
/// The style of the binding
enum class BindingStyle
{
Unknown,
DirectX,
OpenGl,
Vulkan,
CPU,
CUDA,
CountOf,
};
class ITransientResourceHeap;
class IShaderProgram: public ISlangUnknown
{
public:
struct Desc
{
slang::IComponentType* slangProgram;
};
};
#define SLANG_UUID_IShaderProgram \
{ \
0x9d32d0ad, 0x915c, 0x4ffd, { 0x91, 0xe2, 0x50, 0x85, 0x54, 0xa0, 0x4a, 0x76 } \
}
// Dont' change without keeping in sync with Format
#define GFX_FORMAT(x) \
x( Unknown, 0, 0) \
\
x(R32G32B32A32_TYPELESS, 16, 1) \
x(R32G32B32_TYPELESS, 12, 1) \
x(R32G32_TYPELESS, 8, 1) \
x(R32_TYPELESS, 4, 1) \
\
x(R16G16B16A16_TYPELESS, 8, 1) \
x(R16G16_TYPELESS, 4, 1) \
x(R16_TYPELESS, 2, 1) \
\
x(R8G8B8A8_TYPELESS, 4, 1) \
x(R8G8_TYPELESS, 2, 1) \
x(R8_TYPELESS, 1, 1) \
x(B8G8R8A8_TYPELESS, 4, 1) \
\
x(R32G32B32A32_FLOAT, 16, 1) \
x(R32G32B32_FLOAT, 12, 1) \
x(R32G32_FLOAT, 8, 1) \
x(R32_FLOAT, 4, 1) \
\
x(R16G16B16A16_FLOAT, 8, 1) \
x(R16G16_FLOAT, 4, 1) \
x(R16_FLOAT, 2, 1) \
\
x(R32G32B32A32_UINT, 16, 1) \
x(R32G32B32_UINT, 12, 1) \
x(R32G32_UINT, 8, 1) \
x(R32_UINT, 4, 1) \
\
x(R16G16B16A16_UINT, 8, 1) \
x(R16G16_UINT, 4, 1) \
x(R16_UINT, 2, 1) \
\
x(R8G8B8A8_UINT, 4, 1) \
x(R8G8_UINT, 2, 1) \
x(R8_UINT, 1, 1) \
\
x(R32G32B32A32_SINT, 16, 1) \
x(R32G32B32_SINT, 12, 1) \
x(R32G32_SINT, 8, 1) \
x(R32_SINT, 4, 1) \
\
x(R16G16B16A16_SINT, 8, 1) \
x(R16G16_SINT, 4, 1) \
x(R16_SINT, 2, 1) \
\
x(R8G8B8A8_SINT, 4, 1) \
x(R8G8_SINT, 2, 1) \
x(R8_SINT, 1, 1) \
\
x(R16G16B16A16_UNORM, 8, 1) \
x(R16G16_UNORM, 4, 1) \
x(R16_UNORM, 2, 1) \
\
x(R8G8B8A8_UNORM, 4, 1) \
x(R8G8B8A8_UNORM_SRGB, 4, 1) \
x(R8G8_UNORM, 2, 1) \
x(R8_UNORM, 1, 1) \
x(B8G8R8A8_UNORM, 4, 1) \
x(B8G8R8A8_UNORM_SRGB, 4, 1) \
\
x(R16G16B16A16_SNORM, 8, 1) \
x(R16G16_SNORM, 4, 1) \
x(R16_SNORM, 2, 1) \
\
x(R8G8B8A8_SNORM, 4, 1) \
x(R8G8_SNORM, 2, 1) \
x(R8_SNORM, 1, 1) \
\
x(D32_FLOAT, 4, 1) \
x(D16_UNORM, 2, 1) \
\
x(B4G4R4A4_UNORM, 2, 1) \
x(B5G6R5_UNORM, 2, 1) \
x(B5G5R5A1_UNORM, 2, 1) \
\
x(R9G9B9E5_SHAREDEXP, 4, 1) \
x(R10G10B10A2_TYPELESS, 4, 1) \
x(R10G10B10A2_UNORM, 4, 1) \
x(R10G10B10A2_UINT, 4, 1) \
x(R11G11B10_FLOAT, 4, 1) \
\
x(BC1_UNORM, 8, 16) \
x(BC1_UNORM_SRGB, 8, 16) \
x(BC2_UNORM, 16, 16) \
x(BC2_UNORM_SRGB, 16, 16) \
x(BC3_UNORM, 16, 16) \
x(BC3_UNORM_SRGB, 16, 16) \
x(BC4_UNORM, 8, 16) \
x(BC4_SNORM, 8, 16) \
x(BC5_UNORM, 16, 16) \
x(BC5_SNORM, 16, 16) \
x(BC6H_UF16, 16, 16) \
x(BC6H_SF16, 16, 16) \
x(BC7_UNORM, 16, 16) \
x(BC7_UNORM_SRGB, 16, 16)
/// Different formats of things like pixels or elements of vertices
/// NOTE! Any change to this type (adding, removing, changing order) - must also be reflected in changes GFX_FORMAT
enum class Format
{
// D3D formats omitted: 19-22, 44-47, 65-66, 68-70, 73, 76, 79, 82, 88-89, 92-94, 97, 100-114
// These formats are omitted due to lack of a corresponding Vulkan format. D24_UNORM_S8_UINT (DXGI_FORMAT 45)
// has a matching Vulkan format but is also omitted as it is only supported by Nvidia.
Unknown,
R32G32B32A32_TYPELESS,
R32G32B32_TYPELESS,
R32G32_TYPELESS,
R32_TYPELESS,
R16G16B16A16_TYPELESS,
R16G16_TYPELESS,
R16_TYPELESS,
R8G8B8A8_TYPELESS,
R8G8_TYPELESS,
R8_TYPELESS,
B8G8R8A8_TYPELESS,
R32G32B32A32_FLOAT,
R32G32B32_FLOAT,
R32G32_FLOAT,
R32_FLOAT,
R16G16B16A16_FLOAT,
R16G16_FLOAT,
R16_FLOAT,
R32G32B32A32_UINT,
R32G32B32_UINT,
R32G32_UINT,
R32_UINT,
R16G16B16A16_UINT,
R16G16_UINT,
R16_UINT,
R8G8B8A8_UINT,
R8G8_UINT,
R8_UINT,
R32G32B32A32_SINT,
R32G32B32_SINT,
R32G32_SINT,
R32_SINT,
R16G16B16A16_SINT,
R16G16_SINT,
R16_SINT,
R8G8B8A8_SINT,
R8G8_SINT,
R8_SINT,
R16G16B16A16_UNORM,
R16G16_UNORM,
R16_UNORM,
R8G8B8A8_UNORM,
R8G8B8A8_UNORM_SRGB,
R8G8_UNORM,
R8_UNORM,
B8G8R8A8_UNORM,
B8G8R8A8_UNORM_SRGB,
R16G16B16A16_SNORM,
R16G16_SNORM,
R16_SNORM,
R8G8B8A8_SNORM,
R8G8_SNORM,
R8_SNORM,
D32_FLOAT,
D16_UNORM,
B4G4R4A4_UNORM,
B5G6R5_UNORM,
B5G5R5A1_UNORM,
R9G9B9E5_SHAREDEXP,
R10G10B10A2_TYPELESS,
R10G10B10A2_UNORM,
R10G10B10A2_UINT,
R11G11B10_FLOAT,
BC1_UNORM,
BC1_UNORM_SRGB,
BC2_UNORM,
BC2_UNORM_SRGB,
BC3_UNORM,
BC3_UNORM_SRGB,
BC4_UNORM,
BC4_SNORM,
BC5_UNORM,
BC5_SNORM,
BC6H_UF16,
BC6H_SF16,
BC7_UNORM,
BC7_UNORM_SRGB,
CountOf,
};
struct FormatInfo
{
uint8_t channelCount; ///< The amount of channels in the format. Only set if the channelType is set
uint8_t channelType; ///< One of SlangScalarType None if type isn't made up of elements of type.
uint32_t blockSizeInBytes; ///< The size of a block in bytes.
uint32_t pixelsPerBlock; ///< The number of pixels contained in a block.
uint32_t blockWidth;
uint32_t blockHeight;
};
enum class InputSlotClass
{
PerVertex, PerInstance
};
struct InputElementDesc
{
char const* semanticName;
UInt semanticIndex;
Format format;
UInt offset;
InputSlotClass slotClass;
UInt bufferSlotIndex;
UInt instanceDataStepRate;
};
enum class PrimitiveType
{
Point, Line, Triangle, Patch
};
enum class PrimitiveTopology
{
TriangleList,
};
enum class ResourceState
{
Undefined,
General,
PreInitialized,
VertexBuffer,
IndexBuffer,
ConstantBuffer,
StreamOutput,
ShaderResource,
UnorderedAccess,
RenderTarget,
DepthRead,
DepthWrite,
Present,
IndirectArgument,
CopySource,
CopyDestination,
ResolveSource,
ResolveDestination,
AccelerationStructure,
_Count
};
struct ResourceStateSet
{
public:
void add(ResourceState state) { m_bitFields |= (1LL << (uint32_t)state); }
template <typename... TResourceState> void add(ResourceState s, TResourceState... states)
{
add(s);
add(states...);
}
bool contains(ResourceState state) const { return (m_bitFields & (1LL << (uint32_t)state)) != 0; }
ResourceStateSet()
: m_bitFields(0)
{}
ResourceStateSet(const ResourceStateSet& other) = default;
ResourceStateSet(ResourceState state) { add(state); }
template <typename... TResourceState> ResourceStateSet(TResourceState... states)
{
add(states...);
}
private:
uint64_t m_bitFields = 0;
void add() {}
};
/// Combinations describe how a resource can be accessed (typically by the host/cpu)
struct AccessFlag
{
enum Enum
{
Read = 0x1,
Write = 0x2
};
};
enum class InteropHandleAPI
{
Unknown,
D3D12,
Vulkan,
CUDA,
Win32,
FileDescriptor,
};
struct InteropHandle
{
InteropHandleAPI api = InteropHandleAPI::Unknown;
uint64_t handleValue = 0;
};
class IResource: public ISlangUnknown
{
public:
/// The type of resource.
/// NOTE! The order needs to be such that all texture types are at or after Texture1D (otherwise isTexture won't work correctly)
enum class Type
{
Unknown, ///< Unknown
Buffer, ///< A buffer (like a constant/index/vertex buffer)
Texture1D, ///< A 1d texture
Texture2D, ///< A 2d texture
Texture3D, ///< A 3d texture
TextureCube, ///< A cubemap consists of 6 Texture2D like faces
CountOf,
};
/// Base class for Descs
struct DescBase
{
bool hasCpuAccessFlag(AccessFlag::Enum accessFlag) { return (cpuAccessFlags & accessFlag) != 0; }
Type type = Type::Unknown;
ResourceState defaultState = ResourceState::Undefined;
ResourceStateSet allowedStates = ResourceStateSet();
int cpuAccessFlags = 0; ///< Combination of Resource::AccessFlag
InteropHandle existingHandle = {};
bool isShared = false;
};
virtual SLANG_NO_THROW Type SLANG_MCALL getType() = 0;
virtual SLANG_NO_THROW Result SLANG_MCALL getNativeResourceHandle(InteropHandle* outHandle) = 0;
virtual SLANG_NO_THROW Result SLANG_MCALL getSharedHandle(InteropHandle* outHandle) = 0;
virtual SLANG_NO_THROW Result SLANG_MCALL setDebugName(const char* name) = 0;
virtual SLANG_NO_THROW const char* SLANG_MCALL getDebugName() = 0;
};
#define SLANG_UUID_IResource \
{ \
0xa0e39f34, 0x8398, 0x4522, { 0x95, 0xc2, 0xeb, 0xc0, 0xf9, 0x84, 0xef, 0x3f } \
}
class IBufferResource: public IResource
{
public:
struct Desc: public DescBase
{
size_t sizeInBytes = 0; ///< Total size in bytes
int elementSize = 0; ///< Get the element stride. If > 0, this is a structured buffer
Format format = Format::Unknown;
};
virtual SLANG_NO_THROW Desc* SLANG_MCALL getDesc() = 0;
virtual SLANG_NO_THROW DeviceAddress SLANG_MCALL getDeviceAddress() = 0;
};
#define SLANG_UUID_IBufferResource \
{ \
0x1b274efe, 0x5e37, 0x492b, { 0x82, 0x6e, 0x7e, 0xe7, 0xe8, 0xf5, 0xa4, 0x9b } \
}
struct DepthStencilClearValue
{
float depth = 1.0f;
uint32_t stencil = 0;
};
union ColorClearValue
{
float floatValues[4];
uint32_t uintValues[4];
};
struct ClearValue
{
ColorClearValue color = {{0.0f, 0.0f, 0.0f, 0.0f}};
DepthStencilClearValue depthStencil;
};
struct BufferRange
{
uint32_t firstElement;
uint32_t elementCount;
};
enum class TextureAspect : uint32_t
{
Default = 0,
Color = 0x00000001,
Depth = 0x00000002,
Stencil = 0x00000004,
MetaData = 0x00000008,
Plane0 = 0x00000010,
Plane1 = 0x00000020,
Plane2 = 0x00000040,
};
struct SubresourceRange
{
TextureAspect aspectMask;
uint32_t mipLevel;
uint32_t mipLevelCount;
uint32_t baseArrayLayer; // For Texture3D, this is WSlice.
uint32_t layerCount; // For cube maps, this is a multiple of 6.
};
class ITextureResource: public IResource
{
public:
static const uint32_t kTexturePitchAlignment = 256;
static const uint32_t kRemainingTextureSize = 0xFFFFFFFF;
struct Offset3D
{
uint32_t x = 0;
uint32_t y = 0;
uint32_t z = 0;
Offset3D() = default;
Offset3D(uint32_t _x, uint32_t _y, uint32_t _z) :x(_x), y(_y), z(_z) {}
};
struct SampleDesc
{
int numSamples = 1; ///< Number of samples per pixel
int quality = 0; ///< The quality measure for the samples
};
struct Size
{
int width = 0; ///< Width in pixels
int height = 0; ///< Height in pixels (if 2d or 3d)
int depth = 0; ///< Depth (if 3d)
};
struct Desc: public DescBase
{
Size size;
int arraySize = 0; ///< Array size
int numMipLevels = 0; ///< Number of mip levels - if 0 will create all mip levels
Format format; ///< The resources format
SampleDesc sampleDesc; ///< How the resource is sampled
ClearValue optimalClearValue;
};
/// Data for a single subresource of a texture.
///
/// Each subresource is a tensor with `1 <= rank <= 3`,
/// where the rank is deterined by the base shape of the
/// texture (Buffer, 1D, 2D, 3D, or Cube). For the common
/// case of a 2D texture, `rank == 2` and each subresource
/// is a 2D image.
///
/// Subresource tensors must be stored in a row-major layout,
/// so that the X axis strides over texels, the Y axis strides
/// over 1D rows of texels, and the Z axis strides over 2D
/// "layers" of texels.
///
/// For a texture with multiple mip levels or array elements,
/// each mip level and array element is stores as a distinct
/// subresource. When indexing into an array of subresources,
/// the index of a subresoruce for mip level `m` and array
/// index `a` is `m + a*mipLevelCount`.
///
struct SubresourceData
{
/// Pointer to texel data for the subresource tensor.
void const* data;
/// Stride in bytes between rows of the subresource tensor.
///
/// This is the number of bytes to add to a pointer to a texel
/// at (X,Y,Z) to get to a texel at (X,Y+1,Z).
///
/// Devices may not support all possible values for `strideY`.
/// In particular, they may only support strictly positive strides.
///
int64_t strideY;
/// Stride in bytes between layers of the subresource tensor.
///
/// This is the number of bytes to add to a pointer to a texel
/// at (X,Y,Z) to get to a texel at (X,Y,Z+1).
///
/// Devices may not support all possible values for `strideZ`.
/// In particular, they may only support strictly positive strides.
///
int64_t strideZ;
};
virtual SLANG_NO_THROW Desc* SLANG_MCALL getDesc() = 0;
};
#define SLANG_UUID_ITextureResource \
{ \
0xcf88a31c, 0x6187, 0x46c5, { 0xa4, 0xb7, 0xeb, 0x58, 0xc7, 0x33, 0x40, 0x17 } \
}
enum class ComparisonFunc : uint8_t
{
Never = 0,
Less = 0x01,
Equal = 0x02,
LessEqual = 0x03,
Greater = 0x04,
NotEqual = 0x05,
GreaterEqual = 0x06,
Always = 0x07,
};
enum class TextureFilteringMode
{
Point,
Linear,
};
enum class TextureAddressingMode
{
Wrap,
ClampToEdge,
ClampToBorder,
MirrorRepeat,
MirrorOnce,
};
enum class TextureReductionOp
{
Average,
Comparison,
Minimum,
Maximum,
};
class ISamplerState : public ISlangUnknown
{
public:
struct Desc
{
TextureFilteringMode minFilter = TextureFilteringMode::Linear;
TextureFilteringMode magFilter = TextureFilteringMode::Linear;
TextureFilteringMode mipFilter = TextureFilteringMode::Linear;
TextureReductionOp reductionOp = TextureReductionOp::Average;
TextureAddressingMode addressU = TextureAddressingMode::Wrap;
TextureAddressingMode addressV = TextureAddressingMode::Wrap;
TextureAddressingMode addressW = TextureAddressingMode::Wrap;
float mipLODBias = 0.0f;
uint32_t maxAnisotropy = 1;
ComparisonFunc comparisonFunc = ComparisonFunc::Never;
float borderColor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
float minLOD = -FLT_MAX;
float maxLOD = FLT_MAX;
};
};
#define SLANG_UUID_ISamplerState \
{ \
0x8b8055df, 0x9377, 0x401d, { 0x91, 0xff, 0x3f, 0xa3, 0xbf, 0x66, 0x64, 0xf4 } \
}
class IResourceView : public ISlangUnknown
{
public:
enum class Type
{
Unknown,
RenderTarget,
DepthStencil,
ShaderResource,
UnorderedAccess,
AccelerationStructure,
};
struct RenderTargetDesc
{
// The resource shape of this render target view.
IResource::Type shape;
uint32_t mipSlice;
uint32_t arrayIndex;
uint32_t arraySize;
uint32_t planeIndex;
};
struct Desc
{
Type type;
Format format;
// Fields for `RenderTarget` and `DepthStencil` views.
RenderTargetDesc renderTarget;
SubresourceRange subresourceRange;
BufferRange bufferRange;
};
virtual SLANG_NO_THROW Desc* SLANG_MCALL getViewDesc() = 0;
};
#define SLANG_UUID_IResourceView \
{ \
0x7b6c4926, 0x884, 0x408c, { 0xad, 0x8a, 0x50, 0x3a, 0x8e, 0x23, 0x98, 0xa4 } \
}
class IAccelerationStructure : public IResourceView
{
public:
enum class Kind
{
TopLevel,
BottomLevel
};
struct BuildFlags
{
// The enum values are intentionally consistent with
// D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS.
enum Enum
{
None,
AllowUpdate = 1,
AllowCompaction = 2,
PreferFastTrace = 4,
PreferFastBuild = 8,
MinimizeMemory = 16,
PerformUpdate = 32
};
};
enum class GeometryType
{
Triangles, ProcedurePrimitives
};
struct GeometryFlags
{
// The enum values are intentionally consistent with
// D3D12_RAYTRACING_GEOMETRY_FLAGS.
enum Enum
{
None,
Opaque = 1,
NoDuplicateAnyHitInvocation = 2
};
};
struct TriangleDesc
{
DeviceAddress transform3x4;
Format indexFormat;
Format vertexFormat;
uint32_t indexCount;
uint32_t vertexCount;
DeviceAddress indexData;
DeviceAddress vertexData;
uint64_t vertexStride;
};
struct ProceduralAABB
{
float minX;
float minY;
float minZ;
float maxX;
float maxY;
float maxZ;
};
struct ProceduralAABBDesc
{
/// Number of AABBs.
uint64_t count;
/// Pointer to an array of `ProceduralAABB` values in device memory.
DeviceAddress data;
/// Stride in bytes of the AABB values array.
uint64_t stride;
};
struct GeometryDesc
{
GeometryType type;
GeometryFlags::Enum flags;
union
{
TriangleDesc triangles;
ProceduralAABBDesc proceduralAABBs;
} content;
};
struct GeometryInstanceFlags
{
// The enum values are kept consistent with D3D12_RAYTRACING_INSTANCE_FLAGS
// and VkGeometryInstanceFlagBitsKHR.
enum Enum : uint32_t
{
None = 0,
TriangleFacingCullDisable = 0x00000001,
TriangleFrontCounterClockwise = 0x00000002,
ForceOpaque = 0x00000004,
NoOpaque = 0x00000008
};
};
// The layout of this struct is intentionally consistent with D3D12_RAYTRACING_INSTANCE_DESC
// and VkAccelerationStructureInstanceKHR.
struct InstanceDesc
{
float transform[3][4];
uint32_t instanceID : 24;
uint32_t instanceMask : 8;
uint32_t instanceContributionToHitGroupIndex : 24;
GeometryInstanceFlags::Enum flags : 8;
DeviceAddress accelerationStructure;
};
struct PrebuildInfo
{
uint64_t resultDataMaxSize;
uint64_t scratchDataSize;
uint64_t updateScratchDataSize;
};
struct BuildInputs
{
Kind kind;
BuildFlags::Enum flags;
int32_t descCount;
/// Array of `InstanceDesc` values in device memory.
/// Used when `kind` is `TopLevel`.
DeviceAddress instanceDescs;
/// Array of `GeometryDesc` values.
/// Used when `kind` is `BottomLevel`.
const GeometryDesc* geometryDescs;
};
struct CreateDesc
{
Kind kind;
IBufferResource* buffer;
uint64_t offset;
uint64_t size;
};
struct BuildDesc
{
BuildInputs inputs;
IAccelerationStructure* source;
IAccelerationStructure* dest;
DeviceAddress scratchData;
};
virtual SLANG_NO_THROW DeviceAddress SLANG_MCALL getDeviceAddress() = 0;
};
#define SLANG_UUID_IAccelerationStructure \
{ \
0xa5cdda3c, 0x1d4e, 0x4df7, { 0x8e, 0xf2, 0xb7, 0x3f, 0xce, 0x4, 0xde, 0x3b } \
}
class IFence : public ISlangUnknown
{
public:
struct Desc
{
uint64_t initialValue = 0;
bool isShared = false;
};
/// Returns the currently signaled value on the device.
virtual SLANG_NO_THROW Result SLANG_MCALL getCurrentValue(uint64_t* outValue) = 0;
/// Signals the fence from the host with the specified value.
virtual SLANG_NO_THROW Result SLANG_MCALL setCurrentValue(uint64_t value) = 0;
virtual SLANG_NO_THROW Result SLANG_MCALL getSharedHandle(InteropHandle* outHandle) = 0;
virtual SLANG_NO_THROW Result SLANG_MCALL getNativeHandle(InteropHandle* outNativeHandle) = 0;
};
#define SLANG_UUID_IFence \
{ \
0x7fe1c283, 0xd3f4, 0x48ed, { 0xaa, 0xf3, 0x1, 0x51, 0x96, 0x4e, 0x7c, 0xb5 } \
}
struct ShaderOffset
{
SlangInt uniformOffset = 0;
SlangInt bindingRangeIndex = 0;
SlangInt bindingArrayIndex = 0;
uint32_t getHashCode() const
{
return (uint32_t)(((bindingRangeIndex << 20) + bindingArrayIndex) ^ uniformOffset);
}
bool operator==(const ShaderOffset& other) const
{
return uniformOffset == other.uniformOffset
&& bindingRangeIndex == other.bindingRangeIndex
&& bindingArrayIndex == other.bindingArrayIndex;
}
bool operator!=(const ShaderOffset& other) const
{
return !this->operator==(other);
}
bool operator<(const ShaderOffset& other) const
{
if (bindingRangeIndex < other.bindingRangeIndex)
return true;
if (bindingRangeIndex > other.bindingRangeIndex)
return false;
if (bindingArrayIndex < other.bindingArrayIndex)
return true;
if (bindingArrayIndex > other.bindingArrayIndex)
return false;
return uniformOffset < other.uniformOffset;
}
bool operator<=(const ShaderOffset& other) const { return (*this == other) || (*this) < other; }
bool operator>(const ShaderOffset& other) const { return other < *this; }
bool operator>=(const ShaderOffset& other) const { return other <= *this; }
};
enum class ShaderObjectContainerType
{
None, Array, StructuredBuffer
};
class IShaderObject : public ISlangUnknown
{
public:
SLANG_NO_THROW ComPtr<IShaderObject> SLANG_MCALL getObject(ShaderOffset const& offset)
{
ComPtr<IShaderObject> object = nullptr;
SLANG_RETURN_NULL_ON_FAIL(getObject(offset, object.writeRef()));
return object;
}
virtual SLANG_NO_THROW slang::TypeLayoutReflection* SLANG_MCALL getElementTypeLayout() = 0;
virtual SLANG_NO_THROW ShaderObjectContainerType SLANG_MCALL getContainerType() = 0;
virtual SLANG_NO_THROW UInt SLANG_MCALL getEntryPointCount() = 0;
ComPtr<IShaderObject> getEntryPoint(UInt index)
{
ComPtr<IShaderObject> entryPoint = nullptr;
SLANG_RETURN_NULL_ON_FAIL(getEntryPoint(index, entryPoint.writeRef()));
return entryPoint;
}
virtual SLANG_NO_THROW Result SLANG_MCALL
getEntryPoint(UInt index, IShaderObject** entryPoint) = 0;
virtual SLANG_NO_THROW Result SLANG_MCALL
setData(ShaderOffset const& offset, void const* data, size_t size) = 0;
virtual SLANG_NO_THROW Result SLANG_MCALL
getObject(ShaderOffset const& offset, IShaderObject** object) = 0;
virtual SLANG_NO_THROW Result SLANG_MCALL
setObject(ShaderOffset const& offset, IShaderObject* object) = 0;
virtual SLANG_NO_THROW Result SLANG_MCALL
setResource(ShaderOffset const& offset, IResourceView* resourceView) = 0;
virtual SLANG_NO_THROW Result SLANG_MCALL
setSampler(ShaderOffset const& offset, ISamplerState* sampler) = 0;
virtual SLANG_NO_THROW Result SLANG_MCALL setCombinedTextureSampler(
ShaderOffset const& offset, IResourceView* textureView, ISamplerState* sampler) = 0;