GLSL icon indicating copy to clipboard operation
GLSL copied to clipboard

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

Open realazthat opened this issue 3 years ago • 5 comments

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:

realazthat avatar Nov 14 '20 23:11 realazthat

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.

Cazadorro avatar Nov 15 '20 09:11 Cazadorro

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.

azhirnov avatar Nov 17 '20 00:11 azhirnov

@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.

Cazadorro avatar Nov 17 '20 04:11 Cazadorro

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

azhirnov avatar Nov 17 '20 04:11 azhirnov

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.

enerc avatar Nov 24 '20 11:11 enerc