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

Constant buffer madness! [fixed]

11 posts in this topic

Hey people,

 

I'm getting some strange errors setting up a constant buffer to send in to my pixel shader (used for rendering directional lights in a deferred renderer).

 

So this is the buffer, C++ followed by HLSL :

struct LightingConstants
{
  DirectX::XMFLOAT3	myLightDirection;
  DirectX::XMFLOAT3	myLightColour;
  DirectX::XMFLOAT3	myCameraPosition;
  DirectX::XMFLOAT2	myHalfPixel;
  DirectX::XMMATRIX	myInverseViewProjection;
  float			myPadding;
};

...

cbuffer LightingBuffer
{
  float3	 myLightDirection;
  float3	 myLightColour;
  float3	 myCameraPosition;
  float2	 myHalfPixel;
  float4x4       myInverseViewProjection;
  float	         myPadding;
};

Which should be 112 bytes.

 

The trouble is when it gets to the pixel shader, some of the values I'm getting are messed up. Setting a light colour of (1.0f, 1.0f, 1.0f) will result in (1.0f, 1.0f, 0.0f) when trying to read from the the buffer, and setting the padding to 0.0f results in a reading of 10.0f (looking at the pix capture).

 

The really odd thing is that when I calculate the size of the buffer code-side (sizeof(LightingConstants)) I get a value of 128, not 112.

 

Stranger still, when I enter the buffer layout in to PIX and take a look at the light colour value - it looks correct, but reading it still gives me (1.0f, 1.0f, 0.0f).

 

Any ideas what I'm doing wrong here? This is how I'm setting up the constant buffer for the pixel shader (I do use a constant buffer for the vertex shader as well, but didn't think that the two would interfere with each other) :

 

Building the buffer :

HRESULT             result;
D3D11_BUFFER_DESC   bufferDescription;

VEDirectXInterface* renderInterface = VoxelEngine::GetInstance()->GetRenderInterface();
assert( renderInterface != NULL );

// Set up the buffer with a byte width of three matrices
bufferDescription.BindFlags             = D3D11_BIND_CONSTANT_BUFFER;
bufferDescription.ByteWidth             = sizeof(LightingConstants);
bufferDescription.CPUAccessFlags        = D3D11_CPU_ACCESS_WRITE;
bufferDescription.MiscFlags             = 0;
bufferDescription.StructureByteStride   = 0;
bufferDescription.Usage                 = D3D11_USAGE_DYNAMIC;

// Create the buffer
result = renderInterface->GetDevice()->CreateBuffer(
	&bufferDescription,
	NULL,
	&myPixelConstantBuffer);

if( FAILED(result) )
{
  return false;
}

return true;

Populating the buffer :

// Grab the lighting constant buffer
result = deviceContext->Map( myPixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource );
if( FAILED(result) )
{
  return false;
}

// Fill in the lighting buffer
lightingBuffer				= (LightingConstants*)mappedResource.pData;
lightingBuffer->myCameraPosition	= aCamera->GetPosition();
lightingBuffer->myHalfPixel		= renderManager->GetHalfPixel();
lightingBuffer->myLightColour		= directionalLight->GetColour();
lightingBuffer->myLightDirection	= directionalLight->GetDirection();
lightingBuffer->myPadding		= 0.0f;

// Calculate the inverse view-projection matrix
XMMATRIX viewProj = XMMatrixMultiply( XMLoadFloat4x4(&aCamera->GetView()), XMLoadFloat4x4(&aCamera->GetProjection()) );
XMMATRIX inverseViewProj = XMMatrixInverse( NULL, viewProj );
lightingBuffer->myInverseViewProjection = XMMatrixTranspose(inverseViewProj);

// Release the buffer
deviceContext->Unmap( myPixelConstantBuffer, 0 );

// Set the constant buffer for the pixel shader
deviceContext->PSSetConstantBuffers( 0, 1, &myPixelConstantBuffer );

Thanks for the help!

Edited by Orangeatang
1

Share this post


Link to post
Share on other sites

Using DirectX::XMFLOAT4x4 for the inverse view/projection matrix seems to fix the size of the buffer code side :

struct LightingConstants
{
 DirectX::XMFLOAT3	myLightDirection;
 DirectX::XMFLOAT3	myLightColour;
 DirectX::XMFLOAT3	myCameraPosition;
 DirectX::XMFLOAT2	myHalfPixel;
 DirectX::XMFLOAT4X4	myInverseViewProjection;
 float			myPadding;
};

And it looks right in PIX as well; (1.0f, 1.0f, 1.0f) for the colour and 0.0f for the padding... but reading in the colour for the pixel shader still returns (1.0f, 1.0f, 0.0f) :

float4 PS( PixelShaderInput anInput ) : SV_TARGET
{
 return float4( myLightColour, 1.0f ); 
}
0

Share this post


Link to post
Share on other sites

No change unfortunately.

 

Switching to use XMFLOAT4x4 does make the constant buffer look correct in a PIX frame capture, but I'm still getting strange values when reading those constants.

Edited by Orangeatang
0

Share this post


Link to post
Share on other sites

Actually, scratch what I said in those last two posts... aligning the members to 16 bytes does work :)

 

Thanks for fixing that up! I thought all off the XM stuff was aligned, guess not!

1

Share this post


Link to post
Share on other sites

Actually, scratch what I said in those last two posts... aligning the members to 16 bytes does work smile.png

 

Thanks for fixing that up! I thought all off the XM stuff was aligned, guess not!

You're welcome. Glad you solve your problem smile.png

1

Share this post


Link to post
Share on other sites

 

Each field in a constant buffer must be 16-byte aligned

