My question is about passing variables to GLSL shader. I'm not sure how that works and what are the performance implications.

Say I got a function that accepts a "vec4" variable. The question is - is that variable copied at the entrance? I guess it makes impact on performance if so. And if it happens to be that way is there a way to pass only references like in C/C++?

share|improve this question
    
GLSL doesn't run on the CPU so you shouldn't think about it in the same terms as code that does run on the CPU. – Le Comte du Merde-fou Mar 4 '14 at 21:00
up vote 6 down vote accepted

GLSL always uses copying, but this doesn't have the same performance implications as C++. In particular, because there is no recursion, there isn't a stack, and typically either functions are inlined and optimized there by the GLSL compiler, or parameters are in fixed register locations and copying is unnecessary.

Note that passing by const& in C++ is completely different from "passing by reference." In passing by reference, the function is allowed to change the argument, and these changes are visible to the calling function. "inout" achieves a similar result, but is not quite the same as vec4& in C++ if you pass the same variable to different parameters of a function.

vec4 is a primitive type in GLSL, you can expect it to behave like a int or float for performance. Finally, vec4 is only 128 bits, which is one or two cycles to copy.

share|improve this answer
    
Thanks, that explains a lot. – spectre Mar 4 '14 at 21:01

You can mark the variable in the signature as "input". This will work as a reference pass (at least I'm the way you have asked).

share|improve this answer

@Vaughan Hilts, this is actually not the case. The GLSL specification states:

6.1.1 Function Calling Conventions

Input arguments are copied into the function at call time, and output arguments are copied back to the caller before function exit.

'in' is the default qualifier and will pass the argument by value.

share|improve this answer
1  
This appears to be a comment. If your asserting that another answer is wrong, simply downvote and leave a comment. – Gnemlock Mar 28 at 23:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.