- Home /
Two dimensional array in compute shader
I want to use a compute shader to calculate movements of cloth particles.
Cloth particle is a struct that contains three Vector3s its current position, previous position and acceleration to add. On CPU side I keep those particles in a 2D array, this way I know the place of each particle in the structure. Each frame I calculate new position for each particle in the structure. I want to do the same with a compute shader.
The question is, can I pass the 2D array of structures to compute shader via the ComputeBuffer.SetData() and access each particle by its position in the structure? Or do I have to pass it as a 1D array and restructure my code to process data this way?
I'm now to compute shaders and I need to understand some fundamental things to get everything right.
Answer by _dns_ · Jul 09, 2020 at 03:58 PM
Hi, I'm doing something like this with independents particles (not in a 2D organization) so the high level concept you describe works :-) For 2D arrays, I don't think it's possible to declare such thing for Structured Buffers but you can emulate a 2D array with a 1D array anyway (table[ x + y * size_x], It also saves a bounds check in C# so it's always good to do that when CPU is important). There are also some variant of SetData() and ComputeBuffers related API that works with NativeArrays, I would also recommend to use that to reduce Garbage creation and maybe some memcopy :-)
I think you could also work with 1 (or more) Texture that you can read and write to in a Compute Shader like a 2D array. I'm not sure of the performance though, and it could be difficult to organize/store all Vectors in different textures RGBA floats. I guess Structured Buffers were designed to not have to deal with that :-)
Thanks for your answer. I just wanted to clarify for myself if 2D arrays in compute buffer are possible at all. I want to have two copies of my code: GPU and CPU for falling back if compute shaders are not supported. And I want to know how to organize things on the CPU side to copy the array to and from GPU more easily, without converting it. It looks like I'll need to update my current code to work with 1D array.
Well, even if it's better, having the (nearly) exact same code for CPU and GPU is not always possible: customization may be better for some optimizations. I would also recommend to try the job system from the new DOTS framework for the CPU fallback code. It's quite easy to use and works with 1D arrays too (and it's easy to switch between single/multi threaded for debugging purpose, and the code should be closer to the compute shader one, and this does not have to involve the Entity system: just the convenient Unity C# Jobs system :-)
Thanks a lot! This makes a lot of sense. I'll try it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613002821im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Lerp() and compute shader 0 Answers
How (Or Why) to use Consume Buffers safely on arbitrary data lengths 0 Answers
Callback when (compute) shader is reloaded 0 Answers
,Drawing 100 000+ cubes 1 Answer
How do you use Mathf.PerlinNoise in a compute shader? 2 Answers