• 9
• 9
• 11
• 13
• 9

# Compiler error when writing to a buffer in a conditional expression

This topic is 932 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Code demonstrating the issue, not the actual monster: ( I believe this is the culprit, if not I apologize, although the example by it self produced the error )

// Example demonstrating the issue
[unroll] for (uint i = 0; i < 5; i++)
{
if (i < 2)
{
buffer[5 * 3 + i] = 0;
}
}


Error:

Resources being indexed cannot come from conditional expressions, they must come from literal expressions

.

What's the reasoning behind this error? I assume it's some underlying way the API is set up, but I would have assumed it would be an acceptable thing to do.

Edited by Migi0027 (????)

##### Share on other sites
buffer declaration, please. I bet it's some Buffer buffer[10]. This translates to several SRV/UAV slots and indeed cannot be indexed the way you wish. You need another approach.

##### Share on other sites

I probably did something incredibly stupid, like usual!

RWBuffer<float3> g_bffSampleLow : register(u1);

Edited by Migi0027 (????)

##### Share on other sites
Hmmm, I was wrong then. Give me a moment...

This...


RWBuffer<float3> buffer : register(u1);

void PS()
{
[unroll] for (uint i = 0; i < 5; i++)
{
if (i < 2)
{
buffer[5 * 3 + i] = 0;
}
}
}

... compiles fine for me.

So... provide a snippet which actually triggers that error. Edited by unbird

##### Share on other sites

Ahh, the issue was my fault. I did the following:

RWBuffer<float3> bffRay = (screen.Roughness < 0.2f ? g_bffRaysHigh : g_bffRaysLow);

.

And then used bffRay as the "buffer". I naively assumed it was valid.

Thank you for the help thought.

Edited by Migi0027 (????)

##### Share on other sites

Yeah, that indeed needs a true branch.

##### Share on other sites

It seems so.

Would have been nice to avoid it.

##### Share on other sites


RWBuffer<float3> g_bffRays;
void PS(float rough: ROUGH)
{
[unroll] for (uint i = 0; i < 5; i++)
{
uint index = 5*3+i;
index *= 2;
if (rough < 0.2) // just some condition
{
index++;
}
g_bffRays[index] = 0;
}
}

So, low and high rays alternate. Not sure if this is useful, but it compiles