Skip to content

Commit 53dd592

Browse files
authored
Fixing the wrong implementation of some math intrinsic (shader-slang#5491)
* Fixing the wrong implementation of some math intrinsic Close the issue shader-slang#5282 The root cause of the issue is that log10 is not supported in wgsl. So add the implementation. Also ldexp in wgsl doesn't support float type exponent, so fix the implementation of the intrinsic. * re-enable the tests
1 parent 7c2ff54 commit 53dd592

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

source/slang/hlsl.meta.slang

+8-4
Original file line numberDiff line numberDiff line change
@@ -9498,7 +9498,8 @@ T ldexp(T x, T exp)
94989498
__target_switch
94999499
{
95009500
case hlsl: __intrinsic_asm "ldexp";
9501-
case wgsl: __intrinsic_asm "ldexp";
9501+
// In WGSL spec, ldexp can only take integer as the exponent.
9502+
case wgsl: __intrinsic_asm "($0 * exp2($1))";
95029503
default:
95039504
return x * exp2(exp);
95049505
}
@@ -9512,7 +9513,8 @@ vector<T, N> ldexp(vector<T, N> x, vector<T, N> exp)
95129513
__target_switch
95139514
{
95149515
case hlsl: __intrinsic_asm "ldexp";
9515-
case wgsl: __intrinsic_asm "ldexp";
9516+
// In WGSL spec, ldexp can only take integer as the exponent.
9517+
case wgsl: __intrinsic_asm "($0 * exp2($1))";
95169518
default:
95179519
return x * exp2(exp);
95189520
}
@@ -9737,13 +9739,14 @@ matrix<T, N, M> log(matrix<T, N, M> x)
97379739
/// @category math
97389740
__generic<T : __BuiltinFloatingPointType>
97399741
[__readNone]
9740-
[require(cpp_cuda_glsl_hlsl_metal_spirv, sm_4_0_version)]
9742+
[require(cpp_cuda_glsl_hlsl_metal_spirv_wgsl, sm_4_0_version)]
97419743
T log10(T x)
97429744
{
97439745
__target_switch
97449746
{
97459747
case hlsl: __intrinsic_asm "log10";
97469748
case metal: __intrinsic_asm "log10";
9749+
case wgsl: __intrinsic_asm "(log( $0 ) * $S0( 0.43429448190325182765112891891661) )";
97479750
case glsl: __intrinsic_asm "(log( $0 ) * $S0( 0.43429448190325182765112891891661) )";
97489751
case cuda: __intrinsic_asm "$P_log10($0)";
97499752
case cpp: __intrinsic_asm "$P_log10($0)";
@@ -9760,13 +9763,14 @@ T log10(T x)
97609763

97619764
__generic<T : __BuiltinFloatingPointType, let N : int>
97629765
[__readNone]
9763-
[require(cpp_cuda_glsl_hlsl_metal_spirv, sm_4_0_version)]
9766+
[require(cpp_cuda_glsl_hlsl_metal_spirv_wgsl, sm_4_0_version)]
97649767
vector<T,N> log10(vector<T,N> x)
97659768
{
97669769
__target_switch
97679770
{
97689771
case hlsl: __intrinsic_asm "log10";
97699772
case metal: __intrinsic_asm "log10";
9773+
case wgsl: __intrinsic_asm "(log( $0 ) * $S0(0.43429448190325182765112891891661) )";
97709774
case glsl: __intrinsic_asm "(log( $0 ) * $S0(0.43429448190325182765112891891661) )";
97719775
case spirv:
97729776
{

tests/expected-failure-github.txt

-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ tests/hlsl-intrinsic/classify-float.slang.5 syn (wgpu)
6969
tests/hlsl-intrinsic/matrix-float.slang.6 syn (wgpu)
7070
tests/hlsl-intrinsic/matrix-int.slang.6 syn (wgpu)
7171
tests/hlsl-intrinsic/scalar-double-simple.slang.7 syn (wgpu)
72-
tests/hlsl-intrinsic/scalar-float.slang.5 syn (wgpu)
73-
tests/hlsl-intrinsic/vector-float.slang.5 syn (wgpu)
7472
tests/ir/string-literal-hash.slang.2 syn (wgpu)
7573
tests/language-feature/anonymous-struct.slang.1 syn (wgpu)
7674
tests/language-feature/constants/constexpr-loop.slang.2 syn (wgpu)

tests/hlsl-intrinsic/scalar-float.slang

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj
55
//DISABLED_TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj
66
//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj
7-
//DISABLE_TEST(compute):COMPARE_COMPUTE_EX:-wgpu
7+
//TEST(compute):COMPARE_COMPUTE_EX:-wgpu -compute -shaderobj
88

99
//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name outputBuffer
1010
RWStructuredBuffer<int> outputBuffer;

tests/hlsl-intrinsic/vector-float.slang

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -dx12 -use-dxil -output-using-type -shaderobj
44
//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -output-using-type -shaderobj
55
//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-cuda -compute -output-using-type -shaderobj
6-
//DISABLE_TEST(compute):COMPARE_COMPUTE_EX:-wgpu
6+
//TEST(compute):COMPARE_COMPUTE_EX:-wgpu -compute -output-using-type -shaderobj
77

88
//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], stride=4):out,name outputBuffer
99
RWStructuredBuffer<float4> outputBuffer;

0 commit comments

Comments
 (0)