Jump to content
  • Advertisement
MasterReDWinD

3D Accessing AppendStructuredBuffer count

Recommended Posts

Hi,

I am using a Unity compute shader to create a vertex buffer and index buffer.

The vertex buffer is an

AppendStructuredBuffer<float3> vertexBuffer

Each thread calculates a vertex and then calls

vertexBuffer.Append(vertex)

Later in that same function (in that same kernel) I have a

 RWStructuredBuffer<int> indexBuffer

I would like to add the index of the vertex just placed in vertexBuffer to indexbuffer.  However there doesn't seem to be any way to get the index of the last item added to the vertex Append buffer?

I'm aware of CopyCount but that doesn't seem appropriate here.  I've also looked into CopyStructureCount but I don't think that can be called in a Unity compute shader?

Thanks

Share this post


Link to post
Share on other sites
Advertisement
Posted (edited)

Seems you need to implement the append logic manually to get the counter value. I haven't done this, but e.g. this shader compiles:

RWStructuredBuffer<float3> Vertices;
RWStructuredBuffer<uint> Indices;

[numthreads(8,8,4)]
void CS(uint3 index: SV_DispatchThreadID)
{
	uint v0 = Vertices.IncrementCounter()-1;
	Vertices[v0] = float3(index);
	uint v1 = Vertices.IncrementCounter()-1;
	Vertices[v1] = float3(index) + 1;
	uint v2 = Vertices.IncrementCounter()-1;
	Vertices[v2] = float3(index) + 2;
	
	uint i0 = Indices.IncrementCounter()-1;
	Indices[i0] = v0;
	uint i1 = Indices.IncrementCounter()-1;
	Indices[i1] = v1;
	uint i2 = Indices.IncrementCounter()-1;
	Indices[i2] = v2;
}

Take a look at the documentation : You need to create the buffers with the  D3D11_BUFFER_UAV_FLAG_COUNTER flag. Hopefully Unity lets you do this.

PS: What do you mean by CopyCount ? I'm not familiar with that. Is that Unity specific (or was that a typo) ?

Edited by unbird

Share this post


Link to post
Share on other sites

Thanks a lot for your reply.  I had come across IncrementCounter and thought I might need to make the buffer manually.  Your example really helps. 

ComputeBuffer.CopyCount copies the counter from one buffer to another buffer on the CPU side.  I use it at the moment to get the size of the Append buffers.  Luckily, from looking at the documentation it sounds like it supports any buffer created with the D3D11_BUFFER_UAV_FLAG_COUNTER flag, which means it should work still when manually implementing the Append buffer logic.

Share this post


Link to post
Share on other sites

Ah I see. Yeah, so it's basically Unity's version of CopyStructureCount.

Definitively experiment with the flags and HLSL, Though an append buffer seems to be sort of an RW buffer, my snippet did not compile then (append buffer does not allow IncrementCounter et.al.). Good luck anyway. Compute shaders are fun.

Share this post


Link to post
Share on other sites
Posted (edited)

Hmmm, can't edit posts after some time ? So sorry for the bump. I have one additional note to make: You can of course use an append buffer for the indices:

RWStructuredBuffer<float3> Vertices;
AppendStructuredBuffer<uint3> IndicesAppend;

[numthreads(8,8,4)]
void CS2(uint3 index: SV_DispatchThreadID)
{
	uint v0 = Vertices.IncrementCounter()-1;
	Vertices[v0] = float3(index);
	uint v1 = Vertices.IncrementCounter()-1;
	Vertices[v1] = float3(index) + 1;
	uint v2 = Vertices.IncrementCounter()-1;
	Vertices[v2] = float3(index) + 2;
	
	IndicesAppend.Append(uint3(v0,v1,v2));
}

Here the triangle indices are also pushed together. I always suspect race conditions with such sequences like in the first snippet (which would rip a triangle list apart:P )

Edited by unbird

Share this post


Link to post
Share on other sites

Thanks for the update.  I've settled on a mix of RWStructuredBuffer and Append/Consume buffers at the moment.

I've had some fun with race conditions already :)

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!