-
Notifications
You must be signed in to change notification settings - Fork 1
/
Renderer.h
280 lines (230 loc) · 7.79 KB
/
Renderer.h
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
#pragma once
#include <vector>
#include <d3d11.h>
#include <DirectXMath.h>
#include <D3Dcompiler.h>
#include "Globals.h"
#include "Color.h"
#include "Log.h"
using DirectX::XMFLOAT4;
using DirectX::XMMATRIX;
const char g_szShader[] =
R"(cbuffer m_pViewMatrix : register(b0)
{
matrix m_ViewMatrix;
};
struct VS_OUTPUT
{
float4 pos : SV_POSITION;
float4 col : COLOR;
};
struct VS_INPUT
{
float4 pos : POSITION;
float4 col : COLOR;
};
VS_OUTPUT VS(VS_INPUT input)
{
VS_OUTPUT output;
output.pos = mul(m_ViewMatrix, float4(input.pos.xy, 0.f, 1.f));
output.col = input.col;
return output;
}
float4 PS(VS_OUTPUT input) : SV_TARGET
{
return input.col;
})";
////other shitty shader
//char szCast[] =
//"struct VS_OUT"
//"{"
//" float4 Position : POSITION;"
//" float4 Color : COLOR;"
//"};"
//
//"float4 main( VS_OUT input ) : SV_Target"
//"{"
//" float4 fake;"
//" fake.a = 1.0f;"
//" fake.r = %f;"
//" fake.g = %f;"
//" fake.b = %f;"
//" return fake;"
//"}";
typedef struct Vertex
{
XMFLOAT4 xyzrhw;
XMFLOAT4 color;
// D3DCOLOR color;
} Vertex;
typedef struct RenderBatch
{
RenderBatch(UINT count, D3D11_PRIMITIVE_TOPOLOGY topology)
: count(count), topology(topology)
{}
UINT count;
D3D11_PRIMITIVE_TOPOLOGY topology;
} RenderBatch;
class RenderList
{
friend class Renderer;
public:
RenderList()
: m_vertices(), m_uMaxVertices(0)
{
}
RenderList(unsigned int uReserveVertices)
: m_uMaxVertices(uReserveVertices)
{
m_vertices.reserve(uReserveVertices);
}
void AddVertex(const Vertex& vertex, D3D11_PRIMITIVE_TOPOLOGY topology)
{
assert(topology != D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP
&& "addVertex >Use addVertices to draw line/triangle strips!");
assert(topology != D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP_ADJ
&& "addVertex >Use addVertices to draw line/triangle strips!");
assert(topology != D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP
&& "addVertex >Use addVertices to draw line/triangle strips!");
assert(topology != D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP_ADJ
&& "addVertex >Use addVertices to draw line/triangle strips!");
if (m_vertices.size() >= m_uMaxVertices)
{
WARN("Vertex buffer exhausted! Increase the size of the vertex buffer or add a custom implementation.");
return;
}
if (m_batches.empty() || m_batches.back().topology != topology)
m_batches.emplace_back(0, topology);
m_batches.back().count++;
m_vertices.emplace_back(vertex);
}
void AddVertex(const Vertex* pVertices, UINT uVerticeCount, D3D11_PRIMITIVE_TOPOLOGY topology)
{
assert(topology != D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP
&& "AddVertex >Use AddVertices to draw line/triangle strips!");
assert(topology != D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP_ADJ
&& "AddVertex >Use AddVertices to draw line/triangle strips!");
assert(topology != D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP
&& "AddVertex >Use AddVertices to draw line/triangle strips!");
assert(topology != D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP_ADJ
&& "AddVertex >Use AddVertices to draw line/triangle strips!");
if (!pVertices)
return;
if (m_vertices.size() + uVerticeCount >= m_uMaxVertices)
{
WARN("Vertex buffer exhausted! Increase the size of the vertex buffer or add a custom implementation.");
return;
}
if (m_batches.empty() || m_batches.back().topology != topology)
m_batches.emplace_back(0, topology);
m_batches.back().count += uVerticeCount;
m_vertices.resize(m_vertices.size() + uVerticeCount);
memcpy(&m_vertices[m_vertices.size() - uVerticeCount], pVertices, uVerticeCount * sizeof(Vertex));
switch (topology)
{
case D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP:
case D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP_ADJ:
case D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP:
case D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP_ADJ:
Vertex seperator = {};
AddVertex(seperator, D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED);
return;
}
}
template<unsigned int N>
void AddVertices(Vertex(&vertices)[N], D3D11_PRIMITIVE_TOPOLOGY topology)
{
if (m_vertices.size() + N >= m_uMaxVertices)
{
WARN("Vertex buffer exhausted! Increase the size of the vertex buffer or add a custom implementation.");
return;
}
if (m_batches.empty() || m_batches.back().topology != topology)
m_batches.emplace_back(0, topology);
m_batches.back().count += N;
m_vertices.resize(m_vertices.size() + N);
memcpy(&m_vertices[m_vertices.size() - N], &vertices[0], N * sizeof(Vertex));
switch (topology)
{
case D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP:
case D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP_ADJ:
case D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP:
case D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP_ADJ:
Vertex seperator = {};
AddVertex(seperator, D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED);
return;
}
}
void clear()
{
m_vertices.clear();
m_batches.clear();
}
protected:
unsigned int m_uMaxVertices;
std::vector<Vertex> m_vertices;
std::vector<RenderBatch> m_batches;
};
class Renderer
{
public:
Renderer() : m_pDevice(NULL), m_pDeviceContext(NULL),
m_pVertexShader(NULL), m_pOriginalVertexShader(NULL),
m_pVertexBuffer(NULL), m_pOriginalVertexBuffer(NULL),
m_pPixelShader(NULL), m_pOriginalPixelShader(NULL),
m_pPixelBuffer(NULL), m_pOriginalPixelBuffer(NULL),
m_pInputLayout(NULL), m_pOriginalInputLayout(NULL),
m_pViewMatrix(NULL), m_RenderList(16384 * 8 * 4 * 3)
{
m_ViewMatrix = DirectX::XMMatrixIdentity();
}
~Renderer();
HRESULT Initalize(ID3D11Device* pDevice, ID3D11DeviceContext* pDeviceContext, ID3D11Buffer* pVertexBuffer);
HRESULT Initalize(ID3D11Device* pDevice, ID3D11DeviceContext* pDeviceContext);
void Release();
HRESULT Begin(void);
HRESULT End(void);
HRESULT SaveState(void);
HRESULT RestoreState(void);
void Draw(const RenderList& list);
void Draw(void);
template<size_t N>
HRESULT GeneratePixelShader(ID3D11PixelShader** pShader, ID3DBlob** ppBlob, const char (&szShader)[N]);
HRESULT GeneratePixelShader(ID3D11PixelShader** pShader, ID3DBlob** ppBlob, const char* szShader, size_t cchSource);
template<size_t N>
HRESULT GenerateVertexShader(ID3D11VertexShader** pShader, ID3DBlob** ppBlob, const char (&szShader)[N]);
HRESULT GenerateVertexShader(ID3D11VertexShader** pShader, ID3DBlob** ppBlob, const char* szShader, size_t cchSource);
void DrawPixel(int x, int y, const Color& color);
void DrawPixel(const RenderList& list, int x, int y, D3DCOLOR color);
void DrawLine(int x0, int y0, int x1, int y1, const Color& color);
void DrawLine(const RenderList& list, int x0, int y0, int x1, int y1, D3DCOLOR color);
void DrawText(int x, int y, const Color& color, const char* szText, ...);
void DrawFilledRect(int x, int y, int width, int height, const Color& color);
void DrawFilledRect(const RenderList& list, const Vector4& rect, const Color& color);
void DrawRectOutline(int x, int y, int w, int h, D3DCOLOR color);
void DrawRectBordered(int x, int y, int width, int height, int thickness, const Color& color);
void DrawRectCorners(int x, int y, int w, int h, int thickness, const Color& color);
void DrawHealthBar(int x, int y, int height, int health, int max_health);
private:
__forceinline void BuildVertex(XMFLOAT4 xyzrhw, D3DCOLOR color, Vertex* vertices, int index)
{
vertices[index].xyzrhw = xyzrhw;
vertices[index].color = XMFLOAT4(((color >> 16) & 0xFF) / 255.f, ((color >> 8) & 0xFF) / 255.f, (color & 0xFF) / 255.f, ((color >> 24) & 0xFF) / 255.f);
}
ID3D11Device* m_pDevice;
ID3D11DeviceContext* m_pDeviceContext;
ID3D11VertexShader* m_pVertexShader;
ID3D11VertexShader* m_pOriginalVertexShader;
ID3D11Buffer* m_pVertexBuffer;
ID3D11Buffer* m_pOriginalVertexBuffer;
ID3D11PixelShader* m_pPixelShader;
ID3D11PixelShader* m_pOriginalPixelShader;
ID3D11Buffer* m_pPixelBuffer;
ID3D11Buffer* m_pOriginalPixelBuffer;
ID3D11InputLayout* m_pInputLayout;
ID3D11InputLayout* m_pOriginalInputLayout;
ID3D11Buffer* m_pViewMatrix;
XMMATRIX m_ViewMatrix;
RenderList m_RenderList;
};
extern Renderer* g_pRenderer;