chrisforbes / mesa

chrisf's mesa hacks
2 stars 0 forks source link

GLSL FS backend: Extremely poor code generation for loops that reference constant arrays! #35

Open chrisforbes opened 11 years ago

chrisforbes commented 11 years ago

Things like this generate a horrendous mess currently. The nonconstant index is lowered to a massive conditional tree for all possible indexes.

If the loop were just unrolled instead, or even better, induction variables were allowed as array indexes, things would be quite a lot better.

const float xs[16] = { ... }

float z = 0.0f;
for(int i=0; i<16; i++)
    z += xs[i];
chrisforbes commented 11 years ago

Here's a real example, from KSP. It's a 60-tap poisson disc filter, butchered horribly.

http://sprunge.us/cSKL

chrisforbes commented 11 years ago

In D3D you'd write that as a cbuffer -- which behaves more or less like our UBO. (In fact, the NV paper on this shadow technique provides a sample HLSL shader using a cbuffer for exactly this purpose.

Suggests that it's both acceptable (from a perf perspective) and possible to shunt the block of constants as a whole, into a block of uniforms.

chrisforbes commented 11 years ago

Looks like the lowering pass can be tweaked slightly to avoid lowering references into constant arrays. Unsure what else would have to happen -- probably shipping the array out to pull constants [as we do with uniforms] needs to be handled nicely in FS too.