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
|
// reflection.cpp
#include "reflection.h"
#include "compiler.h"
#include "type-layout.h"
#include <assert.h>
// Don't signal errors for stuff we don't implement here,
// and instead just try to return things defensively
//
// Slang developers can switch this when debugging.
#define SLANG_REFLECTION_UNEXPECTED() do {} while(0)
// Implementation to back public-facing reflection API
using namespace Slang;
// Conversion routines to help with strongly-typed reflection API
static inline Type* convert(SlangReflectionType* type)
{
return (Type*) type;
}
static inline SlangReflectionType* convert(Type* type)
{
return (SlangReflectionType*) type;
}
static inline TypeLayout* convert(SlangReflectionTypeLayout* type)
{
return (TypeLayout*) type;
}
static inline SlangReflectionTypeLayout* convert(TypeLayout* type)
{
return (SlangReflectionTypeLayout*) type;
}
static inline VarDeclBase* convert(SlangReflectionVariable* var)
{
return (VarDeclBase*) var;
}
static inline SlangReflectionVariable* convert(VarDeclBase* var)
{
return (SlangReflectionVariable*) var;
}
static inline VarLayout* convert(SlangReflectionVariableLayout* var)
{
return (VarLayout*) var;
}
static inline SlangReflectionVariableLayout* convert(VarLayout* var)
{
return (SlangReflectionVariableLayout*) var;
}
static inline EntryPointLayout* convert(SlangReflectionEntryPoint* entryPoint)
{
return (EntryPointLayout*) entryPoint;
}
static inline SlangReflectionEntryPoint* convert(EntryPointLayout* entryPoint)
{
return (SlangReflectionEntryPoint*) entryPoint;
}
static inline ProgramLayout* convert(SlangReflection* program)
{
return (ProgramLayout*) program;
}
static inline SlangReflection* convert(ProgramLayout* program)
{
return (SlangReflection*) program;
}
// type Reflection
SLANG_API SlangTypeKind spReflectionType_GetKind(SlangReflectionType* inType)
{
auto type = convert(inType);
if(!type) return SLANG_TYPE_KIND_NONE;
// TODO(tfoley: Don't emit the same type more than once...
if (auto basicType = type->As<BasicExpressionType>())
{
return SLANG_TYPE_KIND_SCALAR;
}
else if (auto vectorType = type->As<VectorExpressionType>())
{
return SLANG_TYPE_KIND_VECTOR;
}
else if (auto matrixType = type->As<MatrixExpressionType>())
{
return SLANG_TYPE_KIND_MATRIX;
}
else if (auto constantBufferType = type->As<ConstantBufferType>())
{
return SLANG_TYPE_KIND_CONSTANT_BUFFER;
}
else if (type->As<TextureBufferType>())
{
return SLANG_TYPE_KIND_TEXTURE_BUFFER;
}
else if (type->As<GLSLShaderStorageBufferType>())
{
return SLANG_TYPE_KIND_SHADER_STORAGE_BUFFER;
}
else if (auto samplerStateType = type->As<SamplerStateType>())
{
return SLANG_TYPE_KIND_SAMPLER_STATE;
}
else if (auto textureType = type->As<TextureTypeBase>())
{
return SLANG_TYPE_KIND_RESOURCE;
}
// TODO: need a better way to handle this stuff...
#define CASE(TYPE) \
else if(type->As<TYPE>()) do { \
return SLANG_TYPE_KIND_RESOURCE; \
} while(0)
CASE(HLSLStructuredBufferType);
CASE(HLSLRWStructuredBufferType);
CASE(HLSLAppendStructuredBufferType);
CASE(HLSLConsumeStructuredBufferType);
CASE(HLSLByteAddressBufferType);
CASE(HLSLRWByteAddressBufferType);
CASE(UntypedBufferResourceType);
#undef CASE
else if (auto arrayType = type->As<ArrayExpressionType>())
{
return SLANG_TYPE_KIND_ARRAY;
}
else if( auto declRefType = type->As<DeclRefType>() )
{
auto declRef = declRefType->declRef;
if( auto structDeclRef = declRef.As<StructDecl>() )
{
return SLANG_TYPE_KIND_STRUCT;
}
}
else if (auto errorType = type->As<ErrorType>())
{
// This means we saw a type we didn't understand in the user's code
return SLANG_TYPE_KIND_NONE;
}
SLANG_REFLECTION_UNEXPECTED();
return SLANG_TYPE_KIND_NONE;
}
SLANG_API unsigned int spReflectionType_GetFieldCount(SlangReflectionType* inType)
{
auto type = convert(inType);
if(!type) return 0;
// TODO: maybe filter based on kind
if(auto declRefType = dynamic_cast<DeclRefType*>(type))
{
auto declRef = declRefType->declRef;
if( auto structDeclRef = declRef.As<StructDecl>())
{
return GetFields(structDeclRef).Count();
}
}
return 0;
}
SLANG_API SlangReflectionVariable* spReflectionType_GetFieldByIndex(SlangReflectionType* inType, unsigned index)
{
auto type = convert(inType);
if(!type) return nullptr;
// TODO: maybe filter based on kind
if(auto declRefType = dynamic_cast<DeclRefType*>(type))
{
auto declRef = declRefType->declRef;
if( auto structDeclRef = declRef.As<StructDecl>())
{
auto fieldDeclRef = GetFields(structDeclRef).ToArray()[index];
return (SlangReflectionVariable*) fieldDeclRef.getDecl();
}
}
return nullptr;
}
SLANG_API size_t spReflectionType_GetElementCount(SlangReflectionType* inType)
{
auto type = convert(inType);
if(!type) return 0;
if(auto arrayType = dynamic_cast<ArrayExpressionType*>(type))
{
return arrayType->ArrayLength ? (size_t) GetIntVal(arrayType->ArrayLength) : 0;
}
else if( auto vectorType = dynamic_cast<VectorExpressionType*>(type))
{
return (size_t) GetIntVal(vectorType->elementCount);
}
return 0;
}
SLANG_API SlangReflectionType* spReflectionType_GetElementType(SlangReflectionType* inType)
{
auto type = convert(inType);
if(!type) return nullptr;
if(auto arrayType = dynamic_cast<ArrayExpressionType*>(type))
{
return (SlangReflectionType*) arrayType->baseType.Ptr();
}
else if( auto constantBufferType = dynamic_cast<ConstantBufferType*>(type))
{
return convert(constantBufferType->elementType.Ptr());
}
else if( auto vectorType = dynamic_cast<VectorExpressionType*>(type))
{
return convert(vectorType->elementType.Ptr());
}
else if( auto matrixType = dynamic_cast<MatrixExpressionType*>(type))
{
return convert(matrixType->getElementType());
}
return nullptr;
}
SLANG_API unsigned int spReflectionType_GetRowCount(SlangReflectionType* inType)
{
auto type = convert(inType);
if(!type) return 0;
if(auto matrixType = dynamic_cast<MatrixExpressionType*>(type))
{
return (unsigned int) GetIntVal(matrixType->getRowCount());
}
else if(auto vectorType = dynamic_cast<VectorExpressionType*>(type))
{
return 1;
}
else if( auto basicType = dynamic_cast<BasicExpressionType*>(type) )
{
return 1;
}
return 0;
}
SLANG_API unsigned int spReflectionType_GetColumnCount(SlangReflectionType* inType)
{
auto type = convert(inType);
if(!type) return 0;
if(auto matrixType = dynamic_cast<MatrixExpressionType*>(type))
{
return (unsigned int) GetIntVal(matrixType->getColumnCount());
}
else if(auto vectorType = dynamic_cast<VectorExpressionType*>(type))
{
return (unsigned int) GetIntVal(vectorType->elementCount);
}
else if( auto basicType = dynamic_cast<BasicExpressionType*>(type) )
{
return 1;
}
return 0;
}
SLANG_API SlangScalarType spReflectionType_GetScalarType(SlangReflectionType* inType)
{
auto type = convert(inType);
if(!type) return 0;
if(auto matrixType = dynamic_cast<MatrixExpressionType*>(type))
{
type = matrixType->getElementType();
}
else if(auto vectorType = dynamic_cast<VectorExpressionType*>(type))
{
type = vectorType->elementType.Ptr();
}
if(auto basicType = dynamic_cast<BasicExpressionType*>(type))
{
switch (basicType->baseType)
{
#define CASE(BASE, TAG) \
case BaseType::BASE: return SLANG_SCALAR_TYPE_##TAG
CASE(Void, VOID);
CASE(Int, INT32);
CASE(Float, FLOAT32);
CASE(UInt, UINT32);
CASE(Bool, BOOL);
CASE(UInt64, UINT64);
#undef CASE
default:
SLANG_REFLECTION_UNEXPECTED();
return SLANG_SCALAR_TYPE_NONE;
break;
}
}
return SLANG_SCALAR_TYPE_NONE;
}
SLANG_API SlangResourceShape spReflectionType_GetResourceShape(SlangReflectionType* inType)
{
auto type = convert(inType);
if(!type) return 0;
while(auto arrayType = type->As<ArrayExpressionType>())
{
type = arrayType->baseType.Ptr();
}
if(auto textureType = type->As<TextureTypeBase>())
{
return textureType->getShape();
}
// TODO: need a better way to handle this stuff...
#define CASE(TYPE, SHAPE, ACCESS) \
else if(type->As<TYPE>()) do { \
return SHAPE; \
} while(0)
CASE(HLSLStructuredBufferType, SLANG_STRUCTURED_BUFFER, SLANG_RESOURCE_ACCESS_READ);
CASE(HLSLRWStructuredBufferType, SLANG_STRUCTURED_BUFFER, SLANG_RESOURCE_ACCESS_READ_WRITE);
CASE(HLSLAppendStructuredBufferType, SLANG_STRUCTURED_BUFFER, SLANG_RESOURCE_ACCESS_APPEND);
CASE(HLSLConsumeStructuredBufferType, SLANG_STRUCTURED_BUFFER, SLANG_RESOURCE_ACCESS_CONSUME);
CASE(HLSLByteAddressBufferType, SLANG_BYTE_ADDRESS_BUFFER, SLANG_RESOURCE_ACCESS_READ);
CASE(HLSLRWByteAddressBufferType, SLANG_BYTE_ADDRESS_BUFFER, SLANG_RESOURCE_ACCESS_READ_WRITE);
CASE(UntypedBufferResourceType, SLANG_BYTE_ADDRESS_BUFFER, SLANG_RESOURCE_ACCESS_READ);
#undef CASE
return SLANG_RESOURCE_NONE;
}
SLANG_API SlangResourceAccess spReflectionType_GetResourceAccess(SlangReflectionType* inType)
{
auto type = convert(inType);
if(!type) return 0;
while(auto arrayType = type->As<ArrayExpressionType>())
{
type = arrayType->baseType.Ptr();
}
if(auto textureType = type->As<TextureTypeBase>())
{
return textureType->getAccess();
}
// TODO: need a better way to handle this stuff...
#define CASE(TYPE, SHAPE, ACCESS) \
else if(type->As<TYPE>()) do { \
return ACCESS; \
} while(0)
CASE(HLSLStructuredBufferType, SLANG_STRUCTURED_BUFFER, SLANG_RESOURCE_ACCESS_READ);
CASE(HLSLRWStructuredBufferType, SLANG_STRUCTURED_BUFFER, SLANG_RESOURCE_ACCESS_READ_WRITE);
CASE(HLSLAppendStructuredBufferType, SLANG_STRUCTURED_BUFFER, SLANG_RESOURCE_ACCESS_APPEND);
CASE(HLSLConsumeStructuredBufferType, SLANG_STRUCTURED_BUFFER, SLANG_RESOURCE_ACCESS_CONSUME);
CASE(HLSLByteAddressBufferType, SLANG_BYTE_ADDRESS_BUFFER, SLANG_RESOURCE_ACCESS_READ);
CASE(HLSLRWByteAddressBufferType, SLANG_BYTE_ADDRESS_BUFFER, SLANG_RESOURCE_ACCESS_READ_WRITE);
CASE(UntypedBufferResourceType, SLANG_BYTE_ADDRESS_BUFFER, SLANG_RESOURCE_ACCESS_READ);
// This isn't entirely accurate, but I can live with it for now
CASE(GLSLShaderStorageBufferType, SLANG_STRUCTURED_BUFFER, SLANG_RESOURCE_ACCESS_READ_WRITE);
#undef CASE
return SLANG_RESOURCE_ACCESS_NONE;
}
SLANG_API SlangReflectionType* spReflectionType_GetResourceResultType(SlangReflectionType* inType)
{
auto type = convert(inType);
if(!type) return nullptr;
while(auto arrayType = type->As<ArrayExpressionType>())
{
type = arrayType->baseType.Ptr();
}
if (auto textureType = type->As<TextureTypeBase>())
{
return convert(textureType->elementType.Ptr());
}
// TODO: need a better way to handle this stuff...
#define CASE(TYPE, SHAPE, ACCESS) \
else if(type->As<TYPE>()) do { \
return convert(type->As<TYPE>()->elementType.Ptr()); \
} while(0)
// TODO: structured buffer needs to expose type layout!
CASE(HLSLStructuredBufferType, SLANG_STRUCTURED_BUFFER, SLANG_RESOURCE_ACCESS_READ);
CASE(HLSLRWStructuredBufferType, SLANG_STRUCTURED_BUFFER, SLANG_RESOURCE_ACCESS_READ_WRITE);
CASE(HLSLAppendStructuredBufferType, SLANG_STRUCTURED_BUFFER, SLANG_RESOURCE_ACCESS_APPEND);
CASE(HLSLConsumeStructuredBufferType, SLANG_STRUCTURED_BUFFER, SLANG_RESOURCE_ACCESS_CONSUME);
#undef CASE
return nullptr;
}
// type Layout Reflection
SLANG_API SlangReflectionType* spReflectionTypeLayout_GetType(SlangReflectionTypeLayout* inTypeLayout)
{
auto typeLayout = convert(inTypeLayout);
if(!typeLayout) return nullptr;
return (SlangReflectionType*) typeLayout->type.Ptr();
}
SLANG_API size_t spReflectionTypeLayout_GetSize(SlangReflectionTypeLayout* inTypeLayout, SlangParameterCategory category)
{
auto typeLayout = convert(inTypeLayout);
if(!typeLayout) return 0;
auto info = typeLayout->FindResourceInfo(LayoutResourceKind(category));
if(!info) return 0;
return info->count;
}
SLANG_API SlangReflectionVariableLayout* spReflectionTypeLayout_GetFieldByIndex(SlangReflectionTypeLayout* inTypeLayout, unsigned index)
{
auto typeLayout = convert(inTypeLayout);
if(!typeLayout) return nullptr;
if(auto structTypeLayout = dynamic_cast<StructTypeLayout*>(typeLayout))
{
return (SlangReflectionVariableLayout*) structTypeLayout->fields[index].Ptr();
}
return nullptr;
}
SLANG_API size_t spReflectionTypeLayout_GetElementStride(SlangReflectionTypeLayout* inTypeLayout, SlangParameterCategory category)
{
auto typeLayout = convert(inTypeLayout);
if(!typeLayout) return 0;
if( auto arrayTypeLayout = dynamic_cast<ArrayTypeLayout*>(typeLayout))
{
switch (category)
{
// We store the stride explictly for the uniform case
case SLANG_PARAMETER_CATEGORY_UNIFORM:
return arrayTypeLayout->uniformStride;
// For most other cases (resource registers), the "stride"
// of an array is simply the number of resources (if any)
// consumed by its element type.
default:
{
auto elementTypeLayout = arrayTypeLayout->elementTypeLayout;
auto info = elementTypeLayout->FindResourceInfo(LayoutResourceKind(category));
if(!info) return 0;
return info->count;
}
// An import special case, though, is Vulkan descriptor-table slots,
// where an entire array will use a single `binding`, so that the
// effective stride is zero:
case SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT:
return 0;
}
}
return 0;
}
SLANG_API SlangReflectionTypeLayout* spReflectionTypeLayout_GetElementTypeLayout(SlangReflectionTypeLayout* inTypeLayout)
{
auto typeLayout = convert(inTypeLayout);
if(!typeLayout) return nullptr;
if( auto arrayTypeLayout = dynamic_cast<ArrayTypeLayout*>(typeLayout))
{
return (SlangReflectionTypeLayout*) arrayTypeLayout->elementTypeLayout.Ptr();
}
else if( auto constantBufferTypeLayout = dynamic_cast<ParameterBlockTypeLayout*>(typeLayout))
{
return convert(constantBufferTypeLayout->elementTypeLayout.Ptr());
}
else if( auto structuredBufferTypeLayout = dynamic_cast<StructuredBufferTypeLayout*>(typeLayout))
{
return convert(structuredBufferTypeLayout->elementTypeLayout.Ptr());
}
return nullptr;
}
static SlangParameterCategory getParameterCategory(
LayoutResourceKind kind)
{
return SlangParameterCategory(kind);
}
static SlangParameterCategory getParameterCategory(
TypeLayout* typeLayout)
{
auto resourceInfoCount = typeLayout->resourceInfos.Count();
if(resourceInfoCount == 1)
{
return getParameterCategory(typeLayout->resourceInfos[0].kind);
}
else if(resourceInfoCount == 0)
{
// TODO: can this ever happen?
return SLANG_PARAMETER_CATEGORY_NONE;
}
return SLANG_PARAMETER_CATEGORY_MIXED;
}
SLANG_API SlangParameterCategory spReflectionTypeLayout_GetParameterCategory(SlangReflectionTypeLayout* inTypeLayout)
{
auto typeLayout = convert(inTypeLayout);
if(!typeLayout) return SLANG_PARAMETER_CATEGORY_NONE;
return getParameterCategory(typeLayout);
}
SLANG_API unsigned spReflectionTypeLayout_GetCategoryCount(SlangReflectionTypeLayout* inTypeLayout)
{
auto typeLayout = convert(inTypeLayout);
if(!typeLayout) return 0;
return (unsigned) typeLayout->resourceInfos.Count();
}
SLANG_API SlangParameterCategory spReflectionTypeLayout_GetCategoryByIndex(SlangReflectionTypeLayout* inTypeLayout, unsigned index)
{
auto typeLayout = convert(inTypeLayout);
if(!typeLayout) return SLANG_PARAMETER_CATEGORY_NONE;
return typeLayout->resourceInfos[index].kind;
}
// Variable Reflection
SLANG_API char const* spReflectionVariable_GetName(SlangReflectionVariable* inVar)
{
auto var = convert(inVar);
if(!var) return nullptr;
// If the variable is one that has an "external" name that is supposed
// to be exposed for reflection, then report it here
if(auto reflectionNameMod = var->FindModifier<ParameterBlockReflectionName>())
return getText(reflectionNameMod->nameAndLoc.name).Buffer();
return getText(var->getName()).Buffer();
}
SLANG_API SlangReflectionType* spReflectionVariable_GetType(SlangReflectionVariable* inVar)
{
auto var = convert(inVar);
if(!var) return nullptr;
return convert(var->getType());
}
// Variable Layout Reflection
SLANG_API SlangReflectionVariable* spReflectionVariableLayout_GetVariable(SlangReflectionVariableLayout* inVarLayout)
{
auto varLayout = convert(inVarLayout);
if(!varLayout) return nullptr;
return convert(varLayout->varDecl.getDecl());
}
SLANG_API SlangReflectionTypeLayout* spReflectionVariableLayout_GetTypeLayout(SlangReflectionVariableLayout* inVarLayout)
{
auto varLayout = convert(inVarLayout);
if(!varLayout) return nullptr;
return convert(varLayout->getTypeLayout());
}
SLANG_API size_t spReflectionVariableLayout_GetOffset(SlangReflectionVariableLayout* inVarLayout, SlangParameterCategory category)
{
auto varLayout = convert(inVarLayout);
if(!varLayout) return 0;
auto info = varLayout->FindResourceInfo(LayoutResourceKind(category));
if(!info) return 0;
return info->index;
}
SLANG_API size_t spReflectionVariableLayout_GetSpace(SlangReflectionVariableLayout* inVarLayout, SlangParameterCategory category)
{
auto varLayout = convert(inVarLayout);
if(!varLayout) return 0;
auto info = varLayout->FindResourceInfo(LayoutResourceKind(category));
if(!info) return 0;
return info->space;
}
// Shader Parameter Reflection
SLANG_API unsigned spReflectionParameter_GetBindingIndex(SlangReflectionParameter* inVarLayout)
{
auto varLayout = convert(inVarLayout);
if(!varLayout) return 0;
if(varLayout->resourceInfos.Count() > 0)
{
return (unsigned) varLayout->resourceInfos[0].index;
}
return 0;
}
SLANG_API unsigned spReflectionParameter_GetBindingSpace(SlangReflectionParameter* inVarLayout)
{
auto varLayout = convert(inVarLayout);
if(!varLayout) return 0;
if(varLayout->resourceInfos.Count() > 0)
{
return (unsigned) varLayout->resourceInfos[0].space;
}
return 0;
}
// Helpers for getting parameter count
namespace Slang
{
static unsigned getParameterCount(RefPtr<TypeLayout> typeLayout)
{
if(auto parameterBlockLayout = typeLayout.As<ParameterBlockTypeLayout>())
{
typeLayout = parameterBlockLayout->elementTypeLayout;
}
if(auto structLayout = typeLayout.As<StructTypeLayout>())
{
return (unsigned) structLayout->fields.Count();
}
return 0;
}
static VarLayout* getParameterByIndex(RefPtr<TypeLayout> typeLayout, unsigned index)
{
if(auto parameterBlockLayout = typeLayout.As<ParameterBlockTypeLayout>())
{
typeLayout = parameterBlockLayout->elementTypeLayout;
}
if(auto structLayout = typeLayout.As<StructTypeLayout>())
{
return structLayout->fields[index];
}
return 0;
}
}
// Entry Point Reflection
SLANG_API char const* spReflectionEntryPoint_getName(
SlangReflectionEntryPoint* inEntryPoint)
{
auto entryPointLayout = convert(inEntryPoint);
if(!entryPointLayout) return 0;
return getText(entryPointLayout->entryPoint->getName()).begin();
}
SLANG_API unsigned spReflectionEntryPoint_getParameterCount(
SlangReflectionEntryPoint* inEntryPoint)
{
auto entryPointLayout = convert(inEntryPoint);
if(!entryPointLayout) return 0;
return getParameterCount(entryPointLayout);
}
SLANG_API SlangReflectionVariableLayout* spReflectionEntryPoint_getParameterByIndex(
SlangReflectionEntryPoint* inEntryPoint,
unsigned index)
{
auto entryPointLayout = convert(inEntryPoint);
if(!entryPointLayout) return 0;
return convert(getParameterByIndex(entryPointLayout, index));
}
SLANG_API SlangStage spReflectionEntryPoint_getStage(SlangReflectionEntryPoint* inEntryPoint)
{
auto entryPointLayout = convert(inEntryPoint);
if(!entryPointLayout) return SLANG_STAGE_NONE;
return SlangStage(entryPointLayout->profile.GetStage());
}
SLANG_API void spReflectionEntryPoint_getComputeThreadGroupSize(
SlangReflectionEntryPoint* inEntryPoint,
SlangUInt axisCount,
SlangUInt* outSizeAlongAxis)
{
auto entryPointLayout = convert(inEntryPoint);
if(!entryPointLayout) return;
if(!axisCount) return;
if(!outSizeAlongAxis) return;
auto entryPointFunc = entryPointLayout->entryPoint;
if(!entryPointFunc) return;
SlangUInt sizeAlongAxis[3] = { 1, 1, 1 };
// First look for the HLSL case, where we have an attribute attached to the entry point function
auto numThreadsAttribute = entryPointFunc->FindModifier<HLSLNumThreadsAttribute>();
if (numThreadsAttribute)
{
sizeAlongAxis[0] = numThreadsAttribute->x;
sizeAlongAxis[1] = numThreadsAttribute->y;
sizeAlongAxis[2] = numThreadsAttribute->z;
}
else
{
// Fall back to the GLSL case, which requires a search over global-scope declarations
// to look for anything with the `local_size_*` qualifier
auto module = dynamic_cast<ModuleDecl*>(entryPointFunc->ParentDecl);
if (module)
{
for (auto dd : module->Members)
{
for (auto mod : dd->GetModifiersOfType<GLSLLocalSizeLayoutModifier>())
{
if (auto xMod = dynamic_cast<GLSLLocalSizeXLayoutModifier*>(mod))
sizeAlongAxis[0] = (SlangUInt) getIntegerLiteralValue(xMod->valToken);
else if (auto yMod = dynamic_cast<GLSLLocalSizeYLayoutModifier*>(mod))
sizeAlongAxis[1] = (SlangUInt) getIntegerLiteralValue(yMod->valToken);
else if (auto zMod = dynamic_cast<GLSLLocalSizeZLayoutModifier*>(mod))
sizeAlongAxis[2] = (SlangUInt) getIntegerLiteralValue(zMod->valToken);
}
}
}
}
//
if(axisCount > 0) outSizeAlongAxis[0] = sizeAlongAxis[0];
if(axisCount > 1) outSizeAlongAxis[1] = sizeAlongAxis[1];
if(axisCount > 2) outSizeAlongAxis[2] = sizeAlongAxis[2];
for( SlangUInt aa = 3; aa < axisCount; ++aa )
{
outSizeAlongAxis[aa] = 1;
}
}
SLANG_API int spReflectionEntryPoint_usesAnySampleRateInput(
SlangReflectionEntryPoint* inEntryPoint)
{
auto entryPointLayout = convert(inEntryPoint);
if(!entryPointLayout)
return 0;
if (entryPointLayout->profile.GetStage() != Stage::Fragment)
return 0;
return (entryPointLayout->flags & EntryPointLayout::Flag::usesAnySampleRateInput) != 0;
}
// Shader Reflection
SLANG_API unsigned spReflection_GetParameterCount(SlangReflection* inProgram)
{
auto program = convert(inProgram);
if(!program) return 0;
auto globalLayout = program->globalScopeLayout;
if(auto globalConstantBufferLayout = globalLayout.As<ParameterBlockTypeLayout>())
{
globalLayout = globalConstantBufferLayout->elementTypeLayout;
}
if(auto globalStructLayout = globalLayout.As<StructTypeLayout>())
{
return (unsigned) globalStructLayout->fields.Count();
}
return 0;
}
SLANG_API SlangReflectionParameter* spReflection_GetParameterByIndex(SlangReflection* inProgram, unsigned index)
{
auto program = convert(inProgram);
if(!program) return nullptr;
auto globalLayout = program->globalScopeLayout;
if(auto globalConstantBufferLayout = globalLayout.As<ParameterBlockTypeLayout>())
{
globalLayout = globalConstantBufferLayout->elementTypeLayout;
}
if(auto globalStructLayout = globalLayout.As<StructTypeLayout>())
{
return convert(globalStructLayout->fields[index].Ptr());
}
return nullptr;
}
SLANG_API SlangUInt spReflection_getEntryPointCount(SlangReflection* inProgram)
{
auto program = convert(inProgram);
if(!program) return 0;
return SlangUInt(program->entryPoints.Count());
}
SLANG_API SlangReflectionEntryPoint* spReflection_getEntryPointByIndex(SlangReflection* inProgram, SlangUInt index)
{
auto program = convert(inProgram);
if(!program) return 0;
return convert(program->entryPoints[(int) index].Ptr());
}
|