Qualifiers for function arguments:

Function arguments are passed by default in the form of copy, that is, value passing. Any variable passed to a function argument is copied for its value and then passed inside the function for processing. Therefore, you can add qualifiers to parameters to pass references.

qualifiers instructions
none:default The in qualifier is used by default
in/centroid in Copy it to a function and read it and write it to it. The default qualifier, which actually passes in a function parameter, is a copy of the argument. In a function, modifying the in parameter does not affect the argument itself. The centroid in variable is related to the interpolation type
out/centroid out It is copied from the function when it returns. Its function is to pass properties outside the function. In out mode, the parameters passed in are write-only. In a function, modifying out parameters affects the arguments themselves. The Centroid out variable is related to the interpolation type
ninout Copy it into the function and copy it out when the bin returns. Readable and writable. In a function, modifying the shape of the inout modifier affects the argument itself

GLSL functions:

GLSL allows functions to be declared on the outermost side of a program. Functions cannot be nested, cannot be called recursively, and must declare a return value type (void if there is no return value).

    vec4 getPosition(){
            vec4 v4=vec4(0.0.0.0.0.0.1.0);
            return v4;
    }
    void doubleSize(inout float size){
            size=size*2.0;
    }
    void main(){
        float psize= 10.0;
        doubleSize(psize);
        gl_Position = getPosition();
        gl_PointSize = psize;
    }
Copy the code

Constructor:

GLSL variables can be initialized at declaration time or declared first and assigned as needed. Objects of aggregate type, such as vectors, proofs, arrays, structures, need to be initialized using their constructors.

    // Common type
    float pSize=10.0;
    float pSize1;
    pSize1=10.0;
    // a composite type, such as vector
    vec4 color=vec4(0.0.1.0.0.0.1.0);
    vec4 color1;
    color1=vec4(0.0.1.0.0.0.1.0);
    / / structure
    struct light{
            float intensity;
            vec3 position;
    }
    light lightVar=light(3.0.vec3(1.0.2.0.3.0));
    / / array
    const float c[3] =float[3] (5.0.7.2.1.1);
Copy the code

Precision limit:

GLSL provides three floating-point precision because it generates a lot of floating-point operations when rasterizing colorization, which may be beyond the capabilities of current devices. Add highp, mediump, lowp in front of the variable to declare the accuracy of the variable.

  • Highp (-2 to the 16th, 2 to the 16th);
  • Mediump (-2 to the tenth, 2 to the tenth);
  • Lowp (minus 2 to the eighth, 2 to the eighth);
    lowp float color;
    varying mediump vec2 Coord;
    lowp ivec2 foo(lowp mat3);
    highp mat4 m
Copy the code

Determine accuracy:

The precision of a variable is first determined by the precision qualifier. If there is no precision qualifier, look for the variable in the expression on the right that has a certain precision. Once found, the whole expression will run under that precision. If more than one is found, the higher precision type is selected; if not, the default or higher precision type is used.

    uniform highp float h1;
    highp float h2=2.3*4.7;// Both the process and the result will use high precision
    mediump float m;
    m=3.7*h1*h2;// The operation process is high precision
    h2=m*h1;// The operation process is high precision
    h2=m+m// The operation process and the result are medium precision
    // The parameter p is high precision, and the passed 3.3 is high precision
    void f(highp float p);
    f(3.3);
Copy the code

invariant

Key: Because shaders perform some internal optimizations during compilation, the same operation may not be exactly the same in different shaders, which can cause some problems, especially when vertex Shaders pass values to fragment shaders. So you need to use the invariant keyword to show that the calculation must be exact all the time. It is also possible to use #pragma STDGL invariant(all) to command that all output variables must be exactly the same, but this limits compiler optimization and reduces performance.

#pragma STDGL invariant(all
invariant invariant varying texCoord; // VARYING is declared as invariant when passing data
Copy the code

Order of qualifiers: When more than one qualifier is required, follow the following order: 1. In general variables: invariant > storage > Precision 2. In parameters: Storage > parameter > Precision

Precompiled instruction

Precompiled instructions that begin with # are commonly used: #define #undef #if #ifdef #ifndef #else #elif #endif #error #pragma #extension #version #line 100, specifies that the current shader is compiled using the GLSL ES 1.00 standard. If this directive is used, it must appear at the beginning of the program.

Built-in macro: LINE: LINE number in current source code. VERSION: an integer indicating the current GLSL VERSION. GL_ES: set GL_ES to 1 if the current system is running in OpenGL ES. GL_FRAGMENT_PRECISION_HIGH: set GL_ES to 1 if the current system GLSL chip shader supports high floating-point precision. Generally used to check the accuracy of shaders.

Example: 1. How to select the appropriate precision by judging the system environment:

    #ifdef GL_ES //
    #ifdef GL_FRAGMENT_PRECISION_HIGH
    precision highp float;
    #else
    precision mediump float;
    #endif
    #endif
Copy the code

2. Custom macros:

    #define NUM 100 
    #if NUM==100 
    #endif
Copy the code

Built-in special variables

GLSL programs use special built-in variables to communicate with the hardware. There are roughly two types, the input type, which sends data to the hardware (rendering pipeline), and the output type, which sends data back to the program for programming purposes. In vertex shader: built-in variable of type output

variable instructions unit
highp vec4 gl_Position Gl_Position places vertex coordinate information vec4
mediump float gl_PointSize Gl_PointSize Specifies the size of the point to draw (only valid in GL.points mode) float

Fragment shader: Input type:

variable instructions unit
mediump vec4 gl_FragCoord The relative positions of the elements in the framebuffer vec4
bool gl_FrontFaceing Flags whether the current icon is part of the front icon bool
mediump vec2 gl_PointCoord The texture coordinates after interpolation are in the range of 0.0 to 1.0 vec2

The output type:

variable instructions unit
mediump vec4 gl_FragColor Sets the color of the current slice vec4 RGBA color
mediump vec4 gl_FragData[n] Sets the color of the current slice, using the glDrawBuffers data array vec4 RGBA color