Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: pass by reference buffers in shaders

  1. #1
    Intern Contributor
    Join Date
    Oct 2012
    Posts
    52

    pass by reference buffers in shaders

    Hi

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

    Inside the shader:
    Code :
    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
    Code :
    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 "&"
    Last edited by driver; 01-27-2014 at 12:39 PM.

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,212
    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.

  3. #3
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,216
    Quote Originally Posted by driver View Post
    I wanted to create a helper function
    Code :
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •