Jump to content
  • Advertisement
Sign in to follow this  
ankhd

How Does D3D10_APPEND_ALIGNED_ELEMENT Work

This topic is 1503 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

Hello All.

I was playing around with my input element discription and I thought I would use D3D10_APPEND_ALIGNED_ELEMENT, But if I use this my app crashes in strange places.

 

I normaly do this


//this is for my old vertex skinning witch we are using now
const D3D10_INPUT_ELEMENT_DESC VertexBlendVertex::VertexBlendLayout[5] =
{
  	   
	{ "POSITION",	0, DXGI_FORMAT_R32G32B32_FLOAT,			0,		0,  D3D10_INPUT_PER_VERTEX_DATA,		0 },
        { "WEIGHTS",	0, DXGI_FORMAT_R32G32B32A32_FLOAT,		0,		12, D3D10_INPUT_PER_VERTEX_DATA,	0 },
        { "BONES",	0, DXGI_FORMAT_R32G32B32A32_UINT,		0,              28, D3D10_INPUT_PER_VERTEX_DATA,	0 },
        { "NORMAL",	0, DXGI_FORMAT_R32G32B32_FLOAT,		        0,              44, D3D10_INPUT_PER_VERTEX_DATA,	0 },
        { "TEXCOORD",	0, DXGI_FORMAT_R32G32_FLOAT,			0,              56, D3D10_INPUT_PER_VERTEX_DATA,	0 },

};




But if I do this it crashes in random locations

const D3D10_INPUT_ELEMENT_DESC VertexBlendVertex::VertexBlendLayout[5] =
  {
	{ "POSITION",	0, DXGI_FORMAT_R32G32B32_FLOAT,			0,	0,                            D3D10_INPUT_PER_VERTEX_DATA,		0 },
        { "WEIGHTS",	0, DXGI_FORMAT_R32G32B32A32_FLOAT,		0,	D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_VERTEX_DATA,	0 },
        { "BONES",	0, DXGI_FORMAT_R32G32B32A32_UINT,		0,      D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_VERTEX_DATA,	0 },
        { "NORMAL",	0, DXGI_FORMAT_R32G32B32_FLOAT,			0,      D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_VERTEX_DATA,	0 },
        { "TEXCOORD",	0, DXGI_FORMAT_R32G32_FLOAT,			0,      D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_VERTEX_DATA,	0 },
 
 };

Share this post


Link to post
Share on other sites
Advertisement

Your use of D3D10_APPEND_ALIGNED_ELEMENT appears to be correct, so it's likely something else is causing the problem.

 

What do you mean by "crash"?

Share this post


Link to post
Share on other sites
Is the top input element correct with byte offsets. If it is then it could be alignment issue with extra padding being added.

Share this post


Link to post
Share on other sites

Is the top input element correct with byte offsets. If it is then it could be alignment issue with extra padding being added.

 

Yes, the byte offsets are correct.

Share this post


Link to post
Share on other sites

Hi again.

 

Then could it be my vertex structure it got 2 static members. like so



//--------------------------------------------------------------------------
//position normal and textures vertex 
//---------------------------------------------------------------------------
struct VertexBlendVertex
{

	D3DXVECTOR3 Pos;
	D3DXVECTOR4 Weights;
	UINT Bones[4];
    D3DXVECTOR3 Norm;
    D3DXVECTOR2 Tex;

	

	static const D3D10_INPUT_ELEMENT_DESC VertexBlendLayout[5];
  
	static UINT GetNumElements(void){return 5;}
};

How would I align this.

 

and I Created a template class to map vertex structures maybe wrong here it is.



//--------------------------------------------------------------------------------------------------
//this class will lock and unlock a vertex buffer for a mesh the
//it gives us our pointer to our index buffer
//must chech pSourceIndexBuffer for null
//must now call unlock
//we can now pass this to a function with the addition of 2 new functions a default constructor
//and a Lock function
//---------------------------------------------------------------------------------------------------
template <typename T>
class cVertexBuffer 
{
public:
	cVertexBuffer(ID3DX10Mesh *mesh, UINT ibuff)
	{
		pSourceVertexBuffer	= NULL;
		pSourceVertices		= NULL;
		SIZE_T loadingSize = 0;
		Error = false;

		HRESULT hr = mesh->GetVertexBuffer(ibuff, &pSourceVertexBuffer);
		if(FAILED(hr))
		{
			OutPutDebugText("cVertexBuffer Failed To Get Vertex buffer",//the message to display
				"cVertexBuffer::cVertexBuffer",//the location the message was called Eg. what function
						 true);//bool Usewindowmsgbox = false);//true if you also want to display it in a messagebox

			Error = true;
			return;
		}
	
		hr = pSourceVertexBuffer->Map((void**)&pSourceVertices,&loadingSize);
		if(FAILED(hr))
		{
			OutPutDebugText("cVertexBuffer Failed To Map Vertex buffer",//the message to display
				"cVertexBuffer::cVertexBuffer(mesh)",//the location the message was called Eg. what function
						 true);//bool Usewindowmsgbox = false);//true if you also want to display it in a messagebox

			Error = true;
			return;
		}

		//ok all good to use



	}//end constructor 
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////


