Platform SDK: DirectX |
Texture management, in short, is the process of determining which textures are needed for rendering at a given time, and ensuring that those textures are loaded into video memory. Like any algorithm, texture management schemes vary in complexity, but any approach to texture management involves the following key tasks.
Historically, Direct3D applications were responsible for managing textures on their own. Direct3D Immediate Mode for DirectX 6.0 introduced system-supported texture management, where Direct3D efficiently and intelligently performs texture management, ensuring that textures are loaded for optimal performance. (Texture surfaces that Direct3D manages are casually referred to as "managed textures.")
The texture manager uses a least-recently-used (LRU) algorithm to determine which textures should be evicted, which tracks textures with a time-stamp that identifies when the texture was last used. Texture priorities are used as "tie breakers" when two textures are targeted for removal from memory. If two textures have the same priority value, the least recently used texture will be removed in favor of the other texture. However, if two textures have the same time-stamp, the texture that has a lower priority will be evicted first.
You request automatic texture management for textures surfaces when you create them. To get a managed texture in a C++ application, simply create a texture surface that also includes the DDSCAPS2_TEXTUREMANAGE or DDSCAPS2_D3DTEXTUREMANAGE flags in the dwCaps2 member of the associated DDSCAPS2 structure. Note that you are not allowed to specify where you want the texture created–you can't use the DDSCAPS_SYSTEMMEMORY or DDSCAPS_VIDEOMEMORY flags when creating a managed texture. After creating the managed texture, you can call the IDirect3DDevice7::SetTexture method to set it to a stage in the rendering device's texture cascade.
You can assign a priority to managed textures by calling the IDirectDrawSurface7::SetPriority method for the texture surface.
You request automatic texture management for textures surfaces when you create them. To get a managed texture from Visual Basic, create a texture surface that also includes the DDSCAPS2_TEXTUREMANAGE or DDSCAPS2_D3DTEXTUREMANAGE flags in the lCaps2 member of the associated DDSCAPS2 type. Note that you are not allowed to specify where you want the texture created–you can't use the DDSCAPS_SYSTEMMEMORY or DDSCAPS_VIDEOMEMORY flags when creating a managed texture. After creating the managed texture, you can call the Direct3DDevice7.SetTexture method to set it to a stage in the rendering device's texture cascade.
You can assign a priority to managed textures by calling the DirectDrawSurface7.SetPriority method for the texture surface.
Direct3D automatically downloads textures into video memory as needed. (The system might cache managed textures in local or non-local video memory, depending on the availability of non-local video memory or other factors. Where your managed textures are cached is not communicated to your application, nor is this knowledge required to take advantage of automatic texture management.) If your application uses more textures than can fit in video memory, Direct3D will evict older textures from video memory to make room for the new textures. If you use an evicted texture again, the system uses the original system memory texture surface to reload the texture in the video memory cache. Reloading the texture is a minor, but obviously necessary, performance hit to the application.
You can dynamically modify the original system memory copy of the texture by blitting or locking the texture surface. When the system detects a "dirty" surface—after a blit is completed, or when the surface is unlocked—the texture manager automatically updates the video memory copy of the texture. The performance hit incurred is similar to reloading an evicted texture.
When entering a new level in a game, your application may need to flush all managed textures from video memory. You can explicitly request that all managed textures be evicted by calling the IDirect3D7::EvictManagedTextures method. When you call this method, Direct3D destroys any cached local and non-local video memory textures, but leaves the original system memory copies untouched.
When entering a new level in a game, your application may need to flush all managed textures from video memory. You can explicitly request that all managed textures be evicted by calling the Direct3D7.EvictManagedTextures method. When you call this method, Direct3D destroys any cached local and non-local video memory textures, but leaves the original system memory copies untouched.