gfx-rs / wgpu

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

d3d12: Device loss due to invalid usage of CreateShaderResourceView #5097

Closed nical closed 9 months ago

nical commented 9 months ago

Description A clear and concise description of what the bug is.

Repro steps

The following triggers a device loss in Firefox:

<script>
document.addEventListener("DOMContentLoaded", async () => {
    const adpt = await self.navigator.gpu.requestAdapter({})
    const dev = await adpt.requestDevice({})
    const tex = dev.createTexture({
        "size": {"width": 88},
        "format": "stencil8",
        "usage": GPUTextureUsage.TEXTURE_BINDING
    })
    tex.createView()
    dev.createSampler({})
})
</script>

Log:

D3D12 ERROR: ID3D12Device::CreateShaderResourceView: The Plane Slice 0 cannot be used when the resource format is R24G8_TYPELESS and the view format is X24_TYPELESS_G8_UINT.  See documentation for the set of valid view format names for this resource format, determining which how the resource (or part of it) will appear to shader. [ STATE_CREATION ERROR #29: CREATESHADERRESOURCEVIEW_INVALIDVIDEOPLANESLICE]
D3D12: Removing Device.
D3D12 ERROR: ID3D12Device::RemoveDevice: Device removal has been triggered for the following reason (DXGI_ERROR_INVALID_CALL: There is strong evidence that the application has performed an illegal or undefined operation, and such a condition could not be returned to the application cleanly through a return code). [ EXECUTION ERROR #232: DEVICE_REMOVAL_PROCESS_AT_FAULT]
Exception thrown at 0x00007FFA9C07CF19 in firefox.exe: Microsoft C++ exception: _com_error at memory location 0x0000001DE87F9A80.
Exception thrown at 0x00007FFA9C07CF19 in firefox.exe: Microsoft C++ exception: _com_error at memory location 0x0000001DE87F9DF8.
Exception thrown at 0x00007FFA9C07CF19 in firefox.exe: Microsoft C++ exception: _com_error at memory location 0x0000001DE87FAB80.

Platform

Windows with d3d12

nical commented 9 months ago

Firefox bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1874478

nical commented 9 months ago

wgpu trace (recorded on another computer):

[
Init(
    desc: (
        label: None,
        required_features: 0,
        required_limits: (
            maxTextureDimension1D: 8192,
            maxTextureDimension2D: 8192,
            maxTextureDimension3D: 2048,
            maxTextureArrayLayers: 256,
            maxBindGroups: 4,
            maxBindingsPerBindGroup: 1000,
            maxDynamicUniformBuffersPerPipelineLayout: 8,
            maxDynamicStorageBuffersPerPipelineLayout: 4,
            maxSampledTexturesPerShaderStage: 16,
            maxSamplersPerShaderStage: 16,
            maxStorageBuffersPerShaderStage: 8,
            maxStorageTexturesPerShaderStage: 4,
            maxUniformBuffersPerShaderStage: 12,
            maxUniformBufferBindingSize: 65536,
            maxStorageBufferBindingSize: 134217728,
            maxVertexBuffers: 8,
            maxBufferSize: 268435456,
            maxVertexAttributes: 16,
            maxVertexBufferArrayStride: 2048,
            minUniformBufferOffsetAlignment: 256,
            minStorageBufferOffsetAlignment: 256,
            maxInterStageShaderComponents: 60,
            maxComputeWorkgroupStorageSize: 16384,
            maxComputeInvocationsPerWorkgroup: 256,
            maxComputeWorkgroupSizeX: 256,
            maxComputeWorkgroupSizeY: 256,
            maxComputeWorkgroupSizeZ: 64,
            maxComputeWorkgroupsPerDimension: 65535,
            maxPushConstantSize: 256,
            maxNonSamplerBindings: 10000,
        ),
    ),
    backend: Vulkan,
),
CreateTexture(Id(0, 1, Vulkan), (
    label: None,
    size: (
        width: 88,
        height: 1,
        depthOrArrayLayers: 1,
    ),
    mip_level_count: 1,
    sample_count: 1,
    dimension: r#2d,
    format: "stencil8",
    usage: 4,
    view_formats: [],
)),
CreateTextureView(
    id: Id(0, 1, Vulkan),
    parent_id: Id(0, 1, Vulkan),
    desc: (
        label: None,
        format: None,
        dimension: None,
        range: (
            aspect: all,
            baseMipLevel: 0,
            mipLevelCount: None,
            baseArrayLayer: 0,
            arrayLayerCount: None,
        ),
    ),
),
CreateSampler(Id(0, 1, Vulkan), (
    label: None,
    address_modes: (r#clamp-to-edge, r#clamp-to-edge, r#clamp-to-edge),
    mag_filter: nearest,
    min_filter: nearest,
    mipmap_filter: nearest,
    lod_min_clamp: 0.0,
    lod_max_clamp: 1000.0,
    compare: None,
    anisotropy_clamp: 1,
    border_color: None,
)),
DestroySampler(Id(0, 1, Vulkan)),
DestroyTextureView(Id(0, 1, Vulkan)),
DestroyTexture(Id(0, 1, Vulkan)),
]
dtzxporter commented 9 months ago

The root of this issue is that D24_UNORM_S8_UINT Isn't allowed in an SRV according to the dx12 spec: https://learn.microsoft.com/en-us/windows/win32/direct3ddxgi/hardware-support-for-direct3d-12-1-formats#dxgi_format_d24_unorm_s8_uintv-45

Supported SRVs are only:

teoxoy commented 9 months ago

We create a view with the X24_TYPELESS_G8_UINT format (which supports "Shader ld") from the texture with the R24G8_TYPELESS format.

This should work but I guess we should use plane 1 not 0 since that's where the G8 data is.

dtzxporter commented 9 months ago

5100 fixes the issue for me.