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

  • 2
    \$\begingroup\$ The hlsl float4 type already allows you to get and set its components by array index, similar to float[]. So myFloat4[0], myFloat4.x, and myFloat4.r are equivalent. You can even use a variable to index into the float4 inside a loop to set myFloat4[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\$
    – DMGregory
    Apr 7, 2022 at 11:42
  • \$\begingroup\$ I just did not see that float4 /float[4] are compatible and better (and somehow faster). I was focusing on using a single float to read my rgba texture just because you need only a float to compare depth or distances, and thus selecting the appropriate x or y or z or w component depending on the light loop value (l here from 0 to 3 included). So I was doing this : P = (l==0)?color.r:(l==1)?color.g:(l==2)?:color.b:color.a; A little bit tricky but working. \$\endgroup\$
    – philB
    Apr 7, 2022 at 14:20

1 Answer 1


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!


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .