-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphics.cpp
More file actions
289 lines (215 loc) · 8.91 KB
/
Graphics.cpp
File metadata and controls
289 lines (215 loc) · 8.91 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
#include "Graphics.h"
bool Graphics::Initialize(HWND handle, int width, int height) {
if (!InitializeDirectX(handle, width, height))
return false;
if (!InitializeShaders())
return false;
if (!InitializeScene())
return false;
return true;
}
static float bufferOffset = 0.0f;
void Graphics::RenderFrame() {
float bgColor[] = { .0f, .0f, .0f, 1.0f };
this->deviceContext->ClearRenderTargetView(this->renderTargetView.Get(), bgColor);
this->deviceContext->ClearDepthStencilView(this->depthStencilView.Get(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.f, 0);
this->deviceContext->IASetInputLayout(this->vertexShader.GetInputLayout());
this->deviceContext->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
this->deviceContext->RSSetState(this->rasterizerState.Get());
this->deviceContext->OMSetDepthStencilState(this->depthStencilState.Get(), 0);
this->deviceContext->PSSetSamplers(0, 1, this->samplerState.GetAddressOf());
this->deviceContext->VSSetShader(vertexShader.GetShader(), 0, 0);
this->deviceContext->PSSetShader(pixelShader.GetShader(), 0, 0);
UINT offset = 0;
bufferOffset += 0.02;
constantBuffer.data.xOffset = cosf(bufferOffset) * .5f, constantBuffer.data.yOffset = sinf(bufferOffset) * .5f;
if (!constantBuffer.Update())
return;
this->deviceContext->VSSetConstantBuffers(0, 1, constantBuffer.GetAddressOf());
// Setting texture in shader
this->deviceContext->PSSetShaderResources(0, 1, this->myTexture.GetAddressOf());
// FIRST
this->deviceContext->IASetVertexBuffers(0, 1, this->vertexBuffer.GetAddressOf(), vertexBuffer.StridePtr(), &offset);
this->deviceContext->IASetIndexBuffer(indexBuffer.Get(), DXGI_FORMAT_R32_UINT, 0);
this->deviceContext->DrawIndexed(indexBuffer.BufferSize(), 0, 0);
// Text rendering
spriteBatch->Begin();
spriteFont->DrawString(spriteBatch.get(), L"IncredibleX Game Engine", DirectX::XMFLOAT2(0, 0), DirectX::Colors::White, 0.0f, DirectX::XMFLOAT2(0, 0), DirectX::XMFLOAT2(1.0f, 1.0f));
spriteBatch->End();
this->swapChain->Present(1, 0);
}
bool Graphics::InitializeDirectX(HWND handle, int width, int height) {
std::vector<AdapterData> adapters = AdapterReader::GetAdapterData();
if (adapters.size() < 1) {
Logger::Log("No IDXGI Adapters found.");
return false;
}
D3D_DRIVER_TYPE driverTypes[] = {
D3D_DRIVER_TYPE_HARDWARE, // Best driver
D3D_DRIVER_TYPE_WARP, // CPU
D3D_DRIVER_TYPE_REFERENCE // Worst
};
D3D_FEATURE_LEVEL featureLevels[] = { // For latest features
D3D_FEATURE_LEVEL_11_0,
};
UINT driverCount = ARRAYSIZE(driverTypes);
UINT featureCount = ARRAYSIZE(featureLevels);
DXGI_SWAP_CHAIN_DESC scd;
ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
scd.BufferDesc.Width = width;
scd.BufferDesc.Height = height;
scd.BufferDesc.RefreshRate.Numerator = 60;
scd.BufferDesc.RefreshRate.Denominator = 1;
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // For RGBA
scd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
scd.SampleDesc.Count = 1;
scd.SampleDesc.Quality = 0;
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
scd.BufferCount = 1;
scd.OutputWindow = handle;
scd.Windowed = TRUE;
scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
HRESULT result;
for (int i = 0; i < driverCount;) {
result = D3D11CreateDeviceAndSwapChain(0, driverTypes[i], 0, 0, featureLevels, featureCount,
D3D11_SDK_VERSION, &scd, this->swapChain.GetAddressOf(),
this->device.GetAddressOf(), &featureLevel, this->deviceContext.GetAddressOf());
// If first driver succeeds, break out of this loop.
if (SUCCEEDED(result))
break;
++i;
}
Microsoft::WRL::ComPtr<ID3D11Texture2D> backBuffer;
result = this->swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(backBuffer.GetAddressOf()));
if (FAILED(result)) {
Logger::Log(result, "Failed to get backbuffer.");
return false;
}
result = this->device->CreateRenderTargetView(backBuffer.Get(), 0, this->renderTargetView.GetAddressOf());
if (FAILED(result)) {
Logger::Log(result, "Failed to create render target view.");
return false;
}
D3D11_TEXTURE2D_DESC depthStencilDesc;
depthStencilDesc.Width = width;
depthStencilDesc.Height = height;
depthStencilDesc.MipLevels = 1;
depthStencilDesc.ArraySize = 1;
depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilDesc.SampleDesc.Count = 1;
depthStencilDesc.SampleDesc.Quality = 0;
depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthStencilDesc.CPUAccessFlags = 0;
depthStencilDesc.MiscFlags = 0;
result = this->device->CreateTexture2D(&depthStencilDesc, 0, this->depthStencilBuffer.GetAddressOf());
if (FAILED(result)) {
Logger::Log(result, "Failed to create depth stencil buffer.");
return false;
}
result = this->device->CreateDepthStencilView(this->depthStencilBuffer.Get(), 0, this->depthStencilView.GetAddressOf());
if (FAILED(result)) {
Logger::Log(result, "Failed to create depth stencil buffer.");
return false;
}
this->deviceContext->OMSetRenderTargets(1, this->renderTargetView.GetAddressOf(), this->depthStencilView.Get());
// Create depth stencil state.
D3D11_DEPTH_STENCIL_DESC depthStencilStateDesc;
ZeroMemory(&depthStencilStateDesc, sizeof(D3D11_DEPTH_STENCIL_DESC));
depthStencilStateDesc.DepthEnable = true;
depthStencilStateDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilStateDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL;
result = this->device->CreateDepthStencilState(&depthStencilStateDesc, this->depthStencilState.GetAddressOf());
if (FAILED(result)) {
Logger::Log(result, "Failed to create depth stencil state.");
return false;
}
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
viewport.TopLeftX = 0, viewport.TopLeftY = 0;
viewport.Width = width, viewport.Height = height;
viewport.MinDepth = 0.0f, viewport.MaxDepth = 1.0f;
// Setting viewport
this->deviceContext->RSSetViewports(1, &viewport);
D3D11_RASTERIZER_DESC rasterizerDesc;
ZeroMemory(&rasterizerDesc, sizeof(D3D11_RASTERIZER_DESC));
// Font initiaization
spriteBatch = std::make_unique<DirectX::SpriteBatch>(this->deviceContext.Get());
spriteFont = std::make_unique<DirectX::SpriteFont>(this->device.Get(), L"../Data/Fonts/sourcesanspro.spritefont");
D3D11_SAMPLER_DESC samplerDesc;
ZeroMemory(&samplerDesc, sizeof(D3D11_SAMPLER_DESC));
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; // X-COORDINATE
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; // Y-COORDINATE
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; // Z-COORDINATE
samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
result = this->device->CreateSamplerState(&samplerDesc, this->samplerState.GetAddressOf());
if (FAILED(result)) {
Logger::Log(result, "Failed to create sampler");
return false;
}
return true;
}
bool Graphics::InitializeShaders() {
std::wstring shaderFolder;
#pragma region DetermineShaderPath
if (IsDebuggerPresent()) {
#ifdef _DEBUG
#ifdef _WIN64
shaderFolder = L"../x64/Debug/";
#else
shaderFolder = L"../Debug/";
#endif
#else // Release mode
#ifdef _WIN64
shaderFolder = L"../x64/Release/";
#else
shaderFolder = L"../Release/";
#endif
#endif
}
D3D11_INPUT_ELEMENT_DESC layout[] = {
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0}
};
UINT elementCount = ARRAYSIZE(layout);
if (!vertexShader.Initialize(this->device, shaderFolder + L"vertexshader.cso", layout, elementCount))
return false;
if (!pixelShader.Initialize(this->device, shaderFolder + L"pixelshader.cso"))
return false;
return true;
}
bool Graphics::InitializeScene() {
Vertex v[] = {
// Order has to be clock-wise for default rasterizer state
Vertex(Vector3(-0.5f, -0.5f, 1.0f), Vector2(0.0f, 1.0f)), // bottom left [0]
Vertex(Vector3(-0.5f, 0.5f, 1.0f), Vector2(0.0f, 0.0f)), // top left [1]
Vertex(Vector3(0.5f, 0.5f, 1.0f), Vector2(1.0f, 0.0f)), // right top [2]
Vertex(Vector3(0.5f, -0.5f, 1.0f), Vector2(1.0f, 1.0f)) // right bottom[3]
};
DWORD indices[] = {
0, 1, 2,
0, 2, 3
};
vertexBuffer.Initialize(this->device.Get(), v, ARRAYSIZE(v));
indexBuffer.Initialize(this->device.Get(), indices, ARRAYSIZE(indices));
HRESULT hr = DirectX::CreateWICTextureFromFile(this->device.Get(), L"../Data/Textures/Wood.jpg", 0, myTexture.GetAddressOf());
if (FAILED(hr)) {
Logger::Log(hr, "Failed to load texture");
return false;
}
if (FAILED(hr)) {
Logger::Log(hr, "Failed to load texture");
return false;
}
hr = this->constantBuffer.Initialize(this->device.Get(), this->deviceContext.Get());
if (FAILED(hr)) {
Logger::Log(hr, "Failed to initialize constant buffer");
return false;
}
return true;
}