Skip to main content
GameDev.net gamedev.net
🔒 Locked

DX11: how to transfer matrix to VS

Started by fn1186 Aug 19, 2011 at 12:30 PM 2 replies 2.8k views
Original Post
fn1186
fn1186
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
n3Xus
n3Xus
struct VS_In
{
float3 pos: POSITION;
float4x4 transformMatrix: WORLDVIEW; //Per-instance value

};
kauna
kauna
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!
fn1186
fn1186
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 =).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.