Mandatory type conversion
In order to facilitate the preparation of the shader and the efficiency of the code generated, it is best to be familiar with the mandatory type conversion mechanism of HLSL. Forced type conversion is often used to extend or reduce the selected variable to match the variable to be assigned. For example, in the following example, the FLOAT type constant 0.0F, 0.0F, 0.0F, 0.0F} were converted to Float4 type {0.0F, 0.0F, 0.0F, 0.0F} when the VRESULT was initialized.
FLOAT4 VRESULT = 0.0F;
Similar mandatory conversions occur when a high dimensional data type is assigned to a low dimensional data type, such as a vector or matrix type. In these cases, additional data is omitted. For example, write the following code:
FLOAT3 VLIGHT;
Float ffinal, fcolor;
FFINAL = VLIGHT * FCOLOR;
In this example, just multiplied the float type of scalar Fcolor with the first member in Vlight, thus enforces VLight to float type. FFinal is equal to VLight.x * Fcolor.
It is best to get familiar with Table 4, HLSL's mandatory type conversion rules:
Table 6. Forced type conversion rules for HLSL
Scalar-to-scalar has been valid. When the Boolean is forced to convert to an integer or floating point, FALSE changes to 0, and TRUE becomes 1. When an integer or floating point is forced to convert to a Boolean, 0 changes to false, non-0 changes to TRUE. When the floating-point is forced to convert to an integer type, the value is rounded to 0, which is the same as the same truncation mechanism in the C language. Scalar-to-vector has been valid. This mandatory conversion operation is filled with the vector. Scalar-to-Matrix has been valid. This mandatory conversion operation is filld into a matrix by copying the scalar. Scalar-to-structure This mandatory conversion operation is filld into the structure by copying the scalar. Vector-to-scalar has been valid. Select the first part of the vector. Vector-to-vector target vector must not be greater than the resource vector. This mandatory conversion operation is removed by retaining the leftmost value. The purpose of this is to view the row matrix, column matrix and digital structure. Vector-to-matrix vector size must be equal to the matrix size. The vector-to-structure structure is not more than vectors, and each part of the structural body is valid. Matrix-to-scalar has been valid. The top left part of the matrix is selected. The Matrix-to-Vector matrix size must be equal to the vector size. The Matrix-to-Matrix target matrix is not greater than the source matrix in any one, which is removed by keeping the upper left value. The size of the Matrix-to-Structure structure is equal to the size of the matrix, and all members of the structure are numbers. The Structure-to-Scalar structure must contain at least one digital member Structure-to-Vector structure must be at least the same as the vector size, the first member must be a number, until the size of the vector. (Translator Note: That is, the number of members is the same as vector size) The structure-to-matrix structure must be at least the same as the size of the matrix. The first member must be a number, until the size of the matrix. (Translator Note: That is, the number of members is the same as matrix size) The structure-to-Object structure contains at least one object of an object. The member's type must be identical to the object type. The structure-to-structure target structure must not be greater than the source structure. A valid forced conversion must exist between all corresponding source members and destination members.
Structural body
As the first shader example is displayed on the top, define a structure in the HLSL shader often brings convenience. For example, many shader writers define an output structure in their vertex shader code, using the structure as the return type of their vertex shader main function. (For pixel shaders, it is considered to be because most pixel shaders have only one float4 output.) An example of a structure is as follows, from the NPR Metallic shader, we will discuss the shader behind. (Translator Note: NPR (Non-Photo Reality) is a unique two W-dimensional effect) Struct vs_output
{
FLOAT4 POS: Position;
Float3 View: Texcoord0;
FLOAT3 NORMAL: TEXCOORD1;
FLOAT3 LIGHT1: TEXCOORD2;
FLOAT3 LIGHT2: TEXCOORD3;
FLOAT3 LIGHT3: TEXCOORD4;
}
The structure can also be declared as ordinary use in the HLSL shader. Also follow the mandatory type conversion rules summarized on the upper side.
Sampler
If you want to sample every different texture map in the pixel shader, a sampler must be declared. Then call the HLSL_Rings () in the shader described before,:
Float4 lightwood; // xyz == Light Wood Color
FLOAT4 DARKWOOD; // XYZ == Dark Wood Color
Float Ringfreq; // Ring Frequency
Sampler PulseTrainsample;
FLOAT4 HLSL_RINGS (FLOAT4 PSHADE: TEXCOORD0): Color
{
Float scaleddistfromzaxis = sqrt (dot (pshade.xy, pshade.xy) * RingFReq;
Float BlendFactor = TEX1D (Pulsetrainsample, ScaledDistFromzaxis);
Return Lerp (Darkwood, Lightwood, BlendFactor);
}
In this shader, we declare a sampler called PulseTrainsampler in the global scope and passed it as the first parameter to the internal function Tex1d () (the internal function will be discussed in the next part). The HLSL sampler has a very direct mapping - rotation between APIs based on sampler concepts and actual silicon (in a 3D graphics processor responsible for addressing texture and filter texture). In a shader, you must define a sampler for each texture map you plan, but you can use a given sampler multiple times in a shader. This processing method is very common in the image processing program (discussed in the "Advanced Image Processing With Directx 9 Pixel Shade Processing With Directx 9 Pixel Shade Processing With Directx 9 Pixel Shaders" in the image processing program), in order to provide data to one internal filter represented by the shader code, The input image is sampled multiple times with different texture coordinates. For example, the following shader engine (Rasterizer) converts a height map into a normal map using a rasterizing engine (Rasterizer). Normal Map.
Sampler InputImage;
FLOAT4 Main (Float2 Topleft: Texcoord0, Float2 Left: Texcoord1,
FLOAT2 BOTTOMLT: TEXCOORD2, FLOAT2 TOP: Texcoord3,
FLOAT2 BOTTOM: TEXCOORD4, FLOAT2 TOPRIGHT: TEXCOORD5,
Float2 Right: Texcoord6, Float2 Bottomright: Texcoord7): Color {
// Take All Eight TAPS
FLOAT4 TL = TEX2D (InputImage, Topleft);
Float4 L = TEX2D (InputImage, Left);
FLOAT4 BL = TEX2D (InputImage, Bottomlease);
FLOAT4 T = TEX2D (InputImage, TOP);
FLOAT4 B = TEX2D (InputImage, Bottom);
FLOAT4 TR = TEX2D (InputImage, TOPRIGHT);
FLOAT4 R = TEX2D (InputImage, Right);
Float4 Br = TEX2D (InputImage, Bottomright);
// Compute DX Using Sobel Operator:
//
// -1 0 1
// -2 0 2
// -1 0 1
FLOAT DX = -tl.a - 2.0f * l.a - bl .a tr.a 2.0f * r.a br.a;
// compute DY Using Sobel Operator:
//
// -1 -2 -1
// 0 0 0
// 1 2 1
Float Dy = -tl.a - 2.0f * t.a - tr.a bl .a 2.0f * b.a br.a;
// compute cross-product and renormalize
Float4 n = float4 (Normalize (FLOAT3 (-DX,-Dy, 1)), TL.A);
// convert signed value from -1..1 to 0..1 Range and Return
RETURN N * 0.5F 0.5F;
}
This shader uses only one sampler: InputImage, but the internal function TEX2D () is called eight times in the example.