[fpc-pascal] Float packing

Ryan Joseph ryan at thealchemistguild.com
Mon Nov 6 14:32:50 CET 2017

> On Nov 6, 2017, at 7:36 PM, Karoly Balogh (Charlie/SGR) <charlie at scenergy.dfmk.hu> wrote:
> But anyway, I guess this is not so useful for you any more. So if you can
> tell what *actually* you want to do, like convert a vec4 of GLSL float to
> Pascal, or convert a 32bit RGBA value to a GLSL vec4, maybe that would be
> more easy to help you with.

I need to encode an integer (2 byte word is actually enough) into the pixels of a 2D texture where each component is a byte (0-255). In the shader I then decode that value from the pixel (RGB value) of the texture. It’s for sorting value in a 2D game

I guess I need to pay attention to byte swapping (not a big problem right?) but your example writeln((c.a shl 24) + (c.b shl 16) + (c.g shl 8) + c.a; is basically what I needed, however I’m using an older version of GLSL which doesn’t have bit shift operators.

Another user contacted me privately and suggested this method using just arithmetic. From the vec3 I translate the fraction back to 0-255 then basically do what you did with bit shifting. I can kind of see how this is shifting by 256 each time which is the size of a byte and therefore bit shifting.

float unpack (vec3 c) {
	c.r = c.r*255;
	c.g = c.g*255;
	c.b = c.b*255;
	return floor(c.r + c.g * 256 + c.b * 256 * 256);

I think I got this solved but i need to implement it fully.

	Ryan Joseph

More information about the fpc-pascal mailing list