Fix signed % wrong for negative operands in GLSL (OpenGL/GLES)#9687
Open
mstampfli wants to merge 1 commit into
Open
Fix signed % wrong for negative operands in GLSL (OpenGL/GLES)#9687mstampfli wants to merge 1 commit into
% wrong for negative operands in GLSL (OpenGL/GLES)#9687mstampfli wants to merge 1 commit into
Conversation
b72b897 to
cb14c32
Compare
…ned) WGSL defines signed integer `%` for all non-degenerate operands, but the GLSL backend emitted a raw `a % b`. GLSL leaves `%` undefined when either operand is negative (GLSL spec 5.9), so `-1 % 768` returned `255` instead of `-1` on OpenGL/GLES (confirmed on NVIDIA). Same class of bug as gfx-rs#8191 on Vulkan, just a different backend. Integer division `/` truncates toward zero and is well defined, so lower signed and unsigned integer `%` as `a - b * (a / b)`, mirroring the HLSL and Metal backends. Float modulo is unchanged. Verified on an NVIDIA RTX 5060 GL driver: negative dividends now match the CPU reference (e.g. -1 % 3 == -1, -1 % 768 == -1).
% for negative operands% wrong for negative operands in GLSL (OpenGL/GLES)
cb14c32 to
82844e3
Compare
Collaborator
|
This is a great bug report, thanks for filing. Will take a look at the code soon. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sibling of #9674 and the same class of bug as #8191: signed integer
%wrong for negative operands, this time in the GLSL (OpenGL/GLES) backend instead of SPIR-V.Problem
WGSL defines signed integer
%for all non-degenerate operands.-1 % 768must be-1(truncated remainder, sign of the dividend; WGSL 8.7). But rendered through the GLSL backend it returns255, i.e.(-1 as u32) % 768.naga's GLSL backend emits a raw
a % bfor integer modulo. GLSL leaves%undefined when either operand is negative (GLSL spec 5.9), so the result is implementation defined. HLSL and Metal already reconstruct around this; GLSL did not. Same gap as #8191 on Vulkan, different backend.Evidence
WGSL
a % brun through the GLSL backend on an NVIDIA RTX 5060 OpenGL driver (610.43.02), before this change:Every negative dividend was wrong (32 of the 64 tested values), the same unsigned-fold symptom as the Vulkan case.
Fix
Integer division
/truncates toward zero and is well defined in GLSL, so signed and unsigned%is lowered asa - b * (a / b), exactly mirroring the existing HLSL and Metal backends. Float modulo (already atrunc-based reconstruction) is unchanged.Unlike the Vulkan fix this is unconditional rather than driver gated: GLSL's
%is undefined for negative operands per spec, so it affects every OpenGL/GLES target, not one vendor.Testing
operatorsandimageGLSL outputs change (integer%becomes the reconstruction). HLSL, MSL, SPIR-V, and WGSL outputs are byte-for-byte unchanged.operators.wgslalready covers scalar, vector, and mixed scalar/vector modulo for signed and unsigned ints.-1 % 3 == -1,-2 % 3 == -2,-1 % 768 == -1), 0 mismatches across the tested range.Squash or rebase? Squash.