-
Notifications
You must be signed in to change notification settings - Fork 1
/
RenderDevice.h
515 lines (431 loc) · 16.4 KB
/
RenderDevice.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
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#pragma once
#include <map>
#include "typeDefs3D.h"
#include "Material.h"
#include "Texture.h"
#include "stdafx.h"
#include "IndexBuffer.h"
#include "VertexBuffer.h"
#include "TextureManager.h"
#ifdef ENABLE_VR
#include <openvr.h>
#endif
void ReportFatalError(const HRESULT hr, const char *file, const int line);
void ReportError(const char *errorText, const HRESULT hr, const char *file, const int line);
#ifdef _DEBUG
#ifdef ENABLE_SDL
void checkGLErrors(const char *file, const int line);
//#define CHECKD3D(s) {s;}
#define CHECKD3D(s) { s;checkGLErrors(__FILE__, __LINE__);}
#else //ENABLE_SDL
#define CHECKD3D(s) { const HRESULT hrTmp = (s); if (FAILED(hrTmp)) ReportFatalError(hrTmp, __FILE__, __LINE__); }
#endif
#else //_DEBUG
#define CHECKD3D(s) {s;}
#endif
bool IsWindows10_1803orAbove();
struct VideoMode
{
int width;
int height;
int depth;
int refreshrate;
};
struct DisplayConfig
{
int display;
int adapter;
int top;
int left;
int width;
int height;
bool isPrimary;
char DeviceName[32];
char GPU_Name[MAX_DEVICE_IDENTIFIER_STRING];
};
int getNumberOfDisplays();
void EnumerateDisplayModes(const int display, std::vector<VideoMode>& modes);
bool getDisplaySetupByID(const int display, int &x, int &y, int &width, int &height);
int getDisplayList(std::vector<DisplayConfig>& displays);
int getPrimaryDisplay();
enum TransformStateType {
#ifdef ENABLE_SDL
TRANSFORMSTATE_WORLD = 0,
TRANSFORMSTATE_VIEW = 1,
TRANSFORMSTATE_PROJECTION = 2
#else
TRANSFORMSTATE_WORLD = D3DTS_WORLD,
TRANSFORMSTATE_VIEW = D3DTS_VIEW,
TRANSFORMSTATE_PROJECTION = D3DTS_PROJECTION
#endif
};
#ifdef ENABLE_SDL
enum UsageFlags {
USAGE_STATIC = GL_STATIC_DRAW,
USAGE_DYNAMIC = GL_DYNAMIC_DRAW
};
#else
enum UsageFlags {
USAGE_STATIC = D3DUSAGE_WRITEONLY,
USAGE_DYNAMIC = D3DUSAGE_DYNAMIC // to be used for vertex/index buffers which are locked every frame/very often
};
#endif
class TextureManager;
class Shader;
class RenderDevice
{
public:
RenderDevice(HWND* const hwnd, const int width, const int height, const bool fullscreen, const int colordepth, int VSync, const float AAfactor, const int stereo3D, const unsigned int FXAA, const bool ss_refl, const bool useNvidiaApi, const bool disable_dwm, const int BWrendering, const RenderDevice* primaryDevice = NULL);
#ifdef ENABLE_SDL
enum RenderStates
{
ALPHABLENDENABLE = GL_BLEND,
ZENABLE = GL_DEPTH_TEST,
DEPTHBIAS = GL_POLYGON_OFFSET_FILL,
ALPHATESTENABLE,
ALPHAREF,
ALPHAFUNC,
BLENDOP,
CLIPPING,
CLIPPLANEENABLE,
CULLMODE,
DESTBLEND,
LIGHTING,
SRCBLEND,
SRGBWRITEENABLE,
ZFUNC,
ZWRITEENABLE,
COLORWRITEENABLE
};
enum RenderStateValue
{
//Booleans
RS_FALSE = 0,
RS_TRUE = 1,
//Culling
CULL_NONE = 0,
CULL_CW = GL_CW,
CULL_CCW = GL_CCW,
//Depth functions
Z_ALWAYS = GL_ALWAYS,
Z_LESS = GL_LESS,
Z_LESSEQUAL = GL_LEQUAL,
Z_GREATER = GL_GREATER,
Z_GREATEREQUAL = GL_GEQUAL,
//Blending ops
BLENDOP_MAX = GL_MAX,
BLENDOP_ADD = GL_FUNC_ADD,
BLENDOP_SUB = GL_FUNC_SUBTRACT,
BLENDOP_REVSUBTRACT = GL_FUNC_REVERSE_SUBTRACT,
//Blending values
ZERO = GL_ZERO,
ONE = GL_ONE,
SRC_ALPHA = GL_SRC_ALPHA,
DST_ALPHA = GL_DST_ALPHA,
SRC_COLOR = GL_SRC_COLOR,
DST_COLOR = GL_DST_COLOR,
INVSRC_ALPHA = GL_ONE_MINUS_SRC_ALPHA,
INVSRC_COLOR = GL_ONE_MINUS_SRC_COLOR,
//Clipping planes
PLANE0 = 1,
UNDEFINED
};
enum SamplerStateValues {
NONE = 0,
POINT = 0,
LINEAR = 1,
TEX_WRAP = GL_REPEAT,
TEX_CLAMP = GL_CLAMP_TO_EDGE,
TEX_MIRROR = GL_MIRRORED_REPEAT
};
enum PrimitveTypes {
TRIANGLEFAN = GL_TRIANGLE_FAN,
TRIANGLESTRIP = GL_TRIANGLE_STRIP,
TRIANGLELIST = GL_TRIANGLES,
POINTLIST = GL_POINTS,
LINELIST = GL_LINES,
LINESTRIP = GL_LINE_STRIP
};
SDL_Window *m_sdl_playfieldHwnd;
SDL_GLContext m_sdl_context;
#else
enum RenderStates
{
ALPHABLENDENABLE = D3DRS_ALPHABLENDENABLE,
ALPHATESTENABLE = D3DRS_ALPHATESTENABLE,
ALPHAREF = D3DRS_ALPHAREF,
ALPHAFUNC = D3DRS_ALPHAFUNC,
BLENDOP = D3DRS_BLENDOP,
CLIPPING = D3DRS_CLIPPING,
CLIPPLANEENABLE = D3DRS_CLIPPLANEENABLE,
CULLMODE = D3DRS_CULLMODE,
DESTBLEND = D3DRS_DESTBLEND,
LIGHTING = D3DRS_LIGHTING,
SRCBLEND = D3DRS_SRCBLEND,
SRGBWRITEENABLE = D3DRS_SRGBWRITEENABLE,
ZENABLE = D3DRS_ZENABLE,
ZFUNC = D3DRS_ZFUNC,
ZWRITEENABLE = D3DRS_ZWRITEENABLE,
DEPTHBIAS = D3DRS_DEPTHBIAS,
COLORWRITEENABLE = D3DRS_COLORWRITEENABLE
};
enum RenderStateValue
{
//Booleans
RS_FALSE = FALSE,
RS_TRUE = TRUE,
//Culling
CULL_NONE = D3DCULL_NONE,
CULL_CW = D3DCULL_CW,
CULL_CCW = D3DCULL_CCW,
//Depth functions
Z_ALWAYS = D3DCMP_ALWAYS,
Z_LESS = D3DCMP_LESS,
Z_LESSEQUAL = D3DCMP_LESSEQUAL,
Z_GREATER = D3DCMP_GREATER,
Z_GREATEREQUAL = D3DCMP_GREATEREQUAL,
//Blending ops
BLENDOP_MAX = D3DBLENDOP_MAX,
BLENDOP_ADD = D3DBLENDOP_ADD,
BLENDOP_REVSUBTRACT = D3DBLENDOP_REVSUBTRACT,
//Blending values
ZERO = D3DBLEND_ZERO,
ONE = D3DBLEND_ONE,
SRC_ALPHA = D3DBLEND_SRCALPHA,
DST_ALPHA = D3DBLEND_DESTALPHA,
INVSRC_ALPHA = D3DBLEND_INVSRCALPHA,
INVSRC_COLOR = D3DBLEND_INVSRCCOLOR,
//Clipping planes
PLANE0 = D3DCLIPPLANE0,
UNDEFINED
};
enum SamplerStateValues {
NONE = D3DTEXF_NONE,
POINT = D3DTEXF_POINT,
LINEAR = D3DTEXF_LINEAR,
TEX_WRAP = D3DTADDRESS_WRAP,
TEX_CLAMP = D3DTADDRESS_CLAMP,
TEX_MIRROR = D3DTADDRESS_MIRROR
};
enum PrimitveTypes {
TRIANGLEFAN = D3DPT_TRIANGLEFAN,
TRIANGLESTRIP = D3DPT_TRIANGLESTRIP,
TRIANGLELIST = D3DPT_TRIANGLELIST,
POINTLIST = D3DPT_POINTLIST,
LINELIST = D3DPT_LINELIST,
LINESTRIP = D3DPT_LINESTRIP
};
#endif
void CreateDevice(int &refreshrate, UINT adapterIndex = D3DADAPTER_DEFAULT);
bool LoadShaders();
void InitVR();
~RenderDevice();
void BeginScene();
void EndScene();
void Clear(const DWORD flags, const D3DCOLOR color, const D3DVALUE z, const DWORD stencil);
void Flip(const bool vsync);
bool SetMaximumPreRenderedFrames(const DWORD frames);
D3DTexture* GetBackBufferTexture() const { return m_pOffscreenBackBufferTexture; }
D3DTexture* GetBackBufferTmpTexture() const { return m_pOffscreenBackBufferStereoTexture; }
D3DTexture* GetOffscreenVR(int eye) const { return eye == 0 ? m_pOffscreenVRLeft : m_pOffscreenVRRight;}
D3DTexture* GetBackBufferSMAATexture() const { return m_pOffscreenBackBufferSMAATexture; }
D3DTexture* GetMirrorTmpBufferTexture() const { return m_pMirrorTmpBufferTexture; }
D3DTexture* GetReflectionBufferTexture() const { return m_pReflectionBufferTexture; }
RenderTarget* GetOutputBackBuffer() const { return m_pBackBuffer; }
D3DTexture* GetBloomBufferTexture() const { return m_pBloomBufferTexture; }
D3DTexture* GetBloomTmpBufferTexture() const { return m_pBloomTmpBufferTexture; }
RenderTarget* DuplicateRenderTarget(RenderTarget* src);
D3DTexture* DuplicateTexture(RenderTarget* src);
D3DTexture* DuplicateTextureSingleChannel(RenderTarget* src);
D3DTexture* DuplicateDepthTexture(RenderTarget* src);
static bool isVRinstalled();
static bool isVRturnedOn();
static void turnVROff();
#ifndef ENABLE_SDL
void SetRenderTarget(RenderTarget* surf, bool ignoreStereo = false);
void SetZBuffer(D3DTexture* surf);
void* AttachZBufferTo(D3DTexture* surfTexture);
#endif
void SetRenderTarget(D3DTexture* texture, bool ignoreStereo = false);
void SetZBuffer(RenderTarget* surf);
void UnSetZBuffer();
void* AttachZBufferTo(RenderTarget* surf);
void CopySurface(RenderTarget* dest, RenderTarget* src);
#ifdef ENABLE_SDL
void CopyDepth(RenderTarget* dest, RenderTarget* src);
#else
void CopySurface(D3DTexture* dest, RenderTarget* src);
void CopySurface(RenderTarget* dest, D3DTexture* src);
void CopySurface(D3DTexture* dest, D3DTexture* src);
void CopySurface(void* dest, void* src);
void CopyDepth(D3DTexture* dest, RenderTarget* src);
void CopyDepth(D3DTexture* dest, D3DTexture* src);
void CopyDepth(D3DTexture* dest, void* src);
#endif
bool DepthBufferReadBackAvailable();
#ifndef ENABLE_SDL
D3DTexture* CreateSystemTexture(BaseTexture* const surf, const bool linearRGB);
D3DTexture* CreateSystemTexture(const int texwidth, const int texheight, const D3DFORMAT texformat, const void* data, const int pitch, const bool linearRGB);
#endif
D3DTexture* UploadTexture(BaseTexture* const surf, int* const pTexWidth = NULL, int* const pTexHeight = NULL, const bool linearRGB = true);
void UpdateTexture(D3DTexture* const tex, BaseTexture* const surf, const bool linearRGB);
void SetRenderState(const RenderStates p1, DWORD p2);
bool SetRenderStateCache(const RenderStates p1, DWORD p2);
void SetRenderStateCulling(RenderStateValue cull);
void SetRenderStateDepthBias(float bias);
void SetRenderStateClipPlane0(bool enabled);
void SetRenderStateAlphaTestFunction(DWORD testValue, RenderStateValue testFunction, bool enabled);
void SetTextureFilter(const DWORD texUnit, DWORD mode);
void SetTextureAddressMode(const DWORD texUnit, const SamplerStateValues mode);
#ifndef ENABLE_SDL
void SetTextureStageState(const DWORD stage, const D3DTEXTURESTAGESTATETYPE type, const DWORD value);
#endif
void SetSamplerState(const DWORD Sampler, const DWORD minFilter, const DWORD magFilter, const SamplerStateValues mipFilter);
void SetSamplerAnisotropy(const DWORD Sampler, DWORD Value);
D3DTexture* CreateTexture(UINT Width, UINT Height, UINT Levels, textureUsage Usage, colorFormat Format, void* data, int stereo);
// HRESULT CreateTexture(UINT Width, UINT Height, UINT Levels, textureUsage Usage, colorFormat Format, memoryPool Pool, D3DTexture** ppTexture, HANDLE* pSharedHandle);
#ifdef ENABLE_SDL
HRESULT Create3DFont(INT Height, UINT Width, UINT Weight, UINT MipLevels, BOOL Italic, DWORD CharSet, DWORD OutputPrecision, DWORD Quality, DWORD PitchAndFamily, LPCTSTR pFacename, TTF_Font *ppFont);
#else
HRESULT Create3DFont(INT Height, UINT Width, UINT Weight, UINT MipLevels, BOOL Italic, DWORD CharSet, DWORD OutputPrecision, DWORD Quality, DWORD PitchAndFamily, LPCTSTR pFacename, FontHandle *ppFont);
#endif
void DrawTexturedQuad();
void DrawTexturedQuadPostProcess();
void DrawPrimitiveVB(const PrimitveTypes type, const DWORD fvf, VertexBuffer* vb, const DWORD startVertex, const DWORD vertexCount, bool stereo);
void DrawIndexedPrimitiveVB(const PrimitveTypes type, const DWORD fvf, VertexBuffer* vb, const DWORD startVertex, const DWORD vertexCount, IndexBuffer* ib, const DWORD startIndex, const DWORD indexCount);
void SetViewport(const ViewPort*);
void GetViewport(ViewPort*);
void SetTransformVR();
void ForceAnisotropicFiltering(const bool enable) { m_force_aniso = enable; }
void CompressTextures(const bool enable) { m_compress_textures = enable; }
//VR stuff
unsigned int getBufwidth() { return m_Buf_width; }
unsigned int getBufheight() { return m_Buf_height; }
unsigned int getBufwidthBlur() { return m_Buf_widthBlur; }
unsigned int getBufheightBlur() { return m_Buf_heightBlur; }
void UpdateVRPosition();
void tableUp();
void tableDown();
void recenterTable();
void recenterRoom();
void updateTableMatrix();
float slope, orientation, tablex, tabley, tablez, roomOrientation, roomx, roomy;
#ifdef ENABLE_VR
vr::TrackedDevicePose_t hmdPosition;
#endif
// performance counters
unsigned int Perf_GetNumDrawCalls() const { return m_frameDrawCalls; }
unsigned int Perf_GetNumStateChanges() const { return m_frameStateChanges; }
unsigned int Perf_GetNumTextureChanges() const { return m_frameTextureChanges; }
unsigned int Perf_GetNumParameterChanges() const { return m_frameParameterChanges; }
unsigned int Perf_GetNumTechniqueChanges() const { return m_frameTechniqueChanges; }
unsigned int Perf_GetNumTextureUploads() const { return m_frameTextureUpdates; }
void FreeShader();
void CreateVertexDeclaration(const VertexElement * const element, VertexDeclaration ** declaration);
void SetVertexDeclaration(VertexDeclaration * declaration);
#ifndef ENABLE_SDL
inline IDirect3DDevice9* GetCoreDevice() const
{
return m_pD3DDevice;
}
#endif
HWND getHwnd() { return m_windowHwnd; }
HWND m_windowHwnd;
int m_width;
int m_height;
bool m_fullscreen;
int m_colorDepth;
int m_vsync;
float m_AAfactor;
int m_stereo3D;
bool m_ssRefl;
bool m_disableDwm;
unsigned int m_FXAA;
int m_BWrendering;
UINT m_adapter;
private:
void DrawPrimitive(const PrimitveTypes type, const DWORD fvf, const void* vertices, const DWORD vertexCount);
void UploadAndSetSMAATextures();
D3DTexture* m_SMAAsearchTexture;
D3DTexture* m_SMAAareaTexture;
#ifdef ENABLE_SDL
#else
#ifdef USE_D3D9EX
IDirect3D9Ex* m_pD3DEx;
IDirect3DDevice9Ex* m_pD3DDeviceEx;
#endif
IDirect3D9* m_pD3D;
IDirect3DDevice9* m_pD3DDevice;
#endif
RenderTarget* m_pBackBuffer;
//If stereo is enabled the right eye is the right/bottom part with 4px in between
D3DTexture* m_pOffscreenBackBufferTexture;
D3DTexture* m_pOffscreenBackBufferStereoTexture; // stereo/FXAA only
D3DTexture* m_pOffscreenBackBufferSMAATexture;// SMAA only
D3DTexture* m_pOffscreenVRLeft;
D3DTexture* m_pOffscreenVRRight;
D3DTexture* m_pBloomBufferTexture;
D3DTexture* m_pBloomTmpBufferTexture;
D3DTexture* m_pMirrorTmpBufferTexture;
D3DTexture* m_pReflectionBufferTexture;
static const DWORD TEXTURE_STATE_CACHE_SIZE = 256;
static const DWORD TEXTURE_SAMPLER_CACHE_SIZE = 14;
std::map<RenderStates, DWORD> renderStateCache; // for caching
DWORD textureStateCache[8][TEXTURE_STATE_CACHE_SIZE]; // dto.
DWORD textureSamplerCache[8][TEXTURE_SAMPLER_CACHE_SIZE]; // dto.
VertexDeclaration *currentDeclaration; // for caching
static VertexBuffer *m_quadVertexBuffer; // internal vb for rendering quads
DWORD m_maxaniso;
bool m_mag_aniso;
bool m_autogen_mipmap;
//bool m_RESZ_support;
bool m_force_aniso;
bool m_compress_textures;
bool m_dwm_was_enabled;
bool m_dwm_enabled;
unsigned int m_Buf_width;
unsigned int m_Buf_height;
unsigned int m_Buf_widthBlur;
unsigned int m_Buf_heightBlur;
unsigned int m_Buf_widthSS;
unsigned int m_Buf_heightSS;
//VR/Stereo Stuff
#ifdef ENABLE_VR
static vr::IVRSystem *m_pHMD;
Matrix3D m_matProj[2];
Matrix3D m_matView;
Matrix3D m_tableWorld;
Matrix3D m_roomWorld;
vr::TrackedDevicePose_t *m_rTrackedDevicePose;
float m_scale;
#endif
public:
bool m_useNvidiaApi;
static bool m_INTZ_support;
// performance counters
unsigned int m_curDrawCalls, m_frameDrawCalls;
unsigned int m_curStateChanges, m_frameStateChanges;
unsigned int m_curTextureChanges, m_frameTextureChanges;
unsigned int m_curParameterChanges, m_frameParameterChanges;
unsigned int m_curTechniqueChanges, m_frameTechniqueChanges;
unsigned int m_curTextureUpdates, m_frameTextureUpdates;
Shader *ballShader;
Shader *basicShader;
Shader *DMDShader;
Shader *FBShader;
Shader *flasherShader;
Shader *lightShader;
Shader *StereoShader;
#ifdef SEPARATE_CLASSICLIGHTSHADER
Shader *classicLightShader;
#else
#define classicLightShader basicShader
#endif
//Shader* m_curShader; // for caching
TextureManager m_texMan;
unsigned int m_stats_drawn_triangles;
static VertexDeclaration* m_pVertexTexelDeclaration;
static VertexDeclaration* m_pVertexNormalTexelDeclaration;
//static VertexDeclaration* m_pVertexNormalTexelTexelDeclaration;
static VertexDeclaration* m_pVertexTrafoTexelDeclaration;
};