How to: Initialize a Texture Programmatically

You can initialize a texture during object creation, or you can fill the object programmatically after it is created. This topic has several examples showing how to initialize textures that are created with different types of usages. This example assumes you already know how to Create a Texture.

Default Usage

The most common type of usage is default usage. To fill a default texture (one created with D3D11_USAGE_DEFAULT) you can either:

Dynamic Usage

To fill a dynamic texture (one created with D3D11_USAGE_DYNAMIC):

  1. Get a pointer to the texture memory by passing in D3D11_MAP_WRITE_DISCARD when calling ID3D11DeviceContext::Map.
  2. Write data to the memory.
  3. Call ID3D11DeviceContext::Unmap when you are finished writing data.

Staging Usage

To fill a staging texture (one created with D3D11_USAGE_STAGING):

  1. Get a pointer to the texture memory by passing in D3D11_MAP_WRITE when calling ID3D11DeviceContext::Map.
  2. Write data to the memory.
  3. Call ID3D11DeviceContext::Unmap when you are finished writing data.

A staging texture can then be used as the source parameter to ID3D11DeviceContext::CopyResource or ID3D11DeviceContext::CopySubresourceRegion to fill a default or dynamic resource.

How to Use Direct3D 11

Textures