Skip to content

Commit 2b76466

Browse files
Add CalculateLevelOfDetail* overloads for comparison samplers (shader-slang#6018)
* add CalculateLevelOfDetail* intrinsics for comparison samplers * fix dx12 test * fix metallib test * fix merge conflict --------- Co-authored-by: Yong He <yonghe@outlook.com>
1 parent 55ff468 commit 2b76466

File tree

5 files changed

+171
-3
lines changed

5 files changed

+171
-3
lines changed

source/slang/hlsl.meta.slang

+50
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ extension _Texture<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
814814
case spirv:
815815
return (spirv_asm
816816
{
817+
OpCapability ImageQuery;
817818
result:$$float2 = OpImageQueryLod $this $location
818819
}).x;
819820
}
@@ -839,6 +840,7 @@ extension _Texture<T,Shape,isArray,isMS,sampleCount,0,isShadow,1,format>
839840
case spirv:
840841
return (spirv_asm
841842
{
843+
OpCapability ImageQuery;
842844
result:$$float2 = OpImageQueryLod $this $location
843845
}).y;
844846
}
@@ -1517,6 +1519,7 @@ extension _Texture<T,Shape,isArray,isMS,sampleCount,access,isShadow,0,format>
15171519
__intrinsic_asm "textureQueryLod($p, $2).x";
15181520
case spirv:
15191521
return (spirv_asm {
1522+
OpCapability ImageQuery;
15201523
%sampledImage : __sampledImageType(this) = OpSampledImage $this $s;
15211524
result:$$float2 = OpImageQueryLod %sampledImage $location;
15221525
}).x;
@@ -1539,6 +1542,53 @@ extension _Texture<T,Shape,isArray,isMS,sampleCount,access,isShadow,0,format>
15391542
__intrinsic_asm "textureQueryLod($p, $2).y";
15401543
case spirv:
15411544
return (spirv_asm {
1545+
OpCapability ImageQuery;
1546+
%sampledImage : __sampledImageType(this) = OpSampledImage $this $s;
1547+
result:$$float2 = OpImageQueryLod %sampledImage $location;
1548+
}).y;
1549+
}
1550+
}
1551+
1552+
[__readNone]
1553+
[ForceInline]
1554+
[require(glsl_hlsl_metal_spirv, texture_querylod)]
1555+
float CalculateLevelOfDetail(SamplerComparisonState s, TextureCoord location)
1556+
{
1557+
__requireComputeDerivative();
1558+
__target_switch
1559+
{
1560+
case hlsl:
1561+
__intrinsic_asm ".CalculateLevelOfDetail";
1562+
case metal:
1563+
__intrinsic_asm ".calculate_clamped_lod";
1564+
case glsl:
1565+
__intrinsic_asm "textureQueryLod($p, $2).x";
1566+
case spirv:
1567+
return (spirv_asm {
1568+
OpCapability ImageQuery;
1569+
%sampledImage : __sampledImageType(this) = OpSampledImage $this $s;
1570+
result:$$float2 = OpImageQueryLod %sampledImage $location;
1571+
}).x;
1572+
}
1573+
}
1574+
1575+
[__readNone]
1576+
[ForceInline]
1577+
[require(glsl_hlsl_metal_spirv, texture_querylod)]
1578+
float CalculateLevelOfDetailUnclamped(SamplerComparisonState s, TextureCoord location)
1579+
{
1580+
__requireComputeDerivative();
1581+
__target_switch
1582+
{
1583+
case hlsl:
1584+
__intrinsic_asm ".CalculateLevelOfDetailUnclamped";
1585+
case metal:
1586+
__intrinsic_asm ".calculate_unclamped_lod";
1587+
case glsl:
1588+
__intrinsic_asm "textureQueryLod($p, $2).y";
1589+
case spirv:
1590+
return (spirv_asm {
1591+
OpCapability ImageQuery;
15421592
%sampledImage : __sampledImageType(this) = OpSampledImage $this $s;
15431593
result:$$float2 = OpImageQueryLod %sampledImage $location;
15441594
}).y;
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
//TEST:CROSS_COMPILE(filecheck=CHECK): -profile ps_5_0 -entry main -target glsl
22

3-
// CHECK: textureQueryLod(sampler2D(t_0,s_0), ({{.*}})).x
4-
// CHECK: textureQueryLod(sampler2D(t_0,s_0), ({{.*}})).y
3+
// CHECK: textureQueryLod(sampler2D(
4+
// CHECK: textureQueryLod(sampler2D(
5+
// CHECK: textureQueryLod(sampler2DShadow(
6+
// CHECK: textureQueryLod(sampler2DShadow(
57

68
Texture2D t;
79
SamplerState s;
10+
SamplerComparisonState sc;
11+
812
float main()
913
{
10-
return t.CalculateLevelOfDetail(s, float2(0,0)) + t.CalculateLevelOfDetailUnclamped(s, float2(0,0));
14+
float result = 0.0;
15+
result += t.CalculateLevelOfDetail(s, float2(0,0)) + t.CalculateLevelOfDetailUnclamped(s, float2(0,0));
16+
result += t.CalculateLevelOfDetail(sc, float2(0,0)) + t.CalculateLevelOfDetailUnclamped(sc, float2(0,0));
17+
return result;
1118
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//TEST:SIMPLE(filecheck=CHECK_HLSL): -stage fragment -entry fragmentMain -target hlsl
2+
//TEST:SIMPLE(filecheck=CHECK_SPIRV): -stage fragment -entry fragmentMain -target spirv
3+
4+
Texture2D t;
5+
SamplerState s;
6+
SamplerComparisonState sc;
7+
8+
float fragmentMain()
9+
{
10+
float result = 0.0;
11+
12+
// CHECK_SPIRV: OpCapability ImageQuery
13+
14+
// CHECK_HLSL: CalculateLevelOfDetail
15+
// CHECK_HLSL: CalculateLevelOfDetailUnclamped
16+
// CHECK_SPIRV: OpImageQueryLod
17+
// CHECK_SPIRV: OpImageQueryLod
18+
result += t.CalculateLevelOfDetail(s, float2(0,0)) + t.CalculateLevelOfDetailUnclamped(s, float2(0,0));
19+
20+
// CHECK_HLSL: CalculateLevelOfDetail
21+
// CHECK_HLSL: CalculateLevelOfDetailUnclamped
22+
// CHECK_SPIRV: OpImageQueryLod
23+
// CHECK_SPIRV: OpImageQueryLod
24+
result += t.CalculateLevelOfDetail(sc, float2(0,0)) + t.CalculateLevelOfDetailUnclamped(sc, float2(0,0));
25+
26+
return result;
27+
}

tests/metal/texture-sampler-less.slang

+36
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,24 @@ bool TEST_texture_float()
236236
// METALLIB: call {{.*}}.calculate_clamped_lod_texture_cube_array(
237237
&& float(0) == tCubeArray.CalculateLevelOfDetail(normalize(float3(u, 1 - u, u)))
238238

239+
// Shadow variant
240+
241+
// METAL: d2D{{.*}}.calculate_clamped_lod({{.*}}
242+
// METALLIB: call {{.*}}.calculate_clamped_lod_depth_2d(
243+
&& float(0) == d2D.CalculateLevelOfDetail(float2(u, u))
244+
245+
// METAL: dCube{{.*}}.calculate_clamped_lod({{.*}}
246+
// METALLIB: call {{.*}}.calculate_clamped_lod_depth_cube(
247+
&& float(0) == dCube.CalculateLevelOfDetail(normalize(float3(u, 1 - u, u)))
248+
249+
// METAL: d2DArray{{.*}}.calculate_clamped_lod({{.*}}
250+
// METALLIB: call {{.*}}.calculate_clamped_lod_depth_2d_array(
251+
&& float(0) == d2DArray.CalculateLevelOfDetail(float2(u, u))
252+
253+
// METAL: dCubeArray{{.*}}.calculate_clamped_lod({{.*}}
254+
// METALLIB: call {{.*}}.calculate_clamped_lod_depth_cube_array(
255+
&& float(0) == dCubeArray.CalculateLevelOfDetail(normalize(float3(u, 1 - u, u)))
256+
239257
// ========================================
240258
// float CalculateLevelOfDetailUnclamped()
241259
// ========================================
@@ -260,6 +278,24 @@ bool TEST_texture_float()
260278
// METALLIB: call {{.*}}.calculate_unclamped_lod_texture_cube_array(
261279
&& float(0) >= tCubeArray.CalculateLevelOfDetailUnclamped(normalize(float3(u, 1 - u, u)))
262280

281+
// Shadow variant
282+
283+
// METAL: d2D{{.*}}.calculate_unclamped_lod({{.*}}
284+
// METALLIB: call {{.*}}.calculate_unclamped_lod_depth_2d(
285+
&& float(0) >= d2D.CalculateLevelOfDetailUnclamped(float2(u, u))
286+
287+
// METAL: dCube{{.*}}.calculate_unclamped_lod({{.*}}
288+
// METALLIB: call {{.*}}.calculate_unclamped_lod_depth_cube(
289+
&& float(0) >= dCube.CalculateLevelOfDetailUnclamped(normalize(float3(u, 1 - u, u)))
290+
291+
// METAL: d2DArray{{.*}}.calculate_unclamped_lod({{.*}}
292+
// METALLIB: call {{.*}}.calculate_unclamped_lod_depth_2d_array(
293+
&& float(0) >= d2DArray.CalculateLevelOfDetailUnclamped(float2(u, u))
294+
295+
// METAL: dCubeArray{{.*}}.calculate_unclamped_lod({{.*}}
296+
// METALLIB: call {{.*}}.calculate_unclamped_lod_depth_cube_array(
297+
&& float(0) >= dCubeArray.CalculateLevelOfDetailUnclamped(normalize(float3(u, 1 - u, u)))
298+
263299
// ===========
264300
// T Sample()
265301
// ===========

tests/metal/texture.slang

+48
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,31 @@ bool TEST_texture(
341341
// METALLIB: call {{.*}}.calculate_clamped_lod_texture_cube_array(
342342
&& float(0) == tCubeArray.CalculateLevelOfDetail(samplerState, normalize(float3(u, 1 - u, u)))
343343

344+
// SamplerComparisonState variant
345+
346+
// Functional tests that require SM higher than 6.6 (as of writing) cannot run.
347+
#if !defined(EXCLUDE_SM_6_7)
348+
// METAL: t2D{{.*}}.calculate_clamped_lod({{.*}}
349+
// METALLIB: call {{.*}}.calculate_clamped_lod_texture_2d(
350+
&& float(0) == t2D.CalculateLevelOfDetail(shadowSampler, float2(u, u))
351+
352+
// METAL: t3D{{.*}}.calculate_clamped_lod({{.*}}
353+
// METALLIB: call {{.*}}.calculate_clamped_lod_texture_3d(
354+
&& float(0) == t3D.CalculateLevelOfDetail(shadowSampler, float3(u, u, u))
355+
356+
// METAL: tCube{{.*}}.calculate_clamped_lod({{.*}}
357+
// METALLIB: call {{.*}}.calculate_clamped_lod_texture_cube(
358+
&& float(0) == tCube.CalculateLevelOfDetail(shadowSampler, normalize(float3(u, 1 - u, u)))
359+
360+
// METAL: t2DArray{{.*}}.calculate_clamped_lod({{.*}}
361+
// METALLIB: call {{.*}}.calculate_clamped_lod_texture_2d_array(
362+
&& float(0) == t2DArray.CalculateLevelOfDetail(shadowSampler, float2(u, u))
363+
364+
// METAL: tCubeArray{{.*}}.calculate_clamped_lod({{.*}}
365+
// METALLIB: call {{.*}}.calculate_clamped_lod_texture_cube_array(
366+
&& float(0) == tCubeArray.CalculateLevelOfDetail(shadowSampler, normalize(float3(u, 1 - u, u)))
367+
#endif
368+
344369
// ========================================
345370
// float CalculateLevelOfDetailUnclamped()
346371
// ========================================
@@ -367,6 +392,29 @@ bool TEST_texture(
367392
// METALLIB: call {{.*}}.calculate_unclamped_lod_texture_cube_array(
368393
&& float(0) >= tCubeArray.CalculateLevelOfDetailUnclamped(samplerState, normalize(float3(u, 1 - u, u)))
369394

395+
// SamplerComparisonState variant
396+
#if !defined(EXCLUDE_SM_6_7)
397+
// METAL: t2D{{.*}}.calculate_unclamped_lod({{.*}}
398+
// METALLIB: call {{.*}}.calculate_unclamped_lod_texture_2d(
399+
&& float(0) >= t2D.CalculateLevelOfDetailUnclamped(shadowSampler, float2(u, u))
400+
401+
// METAL: t3D{{.*}}.calculate_unclamped_lod({{.*}}
402+
// METALLIB: call {{.*}}.calculate_unclamped_lod_texture_3d(
403+
&& float(0) >= t3D.CalculateLevelOfDetailUnclamped(shadowSampler, float3(u, u, u))
404+
405+
// METAL: tCube{{.*}}.calculate_unclamped_lod({{.*}}
406+
// METALLIB: call {{.*}}.calculate_unclamped_lod_texture_cube(
407+
&& float(0) >= tCube.CalculateLevelOfDetailUnclamped(shadowSampler, normalize(float3(u, 1 - u, u)))
408+
409+
// METAL: t2DArray{{.*}}.calculate_unclamped_lod({{.*}}
410+
// METALLIB: call {{.*}}.calculate_unclamped_lod_texture_2d_array(
411+
&& float(0) >= t2DArray.CalculateLevelOfDetailUnclamped(shadowSampler, float2(u, u))
412+
413+
// METAL: tCubeArray{{.*}}.calculate_unclamped_lod({{.*}}
414+
// METALLIB: call {{.*}}.calculate_unclamped_lod_texture_cube_array(
415+
&& float(0) >= tCubeArray.CalculateLevelOfDetailUnclamped(shadowSampler, normalize(float3(u, 1 - u, u)))
416+
#endif
417+
370418
// ===========
371419
// T Sample()
372420
// ===========

0 commit comments

Comments
 (0)