@@ -687,6 +687,7 @@ struct HLSLRayTracingLayoutRulesImpl : DefaultVaryingLayoutRulesImpl
687
687
{}
688
688
};
689
689
690
+ DefaultLayoutRulesImpl kDefaultLayoutRulesImpl ;
690
691
Std140LayoutRulesImpl kStd140LayoutRulesImpl ;
691
692
Std430LayoutRulesImpl kStd430LayoutRulesImpl ;
692
693
HLSLConstantBufferLayoutRulesImpl kHLSLConstantBufferLayoutRulesImpl ;
@@ -710,6 +711,7 @@ HLSLRayTracingLayoutRulesImpl kHLSLHitAttributesParameterLayoutRulesImpl(LayoutR
710
711
711
712
struct GLSLLayoutRulesFamilyImpl : LayoutRulesFamilyImpl
712
713
{
714
+ virtual LayoutRulesImpl* getAnyValueRules () override ;
713
715
virtual LayoutRulesImpl* getConstantBufferRules () override ;
714
716
virtual LayoutRulesImpl* getPushConstantBufferRules () override ;
715
717
virtual LayoutRulesImpl* getTextureBufferRules () override ;
@@ -730,6 +732,7 @@ struct GLSLLayoutRulesFamilyImpl : LayoutRulesFamilyImpl
730
732
731
733
struct HLSLLayoutRulesFamilyImpl : LayoutRulesFamilyImpl
732
734
{
735
+ virtual LayoutRulesImpl* getAnyValueRules () override ;
733
736
virtual LayoutRulesImpl* getConstantBufferRules () override ;
734
737
virtual LayoutRulesImpl* getPushConstantBufferRules () override ;
735
738
virtual LayoutRulesImpl* getTextureBufferRules () override ;
@@ -750,6 +753,7 @@ struct HLSLLayoutRulesFamilyImpl : LayoutRulesFamilyImpl
750
753
751
754
struct CPULayoutRulesFamilyImpl : LayoutRulesFamilyImpl
752
755
{
756
+ virtual LayoutRulesImpl* getAnyValueRules () override ;
753
757
virtual LayoutRulesImpl* getConstantBufferRules () override ;
754
758
virtual LayoutRulesImpl* getPushConstantBufferRules () override ;
755
759
virtual LayoutRulesImpl* getTextureBufferRules () override ;
@@ -769,6 +773,7 @@ struct CPULayoutRulesFamilyImpl : LayoutRulesFamilyImpl
769
773
770
774
struct CUDALayoutRulesFamilyImpl : LayoutRulesFamilyImpl
771
775
{
776
+ virtual LayoutRulesImpl* getAnyValueRules () override ;
772
777
virtual LayoutRulesImpl* getConstantBufferRules () override ;
773
778
virtual LayoutRulesImpl* getPushConstantBufferRules () override ;
774
779
virtual LayoutRulesImpl* getTextureBufferRules () override ;
@@ -903,6 +908,12 @@ LayoutRulesImpl kCPULayoutRulesImpl_ = {
903
908
&kCPULayoutRulesFamilyImpl , &kCPULayoutRulesImpl , &kCPUObjectLayoutRulesImpl ,
904
909
};
905
910
911
+ LayoutRulesImpl kCPUAnyValueLayoutRulesImpl_ = {
912
+ &kCPULayoutRulesFamilyImpl ,
913
+ &kDefaultLayoutRulesImpl ,
914
+ &kCPUObjectLayoutRulesImpl ,
915
+ };
916
+
906
917
// CUDA
907
918
908
919
static CUDAObjectLayoutRulesImpl kCUDAObjectLayoutRulesImpl ;
@@ -912,6 +923,11 @@ LayoutRulesImpl kCUDALayoutRulesImpl_ = {
912
923
&kCUDALayoutRulesFamilyImpl , &kCUDALayoutRulesImpl , &kCUDAObjectLayoutRulesImpl ,
913
924
};
914
925
926
+ LayoutRulesImpl kCUDAAnyValueLayoutRulesImpl_ = {
927
+ &kCUDALayoutRulesFamilyImpl ,
928
+ &kDefaultLayoutRulesImpl ,
929
+ &kCUDAObjectLayoutRulesImpl ,
930
+ };
915
931
916
932
// GLSL cases
917
933
@@ -923,6 +939,12 @@ LayoutRulesImpl kStd430LayoutRulesImpl_ = {
923
939
&kGLSLLayoutRulesFamilyImpl , &kStd430LayoutRulesImpl , &kGLSLObjectLayoutRulesImpl ,
924
940
};
925
941
942
+ LayoutRulesImpl kGLSLAnyValueLayoutRulesImpl_ = {
943
+ &kGLSLLayoutRulesFamilyImpl ,
944
+ &kDefaultLayoutRulesImpl ,
945
+ &kGLSLPushConstantBufferObjectLayoutRulesImpl_ ,
946
+ };
947
+
926
948
LayoutRulesImpl kGLSLPushConstantLayoutRulesImpl_ = {
927
949
&kGLSLLayoutRulesFamilyImpl , &kStd430LayoutRulesImpl , &kGLSLPushConstantBufferObjectLayoutRulesImpl_ ,
928
950
};
@@ -962,6 +984,12 @@ LayoutRulesImpl kGLSLStructuredBufferLayoutRulesImpl_ = {
962
984
963
985
// HLSL cases
964
986
987
+ LayoutRulesImpl kHLSLAnyValueLayoutRulesImpl_ = {
988
+ &kHLSLLayoutRulesFamilyImpl ,
989
+ &kDefaultLayoutRulesImpl ,
990
+ &kHLSLObjectLayoutRulesImpl ,
991
+ };
992
+
965
993
LayoutRulesImpl kHLSLConstantBufferLayoutRulesImpl_ = {
966
994
&kHLSLLayoutRulesFamilyImpl , &kHLSLConstantBufferLayoutRulesImpl , &kHLSLObjectLayoutRulesImpl ,
967
995
};
@@ -992,6 +1020,11 @@ LayoutRulesImpl kHLSLHitAttributesParameterLayoutRulesImpl_ = {
992
1020
993
1021
// GLSL Family
994
1022
1023
+ LayoutRulesImpl* GLSLLayoutRulesFamilyImpl::getAnyValueRules ()
1024
+ {
1025
+ return &kGLSLAnyValueLayoutRulesImpl_ ;
1026
+ }
1027
+
995
1028
LayoutRulesImpl* GLSLLayoutRulesFamilyImpl::getConstantBufferRules ()
996
1029
{
997
1030
return &kStd140LayoutRulesImpl_ ;
@@ -1060,6 +1093,11 @@ LayoutRulesImpl* GLSLLayoutRulesFamilyImpl::getStructuredBufferRules()
1060
1093
1061
1094
// HLSL Family
1062
1095
1096
+ LayoutRulesImpl* HLSLLayoutRulesFamilyImpl::getAnyValueRules ()
1097
+ {
1098
+ return &kHLSLAnyValueLayoutRulesImpl_ ;
1099
+ }
1100
+
1063
1101
LayoutRulesImpl* HLSLLayoutRulesFamilyImpl::getConstantBufferRules ()
1064
1102
{
1065
1103
return &kHLSLConstantBufferLayoutRulesImpl_ ;
@@ -1129,6 +1167,11 @@ LayoutRulesImpl* HLSLLayoutRulesFamilyImpl::getHitAttributesParameterRules()
1129
1167
1130
1168
// CPU Family
1131
1169
1170
+ LayoutRulesImpl* CPULayoutRulesFamilyImpl::getAnyValueRules ()
1171
+ {
1172
+ return &kCPUAnyValueLayoutRulesImpl_ ;
1173
+ }
1174
+
1132
1175
LayoutRulesImpl* CPULayoutRulesFamilyImpl::getConstantBufferRules ()
1133
1176
{
1134
1177
return &kCPULayoutRulesImpl_ ;
@@ -1190,6 +1233,11 @@ LayoutRulesImpl* CPULayoutRulesFamilyImpl::getStructuredBufferRules()
1190
1233
1191
1234
// CUDA Family
1192
1235
1236
+ LayoutRulesImpl* CUDALayoutRulesFamilyImpl::getAnyValueRules ()
1237
+ {
1238
+ return &kCUDAAnyValueLayoutRulesImpl_ ;
1239
+ }
1240
+
1193
1241
LayoutRulesImpl* CUDALayoutRulesFamilyImpl::getConstantBufferRules ()
1194
1242
{
1195
1243
return &kCUDALayoutRulesImpl_ ;
@@ -3613,43 +3661,65 @@ static TypeLayoutResult _createTypeLayout(
3613
3661
typeLayout->type = type;
3614
3662
typeLayout->rules = rules;
3615
3663
3616
- if (isCPUTarget (context.targetReq ) || isCUDATarget (context.targetReq ))
3664
+ LayoutSize fixedExistentialValueSize = 0 ;
3665
+ bool useFixedSizedExistentialValue =
3666
+ isCPUTarget (context.targetReq ) || isCUDATarget (context.targetReq );
3667
+ if (useFixedSizedExistentialValue)
3617
3668
{
3618
- LayoutSize fixedSize = 16 ;
3669
+ fixedExistentialValueSize = 16 ;
3619
3670
if (auto anyValueAttr =
3620
3671
interfaceDeclRef.getDecl ()->findModifier <AnyValueSizeAttribute>())
3621
3672
{
3622
- fixedSize + = anyValueAttr->size ;
3673
+ fixedExistentialValueSize = anyValueAttr->size ;
3623
3674
}
3624
- else
3625
- {
3626
- // The interface type does not have an `[anyValueSize]` attribute,
3627
- // assume a default of 8 bytes.
3628
- fixedSize += 8 ;
3629
- }
3630
- typeLayout->addResourceUsage (LayoutResourceKind::Uniform, fixedSize);
3675
+ typeLayout->addResourceUsage (LayoutResourceKind::Uniform, fixedExistentialValueSize + 16 );
3631
3676
}
3632
3677
typeLayout->addResourceUsage (LayoutResourceKind::ExistentialTypeParam, 1 );
3633
3678
typeLayout->addResourceUsage (LayoutResourceKind::ExistentialObjectParam, 1 );
3634
3679
3635
3680
// If there are any concrete types available, the first one will be
3636
3681
// the value that should be plugged into the slot we just introduced.
3637
3682
//
3638
- if ( context.specializationArgCount )
3683
+ if ( context.specializationArgCount )
3639
3684
{
3640
3685
auto & specializationArg = context.specializationArgs [0 ];
3641
3686
Type* concreteType = as<Type>(specializationArg.val );
3642
3687
SLANG_ASSERT (concreteType);
3643
3688
3644
- RefPtr<TypeLayout> concreteTypeLayout = createTypeLayout (context, concreteType);
3645
-
3646
- // Layout for this specialized interface type then results
3647
- // in a type layout that tracks both the resource usage of the
3648
- // interface type itself (just the type + value slots introduced
3649
- // above), plus a "pending data" type that represents the value
3650
- // conceptually pointed to by the interface-type field/variable at runtime.
3651
- //
3652
- typeLayout->pendingDataTypeLayout = concreteTypeLayout;
3689
+ // Always use AnyValueRules regardless of the enclosing environment's layout rule
3690
+ // for existential values.
3691
+ auto anyValueRules = context.getRulesFamily ()->getAnyValueRules ();
3692
+
3693
+ // TODO: for traditional GPU targets (HLSL/GLSL) we don't force
3694
+ // anyValueRule for now, since it requires additional work to load
3695
+ // the existential value. We should remove this special case logic
3696
+ // and always use anyValueRule once we implement the correct loading
3697
+ // code gen logic for these targets.
3698
+ if (!(isCPUTarget (context.targetReq ) || isCUDATarget (context.targetReq )))
3699
+ anyValueRules = context.rules ;
3700
+
3701
+ RefPtr<TypeLayout> concreteTypeLayout =
3702
+ createTypeLayout (context.with (anyValueRules), concreteType);
3703
+ auto uniformLayoutInfo =
3704
+ concreteTypeLayout->FindResourceInfo (LayoutResourceKind::Uniform);
3705
+ if (!useFixedSizedExistentialValue ||
3706
+ (uniformLayoutInfo && uniformLayoutInfo->count > fixedExistentialValueSize))
3707
+ {
3708
+ // TODO: for targets that supports pointers, oversized existential values
3709
+ // should be placed in an overflow region and only a pointer is needed in
3710
+ // the place of the fixed sized uniform slot.
3711
+ // We only need the "pending layout" mechanism for targets that does not
3712
+ // support pointers.
3713
+
3714
+ // For legacy targets without pointer support, the layout for this
3715
+ // specialized interface type then results in a type layout that tracks
3716
+ // both the resource usage of the interface type itself (just the
3717
+ // type + value slots introduced above), plus a "pending data" type that
3718
+ // represents the value conceptually pointed to by the interface-type
3719
+ // field/variable at runtime.
3720
+ //
3721
+ typeLayout->pendingDataTypeLayout = concreteTypeLayout;
3722
+ }
3653
3723
}
3654
3724
3655
3725
return TypeLayoutResult (typeLayout, SimpleLayoutInfo ());
0 commit comments