OpenGL OpenGL Shaders 3 — Questions and Answers
Question 1: In GLSL, what is the data type of a 2D texture sampler?
- texture2D
- sampler2D (Correct answer)
- image2D
- uniform2D
Correct answer: sampler2D
sampler2D is the GLSL opaque type for binding a 2D texture to a fragment or vertex shader.
Question 2: What does the GLSL discard statement do in a fragment shader?
- Discards all vertex data
- Prevents the fragment from being written to the framebuffer (Correct answer)
- Resets uniform values
- Ends the current draw call
Correct answer: Prevents the fragment from being written to the framebuffer
discard terminates the fragment shader and drops the fragment so it does not update the framebuffer.
Question 3: Which GLSL built-in provides the window-space position of the current fragment?
- gl_Position
- gl_FragDepth
- gl_FragCoord (Correct answer)
- gl_PointCoord
Correct answer: gl_FragCoord
gl_FragCoord is a read-only built-in containing the (x, y, z, 1/w) window-space coordinates of the fragment.
Question 4: What OpenGL function retrieves the location of a uniform variable in a linked shader program?
- glGetAttribLocation
- glGetUniformLocation (Correct answer)
- glBindUniform
- glUniform1i
Correct answer: glGetUniformLocation
glGetUniformLocation(program, name) returns the integer location index used to set that uniform's value.
Question 5: Which shader stage runs once per primitive and can modify or generate vertices between the vertex and fragment stages?
- Compute shader
- Geometry shader (Correct answer)
- Tessellation shader
- Transform feedback
Correct answer: Geometry shader
The geometry shader receives a complete primitive and can emit zero or more new primitives to the rasterizer.
Question 6: What GLSL built-in variable gives the index of the current vertex in a draw call?
- gl_InstanceID
- gl_VertexID (Correct answer)
- gl_DrawID
- gl_PrimitiveID
Correct answer: gl_VertexID
gl_VertexID contains the index of the current vertex, useful for procedurally generating per-vertex data.
In GLSL, what is the data type of a 2D texture sampler?