Jump to content
  • Advertisement
Sign in to follow this  
fn1186

DX11 DX11: how to transfer matrix to VS

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

Share this post


Link to post
Share on other sites
Advertisement
struct VS_In
{
float3 pos: POSITION;
float4x4 transformMatrix: WORLDVIEW; //Per-instance value

};

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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 =).

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!