• 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
jakovo

constant buffer data layout mismatch between app/shader

8 posts in this topic

I started making a basic D3D11 application for Win8 without the Effects framework, everything was ok until I tried to pass constant buffers data to the shader, the data I'm passing to the shader doesn't match the actuall data the shader is reading.

 

 

I have a self made Matrix structure, and a struct containing a float value and the Matrix structure:

struct Matrix
{
    float m00, m01, m02, m03;
    float m10, m11, m12, m13;
    float m20, m21, m22, m23;
    float m30, m31, m32, m33;
};


struct ConstantBuffer
{
    float  value1;
    Matrix value2;
} g_CBData;

.

 

 

After creating the constant buffer object, y update the data in my shader using:

//---------- update constant buffer ----------//
Matrix m =
{
  1.0f, 1.0f, 1.0f, 1.0f,
  0.0f, 1.0f, 0.0f, 0.0f,
  0.0f, 0.0f, 1.0f, 0.0f,
  0.0f, 0.0f, 0.0f, 1.0f
};

g_CBData.value1 = 1.0f;
g_CBData.value2 = m;

D3D11_MAPPED_SUBRESOURCE mappedResource;
g_pDeviceContext->Map(g_pConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
CopyMemory(mappedResource.pData, &g_CBData, sizeof(ConstantBuffer) );
g_pDeviceContext->Unmap(g_pConstantBuffer, 0);

g_pDeviceContext->VSSetConstantBuffers( 0, 1, &g_pConstantBuffer );

.

 

 

However, if I try to read the data I'm sending to the vertex shader using something like this:

buffer cbPerFrame : register (b0)
{
    float  value1;
    float4x4 value2;
};


struct VSInput
{
    float3 Pos : POSITION;
    float4 Col : COLOR;
};


struct VSOutput
{
    float4 Pos : SV_POSITION;
    float4 Col : COLOR;
};


VSOutput main( VSInput vs_in )
{
    VSOutput vs_out;

    vs_out.Pos = float4( vs_in.Pos, 1 );
    vs_out.Col = vs_in.Col * value2._m11; // <- checking "value1" and the values in "value2"

    return vs_out;
}

.

 

the data layout:simply does not match to what I'm sending to it!

 

The weird thing is that if I use in the constant buffer ONLY the "float value1" or "Matrix value2", the shader reads the corresponding data properly (the Matrix gets transposed, as I read it is expected), but if I include in the constant buffer structure both values, it looks like the binary layout simply gets messed up inside the vertex shader.. with apparently no logical order.

 

so.... what am I doing wrong?... how am I supposed to pass the data to the shader so I can read it properly?

 

Thanks!

 

 

0

Share this post


Link to post
Share on other sites
The transpose for the matrix is needed since the mul wants it in column-major order. Also, you have a (probably) padding mismatch between your C++ struct and the shader constant. In HLSL, if a (vector) value crosses a float4 border, it will be aligned to the next float4. Your C++ struct should look something like this.
 

struct ConstantBuffer
{
    float  value1;
    float pad1, pad2, pad3; // dummy pads
    Matrix value2;
} g_CBData;
I haven't checked this. But you can check the offsets with the C++ offsetof macro and on the shader side by looking at the HLSL assembly (or shader reflection). Use fxc.exe with the option /Fx to dump the assembly.

Edit: Ahhh, beaten wink.png Edited by unbird
2

Share this post


Link to post
Share on other sites

Aahhh!... thankyou Hodgman, unbird.

 

yeah, it was the padding on the C++ struct the one causing all the trouble.

 

uhmmm... so I think I will need to find a nice way to force every variable in the C++ structure to be aligned to 16-bytes.

 

Thanks!

0

Share this post


Link to post
Share on other sites

just to complete the post (for anyone else who might run into the same problem and founds this post).

 

After making some experiments with the data layout, it looks like when using more then one data type whose length is less that 16-bytes, the GPU packs them into the same 16-byte "array".

 

What I mean is:

 

If you want to pass properly the data to a constant buffer like this in your shader:

buffer cbPerFrame : register (b0)
{
    float value1;
    float value2;
    float4x4 value3;
};

.

I first thought on giving each independent value a padding like this:

struct ConstantBuffer
{
    float value1;
    float value1_dummy[3];
    float value2;
    float value2_dummy[3];
    Matrix value3;
};

.

Expecting the GPU would read the data with a layout like this:

V1 xx xx xx
V2 xx xx xx
V3 V3 V3 V3
V3 V3 V3 V3
V3 V3 V3 V3
V3 V3 V3 V3

.

However, it turned out that what the GPU is expecting is something like this:

V1 V2 xx xx
V3 V3 V3 V3
V3 V3 V3 V3
V3 V3 V3 V3
V3 V3 V3 V3

.

So, you would have to write your C++ struct like this:

struct ConstantBuffer
{
    float value1;
    float value2;
    float value2_dummy[2];
    Matrix value3;
};

=)

 

just wanted to point it out if anyone runs into the same trouble =P

 

Cheers!

2

Share this post


Link to post
Share on other sites

The issue is that the float4x4 needs to be aligned to 16-bytes to satisfy the constant buffer packing rules (described in the link Hodgman provided). This is why the two float variables are packed normally, and then 8 bytes of padding is inserted. I'd suggesting reading that documentation and becoming familiar with the exact rules.

Note that on the C++ side of things, you can also achieve the same result by having the compiler align the matrix for you using __declspec(align(16)) when defining your Matrix type, or when declaring the Matrix member of your struct.

2

Share this post


Link to post
Share on other sites

 

Plus, if you don't want the matrix to be transposed, add:
#pragma pack_matrix( row_major )

at the top of every shader file you want the matrix to work as passed in.

 

 

You can also use row_major when declaring the float4x4 in your constant buffer, or use the /Zpr command-line option for fxc.exe.

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