Jump to content

  • Log In with Google      Sign In   
  • Create Account


DX11: how to transfer matrix to VS


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 fn1186   Members   -  Reputation: 102

Like
0Likes
Like

Posted 19 August 2011 - 06:30 AM

Hi to everyone!

I use DX11 + SM 4.0.
I want to render some amount of spheres (from 1 to 1000).
Each sphere has pre-computed on a CPU transform matrix: XMMATRIX xm = world*view*projection.

The problem I found is how to transfer this matrix to VS over instance buffer.
I have to split the matrix to 4 float4 values, so my layout seems like this:

{"POSITION",  0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA,   0},
...
{"WORLDVIEW", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1},
{"WORLDVIEW", 1, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1},
{"WORLDVIEW", 2, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1},
{"WORLDVIEW", 3, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1},

Vertex shader has this code:
struct VS_In
{
	float3 pos: POSITION;
	float4 transformMatrix0: WORLDVIEW0; //Per-instance value
	float4 transformMatrix1: WORLDVIEW1; //Per-instance value
	float4 transformMatrix2: WORLDVIEW2; //Per-instance value
	float4 transformMatrix3: WORLDVIEW3; //Per-instance value
};

float4 VS(VS_In v) : SV_POSITION
{
	float4 vp = float4(v.pos, 1.0);
	float4x4 mt = float4x4(v.transformMatrix0, v.transformMatrix1, v.transformMatrix2, v.transformMatrix3);
	float4 ret = mul(vp,mt);
	return ret;
}

The question is: is there more elegant way of transferring float4*4 to VS over 1 value?

Thanks in advance

Sponsor:

#2 n3Xus   Members   -  Reputation: 700

Like
1Likes
Like

Posted 19 August 2011 - 06:38 AM

struct VS_In

{

	float3 pos: POSITION;

	float4x4 transformMatrix: WORLDVIEW; //Per-instance value

  

};


#3 kauna   Crossbones+   -  Reputation: 2167

Like
1Likes
Like

Posted 19 August 2011 - 07:00 AM

You may also use a constant buffer to transfer the required matrices. You can store 4x4 matrices in the constant buffer directly so you don't have to reconstruct them in the shader.

You may use SV_InstanceID to index correct matrix in the constant buffer.

Otherwise, precomputing view-projection-world matrix on CPU may be slower than multiplying by world and viewproj matrices in the vertex shader.

Cheers!

#4 fn1186   Members   -  Reputation: 102

Like
1Likes
Like

Posted 22 August 2011 - 01:41 AM

Thank you, n3Xus for help.
There is one minor change to VS_In structure definition is required.
In other case it compiles to very different assembly code.

struct VS_In
{
	float3 pos: POSITION;
-->>> 	row_major matrix transformMatrix: WORLDVIEW; //Per-instance value
};

float4 VS(VS_In v) : SV_POSITION
{
	return mul(float4(v.pos, 1.0), v.transformMatrix);
}

Also I've simplified VS code =).




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS