Hi!
The term „dynamic memory allocation“ caught my attention. In your case it is not needed, true, but if you stumble upon the case where you would like to allocate memory in shaders, there is a nice workaround.
First, you have to preallocate enough memory (you can’t get around this, since dynamic allocations are not possible.)
You could do so by creating a structured buffer that has room for enough elements. Structured buffers can have an internal counter, which is (and that’s interesting) faster than a plain atomic counter. You can use the current value of the counter as address in the structured buffer. If you’d like to add a new element, you can simply increment the counter and thereby get a new exclusive address. This works without problems across multiple threads (although it is a little bottleneck) and is currently only available in pixel shaders and compute shaders. Dx11.1 will add it to the other shader stages as well.
By the way, you can build linked lists with that, too. The
order-independent transparency demos,
screen contribution blending and
tile-based deferred shading do exactly that.
Declaration in the HLSL:
struct DataElement {
int someData;
};
RWStructuredBuffer< DataElement > myBuffer : register( u0 );
And then the usage:
// prepare the data element to write
DataElement element;
element.someData = 1;
// get address
uint nAddress= myBuffer.IncrementCounter();
// write the data element
myBuffer[ nAddress ] = element;
Cheers!