Graphics Reference
In-Depth Information
The device manager (the
DeviceManager
class) takes care of creating our Direct3D device
and context within its
Initialize
function. In addition, the device manager provides an
event to notify any listeners whenever the device manager is initialized/reinitialized—the
event that our
CreateDeviceDependentResources
function is tied to. This facilitates the
recreation of resources when a device is lost/recreated. This is necessary as resources that
have been created with a specific device must now be recreated with the new device.
The
D3DApplicationBase
base class provides appropriate methods that can be
overridden to participate within the rendering process and Direct3D resource management.
The
D3DApplicationDesktop
class descends from this base class and provides the ability
to initialize a swap chain from a Windows Desktop window handle. We then implement the
abstract base
Run()
method in order to provide a render and message loop.
By overriding the
D3DApplicationBase.CreateSwapChainDescription
method,
we are able to control the creation of the swap chain and render target. For example, if we
wanted to implement multisample antialiasing (MSAA), we would override this method and
update the description accordingly.
We override the
D3DApplicationBase.CreateDeviceDependentResources
method
to create any Direct3D resources that depend on the Direct3D device instance. This is an
event-handler that is attached to the device manager that is triggered whenever the Direct3D
device is created/recreated.
In addition, we create any resources that depend upon the swap chain/render target size
within an overridden
D3DApplicationBase.CreateSizeDependentResources
function.
This is an event-handler that is attached to any appropriate window size change events.
Many of our
Common
project classes descend from the
SharpDX.Component
class.
This utility class includes methods for managing the
IDisposable
objects. As a majority of
our code interacts with Direct3D, a native COM-based API, it is important that we are correctly
disposing of these objects to prevent memory/resource leaks. The
SharpDX.Component.
ToDispose<T>(T obj)
method allows us to create an instance of
IDisposable
objects
without having to explicitly dispose of the instance; instead, any objects registered within
the
ToDispose
method will be automatically released upon disposal of our
SharpDX.
Component
instance.
By declaring the
D3DApp
instance within a
using
block, as long as our
D3DApp
class passes all created Direct3D resources into the
ToDispose
method,
they will be correctly released. The counterpart to this is the
SharpDX.Component.
RemoveAndDispose<T>(ref T obj)
function, where we can manually release resources
at the beginning of the implementation of our
CreateDeviceDependentResources
or
CreateSizeDependentResources
methods. The following code snippet shows how to
reinitialize a resource. Note that there is no need to check for
null
:
RemoveAndDispose(ref myDirect3DResource);
...
myDirect3DResource = ToDispose(new ...);