ArticleDirectory
- 16.4.1 keywords
- 16.4.2 basic procedure process
- 16.4.3 forced conversion (casting)
16.4 keywords, statements, and mandatory conversions 16.4.1 keywords
For reference, a list of keywords defined by HLSL is provided here:
ASM bool compile const Decl do
Double else extern false float
Half if in inline inout int
Matrix out pass pixelshader return Sampler
Shared static string struct technique texture
True typedef uniform vector vertexshader void
Volatile while
The following collection shows the reserved and unused identifiers that may become keywords in the future:
Auto break case catch char class
Const_cast continue default Delete dynamic cast Enum
Explicit friend goto long mutable namespace
New operator private protected public register
Reinterpret_cast short signed sizeof static_cast Switch
Template this throw try typename Union
Unsigned using virtual
16.4.2 basic Program Process
HLSL supports many similar selection, repetition, and general program flow statements as C ++. The syntax of these statements is very similar to that of C ++.
Return Statement:
Return (expression );
If And if... Else statement:
If (condition)
{
Statement (s );
}
If (condition)
{
Statement (s );
}
Else
{
Statement (s );
}
For statement:
For (initial; condition; increment)
{
Statement (s );
}
While statement:
While (condition)
{
Statement (s );
}
Do... While statement:
Do
{
Statement (s );
} While (condition );
16.4.3 forced conversion (casting)
HLSL supports a very free forced conversion design. The syntax for force conversion in HLSL is the same as that in C programming language. For example, to convert float to matrix, we write:
Float F = 5.0f;
Matrix M = (matrix) F;
16.5 Operator
HLSL supports many operators similar to C ++. Except for a few of the following annotations, their usage is exactly the same as that in C ++. The following table lists the HLSL operators:
[] |
|
> |
|
<= |
= |
! = |
= |
! |
&& |
|
? : |
+ |
+ = |
- |
-= |
* |
* = |
/ |
/= |
% |
% = |
++ |
-- |
= |
() |
' |
|
|
|
Although the operator behavior is similar to that of C ++, there are also some differences. First, the modulo % operator applies to both integer and floating point types. To use the modulo operator, the value on the left and the value on the right must have the same positive and negative signs (for example, the values on the left and right must be both positive and negative ).
Second, note that the HLSL operation is based on each component. In fact, vectors and matrices are built in languages and these types are composed of several components. By applying these operations to the component level, we can perform operations such as addition, subtraction, and Equality testing of vectors/matrices just like using numerical values (), as shown in the following example:
Note: The operator behavior is the same as that for numeric operations (that is, in general C ++ mode ).
Vector u = {1.0f, 0.0f,-3.0f, 1.0f };
Vector v = {-4.0f, 2.0f, 1.0f, 0.0f };
// Adds corresponding components
Vector Sum = u + V; // sum = (-3.0f, 2.0f,-2.0f, 1.0f)
An incremental vector is to increment each of its components:
// Before increment: Sum = (-3.0f, 2.0f,-2.0f, 1.0f)
Sum ++; // After increment: Sum = (-2.0f, 3.0f,-1.0f, 2.0f)
Vector multiplication is also based on components:
Vector u = {1.0f, 0.0f,-3.0f, 1.0f };
Vector v = {-4.0f, 2.0f, 1.0f, 0.0f };
// Multiply corresponding components
Vector Sum = u * V; // Product = (-4.0f, 0.0f,-3.0f, 0.0f)
The comparison operation is performed by component and returns a vector or array with each component of the bool type. The "bool" vector as the result contains the comparison results of each component. For example:
Vector u = {1.0f, 0.0f,-3.0f, 1.0f };
Vector v = {-4.0f, 0.0f, 1.0f, 1.0f };
Vector B = (u = V); // B = (false, true, false, true)
Finally, we will end with the discussion of variable promotion for binary operations:
For a binary operation, if the left and right dimensions of the operator are different, the side with fewer dimensions will be upgraded (forced conversion) to the same dimension as the side with larger dimensions. For example, if the X type is float and the Y type is float3, the variable X is promoted to float3 in the expression (x + y, the calculated expression value also belongs to float3. Upgrade with the defined conversion. Note: If the conversion is undefined, the upgrade is also undefined. For example, we cannot convert float2 to float3 because this conversion is not defined.
For binary operations, if the left and right types are different, the lower Type resolution is promoted (forced conversion) to the high-precision type with the same type (the higher type resolution ). For example, if the X type is int and the Y type is half, the variable X in the expression (X + Y) is promoted to half, the Value Type of the calculated expression is half.
16.6 User-Defined Functions
Functions in HLSL have the following attributes:
Function syntax similar to C ++
Parameters are always passed by value
Recursion is not supported
The function is always inline.
In addition, some additional keywords are added to the function. For example, consider the following function written in HLSL:
Bool Foo (in const bool B, // input bool Out int R1, // output int Inout float R2) // input/output float { If (B) // test input value { R1 = 5; // output a value through r1 } Else { R1 = 1; // output a value through r1 } // Since R2 is inout we can use it as an input // Value and also output a value through it R2 = R2 * R2 * R2; Return true; } |
Functions are almost the same as C ++ functions, except for the In, out, And inout keywords:
In -- indicates that the final parameter (argument, a variable passed to the real parameter) should be copied to the real parameter before the function starts. You do not need to specify the input parameter because the real parameter is in by default. For example, the following two sections are equivalent:
Float square (in float X) { Return x * X; } |
You can also choose not to forcibly specify in:
Float square (float X) { Return x * X; } |
Out -- specifies that the real parameter should be copied to the type parameter when the function returns. In this way, you can return values through parameters. The out keyword is required because HLSL cannot pass a reference or a pointer. Note: If the real parameter is marked as out, the type parameter is not copied to the real parameter before the function starts. In other words, the out real parameter can only be used for output data-it cannot be used for input.
Void square (in float X, out float y) { Y = x * X; } |
Here, we enter the number X to be multiplied, and return the multiplication of X through parameter Y.
Inout -- this is a shortcut that indicates the real parameters used for both input and output. If you want to use real parameters for both input and output, specify inout.
Void square (inout float X) { X = x * X; } |
Here, we enter the number X to be multiplied, and return the multiplication of X through X.