From f5dfa1ed6a51809e8593f5f2abc292ab39f35dcb Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Mon, 11 May 2020 14:57:05 -0700 Subject: Add GLSL translation for HLSL fmod() (#1342) The existing code was assuming `fmod()` was available as a builtin in GLSL, which isn't true. It also isn't possible to translate the HLSL `fmod()` to the GLSL `mod()` since the two have slightly different semantics. This change introduces a definition for `fmod(x,y)` that amounts to `x - y*trunc(x/y)` which should agree with the HLSL version except in corner cases (e.g., there are some cases where the HLSL version returns `-0` and this one will return `0`). --- source/slang/hlsl.meta.slang | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'source') diff --git a/source/slang/hlsl.meta.slang b/source/slang/hlsl.meta.slang index c0dba51e3..fcd028b6e 100644 --- a/source/slang/hlsl.meta.slang +++ b/source/slang/hlsl.meta.slang @@ -1325,14 +1325,15 @@ matrix fma(matrix a, matrix b, matrix< // Floating point remainder of x/y __generic __target_intrinsic(hlsl) -__target_intrinsic(glsl) __target_intrinsic(cuda, "$P_fmod($0, $1)") __target_intrinsic(cpp, "$P_fmod($0, $1)") -T fmod(T x, T y); +T fmod(T x, T y) +{ + return x - y * trunc(x/y); +} __generic __target_intrinsic(hlsl) -__target_intrinsic(glsl) vector fmod(vector x, vector y) { VECTOR_MAP_BINARY(T, N, fmod, x, y); -- cgit v1.2.3