• Create Account

## Packing DXGI_FORMAT_R11G11B10_FLOAT

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1barsiwek  Members

114
Like
0Likes
Like

Posted 14 October 2012 - 06:23 AM

Hi everyone,

I would like to ask how to properly pack values of a vertex normal using DXGI_FORMAT_R11G11B10_FLOAT format.
My input, per vertex, is a 3 dimensional vector with components in range [-1,1] stored in 32bit floats. I do know that I have to scale that into the [0, 1] range since there is no signs in R11G11B10 format, but what am I suppose to do after? I can extract the exponent and mantisa but is truncating them bitwise the right way to go?

### #2Erik Rufelt  Members

5901
Like
0Likes
Like

Posted 14 October 2012 - 07:19 AM

Hi everyone,

I would like to ask how to properly pack values of a vertex normal using DXGI_FORMAT_R11G11B10_FLOAT format.
My input, per vertex, is a 3 dimensional vector with components in range [-1,1] stored in 32bit floats. I do know that I have to scale that into the [0, 1] range since there is no signs in R11G11B10 format, but what am I suppose to do after? I can extract the exponent and mantisa but is truncating them bitwise the right way to go?

What exactly do you want to do?
Get your normal with as high precision as possible to your shader using only 32 bits total for the vector?

### #3kauna  Members

2918
Like
0Likes
Like

Posted 14 October 2012 - 10:07 AM

store in the shader:

stored_normal = normal * 0.5f + 0.5f;

restore in the shader:

restored_normal = stored_normal * 2.0f - 1.0f;

Nothing more complicated than that. However, I'm not sure if DXGI_FORMAT_R11G11B10_FLOAT has good precision for normals.

Cheers!

Edited by kauna, 14 October 2012 - 10:07 AM.

### #4barsiwek  Members

114
Like
0Likes
Like

Posted 14 October 2012 - 10:12 AM

What exactly do you want to do?
Get your normal with as high precision as possible to your shader using only 32 bits total for the vector?

I want to try out different formats for storing normals at vertices and compare quality to pick the one that suits me the best. Most formats are either simple to pack to have functions that do that for you (for example - D3DX_FLOAT4_to_R10G10B10A2_UNORM). With this one I do not know how to do it correctly.

store in the shader:

stored_normal = normal * 0.5f + 0.5f;

restore in the shader:

restored_normal = stored_normal * 2.0f - 1.0f;

Nothing more complicated than that. However, I'm not sure if DXGI_FORMAT_R11G11B10_FLOAT has good precision for normals.

Cheers!

I was hoping on a description how to do it on application side (in C++ for example) - so I can store this in a binary file for fast loading later.

### #5Erik Rufelt  Members

5901
Like
0Likes
Like

Posted 14 October 2012 - 11:23 AM

I want to try out different formats for storing normals at vertices and compare quality to pick the one that suits me the best. Most formats are either simple to pack to have functions that do that for you (for example - D3DX_FLOAT4_to_R10G10B10A2_UNORM). With this one I do not know how to do it correctly.

I see. The source for that function is in D3DX_DXGIFormatConvert.inl in the DX SDK Include folder., so you can always do it the same way.
Looking at the source, that function also saturates your input to [0, 1], so you'd have to do that first to use the same method, with (x + 1) / 2. Then modify the scale to match R11G11B10, which means 2047 for x, 2047 for y, shift with 11 instead of 10 for y, and still 1023 for z but shift by 22. W is skipped.
In the unpack function replace the shifts and scales the same way, shift 11 for y and 22 for z, divide by 2047 for x and y, and the mask for x and y is 0x7ff instead of 0x3ff.
And to restore your values from [0, 1] to [-1, 1] use x * 2 - 1.

Actually never mind, you're trying to use a float format. Sorry about that.

Edited by Erik Rufelt, 14 October 2012 - 11:27 AM.

### #6Erik Rufelt  Members

5901
Like
0Likes
Like

Posted 14 October 2012 - 11:44 AM

This is completely untested.. but something like the following will probably work if the input is in [0, 1]:

typedef unsigned int uint32;

uint32 convertFloat32ToFloat11Bits(float f) {

uint32 floatBits;

memcpy(&floatBits, &f, 4);

uint32 exponent = (floatBits >> 23U) & 0xffU;

uint32 mantissa = floatBits & 0x7fffffU;

uint32 float11Exponent = exponent - 127 + 15;

uint32 float11Mantissa = mantissa >> 17;

if(exponent < 112) {

float11Exponent = 0;

float11Mantissa = 0;

}

uint32 float11Bits = (float11Exponent << 6) | float11Mantissa;

return float11Bits;

}

uint32 convertFloat32ToFloat10Bits(float f) {

uint32 floatBits;

memcpy(&floatBits, &f, 4);

uint32 exponent = (floatBits >> 23U) & 0xffU;

uint32 mantissa = floatBits & 0x7fffffU;

uint32 float10Exponent = exponent - 127 + 15;

uint32 float10Mantissa = mantissa >> 18;

if(exponent < 112) {

float10Exponent = 0;

float10Mantissa = 0;

}

uint32 float10Bits = (float10Exponent << 5) | float10Mantissa;

return float10Bits;

}

uint32 convertFloat32VectorToR11G11B10(XMFLOAT3 vec) {

uint32 rbits = convertFloat32ToFloat11Bits(vec.x);

uint32 gbits = convertFloat32ToFloat11Bits(vec.y);

uint32 bbits = covnertFloat32ToFloat10Bits(vec.z);

uint32 result = rbits | (gbits << 11) | (bbits << 22);

return result;

}



[Edited to fix too small values].

Edited by Erik Rufelt, 14 October 2012 - 11:57 AM.

### #7MJP  Moderators

18215
Like
0Likes
Like

Posted 14 October 2012 - 12:04 PM

DirectXMath has a whole bunch of format conversion types in the PackedVector namespace, including XMFLOAT3PK which works for R11G11B10_FLOAT. You can just use that directly, or look at the implementation XMLoadFloat3PK/XMStoreFloat3PK to see how it's done.

### #8barsiwek  Members

114
Like
0Likes
Like

Posted 14 October 2012 - 01:31 PM

Wow! Thank you Erik for the code and MJP for reference.
Microsoft should work a bit on their documentation - I did a bunch of searching on MSDN and did not come over this article at all :/

### #9Matias Goldberg  Members

9067
Like
0Likes
Like

Posted 14 October 2012 - 05:56 PM

If you want to efficiently store normals in vertices, I strongly suggest you that you read:

Crytek's QTangents: Storing normal, tangent & reflection data in just 4 floats (very useful for normal mapping). Starts at slide 8
Emil Persson's (Avalanche Studios) Creating Vast Game Worlds: Store normal, tangent & bitangents in just 4 bytes. Starts at slide 19

As for storing the normal maps in textures with just an RGB888 texture, Google Crytek's Best-fit normals technique

Cheers
Dark Sylinc