Graphics Reference
In-Depth Information
How it works…
First, we will update our HLSL to include two global variables for storing the SRV and the
sampler state we created in our renderers. Next, we will update our shader to accept a
texture UV coordinate as input rather than a color ( float2 TextureUV : TEXCOORD0; ).
Texels, in a 2D texture, are addressed using the x and y axes. However,
as these names are already used in 3D space, we refer to them as UV
coordinates instead. UV coordinates are normalized, where the upper left
corner is 0(U),0(V) and the bottom right corner is 1(U),1(V). Therefore,
the UV coordinate for the pixel located at 100, 150 within a 200 x 200
texture would be 0.5(U), 0.75(V) (that is, 100/200, 150/200). There is also
a third component, W (z axis) that can be used to address 3D textures or
volume textures and cube maps. Therefore, the full term is therefore a UVW
coordinate, however, we generally use a 2D texture and drop the W.
The vertex shader now passes through an unchanged UV coordinate. The pixel shader then
determines the color by sampling from the ShaderTexture global variable provided by the
SRV by using the sampler state and interpolated vertex UV coordinate.
Since we changed the structure of the vertex, we must also update the input layout for the
IA so that it is now expecting four floats for the vertex position and two floats for the UV.
In each of the renderers, we are loading a SRV directly from the texture file using
ShaderResourceView.FromFile . Internally this creates a Texture2D resource
and returns a SRV so that it can be bound to the pipeline.
ShaderResourceView.FromFile is unavailable for the
Windows Store apps, Chapter 11 , Integrating Direct3D with XAML
and Windows 8.1 , covers how to replace this method in the recipe
Loading and compiling resources asynchronously .
The SamplerState global variable that we create controls how the sampling in the pixel
shader determines the correct coordinate and which filter method to use. The AddressU/V/W
properties of the description control how to handle values outside of the 0.0 - 1.0 UVW
range. For example, whereas TextureAddressMode.Wrap repeats the texture,
using TextureAddressMode.Clamp will make anything outside the range appear
smeared (clamping to zero or one)—try clamping the quad renderer to see this effect.
 
Search WWH ::




Custom Search