1+ //
2+ // DeviceResources.h - A wrapper for the Direct3D 11 device and swapchain
3+ //
4+
5+ #pragma once
6+
7+ namespace DX
8+ {
9+ // Provides an interface for an application that owns DeviceResources to be notified of the device being lost or created.
10+ interface IDeviceNotify
11+ {
12+ virtual void OnDeviceLost () = 0 ;
13+ virtual void OnDeviceRestored () = 0 ;
14+ };
15+
16+ // Controls all the DirectX device resources.
17+ class DeviceResources
18+ {
19+ public:
20+ DeviceResources (DXGI_FORMAT backBufferFormat = DXGI_FORMAT_B8G8R8A8_UNORM,
21+ DXGI_FORMAT depthBufferFormat = DXGI_FORMAT_D24_UNORM_S8_UINT,
22+ UINT backBufferCount = 2 ,
23+ D3D_FEATURE_LEVEL minFeatureLevel = D3D_FEATURE_LEVEL_9_3);
24+
25+ void CreateDeviceResources ();
26+ void CreateWindowSizeDependentResources ();
27+ void SetWindow (IUnknown* window, int width, int height, DXGI_MODE_ROTATION rotation);
28+ bool WindowSizeChanged (int width, int height, DXGI_MODE_ROTATION rotation);
29+ void ValidateDevice ();
30+ void HandleDeviceLost ();
31+ void RegisterDeviceNotify (IDeviceNotify* deviceNotify) { m_deviceNotify = deviceNotify; }
32+ void Trim ();
33+ void Present ();
34+
35+ // Device Accessors.
36+ RECT GetOutputSize () const { return m_outputSize; }
37+ DXGI_MODE_ROTATION GetRotation () const { return m_rotation; }
38+
39+ // Direct3D Accessors.
40+ ID3D11Device2* GetD3DDevice () const { return m_d3dDevice.Get (); }
41+ ID3D11DeviceContext2* GetD3DDeviceContext () const { return m_d3dContext.Get (); }
42+ IDXGISwapChain3* GetSwapChain () const { return m_swapChain.Get (); }
43+ D3D_FEATURE_LEVEL GetDeviceFeatureLevel () const { return m_d3dFeatureLevel; }
44+ ID3D11RenderTargetView* GetBackBufferRenderTargetView () const { return m_d3dRenderTargetView.Get (); }
45+ ID3D11DepthStencilView* GetDepthStencilView () const { return m_d3dDepthStencilView.Get (); }
46+ DXGI_FORMAT GetBackBufferFormat () const { return m_backBufferFormat; }
47+ DXGI_FORMAT GetDepthBufferFormat () const { return m_depthBufferFormat; }
48+ D3D11_VIEWPORT GetScreenViewport () const { return m_screenViewport; }
49+ UINT GetBackBufferCount () const { return m_backBufferCount; }
50+ DirectX::XMFLOAT4X4 GetOrientationTransform3D () const { return m_orientationTransform3D; }
51+
52+ private:
53+ void GetHardwareAdapter (IDXGIAdapter1** ppAdapter);
54+
55+ // Direct3D objects.
56+ Microsoft::WRL::ComPtr<ID3D11Device3> m_d3dDevice;
57+ Microsoft::WRL::ComPtr<ID3D11DeviceContext2> m_d3dContext;
58+ Microsoft::WRL::ComPtr<IDXGISwapChain3> m_swapChain;
59+
60+ // Direct3D rendering objects. Required for 3D.
61+ Microsoft::WRL::ComPtr<ID3D11RenderTargetView> m_d3dRenderTargetView;
62+ Microsoft::WRL::ComPtr<ID3D11DepthStencilView> m_d3dDepthStencilView;
63+ D3D11_VIEWPORT m_screenViewport;
64+
65+ // Direct3D properties.
66+ DXGI_FORMAT m_backBufferFormat;
67+ DXGI_FORMAT m_depthBufferFormat;
68+ UINT m_backBufferCount;
69+ D3D_FEATURE_LEVEL m_d3dMinFeatureLevel;
70+
71+ // Cached device properties.
72+ IUnknown* m_window;
73+ D3D_FEATURE_LEVEL m_d3dFeatureLevel;
74+ DXGI_MODE_ROTATION m_rotation;
75+ RECT m_outputSize;
76+
77+ // Transforms used for display orientation.
78+ DirectX::XMFLOAT4X4 m_orientationTransform3D;
79+
80+ // The IDeviceNotify can be held directly as it owns the DeviceResources.
81+ IDeviceNotify* m_deviceNotify;
82+ };
83+ }
0 commit comments