User Tools

Site Tools


tutorial_207_20-_20texture_20mapping_20and_20constant_20buffers

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
tutorial_207_20-_20texture_20mapping_20and_20constant_20buffers [2018/04/13 22:44]
richardrussell Added syntax highlighting
tutorial_207_20-_20texture_20mapping_20and_20constant_20buffers [2018/04/17 19:16] (current)
tbest3112 Added syntax highlighting for HLSL (useing glsl as close enough)
Line 58: Line 58:
  
 Since the input layout changed, the corresponding vertex shader input must also be modified to match the addition:\\ \\  Since the input layout changed, the corresponding vertex shader input must also be modified to match the addition:\\ \\ 
 +<code glsl>
   struct VS_INPUT   struct VS_INPUT
   {   {
Line 63: Line 64:
       float2 Tex : TEXCOORD;       float2 Tex : TEXCOORD;
   };   };
 +</​code>​
 \\  Finally, we are ready to include texture coordinates in our vertices we defined back in Tutorial 4. Note the second parameter input is a D3DXVECTOR2 containing the texture coordinates. Each vertex on the cube will correspond to a corner of the texture. This creates a simple mapping where each vertex gets (0,0) (0,1) (1,0) or (1,1) as the coordinate: \\  Finally, we are ready to include texture coordinates in our vertices we defined back in Tutorial 4. Note the second parameter input is a D3DXVECTOR2 containing the texture coordinates. Each vertex on the cube will correspond to a corner of the texture. This creates a simple mapping where each vertex gets (0,0) (0,1) (1,0) or (1,1) as the coordinate:
  
Line 121: Line 123:
 ===== Applying the Texture ===== ===== Applying the Texture =====
 \\  To map the texture on top of the geometry, we will be calling a texture lookup function within the pixel shader. The function Sample will perform a texture lookup of a 2D texture, and then return the sampled colour. The pixel shader shown below calls this function and multiplies it by the underlying mesh colour (or material colour), and then outputs the final colour.\\ \\  \\  To map the texture on top of the geometry, we will be calling a texture lookup function within the pixel shader. The function Sample will perform a texture lookup of a 2D texture, and then return the sampled colour. The pixel shader shown below calls this function and multiplies it by the underlying mesh colour (or material colour), and then outputs the final colour.\\ \\ 
 +<code glsl>
   // Pixel Shader   // Pixel Shader
   float4 PS( PS_INPUT input) : SV_Target   float4 PS( PS_INPUT input) : SV_Target
Line 126: Line 129:
       return txDiffuse.Sample( samLinear, input.Tex ) * vMeshColor;       return txDiffuse.Sample( samLinear, input.Tex ) * vMeshColor;
   }   }
 +</​code>​
   * txDiffuse is the object storing our texture that we passed in from the code above, when we bound the resource view g_pTextureRV to it.   * txDiffuse is the object storing our texture that we passed in from the code above, when we bound the resource view g_pTextureRV to it.
   * samLinear is the sampler specifications for the texture lookup.   * samLinear is the sampler specifications for the texture lookup.
   * input.Tex is the coordinates of the texture that we have specified in the source.   * input.Tex is the coordinates of the texture that we have specified in the source.
 \\  Another thing we must remember to do is to pass the texture coordinates through the vertex shader. If we don't, the data is lost when it gets to the pixel shader. Here, we just copy the input'​s coordinates to the output, and let the hardware handle the rest:\\ \\  \\  Another thing we must remember to do is to pass the texture coordinates through the vertex shader. If we don't, the data is lost when it gets to the pixel shader. Here, we just copy the input'​s coordinates to the output, and let the hardware handle the rest:\\ \\ 
 +<code glsl>
   // Vertex Shader   // Vertex Shader
   PS_INPUT VS( VS_INPUT input )   PS_INPUT VS( VS_INPUT input )
Line 142: Line 146:
       return output;       return output;
   }   }
 +</​code>​
 \\  \\ 
 ===== Constant Buffers ===== ===== Constant Buffers =====
 \\  In the previous tutorials, we used a single constant buffer to hold all of the shader constants we need. But the best way to efficiently use constant buffers is to organize shader variables into constant buffers based on their frequency of update. This allows an application to minimize the bandwidth required for updating shader constants. As an example, this tutorial groups constants into three structures: one for variables that change every frame, one for variables that change only when a window size is changed, and one for variables that are set once and then do not change.\\ \\  The following constant buffers are defined in this tutorial'​s .fx file:\\ \\  \\  In the previous tutorials, we used a single constant buffer to hold all of the shader constants we need. But the best way to efficiently use constant buffers is to organize shader variables into constant buffers based on their frequency of update. This allows an application to minimize the bandwidth required for updating shader constants. As an example, this tutorial groups constants into three structures: one for variables that change every frame, one for variables that change only when a window size is changed, and one for variables that are set once and then do not change.\\ \\  The following constant buffers are defined in this tutorial'​s .fx file:\\ \\ 
 +<code glsl>
       cbuffer cbNeverChanges       cbuffer cbNeverChanges
       {       {
Line 160: Line 166:
           float4 vMeshColor;           float4 vMeshColor;
       };       };
 +</​code>​
 \\  To work with these constant buffers, you need to create a ID3D11Buffer object for each one. Then you can call ID3D11DeviceContext::​UpdateSubresource() to update each constant buffer when needed without affecting the other constant buffers: \\  To work with these constant buffers, you need to create a ID3D11Buffer object for each one. Then you can call ID3D11DeviceContext::​UpdateSubresource() to update each constant buffer when needed without affecting the other constant buffers:
  
tutorial_207_20-_20texture_20mapping_20and_20constant_20buffers.txt ยท Last modified: 2018/04/17 19:16 by tbest3112