1#ifndef ENGINE_CLIENT_GRAPHICS_THREADED_H
2#define ENGINE_CLIENT_GRAPHICS_THREADED_H
3
4#include <base/system.h>
5
6#include <engine/graphics.h>
7#include <engine/shared/config.h>
8
9#include <atomic>
10#include <cstddef>
11#include <mutex>
12#include <string>
13#include <vector>
14
15constexpr int CMD_BUFFER_DATA_BUFFER_SIZE = 1024 * 1024 * 2;
16constexpr int CMD_BUFFER_CMD_BUFFER_SIZE = 1024 * 256;
17
18namespace TextureFlag
19{
20 inline constexpr uint32_t NO_MIPMAPS = 1 << 0;
21 inline constexpr uint32_t TO_3D_TEXTURE = 1 << 1;
22 inline constexpr uint32_t TO_2D_ARRAY_TEXTURE = 1 << 2;
23 inline constexpr uint32_t NO_2D_TEXTURE = 1 << 3;
24};
25
26enum class EPrimitiveType
27{
28 LINES,
29 QUADS,
30 TRIANGLES,
31};
32
33enum class EBlendMode
34{
35 NONE,
36 ALPHA,
37 ADDITIVE,
38};
39
40enum class EWrapMode
41{
42 REPEAT,
43 CLAMP,
44};
45
46class CCommandBuffer
47{
48 class CBuffer
49 {
50 unsigned char *m_pData;
51 unsigned m_Size;
52 unsigned m_Used;
53
54 public:
55 CBuffer(unsigned BufferSize)
56 {
57 m_Size = BufferSize;
58 m_pData = new unsigned char[m_Size];
59 m_Used = 0;
60 }
61
62 ~CBuffer()
63 {
64 delete[] m_pData;
65 m_pData = nullptr;
66 m_Used = 0;
67 m_Size = 0;
68 }
69
70 void Reset()
71 {
72 m_Used = 0;
73 }
74
75 void *Alloc(unsigned Requested, unsigned Alignment = alignof(std::max_align_t))
76 {
77 size_t Offset = reinterpret_cast<uintptr_t>(m_pData + m_Used) % Alignment;
78 if(Offset)
79 Offset = Alignment - Offset;
80
81 if(Requested + Offset + m_Used > m_Size)
82 return nullptr;
83
84 void *pPtr = &m_pData[m_Used + Offset];
85 m_Used += Requested + Offset;
86 return pPtr;
87 }
88
89 unsigned char *DataPtr() { return m_pData; }
90 unsigned DataSize() const { return m_Size; }
91 unsigned DataUsed() const { return m_Used; }
92 };
93
94public:
95 CBuffer m_CmdBuffer;
96 size_t m_CommandCount = 0;
97 size_t m_RenderCallCount = 0;
98
99 CBuffer m_DataBuffer;
100
101 enum
102 {
103 MAX_TEXTURES = 1024 * 8,
104 MAX_VERTICES = 32 * 1024,
105 };
106
107 enum
108 {
109 // command groups
110 CMDGROUP_CORE = 0, // commands that everyone has to implement
111 CMDGROUP_PLATFORM_GL = 10000, // commands specific to a platform
112 CMDGROUP_PLATFORM_SDL = 20000,
113
114 CMD_FIRST = CMDGROUP_CORE,
115 };
116
117 enum ECommandBufferCMD
118 {
119 // synchronization
120 CMD_SIGNAL = CMD_FIRST,
121
122 // texture commands
123 CMD_TEXTURE_CREATE,
124 CMD_TEXTURE_DESTROY,
125 CMD_TEXT_TEXTURES_CREATE,
126 CMD_TEXT_TEXTURES_DESTROY,
127 CMD_TEXT_TEXTURE_UPDATE,
128
129 // rendering
130 CMD_CLEAR,
131 CMD_RENDER,
132 CMD_RENDER_TEX3D,
133
134 // opengl 2.0+ commands (some are just emulated and only exist in opengl 3.3+)
135 CMD_CREATE_BUFFER_OBJECT, // create vbo
136 CMD_RECREATE_BUFFER_OBJECT, // recreate vbo
137 CMD_UPDATE_BUFFER_OBJECT, // update vbo
138 CMD_COPY_BUFFER_OBJECT, // copy vbo to another
139 CMD_DELETE_BUFFER_OBJECT, // delete vbo
140
141 CMD_CREATE_BUFFER_CONTAINER, // create vao
142 CMD_DELETE_BUFFER_CONTAINER, // delete vao
143 CMD_UPDATE_BUFFER_CONTAINER, // update vao
144
145 CMD_INDICES_REQUIRED_NUM_NOTIFY, // create indices that are required
146
147 CMD_RENDER_TILE_LAYER, // render a tilelayer
148 CMD_RENDER_BORDER_TILE, // render one tile multiple times
149 CMD_RENDER_QUAD_LAYER, // render a quad layer
150 CMD_RENDER_QUAD_LAYER_GROUPED, // render a quad layer in groups meaning they all share the same envelope and offset (which can be none)
151 CMD_RENDER_TEXT, // render text
152 CMD_RENDER_QUAD_CONTAINER, // render a quad buffer container
153 CMD_RENDER_QUAD_CONTAINER_EX, // render a quad buffer container with extended parameters
154 CMD_RENDER_QUAD_CONTAINER_SPRITE_MULTIPLE, // render a quad buffer container as sprite multiple times
155
156 // swap
157 CMD_SWAP,
158
159 // misc
160 CMD_MULTISAMPLING,
161 CMD_VSYNC,
162 CMD_TRY_SWAP_AND_READ_PIXEL,
163 CMD_TRY_SWAP_AND_SCREENSHOT,
164 CMD_UPDATE_VIEWPORT,
165
166 // in Android a window that minimizes gets destroyed
167 CMD_WINDOW_CREATE_NTF,
168 CMD_WINDOW_DESTROY_NTF,
169
170 CMD_COUNT,
171 };
172
173 typedef vec2 SPoint;
174 typedef vec2 STexCoord;
175 typedef GL_SColorf SColorf;
176 typedef GL_SColor SColor;
177 typedef GL_SVertex SVertex;
178 typedef GL_SVertexTex3D SVertexTex3D;
179 typedef GL_SVertexTex3DStream SVertexTex3DStream;
180
181 struct SCommand
182 {
183 public:
184 SCommand(unsigned Cmd) :
185 m_Cmd(Cmd), m_pNext(nullptr) {}
186 unsigned m_Cmd;
187 SCommand *m_pNext;
188 };
189
190 struct SState
191 {
192 EBlendMode m_BlendMode;
193 EWrapMode m_WrapMode;
194 int m_Texture;
195 SPoint m_ScreenTL;
196 SPoint m_ScreenBR;
197
198 // clip
199 bool m_ClipEnable;
200 int m_ClipX;
201 int m_ClipY;
202 int m_ClipW;
203 int m_ClipH;
204 };
205
206 struct SCommand_Clear : public SCommand
207 {
208 SCommand_Clear() :
209 SCommand(CMD_CLEAR) {}
210 SColorf m_Color;
211 bool m_ForceClear;
212 };
213
214 struct SCommand_Signal : public SCommand
215 {
216 SCommand_Signal() :
217 SCommand(CMD_SIGNAL) {}
218 CSemaphore *m_pSemaphore;
219 };
220
221 struct SCommand_Render : public SCommand
222 {
223 SCommand_Render() :
224 SCommand(CMD_RENDER) {}
225 SState m_State;
226 EPrimitiveType m_PrimType;
227 unsigned m_PrimCount;
228 SVertex *m_pVertices; // you should use the command buffer data to allocate vertices for this command
229 };
230
231 struct SCommand_RenderTex3D : public SCommand
232 {
233 SCommand_RenderTex3D() :
234 SCommand(CMD_RENDER_TEX3D) {}
235 SState m_State;
236 EPrimitiveType m_PrimType;
237 unsigned m_PrimCount;
238 SVertexTex3DStream *m_pVertices; // you should use the command buffer data to allocate vertices for this command
239 };
240
241 struct SCommand_CreateBufferObject : public SCommand
242 {
243 SCommand_CreateBufferObject() :
244 SCommand(CMD_CREATE_BUFFER_OBJECT) {}
245
246 int m_BufferIndex;
247
248 bool m_DeletePointer;
249 void *m_pUploadData;
250 size_t m_DataSize;
251
252 int m_Flags; // @see EBufferObjectCreateFlags
253 };
254
255 struct SCommand_RecreateBufferObject : public SCommand
256 {
257 SCommand_RecreateBufferObject() :
258 SCommand(CMD_RECREATE_BUFFER_OBJECT) {}
259
260 int m_BufferIndex;
261
262 bool m_DeletePointer;
263 void *m_pUploadData;
264 size_t m_DataSize;
265
266 int m_Flags; // @see EBufferObjectCreateFlags
267 };
268
269 struct SCommand_UpdateBufferObject : public SCommand
270 {
271 SCommand_UpdateBufferObject() :
272 SCommand(CMD_UPDATE_BUFFER_OBJECT) {}
273
274 int m_BufferIndex;
275
276 bool m_DeletePointer;
277 void *m_pOffset;
278 void *m_pUploadData;
279 size_t m_DataSize;
280 };
281
282 struct SCommand_CopyBufferObject : public SCommand
283 {
284 SCommand_CopyBufferObject() :
285 SCommand(CMD_COPY_BUFFER_OBJECT) {}
286
287 int m_WriteBufferIndex;
288 int m_ReadBufferIndex;
289
290 size_t m_ReadOffset;
291 size_t m_WriteOffset;
292 size_t m_CopySize;
293 };
294
295 struct SCommand_DeleteBufferObject : public SCommand
296 {
297 SCommand_DeleteBufferObject() :
298 SCommand(CMD_DELETE_BUFFER_OBJECT) {}
299
300 int m_BufferIndex;
301 };
302
303 struct SCommand_CreateBufferContainer : public SCommand
304 {
305 SCommand_CreateBufferContainer() :
306 SCommand(CMD_CREATE_BUFFER_CONTAINER) {}
307
308 int m_BufferContainerIndex;
309
310 int m_Stride;
311 int m_VertBufferBindingIndex;
312
313 size_t m_AttrCount;
314 SBufferContainerInfo::SAttribute *m_pAttributes;
315 };
316
317 struct SCommand_UpdateBufferContainer : public SCommand
318 {
319 SCommand_UpdateBufferContainer() :
320 SCommand(CMD_UPDATE_BUFFER_CONTAINER) {}
321
322 int m_BufferContainerIndex;
323
324 int m_Stride;
325 int m_VertBufferBindingIndex;
326
327 size_t m_AttrCount;
328 SBufferContainerInfo::SAttribute *m_pAttributes;
329 };
330
331 struct SCommand_DeleteBufferContainer : public SCommand
332 {
333 SCommand_DeleteBufferContainer() :
334 SCommand(CMD_DELETE_BUFFER_CONTAINER) {}
335
336 int m_BufferContainerIndex;
337 bool m_DestroyAllBO;
338 };
339
340 struct SCommand_IndicesRequiredNumNotify : public SCommand
341 {
342 SCommand_IndicesRequiredNumNotify() :
343 SCommand(CMD_INDICES_REQUIRED_NUM_NOTIFY) {}
344
345 unsigned int m_RequiredIndicesNum;
346 };
347
348 struct SCommand_RenderTileLayer : public SCommand
349 {
350 SCommand_RenderTileLayer() :
351 SCommand(CMD_RENDER_TILE_LAYER) {}
352 SState m_State;
353 SColorf m_Color; // the color of the whole tilelayer -- already enveloped
354
355 // the char offset of all indices that should be rendered, and the amount of renders
356 char **m_pIndicesOffsets;
357 unsigned int *m_pDrawCount;
358
359 int m_IndicesDrawNum;
360 int m_BufferContainerIndex;
361 };
362
363 struct SCommand_RenderBorderTile : public SCommand
364 {
365 SCommand_RenderBorderTile() :
366 SCommand(CMD_RENDER_BORDER_TILE) {}
367 SState m_State;
368 SColorf m_Color; // the color of the whole tilelayer -- already enveloped
369 char *m_pIndicesOffset;
370 uint32_t m_DrawNum;
371 int m_BufferContainerIndex;
372
373 vec2 m_Offset;
374 vec2 m_Scale;
375 };
376
377 struct SCommand_RenderQuadLayer : public SCommand
378 {
379 SCommand_RenderQuadLayer(bool Grouped) :
380 SCommand(Grouped ? CMD_RENDER_QUAD_LAYER_GROUPED : CMD_RENDER_QUAD_LAYER) {}
381 SState m_State;
382
383 int m_BufferContainerIndex;
384 SQuadRenderInfo *m_pQuadInfo;
385 size_t m_QuadNum;
386 int m_QuadOffset;
387 };
388
389 struct SCommand_RenderText : public SCommand
390 {
391 SCommand_RenderText() :
392 SCommand(CMD_RENDER_TEXT) {}
393 SState m_State;
394
395 int m_BufferContainerIndex;
396 int m_TextureSize;
397
398 int m_TextTextureIndex;
399 int m_TextOutlineTextureIndex;
400
401 int m_DrawNum;
402 ColorRGBA m_TextColor;
403 ColorRGBA m_TextOutlineColor;
404 };
405
406 struct SCommand_RenderQuadContainer : public SCommand
407 {
408 SCommand_RenderQuadContainer() :
409 SCommand(CMD_RENDER_QUAD_CONTAINER) {}
410 SState m_State;
411
412 int m_BufferContainerIndex;
413
414 unsigned int m_DrawNum;
415 void *m_pOffset;
416 };
417
418 struct SCommand_RenderQuadContainerEx : public SCommand
419 {
420 SCommand_RenderQuadContainerEx() :
421 SCommand(CMD_RENDER_QUAD_CONTAINER_EX) {}
422 SState m_State;
423
424 int m_BufferContainerIndex;
425
426 float m_Rotation;
427 SPoint m_Center;
428
429 SColorf m_VertexColor;
430
431 unsigned int m_DrawNum;
432 void *m_pOffset;
433 };
434
435 struct SCommand_RenderQuadContainerAsSpriteMultiple : public SCommand
436 {
437 SCommand_RenderQuadContainerAsSpriteMultiple() :
438 SCommand(CMD_RENDER_QUAD_CONTAINER_SPRITE_MULTIPLE) {}
439 SState m_State;
440
441 int m_BufferContainerIndex;
442
443 IGraphics::SRenderSpriteInfo *m_pRenderInfo;
444
445 SPoint m_Center;
446 SColorf m_VertexColor;
447
448 unsigned int m_DrawNum;
449 unsigned int m_DrawCount;
450 void *m_pOffset;
451 };
452
453 struct SCommand_TrySwapAndReadPixel : public SCommand
454 {
455 SCommand_TrySwapAndReadPixel() :
456 SCommand(CMD_TRY_SWAP_AND_READ_PIXEL) {}
457 ivec2 m_Position;
458 SColorf *m_pColor; // processor will fill this out
459 bool *m_pSwapped; // processor may set this to true, must be initialized to false by the caller
460 };
461
462 struct SCommand_TrySwapAndScreenshot : public SCommand
463 {
464 SCommand_TrySwapAndScreenshot() :
465 SCommand(CMD_TRY_SWAP_AND_SCREENSHOT) {}
466 CImageInfo *m_pImage; // processor will fill this out, the one who adds this command must free the data as well
467 bool *m_pSwapped; // processor may set this to true, must be initialized to false by the caller
468 };
469
470 struct SCommand_Swap : public SCommand
471 {
472 SCommand_Swap() :
473 SCommand(CMD_SWAP) {}
474 };
475
476 struct SCommand_VSync : public SCommand
477 {
478 SCommand_VSync() :
479 SCommand(CMD_VSYNC) {}
480
481 int m_VSync;
482 bool *m_pRetOk;
483 };
484
485 struct SCommand_MultiSampling : public SCommand
486 {
487 SCommand_MultiSampling() :
488 SCommand(CMD_MULTISAMPLING) {}
489
490 uint32_t m_RequestedMultiSamplingCount;
491 uint32_t *m_pRetMultiSamplingCount;
492 bool *m_pRetOk;
493 };
494
495 struct SCommand_Update_Viewport : public SCommand
496 {
497 SCommand_Update_Viewport() :
498 SCommand(CMD_UPDATE_VIEWPORT) {}
499
500 int m_X;
501 int m_Y;
502 int m_Width;
503 int m_Height;
504 bool m_ByResize; // resized by an resize event.. a hint to make clear that the viewport update can be deferred if wanted
505 };
506
507 struct SCommand_Texture_Create : public SCommand
508 {
509 SCommand_Texture_Create() :
510 SCommand(CMD_TEXTURE_CREATE) {}
511
512 // texture information
513 int m_Slot;
514
515 size_t m_Width;
516 size_t m_Height;
517 int m_Flags;
518 // data must be in RGBA format
519 uint8_t *m_pData; // will be freed by the command processor
520 };
521
522 struct SCommand_Texture_Destroy : public SCommand
523 {
524 SCommand_Texture_Destroy() :
525 SCommand(CMD_TEXTURE_DESTROY) {}
526
527 // texture information
528 int m_Slot;
529 };
530
531 struct SCommand_TextTextures_Create : public SCommand
532 {
533 SCommand_TextTextures_Create() :
534 SCommand(CMD_TEXT_TEXTURES_CREATE) {}
535
536 // texture information
537 int m_Slot;
538 int m_SlotOutline;
539
540 size_t m_Width;
541 size_t m_Height;
542
543 uint8_t *m_pTextData; // will be freed by the command processor
544 uint8_t *m_pTextOutlineData; // will be freed by the command processor
545 };
546
547 struct SCommand_TextTextures_Destroy : public SCommand
548 {
549 SCommand_TextTextures_Destroy() :
550 SCommand(CMD_TEXT_TEXTURES_DESTROY) {}
551
552 // texture information
553 int m_Slot;
554 int m_SlotOutline;
555 };
556
557 struct SCommand_TextTexture_Update : public SCommand
558 {
559 SCommand_TextTexture_Update() :
560 SCommand(CMD_TEXT_TEXTURE_UPDATE) {}
561
562 // texture information
563 int m_Slot;
564
565 int m_X;
566 int m_Y;
567 size_t m_Width;
568 size_t m_Height;
569 uint8_t *m_pData; // will be freed by the command processor
570 };
571
572 struct SCommand_WindowCreateNtf : public CCommandBuffer::SCommand
573 {
574 SCommand_WindowCreateNtf() :
575 SCommand(CMD_WINDOW_CREATE_NTF) {}
576
577 uint32_t m_WindowId;
578 };
579
580 struct SCommand_WindowDestroyNtf : public CCommandBuffer::SCommand
581 {
582 SCommand_WindowDestroyNtf() :
583 SCommand(CMD_WINDOW_DESTROY_NTF) {}
584
585 uint32_t m_WindowId;
586 };
587
588 //
589 CCommandBuffer(unsigned CmdBufferSize, unsigned DataBufferSize) :
590 m_CmdBuffer(CmdBufferSize), m_DataBuffer(DataBufferSize), m_pCmdBufferHead(nullptr), m_pCmdBufferTail(nullptr)
591 {
592 }
593
594 void *AllocData(unsigned WantedSize)
595 {
596 return m_DataBuffer.Alloc(Requested: WantedSize);
597 }
598
599 template<class T>
600 bool AddCommandUnsafe(const T &Command)
601 {
602 // make sure that we don't do something stupid like ->AddCommand(&Cmd);
603 (void)static_cast<const SCommand *>(&Command);
604
605 // allocate and copy the command into the buffer
606 T *pCmd = (T *)m_CmdBuffer.Alloc(Requested: sizeof(*pCmd), Alignment: alignof(T));
607 if(!pCmd)
608 return false;
609 *pCmd = Command;
610 pCmd->m_pNext = nullptr;
611
612 if(m_pCmdBufferTail)
613 m_pCmdBufferTail->m_pNext = pCmd;
614 if(!m_pCmdBufferHead)
615 m_pCmdBufferHead = pCmd;
616 m_pCmdBufferTail = pCmd;
617
618 ++m_CommandCount;
619
620 return true;
621 }
622
623 const SCommand *Head() const { return m_pCmdBufferHead; }
624 SCommand *Head() { return m_pCmdBufferHead; }
625
626 void Reset()
627 {
628 m_pCmdBufferHead = m_pCmdBufferTail = nullptr;
629 m_CmdBuffer.Reset();
630 m_DataBuffer.Reset();
631
632 m_CommandCount = 0;
633 m_RenderCallCount = 0;
634 }
635
636 void AddRenderCalls(size_t RenderCallCountToAdd)
637 {
638 m_RenderCallCount += RenderCallCountToAdd;
639 }
640
641private:
642 SCommand *m_pCmdBufferHead;
643 SCommand *m_pCmdBufferTail;
644};
645
646enum EGraphicsBackendErrorCodes
647{
648 GRAPHICS_BACKEND_ERROR_CODE_NONE = 0,
649 GRAPHICS_BACKEND_ERROR_CODE_GL_CONTEXT_FAILED,
650 GRAPHICS_BACKEND_ERROR_CODE_GL_VERSION_FAILED,
651 GRAPHICS_BACKEND_ERROR_CODE_GLEW_INIT_FAILED,
652 GRAPHICS_BACKEND_ERROR_CODE_SDL_INIT_FAILED,
653 GRAPHICS_BACKEND_ERROR_CODE_SDL_SCREEN_REQUEST_FAILED,
654 GRAPHICS_BACKEND_ERROR_CODE_SDL_SCREEN_INFO_REQUEST_FAILED,
655 GRAPHICS_BACKEND_ERROR_CODE_SDL_SCREEN_RESOLUTION_REQUEST_FAILED,
656 GRAPHICS_BACKEND_ERROR_CODE_SDL_WINDOW_CREATE_FAILED,
657};
658
659// interface for the graphics backend
660// all these functions are called on the main thread
661class IGraphicsBackend
662{
663public:
664 enum
665 {
666 INITFLAG_FULLSCREEN = 1 << 0,
667 INITFLAG_VSYNC = 1 << 1,
668 INITFLAG_RESIZABLE = 1 << 2,
669 INITFLAG_BORDERLESS = 1 << 3,
670 INITFLAG_DESKTOP_FULLSCREEN = 1 << 4,
671 };
672
673 virtual ~IGraphicsBackend() = default;
674
675 virtual int Init(const char *pName, int *pScreen, int *pWidth, int *pHeight, int *pRefreshRate, int *pFsaaSamples, int Flags, int *pDesktopWidth, int *pDesktopHeight, int *pCurrentWidth, int *pCurrentHeight, class IStorage *pStorage) = 0;
676 virtual int Shutdown() = 0;
677
678 virtual uint64_t TextureMemoryUsage() const = 0;
679 virtual uint64_t BufferMemoryUsage() const = 0;
680 virtual uint64_t StreamedMemoryUsage() const = 0;
681 virtual uint64_t StagingMemoryUsage() const = 0;
682
683 virtual const TTwGraphicsGpuList &GetGpus() const = 0;
684
685 virtual void GetVideoModes(CVideoMode *pModes, int MaxModes, int *pNumModes, float HiDPIScale, int MaxWindowWidth, int MaxWindowHeight, int Screen) = 0;
686 virtual void GetCurrentVideoMode(CVideoMode &CurMode, float HiDPIScale, int MaxWindowWidth, int MaxWindowHeight, int Screen) = 0;
687
688 virtual int GetNumScreens() const = 0;
689 virtual const char *GetScreenName(int Screen) const = 0;
690
691 virtual void Minimize() = 0;
692 virtual void SetWindowParams(int FullscreenMode, bool IsBorderless) = 0;
693 virtual bool SetWindowScreen(int Index, bool MoveToCenter) = 0;
694 virtual bool UpdateDisplayMode(int Index) = 0;
695 virtual int GetWindowScreen() = 0;
696 virtual int WindowActive() = 0;
697 virtual int WindowOpen() = 0;
698 virtual void SetWindowGrab(bool Grab) = 0;
699 // returns true, if the video mode changed
700 virtual bool ResizeWindow(int w, int h, int RefreshRate) = 0;
701 virtual void GetViewportSize(int &w, int &h) = 0;
702 virtual void NotifyWindow() = 0;
703 virtual bool IsScreenKeyboardShown() = 0;
704
705 virtual void WindowDestroyNtf(uint32_t WindowId) = 0;
706 virtual void WindowCreateNtf(uint32_t WindowId) = 0;
707
708 virtual void RunBuffer(CCommandBuffer *pBuffer) = 0;
709 virtual void RunBufferSingleThreadedUnsafe(CCommandBuffer *pBuffer) = 0;
710 virtual bool IsIdle() const = 0;
711 virtual void WaitForIdle() = 0;
712
713 virtual bool GetDriverVersion(EGraphicsDriverAgeType DriverAgeType, int &Major, int &Minor, int &Patch, const char *&pName, EBackendType BackendType) = 0;
714 // checks if the current values of the config are a graphics modern API
715 virtual bool IsConfigModernAPI() { return false; }
716 virtual bool UseTrianglesAsQuad() { return false; }
717 virtual bool HasTileBuffering() { return false; }
718 virtual bool HasQuadBuffering() { return false; }
719 virtual bool HasTextBuffering() { return false; }
720 virtual bool HasQuadContainerBuffering() { return false; }
721 virtual bool Uses2DTextureArrays() { return false; }
722 virtual bool HasTextureArraysSupport() { return false; }
723 virtual const char *GetErrorString() { return nullptr; }
724
725 virtual const char *GetVendorString() = 0;
726 virtual const char *GetVersionString() = 0;
727 virtual const char *GetRendererString() = 0;
728
729 // be aware that this function should only be called from the graphics thread, and even then you should really know what you are doing
730 virtual TGLBackendReadPresentedImageData &GetReadPresentedImageDataFuncUnsafe() = 0;
731
732 virtual bool GetWarning(std::vector<std::string> &WarningStrings) = 0;
733
734 /**
735 * @see IGraphics::ShowMessageBox
736 */
737 virtual std::optional<int> ShowMessageBox(const IGraphics::CMessageBox &MessageBox) = 0;
738};
739
740class CGraphics_Threaded : public IEngineGraphics
741{
742 enum class EDrawing
743 {
744 NONE,
745 QUADS,
746 LINES,
747 TRIANGLES,
748 };
749
750 CCommandBuffer::SState m_State;
751 IGraphicsBackend *m_pBackend;
752 bool m_GLTileBufferingEnabled;
753 bool m_GLQuadBufferingEnabled;
754 bool m_GLTextBufferingEnabled;
755 bool m_GLQuadContainerBufferingEnabled;
756 bool m_GLUses2DTextureArrays;
757 bool m_GLHasTextureArraysSupport;
758 bool m_GLUseTrianglesAsQuad;
759
760 CCommandBuffer *m_apCommandBuffers[2];
761 CCommandBuffer *m_pCommandBuffer;
762 unsigned m_CurrentCommandBuffer;
763
764 //
765 class IStorage *m_pStorage;
766 class IEngine *m_pEngine;
767
768 int m_CurIndex;
769
770 CCommandBuffer::SVertex m_aVertices[CCommandBuffer::MAX_VERTICES];
771 CCommandBuffer::SVertexTex3DStream m_aVerticesTex3D[CCommandBuffer::MAX_VERTICES];
772 int m_NumVertices;
773
774 CCommandBuffer::SColor m_aColor[4];
775 CCommandBuffer::STexCoord m_aTexture[4];
776
777 bool m_RenderEnable;
778
779 float m_Rotation;
780 EDrawing m_Drawing;
781 bool m_DoScreenshot;
782 char m_aScreenshotName[IO_MAX_PATH_LENGTH];
783
784 CTextureHandle m_NullTexture;
785
786 std::vector<int> m_vTextureIndices;
787 size_t m_FirstFreeTexture;
788 int m_TextureMemoryUsage;
789
790 std::atomic<bool> m_WarnPngliteIncompatibleImages = false;
791
792 std::mutex m_WarningsMutex;
793 std::vector<SWarning> m_vWarnings;
794
795 // is a non full windowed (in a sense that the viewport won't include the whole window),
796 // forced viewport, so that it justifies our UI ratio needs
797 bool m_IsForcedViewport = false;
798
799 struct SVertexArrayInfo
800 {
801 SVertexArrayInfo() :
802 m_FreeIndex(-1) {}
803 // keep a reference to it, so we can free the ID
804 int m_AssociatedBufferObjectIndex;
805
806 int m_FreeIndex;
807 };
808 std::vector<SVertexArrayInfo> m_vVertexArrayInfo;
809 int m_FirstFreeVertexArrayInfo;
810
811 std::vector<int> m_vBufferObjectIndices;
812 int m_FirstFreeBufferObjectIndex;
813
814 struct SQuadContainer
815 {
816 SQuadContainer(bool AutomaticUpload = true)
817 {
818 m_vQuads.clear();
819 m_QuadBufferObjectIndex = m_QuadBufferContainerIndex = -1;
820 m_FreeIndex = -1;
821
822 m_AutomaticUpload = AutomaticUpload;
823 }
824
825 struct SQuad
826 {
827 CCommandBuffer::SVertex m_aVertices[4];
828 };
829
830 std::vector<SQuad> m_vQuads;
831
832 int m_QuadBufferObjectIndex;
833 int m_QuadBufferContainerIndex;
834
835 int m_FreeIndex;
836
837 bool m_AutomaticUpload;
838 };
839 std::vector<SQuadContainer> m_vQuadContainers;
840 int m_FirstFreeQuadContainer;
841
842 std::vector<WINDOW_RESIZE_FUNC> m_vResizeListeners;
843 std::vector<WINDOW_PROPS_CHANGED_FUNC> m_vPropChangeListeners;
844
845 void *AllocCommandBufferData(size_t AllocSize);
846
847 void AddVertices(int Count);
848 void AddVertices(int Count, CCommandBuffer::SVertex *pVertices);
849 void AddVertices(int Count, CCommandBuffer::SVertexTex3DStream *pVertices);
850
851 template<typename TName>
852 void Rotate(const CCommandBuffer::SPoint &Center, TName *pPoints, int NumPoints)
853 {
854 float c = std::cos(x: m_Rotation);
855 float s = std::sin(x: m_Rotation);
856 float x, y;
857 int i;
858
859 TName *pVertices = pPoints;
860 for(i = 0; i < NumPoints; i++)
861 {
862 x = pVertices[i].m_Pos.x - Center.x;
863 y = pVertices[i].m_Pos.y - Center.y;
864 pVertices[i].m_Pos.x = x * c - y * s + Center.x;
865 pVertices[i].m_Pos.y = x * s + y * c + Center.y;
866 }
867 }
868
869 template<typename TName>
870 void AddCmd(
871 TName &Cmd, const std::function<bool()> &FailFunc = [] { return true; })
872 {
873 if(m_pCommandBuffer->AddCommandUnsafe(Cmd))
874 return;
875
876 // kick command buffer and try again
877 KickCommandBuffer();
878
879 dbg_assert(FailFunc(), "graphics: failed to run fail handler for command '%s'", typeid(TName).name());
880 dbg_assert(m_pCommandBuffer->AddCommandUnsafe(Cmd), "graphics: failed to add command '%s' to command buffer", typeid(TName).name());
881 }
882
883 void KickCommandBuffer();
884
885 void AddBackEndWarningIfExists();
886
887 void AdjustViewport(bool SendViewportChangeToBackend);
888
889 ivec2 m_ReadPixelPosition = ivec2(0, 0);
890 ColorRGBA *m_pReadPixelColor = nullptr;
891 void ReadPixelDirect(bool *pSwapped);
892 void ScreenshotDirect(bool *pSwapped);
893
894 int IssueInit();
895 int InitWindow();
896
897public:
898 CGraphics_Threaded();
899
900 void ClipEnable(int x, int y, int w, int h) override;
901 void ClipDisable() override;
902
903 void BlendNone() override;
904 void BlendNormal() override;
905 void BlendAdditive() override;
906
907 void WrapNormal() override;
908 void WrapClamp() override;
909
910 uint64_t TextureMemoryUsage() const override;
911 uint64_t BufferMemoryUsage() const override;
912 uint64_t StreamedMemoryUsage() const override;
913 uint64_t StagingMemoryUsage() const override;
914
915 const TTwGraphicsGpuList &GetGpus() const override;
916
917 void MapScreen(float TopLeftX, float TopLeftY, float BottomRightX, float BottomRightY) override;
918 void GetScreen(float *pTopLeftX, float *pTopLeftY, float *pBottomRightX, float *pBottomRightY) const override;
919
920 void LinesBegin() override;
921 void LinesEnd() override;
922 void LinesDraw(const CLineItem *pArray, size_t Num) override;
923
924 void LinesBatchBegin(CLineItemBatch *pBatch) override;
925 void LinesBatchEnd(CLineItemBatch *pBatch) override;
926 void LinesBatchDraw(CLineItemBatch *pBatch, const CLineItem *pArray, size_t Num) override;
927
928 IGraphics::CTextureHandle FindFreeTextureIndex();
929 void FreeTextureIndex(CTextureHandle *pIndex);
930 void UnloadTexture(IGraphics::CTextureHandle *pIndex) override;
931 void LoadTextureAddWarning(size_t Width, size_t Height, int Flags, const char *pTexName);
932 IGraphics::CTextureHandle LoadTextureRaw(const CImageInfo &Image, int Flags, const char *pTexName = nullptr) override;
933 IGraphics::CTextureHandle LoadTextureRawMove(CImageInfo &Image, int Flags, const char *pTexName = nullptr) override;
934
935 bool LoadTextTextures(size_t Width, size_t Height, CTextureHandle &TextTexture, CTextureHandle &TextOutlineTexture, uint8_t *pTextData, uint8_t *pTextOutlineData) override;
936 bool UnloadTextTextures(CTextureHandle &TextTexture, CTextureHandle &TextOutlineTexture) override;
937 bool UpdateTextTexture(CTextureHandle TextureId, int x, int y, size_t Width, size_t Height, uint8_t *pData, bool IsMovedPointer) override;
938
939 CTextureHandle LoadSpriteTexture(const CImageInfo &FromImageInfo, const struct CDataSprite *pSprite) override;
940
941 bool IsImageSubFullyTransparent(const CImageInfo &FromImageInfo, int x, int y, int w, int h) override;
942 bool IsSpriteTextureFullyTransparent(const CImageInfo &FromImageInfo, const struct CDataSprite *pSprite) override;
943
944 // simple uncompressed RGBA loaders
945 IGraphics::CTextureHandle LoadTexture(const char *pFilename, int StorageType, int Flags = 0) override;
946 bool LoadPng(CImageInfo &Image, const char *pFilename, int StorageType) override;
947 bool LoadPng(CImageInfo &Image, const uint8_t *pData, size_t DataSize, const char *pContextName) override;
948
949 bool CheckImageDivisibility(const char *pContextName, CImageInfo &Image, int DivX, int DivY, bool AllowResize) override;
950 bool IsImageFormatRgba(const char *pContextName, const CImageInfo &Image) override;
951
952 void TextureSet(CTextureHandle TextureId) override;
953
954 void Clear(float r, float g, float b, bool ForceClearNow = false) override;
955
956 void QuadsBegin() override;
957 void QuadsEnd() override;
958 void QuadsTex3DBegin() override;
959 void QuadsTex3DEnd() override;
960 void TrianglesBegin() override;
961 void TrianglesEnd() override;
962 void QuadsEndKeepVertices() override;
963 void QuadsDrawCurrentVertices(bool KeepVertices = true) override;
964 void QuadsSetRotation(float Angle) override;
965
966 template<typename TName>
967 void SetColor(TName *pVertex, int ColorIndex)
968 {
969 TName *pVert = pVertex;
970 pVert->m_Color = m_aColor[ColorIndex];
971 }
972
973 void SetColorVertex(const CColorVertex *pArray, size_t Num) override;
974 void SetColor(float r, float g, float b, float a) override;
975 void SetColor(ColorRGBA Color) override;
976 void SetColor4(ColorRGBA TopLeft, ColorRGBA TopRight, ColorRGBA BottomLeft, ColorRGBA BottomRight) override;
977
978 // go through all vertices and change their color (only works for quads)
979 void ChangeColorOfCurrentQuadVertices(float r, float g, float b, float a) override;
980 void ChangeColorOfQuadVertices(size_t QuadOffset, unsigned char r, unsigned char g, unsigned char b, unsigned char a) override;
981
982 void QuadsSetSubset(float TlU, float TlV, float BrU, float BrV) override;
983 void QuadsSetSubsetFree(
984 float x0, float y0, float x1, float y1,
985 float x2, float y2, float x3, float y3, int Index = -1) override;
986
987 void QuadsDraw(CQuadItem *pArray, int Num) override;
988
989 template<typename TName>
990 void QuadsDrawTLImpl(TName *pVertices, const CQuadItem *pArray, int Num)
991 {
992 CCommandBuffer::SPoint Center;
993
994 dbg_assert(m_Drawing == EDrawing::QUADS, "called Graphics()->QuadsDrawTL without begin");
995
996 if(g_Config.m_GfxQuadAsTriangle && !m_GLUseTrianglesAsQuad)
997 {
998 for(int i = 0; i < Num; ++i)
999 {
1000 // first triangle
1001 pVertices[m_NumVertices + 6 * i].m_Pos.x = pArray[i].m_X;
1002 pVertices[m_NumVertices + 6 * i].m_Pos.y = pArray[i].m_Y;
1003 pVertices[m_NumVertices + 6 * i].m_Tex = m_aTexture[0];
1004 SetColor(&pVertices[m_NumVertices + 6 * i], 0);
1005
1006 pVertices[m_NumVertices + 6 * i + 1].m_Pos.x = pArray[i].m_X + pArray[i].m_Width;
1007 pVertices[m_NumVertices + 6 * i + 1].m_Pos.y = pArray[i].m_Y;
1008 pVertices[m_NumVertices + 6 * i + 1].m_Tex = m_aTexture[1];
1009 SetColor(&pVertices[m_NumVertices + 6 * i + 1], 1);
1010
1011 pVertices[m_NumVertices + 6 * i + 2].m_Pos.x = pArray[i].m_X + pArray[i].m_Width;
1012 pVertices[m_NumVertices + 6 * i + 2].m_Pos.y = pArray[i].m_Y + pArray[i].m_Height;
1013 pVertices[m_NumVertices + 6 * i + 2].m_Tex = m_aTexture[2];
1014 SetColor(&pVertices[m_NumVertices + 6 * i + 2], 2);
1015
1016 // second triangle
1017 pVertices[m_NumVertices + 6 * i + 3].m_Pos.x = pArray[i].m_X;
1018 pVertices[m_NumVertices + 6 * i + 3].m_Pos.y = pArray[i].m_Y;
1019 pVertices[m_NumVertices + 6 * i + 3].m_Tex = m_aTexture[0];
1020 SetColor(&pVertices[m_NumVertices + 6 * i + 3], 0);
1021
1022 pVertices[m_NumVertices + 6 * i + 4].m_Pos.x = pArray[i].m_X + pArray[i].m_Width;
1023 pVertices[m_NumVertices + 6 * i + 4].m_Pos.y = pArray[i].m_Y + pArray[i].m_Height;
1024 pVertices[m_NumVertices + 6 * i + 4].m_Tex = m_aTexture[2];
1025 SetColor(&pVertices[m_NumVertices + 6 * i + 4], 2);
1026
1027 pVertices[m_NumVertices + 6 * i + 5].m_Pos.x = pArray[i].m_X;
1028 pVertices[m_NumVertices + 6 * i + 5].m_Pos.y = pArray[i].m_Y + pArray[i].m_Height;
1029 pVertices[m_NumVertices + 6 * i + 5].m_Tex = m_aTexture[3];
1030 SetColor(&pVertices[m_NumVertices + 6 * i + 5], 3);
1031
1032 if(m_Rotation != 0)
1033 {
1034 Center.x = pArray[i].m_X + pArray[i].m_Width / 2;
1035 Center.y = pArray[i].m_Y + pArray[i].m_Height / 2;
1036
1037 Rotate(Center, &pVertices[m_NumVertices + 6 * i], 6);
1038 }
1039 }
1040
1041 AddVertices(3 * 2 * Num, pVertices);
1042 }
1043 else
1044 {
1045 for(int i = 0; i < Num; ++i)
1046 {
1047 pVertices[m_NumVertices + 4 * i].m_Pos.x = pArray[i].m_X;
1048 pVertices[m_NumVertices + 4 * i].m_Pos.y = pArray[i].m_Y;
1049 pVertices[m_NumVertices + 4 * i].m_Tex = m_aTexture[0];
1050 SetColor(&pVertices[m_NumVertices + 4 * i], 0);
1051
1052 pVertices[m_NumVertices + 4 * i + 1].m_Pos.x = pArray[i].m_X + pArray[i].m_Width;
1053 pVertices[m_NumVertices + 4 * i + 1].m_Pos.y = pArray[i].m_Y;
1054 pVertices[m_NumVertices + 4 * i + 1].m_Tex = m_aTexture[1];
1055 SetColor(&pVertices[m_NumVertices + 4 * i + 1], 1);
1056
1057 pVertices[m_NumVertices + 4 * i + 2].m_Pos.x = pArray[i].m_X + pArray[i].m_Width;
1058 pVertices[m_NumVertices + 4 * i + 2].m_Pos.y = pArray[i].m_Y + pArray[i].m_Height;
1059 pVertices[m_NumVertices + 4 * i + 2].m_Tex = m_aTexture[2];
1060 SetColor(&pVertices[m_NumVertices + 4 * i + 2], 2);
1061
1062 pVertices[m_NumVertices + 4 * i + 3].m_Pos.x = pArray[i].m_X;
1063 pVertices[m_NumVertices + 4 * i + 3].m_Pos.y = pArray[i].m_Y + pArray[i].m_Height;
1064 pVertices[m_NumVertices + 4 * i + 3].m_Tex = m_aTexture[3];
1065 SetColor(&pVertices[m_NumVertices + 4 * i + 3], 3);
1066
1067 if(m_Rotation != 0)
1068 {
1069 Center.x = pArray[i].m_X + pArray[i].m_Width / 2;
1070 Center.y = pArray[i].m_Y + pArray[i].m_Height / 2;
1071
1072 Rotate(Center, &pVertices[m_NumVertices + 4 * i], 4);
1073 }
1074 }
1075
1076 AddVertices(4 * Num, pVertices);
1077 }
1078 }
1079
1080 void QuadsDrawTL(const CQuadItem *pArray, int Num) override;
1081
1082 void QuadsTex3DDrawTL(const CQuadItem *pArray, int Num) override;
1083
1084 void QuadsDrawFreeform(const CFreeformItem *pArray, int Num) override;
1085 void QuadsText(float x, float y, float Size, const char *pText) override;
1086
1087 void DrawRectExt(float x, float y, float w, float h, float r, int Corners) override;
1088 void DrawRectExt4(float x, float y, float w, float h, ColorRGBA ColorTopLeft, ColorRGBA ColorTopRight, ColorRGBA ColorBottomLeft, ColorRGBA ColorBottomRight, float r, int Corners) override;
1089 int CreateRectQuadContainer(float x, float y, float w, float h, float r, int Corners) override;
1090 void DrawRect(float x, float y, float w, float h, ColorRGBA Color, int Corners, float Rounding) override;
1091 void DrawRect4(float x, float y, float w, float h, ColorRGBA ColorTopLeft, ColorRGBA ColorTopRight, ColorRGBA ColorBottomLeft, ColorRGBA ColorBottomRight, int Corners, float Rounding) override;
1092 void DrawCircle(float CenterX, float CenterY, float Radius, int Segments) override;
1093
1094 int CreateQuadContainer(bool AutomaticUpload = true) override;
1095 void QuadContainerChangeAutomaticUpload(int ContainerIndex, bool AutomaticUpload) override;
1096 void QuadContainerUpload(int ContainerIndex) override;
1097 int QuadContainerAddQuads(int ContainerIndex, CQuadItem *pArray, int Num) override;
1098 int QuadContainerAddQuads(int ContainerIndex, CFreeformItem *pArray, int Num) override;
1099 void QuadContainerReset(int ContainerIndex) override;
1100 void DeleteQuadContainer(int &ContainerIndex) override;
1101 void RenderQuadContainer(int ContainerIndex, int QuadDrawNum) override;
1102 void RenderQuadContainer(int ContainerIndex, int QuadOffset, int QuadDrawNum, bool ChangeWrapMode = true) override;
1103 void RenderQuadContainerEx(int ContainerIndex, int QuadOffset, int QuadDrawNum, float X, float Y, float ScaleX = 1.f, float ScaleY = 1.f) override;
1104 void RenderQuadContainerAsSprite(int ContainerIndex, int QuadOffset, float X, float Y, float ScaleX = 1.f, float ScaleY = 1.f) override;
1105 void RenderQuadContainerAsSpriteMultiple(int ContainerIndex, int QuadOffset, int DrawCount, SRenderSpriteInfo *pRenderInfo) override;
1106
1107 // sprites
1108private:
1109 vec2 m_SpriteScale = vec2(-1.0f, -1.0f);
1110
1111protected:
1112 void SelectSprite(const CDataSprite *pSprite, int Flags);
1113
1114public:
1115 void SelectSprite(int Id, int Flags = 0) override;
1116 void SelectSprite7(int Id, int Flags = 0) override;
1117
1118 void GetSpriteScale(const CDataSprite *pSprite, float &ScaleX, float &ScaleY) const override;
1119 void GetSpriteScale(int Id, float &ScaleX, float &ScaleY) const override;
1120 void GetSpriteScaleImpl(int Width, int Height, float &ScaleX, float &ScaleY) const override;
1121
1122 void DrawSprite(float x, float y, float Size) override;
1123 void DrawSprite(float x, float y, float ScaledWidth, float ScaledHeight) override;
1124
1125 int QuadContainerAddSprite(int QuadContainerIndex, float x, float y, float Size) override;
1126 int QuadContainerAddSprite(int QuadContainerIndex, float Size) override;
1127 int QuadContainerAddSprite(int QuadContainerIndex, float Width, float Height) override;
1128 int QuadContainerAddSprite(int QuadContainerIndex, float X, float Y, float Width, float Height) override;
1129
1130 template<typename TName>
1131 void FlushVerticesImpl(bool KeepVertices, EPrimitiveType &PrimType, size_t &PrimCount, size_t &NumVerts, TName &Command, size_t VertSize)
1132 {
1133 Command.m_pVertices = nullptr;
1134 if(m_NumVertices == 0)
1135 return;
1136
1137 NumVerts = m_NumVertices;
1138
1139 if(!KeepVertices)
1140 m_NumVertices = 0;
1141
1142 if(m_Drawing == EDrawing::QUADS)
1143 {
1144 if(g_Config.m_GfxQuadAsTriangle && !m_GLUseTrianglesAsQuad)
1145 {
1146 PrimType = EPrimitiveType::TRIANGLES;
1147 PrimCount = NumVerts / 3;
1148 }
1149 else
1150 {
1151 PrimType = EPrimitiveType::QUADS;
1152 PrimCount = NumVerts / 4;
1153 }
1154 }
1155 else if(m_Drawing == EDrawing::LINES)
1156 {
1157 PrimType = EPrimitiveType::LINES;
1158 PrimCount = NumVerts / 2;
1159 }
1160 else if(m_Drawing == EDrawing::TRIANGLES)
1161 {
1162 PrimType = EPrimitiveType::TRIANGLES;
1163 PrimCount = NumVerts / 3;
1164 }
1165 else
1166 return;
1167
1168 Command.m_pVertices = (decltype(Command.m_pVertices))AllocCommandBufferData(AllocSize: VertSize * NumVerts);
1169 Command.m_State = m_State;
1170
1171 Command.m_PrimType = PrimType;
1172 Command.m_PrimCount = PrimCount;
1173
1174 AddCmd(Command, [&] {
1175 Command.m_pVertices = (decltype(Command.m_pVertices))m_pCommandBuffer->AllocData(WantedSize: VertSize * NumVerts);
1176 return Command.m_pVertices != nullptr;
1177 });
1178
1179 m_pCommandBuffer->AddRenderCalls(RenderCallCountToAdd: 1);
1180 }
1181
1182 void FlushVertices(bool KeepVertices = false) override;
1183 void FlushVerticesTex3D() override;
1184
1185 void RenderTileLayer(int BufferContainerIndex, const ColorRGBA &Color, char **pOffsets, unsigned int *pIndicedVertexDrawNum, size_t NumIndicesOffset) override;
1186 void RenderBorderTiles(int BufferContainerIndex, const ColorRGBA &Color, char *pIndexBufferOffset, const vec2 &Offset, const vec2 &Scale, uint32_t DrawNum) override;
1187 void RenderQuadLayer(int BufferContainerIndex, SQuadRenderInfo *pQuadInfo, size_t QuadNum, int QuadOffset, bool Grouped = false) override;
1188 void RenderText(int BufferContainerIndex, int TextQuadNum, int TextureSize, int TextureTextIndex, int TextureTextOutlineIndex, const ColorRGBA &TextColor, const ColorRGBA &TextOutlineColor) override;
1189
1190 // modern GL functions
1191 int CreateBufferObject(size_t UploadDataSize, void *pUploadData, int CreateFlags, bool IsMovedPointer = false) override;
1192 void RecreateBufferObject(int BufferIndex, size_t UploadDataSize, void *pUploadData, int CreateFlags, bool IsMovedPointer = false) override;
1193 void UpdateBufferObjectInternal(int BufferIndex, size_t UploadDataSize, void *pUploadData, void *pOffset, bool IsMovedPointer = false);
1194 void CopyBufferObjectInternal(int WriteBufferIndex, int ReadBufferIndex, size_t WriteOffset, size_t ReadOffset, size_t CopyDataSize);
1195 void DeleteBufferObject(int BufferIndex) override;
1196
1197 int CreateBufferContainer(SBufferContainerInfo *pContainerInfo) override;
1198 // destroying all buffer objects means, that all referenced VBOs are destroyed automatically, so the user does not need to save references to them
1199 void DeleteBufferContainer(int &ContainerIndex, bool DestroyAllBO = true) override;
1200 void UpdateBufferContainerInternal(int ContainerIndex, SBufferContainerInfo *pContainerInfo);
1201 void IndicesNumRequiredNotify(unsigned int RequiredIndicesCount) override;
1202
1203 int GetNumScreens() const override;
1204 const char *GetScreenName(int Screen) const override;
1205
1206 void Minimize() override;
1207 void WarnPngliteIncompatibleImages(bool Warn) override;
1208 void SetWindowParams(int FullscreenMode, bool IsBorderless) override;
1209 bool SetWindowScreen(int Index, bool MoveToCenter) override;
1210 bool SwitchWindowScreen(int Index, bool MoveToCenter) override;
1211 void Move(int x, int y) override;
1212 bool Resize(int w, int h, int RefreshRate) override;
1213 void ResizeToScreen() override;
1214 void GotResized(int w, int h, int RefreshRate) override;
1215 void UpdateViewport(int X, int Y, int W, int H, bool ByResize) override;
1216 bool IsScreenKeyboardShown() override;
1217
1218 void AddWindowResizeListener(WINDOW_RESIZE_FUNC pFunc) override;
1219 void AddWindowPropChangeListener(WINDOW_PROPS_CHANGED_FUNC pFunc) override;
1220 int GetWindowScreen() override;
1221
1222 void WindowDestroyNtf(uint32_t WindowId) override;
1223 void WindowCreateNtf(uint32_t WindowId) override;
1224
1225 int WindowActive() override;
1226 int WindowOpen() override;
1227
1228 void SetWindowGrab(bool Grab) override;
1229 void NotifyWindow() override;
1230
1231 int Init() override;
1232 void Shutdown() override;
1233
1234 void ReadPixel(ivec2 Position, ColorRGBA *pColor) override;
1235 void TakeScreenshot(const char *pFilename) override;
1236 void TakeCustomScreenshot(const char *pFilename) override;
1237 void Swap() override;
1238 bool SetVSync(bool State) override;
1239 bool SetMultiSampling(uint32_t ReqMultiSamplingCount, uint32_t &MultiSamplingCountBackend) override;
1240
1241 int GetVideoModes(CVideoMode *pModes, int MaxModes, int Screen) override;
1242 void GetCurrentVideoMode(CVideoMode &CurMode, int Screen) override;
1243
1244 virtual int GetDesktopScreenWidth() const { return g_Config.m_GfxDesktopWidth; }
1245 virtual int GetDesktopScreenHeight() const { return g_Config.m_GfxDesktopHeight; }
1246
1247 // synchronization
1248 void InsertSignal(CSemaphore *pSemaphore) override;
1249 bool IsIdle() const override;
1250 void WaitForIdle() override;
1251
1252 void AddWarning(const SWarning &Warning);
1253 std::optional<SWarning> CurrentWarning() override;
1254
1255 std::optional<int> ShowMessageBox(const CMessageBox &MessageBox) override;
1256
1257 bool IsBackendInitialized() override;
1258
1259 bool GetDriverVersion(EGraphicsDriverAgeType DriverAgeType, int &Major, int &Minor, int &Patch, const char *&pName, EBackendType BackendType) override { return m_pBackend->GetDriverVersion(DriverAgeType, Major, Minor, Patch, pName, BackendType); }
1260 bool IsConfigModernAPI() override { return m_pBackend->IsConfigModernAPI(); }
1261 bool IsTileBufferingEnabled() override { return m_GLTileBufferingEnabled; }
1262 bool IsQuadBufferingEnabled() override { return m_GLQuadBufferingEnabled; }
1263 bool IsTextBufferingEnabled() override { return m_GLTextBufferingEnabled; }
1264 bool IsQuadContainerBufferingEnabled() override { return m_GLQuadContainerBufferingEnabled; }
1265 bool Uses2DTextureArrays() override { return m_GLUses2DTextureArrays; }
1266 bool HasTextureArraysSupport() override { return m_GLHasTextureArraysSupport; }
1267
1268 const char *GetVendorString() override;
1269 const char *GetVersionString() override;
1270 const char *GetRendererString() override;
1271
1272 TGLBackendReadPresentedImageData &GetReadPresentedImageDataFuncUnsafe() override;
1273};
1274
1275typedef std::function<const char *(const char *, const char *)> TTranslateFunc;
1276extern IGraphicsBackend *CreateGraphicsBackend(TTranslateFunc &&TranslateFunc);
1277
1278#endif // ENGINE_CLIENT_GRAPHICS_THREADED_H
1279