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

Derivatives of centroid-interpolated variables #4854

Open
kainino0x opened this issue Sep 5, 2024 · 5 comments
Open

Derivatives of centroid-interpolated variables #4854

kainino0x opened this issue Sep 5, 2024 · 5 comments
Assignees
Labels
copyediting Pure editorial stuff (copyediting, *.bs file syntax, etc.) wgsl WebGPU Shading Language Issues
Milestone

Comments

@kainino0x
Copy link
Contributor

kainino0x commented Sep 5, 2024

This very useful 2007 article about centroid shading says that - in OpenGL in 2007 - derivatives on centroid-interpolated variables have undefined results: https://www.opengl.org/pipeline/article/vol003_6/

When you need accurate derivatives (explicit or implied, such as with mipmapped or anisotropic texture fetches). The shading language specification considers derivatives derived from centroid varings to be so fraught with inaccuracy that it was resolved they are simply undefined. In such a case, strongly consider at least adding: centroid varying float myMixer; // beware of derivative!

Vulkan is a bit less harsh than that, at least in this non-normative note (I couldn't find normative text):

As Centroid restricts the possible interpolation position to the covered area of the primitive, the position can be forced to vary between neighboring fragments when it otherwise would not. Derivatives calculated based on these differing locations can produce inconsistent results compared to undecorated inputs. It is recommended that input variables used in derivative calculations are not decorated with Centroid.

FWIW, D3D is very strict about centroid interpolation, so it probably doesn't have issues with derivatives: https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm#3.5.5%20Centroid%20Sampling%20of%20Attributes

But anyway, should WGSL say this is a dynamic error? Should it be a compile-time warning or error? Or is it fine to allow?

@kainino0x kainino0x added the wgsl WebGPU Shading Language Issues label Sep 5, 2024
@kainino0x kainino0x added this to the Milestone 0 milestone Sep 5, 2024
@alan-baker
Copy link
Contributor

Am I interpreting correctly that it's not that the derivatives are wrong, but that the values of the inputs might not be what authors anticipate? If that's the case I wouldn't want to change derivatives, but it might make sense to add a note to centroid sampling that calls out the situation in some way.

@kainino0x
Copy link
Contributor Author

That's my understanding of Vulkan, yes. I would be surprised if OpenGL is ever actually worse than that in practice, even if the spec allows that (or used to allow it - I didn't dig up the current OpenGL spec text).

@jimblandy
Copy link
Contributor

This sounds like a note in the spec is sufficient?

@kdashg
Copy link
Contributor

kdashg commented Sep 17, 2024

Yeah it's probably useful to as an example call out that "because centroids are not necessarily one pixel width apart, derivative functions might not have the values you expect".

@dneto0
Copy link
Contributor

dneto0 commented Sep 17, 2024

MSL and Vulkan wording is simliar to what exists in WGSL:

MSL: (5.4 sampling and interpolation attributes)

If a centroid attribute variant is specified (centroid_perspective and
centroid_no_perspective), the interpolation point sampled needs to be within both the
primitive and the centroid of the pixel.

Vulkan: 9.12 interpolation decorations (the normative above the paragraph Kai quoted):

If the Centroid decoration is used, the interpolation position used for the variable must also fall within the bounds of the primitive being rasterized.

WGSL:

centroid: Interpolation is performed at a point that lies within all the samples covered by the fragment within the current primitive. This value is the same for all samples in the primitive.

D3D is more prescriptive about exactly what sample location is chosen, while the others allow a lot more choices, potentially.

But all three have the "you're not likely to get something intuitive" vibe. I'd go with a note at best.

@kdashg kdashg modified the milestones: Milestone 0, Milestone 1 Sep 17, 2024
@kdashg kdashg added the copyediting Pure editorial stuff (copyediting, *.bs file syntax, etc.) label Sep 17, 2024
@alan-baker alan-baker self-assigned this Sep 17, 2024
@alan-baker alan-baker added this to WGSL Sep 17, 2024
@alan-baker alan-baker moved this to Resolved: Needs Specification Work in WGSL Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
copyediting Pure editorial stuff (copyediting, *.bs file syntax, etc.) wgsl WebGPU Shading Language Issues
Projects
Status: Resolved: Needs Specification Work
Development

No branches or pull requests

5 participants