ComputeSharp icon indicating copy to clipboard operation
ComputeSharp copied to clipboard

AppendStructuredBuffer

Open Vilo176 opened this issue 3 years ago • 8 comments

Hi Friends,

I have a shader that perfoms a calculation on 3 axis.

For each tuple (ThreadIds.X, ThreadIds.Y, ThreadIds.Z), the shader makes several tests to find matching indexed objects. So, I have to store a list of int for each cell (X,Y,Z).

One way could have been to use a fixed sizes multi-dimensional ReadWriteBuffer, which doesn't seams to exists. Another way could have been to use a HLSL AppendStructuredBuffer<T> (with T = struct { ... }), but I don't know how to do it since CompSharp does not allow this type for now.

Any advice ? Thanks ;-)

Vilo176 avatar Jul 10 '22 12:07 Vilo176

I'm not sure I fully understand your issue, can you elaborate a bit more maybe show a small code sample..? If you're dispatching a shader on 3 axes and for each one you need to store an int value with the result of the computation for that (X, Y, Z) tuple, can't you use a ReadWriteTexture3D<int> and have each shader invocation write the result for that tuple there? Or, even just a ReadWriteBuffer<int> and then you'd calculate the right offset to write the result at yourself. Would that not work?

Sergio0694 avatar Jul 10 '22 13:07 Sergio0694

Hi Sergio,

You are not far from it : for each (x, y, z) I have to store an array of int (from 0 to several tenth of values), representing the indexes of the matching objects for that tuple (actually triangles that are cutted by the cell at xyz).

Vilo176 avatar Jul 10 '22 14:07 Vilo176

I see. Unfortunately I don't really see a way to implement this as a built-in buffer type. The buffer types ComputeSharp exposes are just mapping the real HLSL buffer types, but there's no type like this. In your scenario, you might need to allocate a 3D texture with a depth of the maximum possible number of matches for each item, and then have each tuple write to the right location. This might end up using too memory though, so you might need to find an alternative solution.

Sergio0694 avatar Aug 18 '22 10:08 Sergio0694

Yes indeed, memory consumption would explose. Why couldn't "AppendStructuredBuffer" be a good candidate ?

Vilo176 avatar Aug 18 '22 12:08 Vilo176

Because there's no data type in HLSL that represents that. All resource types in ComputeSharp directly map to existing HLSL data types, it's not possible to just "create a new type". GPUs don't work like normal CPUs, and unfortunately this means you can't just easily create a data structure like that. You might be able to create something like this, but it'd be a custom-built thing with specific logic to support this, and it would need to be structured quite differently. And most importantly, you can't allocate new memory from a shader, which means you'd necessarily need to have an upper bound of maximum values to discover/set.

Sergio0694 avatar Aug 18 '22 12:08 Sergio0694

Ok I was mislead by https://docs.microsoft.com/fr-fr/windows/win32/direct3dhlsl/sm5-object-appendstructuredbuffer

Vilo176 avatar Aug 18 '22 12:08 Vilo176

Uuuh... I had completely missed those two new buffer types 👀 Reopening this for tracking, yeah if those exist in HLSL it would be nice to add support for them in the future. Not for 2.0 at this point, but for a future release for sure. Thank you! 😄

Sergio0694 avatar Aug 18 '22 12:08 Sergio0694

Any updates here?

Avid29 avatar Jan 28 '24 05:01 Avid29