Sergio0694 / ComputeSharp

A .NET library to run C# code in parallel on the GPU through DX12, D2D1, and dynamically generated HLSL compute and pixel shaders, with the goal of making GPU computing easy to use for all .NET developers! 🚀
MIT License
2.71k stars 121 forks source link

AppendStructuredBuffer #321

Open Vilo176 opened 2 years ago

Vilo176 commented 2 years ago

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 (with T = struct { ... }), but I don't know how to do it since CompSharp does not allow this type for now.

Any advice ? Thanks ;-)

Sergio0694 commented 2 years ago

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?

Vilo176 commented 2 years ago

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

Sergio0694 commented 2 years ago

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.

Vilo176 commented 2 years ago

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

Sergio0694 commented 2 years ago

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.

Vilo176 commented 2 years ago

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

Sergio0694 commented 2 years ago

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! 😄

Avid29 commented 7 months ago

Any updates here?