aboutsummaryrefslogtreecommitdiff
path: root/samples/dual_layer/D3D11/src/DeviceManager11.h
blob: 8d1db18a0a3c86a0ccb54dace663dd99d2962399 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright (c) 2008-2018, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/

#pragma once
#include <Windows.h>
#include <DXGI.h>
#include <D3D11.h>
#include <list>
#include <string>

struct DeviceCreationParameters
{
    bool startMaximized;
    bool startFullscreen;
    int backBufferWidth;
    int backBufferHeight;
    int refreshRate;
    int swapChainBufferCount;
    DXGI_FORMAT swapChainFormat;
    DXGI_USAGE swapChainUsage;
    int swapChainSampleCount;
    int swapChainSampleQuality;
    D3D_DRIVER_TYPE driverType;
    D3D_FEATURE_LEVEL featureLevel;
    bool enableDebugRuntime;

    // For use in the case of multiple adapters.  If this is non-null, device creation will try to match
    // the given string against an adapter name.  If the specified string exists as a sub-string of the 
    // adapter name, the device and window will be created on that adapter.  Case sensitive.
    const WCHAR* adapterNameSubstring;

    DeviceCreationParameters()
        : startMaximized(false)
        , startFullscreen(false)
        , backBufferWidth(1280)
        , backBufferHeight(720)
        , refreshRate(0)
        , swapChainBufferCount(1)
        , swapChainFormat(DXGI_FORMAT_R8G8B8A8_UNORM)
        , swapChainUsage(DXGI_USAGE_SHADER_INPUT | DXGI_USAGE_RENDER_TARGET_OUTPUT)
        , swapChainSampleCount(1)
        , swapChainSampleQuality(0)
        , driverType(D3D_DRIVER_TYPE_HARDWARE)
        , featureLevel(D3D_FEATURE_LEVEL_11_0)
        , enableDebugRuntime(false)
        , adapterNameSubstring(L"")
    { }
};

typedef ID3D11RenderTargetView* RenderTargetView;

#pragma warning(push)
#pragma warning(disable: 4100) // unreferenced formal parameter
class IVisualController
{
private:
    bool            m_Enabled;
public:
    IVisualController() : m_Enabled(true) { }

    virtual LRESULT MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { return 1; }
    virtual void Render(RenderTargetView RTV) { }
    virtual void Animate(double fElapsedTimeSeconds) { }
    virtual HRESULT DeviceCreated() { return S_OK; }
    virtual void DeviceDestroyed() { }
    virtual void BackBufferResized(uint32_t width, uint32_t height, uint32_t sampleCount) { }

    virtual void EnableController() { m_Enabled = true; }
    virtual void DisableController() { m_Enabled = false; }
    virtual bool IsEnabled() { return m_Enabled; }
};
#pragma warning(pop)

class DeviceManager
{
public:
    enum WindowState
    {
        kWindowNone,
        kWindowNormal,
        kWindowMinimized,
        kWindowMaximized,
        kWindowFullscreen
    };

protected:
    ID3D11Device*           m_Device;
    ID3D11DeviceContext*    m_ImmediateContext;
    IDXGISwapChain*         m_SwapChain;
    ID3D11RenderTargetView* m_BackBufferRTV;
    DXGI_SWAP_CHAIN_DESC    m_SwapChainDesc;
    bool					m_IsNvidia;
    HWND                    m_hWnd;
    std::list<IVisualController*> m_vControllers;
    std::wstring            m_WindowTitle;
    double                  m_FixedFrameInterval;
    UINT                    m_SyncInterval;
    std::list<double>       m_vFrameTimes;
    double                  m_AverageFrameTime;
    double                  m_AverageTimeUpdateInterval;
    bool                    m_InSizingModalLoop;
    SIZE                    m_NewWindowSize;
    bool                    m_ShutdownCalled;
    bool                    m_EnableRenderTargetClear;
    float                   m_RenderTargetClearColor[4];
private:
    HRESULT                 CreateRenderTarget();
    void                    ResizeSwapChain();
public:

    DeviceManager()
        : m_Device(NULL)
        , m_ImmediateContext(NULL)
        , m_SwapChain(NULL)
        , m_BackBufferRTV(NULL)
        , m_IsNvidia(false)
        , m_hWnd(NULL)
        , m_WindowTitle(L"")
        , m_FixedFrameInterval(-1)
        , m_SyncInterval(0)
        , m_AverageFrameTime(0)
        , m_AverageTimeUpdateInterval(0.5)
        , m_InSizingModalLoop(false)
        , m_ShutdownCalled(false)
    { }

    virtual ~DeviceManager()
    {
        Shutdown();
    }

    virtual HRESULT CreateWindowDeviceAndSwapChain(const DeviceCreationParameters& params, LPWSTR windowTitle);
    virtual HRESULT ChangeBackBufferFormat(DXGI_FORMAT format, UINT sampleCount);
    virtual HRESULT ResizeWindow(int width, int height);
    virtual HRESULT EnterFullscreenMode(int width = 0, int height = 0);
    virtual HRESULT LeaveFullscreenMode(int windowWidth = 0, int windowHeight = 0);
    virtual HRESULT ToggleFullscreen();

    virtual void    Shutdown();
    virtual void    MessageLoop();
    virtual LRESULT MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    virtual void    Render();
    virtual void    Animate(double fElapsedTimeSeconds);
    virtual void    DeviceCreated();
    virtual void    DeviceDestroyed();
    virtual void    BackBufferResized();

    void            AddControllerToFront(IVisualController* pController);
    void            AddControllerToBack(IVisualController* pController);
    void            RemoveController(IVisualController* pController);

    void            SetFixedFrameInterval(double seconds) { m_FixedFrameInterval = seconds; }
    void            DisableFixedFrameInterval() { m_FixedFrameInterval = -1; }

    bool			IsNvidia() const { return m_IsNvidia; }
    HWND            GetHWND() { return m_hWnd; }
    ID3D11Device*   GetDevice() { return m_Device; }
    ID3D11DeviceContext* GetImmediateContext() { return m_ImmediateContext; }
    WindowState     GetWindowState();
    bool            GetVsyncEnabled() { return m_SyncInterval > 0; }
    void            SetVsyncEnabled(bool enabled) { m_SyncInterval = enabled ? 1 : 0; }
    HRESULT         GetDisplayResolution(int& width, int& height);
    IDXGIAdapter*   GetDXGIAdapter();
    double          GetAverageFrameTime() { return m_AverageFrameTime; }
    void            SetAverageTimeUpdateInterval(double value) { m_AverageTimeUpdateInterval = value; }
    void            SetPrimaryRenderTargetClearColor(bool enableClear, const float* pColor);
};