	cVertexBuffer(void)
	{
		pSourceVertexBuffer	= NULL;
		pSourceVertices		= NULL;
		SIZE_T loadingSize = 0;
		Error = false;

	}//end constructor 
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////




	~cVertexBuffer(void)
	{
	
	}


	bool Lock(ID3DX10Mesh *mesh, UINT ibuff)
	{
		SIZE_T loadingSize = 0;
		HRESULT hr = mesh->GetVertexBuffer(ibuff, &pSourceVertexBuffer);
		if(FAILED(hr))
		{
			OutPutDebugText("cVertexBuffer Failed To Get Vertex buffer",//the message to display
				"cVertexBuffer::Lock(Mesh)",//the location the message was called Eg. what function
						 true);//bool Usewindowmsgbox = false);//true if you also want to display it in a messagebox

			Error = true;
			return false;
		}
	
		hr = pSourceVertexBuffer->Map((void**)&pSourceVertices,&loadingSize);
		if(FAILED(hr))
		{
			OutPutDebugText("cVertexBuffer Failed To Map Vertex buffer",//the message to display
				"cVertexBuffer::Lock(mesh)",//the location the message was called Eg. what function
						 true);//bool Usewindowmsgbox = false);//true if you also want to display it in a messagebox

			Error = true;
			return false;
		}

		//ok all good to use


		return true;//locked ok

	}//end lock 
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////



	void UnLock(bool unmap = true)
	{
		if(pSourceVertexBuffer)
		{
			if(unmap)
				pSourceVertexBuffer->Unmap();

			SAFE_RELEASE(pSourceVertexBuffer);

		}	
	
	}

	//--------------------------------------------------
	//public members
	//--------------------------------------------------

	ID3DX10MeshBuffer *pSourceVertexBuffer;
	T *pSourceVertices;
	bool Error;//true if there is a err\or
};//end class cVertexBuffer
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////


is doing the above map wrong, and if so what way should I create the lock.

 

Thanks everyone for you time and help, I am like a fish out of water here.

Share this post


Link to post
Share on other sites


Then could it be my vertex structure it got 2 static members.

 

What does you mean by "it"? If you mean "my app crashes" from Post #1, then a better approach is to respond to previous questions.

 


What do you mean by "crash"?

 

Searching for the solution to an undefined problem by staring at code that may or may not have relevance is generally unproductive.

 

Which line of code causes the problem and what exactly is the error?

Share this post


Link to post
Share on other sites

I'ts just your run of the mill error.

First-chance exception at 0x00c552ca in RTSTowers.exe: 0xC0000005: Access violation reading location 0x0ad45000.
Unhandled exception at 0x00c552ca in RTSTowers.exe: 0xC0000005: Access violation reading location 0x0ad45000.
The program '[2120] RTSTowers.exe: Native' has exited with code -1073741819 (0xc0000005).

But remember it only happens when I set my vertex input element discription

 

From this

{ "POSITION",		0, DXGI_FORMAT_R32G32B32_FLOAT,		0, 0,  D3D10_INPUT_PER_VERTEX_DATA, 0 },
	{ "NORMAL",			0, DXGI_FORMAT_R32G32B32_FLOAT,		0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },
	{ "TEXCOORD",		0,  DXGI_FORMAT_R32G32_FLOAT,		0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 },

To This


	{ "POSITION",		0, DXGI_FORMAT_R32G32B32_FLOAT,		0, 0,  D3D10_INPUT_PER_VERTEX_DATA, 0 },
	{ "NORMAL",			0, DXGI_FORMAT_R32G32B32_FLOAT,		0, D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_VERTEX_DATA, 0 },
	{ "TEXCOORD",		0,  DXGI_FORMAT_R32G32_FLOAT,		0, D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_VERTEX_DATA, 0 },

