DDraceNetwork Docs
backend_sdl.h
Go to the documentation of this file.
1#ifndef ENGINE_CLIENT_BACKEND_SDL_H
2#define ENGINE_CLIENT_BACKEND_SDL_H
3
4#include <SDL_video.h>
5
6#include <base/detect.h>
7
8#include <engine/graphics.h>
9
11
13
14#include <atomic>
15#include <condition_variable>
16#include <cstddef>
17#include <cstdint>
18#include <mutex>
19#include <vector>
20
21#if defined(CONF_PLATFORM_MACOS)
22#include <objc/objc-runtime.h>
23
24class CAutoreleasePool
25{
26private:
27 id m_Pool;
28
29public:
30 CAutoreleasePool()
31 {
32 Class NSAutoreleasePoolClass = (Class)objc_getClass("NSAutoreleasePool");
33 m_Pool = class_createInstance(NSAutoreleasePoolClass, 0);
34 SEL selector = sel_registerName("init");
35 ((id(*)(id, SEL))objc_msgSend)(m_Pool, selector);
36 }
37
38 ~CAutoreleasePool()
39 {
40 SEL selector = sel_registerName("drain");
41 ((id(*)(id, SEL))objc_msgSend)(m_Pool, selector);
42 }
43};
44#endif
45
46// basic threaded backend, abstract, missing init and shutdown functions
48{
49private:
52
53public:
54 // constructed on the main thread, the rest of the functions is run on the render thread
56 {
57 public:
58 virtual ~ICommandProcessor() = default;
59 virtual void RunBuffer(CCommandBuffer *pBuffer) = 0;
60
61 virtual const SGfxErrorContainer &GetError() const = 0;
62 virtual void ErroneousCleanup() = 0;
63
64 virtual const SGfxWarningContainer &GetWarning() const = 0;
65 };
66
68
69 void RunBuffer(CCommandBuffer *pBuffer) override;
70 void RunBufferSingleThreadedUnsafe(CCommandBuffer *pBuffer) override;
71 bool IsIdle() const override;
72 void WaitForIdle() override;
73
74 void ProcessError(const SGfxErrorContainer &Error);
75
76protected:
77 void StartProcessor(ICommandProcessor *pProcessor);
78 void StopProcessor();
79
81 {
83 }
84
85private:
88 std::condition_variable m_BufferSwapCond;
90 std::atomic_bool m_Shutdown;
91 bool m_Started = false;
92 std::atomic_bool m_BufferInProcess;
93 void *m_pThread;
94
95 static void ThreadFunc(void *pUser);
96
97public:
98 bool GetWarning(std::vector<std::string> &WarningStrings) override;
99};
100
101// takes care of implementation independent operations
103{
104 void Cmd_Nop();
105 void Cmd_Signal(const CCommandBuffer::SCommand_Signal *pCommand);
106
107public:
108 bool RunCommand(const CCommandBuffer::SCommand *pBaseCommand);
109};
110
112{
117
124
125 // use quads as much as possible, even if the user config says otherwise
127
131};
132
133// takes care of sdl related commands
135{
136 // SDL stuff
137 SDL_Window *m_pWindow = nullptr;
138 SDL_GLContext m_GLContext = nullptr;
139
140public:
141 enum
142 {
145 };
146
148 {
151 SDL_Window *m_pWindow;
152 SDL_GLContext m_GLContext;
153 };
154
156 {
159 };
160
161private:
162 void Cmd_Init(const SCommand_Init *pCommand);
163 void Cmd_Shutdown(const SCommand_Shutdown *pCommand);
164 void Cmd_Swap(const CCommandBuffer::SCommand_Swap *pCommand);
165 void Cmd_VSync(const CCommandBuffer::SCommand_VSync *pCommand);
168
169public:
171
172 bool RunCommand(const CCommandBuffer::SCommand *pBaseCommand);
173};
174
175// command processor implementation, uses the fragments to combine into one processor
177{
181
183
186
187public:
188 CCommandProcessor_SDL_GL(EBackendType BackendType, int GLMajor, int GLMinor, int GLPatch);
190 void RunBuffer(CCommandBuffer *pBuffer) override;
191
192 const SGfxErrorContainer &GetError() const override;
193 void ErroneousCleanup() override;
194
195 const SGfxWarningContainer &GetWarning() const override;
196
197 void HandleError();
198 void HandleWarning();
199};
200
201static constexpr size_t gs_GpuInfoStringSize = 256;
202
203// graphics backend implemented with SDL and the graphics library @see EBackendType
205{
206 SDL_Window *m_pWindow = nullptr;
207 SDL_GLContext m_GLContext = nullptr;
209 std::atomic<uint64_t> m_TextureMemoryUsage{0};
210 std::atomic<uint64_t> m_BufferMemoryUsage{0};
211 std::atomic<uint64_t> m_StreamMemoryUsage{0};
212 std::atomic<uint64_t> m_StagingMemoryUsage{0};
213
215
217
219
221
225
227
228 char m_aErrorString[256];
229
231 static void ClampDriverVersion(EBackendType BackendType);
232
233public:
235 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) override;
236 int Shutdown() override;
237
238 uint64_t TextureMemoryUsage() const override;
239 uint64_t BufferMemoryUsage() const override;
240 uint64_t StreamedMemoryUsage() const override;
241 uint64_t StagingMemoryUsage() const override;
242
243 const TTwGraphicsGpuList &GetGpus() const override;
244
245 int GetNumScreens() const override { return m_NumScreens; }
246 const char *GetScreenName(int Screen) const override;
247
248 void GetVideoModes(CVideoMode *pModes, int MaxModes, int *pNumModes, int HiDPIScale, int MaxWindowWidth, int MaxWindowHeight, int ScreenId) override;
249 void GetCurrentVideoMode(CVideoMode &CurMode, int HiDPIScale, int MaxWindowWidth, int MaxWindowHeight, int ScreenId) override;
250
251 void Minimize() override;
252 void Maximize() override;
253 void SetWindowParams(int FullscreenMode, bool IsBorderless) override;
254 bool SetWindowScreen(int Index) override;
255 bool UpdateDisplayMode(int Index) override;
256 int GetWindowScreen() override;
257 int WindowActive() override;
258 int WindowOpen() override;
259 void SetWindowGrab(bool Grab) override;
260 bool ResizeWindow(int w, int h, int RefreshRate) override;
261 void GetViewportSize(int &w, int &h) override;
262 void NotifyWindow() override;
263
264 void WindowDestroyNtf(uint32_t WindowId) override;
265 void WindowCreateNtf(uint32_t WindowId) override;
266
267 bool GetDriverVersion(EGraphicsDriverAgeType DriverAgeType, int &Major, int &Minor, int &Patch, const char *&pName, EBackendType BackendType) override;
268 bool IsConfigModernAPI() override { return IsModernAPI(m_BackendType); }
276
277 const char *GetErrorString() override
278 {
279 if(m_aErrorString[0] != '\0')
280 return m_aErrorString;
281
282 return NULL;
283 }
284
285 const char *GetVendorString() override
286 {
287 return m_aVendorString;
288 }
289
290 const char *GetVersionString() override
291 {
292 return m_aVersionString;
293 }
294
295 const char *GetRendererString() override
296 {
297 return m_aRendererString;
298 }
299
301
302 bool ShowMessageBox(unsigned Type, const char *pTitle, const char *pMsg) override;
303
304 static bool IsModernAPI(EBackendType BackendType);
305};
306
307#endif // ENGINE_CLIENT_BACKEND_SDL_H
@ GFX_WARNING_TYPE_NONE
Definition: backend_base.h:51
static constexpr size_t gs_GpuInfoStringSize
Definition: backend_sdl.h:201
Definition: graphics_threaded.h:16
@ CMDGROUP_PLATFORM_SDL
Definition: graphics_threaded.h:81
Definition: backend_base.h:82
Definition: backend_sdl.h:103
void Cmd_Signal(const CCommandBuffer::SCommand_Signal *pCommand)
Definition: backend_sdl.cpp:183
bool RunCommand(const CCommandBuffer::SCommand *pBaseCommand)
Definition: backend_sdl.cpp:188
Definition: backend_sdl.h:135
@ CMD_INIT
Definition: backend_sdl.h:143
@ CMD_SHUTDOWN
Definition: backend_sdl.h:144
void Cmd_WindowCreateNtf(const CCommandBuffer::SCommand_WindowCreateNtf *pCommand)
Definition: backend_sdl.cpp:227
void Cmd_VSync(const CCommandBuffer::SCommand_VSync *pCommand)
Definition: backend_sdl.cpp:221
SDL_GLContext m_GLContext
Definition: backend_sdl.h:138
bool RunCommand(const CCommandBuffer::SCommand *pBaseCommand)
Definition: backend_sdl.cpp:251
void Cmd_Init(const SCommand_Init *pCommand)
Definition: backend_sdl.cpp:201
void Cmd_Swap(const CCommandBuffer::SCommand_Swap *pCommand)
Definition: backend_sdl.cpp:215
void Cmd_WindowDestroyNtf(const CCommandBuffer::SCommand_WindowDestroyNtf *pCommand)
Definition: backend_sdl.cpp:239
SDL_Window * m_pWindow
Definition: backend_sdl.h:137
void Cmd_Shutdown(const SCommand_Shutdown *pCommand)
Definition: backend_sdl.cpp:209
Definition: backend_sdl.h:177
void RunBuffer(CCommandBuffer *pBuffer) override
Definition: backend_sdl.cpp:328
CCommandProcessorFragment_SDL m_SDL
Definition: backend_sdl.h:179
const SGfxErrorContainer & GetError() const override
Definition: backend_sdl.cpp:419
const SGfxWarningContainer & GetWarning() const override
Definition: backend_sdl.cpp:429
CCommandProcessor_SDL_GL(EBackendType BackendType, int GLMajor, int GLMinor, int GLPatch)
Definition: backend_sdl.cpp:364
virtual ~CCommandProcessor_SDL_GL()
Definition: backend_sdl.cpp:414
void HandleError()
Definition: backend_sdl.cpp:272
SGfxWarningContainer m_Warning
Definition: backend_sdl.h:185
void HandleWarning()
Definition: backend_sdl.cpp:306
SGfxErrorContainer m_Error
Definition: backend_sdl.h:184
void ErroneousCleanup() override
Definition: backend_sdl.cpp:424
CCommandProcessorFragment_General m_General
Definition: backend_sdl.h:180
EBackendType m_BackendType
Definition: backend_sdl.h:182
CCommandProcessorFragment_GLBase * m_pGLBackend
Definition: backend_sdl.h:178
Definition: backend_sdl.h:205
const char * GetScreenName(int Screen) const override
Definition: backend_sdl.cpp:886
int WindowActive() override
Definition: backend_sdl.cpp:1563
void SetWindowGrab(bool Grab) override
Definition: backend_sdl.cpp:1573
void WindowDestroyNtf(uint32_t WindowId) override
Definition: backend_sdl.cpp:1633
SDL_GLContext m_GLContext
Definition: backend_sdl.h:207
bool ResizeWindow(int w, int h, int RefreshRate) override
Definition: backend_sdl.cpp:1578
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) override
Definition: backend_sdl.cpp:1004
char m_aVendorString[gs_GpuInfoStringSize]
Definition: backend_sdl.h:222
void GetVideoModes(CVideoMode *pModes, int MaxModes, int *pNumModes, int HiDPIScale, int MaxWindowWidth, int MaxWindowHeight, int ScreenId) override
Definition: backend_sdl.cpp:905
const char * GetRendererString() override
Definition: backend_sdl.h:295
std::atomic< uint64_t > m_StagingMemoryUsage
Definition: backend_sdl.h:212
const char * GetVendorString() override
Definition: backend_sdl.h:285
bool HasQuadContainerBuffering() override
Definition: backend_sdl.h:273
void GetCurrentVideoMode(CVideoMode &CurMode, int HiDPIScale, int MaxWindowWidth, int MaxWindowHeight, int ScreenId) override
Definition: backend_sdl.cpp:969
bool UpdateDisplayMode(int Index) override
Definition: backend_sdl.cpp:1542
static void ClampDriverVersion(EBackendType BackendType)
Definition: backend_sdl.cpp:723
uint64_t StagingMemoryUsage() const override
Definition: backend_sdl.cpp:1451
ICommandProcessor * m_pProcessor
Definition: backend_sdl.h:208
TGLBackendReadPresentedImageData m_ReadPresentedImageDataFunc
Definition: backend_sdl.h:216
void Maximize() override
Definition: backend_sdl.cpp:1466
bool SetWindowScreen(int Index) override
Definition: backend_sdl.cpp:1520
void SetWindowParams(int FullscreenMode, bool IsBorderless) override
Definition: backend_sdl.cpp:1471
std::atomic< uint64_t > m_StreamMemoryUsage
Definition: backend_sdl.h:211
uint64_t StreamedMemoryUsage() const override
Definition: backend_sdl.cpp:1446
bool HasTextBuffering() override
Definition: backend_sdl.h:272
SDL_Window * m_pWindow
Definition: backend_sdl.h:206
int GetWindowScreen() override
Definition: backend_sdl.cpp:1558
std::atomic< uint64_t > m_BufferMemoryUsage
Definition: backend_sdl.h:210
int GetNumScreens() const override
Definition: backend_sdl.h:245
const TTwGraphicsGpuList & GetGpus() const override
Definition: backend_sdl.cpp:1456
TGLBackendReadPresentedImageData & GetReadPresentedImageDataFuncUnsafe() override
Definition: backend_sdl.cpp:1642
bool Uses2DTextureArrays() override
Definition: backend_sdl.h:274
int m_NumScreens
Definition: backend_sdl.h:218
static bool IsModernAPI(EBackendType BackendType)
Definition: backend_sdl.cpp:795
bool HasTextureArraysSupport() override
Definition: backend_sdl.h:275
uint64_t BufferMemoryUsage() const override
Definition: backend_sdl.cpp:1441
void Minimize() override
Definition: backend_sdl.cpp:1461
const char * GetErrorString() override
Definition: backend_sdl.h:277
bool UseTrianglesAsQuad() override
Definition: backend_sdl.h:269
bool IsConfigModernAPI() override
Definition: backend_sdl.h:268
int Shutdown() override
Definition: backend_sdl.cpp:1401
TTwGraphicsGpuList m_GpuList
Definition: backend_sdl.h:214
char m_aErrorString[256]
Definition: backend_sdl.h:228
std::atomic< uint64_t > m_TextureMemoryUsage
Definition: backend_sdl.h:209
bool HasQuadBuffering() override
Definition: backend_sdl.h:271
bool HasTileBuffering() override
Definition: backend_sdl.h:270
EBackendType m_BackendType
Definition: backend_sdl.h:226
void GetViewportSize(int &w, int &h) override
Definition: backend_sdl.cpp:1616
char m_aRendererString[gs_GpuInfoStringSize]
Definition: backend_sdl.h:224
bool ShowMessageBox(unsigned Type, const char *pTitle, const char *pMsg) override
Definition: backend_sdl.cpp:778
CGraphicsBackend_SDL_GL(TTranslateFunc &&TranslateFunc)
Definition: backend_sdl.cpp:998
const char * GetVersionString() override
Definition: backend_sdl.h:290
void WindowCreateNtf(uint32_t WindowId) override
Definition: backend_sdl.cpp:1637
void NotifyWindow() override
Definition: backend_sdl.cpp:1621
SBackendCapabilites m_Capabilites
Definition: backend_sdl.h:220
static EBackendType DetectBackend()
Definition: backend_sdl.cpp:688
int WindowOpen() override
Definition: backend_sdl.cpp:1568
uint64_t TextureMemoryUsage() const override
Definition: backend_sdl.cpp:1436
bool GetDriverVersion(EGraphicsDriverAgeType DriverAgeType, int &Major, int &Minor, int &Patch, const char *&pName, EBackendType BackendType) override
Definition: backend_sdl.cpp:807
char m_aVersionString[gs_GpuInfoStringSize]
Definition: backend_sdl.h:223
virtual const SGfxErrorContainer & GetError() const =0
virtual void RunBuffer(CCommandBuffer *pBuffer)=0
virtual const SGfxWarningContainer & GetWarning() const =0
Definition: backend_sdl.h:48
static void ThreadFunc(void *pUser)
Definition: backend_sdl.cpp:48
void StartProcessor(ICommandProcessor *pProcessor)
Definition: backend_sdl.cpp:86
CCommandBuffer * m_pBuffer
Definition: backend_sdl.h:89
bool HasWarning()
Definition: backend_sdl.h:80
bool IsIdle() const override
Definition: backend_sdl.cpp:145
TTranslateFunc m_TranslateFunc
Definition: backend_sdl.h:50
std::condition_variable m_BufferSwapCond
Definition: backend_sdl.h:88
std::atomic_bool m_Shutdown
Definition: backend_sdl.h:90
ICommandProcessor * m_pProcessor
Definition: backend_sdl.h:86
bool m_Started
Definition: backend_sdl.h:91
void RunBufferSingleThreadedUnsafe(CCommandBuffer *pBuffer) override
Definition: backend_sdl.cpp:140
void WaitForIdle() override
Definition: backend_sdl.cpp:150
void ProcessError(const SGfxErrorContainer &Error)
Definition: backend_sdl.cpp:156
void RunBuffer(CCommandBuffer *pBuffer) override
Definition: backend_sdl.cpp:109
bool GetWarning(std::vector< std::string > &WarningStrings) override
Definition: backend_sdl.cpp:170
CGraphicsBackend_Threaded(TTranslateFunc &&TranslateFunc)
Definition: backend_sdl.cpp:77
void * m_pThread
Definition: backend_sdl.h:93
SGfxWarningContainer m_Warning
Definition: backend_sdl.h:51
std::mutex m_BufferSwapMutex
Definition: backend_sdl.h:87
std::atomic_bool m_BufferInProcess
Definition: backend_sdl.h:92
void StopProcessor()
Definition: backend_sdl.cpp:97
Definition: graphics.h:72
Definition: graphics_threaded.h:672
Definition: storage.h:20
EBackendType
Definition: graphics.h:142
@ BACKEND_TYPE_AUTO
Definition: graphics.h:148
EGraphicsDriverAgeType
Definition: graphics.h:133
std::function< bool(uint32_t &Width, uint32_t &Height, CImageInfo::EImageFormat &Format, std::vector< uint8_t > &vDstData)> TGLBackendReadPresentedImageData
Definition: graphics.h:180
std::function< const char *(const char *, const char *)> TTranslateFunc
Definition: graphics_threaded.h:1273
Definition: graphics_threaded.h:220
Definition: graphics_threaded.h:483
Definition: graphics_threaded.h:489
Definition: graphics_threaded.h:585
Definition: graphics_threaded.h:593
Definition: graphics_threaded.h:185
SCommand(unsigned Cmd)
Definition: graphics_threaded.h:187
SDL_Window * m_pWindow
Definition: backend_sdl.h:151
SDL_GLContext m_GLContext
Definition: backend_sdl.h:152
SCommand_Init()
Definition: backend_sdl.h:149
SCommand_Shutdown()
Definition: backend_sdl.h:157
Definition: backend_sdl.h:112
bool m_2DArrayTexturesAsExtension
Definition: backend_sdl.h:122
int m_ContextPatch
Definition: backend_sdl.h:130
bool m_NPOTTextures
Definition: backend_sdl.h:119
bool m_TextBuffering
Definition: backend_sdl.h:115
int m_ContextMajor
Definition: backend_sdl.h:128
bool m_QuadBuffering
Definition: backend_sdl.h:114
bool m_QuadContainerBuffering
Definition: backend_sdl.h:116
bool m_2DArrayTextures
Definition: backend_sdl.h:121
bool m_TrianglesAsQuads
Definition: backend_sdl.h:126
bool m_3DTextures
Definition: backend_sdl.h:120
bool m_MipMapping
Definition: backend_sdl.h:118
bool m_ShaderSupport
Definition: backend_sdl.h:123
bool m_TileBuffering
Definition: backend_sdl.h:113
int m_ContextMinor
Definition: backend_sdl.h:129
Definition: backend_base.h:60
Definition: backend_base.h:76
EGfxWarningType m_WarningType
Definition: backend_base.h:77
Definition: graphics.h:154