OpenGL es Shader language variables and data types (i) (Official document fourth)
all variables and functions must be declared before they are used. Variables and function names are identifiers .
Without a default type, all variables and function declarations must contain a claim type and an optional modifier. Variables are declared in the first place to indicate the type, followed by a number of variables, separated by commas. In many cases, a variable can be initialized with an equal sign "=" when declaring it.
User-defined types can use structs, and all variable types in the struct must be keywords defined by the OpenGL ES shader language. OpenGL es Shading languages are type-safe and therefore do not support implicit type conversions.
4.1 Basic data types
4.1.1 Void
The function does not have a return value that must be declared void, and there is no default function return value. The keyword void cannot be used for other declarations, except for an empty parameter list.
4.1.2 Booleans
A Boolean value that has only two values of true or false.
BOOL success, done = false;
4.1.3 integers
The integral type is primarily used as a programming aid role. At the hardware level, real integers help to effectively implement loops and array indexes, and texture cell indexes. However, it is not necessary to map the integer number to the hardware level integer in the shading language. We do not agree that the underlying hardware adequately supports a wide range of integer operations. OpenGL es coloring language converts integers into floating-point numbers for operation. Integers can be in decimal (not 0), octal (0 starting array), and hexadecimal (beginning with 0x digits).
int I, j = 42;
4.1.4 Floats
Floating-point types are used for a wide range of scalar computations. You can define a floating-point number as follows:
float A, b = 1.5;
4.1.5 Vectors
OpenGL es coloring language contains generic representations like 2-,3-, 4-floating-point, Integer, and Booleans-type vectors. floating-point vectors can hold a variety of useful graphical data, such as color, position, and texture coordinates.
VEC2 TexCoord1, TexCoord2;
VEC3 position;
VEC4 Rgba;
IVEC2 Texturelookup;
BVEC3 LessThan;
The initialization of the vector can be done in the constructor.
4.1.6 matrices
The matrix is another very useful data type in computer graphics, OpenGL es coloring language supports 2*2, 3*3, 4*4 floating point matrix.
MAT2 mat2d;
MAT3 Optmatrix;
MAT4 view, projection;
The initialization of the matrix can be done in the constructor.
4.1.7 Sampler
The sampler type (such as sampler2d) is actually an opaque handle to the texture. They use the built-in texture function to indicate which texture to access. They can only be declared as function arguments or uniforms. In addition to texture lookup function parameters, array indexes, struct field selection, and parentheses, the sampler is not allowed to appear in an expression. The sampler cannot be an lvalue, nor can it be an out or inout parameter of a function. These restrictions also apply to structs that contain samplers. As uniforms, they are initialized via the OpenGL ES API. As a function parameter, only the matching sampler type can be passed in. This allows for a consistency check of shader texture access and OpenGL ES texture states before the shader runs.
4.1.8 Structures
Users of the struct can create their own data types.
struct light{
float intensity;
VEC3 position;
}lightvar;
Structs do not support internal anonymous struct objects, nor do they support internally embedded structures, but can declare variables for another struct.
4.1.9 Arrays
Variables of the same type can be stored and managed in an array. The array length must be a constant integer greater than 0. It is not legal to index an array with negative numbers or an index value greater than or equal to the array degree. An array must also indicate the array length as a function parameter. Only one-dimensional arrays are supported, and both the base data type and the struct type are available as array elements.
float frequencies[3];
Uniform VEC4 lightposition[4];
const int numlights = 2;
Light Lights[bumlights];
You cannot initialize an array while it is being declared in a shader.
4.2 Scopes
The scope of the declaration determines the visibility of the variable. GLSL es uses a static nesting range that allows a variable to be redefined within the shader.
Definition of 4.2.1 terminology
The term scope describes a specific area of the program that is visible when a variable is defined in the region.
4.2.2 Range Type
4.2.3 re-declaring variables
In a compilation unit, a variable with the same name cannot be re-declared in the same scope. A variable of the same name can be declared in a different scope, but there is no way to access a variable of the same name in the outer range.
4.2.4 Sharing Global Variables
A shared global variable refers to a variable that can be accessed in multiple compilation units. Only uniform variables in GLSL es can be used as global shared variables. Varying variables cannot be used as global shared variables because they must be passed through rasterization before the slice shader can read them.
Shared global variables must have the same name, storage, and precision modifiers. They must have the same equivalent rule as the following: must have the same precision, basic type and size. A scalar must have the same type name and type definition, and the field name must be of the same type.
4.3 Store modifiers
Local variables can only use the store modifier const.
Function arguments can only be used with Const. function return value type and struct field do not use Const.
There is no data type communication between a run-time shader and the next runtime shader. This prevents the same shader from executing at the same time between multiple vertices and slices.
Global variables that do not have a store modifier or just use the const modifier may be initialized before main () is executed. Uniforms, attributes, and varyings may not have an initializer.
4.3.1 default store modifier
If there are no modifiers before the global variables, they are not associated with the shader on the application and other processors. For global or local non-modifier variables, the declaration allocates memory on the processor to which it belongs. This variable will provide read and write access to the allocated memory.
4.3.2 Constant modifier
Named compile-time constants can be declared with a const. Any variable declared with a const is read-only in the shader to which it belongs. Declaring a variable as a constant reduces the number constants that use hard lines. Const can be used to modify any basic data type. Typically, a const variable is initialized at the same time it is declared:
Const VEC3 Zaxis = VEC3 (0.0, 0.0, 1.0);
struct fields cannot use const adornments, but variables can be initialized by constructors. Arrays and structs that contain arrays cannot be declared as constants because they cannot be initialized.
4.3.3 Attribute
the attribute modifier is used to declare the value of a variable passed to the vertex shader through an OpenGL ES application . It is wrong to declare the attribute variable in any other shader that is not a vertex shader. The attribute variable is read-only before the vertex shader is used by the program. The value of the attribute variable is passed into the vertex shader through the OpenGL ES Vertex API or as part of the vertex array. They pass vertex property values to the vertex shader and change in each running vertex shader. The attribute modifier can only be modified by float, VEC2, VEC3, Vec4,mat2,mat3,mat4. The attribute variable cannot be declared as an array or struct. Such as:
attribute VEC4 position;
attribute VEC3 normal;
Attribute VEC2 Texcoord;
You might want the graphics hardware to have a very small number of fixed positions to pass the vertex properties. So, OpenGL es gives each non-matrix variable a space to upgrade to 4 floating-point values, such as VEC4. In OpenGL ES, there is a limit to the number of attribute variables that can be used, and if this limit is exceeded, a link error will occur. (attribute variables declared but not used are not subject to this limitation.) A floating-point attribute is also subject to this limitation, so you should try to package four unrelated float variables into a pack to optimize the compatibility of the underlying hardware. One mat4 and using 4 VEC4 variables is consistent, in the same vein, a MAT3 and using 3 VEC3 variables is consistent, one MAT2 and 2 vec2 variables are consistent. The shading language and APIs hide how these spaces are used by matrices. Attribute variables need to be declared as global variables.
4.3.4 Uniform
The uniform modifier is used to modify global variables that remain constant throughout the process of the entity being processed. all of the uniform variables are read-only and can be initialized via the application Invoke API command, or indirectly via OpenGL ES.
Uniform VEC4 lightposition;
The uniform modifier can be used with any basic data type, or contains an array of primitive data type elements and struct bodies. There is a limit to the number of uniform variables for each type of shader, and if this limit is exceeded, it will cause compilation or link-time errors. Uniform variables that are declared but not statically used are not subject to this limitation. Static use refers to a shader that contains a reference to a variable after preprocessing. User-defined uniform variables and built-in uniform variables that are statically used in shaders will collectively determine whether there is more than the available uniform storage range.
When a vertex shader and a slice shader are linked together, they share the same namespace. This means that all variables with the same name in the shader that are connected to the same executable program must also have the same type and precision.
4.3.4 Varying
The varying variable provides the interface between the vertex shader, the slice shader, and the communication control module . The vertex shader calculates the values of each vertex (such as color, texture coordinates, and so on) and writes them to the varying variable. The vertex shader also reads the value from the varying variable, getting and writing the same value. If you read a varying variable from the vertex shader that has not yet been written, the undefined value is returned.
By definition, the varying variable for each vertex is inserted into the entity being rendered as a perspective correction. If it is a single sample, the interpolation is the element center. In the case of multi-sampling, the interpolation can be anywhere in the pixel, including the element center or one of the slice elements sampled.
The slice shader reads the value of the varying variable, and the value being read is used as the interpolator as a function of the element's position in the primitive. The varying variable is read-only for the element shader.
The type of the varying variable with the same name declared in both the vertex and the element shader must match, or it will cause a link error.
The following table summarizes the rules for vertex and slice shader matching:
The term "static use" means that after preprocessing, the shader contains at least one statement that accesses the varying variable, even if the statement is not actually executed.
Varying vec3 normal;
The varying modifier can only be used on a float, vec2, VEC3, VEC4, Mat2, MAT3, MAT4, and an array containing these types of elements, not for decorating the struct.
The varying variable needs to be declared as a global variable.
4.4 Parameter modifiers
There are several function parameter modifiers:
(1) <none:default>, by default, is in
(2) in, as an incoming parameter to a function
(3) out, as the outgoing parameter of the function
(4) InOut, that is, as an incoming parameter, and as an outgoing parameter
Varying,uniform,attribute modification range of OpenGL es coloring language
Varying
Modifiers can only be used on floats, VEC2, VEC3, VEC4, Mat2, MAT3, MAT4, and arrays containing these types of elements, and cannot be used to decorate a struct.
Uniform
Modifiers can be used with any basic data type, or contain arrays and structs of elements of the base data type.
Attribute
Modifiers can only be modified by float, VEC2, VEC3, Vec4,mat2,mat3,mat4. The attribute variable cannot be declared as an array or struct.
OpenGL es shader language variables and data types (i) (Official document fourth) and Varying,uniform,attribute modification scope