Variables and data types for OpenGL es shader Language (ii) (Official document, chapter fourth)

Source: Internet
Author: User
Tags modifiers

Variables and data types for OpenGL es shader Language (ii) (Official document, chapter fourth)

4.5 Precision and precision modifiers 4.5.1 range and precision

The sources used to store and present floating-point numbers, the range of integer variables, and precision-dependent values (varying,uniform, texture lookups, and so on), are not vertex or slice shaders, and other details of the underlying implementation. Minimum storage needs to be declared with precision modifiers. Typically, precision operations must preserve the precision storage that the variable contains. The only exception is built-in functions that require a large number of complex computations, such as Atan (), and the accuracy of the return value is less than the declared precision.

It is strongly recommended that the vertex language provide a floating-point range and precision that matches the IEEE single-precision floating-point number or higher-precision floating-point number. This requires that the vertex language provide a range of floating-point variables at least ( -2^62, 2^62) with a precision of at least 65536.

The vertex language must provide an integer precision of at least 16 bits, plus a sign bit.

It is necessary, but not necessary, to provide the same floating-point range and precision as the vertex shader in the chip-meta language. This requires that the range of floating-point numbers provided by the chip-language is at least ( -16384,+16384), with a precision of at least 1024.

The element language must provide an integer precision of at least 10, plus a sign bit.

4.5.2 Precision Modifier

Any floating-point number or integer declaration can be preceded by the following precision modifiers:

Example:

LOWP float color;varying mediump vec2 coord;lowp ivec2 foo (lowp mat3); highp mat4 m;

The precision modifier declares the minimum range and precision that the underlying implementation must use to store these variables. Implementations may use a greater range and precision than required, but never less than the requirements.

The minimum range and precision required by the precision modifier:

Floating point Magnitude Range is a non-0 magnitude range. For floating point precision,relative, the precision of the value of any measure is relative to this value. For all levels of accuracy, 0 must be accurately represented. Any implementation that cannot provide the precision declared by the shader storage variable will cause a compile or link error.

For high-precision and intermediate-precision, the integral range must be accurately converted to the float type represented by the same precision modifier. In this case, HIGHP int can be converted to HIGHP float, Mediump int can be converted to mediump float, but lowp int cannot be converted to the corresponding LOWP float.

The vertex language requires compiling and linking any lowp, and mediump and HIGHP applications cannot have errors.

The chip language requires that any LOWP be compiled and linked, and mediump applications cannot have errors. However, HIGHP support is optional.

Character constants and Booleans do not have a precision modifier. The precision modifier is not required when floating-point numbers and integer constructors do not contain parameters with precision modifiers.

In this document, the operation contains operators, built-in functions and constructors, and operands contain function arguments and constructor parameters.

For constant expressions or sub-expressions that are not defined by precision, the evaluated precision result is the highest precision in all operands (Mediump or HIGHP). Constant expressions with evaluation must be fixed and made at compile time.

In addition, for operands that do not have a precision modifier, the precision is derived from the other operands. If none of the operands have any precision, then look at other expressions that use the result of the calculation. This operation is recursive until a precision operator is found. If necessary, this operation also contains the lvalue of the assignment operation, initializes the declared variable, the function parameter, and the function return value. If this still does not determine the accuracy, if all the operands that make up the expression have no precision, and if the result is not assigned, it is not passed into the function as a parameter. Then the default or larger type will be used. When this happens in the slice shader, the default precision must be defined.

Like what:

Uniform HIGHP float H1;

HIGHP float h2 = 2.3*4.7; operation and results are high precision

Mediump float m;

m = 3.7*h1*h2;//All operations are high precision

H2 = m * h1;//operation is high precision

m = h2-h1;//operation is high precision

H2 = m + m;//addition and results are mediump precision

void f (HIGHP p);

F (3.3);//3.3 will pass into the function as a high-precision value

4.5.3 default Precision modifier

Precision precision-qualifier type;

The precision can be used to determine the default precision modifier. type can be an int or float or sampler type,precision-qualifier can be lowp, mediump, or HIGHP. Any other type and modifier can cause an error. If type is float, then the precision (precision-qualifier) will apply to all floating-point number declarations (scalars, vectors, matrices) with no precision modifiers. If type is an int, the precision (precision-qualifier) will apply to all integer declarations (scalars, vectors) of all non-precision modifiers. This includes global variable declarations, function return value declarations, function parameter declarations, and local variable declarations. A variable that does not declare the precision modifier will use the precision in its nearest precision statement.

The following predefined global default precision statements are available in the vertex language:

