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

WGSL parser error in generated shader while implementing ISF #4341

Closed
mitchmindtree opened this issue Jun 8, 2021 · 3 comments
Closed

WGSL parser error in generated shader while implementing ISF #4341

mitchmindtree opened this issue Jun 8, 2021 · 3 comments
Labels
naga Shader Translator type: question Further information is requested

Comments

@mitchmindtree
Copy link
Contributor

Hi folks, I'm updating this PR (implementing ISF) to wgpu 0.8 and running into some parser errors with the shader code generated via the generated GLSL.

Here's an example of an ISF-generated GLSL shader that, when compiled to SPIR-V (via the glsl-to-spirv crate) results in a WGSL parser error when loading the shader module (please disregard the odd formatting, this is generated from one of the original ISF test shaders):

/*{
        "DESCRIPTION": "demonstrates the use of float-type inputs",
        "CREDIT": "by zoidberg",
        "ISFVSN": "2.0",
        "CATEGORIES": [
                "TEST-GLSL FX"
        ],
        "INPUTS": [
                {
                        "NAME": "inputImage",
                        "TYPE": "image"
                },
                {
                        "NAME": "level",
                        "TYPE": "float",
                        "DEFAULT": 0.5,
                        "MIN": 0.0,
                        "MAX": 1.0
                }
        ]
}*/

// Generated ISF GLSL begins here.

#version 450                                                                                                           

layout(location = 0) out vec4 FragColor;                                                                               
layout(location = 0) in vec2 isf_FragNormCoord;                                                                        
                                                                                                                       
layout(set = 0, binding = 0) uniform IsfData {                                                                         
    vec4 DATE;                                        
    vec2 RENDERSIZE;                     
    float TIME;                                   
    float TIMEDELTA;
    int PASSINDEX;
    int FRAMEINDEX;                                   
};                                  
                                                           
layout(set = 1, binding = 0) uniform IsfDataInputs {
    float level;
};                                                                                                                     
                                                           
layout(set = 2, binding = 0) uniform sampler img_sampler;
layout(set = 2, binding = 1) uniform texture2D inputImage;
                                                           
// ISF provided short-hand for retrieving image size.
ivec2 IMG_SIZE(texture2D img) {
    return textureSize(sampler2D(img, img_sampler), 0);
}                                            
                                                           
// ISF provided short-hand for retrieving image color.
vec4 IMG_NORM_PIXEL(texture2D img, vec2 norm_px_coord) {
    return texture(sampler2D(img, img_sampler), norm_px_coord);
}                                       
                                                           
// ISF provided short-hand for retrieving image color.
vec4 IMG_PIXEL(texture2D img, vec2 px_coord) {
    ivec2 s = IMG_SIZE(img);
    vec2 norm_px_coord = vec2(px_coord.x / float(s.x), px_coord.y / float(s.y));
    return IMG_NORM_PIXEL(img, px_coord);
}

// ISF provided short-hand for retrieving image color.
vec4 IMG_THIS_NORM_PIXEL(texture2D img) {
    return IMG_NORM_PIXEL(img, isf_FragNormCoord);
}

// ISF provided short-hand for retrieving image color.
vec4 IMG_THIS_PIXEL(texture2D img) {
    return IMG_THIS_NORM_PIXEL(img);
}

// Generated ISF GLSL ends here.

void main()
{
        vec4            srcPixel = IMG_THIS_PIXEL(inputImage);
        float           luma = (srcPixel.r+srcPixel.g+srcPixel.b)/3.0;
        vec4            dstPixel = (luma>level) ? srcPixel : vec4(0,0,0,1);
        FragColor = dstPixel;
}

And the resulting error:

wgpu error: Validation Error                                                                                           
                                                                                                                       
Caused by:                                                                                                             
    In Device::create_shader_module                                                                                    
      note: label = `nannou_shader_module`                                                                             
    Failed to parse WGSL                                                                                               
                                                                                                                       
                                                                                                                       
