Skip to content

Commit cc88530

Browse files
authored
Support combined textures for Metal target (shader-slang#4169)
1 parent 4edc72e commit cc88530

File tree

4 files changed

+659
-44
lines changed

4 files changed

+659
-44
lines changed

source/slang/hlsl.meta.slang

+82-24
Original file line numberDiff line numberDiff line change
@@ -529,13 +529,14 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
529529

530530
[__readNone]
531531
[ForceInline]
532-
[require(glsl_hlsl_spirv, texture_querylod)]
532+
[require(glsl_hlsl_metal_spirv, texture_querylod)]
533533
float CalculateLevelOfDetail(TextureCoord location)
534534
{
535535
__requireComputeDerivative();
536536
__target_switch
537537
{
538538
case hlsl:
539+
case metal:
539540
{
540541
return __getTexture().CalculateLevelOfDetail(__getSampler(), location);
541542
}
@@ -551,13 +552,14 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
551552

552553
[__readNone]
553554
[ForceInline]
554-
[require(glsl_hlsl_spirv, texture_querylod)]
555+
[require(glsl_hlsl_metal_spirv, texture_querylod)]
555556
float CalculateLevelOfDetailUnclamped(TextureCoord location)
556557
{
557558
__requireComputeDerivative();
558559
__target_switch
559560
{
560561
case hlsl:
562+
case metal:
561563
{
562564
return __getTexture().CalculateLevelOfDetailUnclamped(__getSampler(), location);
563565
}
@@ -573,14 +575,15 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
573575

574576
[__readNone]
575577
[ForceInline]
576-
[require(cpp_cuda_glsl_hlsl_spirv, texture_sm_4_1_fragment)]
578+
[require(cpp_cuda_glsl_hlsl_metal_spirv, texture_sm_4_1_fragment)]
577579
T Sample(vector<float, Shape.dimensions+isArray> location)
578580
{
579581
__requireComputeDerivative();
580582
__target_switch
581583
{
582584
case cpp:
583585
case hlsl:
586+
case metal:
584587
return __getTexture().Sample(__getSampler(), location);
585588
case glsl:
586589
__intrinsic_asm "$ctexture($0, $1)$z";
@@ -622,17 +625,42 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
622625
}
623626
}
624627

628+
[__readNone]
629+
[ForceInline]
630+
[require(cpp_glsl_hlsl_metal_spirv, texture_sm_4_1_fragment)]
631+
T Sample(vector<float, Shape.dimensions+isArray> location, constexpr vector<int, Shape.planeDimensions> offset)
632+
{
633+
__requireComputeDerivative();
634+
__target_switch
635+
{
636+
case cpp:
637+
case hlsl:
638+
case metal:
639+
return __getTexture().Sample(__getSampler(), location, offset);
640+
case glsl:
641+
__intrinsic_asm "$ctextureOffsetClampARB($0, $1, $2)$z";
642+
case spirv:
643+
return spirv_asm
644+
{
645+
OpCapability MinLod;
646+
%sampled : __sampledType(T) = OpImageSampleImplicitLod $this $location None|ConstOffset $offset;
647+
__truncate $$T result __sampledType(T) %sampled;
648+
};
649+
}
650+
}
651+
625652
[__readNone]
626653
[ForceInline]
627654
__glsl_extension(GL_ARB_sparse_texture_clamp)
628-
[require(cpp_glsl_hlsl_spirv, texture_sm_4_1_fragment)]
655+
[require(cpp_glsl_hlsl_metal_spirv, texture_sm_4_1_fragment)]
629656
T Sample(vector<float, Shape.dimensions+isArray> location, vector<int, Shape.planeDimensions> offset, float clamp)
630657
{
631658
__requireComputeDerivative();
632659
__target_switch
633660
{
634661
case cpp:
635662
case hlsl:
663+
case metal:
636664
return __getTexture().Sample(__getSampler(), location, offset, clamp);
637665
case glsl:
638666
__intrinsic_asm "$ctextureOffsetClampARB($0, $1, $2, $3)$z";
@@ -648,7 +676,7 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
648676

649677
[__readNone]
650678
[ForceInline]
651-
[require(cpp_glsl_hlsl_spirv, texture_sm_4_1_fragment)]
679+
[require(cpp_glsl_hlsl_metal_spirv, texture_sm_4_1_fragment)]
652680
T Sample(vector<float, Shape.dimensions+isArray> location, vector<int, Shape.planeDimensions> offset, float clamp, out uint status)
653681
{
654682
__target_switch
@@ -662,14 +690,15 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
662690

663691
[__readNone]
664692
[ForceInline]
665-
[require(cpp_glsl_hlsl_spirv, texture_sm_4_1_fragment)]
693+
[require(cpp_glsl_hlsl_metal_spirv, texture_sm_4_1_fragment)]
666694
T SampleBias(vector<float, Shape.dimensions+isArray> location, float bias)
667695
{
668696
__requireComputeDerivative();
669697
__target_switch
670698
{
671699
case cpp:
672700
case hlsl:
701+
case metal:
673702
return __getTexture().SampleBias(__getSampler(), location, bias);
674703
case glsl:
675704
__intrinsic_asm "$ctexture($0, $1, $2)$z";
@@ -685,14 +714,15 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
685714

686715
[__readNone]
687716
[ForceInline]
688-
[require(cpp_glsl_hlsl_spirv, texture_sm_4_1_fragment)]
717+
[require(cpp_glsl_hlsl_metal_spirv, texture_sm_4_1_fragment)]
689718
T SampleBias(vector<float, Shape.dimensions+isArray> location, float bias, constexpr vector<int, Shape.planeDimensions> offset)
690719
{
691720
__requireComputeDerivative();
692721
__target_switch
693722
{
694723
case cpp:
695724
case hlsl:
725+
case metal:
696726
return __getTexture().SampleBias(__getSampler(), location, bias, offset);
697727
case glsl:
698728
__intrinsic_asm "$ctextureOffset($0, $1, $3, $2)$z";
@@ -707,7 +737,7 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
707737

708738
[__readNone]
709739
[ForceInline]
710-
[require(glsl_hlsl_spirv, texture_shadowlod)]
740+
[require(glsl_hlsl_metal_spirv, texture_shadowlod)]
711741
float SampleCmp(vector<float, Shape.dimensions+isArray> location, float compareValue)
712742
{
713743
__target_switch
@@ -726,6 +756,7 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
726756
return __glsl_texture(this, __makeVector(location, compareValue));
727757
}
728758
case hlsl:
759+
case metal:
729760
return __getTexture().SampleCmp(__getComparisonSampler(), location, compareValue);
730761
case spirv:
731762
return spirv_asm
@@ -737,7 +768,7 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
737768

738769
[__readNone]
739770
[ForceInline]
740-
[require(glsl_hlsl_spirv, texture_shadowlod)]
771+
[require(glsl_hlsl_metal_spirv, texture_shadowlod)]
741772
float SampleCmpLevelZero(vector<float, Shape.dimensions+isArray> location, float compareValue)
742773
{
743774
__target_switch
@@ -752,6 +783,7 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
752783
return __glsl_texture_level_zero(this, __makeVector(location, compareValue));
753784
}
754785
case hlsl:
786+
case metal:
755787
return __getTexture().SampleCmpLevelZero(__getComparisonSampler(), location, compareValue);
756788
case spirv:
757789
const float zeroFloat = 0.0f;
@@ -764,7 +796,7 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
764796

765797
[__readNone]
766798
[ForceInline]
767-
[require(glsl_hlsl_spirv, texture_shadowlod)]
799+
[require(glsl_hlsl_metal_spirv, texture_shadowlod)]
768800
float SampleCmp(vector<float, Shape.dimensions+isArray> location, float compareValue, constexpr vector<int, Shape.planeDimensions> offset)
769801
{
770802
__target_switch
@@ -779,6 +811,7 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
779811
return __glsl_texture_offset(this, __makeVector(location, compareValue), offset);
780812
}
781813
case hlsl:
814+
case metal:
782815
return __getTexture().SampleCmp(__getComparisonSampler(), location, compareValue, offset);
783816
case spirv:
784817
return spirv_asm
@@ -790,7 +823,7 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
790823

791824
[__readNone]
792825
[ForceInline]
793-
[require(glsl_hlsl_spirv, texture_shadowlod)]
826+
[require(glsl_hlsl_metal_spirv, texture_shadowlod)]
794827
float SampleCmpLevelZero(vector<float, Shape.dimensions+isArray> location, float compareValue, constexpr vector<int, Shape.planeDimensions> offset)
795828
{
796829
__target_switch
@@ -805,6 +838,7 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
805838
return __glsl_texture_offset_level_zero(this, __makeVector(location, compareValue), offset);
806839
}
807840
case hlsl:
841+
case metal:
808842
return __getTexture().SampleCmpLevelZero(__getComparisonSampler(), location, compareValue, offset);
809843
case spirv:
810844
const float zeroFloat = 0.0f;
@@ -817,13 +851,14 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
817851

818852
[__readNone]
819853
[ForceInline]
820-
[require(cpp_glsl_hlsl_spirv, texture_sm_4_1)]
854+
[require(cpp_glsl_hlsl_metal_spirv, texture_sm_4_1)]
821855
T SampleGrad(vector<float, Shape.dimensions+isArray> location, vector<float, Shape.dimensions> gradX, vector<float, Shape.dimensions> gradY)
822856
{
823857
__target_switch
824858
{
825859
case cpp:
826860
case hlsl:
861+
case metal:
827862
return __getTexture().SampleGrad(__getSampler(), location, gradX, gradY);
828863

829864
case glsl:
@@ -839,13 +874,14 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
839874

840875
[__readNone]
841876
[ForceInline]
842-
[require(cpp_glsl_hlsl_spirv, texture_sm_4_1)]
877+
[require(cpp_glsl_hlsl_metal_spirv, texture_sm_4_1)]
843878
T SampleGrad(vector<float, Shape.dimensions+isArray> location, vector<float, Shape.dimensions> gradX, vector<float, Shape.dimensions> gradY, constexpr vector<int, Shape.dimensions> offset)
844879
{
845880
__target_switch
846881
{
847882
case cpp:
848883
case hlsl:
884+
case metal:
849885
return __getTexture().SampleGrad(__getSampler(), location, gradX, gradY, offset);
850886
case glsl:
851887
__intrinsic_asm "$ctextureGradOffset($0, $1, $2, $3, $4)$z";
@@ -861,13 +897,14 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
861897
[__readNone]
862898
[ForceInline]
863899
__glsl_extension(GL_ARB_sparse_texture_clamp)
864-
[require(cpp_glsl_hlsl_spirv, texture_sm_4_1)]
900+
[require(cpp_glsl_hlsl_metal_spirv, texture_sm_4_1)]
865901
T SampleGrad(vector<float, Shape.dimensions+isArray> location, vector<float, Shape.dimensions> gradX, vector<float, Shape.dimensions> gradY, constexpr vector<int, Shape.dimensions> offset, float lodClamp)
866902
{
867903
__target_switch
868904
{
869905
case cpp:
870906
case hlsl:
907+
case metal:
871908
return __getTexture().SampleGrad(__getSampler(), location, gradX, gradY, offset, lodClamp);
872909
case glsl:
873910
__intrinsic_asm "$ctextureGradOffsetClampARB($0, $1, $2, $3, $4, $5)$z";
@@ -883,13 +920,14 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
883920

884921
[__readNone]
885922
[ForceInline]
886-
[require(cpp_cuda_glsl_hlsl_spirv, texture_sm_4_1)]
923+
[require(cpp_cuda_glsl_hlsl_metal_spirv, texture_sm_4_1)]
887924
T SampleLevel(vector<float, Shape.dimensions+isArray> location, float level)
888925
{
889926
__target_switch
890927
{
891928
case cpp:
892929
case hlsl:
930+
case metal:
893931
return __getTexture().SampleLevel(__getSampler(), location, level);
894932
case glsl:
895933
__intrinsic_asm "$ctextureLod($0, $1, $2)$z";
@@ -935,13 +973,14 @@ extension __TextureImpl<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
935973

936974
[__readNone]
937975
[ForceInline]
938-
[require(cpp_glsl_hlsl_spirv, texture_sm_4_1)]
976+
[require(cpp_glsl_hlsl_metal_spirv, texture_sm_4_1)]
939977
T SampleLevel(vector<float, Shape.dimensions+isArray> location, float level, constexpr vector<int, Shape.planeDimensions> offset)
940978
{
941979
__target_switch
942980
{
943981
case cpp:
944982
case hlsl:
983+
case metal:
945984
return __getTexture().SampleLevel(__getSampler(), location, level, offset);
946985

947986
case glsl:
@@ -2109,15 +2148,29 @@ for (int isScalarTexture = 0; isScalarTexture <= 1; isScalarTexture++) {
21092148
}}}}
21102149
{ // begin extension for gather
21112150
${{{{
2151+
if (isCombined)
2152+
{
2153+
}}}}
2154+
__intrinsic_op($(kIROp_CombinedTextureSamplerGetTexture))
2155+
__TextureImpl<T, Shape, isArray, isMS, sampleCount, 0, isShadow, 0, format> __getTexture();
2156+
2157+
__intrinsic_op($(kIROp_CombinedTextureSamplerGetSampler))
2158+
SamplerState __getSampler();
2159+
2160+
__intrinsic_op($(kIROp_CombinedTextureSamplerGetSampler))
2161+
SamplerComparisonState __getComparisonSampler();
2162+
${{{{
2163+
}
2164+
21122165
// Gather component
21132166
const char* samplerStateParam = isCombined ? "" : " s,";
2114-
const char* metalSupport = isCombined ? "" : "metal_";
2115-
const char* caseMetal = isCombined ? "" : "case metal:";
2167+
const char* getTexture = isCombined ? "__getTexture()" : "this";
21162168
for (int isCmp = 0; isCmp < 2; ++isCmp) {
21172169
const char* cmp = isCmp ? "Cmp" : "";
21182170
const char* cmpParam = isCmp ? ", T compareValue" : "";
21192171
const char* compareArg = isCmp ? ", compareValue" : "";
21202172
const char* samplerStateType = isCombined ? "" : (isCmp ? "SamplerComparisonState" : "SamplerState");
2173+
const char* getSampler = isCombined ? (isCmp ? " __getComparisonSampler()," : " __getSampler(),") : samplerStateParam;
21212174
const char* componentNames[] = { "", "Red", "Green", "Blue", "Alpha"};
21222175
const char* glslComponentNames[] = { ", 0", ", 1", ", 2", ", 3" };
21232176

@@ -2126,26 +2179,28 @@ ${{{{
21262179
auto glslComponent = (isCmp ? "" :glslComponentNames[componentId == 0 ? 0 : componentId - 1]);
21272180
}}}}
21282181
[ForceInline]
2129-
[require(glsl_hlsl_$(metalSupport)spirv, texture_gather)]
2182+
[require(glsl_hlsl_metal_spirv, texture_gather)]
21302183
vector<T,4> Gather$(cmp)$(componentName)($(samplerStateType)$(samplerStateParam) vector<float, Shape.dimensions+isArray> location $(cmpParam))
21312184
{
21322185
__target_switch
21332186
{
21342187
case hlsl: __intrinsic_asm ".Gather$(cmp)$(componentName)";
2135-
$(caseMetal)
2188+
case metal:
2189+
return __texture_gather$(cmp)<T>($(getTexture),$(getSampler) location $(compareArg) $(glslComponent));
21362190
case glsl:
21372191
case spirv:
21382192
return __texture_gather$(cmp)<T>(this,$(samplerStateParam) location $(compareArg) $(glslComponent));
21392193
}
21402194
}
21412195
[ForceInline]
2142-
[require(glsl_hlsl_$(metalSupport)spirv, texture_gather)]
2196+
[require(glsl_hlsl_metal_spirv, texture_gather)]
21432197
vector<T,4> Gather$(cmp)$(componentName)($(samplerStateType)$(samplerStateParam) vector<float, Shape.dimensions+isArray> location $(cmpParam), constexpr vector<int, Shape.planeDimensions> offset)
21442198
{
21452199
__target_switch
21462200
{
21472201
case hlsl: __intrinsic_asm ".Gather$(cmp)$(componentName)";
2148-
$(caseMetal)
2202+
case metal:
2203+
return __texture_gather$(cmp)_offset<T>($(getTexture),$(getSampler) location $(compareArg), offset $(glslComponent));
21492204
case glsl:
21502205
case spirv:
21512206
return __texture_gather$(cmp)_offset<T>(this,$(samplerStateParam) location $(compareArg), offset $(glslComponent));
@@ -2347,14 +2402,16 @@ extension __TextureImpl<T,Shape,isArray,0,sampleCount,0,isShadow,isCombined,form
23472402
__glsl_extension(GL_EXT_samplerless_texture_functions)
23482403
[__readNone]
23492404
[ForceInline]
2350-
[require(cpp_glsl_hlsl_spirv, texture_sm_4_1_samplerless)]
2405+
[require(cpp_glsl_hlsl_metal_spirv, texture_sm_4_1_samplerless)]
23512406
get
23522407
{
23532408
__target_switch
23542409
{
23552410
case cpp:
23562411
case hlsl:
23572412
__intrinsic_asm ".operator[]";
2413+
case metal:
2414+
return Load(__makeVector(location, 0));
23582415
case glsl:
23592416
return __glsl_load(location);
23602417
case spirv:
@@ -2511,14 +2568,15 @@ extension __TextureImpl<T,Shape,isArray,1,sampleCount,0,isShadow,isCombined,form
25112568
__glsl_extension(GL_EXT_samplerless_texture_functions)
25122569
[__readNone]
25132570
[ForceInline]
2514-
[require(cpp_glsl_hlsl_spirv, texture_sm_4_1_samplerless)]
2571+
[require(cpp_glsl_hlsl_metal_spirv, texture_sm_4_1_samplerless)]
25152572
get
25162573
{
25172574
__target_switch
25182575
{
25192576
case cpp:
25202577
case hlsl:
25212578
__intrinsic_asm "($0).sample[$2][$1]";
2579+
case metal:
25222580
case glsl:
25232581
case spirv:
25242582
return Load(location, sampleIndex);

0 commit comments

Comments
 (0)