-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDXRender.cpp
294 lines (231 loc) · 7.02 KB
/
DXRender.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "DXRender.hpp"
ID3D11Device *DXRender::GetDevice()
{
return m_pDevice;
}
ID3D11DeviceContext *DXRender::GetDeviceContext()
{
return m_pDeviceContext;
}
HRESULT DXRender::ChangeRasterizerStateToWireframe()
{
HRESULT hr;
D3D11_RASTERIZER_DESC wfdesc;
ZeroMemory(&wfdesc, sizeof(D3D11_RASTERIZER_DESC));
wfdesc.FillMode = D3D11_FILL_WIREFRAME;
wfdesc.CullMode = D3D11_CULL_NONE;
// Ñîçäàåì ðàñòåðèçàòîð ñ äàííûìè íàñòðîéêàìè
hr = m_pDevice->CreateRasterizerState(&wfdesc, &m_pRasterizerState);
// Âêëþ÷àåì óêàçàííûå íàñòðîéêè ðàñòåðèçàöèè
m_pDeviceContext->RSSetState(m_pRasterizerState);
return hr;
}
void DXRender::InitViewport(HWND hWnd)
{
RECT rc;
GetClientRect(hWnd, &rc);
UINT width = rc.right - rc.left;
UINT height = rc.bottom - rc.top;
D3D11_VIEWPORT vp;
vp.Width = (FLOAT)width;
vp.Height = (FLOAT)height;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
/* connect viewport to device context */
UINT countViewports = 1;
m_pDeviceContext->RSSetViewports(countViewports, &vp);
}
HRESULT DXRender::CreateBackBuffer()
{
/* front buffer - RenderTargetOutput */
/* back buffer - RenderTargetView */
ID3D11Texture2D* pBackBuffer = NULL;
HRESULT hr = m_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
if (FAILED(hr)) {
printf("Error: cannot create backbuffer\n");
return hr;
}
hr = m_pDevice->CreateRenderTargetView(pBackBuffer, NULL, &m_pRenderTargetView);
pBackBuffer->Release(); /* now that's object does not needed */
if (FAILED(hr)) {
printf("Error: cannot create render target view\n");
return hr;
}
return hr;
}
HRESULT DXRender::ChangeRasterizerStateToSolid()
{
HRESULT hr;
D3D11_RASTERIZER_DESC solidDesc;
ZeroMemory(&solidDesc, sizeof(D3D11_RASTERIZER_DESC));
solidDesc.FillMode = D3D11_FILL_SOLID;
solidDesc.CullMode = D3D11_CULL_FRONT;
// Ñîçäàåì ðàñòåðèçàòîð ñ äàííûìè íàñòðîéêàìè
hr = m_pDevice->CreateRasterizerState(&solidDesc, &m_pRasterizerState);
// Âêëþ÷àåì óêàçàííûå íàñòðîéêè ðàñòåðèçàöèè
m_pDeviceContext->RSSetState(m_pRasterizerState);
return hr;
}
HRESULT DXRender::CreateDepthStencil(HWND hWnd)
{
HRESULT hr;
RECT rc;
GetClientRect(hWnd, &rc);
UINT width = rc.right - rc.left;
UINT height = rc.bottom - rc.top;
// Ñîçäàåì òåêñòóðó-îïèñàíèå áóôåðà ãëóáèí
D3D11_TEXTURE2D_DESC descDepth;
ZeroMemory(&descDepth, sizeof(descDepth));
descDepth.Width = width; // øèðèíà è
descDepth.Height = height; // âûñîòà òåêñòóðû
descDepth.MipLevels = 1; // óðîâåíü èíòåðïîëÿöèè
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; // ôîðìàò (ðàçìåð ïèêñåëÿ)
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL; // âèä - áóôåð ãëóáèí
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;
// Ïðè ïîìîùè çàïîëíåííîé ñòðóêòóðû-îïèñàíèÿ ñîçäàåì îáúåêò òåêñòóðû
hr = m_pDevice->CreateTexture2D(&descDepth, NULL, &m_pDepthStencil);
if (FAILED(hr))
return hr;
// Òåïåðü íàäî ñîçäàòü ñàì îáúåêò áóôåðà ãëóáèí
D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
ZeroMemory(&descDSV, sizeof(descDSV));
descDSV.Format = descDepth.Format; // ôîðìàò êàê â òåêñòóðå
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;
// Ïðè ïîìîùè çàïîëíåííîé ñòðóêòóðû-îïèñàíèÿ è òåêñòóðû ñîçäàåì îáúåêò áóôåðà ãëóáèí
hr = m_pDevice->CreateDepthStencilView(m_pDepthStencil, &descDSV, &m_pDepthStencilView);
return hr;
}
HRESULT DXRender::CreateBlendState()
{
HRESULT hr;
D3D11_RENDER_TARGET_BLEND_DESC rtbd;
ZeroMemory(&rtbd, sizeof(rtbd));
rtbd.BlendEnable = TRUE;
rtbd.SrcBlend = D3D11_BLEND_SRC_ALPHA;
rtbd.DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
rtbd.BlendOp = D3D11_BLEND_OP_ADD;
rtbd.SrcBlendAlpha = D3D11_BLEND_ONE;
rtbd.DestBlendAlpha = D3D11_BLEND_ZERO;
rtbd.BlendOpAlpha = D3D11_BLEND_OP_ADD;
rtbd.RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
D3D11_BLEND_DESC blendDesc;
ZeroMemory(&blendDesc, sizeof(blendDesc));
blendDesc.RenderTarget[0] = rtbd;
hr = m_pDevice->CreateBlendState(&blendDesc, &m_pBlendStateTransparency);
return hr;
}
HRESULT DXRender::Init(HWND hWnd, bool vsync)
{
m_vsync = vsync;
RECT rc;
GetClientRect(hWnd, &rc);
UINT width = rc.right - rc.left;
UINT height = rc.bottom - rc.top;
/* properties front buffer and attach it to window */
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));
sd.BufferCount = 1; /* counts buffer = 1 */
sd.BufferDesc.Width = width; /* buffer width */
sd.BufferDesc.Height = height; /* buffer height */
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; /* pixel format in buffer */
sd.BufferDesc.RefreshRate.Numerator = 60; /* screen refresh rate */
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; /* target is back buffer */
sd.OutputWindow = hWnd; // Set window
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE; // Window mode
D3D_FEATURE_LEVEL featureLevels[] = {
D3D_FEATURE_LEVEL_11_0
};
UINT arraySize = ARRAYSIZE(featureLevels);
HRESULT hr = D3D11CreateDeviceAndSwapChain(
NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
0,
featureLevels,
arraySize,
D3D11_SDK_VERSION,
&sd,
&m_pSwapChain,
&m_pDevice,
NULL,
&m_pDeviceContext
);
if (FAILED(hr)) {
printf("Error: cannot CreateDeviceAndSwapChain\n");
return hr;
}
hr = CreateBackBuffer();
if (FAILED(hr)) {
printf("Error: cannot CreateBackBuffer\n");
return hr;
}
hr = CreateDepthStencil(hWnd);
if (FAILED(hr)) {
printf("Error: cannot CreateDepthStencil\n");
return hr;
}
hr = CreateBlendState();
if (FAILED(hr)) {
printf("Error: cannot CreateBlendState\n");
return hr;
}
/* connect back buffer to device context */
m_pDeviceContext->OMSetRenderTargets(1, &m_pRenderTargetView, m_pDepthStencilView);
InitViewport(hWnd);
hr = ChangeRasterizerStateToSolid();
if (FAILED(hr)) {
printf("Error: cannot ChangeRasterizerStateToSolid\n");
return hr;
}
return hr;
}
void DXRender::Cleanup()
{
if (m_pDeviceContext)
m_pDeviceContext->ClearState();
if (m_pDepthStencilView)
m_pDepthStencilView->Release();
if (m_pRenderTargetView)
m_pRenderTargetView->Release();
if (m_pSwapChain)
m_pSwapChain->Release();
if (m_pRasterizerState)
m_pRasterizerState->Release();
if (m_pBlendStateTransparency)
m_pBlendStateTransparency->Release();
if (m_pDeviceContext)
m_pDeviceContext->Release();
if (m_pDevice)
m_pDevice->Release();
}
void DXRender::RenderStart()
{
// Î÷èùàåì çàäíèé áóôåð
float clearColor[4] = { 0.49804f, 0.78431f, 0.94510f, 1.0f };
m_pDeviceContext->ClearRenderTargetView(m_pRenderTargetView, clearColor);
// Î÷èùàåì áóôåð ãëóáèí äî åäèöèíû (ìàêñèìàëüíàÿ ãëóáèíà)
m_pDeviceContext->ClearDepthStencilView(m_pDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);
float blendFactor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
UINT sampleMask = 0xffffffff;
m_pDeviceContext->OMSetBlendState(m_pBlendStateTransparency, NULL, sampleMask);
}
void DXRender::RenderEnd()
{
if (m_vsync) {
m_pSwapChain->Present(1, 0);
}
else {
m_pSwapChain->Present(0, 0);
}
}