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

Crash when creating render pipeline on Windows/Vulkan/NVIDIA with InstanceFlags::DEBUG #4614

Closed
yanchith opened this issue Oct 30, 2023 · 1 comment

Comments

@yanchith
Copy link
Contributor

Description
Turning on InstanceFlags::DEBUG with on the Vulkan backend on Windows creates an OOB access when compiling a shader. Doesn't crash on D3D12 or without the DEBUG flag.

Backtrace:

thread 'main' panicked at 'byte index 59 is out of bounds of ``': C:\Users\yanchith\.cargo\registry\s
rc\index.crates.io-6f17d22bba15001f\naga-0.14.0\src\span.rs:73
thread 'main' panicked at C:\Users\yanchith\.cargo\registry\src\index.crates.io-6f17d22bba15001f\naga-0.14.0\src\span.rs:73:29:
byte index 59 is out of bounds of ``
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/d627cf07ce46d230a93732a4714d16f00df9466b/library\std\src\panicking.rs:597
   1: core::panicking::panic_fmt
             at /rustc/d627cf07ce46d230a93732a4714d16f00df9466b/library\core\src\panicking.rs:72
   2: core::fmt::Arguments::new_v1
             at /rustc/d627cf07ce46d230a93732a4714d16f00df9466b/library\core\src\fmt\mod.rs:332
   3: core::str::slice_error_fail_rt
             at /rustc/d627cf07ce46d230a93732a4714d16f00df9466b/library\core\src\str\mod.rs:110
   4: core::str::slice_error_fail
             at /rustc/d627cf07ce46d230a93732a4714d16f00df9466b/library\core\src\str\mod.rs:87
   5: core::str::traits::impl$9::index
             at /rustc/d627cf07ce46d230a93732a4714d16f00df9466b\library\core\src\str\traits.rs:361
   6: core::str::traits::impl$4::index<core::ops::range::RangeTo<usize> >
             at /rustc/d627cf07ce46d230a93732a4714d16f00df9466b\library\core\src\str\traits.rs:61
   7: naga::span::Span::location
             at C:\Users\yanchith\.cargo\registry\src\index.crates.io-6f17d22bba15001f\naga-0.14.0\src\span.rs:73
   8: naga::back::spv::BlockContext::write_block
             at C:\Users\yanchith\.cargo\registry\src\index.crates.io-6f17d22bba15001f\naga-0.14.0\src\back\spv\block.rs:1759
   9: naga::back::spv::Writer::write_function
             at C:\Users\yanchith\.cargo\registry\src\index.crates.io-6f17d22bba15001f\naga-0.14.0\src\back\spv\writer.rs:711
  10: naga::back::spv::Writer::write_logical_layout
             at C:\Users\yanchith\.cargo\registry\src\index.crates.io-6f17d22bba15001f\naga-0.14.0\src\back\spv\writer.rs:1969
  11: naga::back::spv::Writer::write
             at C:\Users\yanchith\.cargo\registry\src\index.crates.io-6f17d22bba15001f\naga-0.14.0\src\back\spv\writer.rs:2040
  12: naga::back::spv::write_vec
             at C:\Users\yanchith\.cargo\registry\src\index.crates.io-6f17d22bba15001f\naga-0.14.0\src\back\spv\mod.rs:747
  13: wgpu_hal::vulkan::Device::compile_stage
             at C:\Users\yanchith\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-hal-0.18.0\src\vulkan\device.rs:758
  14: wgpu_hal::vulkan::device::impl$4::create_render_pipeline
             at C:\Users\yanchith\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-hal-0.18.0\src\vulkan\device.rs:1624
  15: wgpu_core::device::resource::Device<wgpu_hal::vulkan::Api>::create_render_pipeline<wgpu_hal::vulkan::Api,wgpu_core::identity::Identity
ManagerFactory>
             at C:\Users\yanchith\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-core-0.18.0\src\device\resource.rs:3119
  16: wgpu_core::global::Global<wgpu_core::identity::IdentityManagerFactory>::device_create_render_pipeline<wgpu_core::identity::IdentityMan
agerFactory,wgpu_hal::vulkan::Api>
             at C:\Users\yanchith\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-core-0.18.0\src\device\global.rs:1874
  17: wgpu::backend::direct::impl$7::device_create_render_pipeline
             at C:\Users\yanchith\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-0.18.0\src\backend\direct.rs:1210
  18: wgpu::context::impl$5::device_create_render_pipeline<wgpu::backend::direct::Context>
             at C:\Users\yanchith\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-0.18.0\src\context.rs:2334
  19: wgpu::Device::create_render_pipeline
             at C:\Users\yanchith\.cargo\registry\src\index.crates.io-6f17d22bba15001f\wgpu-0.18.0\src\lib.rs:2552
  20: g1_renderer_wgpu::WgpuRenderer::new<winit::window::Window>
             at .\g1_renderer_wgpu\src\lib.rs:487
  21: g1_platform_winit::main
             at .\g1_platform_winit\src\main.rs:413
  22: core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
             at /rustc/d627cf07ce46d230a93732a4714d16f00df9466b\library\core\src\ops\function.rs:250
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
error: process didn't exit successfully: `target\debug\g1_platform_winit.exe` (exit code: 101)

