Moment For Technology

OpenGL-ES-GLSL - custom shader common API

Posted on Oct. 20, 2023, 1:31 p.m. by Arnav Chanda
Category: ios Tag: OpenGL

EGL(Embedded Graphics Library)

  • Required by the OpenGL ES commandRender contextandDrawing on the surface ofTo complete the graphics image drawing
  • Render context: Stores the relevant OpenGL ES state and is a state machine
  • Draw surface: The surface used to draw primions. You need to specify the cache area for rendering, such as color buffer, depth, and template
  • The OpenGL ES API does not provide information on how to create a rendering context or how a context is connected to a native windowed system. EGL is the interface between Khronos rendering apis (such as OpenGL ES) and the native window system.The only platform that supports OpenGL ES but does not support EGL is iOS. Apple provides its own iOS implementation of the EGL API, called EAGL
  • Because each window-system has a different definition, EGL provides a basic opaque type, EGLDisplay, which encapsulates all system dependencies and is used to interface with native window-systems

The main function

  • Talk to native Windowing systems
  • Query available configurations
  • Create "Drawing surfaces" available in OpenGL ES
  • Synchronize rendering between different classes of apis, such as between OpenGL ES and OpenVG, or between OpenGL and local window drawing commands
  • Manage "Rendering resources", such as Rendering map.

GLSL language

  • GLSL compilation and concatenation of vertex/slice shaders is not supported in Xcode, so you need to create two empty files in your project namedshader.vshandshaderv.fsh
    • useVSH, FSHThe reason for the suffix is to distinguish between shaders, which is essentially a string
    • Can I just use NSString? This is not recommended because the code structure is not clear and easy to read
    • Can Chinese annotations be added to these two files? It is not recommended to add Chinese annotations, which will lead to strange errors. Since GLSL is written in Xcode, it is completely handwritten without any hint, so it is not easy to troubleshoot problems

Here is a summary of some of the types and apis

Vector data type

Vec2, VEC3, and VEC4 are commonly used, and the default is floating point

type describe
vec2,vec3,vec4 2-component, 3-component, 4-component floating point vectors
ivec2,ivec3,ivec4 2-component, 3-component, 4-component integral vector
uvec2,uvec3,uvec4 2-component, 3-component, 4-component unsigned integer vector
bvec2,bvec3,bvec4 2-component, 3-component, 4-component bool vectors

Matrix data type

The most common ones are MAT3 and MAT4

Type (MAT column × row) describe
mat2,mat2x2 Two lines of two columns
mat3,mat3x3 Three lines of three columns
mat4,mat4x4 Four rows and four columns
mat2x3 Three lines of two columns
mat2x4 Four lines of two columns
mat3x2 Two rows of three columns
mat3x4 Four lines of three columns
mat4x2 Two lines of four columns
mat4x3 Three lines of four columns

Variables store qualifiers

Varying, attribute, and Uniform are commonly used

  • Varying modifier: When data from the vertex shader is passed to the chip shader, it is required to modify the identical texture coordinate variables of the two shaders
  • Attribute: Data can only be passed from the client to the vertex shader and can only be used in the vertex shader
    • Modified data: vertices, textures, colors, normals, etc
    • API normally withglVertex...Beginning, for exampleglVertexAttribPointer
    • The texture coordinates need to be passed indirectly to the slice shader by the vertex shader. The texture coordinates need to be defined exactly the same in the vertex and slice shader. The texture coordinate data is passed indirectly to the slice shader through this variable.varying lowp vec2 varyTextCoord;
    • The vertex results computed by the vertex shader need to be assigned to GLSL's built-in variablesgl_Position
attribute vec4 position;
attribute vec2 textCoordinate;
varying lowp vec2 varyTextCoord;

void main()
    varyTextCoord = textCoordinate;
    gl_Position = position;

Copy the code
  • Uniform: Variables passed from APP code to vertex, fragment
    • Uniform is a constant in Vertex, fragment
    • Uniform Passable data: view matrix, projection matrix, projection view matrix
    • API normally withglUniform...At the beginning
    • The final color in the pixel shader, that is, the grain elements corresponding to the texture coordinates. The stripper is the color value of the texture's corresponding pixels, which needs to be used by the built-in functionTexture2D (Texture, texture coordinates)Calculates and assigns the final returned color value to the built-in variablegl_FragColor
Precsion highp float; // The texture coordinates must be exactly the same as those in the vertex shader. // Texture Uniform sampler2D colorMap; Void main(){//1. Lowp vec4 temp = texture2D(colorMap, varyTextCoord); //2, very important and necessary built-in variable: gl_FragColor gl_FragColor = temp; }Copy the code
qualifiers describe
Just ordinary local variables, external invisible, external inaccessible
const A compiler constant, or an argument that is read-only to a function
in/varying Variables passed in from the previous phase
in/varying centroid A variable passed from the previous stage, using centroid interpolation
out/attribute Pass to the next processing phase or specify a return value in a function
out/attribute centroid Pass on to the next stage of processing, centroid interpolation
uniform A variable passed from client code, unchanged between vertices

OpenGL ES error handling

If the OpenGL ES command is used incorrectly, the application generates an error code, which is logged, and can be queried with glGetError. Once the error code is queried, the current error code is reset to GL_NO_ERROR

The error code describe
GL_NO_ERROR No errors have been generated since the last call to glGetError
GL_INVALID_ENUM GLenum parameter out of range, ignored generating error command
GL_INVALID_VALUE The numeric parameter is out of range and the generated error command is ignored
GL_INVALID_OPERATION The specified command cannot be executed in the current OpenGL ES state
GL_OUT_OF_MEMORY This command is executed when out of memory. If this error is encountered, the state of the OpenGL ES pipeline is considered undefined unless the current error code is encountered

OpenGL ES common API for custom shaders

Custom shaders

Custom shaders generally have the following steps:

  • Create vertex shader/slice shaderglCreateShader
  • Specify shader's source --glShaderSource
  • Compile the shader,glCompileShader

Here are the apis for creating and compiling a shader

Custom program

A custom program generally has the following steps:

  • Create a program objectglCreateProgram
  • Shader attached to program --glAttachShader
  • Link program --glLinkProgram
  • Use program --glUseProgram

Here are the apis for creating linked programs

Shader and program compiler link

  • You need to create two basic objects to infect with a shader: a shader object and a program object
  • The general compilation linking of the shader object after obtaining the link is divided into 6 steps:
    • Create a vertex shader object and a slice shader object
    • Link the source code to each shader object
    • Compile the shader object
    • Create a program object
    • Connects the compiled shader object to the program object
    • Linker object
About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.