Path: blob/master/samples/wp8/OcvRotatingCube/PhoneXamlDirect3DApp1/PhoneXamlDirect3DApp1Comp/Direct3DBase.cpp
16354 views
#include "pch.h"1#include "Direct3DBase.h"23using namespace DirectX;4using namespace Microsoft::WRL;5using namespace Windows::UI::Core;6using namespace Windows::Foundation;7using namespace Windows::Graphics::Display;89// Constructor.10Direct3DBase::Direct3DBase()11{12}1314// Initialize the Direct3D resources required to run.15void Direct3DBase::Initialize()16{17CreateDeviceResources();18}1920// These are the resources that depend on the device.21void Direct3DBase::CreateDeviceResources()22{23// This flag adds support for surfaces with a different color channel ordering24// than the API default. It is required for compatibility with Direct2D.25UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;2627#if defined(_DEBUG)28// If the project is in a debug build, enable debugging via SDK Layers with this flag.29creationFlags |= D3D11_CREATE_DEVICE_DEBUG;30#endif3132// This array defines the set of DirectX hardware feature levels this app will support.33// Note the ordering should be preserved.34// Don't forget to declare your application's minimum required feature level in its35// description. All applications are assumed to support 9.1 unless otherwise stated.36D3D_FEATURE_LEVEL featureLevels[] =37{38D3D_FEATURE_LEVEL_11_1,39D3D_FEATURE_LEVEL_11_0,40D3D_FEATURE_LEVEL_10_1,41D3D_FEATURE_LEVEL_10_0,42D3D_FEATURE_LEVEL_9_343};4445// Create the Direct3D 11 API device object and a corresponding context.46ComPtr<ID3D11Device> device;47ComPtr<ID3D11DeviceContext> context;48DX::ThrowIfFailed(49D3D11CreateDevice(50nullptr, // Specify nullptr to use the default adapter.51D3D_DRIVER_TYPE_HARDWARE,52nullptr,53creationFlags, // Set set debug and Direct2D compatibility flags.54featureLevels, // List of feature levels this app can support.55ARRAYSIZE(featureLevels),56D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION.57&device, // Returns the Direct3D device created.58&m_featureLevel, // Returns feature level of device created.59&context // Returns the device immediate context.60)61);6263// Get the Direct3D 11.1 API device and context interfaces.64DX::ThrowIfFailed(65device.As(&m_d3dDevice)66);6768DX::ThrowIfFailed(69context.As(&m_d3dContext)70);71}7273// Allocate all memory resources that depend on the window size.74void Direct3DBase::CreateWindowSizeDependentResources()75{76// Create a descriptor for the render target buffer.77CD3D11_TEXTURE2D_DESC renderTargetDesc(78DXGI_FORMAT_B8G8R8A8_UNORM,79static_cast<UINT>(m_renderTargetSize.Width),80static_cast<UINT>(m_renderTargetSize.Height),811,821,83D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE84);85renderTargetDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX | D3D11_RESOURCE_MISC_SHARED_NTHANDLE;8687// Allocate a 2-D surface as the render target buffer.88DX::ThrowIfFailed(89m_d3dDevice->CreateTexture2D(90&renderTargetDesc,91nullptr,92&m_renderTarget93)94);9596DX::ThrowIfFailed(97m_d3dDevice->CreateRenderTargetView(98m_renderTarget.Get(),99nullptr,100&m_renderTargetView101)102);103104// Create a depth stencil view.105CD3D11_TEXTURE2D_DESC depthStencilDesc(106DXGI_FORMAT_D24_UNORM_S8_UINT,107static_cast<UINT>(m_renderTargetSize.Width),108static_cast<UINT>(m_renderTargetSize.Height),1091,1101,111D3D11_BIND_DEPTH_STENCIL112);113114ComPtr<ID3D11Texture2D> depthStencil;115DX::ThrowIfFailed(116m_d3dDevice->CreateTexture2D(117&depthStencilDesc,118nullptr,119&depthStencil120)121);122123CD3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc(D3D11_DSV_DIMENSION_TEXTURE2D);124DX::ThrowIfFailed(125m_d3dDevice->CreateDepthStencilView(126depthStencil.Get(),127&depthStencilViewDesc,128&m_depthStencilView129)130);131132// Set the rendering viewport to target the entire window.133CD3D11_VIEWPORT viewport(1340.0f,1350.0f,136m_renderTargetSize.Width,137m_renderTargetSize.Height138);139140m_d3dContext->RSSetViewports(1, &viewport);141}142143void Direct3DBase::UpdateForRenderResolutionChange(float width, float height)144{145m_renderTargetSize.Width = width;146m_renderTargetSize.Height = height;147148ID3D11RenderTargetView* nullViews[] = {nullptr};149m_d3dContext->OMSetRenderTargets(ARRAYSIZE(nullViews), nullViews, nullptr);150m_renderTarget = nullptr;151m_renderTargetView = nullptr;152m_depthStencilView = nullptr;153m_d3dContext->Flush();154CreateWindowSizeDependentResources();155}156157void Direct3DBase::UpdateForWindowSizeChange(float width, float height)158{159m_windowBounds.Width = width;160m_windowBounds.Height = height;161}162163164