Sunday, July 10, 2022

Implementation:Shader programs

Overview
 
The following provide an overview of shader programs and their implementation in this tutorial.

Shaders
Shaders are small, specialized programs that run in parallel on the multiple GPU cores to calculate rendering effects, transforming input data (vertices) into output images (pixels). Primarily written in languages like GLSL, they dictate lighting, textures, and geometry, enabling real-time 3D graphics.
Shaders provide the code for certain programmable stages of the rendering pipeline.
The OpenGL rendering pipeline defines the following shader stages, with their enumerator name:
Vertex Shaders( GL_VERTEX_SHADER)
Geometry Shaders (GL_GEOMETRY_SHADER)
Fragment Shaders (GL_FRAGMENT_SHADER)

Program Object
A program object can combine multiple shader stages (built from shader objects) into a single, linked whole. A program pipeline object can combine programs that contain individual shader stages into a whole pipeline.

Uniforms
Data such as vectors, matrices can be shared across the C++ application and shaders using a uniform.
A uniform is a global Shader variable declared with the "uniform" storage qualifier. These act as parameters that the user of a shader program can pass to that program. Their values are stored in a program object.
Uniforms are so named because they do not change from one shader invocation to the next within a particular rendering call thus their value is uniform among all invocations. This makes them unlike shader stage inputs and outputs, which are often different for each invocation of a shader stage.

Details
The shader programs are written in a 'C' like GLSL programming language. They support declaring input and output parameters. They also support different data types such as int, float, vectors, matrices etc.
uniforms can be generally considered as global variables that application can send down values to the shaders.

vertex shader 
Vertex shaders are used to send vertex,color, normal and texture coordinates to the GPU. The following example demonstrates sending vertex data.  
#version
indicates GLSL language version depends on the ca[abilities of the hardware. Most new hardware support at least 3.3 or higher. It can be obtained from making the following call:
glGetString(GL_SHADING_LANGUAGE_VERSION)

in,out 
Shader programs can receive inputs in the in parameters from the application and pass values back to another shader or application in out parameters.

core 
indicates legacy features are not available.

layout (location=0) 
indicates where to access vertex data from VBO buffers. In this case the Position data is available

uniform
uniforms are used by application to share data such as transformation matrix, color etc.

main
Entry point of the shader program

gl_Position
Built in variable that stores the position of the vertex to be rendered.

Fragment shader 
Fragment shaders are similar to vertex shader. They are executed after the rasterization process. They are responsible for determining the color of the vertex. 


FragColor
is a built-in output variable. It's assigned the color of the vertex from the input parameter fcolor coming in from the vertex shader.

ShaderUtil 
ShaderUtil class loads shader code such as vertex shader and fragment shader , creates program object, attaches shader code and links them to the program object. In addition shaderUtil also stores attribute locations and uniform locations. Errors in compilation are written to the error logs.

Members
NameDescription
program
Unique id generated by OpenGL required for linking vertex shader and fragment shader programs.
shadermap
Contains unique ids generated by OpenGL after compiling shader programs.
attmap
Contains unique ids generated by OpenGL after linking attributes with the program
unimap
Contains unique ids generated by OpenGL after linking uniforms with the program

Methods
NameDescription
Cleanup Destroys the instance of the program
CreateAndLinkProgramCompiles the programs and link it with unique id program
LoadFromFileLoads shader program from a file and compiles it. Generates unique id and stores it in shadermap
LoadFromStringLoads shader program from a string and compiles it. Generates unique id and stores it in shadermap
Use
Uses the program at program to draw.
Release
Unuses the program at program after draw.
GetAttributeLocationAdds or returns unique id for the attribute from attmap
GetUniformLocationAdds or returns unique id for the uniform from unimap





No comments:

Post a Comment