Jump to content

  • Log In with Google      Sign In   
  • Create Account


How to create a constant buffer with custom data structures


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
2 replies to this topic

#1 Ronan085   Members   -  Reputation: 169

Like
0Likes
Like

Posted 14 December 2013 - 04:31 PM

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
{
MATRIX WVP;
MATRIX World;
 
BOOL hasNormMap;
BOOL isInstance;
}cbPerObj;
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


Sponsor:

#2 MJP   Moderators   -  Reputation: 11010

Like
1Likes
Like

Posted 14 December 2013 - 04:39 PM

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



#3 Ronan085   Members   -  Reputation: 169

Like
0Likes
Like

Posted 14 December 2013 - 05:23 PM

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






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