Repository navigation
[wgsl]: Add fmod(), frem() builtins - #1913
ben-clayton wants to merge 3 commits into
Conversation
And remove operator `%` for floats. Fixes: gpuweb#1696
|
Where was the consensus reached to remove |
Yeah, I really wasn't sure of the consensus (see #1696 (comment) and my next comment), but I thought if I made the effort to post a PR, it might speed up the process a little. |
See gpuweb#1913 (comment) for why. Also remove OpFMod from the grammar for multiplicative_expression
kvark
left a comment
There was a problem hiding this comment.
Contents of the PR are LGTM
... making clear that their mappings to 'OpFMod' and 'OpFRem' are not a mistake.
kvark
left a comment
There was a problem hiding this comment.
Why do we need both of these methods? What would the MSL/HLSL implementation look like?
Because there are two SPIR-V ops. We could polyfill the other op when parsing the SPIR-V, but we've got a WGSL intrinsic for nearly every other SPIR-V op, so I didn't see why we wouldn't do the same here.
Combining the research from #1696 (comment) and #1696 (comment):
|
SPIR-V has a whole ton of instructions, and we aren't having all of them. This ship has sailed a while ago. |
|
Okay. I have no strong options here. Do we want |
|
We should talk about this again, it seems. Given that we only need one, it seems logical to me to just use |
I'm inclined to agree. I'll close this, and re-draft the original PR I put together last week that just cleans up the |
Instead of `OpFMod`. Fixes: gpuweb#1696 See also: gpuweb#1913
WGSL meeting minutes 2021-08-03
|
|
As user of WGSL with WebGL background I prefer OpFMod (x-y*(floor(x/y))). Here examples of code: // Changing color space
fn hsv2rgb(c: vec3<f32>) -> vec3<f32> {
let rgb = abs(((vec3<f32>(c.x * 6.) + vec3<f32>(0.,4.,2.)) % vec3<f32>(6.))-vec3<f32>(3.))-vec3<f32>(1.);
return c.z * mix(vec3<f32>(1.), clamp(rgb, vec3<f32>(0.), vec3<f32>(1.)), vec3<f32>(c.y));
}
// Infinite Repetition of 3d SDF objects
fn opInfArray(p: vec3<f32>, c: vec3<f32>) -> vec3<f32> {
return (p + 0.5 * c % c) - 0.5 * c;
}
// 2d sdf star. Polar finite repetition
fn sdStar(p: vec2<f32>, r: f32, n: u32, m: f32) ->f32 {
let an = 3.141593 / f32(n);
let en = 3.141593 / m;
let acs = vec2<f32>(cos(an), sin(an));
let ecs = vec2<f32>(cos(en), sin(en));
let bn = (atan2(p.x, p.y) % (2. * an)) - an;
var q: vec2<f32> = length(p) * vec2<f32>(cos(bn), abs(sin(bn)));
q = q - r * acs;
q = q + ecs * clamp( -dot(q, ecs), 0., r * acs.y / ecs.y);
return length(q) * sign(q.x);
}I can easily fix only polar finite repetition. Seems that inlining fn fmod3(x: vec3<f32>, y: vec3<f32>) -> vec3<f32> {
return x-y*(floor(x/y));
}
fn fmod1(x: f32, y: f32) -> f32 {
return x-y*(floor(x/y));
}My small brain can't imagine situation when someone need OpFRem (look at green chart from wiki). |
|
There has been a request in the Tint issue tracker for |
|
(and now I see you're in fact the same person 😄) |
Looks like your approach is exceptional then, since neither HLSL or MSL offer a builtin function to do what you prefer.
You may add more global helpers in the future, or constants, or anything else. Your project is still evolving. |
|
You right. I am tried to check HLSL/MSL code with the same functionality, and I found that they inlining |
|
Closing this for now |
|
We are working on converting glsl to wgsl in TFJS. In order to convert glsl mod to wgsl, we first use %, but it not work. Then we implement mod as below: Based on our experience, it's straightforward to use % as mod. So we strongly agree % should have its conventional OpFMod behavior |

And remove operator
%for floats.Fixes: #1696