gfx-rs / wgpu

A cross-platform, safe, pure-Rust graphics API.
https://wgpu.rs
Apache License 2.0
12.79k stars 938 forks source link

A shader is causing a vulkan validation error followed by segmentation fault #5302

Open amokfa opened 9 months ago

amokfa commented 9 months ago

Description A shader is causing a vulkan validation error saying "SPIR-V module not valid: Uniform id '9' is missing Block or BufferBlock decoration."

Repro steps This is the shader:

struct CameraInfo {
    pos: vec2<f32>,
    size: vec2<f32>,
}

struct SpriteCoords {
    pos: vec2<f32>,
    size: vec2<f32>,
    rot: f32,
}

struct SpritePieceInfo {
    this_count: u32,
    padding1: u32,
    total_size: vec2<f32>,
    final_offset: vec2<f32>,
    final_size: vec2<f32>,
    bottom_left: vec2<f32>,
    top_right: vec2<f32>,
    padding2: vec2<f32>,
}

@group(0) @binding(0) var<uniform> camera: CameraInfo;
@group(0) @binding(1) var<uniform> sprite_coords: SpriteCoords;

@group(1) @binding(0) var colormaps: binding_array<texture_2d<f32>, 1>;
@group(1) @binding(1) var alphamaps: binding_array<texture_2d<f32>, 1>;
@group(1) @binding(2) var mSampler: sampler;
@group(1) @binding(3) var<uniform> crops: binding_array<SpritePieceInfo, 1>;

struct VertexOutput {
    @builtin(position) position: vec4<f32>,
    @location(0) coords: vec2<f32>,
}

@fragment
fn fs_main(i: VertexOutput) -> @location(0) vec4<f32> {
    var crop = crops[0];

//    crop.total_size = vec2<f32>(895.0, 649.0);
//    crop.final_offset = vec2<f32>(179.0, 0.0);
//    crop.final_size = vec2<f32>(501.0, 649.0);
//    crop.bottom_left = vec2<f32>(0.002441, 0.364746);
//    crop.top_right = vec2<f32>(0.490723, 0.997559);

    let crop_start = crop.final_offset / crop.total_size;
    let crop_end = crop_start + crop.final_size / crop.total_size;
    if (all(i.coords > crop_start) && all(i.coords < crop_end)) {
        let scale = (crop.final_size.x / crop.total_size.x);
        var tex_coords_1 = (i.coords - crop_start) / (crop_end - crop_start);
        var tex_coords_2 = tex_coords_1 * (crop.top_right - crop.bottom_left) + crop.bottom_left;
        tex_coords_2.y = 1.0 - tex_coords_2.y;
        var col =  textureSample(colormaps[0], mSampler, tex_coords_2);
        let alpha = textureSample(alphamaps[0], mSampler, tex_coords_2);
        col.a = alpha.r;
        return col;
    } else {
        return vec4<f32>(0.0, 0.0, 0.0, 0.0);
    }
}

Validation messages:

[2024-02-25T11:27:15Z WARN  wgpu_hal::vulkan::instance] GENERAL [Loader Message (0x0)]
        terminator_CreateInstance: Failed to CreateInstance in ICD 3.  Skipping ICD.
[2024-02-25T11:27:15Z WARN  wgpu_hal::vulkan::instance]         objects: (type: INSTANCE, hndl: 0x6387d4b84370, name: ?)
[2024-02-25T11:27:15Z ERROR wgpu_core::device::resource] Feature 'trace' is not enabled
[2024-02-25T11:27:15Z ERROR wgpu_hal::vulkan::instance] VALIDATION [UNASSIGNED-CoreValidation-Shader-InconsistentSpirv (0x6bbb14)]
        Validation Error: [ UNASSIGNED-CoreValidation-Shader-InconsistentSpirv ] Object 0: handle = 0x6387d52676f0, type = VK_OBJECT_TYPE_DEVICE; | MessageID = 0x6bbb14 | SPIR-V module not valid: Uniform id '9' is missing Block or BufferBlock decoration.
    From Vulkan spec, section 14.5.2:
    Such variables must be identified with a Block or BufferBlock decoration
      %SpritePieceInfo = OpTypeStruct %uint %uint %v2float %v2float %v2float %v2float %v2float %v2float

[2024-02-25T11:27:15Z ERROR wgpu_hal::vulkan::instance]         objects: (type: DEVICE, hndl: 0x6387d52676f0, name: ?)
[2024-02-25T11:27:15Z ERROR wgpu_hal::vulkan::instance] VALIDATION [VUID-VkGraphicsPipelineCreateInfo-layout-00756 (0x45717876)]
        Validation Error: [ VUID-VkGraphicsPipelineCreateInfo-layout-00756 ] Object 0: handle = 0x6387d52676f0, type = VK_OBJECT_TYPE_DEVICE; | MessageID = 0x45717876 | Type mismatch on descriptor slot 1.3 (expected ``) but descriptor of type VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER The Vulkan spec states: layout must be consistent with all shaders specified in pStages (https://www.khronos.org/registry/vulkan/specs/1.3-extensions/html/vkspec.html#VUID-VkGraphicsPipelineCreateInfo-layout-00756)
[2024-02-25T11:27:15Z ERROR wgpu_hal::vulkan::instance]         objects: (type: DEVICE, hndl: 0x6387d52676f0, name: ?)
[2024-02-25T11:27:15Z WARN  wgpu_hal::vulkan::instance] PERFORMANCE [../src/intel/vulkan/anv_pipeline.c:1883 (0x0)]
        Found a partial pipeline in the cache.  This is most likely caused by an incomplete pipeline cache import or export
[2024-02-25T11:27:15Z WARN  wgpu_hal::vulkan::instance]         objects: (type: DEVICE, hndl: 0x6387d52676f0, name: ?)

Process finished with exit code 139 (interrupted by signal 11:SIGSEGV)

Expected vs observed behavior Clearly describe what you get, and how it goes across your expectations.

Extra materials Screenshots to help explain your problem. Validation logs can be attached in case there are warnings and errors. Zip-compressed API traces and GPU captures can also land here.

Platform Information about your OS, version of wgpu, your tech stack, etc.

teoxoy commented 9 months ago

It seems we don't add the decoration for structs used in binding_arrays.