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

## Recommended Posts

Migi0027    4630

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
unbird    8338
[tt]buffer[/tt] declaration, please. I bet it's some [tt]Buffer buffer[10][/tt]. This translates to several SRV/UAV slots and indeed cannot be indexed the way you wish. You need another approach.

##### Share on other sites
Migi0027    4630

I probably did something incredibly stupid, like usual!

RWBuffer<float3> g_bffSampleLow : register(u1);

Edited by Migi0027 (????)

##### Share on other sites
unbird    8338
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
Migi0027    4630

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
unbird    8338

Yeah, that indeed needs a true branch.

##### Share on other sites
Migi0027    4630

It seems so.

Would have been nice to avoid it.

##### Share on other sites
unbird    8338


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

##### Share on other sites
Migi0027    4630

"Do they need to be in separate buffers?" Definitely not, it would be extremely easy to pack it together, but I'm trying out a new technique so I'd like to take things one step at a time.

## Create an account

Register a new account