Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle differences between the glsl mod function and the metal::fmod function. #802

Closed
expenses opened this issue Apr 29, 2021 · 2 comments · Fixed by #1867
Closed

Handle differences between the glsl mod function and the metal::fmod function. #802

expenses opened this issue Apr 29, 2021 · 2 comments · Fixed by #1867
Labels
area: back-end Outputs of shader conversion area: front-end Input formats for conversion kind: bug Something isn't working lang: SPIR-V Binary SPIR-V input and output

Comments

@expenses
Copy link
Contributor

Utgh.

When using spirv-cross on a spir-v file that uses OpFMod, it emits the following metal function:

// Implementation of the GLSL mod() function, which is slightly different than Metal fmod()
template<typename Tx, typename Ty>
inline Tx mod(Tx x, Ty y)
{
    return x - y * floor(x / y);
}

Currently we emit metal::fmod for OpFMod functions. This results in some glitches in shaders. For example, the shadertoy shader https://www.shadertoy.com/view/XsXXDn looks like this with spirv-cross (using repi/shadertoy-browser#63):

Screen Shot 2021-04-29 at 13 27 05

But like this with naga:

Screen Shot 2021-04-29 at 13 27 47

@expenses
Copy link
Contributor Author

expenses commented Apr 29, 2021

From https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf table 6.4, it looks like metal uses x - y * trunc(x / y), and trunc has slightly different semantics. We should probably have a comment saying what the semantics of the IR is meant to be.

@kvark
Copy link
Member

kvark commented Apr 29, 2021

Oh I see, so this is about the sign. Filed gpuweb/gpuweb#1696
Let's change our SPIR-V frontend/backend to use OpFRem instead of OpFMod here.

@kvark kvark added area: back-end Outputs of shader conversion area: front-end Input formats for conversion kind: bug Something isn't working lang: SPIR-V Binary SPIR-V input and output labels Apr 29, 2021
@kvark kvark changed the title [msl-out] Handle differences between the glsl mod function and the metal::fmod function. Handle differences between the glsl mod function and the metal::fmod function. Apr 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: back-end Outputs of shader conversion area: front-end Input formats for conversion kind: bug Something isn't working lang: SPIR-V Binary SPIR-V input and output
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants