Texture Object
In Direct3D 10, you specify the samplers and textures independently; texture sampling is implemented by using a templated-texture object. This templated-texture object has a specific format, returns a specific type, and implements several methods.
Differences between Direct3D9 and Direct3D10:
- In Direct3D 9, samplers are bound to specific textures.
- In Direct3D 10, textures and samplers are independent objects. Each templated-texture object implements texture sampling methods that take both the texture and the sampler as input parameters.
Not all formats are supported for all functions. So you should use CheckFeatureSupport to check whether a format is supported for a particular usage.
Here is the syntax for creating all texture objects (except multisampled objects).
Object1 [<Type>] Name; |
---|
Multisampled objects (Texture2DMS and Texture2DMSArray) require the texture size to be explicitly stated and expressed as the number of samples.
Object2 [<Type, Samples>] Name; |
---|
Parameters
Item | Description | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Object |
A texture object. Must be one of the following types.
|
||||||||||||||||||||||||
Type |
Optional. Any scalar HLSL type or vector HLSL type, surrounded by angle brackets. The default type is float4. |
||||||||||||||||||||||||
Name |
An ASCII string that specifies the texture object name. |
||||||||||||||||||||||||
Samples |
The number of samples (ranges between 1 and 128). |
Example 1
Here is an example of declaring a texture object.
Texture2D <float4> MyTex;
Texture2DMS <float4, 128> MyMSTex;
Texture Object Methods
Each texture object implements certain methods; here's the table that lists all of the methods. See the reference page for each method to see what objects can use that method.
Texture Method | Description | vs_4_0 | vs_4_1 | ps_4_0 | ps_4_1 | gs_4_0 | gs_4_1 |
---|---|---|---|---|---|---|---|
CalculateLevelOfDetail | Calculate the LOD, return a clamped result. | x | |||||
CalculateLevelOfDetailUnclamped | Calculate the LOD, return an unclamped result. | x | |||||
Gather | Gets the four samples (red component only) that would be used for bilinear interpolation when sampling a texture. | x | x | x | |||
GetDimensions | Get the texture dimension for a specified mipmap level. | x | x | x | x | x | x |
GetDimensions (MultiSample) | Get the texture dimension for a specified mipmap level. | x | x | x | |||
GetSamplePosition | Get the position of the specified sample. | x | x | x | |||
Load | Load data without any filtering or sampling. | x | x | x | x | x | x |
Load (Multisample) | Load data without any filtering or sampling. | x | x | x | x | ||
Sample | Sample a texture. | x | x | ||||
SampleBias | Sample a texture, after applying the bias value to the mipmap level. | x | x | ||||
SampleCmp | Sample a texture, using a comparison value to reject samples. | x | x | ||||
SampleCmpLevelZero | Sample a texture (mipmap level 0 only), using a comparison value to reject samples. | x | x | x | x | x | x |
SampleGrad | Sample a texture using a gradient to influence the way the sample location is calculated. | x | x | x | x | x | x |
SampleLevel | Sample a texture on the specified mipmap level. | x | x | x | x | x | x |
Return Type
The return type of a texture object method is float4 unless specified otherwise, with the exception of the multisampled anti-aliased texture objects that always need the type and sample count specified. The return type is the same as the texture resource type (DXGI_FORMAT). In other words, it can be any of the following types.
Type | Description |
---|---|
float | 32-bit float (see Floating-Point Rules for differences from IEEE float) |
int | 32-bit signed integer |
unsigned int | 32-bit unsigned integer |
snorm | 32-bit float in range -1 to 1 inclusive (see Floating-Point Rules for differences from IEEE float) |
unorm | 32-bit float in range 0 to 1 inclusive (see Floating-Point Rules for differences from IEEE float) |
any texture type or struct | The number of components returned must be between 1 and 3 inclusive. |
In addition, the return type can be any texture type including a structure but, it must be less than 4 components such as a float1 type which returns one component.
Default Values for Missing Components in a Texture
The default value for missing components in a texture resource type is zero for any component except the alpha component (A); the default value for the missing A is one. The way that this one appears to the shader depends on the texture resource type. It takes the form of the first typed component that is actually present in the texture resource type (starting from the left in RGBA order). If this form is UNORM or FLOAT, the default value for the missing A is 1.0f. If the form is SINT or UINT, the default value for the missing A is 0x1.
For example, when a shader reads the DXGI_FORMAT_R24_UNORM_X8_TYPELESS texture resource type, the default values for G and B are zero and the default value for A is 1.0f; when a shader reads the DXGI_FORMAT_R16G16_UINT texture resource type, the default value for B is zero and the default value for A is 0x00000001; when a shader reads the DXGI_FORMAT_R16_SINT texture resource type, the default values for G and B are zero and the default value for A is 0x00000001.
Example 2
Here is an example of texture sampling using a texture method.
sampler MySamp;
Texture2D <float4> MyTex;
float4 main( float2 TexCoords[2] : TEXCOORD ) : SV_Target
{
return MyTex.Sample( MySamp, TexCoords[0] ));
}
Minimum Shader Model
This object is supported in the following shader models.
Shader Model | Supported |
---|---|
Shader Model 4 and higher shader models | yes |