Vertex shader:

#version 450

layout(set = 0, binding = 0, std140) uniform Draw2DUniforms {
    uint u_screen_width;
    uint u_screen_height;
    // Uniform buffer structs are padded to a multiple of the size of a vec4.
    uint _pad1;
    uint _pad2;
};

layout(set = 1, binding = 0) uniform sampler u_sampler;
layout(set = 1, binding = 1) uniform texture2D u_color_texture;

layout(location = 0) in vec4 a_rect;
layout(location = 1) in vec4 a_texture_rect;
layout(location = 2) in uint a_color;

layout(location = 0) out vec2 v_tex_coord;
layout(location = 1) out vec4 v_color;

void main() {
    // Remap the pixel based coordinates to normalized NDC (for destination) and
    // UV (for source) coordinates. Note that we have to flip dst_y, because
    // WebGPU framebuffer is Y-up.
    float dst_width = 2.0 * a_rect.z / u_screen_width;
    float dst_height = 2.0 * a_rect.w / u_screen_height;
    float dst_x = 2.0 * a_rect.x / u_screen_width - 1.0;
    float dst_y = 2.0 * (1.0 - a_rect.y / u_screen_height) - 1.0 - dst_height;

    vec2 src_size = textureSize(sampler2D(u_color_texture, u_sampler), 0);
    vec4 src_rect = a_texture_rect / vec4(src_size.x,
                                          src_size.y,
                                          src_size.x,
                                          src_size.y);


    float dst_xmin = dst_x;
    float dst_ymin = dst_y;
    float dst_xmax = dst_x + dst_width;
    float dst_ymax = dst_y + dst_height;

    float src_xmin = src_rect.x;
    float src_ymin = src_rect.y;
    float src_xmax = src_rect.x + src_rect.z;
    float src_ymax = src_rect.y + src_rect.w;

    // Produce a quad (two CCW triangles) with tex coords in WebGPU NDC space.
    // (https://gpuweb.github.io/gpuweb/#coordinate-systems).
    //
    // 3----2
    // |    |
    // |    |
    // 0----1
    //
    // Texture coordinates use WebGPU texture coordinate system, where [0, 0] is
    // the first texel in image memory.

    switch (gl_VertexIndex % 6) {
    // 0, 1, 2
    case 0:
        v_tex_coord = vec2(src_xmin, src_ymax);
        gl_Position = vec4(dst_xmin, dst_ymin, 0, 1);
        break;
    case 1:
        v_tex_coord = vec2(src_xmax, src_ymax);
        gl_Position = vec4(dst_xmax, dst_ymin, 0, 1);
        break;
    case 2:
        v_tex_coord = vec2(src_xmax, src_ymin);
        gl_Position = vec4(dst_xmax, dst_ymax, 0, 1);
        break;

    // 2, 3, 0
    case 3:
        v_tex_coord = vec2(src_xmax, src_ymin);
        gl_Position = vec4(dst_xmax, dst_ymax, 0, 1);
        break;
    case 4:
        v_tex_coord = vec2(src_xmin, src_ymin);
        gl_Position = vec4(dst_xmin, dst_ymax, 0, 1);
        break;
    case 5:
        v_tex_coord = vec2(src_xmin, src_ymax);
        gl_Position = vec4(dst_xmin, dst_ymin, 0, 1);
        break;
    }

    v_color = vec4((a_color >> 24) & 0xff,
                   (a_color >> 16) & 0xff,
                   (a_color >> 8) & 0xff,
                   (a_color >> 0) & 0xff) / 255.0;
}

Fragment shader:

#version 450

layout(set = 0, binding = 0, std140) uniform Draw2DUniforms {
    uint u_screen_width;
    uint u_screen_height;
    // Uniform buffer structs are padded to a multiple of the size of a vec4.
    uint _pad1;
    uint _pad2;
};

layout(set = 1, binding = 0) uniform sampler u_sampler;
layout(set = 1, binding = 1) uniform texture2D u_color_texture;

layout(location = 0) in vec2 v_tex_coord;
layout(location = 1) in vec4 v_color;

layout(location = 0) out vec4 f_color;

void main() {
    f_color = v_color * texture(sampler2D(u_color_texture, u_sampler), v_tex_coord);
}

Repro steps
For now, I just had time to file the bug, but if it helps, tag me here, and I can make a repro repo or investigate myself in a few days.

Expected vs observed behavior
If there's something wrong with the shader, I'd like wgpu to tell me instead of crashing.

Platform
wgpu: 0.18.0
Windows 10, NVIDIA GeForce GTX 1650 (Vulkan)

@cwfitzgerald
Copy link
Member

Thanks for filing!

This seems like a dupe of #4569

@cwfitzgerald cwfitzgerald closed this as not planned Won't fix, can't repro, duplicate, stale Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants