Public Group

# CreateBuffer returns E_INVALIDARG

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

## Recommended Posts

I'm having trouble creating a constant buffer. This is the buffer structure.

struct ConstantBuffer
{
D3DXMATRIX matWorld;
D3DXMATRIX matView;
D3DXMATRIX matProjection;
D3DXVECTOR3 vLightDir;
D3DXVECTOR4 vLightCol;
};



Here's how I create the buffer

	D3D11_BUFFER_DESC BufferDesc;
CLEAR(BufferDesc);
BufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
BufferDesc.ByteWidth = sizeof(ConstantBuffer);
BufferDesc.Usage = D3D11_USAGE_DEFAULT;
V_RETURN(m_pD3DDev->CreateBuffer(&BufferDesc,0,&pConstantBuffer));



What the CLEAR does is just ZeroMemory

I have create the constant buffer that only contains the matrices successfully, but this one fails.

##### Share on other sites

What does sizeof(ConstantBuffer) return? The ByteWidth value must be a multiple of 16.

Also, if you're using D3D11, you really should stop using D3DX* structures and start using the XM* equivalents from the DirectXMath library.

##### Share on other sites

What does sizeof(ConstantBuffer) return? The ByteWidth value must be a multiple of 16.

Also, if you're using D3D11, you really should stop using D3DX* structures and start using the XM* equivalents from the DirectXMath library.

What's the difference between XM and D3DX math? I tried to use XM but  the application crashed when I transpose a matrix so I keep using D3DX math

##### Share on other sites

XMMath is a newer version of the math library based of the Xbox math library, So basically it is meant to supersede D3DX math.

Or that is my understanding of it.

##### Share on other sites

application crashed when I transpose a matrix

Matrix wasn't aligned properly. Googling should give an answer.

##### Share on other sites

As already pointed out the structure must be 16byte-aligned, this is not the cast with your vector3. You should change it to:

struct ConstantBuffer
{
D3DXMATRIX matWorld;
D3DXMATRIX matView;
D3DXMATRIX matProjection;
D3DXVECTOR3 vLightDir;
D3DXVECTOR4 vLightCol;
};


or

struct ConstantBuffer
{
D3DXMATRIX matWorld;
D3DXMATRIX matView;
D3DXMATRIX matProjection;
D3DXVECTOR4 vLightCol;
D3DXVECTOR3 vLightDir;
};


or simply modify the return of sizeof(ConstantBuffer) to be a multiple of 16.

##### Share on other sites

XMMath is a newer version of the math library based of the Xbox math library, So basically it is meant to supersede D3DX math.

Or that is my understanding of it.

Not only is XNAMath (XM, which is now renamed to DirectXMath already) newer it also supports SSE2 by default where D3DXMath didnt, it did if you turned the compile options in VS for it on. DirectXMath actually in the header detects whether the target it is being compiled for supports SSE2 and if so starts using SSE2 intrinsics and all the proper ways of using these.

As already pointed out the structure must be 16byte-aligned, this is not the cast with your vector3. You should change it to:

struct ConstantBuffer
{
D3DXMATRIX matWorld;
D3DXMATRIX matView;
D3DXMATRIX matProjection;
D3DXVECTOR3 vLightDir;
D3DXVECTOR4 vLightCol;
};


or

struct ConstantBuffer
{
D3DXMATRIX matWorld;
D3DXMATRIX matView;
D3DXMATRIX matProjection;
D3DXVECTOR4 vLightCol;
D3DXVECTOR3 vLightDir;
};


or simply modify the return of sizeof(ConstantBuffer) to be a multiple of 16.

__declspec( align ( 16 ) ) float;
#define ALIGN16 __declspec( align(16) )
ALIGN16 float


When using the declspec or macro the compiler will automatically add padding when needed without you having to write this explicitly, it keeps your code slightly cleaner and easier to understand.

As an example

struct MaterialContent
{
MaterialContent()
{
m_ambient[0] = 0.2f;
m_ambient[1] = 0.2f;
m_ambient[2] = 0.2f;
m_ambient[3] = 1.0f;

m_diffuse[0] = 0.8f;
m_diffuse[1] = 0.8f;
m_diffuse[2] = 0.8f;
m_diffuse[3] = 1.0f;

m_specular[0] = 0.0f;
m_specular[1] = 0.0f;
m_specular[2] = 0.0f;
m_specular[3] = 1.0f;

m_emissive[0] = 0.0f;
m_emissive[1] = 0.0f;
m_emissive[2] = 0.0f;
m_emissive[3] = 1.0f;
m_shininess = 1000.0f;
}

float m_ambient[4];
float m_diffuse[4];
float m_specular[4];
float m_emissive[4];
ALIGN16 float m_shininess;
};

Edited by NightCreature83

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 19
• 15
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634097
• Total Posts
3015509
×