And for why I posted the Vertex lock class is it fails when using its locked buffer Like this

	  
//ok we want to set all default bone ids and some
//now we want to fill the vertex and index buffers of this mesh
cVertexBuffer<MultiTextureTerrainVertex> VBPMesh(MC->pMesh, 0);//this is the vertex structure I changedcV
ertexBuffer<VertexBlendVertex> VB(tempmesh, 0);//this is my first posts vertex buffer the skinning one					
if(VB.Error)
{
   OutPutDebugText("Failed To Get Vertex buffer",//the message to display
	"cXMeshParser::CreateSkinnedMesh()",//the location the message was called Eg. what function
	true);//bool Usewindowmsgbox = false);//true if you also want to display it in a messagebox
					
						
	return E_FAIL;//error

}
					
if(VBPMesh.Error)
{
	OutPutDebugText("Failed To Get Vertex buffer VBPMesh",//the message to display
			"cXMeshParser::CreateSkinnedMesh()",//the location the message was called Eg. what function
			 true);//bool Usewindowmsgbox = false);//true if you also want to display it in a messagebox
					
						
		return E_FAIL;//error

}





//set the new vertex positions and later we can set the bone ids and weights to thier values
DWORD Sizev = tempmesh->GetVertexCount();//m_MeshData[ctr].m_Vertices.size();
float w = 1.0f;
//for all the vertices set the vertex data
for(DWORD v = 0; v < Sizev; v++)
{
BREAKS RIGHT HERE 
        VB.pSourceVertices[v].Pos		= VBPMesh.pSourceVertices[v].pos;THIS THE ONE THAT IS NOT VALID POS X,Y,Z = INVALID	
        VB.pSourceVertices[v].Norm		= VBPMesh.pSourceVertices[v].normal;
	VB.pSourceVertices[v].Tex		= VBPMesh.pSourceVertices[v].tex0;
	VB.pSourceVertices[v].Weights		= D3DXVECTOR4(w, w, w, w);//0.0f;
	VB.pSourceVertices[v].Bones[0]		= -1;//rand()%35;//1.0f;
        VB.pSourceVertices[v].Bones[1]		= -1;
	VB.pSourceVertices[v].Bones[2]		= -1;
	VB.pSourceVertices[v].Bones[3]		= -1;
}//end all vertices default skinned 


					
VBPMesh.UnLock();
VB.UnLock();

DID you mean this "Then could it be my vertex structure it's got 2 static members. like so" I forgot the 's. I was talking about the static members of the vertex structure I posted.

 

Lock class is Foo or the alignment is the problem. The meshes all load in other apps like assimp or mveiw and the later one.

 

Is there any code you would like to see I'll post more if needed.

 

And this is what the vertex buffer looks like when rendered. then I used the lock and it crashes out.

 

 

 

ChangeInputElements.jpg?psid=1

Share this post


Link to post
Share on other sites

Out of curiosity, does the error occur accessing VB.pSourceVertices[ 0 ].Pos, or after that? I'm thinking accessing the 0 entry should be okay. Any misalignment would occur after that.

 

In any case, it may be the inclusion of the const variables in the vertex buffer, or the padding (or lack thereof), or a combination of both. I'm not sure how the const variables are handled internally.

 

First: remove any variables from the structure other than those explicitly described in the input element description (not including the dummy padding described below). I.e., the const variables.

 

You might want to just try that first, without the following padding additions.

 

If you have the same problem, to pad the structures, add dummy variables following each element that is not 16 bytes.

 

E.g., following D3DXVECTOR3 variables, add float dummy1; to pad that element with an additional 4 bytes (D3DXVECTOR3(12 bytes) + 4 bytes = 16 bytes). D3DXVECTOR4 entries require no padding as they're 16 bytes (4 floats). D3DXVECTOR2 entries will need an additional 8 bytes, so pad with something like float dummy2, dummy3;

 

So, something like:

D3DXVECTOR3 pos;
float dummy1;
D3DXVECTOR3 norm;
float dummy2;
D3DXVECTOR4 bones;
D3DXVECTOR2 texcoords;
float dummy3, dummy4;

EDIT: It looks like the Lock member function isn't used, as you map it in the constructor.

Edited by Buckeye

Share this post


Link to post
Share on other sites

Hi.

 

Out of curiosity, does the error occur accessing VB.pSourceVertices[ 0 ].Pos, or after that? I'm thinking accessing the 0 entry should be okay. Any misalignment would occur after that

 

at 722 index it has 1722 1000 shy nice even number too.

 

I'll set my structure up how you posted and I'll be back.

Share this post


Link to post
Share on other sites

Ok. That moved the error down to Tex0 member and breaks at vertex 682 not that that matters.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net 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!