Skip to content

[wgsl]: Add fmod(), frem() builtins - #1913

Closed
ben-clayton wants to merge 3 commits into
gpuweb:mainfrom
ben-clayton:fmod-frem
Closed

ben-clayton wants to merge 3 commits into
gpuweb:mainfrom
ben-clayton:fmod-frem

Conversation

@ben-clayton

Copy link
Copy Markdown
Contributor

And remove operator % for floats.

Fixes: #1696

And remove operator `%` for floats.

Fixes: gpuweb#1696
@kvark

kvark commented Jul 7, 2021

Copy link
Copy Markdown
Contributor

Where was the consensus reached to remove %? I don't remember it. This topic was discussed with a lot of movement in the positions, so it's hard to recall. My understanding was that we'd have % on all types strictly defined (in a way of FRem).

@ben-clayton

Copy link
Copy Markdown
Contributor Author

Where was the consensus reached to remove %? I don't remember it. This topic was discussed with a lot of movement in the positions, so it's hard to recall. My understanding was that we'd have % on all types strictly defined (in a way of FRem).

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.
I'd really like to settle on a solution for this, I don't particularly care which way we land.

@ben-clayton ben-clayton added the wgsl WebGPU Shading Language Issues label Jul 13, 2021
Comment thread wgsl/index.bs Resolved
See gpuweb#1913 (comment) for why.

Also remove OpFMod from the grammar for multiplicative_expression
@github-actions

Copy link
Copy Markdown
Contributor

Previews, as seen when this build job started (ed0f80b):
WebGPU | IDL
WGSL
Explainer

@kvark kvark left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Contents of the PR are LGTM

Comment thread wgsl/index.bs Resolved
... making clear that their mappings to  'OpFMod' and 'OpFRem' are not a mistake.
@github-actions

Copy link
Copy Markdown
Contributor

Previews, as seen when this build job started (abad5ba):
WebGPU | IDL
WGSL
Explainer

@kvark kvark left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need both of these methods? What would the MSL/HLSL implementation look like?

@ben-clayton

ben-clayton commented Jul 14, 2021 •

Copy link
Copy Markdown
Contributor Author

Why do we need both of these methods?

Because there are two SPIR-V ops. operator %(f32, f32) clearly only needs one, but I'm also addressing a TODO that we have a SPIR-V op that doesn't have anything to translate to.

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.

What would the MSL/HLSL implementation look like?

Combining the research from #1696 (comment) and #1696 (comment):

SPIR-V WGSL MSL HLSL
OpFRem fmod fmod %
OpFMod frem x-y*(floor(x/y)) x-y*(floor(x/y))

@kvark

kvark commented Jul 14, 2021

Copy link
Copy Markdown
Contributor

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.

SPIR-V has a whole ton of instructions, and we aren't having all of them. This ship has sailed a while ago.
So I'm not seeing substantial evidence to expose OpFMod to users, given that only 1 out of 3 target APIs support it, and it's fairly niche and easy to work around.

@ben-clayton

Copy link
Copy Markdown
Contributor Author

Okay. I have no strong options here.

Do we want OpFRem to map to a fmod built-in, or to a operator%(f32, f32) ?

@kvark

kvark commented Jul 14, 2021

Copy link
Copy Markdown
Contributor

We should talk about this again, it seems. Given that we only need one, it seems logical to me to just use % operator for this (on integers and floats alike).

@ben-clayton

Copy link
Copy Markdown
Contributor Author

We should talk about this again, it seems. Given that we only need one, it seems logical to me to just use % operator for this (on integers and floats alike).

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 % operator.

ben-clayton added a commit to ben-clayton/gpuweb that referenced this pull request Jul 15, 2021
@ben-clayton
ben-clayton deleted the fmod-frem branch July 16, 2021 20:59
@ben-clayton
ben-clayton restored the fmod-frem branch July 20, 2021 11:51
@ben-clayton ben-clayton reopened this Jul 20, 2021
@github-actions

Copy link
Copy Markdown
Contributor

Previews, as seen when this build job started (abad5ba):
WebGPU | IDL
WGSL
Explainer

ben-clayton added a commit that referenced this pull request Jul 21, 2021
Instead of `OpFMod`.

Fixes: #1696
See also: #1913
@kdashg

kdashg commented Aug 9, 2021

Copy link
Copy Markdown
Contributor
WGSL meeting minutes 2021-08-03
  • BC: The modulo operator for f32. We kept it open because David wanted builtins as well. He’s on vacation, though. Maybe we just punt
  • JG: I was told we have someone who speaks for david.
  • AB: I remember that not everyone wanted these builtins
  • DM: I was against them. One of the builtins was going to have to be implemented/polyfilled everywhere except spir-v. It has a very limited use. One is not useful. The other one is represented by an operator. Maybe we don’t need either.
  • DM: We can punt if we want
  • AB: We think there is value in having them. They’re used for different things.
  • AB: It’s not a difficult polyfill
  • DM: It would help if the SPIR-V didn’t end up being a polyfill anyway, in the driver.
  • MM: Yes. If it ends up being slower, that decreases the case for having them.
  • AB: Let’s defer for now
  • RESOLVED: Defer until post-MVP

@munrocket

Copy link
Copy Markdown
Contributor

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 x-y*(floor(x/y)) will be not readable and fmod() will be the only one global helper function that I will create. Unfortunately.

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).

@ben-clayton

Copy link
Copy Markdown
Contributor Author

@ben-clayton

Copy link
Copy Markdown
Contributor Author

(and now I see you're in fact the same person 😄)

@kvark

kvark commented Aug 26, 2021

Copy link
Copy Markdown
Contributor

@munrocket

As user of WGSL with WebGL background I prefer OpFMod (x-y*(floor(x/y)))
My small brain can't imagine situation when someone need OpFRem (look at green chart from wiki).

Looks like your approach is exceptional then, since neither HLSL or MSL offer a builtin function to do what you prefer.

Seems that inlining x-y*(floor(x/y)) will be not readable and fmod() will be the only one global helper function that I will create. Unfortunately.

You may add more global helpers in the future, or constants, or anything else. Your project is still evolving.

@munrocket

Copy link
Copy Markdown
Contributor

You right. I am tried to check HLSL/MSL code with the same functionality, and I found that they inlining floor or frac.
So I will continue without global helpers for now.

@ben-clayton

Copy link
Copy Markdown
Contributor Author

Closing this for now

@axinging

Copy link
Copy Markdown

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:

  // From https://github.com/tensorflow/tfjs/pull/5589/files
  fn mod(a : f32, b : f32) -> f32 {
    return a - b * floor(a/b);
  }
  fn modVec4(a : vec4<f32>, b : vec4<f32>) -> vec4<f32> {
    return a - b * floor(a/b);
  }

Based on our experience, it's straightforward to use % as mod. So we strongly agree % should have its conventional OpFMod behavior

@kainino0x

kainino0x commented Sep 10, 2021 •

Copy link
Copy Markdown
Contributor

@axinging sorry, I should have provided a different link for you to reply on. I'm going to copy your comment and reply on the issue #1696 instead of on this PR.

@ben-clayton
ben-clayton deleted the fmod-frem branch September 6, 2022 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wgsl WebGPU Shading Language Issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FMod vs FRem semantics

7 participants