2
#include "Direct3DBase.h"
4
using namespace DirectX;
5
using namespace Microsoft::WRL;
6
using namespace Windows::UI::Core;
7
using namespace Windows::Foundation;
8
using namespace Windows::Graphics::Display;
11
Direct3DBase::Direct3DBase()
15
// Initialize the Direct3D resources required to run.
16
void Direct3DBase::Initialize()
18
CreateDeviceResources();
21
// These are the resources that depend on the device.
22
void Direct3DBase::CreateDeviceResources()
24
// This flag adds support for surfaces with a different color channel ordering
25
// than the API default. It is required for compatibility with Direct2D.
26
UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
29
// If the project is in a debug build, enable debugging via SDK Layers with this flag.
30
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
33
// This array defines the set of DirectX hardware feature levels this app will support.
34
// Note the ordering should be preserved.
35
// Don't forget to declare your application's minimum required feature level in its
36
// description. All applications are assumed to support 9.1 unless otherwise stated.
37
D3D_FEATURE_LEVEL featureLevels[] =
39
D3D_FEATURE_LEVEL_11_1,
40
D3D_FEATURE_LEVEL_11_0,
41
D3D_FEATURE_LEVEL_10_1,
42
D3D_FEATURE_LEVEL_10_0,
46
// Create the Direct3D 11 API device object and a corresponding context.
47
ComPtr<ID3D11Device> device;
48
ComPtr<ID3D11DeviceContext> context;
51
nullptr, // Specify nullptr to use the default adapter.
52
D3D_DRIVER_TYPE_HARDWARE,
54
creationFlags, // Set set debug and Direct2D compatibility flags.
55
featureLevels, // List of feature levels this app can support.
56
ARRAYSIZE(featureLevels),
57
D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION.
58
&device, // Returns the Direct3D device created.
59
&m_featureLevel, // Returns feature level of device created.
60
&context // Returns the device immediate context.
64
// Get the Direct3D 11.1 API device and context interfaces.
66
device.As(&m_d3dDevice)
70
context.As(&m_d3dContext)
74
// Allocate all memory resources that depend on the window size.
75
void Direct3DBase::CreateWindowSizeDependentResources()
77
// Create a descriptor for the render target buffer.
78
CD3D11_TEXTURE2D_DESC renderTargetDesc(
79
DXGI_FORMAT_B8G8R8A8_UNORM,
80
static_cast<UINT>(m_renderTargetSize.Width),
81
static_cast<UINT>(m_renderTargetSize.Height),
84
D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE
86
renderTargetDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX | D3D11_RESOURCE_MISC_SHARED_NTHANDLE;
88
// Allocate a 2-D surface as the render target buffer.
90
m_d3dDevice->CreateTexture2D(
98
m_d3dDevice->CreateRenderTargetView(
105
// Create a depth stencil view.
106
CD3D11_TEXTURE2D_DESC depthStencilDesc(
107
DXGI_FORMAT_D24_UNORM_S8_UINT,
108
static_cast<UINT>(m_renderTargetSize.Width),
109
static_cast<UINT>(m_renderTargetSize.Height),
112
D3D11_BIND_DEPTH_STENCIL
115
ComPtr<ID3D11Texture2D> depthStencil;
117
m_d3dDevice->CreateTexture2D(
124
CD3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc(D3D11_DSV_DIMENSION_TEXTURE2D);
126
m_d3dDevice->CreateDepthStencilView(
128
&depthStencilViewDesc,
133
// Set the rendering viewport to target the entire window.
134
CD3D11_VIEWPORT viewport(
137
m_renderTargetSize.Width,
138
m_renderTargetSize.Height
141
m_d3dContext->RSSetViewports(1, &viewport);
144
void Direct3DBase::UpdateForRenderResolutionChange(float width, float height)
146
m_renderTargetSize.Width = width;
147
m_renderTargetSize.Height = height;
149
ID3D11RenderTargetView* nullViews[] = {nullptr};
150
m_d3dContext->OMSetRenderTargets(ARRAYSIZE(nullViews), nullViews, nullptr);
151
m_renderTarget = nullptr;
152
m_renderTargetView = nullptr;
153
m_depthStencilView = nullptr;
154
m_d3dContext->Flush();
155
CreateWindowSizeDependentResources();
158
void Direct3DBase::UpdateForWindowSizeChange(float width, float height)
160
m_windowBounds.Width = width;
161
m_windowBounds.Height = height;