Reputation: 7961
I have a buffer descriptor defined in GLSL shader in the following way:
layout(set = 1, binding = 0) buffer ShadowMapData { ShadowMapItem items[]; }
uShadowMapDataRegister[];
Is there a way to create a variable from an index of this descriptor globally. I tried the following but it throws syntax errors:
ShadowMapData uShadowMap = uShadowMapDataRegister[6];
error: '' : syntax error, unexpected IDENTIFIER, expecting LEFT_BRACE or COMMA or SEMICOLON
Upvotes: 0
Views: 370
Reputation: 7961
After a while trying to figure out the way to do this, since I was already using preprocessor directives, I used #define
to simplify it while still retrieving the values inline:
// Make sure to not add ";" at the end of this
#define uShadowMap uShadowMapDataRegister[6]
void main() {
uShadowMap.items[1];
}
Since these preprocessor directives work similar to how they work in C, it is also possible to create "functions" from them:
// Make sure to not add ";" at the end of this
#define getShadowMap(index) uShadowMapDataRegister[6].items[(index)]
void main() {
getShadowMap(uSceneData.shadowMapIndex)...
}
Upvotes: 1
Reputation: 473192
ShadowMapData
is not a typename; it's just the name of the descriptor. As far as GLSL is concerned, uShadowMapDataRegister[x]
has no "type".
SPIR-V is actually more concrete than this. For SPIR-V, an entry in an SSBO array does resolve to some particular type, which you can access the elements of. That is, every SSBO has an actual type defined for it that contains its member declarations. So if you were working directly in SPIR-V, you could do it (sort of).
That being said, the specific thing you're trying to do (declare a variable and initialize it from a buffer) is a bad idea. That would provoke a de-jure copy of that buffer into local memory. That's not going to be cheap. And while a compiler might avoid the copy via various means, it's probably best not to ask to make such a copy to begin with. GLSL has no notion of references, so you're just going to have to spell it out.
Also, it wouldn't even work in SPIR-V. An unbounded array can only be created in a limited number of circumstances, such as within a buffer as the last member of the main type of the buffer variable. So you can't create a local variable that is of an unbounded array size.
Upvotes: 2