-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderSystem.cpp
More file actions
163 lines (135 loc) · 6.08 KB
/
RenderSystem.cpp
File metadata and controls
163 lines (135 loc) · 6.08 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
#include "RenderSystem.h"
RenderSystem::RenderSystem()
{
/*
* Documentation
* https://docs.microsoft.com/en-us/windows/win32/api/d3dcommon/ne-d3dcommon-d3d_driver_type
*
* D3D_DRIVER_TYPE_HARDWARE
* A hardware driver, which implements Direct3D features in hardware.
* This is the primary driver that you should use in your Direct3D
* applications because it provides the best performance.
* A hardware driver uses hardware acceleration (on supported hardware)
* but can also use software for parts of the pipeline that are not supported in hardware.
* This driver type is often referred to as a hardware abstraction layer or HAL.
*
* D3D_DRIVER_TYPE_REFERENCE
* A reference driver, which is a software implementation that supports every Direct3D feature.
* Used for testing, debugging or verifying bugs in other drivers.
*
* D3D_DRIVER_TYPE_WARP
* A WARP driver, which is a high-performance software rasterizer.
* The rasterizer supports feature levels 9_1 through level 10_1 with a high performance software implementation.
* https://docs.microsoft.com/en-us/windows/win32/direct3darticles/directx-warp
*/
D3D_FEATURE_LEVEL feature_levels[] =
{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_2,
D3D_FEATURE_LEVEL_9_1
};
/*
* Feature levels
* https://docs.microsoft.com/en-us/windows/win32/direct3d11/overviews-direct3d-11-devices-downlevel-intro
* Using feature levels, you can develop an application for Direct3D 9, Microsoft Direct3D 10, or Direct3D 11,
* and then run it on 9, 10 or 11 hardware.
* (with some exceptions; for example, new 11 features will not run on an existing 9 card)
*/
HRESULT res = D3D11CreateDevice(
nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, NULL,
feature_levels, ARRAYSIZE(feature_levels), D3D11_SDK_VERSION,
&mD3DDevice, &mFeatureLevel, &mImmContext
);
if (FAILED(res)) throw std::exception("RenderSystem creation error. Could not create the D3D Device.");
mImmDeviceContext = std::make_shared<DeviceContext>(mImmContext, this);
mD3DDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&mDxgiDevice);
mDxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter);
mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory);
initRasterizerState();
}
RenderSystem::~RenderSystem()
{
mDxgiDevice->Release();
mDxgiAdapter->Release();
mDxgiFactory->Release();
mD3DDevice->Release();
}
SwapChainPtr RenderSystem::createSwapChain(HWND hwnd, UINT width, UINT height)
{
return std::make_shared<SwapChain>(hwnd, width, height, this);
}
VertexBufferPtr RenderSystem::createVertexBuffer(void* list_vertices, UINT size_vertex, UINT size_list, void* shader_byte_code, size_t size_byte_shader)
{
return std::make_shared<VertexBuffer>(list_vertices, size_vertex, size_list, shader_byte_code, size_byte_shader, this);
}
VertexShaderPtr RenderSystem::createVertexShader(const void* shader_byte_code, size_t byte_code_size)
{
return std::make_shared<VertexShader>(shader_byte_code, byte_code_size, this);
}
PixelShaderPtr RenderSystem::createPixelShader(const void* shader_byte_code, size_t byte_code_size)
{
return std::make_shared<PixelShader>(shader_byte_code, byte_code_size, this);
}
ConstantBufferPtr RenderSystem::createConstantBuffer(void* buffer, UINT size_buffer)
{
return std::make_shared<ConstantBuffer>(buffer, size_buffer, this);
}
IndexBufferPtr RenderSystem::createIndexBuffer(void* list_indices, UINT size_list)
{
return std::make_shared<IndexBuffer>(list_indices, size_list, this);
}
DeviceContextPtr RenderSystem::getImmediateDeviceContext()
{
return this->mImmDeviceContext;
}
bool RenderSystem::compileVertexShader(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, &mBlob, &error_blob))) {
if (error_blob) error_blob->Release();
return false;
}
*shader_byte_code = this->mBlob->GetBufferPointer();
*byte_code_size = this->mBlob->GetBufferSize();
return true;
}
bool RenderSystem::compilePixelShader(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, &mBlob, &error_blob))) {
if (error_blob) error_blob->Release();
return false;
}
*shader_byte_code = mBlob->GetBufferPointer();
*byte_code_size = (UINT)mBlob->GetBufferSize();
return true;
}
void RenderSystem::releaseCompiledShader()
{
if (mBlob) mBlob->Release();
}
void RenderSystem::setRasterizerState(bool cull_front)
{
/*
* Rasterization (or rasterisation) is the task of taking an image described in a vector graphics format (shapes)
* and converting it into a raster image (a series of pixels, dots or lines, which, when displayed together,
* create the image which was represented via shapes). Method used to indicate the cull mode for the rasterizer
*/
if (cull_front) mImmContext->RSSetState(mCullFrontState);
else mImmContext->RSSetState(mCullBackState);
}
void RenderSystem::initRasterizerState()
{
D3D11_RASTERIZER_DESC desc = {};
desc.DepthClipEnable = true; // Enable clipping based on distance.
desc.FillMode = D3D11_FILL_SOLID; // Fill the triangles formed by the vertices. Adjacent vertices are not drawn
// Cull mode indicates triangles facing the specified direction to not be drawn.
desc.CullMode = D3D11_CULL_FRONT; // Do not draw triangles that are front-facing
mD3DDevice->CreateRasterizerState(&desc, &mCullFrontState);
desc.CullMode = D3D11_CULL_BACK; // Do not draw triangles that are back-facing
mD3DDevice->CreateRasterizerState(&desc, &mCullBackState);
}