-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphics.cpp
More file actions
451 lines (375 loc) · 12.2 KB
/
Graphics.cpp
File metadata and controls
451 lines (375 loc) · 12.2 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include "Graphics.h"
#include <sstream>
#include <cmath>
#include "GraphicsMacros.h"
#include "imgui-1.88/imgui_impl_dx11.h"
#include "imgui-1.88/imgui_impl_win32.h"
namespace wrl = Microsoft::WRL;
namespace dx = DirectX;
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "D3DCompiler.lib")
Graphics::Graphics(HWND hWnd) {
DXGI_SWAP_CHAIN_DESC sd = {};
sd.BufferDesc.Width = 0;
sd.BufferDesc.Height = 0;
sd.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 0;
sd.BufferDesc.RefreshRate.Denominator = 0;
sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.BufferCount = 2;
sd.OutputWindow = hWnd;
sd.Windowed = TRUE;
sd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
sd.Flags = 0;
UINT swapCreateFlags = 0u;
#ifndef NDEBUG
swapCreateFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
HRESULT hr;
// Creates device with swap chain(front and back buffer) with render context
GFX_THROW_INFO(D3D11CreateDeviceAndSwapChain(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
swapCreateFlags,
nullptr,
0,
D3D11_SDK_VERSION,
&sd,
&pSwap,
&pDevice,
nullptr,
&pContext
));
wrl::ComPtr<ID3D11Resource> pBackBuffer;
GFX_THROW_INFO(pSwap->GetBuffer(0, __uuidof(ID3D11Resource), &pBackBuffer));
if (pBackBuffer != nullptr) {
GFX_THROW_INFO(pDevice->CreateRenderTargetView(
pBackBuffer.Get(),
nullptr,
&pTarget
));
}
else {
throw GFX_EXCEPT((E_INVALIDARG));
}
// Creates depth stencil state
D3D11_DEPTH_STENCIL_DESC dsDesc = {};
dsDesc.DepthEnable = TRUE;
dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
dsDesc.DepthFunc = D3D11_COMPARISON_LESS;
wrl::ComPtr<ID3D11DepthStencilState> pDSState;
GFX_THROW_INFO(pDevice->CreateDepthStencilState(&dsDesc, &pDSState));
// Bind Depth State
pContext->OMSetDepthStencilState(pDSState.Get(), 1u);
// Create depth stencil texture
wrl::ComPtr<ID3D11Texture2D> pDepthStencil;
D3D11_TEXTURE2D_DESC descDepth = {};
descDepth.Width = 800u;
descDepth.Height = 600u;
descDepth.MipLevels = 1u;
descDepth.ArraySize = 1u;
descDepth.Format = DXGI_FORMAT_D32_FLOAT;
descDepth.SampleDesc.Count = 1u;
descDepth.SampleDesc.Quality = 0u;
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
GFX_THROW_INFO(pDevice->CreateTexture2D(&descDepth, nullptr, &pDepthStencil));
// Grab view of the depth stencil texture so we can add it to the pipeline
D3D11_DEPTH_STENCIL_VIEW_DESC descDSV = {};
descDSV.Format = DXGI_FORMAT_D32_FLOAT;
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0u;
GFX_THROW_INFO(pDevice->CreateDepthStencilView(pDepthStencil.Get(), &descDSV, &pDSV));
// Attach view to pipeline (specifically Output Merger)
pContext->OMSetRenderTargets(1u, pTarget.GetAddressOf(), pDSV.Get());
// configure viewport
D3D11_VIEWPORT vp;
vp.Width = 800.0f;
vp.Height = 600.0f;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0.0f;
vp.TopLeftY = 0.0f;
pContext->RSSetViewports(1u, &vp);
ImGui_ImplDX11_Init(pDevice.Get(), pContext.Get());
}
Graphics::~Graphics() {
ImGui_ImplDX11_Shutdown();
}
void Graphics::EndFrame() {
if (imguiEnabled) {
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
}
HRESULT hr;
#ifdef NDEBUG
infoManager.Set();
#endif
if (FAILED(hr = pSwap->Present(1u, 0u))) {
if (hr == DXGI_ERROR_DEVICE_REMOVED) {
throw GFX_DEVICE_REMOVED_EXCEPT(pDevice->GetDeviceRemovedReason());
}
else {
throw GFX_EXCEPT(hr);
}
}
}
void Graphics::BeginFrame(float red, float green, float blue) noexcept {
if (imguiEnabled) {
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
}
const float color[] = { red, green, blue, 1.0f };
pContext->ClearRenderTargetView(pTarget.Get(), color);
pContext->ClearDepthStencilView(pDSV.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0u);
}
void Graphics::DrawIndexed(UINT count) noexcept(!IS_DEBUG) {
HRESULT hr;
//struct Vertex
//{
// struct {
// float x;
// float y;
// float z;
// } pos;
// /*struct {
// unsigned char r;
// unsigned char g;
// unsigned char b;
// unsigned char a;
// } color;*/
//
//};
////Create Vertex Buffer (2d Triangle)
//const Vertex vertices[] = {
// {-1.0f, -1.0f, -1.0f},
// {1.0f, -1.0f, -1.0f},
// {-1.0f, 1.0f, -1.0f},
// {1.0f, 1.0f, -1.0f},
// {-1.0f, -1.0f, 1.0f},
// {1.0f, -1.0f, 1.0f},
// {-1.0f, 1.0f, 1.0f},
// {1.0f, 1.0f, 1.0f}
//
//};
//wrl::ComPtr<ID3D11Buffer> pVertexBuffer;
//D3D11_BUFFER_DESC bd = {};
//bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
//bd.Usage = D3D11_USAGE_DEFAULT;
//bd.CPUAccessFlags = 0u;
//bd.MiscFlags = 0u;
//bd.ByteWidth = sizeof(vertices);
//bd.StructureByteStride = sizeof(Vertex);
//D3D11_SUBRESOURCE_DATA sd = {};
//sd.pSysMem = vertices;
//GFX_THROW_INFO(pDevice->CreateBuffer(&bd, &sd, &pVertexBuffer));
//// Attach Vertex Buffer to Pipeline
//const UINT stride = sizeof(Vertex);
//const UINT offset = 0u;
//pContext->IASetVertexBuffers(0u,1u, pVertexBuffer.GetAddressOf(), &stride, &offset);
//// Create Index Buffer (Used for reusing predfined vertices for multiple triangles)
//const unsigned short indices[] = {
// 0,2,1, 2,3,1,
// 1,3,5, 3,7,5,
// 2,6,3, 3,6,7,
// 4,5,7, 4,7,6,
// 0,4,2, 2,4,6,
// 0,1,4, 1,5,4,
//};
//wrl::ComPtr<ID3D11Buffer> pIndexBuffer;
//D3D11_BUFFER_DESC ibd = {};
//ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
//ibd.Usage = D3D11_USAGE_DEFAULT;
//ibd.CPUAccessFlags = 0u;
//ibd.MiscFlags = 0u;
//ibd.ByteWidth = sizeof(indices);
//ibd.StructureByteStride = sizeof(unsigned short);
//D3D11_SUBRESOURCE_DATA isd = {};
//isd.pSysMem = indices;
//GFX_THROW_INFO(pDevice->CreateBuffer(&ibd, &isd, &pIndexBuffer));
//// Attach Index Buffer to Pipeline
//pContext->IASetIndexBuffer(pIndexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0u);
//// Create const buffer for transformation matrix (Do matrix transformation instead of moving sending a new vertex buffer)
//struct ConstantBuffer {
// dx::XMMATRIX transform;
//};
//const ConstantBuffer cb = {
// {
// dx::XMMatrixTranspose(
// dx::XMMatrixRotationZ(angle) *
// dx::XMMatrixRotationX(angle) *
// dx::XMMatrixTranslation(x, 0.0f, z + 4.0f) *
// dx::XMMatrixPerspectiveLH(1.0f, 3.0f/ 4.0f, 0.5f, 10.0f)
// )
// }
//};
//wrl::ComPtr<ID3D11Buffer> pConstantBuffer;
//D3D11_BUFFER_DESC cbd = {};
//cbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
//cbd.Usage = D3D11_USAGE_DYNAMIC;
//cbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
//cbd.MiscFlags = 0u;
//cbd.ByteWidth = sizeof(cb);
//cbd.StructureByteStride = 0u;
//D3D11_SUBRESOURCE_DATA csd = {};
//csd.pSysMem = &cb;
//GFX_THROW_INFO(pDevice->CreateBuffer(&cbd, &csd, &pConstantBuffer));
//// Attach const buffer to vertex shader
//pContext->VSSetConstantBuffers(0u, 1u, pConstantBuffer.GetAddressOf());
//// Create another constant buffer for tagging triangle indexes to colors (individual colors per cube face)
//struct ConstantBuffer2 {
// struct {
// float r;
// float g;
// float b;
// float a;
// }face_colors[6];
//};
//const ConstantBuffer2 cb2 = {
// {
// {1.0f, 0.0f, 1.0f},
// {1.0f, 0.0f, 0.0f},
// {0.0f, 1.0f, 0.0f},
// {0.0f, 0.0f, 1.0f},
// {1.0f, 1.0f, 0.0f},
// {0.0f, 1.0f, 1.0f},
// }
//};
//wrl::ComPtr<ID3D11Buffer> pConstantBuffer2;
//D3D11_BUFFER_DESC cbd2 = {};
//cbd2.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
//cbd2.Usage = D3D11_USAGE_DEFAULT;
//cbd2.CPUAccessFlags = 0u;
//cbd2.MiscFlags = 0u;
//cbd2.ByteWidth = sizeof(cb2);
//cbd2.StructureByteStride = 0u;
//D3D11_SUBRESOURCE_DATA csd2 = {};
//csd2.pSysMem = &cb2;
//GFX_THROW_INFO(pDevice->CreateBuffer(&cbd2, &csd2, &pConstantBuffer2));
//// Attach const buffer to vertex shader
//pContext->PSSetConstantBuffers(0u, 1u, pConstantBuffer2.GetAddressOf());
//// Create Pixel Shader
//wrl::ComPtr<ID3D11PixelShader> pPixelShader;
//wrl::ComPtr<ID3DBlob> pBlob;
//GFX_THROW_INFO(D3DReadFileToBlob(L"PixelShader.cso", &pBlob));
//GFX_THROW_INFO(pDevice->CreatePixelShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), nullptr, &pPixelShader));
//// Attach Pixel Shader
//pContext->PSSetShader(pPixelShader.Get(), nullptr, 0u);
//// Create Vertex Shader
//wrl::ComPtr<ID3D11VertexShader> pVertexShader;
//GFX_THROW_INFO(D3DReadFileToBlob(L"VertexShader.cso", &pBlob));
//GFX_THROW_INFO(pDevice->CreateVertexShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), nullptr, &pVertexShader));
//// Attach Vertex Shader to Pipeline
//pContext->VSSetShader(pVertexShader.Get(), nullptr, 0u);
//// Input (vertex) layout (2d position only)
//wrl::ComPtr<ID3D11InputLayout> pInputLayout;
//const D3D11_INPUT_ELEMENT_DESC ied[] = {
// // Reads positions as 2 4 byte values
// {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
// // Reads color as 4 byte values and normalizes them from 0 - 1
// // {"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0}
//};
//GFX_THROW_INFO(pDevice->CreateInputLayout(
// ied, (UINT)std::size(ied),
// pBlob->GetBufferPointer(),
// pBlob->GetBufferSize(),
// &pInputLayout
//));
//// Bind Input Layout
//pContext->IASetInputLayout(pInputLayout.Get());
// Bind Render Target
// **COME BACK TO THIS IN CASE ERROR**
pContext->OMSetRenderTargets(1u, pTarget.GetAddressOf(), pDSV.Get());
// Set primitive topology to triangle list (groups of verticies make multiple triangles)
//pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
GFX_THROW_INFO_ONLY(pContext->DrawIndexed(count, 0u, 0u));
}
void Graphics::SetProjecton(DirectX::FXMMATRIX proj) noexcept {
projection = proj;
}
DirectX::XMMATRIX Graphics::GetProjection() const noexcept {
return projection;
}
void Graphics::SetCamera(DirectX::FXMMATRIX camOri) noexcept {
camera = camOri;
}
DirectX::XMMATRIX Graphics::GetCamera() const noexcept {
return camera;
}
void Graphics::EnableImgui() noexcept {
imguiEnabled = true;
}
void Graphics::DisableImgui() noexcept {
imguiEnabled = false;
}
bool Graphics::IsImguiEnabled() const noexcept {
return imguiEnabled;
}
Graphics::HrException::HrException(int line, const char* file, HRESULT hr, std::vector<std::string> infoMsgs) noexcept : Exception(line,file), hr(hr) {
for (const auto& m : infoMsgs) {
info += m;
info.push_back('\n');
}
if (!info.empty()) {
info.pop_back();
}
}
const char* Graphics::HrException::what() const noexcept {
std::ostringstream oss;
oss << GetType() << std::endl
<< "[Error Code] 0x" << std::hex << std::uppercase << GetErrorCode()
<< std::dec << " (" << (unsigned long)GetErrorCode() << ")" << std::endl
<< "[Description] " << GetErrorDescription() << std::endl;
if (!info.empty()) {
oss << "\n[Error Info]\n" << GetErrorInfo() << std::endl << std::endl;
}
oss << GetOriginString();
whatBuffer = oss.str();
return whatBuffer.c_str();
}
std::string Graphics::HrException::GetErrorInfo() const noexcept {
return info;
}
const char* Graphics::HrException::GetType() const noexcept {
return "Graphics HResult Exception";
}
HRESULT Graphics::HrException::GetErrorCode() const noexcept { return hr; }
std::string Graphics::HrException::GetErrorDescription() const noexcept {
return TranslateErrorCode(hr);
}
const char* Graphics::DeviceRemovedException::GetType() const noexcept {
return "Graphics Device Removed Exception";
}
std::string Graphics::Exception::TranslateErrorCode(HRESULT hr) noexcept {
char* pMsgBuf = nullptr;
return std::system_category().message(hr);
}
Graphics::InfoException::InfoException(int line, const char* file, std::vector<std::string> infoMsgs) noexcept : Exception(line, file) {
for (const auto& m : infoMsgs) {
info += m;
info.push_back('\n');
}
if (!info.empty()) {
info.pop_back();
}
}
const char* Graphics::InfoException::what() const noexcept {
std::ostringstream oss;
oss << GetType() << std::endl
<< "\n[Error Info]\n" << GetErrorInfo() << std::endl << std::endl;
oss << GetOriginString();
whatBuffer = oss.str();
return whatBuffer.c_str();
}
const char* Graphics::InfoException::GetType() const noexcept {
return "Graphics Info Exception";
}
std::string Graphics::InfoException::GetErrorInfo() const noexcept {
return info;
}