Skip to content

Instantly share code, notes, and snippets.

@pixelmager
Last active October 23, 2024 13:01
Show Gist options
  • Save pixelmager/e99d81449c6724b78e62745604049c35 to your computer and use it in GitHub Desktop.
Save pixelmager/e99d81449c6724b78e62745604049c35 to your computer and use it in GitHub Desktop.
packing floats
//note: we are writing 1bit boolean+8b fallback into a 16b float rendertarget (we can use 1 sign bit 10b mantissa... exponent is complexicated)
//      f32: 1S 8E 23M -> f16: 1S 5E 10M
//      f32: SEEEEEEEEMMMMMMMMMMMMMMMMMMMMMMM
//      f16: SEEEEEMMMMMMMMM
//      conversion: sign remains, exponent is converted, mantissa is truncated
//      (so we need to shift 13b to get into the valid 10b for float16 (assuming truncation))
//      f32: SxxxxxxxxMMMMMMMMMMxxxxxxxxxxxxx (valid f32 values to pack into)
//      f32: SxxxxxxxxMMMMMMMMMMxxxxxxxxxxxxx (packed f16 values read into f32)
//
//      packed values in mantissa: [xbffffffff]
//float4 packResultSSR( in float3 ssr_result, in const bool bIntersected, in float fFallback01 )
//{
//	uint ret = asuint(1.0f);
//	ret |= uint( 255.0f * fFallback01 ) << 13u;
//	//ret |= uint(bIntersected) << (13u+8u); //TODO: pack in sign-bit
//	return float4( ssr_result, asfloat(ret) );
//}
void unpackResultSSR( in float4 res,
					  out float3 ssr_result, out float fFallback01 )
{
	ssr_result = res.rgb;
	
	uint input = asuint( res.a ) >> 13;
	//bIntersected = (input & 0x100u) != 0;
	//bIntersected = (input & 0xffu) != 0xff; //note: intersected if fallback isn't full

	fFallback01 = float( input & 0xffu ) * (1.0f/255.0f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment