George
George

Reputation: 7317

Trouble Understanding Vulkan Shader

From the Vulkan Tutorial:

#version 450
#extension GL_ARB_separate_shader_objects : enable

out gl_PerVertex {
    vec4 gl_Position;
};

vec2 positions[3] = vec2[](
    vec2(0.0, -0.5),
    vec2(0.5, 0.5),
    vec2(-0.5, 0.5)
);

void main() {
    gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
}

Question 1: What does this designate?

out gl_PerVertex {
    vec4 gl_Position;
};

Question 2: What explains the syntax vec2 positions[3] = vec2[](...)? To initialize the array, shouldn't the syntax be

vec2 positions[3] = {
    vec2(0.0, -0.5),
    vec2(0.5, 0.5),
    vec2(-0.5, 0.5)
};

Is this shader-specific syntax, or can arrayType[](...) be used as a constructer in C++ also?

Upvotes: 1

Views: 284

Answers (1)

Rabbid76
Rabbid76

Reputation: 210909

Question 1: What does this designate?

This

out gl_PerVertex {
    vec4 gl_Position;
};

is an Output Interface Block. (See further GLSL Specification - 4.3.9 Interface Blocks)


Question 2: What explains the syntax vec2 positions3 = vec2? To initialize the array, shouldn't the syntax be ...

No


See Array constructors

Arrays can be constructed using array constructor syntax. In this case, the type also contains the [] array notation:

const float array[3] = float[3](2.5, 7.0, 1.5);


See GLSL Specification - 4.1.11 Initializers:

If an initializer is a list of initializers enclosed in curly braces, the variable being declared must be a vector, a matrix, an array, or a structure.

int i = { 1 }; // illegal, i is not a composite 

....

All of the following declarations result in a compile-time error.

float a[2] = { 3.4, 4.2, 5.0 }; // illegal

....

If an initializer (of either form) is provided for an unsized array, the size of the array is determined by the number of top-level (non-nested) initializers within the initializer. All of the following declarations create arrays explicitly sized with five elements:

float a[] = float[](3.4, 4.2, 5.0, 5.2, 1.1);
float b[] = { 3.4, 4.2, 5.0, 5.2, 1.1 };
float c[] = a; // c is explicitly size 5
float d[5] = b; // means the same thing

Upvotes: 1

Related Questions