Jump to content

  • Log In with Google      Sign In   
  • Create Account


Constant buffer madness! [fixed]


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.

  • You cannot reply to this topic
11 replies to this topic

#1 Orangeatang   Members   -  Reputation: 1438

Like
1Likes
Like

Posted 15 July 2013 - 04:53 AM

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, 15 July 2013 - 05:54 AM.


Sponsor:

#2 Orangeatang   Members   -  Reputation: 1438

Like
0Likes
Like

Posted 15 July 2013 - 05:14 AM

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 ); 
}


#3 Tournicoti   Prime Members   -  Reputation: 683

Like
3Likes
Like

Posted 15 July 2013 - 05:46 AM

[EDITED]

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

 

Please check this http://msdn.microsoft.com/en-us/library/bb509632%28v=vs.85%29.aspx


Edited by Tournicoti, 16 July 2013 - 01:39 AM.


#4 Orangeatang   Members   -  Reputation: 1438

Like
0Likes
Like

Posted 15 July 2013 - 05:47 AM

I'm pretty sure that XM types are byte aligned, but I'll give that a go, thanks.


Edited by Orangeatang, 15 July 2013 - 05:54 AM.


#5 Orangeatang   Members   -  Reputation: 1438

Like
0Likes
Like

Posted 15 July 2013 - 05:49 AM

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, 15 July 2013 - 05:53 AM.


#6 Orangeatang   Members   -  Reputation: 1438

Like
1Likes
Like

Posted 15 July 2013 - 05:51 AM

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!



#7 Tournicoti   Prime Members   -  Reputation: 683

Like
1Likes
Like

Posted 15 July 2013 - 05:57 AM

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



#8 MJP   Moderators   -  Reputation: 10607

Like
3Likes
Like

Posted 15 July 2013 - 02:33 PM

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
}


#9 Tournicoti   Prime Members   -  Reputation: 683

Like
1Likes
Like

Posted 15 July 2013 - 05:04 PM

 

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, 16 July 2013 - 01:38 AM.


#10 BornToCode   Members   -  Reputation: 915

Like
0Likes
Like

Posted 15 July 2013 - 10:05 PM

 

 

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.



#11 MJP   Moderators   -  Reputation: 10607

Like
1Likes
Like

Posted 16 July 2013 - 12:02 AM

 

 

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.



#12 Orangeatang   Members   -  Reputation: 1438

Like
0Likes
Like

Posted 16 July 2013 - 01:13 AM

Thanks for all the help and information folks!






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