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

[glsl-out] fix usage of gsamplerCubeArrayShadow #4455

Closed
kvark opened this issue Dec 21, 2021 · 14 comments
Closed

[glsl-out] fix usage of gsamplerCubeArrayShadow #4455

kvark opened this issue Dec 21, 2021 · 14 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

@kvark
Copy link
Member

kvark commented Dec 21, 2021

from bevyengine/bevy#841 (comment)

Internal error in FRAGMENT shader: gsamplerCubeArrayShadow isn't supported in textureGrad, textureLod or texture with bias

@kvark kvark added kind: bug area: naga back-end Outputs of naga shader conversion lang: GLSL OpenGL Shading Language labels Dec 21, 2021
@magcius
Copy link
Contributor

magcius commented Dec 21, 2021

This is literally impossible -- the GLSL spec doesn't define any of those functions for gsamplerCubeArrayShadow. https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.4.60.pdf#page=183

Neither does WGSL: https://gpuweb.github.io/gpuweb/wgsl/#texturesamplecompare

What does the input shader look like?

@kvark
Copy link
Member Author

kvark commented Dec 21, 2021

We have a workaround that uses textureGrad instead of textureLod on cubemaps. It's probably involved:
https://github.com/gfx-rs/naga/blob/54178dede2a8b3f6304e3c71081db31189f786ee/src/back/glsl/mod.rs#L1963

@magcius
Copy link
Contributor

magcius commented Dec 21, 2021

That's after the code that's outputting that error.

@JCapucho
Copy link
Collaborator

JCapucho commented Jan 7, 2022

@kvark as magicus pointed out this is impossible, this workaround doesn't work on gsamplerCubeArrayShadow and the only functions that do support it are textureSize, texture and textureGather, but those would could potentially violate uniform control flow rules so I don't think there's anything we can do here.

@kvark
Copy link
Member Author

kvark commented Jan 7, 2022

If we can't do this, we should return an error from the backend (instead of generating bad code).

@magcius
Copy link
Contributor

magcius commented Jan 7, 2022

We're not generating bad code -- we're returning an error (admittedly a confusing one). What is the WGSL that's generating this error?

@kvark
Copy link
Member Author

kvark commented Jan 7, 2022

Damn, you are right.
@jakobhellermann could you provide the WGSL code for the shader triggering this? Or was it SPIR-V/GLSL source?

@jakobhellermann
Copy link
Contributor

The code in question is https://github.com/bevyengine/bevy/blob/963e2f08a2f762d896eb28ef4a80ba2ec124655b/crates/bevy_pbr/src/render/pbr.wgsl#L387.

Minimal example:

[[group(0), binding(2)]]
var point_shadow_textures: texture_depth_cube_array;
[[group(0), binding(3)]]
var point_shadow_textures_sampler: sampler_comparison;

[[stage(fragment)]]
fn main() {
    let frag_ls = vec3<f32>(0.0);
    let depth: f32 = 0.0;
    let value = textureSampleCompareLevel(point_shadow_textures, point_shadow_textures_sampler, frag_ls, i32(0), depth);
}
> naga repro.wgsl out.frag
gsamplerCubeArrayShadow isn't supported in textureGrad, textureLod or texture with bias

@magcius
Copy link
Contributor

magcius commented Jan 7, 2022

This could be supported if with this OpenGL extension: https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_texture_shadow_lod.txt

Obviously not going to work with WebGL, though. If you only have a single mipmap in your cube (not sure why you'd have more?), consider using textureSampleCompare instead. Another workaround would be to pack them into a depth texture array and do the 3D -> 2D array coordinate conversion in the shader.

@jakobhellermann
Copy link
Contributor

I believe the reason for using the textureSampleCompareLevel function instead of textureSampleCompare is that the latter must be called in uniform control flow, which isn't the case here.

// NOTE: Due to the non-uniform control flow above, we must use the Level variant of
// textureSampleCompare to avoid undefined behaviour due to some of the fragments in
// a quad (2x2 fragments) being processed not being sampled, and this messing with
// mip-mapping functionality. The shadow maps have no mipmaps so Level just samples
// from LOD 0.

@magcius
Copy link
Contributor

magcius commented Jan 7, 2022

boo... I understand the WGSL compiler can't possibly know that the texture has no mipmaps, but this sure is unfortunate. As a hack, Naga might be able to generate texture() in this case and pray it has no mipmaps? All options are unfortunate.

@allsey87

This comment was marked as off-topic.

@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
@magcius
Copy link
Contributor

magcius commented Dec 30, 2023

I realize immediately below the code that errors out is code that checks for TEXTURE_SHADOW_LOD. Assuming the user has support for TEXTURE_SHADOW_LOD, this shouldn't be impossible to fix.

@teoxoy
Copy link
Member

teoxoy commented Feb 8, 2024

#5171 should have resolved this, closing.

@teoxoy teoxoy closed this as completed Feb 8, 2024
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