Texture Management

[Previous] [Next]

Texture management is the process of determining which textures are needed for rendering, and making sure that those textures are loaded in video memory so that Direct3D can apply them to objects. Three primary tasks are required for performing texture management:

  • Tracking the available texture memory
  • Determining which textures are needed for rendering the current frame
  • Deciding which of the existing texture surfaces you can reload with another texture image and which surfaces you should replace with new texture surfaces

Before the release of DirectX 6, you had to implement your own texture management scheme, which entailed spending extra time on software development that you probably would've preferred to spend on content development. Current versions of Direct3D can perform texture management for you, guaranteeing that textures are automatically loaded for optimal performance. The texture surfaces that Direct3D manages are known as managed textures. You can still do your own texture management if you prefer, but Direct3D's texture management does everything you need for most purposes.

The CreateTextureFromBitmap function shown in the "Creating Texture Surfaces" section of this chapter illustrates how to turn on texture management when a hardware Direct3D device is being used. You should not turn on texture management when using a software Direct3D device. If the GUID of the current Direct3D device is IID_IDirect3DHALDevice or IID_IDirect3DTnLHalDevice, the DDSCAPS2_TEXTUREMANAGE flag is set to make the new texture use Direct3D's texture management.

Three other flags pertain to texture management and help the system understand what the application will do with its textures: DDSCAPS2_HINTDYNAMIC, DDSCAPS2_HINTSTATIC, and DDSCAPS2_OPAQUE. You can use these flags only for texture surfaces, which are defined with the DDSCAPS_TEXTURE flag set in the dwCaps member. These flags are optional, but they can improve performance. You can't set more than one of these flags on a texture.

Here are the definitions of these flags:

  • DDSCAPS2_HINTDYNAMIC Tells the driver that the texture surface will often be locked. This is frequently the case for procedural textures (computer-generated textures), dynamic light maps (dynamic computer-generated textures that simulate lighting conditions), and similar objects.
  • DDSCAPS2_HINTSTATIC Tells the driver that a texture surface can be reordered or retiled on load. This operation is fairly fast and symmetrical because an application can lock these bits. (Remember, the application will take a performance hit when this operation occurs.) Reordering or retiling a texture doesn't affect its size.
  • DDSCAPS2_OPAQUE Tells the driver that this texture surface won't be locked again. The driver can therefore optimize this surface by retiling and compressing it when it needs to. You can't lock this surface. You also can't used it in blit operations. If you try to either lock or blit a surface with this capability, the call will fail.

Direct3D's texture-management scheme automatically loads textures into video memory as needed. Be aware that Direct3D can store managed textures in local or nonlocal video memory. Direct3D doesn't indicate where the texture is cached because it determines when an application is trying to use more textures than will fit in video memory. If an application exceeds video-memory capacity with its textures, Direct3D will remove, or evict, older textures from video memory to free up space for new ones.

If an application needs to reuse a texture that Direct 3D has evicted, Direct3D will reload the original texture surface from system memory into the video memory cache. Each time a texture requires reloading, it will slow your application slightly. However, the lag time is minimal and, of course, unavoidable when implementing this efficient approach of texture handling.

You can dynamically modify the system memory's original copy of a texture by blitting to or locking the texture surface. Once a blit is completed or the surface is unlocked—in other words, once Direct3D finds a dirty texture—the texture manager will automatically update the video memory's copy of the texture. This update will cause a performance hit comparable to that of reloading an evicted texture. Typically you'll need to evict all the managed textures from video memory when you move from one game level to another or when you load a new segment of a map (as with the RoadRage application we're building in this book). You can evict all managed textures using the IDirect3D7::EvictManagedTextures method, which destroys any cached local and nonlocal textures in video memory and leaves the original copies in system memory alone.

Here's the declaration for the IDirect3D7::EvictManagedTextures method:

 HRESULT IDirect3D7::EvictManagedTextures() 

This method doesn't have any parameters.

One performance enhancement related to textures that you might want to consider is to reverse the order in which you render polygons from frame to frame. This prevents texture thrashing during the process of evicting textures used in the current frame to reclaim texture memory for subsequent frames. Texture thrashing can occur when the textures you require exceed the available video memory. If your frame requires textures A, B, C, and D but you have only enough video memory for three textures, you should render the polygons that need A first, followed by those that need B, then those that need C, and finally those that need D. For the next frame, you should start with the polygons that need texture D, followed by those that need C, then B, then A. This way, you'll need to evict and reload only one texture (A); this is obviously preferable to having the Least Recently Used algorithm in the texture manager force you to reload every texture!

You might be wondering, "Why not always use Direct3D's texture management?" The answer is the same as the answer to the question, "Why not always use Direct3D's transformation and lighting?" Start with using the facilities that Direct3D provides and when your application is fully fleshed out, profile it to determine whether writing your own algorithms will improve its performance. Most of the time, you'll be quite happy with Direct3D's performance.



Inside Direct3D
Inside Direct3D (Dv-Mps Inside)
ISBN: 0735606137
EAN: 2147483647
Year: 1999
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net