-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenderSystem.cpp
247 lines (177 loc) · 5.48 KB
/
RenderSystem.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
#include "RenderSystem.h"
#include "SwapChain.h"
#include "DeviceContext.h"
#include "VertexBuffer.h"
#include "ConstantBuffer.h"
#include "IndexBuffer.h"
#include "VertexShader.h"
#include "PixelShader.h"
#include <d3dcompiler.h>
#include <exception>
RenderSystem::RenderSystem()
{
D3D_DRIVER_TYPE driver_types[] =
{
D3D_DRIVER_TYPE_HARDWARE,
D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE
};
UINT num_driver_types = ARRAYSIZE(driver_types);
D3D_FEATURE_LEVEL feature_levels[] =
{
D3D_FEATURE_LEVEL_11_0
};
UINT num_feature_levels = ARRAYSIZE(feature_levels);
HRESULT result = 0;
for (UINT driver_type_index = 0; driver_type_index < num_driver_types;)
{
result = D3D11CreateDevice(m_dxgi_adapter, driver_types[driver_type_index], NULL, D3D11_CREATE_DEVICE_BGRA_SUPPORT, feature_levels, num_feature_levels, D3D11_SDK_VERSION,
&m_d3d_device, &m_feature_level, &m_imm_context); //out params
if (SUCCEEDED(result))break;
++driver_type_index;
}
if (FAILED(result))
throw std::exception("RenderSystem was not created successfully");
m_imm_device_context = std::make_shared<DeviceContext>(m_imm_context,this);
m_d3d_device->QueryInterface(__uuidof(IDXGIDevice), (void**)&m_dxgi_device);
m_dxgi_device->GetParent(__uuidof(IDXGIAdapter), (void**)&m_dxgi_adapter);
m_dxgi_adapter->GetParent(__uuidof(IDXGIFactory), (void**)&m_dxgi_factory);
initRasterizerState();
}
RenderSystem::~RenderSystem()
{
if (m_vsblob)m_vsblob->Release();
if (m_psblob)m_psblob->Release();
m_dxgi_device->Release();
m_dxgi_adapter->Release();
m_dxgi_factory->Release();
m_d3d_device->Release();
}
SwapChainPtr RenderSystem::createSwapChain(HWND hwnd, UINT width, UINT height)
{
SwapChainPtr swapchain = nullptr;
try
{
swapchain = std::make_shared<SwapChain>(hwnd, width, height, this);
}
catch(...){}
return swapchain;
}
DeviceContextPtr RenderSystem::getImmediateDeviceContext()
{
return this->m_imm_device_context;
}
ID3D11Device* RenderSystem::getDevice()
{
return m_d3d_device;
}
VertexBufferPtr RenderSystem::createVertexBuffer(void* list_vertices, UINT size_vertex, UINT size_list, void* shader_byte_code, size_t size_byte_shader)
{
VertexBufferPtr vertexbuffer = nullptr;
try
{
vertexbuffer = std::make_shared<VertexBuffer>(list_vertices, size_vertex, size_list, shader_byte_code, size_byte_shader,this);
}
catch (...) {}
return vertexbuffer;
}
ConstantBufferPtr RenderSystem::createConstantBuffer(void* buffer, UINT size_buffer)
{
ConstantBufferPtr constantbuffer = nullptr;
try
{
constantbuffer = std::make_shared<ConstantBuffer>(buffer, size_buffer,this);
}
catch (...) {}
return constantbuffer;
}
IndexBufferPtr RenderSystem::createIndexBuffer(void* list_indices, UINT size_list)
{
IndexBufferPtr indexbuffer = nullptr;
try
{
indexbuffer = std::make_shared<IndexBuffer>(list_indices, size_list,this);
}
catch (...) {}
return indexbuffer;
}
VertexShaderPtr RenderSystem::createVertexShader(const void* shader_byte_code, size_t byte_code_size)
{
VertexShaderPtr vertex_shader = nullptr;
try
{
vertex_shader = std::make_shared <VertexShader>(shader_byte_code, byte_code_size, this);
}
catch (...) {}
return vertex_shader;
}
bool RenderSystem::compileVertexShaders(const wchar_t* file_name, const char* entry_point_name, void** shader_byte_code, size_t* byte_code_size)
{
ID3DBlob* error_blob = nullptr;
if (!SUCCEEDED(D3DCompileFromFile(file_name, nullptr, nullptr, entry_point_name, "vs_5_0", 0, 0, &m_blob, &error_blob)))
{
if (error_blob)error_blob->Release();
return false;
}
*shader_byte_code = m_blob->GetBufferPointer();
*byte_code_size = m_blob->GetBufferSize();
return true;
}
void RenderSystem::releaseCompiledShader()
{
if (m_blob)m_blob->Release();
}
PixelShaderPtr RenderSystem::createPixelShader(const void* shader_byte_code, size_t byte_code_size)
{
PixelShaderPtr pixel_shader = nullptr;
try
{
pixel_shader = std::make_shared<PixelShader>(shader_byte_code, byte_code_size, this);
}
catch (...) {}
return pixel_shader;
}
bool RenderSystem::compilePixelShaders(const wchar_t* file_name, const char* entry_point_name, void** shader_byte_code, size_t* byte_code_size)
{
ID3DBlob* error_blob = nullptr;
if (!SUCCEEDED(D3DCompileFromFile(file_name, nullptr, nullptr, entry_point_name, "ps_5_0", 0, 0, &m_blob, &error_blob)))
{
if (error_blob)error_blob->Release();
return false;
}
*shader_byte_code = m_blob->GetBufferPointer();
*byte_code_size = m_blob->GetBufferSize();
return true;
}
void RenderSystem::setRasterizerState(bool cull, bool cull_front)
{
if(cull)
{
if (cull_front)
{
m_imm_context->RSSetState(m_cull_front_state);
}
else
{
m_imm_context->RSSetState(m_cull_back_state);
}
}
else
{
m_imm_context->RSSetState(m_no_cull_state);
}
}
void RenderSystem::initRasterizerState()
{
D3D11_RASTERIZER_DESC desc = {};
desc.CullMode = D3D11_CULL_FRONT;
desc.DepthClipEnable = true;
desc.FillMode = D3D11_FILL_SOLID;
m_d3d_device->CreateRasterizerState(&desc,&m_cull_front_state);
desc.CullMode = D3D11_CULL_NONE;
desc.DepthClipEnable = true;
desc.FillMode = D3D11_FILL_SOLID;
m_d3d_device->CreateRasterizerState(&desc, &m_no_cull_state);
desc.CullMode = D3D11_CULL_BACK;
m_d3d_device->CreateRasterizerState(&desc,&m_cull_back_state);
}