Shaders Flashcards
Lecture 2 (18 cards)
What is a Shader?
- Shader is a computer program that runs on the GPU.
- A Shader has single instruction with mulitple data.
- Each shader has no communication between threads.
What is a Shader Program?
- Shader program is a set of shaders required for the entire pipeline
- There are different Shader programs and each of them have their uses depending on which pipeline it’s used in and which step.
What is the difference of use for Shader Program in Rasterized pipeline and General purpose pipeline?
- Shader Programs such as Vertex shader and fragment shader are mandatory in the Rasterized Pipeline.
- Though it is optional to include geometry shader and tessellation shaders.
- On the otherhand in the General purpose pipeline, only one compute shader and it cannot be combined with other types.
What are Attributes?
- Attributes are variables that are specific to each instance (vertex or fragment for us, but there are more sophisticaed uses)
- An attribute could e.g. be output one stage but input the next stage. Application provides the attributes for the first stage (Vertex Attribute)
- There are attributes in the data stores in VBO or pointers stored in VAO
- Some basic types are: int, float, bool, vectors and matrices
What are Uniforms?
- Uniforms are variables with same value for all the data set (one call of glDrawXX)
- This variables can change every frame and is provided by the application.
- Some basic types are: int, float, bool, vectors and matrices
What are Constants?
- Constants are values that never change
How do you Compile shaders? Give an example
- Shader code needs to be compiled to the specific hardware.
- Compile each shader type separately
- Don’t use shaders directly, they need to generate shader programs
- You are allowed to link one or more shaders to generate a shader program.
Example:
* Set vertex shader source code
* Compile vertex shader
* Set fragment shader source code
* Compile fragment shader
* Attach vertex shader and thereafter fragment shader to program
* Link shader program
Which shaders can be used during Vertex Processing?
Vertex Processing: the 2nd step in the rasterization pipeline
- Vertex Shader
- Tessellation
- Geometry Shader
What is a Vertex Shader?
A vertex shader is a programmable stage in the graphics pipeline that processes each vertex, transforming its position and passing data like color or texture coordinates to the next stage.
What is a Tessellation?
- Tessellation is the process that subdivides patches of vertex data into primitives.
This is done so in 3 steps
1. Tesselation control shader (which is optional) controls how much tessellation each patch gets. Optionally, if not provided, all patches have the same tessellation
2. Tessellation primitive generation
3. Tessellation evaluation shader is required if tessellation is enabled. Here we set the attributes for all the vertices of the tessellated patch and it is executed once for all tessellation output vertex.
What is a Geometry shader?
A geometry shader is a programmable shader stage that takes primitives (points, lines, triangles) as input and can generate new geometry (like additional vertices or shapes) before rasterization.
Which shaders can be used during Fragment Processing?
Fragment Processing: the 6th step in the rasterization pipeline
- Fragment shader
- Data interpolation
What is a Fragment shader?
- Fragment shader is a shader that processes one of the fragments generated by the rasterizer.
- This shader generates one color value per framebuffer + one depth value.
- The input attribute is the same as output attribute from previous stage, usually the vertex shader.
- The attributes are Linearly interpolated at the position of the fragment from the rasterizer.
- The main output of this shader is a color, the format depends on the framebuffer. The most common format for this output is 4 components (RGBA) in normalized float (0.0 - 1.0)
- If depth buffer is enabled an additional depth output will appear
- By default, the depth output is implicitly computed from the position, but it is possible to output custom depth value.
What is Data interpolation?
- Input attributes linearly interpolated from vertices
- Not all data is linear and **not **all data can be interpolated
- A way to combat non-linear data is to normalize the data, since we preserve the direction but not the length. This means that we can more easily calculate values when the value is normalized. Another way is if the projected position is in homogeneous coordinates. This would mean that we can divide the position by the fourth component (w).
- A data that can’t be interpolated is Integers for instance. This data type is discrete and does not represent the precise values of floats that we need in order to have smooth interpolation. Worst case you can use interpolation qualifier flat if you want to use an integer.
Linear interpolation: is the simplest form of interpolation that calculates a value between two known points by assuming a straight-line change between them.
Which shaders can you use in the General purpose pipeline?
- Compute shader
- Shader Storage Buffer Object (SSBO
What is a Compute shader?
A compute shader is a GPU program used for general-purpose parallel computation, allowing custom data processing without the traditional graphics pipeline.
What is a Shader storage buffer object (SSBO)?
- Shader storage buffer object (SSBO) is an Buffer Object like VBO and EBO.
- The target for this BO is (GL SHADER STORAGE BUFFER) and contains any kind of shader data.
The buffer layout on GPU is defined in the compute shader:
layout(std430) buffer Name { int foo; float bar[100]; }
The buffer layout on CPU is defined by the data stored in it.
std430: is a set of rules that define the layout to be similar to CPU.
* When writing to the SSBO we need to be careful, other threads may write in parallel to the same memory and other threads may need to read the memory we are writing to.
* Divide the buffer access to be exclusive by thread, an example: Buffer has an array, each thread only write to array[threadID]
* If we need to write to the same variable, we can use atomic operations
Atomic Operations: invisible operation preventing interference - avoids race conditions when doing parallel operations.
What is GLSL?
- GLSL is OpenGL Shading Language. The language is high level and is similar to C.
- This language is targeted to vector and matrix manipulation.
- Each point for each shader should start with void main()
- There are no pointers in GLSL but other data types exists like Scalars, Vectors, Matrices and Samplers.
- There are also Variable qualifiers such as const, uniform, in, out, layout or in the context of fragment shader: smooth, flat and noperspective.