I'm trying to understand how passing parameters is implemented in shader languages.
I've read several articles and documentation, but still I have some doubts. In particular I'm trying to understand the differences with a C++
function call, with a particular emphasis on performances.
There are slightly differences between HLSL,Cg
and GLSL
but I guess the underline implementation is quite similar.
What I've understood so far:
- Unless otherwise specified a function parameter is always passed by value (is this true even for matrix?)
- Passing by value in this context hasn't the same implications as with
C++
. No recursion is supported, so the stack isn't used and most function are inlined and arguments directly put into registers. - functions are often inlined by default (
HLSL
) or at least inline keyword is always respected by the compiler (Cg
)
Are the considerations above right?
Now 2 specific question:
Passing a matrix as function parameter
inline float4 DoSomething(in Mat4x4 mat, in float3 vec) { ... }
Considering the function above, in C++
that would be nasty and would be definitely better to use references : const Mat4x4&
.
What about shaders? Is this a bad approach? I read that for example inout
qualifier could be used to pass a matrix by reference, but actually it implicates that matrix be modified by the called function..
Does the number (and type of arguments) have any implication? For example is better use functions with a limited set of arguments?Or avoid passing matrices? Is
inout
modifier a valid way to improve performance here? If so, anyone does know how a typical compiler implement this?Are there any difference between
HLSL
anGLSL
on this? Does anyone have hints on this?