Shader language features:
- Shader language is a high-level graphics editing language and process language.
- Vertex shaders and slice shaders use the same language.
- Based on C/C++ syntax and flow control, but the language does not support double,byte,short,long data types, and does not support union,enum,unsigned, and bit operations.
- Perfect support vector and matrix operations.
- Input and output are managed through type qualifiers.
- Has a large number of built-in functions to provide rich functionality.
Language composition: scalar, vector, matrix, sampler, structure, array, null type
Basic types of
type | instructions |
---|---|
void | An empty type that does not return any value |
bool | Boolean type, true/false (scalar) |
int | Signed integer (scalar) |
float | A signed floating scalar |
vec2,vec3,vec4 | N-component Floating point vector |
bvec2,bvec3,bvec4 | N Boolea vector boolea vector |
ivec2,ivec3,ivec4 | N dimensional integer vector signed integer vector |
mat2,mat3,mat4 | 2 dimensional (2* 2), 3 dimensional (3* 3), 4 dimensional (4*4) float matrix |
sampler2D | A 2D texture |
sampler3D | A 3D texture B 3D texture |
samplerCube | The cube mapped texture |
The old GLSL int range was 16 bits, now 32 bits.
GLSL vectors can be regarded as scalar components of the same type, for example, 2-dimensional vectors are represented as v2(x,y), and 3-dimensional vectors are represented as V3 (x,y,z) vectors. In shader language, they are used to store and manipulate colors, positions, texture coordinates, etc.
The basic syntax for accessing a component individually is < vector name >·< component name >, such as color(r,g,b,a), color.r=0.6.
In GLSL, matrices are organized in column order, and a matrix can be regarded as consisting of several column vectors. For example, MAT3 can be regarded as composed of three VEC3. For matrix access, you can think of a matrix as an array of column vectors. For example, matrix is mat4, access the second column, use matrix[1], whose value is a VEC4, obtain the second component of the third column, use matrix[2][1], whose value is float;
Samplers, such as sampler2D, are used for texture sampling. In general, a sampler variable represents one or a set of texture maps. The sampler variable cannot be initialized in the shader. Sampler variables are usually decorated with a Uniform qualifier to accept values passed into the shader from the host language (such as Java).
Basic structures and arrays
type | instructions |
---|---|
structure | Struct type-name{} C – like structure |
An array of | Float Foo [3] GLSL only supports 1-dimensional arrays, which can be struct members |
Operation between basic types: |
In GLSL, there is no implicit type conversion. In principle, left-value = right-value must be the same type on both sides of the expression. If the type is different, display conversion (using constructor conversion) must be used.
1. Float and int operate directly between the same type, different types need to display conversion.
int a=int(2.0);
float a=float(2) *6.0+2.3;
Copy the code
Vec (vector)/mat(matrix) vec(vector)/mat(matrix) veC (vector)/mat(matrix) veC Most of GLSL involves VEC, and mat’s operations are component-by-component operations, which are linear. For example, the results of VEC and float operations are STILL VEC.
vec3 v=vec3(1.0.2.0.3.0);
mat3 m=mat3(1.0);
float f=10.0;
vec3 a=f*v; / / vec3 (10.0, 20.0, 30.0)
vec3 b=v*f; / / vec3 (10.0, 20.0, 30.0)
mat3 c=f*m;; / / mat3 (10.0)
mat3 d=m*f; / / mat3 (10.0)
Copy the code
Int is unoperable with vec and mat, because each component in vec and mat is of type float and cannot be evaluated with int component by component.
3. The operation between veC and VEC (vector and vector) must ensure the same order of operands, that is, the same dimension, otherwise it cannot be calculated. The calculation method is that the components of the two operands in the same position are operated separately, and the order is unchanged. In essence, it is still carried out component by component.
vec3 a=vec3(1.0.2.0.3.0);
vec3 b=vec3(0.1.0.2.0.3);
vec3 c=a+b; / / vec3 (1.1, 2.2, 3.3)
vec3 d=a*b; / / vec3 (0.1, 0.4, 0.9)
Copy the code
4. Vec and MAT (vector and matrix) should ensure that the order of operands is the same, and only the multiplication operation exists between VEc and MAT, and the result is VEc (vector). The calculation is the same as matrix multiplication in linear algebra.
vec2 v=vec2(10.0.20.0);
mat2 m=mat2(1.0.2.0.3.0.4.0);
vec2 a=m*v; / / vec2 ((1.0 * 10.0 * 20.0 + 3.0), (10.0 + 2.0 * 4.0 * 20.0)) - vec2 (70100).
vec2 b=v*m / / vec2 ((10.0 * 1.0 * 2.0 + 20.0), (3.0 + 10.0 * 20.0 * 4.0)) - vec2 (50110).
Copy the code
Vector and matrix multiplication rule:
Matrix A, B, C; Note: 1. The number of columns of A is equal to the number of rows of B. 2. The number of rows of A is the number of rows of C, and the number of columns of B is the number of columns of C. 3. The MTH row and NTH column of C is the sum of the MTH row of A multiplied by the NTH column of B
4. Mat and MAT (matrix and matrix) must have the same order of operands, and the multiplication operation is linear algebra operation. The rest of the operations are still component by component
mat2 a=mat2(1.0.2.0.3.0.4.0);
mat2 b=mat2(10.0.20.0.30.0.40.0);
mat2 c=a+b;/ / mat2 (11.0, 22.0, 33.0, 44.0);
mat2 d=a*b;/ / mat2 ((1.0 * 10.0 * 20.0 + 3.0), (2.0 * 10.0 * 20.0 + 4.0), (1.0 * 30.0 * 40.0 + 3.0), (30.0 + 2.0 * 4.0 * 40.0)) - mat2 (70.0, 100.0, 150.0, 220.0)
Copy the code
Variable qualifier
The modifier | instructions |
---|---|
none | (default, omitted) a local variable that can be read or written to the function as its input parameter |
const | Declare the arguments of a variable or function as read-only types |
attribute | Can only exist in a Vertex shader, usually used to hold vertex or normal data, which can be read from a data buffer |
uniform | A shader cannot change uniform variables at runtime. It is used to place the transformation matrix passed to the shader by the program, from materials to lighting parameters, and so on |
varying | Is responsible for passing variables between vertex and fragment shaders |
Note that the global variable qualifier can only be one of const, Attribute, UNIFORM, or VARYING. Do not compound