microsoft / microsoft/DirectXShaderCompiler

Unexpected compilation error using EvaluateAttributeAtSample

Open
#6,225 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug tech-debt
Dominant language
C++
Stars
3.7k
Forks
900
Avg merge
2d 11h
Merged PRs (30d)
44

Description

Description
User sees a compilation error when using EvaluateAttributeAtSample in certain cases where they would not expect it to fail.

error: attribute evaluation can only be done on values taken directly from inputs.
O.x[i] = EvaluateAttributeAtSample(O.x[i],S);

They're able to reproduce this in all versions of DXC here:
https://godbolt.org/z/7PGn6Ts6z

Set ARRAY_INTERPOLATORS to 1 to demonstrate the problem.
Set INLINE_EVAL_AT_SAMPLE to 1 to work around the problem.

The bug happens when an array interpolant is passed to a function which then calls EvaluateAttributeAtSample on it.
Using a non-array interpolant works.
Inlining the code into the “Main” entry point also works.

Steps to Reproduce

This is code that's in the above godbolt link:

// The entry point and target profile are needed to compile this example:
// -T ps_6_6 -E PSMain

// Enabling this causes a compiler error
#define ARRAY_INTERPOLANTS (1) 
// Enabling this works around the compiler error when ARRAY_INTERPOLANTS is on
#define INLINE_EVAL_AT_SAMPLE (0)

struct PSInput
{
#if ARRAY_INTERPOLANTS
    float3 x[4] : TEST;
#else
    float3 x0 : TEST0;
    float3 x1 : TEST1;
    float3 x2 : TEST2;
    float3 x3 : TEST3;
#endif
};

PSInput EvalInterpAtSample(PSInput I, int S)
{
    PSInput O = I;
#if ARRAY_INTERPOLANTS
    [unroll]
    for( int i =0; i<4; ++i)
    {
        O.x[i] = EvaluateAttributeAtSample(O.x[i],S);
    }
#else
    O.x0 = EvaluateAttributeAtSample(O.x0,S);
    O.x1 = EvaluateAttributeAtSample(O.x1,S);
    O.x2 = EvaluateAttributeAtSample(O.x2,S);
    O.x3 = EvaluateAttributeAtSample(O.x3,S);
#endif
    return O;
}

void PSMain(
    float4 Position : SV_Position,
    PSInput input,
    out float3 OutColor : SV_Target0
)
{
    OutColor = 0;
    [unroll]
    for ( uint i=0; i <4; ++i)
    {
#if INLINE_EVAL_AT_SAMPLE
        PSInput I = input;
        #if ARRAY_INTERPOLANTS
            I.x[0] = EvaluateAttributeAtSample(input.x[0],i);
            I.x[1] = EvaluateAttributeAtSample(input.x[1],i);
            I.x[2] = EvaluateAttributeAtSample(input.x[2],i);
            I.x[3] = EvaluateAttributeAtSample(input.x[3],i);
        #else
            I.x0 = EvaluateAttributeAtSample(I.x0,i);
            I.x1 = EvaluateAttributeAtSample(I.x1,i);
            I.x2 = EvaluateAttributeAtSample(I.x2,i);
            I.x3 = EvaluateAttributeAtSample(I.x3,i);
        #endif
#else
        PSInput I = EvalInterpAtSample(input,i);
#endif    
        #if ARRAY_INTERPOLANTS
            OutColor += I.x[0] + I.x[1] + I.x[2] + I.x[3];
        #else
            OutColor += I.x0 + I.x1 + I.x2 + I.x3;
        #endif
    }
}

Actual Behavior

Compilation fails with the following error, although the user believes the code is valid:

error: attribute evaluation can only be done on values taken directly from inputs.
        O.x[i] = EvaluateAttributeAtSample(O.x[i],S);

Environment

  • DXC version: all versions currently supported by godbolt
  • Host Operating System windows

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 with the provided HLSL reproducer, compiling it with DXC using -T ps_6_6 -E PSMain and ARRAY_INTERPOLANTS enabled. Compare EvalInterpAtSample with INLINE_EVAL_AT_SAMPLE enabled and disabled; done means the array-interpolant call compiles without the reported direct-input error.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.