CreateBuffer returns E_INVALIDARG

Started by
5 comments, last by NightCreature83 10 years, 2 months ago

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.

Advertisement

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 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

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.

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.


application crashed when I transpose a matrix

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

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;
        float padding; // unused
	D3DXVECTOR4 vLightCol;
};

or


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

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

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;
        float padding; // unused
	D3DXVECTOR4 vLightCol;
};

or


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

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

You dont need to add padding just :


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

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

This topic is closed to new replies.

Advertisement