Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GLSLの関数の引数で指定するout修飾子とinout修飾子の違いについて

Last updated at Posted at 2018-05-02

GLSLの関数で参照渡しをするときに、以下のように引数にoutinoutといった修飾子を指定すると思います。

void func(out output) {
   ...
}

outinoutの違いを今までわかっていなかったのですが、以下のページに書いてあったので備忘録としてまとめておきます。
https://www.khronos.org/opengl/wiki/Core_Language_(GLSL)

まずinoutですが、こちらin修飾子と同じように関数の呼び出し時に実引数の値を引数にコピーします。例えば、以下のようなフラグメントシェーダーの場合、colorは最終的に(1.0, 1.0, 0.0)となり、黄色でレンダリングされます。

fragment_shader

void testInout(inout color) {
  color += vec3(0.0, 1.0, 0.0);
}

vod main() {
  vec3 color = vec3(1.0, 0.0, 0.0);
  testInout(color);
  gl_FragColor = vec4(color, 1.0);
}

一方でoutでは関数の呼び出し時に値のコピーをおこないません。そのため値は未定義(値がなにか不明)の状態になります。以下のようなフラグメントシェーダーでは最終的なcolorの値は予測できなくなります。実際に実行してみるとノイズのようなものが出力されることを確認できました。
http://glslsandbox.com/e#46725.0

fragment_shader

void testOut(out color) {
  color += vec3(0.0, 1.0, 0.0);
}

vod main() {
  vec3 color = vec3(1.0, 0.0, 0.0);
  testOut(color);
  gl_FragColor = vec4(color, 1.0);
}

8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?