Advertisement Jump to content
Sign in to follow this  

How to create a constant buffer with custom data structures

This topic is 1863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

struct cbPerObject
BOOL hasNormMap;
BOOL isInstance;
MATRIX is one of my custom data types and looks like this
struct MATRIX
float _a1, _a2, _a3, _a4;
float _b1, _b2, _b3, _b4;
float _c1, _c2, _c3, _c4;
float _d1, _d2, _d3, _d4;
(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

Share this post

Link to post
Share on other sites

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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!