From 237af82f7e9ed1dd5d97a981939fc23aec2008b3 Mon Sep 17 00:00:00 2001 From: Darren Wihandi <65404740+fairywreath@users.noreply.github.com> Date: Thu, 19 Dec 2024 12:13:02 -0500 Subject: [PATCH] Add HLSL texture sample with status intrinsics (#5901) * Implement HLSL texture sample with status intrinsics * fix test and cleanup --------- Co-authored-by: Yong He --- source/slang/hlsl.meta.slang | 156 +++++++++++++++++- .../texture/texture-intrinsics.slang | 60 +++++-- 2 files changed, 201 insertions(+), 15 deletions(-) diff --git a/source/slang/hlsl.meta.slang b/source/slang/hlsl.meta.slang index 194c14b198..85a81eabf3 100644 --- a/source/slang/hlsl.meta.slang +++ b/source/slang/hlsl.meta.slang @@ -1057,6 +1057,22 @@ extension _Texture } } + [__readNone] + [ForceInline] + [require(hlsl, sm_5_0)] + T SampleBias(vector location, float bias, constexpr vector offset, float clamp, out uint status) + { + __requireComputeDerivative(); + __target_switch + { + case hlsl: + static_assert(T is float || T is vector || T is vector || T is vector + || T is half || T is vector || T is vector || T is vector + , "HLSL supports only float and half type textures"); + return __getTexture().SampleBias(__getSampler(), location, bias, offset, clamp, status); + } + } + [__readNone] [ForceInline] [require(glsl_hlsl_metal_spirv_wgsl, texture_shadowlod)] @@ -1163,6 +1179,23 @@ extension _Texture } } + + [__readNone] + [ForceInline] + [require(hlsl, sm_5_0)] + float SampleCmp(vector location, float compareValue, constexpr vector offset, float clamp, out uint status) + { + __requireComputeDerivative(); + __target_switch + { + case hlsl: + static_assert(T is float || T is vector || T is vector || T is vector + || T is half || T is vector || T is vector || T is vector + , "HLSL supports only float and half type textures"); + return __getTexture().SampleCmp(__getComparisonSampler(), location, compareValue, offset, clamp, status); + } + } + [__readNone] [ForceInline] [require(glsl_hlsl_metal_spirv_wgsl, texture_shadowlod)] @@ -1197,6 +1230,21 @@ extension _Texture } } + [__readNone] + [ForceInline] + [require(hlsl, sm_5_0)] + float SampleCmpLevelZero(vector location, float compareValue, constexpr vector offset, out uint status) + { + __target_switch + { + case hlsl: + static_assert(T is float || T is vector || T is vector || T is vector + || T is half || T is vector || T is vector || T is vector + , "HLSL supports only float and half type textures"); + return __getTexture().SampleCmpLevelZero(__getComparisonSampler(), location, compareValue, offset, status); + } + } + [__readNone] [ForceInline] [require(cpp_glsl_hlsl_metal_spirv_wgsl, texture_sm_4_0)] @@ -1281,6 +1329,21 @@ extension _Texture } } + [__readNone] + [ForceInline] + [require(hlsl, sm_5_0)] + T SampleGrad(vector location, vector gradX, vector gradY, constexpr vector offset, float lodClamp, out uint status) + { + __target_switch + { + case hlsl: + static_assert(T is float || T is vector || T is vector || T is vector + || T is half || T is vector || T is vector || T is vector + , "HLSL supports only float and half type textures"); + return __getTexture().SampleGrad(__getSampler(), location, gradX, gradY, offset, lodClamp, status); + } + } + [__readNone] [ForceInline] [require(cpp_cuda_glsl_hlsl_metal_spirv_wgsl, texture_sm_4_0)] @@ -1367,6 +1430,21 @@ extension _Texture return __getTexture().SampleLevel(__getSampler(), location, level, offset); } } + + [__readNone] + [ForceInline] + [require(hlsl, sm_5_0)] + T SampleLevel(vector location, float level, constexpr vector offset, out uint status) + { + __target_switch + { + case hlsl: + static_assert(T is float || T is vector || T is vector || T is vector + || T is half || T is vector || T is vector || T is vector + , "HLSL supports only float and half type textures"); + return __getTexture().SampleLevel(__getSampler(), location, level, offset, status); + } + } } // Non-combined texture types specific functions @@ -1783,6 +1861,22 @@ extension _Texture } } + [__readNone] + [ForceInline] + [require(hlsl, sm_5_0)] + T SampleBias(SamplerState s, vector location, float bias, constexpr vector offset, float clamp, out uint status) + { + __requireComputeDerivative(); + __target_switch + { + case hlsl: + static_assert(T is float || T is vector || T is vector || T is vector + || T is half || T is vector || T is vector || T is vector + , "HLSL supports only float and half type textures"); + __intrinsic_asm ".SampleBias"; + } + } + [__readNone] [ForceInline] [require(glsl_hlsl_metal_spirv_wgsl, texture_shadowlod)] @@ -1988,6 +2082,22 @@ extension _Texture } } + [__readNone] + [ForceInline] + [require(hlsl, sm_5_0)] + float SampleCmp(SamplerComparisonState s, vector location, float compareValue, constexpr vector offset, float clamp, out uint status) + { + __requireComputeDerivative(); + __target_switch + { + case hlsl: + static_assert(T is float || T is vector || T is vector || T is vector + || T is half || T is vector || T is vector || T is vector + , "HLSL supports only float and half type textures"); + __intrinsic_asm ".SampleCmp"; + } + } + [__readNone] [ForceInline] [require(glsl_hlsl_metal_spirv_wgsl, texture_shadowlod)] @@ -2055,6 +2165,21 @@ extension _Texture } } + [__readNone] + [ForceInline] + [require(hlsl, sm_5_0)] + float SampleCmpLevelZero(SamplerComparisonState s, vector location, float compareValue, constexpr vector offset, out uint status) + { + __target_switch + { + case hlsl: + static_assert(T is float || T is vector || T is vector || T is vector + || T is half || T is vector || T is vector || T is vector + , "HLSL supports only float and half type textures"); + __intrinsic_asm ".SampleCmpLevelZero"; + } + } + [__readNone] [ForceInline] [require(cpp_glsl_hlsl_metal_spirv_wgsl, texture_sm_4_0)] @@ -2235,6 +2360,21 @@ extension _Texture } } + [__readNone] + [ForceInline] + [require(hlsl, sm_5_0)] + T SampleGrad(SamplerState s, vector location, vector gradX, vector gradY, constexpr vector offset, float lodClamp, out uint status) + { + __target_switch + { + case hlsl: + static_assert(T is float || T is vector || T is vector || T is vector + || T is half || T is vector || T is vector || T is vector + , "HLSL supports only float and half type textures"); + __intrinsic_asm ".SampleGrad"; + } + } + [__readNone] [ForceInline] [require(cpp_cuda_glsl_hlsl_metal_spirv_wgsl, texture_sm_4_0)] @@ -2337,7 +2477,6 @@ extension _Texture [__readNone] [ForceInline] - [require(cpp_glsl_hlsl_metal_spirv_wgsl, texture_sm_4_0)] T SampleLevel(SamplerState s, vector location, float level, constexpr vector offset) { @@ -2399,6 +2538,21 @@ extension _Texture __intrinsic_asm "textureSampleLevel($0, $1, $2, $3, $4)$z"; } } + + [__readNone] + [ForceInline] + [require(hlsl, sm_5_0)] + T SampleLevel(SamplerState s, vector location, float level, constexpr vector offset, out uint status) + { + __target_switch + { + case hlsl: + static_assert(T is float || T is vector || T is vector || T is vector + || T is half || T is vector || T is vector || T is vector + , "HLSL supports only float and half type textures"); + __intrinsic_asm ".SampleLevel"; + } + } } // Texture.GetDimensions and Sampler.GetDimensions diff --git a/tests/hlsl-intrinsic/texture/texture-intrinsics.slang b/tests/hlsl-intrinsic/texture/texture-intrinsics.slang index 8ae3b2fda1..0543c71b28 100644 --- a/tests/hlsl-intrinsic/texture/texture-intrinsics.slang +++ b/tests/hlsl-intrinsic/texture/texture-intrinsics.slang @@ -43,11 +43,14 @@ void computeMain(int3 dispatchThreadID: SV_DispatchThreadID) uint numLevels = 0, elements = 0; float fnumLevels = 0.0f, felements = 0.0f; + float clamp = 0.0f; + float bias = 0.0f; + uint status; /*