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

Vertex shaders with derivative functions in dead code fail to compile on WebGL #4368

Closed
johanhelsing opened this issue Feb 22, 2022 · 5 comments
Labels
area: naga back-end Outputs of naga shader conversion lang: GLSL OpenGL Shading Language naga Shader Translator type: bug Something isn't working

Comments

@johanhelsing
Copy link

Description

If a wgsl shader contains a function with fwidth, dpdx or dpdy in it and that shader file is used as both a vertex and a fragment shader (just with different entry points), the shader fails in validation on WebGL even if the function is not used from the vertex shader.

i.e.

[[stage(vertex)]]
fn vertex(vertex: Vertex) -> VertexOutput {
    var out: VertexOutput;
    out.clip_position = view.view_proj * mesh.model * vec4<f32>(vertex.position, 1.0);
    out.color = vertex.color;
    return out;
}
struct FragmentInput {
    [[location(0)]] color: vec4<f32>;
};
// compiler errors when vertex shader compiles
fn fwidth_wrapper(color: vec3<f32>) -> vec3<f32> {
    return fwidth(color.rgb);
}
[[stage(fragment)]]
fn fragment(in: FragmentInput) -> [[location(0)]] vec4<f32> {
    // return vec4<f32>(fwidth(in.color.rgb), in.color.a); // <-- this works
    return vec4<f32>(fwidth_wrapper(in.color.rgb), in.color.a);
}

Repro steps

  1. Install wasm toolchain
  2. Check out https://github.com/johanhelsing/bevy/tree/wasm-fwidth-repro
  3. cargo install wasm-server-runner
  4. CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER=wasm-server-runner cargo run --target wasm32-unknown-unknown --example mesh2d_manual
  5. Open browser link and observe console log output

Expected behavior

I'd expect to see a pretty dark star (with some color on it) in the middle of the browser window.

Observed behavior

No star,

panicked at 'wgpu error: Validation Error

Caused by:
    In Device::create_render_pipeline
    Internal error in VERTEX shader: ERROR: 0:44: 'fwidth' : no matching overloaded function found
ERROR: 0:803: 'fwidth' : no matching overloaded function found

Extra materials

For what it's worth, it works fine on native (cargo run --example mesh2d_manual)

I don't understand the stack that well, but I assume this could be worked around by at some level trying to figure out whether fwidth and friends is called from a function that is unreachable by the entry point and in that case skip it.

Platform

  • WebGL, Firefox 97.0.1 Windows
  • wgpu v0.12.0
@kvark kvark transferred this issue from gfx-rs/wgpu Feb 22, 2022
@kvark kvark added area: naga back-end Outputs of naga shader conversion kind: bug lang: GLSL OpenGL Shading Language labels Feb 22, 2022
@kvark
Copy link
Member

kvark commented Feb 22, 2022

@JCapucho aren't we filtering out the functions unused by the entry point?

@JCapucho
Copy link
Collaborator

Hmm, weird seems like we at some point removed it and are just printing everything

@emilk
Copy link
Contributor

emilk commented Apr 15, 2023

I just hit this bug. We usually put the vertex shader and fragment shader in the same .wgsl file. Splitting them into two separate files solved the problem for us, as expected.

@james-j-obrien
Copy link

I have also hit this issue while trying to run https://github.com/james-j-obrien/bevy_vector_shapes on web.

Wumpf referenced this issue in rerun-io/rerun May 3, 2023
* transform cache now deals with Affine3 matrices only

* use a perspective camera in 2d views that sit at a space camera

* clarify/simplify use of focal length

* refine camera plane distance heuristic for 2D scenes and make it configurable again

* comment wip

* merge fixup and comment improvements

* line & point builder now work with affine transforms

* wip

* improved image plane heuristic for 2D

* hack for image plane distance for in inverse pinhole transforms. remove setting from ui again for 2d views

* add viewport transformation to viewbuilder

* better viewport transform

* limit zoom, correctly handle ui scale under viewport zoom

* 2D points now draw as real 2D circles

* better 2D rendering for lines with perspective camera around

* disable 3D labels in 2D views

* space camera no longer required for correct pinhole camera in ui_2d

* consistent canvas rect handling, take principal point into account when displaying 2d canvas

* comments on the nature of our interim 3D->2D solution

* point out that picking should use same transforms

* easier point/line flag building

* minor cleanup

* doc test fix

* clarify what sphere_quad's coverage methods do

* fix taking only one axis into account for pixel size approximation

* comment explaining how to use FORCE_ORTHO_SPANNING

* remove unnecessary affine3a multiply method

* impl From<glam::Affine3A> for wgpu_buffer_types::Mat4

* rename rect top_left to min

* remove unnecessary quaternion on pinhole transform calc

* make error swallowing on screenshots more explicit

* failure to compute camera now logs error and stops from rendering

* note on non-square pixels

* better handle different x & y focal length + comment

* renaming and tests around RectTransform

* yet another workaround for https://github.com/gfx-rs/naga/issues/1743

* remove h word
@cwfitzgerald cwfitzgerald added the naga Shader Translator label Oct 25, 2023
@cwfitzgerald cwfitzgerald transferred this issue from gfx-rs/naga Oct 25, 2023
@cwfitzgerald cwfitzgerald added type: bug Something isn't working and removed kind: bug labels Oct 25, 2023
@teoxoy
Copy link
Member

teoxoy commented Nov 2, 2023

This has been fixed by gfx-rs/naga#2531.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: naga back-end Outputs of naga shader conversion lang: GLSL OpenGL Shading Language naga Shader Translator type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants