forked from hoffstadt/DearPyGui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmvGraphics_win32.cpp
206 lines (164 loc) · 5.17 KB
/
mvGraphics_win32.cpp
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "mvGraphics.h"
#include "mvWindowsSpecifics.h"
static std::vector <IDXGIAdapter*>
EnumerateAdapters()
{
IDXGIAdapter* pAdapter;
std::vector <IDXGIAdapter*> vAdapters;
IDXGIFactory* pFactory = NULL;
// Create a DXGIFactory object.
if (FAILED(CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&pFactory)))
{
return vAdapters;
}
for (UINT i = 0;
pFactory->EnumAdapters(i, &pAdapter) != DXGI_ERROR_NOT_FOUND;
++i)
{
vAdapters.push_back(pAdapter);
}
if (pFactory)
{
pFactory->Release();
}
return vAdapters;
}
mvGraphics
setup_graphics(mvViewport& viewport)
{
mvGraphics graphics{};
graphics.backendSpecifics = new mvGraphics_D3D11();
mvViewportData* viewportData = (mvViewportData*)viewport.platformSpecifics;
mvGraphics_D3D11* graphicsData = (mvGraphics_D3D11*)graphics.backendSpecifics;
// Setup swap chain
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));
sd.BufferCount = 2;
sd.BufferDesc.Width = 0;
sd.BufferDesc.Height = 0;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = viewportData->handle;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
auto adapters = EnumerateAdapters();
UINT createDeviceFlags = 0;
D3D_FEATURE_LEVEL featureLevel;
const D3D_FEATURE_LEVEL featureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, };
// use default adapter
if (GContext->IO.info_auto_device)
{
int index = 0;
SIZE_T adapterMemory = 0;
for (int i = 0; i < adapters.size(); i++)
{
DXGI_ADAPTER_DESC adpdesc;
adapters[i]->GetDesc(&adpdesc);
if (adpdesc.DedicatedVideoMemory > adapterMemory)
{
adapterMemory = adpdesc.DedicatedVideoMemory;
index = i;
}
}
if (D3D11CreateDeviceAndSwapChain(adapters[index], D3D_DRIVER_TYPE_UNKNOWN, nullptr,
createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &graphicsData->swapChain,
&graphicsData->device, &featureLevel, &graphicsData->deviceContext) != S_OK)
return graphics;
}
else if (GContext->IO.info_device == -1)
{
if (D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr,
createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &graphicsData->swapChain,
&graphicsData->device, &featureLevel, &graphicsData->deviceContext) != S_OK)
return graphics;
}
else
{
if (D3D11CreateDeviceAndSwapChain(adapters[GContext->IO.info_device], D3D_DRIVER_TYPE_UNKNOWN, nullptr,
createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &graphicsData->swapChain,
&graphicsData->device, &featureLevel, &graphicsData->deviceContext) != S_OK)
return graphics;
}
// create render target
graphicsData->swapChain->GetBuffer(0, IID_PPV_ARGS(&graphicsData->backBuffer));
graphicsData->device->CreateRenderTargetView(graphicsData->backBuffer, nullptr, &graphicsData->target);
graphics.ok = true;
ImGui_ImplDX11_Init(graphicsData->device, graphicsData->deviceContext);
return graphics;
}
void
cleanup_graphics(mvGraphics& graphics)
{
mvGraphics_D3D11* graphicsData = (mvGraphics_D3D11*)graphics.backendSpecifics;
ImGui_ImplDX11_Shutdown();
if (graphicsData->target)
{
graphicsData->target->Release();
graphicsData->target = nullptr;
}
if (graphicsData->backBuffer)
{
graphicsData->backBuffer->Release();
graphicsData->backBuffer = nullptr;
}
if (graphicsData->swapChain)
{
graphicsData->swapChain->Release();
graphicsData->swapChain = nullptr;
}
if (graphicsData->deviceContext)
{
graphicsData->deviceContext->Release();
graphicsData->deviceContext = nullptr;
}
if (graphicsData->device)
{
graphicsData->device->Release();
graphicsData->device = nullptr;
}
delete graphicsData;
graphicsData = nullptr;
}
void
resize_swapchain(mvGraphics& graphics, int width, int height)
{
mvGraphics_D3D11* graphicsData = (mvGraphics_D3D11*)graphics.backendSpecifics;
if (graphicsData->target)
{
graphicsData->target->Release();
graphicsData->target = nullptr;
}
if (graphicsData->backBuffer)
{
graphicsData->backBuffer->Release();
graphicsData->backBuffer = nullptr;
}
graphicsData->swapChain->ResizeBuffers(0, (UINT)width, (UINT)height, DXGI_FORMAT_UNKNOWN, 0);
// recreate render target
graphicsData->swapChain->GetBuffer(0, IID_PPV_ARGS(&graphicsData->backBuffer));
graphicsData->device->CreateRenderTargetView(graphicsData->backBuffer, nullptr, &graphicsData->target);
}
void
present(mvGraphics& graphics, mvColor& clearColor, bool vsync)
{
MV_PROFILE_SCOPE("Presentation")
mvGraphics_D3D11* graphicsData = (mvGraphics_D3D11*)graphics.backendSpecifics;
// Rendering
ImGui::Render();
graphicsData->deviceContext->OMSetRenderTargets(1, &graphicsData->target, nullptr);
graphicsData->deviceContext->ClearRenderTargetView(graphicsData->target, clearColor);
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
static UINT presentFlags = 0;
if (graphicsData->swapChain->Present(vsync ? 1 : 0, presentFlags) == DXGI_STATUS_OCCLUDED)
{
presentFlags = DXGI_PRESENT_TEST;
Sleep(20);
}
else
presentFlags = 0;
}