Jump to content
  • Advertisement
Sign in to follow this  
theScore

DX11 Memory alignment problem (CPU and GPU)

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

Hi !

I work with DX11 and memory alignment is not correct (i am under windows 64 bit), this is what I have from CPU's program (variables declarations):

__declspec(align(16))
struct VertexInfo
{
	XMFLOAT4A positions;
	XMFLOAT4A normals ;
	XMFLOAT4A texCoords ;
	
};

And my input layout from cpu side :

D3D11_INPUT_ELEMENT_DESC layout[6];
	layout[indexLayout].SemanticName = "POSITION";
	layout[indexLayout].SemanticIndex = 0;
	layout[indexLayout].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;//16 bytes
	layout[indexLayout].InputSlot = 0;
	layout[indexLayout].AlignedByteOffset = 0;
	layout[indexLayout].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
	layout[indexLayout].InstanceDataStepRate = 0;
	indexLayout++ ;

	layout[indexLayout].SemanticName = "NORMAL";
	layout[indexLayout].SemanticIndex = 0;
	layout[indexLayout].Format = DXGI_FORMAT_R16G16B16A16_FLOAT;//8 bytes
	layout[indexLayout].InputSlot = 0;
	layout[indexLayout].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
	layout[indexLayout].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
	layout[indexLayout].InstanceDataStepRate = 0;
	indexLayout++ ;

	layout[indexLayout].SemanticName = "COLOR";
	layout[indexLayout].SemanticIndex = 0;
	layout[indexLayout].Format = DXGI_FORMAT_R8G8B8A8_UNORM;//DXGI_FORMAT_R32G32B32A32_FLOAT a la place ?? //4 bytes
	layout[indexLayout].InputSlot = 0;
	layout[indexLayout].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
	layout[indexLayout].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
	layout[indexLayout].InstanceDataStepRate = 0;
	indexLayout++ ;

	layout[indexLayout].SemanticName = "TEXCOORD";
	layout[indexLayout].SemanticIndex = 0;
	layout[indexLayout].Format = DXGI_FORMAT_R8G8B8A8_UNORM;//4 bytes
	layout[indexLayout].InputSlot = 0;
	layout[indexLayout].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
	layout[indexLayout].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
	layout[indexLayout].InstanceDataStepRate = 0;
	indexLayout++ ;

	layout[indexLayout].SemanticName = "SV_POSITION";
	layout[indexLayout].SemanticIndex = 0;
	layout[indexLayout].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;//laisser à 32 ? //16 bytes
	layout[indexLayout].InputSlot = 0;
	layout[indexLayout].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
	layout[indexLayout].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
	layout[indexLayout].InstanceDataStepRate = 0;
	indexLayout++ ;

	layout[indexLayout].SemanticName = "POSITION";
	layout[indexLayout].SemanticIndex = 1;
	layout[indexLayout].Format = DXGI_FORMAT_R32G32B32A32_FLOAT; //16 bytes
	layout[indexLayout].InputSlot = 0;
	layout[indexLayout].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
	layout[indexLayout].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
	layout[indexLayout].InstanceDataStepRate = 0;
	indexLayout++ ;

And this is my vertex shader struct, the input paralmeters in hlsl:

struct vsIn
{
	float4 position : POSITION ;
 	float4 normal   : NORMAL ;
 	float4 color    : COLOR0 ;
	float4 texCoord : TEXCOORD0 ; 
};

Somebody can help me about data alignment, it seems to be wrong... sad.png If you need more infos/code, don't hesitate to ask me ! smile.png

Edited by theScore

Share this post


Link to post
Share on other sites
Advertisement

Hi !

Thanks for your answer, when you say "your vertex struct contains 32-bit floats" you are talking about hlsl program ?

if yes, I tried to put half4 instead float for normals (positions are 32 bit, so I let float4), I also put colors back in my code but I have the same result sad.png 

Since I 'm using 16 bit normals, should I use another type instead of XMFLOAT4A ? (if yes, which one could I use ?)

Edited by theScore

Share this post


Link to post
Share on other sites


Since I 'm using 16 bit normals, should I use another type instead of XMFLOAT4A ? (if yes, which one could I use ?)

XMHALF4, would likely be what you need.

Share this post


Link to post
Share on other sites

Why do you have POSITION twice? Why do you even have SV_POSITION there? It might be allowed but really confusing.

 

The second POSITION has a SemanticIndex of 1 so that's legal and can be used if you're doing e.g frame interpolation (although it's unusual to not see NORMAL also duplicated).  SV_POSITION in the input layout is totally unnecessary.  The CPU-side vertex struct, the input layout and the HLSL vertex struct also don't match each other, which gives the impression that this code is probably copy/paste from multiple different sources without understanding what's actually being done in each.

Share this post


Link to post
Share on other sites

There is a second POSITION semantic, because the first one is used in order to display the scene in  itself, and the second one is used to store positions (I do the same thing for normals) which I get back in my pixel shader to really store them into surfaces. The purpose is to do deferred rendering, which I did before with directx 9 and works perfectly.

I am newer with directx 11 and I am writing this deferred renderer (DX9) to DX11, which has not the same requirements than DX9, notably for memory alignment for which it is more difficult for me to find a documentation which lights me completely about this subject.

So I try to do my own code but i am missing informations, and if I understand well, each type of data (positions, colors,...) must be in the same order ? And if understand well, their sizes occupied in memory have to be respected too ?

Edited by theScore

Share this post


Link to post
Share on other sites

Maybe I should show you the vertex shader output structure :

struct vsOut
{
	float4 finalPos :SV_POSITION ;
	half4 normal   : NORMAL   ;
	half4 texCoord : TEXCOORD0 ;
	float4 color : COLOR0 ;
	float4 RTposition : POSITION1 ;
};

Share this post


Link to post
Share on other sites

Thanks a lot, I just read your post, I am going to try to fix it.

What I find strange is that it is not easy to find the informations you gave me here, it should be easier, but thank you !

Edited by theScore

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!