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