thread 'main' panicked at 'Handling wgpu errors as fatal by default', /home/mindtree/.cargo/registry/src/github.com-1ecc6299db9ec823/wgpu-0.8.1/src/backend/direct.rs:1955:5                                                                   
stack backtrace:                                                                                                       
   0: std::panicking::begin_panic                                                                                      
             at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0/library/std/src/panicking.rs:519:12                    
   1: wgpu::backend::direct::default_error_handler                                                                                                                                                                                             
             at /home/mindtree/.cargo/registry/src/github.com-1ecc6299db9ec823/wgpu-0.8.1/src/backend/direct.rs:1955:5                                                                                                                         
   2: core::ops::function::Fn::call                                                                                    
             at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0/library/core/src/ops/function.rs:70:5                  
   3: <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call                                                  
             at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0/library/alloc/src/boxed.rs:1535:9                      
   4: wgpu::backend::direct::ErrorSinkRaw::handle_error                                                                
             at /home/mindtree/.cargo/registry/src/github.com-1ecc6299db9ec823/wgpu-0.8.1/src/backend/direct.rs:1942:9 
   5: wgpu::backend::direct::Context::handle_error                                                                     
             at /home/mindtree/.cargo/registry/src/github.com-1ecc6299db9ec823/wgpu-0.8.1/src/backend/direct.rs:93:9   
   6: <wgpu::backend::direct::Context as wgpu::Context>::device_create_shader_module                                                                                                                                                           
             at /home/mindtree/.cargo/registry/src/github.com-1ecc6299db9ec823/wgpu-0.8.1/src/backend/direct.rs:819:13                                                                                                                         
   7: wgpu::Device::create_shader_module                                                                                                                                                                                                       
             at /home/mindtree/.cargo/registry/src/github.com-1ecc6299db9ec823/wgpu-0.8.1/src/lib.rs:1537:17           
   8: nannou::wgpu::shader_from_spirv_bytes                                                                            
             at /home/mindtree/programming/rust/nannou/nannou/src/wgpu/mod.rs:95:5                                                                                                                                                             
   9: nannou_isf::pipeline::compile_isf_shader::{{closure}}                                                                                                                                                                                    
             at /home/mindtree/programming/rust/nannou/nannou_isf/src/pipeline.rs:440:32                               
  10: core::option::Option<T>::map                                                                                     
             at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0/library/core/src/option.rs:487:29                      
  11: nannou_isf::pipeline::compile_isf_shader                                                                         
             at /home/mindtree/programming/rust/nannou/nannou_isf/src/pipeline.rs:440:18                               
  12: nannou_isf::pipeline::Shader::fragment_from_path                                                                 
             at /home/mindtree/programming/rust/nannou/nannou_isf/src/pipeline.rs:459:31                               
  13: nannou_isf::pipeline::IsfPipeline::new                                                                           
             at /home/mindtree/programming/rust/nannou/nannou_isf/src/pipeline.rs:519:18                               
  14: isf_demo::create_isf_state                                                                                       
             at /home/mindtree/programming/rust/nannou/examples/isf/isf_demo.rs:471:20                                 
  15: isf_demo::model::{{closure}}                                                                                                                                                                                                             
             at /home/mindtree/programming/rust/nannou/examples/isf/isf_demo.rs:101:24                                 
  16: core::option::Option<T>::map                                                                                     
             at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0/library/core/src/option.rs:487:29                      
  17: isf_demo::model                                                                                                                                                                                                                          
             at /home/mindtree/programming/rust/nannou/examples/isf/isf_demo.rs:98:15                                                                                                                                                          
  18: nannou::app::Builder<M,E>::run                                                                                   
             at /home/mindtree/programming/rust/nannou/nannou/src/app.rs:484:21                                        
  19: isf_demo::main                                                                                                   
             at /home/mindtree/programming/rust/nannou/examples/isf/isf_demo.rs:10:5                                   
  20: core::ops::function::FnOnce::call_once                                                                           
             at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0/library/core/src/ops/function.rs:227:5

The source of the issue appears to be this line:

        vec4            srcPixel = IMG_THIS_PIXEL(inputImage);

If I replace this with something like

vec4 srcPixel = vec4(0);

the shader appears to compile successfully. If I inline the IMG_THIS_PIXEL function as follows:

vec4 srcPixel = texture(sampler2D(inputImage, img_sampler), isf_FragNormCoord);

I no longer run into a WGSL parser error.

That said, I do run into the following validation error, though I'm not yet sure if this is related to the parser error - investigating now.

wgpu error: Validation Error   
                                                           
Caused by:                                   
    In Device::create_render_pipeline                      
      note: label = `nannou render pipeline`          
    error matching FRAGMENT shader requirements against the pipeline                                                   
    shader global ResourceBinding { group: 2, binding: 1 } is not available in the layout pipeline layout              
    binding is missing from the pipeline layout
                                                           
                                                           
