Sign in to follow this  
AdeptStrain

Compute/DrawIndirect question

Recommended Posts

AdeptStrain    406

Hi all,

 

I have a compute shader that generates a number of points which I then want to render via DrawIndirect. I understand how to generate and copy over the number of points and such (AppendStructuredBuffer -> CopyStructureCount -> DrawIndirectArgs buffer), but I don't understand how to bind the output from the compute shader (i.e. the AppendStructuredBuffer) to the input assembler so it can be drawn once I call DrawIndirect.

 

Do I have to create a separate vertex buffer, copy the output into that, and then bind and render that? Or is there a way to tell the assembler, here's a buffer, using a point topology, and then just send things down the pipeline like normal?

Share this post


Link to post
Share on other sites
MJP    19791

You don't actually need to bind it to the input assembler. You can just bind a NULL input layout, and bind the structured buffer to the VS stage as a shader resource view. Then in your vertex shader, you can manually sample from the structured buffer using SV_VertexID or SV_InstanceID.

Share this post


Link to post
Share on other sites
AdeptStrain    406

Ah, I see. So you use the SV_VertexID (which I assume comes from the draw indirect params) as an index into the structured buffer, grab the data, and pass it along the pipeline from there. That makes sense. Thanks MJP.

Share this post


Link to post
Share on other sites
MJP    19791

SV_VertexID is a system-generated value that's always available to your vertex shader, and it gives you the index of the vertex that you're operating on. So for non-indexed draws it will be 0-(N-1), where N is the number of vertices that you submitted. For indexed draws, it will be the value that was retrieved from your index buffer.

When you do it this way you can actually do cool things like expanding from point->quad without a geometry shader. Basically you set it up so that your indirect draw arguments specify that you have 4 vertices and N instances (where N is the number of points from your append buffer). In your vertex shader you then use SV_InstanceID to read the quad position from your structured buffer, and you use SV_VertexID to figure out which corner of the quad you're dealing with and offset the vertex accordingly.

Share this post


Link to post
Share on other sites
AdeptStrain    406

One other question, MJP. If you have a buffer bound as an AppendStructureBuffer in one shader, can it be just a simple "read only" StructureBuffer in another shader, or do you have to bind is as a RWStructureBuffer even if you have no intention of writing to it?

Share this post


Link to post
Share on other sites
MJP    19791

You can bind it as a StructuredBuffer. When you make a shader resource view and bind it to the pipeline, it always correspond to the "read only" version of a particular resource when you're dealing with it in shader code (Texture2D, Buffer, StructuredBuffer, etc.). Unordered access views always correspond to the "read/write" version of a resource (RWStructuredBuffer, RWTexture2D, RWBuffer, etc.).

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