#define ALIGN_REGISTER _declspec(align(16))

struct LightingConstants
{
     DirectX::XMFLOAT3    myLightDirection;
     ALIGN_REGISTER DirectX::XMFLOAT3    myLightColour;
     ALIGN_REGISTER DirectX::XMFLOAT3    myCameraPosition;
     ALIGN_REGISTER DirectX::XMFLOAT2    myHalfPixel;
     ALIGN_REGISTER DirectX::XMFLOAT4X4    myInverseViewProjection;
     float            myPadding;
};

.... should work. But maybe XM... types are already aligned, with D3DX I have to manually align the fields.

 

My explanation is quite bad, please check this http://msdn.microsoft.com/en-us/library/bb509632%28v=vs.85%29.aspx

Not all variables in a constant buffer will be aligned to 16-byte boundaries. Alignment issues only occur when a variable will straddle a 16-byte boundary. So for instance this constant buffer won't require any special alignment in your C++ struct:

cbuffer Constants
{
    float Var0; // Offset == 0
    float Var1; // Offset == 4
    float Var2; // Offset == 8
    float Var3; // Offset == 12
}
However something like this will cause the alignment rules to kick in:
cbuffer Constants
{
    float Var0;  // Offset == 0
    float Var1;  // Offset == 4
    float3 Var2; // Offset == 16, since a float 3 would occupy bytes [8-20] which would cross the boundar
    float Var3;  // Offset == 20
}

Hello MJP. That's why I suggested to follow the link....

Could we say that vectors and matrices must be 16-bytes aligned and scalars musn't ?

Thank you for your corrections

Edited by Tournicoti
1

Share this post


Link to post
Share on other sites

 

 

Each field in a constant buffer must be 16-byte aligned

#define ALIGN_REGISTER _declspec(align(16))

struct LightingConstants
{
     DirectX::XMFLOAT3    myLightDirection;
     ALIGN_REGISTER DirectX::XMFLOAT3    myLightColour;
     ALIGN_REGISTER DirectX::XMFLOAT3    myCameraPosition;
     ALIGN_REGISTER DirectX::XMFLOAT2    myHalfPixel;
     ALIGN_REGISTER DirectX::XMFLOAT4X4    myInverseViewProjection;
     float            myPadding;
};

.... should work. But maybe XM... types are already aligned, with D3DX I have to manually align the fields.

 

My explanation is quite bad, please check this http://msdn.microsoft.com/en-us/library/bb509632%28v=vs.85%29.aspx

Not all variables in a constant buffer will be aligned to 16-byte boundaries. Alignment issues only occur when a variable will straddle a 16-byte boundary. So for instance this constant buffer won't require any special alignment in your C++ struct:

cbuffer Constants
{
    float Var0; // Offset == 0
    float Var1; // Offset == 4
    float Var2; // Offset == 8
    float Var3; // Offset == 12
}
However something like this will cause the alignment rules to kick in:
cbuffer Constants
{
    float Var0;  // Offset == 0
    float Var1;  // Offset == 4
    float3 Var2; // Offset == 16, since a float 3 would occupy bytes [8-20] which would cross the boundar
    float Var3;  // Offset == 20
}

Hello MJP. That's why I suggested to follow the link....

Could we say that vectors and matrices must be 16-bytes aligned and scalars musn't ?

 

The CBuffer size cannot be lest than 16 bytes. So the total size of the buffer must be 16 byte aligned not each elements.

0

Share this post


Link to post
Share on other sites

 

 

Each field in a constant buffer must be 16-byte aligned

#define ALIGN_REGISTER _declspec(align(16))

struct LightingConstants
{
     DirectX::XMFLOAT3    myLightDirection;
     ALIGN_REGISTER DirectX::XMFLOAT3    myLightColour;
     ALIGN_REGISTER DirectX::XMFLOAT3    myCameraPosition;
     ALIGN_REGISTER DirectX::XMFLOAT2    myHalfPixel;
     ALIGN_REGISTER DirectX::XMFLOAT4X4    myInverseViewProjection;
     float            myPadding;
};

.... should work. But maybe XM... types are already aligned, with D3DX I have to manually align the fields.

 

My explanation is quite bad, please check this http://msdn.microsoft.com/en-us/library/bb509632%28v=vs.85%29.aspx

Not all variables in a constant buffer will be aligned to 16-byte boundaries. Alignment issues only occur when a variable will straddle a 16-byte boundary. So for instance this constant buffer won't require any special alignment in your C++ struct:

cbuffer Constants
{
    float Var0; // Offset == 0
    float Var1; // Offset == 4
    float Var2; // Offset == 8
    float Var3; // Offset == 12
}
However something like this will cause the alignment rules to kick in:
cbuffer Constants
{
    float Var0;  // Offset == 0
    float Var1;  // Offset == 4
    float3 Var2; // Offset == 16, since a float 3 would occupy bytes [8-20] which would cross the boundar
    float Var3;  // Offset == 20
}

Hello MJP. That's why I suggested to follow the link....

Could we say that vectors and matrices must be 16-bytes aligned and scalars musn't ?

 

Yes indeed the link spells out all of the rules in detail, I just wanted to make sure that anyone reading in this thread didn't come away with incorrect information. smile.png

Saying that vectors need to be aligned and scalars don't is also incorrect. A float2 vector won't need to be aligned if it starts on byte 4 or byte 8, since it can still fit within a 16-byte boundary. Same goes for a float3 that starts on byte 4. Matrices can also be a bit weird if you don't use a float4x4, since you have to consider the alignment for each row. For instance a float3x3 will get packed as 3 float3's, with each float3 aligned to the next 16-byte boundary.

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