Skip to content

Commit 2e5adbe

Browse files
committed
Implement SampleCmpBias and SampleCmpGrad
these are new for HLSL shader model 6.8 fixes #8929
1 parent 8b2faea commit 2e5adbe

File tree

5 files changed

+374
-1
lines changed

5 files changed

+374
-1
lines changed

docs/command-line-slangc-reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,7 @@ A capability describes an optional feature that a target may or may not support.
15401540
* `texture_querylod`
15411541
* `texture_querylevels`
15421542
* `texture_shadowlod`
1543+
* `texture_shadowgrad`
15431544
* `atomic_glsl_float1`
15441545
* `atomic_glsl_float2`
15451546
* `atomic_glsl_halfvec`

docs/user-guide/a3-02-reference-capability-atoms.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,10 @@ Compound Capabilities
13241324
`texture_querylod`
13251325
> Capabilities required to query texture LOD info
13261326
1327+
`texture_shadowgrad`
1328+
> Capabilities required for shadow texture sampling with bias and gradients.
1329+
> New in HLSL SM6.8 but existed in older GLSL and SPIRV targets.
1330+
13271331
`texture_shadowlod`
13281332
> Capabilities required to query shadow texture lod info
13291333

source/slang/hlsl.meta.slang

Lines changed: 309 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ extension _Texture<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
10621062
///
10631063
/// For HLSL/D3D targets, the texture element type must be a scalar or vector of float or half types.
10641064
///
1065-
///@see `SampleBias`, `SampleLevel`, `SampleLevelZero`, `SampleGrad`, `SampleCmp`, `SampleCmpLevelZero`, `SampleCmpLevel`.
1065+
///@see `SampleBias`, `SampleLevel`, `SampleLevelZero`, `SampleGrad`, `SampleCmp`, `SampleCmpLevelZero`, `SampleCmpLevel`, `SampleCmpBias`, `SampleCmpGrad`.
10661066
[__readNone]
10671067
[ForceInline]
10681068
[require(cpp_cuda_glsl_hlsl_metal_spirv_wgsl, texture_sm_4_0_fragment)]
@@ -1584,6 +1584,157 @@ extension _Texture<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
15841584
}
15851585
}
15861586

