Compute shaders : How do you pass not-so-regular structures as parameters?

85 Views Asked by At

I am working in C#, with Unity. I have created a compute shader to which I want to pass my array of C# data structures. That's when I realized I might have gone overboard with fancy data structures, instead of sticking to the C-like structures of shaders.

More specifically, I'm hitting two road blocks:

1. Each of the objects I want to pass to the shader's buffer contains a linked list, which means the size varies between objects. Can the shader buffer be defined as anything else than an array with its size passed explicitly?

2. Each of the objects I want to pass to the shader is polymorphic. i.e. they all have a field "type", but then if type==0 they have a field value0, while the ones with type==1 have a field value1. Is it reasonable to try and achieve that in shaders language (with a C-like union maybe)?

interface IObject {
   public int type { get; }
}

class Type0 : IObject {
   public int type => 0;
   public int field0;
}

class Type1 : IObject {
   public int type => 1;
   public float field1;
}

class ItemForShader {
   public List<IObject> objects { get; set; }
}
...

// Can I pass this to the shader?
var shaderParameters = new List<ItemForShader>() {
    new ItemForShader() {
       objects = new() { new Type0(), new Type1() }
    },
    new ItemForShader() {
       objects = new() { new Type1(), new Type0(), new Type0() }
    }
}

Please note : I don't need you to detail any solution at length (I can already see you describe how to pass a shared pool of linked-list nodes and then share those nodes between all the objects with a fancy indexing system).

I simply want to know what's reasonable and how it's usually done (or not done). Maybe describe in essence what C structure matches what C# structure in that scenario?

EDIT: As I feared, this does not bode well : GLSL array uniform with a dynamic length

Make multiple shaders, one for each number of potential items in the linked lists you want to support. GLSL does not support variable sized arrays. The only other solution is to create arrays with the largest size you'll ever use.

0

There are 0 best solutions below