forked from vpinball/vpinball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenderDevice.h
522 lines (439 loc) · 16.1 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
516
517
518
519
520
521
522
#pragma once
#include <map>
#include "typedefs3D.h"
#include "Material.h"
#include "Texture.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);
#if 1//def _DEBUG
#ifdef ENABLE_SDL
void checkGLErrors(const char *file, const int line);
#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[CCHDEVICENAME];
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
};
enum UsageFlags {
#ifdef ENABLE_SDL
USAGE_STATIC = GL_STATIC_DRAW,
USAGE_DYNAMIC = GL_DYNAMIC_DRAW
#else
USAGE_STATIC = D3DUSAGE_WRITEONLY, // to be used for vertex/index buffers which are uploaded once and never touched again
USAGE_DYNAMIC = D3DUSAGE_DYNAMIC // to be used for vertex/index buffers which are locked every frame/very often
#endif
};
class Shader;
class RenderDevice
{
public:
#ifdef ENABLE_SDL
enum RenderStates
{
ALPHABLENDENABLE,
ZENABLE,
DEPTHBIAS,
ALPHATESTENABLE,
ALPHAREF,
ALPHAFUNC,
BLENDOP,
CLIPPING,
CLIPPLANEENABLE,
CULLMODE,
DESTBLEND,
LIGHTING,
SRCBLEND,
SRGBWRITEENABLE,
ZFUNC,
ZWRITEENABLE,
COLORWRITEENABLE,
RENDERSTATE_COUNT,
RENDERSTATE_INVALID
};
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 PrimitiveTypes {
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,
TEXTUREFACTOR = D3DRS_TEXTUREFACTOR,
DEPTHBIAS = D3DRS_DEPTHBIAS,
COLORWRITEENABLE = D3DRS_COLORWRITEENABLE,
RENDERSTATE_COUNT,
RENDERSTATE_INVALID
};
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 TextureAddressMode {
TEX_WRAP = D3DTADDRESS_WRAP,
TEX_CLAMP = D3DTADDRESS_CLAMP,
TEX_MIRROR = D3DTADDRESS_MIRROR
};
enum PrimitiveTypes {
TRIANGLEFAN = D3DPT_TRIANGLEFAN,
TRIANGLESTRIP = D3DPT_TRIANGLESTRIP,
TRIANGLELIST = D3DPT_TRIANGLELIST,
POINTLIST = D3DPT_POINTLIST,
LINELIST = D3DPT_LINELIST,
LINESTRIP = D3DPT_LINESTRIP
};
#endif
RenderDevice(const HWND hwnd, const int width, const int height, const bool fullscreen, const int colordepth, int VSync, const bool useAA, const bool stereo3D, const unsigned int FXAA, const bool sharpen, const bool ss_refl, const bool useNvidiaApi, const bool disable_dwm, const int BWrendering);
~RenderDevice();
void CreateDevice(int &refreshrate, UINT adapterIndex = D3DADAPTER_DEFAULT);
bool LoadShaders();
void BeginScene();
void EndScene();
void Clear(const DWORD numRects, const D3DRECT* rects, 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_pOffscreenBackBufferTmpTexture; } // stereo/FXAA only
D3DTexture* GetBackBufferTmpTexture2() const { return m_pOffscreenBackBufferTmpTexture2; } // SMAA only
#ifdef ENABLE_SDL
D3DTexture* GetNonMSAABlitTexture(int m_MSAASamples) const { return m_MSAASamples == 1 ? m_pOffscreenBackBufferTexture : m_pOffscreenNonMSAABlitTexture; }
D3DTexture* GetOffscreenVR(int eye) const { return eye == 0 ? m_pOffscreenVRLeft : m_pOffscreenVRRight;}
#endif
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);
#ifdef ENABLE_SDL
static bool isVRinstalled();
static bool isVRturnedOn();
static void turnVROff();
#endif
#ifndef ENABLE_SDL
void SetRenderTarget(RenderTarget* surf);
void SetRenderTarget(D3DTexture* tex);
#endif
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* UploadTexture(BaseTexture* const surf, int* const pTexWidth, int* const pTexHeight, const bool linearRGB, const bool clamptoedge = false);
#endif
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(const bool enabled);
void SetRenderStateAlphaTestFunction(const DWORD testValue, const RenderStateValue testFunction, const bool enabled);
void SetTextureFilter(const DWORD texUnit, DWORD mode);
void SetTextureAddressMode(const DWORD texUnit, const TextureAddressMode mode);
#ifndef ENABLE_SDL
void SetTextureStageState(const DWORD stage, const D3DTEXTURESTAGESTATETYPE type, const DWORD value);
#endif
void SetSamplerState(const DWORD Sampler, const D3DSAMPLERSTATETYPE Type, const DWORD Value);
//!! Remove:
//void CreateVertexBuffer(const unsigned int numVerts, const DWORD usage, const DWORD fvf, VertexBuffer **vBuffer);
//void CreateIndexBuffer(const unsigned int numIndices, const DWORD usage, const IndexBuffer::Format format, IndexBuffer **idxBuffer);
//IndexBuffer* CreateAndFillIndexBuffer(const unsigned int numIndices, const unsigned int * indices);
//IndexBuffer* CreateAndFillIndexBuffer(const unsigned int numIndices, const WORD * indices);
//IndexBuffer* CreateAndFillIndexBuffer(const std::vector<unsigned int>& indices);
//IndexBuffer* CreateAndFillIndexBuffer(const std::vector<WORD>& indices);
void DrawTexturedQuad(const Vertex3D_TexelOnly* vertices);
void DrawFullscreenTexturedQuad();
void DrawPrimitiveVB(const PrimitiveTypes type, const DWORD fvf, VertexBuffer* vb, const DWORD startVertex, const DWORD vertexCount, const bool stereo);
void DrawIndexedPrimitiveVB(const PrimitiveTypes 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 SetTransform(const TransformStateType, const D3DMATRIX*);
void GetTransform(const TransformStateType, D3DMATRIX*);
void ForceAnisotropicFiltering(const bool enable) { m_force_aniso = enable; }
void CompressTextures(const bool enable) { m_compress_textures = enable; }
//VR stuff
#ifdef ENABLE_VR
void SetTransformVR();
void UpdateVRPosition();
void tableUp();
void tableDown();
void recenterTable();
void recenterRoom();
float m_slope, m_orientation, m_tablex, m_tabley, m_tablez, m_roomOrientation, m_roomx, m_roomy;
void updateTableMatrix();
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; }
unsigned int Perf_GetNumLockCalls() const;
void FreeShader();
inline void CreateVertexDeclaration(const VertexElement * const element, VertexDeclaration ** declaration)
{
CHECKD3D(m_pD3DDevice->CreateVertexDeclaration(element, declaration));
}
inline void SetVertexDeclaration(VertexDeclaration * declaration)
{
if (declaration != currentDeclaration)
{
CHECKD3D(m_pD3DDevice->SetVertexDeclaration(declaration));
currentDeclaration = declaration;
m_curStateChanges++;
}
}
inline IDirect3DDevice9* GetCoreDevice() const
{
return m_pD3DDevice;
}
HWND m_windowHwnd;
int m_width;
int m_height;
bool m_fullscreen;
int m_colorDepth;
int m_vsync;
bool m_useAA;
bool m_stereo3D;
bool m_ssRefl;
bool m_disableDwm;
bool m_sharpen;
unsigned int m_FXAA;
int m_BWrendering;
private:
void DrawPrimitive(const PrimitiveTypes type, const DWORD fvf, const void* vertices, const DWORD vertexCount);
void UploadAndSetSMAATextures();
D3DTexture* m_SMAAsearchTexture;
D3DTexture* m_SMAAareaTexture;
#ifndef ENABLE_SDL
#ifdef USE_D3D9EX
IDirect3D9Ex* m_pD3DEx;
IDirect3DDevice9Ex* m_pD3DDeviceEx;
#endif
IDirect3D9* m_pD3D;
IDirect3DDevice9* m_pD3DDevice;
#endif
IDirect3DSurface9* m_pBackBuffer;
D3DTexture* m_pOffscreenBackBufferTexture;
D3DTexture* m_pOffscreenBackBufferTmpTexture; // stereo/FXAA only
D3DTexture* m_pOffscreenBackBufferTmpTexture2;// SMAA only
#ifdef ENABLE_SDL
D3DTexture* m_pOffscreenNonMSAABlitTexture;
D3DTexture* m_pOffscreenVRLeft;
D3DTexture* m_pOffscreenVRRight;
#endif
D3DTexture* m_pBloomBufferTexture;
D3DTexture* m_pBloomTmpBufferTexture;
D3DTexture* m_pMirrorTmpBufferTexture;
D3DTexture* m_pReflectionBufferTexture;
UINT m_adapter; // index of the display adapter to use
static constexpr DWORD TEXTURE_SAMPLERS = 8;
static constexpr DWORD TEXTURE_STATE_CACHE_SIZE = 256;
static constexpr DWORD TEXTURE_SAMPLER_CACHE_SIZE = 14;
std::map<RenderStates, DWORD> renderStateCache; // for caching
DWORD textureStateCache[TEXTURE_SAMPLERS][TEXTURE_STATE_CACHE_SIZE]; // dto.
DWORD textureSamplerCache[TEXTURE_SAMPLERS][TEXTURE_SAMPLER_CACHE_SIZE]; // dto.
VertexDeclaration *currentDeclaration; // for caching
#ifdef ENABLE_SDL
GLfloat m_maxaniso;
int m_GLversion;
#else
DWORD m_maxaniso;
bool m_mag_aniso;
#endif
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;
//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:
#ifndef ENABLE_SDL
static bool m_useNvidiaApi;
static bool m_INTZ_support;
#endif
static VertexBuffer* m_quadVertexBuffer; // internal vb for rendering quads //!! only on primary device for now!
//static VertexBuffer *m_quadDynVertexBuffer; // internal vb for rendering dynamic quads //!!
// 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 *basicShader;
Shader *DMDShader;
Shader *FBShader;
Shader *flasherShader;
Shader *lightShader;
#ifdef SEPARATE_CLASSICLIGHTSHADER
Shader *classicLightShader;
#else
#define classicLightShader basicShader
#endif
//Shader* m_curShader; // for caching
TextureManager m_texMan;
static unsigned int m_stats_drawn_triangles;
static VertexDeclaration* m_pVertexTexelDeclaration;
static VertexDeclaration* m_pVertexNormalTexelDeclaration;
//static VertexDeclaration* m_pVertexNormalTexelTexelDeclaration;
static VertexDeclaration* m_pVertexTrafoTexelDeclaration;
};
#include "Shader.h"