Shadder and Effect - 1.6 User Definition Function

zhaozj2021-02-16  52

1.6 User Defined Function

Read this article shows that you have agreed to the statement

The functions in HLSL have the following properties:

n function uses a syntax similar to C

n parameter always passes value

N recursion is not supported

n function is always inline

In addition, the function also adds some additional keywords therebet. For example, consider a 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;

}

The function is almost the same as the C function, except for IN, OUT and INOUT keywords:

n in - Specify type (Argument, variables passing to the argument) should be copied to the arguments before the function begins. The incoming parameters do not have to force the specified because the argument is IN. For example, the following is equivalent:

Float Square (in float x)

{

Return x * x;

}

You can also not force the specified in:

Float Square (Float X)

{

Return x * x;

}

n OUT - Specifies that the actor should be copied when the function returns. This can be returned by the parameter. The OUT keyword is required because HLSL does not allow a reference or a pointer to be delivered. We must pay attention: If the reference is OUT, the type is not copied to the actual parameters before the function begins. In other words, OUT can only be used to output data - it cannot be used for input.

Void Square (In Float X, Out Float Y)

{

Y = x * x;

}

Here, we entered the number x to be passed, and returned the multiplier of X by the parameter y.

n inout - This is a shortcut method indicating the internally for inputting and for output. If you want to use the actual parametery for input and output, you specify inout.

Void Square (Inout Float X)

{

x = x * x;

}

Here, we entered the number x to be multiplied, and the multiplier returned by X returned.

[Declaration]: Introduction to 3D Game Programming With DirectX 9.0 "in this article, it is limited to the translator level, and it is inevitable that there is a mistake in the text. Welcome all netizen criticism; this article is only used for learning exchange and reference usage, not to use In any form of commercial use; if you need to reprint the author's own and translator's consent, maintain the integrity of the article, indicate the author, translator and source, for the consequences of violating the above terms, the translator Do not bear any responsibility. My email address is raymond_king123@hotmade.com, welcome 3D graphics and games, and friends with a certain graphical programming experience to communicate.

转载请注明原文地址:https://www.9cbs.com/read-23180.html

New Post(0)