2

I'm working on porting our application from Windows to Linux and I need to understand what does operator [] do in HLSL and how to port in to GLSL.

I have code like this:

red = texture.Sample(sampler, uv)[x];

Sample function should return a pixel value, right? Is x a kind of offset?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Dmitry
  • 317
  • 3
  • 10

1 Answers1

2

[]-operator is the array member selection operator and can also be used to access a vector.

The equivalent glsl code to

red = texture.Sample(sampler, uv)[x];

is

float red = texture(sampler, uv)[x];

texture returns a value of type vec4 (e.g. for sampler2D). The components of the vector can be accessed by the index operator. Since x is the index, it has to be a variable or constant with an integral data type. The vector has 4 components (.x, .y, .z, .w respectively .r, .g, .b, .a), so the value of x has to be 0, 1, 2 or 3.
texture(sampler, uv)[0] is the same as texture(sampler, uv).r. See also GLSL- Swizzling.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174