Precision HIGHP float;

Precision HIGHP int;

Precision LOWP sampler2d;

Precision LOWP Samplercube;

There are the following predefined global default precision statements in the slice language:

Precision Mediump int;

Precision LOWP sampler2d;

Precision LOWP Samplercube;

The slice language does not have a default floating-point precision modifier. Therefore, for floating-point numbers, floating-point vector and matrix variable declarations, either the declaration must contain a precision modifier, or the default precision modifier was previously declared.

4.5.4 Available Precision Modifiers

Nechanhong Gl_fragment_precision_high is defined in a chip language that supports HIGHP precision, but is undefined in unsupported systems. Once defined, it can be used in both vertex and slice languages.

#defien Gl_fragment_precision_high 1;

4.6 Variants and invariant modifiers

In this section, mutation refers to the possibility of returning different values in the same statement in different shaders. For example, two vertex shaders use the same expression to set gl_position, and the value of the Shishun expression is the same when the shader executes it. It is entirely possible, Because of the two shader-independent compilation environments, the values assigned to Gl_position when the shader is run are not necessarily the same. In this example, the geometry alignment problem of the multi-path algorithm is caused.

Typically, this variation between shaders is allowed. If you want to avoid this mutation, variables can be declared using invariant.

4.6.1invariant modifier

to ensure that a particular output variable is immutable, you can use the invariant modifier. It can modify variables that have been previously defined, such as:

Invariant gl_position;

It can also be used in the declaration of a variable:

Invariant varying mediump vec3 Color;

Only the following variables can be declared as invariant:

(1) Built-in specific output variables in the vertex shader

(2) Output varying variable in vertex shader

(3) Specific input variables in the slice shader

(4) Input varying variable in the chip shader

(5) Built-in output variables in the chip shader

Invariant can also be followed by a comma-delimited list of previously declared identifiers.

To ensure that a specific output variable in two shaders does not mutate. The following rules should also be followed:

(1) The output variables in vertex and slice shaders are declared as invariant

(2) The same value must be entered into the expression assigned to the output variable or all shader input variables of the control flow.

(3) Any texture function call on the output variable needs to be set in the same way when using texture formatting, texture pixel values, and texture filtering.

(4) All input variables are operated in the same way.

Initially, all of the default output variables are allowed to mutate. If you want to force all the output variables to be immutable, use them before all the variable declarations of the shader

#pragma stdgl invariant (all)

4.6.2 non-variants in shaders

When a value is stored in a variable, we usually assume that it is a constant unless it is shown to change its value. However, during the optimization process, the compilation period may recalculate a value instead of saving it to a register. Because the precision of the operation is not fully specified (for example, low-precision operations are converted to medium-precision or high-precision) , the recalculated value is likely to be inconsistent with the original value.

variants are allowed in shaders. If you want to avoid variations, you can use the invariant modifier or the invariant pragma.

Precision Mediump;

Vec4 Col;

VEC2 a = ...;

........

Col = Texture2d (Tex, a);//At this point the value of a is assumed to be A1

..............

Col = Texture2d (Tex, a);//At this point the value of a is assumed to be A2, but it is possible A1 is not equal to A2

If you force a constant, you can use:

#pragma stdgl invariant (all)

Example two:

VEC2 m = ...;

VEC2 n = ...;

Vec2 a = m + N;

Vec2 B = m + n;//no guarantee that A and B are exactly equal

4.6.3 non-variant of constant expression

Constant expressions must be guaranteed to be non-variant. A particular expression must have the same result in the same or a different shader. This includes the same expression appearing in the same vertex and element shader, or in different vertices and slice shaders.

A constant expression must derive the same value if the following conditions are true:

(1) The input value of the expression is the same

(2) Perform the same operation and the same order

(3) All operations are performed with the same accuracy

4.6.4 non-variant and linked devices

The non-variant varying variables declared in vertex and element shaders must match. For built-in specific variables, Gl_fragcoord can be declared as invariant only if the gl_position is declared as invariant. Similarly, when and only if GL When _positionsize is declared as invariant, Gl_pointcoord can be declared as invariant. Declaring gl_frontfacing as invariant is an error. Gl_ The non-variant of frontfacing and the non-variant of gl_position are the same.

4.7 Modifier Order

When you need to use multiple adornments, they must follow a strict order:

(1) invariant-qualifier storage-qualifier precision-qualifier

(2) storage-qualifier parameter-qualifier precision-qualifier

Variables and data types for OpenGL es shader Language (ii) (Official document, chapter fourth)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.