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