thread 'main' panicked at 'Handling wgpu errors as fatal by default', /home/mindtree/.cargo/registry/src/github.com-1ecc6299db9ec823/wgpu-0.8.1/src/backend/direct.rs:1955:5                                                                   
stack backtrace:            
   0: std::panicking::begin_panic                                                                                      
             at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0/library/std/src/panicking.rs:519:12                    
   1: wgpu::backend::direct::default_error_handler         
             at /home/mindtree/.cargo/registry/src/github.com-1ecc6299db9ec823/wgpu-0.8.1/src/backend/direct.rs:1955:5 
   2: core::ops::function::Fn::call                   
             at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0/library/core/src/ops/function.rs:70:5                  
   3: <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call                                                  
             at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0/library/alloc/src/boxed.rs:1535:9                      
   4: wgpu::backend::direct::ErrorSinkRaw::handle_error                                                                
             at /home/mindtree/.cargo/registry/src/github.com-1ecc6299db9ec823/wgpu-0.8.1/src/backend/direct.rs:1942:9 
   5: wgpu::backend::direct::Context::handle_error
             at /home/mindtree/.cargo/registry/src/github.com-1ecc6299db9ec823/wgpu-0.8.1/src/backend/direct.rs:93:9   
   6: <wgpu::backend::direct::Context as wgpu::Context>::device_create_render_pipeline                                 
             at /home/mindtree/.cargo/registry/src/github.com-1ecc6299db9ec823/wgpu-0.8.1/src/backend/direct.rs:1052:13                                                                                                                        
   7: wgpu::Device::create_render_pipeline                                                                             
             at /home/mindtree/.cargo/registry/src/github.com-1ecc6299db9ec823/wgpu-0.8.1/src/lib.rs:1595:17           
   8: nannou::wgpu::render_pipeline_builder::build       
             at /home/mindtree/programming/rust/nannou/nannou/src/wgpu/render_pipeline_builder.rs:564:5                
   9: nannou::wgpu::render_pipeline_builder::RenderPipelineBuilder::build                                              
             at /home/mindtree/programming/rust/nannou/nannou/src/wgpu/render_pipeline_builder.rs:484:40               
  10: nannou_isf::pipeline::create_render_pipeline
             at /home/mindtree/programming/rust/nannou/nannou_isf/src/pipeline.rs:947:5                                
  11: nannou_isf::pipeline::IsfPipeline::new 
             at /home/mindtree/programming/rust/nannou/nannou_isf/src/pipeline.rs:607:50                               
  12: isf_demo::create_isf_state                      
             at /home/mindtree/programming/rust/nannou/examples/isf/isf_demo.rs:471:20                                 
  13: isf_demo::model::{{closure}}                                                                                     
             at /home/mindtree/programming/rust/nannou/examples/isf/isf_demo.rs:101:24                                 
  14: core::option::Option<T>::map                         
             at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0/library/core/src/option.rs:487:29                      
  15: isf_demo::model                                                                                                                                                                                                                          
             at /home/mindtree/programming/rust/nannou/examples/isf/isf_demo.rs:98:15                                  
  16: nannou::app::Builder<M,E>::run                                                                                   
             at /home/mindtree/programming/rust/nannou/nannou/src/app.rs:484:21                                        
  17: isf_demo::main                                       
             at /home/mindtree/programming/rust/nannou/examples/isf/isf_demo.rs:10:5                                   
  18: core::ops::function::FnOnce::call_once          
             at /rustc/2fd73fabe469357a12c2c974c140f67e7cdd76d0/library/core/src/ops/function.rs:227:5                 

Fwiw, this test example did kind of "work" prior to wgpu 0.8, however

Tested with wgpu 0.8.1 on NixOS 21.05.

@mitchmindtree
Copy link
Contributor Author

OK, I've fixed the "missing binding" error however the original WGSL parser error still persists.

For future reference, what's the easiest way to check the generated WGSL code? How do you approach debugging parser errors like this?

@kvark
Copy link
Member

kvark commented Jun 8, 2021

Thank you for filing!
The first error is gfx-rs/naga#950. Sorry for it being totally unclear!
You are passing a texture in a function, and we don't like that yet. But we want to like it :)

The way we generally debug this is - doing RUST_LOG=warn cargo run separately with naga on the shader. It often tells what's going on. But really, the errors need to be reported better. The current state of it is not acceptable by any means.

@cwfitzgerald cwfitzgerald transferred this issue from gfx-rs/naga Oct 25, 2023
@cwfitzgerald cwfitzgerald added naga Shader Translator type: question Further information is requested and removed kind: question labels Oct 25, 2023
@teoxoy
Copy link
Member

teoxoy commented Nov 3, 2023

gfx-rs/naga#1022 fixed gfx-rs/naga#950, closing.

@teoxoy teoxoy closed this as completed Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
naga Shader Translator type: question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants