Per-instance data in (non-vertex) shaders

Started by
2 comments, last by Husbj 9 years, 10 months ago

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.

Advertisement

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...

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.

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

Thanks!

This topic is closed to new replies.

Advertisement