开发者

How can I encode four unsigned bytes (0-255) to a float and back again using HLSL?

I am facing a task where one of my hlsl shaders require multiple texture lookups per pixel. My 2d textures are fixed to 256*256, so two bytes should be sufficient to address any given texel given this constraint. My idea is then to put two xy-coordinates in each float, giving me eight xy-coordinates in pixel space when packed in a Vector4 format image. These eight coordinates are then used to sample another texture(s).

The reason for doing this is to save graphics memory a开发者_开发问答nd an attempt to optimize processing time, since then I don't require multiple texture lookups.

By the way: Does anyone know if encoding/decoding 16 bytes from/to 4 floats using 1 sampling is slower than 4 samplings with unencoded data?


Edit: This is for Shader Model 3


If you are targeting SM 4.0-SM 5.0 you can use Binary Casts and Bitwise operations:

uint4 myUInt4 = asuint(myFloat4);

uint x0 = myUInt4.x & 0x000000FF; //extract two xy-coordinates (x0,y0), (x1,y1)
uint y0 = (myUInt4.x & 0x0000FF00) >> 8;
uint x1 = (myUInt4.x & 0x00FF0000) >> 16;
uint y1 = (myUInt4.x & 0xFF000000) >> 24;

//repeat operation for .y .z and .w coordinates

For previous Shader Models, I think it could be more complicated since it depends on FP precision.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