Graphics Reference
In-Depth Information
The
async
keyword works for us here because
CreateDeviceDependentResources
is an overridden event handler attached to the
DeviceManager
instance within the
D3DApplicationBase
class constructor as shown here:
DeviceManager.OnInitialize
+= CreateDeviceDependentResources;
As the
CreateDeviceDependentResources
event handler will now continue running
asynchronously while the
CompositionTarget.Rendering
event fires for each frame, we
must set a flag to indicate when the resources are ready to be used and then check this flag
within the
D3DApp.Render
function before attempting to render. With the main Direct3D
application class implementing asynchronous resource creation, there is no need to use the
async
/
await
keywords within other renderers'
CreateDeviceDependentResources
functions. If we did this, it would be necessary to also check when those renderers' resources
are ready before attempting to render a frame.
Loading a mesh at runtime uses the
await
keyword as well, because the
Common.Mesh.
LoadFromFileAsync
function utilizes the
Windows.Storage.StorageFile
class for file
I/O in Windows Store app. The
CompileFromFileAsync
method provides a wrapper around
the Direct3D 11
Compile
function, implementing the
async
/
await
keywords. The ability to
compile shaders at runtime within Windows Store apps is new to Windows 8.1. For Windows
Store apps under Windows 8.0, it was possible to do so within development environments and
only if copying the library manually to the build directory.
There's moreā¦
In the downloadable content for this topic, available on the Packt website, the projects
Ch11_03CreatingResourcesAsync
and
Common.WinRT
also include changes necessary
to load textures from a file. Windows Store apps do not support the
ShaderResourceView.
FromFile
function and instead require you to provide your own DirectDraw Surface (DDS)
file and image-loading routines.
The static class
Common.LoadTexture
includes the following functions:
F
LoadBitmap
: This loads a bitmap using Windows Imaging Component (WIC)
F
CreateTexture2DFromBitmap
: This creates a
Texture2D
resource from
SharpDX.WIC.BitmapSource
F
SRVFromFile
and
LoadFromFile
: These either load a DDS file using a C# ported
version of DDS code from Microsoft or load regular image formats using WIC.
They return a
ShaderResourceView
method and/or a
SharpDX.Direct3D11.
Resource
(
Texture1D
/
Texture2D
/
Texture3D
) function.
The only change to the updated
MeshRenderer
class in the completed project for this recipe
is to call
LoadTexture.SRVFromFile
instead of
ShaderResourceView.FromFile
,
which is no longer available with Windows Store apps.