Sign in to follow this  

Per-instance data in (non-vertex) shaders

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

So I've been using per-instance input slots for providing world and world-view-projection matrices to my vertex shaders for instanced meshes. This works just fine and I've been passing them along through the output of the vertex shader for some small tests when needed. However as shaders get more complex it cannot be the best way to copy such per-instance data for each vertex just to make it available to other shader stages. I know I could set up a constant buffer that contains arrays indexable by the instance id that is a smaller overhead to pass along throughout the shader pipeline, but is there any other way to achieve this? I'd rather not use the cbuffer array approach since the amount of instances in any given frame may vary.

Share this post


Link to post
Share on other sites

You could do more or less the same thing as the constant buffer approach, but instead use an SRV to hold the data.  That allows you to have a variable sized resource that has a much larger potential size than a CB, and you can still access the data in a simple way.  You will have to test it out to see if there is a performance delta for your particular situation though, since you will be trading device memory accesses for interpolants.  It may or may not be a good trade off...

Share this post


Link to post
Share on other sites

Like Jason mentioned if you have your instance data in a buffer or texture (StructuredBuffer is probably the easiest to work with) then you can fetch the data in any stage as long as you pass the InstanceID downward from the vertex shader. 

Share this post


Link to post
Share on other sites

Ah, that's clever, didn't know there were separate buffer resources in addition to having to rebuild and sample a texture.

Thanks!

Edited by Husbjörn

Share this post


Link to post
Share on other sites

This topic is 1273 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.

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