Sign in to follow this  
Kamil9132

Constant buffer - Memory error

Recommended Posts

Kamil9132    135

I have constant buffer with 2 variables, when i add one more variable to constant buffer i got this error in output window:

First-chance exception at 0x7673C42D in Game.exe: Microsoft C++ exception: _com_error at memory location 0x0021F90C.
First-chance exception at 0x7673C42D in Game.exe: Microsoft C++ exception: _com_error at memory location 0x0021F90C.

What is wrong in this code?

 

.fx:

Texture2D txDiffuse : register( t0 );
SamplerState samLinear : register( s0 );

cbuffer cbNeverChanges : register( b0 )
{
	matrix View;
};

cbuffer cbChangeOnResize : register( b1 )
{
    matrix Projection;
};

cbuffer cbChangesEveryFrame : register( b2 )
{
    matrix World;
	bool cPrzez;
	int kolorP;
};

cbuffer cbPixelShader : register( b3 )
{
	float4 kolorS;
	float4 kolorL;
	int Trans;

};
 

main.cpp:

struct SimpleVertex{

    XMFLOAT3 Pos;
    XMFLOAT2 Tex;

};

struct CBNeverChanges{  

	XMMATRIX mView;

};

struct CBChangeOnResize{

    XMMATRIX mProjection;

};

struct CBChangesEveryFrame{

	XMMATRIX mWorld;
	int cPrzez;
	int cKolorP;

};

struct CBPixelShader{

	XMFLOAT4 kolorS;
	XMFLOAT4 kolorL;
	int Trans;

};

struct INS{

	XMFLOAT4X4 IN_N;
	float Light_Simple;

};
    bd.Usage = D3D11_USAGE_DEFAULT;
    bd.ByteWidth = sizeof(CBNeverChanges);
    bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    bd.CPUAccessFlags = 0;
    d3d11Device->CreateBuffer( &bd, NULL, &g_pCBNeverChanges );
 
    bd.ByteWidth = sizeof(CBChangeOnResize);
    d3d11Device->CreateBuffer( &bd, NULL, &g_pCBChangeOnResize );

    bd.ByteWidth = sizeof(CBPixelShader);
    d3d11Device->CreateBuffer( &bd, NULL, &g_pCBPixelShader );
  
	bd.ByteWidth = sizeof(CBChangesEveryFrame);
    d3d11Device->CreateBuffer( &bd, NULL, &g_pCBChangesEveryFrame );

I got error on this line:

    d3d11Device->CreateBuffer( &bd, NULL, &g_pCBPixelShader );

If i delete int Trans from main.cpp and .fx, program will work properly.

Edited by Kamil9132

Share this post


Link to post
Share on other sites
mhagain    13430

Thank you for help. I thought that padding is automatically added.

 

Yes in HLSL, no in C/C++.

 

Well, actually yes in the latter too, but to a different alignment (which may vary based on your compiler and build options).  When in doubt an explicit #pragma pack on structs that are going to be used in cbuffers is your friend (although it doesn't resolve every possible difference).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this