• Create Account

# DXGI_FORMAT Codes and endianness

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.

3 replies to this topic

### #1R. LeBlanc  Members   -  Reputation: 140

Like
0Likes
Like

Posted 13 October 2012 - 05:25 PM

I've been working through Frank D. Luna's book "Introduction to 3D programming with DirectX 10" and one of the problems is to switch from using

D3DXCOLOR color (128 bits)
to UINT color (32 bits)

Presumably the format code to use is: DXGI_FORMAT_R8G8B8A8_UNORM.

In my mind this means you have a variable which at the byte level has information about the channels in the exact order: RGBA (Is this the correct interpretation?--Asking because I'm sure I've read that when you want RGBA you really need a code like: A#R#G#B# where the alpha channel is specified first.

Anyway, I opted (there's probably a better way) to do:
UINT color = (UINT)WHITE;

where WHITE is defined: const D3DXCOLOR WHITE(1.0f, 1.0f, 1.0f, 1.0f);

This cast is defined in the extension to D3DXCOLOR.
However, when DXGI_FORMAT_R8G8B8A8_UNORM is used with the UINT color variable you get the wrong results. Luna attributes this to endianness.

Is this because the cast from D3DXCOLOR produces a UINT of the form RGBA but because intel x86 uses little endiann then at byte level you really get 'ABGR'?? So when this variable actually gets interpreted the shader sees ABGR instead of RGBA? Shouldn't it just know when interpreting bytes that the higher order bits are at the smaller address? And the last question: Since the code is specified as DXGI_FORMAT_R8G8B8A8_UNORM, does this mean that R should be the smallest address and A should be at the largest? I'm sure there are a ton of misconceptions I have so please feel free to dispel them.

### #2MJP  Moderators   -  Reputation: 11774

Like
0Likes
Like

Posted 14 October 2012 - 12:23 AM

For any DXGI format, the byte order is the order of the components in the format name. So for R8G8B8A8, R should be the first (lowest) byte and A should be the last (highest) byte. Endianness really doesn't have anything to do with it in this particular case, since the order is explicit and each component is the one byte in size. The reason why you're not getting the right result is because the DWORD casting operator for D3DXCOLOR is defined like this:

D3DXCOLOR::operator DWORD () const
{
DWORD dwR = r >= 1.0f ? 0xff : r <= 0.0f ? 0x00 : (DWORD) (r * 255.0f + 0.5f);
DWORD dwG = g >= 1.0f ? 0xff : g <= 0.0f ? 0x00 : (DWORD) (g * 255.0f + 0.5f);
DWORD dwB = b >= 1.0f ? 0xff : b <= 0.0f ? 0x00 : (DWORD) (b * 255.0f + 0.5f);
DWORD dwA = a >= 1.0f ? 0xff : a <= 0.0f ? 0x00 : (DWORD) (a * 255.0f + 0.5f);
return (dwA << 24) | (dwR << 16) | (dwG << 8) | dwB;
}


So as you can see the result you get is in BGRA order and not RGBA order, so it would be appropriate for DXGI_FORMAT_B8G8R8A8_UNORM. If you swap B and R you should get the results you want. Or you can just make your own function.

Another alternative is to ditch D3DX math altogether in favor of DirectXMath. DirectXMath has conversions for all kinds of DXGI formats, which are listed here. In your case you'd want to us XMUBYTEN4, and use x=R, y=G, z=B,w=A.

Edited by MJP, 14 October 2012 - 12:25 AM.

### #3R. LeBlanc  Members   -  Reputation: 140

Like
0Likes
Like

Posted 14 October 2012 - 02:24 PM

Ok so just to make sure I'm getting it right:

D3DXCOLOR::operator UINT () const
{
UINT dwR = r >= 1.0f ? 0xff : r <= 0.0f ? 0x00 : (UINT) (r * 255.0f + 0.5f);
UINT dwG = g >= 1.0f ? 0xff : g <= 0.0f ? 0x00 : (UINT) (g * 255.0f + 0.5f);
UINT dwB = b >= 1.0f ? 0xff : b <= 0.0f ? 0x00 : (UINT) (b * 255.0f + 0.5f);
UINT dwA = a >= 1.0f ? 0xff : a <= 0.0f ? 0x00 : (UINT) (a * 255.0f + 0.5f);
return (dwA << 24) | (dwR << 16) | (dwG << 8) | (dwB << 0);
}


the return statement is essentially a 32 bit entity that looks like

| --byte1=Alpha--|--byte2=Red--|--byte3=Green--|--byte4=Blue--|

Does it make sense that if x86 intel machines are little-endian that I should think of this as

| --byte1=Alpha--|--byte2=Red--|--byte3=Green--|--byte4=Blue--|
LOW ------------------------------------------------------------------- HIGH

-----------------------------------------------------------------------------------------
The function that I used was given by Luna as:

D3DX10INLINE UINT ARGB2ABGR(UINT argb)
{
BYTE A = (argb >> 24) & 0xff;
BYTE R = (argb >> 16) & 0xff;
BYTE G = (argb >>  8) & 0xff;
BYTE B = (argb >>  0) & 0xff;
return (A << 24) | (B << 16) | (G << 8) | (R << 0);
}


Which to me seems to imply:

| --byte1=Alpha--|--byte2=Blue--|--byte3=Green--|--byte4=Red--|
LOW -------------------------------------------------------------------- HIGH

This works but obviously my understanding of the endianness is incorrect because the format code I am using is:
DXGI_FORMAT_R8G8B8A8_UNORM

So this means the expected byte order is Red, Green, Blue, Alpha. Which if you read opposite of what I have above makes sense. To me it looks like the bytes are ordered backwards in memory (ABGR). Maybe I'm misunderstanding the shifting operation.

Does: (A << 24) | (B << 16) | (G << 8) | (R << 0) create the following sort of thing

AAAAAAAA000000000000000000000000 (A bits shifted 24 to the left) OR'd With
00000000BBBBBBBB0000000000000000 (B bits shifted 16 to the left) OR'd With
0000000000000000GGGGGGGG00000000 (G bits shifted 8 to the left) OR'd With
000000000000000000000000RRRRRRRR (R bits shifted 0 to the left) Which results in
-------------------------------
AAAAAAAABBBBBBBBGGGGGGGGRRRRRRRR

Is it just a case of convention where I should start at the far right and call that byte 1 or am I missing something else?
Also does the + 0.5f in (r * 255.0f + 0.5f) and the like, cause rounding upwards or is it doing something else?

Edited by robert.leblanc, 14 October 2012 - 02:54 PM.

### #4MJP  Moderators   -  Reputation: 11774

Like
0Likes
Like

Posted 14 October 2012 - 09:39 PM

Yeah you seem to have misunderstood how the shifting operators work. When you have this:

	(dwA << 24) | (dwR << 16) | (dwG << 8) | (dwB << 0)


in a little-endian system this means that B will be at the lowest byte, which is byte 0. A will be at the highest byte, which is byte 3.

What's a little weird is that it's often common to show byte order as going from right to left. When you show things this way (like what you did in that diagram you made) then the byte order direction is consistent with the shifting operators, and is also consistent with how you construct numbers (since when you write a number you put the most significant digits on the left). Using such notation, R8G8B8A8 woud actually be written as "ABGR" byte order. However this is backwards from the actual order in terms of memory addresses, and DXGI lists components in terms of lowest to highest memory address. So for R8G8B8A8, you want (A << 24) | (B << 16) | (G << 8) | (R << 0),

Also does the + 0.5f in (r * 255.0f + 0.5f) and the like, cause rounding upwards or is it doing something else?

Yes, that is for implementing round-to-nearest, which is what the hardware uses when converting from FLOAT to UNORM formats.

Edited by MJP, 14 October 2012 - 09:39 PM.

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.

PARTNERS