KhronosGroup / GLSL

GLSL Shading Language Issue Tracker
328 stars 96 forks source link

Allow passing dynamic arrays (in SSBOs) as function arguments. #142

Open realazthat opened 3 years ago

realazthat commented 3 years ago

Like the title says: Allow passing dynamic arrays (in SSBOs) as function arguments. (By reference).

I have some complicated functions that walk a tree-structure stored in an SSBO dynamic array, like:


layout(std430) buffer TreeNodes_ {
  TreeNode TreeNodes[];
};

And I have some utility functions that are able to peruse the tree, like:

NodeID GetNode(NodeID from, TreePath path);

Alas, I am unable to use multiple trees in a single program, because I am forced to use a global tree variable, because glsl does not allow one to pass a reference to a dynamic array.

Something like this would be nice:

NodeID GetNode(TreeNode TreeNodes[], NodeID from, TreePath path);

Related:

Cazadorro commented 3 years ago

The fact this isn't allowed makes zero sense, as I understand it, it's an extremely old conservative holdover restriction not even directly related to SSBO's themselves. It's been complained about for as long as SSBO's have been introduced, yet we've seen no movement on this issue for whatever reason.

Not allowing buffers to be passed to functions in this manner forces massive amounts of duplicated code, such as indexing code, or any code that needs buffers of the same type.

My understanding of problem is that the semantics of GLSL require knowing the size of the type because the "abstract machine" GLSL uses is always copy in, copy out. This doesn't work if you don't know the size at compile time. What I don't understand is why they can't make a special case for buffers, in which you copy the handle/pointer to the buffer around conceptually. It seems like a pedantic problem, not a practical one.

azhirnov commented 3 years ago

Some years ago I've pathed glslang to use dynamic arrays in functions. I allow compiler to keep open array in function arguments. Then walk through glslang AST, inline functions with this arguments and replace argument by SSB name.

Cazadorro commented 3 years ago

@azhirnov If I understand you correctly, you parsed GLSL as if it had support for SSBOs as function arguments, then inlined the call and spit out regular GLSL? Do you have an example of this code? I figured this would be the solution if this wouldn't be acceptable in SPIR-V, in fact I believe this same strategy can work with opaque references as well.

azhirnov commented 3 years ago

@Cazadorro example: origin source, after inlining. translator, but it requires modified glslang which I can not find

enerc commented 3 years ago

Same here. I have a dataset bigger than 4GB, so I have to split it in multiple buffers. But I can't select the proper buffer and pass it to my functions. Needs lots of code to test which buffer to use with the performance penalties of wave/warp divergence.