PDA

View Full Version : pass by reference buffers in shaders



driver
01-27-2014, 12:27 PM
Hi

Can pass by reference buffers work in shaders (opengl 4.3)?

Inside the shader:


struct A{
int a;
int b;
}

layout(std430,binding=0) buffer Buffer1{
A a[];
}buffera;

layout(std430,binding=1) buffer Buffer2{
A a[];
}bufferb;





I wanted to create a helper function


void myhelper(buffer &b){
b.a[i]....//do something here
}

myhelper(buffera);


I didn't see anything in the docs to suggest that something like this can be done. It is not working for me when I code it, so I am wondering if pass by reference can be implemented in shaders at all?

thanks

The error I get is:

error C0000: syntax error,unexpected '&', expecting '::' at token "&"

mhagain
01-27-2014, 01:03 PM
From the GLSL 4.3 spec (section 6.1.1):


Functions are called by value-return. This means input arguments are copied into the function at call time, and output arguments are copied back to the caller before function exit.

There is no pass-by-reference, but then GLSL is not C++. You're not dealing with arbitrary blocks of memory here but with values loaded into GPU registers.

Dark Photon
01-27-2014, 06:12 PM
I wanted to create a helper function


void myhelper(buffer &b){
b.a[i]....//do something here
}

myhelper(buffera);


I didn't see anything in the docs to suggest that something like this can be done. It is not working for me when I code it, so I am wondering if pass by reference can be implemented in shaders at all? ... The error I get is:

error C0000: syntax error,unexpected '&', expecting '::' at token "&"

To your question about passing a reference. You can get similar behavior by using inout parameters.

As to uour desire to pass in a buffer and access an array within in it, you can pass arrays as arguments to a function (e.g. out vec4 myarray[5]). So you could do that. However, they must be explicitly sized.

I'm not sure about the buffers-as-parameters question. Don't think so though because it says buffer variables can only be declared at global scope. Try it and see.