microsoft / DirectXShaderCompiler

This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.
Other
3.05k stars 679 forks source link

[SPIR-V] Invalid SPIR-V generated when using RW resource returned from function #4136

Open TLaviron opened 2 years ago

TLaviron commented 2 years ago

The following shader generates invalid SPIR-V:

RWTexture2D<float2> textures[];

cbuffer CB {
    uint index;
}

RWTexture2D<float2> GetTexture() {
    return textures[index];
}

[numthreads(1, 1, 1)]
void main(uint3 tid : SV_DispatchThreadID)
{
    GetTexture()[tid.xy] = float2(1.0, 0.0);
    // workaround:
    // RWTexture2D<float2> tex = GetTexture(); tex[tid.xy] = float2(1.0, 0.0);
}

Compiled using latest release (v1.6.2112) with command line dxc.exe -E main -T cs_6_0 -Fo uav_indexing.comp.spv -spirv -fspv-target-env=vulkan1.1 uav_indexing.comp.hlsl

As commented in the code, using the returned value from the function and storing it in a resource variable works and has the intended effect.

kuhar commented 2 years ago

Thanks for reporting, @TLaviron.

@jaebaek, can you take a look?

jaebaek commented 2 years ago

Sure. I will take a look.

s-perron commented 3 weeks ago

For some reason we have two loads of the texture. The first is in GetTexture, and another after the function call.

I think we need to avoid the one after the function call.