- Home /
Generate a large data structure using a compute shader
I know that RWTexture2D Result;
void CSMain (uint3 id : SV_DispatchThreadID)
{
Result[id.xy] = float3(1,2,3);
}
will result in an texture containing the numbers 1,2,3 however I'm now looking for a way to control a compute shader in such a way as to generate either a 3D texture with control over which order my operation occur or a way to fit a data structure in the return value of the compute shader.
For a simplified example let's say that I want to generate either a 2D array of linked lists with each value in the linked list being equal to 1+the next value so
9-8-7-6 , 11-10-9-8
8-7-6-5 , 6-5-4-3-2
from the input texture
6,8
5,2
or store the same in a 3D texture (the real version will be more complex so we will not be able compute that 5+2=7 but instead have to relly on the fact that 5+1=6 6+1=7)
Any idea how to do this?
Answer by joelv · Apr 01, 2015 at 06:35 AM
You can dispatch a compute job over the 2D domain (X, Y) that is your source texture and read the texture data for that position. You can then have that compute thread write the values required in sequence to your destination.
If you know your max depth (maximum number of values in the new dimension) that will be written you can easily store this as just a buffer of size width*height*depth and index into it, or you can set up your RenderTexture with volume.
If you don't know the max depth (or your worst case does not fit in memory) and want to go the linked list way, keep in mind that you cannot use pointers so you need to allocate links from a pool of some sort (using atomics) and use indices into this pool as your link pointers.
Your answer
![](https://koobas.hobune.stream/wayback/20220613190302im_/https://answers.unity.com/themes/thub/images/avi.jpg)