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:
Call ID3D11Device::CreateTexture2D and initialize pInitialData to point to data provided from an application.
or
After calling ID3D11Device::CreateTexture2D, use ID3D11DeviceContext::UpdateSubresource to fill the default texture with data from a pointer provided by the application.
Dynamic Usage
To fill a dynamic texture (one created with D3D11_USAGE_DYNAMIC):
- Get a pointer to the texture memory by passing in D3D11_MAP_WRITE_DISCARD when calling ID3D11DeviceContext::Map.
- Write data to the memory.
- Call ID3D11DeviceContext::Unmap when you are finished writing data.
Staging Usage
To fill a staging texture (one created with D3D11_USAGE_STAGING):
- Get a pointer to the texture memory by passing in D3D11_MAP_WRITE when calling ID3D11DeviceContext::Map.
- Write data to the memory.
- 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.
Related topics