microsoft / microsoft/DirectXShaderCompiler

Weird behavior when returning texture

Open
#5,116 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

correctness dxil incorrect-code
Dominant language
C++
Stars
3.7k
Forks
900
Avg merge
2d 11h
Merged PRs (30d)
44

Description

The following function won't compile on dxc:

#define MAX_TEXTURES 128
Texture2D _allTextures[MAX_TEXTURES];

SamplerState linearSampler;
SamplerState anisoSampler;

enum TexturePrimitive {
    TexturePrimitive_2D,
    TexturePrimitive_3D,
    TexturePrimitive_Cube
};

static const float floatMax = 1e38;        //Approx

bool getTextureFromId(inout Texture2D tex, uint textureId) {

    //Remove this! Don't use this without checking the return bool!
    //This is super sketchy but otherwise it won't compile.
    //DXC can't understand it otherwise.

    //tex = _allTextures[0];

    if (textureId) {

        if ((textureId >> 20) != TexturePrimitive_2D) // Validate if it's a texture2d
            return false;

        textureId = textureId << 12 >> 12;

        if (textureId > MAX_TEXTURES)
            return false;

        tex = _allTextures[NonUniformResourceIndex(max((int)textureId - 1, 0))];
        return true;
    }

    return false;
}

float4 sampleTextureGrad(
    float2 uv, uint textureId, bool useTriplanar,
    float2 uvDdx, float2 uvDdy,
    float4 defaultValue = 1.rrrr
) {

    float4 t = defaultValue;
    Texture2D tex2d;

    if (getTextureFromId(tex2d, textureId)) {
        
        uint w, h, mips;
        tex2d.GetDimensions(0, w, h, mips);

        //Correct for aspect ratio

        if(useTriplanar) {

            float aspect = float(w) / float(h);

            uv.y    *= aspect;
            uvDdx.y *= aspect;        //uvX - uv. If both .y scale by aspect then it's safe to scale the resulting .y by the same amount.
            uvDdy.y *= aspect;
        }
    
        if(uvDdx.x == floatMax)
            t = tex2d.SampleLevel(linearSampler, uv, 0);

        else t = tex2d.SampleGrad(anisoSampler, uv, uvDdx, uvDdy); 
    }
    
    return t;
}

RWTexture2D<float4> v;

[numthreads(1,1,1)]
void main(int2 i : SV_DispatchThreadID) {
    
    float4 t = sampleTextureGrad(0.xx, i.x & 127, false, 0.xx, 0.xx);
    v[i] = t;
}

If you remove the default initializer of tex = _allTextures[0] then it will not compile.
This is because the compiler will assume that the texture handle is going to be used always so will need a value. Even though it's used in the if.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the provided HLSL reproducer with dxc, comparing behavior with and without the default texture initializer. The issue names no source file or test; done would require a confirmed diagnosis and a regression test showing the valid conditional texture use compiles without the initializer.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.