home/ atoms/ glsl-function-qualifiers-in-out-inout

GLSL's in/out/inout qualifiers set whether a function reads, writes, or modifies an argument

GLSL supports three argument qualifiers specifying data-flow direction. in (read-only, the default) passes a copy into the function; the caller’s variable is unaffected. out (write-only) gives the function storage to write a result into; the value is undefined on entry. inout (read-write) is like pass-by-reference in C: the function receives the current value and any change is reflected in the caller. Choosing the right qualifier clarifies intent and can help the compiler. Utility functions like rgb2hsv typically use in, while functions returning several values use out parameters instead of structs.

Examples

void splitColor(in vec3 color, out float h, out float s, out float b) {
  vec3 hsb = rgb2hsv(color);
  h = hsb.x; s = hsb.y; b = hsb.z;
}
void clampShift(inout vec3 color, in float shift) {
  color = clamp(color + shift, 0.0, 1.0);
}

Assessment

Write a GLSL function that takes a vec3 color as inout and raises its brightness by 0.2. Explain why an in parameter could not achieve the same effect on the caller’s variable.

“it specifies that the variable is read only. In future examples we will see that it is also possible to define arguments as `out` or `inout`.”
corpus · the-book-of-shaders-chapter-6-color-patricio-gonzalez-vivo-a · chunk 2