Textures in DirectX Computational Graphics

Textures are fundamental to creating visually rich and detailed graphics in DirectX. They are essentially 2D, 3D, or even 1D images that are applied to the surfaces of 3D models to add color, detail, and various surface properties.

What are Textures?

In computer graphics, a texture is a data structure used to give a surface more detail, or to modify its visual properties. Textures can represent anything from simple color maps to complex data like bump maps, normal maps, specular maps, and more. They are typically loaded from image files (like .dds, .png, .jpg) or generated procedurally.

Texture Mapping

The process of applying a texture to a 3D model is called texture mapping. This involves defining how the 2D texture coordinates (UV coordinates) correspond to points on the 3D model's surface. Each vertex of a 3D model can be assigned UV coordinates, which are then interpolated across the faces of the model by the graphics hardware.

UV Coordinates

UV coordinates are a 2D coordinate system, usually ranging from 0.0 to 1.0, where 'U' typically represents the horizontal axis and 'V' represents the vertical axis of the texture. A UV coordinate of (0,0) usually maps to the bottom-left corner of the texture, and (1,1) to the top-right corner, though conventions can vary.

Types of Textures

Color Textures (Albedo Maps)

These are the most common type of textures, providing the base color of a surface. They define the diffuse color that is visible under direct lighting.

Normal Maps

Normal maps store surface normal information, which can simulate intricate surface details like bumps, wrinkles, and pores without adding additional geometric complexity to the model. This significantly improves the visual fidelity of the scene.

Specular Maps

Specular maps control the shininess of a surface, determining how light reflects off it. They can be used to simulate materials like metal or polished surfaces.

Roughness Maps

Roughness maps define how rough or smooth a surface is. This influences the spread and intensity of specular highlights. Often used in Physically Based Rendering (PBR).

Metallic Maps

In PBR workflows, metallic maps define whether a surface is metallic or dielectric (non-metallic), affecting how light interacts with the material.

Height Maps (Displacement Maps)

These textures store height information that can be used to displace the geometry of the surface, creating true geometric detail. This is more computationally intensive than normal mapping.

Texture Formats and Compression

DirectX supports a wide range of texture formats, including uncompressed formats like RGBA8 and compressed formats like BC1 (DXT1), BC3 (DXT5), BC7, and others. Compressed formats reduce memory usage and bandwidth requirements, which are crucial for performance in real-time graphics.

Common Compressed Formats

Working with Textures in DirectX

In DirectX, textures are represented by ID3D11ShaderResourceView (for Direct3D 11) or ID3D12Resource and associated views (for Direct3D 12). These objects are created from texture data and are bound to the graphics pipeline so shaders can sample from them.

Shader Sampling

Shaders use texture samplers to fetch texel data from textures. The sampler state defines how the texture is filtered (e.g., linear, point sampling) and how out-of-bounds coordinates are handled (e.g., wrap, clamp).


// Example HLSL code for sampling a texture
Texture2D myTexture;
SamplerState mySampler;

float4 main(float2 uv : TEXCOORD) : SV_TARGET
{
    return myTexture.Sample(mySampler, uv);
}
            

Mipmaps

Mipmaps are pre-calculated, recursively downsampled versions of a texture. They are used to improve performance and visual quality by selecting the most appropriate texture resolution based on the object's distance from the camera. This reduces aliasing and saves GPU cache memory.