DirectXShaderCompiler icon indicating copy to clipboard operation
DirectXShaderCompiler copied to clipboard

Returning a `globallycoherent` resource from a function results in a "global coherent mismatch" warning

Open jeremyong opened this issue 3 years ago • 1 comments

Premise

While other RHIs still do not have dynamic resource indexing, it's convenient to implement a common platform-neutral interface with getters for various resource types. However, an issue seems to arise specifically when trying to return a globallycoherent annotated UAV from a function. While the DXIL generated correctly annotates the resource as globallycoherent, the shader compilation result produces warnings. Please let me know if I missed something in the correct way to do this, thanks!

Issue Description

Consider the following snippet:

// test.hlsl
RWTexture2D<float> get_texture() {
    globallycoherent RWTexture2D<float> output = ResourceDescriptorHeap[0];
    return output;
}
[numthreads(1, 1, 1)]
void CSMain() {
    get_texture()[uint2(0, 0)] = 1.0;
}

Compiling this as dxc.exe -E CSMain -T cs_6_6 -HV 2021 test.hlsl > test.dxil produces the following warning:

.\test.hlsl::4:5: warning: global coherent mismatch
    return output;
    ^

which is the result of this diagnostic.

However, there doesn't appear to be a clean way to propagate the annotation to the function's return type. For example:

globallycoherent RWTexture2D<float> get_texture() {
    globallycoherent RWTexture2D<float> output = ResourceDescriptorHeap[0];
    return output;
}
[numthreads(1, 1, 1)]
void CSMain() {
    get_texture()[uint2(0, 0)] = 1.0;
}

resolves the diagnostic above, but leaves you with a new warning:

.\test.hlsl:1:1: warning: 'globallycoherent' attribute only applies to variables [-Wignored-attributes]
globallycoherent RWTexture2D<float> get_texture()
^

This warning may be suppressed using -Wno-ignored-attributes but would globally turn off the warning.

jeremyong avatar Jun 30 '22 13:06 jeremyong

We've also ran into this with our abstractions, and haven't found a nice way around it other then to break them.

Having globallycoherent as a type modifier makes sense to me but it would also mean that it needs to be valid on return types.

Jasper-Bekkers avatar Jul 22 '22 09:07 Jasper-Bekkers

We have also started running into this when converting our shaders to bindless. Since we do a direct variable -> function, the function type has to return a globallycoherent type.

EpicChrisWaters avatar Mar 06 '23 21:03 EpicChrisWaters