Sign in to follow this  

Question about hardware instancing and HLSL semantics

Recommended Posts

When you are doing instancing your input layout might look something like this (from Frank Luna's DX11 book):

const D3D11_INPUT_ELEMENT_DESC InputLayoutDesc::InstancedBasic32[8] = 
{
    {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
    {"NORMAL",   0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
    {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
    { "WORLD", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
    { "WORLD", 1, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 16, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
    { "WORLD", 2, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 32, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
    { "WORLD", 3, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 48, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
    { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 64,  D3D11_INPUT_PER_INSTANCE_DATA, 1 }
};

My question is, what semantic names can I give to the instanced data? Here Frank Luna is using "WORLD" but I don't see that mentioned on any HLSL semantic reference pages. Also, what if I want to pass a view-projection matrix as part of the instanced data? Can I use "VIEWPROJ"? Does it matter? Does the runtime use the semantics at all?

Cheers!

Share this post


Link to post
Share on other sites
51 minutes ago, belfegor said:

Can you elaborate why is it better to use Structured buffer for instance data?

Thanks.

Because :)

The per instance vertex stream is the historic solution at an age GPU where not able to read memory randomly and Westeros was still the land of the children of the forest ! When you use that, you quickly reach the limit of streams and definitely increase the API calls to bind vertex buffers, plus they are only received by the vertex shader, any property for the pixel shader have to be sent by interpolators, it is costly. I could add more reasons but you get the idea.

 

With a structured buffer, you can store as many information as you want, you can read them at any stage, the only requirement is to forward the instance id. The only quirk is to provide manually the offset to the first instance, usually in a constant buffer as sv_instanceid start at 0.

 

 

Share this post


Link to post
Share on other sites
Just now, Infinisearch said:

Is this really that much of a concern?

It can :) , depends of your data set load, if you draw pong, go for it, if you draw starcraft 2, forget about it ! You can also be concerned with under the hood shader recompilation as there is no input assembly hardware anymore and all is memory fetch inline in the shader code anyway. The only benefit to use vertex stream is to make use of typed load from exotic formats. Really, it is not a big loss in practice.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this