Graphics Reference
In-Depth Information
How to do it…
For this recipe, we will first change the HLSL shader code to accept the SRV and sampler
state. Then, we will update our renderers to use texture coordinates, load the textures,
and bind the SRVs to the appropriate stages of the pipeline. To do so, follow the given steps:
1. Add the two texture files,
Texture.png
and
Texture2.png
, to the project.
Then, select Copy if newer as the Copy to Output value in the Properties window.
2.
Add the following global variables to
Simple.hlsl
:
// Globals for texture sampling
Texture2D ShaderTexture : register(t0);
SamplerState Sampler : register(s0);
3.
Modify
Simple.hlsl
so that the vertex structures look like the following
code snippet:
struct VertexShaderInput {
float4 Position : SV_Position;
float2 TextureUV : TEXCOORD0;
};
struct VertexShaderOutput {
float4 Position : SV_Position;
float2 TextureUV : TEXCOORD0;
};
4.
Change the
VSMain
HLSL function so that it passes through a
TextureUV
property rather than
Color
.
output.TextureUV = input.TextureUV;
5.
Replace the content of the
PSMain
function so that it samples the texture as
shown in the following code:
// Sample the pixel color using the sampler and texture
// using the input texture coordinate
return ShaderTexture.Sample(Sampler, input.TextureUV);