• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
robert.leblanc

DXGI_FORMAT Codes and endianness

3 posts in this topic

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.
0

Share this post


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

[code]
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;
}
[/code]

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 [url="http://msdn.microsoft.com/en-us/library/windows/desktop/ee418728%28v=vs.85%29.aspx#graphics_library_type_equivalence"]here[/url]. In your case you'd want to us XMUBYTEN4, and use x=R, y=G, z=B,w=A. Edited by MJP
1

Share this post


Link to post
Share on other sites
Ok so just to make sure I'm getting it right:

[CODE]
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);
}
[/CODE]

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



[b]-----------------------------------------------------------------------------------------[/b]
The function that I used was given by Luna as:

[CODE]
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);
}
[/CODE]

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


[font=courier new,courier,monospace]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[/font]


[font=courier new,courier,monospace]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?[/font]
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
0

Share this post


Link to post
Share on other sites
Yeah you seem to have misunderstood how the shifting operators work. When you have this:

[code]
(dwA << 24) | (dwR << 16) | (dwG << 8) | (dwB << 0)
[/code]

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),

[quote name='robert.leblanc' timestamp='1350246258' post='4990137']
Also does the + 0.5f in (r * 255.0f + 0.5f) and the like, cause rounding upwards or is it doing something else?
[/quote]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0