1587+
[__readNone]
1588+
[ForceInline]
1589+
[require(glsl_hlsl_spirv, texture_shadowgrad)]
1590+
float SampleCmpGrad(vector<float, Shape.dimensions+isArray> location, float compareValue, vector<float, Shape.dimensions> gradX, vector<float, Shape.dimensions> gradY)
1591+
{
1592+
__target_switch
1593+
{
1594+
case glsl:
1595+
__intrinsic_asm "textureGrad";
1596+
case hlsl:
1597+
static_assert(T is float || T is vector<float,2> || T is vector<float,3> || T is vector<float,4>
1598+
|| T is half || T is vector<half,2> || T is vector<half,3> || T is vector<half,4>
1599+
, "HLSL supports only float and half type textures");
1600+
return __getTexture().SampleCmpGrad(__getComparisonSampler(), location, compareValue, gradX, gradY);
1601+
case spirv:
1602+
return spirv_asm
1603+
{
1604+
result:$$float = OpImageSampleDrefExplicitLod $this $location $compareValue Grad $gradX $gradY;
1605+
};
1606+
}
1607+
}
1608+
1609+
[__readNone]
1610+
[ForceInline]
1611+
[require(glsl_hlsl_spirv, texture_shadowgrad)]
1612+
float SampleCmpGrad(vector<float, Shape.dimensions+isArray> location, float compareValue, vector<float, Shape.dimensions> gradX, vector<float, Shape.dimensions> gradY, constexpr vector<int, Shape.planeDimensions> offset)
1613+
{
1614+
__target_switch
1615+
{
1616+
case glsl:
1617+
__intrinsic_asm "textureGradOffset";
1618+
case hlsl:
1619+
static_assert(T is float || T is vector<float,2> || T is vector<float,3> || T is vector<float,4>
1620+
|| T is half || T is vector<half,2> || T is vector<half,3> || T is vector<half,4>
1621+
, "HLSL supports only float and half type textures");
1622+
return __getTexture().SampleCmpGrad(__getComparisonSampler(), location, compareValue, gradX, gradY, offset);
1623+
case spirv:
1624+
return spirv_asm
1625+
{
1626+
result:$$float = OpImageSampleDrefExplicitLod $this $location $compareValue Grad|ConstOffset $gradX $gradY $offset;
1627+
};
1628+
}
1629+
}
1630+
1631+
[__readNone]
1632+
[ForceInline]
1633+
[require(glsl_hlsl_spirv, texture_shadowgrad)]
1634+
float SampleCmpGrad(vector<float, Shape.dimensions+isArray> location, float compareValue, vector<float, Shape.dimensions> gradX, vector<float, Shape.dimensions> gradY, constexpr vector<int, Shape.planeDimensions> offset, out uint status)
1635+
{
1636+
__target_switch
1637+
{
1638+
case glsl:
1639+
status = 0;
1640+
__intrinsic_asm "textureGradOffset";
1641+
case hlsl:
1642+
static_assert(T is float || T is vector<float,2> || T is vector<float,3> || T is vector<float,4>
1643+
|| T is half || T is vector<half,2> || T is vector<half,3> || T is vector<half,4>
1644+
, "HLSL supports only float and half type textures");
1645+
return __getTexture().SampleCmpGrad(__getComparisonSampler(), location, compareValue, gradX, gradY, offset, status);
1646+
case spirv:
1647+
return spirv_asm
1648+
{
1649+
OpCapability SparseResidency;
1650+
%sparseResultType = OpTypeStruct $$uint $$float;
1651+
1652+
%sparseResult:%sparseResultType = OpImageSparseSampleDrefExplicitLod $this $location $compareValue Grad|ConstOffset $gradX $gradY $offset;
1653+
1654+
%residentCode:$$uint = OpCompositeExtract %sparseResult 0;
1655+
OpStore &status %residentCode;
1656+
result:$$float = OpCompositeExtract %sparseResult 1;
1657+
};
1658+
}
1659+
}
1660+
1661+
[__readNone]
1662+
[ForceInline]
1663+
[require(glsl_hlsl_spirv, texture_shadowgrad)]
1664+
float SampleCmpBias(vector<float, Shape.dimensions+isArray> location, float compareValue, float bias)
1665+
{
1666+
__requireComputeDerivative();
1667+
__target_switch
1668+
{
1669+
case glsl:
1670+
__intrinsic_asm "texture";
1671+
case hlsl:
1672+
static_assert(T is float || T is vector<float,2> || T is vector<float,3> || T is vector<float,4>
1673+
|| T is half || T is vector<half,2> || T is vector<half,3> || T is vector<half,4>
1674+
, "HLSL supports only float and half type textures");
1675+
return __getTexture().SampleCmpBias(__getComparisonSampler(), location, compareValue, bias);
1676+
case spirv:
1677+
return spirv_asm
1678+
{
1679+
result:$$float = OpImageSampleDrefImplicitLod $this $location $compareValue Bias $bias;
1680+
};
1681+
}
1682+
}
1683+
1684+
[__readNone]
1685+
[ForceInline]
1686+
[require(glsl_hlsl_spirv, texture_shadowgrad)]
1687+
float SampleCmpBias(vector<float, Shape.dimensions+isArray> location, float compareValue, float bias, constexpr vector<int, Shape.planeDimensions> offset)
1688+
{
1689+
__requireComputeDerivative();
1690+
__target_switch
1691+
{
1692+
case glsl:
1693+
__intrinsic_asm "textureOffset";
1694+
case hlsl:
1695+
static_assert(T is float || T is vector<float,2> || T is vector<float,3> || T is vector<float,4>
1696+
|| T is half || T is vector<half,2> || T is vector<half,3> || T is vector<half,4>
1697+
, "HLSL supports only float and half type textures");
1698+
return __getTexture().SampleCmpBias(__getComparisonSampler(), location, compareValue, bias, offset);
1699+
case spirv:
1700+
return spirv_asm
1701+
{
1702+
result:$$float = OpImageSampleDrefImplicitLod $this $location $compareValue Bias|ConstOffset $bias $offset;
1703+
};
1704+
}
1705+
}
1706+
1707+
[__readNone]
1708+
[ForceInline]
1709+
[require(glsl_hlsl_spirv, texture_shadowgrad)]
1710+
float SampleCmpBias(vector<float, Shape.dimensions+isArray> location, float compareValue, float bias, constexpr vector<int, Shape.planeDimensions> offset, out uint status)
1711+
{
1712+
__requireComputeDerivative();
1713+
__target_switch
1714+
{
1715+
case glsl:
1716+
status = 0;
1717+
__intrinsic_asm "textureOffset";
1718+
case hlsl:
1719+
static_assert(T is float || T is vector<float,2> || T is vector<float,3> || T is vector<float,4>
1720+
|| T is half || T is vector<half,2> || T is vector<half,3> || T is vector<half,4>
1721+
, "HLSL supports only float and half type textures");
1722+
return __getTexture().SampleCmpBias(__getComparisonSampler(), location, compareValue, bias, offset, status);
1723+
case spirv:
1724+
return spirv_asm
1725+
{
1726+
OpCapability SparseResidency;
1727+
%sparseResultType = OpTypeStruct $$uint $$float;
1728+
1729+
%sparseResult:%sparseResultType = OpImageSparseSampleDrefImplicitLod $this $location $compareValue Bias|ConstOffset $bias $offset;
1730+
1731+
%residentCode:$$uint = OpCompositeExtract %sparseResult 0;
1732+
OpStore &status %residentCode;
1733+
result:$$float = OpCompositeExtract %sparseResult 1;
1734+
};
1735+
}
1736+
}
1737+
15871738
[__readNone]
15881739
[ForceInline]
15891740
[require(cpp_glsl_hlsl_metal_spirv_wgsl, texture_sm_4_0)]
@@ -2783,6 +2934,163 @@ extension _Texture<T,Shape,isArray,isMS,sampleCount,0,isShadow,0,format>
27832934
}
27842935
}
27852936

