Constant buffer madness! [fixed]

Started by
10 comments, last by Capoeirista 10 years, 9 months ago

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!

Advertisement

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

[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

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

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.

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!

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

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
}

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

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.

This topic is closed to new replies.

Advertisement