How to create a constant buffer with custom data structures

Started by
1 comment, last by Ronan085 10 years, 4 months ago

Hi I've been trying to implement my own math library for use with DirectX11 and I've run into trouble creating the constant buffer.

My data to be sent to the constant buffer looks like this

[source]
struct cbPerObject
{
MATRIX WVP;
MATRIX World;
BOOL hasNormMap;
BOOL isInstance;
}cbPerObj;
[/source]
MATRIX is one of my custom data types and looks like this
[source]
struct MATRIX
{
float _a1, _a2, _a3, _a4;
float _b1, _b2, _b3, _b4;
float _c1, _c2, _c3, _c4;
float _d1, _d2, _d3, _d4;
}
[/source]
(I've left out all the functions MATRIX contains just to keep the post simple)
And when I try to create the constant buffer I keep getting an invalid parameter passed error, if anyone knows a work around for it would be great thanks
Advertisement

Create your device with the D3D11_CREATE_DEVICE_DEBUG and check your Output window in Visual Studio, it will have error messages telling you what the problem is. My guess would be that you're creating your constant buffer buffer using sizeof(cbPerObject), and it's failing because the size isn't a multiple of 16 bytes (which is a requirement for constant buffers). There's 3 easy ways to fix that:

1. Pad your struct to the next multiple of 16 bytes

2. Use __declspec(align(16)) to force the compiler to pad the size to 16 bytes

3. Round up the size when creating the constant buffer by doing (sizeof(cbPerObject) + 15) / 16

thanks @MJP all i had to do was bump sizeof(cbPerObject) up to the next multiple of 16 like you said

This topic is closed to new replies.

Advertisement