2937+
[__readNone]
2938+
[ForceInline]
2939+
[require(glsl_hlsl_spirv, texture_shadowgrad)]
2940+
float SampleCmpGrad(SamplerComparisonState s, vector<float, Shape.dimensions+isArray> location, float compareValue, vector<float, Shape.dimensions> gradX, vector<float, Shape.dimensions> gradY)
2941+
{
2942+
__target_switch
2943+
{
2944+
case glsl:
2945+
__intrinsic_asm "textureGrad";
2946+
case hlsl:
2947+
static_assert(T is float || T is vector<float,2> || T is vector<float,3> || T is vector<float,4>
2948+
|| T is half || T is vector<half,2> || T is vector<half,3> || T is vector<half,4>
2949+
, "HLSL supports only float and half type textures");
2950+
__intrinsic_asm ".SampleCmpGrad";
2951+
case spirv:
2952+
return spirv_asm
2953+
{
2954+
%sampledImage : __sampledImageType(this) = OpSampledImage $this $s;
2955+
result:$$float = OpImageSampleDrefExplicitLod %sampledImage $location $compareValue Grad $gradX $gradY;
2956+
};
2957+
}
2958+
}
2959+
2960+
[__readNone]
2961+
[ForceInline]
2962+
[require(glsl_hlsl_spirv, texture_shadowgrad)]
2963+
float SampleCmpGrad(SamplerComparisonState s, vector<float, Shape.dimensions+isArray> location, float compareValue, vector<float, Shape.dimensions> gradX, vector<float, Shape.dimensions> gradY, constexpr vector<int, Shape.planeDimensions> offset)
2964+
{
2965+
__target_switch
2966+
{
2967+
case glsl:
2968+
__intrinsic_asm "textureGradOffset";
2969+
case hlsl:
2970+
static_assert(T is float || T is vector<float,2> || T is vector<float,3> || T is vector<float,4>
2971+
|| T is half || T is vector<half,2> || T is vector<half,3> || T is vector<half,4>
2972+
, "HLSL supports only float and half type textures");
2973+
__intrinsic_asm ".SampleCmpGrad";
2974+
case spirv:
2975+
return spirv_asm
2976+
{
2977+
%sampledImage : __sampledImageType(this) = OpSampledImage $this $s;
2978+
result:$$float = OpImageSampleDrefExplicitLod %sampledImage $location $compareValue Grad|ConstOffset $gradX $gradY $offset;
2979+
};
2980+
}
2981+
}
2982+
2983+
[__readNone]
2984+
[ForceInline]
2985+
[require(glsl_hlsl_spirv, texture_shadowgrad)]
2986+
float SampleCmpGrad(SamplerComparisonState s, vector<float, Shape.dimensions+isArray> location, float compareValue, vector<float, Shape.dimensions> gradX, vector<float, Shape.dimensions> gradY, constexpr vector<int, Shape.planeDimensions> offset, out uint status)
2987+
{
2988+
__target_switch
2989+
{
2990+
case glsl:
2991+
status = 0;
2992+
__intrinsic_asm "textureGradOffset";
2993+
case hlsl:
2994+
static_assert(T is float || T is vector<float,2> || T is vector<float,3> || T is vector<float,4>
2995+
|| T is half || T is vector<half,2> || T is vector<half,3> || T is vector<half,4>
2996+
, "HLSL supports only float and half type textures");
2997+
__intrinsic_asm ".SampleCmpGrad";
2998+
case spirv:
2999+
return spirv_asm
3000+
{
3001+
OpCapability SparseResidency;
3002+
%sparseResultType = OpTypeStruct $$uint $$float;
3003+
%sampledImage:__sampledImageType(this) = OpSampledImage $this $s;
3004+
3005+
%sparseResult:%sparseResultType = OpImageSparseSampleDrefExplicitLod %sampledImage $location $compareValue Grad|ConstOffset $gradX $gradY $offset;
3006+
3007+
%residentCode:$$uint = OpCompositeExtract %sparseResult 0;
3008+
OpStore &status %residentCode;
3009+
result:$$float = OpCompositeExtract %sparseResult 1;
3010+
};
3011+
}
3012+
}
3013+
3014+
[__readNone]
3015+
[ForceInline]
3016+
[require(glsl_hlsl_spirv, texture_shadowgrad)]
3017+
float SampleCmpBias(SamplerComparisonState s, vector<float, Shape.dimensions+isArray> location, float compareValue, float bias)
3018+
{
3019+
__requireComputeDerivative();
3020+
__target_switch
3021+
{
3022+
case glsl:
3023+
__intrinsic_asm "texture";
3024+
case hlsl:
3025+
static_assert(T is float || T is vector<float,2> || T is vector<float,3> || T is vector<float,4>
3026+
|| T is half || T is vector<half,2> || T is vector<half,3> || T is vector<half,4>
3027+
, "HLSL supports only float and half type textures");
3028+
__intrinsic_asm ".SampleCmpBias";
3029+
case spirv:
3030+
return spirv_asm
3031+
{
3032+
%sampledImage : __sampledImageType(this) = OpSampledImage $this $s;
3033+
result:$$float = OpImageSampleDrefImplicitLod %sampledImage $location $compareValue Bias $bias;
3034+
};
3035+
}
3036+
}
3037+
3038+
[__readNone]
3039+
[ForceInline]
3040+
[require(glsl_hlsl_spirv, texture_shadowgrad)]
3041+
float SampleCmpBias(SamplerComparisonState s, vector<float, Shape.dimensions+isArray> location, float compareValue, float bias, constexpr vector<int, Shape.planeDimensions> offset)
3042+
{
3043+
__requireComputeDerivative();
3044+
__target_switch
3045+
{
3046+
case glsl:
3047+
__intrinsic_asm "textureOffset";
3048+
case hlsl:
3049+
static_assert(T is float || T is vector<float,2> || T is vector<float,3> || T is vector<float,4>
3050+
|| T is half || T is vector<half,2> || T is vector<half,3> || T is vector<half,4>
3051+
, "HLSL supports only float and half type textures");
3052+
__intrinsic_asm ".SampleCmpBias";
3053+
case spirv:
3054+
return spirv_asm
3055+
{
3056+
%sampledImage : __sampledImageType(this) = OpSampledImage $this $s;
3057+
result:$$float = OpImageSampleDrefImplicitLod %sampledImage $location $compareValue Bias|ConstOffset $bias $offset;
3058+
};
3059+
}
3060+
}
3061+
3062+
[__readNone]
3063+
[ForceInline]
3064+
[require(glsl_hlsl_spirv, texture_shadowgrad)]
3065+
float SampleCmpBias(SamplerComparisonState s, vector<float, Shape.dimensions+isArray> location, float compareValue, float bias, constexpr vector<int, Shape.planeDimensions> offset, out uint status)
3066+
{
3067+
__requireComputeDerivative();
3068+
__target_switch
3069+
{
3070+
case glsl:
3071+
status = 0;
3072+
__intrinsic_asm "textureOffset";
3073+
case hlsl:
3074+
static_assert(T is float || T is vector<float,2> || T is vector<float,3> || T is vector<float,4>
3075+
|| T is half || T is vector<half,2> || T is vector<half,3> || T is vector<half,4>
3076+
, "HLSL supports only float and half type textures");
3077+
__intrinsic_asm ".SampleCmpBias";
3078+
case spirv:
3079+
return spirv_asm
3080+
{
3081+
OpCapability SparseResidency;
3082+
%sparseResultType = OpTypeStruct $$uint $$float;
3083+
%sampledImage:__sampledImageType(this) = OpSampledImage $this $s;
3084+
3085+
%sparseResult:%sparseResultType = OpImageSparseSampleDrefImplicitLod %sampledImage $location $compareValue Bias|ConstOffset $bias $offset;
3086+
3087+
%residentCode:$$uint = OpCompositeExtract %sparseResult 0;
3088+
OpStore &status %residentCode;
3089+
result:$$float = OpCompositeExtract %sparseResult 1;
3090+
};
3091+
}
3092+
}
3093+
27863094
[__readNone]
27873095
[ForceInline]
27883096
[require(cpp_glsl_hlsl_metal_spirv_wgsl, texture_sm_4_0)]

source/slang/slang-capabilities.capdef

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,6 +2104,10 @@ alias texture_querylevels = texture_sm_4_1 | GL_ARB_texture_query_levels;
21042104
/// [Compound]
21052105
alias texture_shadowlod = texture_sm_4_1 | GL_EXT_texture_shadow_lod
21062106
| texture_sm_4_1;
2107+
/// Capabilities required for shadow texture sampling with bias and gradients.
2108+
/// New in HLSL SM6.8 but existed in older GLSL and SPIRV targets.
2109+
/// [Compound]
2110+
alias texture_shadowgrad = sm_6_8_version | GL_EXT_texture_shadow_lod;
21072111

21082112
/// (GLSL/SPIRV) Capabilities required to use GLSL-tier-1 float-atomic operations
21092113
/// [Compound]

0 commit comments

Comments
 (0)