Friday, November 11, 2022
HomeGame Developmenthlsl - Placing two R16_UNORM in a R32_UINT

hlsl – Placing two R16_UNORM in a R32_UINT


With my {hardware} limitation for CS5 I am utilizing R32_UINT in my shaders to retailer compacted colours and normasl as 555 bits every. My packing is utilizing uint3 and the result’s of poor high quality.
I am planning to retailer the colour as 565 bits and normals as 77 bits + 2 bits, one for an indication (of regular z) and one for a masks, however in a R16_UNORM every that offers me higher high quality and pack the 2 in my R32_UINT.

beneath the packing/unpacking funcs for float3 shade to 565 bits as R16_unorm and again that works with appropriate high quality in my different shaders. I additionally added the anticipated coding for the Normals to Nxy 77 bits

float C3_R16(float3 Shade)
 (uColor.g<<5) 

float3 R16_C3(float Pack)
{
    uint iPack = uint(Pack*65535);
    return float3(iPack&0x1F, (iPack>>5)&0x3F, (iPack>>11)&0x1F)/float3(31,63,31);
}

float N2_R16(float3 Regular)//unchecked conduct
 (uNormal.g<<7))/65535.0;


float3 R16_N3(float Pack)//unchecked
{
    uint iPack = uint(Pack*65535);
    float3 N; 
    N.xy = float2(iPack&0x7F, (iPack>>7)&0x7F)/float2(127,127)*2-1;//possibly optimze with /float2(63.5,63.5)-1;
    N.z = sqrt(1-dot(N.xy,N.xy));//in view house I mustn't want signal 
    return N;
}

The Regular 77 (N2_R16) enoding is to point out how I’ll proceed for the Nxy encoding/decoding. The 2 remaining bits are added with a check and bitshift like uint(N.z<0)<<15.

Now to transform two R16 right into a R32_UINT I am planning to make use of asuint perform this manner:

uint ColorOut = (asuint(C3_R16(mycolor.rgb))&0x00EFFFFF)>>7;
//uint NormalOut = (asuint(C2_R16(mynormal.rg))&0x00EFFFFF)>>7;//this can be optimized to keep away from latter <<16
uint MyFinalR32_UINT = ColorOut;//| NormalOut<<16; presently just one R16 is added for the colour

On this code I am storing just one R16 in the meanwhile to examine the way it works.

Based on IEEE guidelines the *0x00EFFFFF masks considers that the 9 msb are the signal and eight bit exponent and the decrease 23 bit the mantissa. As the worth reulting from C3_R16 should be within the vary [0..1] I mustn’t want the signal and exponent. The >>7 is to select the 16 MSB little bit of the mantissa (once more if I am appropriate these are essentially the most vital bits). That is have been issues go flawed I believe. My colours are largely blue with some inexperienced on unpacking. Some objects needs to be full purple.

Is my description of float32 well-used?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments