Skip to content

Commit

Permalink
Fix WGSL frexp and modf that returns a struct (#5096)
Browse files Browse the repository at this point in the history
Two WGSL functions have little different behavior compared to other
shader languages: frexp and modf.  They return a struct to return
two values.
  • Loading branch information
jkwak-work authored Sep 18, 2024
1 parent 0716646 commit 2f455d3
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions source/slang/hlsl.meta.slang
Original file line number Diff line number Diff line change
Expand Up @@ -8658,10 +8658,21 @@ T frexp(T x, out int exp)
case spirv: return spirv_asm {
result:$$T = OpExtInst glsl450 Frexp $x &exp
};
case wgsl: __intrinsic_asm "frexp";
case wgsl:
T fract;
__wgsl_frexp<T>(x, fract, exp);
return fract;
}
}

__generic<T : __BuiltinFloatingPointType>
[__readNone]
[require(wgsl)]
void __wgsl_frexp(T x, out T fract, out int exp)
{
__intrinsic_asm "{ var s = frexp($0); $1 = s.fract; $2 = s.exp; }";
}

__generic<T : __BuiltinFloatingPointType, let N : int>
[__readNone]
[require(cpp_cuda_glsl_hlsl_metal_spirv_wgsl, sm_4_0_version)]
Expand All @@ -8675,7 +8686,6 @@ vector<T, N> frexp(vector<T, N> x, out vector<int, N> exp)
case spirv: return spirv_asm {
result:$$vector<T, N> = OpExtInst glsl450 Frexp $x &exp
};
case wgsl: __intrinsic_asm "frexp";
default:
VECTOR_MAP_BINARY(T, N, frexp, x, exp);
}
Expand Down Expand Up @@ -11055,10 +11065,21 @@ T modf(T x, out T ip)
case spirv: return spirv_asm {
result:$$T = OpExtInst glsl450 Modf $x &ip
};
case wgsl: __intrinsic_asm "modf";
case wgsl:
T fract;
__wgsl_modf<T>(x, fract, ip);
return fract;
}
}

__generic<T : __BuiltinFloatingPointType>
[__readNone]
[require(wgsl)]
void __wgsl_modf(T x, out T fract, out T whole)
{
__intrinsic_asm "{ var s = modf($0); $1 = s.fract; $2 = s.whole; }";
}

__generic<T : __BuiltinFloatingPointType, let N : int>
[__readNone]
[require(cpp_cuda_glsl_hlsl_metal_spirv_wgsl, sm_4_0_version)]
Expand All @@ -11072,7 +11093,6 @@ vector<T,N> modf(vector<T,N> x, out vector<T,N> ip)
case spirv: return spirv_asm {
result:$$vector<T,N> = OpExtInst glsl450 Modf $x &ip
};
case wgsl: __intrinsic_asm "modf";
default:
VECTOR_MAP_BINARY(T, N, modf, x, ip);
}
Expand Down

0 comments on commit 2f455d3

Please sign in to comment.