Resource management can be a challenge for Direct3D applications. Before DirectX, display adapters had limited amounts of fast device memory and programmers had to manage that memory manually in order to achieve the best performance. The Direct3D API provides different memory pools to allow the application to specify the lifetime management policy of a resource.
The available resource pools are: default, managed, system memory and scratch. Resources in the default pool live only on the device and must be restored to the device when it is lost. Resources in the managed pool contain a system memory backing store for the resource and are spooled into device memory as they are needed by the runtime. Allowing the runtime to spool the resource onto the device when it is needed simplifies resource management for the application.
Suppose your application has more textures than will fit in device memory. They cannot all be allocated into the default resource pool. Your application will need to create textures at startup that are sufficiently large to handle the largest texture used by your application. They will also need to be created with the appropriate usage flags (render target usage, sRGB usage, etc.) and format.
Worse, you may need to destroy and create textures on the fly due to resource exhaustion. Destroying and creating resources is expensive and can cause a stall in your real-time rendering frame rate. You’ll need to write a bunch of infrastructure code in order to manage the limited amount of device memory for your resources. You’ll probably want a priority system to specify which resources are more important than others and you’ll want a prefetch system to load resources into device memory in advance of when they will be needed to avoid stalling the pipeline.
Congratulations, you’ve just re-implemented the managed resource pool!
The managed resource pool is even better than what I’ve described above. Resources in the managed pool persist across a device reset or a device lost condition. You don’t need to worry about creating a single resource that is big enough to hold your largest asset of a particular format and usage combination; you can simply create managed resources with the desired usage and format. The system memory backing store for a managed resource is used as the source of data when the resource needs to be moved into device memory. The system memory backing store also allows the application to update the resource by modifying the backing store and allowing the runtime to update the changed data on the device as needed.
The SetPriority and PreLoad methods on the IDirect3DResource9 interface let you fine tune the resource management behavior for an individual resource. All managed resources can be evicted from device memory using the EvictManagedResources method on the IDirect3DDevice9 interface.
Windows Vista and Windows 7
The situation is slightly different for devices using the Windows Vista display driver model (WDDM) under Windows Vista or Windows 7. Under the WDDM, device memory is virtualized and the GPU can page in resources on demand similar to the way memory is paged in from the swap file on demand for the CPU. With a WDDM device, you can use IDirect3DDevice9Ex to create resources in the default pool that won’t be lost on a device reset or device lost condition. In effect, the default resource pool becomes equivalent to the managed resource pool in this situation.