hello,structs!
In this section, Helloshaders effect is rewritten using the C-style structure. The use of data structures can better organize multiple shader inputs and outputs than a single parameter. First create a new effect and material in the Nvidia FX composer, just as this chapter starts by adding effect using the Add Effect Wizard. Or a simple copy helloshaders.fx into a new file hellostructs.fx. I prefer the second option because it is often necessary to reuse shader code, based on the previous material. After copying to the Hellostructs.fx file, click the right mouse button in the materials section of the Assets Panel and select Add materials from file to add the shader file to the Nvidia FX composer. Locate and select the Hellostructs.fx file, and you will see that the new Hellostructs and Hellostructs_material objects are already in the assets panel.
List 4.6 contains a full listing of the hellostructs.fx effect.
List 4.6 hellostructs.fx
Cbuffer cbufferperobject
{
float4x4 worldviewprojection:worldviewprojection;
}
Rasterizerstate disableculling
{
cullmode = NONE;
struct Vs_input
{
float4 objectposition:position;
};
struct Vs_output
{
float4 position:sv_position;
};
Vs_output Vertex_shader (vs_input in)
{
vs_output out = (vs_output) 0;
Out. Position = Mul (in. Objectposition, worldviewprojection);
return out;
}
FLOAT4 Pixel_shader (Vs_output in): Sv_target
{return
float4 (1, 0, 0, 1);
}
Technique10 main10
{pass
p0
{setvertexshader (Compileshader (Vs_4_0
, Vertex_shader ()));
Setgeometryshader (NULL);
SetPixelShader (Compileshader (Ps_4_0, Pixel_shader ()));
Setrasterizerstate (disableculling);
}
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/
The difference between helloshaders.fx and hellostructs.fx is small, but important, as they establish the norms used in this book. The first thing to note is what has not changed, Cbufferperobject and Disableculling objects are the same, main10 technique and the corresponding pass is the same. Pixel Shader's internal code has not changed either. It is mainly a new addition of two structural bodies vs_input and vs_output, respectively, representing the input and output of vertex shader. The Vs_input in hellostructs.fx also has an input variable shader that is the same as the helloshaders vertex objectposition. The only difference is that in hellostructs the variable is declared as a FLOAT4 type instead of a FLOAT3 type. There is no need to add a W component to the vector. In addition, vertex shadre now returns a Vs_output instance instead of a float4 type, and the sv_position semantics are no longer associated directly with the return value, but correspond to the vs_output members in the postion structure. The postion member replaces the unnamed return value of vertex shader in helloshaders effect.
Next, analyze the latest vertex shader code block. In this section of code, a Vs_output instance is declared and returned, just like the C programming style, which can be accessed through the dot opertator (dot operator) to the position member in the Vs_output instance. Similarly, the objectposition member in the parameter in of the Vs_input struct type is used in the MUL function call. In addition, the C-style type conversion is used to initialize the members in the out struct variable to 0. Although initializing this step is not mandatory, it is a good programming habit. Finally, you can see that the type of the input parameter in the pixel shader is exactly the output parameter in the vertex shader. In the current example, no member of the input parameter has been used, but will be used in the later shaders. The significance of using a struct to rearrange itself is that it is now possible to increase shader input and output without modifying vertex shaders and pixel shaders. The output of the hellostructs is consistent with the output of the helloshaders, as shown in Figure 4.3.
Summarize
This chapter compiles the first HLSL shaders! Learn about the FX file format, constant buffer, and some knowledge of render states. The HLSL syntax is also further understood, including vector and matrix data types (such as float3,flaot4,float4x4) and custom structures. And this knowledge was used in Nvidia FX composer to generate the first rendering output. The learning tasks completed in this chapter will be the basis of the second section, the "Shader Authoring with HLSL" later chapters.
Exercises
1. Change the values of the RGB channels in the helloshaders or hellostructs pixel shader, and
Observe the results.
2. Modify the Disableculling rasterizer state object by setting Cullmode = FRONT and then back, and
Observe the results.
3. Now it have a couple effects, get comfortable working within NVIDIA FX Composer. Create
Teapot, Torus, and Plane objects, and assign them either the Helloshaders or hellostructs.
Notice How-objects that are assigned the same material are-impacted when your change and recompile
The associated effect.
1. Change the RGB color channel value of pixel shader in helloshaders or hellostructs, and observe the output.
2. Modify Disableculling render state, set Cullmode = FRONT, observe output, then modify to Cullmode = back to observe output.
3. Now you have created two kinds of effects and learned to use Nvidia FX Composer. You can create some other objects, such as Teapot,torus and plane, and specify helloshaders or hellostructs material for these objects. Note that after recompiling the corresponding effect, all objects specify the effect of the same material.
The intended Learning code:
http://download.csdn.net/detail/chenjinxian_3d/9570518