Home > database >  What can I do to fix my code with this HLSL error about array references in loops?
What can I do to fix my code with this HLSL error about array references in loops?

Time:01-07

I've got an HLSL file with the following code:

struct particle
{
    float2 pos;
    float next_pos;
    float angle;
};
particle nearby[32];
float angles[32];
for (uint i = 0; i < number_of_particles; i  )
{
    if ((particle_buffer[i].pos.y <= pos.y   sight && particle_buffer[i].pos.y >= pos.y - sight) && (particle_buffer[i].pos.x <= pos.x   sight && particle_buffer[i].pos.x >= pos.x - sight))//is it within sight square
    {
        nearby[i] = particle_buffer[i];
        count  ;
    }
    if (count >= 32)
    {
        break;
    }

}

for (uint j = 0; j < count; j  )
{
    angles[j] = atan((pos.y - nearby[j].pos.y) / (pos.x - nearby[j].pos.x)); //angle in radians from current to next particle converted to degrees
}

and lines:

nearby[i] = particle_buffer[i];
angles[j] = atan((pos.y - nearby[j].pos.y) / (pos.x - nearby[j].pos.x)); //angle in radians from current to next particle converted to degrees

Both throw the same error about L values and array references:

Shader warning in 'hlsl_file': array reference cannot be used as an l-value; not natively addressable, forcing loop to unroll at kernel move at hlsl_file.compute(line_num) (on d3d11)

I've had a look through the Microsoft docs and some other forums, but I don't understand many of the solutions and most of the answers are for vertex shaders and not compute shaders like mine. The best guess that I can make is that it's because the arrays cannot accept varying index inputs when being assigned to another array but I'm not sure at all.


Edit: after having had a look further on unity forums it looks like for some hardwares you can get this error by trying to address an array inside of a loop with an index variable that is not the loop variable (i or j), my code doesn't do this but maybe it's related?

CodePudding user response:

I ended up just using a RWStructuredBuffer instead of an array, and everthing works perfectly. Not sure if this is efficient or not though.

  •  Tags:  
  • Related