Sign in to follow this  

Packing DXGI_FORMAT_R11G11B10_FLOAT

This topic is 1920 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
[quote name='barsiwek' timestamp='1350217394' post='4990022']
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?
[/quote]

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[quote name='Erik Rufelt' timestamp='1350220791' post='4990028']
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?
[/quote]
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.


[quote name='kauna' timestamp='1350230832' post='4990065']
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!
[/quote]

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.

Share this post


Link to post
Share on other sites
[quote name='barsiwek' timestamp='1350231165' post='4990070']
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.
[/quote]

[s]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.[/s]

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

Share this post


Link to post
Share on other sites
This is completely untested.. but something like the following will probably work if the input is in [0, 1]:
[source]
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;
}
[/source]

[Edited to fix too small values]. Edited by Erik Rufelt

Share this post


Link to post
Share on other sites
DirectXMath has a whole bunch of [url="http://msdn.microsoft.com/en-us/library/windows/desktop/ee418728%28v=vs.85%29.aspx#graphics_library_type_equivalence"]format conversion types[/url] in the PackedVector namespace, including [url="http://msdn.microsoft.com/en-us/library/windows/desktop/microsoft.directx_sdk.reference.xmfloat3pk%28v=vs.85%29.aspx"]XMFLOAT3PK[/url] which works for R11G11B10_FLOAT. You can just use that directly, or look at the implementation XMLoadFloat3PK/XMStoreFloat3PK to see how it's done.

Share this post


Link to post
Share on other sites
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 :/

Share this post


Link to post
Share on other sites
If you want to efficiently store normals in vertices, I strongly suggest you that you read:

[url="http://www.crytek.com/download/izfrey_siggraph2011.pdf"]Crytek's QTangents:[/url] Storing normal, tangent & reflection data in just 4 floats (very useful for normal mapping). Starts at slide 8
[url="http://www.humus.name/Articles/Persson_CreatingVastGameWorlds.pptx"]Emil Persson's (Avalanche Studios) Creating Vast Game Worlds[/url]: 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

Share this post


Link to post
Share on other sites
Sign in to follow this