ddnet/src/engine/client/backend_sdl.h

282 lines
7.6 KiB
C
Raw Normal View History

#ifndef ENGINE_CLIENT_BACKEND_SDL_H
#define ENGINE_CLIENT_BACKEND_SDL_H
#include "SDL.h"
2021-04-30 22:42:37 +00:00
#include <base/detect.h>
2021-05-01 21:33:42 +00:00
#include "graphics_defines.h"
#include "blocklist_driver.h"
#include "graphics_threaded.h"
#include <base/tl/threading.h>
#include <atomic>
2021-10-27 22:52:06 +00:00
#include <condition_variable>
#include <mutex>
#include <thread>
2021-08-21 19:41:51 +00:00
#include <vector>
#if defined(CONF_PLATFORM_MACOS)
#include <objc/objc-runtime.h>
class CAutoreleasePool
{
private:
id m_Pool;
public:
CAutoreleasePool()
{
Class NSAutoreleasePoolClass = (Class)objc_getClass("NSAutoreleasePool");
m_Pool = class_createInstance(NSAutoreleasePoolClass, 0);
SEL selector = sel_registerName("init");
((id(*)(id, SEL))objc_msgSend)(m_Pool, selector);
}
~CAutoreleasePool()
{
SEL selector = sel_registerName("drain");
((id(*)(id, SEL))objc_msgSend)(m_Pool, selector);
}
};
#endif
// basic threaded backend, abstract, missing init and shutdown functions
class CGraphicsBackend_Threaded : public IGraphicsBackend
{
public:
2018-07-10 09:29:02 +00:00
// constructed on the main thread, the rest of the functions is run on the render thread
class ICommandProcessor
{
public:
virtual ~ICommandProcessor() {}
virtual void RunBuffer(CCommandBuffer *pBuffer) = 0;
};
CGraphicsBackend_Threaded();
virtual void RunBuffer(CCommandBuffer *pBuffer);
2022-03-20 17:03:25 +00:00
virtual void RunBufferSingleThreadedUnsafe(CCommandBuffer *pBuffer);
virtual bool IsIdle() const;
virtual void WaitForIdle();
2015-07-09 00:08:14 +00:00
protected:
void StartProcessor(ICommandProcessor *pProcessor);
void StopProcessor();
private:
ICommandProcessor *m_pProcessor;
2021-10-27 22:52:06 +00:00
std::mutex m_BufferSwapMutex;
std::condition_variable m_BufferSwapCond;
std::condition_variable m_BufferDoneCond;
CCommandBuffer *m_pBuffer;
2021-10-15 10:15:39 +00:00
std::atomic_bool m_Shutdown;
bool m_Started = false;
2021-10-27 22:52:06 +00:00
std::atomic_bool m_BufferInProcess;
void *m_Thread;
static void ThreadFunc(void *pUser);
};
// takes care of implementation independent operations
class CCommandProcessorFragment_General
{
void Cmd_Nop();
void Cmd_Signal(const CCommandBuffer::SCommand_Signal *pCommand);
public:
bool RunCommand(const CCommandBuffer::SCommand *pBaseCommand);
};
2021-04-30 22:42:37 +00:00
enum EBackendType
{
BACKEND_TYPE_OPENGL = 0,
BACKEND_TYPE_OPENGL_ES,
2022-03-20 17:03:25 +00:00
BACKEND_TYPE_VULKAN,
2021-04-30 22:42:37 +00:00
};
struct SBackendCapabilites
{
bool m_TileBuffering;
bool m_QuadBuffering;
bool m_TextBuffering;
bool m_QuadContainerBuffering;
bool m_MipMapping;
bool m_NPOTTextures;
bool m_3DTextures;
bool m_2DArrayTextures;
bool m_2DArrayTexturesAsExtension;
bool m_ShaderSupport;
2022-03-20 17:03:25 +00:00
// use quads as much as possible, even if the user config says otherwise
bool m_TrianglesAsQuads;
int m_ContextMajor;
int m_ContextMinor;
int m_ContextPatch;
};
// takes care of sdl related commands
class CCommandProcessorFragment_SDL
{
// SDL stuff
SDL_Window *m_pWindow;
SDL_GLContext m_GLContext;
public:
enum
{
CMD_INIT = CCommandBuffer::CMDGROUP_PLATFORM_SDL,
CMD_SHUTDOWN,
};
struct SCommand_Init : public CCommandBuffer::SCommand
{
SCommand_Init() :
SCommand(CMD_INIT) {}
SDL_Window *m_pWindow;
SDL_GLContext m_GLContext;
};
struct SCommand_Shutdown : public CCommandBuffer::SCommand
{
SCommand_Shutdown() :
SCommand(CMD_SHUTDOWN) {}
};
private:
void Cmd_Init(const SCommand_Init *pCommand);
void Cmd_Shutdown(const SCommand_Shutdown *pCommand);
void Cmd_Swap(const CCommandBuffer::SCommand_Swap *pCommand);
void Cmd_VSync(const CCommandBuffer::SCommand_VSync *pCommand);
2021-08-24 10:18:20 +00:00
void Cmd_WindowCreateNtf(const CCommandBuffer::SCommand_WindowCreateNtf *pCommand);
void Cmd_WindowDestroyNtf(const CCommandBuffer::SCommand_WindowDestroyNtf *pCommand);
public:
CCommandProcessorFragment_SDL();
2020-09-20 17:30:23 +00:00
bool RunCommand(const CCommandBuffer::SCommand *pBaseCommand);
};
2021-04-30 22:42:37 +00:00
// command processor impelementation, uses the fragments to combine into one processor
2022-03-20 17:03:25 +00:00
class CCommandProcessor_SDL_GL : public CGraphicsBackend_Threaded::ICommandProcessor
{
2022-03-20 17:03:25 +00:00
class CCommandProcessorFragment_GLBase *m_pGLBackend;
2015-07-09 00:08:14 +00:00
CCommandProcessorFragment_SDL m_SDL;
CCommandProcessorFragment_General m_General;
2021-04-30 22:42:37 +00:00
EBackendType m_BackendType;
2015-07-09 00:08:14 +00:00
public:
2022-03-20 17:03:25 +00:00
CCommandProcessor_SDL_GL(EBackendType BackendType, int GLMajor, int GLMinor, int GLPatch);
virtual ~CCommandProcessor_SDL_GL();
virtual void RunBuffer(CCommandBuffer *pBuffer);
};
2021-03-26 10:30:24 +00:00
static constexpr size_t gs_GPUInfoStringSize = 256;
2022-03-20 17:03:25 +00:00
// graphics backend implemented with SDL and the graphics library @see EBackendType
class CGraphicsBackend_SDL_GL : public CGraphicsBackend_Threaded
{
2021-08-21 19:41:51 +00:00
SDL_Window *m_pWindow = NULL;
2015-08-24 20:46:28 +00:00
SDL_GLContext m_GLContext;
ICommandProcessor *m_pProcessor;
2022-03-20 17:03:25 +00:00
std::atomic<uint64_t> m_TextureMemoryUsage{0};
std::atomic<uint64_t> m_BufferMemoryUsage{0};
std::atomic<uint64_t> m_StreamMemoryUsage{0};
std::atomic<uint64_t> m_StagingMemoryUsage{0};
TTWGraphicsGPUList m_GPUList;
TGLBackendReadPresentedImageData m_ReadPresentedImageDataFunc;
int m_NumScreens;
SBackendCapabilites m_Capabilites;
2021-03-26 10:30:24 +00:00
char m_aVendorString[gs_GPUInfoStringSize] = {};
char m_aVersionString[gs_GPUInfoStringSize] = {};
char m_aRendererString[gs_GPUInfoStringSize] = {};
EBackendType m_BackendType;
2020-11-05 19:38:37 +00:00
char m_aErrorString[256];
2021-04-30 22:42:37 +00:00
static EBackendType DetectBackend();
static void ClampDriverVersion(EBackendType BackendType);
public:
2022-03-20 17:03:25 +00:00
CGraphicsBackend_SDL_GL();
2021-08-21 19:41:51 +00:00
virtual int Init(const char *pName, int *Screen, int *pWidth, int *pHeight, int *pRefreshRate, int FsaaSamples, int Flags, int *pDesktopWidth, int *pDesktopHeight, int *pCurrentWidth, int *pCurrentHeight, class IStorage *pStorage);
virtual int Shutdown();
2022-03-20 17:03:25 +00:00
virtual uint64_t TextureMemoryUsage() const;
virtual uint64_t BufferMemoryUsage() const;
virtual uint64_t StreamedMemoryUsage() const;
virtual uint64_t StagingMemoryUsage() const;
virtual const TTWGraphicsGPUList &GetGPUs() const;
virtual int GetNumScreens() const { return m_NumScreens; }
2021-08-21 19:41:51 +00:00
virtual void GetVideoModes(CVideoMode *pModes, int MaxModes, int *pNumModes, int HiDPIScale, int MaxWindowWidth, int MaxWindowHeight, int Screen);
virtual void GetCurrentVideoMode(CVideoMode &CurMode, int HiDPIScale, int MaxWindowWidth, int MaxWindowHeight, int Screen);
virtual void Minimize();
virtual void Maximize();
virtual void SetWindowParams(int FullscreenMode, bool IsBorderless, bool AllowResizing);
virtual bool SetWindowScreen(int Index);
virtual bool UpdateDisplayMode(int Index);
virtual int GetWindowScreen();
virtual int WindowActive();
virtual int WindowOpen();
virtual void SetWindowGrab(bool Grab);
virtual bool ResizeWindow(int w, int h, int RefreshRate);
2020-12-12 11:58:59 +00:00
virtual void GetViewportSize(int &w, int &h);
virtual void NotifyWindow();
2022-01-20 09:49:31 +00:00
virtual void WindowDestroyNtf(uint32_t WindowID);
virtual void WindowCreateNtf(uint32_t WindowID);
virtual void GetDriverVersion(EGraphicsDriverAgeType DriverAgeType, int &Major, int &Minor, int &Patch);
virtual bool IsConfigModernAPI() { return IsModernAPI(m_BackendType); }
2022-03-20 17:03:25 +00:00
virtual bool UseTrianglesAsQuad() { return m_Capabilites.m_TrianglesAsQuads; }
virtual bool HasTileBuffering() { return m_Capabilites.m_TileBuffering; }
virtual bool HasQuadBuffering() { return m_Capabilites.m_QuadBuffering; }
virtual bool HasTextBuffering() { return m_Capabilites.m_TextBuffering; }
virtual bool HasQuadContainerBuffering() { return m_Capabilites.m_QuadContainerBuffering; }
virtual bool Has2DTextureArrays() { return m_Capabilites.m_2DArrayTextures; }
2020-11-05 19:38:37 +00:00
virtual const char *GetErrorString()
{
if(m_aErrorString[0] != '\0')
return m_aErrorString;
return NULL;
}
2021-03-26 10:30:24 +00:00
virtual const char *GetVendorString()
{
return m_aVendorString;
}
virtual const char *GetVersionString()
{
return m_aVersionString;
}
virtual const char *GetRendererString()
{
return m_aRendererString;
}
2021-04-30 22:42:37 +00:00
2022-03-20 17:03:25 +00:00
virtual TGLBackendReadPresentedImageData &GetReadPresentedImageDataFuncUnsafe();
2021-04-30 22:42:37 +00:00
static bool IsModernAPI(EBackendType BackendType);
};
#endif // ENGINE_CLIENT_BACKEND_SDL_H