Compute shader: buffers inside buffers

Hi!

I’ve been trying to implement a world generation system using compute shaders. But I’ve gotten into a stalemate, I (atleast think) I require a StructuredBuffer inside of a StructuredBuffer!

What I want is probably easier to explain in code, so here it is:

struct MyStruct
{
    /**
      * bunch of variables
      */
  
    StructuredBuffer<MyOtherStruct> myBufferInsideMyBuffer;
};
StructuredBuffer<MyStruct> myBuffer;

However this is ofcourse impossible because of the fact that a struct has to have a constant stride. So the code above would never compile, however somthing like this would:

#define arraySize 42
struct MyStruct
{
    /**
      * bunch of variables
      */
  
    MyOtherStruct array[arraySize];
};
StructuredBuffer<MyStruct> myBuffer;

The problem is now that I am limited to a set amount of MyOtherStruct, and I also have to play cat and mouse with my freedom (how large I make arraySize) and optimization (how small I make arraySize) since I will inevitably iterate over ‘arraySize’ number of elements.

Is there a workaround anyone know of?

The closest I could think of is this:

struct MyStruct
{
    /**
      * bunch of variables
      */
  
    int from;  // start index of myOtherBuffer
    int to;    // end index of myOtherBuffer
};
StructuredBuffer<MyStruct> myBuffer;
StructuredBuffer<MyOtherStruct> myOtherBuffer;

But it seems to be a pain in the a*s to implement. So any other workaround/solution would be highly appreciated!
.

Unfortunately due to the extremely low-level and data-oriented nature of GPU computing, you can’t really do this in an easy way, otherwise memory management could become a big problem with dynamic allocations and references to divergent locations of memory.

You’re on the right track with your final example there. You basically have to create your own heap and manage your own “pointers” (index) into that heap for the different collections of data.

2 Likes