I've a RGBA16F texturecube for 4 pointlights shadows rendered in one pass. Each color channel stores the object to light distance for one pointlight shadow (e.g red channel is for light 0 and so on). Everything works fine but I'm obliged to do some testing to fill the output color correctly when writing to the texture and the reverse when reading from it. I would like to cast float4 to float[4] to simplify this part of my shaders but it seems casting like this is not possible. I've seen some workaround for glsl but it does not really match the union keyword in C++. Any suggestion welcome
1 Answer
From AMD's Dark Secrets of Shader Development or What Your Mother Never Told You About Shaders, I learned you can just write
float L[4] = (float[4])Tex.rgba
to read rgba as an array of floats. So simple in fact!
float4
type already allows you to get and set its components by array index, similar tofloat[]
. SomyFloat4[0]
,myFloat4.x
, andmyFloat4.r
are equivalent. You can even use a variable to index into thefloat4
inside a loop to setmyFloat4[i] = someFunction(i)
, so I'm not so clear on why you'd need to use a different type here. Can you show an example of the shader code you have now vs. how you'd like to change it? \$\endgroup\$