From 8ffe5826156d07b1feb7fc58bf59a1431e01160f Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Fri, 30 Dec 2011 16:02:22 +0100 Subject: [PATCH 01/18] ugly incomplete hack to put the rendering into another thread so we don't have to wait for the flip --- src/base/tl/threading.h | 78 ++++++++++++++++++++++ src/engine/client/client.cpp | 98 +++++++++++++++++++++++++--- src/engine/client/client.h | 6 ++ src/game/client/components/menus.cpp | 3 + 4 files changed, 177 insertions(+), 8 deletions(-) create mode 100644 src/base/tl/threading.h diff --git a/src/base/tl/threading.h b/src/base/tl/threading.h new file mode 100644 index 000000000..66751ec4c --- /dev/null +++ b/src/base/tl/threading.h @@ -0,0 +1,78 @@ + +#pragma once + +#include "../system.h" + +inline unsigned atomic_inc(volatile unsigned *pValue) +{ + return __sync_fetch_and_add(pValue, 1); +} + +inline unsigned atomic_dec(volatile unsigned *pValue) +{ + return __sync_fetch_and_add(pValue, -1); +} + +inline unsigned atomic_compswap(volatile unsigned *pValue, unsigned comperand, unsigned value) +{ + return __sync_val_compare_and_swap(pValue, comperand, value); +} + +inline void sync_barrier() +{ + __sync_synchronize(); +} + +#include +typedef sem_t SEMAPHORE; +inline void semaphore_init(SEMAPHORE *sem) { sem_init(sem, 0, 0); } +inline void semaphore_wait(SEMAPHORE *sem) { sem_wait(sem); } +inline void semaphore_signal(SEMAPHORE *sem) { sem_post(sem); } +inline void semaphore_destroy(SEMAPHORE *sem) { sem_destroy(sem); } + +class semaphore +{ + SEMAPHORE sem; +public: + semaphore() { semaphore_init(&sem); } + ~semaphore() { semaphore_destroy(&sem); } + void wait() { semaphore_wait(&sem); } + void signal() { semaphore_signal(&sem); } +}; + +class lock +{ + friend class scope_lock; + + LOCK var; + + void take() { lock_wait(var); } + void release() { lock_release(var); } + +public: + lock() + { + var = lock_create(); + } + + ~lock() + { + lock_destroy(var); + } +}; + +class scope_lock +{ + lock *var; +public: + scope_lock(lock *l) + { + var = l; + var->take(); + } + + ~scope_lock() + { + var->release(); + } +}; diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 9f43b9ce1..4107cd85b 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -1688,17 +1689,27 @@ void CClient::InitInterfaces() m_Friends.Init(); } -void CClient::Run() + +enum { - int64 ReportTime = time_get(); - int64 ReportInterval = time_freq()*1; - - m_LocalStartTime = time_get(); - m_SnapshotParts = 0; + GFXSTATE_ERROR = -1, + GFXSTATE_INIT = 0, + GFXSTATE_IDLE, + GFXSTATE_RENDERING, + GFXSTATE_SWAPPING, +}; +void CClient::GraphicsThread() +{ // init graphics if(m_pGraphics->Init() != 0) + { + m_GfxState = GFXSTATE_ERROR; + m_GfxStateSemaphore.signal(); + m_GfxState = GFXSTATE_ERROR; return; + } + // open socket { @@ -1708,6 +1719,7 @@ void CClient::Run() if(!m_NetClient.Open(BindAddr, 0)) { dbg_msg("client", "couldn't start network"); + m_GfxState = GFXSTATE_ERROR; return; } } @@ -1722,16 +1734,66 @@ void CClient::Run() MasterServer()->RefreshAddresses(m_NetClient.NetType()); // init the editor - m_pEditor->Init(); + //m_pEditor->Init(); // init sound, allowed to fail m_SoundInitFailed = Sound()->Init() != 0; // load data if(!LoadData()) + { + m_GfxState = GFXSTATE_ERROR; return; + } GameClient()->OnInit(); + + + while(1) + { + // do idle + sync_barrier(); + m_GfxState = GFXSTATE_IDLE; + m_GfxStateSemaphore.signal(); + m_GfxRenderSemaphore.wait(); + + // do render + m_GfxState = GFXSTATE_RENDERING; + m_GfxStateSemaphore.signal(); + Render(); + sync_barrier(); + + // do swap + m_GfxState = GFXSTATE_SWAPPING; + m_GfxStateSemaphore.signal(); + m_pGraphics->Swap(); + } + + // do shutdown +} + +void CClient::Run() +{ + int64 ReportTime = time_get(); + int64 ReportInterval = time_freq()*1; + + m_LocalStartTime = time_get(); + m_SnapshotParts = 0; + + m_GfxState = GFXSTATE_INIT; + thread_create(GraphicsThreadProxy, this); + + // wait for gfx to init + while(1) + { + m_GfxStateSemaphore.wait(); + if(m_GfxState == GFXSTATE_ERROR) + return; + if(m_GfxState != GFXSTATE_INIT) + break; + } + + char aBuf[256]; str_format(aBuf, sizeof(aBuf), "version %s", GameClient()->NetVersion()); m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "client", aBuf); @@ -1847,7 +1909,27 @@ void CClient::Run() m_EditorActive = false; Update(); + + if(m_GfxState == GFXSTATE_IDLE) + { + // issue new rendering + m_GfxRenderSemaphore.signal(); + + // wait for gfx to finish rendering + while(m_GfxState != GFXSTATE_SWAPPING) + m_GfxStateSemaphore.wait(); + + } + + /* + if(m_pGraphics->AsyncSwapIsDone()) + { + m_pGraphics->BeginScene(); + Render(); + m_pGraphics->EndScene(); + }*/ + /* if(g_Config.m_DbgStress) { if((m_Frames%10) == 0) @@ -1860,7 +1942,7 @@ void CClient::Run() { Render(); m_pGraphics->Swap(); - } + }*/ } AutoScreenshot_Cleanup(); diff --git a/src/engine/client/client.h b/src/engine/client/client.h index 1504a4e44..83553eb41 100644 --- a/src/engine/client/client.h +++ b/src/engine/client/client.h @@ -172,6 +172,12 @@ class CClient : public IClient, public CDemoPlayer::IListner class CHostLookup m_VersionServeraddr; } m_VersionInfo; + semaphore m_GfxRenderSemaphore; + semaphore m_GfxStateSemaphore; + volatile int m_GfxState; + static void GraphicsThreadProxy(void *pThis) { ((CClient*)pThis)->GraphicsThread(); } + void GraphicsThread(); + public: IEngine *Engine() { return m_pEngine; } IEngineGraphics *Graphics() { return m_pGraphics; } diff --git a/src/game/client/components/menus.cpp b/src/game/client/components/menus.cpp index a39139c82..fc1ca2db0 100644 --- a/src/game/client/components/menus.cpp +++ b/src/game/client/components/menus.cpp @@ -627,6 +627,9 @@ int CMenus::RenderMenubar(CUIRect r) void CMenus::RenderLoading() { + // TODO: not supported right now due to separate render thread + return; + static int64 LastLoadRender = 0; float Percent = m_LoadCurrent++/(float)m_LoadTotal; From 8e568857959291f442ba8f11abac059b6bd6a9e9 Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Sat, 31 Dec 2011 01:06:04 +0100 Subject: [PATCH 02/18] initial commit of the threaded graphics. can start game and render the gui. no textures at the moment. still waits for the swap for now --- src/engine/client/client.cpp | 93 +- src/engine/client/graphics.cpp | 2 +- src/engine/client/graphics.h | 342 +++++++ src/engine/client/graphics_threaded.cpp | 1225 +++++++++++++++++++++++ src/game/client/components/menus.cpp | 1 - 5 files changed, 1576 insertions(+), 87 deletions(-) create mode 100644 src/engine/client/graphics_threaded.cpp diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 4107cd85b..bb888c608 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -1689,28 +1689,21 @@ void CClient::InitInterfaces() m_Friends.Init(); } - -enum +void CClient::Run() { - GFXSTATE_ERROR = -1, - GFXSTATE_INIT = 0, - GFXSTATE_IDLE, - GFXSTATE_RENDERING, - GFXSTATE_SWAPPING, -}; + int64 ReportTime = time_get(); + int64 ReportInterval = time_freq()*1; + + m_LocalStartTime = time_get(); + m_SnapshotParts = 0; -void CClient::GraphicsThread() -{ // init graphics if(m_pGraphics->Init() != 0) { - m_GfxState = GFXSTATE_ERROR; - m_GfxStateSemaphore.signal(); - m_GfxState = GFXSTATE_ERROR; + dbg_msg("client", "couldn't init graphics"); return; } - // open socket { NETADDR BindAddr; @@ -1719,7 +1712,6 @@ void CClient::GraphicsThread() if(!m_NetClient.Open(BindAddr, 0)) { dbg_msg("client", "couldn't start network"); - m_GfxState = GFXSTATE_ERROR; return; } } @@ -1741,59 +1733,10 @@ void CClient::GraphicsThread() // load data if(!LoadData()) - { - m_GfxState = GFXSTATE_ERROR; return; - } GameClient()->OnInit(); - - while(1) - { - // do idle - sync_barrier(); - m_GfxState = GFXSTATE_IDLE; - m_GfxStateSemaphore.signal(); - m_GfxRenderSemaphore.wait(); - - // do render - m_GfxState = GFXSTATE_RENDERING; - m_GfxStateSemaphore.signal(); - Render(); - sync_barrier(); - - // do swap - m_GfxState = GFXSTATE_SWAPPING; - m_GfxStateSemaphore.signal(); - m_pGraphics->Swap(); - } - - // do shutdown -} - -void CClient::Run() -{ - int64 ReportTime = time_get(); - int64 ReportInterval = time_freq()*1; - - m_LocalStartTime = time_get(); - m_SnapshotParts = 0; - - m_GfxState = GFXSTATE_INIT; - thread_create(GraphicsThreadProxy, this); - - // wait for gfx to init - while(1) - { - m_GfxStateSemaphore.wait(); - if(m_GfxState == GFXSTATE_ERROR) - return; - if(m_GfxState != GFXSTATE_INIT) - break; - } - - char aBuf[256]; str_format(aBuf, sizeof(aBuf), "version %s", GameClient()->NetVersion()); m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "client", aBuf); @@ -1910,26 +1853,6 @@ void CClient::Run() Update(); - - if(m_GfxState == GFXSTATE_IDLE) - { - // issue new rendering - m_GfxRenderSemaphore.signal(); - - // wait for gfx to finish rendering - while(m_GfxState != GFXSTATE_SWAPPING) - m_GfxStateSemaphore.wait(); - - } - - /* - if(m_pGraphics->AsyncSwapIsDone()) - { - m_pGraphics->BeginScene(); - Render(); - m_pGraphics->EndScene(); - }*/ - /* if(g_Config.m_DbgStress) { if((m_Frames%10) == 0) @@ -1942,7 +1865,7 @@ void CClient::Run() { Render(); m_pGraphics->Swap(); - }*/ + } } AutoScreenshot_Cleanup(); diff --git a/src/engine/client/graphics.cpp b/src/engine/client/graphics.cpp index 617162aee..2d034f3f7 100644 --- a/src/engine/client/graphics.cpp +++ b/src/engine/client/graphics.cpp @@ -950,4 +950,4 @@ int CGraphics_SDL::GetVideoModes(CVideoMode *pModes, int MaxModes) return NumModes; } -extern IEngineGraphics *CreateEngineGraphics() { return new CGraphics_SDL(); } +//extern IEngineGraphics *CreateEngineGraphics() { return new CGraphics_SDL(); } diff --git a/src/engine/client/graphics.h b/src/engine/client/graphics.h index 95e9769ad..84142d9d5 100644 --- a/src/engine/client/graphics.h +++ b/src/engine/client/graphics.h @@ -3,6 +3,348 @@ #ifndef ENGINE_CLIENT_GRAPHICS_H #define ENGINE_CLIENT_GRAPHICS_H +#include + +class CCommandBuffer +{ + class CBuffer + { + unsigned char *m_pData; + unsigned m_Size; + unsigned m_Used; + public: + CBuffer(unsigned BufferSize) + { + m_Size = BufferSize; + m_pData = new unsigned char[m_Size]; + m_Used = 0; + } + + ~CBuffer() + { + delete [] m_pData; + m_pData = 0x0; + m_Used = 0; + m_Size = 0; + } + + void Reset() + { + m_Used = 0; + } + + void *Alloc(unsigned Requested) + { + if(Requested + m_Used > m_Size) + return 0; + void *pPtr = &m_pData[m_Used]; + m_Used += Requested; + return pPtr; + } + + unsigned char *DataPtr() { return m_pData; } + unsigned DataSize() { return m_Size; } + unsigned DataUsed() { return m_Used; } + }; + + CBuffer m_CmdBuffer; + CBuffer m_DataBuffer; +public: + enum + { + // + CMD_NOP = 0, + + // + CMD_INIT, + CMD_SHUTDOWN, + + // + CMD_RUNBUFFER, + + // syncronization + CMD_SIGNAL, + + // texture commands + CMD_TEXTURE_CREATE, + CMD_TEXTURE_DESTROY, + + // rendering + CMD_CLEAR, + CMD_RENDER, + + // swap + CMD_SWAP, + }; + + enum + { + // + PRIMTYPE_INVALID = 0, + PRIMTYPE_LINES, + PRIMTYPE_QUADS, + }; + + enum + { + BLEND_NONE = 0, + BLEND_ALPHA, + BLEND_ADDITIVE, + }; + + struct SPoint { float x, y, z; }; + struct STexCoord { float u, v; }; + struct SColor { float r, g, b, a; }; + + struct SVertex + { + SPoint m_Pos; + STexCoord m_Tex; + SColor m_Color; + } ; + + struct SCommand + { + public: + SCommand(unsigned Cmd) : m_Cmd(Cmd), m_Size(0) {} + unsigned m_Cmd; + unsigned m_Size; + }; + + struct SState + { + int m_BlendMode; + int m_Texture; + SPoint m_ScreenTL; + SPoint m_ScreenBR; + + // clip + bool m_ClipEnable; + int m_ClipX; + int m_ClipY; + int m_ClipW; + int m_ClipH; + }; + + struct SCommand_Clear : public SCommand + { + SCommand_Clear() : SCommand(CMD_CLEAR) {} + SColor m_Color; + }; + + + struct SCommand_Init : public SCommand + { + SCommand_Init() : SCommand(CMD_INIT) {} + volatile int *m_pResult; + }; + + struct SCommand_RunBuffer : public SCommand + { + SCommand_RunBuffer() : SCommand(CMD_RUNBUFFER) {} + CCommandBuffer *m_pOtherBuffer; + }; + + struct SCommand_Render : public SCommand + { + SCommand_Render() : SCommand(CMD_RENDER) {} + SState m_State; + unsigned m_PrimType; + unsigned m_PrimCount; + SVertex *m_pVertices; + }; + + struct SCommand_Swap : public SCommand + { + SCommand_Swap() : SCommand(CMD_SWAP) {} + }; + + // + CCommandBuffer(unsigned CmdBufferSize, unsigned DataBufferSize) + : m_CmdBuffer(CmdBufferSize), m_DataBuffer(DataBufferSize) + { + } + + void *AllocData(unsigned WantedSize) + { + return m_DataBuffer.Alloc(WantedSize); + } + + template + void AddCommand(const T &Command) + { + SCommand *pCmd = (SCommand *)m_CmdBuffer.Alloc(sizeof(Command)); + if(!pCmd) + return; + + mem_copy(pCmd, &Command, sizeof(Command)); + pCmd->m_Size = sizeof(Command); + } + + SCommand *GetCommand(unsigned *pIndex) + { + if(*pIndex >= m_CmdBuffer.DataUsed()) + return NULL; + + SCommand *pCommand = (SCommand *)&m_CmdBuffer.DataPtr()[*pIndex]; + *pIndex += pCommand->m_Size; + return pCommand; + } + + void Reset() + { + m_CmdBuffer.Reset(); + m_DataBuffer.Reset(); + } +}; + +class ICommandProcessor +{ +public: + virtual ~ICommandProcessor() {} + virtual void RunBuffer(CCommandBuffer *pBuffer) = 0; +}; + + +class CCommandProcessorHandler +{ + ICommandProcessor *m_pProcessor; + CCommandBuffer * volatile m_pBuffer; + volatile bool m_Shutdown; + semaphore m_Activity; + semaphore m_BufferDone; + void *m_pThread; + + static void ThreadFunc(void *pUser); + +public: + CCommandProcessorHandler(); + void Start(ICommandProcessor *pProcessor); + void RunBuffer(CCommandBuffer *pBuffer); + bool IsIdle() const { return m_pBuffer == 0; } + void WaitForIdle(); +}; + +class CGraphics_Threaded : public IEngineGraphics +{ + CCommandBuffer::SState m_State; + CCommandProcessorHandler m_Handler; + + CCommandBuffer *m_apCommandBuffers[2]; + CCommandBuffer *m_pCommandBuffer; + unsigned m_CurrentCommandBuffer; + + // + class IStorage *m_pStorage; + class IConsole *m_pConsole; + + enum + { + MAX_VERTICES = 32*1024, + MAX_TEXTURES = 1024*4, + + DRAWING_QUADS=1, + DRAWING_LINES=2 + }; + + CCommandBuffer::SVertex m_aVertices[MAX_VERTICES]; + int m_NumVertices; + + CCommandBuffer::SColor m_aColor[4]; + CCommandBuffer::STexCoord m_aTexture[4]; + + bool m_RenderEnable; + + float m_Rotation; + int m_Drawing; + bool m_DoScreenshot; + char m_aScreenshotName[128]; + + int m_InvalidTexture; + + struct CTexture + { + GLuint m_Tex; + int m_MemSize; + int m_Flags; + int m_Next; + }; + + CTexture m_aTextures[MAX_TEXTURES]; + int m_FirstFreeTexture; + int m_TextureMemoryUsage; + + void Flush(); + void AddVertices(int Count); + void Rotate4(const CCommandBuffer::SPoint &rCenter, CCommandBuffer::SVertex *pPoints); + + static unsigned char Sample(int w, int h, const unsigned char *pData, int u, int v, int Offset, int ScaleW, int ScaleH, int Bpp); + static unsigned char *Rescale(int Width, int Height, int NewWidth, int NewHeight, int Format, const unsigned char *pData); +public: + CGraphics_Threaded(); + + virtual void ClipEnable(int x, int y, int w, int h); + virtual void ClipDisable(); + + virtual void BlendNone(); + virtual void BlendNormal(); + virtual void BlendAdditive(); + + virtual int MemoryUsage() const; + + virtual void MapScreen(float TopLeftX, float TopLeftY, float BottomRightX, float BottomRightY); + virtual void GetScreen(float *pTopLeftX, float *pTopLeftY, float *pBottomRightX, float *pBottomRightY); + + virtual void LinesBegin(); + virtual void LinesEnd(); + virtual void LinesDraw(const CLineItem *pArray, int Num); + + virtual int UnloadTexture(int Index); + virtual int LoadTextureRaw(int Width, int Height, int Format, const void *pData, int StoreFormat, int Flags); + + // simple uncompressed RGBA loaders + virtual int LoadTexture(const char *pFilename, int StorageType, int StoreFormat, int Flags); + virtual int LoadPNG(CImageInfo *pImg, const char *pFilename, int StorageType); + + void ScreenshotDirect(const char *pFilename); + + virtual void TextureSet(int TextureID); + + virtual void Clear(float r, float g, float b); + + virtual void QuadsBegin(); + virtual void QuadsEnd(); + virtual void QuadsSetRotation(float Angle); + + virtual void SetColorVertex(const CColorVertex *pArray, int Num); + virtual void SetColor(float r, float g, float b, float a); + + virtual void QuadsSetSubset(float TlU, float TlV, float BrU, float BrV); + virtual void QuadsSetSubsetFree( + float x0, float y0, float x1, float y1, + float x2, float y2, float x3, float y3); + + virtual void QuadsDraw(CQuadItem *pArray, int Num); + virtual void QuadsDrawTL(const CQuadItem *pArray, int Num); + virtual void QuadsDrawFreeform(const CFreeformItem *pArray, int Num); + virtual void QuadsText(float x, float y, float Size, float r, float g, float b, float a, const char *pText); + + virtual void Minimize(); + virtual void Maximize(); + + virtual int WindowActive(); + virtual int WindowOpen(); + + virtual bool Init(); + virtual void Shutdown(); + + virtual void TakeScreenshot(const char *pFilename); + virtual void Swap(); + + virtual int GetVideoModes(CVideoMode *pModes, int MaxModes); + +}; + class CGraphics_OpenGL : public IEngineGraphics { protected: diff --git a/src/engine/client/graphics_threaded.cpp b/src/engine/client/graphics_threaded.cpp new file mode 100644 index 000000000..c227dbd52 --- /dev/null +++ b/src/engine/client/graphics_threaded.cpp @@ -0,0 +1,1225 @@ +/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ +/* If you are missing that file, acquire a complete release at teeworlds.com. */ + +#include +#include + +#include "SDL.h" +#include "SDL_opengl.h" + +#include +#include + +#include +#include +#include +#include +#include + +#include // cosf, sinf + +#include "graphics.h" + + +static CVideoMode g_aFakeModes[] = { + {320,240,8,8,8}, {400,300,8,8,8}, {640,480,8,8,8}, + {720,400,8,8,8}, {768,576,8,8,8}, {800,600,8,8,8}, + {1024,600,8,8,8}, {1024,768,8,8,8}, {1152,864,8,8,8}, + {1280,768,8,8,8}, {1280,800,8,8,8}, {1280,960,8,8,8}, + {1280,1024,8,8,8}, {1368,768,8,8,8}, {1400,1050,8,8,8}, + {1440,900,8,8,8}, {1440,1050,8,8,8}, {1600,1000,8,8,8}, + {1600,1200,8,8,8}, {1680,1050,8,8,8}, {1792,1344,8,8,8}, + {1800,1440,8,8,8}, {1856,1392,8,8,8}, {1920,1080,8,8,8}, + {1920,1200,8,8,8}, {1920,1440,8,8,8}, {1920,2400,8,8,8}, + {2048,1536,8,8,8}, + + {320,240,5,6,5}, {400,300,5,6,5}, {640,480,5,6,5}, + {720,400,5,6,5}, {768,576,5,6,5}, {800,600,5,6,5}, + {1024,600,5,6,5}, {1024,768,5,6,5}, {1152,864,5,6,5}, + {1280,768,5,6,5}, {1280,800,5,6,5}, {1280,960,5,6,5}, + {1280,1024,5,6,5}, {1368,768,5,6,5}, {1400,1050,5,6,5}, + {1440,900,5,6,5}, {1440,1050,5,6,5}, {1600,1000,5,6,5}, + {1600,1200,5,6,5}, {1680,1050,5,6,5}, {1792,1344,5,6,5}, + {1800,1440,5,6,5}, {1856,1392,5,6,5}, {1920,1080,5,6,5}, + {1920,1200,5,6,5}, {1920,1440,5,6,5}, {1920,2400,5,6,5}, + {2048,1536,5,6,5} +}; + + +class CCommandProcessorFragment_OpenGL +{ +public: + void SetState(const CCommandBuffer::SState &State) + { + // blend + switch(State.m_BlendMode) + { + case CCommandBuffer::BLEND_NONE: + glDisable(GL_BLEND); + break; + case CCommandBuffer::BLEND_ALPHA: + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + break; + case CCommandBuffer::BLEND_ADDITIVE: + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE); + break; + default: + dbg_msg("render", "unknown blendmode %d\n", State.m_BlendMode); + }; + + // clip + /*if(State.m_ClipEnable) + { + glScissor(State.m_ClipX, State.m_ClipY+State.m_ClipH, State.m_ClipW, State.m_ClipH); + glEnable(GL_SCISSOR_TEST); + } + else*/ + glDisable(GL_SCISSOR_TEST); + + // texture + glDisable(GL_TEXTURE_2D); + + // screen mapping + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(State.m_ScreenTL.x, State.m_ScreenBR.x, State.m_ScreenBR.y, State.m_ScreenTL.y, 1.0f, 10.f); + + } + + bool RunCommand(const CCommandBuffer::SCommand * pBaseCommand) + { + + switch(pBaseCommand->m_Cmd) + { + case CCommandBuffer::CMD_TEXTURE_CREATE: + break; + case CCommandBuffer::CMD_TEXTURE_DESTROY: + break; + case CCommandBuffer::CMD_CLEAR: + { + const CCommandBuffer::SCommand_Clear *pCommand = (CCommandBuffer::SCommand_Clear *)pBaseCommand; + glClearColor(pCommand->m_Color.r, pCommand->m_Color.g, pCommand->m_Color.b, 0.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + } break; + case CCommandBuffer::CMD_RENDER: + { + const CCommandBuffer::SCommand_Render *pCommand = (CCommandBuffer::SCommand_Render *)pBaseCommand; + SetState(pCommand->m_State); + + glVertexPointer(3, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices); + glTexCoordPointer(2, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices + sizeof(float)*3); + glColorPointer(4, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices + sizeof(float)*5); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); + + switch(pCommand->m_PrimType) + { + case CCommandBuffer::PRIMTYPE_QUADS: + glDrawArrays(GL_QUADS, 0, pCommand->m_PrimCount*4); + break; + case CCommandBuffer::PRIMTYPE_LINES: + glDrawArrays(GL_LINES, 0, pCommand->m_PrimCount*2); + break; + default: + dbg_msg("render", "unknown primtype %d\n", pCommand->m_Cmd); + }; + } break; + default: + return false; + break; + } + + return true; + } +}; + +class CCommandProcessorFragment_SDL +{ + // SDL stuff + SDL_Surface *m_pScreenSurface; + + int m_ScreenWidth; + int m_ScreenHeight; + + int TryInit() + { + m_ScreenWidth = g_Config.m_GfxScreenWidth; + m_ScreenHeight = g_Config.m_GfxScreenHeight; + + const SDL_VideoInfo *pInfo = SDL_GetVideoInfo(); + SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE); + + // set flags + int Flags = SDL_OPENGL; + if(g_Config.m_DbgResizable) + Flags |= SDL_RESIZABLE; + + if(pInfo->hw_available) // ignore_convention + Flags |= SDL_HWSURFACE; + else + Flags |= SDL_SWSURFACE; + + if(pInfo->blit_hw) // ignore_convention + Flags |= SDL_HWACCEL; + + if(g_Config.m_GfxFullscreen) + Flags |= SDL_FULLSCREEN; + + // set gl attributes + if(g_Config.m_GfxFsaaSamples) + { + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, g_Config.m_GfxFsaaSamples); + } + else + { + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0); + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0); + } + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, g_Config.m_GfxVsync); + + // set caption + SDL_WM_SetCaption("Teeworlds", "Teeworlds"); + + // create window + m_pScreenSurface = SDL_SetVideoMode(m_ScreenWidth, m_ScreenHeight, 0, Flags); + if(m_pScreenSurface == NULL) + { + dbg_msg("gfx", "unable to set video mode: %s", SDL_GetError()); + return -1; + } + + return 0; + } + + + int InitWindow() + { + if(TryInit() == 0) + return 0; + + // try disabling fsaa + while(g_Config.m_GfxFsaaSamples) + { + g_Config.m_GfxFsaaSamples--; + + if(g_Config.m_GfxFsaaSamples) + dbg_msg("gfx", "lowering FSAA to %d and trying again", g_Config.m_GfxFsaaSamples); + else + dbg_msg("gfx", "disabling FSAA and trying again"); + + if(TryInit() == 0) + return 0; + } + + // try lowering the resolution + if(g_Config.m_GfxScreenWidth != 640 || g_Config.m_GfxScreenHeight != 480) + { + dbg_msg("gfx", "setting resolution to 640x480 and trying again"); + g_Config.m_GfxScreenWidth = 640; + g_Config.m_GfxScreenHeight = 480; + + if(TryInit() == 0) + return 0; + } + + dbg_msg("gfx", "out of ideas. failed to init graphics"); + + return -1; + } + + int Init() + { + { + int Systems = SDL_INIT_VIDEO; + + if(g_Config.m_SndEnable) + Systems |= SDL_INIT_AUDIO; + + if(g_Config.m_ClEventthread) + Systems |= SDL_INIT_EVENTTHREAD; + + if(SDL_Init(Systems) < 0) + { + dbg_msg("gfx", "unable to init SDL: %s", SDL_GetError()); + return -1; + } + } + + atexit(SDL_Quit); // ignore_convention + + #ifdef CONF_FAMILY_WINDOWS + if(!getenv("SDL_VIDEO_WINDOW_POS") && !getenv("SDL_VIDEO_CENTERED")) // ignore_convention + putenv("SDL_VIDEO_WINDOW_POS=8,27"); // ignore_convention + #endif + + if(InitWindow() != 0) + return -1; + + SDL_ShowCursor(0); + + // set some default settings + glEnable(GL_BLEND); + glDisable(GL_CULL_FACE); + glDisable(GL_DEPTH_TEST); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glAlphaFunc(GL_GREATER, 0); + glEnable(GL_ALPHA_TEST); + glDepthMask(0); + + return 0; + } + + +public: + CCommandProcessorFragment_SDL() + { + m_pScreenSurface = 0x0; + } + + bool RunCommand(const CCommandBuffer::SCommand *pBaseCommand) + { + + switch(pBaseCommand->m_Cmd) + { + case CCommandBuffer::CMD_NOP: + break; + case CCommandBuffer::CMD_INIT: + { + const CCommandBuffer::SCommand_Init *pCommand = (CCommandBuffer::SCommand_Init *)pBaseCommand; + *pCommand->m_pResult = Init(); + } break; + case CCommandBuffer::CMD_SHUTDOWN: + break; + case CCommandBuffer::CMD_SWAP: + { + SDL_GL_SwapBuffers(); + } break; + default: + return false; + break; + } + + return true; + } +}; + + +class CCommandProcessor_SDL_OpenGL : public ICommandProcessor +{ + CCommandProcessorFragment_OpenGL m_OpenGL; + CCommandProcessorFragment_SDL m_SDL; + public: + virtual void RunBuffer(CCommandBuffer *pBuffer) + { + unsigned CmdIndex = 0; + while(1) + { + CCommandBuffer::SCommand * const pBaseCommand = pBuffer->GetCommand(&CmdIndex); + if(pBaseCommand == 0x0) + break; + + if(m_OpenGL.RunCommand(pBaseCommand)) + continue; + + if(m_SDL.RunCommand(pBaseCommand)) + continue; + + dbg_msg("graphics", "unknown command %d", pBaseCommand->m_Cmd); + } + } +}; + +void CCommandProcessorHandler::ThreadFunc(void *pUser) +{ + CCommandProcessorHandler *pThis = (CCommandProcessorHandler *)pUser; + + while(!pThis->m_Shutdown) + { + pThis->m_Activity.wait(); + if(pThis->m_pBuffer) + { + pThis->m_pProcessor->RunBuffer(pThis->m_pBuffer); + sync_barrier(); + pThis->m_pBuffer = 0x0; + pThis->m_BufferDone.signal(); + } + } +} + +CCommandProcessorHandler::CCommandProcessorHandler() +{ + m_pBuffer = 0x0; + m_pProcessor = 0x0; + m_pThread = 0x0; +} + +void CCommandProcessorHandler::Start(ICommandProcessor *pProcessor) +{ + m_Shutdown = false; + m_pProcessor = pProcessor; + m_pThread = thread_create(ThreadFunc, this); + m_BufferDone.signal(); +} + +void CCommandProcessorHandler::RunBuffer(CCommandBuffer *pBuffer) +{ + WaitForIdle(); + m_pBuffer = pBuffer; + m_Activity.signal(); +} + +void CCommandProcessorHandler::WaitForIdle() +{ + while(m_pBuffer != 0x0) + m_BufferDone.wait(); +} + + +/* +void RenderThread() +{ + .wait() + if(m_pCommandBuffer) + RunBuffer(m_pCommandBuffer); + // reset buffer? + m_pCommandBuffer = 0; +}*/ + +void CGraphics_Threaded::Flush() +{ + if(m_NumVertices == 0) + return; + + int NumVerts = m_NumVertices; + m_NumVertices = 0; + + CCommandBuffer::SCommand_Render Cmd; + Cmd.m_State = m_State; + + if(m_Drawing == DRAWING_QUADS) + { + Cmd.m_PrimType = CCommandBuffer::PRIMTYPE_QUADS; + Cmd.m_PrimCount = NumVerts/4; + } + else if(m_Drawing == DRAWING_LINES) + { + Cmd.m_PrimType = CCommandBuffer::PRIMTYPE_LINES; + Cmd.m_PrimCount = NumVerts/2; + } + else + return; + + Cmd.m_pVertices = (CCommandBuffer::SVertex *)m_pCommandBuffer->AllocData(sizeof(CCommandBuffer::SVertex)*NumVerts); + if(Cmd.m_pVertices == 0x0) + return; + + mem_copy(Cmd.m_pVertices, m_aVertices, sizeof(CCommandBuffer::SVertex)*NumVerts); + m_pCommandBuffer->AddCommand(Cmd); +} + +void CGraphics_Threaded::AddVertices(int Count) +{ + m_NumVertices += Count; + if((m_NumVertices + Count) >= MAX_VERTICES) + Flush(); +} + +void CGraphics_Threaded::Rotate4(const CCommandBuffer::SPoint &rCenter, CCommandBuffer::SVertex *pPoints) +{ + float c = cosf(m_Rotation); + float s = sinf(m_Rotation); + float x, y; + int i; + + for(i = 0; i < 4; i++) + { + x = pPoints[i].m_Pos.x - rCenter.x; + y = pPoints[i].m_Pos.y - rCenter.y; + pPoints[i].m_Pos.x = x * c - y * s + rCenter.x; + pPoints[i].m_Pos.y = x * s + y * c + rCenter.y; + } +} + +unsigned char CGraphics_Threaded::Sample(int w, int h, const unsigned char *pData, int u, int v, int Offset, int ScaleW, int ScaleH, int Bpp) +{ + int Value = 0; + for(int x = 0; x < ScaleW; x++) + for(int y = 0; y < ScaleH; y++) + Value += pData[((v+y)*w+(u+x))*Bpp+Offset]; + return Value/(ScaleW*ScaleH); +} + +unsigned char *CGraphics_Threaded::Rescale(int Width, int Height, int NewWidth, int NewHeight, int Format, const unsigned char *pData) +{ + unsigned char *pTmpData; + int ScaleW = Width/NewWidth; + int ScaleH = Height/NewHeight; + + int Bpp = 3; + if(Format == CImageInfo::FORMAT_RGBA) + Bpp = 4; + + pTmpData = (unsigned char *)mem_alloc(NewWidth*NewHeight*Bpp, 1); + + int c = 0; + for(int y = 0; y < NewHeight; y++) + for(int x = 0; x < NewWidth; x++, c++) + { + pTmpData[c*Bpp] = Sample(Width, Height, pData, x*ScaleW, y*ScaleH, 0, ScaleW, ScaleH, Bpp); + pTmpData[c*Bpp+1] = Sample(Width, Height, pData, x*ScaleW, y*ScaleH, 1, ScaleW, ScaleH, Bpp); + pTmpData[c*Bpp+2] = Sample(Width, Height, pData, x*ScaleW, y*ScaleH, 2, ScaleW, ScaleH, Bpp); + if(Bpp == 4) + pTmpData[c*Bpp+3] = Sample(Width, Height, pData, x*ScaleW, y*ScaleH, 3, ScaleW, ScaleH, Bpp); + } + + return pTmpData; +} + +CGraphics_Threaded::CGraphics_Threaded() +{ + m_State.m_ScreenTL.x = 0; + m_State.m_ScreenTL.y = 0; + m_State.m_ScreenBR.x = 0; + m_State.m_ScreenBR.y = 0; + m_State.m_ClipEnable = false; + m_State.m_ClipX = 0; + m_State.m_ClipY = 0; + m_State.m_ClipW = 0; + m_State.m_ClipH = 0; + m_State.m_Texture = -1; + m_State.m_BlendMode = CCommandBuffer::BLEND_NONE; + + + + m_NumVertices = 0; + + m_ScreenWidth = -1; + m_ScreenHeight = -1; + + m_Rotation = 0; + m_Drawing = 0; + m_InvalidTexture = 0; + + m_TextureMemoryUsage = 0; + + m_RenderEnable = true; + m_DoScreenshot = false; +} + +void CGraphics_Threaded::ClipEnable(int x, int y, int w, int h) +{ + if(x < 0) + w += x; + if(y < 0) + h += y; + + x = clamp(x, 0, ScreenWidth()); + y = clamp(y, 0, ScreenHeight()); + w = clamp(w, 0, ScreenWidth()-x); + h = clamp(h, 0, ScreenHeight()-y); + + m_State.m_ClipEnable = true; + m_State.m_ClipX = x; + m_State.m_ClipY = ScreenHeight()-(y+h); + m_State.m_ClipW = w; + m_State.m_ClipH = h; +} + +void CGraphics_Threaded::ClipDisable() +{ + m_State.m_ClipEnable = false; +} + +void CGraphics_Threaded::BlendNone() +{ + m_State.m_BlendMode = CCommandBuffer::BLEND_NONE; +} + +void CGraphics_Threaded::BlendNormal() +{ + m_State.m_BlendMode = CCommandBuffer::BLEND_ALPHA; +} + +void CGraphics_Threaded::BlendAdditive() +{ + m_State.m_BlendMode = CCommandBuffer::BLEND_ADDITIVE; +} + +int CGraphics_Threaded::MemoryUsage() const +{ + return m_TextureMemoryUsage; +} + +void CGraphics_Threaded::MapScreen(float TopLeftX, float TopLeftY, float BottomRightX, float BottomRightY) +{ + m_State.m_ScreenTL.x = TopLeftX; + m_State.m_ScreenTL.y = TopLeftY; + m_State.m_ScreenBR.x = BottomRightX; + m_State.m_ScreenBR.y = BottomRightY; +} + +void CGraphics_Threaded::GetScreen(float *pTopLeftX, float *pTopLeftY, float *pBottomRightX, float *pBottomRightY) +{ + *pTopLeftX = m_State.m_ScreenTL.x; + *pTopLeftY = m_State.m_ScreenTL.y; + *pBottomRightX = m_State.m_ScreenBR.x; + *pBottomRightY = m_State.m_ScreenBR.y; +} + +void CGraphics_Threaded::LinesBegin() +{ + dbg_assert(m_Drawing == 0, "called Graphics()->LinesBegin twice"); + m_Drawing = DRAWING_LINES; + SetColor(1,1,1,1); +} + +void CGraphics_Threaded::LinesEnd() +{ + dbg_assert(m_Drawing == DRAWING_LINES, "called Graphics()->LinesEnd without begin"); + Flush(); + m_Drawing = 0; +} + +void CGraphics_Threaded::LinesDraw(const CLineItem *pArray, int Num) +{ + dbg_assert(m_Drawing == DRAWING_LINES, "called Graphics()->LinesDraw without begin"); + + for(int i = 0; i < Num; ++i) + { + m_aVertices[m_NumVertices + 2*i].m_Pos.x = pArray[i].m_X0; + m_aVertices[m_NumVertices + 2*i].m_Pos.y = pArray[i].m_Y0; + m_aVertices[m_NumVertices + 2*i].m_Tex = m_aTexture[0]; + m_aVertices[m_NumVertices + 2*i].m_Color = m_aColor[0]; + + m_aVertices[m_NumVertices + 2*i + 1].m_Pos.x = pArray[i].m_X1; + m_aVertices[m_NumVertices + 2*i + 1].m_Pos.y = pArray[i].m_Y1; + m_aVertices[m_NumVertices + 2*i + 1].m_Tex = m_aTexture[1]; + m_aVertices[m_NumVertices + 2*i + 1].m_Color = m_aColor[1]; + } + + AddVertices(2*Num); +} + +int CGraphics_Threaded::UnloadTexture(int Index) +{ + return 0; + if(Index == m_InvalidTexture) + return 0; + + if(Index < 0) + return 0; + + glDeleteTextures(1, &m_aTextures[Index].m_Tex); + m_aTextures[Index].m_Next = m_FirstFreeTexture; + m_TextureMemoryUsage -= m_aTextures[Index].m_MemSize; + m_FirstFreeTexture = Index; + return 0; +} + + +int CGraphics_Threaded::LoadTextureRaw(int Width, int Height, int Format, const void *pData, int StoreFormat, int Flags) +{ + return 0; + + int Mipmap = 1; + unsigned char *pTexData = (unsigned char *)pData; + unsigned char *pTmpData = 0; + int Oglformat = 0; + int StoreOglformat = 0; + int Tex = 0; + + // don't waste memory on texture if we are stress testing + if(g_Config.m_DbgStress) + return m_InvalidTexture; + + // grab texture + Tex = m_FirstFreeTexture; + m_FirstFreeTexture = m_aTextures[Tex].m_Next; + m_aTextures[Tex].m_Next = -1; + + // resample if needed + if(!(Flags&TEXLOAD_NORESAMPLE) && (Format == CImageInfo::FORMAT_RGBA || Format == CImageInfo::FORMAT_RGB)) + { + if(Width > GL_MAX_TEXTURE_SIZE || Height > GL_MAX_TEXTURE_SIZE) + { + int NewWidth = min(Width, GL_MAX_TEXTURE_SIZE); + int NewHeight = min(Height, GL_MAX_TEXTURE_SIZE); + pTmpData = Rescale(Width, Height, NewWidth, NewHeight, Format, pTexData); + pTexData = pTmpData; + Width = NewWidth; + Height = NewHeight; + } + else if(Width > 16 && Height > 16 && g_Config.m_GfxTextureQuality == 0) + { + pTmpData = Rescale(Width, Height, Width/2, Height/2, Format, pTexData); + pTexData = pTmpData; + Width /= 2; + Height /= 2; + } + } + + Oglformat = GL_RGBA; + if(Format == CImageInfo::FORMAT_RGB) + Oglformat = GL_RGB; + else if(Format == CImageInfo::FORMAT_ALPHA) + Oglformat = GL_ALPHA; + + // upload texture + if(g_Config.m_GfxTextureCompression) + { + StoreOglformat = GL_COMPRESSED_RGBA_ARB; + if(StoreFormat == CImageInfo::FORMAT_RGB) + StoreOglformat = GL_COMPRESSED_RGB_ARB; + else if(StoreFormat == CImageInfo::FORMAT_ALPHA) + StoreOglformat = GL_COMPRESSED_ALPHA_ARB; + } + else + { + StoreOglformat = GL_RGBA; + if(StoreFormat == CImageInfo::FORMAT_RGB) + StoreOglformat = GL_RGB; + else if(StoreFormat == CImageInfo::FORMAT_ALPHA) + StoreOglformat = GL_ALPHA; + } + + glGenTextures(1, &m_aTextures[Tex].m_Tex); + glBindTexture(GL_TEXTURE_2D, m_aTextures[Tex].m_Tex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); + gluBuild2DMipmaps(GL_TEXTURE_2D, StoreOglformat, Width, Height, Oglformat, GL_UNSIGNED_BYTE, pTexData); + + // calculate memory usage + { + int PixelSize = 4; + if(StoreFormat == CImageInfo::FORMAT_RGB) + PixelSize = 3; + else if(StoreFormat == CImageInfo::FORMAT_ALPHA) + PixelSize = 1; + + m_aTextures[Tex].m_MemSize = Width*Height*PixelSize; + if(Mipmap) + { + while(Width > 2 && Height > 2) + { + Width>>=1; + Height>>=1; + m_aTextures[Tex].m_MemSize += Width*Height*PixelSize; + } + } + } + + m_TextureMemoryUsage += m_aTextures[Tex].m_MemSize; + mem_free(pTmpData); + return Tex; +} + +// simple uncompressed RGBA loaders +int CGraphics_Threaded::LoadTexture(const char *pFilename, int StorageType, int StoreFormat, int Flags) +{ + return 0; + + int l = str_length(pFilename); + int ID; + CImageInfo Img; + + if(l < 3) + return -1; + if(LoadPNG(&Img, pFilename, StorageType)) + { + if (StoreFormat == CImageInfo::FORMAT_AUTO) + StoreFormat = Img.m_Format; + + ID = LoadTextureRaw(Img.m_Width, Img.m_Height, Img.m_Format, Img.m_pData, StoreFormat, Flags); + mem_free(Img.m_pData); + if(ID != m_InvalidTexture && g_Config.m_Debug) + dbg_msg("graphics/texture", "loaded %s", pFilename); + return ID; + } + + return m_InvalidTexture; +} + +int CGraphics_Threaded::LoadPNG(CImageInfo *pImg, const char *pFilename, int StorageType) +{ + return 0; + + char aCompleteFilename[512]; + unsigned char *pBuffer; + png_t Png; // ignore_convention + + // open file for reading + png_init(0,0); // ignore_convention + + IOHANDLE File = m_pStorage->OpenFile(pFilename, IOFLAG_READ, StorageType, aCompleteFilename, sizeof(aCompleteFilename)); + if(File) + io_close(File); + else + { + dbg_msg("game/png", "failed to open file. filename='%s'", pFilename); + return 0; + } + + int Error = png_open_file(&Png, aCompleteFilename); // ignore_convention + if(Error != PNG_NO_ERROR) + { + dbg_msg("game/png", "failed to open file. filename='%s'", aCompleteFilename); + if(Error != PNG_FILE_ERROR) + png_close_file(&Png); // ignore_convention + return 0; + } + + if(Png.depth != 8 || (Png.color_type != PNG_TRUECOLOR && Png.color_type != PNG_TRUECOLOR_ALPHA)) // ignore_convention + { + dbg_msg("game/png", "invalid format. filename='%s'", aCompleteFilename); + png_close_file(&Png); // ignore_convention + return 0; + } + + pBuffer = (unsigned char *)mem_alloc(Png.width * Png.height * Png.bpp, 1); // ignore_convention + png_get_data(&Png, pBuffer); // ignore_convention + png_close_file(&Png); // ignore_convention + + pImg->m_Width = Png.width; // ignore_convention + pImg->m_Height = Png.height; // ignore_convention + if(Png.color_type == PNG_TRUECOLOR) // ignore_convention + pImg->m_Format = CImageInfo::FORMAT_RGB; + else if(Png.color_type == PNG_TRUECOLOR_ALPHA) // ignore_convention + pImg->m_Format = CImageInfo::FORMAT_RGBA; + pImg->m_pData = pBuffer; + return 1; +} + +void CGraphics_Threaded::ScreenshotDirect(const char *pFilename) +{ + // fetch image data + int y; + int w = m_ScreenWidth; + int h = m_ScreenHeight; + unsigned char *pPixelData = (unsigned char *)mem_alloc(w*(h+1)*3, 1); + unsigned char *pTempRow = pPixelData+w*h*3; + GLint Alignment; + glGetIntegerv(GL_PACK_ALIGNMENT, &Alignment); + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glReadPixels(0,0, w, h, GL_RGB, GL_UNSIGNED_BYTE, pPixelData); + glPixelStorei(GL_PACK_ALIGNMENT, Alignment); + + // flip the pixel because opengl works from bottom left corner + for(y = 0; y < h/2; y++) + { + mem_copy(pTempRow, pPixelData+y*w*3, w*3); + mem_copy(pPixelData+y*w*3, pPixelData+(h-y-1)*w*3, w*3); + mem_copy(pPixelData+(h-y-1)*w*3, pTempRow,w*3); + } + + // find filename + { + char aWholePath[1024]; + png_t Png; // ignore_convention + + IOHANDLE File = m_pStorage->OpenFile(pFilename, IOFLAG_WRITE, IStorage::TYPE_SAVE, aWholePath, sizeof(aWholePath)); + if(File) + io_close(File); + + // save png + char aBuf[256]; + str_format(aBuf, sizeof(aBuf), "saved screenshot to '%s'", aWholePath); + m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "client", aBuf); + png_open_file_write(&Png, aWholePath); // ignore_convention + png_set_data(&Png, w, h, 8, PNG_TRUECOLOR, (unsigned char *)pPixelData); // ignore_convention + png_close_file(&Png); // ignore_convention + } + + // clean up + mem_free(pPixelData); +} + +void CGraphics_Threaded::TextureSet(int TextureID) +{ + return; + + dbg_assert(m_Drawing == 0, "called Graphics()->TextureSet within begin"); + if(TextureID == -1) + { + glDisable(GL_TEXTURE_2D); + } + else + { + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, m_aTextures[TextureID].m_Tex); + } +} + +void CGraphics_Threaded::Clear(float r, float g, float b) +{ + CCommandBuffer::SCommand_Clear Cmd; + Cmd.m_Color.r = r; + Cmd.m_Color.g = g; + Cmd.m_Color.b = b; + Cmd.m_Color.a = 0; + m_pCommandBuffer->AddCommand(Cmd); +} + +void CGraphics_Threaded::QuadsBegin() +{ + dbg_assert(m_Drawing == 0, "called Graphics()->QuadsBegin twice"); + m_Drawing = DRAWING_QUADS; + + QuadsSetSubset(0,0,1,1); + QuadsSetRotation(0); + SetColor(1,1,1,1); +} + +void CGraphics_Threaded::QuadsEnd() +{ + dbg_assert(m_Drawing == DRAWING_QUADS, "called Graphics()->QuadsEnd without begin"); + Flush(); + m_Drawing = 0; +} + +void CGraphics_Threaded::QuadsSetRotation(float Angle) +{ + dbg_assert(m_Drawing == DRAWING_QUADS, "called Graphics()->QuadsSetRotation without begin"); + m_Rotation = Angle; +} + +void CGraphics_Threaded::SetColorVertex(const CColorVertex *pArray, int Num) +{ + dbg_assert(m_Drawing != 0, "called Graphics()->SetColorVertex without begin"); + + for(int i = 0; i < Num; ++i) + { + m_aColor[pArray[i].m_Index].r = pArray[i].m_R; + m_aColor[pArray[i].m_Index].g = pArray[i].m_G; + m_aColor[pArray[i].m_Index].b = pArray[i].m_B; + m_aColor[pArray[i].m_Index].a = pArray[i].m_A; + } +} + +void CGraphics_Threaded::SetColor(float r, float g, float b, float a) +{ + dbg_assert(m_Drawing != 0, "called Graphics()->SetColor without begin"); + CColorVertex Array[4] = { + CColorVertex(0, r, g, b, a), + CColorVertex(1, r, g, b, a), + CColorVertex(2, r, g, b, a), + CColorVertex(3, r, g, b, a)}; + SetColorVertex(Array, 4); +} + +void CGraphics_Threaded::QuadsSetSubset(float TlU, float TlV, float BrU, float BrV) +{ + dbg_assert(m_Drawing == DRAWING_QUADS, "called Graphics()->QuadsSetSubset without begin"); + + m_aTexture[0].u = TlU; m_aTexture[1].u = BrU; + m_aTexture[0].v = TlV; m_aTexture[1].v = TlV; + + m_aTexture[3].u = TlU; m_aTexture[2].u = BrU; + m_aTexture[3].v = BrV; m_aTexture[2].v = BrV; +} + +void CGraphics_Threaded::QuadsSetSubsetFree( + float x0, float y0, float x1, float y1, + float x2, float y2, float x3, float y3) +{ + m_aTexture[0].u = x0; m_aTexture[0].v = y0; + m_aTexture[1].u = x1; m_aTexture[1].v = y1; + m_aTexture[2].u = x2; m_aTexture[2].v = y2; + m_aTexture[3].u = x3; m_aTexture[3].v = y3; +} + +void CGraphics_Threaded::QuadsDraw(CQuadItem *pArray, int Num) +{ + for(int i = 0; i < Num; ++i) + { + pArray[i].m_X -= pArray[i].m_Width/2; + pArray[i].m_Y -= pArray[i].m_Height/2; + } + + QuadsDrawTL(pArray, Num); +} + +void CGraphics_Threaded::QuadsDrawTL(const CQuadItem *pArray, int Num) +{ + CCommandBuffer::SPoint Center; + Center.z = 0; + + dbg_assert(m_Drawing == DRAWING_QUADS, "called Graphics()->QuadsDrawTL without begin"); + + for(int i = 0; i < Num; ++i) + { + m_aVertices[m_NumVertices + 4*i].m_Pos.x = pArray[i].m_X; + m_aVertices[m_NumVertices + 4*i].m_Pos.y = pArray[i].m_Y; + m_aVertices[m_NumVertices + 4*i].m_Tex = m_aTexture[0]; + m_aVertices[m_NumVertices + 4*i].m_Color = m_aColor[0]; + + m_aVertices[m_NumVertices + 4*i + 1].m_Pos.x = pArray[i].m_X + pArray[i].m_Width; + m_aVertices[m_NumVertices + 4*i + 1].m_Pos.y = pArray[i].m_Y; + m_aVertices[m_NumVertices + 4*i + 1].m_Tex = m_aTexture[1]; + m_aVertices[m_NumVertices + 4*i + 1].m_Color = m_aColor[1]; + + m_aVertices[m_NumVertices + 4*i + 2].m_Pos.x = pArray[i].m_X + pArray[i].m_Width; + m_aVertices[m_NumVertices + 4*i + 2].m_Pos.y = pArray[i].m_Y + pArray[i].m_Height; + m_aVertices[m_NumVertices + 4*i + 2].m_Tex = m_aTexture[2]; + m_aVertices[m_NumVertices + 4*i + 2].m_Color = m_aColor[2]; + + m_aVertices[m_NumVertices + 4*i + 3].m_Pos.x = pArray[i].m_X; + m_aVertices[m_NumVertices + 4*i + 3].m_Pos.y = pArray[i].m_Y + pArray[i].m_Height; + m_aVertices[m_NumVertices + 4*i + 3].m_Tex = m_aTexture[3]; + m_aVertices[m_NumVertices + 4*i + 3].m_Color = m_aColor[3]; + + if(m_Rotation != 0) + { + Center.x = pArray[i].m_X + pArray[i].m_Width/2; + Center.y = pArray[i].m_Y + pArray[i].m_Height/2; + + Rotate4(Center, &m_aVertices[m_NumVertices + 4*i]); + } + } + + AddVertices(4*Num); +} + +void CGraphics_Threaded::QuadsDrawFreeform(const CFreeformItem *pArray, int Num) +{ + dbg_assert(m_Drawing == DRAWING_QUADS, "called Graphics()->QuadsDrawFreeform without begin"); + + for(int i = 0; i < Num; ++i) + { + m_aVertices[m_NumVertices + 4*i].m_Pos.x = pArray[i].m_X0; + m_aVertices[m_NumVertices + 4*i].m_Pos.y = pArray[i].m_Y0; + m_aVertices[m_NumVertices + 4*i].m_Tex = m_aTexture[0]; + m_aVertices[m_NumVertices + 4*i].m_Color = m_aColor[0]; + + m_aVertices[m_NumVertices + 4*i + 1].m_Pos.x = pArray[i].m_X1; + m_aVertices[m_NumVertices + 4*i + 1].m_Pos.y = pArray[i].m_Y1; + m_aVertices[m_NumVertices + 4*i + 1].m_Tex = m_aTexture[1]; + m_aVertices[m_NumVertices + 4*i + 1].m_Color = m_aColor[1]; + + m_aVertices[m_NumVertices + 4*i + 2].m_Pos.x = pArray[i].m_X3; + m_aVertices[m_NumVertices + 4*i + 2].m_Pos.y = pArray[i].m_Y3; + m_aVertices[m_NumVertices + 4*i + 2].m_Tex = m_aTexture[3]; + m_aVertices[m_NumVertices + 4*i + 2].m_Color = m_aColor[3]; + + m_aVertices[m_NumVertices + 4*i + 3].m_Pos.x = pArray[i].m_X2; + m_aVertices[m_NumVertices + 4*i + 3].m_Pos.y = pArray[i].m_Y2; + m_aVertices[m_NumVertices + 4*i + 3].m_Tex = m_aTexture[2]; + m_aVertices[m_NumVertices + 4*i + 3].m_Color = m_aColor[2]; + } + + AddVertices(4*Num); +} + +void CGraphics_Threaded::QuadsText(float x, float y, float Size, float r, float g, float b, float a, const char *pText) +{ + float StartX = x; + + QuadsBegin(); + SetColor(r,g,b,a); + + while(*pText) + { + char c = *pText; + pText++; + + if(c == '\n') + { + x = StartX; + y += Size; + } + else + { + QuadsSetSubset( + (c%16)/16.0f, + (c/16)/16.0f, + (c%16)/16.0f+1.0f/16.0f, + (c/16)/16.0f+1.0f/16.0f); + + CQuadItem QuadItem(x, y, Size, Size); + QuadsDrawTL(&QuadItem, 1); + x += Size/2; + } + } + + QuadsEnd(); +} + +bool CGraphics_Threaded::Init() +{ + // fetch pointers + m_pStorage = Kernel()->RequestInterface(); + m_pConsole = Kernel()->RequestInterface(); + + // Set all z to -5.0f + for(int i = 0; i < MAX_VERTICES; i++) + m_aVertices[i].m_Pos.z = -5.0f; + + // init textures + m_FirstFreeTexture = 0; + for(int i = 0; i < MAX_TEXTURES; i++) + m_aTextures[i].m_Next = i+1; + m_aTextures[MAX_TEXTURES-1].m_Next = -1; + + // start the command processor + ICommandProcessor *pProcessor = new CCommandProcessor_SDL_OpenGL; + m_Handler.Start(pProcessor); + + // create command buffers + m_apCommandBuffers[0] = new CCommandBuffer(1024*512, 1024*1024); + m_apCommandBuffers[1] = new CCommandBuffer(1024*512, 1024*1024); + m_pCommandBuffer = m_apCommandBuffers[0]; + + // issue init command + m_pCommandBuffer->Reset(); + volatile int Result; + CCommandBuffer::SCommand_Init Cmd; + Cmd.m_pResult = &Result; + m_pCommandBuffer->AddCommand(Cmd); + m_Handler.RunBuffer(m_pCommandBuffer); + m_Handler.WaitForIdle(); + + + if(Result == 0) + { + // create null texture, will get id=0 + static const unsigned char aNullTextureData[] = { + 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0x00,0xff,0x00,0xff, 0x00,0xff,0x00,0xff, + 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0x00,0xff,0x00,0xff, 0x00,0xff,0x00,0xff, + 0x00,0x00,0xff,0xff, 0x00,0x00,0xff,0xff, 0xff,0xff,0x00,0xff, 0xff,0xff,0x00,0xff, + 0x00,0x00,0xff,0xff, 0x00,0x00,0xff,0xff, 0xff,0xff,0x00,0xff, 0xff,0xff,0x00,0xff, + }; + + m_InvalidTexture = LoadTextureRaw(4,4,CImageInfo::FORMAT_RGBA,aNullTextureData,CImageInfo::FORMAT_RGBA,TEXLOAD_NORESAMPLE); + } + + return Result; +} + +void CGraphics_Threaded::Shutdown() +{ + // TODO: SDL, is this correct? + SDL_Quit(); +} + +void CGraphics_Threaded::Minimize() +{ + SDL_WM_IconifyWindow(); +} + +void CGraphics_Threaded::Maximize() +{ + // TODO: SDL +} + +int CGraphics_Threaded::WindowActive() +{ + return SDL_GetAppState()&SDL_APPINPUTFOCUS; +} + +int CGraphics_Threaded::WindowOpen() +{ + return SDL_GetAppState()&SDL_APPACTIVE; + +} + +void CGraphics_Threaded::TakeScreenshot(const char *pFilename) +{ + // TODO: screenshot support + return; + /* + char aDate[20]; + str_timestamp(aDate, sizeof(aDate)); + str_format(m_aScreenshotName, sizeof(m_aScreenshotName), "screenshots/%s_%s.png", pFilename?pFilename:"screenshot", aDate); + m_DoScreenshot = true; + */ +} + +void CGraphics_Threaded::Swap() +{ + if(0) { + CCommandBuffer::SCommand_Clear Cmd; + Cmd.m_Color.r = 1.0f; + Cmd.m_Color.g = 0.0f; + Cmd.m_Color.b = 0.0f; + Cmd.m_Color.a = 0.0f; + m_pCommandBuffer->AddCommand(Cmd); + } + + CCommandBuffer::SCommand_Swap Cmd; + m_pCommandBuffer->AddCommand(Cmd); + + m_Handler.RunBuffer(m_pCommandBuffer); + m_Handler.WaitForIdle(); + m_pCommandBuffer->Reset(); + + // TODO: screenshot support + /* + if(m_DoScreenshot) + { + ScreenshotDirect(m_aScreenshotName); + m_DoScreenshot = false; + }*/ + + //SDL_GL_SwapBuffers(); + + //if(g_Config.m_GfxFinish) + // glFinish(); +} + + +int CGraphics_Threaded::GetVideoModes(CVideoMode *pModes, int MaxModes) +{ + // TODO: fix support for video modes, using fake modes for now + //int NumModes = sizeof(g_aFakeModes)/sizeof(CVideoMode); + //SDL_Rect **ppModes; + + //if(g_Config.m_GfxDisplayAllModes) + { + int Count = sizeof(g_aFakeModes)/sizeof(CVideoMode); + mem_copy(pModes, g_aFakeModes, sizeof(g_aFakeModes)); + if(MaxModes < Count) + Count = MaxModes; + return Count; + } + + // TODO: fix this code on osx or windows + /* + + ppModes = SDL_ListModes(NULL, SDL_OPENGL|SDL_GL_DOUBLEBUFFER|SDL_FULLSCREEN); + if(ppModes == NULL) + { + // no modes + NumModes = 0; + } + else if(ppModes == (SDL_Rect**)-1) + { + // all modes + } + else + { + NumModes = 0; + for(int i = 0; ppModes[i]; ++i) + { + if(NumModes == MaxModes) + break; + pModes[NumModes].m_Width = ppModes[i]->w; + pModes[NumModes].m_Height = ppModes[i]->h; + pModes[NumModes].m_Red = 8; + pModes[NumModes].m_Green = 8; + pModes[NumModes].m_Blue = 8; + NumModes++; + } + } + + return NumModes;*/ +} + + +extern IEngineGraphics *CreateEngineGraphics() { return new CGraphics_Threaded(); } diff --git a/src/game/client/components/menus.cpp b/src/game/client/components/menus.cpp index fc1ca2db0..0e61ec6e1 100644 --- a/src/game/client/components/menus.cpp +++ b/src/game/client/components/menus.cpp @@ -628,7 +628,6 @@ int CMenus::RenderMenubar(CUIRect r) void CMenus::RenderLoading() { // TODO: not supported right now due to separate render thread - return; static int64 LastLoadRender = 0; float Percent = m_LoadCurrent++/(float)m_LoadTotal; From 09b785a290c8394422b1334e5e07714c7e994521 Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Sat, 31 Dec 2011 01:11:24 +0100 Subject: [PATCH 03/18] splitted out the graphics threaded into a separate header --- src/engine/client/graphics.h | 342 ----------------------- src/engine/client/graphics_threaded.cpp | 2 +- src/engine/client/graphics_threaded.h | 343 ++++++++++++++++++++++++ 3 files changed, 344 insertions(+), 343 deletions(-) create mode 100644 src/engine/client/graphics_threaded.h diff --git a/src/engine/client/graphics.h b/src/engine/client/graphics.h index 84142d9d5..95e9769ad 100644 --- a/src/engine/client/graphics.h +++ b/src/engine/client/graphics.h @@ -3,348 +3,6 @@ #ifndef ENGINE_CLIENT_GRAPHICS_H #define ENGINE_CLIENT_GRAPHICS_H -#include - -class CCommandBuffer -{ - class CBuffer - { - unsigned char *m_pData; - unsigned m_Size; - unsigned m_Used; - public: - CBuffer(unsigned BufferSize) - { - m_Size = BufferSize; - m_pData = new unsigned char[m_Size]; - m_Used = 0; - } - - ~CBuffer() - { - delete [] m_pData; - m_pData = 0x0; - m_Used = 0; - m_Size = 0; - } - - void Reset() - { - m_Used = 0; - } - - void *Alloc(unsigned Requested) - { - if(Requested + m_Used > m_Size) - return 0; - void *pPtr = &m_pData[m_Used]; - m_Used += Requested; - return pPtr; - } - - unsigned char *DataPtr() { return m_pData; } - unsigned DataSize() { return m_Size; } - unsigned DataUsed() { return m_Used; } - }; - - CBuffer m_CmdBuffer; - CBuffer m_DataBuffer; -public: - enum - { - // - CMD_NOP = 0, - - // - CMD_INIT, - CMD_SHUTDOWN, - - // - CMD_RUNBUFFER, - - // syncronization - CMD_SIGNAL, - - // texture commands - CMD_TEXTURE_CREATE, - CMD_TEXTURE_DESTROY, - - // rendering - CMD_CLEAR, - CMD_RENDER, - - // swap - CMD_SWAP, - }; - - enum - { - // - PRIMTYPE_INVALID = 0, - PRIMTYPE_LINES, - PRIMTYPE_QUADS, - }; - - enum - { - BLEND_NONE = 0, - BLEND_ALPHA, - BLEND_ADDITIVE, - }; - - struct SPoint { float x, y, z; }; - struct STexCoord { float u, v; }; - struct SColor { float r, g, b, a; }; - - struct SVertex - { - SPoint m_Pos; - STexCoord m_Tex; - SColor m_Color; - } ; - - struct SCommand - { - public: - SCommand(unsigned Cmd) : m_Cmd(Cmd), m_Size(0) {} - unsigned m_Cmd; - unsigned m_Size; - }; - - struct SState - { - int m_BlendMode; - int m_Texture; - SPoint m_ScreenTL; - SPoint m_ScreenBR; - - // clip - bool m_ClipEnable; - int m_ClipX; - int m_ClipY; - int m_ClipW; - int m_ClipH; - }; - - struct SCommand_Clear : public SCommand - { - SCommand_Clear() : SCommand(CMD_CLEAR) {} - SColor m_Color; - }; - - - struct SCommand_Init : public SCommand - { - SCommand_Init() : SCommand(CMD_INIT) {} - volatile int *m_pResult; - }; - - struct SCommand_RunBuffer : public SCommand - { - SCommand_RunBuffer() : SCommand(CMD_RUNBUFFER) {} - CCommandBuffer *m_pOtherBuffer; - }; - - struct SCommand_Render : public SCommand - { - SCommand_Render() : SCommand(CMD_RENDER) {} - SState m_State; - unsigned m_PrimType; - unsigned m_PrimCount; - SVertex *m_pVertices; - }; - - struct SCommand_Swap : public SCommand - { - SCommand_Swap() : SCommand(CMD_SWAP) {} - }; - - // - CCommandBuffer(unsigned CmdBufferSize, unsigned DataBufferSize) - : m_CmdBuffer(CmdBufferSize), m_DataBuffer(DataBufferSize) - { - } - - void *AllocData(unsigned WantedSize) - { - return m_DataBuffer.Alloc(WantedSize); - } - - template - void AddCommand(const T &Command) - { - SCommand *pCmd = (SCommand *)m_CmdBuffer.Alloc(sizeof(Command)); - if(!pCmd) - return; - - mem_copy(pCmd, &Command, sizeof(Command)); - pCmd->m_Size = sizeof(Command); - } - - SCommand *GetCommand(unsigned *pIndex) - { - if(*pIndex >= m_CmdBuffer.DataUsed()) - return NULL; - - SCommand *pCommand = (SCommand *)&m_CmdBuffer.DataPtr()[*pIndex]; - *pIndex += pCommand->m_Size; - return pCommand; - } - - void Reset() - { - m_CmdBuffer.Reset(); - m_DataBuffer.Reset(); - } -}; - -class ICommandProcessor -{ -public: - virtual ~ICommandProcessor() {} - virtual void RunBuffer(CCommandBuffer *pBuffer) = 0; -}; - - -class CCommandProcessorHandler -{ - ICommandProcessor *m_pProcessor; - CCommandBuffer * volatile m_pBuffer; - volatile bool m_Shutdown; - semaphore m_Activity; - semaphore m_BufferDone; - void *m_pThread; - - static void ThreadFunc(void *pUser); - -public: - CCommandProcessorHandler(); - void Start(ICommandProcessor *pProcessor); - void RunBuffer(CCommandBuffer *pBuffer); - bool IsIdle() const { return m_pBuffer == 0; } - void WaitForIdle(); -}; - -class CGraphics_Threaded : public IEngineGraphics -{ - CCommandBuffer::SState m_State; - CCommandProcessorHandler m_Handler; - - CCommandBuffer *m_apCommandBuffers[2]; - CCommandBuffer *m_pCommandBuffer; - unsigned m_CurrentCommandBuffer; - - // - class IStorage *m_pStorage; - class IConsole *m_pConsole; - - enum - { - MAX_VERTICES = 32*1024, - MAX_TEXTURES = 1024*4, - - DRAWING_QUADS=1, - DRAWING_LINES=2 - }; - - CCommandBuffer::SVertex m_aVertices[MAX_VERTICES]; - int m_NumVertices; - - CCommandBuffer::SColor m_aColor[4]; - CCommandBuffer::STexCoord m_aTexture[4]; - - bool m_RenderEnable; - - float m_Rotation; - int m_Drawing; - bool m_DoScreenshot; - char m_aScreenshotName[128]; - - int m_InvalidTexture; - - struct CTexture - { - GLuint m_Tex; - int m_MemSize; - int m_Flags; - int m_Next; - }; - - CTexture m_aTextures[MAX_TEXTURES]; - int m_FirstFreeTexture; - int m_TextureMemoryUsage; - - void Flush(); - void AddVertices(int Count); - void Rotate4(const CCommandBuffer::SPoint &rCenter, CCommandBuffer::SVertex *pPoints); - - static unsigned char Sample(int w, int h, const unsigned char *pData, int u, int v, int Offset, int ScaleW, int ScaleH, int Bpp); - static unsigned char *Rescale(int Width, int Height, int NewWidth, int NewHeight, int Format, const unsigned char *pData); -public: - CGraphics_Threaded(); - - virtual void ClipEnable(int x, int y, int w, int h); - virtual void ClipDisable(); - - virtual void BlendNone(); - virtual void BlendNormal(); - virtual void BlendAdditive(); - - virtual int MemoryUsage() const; - - virtual void MapScreen(float TopLeftX, float TopLeftY, float BottomRightX, float BottomRightY); - virtual void GetScreen(float *pTopLeftX, float *pTopLeftY, float *pBottomRightX, float *pBottomRightY); - - virtual void LinesBegin(); - virtual void LinesEnd(); - virtual void LinesDraw(const CLineItem *pArray, int Num); - - virtual int UnloadTexture(int Index); - virtual int LoadTextureRaw(int Width, int Height, int Format, const void *pData, int StoreFormat, int Flags); - - // simple uncompressed RGBA loaders - virtual int LoadTexture(const char *pFilename, int StorageType, int StoreFormat, int Flags); - virtual int LoadPNG(CImageInfo *pImg, const char *pFilename, int StorageType); - - void ScreenshotDirect(const char *pFilename); - - virtual void TextureSet(int TextureID); - - virtual void Clear(float r, float g, float b); - - virtual void QuadsBegin(); - virtual void QuadsEnd(); - virtual void QuadsSetRotation(float Angle); - - virtual void SetColorVertex(const CColorVertex *pArray, int Num); - virtual void SetColor(float r, float g, float b, float a); - - virtual void QuadsSetSubset(float TlU, float TlV, float BrU, float BrV); - virtual void QuadsSetSubsetFree( - float x0, float y0, float x1, float y1, - float x2, float y2, float x3, float y3); - - virtual void QuadsDraw(CQuadItem *pArray, int Num); - virtual void QuadsDrawTL(const CQuadItem *pArray, int Num); - virtual void QuadsDrawFreeform(const CFreeformItem *pArray, int Num); - virtual void QuadsText(float x, float y, float Size, float r, float g, float b, float a, const char *pText); - - virtual void Minimize(); - virtual void Maximize(); - - virtual int WindowActive(); - virtual int WindowOpen(); - - virtual bool Init(); - virtual void Shutdown(); - - virtual void TakeScreenshot(const char *pFilename); - virtual void Swap(); - - virtual int GetVideoModes(CVideoMode *pModes, int MaxModes); - -}; - class CGraphics_OpenGL : public IEngineGraphics { protected: diff --git a/src/engine/client/graphics_threaded.cpp b/src/engine/client/graphics_threaded.cpp index c227dbd52..66d17301f 100644 --- a/src/engine/client/graphics_threaded.cpp +++ b/src/engine/client/graphics_threaded.cpp @@ -18,7 +18,7 @@ #include // cosf, sinf -#include "graphics.h" +#include "graphics_threaded.h" static CVideoMode g_aFakeModes[] = { diff --git a/src/engine/client/graphics_threaded.h b/src/engine/client/graphics_threaded.h new file mode 100644 index 000000000..876e94104 --- /dev/null +++ b/src/engine/client/graphics_threaded.h @@ -0,0 +1,343 @@ +#pragma once + +#include + +class CCommandBuffer +{ + class CBuffer + { + unsigned char *m_pData; + unsigned m_Size; + unsigned m_Used; + public: + CBuffer(unsigned BufferSize) + { + m_Size = BufferSize; + m_pData = new unsigned char[m_Size]; + m_Used = 0; + } + + ~CBuffer() + { + delete [] m_pData; + m_pData = 0x0; + m_Used = 0; + m_Size = 0; + } + + void Reset() + { + m_Used = 0; + } + + void *Alloc(unsigned Requested) + { + if(Requested + m_Used > m_Size) + return 0; + void *pPtr = &m_pData[m_Used]; + m_Used += Requested; + return pPtr; + } + + unsigned char *DataPtr() { return m_pData; } + unsigned DataSize() { return m_Size; } + unsigned DataUsed() { return m_Used; } + }; + + CBuffer m_CmdBuffer; + CBuffer m_DataBuffer; +public: + enum + { + // + CMD_NOP = 0, + + // + CMD_INIT, + CMD_SHUTDOWN, + + // + CMD_RUNBUFFER, + + // syncronization + CMD_SIGNAL, + + // texture commands + CMD_TEXTURE_CREATE, + CMD_TEXTURE_DESTROY, + + // rendering + CMD_CLEAR, + CMD_RENDER, + + // swap + CMD_SWAP, + }; + + enum + { + // + PRIMTYPE_INVALID = 0, + PRIMTYPE_LINES, + PRIMTYPE_QUADS, + }; + + enum + { + BLEND_NONE = 0, + BLEND_ALPHA, + BLEND_ADDITIVE, + }; + + struct SPoint { float x, y, z; }; + struct STexCoord { float u, v; }; + struct SColor { float r, g, b, a; }; + + struct SVertex + { + SPoint m_Pos; + STexCoord m_Tex; + SColor m_Color; + } ; + + struct SCommand + { + public: + SCommand(unsigned Cmd) : m_Cmd(Cmd), m_Size(0) {} + unsigned m_Cmd; + unsigned m_Size; + }; + + struct SState + { + int m_BlendMode; + int m_Texture; + SPoint m_ScreenTL; + SPoint m_ScreenBR; + + // clip + bool m_ClipEnable; + int m_ClipX; + int m_ClipY; + int m_ClipW; + int m_ClipH; + }; + + struct SCommand_Clear : public SCommand + { + SCommand_Clear() : SCommand(CMD_CLEAR) {} + SColor m_Color; + }; + + + struct SCommand_Init : public SCommand + { + SCommand_Init() : SCommand(CMD_INIT) {} + volatile int *m_pResult; + }; + + struct SCommand_RunBuffer : public SCommand + { + SCommand_RunBuffer() : SCommand(CMD_RUNBUFFER) {} + CCommandBuffer *m_pOtherBuffer; + }; + + struct SCommand_Render : public SCommand + { + SCommand_Render() : SCommand(CMD_RENDER) {} + SState m_State; + unsigned m_PrimType; + unsigned m_PrimCount; + SVertex *m_pVertices; + }; + + struct SCommand_Swap : public SCommand + { + SCommand_Swap() : SCommand(CMD_SWAP) {} + }; + + // + CCommandBuffer(unsigned CmdBufferSize, unsigned DataBufferSize) + : m_CmdBuffer(CmdBufferSize), m_DataBuffer(DataBufferSize) + { + } + + void *AllocData(unsigned WantedSize) + { + return m_DataBuffer.Alloc(WantedSize); + } + + template + void AddCommand(const T &Command) + { + SCommand *pCmd = (SCommand *)m_CmdBuffer.Alloc(sizeof(Command)); + if(!pCmd) + return; + + mem_copy(pCmd, &Command, sizeof(Command)); + pCmd->m_Size = sizeof(Command); + } + + SCommand *GetCommand(unsigned *pIndex) + { + if(*pIndex >= m_CmdBuffer.DataUsed()) + return NULL; + + SCommand *pCommand = (SCommand *)&m_CmdBuffer.DataPtr()[*pIndex]; + *pIndex += pCommand->m_Size; + return pCommand; + } + + void Reset() + { + m_CmdBuffer.Reset(); + m_DataBuffer.Reset(); + } +}; + +class ICommandProcessor +{ +public: + virtual ~ICommandProcessor() {} + virtual void RunBuffer(CCommandBuffer *pBuffer) = 0; +}; + + +class CCommandProcessorHandler +{ + ICommandProcessor *m_pProcessor; + CCommandBuffer * volatile m_pBuffer; + volatile bool m_Shutdown; + semaphore m_Activity; + semaphore m_BufferDone; + void *m_pThread; + + static void ThreadFunc(void *pUser); + +public: + CCommandProcessorHandler(); + void Start(ICommandProcessor *pProcessor); + void RunBuffer(CCommandBuffer *pBuffer); + bool IsIdle() const { return m_pBuffer == 0; } + void WaitForIdle(); +}; + +class CGraphics_Threaded : public IEngineGraphics +{ + CCommandBuffer::SState m_State; + CCommandProcessorHandler m_Handler; + + CCommandBuffer *m_apCommandBuffers[2]; + CCommandBuffer *m_pCommandBuffer; + unsigned m_CurrentCommandBuffer; + + // + class IStorage *m_pStorage; + class IConsole *m_pConsole; + + enum + { + MAX_VERTICES = 32*1024, + MAX_TEXTURES = 1024*4, + + DRAWING_QUADS=1, + DRAWING_LINES=2 + }; + + CCommandBuffer::SVertex m_aVertices[MAX_VERTICES]; + int m_NumVertices; + + CCommandBuffer::SColor m_aColor[4]; + CCommandBuffer::STexCoord m_aTexture[4]; + + bool m_RenderEnable; + + float m_Rotation; + int m_Drawing; + bool m_DoScreenshot; + char m_aScreenshotName[128]; + + int m_InvalidTexture; + + struct CTexture + { + GLuint m_Tex; + int m_MemSize; + int m_Flags; + int m_Next; + }; + + CTexture m_aTextures[MAX_TEXTURES]; + int m_FirstFreeTexture; + int m_TextureMemoryUsage; + + void Flush(); + void AddVertices(int Count); + void Rotate4(const CCommandBuffer::SPoint &rCenter, CCommandBuffer::SVertex *pPoints); + + static unsigned char Sample(int w, int h, const unsigned char *pData, int u, int v, int Offset, int ScaleW, int ScaleH, int Bpp); + static unsigned char *Rescale(int Width, int Height, int NewWidth, int NewHeight, int Format, const unsigned char *pData); +public: + CGraphics_Threaded(); + + virtual void ClipEnable(int x, int y, int w, int h); + virtual void ClipDisable(); + + virtual void BlendNone(); + virtual void BlendNormal(); + virtual void BlendAdditive(); + + virtual int MemoryUsage() const; + + virtual void MapScreen(float TopLeftX, float TopLeftY, float BottomRightX, float BottomRightY); + virtual void GetScreen(float *pTopLeftX, float *pTopLeftY, float *pBottomRightX, float *pBottomRightY); + + virtual void LinesBegin(); + virtual void LinesEnd(); + virtual void LinesDraw(const CLineItem *pArray, int Num); + + virtual int UnloadTexture(int Index); + virtual int LoadTextureRaw(int Width, int Height, int Format, const void *pData, int StoreFormat, int Flags); + + // simple uncompressed RGBA loaders + virtual int LoadTexture(const char *pFilename, int StorageType, int StoreFormat, int Flags); + virtual int LoadPNG(CImageInfo *pImg, const char *pFilename, int StorageType); + + void ScreenshotDirect(const char *pFilename); + + virtual void TextureSet(int TextureID); + + virtual void Clear(float r, float g, float b); + + virtual void QuadsBegin(); + virtual void QuadsEnd(); + virtual void QuadsSetRotation(float Angle); + + virtual void SetColorVertex(const CColorVertex *pArray, int Num); + virtual void SetColor(float r, float g, float b, float a); + + virtual void QuadsSetSubset(float TlU, float TlV, float BrU, float BrV); + virtual void QuadsSetSubsetFree( + float x0, float y0, float x1, float y1, + float x2, float y2, float x3, float y3); + + virtual void QuadsDraw(CQuadItem *pArray, int Num); + virtual void QuadsDrawTL(const CQuadItem *pArray, int Num); + virtual void QuadsDrawFreeform(const CFreeformItem *pArray, int Num); + virtual void QuadsText(float x, float y, float Size, float r, float g, float b, float a, const char *pText); + + virtual void Minimize(); + virtual void Maximize(); + + virtual int WindowActive(); + virtual int WindowOpen(); + + virtual bool Init(); + virtual void Shutdown(); + + virtual void TakeScreenshot(const char *pFilename); + virtual void Swap(); + + virtual int GetVideoModes(CVideoMode *pModes, int MaxModes); + +}; \ No newline at end of file From 50e75da3334e77d027d5b10d85d4022c5dca0821 Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Sat, 31 Dec 2011 01:23:04 +0100 Subject: [PATCH 04/18] fixed so that the resolusion is set correctly on init --- src/engine/client/graphics_threaded.cpp | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/src/engine/client/graphics_threaded.cpp b/src/engine/client/graphics_threaded.cpp index 66d17301f..c98ab8b99 100644 --- a/src/engine/client/graphics_threaded.cpp +++ b/src/engine/client/graphics_threaded.cpp @@ -141,14 +141,8 @@ class CCommandProcessorFragment_SDL // SDL stuff SDL_Surface *m_pScreenSurface; - int m_ScreenWidth; - int m_ScreenHeight; - int TryInit() { - m_ScreenWidth = g_Config.m_GfxScreenWidth; - m_ScreenHeight = g_Config.m_GfxScreenHeight; - const SDL_VideoInfo *pInfo = SDL_GetVideoInfo(); SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE); @@ -187,7 +181,7 @@ class CCommandProcessorFragment_SDL SDL_WM_SetCaption("Teeworlds", "Teeworlds"); // create window - m_pScreenSurface = SDL_SetVideoMode(m_ScreenWidth, m_ScreenHeight, 0, Flags); + m_pScreenSurface = SDL_SetVideoMode(g_Config.m_GfxScreenWidth, g_Config.m_GfxScreenHeight, 0, Flags); if(m_pScreenSurface == NULL) { dbg_msg("gfx", "unable to set video mode: %s", SDL_GetError()); @@ -382,17 +376,6 @@ void CCommandProcessorHandler::WaitForIdle() m_BufferDone.wait(); } - -/* -void RenderThread() -{ - .wait() - if(m_pCommandBuffer) - RunBuffer(m_pCommandBuffer); - // reset buffer? - m_pCommandBuffer = 0; -}*/ - void CGraphics_Threaded::Flush() { if(m_NumVertices == 0) @@ -1088,6 +1071,10 @@ bool CGraphics_Threaded::Init() if(Result == 0) { + // fetch final resolusion + m_ScreenWidth = g_Config.m_GfxScreenWidth; + m_ScreenHeight = g_Config.m_GfxScreenHeight; + // create null texture, will get id=0 static const unsigned char aNullTextureData[] = { 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0x00,0xff,0x00,0xff, 0x00,0xff,0x00,0xff, From 8a91bfa1ddff0c99d65375b8b5c57e710169543a Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Sat, 31 Dec 2011 09:40:11 +0100 Subject: [PATCH 05/18] fixed texture upload support. removed raw gl calls from the text render --- src/engine/client/graphics.cpp | 13 ++ src/engine/client/graphics.h | 1 + src/engine/client/graphics_threaded.cpp | 278 +++++++++++++----------- src/engine/client/graphics_threaded.h | 55 ++++- src/engine/client/text.cpp | 55 ++--- src/engine/graphics.h | 1 + 6 files changed, 250 insertions(+), 153 deletions(-) diff --git a/src/engine/client/graphics.cpp b/src/engine/client/graphics.cpp index 2d034f3f7..b78e5387f 100644 --- a/src/engine/client/graphics.cpp +++ b/src/engine/client/graphics.cpp @@ -270,6 +270,18 @@ int CGraphics_OpenGL::UnloadTexture(int Index) return 0; } +int CGraphics_OpenGL::LoadTextureRawSub(int TextureID, int x, int y, int Width, int Height, int Format, const void *pData) +{ + int Oglformat = GL_RGBA; + if(Format == CImageInfo::FORMAT_RGB) + Oglformat = GL_RGB; + else if(Format == CImageInfo::FORMAT_ALPHA) + Oglformat = GL_ALPHA; + + glBindTexture(GL_TEXTURE_2D, m_aTextures[TextureID].m_Tex); + glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, Width, Height, Oglformat, GL_UNSIGNED_BYTE, pData); + return 0; +} int CGraphics_OpenGL::LoadTextureRaw(int Width, int Height, int Format, const void *pData, int StoreFormat, int Flags) { @@ -336,6 +348,7 @@ int CGraphics_OpenGL::LoadTextureRaw(int Width, int Height, int Format, const vo glGenTextures(1, &m_aTextures[Tex].m_Tex); glBindTexture(GL_TEXTURE_2D, m_aTextures[Tex].m_Tex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); gluBuild2DMipmaps(GL_TEXTURE_2D, StoreOglformat, Width, Height, Oglformat, GL_UNSIGNED_BYTE, pTexData); diff --git a/src/engine/client/graphics.h b/src/engine/client/graphics.h index 95e9769ad..d97513f38 100644 --- a/src/engine/client/graphics.h +++ b/src/engine/client/graphics.h @@ -89,6 +89,7 @@ public: virtual int UnloadTexture(int Index); virtual int LoadTextureRaw(int Width, int Height, int Format, const void *pData, int StoreFormat, int Flags); + virtual int LoadTextureRawSub(int TextureID, int x, int y, int Width, int Height, int Format, const void *pData); // simple uncompressed RGBA loaders virtual int LoadTexture(const char *pFilename, int StorageType, int StoreFormat, int Flags); diff --git a/src/engine/client/graphics_threaded.cpp b/src/engine/client/graphics_threaded.cpp index c98ab8b99..ade77ef4d 100644 --- a/src/engine/client/graphics_threaded.cpp +++ b/src/engine/client/graphics_threaded.cpp @@ -48,7 +48,13 @@ static CVideoMode g_aFakeModes[] = { class CCommandProcessorFragment_OpenGL { + GLuint m_aTextures[CCommandBuffer::MAX_TEXTURES]; public: + CCommandProcessorFragment_OpenGL() + { + mem_zero(m_aTextures, sizeof(m_aTextures)); + } + void SetState(const CCommandBuffer::SState &State) { // blend @@ -70,22 +76,75 @@ public: }; // clip - /*if(State.m_ClipEnable) + if(State.m_ClipEnable) { - glScissor(State.m_ClipX, State.m_ClipY+State.m_ClipH, State.m_ClipW, State.m_ClipH); + glScissor(State.m_ClipX, State.m_ClipY, State.m_ClipW, State.m_ClipH); glEnable(GL_SCISSOR_TEST); } - else*/ + else glDisable(GL_SCISSOR_TEST); // texture - glDisable(GL_TEXTURE_2D); + if(State.m_Texture >= 0 && State.m_Texture < CCommandBuffer::MAX_TEXTURES) + { + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, m_aTextures[State.m_Texture]); + } + else + glDisable(GL_TEXTURE_2D); // screen mapping glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(State.m_ScreenTL.x, State.m_ScreenBR.x, State.m_ScreenBR.y, State.m_ScreenTL.y, 1.0f, 10.f); + } + int TexFormatToOpenGLFormat(int TexFormat) + { + if(TexFormat == CCommandBuffer::TEXFORMAT_RGB) return GL_RGB; + if(TexFormat == CCommandBuffer::TEXFORMAT_ALPHA) return GL_ALPHA; + if(TexFormat == CCommandBuffer::TEXFORMAT_RGBA) return GL_RGBA; + return GL_RGBA; + } + + void Cmd_Texture_Update(const CCommandBuffer::SCommand_Texture_Update *pCommand) + { + glBindTexture(GL_TEXTURE_2D, m_aTextures[pCommand->m_Slot]); + glTexSubImage2D(GL_TEXTURE_2D, 0, pCommand->m_X, pCommand->m_Y, pCommand->m_Width, pCommand->m_Height, + TexFormatToOpenGLFormat(pCommand->m_Format), GL_UNSIGNED_BYTE, pCommand->m_pData); + mem_free(pCommand->m_pData); + } + + void Cmd_Texture_Destroy(const CCommandBuffer::SCommand_Texture_Destroy *pCommand) + { + glDeleteTextures(1, &m_aTextures[pCommand->m_Slot]); + } + + void Cmd_Texture_Create(const CCommandBuffer::SCommand_Texture_Create *pCommand) + { + int Oglformat = TexFormatToOpenGLFormat(pCommand->m_Format); + + // upload texture + int StoreOglformat = Oglformat; + if(g_Config.m_GfxTextureCompression) + { + StoreOglformat = GL_COMPRESSED_RGBA_ARB; + if(pCommand->m_StoreFormat == CCommandBuffer::TEXFORMAT_RGB) + StoreOglformat = GL_COMPRESSED_RGB_ARB; + else if(Oglformat == CCommandBuffer::TEXFORMAT_ALPHA) + StoreOglformat = GL_COMPRESSED_ALPHA_ARB; + } + else + { + StoreOglformat = TexFormatToOpenGLFormat(pCommand->m_StoreFormat); + } + + glGenTextures(1, &m_aTextures[pCommand->m_Slot]); + glBindTexture(GL_TEXTURE_2D, m_aTextures[pCommand->m_Slot]); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); + gluBuild2DMipmaps(GL_TEXTURE_2D, StoreOglformat, pCommand->m_Width, pCommand->m_Height, Oglformat, GL_UNSIGNED_BYTE, pCommand->m_pData); + mem_free(pCommand->m_pData); } bool RunCommand(const CCommandBuffer::SCommand * pBaseCommand) @@ -94,9 +153,17 @@ public: switch(pBaseCommand->m_Cmd) { case CCommandBuffer::CMD_TEXTURE_CREATE: - break; + { + Cmd_Texture_Create((const CCommandBuffer::SCommand_Texture_Create *)pBaseCommand); + } break; case CCommandBuffer::CMD_TEXTURE_DESTROY: - break; + { + Cmd_Texture_Destroy((const CCommandBuffer::SCommand_Texture_Destroy *)pBaseCommand); + } break; + case CCommandBuffer::CMD_TEXTURE_UPDATE: + { + Cmd_Texture_Update((const CCommandBuffer::SCommand_Texture_Update *)pBaseCommand); + } break; case CCommandBuffer::CMD_CLEAR: { const CCommandBuffer::SCommand_Clear *pCommand = (CCommandBuffer::SCommand_Clear *)pBaseCommand; @@ -593,122 +660,112 @@ void CGraphics_Threaded::LinesDraw(const CLineItem *pArray, int Num) int CGraphics_Threaded::UnloadTexture(int Index) { - return 0; if(Index == m_InvalidTexture) return 0; if(Index < 0) return 0; - glDeleteTextures(1, &m_aTextures[Index].m_Tex); + CCommandBuffer::SCommand_Texture_Destroy Cmd; + Cmd.m_Slot = Index; + m_pCommandBuffer->AddCommand(Cmd); + m_aTextures[Index].m_Next = m_FirstFreeTexture; m_TextureMemoryUsage -= m_aTextures[Index].m_MemSize; m_FirstFreeTexture = Index; return 0; } +static int ImageFormatToTexFormat(int Format) +{ + if(Format == CImageInfo::FORMAT_RGB) return CCommandBuffer::TEXFORMAT_RGB; + if(Format == CImageInfo::FORMAT_RGBA) return CCommandBuffer::TEXFORMAT_RGBA; + if(Format == CImageInfo::FORMAT_ALPHA) return CCommandBuffer::TEXFORMAT_ALPHA; + return CCommandBuffer::TEXFORMAT_RGBA; +} + + +int CGraphics_Threaded::LoadTextureRawSub(int TextureID, int x, int y, int Width, int Height, int Format, const void *pData) +{ + CCommandBuffer::SCommand_Texture_Update Cmd; + Cmd.m_Slot = TextureID; + Cmd.m_X = x; + Cmd.m_Y = y; + Cmd.m_Width = Width; + Cmd.m_Height = Height; + Cmd.m_Format = ImageFormatToTexFormat(Format); + + // calculate memory usage + int PixelSize = 4; + if(Format == CImageInfo::FORMAT_RGB) + PixelSize = 3; + else if(Format == CImageInfo::FORMAT_ALPHA) + PixelSize = 1; + + int MemSize = Width*Height*PixelSize; + + // copy texture data + void *pTmpData = mem_alloc(MemSize, sizeof(void*)); + mem_copy(pTmpData, pData, MemSize); + Cmd.m_pData = pTmpData; + + // + m_pCommandBuffer->AddCommand(Cmd); + return 0; +} int CGraphics_Threaded::LoadTextureRaw(int Width, int Height, int Format, const void *pData, int StoreFormat, int Flags) { - return 0; - - int Mipmap = 1; - unsigned char *pTexData = (unsigned char *)pData; - unsigned char *pTmpData = 0; - int Oglformat = 0; - int StoreOglformat = 0; - int Tex = 0; - // don't waste memory on texture if we are stress testing if(g_Config.m_DbgStress) - return m_InvalidTexture; + return m_InvalidTexture; // grab texture - Tex = m_FirstFreeTexture; + int Tex = m_FirstFreeTexture; m_FirstFreeTexture = m_aTextures[Tex].m_Next; m_aTextures[Tex].m_Next = -1; - // resample if needed - if(!(Flags&TEXLOAD_NORESAMPLE) && (Format == CImageInfo::FORMAT_RGBA || Format == CImageInfo::FORMAT_RGB)) - { - if(Width > GL_MAX_TEXTURE_SIZE || Height > GL_MAX_TEXTURE_SIZE) - { - int NewWidth = min(Width, GL_MAX_TEXTURE_SIZE); - int NewHeight = min(Height, GL_MAX_TEXTURE_SIZE); - pTmpData = Rescale(Width, Height, NewWidth, NewHeight, Format, pTexData); - pTexData = pTmpData; - Width = NewWidth; - Height = NewHeight; - } - else if(Width > 16 && Height > 16 && g_Config.m_GfxTextureQuality == 0) - { - pTmpData = Rescale(Width, Height, Width/2, Height/2, Format, pTexData); - pTexData = pTmpData; - Width /= 2; - Height /= 2; - } - } - - Oglformat = GL_RGBA; - if(Format == CImageInfo::FORMAT_RGB) - Oglformat = GL_RGB; - else if(Format == CImageInfo::FORMAT_ALPHA) - Oglformat = GL_ALPHA; - - // upload texture - if(g_Config.m_GfxTextureCompression) - { - StoreOglformat = GL_COMPRESSED_RGBA_ARB; - if(StoreFormat == CImageInfo::FORMAT_RGB) - StoreOglformat = GL_COMPRESSED_RGB_ARB; - else if(StoreFormat == CImageInfo::FORMAT_ALPHA) - StoreOglformat = GL_COMPRESSED_ALPHA_ARB; - } - else - { - StoreOglformat = GL_RGBA; - if(StoreFormat == CImageInfo::FORMAT_RGB) - StoreOglformat = GL_RGB; - else if(StoreFormat == CImageInfo::FORMAT_ALPHA) - StoreOglformat = GL_ALPHA; - } - - glGenTextures(1, &m_aTextures[Tex].m_Tex); - glBindTexture(GL_TEXTURE_2D, m_aTextures[Tex].m_Tex); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); - gluBuild2DMipmaps(GL_TEXTURE_2D, StoreOglformat, Width, Height, Oglformat, GL_UNSIGNED_BYTE, pTexData); + CCommandBuffer::SCommand_Texture_Create Cmd; + Cmd.m_Slot = Tex; + Cmd.m_Width = Width; + Cmd.m_Height = Height; + Cmd.m_Format = ImageFormatToTexFormat(Format); + Cmd.m_StoreFormat = ImageFormatToTexFormat(StoreFormat); // calculate memory usage - { - int PixelSize = 4; - if(StoreFormat == CImageInfo::FORMAT_RGB) - PixelSize = 3; - else if(StoreFormat == CImageInfo::FORMAT_ALPHA) - PixelSize = 1; + int PixelSize = 4; + if(Format == CImageInfo::FORMAT_RGB) + PixelSize = 3; + else if(Format == CImageInfo::FORMAT_ALPHA) + PixelSize = 1; - m_aTextures[Tex].m_MemSize = Width*Height*PixelSize; - if(Mipmap) - { - while(Width > 2 && Height > 2) - { - Width>>=1; - Height>>=1; - m_aTextures[Tex].m_MemSize += Width*Height*PixelSize; - } - } + int MemSize = Width*Height*PixelSize; + + // copy texture data + void *pTmpData = mem_alloc(MemSize, sizeof(void*)); + mem_copy(pTmpData, pData, MemSize); + Cmd.m_pData = pTmpData; + + // + m_pCommandBuffer->AddCommand(Cmd); + + // calculate memory usage + int MemUsage = MemSize; + while(Width > 2 && Height > 2) + { + Width>>=1; + Height>>=1; + MemUsage += Width*Height*PixelSize; } - m_TextureMemoryUsage += m_aTextures[Tex].m_MemSize; - mem_free(pTmpData); + m_TextureMemoryUsage += MemUsage; + //mem_free(pTmpData); return Tex; } // simple uncompressed RGBA loaders int CGraphics_Threaded::LoadTexture(const char *pFilename, int StorageType, int StoreFormat, int Flags) { - return 0; - int l = str_length(pFilename); int ID; CImageInfo Img; @@ -732,8 +789,6 @@ int CGraphics_Threaded::LoadTexture(const char *pFilename, int StorageType, int int CGraphics_Threaded::LoadPNG(CImageInfo *pImg, const char *pFilename, int StorageType) { - return 0; - char aCompleteFilename[512]; unsigned char *pBuffer; png_t Png; // ignore_convention @@ -782,6 +837,10 @@ int CGraphics_Threaded::LoadPNG(CImageInfo *pImg, const char *pFilename, int Sto void CGraphics_Threaded::ScreenshotDirect(const char *pFilename) { + // TODO: screenshot support + return; + + /* // fetch image data int y; int w = m_ScreenWidth; @@ -822,22 +881,13 @@ void CGraphics_Threaded::ScreenshotDirect(const char *pFilename) // clean up mem_free(pPixelData); + */ } void CGraphics_Threaded::TextureSet(int TextureID) { - return; - dbg_assert(m_Drawing == 0, "called Graphics()->TextureSet within begin"); - if(TextureID == -1) - { - glDisable(GL_TEXTURE_2D); - } - else - { - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, m_aTextures[TextureID].m_Tex); - } + m_State.m_Texture = TextureID; } void CGraphics_Threaded::Clear(float r, float g, float b) @@ -1130,22 +1180,6 @@ void CGraphics_Threaded::TakeScreenshot(const char *pFilename) void CGraphics_Threaded::Swap() { - if(0) { - CCommandBuffer::SCommand_Clear Cmd; - Cmd.m_Color.r = 1.0f; - Cmd.m_Color.g = 0.0f; - Cmd.m_Color.b = 0.0f; - Cmd.m_Color.a = 0.0f; - m_pCommandBuffer->AddCommand(Cmd); - } - - CCommandBuffer::SCommand_Swap Cmd; - m_pCommandBuffer->AddCommand(Cmd); - - m_Handler.RunBuffer(m_pCommandBuffer); - m_Handler.WaitForIdle(); - m_pCommandBuffer->Reset(); - // TODO: screenshot support /* if(m_DoScreenshot) @@ -1154,10 +1188,12 @@ void CGraphics_Threaded::Swap() m_DoScreenshot = false; }*/ - //SDL_GL_SwapBuffers(); + CCommandBuffer::SCommand_Swap Cmd; + m_pCommandBuffer->AddCommand(Cmd); - //if(g_Config.m_GfxFinish) - // glFinish(); + m_Handler.RunBuffer(m_pCommandBuffer); + m_Handler.WaitForIdle(); + m_pCommandBuffer->Reset(); } diff --git a/src/engine/client/graphics_threaded.h b/src/engine/client/graphics_threaded.h index 876e94104..1ec8c61ca 100644 --- a/src/engine/client/graphics_threaded.h +++ b/src/engine/client/graphics_threaded.h @@ -47,6 +47,11 @@ class CCommandBuffer CBuffer m_CmdBuffer; CBuffer m_DataBuffer; public: + enum + { + MAX_TEXTURES=1024*4, + }; + enum { // @@ -65,6 +70,7 @@ public: // texture commands CMD_TEXTURE_CREATE, CMD_TEXTURE_DESTROY, + CMD_TEXTURE_UPDATE, // rendering CMD_CLEAR, @@ -74,6 +80,14 @@ public: CMD_SWAP, }; + enum + { + TEXFORMAT_INVALID = 0, + TEXFORMAT_RGB, + TEXFORMAT_RGBA, + TEXFORMAT_ALPHA, + }; + enum { // @@ -155,6 +169,44 @@ public: { SCommand_Swap() : SCommand(CMD_SWAP) {} }; + + struct SCommand_Texture_Create : public SCommand + { + SCommand_Texture_Create() : SCommand(CMD_TEXTURE_CREATE) {} + + // texture information + int m_Slot; + + int m_Width; + int m_Height; + int m_Format; + int m_StoreFormat; + void *m_pData; // will be freed by the command processor + }; + + struct SCommand_Texture_Update : public SCommand + { + SCommand_Texture_Update() : SCommand(CMD_TEXTURE_UPDATE) {} + + // texture information + int m_Slot; + + int m_X; + int m_Y; + int m_Width; + int m_Height; + int m_Format; + void *m_pData; // will be freed by the command processor + }; + + + struct SCommand_Texture_Destroy : public SCommand + { + SCommand_Texture_Destroy() : SCommand(CMD_TEXTURE_DESTROY) {} + + // texture information + int m_Slot; + }; // CCommandBuffer(unsigned CmdBufferSize, unsigned DataBufferSize) @@ -261,7 +313,7 @@ class CGraphics_Threaded : public IEngineGraphics struct CTexture { - GLuint m_Tex; + int m_State; int m_MemSize; int m_Flags; int m_Next; @@ -298,6 +350,7 @@ public: virtual int UnloadTexture(int Index); virtual int LoadTextureRaw(int Width, int Height, int Format, const void *pData, int StoreFormat, int Flags); + virtual int LoadTextureRawSub(int TextureID, int x, int y, int Width, int Height, int Format, const void *pData); // simple uncompressed RGBA loaders virtual int LoadTexture(const char *pFilename, int StorageType, int StoreFormat, int Flags); diff --git a/src/engine/client/text.cpp b/src/engine/client/text.cpp index 4a3a2eb33..51eed5471 100644 --- a/src/engine/client/text.cpp +++ b/src/engine/client/text.cpp @@ -9,21 +9,11 @@ #include #endif -#ifdef CONF_PLATFORM_MACOSX - #include - #include -#else - #include - #include -#endif - // ft2 texture #include #include FT_FREETYPE_H // TODO: Refactor: clean this up - - enum { MAX_CHARACTERS = 64, @@ -54,7 +44,7 @@ struct CFontSizeData int m_FontSize; FT_Face *m_pFace; - GLuint m_aTextures[2]; + int m_aTextures[2]; int m_TextureWidth; int m_TextureHeight; @@ -107,7 +97,7 @@ class CTextRender : public IEngineTextRender float m_TextOutlineB; float m_TextOutlineA; - int m_FontTextureFormat; + //int m_FontTextureFormat; CFont *m_pDefaultFont; @@ -158,26 +148,25 @@ class CTextRender : public IEngineTextRender void *pMem = mem_alloc(Width*Height, 1); mem_zero(pMem, Width*Height); - if(pSizeData->m_aTextures[0] == 0) - glGenTextures(2, pSizeData->m_aTextures); - else - FontMemoryUsage -= pSizeData->m_TextureWidth*pSizeData->m_TextureHeight*2; + for(int i = 0; i < 2; i++) + { + if(pSizeData->m_aTextures[i] != 0) + { + Graphics()->UnloadTexture(pSizeData->m_aTextures[i]); + FontMemoryUsage -= pSizeData->m_TextureWidth*pSizeData->m_TextureHeight; + pSizeData->m_aTextures[i] = 0; + } + + pSizeData->m_aTextures[i] = Graphics()->LoadTextureRaw(Width, Height, CImageInfo::FORMAT_ALPHA, pMem, CImageInfo::FORMAT_ALPHA, 0); + FontMemoryUsage += Width*Height; + } pSizeData->m_NumXChars = Xchars; pSizeData->m_NumYChars = Ychars; pSizeData->m_TextureWidth = Width; pSizeData->m_TextureHeight = Height; pSizeData->m_CurrentCharacter = 0; - - for(int i = 0; i < 2; i++) - { - glBindTexture(GL_TEXTURE_2D, pSizeData->m_aTextures[i]); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, m_FontTextureFormat, Width, Height, 0, m_FontTextureFormat, GL_UNSIGNED_BYTE, pMem); - FontMemoryUsage += Width*Height; - } - + dbg_msg("", "pFont memory usage: %d", FontMemoryUsage); mem_free(pMem); @@ -254,11 +243,16 @@ class CTextRender : public IEngineTextRender int x = (SlotID%pSizeData->m_NumXChars) * (pSizeData->m_TextureWidth/pSizeData->m_NumXChars); int y = (SlotID/pSizeData->m_NumXChars) * (pSizeData->m_TextureHeight/pSizeData->m_NumYChars); + Graphics()->LoadTextureRawSub(pSizeData->m_aTextures[Texnum], x, y, + pSizeData->m_TextureWidth/pSizeData->m_NumXChars, + pSizeData->m_TextureHeight/pSizeData->m_NumYChars, + CImageInfo::FORMAT_ALPHA, pData); + /* glBindTexture(GL_TEXTURE_2D, pSizeData->m_aTextures[Texnum]); glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, pSizeData->m_TextureWidth/pSizeData->m_NumXChars, pSizeData->m_TextureHeight/pSizeData->m_NumYChars, - m_FontTextureFormat, GL_UNSIGNED_BYTE, pData); + m_FontTextureFormat, GL_UNSIGNED_BYTE, pData);*/ } // 32k of data used for rendering glyphs @@ -455,7 +449,7 @@ public: m_pDefaultFont = 0; // GL_LUMINANCE can be good for debugging - m_FontTextureFormat = GL_ALPHA; + //m_FontTextureFormat = GL_ALPHA; } virtual void Init() @@ -620,11 +614,10 @@ public: if(pCursor->m_Flags&TEXTFLAG_RENDER) { // TODO: Make this better - glEnable(GL_TEXTURE_2D); if (i == 0) - glBindTexture(GL_TEXTURE_2D, pSizeData->m_aTextures[1]); + Graphics()->TextureSet(pSizeData->m_aTextures[1]); else - glBindTexture(GL_TEXTURE_2D, pSizeData->m_aTextures[0]); + Graphics()->TextureSet(pSizeData->m_aTextures[0]); Graphics()->QuadsBegin(); if (i == 0) diff --git a/src/engine/graphics.h b/src/engine/graphics.h index e1652cbe8..cbc6a331f 100644 --- a/src/engine/graphics.h +++ b/src/engine/graphics.h @@ -80,6 +80,7 @@ public: virtual int UnloadTexture(int Index) = 0; virtual int LoadTextureRaw(int Width, int Height, int Format, const void *pData, int StoreFormat, int Flags) = 0; virtual int LoadTexture(const char *pFilename, int StorageType, int StoreFormat, int Flags) = 0; + virtual int LoadTextureRawSub(int TextureID, int x, int y, int Width, int Height, int Format, const void *pData) = 0; virtual void TextureSet(int TextureID) = 0; struct CLineItem From 7a36a103aec1c0aafc89a45a4c7fb82944e6baba Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Sat, 31 Dec 2011 10:04:46 +0100 Subject: [PATCH 06/18] fixed so that the rendering can be done async from the input, network and update --- src/engine/client/client.cpp | 17 +++--- src/engine/client/graphics.cpp | 15 ++++++ src/engine/client/graphics.h | 4 ++ src/engine/client/graphics_threaded.cpp | 70 +++++++++++++++++++++---- src/engine/client/graphics_threaded.h | 11 +++- src/engine/graphics.h | 7 +++ src/engine/shared/config_variables.h | 1 + 7 files changed, 106 insertions(+), 19 deletions(-) diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index bb888c608..53f60fa8f 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -1853,19 +1853,22 @@ void CClient::Run() Update(); - if(g_Config.m_DbgStress) + if(!g_Config.m_GfxAsyncRender || m_pGraphics->IsIdle()) { - if((m_Frames%10) == 0) + if(g_Config.m_DbgStress) + { + if((m_Frames%10) == 0) + { + Render(); + m_pGraphics->Swap(); + } + } + else { Render(); m_pGraphics->Swap(); } } - else - { - Render(); - m_pGraphics->Swap(); - } } AutoScreenshot_Cleanup(); diff --git a/src/engine/client/graphics.cpp b/src/engine/client/graphics.cpp index b78e5387f..03a6e7cd2 100644 --- a/src/engine/client/graphics.cpp +++ b/src/engine/client/graphics.cpp @@ -963,4 +963,19 @@ int CGraphics_SDL::GetVideoModes(CVideoMode *pModes, int MaxModes) return NumModes; } +// syncronization +void CGraphics_SDL::InsertSignal(semaphore *pSemaphore) +{ + pSemaphore->signal(); +} + +bool CGraphics_SDL::IsIdle() +{ + return true; +} + +void CGraphics_SDL::WaitForIdle() +{ +} + //extern IEngineGraphics *CreateEngineGraphics() { return new CGraphics_SDL(); } diff --git a/src/engine/client/graphics.h b/src/engine/client/graphics.h index d97513f38..37276d36f 100644 --- a/src/engine/client/graphics.h +++ b/src/engine/client/graphics.h @@ -144,6 +144,10 @@ public: virtual int GetVideoModes(CVideoMode *pModes, int MaxModes); + // syncronization + virtual void InsertSignal(semaphore *pSemaphore); + virtual bool IsIdle(); + virtual void WaitForIdle(); }; #endif diff --git a/src/engine/client/graphics_threaded.cpp b/src/engine/client/graphics_threaded.cpp index ade77ef4d..e702133a8 100644 --- a/src/engine/client/graphics_threaded.cpp +++ b/src/engine/client/graphics_threaded.cpp @@ -45,6 +45,28 @@ static CVideoMode g_aFakeModes[] = { {2048,1536,5,6,5} }; +class CCommandProcessorFragment_General +{ +public: + bool RunCommand(const CCommandBuffer::SCommand * pBaseCommand) + { + + switch(pBaseCommand->m_Cmd) + { + case CCommandBuffer::CMD_SIGNAL: + { + const CCommandBuffer::SCommand_Signal *pCommand = static_cast(pBaseCommand); + pCommand->m_pSemaphore->signal(); + } break; + default: + return false; + break; + } + + return true; + } +}; + class CCommandProcessorFragment_OpenGL { @@ -154,15 +176,15 @@ public: { case CCommandBuffer::CMD_TEXTURE_CREATE: { - Cmd_Texture_Create((const CCommandBuffer::SCommand_Texture_Create *)pBaseCommand); + Cmd_Texture_Create(static_cast(pBaseCommand)); } break; case CCommandBuffer::CMD_TEXTURE_DESTROY: { - Cmd_Texture_Destroy((const CCommandBuffer::SCommand_Texture_Destroy *)pBaseCommand); + Cmd_Texture_Destroy(static_cast(pBaseCommand)); } break; case CCommandBuffer::CMD_TEXTURE_UPDATE: { - Cmd_Texture_Update((const CCommandBuffer::SCommand_Texture_Update *)pBaseCommand); + Cmd_Texture_Update(static_cast(pBaseCommand)); } break; case CCommandBuffer::CMD_CLEAR: { @@ -172,7 +194,7 @@ public: } break; case CCommandBuffer::CMD_RENDER: { - const CCommandBuffer::SCommand_Render *pCommand = (CCommandBuffer::SCommand_Render *)pBaseCommand; + const CCommandBuffer::SCommand_Render *pCommand = static_cast(pBaseCommand); SetState(pCommand->m_State); glVertexPointer(3, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices); @@ -354,7 +376,7 @@ public: break; case CCommandBuffer::CMD_INIT: { - const CCommandBuffer::SCommand_Init *pCommand = (CCommandBuffer::SCommand_Init *)pBaseCommand; + const CCommandBuffer::SCommand_Init *pCommand = static_cast(pBaseCommand); *pCommand->m_pResult = Init(); } break; case CCommandBuffer::CMD_SHUTDOWN: @@ -372,18 +394,18 @@ public: } }; - class CCommandProcessor_SDL_OpenGL : public ICommandProcessor { CCommandProcessorFragment_OpenGL m_OpenGL; CCommandProcessorFragment_SDL m_SDL; + CCommandProcessorFragment_General m_General; public: virtual void RunBuffer(CCommandBuffer *pBuffer) { unsigned CmdIndex = 0; while(1) { - CCommandBuffer::SCommand * const pBaseCommand = pBuffer->GetCommand(&CmdIndex); + const CCommandBuffer::SCommand *pBaseCommand = pBuffer->GetCommand(&CmdIndex); if(pBaseCommand == 0x0) break; @@ -392,6 +414,9 @@ class CCommandProcessor_SDL_OpenGL : public ICommandProcessor if(m_SDL.RunCommand(pBaseCommand)) continue; + + if(m_General.RunCommand(pBaseCommand)) + continue; dbg_msg("graphics", "unknown command %d", pBaseCommand->m_Cmd); } @@ -547,7 +572,10 @@ CGraphics_Threaded::CGraphics_Threaded() m_State.m_Texture = -1; m_State.m_BlendMode = CCommandBuffer::BLEND_NONE; - + m_CurrentCommandBuffer = 0; + m_pCommandBuffer = 0x0; + m_apCommandBuffers[0] = 0x0; + m_apCommandBuffers[1] = 0x0; m_NumVertices = 0; @@ -748,7 +776,7 @@ int CGraphics_Threaded::LoadTextureRaw(int Width, int Height, int Format, const // m_pCommandBuffer->AddCommand(Cmd); - + // calculate memory usage int MemUsage = MemSize; while(Width > 2 && Height > 2) @@ -1188,14 +1216,34 @@ void CGraphics_Threaded::Swap() m_DoScreenshot = false; }*/ + // add swap command CCommandBuffer::SCommand_Swap Cmd; m_pCommandBuffer->AddCommand(Cmd); - m_Handler.RunBuffer(m_pCommandBuffer); - m_Handler.WaitForIdle(); + + // swap buffer + m_CurrentCommandBuffer ^= 1; + m_pCommandBuffer = m_apCommandBuffers[m_CurrentCommandBuffer]; m_pCommandBuffer->Reset(); } +// syncronization +void CGraphics_Threaded::InsertSignal(semaphore *pSemaphore) +{ + CCommandBuffer::SCommand_Signal Cmd; + Cmd.m_pSemaphore = pSemaphore; + m_pCommandBuffer->AddCommand(Cmd); +} + +bool CGraphics_Threaded::IsIdle() +{ + return m_Handler.IsIdle(); +} + +void CGraphics_Threaded::WaitForIdle() +{ + m_Handler.WaitForIdle(); +} int CGraphics_Threaded::GetVideoModes(CVideoMode *pModes, int MaxModes) { diff --git a/src/engine/client/graphics_threaded.h b/src/engine/client/graphics_threaded.h index 1ec8c61ca..a4c4ab400 100644 --- a/src/engine/client/graphics_threaded.h +++ b/src/engine/client/graphics_threaded.h @@ -143,12 +143,17 @@ public: SColor m_Color; }; - struct SCommand_Init : public SCommand { SCommand_Init() : SCommand(CMD_INIT) {} volatile int *m_pResult; }; + + struct SCommand_Signal : public SCommand + { + SCommand_Signal() : SCommand(CMD_SIGNAL) {} + semaphore *m_pSemaphore; + }; struct SCommand_RunBuffer : public SCommand { @@ -393,4 +398,8 @@ public: virtual int GetVideoModes(CVideoMode *pModes, int MaxModes); + // syncronization + virtual void InsertSignal(semaphore *pSemaphore); + virtual bool IsIdle(); + virtual void WaitForIdle(); }; \ No newline at end of file diff --git a/src/engine/graphics.h b/src/engine/graphics.h index cbc6a331f..1c79ee15e 100644 --- a/src/engine/graphics.h +++ b/src/engine/graphics.h @@ -5,6 +5,8 @@ #include "kernel.h" +#include + class CImageInfo { public: @@ -132,6 +134,11 @@ public: virtual int GetVideoModes(CVideoMode *pModes, int MaxModes) = 0; virtual void Swap() = 0; + + // syncronization + virtual void InsertSignal(semaphore *pSemaphore) = 0; + virtual bool IsIdle() = 0; + virtual void WaitForIdle() = 0; }; class IEngineGraphics : public IGraphics diff --git a/src/engine/shared/config_variables.h b/src/engine/shared/config_variables.h index c812063a7..352cefd87 100644 --- a/src/engine/shared/config_variables.h +++ b/src/engine/shared/config_variables.h @@ -70,6 +70,7 @@ MACRO_CONFIG_INT(GfxTextureQuality, gfx_texture_quality, 1, 0, 1, CFGFLAG_SAVE|C MACRO_CONFIG_INT(GfxFsaaSamples, gfx_fsaa_samples, 0, 0, 16, CFGFLAG_SAVE|CFGFLAG_CLIENT, "FSAA Samples") MACRO_CONFIG_INT(GfxRefreshRate, gfx_refresh_rate, 0, 0, 0, CFGFLAG_SAVE|CFGFLAG_CLIENT, "Screen refresh rate") MACRO_CONFIG_INT(GfxFinish, gfx_finish, 1, 0, 1, CFGFLAG_SAVE|CFGFLAG_CLIENT, "") +MACRO_CONFIG_INT(GfxAsyncRender, gfx_asyncrender, 1, 0, 1, CFGFLAG_SAVE|CFGFLAG_CLIENT, "Do rendering async from the the update") MACRO_CONFIG_INT(InpMousesens, inp_mousesens, 100, 5, 100000, CFGFLAG_SAVE|CFGFLAG_CLIENT, "Mouse sensitivity") From 4e923442e48bd78617061afe9da0ed5bf5d1fa02 Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Sat, 31 Dec 2011 10:15:54 +0100 Subject: [PATCH 07/18] fixed the broken damage indicators --- src/game/client/components/damageind.cpp | 17 ++++++++++------- src/game/client/components/damageind.h | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/game/client/components/damageind.cpp b/src/game/client/components/damageind.cpp index cba476664..9be6722a6 100644 --- a/src/game/client/components/damageind.cpp +++ b/src/game/client/components/damageind.cpp @@ -38,7 +38,7 @@ void CDamageInd::Create(vec2 Pos, vec2 Dir) if (i) { i->m_Pos = Pos; - i->m_Life = 0.75f; + i->m_StartTime = Client()->LocalTime(); i->m_Dir = Dir*-1; i->m_StartAngle = (( (float)rand()/(float)RAND_MAX) - 1.0f) * 2.0f * pi; } @@ -46,19 +46,19 @@ void CDamageInd::Create(vec2 Pos, vec2 Dir) void CDamageInd::OnRender() { + Graphics()->TextureSet(g_pData->m_aImages[IMAGE_GAME].m_Id); Graphics()->QuadsBegin(); for(int i = 0; i < m_NumItems;) { - vec2 Pos = mix(m_aItems[i].m_Pos+m_aItems[i].m_Dir*75.0f, m_aItems[i].m_Pos, clamp((m_aItems[i].m_Life-0.60f)/0.15f, 0.0f, 1.0f)); - - m_aItems[i].m_Life -= Client()->FrameTime(); - if(m_aItems[i].m_Life < 0.0f) + float Life = 0.75f - (Client()->LocalTime() - m_aItems[i].m_StartTime); + if(Life < 0.0f) DestroyI(&m_aItems[i]); else { - Graphics()->SetColor(1.0f,1.0f,1.0f, m_aItems[i].m_Life/0.1f); - Graphics()->QuadsSetRotation(m_aItems[i].m_StartAngle + m_aItems[i].m_Life * 2.0f); + vec2 Pos = mix(m_aItems[i].m_Pos+m_aItems[i].m_Dir*75.0f, m_aItems[i].m_Pos, clamp((Life-0.60f)/0.15f, 0.0f, 1.0f)); + Graphics()->SetColor(1.0f,1.0f,1.0f, Life/0.1f); + Graphics()->QuadsSetRotation(m_aItems[i].m_StartAngle + Life * 2.0f); RenderTools()->SelectSprite(SPRITE_STAR1); RenderTools()->DrawSprite(Pos.x, Pos.y, 48.0f); i++; @@ -66,3 +66,6 @@ void CDamageInd::OnRender() } Graphics()->QuadsEnd(); } + + + diff --git a/src/game/client/components/damageind.h b/src/game/client/components/damageind.h index 2f89e422c..4a5975a81 100644 --- a/src/game/client/components/damageind.h +++ b/src/game/client/components/damageind.h @@ -12,7 +12,7 @@ class CDamageInd : public CComponent { vec2 m_Pos; vec2 m_Dir; - float m_Life; + float m_StartTime; float m_StartAngle; }; From b31abc40537bff7e159091ff61d5af442296c4d9 Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Sat, 31 Dec 2011 10:29:25 +0100 Subject: [PATCH 08/18] fixed so that you can select graphics backend via gfx_threaded --- src/engine/client/client.cpp | 23 +++++++++++++++-------- src/engine/client/graphics.cpp | 2 +- src/engine/client/graphics_threaded.cpp | 2 +- src/engine/graphics.h | 1 + src/engine/shared/config_variables.h | 2 ++ src/game/client/gameclient.cpp | 12 +++++++----- 6 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 53f60fa8f..b54b90842 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -1676,7 +1676,7 @@ void CClient::InitInterfaces() // fetch interfaces m_pEngine = Kernel()->RequestInterface(); m_pEditor = Kernel()->RequestInterface(); - m_pGraphics = Kernel()->RequestInterface(); + //m_pGraphics = Kernel()->RequestInterface(); m_pSound = Kernel()->RequestInterface(); m_pGameClient = Kernel()->RequestInterface(); m_pInput = Kernel()->RequestInterface(); @@ -1698,10 +1698,21 @@ void CClient::Run() m_SnapshotParts = 0; // init graphics - if(m_pGraphics->Init() != 0) { - dbg_msg("client", "couldn't init graphics"); - return; + if(g_Config.m_GfxThreaded) + m_pGraphics = CreateEngineGraphicsThreaded(); + else + m_pGraphics = CreateEngineGraphics(); + + bool RegisterFail = false; + RegisterFail = RegisterFail || !Kernel()->RegisterInterface(static_cast(m_pGraphics)); // register graphics as both + RegisterFail = RegisterFail || !Kernel()->RegisterInterface(static_cast(m_pGraphics)); + + if(RegisterFail || m_pGraphics->Init() != 0) + { + dbg_msg("client", "couldn't init graphics"); + return; + } } // open socket @@ -2220,7 +2231,6 @@ int main(int argc, const char **argv) // ignore_convention IConsole *pConsole = CreateConsole(CFGFLAG_CLIENT); IStorage *pStorage = CreateStorage("Teeworlds", argc, argv); // ignore_convention IConfig *pConfig = CreateConfig(); - IEngineGraphics *pEngineGraphics = CreateEngineGraphics(); IEngineSound *pEngineSound = CreateEngineSound(); IEngineInput *pEngineInput = CreateEngineInput(); IEngineTextRender *pEngineTextRender = CreateEngineTextRender(); @@ -2234,9 +2244,6 @@ int main(int argc, const char **argv) // ignore_convention RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConsole); RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConfig); - RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast(pEngineGraphics)); // register graphics as both - RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast(pEngineGraphics)); - RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast(pEngineSound)); // register as both RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast(pEngineSound)); diff --git a/src/engine/client/graphics.cpp b/src/engine/client/graphics.cpp index 03a6e7cd2..fda85312e 100644 --- a/src/engine/client/graphics.cpp +++ b/src/engine/client/graphics.cpp @@ -978,4 +978,4 @@ void CGraphics_SDL::WaitForIdle() { } -//extern IEngineGraphics *CreateEngineGraphics() { return new CGraphics_SDL(); } +extern IEngineGraphics *CreateEngineGraphics() { return new CGraphics_SDL(); } diff --git a/src/engine/client/graphics_threaded.cpp b/src/engine/client/graphics_threaded.cpp index e702133a8..2b9147ffc 100644 --- a/src/engine/client/graphics_threaded.cpp +++ b/src/engine/client/graphics_threaded.cpp @@ -1293,4 +1293,4 @@ int CGraphics_Threaded::GetVideoModes(CVideoMode *pModes, int MaxModes) } -extern IEngineGraphics *CreateEngineGraphics() { return new CGraphics_Threaded(); } +extern IEngineGraphics *CreateEngineGraphicsThreaded() { return new CGraphics_Threaded(); } diff --git a/src/engine/graphics.h b/src/engine/graphics.h index 1c79ee15e..be113b2db 100644 --- a/src/engine/graphics.h +++ b/src/engine/graphics.h @@ -157,5 +157,6 @@ public: }; extern IEngineGraphics *CreateEngineGraphics(); +extern IEngineGraphics *CreateEngineGraphicsThreaded(); #endif diff --git a/src/engine/shared/config_variables.h b/src/engine/shared/config_variables.h index 352cefd87..e7b1f7daf 100644 --- a/src/engine/shared/config_variables.h +++ b/src/engine/shared/config_variables.h @@ -72,6 +72,8 @@ MACRO_CONFIG_INT(GfxRefreshRate, gfx_refresh_rate, 0, 0, 0, CFGFLAG_SAVE|CFGFLAG MACRO_CONFIG_INT(GfxFinish, gfx_finish, 1, 0, 1, CFGFLAG_SAVE|CFGFLAG_CLIENT, "") MACRO_CONFIG_INT(GfxAsyncRender, gfx_asyncrender, 1, 0, 1, CFGFLAG_SAVE|CFGFLAG_CLIENT, "Do rendering async from the the update") +MACRO_CONFIG_INT(GfxThreaded, gfx_threaded, 1, 0, 1, CFGFLAG_SAVE|CFGFLAG_CLIENT, "Use the threaded graphics backend") + MACRO_CONFIG_INT(InpMousesens, inp_mousesens, 100, 5, 100000, CFGFLAG_SAVE|CFGFLAG_CLIENT, "Mouse sensitivity") MACRO_CONFIG_STR(SvName, sv_name, 128, "unnamed server", CFGFLAG_SERVER, "Server name") diff --git a/src/game/client/gameclient.cpp b/src/game/client/gameclient.cpp index 2fd1c2f3b..d62a16eb7 100644 --- a/src/game/client/gameclient.cpp +++ b/src/game/client/gameclient.cpp @@ -95,7 +95,6 @@ void CGameClient::OnConsoleInit() { m_pEngine = Kernel()->RequestInterface(); m_pClient = Kernel()->RequestInterface(); - m_pGraphics = Kernel()->RequestInterface(); m_pTextRender = Kernel()->RequestInterface(); m_pSound = Kernel()->RequestInterface(); m_pInput = Kernel()->RequestInterface(); @@ -196,10 +195,6 @@ void CGameClient::OnConsoleInit() Console()->Register("vote", "r", CFGFLAG_SERVER, 0, 0, "Force a vote to yes/no"); - // propagate pointers - m_UI.SetGraphics(Graphics(), TextRender()); - m_RenderTools.m_pGraphics = Graphics(); - m_RenderTools.m_pUI = UI(); for(int i = 0; i < m_All.m_Num; i++) m_All.m_paComponents[i]->m_pClient = this; @@ -223,6 +218,13 @@ void CGameClient::OnConsoleInit() void CGameClient::OnInit() { + m_pGraphics = Kernel()->RequestInterface(); + + // propagate pointers + m_UI.SetGraphics(Graphics(), TextRender()); + m_RenderTools.m_pGraphics = Graphics(); + m_RenderTools.m_pUI = UI(); + int64 Start = time_get(); // set the language From 6e57620c2ca9042732d67134e2986a4ad96d2534 Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Sat, 31 Dec 2011 11:18:55 +0100 Subject: [PATCH 09/18] added flags for mipmap generation on textures. fixes missing texts --- src/engine/client/graphics.cpp | 15 ++++++++++++--- src/engine/client/graphics_threaded.cpp | 22 +++++++++++++++++++--- src/engine/client/graphics_threaded.h | 3 +++ src/engine/client/text.cpp | 2 +- src/engine/graphics.h | 3 ++- 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/engine/client/graphics.cpp b/src/engine/client/graphics.cpp index fda85312e..d16f10fc4 100644 --- a/src/engine/client/graphics.cpp +++ b/src/engine/client/graphics.cpp @@ -349,9 +349,18 @@ int CGraphics_OpenGL::LoadTextureRaw(int Width, int Height, int Format, const vo glGenTextures(1, &m_aTextures[Tex].m_Tex); glBindTexture(GL_TEXTURE_2D, m_aTextures[Tex].m_Tex); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); - gluBuild2DMipmaps(GL_TEXTURE_2D, StoreOglformat, Width, Height, Oglformat, GL_UNSIGNED_BYTE, pTexData); + if(Flags&TEXLOAD_NOMIPMAPS) + { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, StoreOglformat, Width, Height, 0, Oglformat, GL_UNSIGNED_BYTE, pData); + } + else + { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); + gluBuild2DMipmaps(GL_TEXTURE_2D, StoreOglformat, Width, Height, Oglformat, GL_UNSIGNED_BYTE, pTexData); + } // calculate memory usage { diff --git a/src/engine/client/graphics_threaded.cpp b/src/engine/client/graphics_threaded.cpp index 2b9147ffc..21e9ef2f0 100644 --- a/src/engine/client/graphics_threaded.cpp +++ b/src/engine/client/graphics_threaded.cpp @@ -163,9 +163,20 @@ public: glGenTextures(1, &m_aTextures[pCommand->m_Slot]); glBindTexture(GL_TEXTURE_2D, m_aTextures[pCommand->m_Slot]); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); - gluBuild2DMipmaps(GL_TEXTURE_2D, StoreOglformat, pCommand->m_Width, pCommand->m_Height, Oglformat, GL_UNSIGNED_BYTE, pCommand->m_pData); + + if(pCommand->m_Flags&CCommandBuffer::TEXFLAG_NOMIPMAPS) + { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, StoreOglformat, pCommand->m_Width, pCommand->m_Height, 0, Oglformat, GL_UNSIGNED_BYTE, pCommand->m_pData); + } + else + { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); + gluBuild2DMipmaps(GL_TEXTURE_2D, StoreOglformat, pCommand->m_Width, pCommand->m_Height, Oglformat, GL_UNSIGNED_BYTE, pCommand->m_pData); + } + mem_free(pCommand->m_pData); } @@ -760,6 +771,11 @@ int CGraphics_Threaded::LoadTextureRaw(int Width, int Height, int Format, const Cmd.m_Format = ImageFormatToTexFormat(Format); Cmd.m_StoreFormat = ImageFormatToTexFormat(StoreFormat); + // flags + Cmd.m_Flags = 0; + if(Flags&IGraphics::TEXLOAD_NOMIPMAPS) + Cmd.m_Flags |= CCommandBuffer::TEXFLAG_NOMIPMAPS; + // calculate memory usage int PixelSize = 4; if(Format == CImageInfo::FORMAT_RGB) diff --git a/src/engine/client/graphics_threaded.h b/src/engine/client/graphics_threaded.h index a4c4ab400..41565ca20 100644 --- a/src/engine/client/graphics_threaded.h +++ b/src/engine/client/graphics_threaded.h @@ -86,6 +86,8 @@ public: TEXFORMAT_RGB, TEXFORMAT_RGBA, TEXFORMAT_ALPHA, + + TEXFLAG_NOMIPMAPS = 1, }; enum @@ -186,6 +188,7 @@ public: int m_Height; int m_Format; int m_StoreFormat; + int m_Flags; void *m_pData; // will be freed by the command processor }; diff --git a/src/engine/client/text.cpp b/src/engine/client/text.cpp index 51eed5471..f00247d2b 100644 --- a/src/engine/client/text.cpp +++ b/src/engine/client/text.cpp @@ -157,7 +157,7 @@ class CTextRender : public IEngineTextRender pSizeData->m_aTextures[i] = 0; } - pSizeData->m_aTextures[i] = Graphics()->LoadTextureRaw(Width, Height, CImageInfo::FORMAT_ALPHA, pMem, CImageInfo::FORMAT_ALPHA, 0); + pSizeData->m_aTextures[i] = Graphics()->LoadTextureRaw(Width, Height, CImageInfo::FORMAT_ALPHA, pMem, CImageInfo::FORMAT_ALPHA, IGraphics::TEXLOAD_NOMIPMAPS); FontMemoryUsage += Width*Height; } diff --git a/src/engine/graphics.h b/src/engine/graphics.h index be113b2db..94d9c1a28 100644 --- a/src/engine/graphics.h +++ b/src/engine/graphics.h @@ -57,7 +57,8 @@ public: */ enum { - TEXLOAD_NORESAMPLE=1, + TEXLOAD_NORESAMPLE = 1, + TEXLOAD_NOMIPMAPS = 2, }; int ScreenWidth() const { return m_ScreenWidth; } From 8b76105cfa70e74b76e38ed2a867e806bba8e0ff Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Sat, 31 Dec 2011 14:00:49 +0100 Subject: [PATCH 10/18] cleaned up some of the code --- src/engine/client/graphics_threaded.cpp | 350 ++++++++++++++++-------- src/engine/client/graphics_threaded.h | 25 +- 2 files changed, 262 insertions(+), 113 deletions(-) diff --git a/src/engine/client/graphics_threaded.cpp b/src/engine/client/graphics_threaded.cpp index 21e9ef2f0..febb3b287 100644 --- a/src/engine/client/graphics_threaded.cpp +++ b/src/engine/client/graphics_threaded.cpp @@ -48,34 +48,27 @@ static CVideoMode g_aFakeModes[] = { class CCommandProcessorFragment_General { public: + void Cmd_Signal(const CCommandBuffer::SCommand_Signal *pCommand) + { + pCommand->m_pSemaphore->signal(); + } + bool RunCommand(const CCommandBuffer::SCommand * pBaseCommand) { - switch(pBaseCommand->m_Cmd) { - case CCommandBuffer::CMD_SIGNAL: - { - const CCommandBuffer::SCommand_Signal *pCommand = static_cast(pBaseCommand); - pCommand->m_pSemaphore->signal(); - } break; - default: - return false; - break; + case CCommandBuffer::CMD_NOP: break; + case CCommandBuffer::CMD_SIGNAL: Cmd_Signal(static_cast(pBaseCommand)); break; + default: return false; } return true; } }; - class CCommandProcessorFragment_OpenGL { GLuint m_aTextures[CCommandBuffer::MAX_TEXTURES]; -public: - CCommandProcessorFragment_OpenGL() - { - mem_zero(m_aTextures, sizeof(m_aTextures)); - } void SetState(const CCommandBuffer::SState &State) { @@ -121,7 +114,7 @@ public: glOrtho(State.m_ScreenTL.x, State.m_ScreenBR.x, State.m_ScreenBR.y, State.m_ScreenTL.y, 1.0f, 10.f); } - int TexFormatToOpenGLFormat(int TexFormat) + static int TexFormatToOpenGLFormat(int TexFormat) { if(TexFormat == CCommandBuffer::TEXFORMAT_RGB) return GL_RGB; if(TexFormat == CCommandBuffer::TEXFORMAT_ALPHA) return GL_ALPHA; @@ -180,56 +173,52 @@ public: mem_free(pCommand->m_pData); } + void Cmd_Clear(const CCommandBuffer::SCommand_Clear *pCommand) + { + glClearColor(pCommand->m_Color.r, pCommand->m_Color.g, pCommand->m_Color.b, 0.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + } + + void Cmd_Render(const CCommandBuffer::SCommand_Render *pCommand) + { + SetState(pCommand->m_State); + + glVertexPointer(3, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices); + glTexCoordPointer(2, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices + sizeof(float)*3); + glColorPointer(4, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices + sizeof(float)*5); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); + + switch(pCommand->m_PrimType) + { + case CCommandBuffer::PRIMTYPE_QUADS: + glDrawArrays(GL_QUADS, 0, pCommand->m_PrimCount*4); + break; + case CCommandBuffer::PRIMTYPE_LINES: + glDrawArrays(GL_LINES, 0, pCommand->m_PrimCount*2); + break; + default: + dbg_msg("render", "unknown primtype %d\n", pCommand->m_Cmd); + }; + } + +public: + CCommandProcessorFragment_OpenGL() + { + mem_zero(m_aTextures, sizeof(m_aTextures)); + } + bool RunCommand(const CCommandBuffer::SCommand * pBaseCommand) { - switch(pBaseCommand->m_Cmd) { - case CCommandBuffer::CMD_TEXTURE_CREATE: - { - Cmd_Texture_Create(static_cast(pBaseCommand)); - } break; - case CCommandBuffer::CMD_TEXTURE_DESTROY: - { - Cmd_Texture_Destroy(static_cast(pBaseCommand)); - } break; - case CCommandBuffer::CMD_TEXTURE_UPDATE: - { - Cmd_Texture_Update(static_cast(pBaseCommand)); - } break; - case CCommandBuffer::CMD_CLEAR: - { - const CCommandBuffer::SCommand_Clear *pCommand = (CCommandBuffer::SCommand_Clear *)pBaseCommand; - glClearColor(pCommand->m_Color.r, pCommand->m_Color.g, pCommand->m_Color.b, 0.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - } break; - case CCommandBuffer::CMD_RENDER: - { - const CCommandBuffer::SCommand_Render *pCommand = static_cast(pBaseCommand); - SetState(pCommand->m_State); - - glVertexPointer(3, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices); - glTexCoordPointer(2, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices + sizeof(float)*3); - glColorPointer(4, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices + sizeof(float)*5); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glEnableClientState(GL_COLOR_ARRAY); - - switch(pCommand->m_PrimType) - { - case CCommandBuffer::PRIMTYPE_QUADS: - glDrawArrays(GL_QUADS, 0, pCommand->m_PrimCount*4); - break; - case CCommandBuffer::PRIMTYPE_LINES: - glDrawArrays(GL_LINES, 0, pCommand->m_PrimCount*2); - break; - default: - dbg_msg("render", "unknown primtype %d\n", pCommand->m_Cmd); - }; - } break; - default: - return false; - break; + case CCommandBuffer::CMD_TEXTURE_CREATE: Cmd_Texture_Create(static_cast(pBaseCommand)); break; + case CCommandBuffer::CMD_TEXTURE_DESTROY: Cmd_Texture_Destroy(static_cast(pBaseCommand)); break; + case CCommandBuffer::CMD_TEXTURE_UPDATE: Cmd_Texture_Update(static_cast(pBaseCommand)); break; + case CCommandBuffer::CMD_CLEAR: Cmd_Clear(static_cast(pBaseCommand)); break; + case CCommandBuffer::CMD_RENDER: Cmd_Render(static_cast(pBaseCommand)); break; + default: return false; } return true; @@ -240,7 +229,8 @@ class CCommandProcessorFragment_SDL { // SDL stuff SDL_Surface *m_pScreenSurface; - + bool m_SystemInited; + /* int TryInit() { const SDL_VideoInfo *pInfo = SDL_GetVideoInfo(); @@ -345,12 +335,6 @@ class CCommandProcessorFragment_SDL } } - atexit(SDL_Quit); // ignore_convention - - #ifdef CONF_FAMILY_WINDOWS - if(!getenv("SDL_VIDEO_WINDOW_POS") && !getenv("SDL_VIDEO_CENTERED")) // ignore_convention - putenv("SDL_VIDEO_WINDOW_POS=8,27"); // ignore_convention - #endif if(InitWindow() != 0) return -1; @@ -369,36 +353,128 @@ class CCommandProcessorFragment_SDL glDepthMask(0); return 0; + }*/ + + void Cmd_Init(const CCommandBuffer::SCommand_Init *pCommand) + { + if(!m_SystemInited) + { + int Systems = SDL_INIT_VIDEO; + + if(g_Config.m_SndEnable) // TODO: remove + Systems |= SDL_INIT_AUDIO; + + if(g_Config.m_ClEventthread) // TODO: remove + Systems |= SDL_INIT_EVENTTHREAD; + + if(SDL_Init(Systems) < 0) + { + dbg_msg("gfx", "unable to init SDL: %s", SDL_GetError()); + *pCommand->m_pResult = -1; + return; + } + + atexit(SDL_Quit); // ignore_convention + + #ifdef CONF_FAMILY_WINDOWS + if(!getenv("SDL_VIDEO_WINDOW_POS") && !getenv("SDL_VIDEO_CENTERED")) // ignore_convention + putenv("SDL_VIDEO_WINDOW_POS=8,27"); // ignore_convention + #endif + + m_SystemInited = true; + } + + const SDL_VideoInfo *pInfo = SDL_GetVideoInfo(); + SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE); + + // set flags + int Flags = SDL_OPENGL; + if(pCommand->m_Flags&CCommandBuffer::INITFLAG_RESIZABLE) + Flags |= SDL_RESIZABLE; + + if(pInfo->hw_available) // ignore_convention + Flags |= SDL_HWSURFACE; + else + Flags |= SDL_SWSURFACE; + + if(pInfo->blit_hw) // ignore_convention + Flags |= SDL_HWACCEL; + + if(pCommand->m_Flags&CCommandBuffer::INITFLAG_FULLSCREEN) + Flags |= SDL_FULLSCREEN; + + // set gl attributes + if(pCommand->m_FsaaSamples) + { + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, pCommand->m_FsaaSamples); + } + else + { + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0); + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0); + } + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, pCommand->m_Flags&CCommandBuffer::INITFLAG_VSYNC ? 1 : 0); + + // set caption + SDL_WM_SetCaption(pCommand->m_aName, pCommand->m_aName); + + // create window + m_pScreenSurface = SDL_SetVideoMode(pCommand->m_ScreenWidth, pCommand->m_ScreenHeight, 0, Flags); + if(m_pScreenSurface) + { + SDL_ShowCursor(0); + + // set some default settings + glEnable(GL_BLEND); + glDisable(GL_CULL_FACE); + glDisable(GL_DEPTH_TEST); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glAlphaFunc(GL_GREATER, 0); + glEnable(GL_ALPHA_TEST); + glDepthMask(0); + + *pCommand->m_pResult = 0; + } + else + { + dbg_msg("gfx", "unable to set video mode: %s", SDL_GetError()); + *pCommand->m_pResult = -1; + } + + + //*pCommand->m_pResult = Init(); } + void Cmd_Shutdown(const CCommandBuffer::SCommand_Shutdown *pCommand) + { + + } + + void Cmd_Swap(const CCommandBuffer::SCommand_Swap *pCommand) + { + SDL_GL_SwapBuffers(); + } public: CCommandProcessorFragment_SDL() { + m_SystemInited = false; m_pScreenSurface = 0x0; } bool RunCommand(const CCommandBuffer::SCommand *pBaseCommand) { - switch(pBaseCommand->m_Cmd) { - case CCommandBuffer::CMD_NOP: - break; - case CCommandBuffer::CMD_INIT: - { - const CCommandBuffer::SCommand_Init *pCommand = static_cast(pBaseCommand); - *pCommand->m_pResult = Init(); - } break; - case CCommandBuffer::CMD_SHUTDOWN: - break; - case CCommandBuffer::CMD_SWAP: - { - SDL_GL_SwapBuffers(); - } break; - default: - return false; - break; + case CCommandBuffer::CMD_INIT: Cmd_Init(static_cast(pBaseCommand)); break; + case CCommandBuffer::CMD_SHUTDOWN: Cmd_Shutdown(static_cast(pBaseCommand)); break; + case CCommandBuffer::CMD_SWAP: Cmd_Swap(static_cast(pBaseCommand)); break; + default: return false; } return true; @@ -1128,6 +1204,66 @@ void CGraphics_Threaded::QuadsText(float x, float y, float Size, float r, float QuadsEnd(); } +int CGraphics_Threaded::IssueInit() +{ + // issue init command + m_pCommandBuffer->Reset(); + + volatile int Result; + CCommandBuffer::SCommand_Init Cmd; + str_copy(Cmd.m_aName, "Teeworlds", sizeof(Cmd.m_aName)); + Cmd.m_pResult = &Result; + Cmd.m_ScreenWidth = g_Config.m_GfxScreenWidth; + Cmd.m_ScreenHeight = g_Config.m_GfxScreenHeight; + Cmd.m_FsaaSamples = g_Config.m_GfxFsaaSamples; + + Cmd.m_Flags = 0; + if(g_Config.m_GfxFullscreen) Cmd.m_Flags |= CCommandBuffer::INITFLAG_FULLSCREEN; + if(g_Config.m_GfxVsync) Cmd.m_Flags |= CCommandBuffer::INITFLAG_VSYNC; + if(g_Config.m_DbgResizable) Cmd.m_Flags |= CCommandBuffer::INITFLAG_RESIZABLE; + + m_pCommandBuffer->AddCommand(Cmd); + + m_Handler.RunBuffer(m_pCommandBuffer); + m_Handler.WaitForIdle(); + return Result; +} + +int CGraphics_Threaded::InitWindow() +{ + if(IssueInit() == 0) + return 0; + + // try disabling fsaa + while(g_Config.m_GfxFsaaSamples) + { + g_Config.m_GfxFsaaSamples--; + + if(g_Config.m_GfxFsaaSamples) + dbg_msg("gfx", "lowering FSAA to %d and trying again", g_Config.m_GfxFsaaSamples); + else + dbg_msg("gfx", "disabling FSAA and trying again"); + + if(IssueInit() == 0) + return 0; + } + + // try lowering the resolution + if(g_Config.m_GfxScreenWidth != 640 || g_Config.m_GfxScreenHeight != 480) + { + dbg_msg("gfx", "setting resolution to 640x480 and trying again"); + g_Config.m_GfxScreenWidth = 640; + g_Config.m_GfxScreenHeight = 480; + + if(IssueInit() == 0) + return 0; + } + + dbg_msg("gfx", "out of ideas. failed to init graphics"); + + return -1; +} + bool CGraphics_Threaded::Init() { // fetch pointers @@ -1153,34 +1289,24 @@ bool CGraphics_Threaded::Init() m_apCommandBuffers[1] = new CCommandBuffer(1024*512, 1024*1024); m_pCommandBuffer = m_apCommandBuffers[0]; - // issue init command - m_pCommandBuffer->Reset(); - volatile int Result; - CCommandBuffer::SCommand_Init Cmd; - Cmd.m_pResult = &Result; - m_pCommandBuffer->AddCommand(Cmd); - m_Handler.RunBuffer(m_pCommandBuffer); - m_Handler.WaitForIdle(); + // try to init the window + if(InitWindow() != 0) + return 0; + + // fetch final resolusion + m_ScreenWidth = g_Config.m_GfxScreenWidth; + m_ScreenHeight = g_Config.m_GfxScreenHeight; + // create null texture, will get id=0 + static const unsigned char aNullTextureData[] = { + 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0x00,0xff,0x00,0xff, 0x00,0xff,0x00,0xff, + 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0x00,0xff,0x00,0xff, 0x00,0xff,0x00,0xff, + 0x00,0x00,0xff,0xff, 0x00,0x00,0xff,0xff, 0xff,0xff,0x00,0xff, 0xff,0xff,0x00,0xff, + 0x00,0x00,0xff,0xff, 0x00,0x00,0xff,0xff, 0xff,0xff,0x00,0xff, 0xff,0xff,0x00,0xff, + }; - if(Result == 0) - { - // fetch final resolusion - m_ScreenWidth = g_Config.m_GfxScreenWidth; - m_ScreenHeight = g_Config.m_GfxScreenHeight; - - // create null texture, will get id=0 - static const unsigned char aNullTextureData[] = { - 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0x00,0xff,0x00,0xff, 0x00,0xff,0x00,0xff, - 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0x00,0xff,0x00,0xff, 0x00,0xff,0x00,0xff, - 0x00,0x00,0xff,0xff, 0x00,0x00,0xff,0xff, 0xff,0xff,0x00,0xff, 0xff,0xff,0x00,0xff, - 0x00,0x00,0xff,0xff, 0x00,0x00,0xff,0xff, 0xff,0xff,0x00,0xff, 0xff,0xff,0x00,0xff, - }; - - m_InvalidTexture = LoadTextureRaw(4,4,CImageInfo::FORMAT_RGBA,aNullTextureData,CImageInfo::FORMAT_RGBA,TEXLOAD_NORESAMPLE); - } - - return Result; + m_InvalidTexture = LoadTextureRaw(4,4,CImageInfo::FORMAT_RGBA,aNullTextureData,CImageInfo::FORMAT_RGBA,TEXLOAD_NORESAMPLE); + return 0; } void CGraphics_Threaded::Shutdown() diff --git a/src/engine/client/graphics_threaded.h b/src/engine/client/graphics_threaded.h index 41565ca20..54455d6df 100644 --- a/src/engine/client/graphics_threaded.h +++ b/src/engine/client/graphics_threaded.h @@ -90,6 +90,13 @@ public: TEXFLAG_NOMIPMAPS = 1, }; + enum + { + INITFLAG_FULLSCREEN = 1, + INITFLAG_VSYNC = 2, + INITFLAG_RESIZABLE = 4, + }; + enum { // @@ -148,9 +155,22 @@ public: struct SCommand_Init : public SCommand { SCommand_Init() : SCommand(CMD_INIT) {} + + char m_aName[256]; + + int m_ScreenWidth; + int m_ScreenHeight; + int m_FsaaSamples; + int m_Flags; + volatile int *m_pResult; }; - + + struct SCommand_Shutdown : public SCommand + { + SCommand_Shutdown() : SCommand(CMD_SHUTDOWN) {} + }; + struct SCommand_Signal : public SCommand { SCommand_Signal() : SCommand(CMD_SIGNAL) {} @@ -337,6 +357,9 @@ class CGraphics_Threaded : public IEngineGraphics static unsigned char Sample(int w, int h, const unsigned char *pData, int u, int v, int Offset, int ScaleW, int ScaleH, int Bpp); static unsigned char *Rescale(int Width, int Height, int NewWidth, int NewHeight, int Format, const unsigned char *pData); + + int IssueInit(); + int InitWindow(); public: CGraphics_Threaded(); From c1942ca6cbafb605e3ebc8e8cfabf982144f388f Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Sun, 1 Jan 2012 13:38:46 +0100 Subject: [PATCH 11/18] fixed the render frame time and cleaned up some more code --- src/engine/client.h | 4 +- src/engine/client/client.cpp | 60 ++++++----- src/engine/client/client.h | 9 +- src/engine/client/graphics_threaded.cpp | 132 +----------------------- src/engine/client/graphics_threaded.h | 10 ++ src/game/client/components/hud.cpp | 2 +- src/game/client/components/items.cpp | 3 - 7 files changed, 54 insertions(+), 166 deletions(-) diff --git a/src/engine/client.h b/src/engine/client.h index 966e8f61a..e9076b76c 100644 --- a/src/engine/client.h +++ b/src/engine/client.h @@ -23,7 +23,7 @@ protected: float m_PredIntraTick; float m_LocalTime; - float m_FrameTime; + float m_RenderFrameTime; int m_GameTickSpeed; public: @@ -68,7 +68,7 @@ public: inline int GameTickSpeed() const { return m_GameTickSpeed; } // other time access - inline float FrameTime() const { return m_FrameTime; } + inline float RenderFrameTime() const { return m_RenderFrameTime; } inline float LocalTime() const { return m_LocalTime; } // actions diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index b54b90842..b7244db4c 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -244,10 +244,11 @@ CClient::CClient() : m_DemoPlayer(&m_SnapshotDelta), m_DemoRecorder(&m_SnapshotD m_pMap = 0; m_pConsole = 0; - m_FrameTime = 0.0001f; - m_FrameTimeLow = 1.0f; - m_FrameTimeHigh = 0.0f; - m_Frames = 0; + m_RenderFrameTime = 0.0001f; + m_RenderFrameTimeLow = 1.0f; + m_RenderFrameTimeHigh = 0.0f; + m_RenderFrames = 0; + m_LastRenderTime = time_get(); m_GameTickSpeed = SERVER_TICK_SPEED; @@ -681,13 +682,13 @@ void CClient::DebugRender() udp = 8 total = 42 */ - FrameTimeAvg = FrameTimeAvg*0.9f + m_FrameTime*0.1f; + FrameTimeAvg = FrameTimeAvg*0.9f + m_RenderFrameTime*0.1f; str_format(aBuffer, sizeof(aBuffer), "ticks: %8d %8d mem %dk %d gfxmem: %dk fps: %3d", m_CurGameTick, m_PredTick, mem_stats()->allocated/1024, mem_stats()->total_allocations, Graphics()->MemoryUsage()/1024, - (int)(1.0f/FrameTimeAvg)); + (int)(1.0f/FrameTimeAvg + 0.5f)); Graphics()->QuadsText(2, 2, 16, 1,1,1,1, aBuffer); @@ -1691,9 +1692,6 @@ void CClient::InitInterfaces() void CClient::Run() { - int64 ReportTime = time_get(); - int64 ReportInterval = time_freq()*1; - m_LocalStartTime = time_get(); m_SnapshotParts = 0; @@ -1772,9 +1770,6 @@ void CClient::Run() while (1) { - int64 FrameStartTime = time_get(); - m_Frames++; - // VersionUpdate(); @@ -1866,9 +1861,22 @@ void CClient::Run() if(!g_Config.m_GfxAsyncRender || m_pGraphics->IsIdle()) { + m_RenderFrames++; + + // update frametime + int64 Now = time_get(); + m_RenderFrameTime = (Now - m_LastRenderTime) / (float)time_freq(); + if(m_RenderFrameTime < m_RenderFrameTimeLow) + m_RenderFrameTimeLow = m_RenderFrameTime; + if(m_RenderFrameTime > m_RenderFrameTimeHigh) + m_RenderFrameTimeHigh = m_RenderFrameTime; + m_FpsGraph.Add(1.0f/m_RenderFrameTime, 1,1,1); + + m_LastRenderTime = Now; + if(g_Config.m_DbgStress) { - if((m_Frames%10) == 0) + if((m_RenderFrames%10) == 0) { Render(); m_pGraphics->Swap(); @@ -1879,6 +1887,9 @@ void CClient::Run() Render(); m_pGraphics->Swap(); } + + + } } @@ -1900,32 +1911,25 @@ void CClient::Run() g_Config.m_DbgHitch = 0; } + /* if(ReportTime < time_get()) { if(0 && g_Config.m_Debug) { dbg_msg("client/report", "fps=%.02f (%.02f %.02f) netstate=%d", m_Frames/(float)(ReportInterval/time_freq()), - 1.0f/m_FrameTimeHigh, - 1.0f/m_FrameTimeLow, + 1.0f/m_RenderFrameTimeHigh, + 1.0f/m_RenderFrameTimeLow, m_NetClient.State()); } - m_FrameTimeLow = 1; - m_FrameTimeHigh = 0; - m_Frames = 0; + m_RenderFrameTimeLow = 1; + m_RenderFrameTimeHigh = 0; + m_RenderFrames = 0; ReportTime += ReportInterval; - } - - // update frametime - m_FrameTime = (time_get()-FrameStartTime)/(float)time_freq(); - if(m_FrameTime < m_FrameTimeLow) - m_FrameTimeLow = m_FrameTime; - if(m_FrameTime > m_FrameTimeHigh) - m_FrameTimeHigh = m_FrameTime; + }*/ + // update local time m_LocalTime = (time_get()-m_LocalStartTime)/(float)time_freq(); - - m_FpsGraph.Add(1.0f/m_FrameTime, 1,1,1); } GameClient()->OnShutdown(); diff --git a/src/engine/client/client.h b/src/engine/client/client.h index 83553eb41..1849830c1 100644 --- a/src/engine/client/client.h +++ b/src/engine/client/client.h @@ -84,9 +84,12 @@ class CClient : public IClient, public CDemoPlayer::IListner int64 m_LocalStartTime; int m_DebugFont; - float m_FrameTimeLow; - float m_FrameTimeHigh; - int m_Frames; + + int64 m_LastRenderTime; + float m_RenderFrameTimeLow; + float m_RenderFrameTimeHigh; + int m_RenderFrames; + NETADDR m_ServerAddress; int m_WindowMustRefocus; int m_SnapCrcErrors; diff --git a/src/engine/client/graphics_threaded.cpp b/src/engine/client/graphics_threaded.cpp index febb3b287..694ecc714 100644 --- a/src/engine/client/graphics_threaded.cpp +++ b/src/engine/client/graphics_threaded.cpp @@ -230,130 +230,6 @@ class CCommandProcessorFragment_SDL // SDL stuff SDL_Surface *m_pScreenSurface; bool m_SystemInited; - /* - int TryInit() - { - const SDL_VideoInfo *pInfo = SDL_GetVideoInfo(); - SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE); - - // set flags - int Flags = SDL_OPENGL; - if(g_Config.m_DbgResizable) - Flags |= SDL_RESIZABLE; - - if(pInfo->hw_available) // ignore_convention - Flags |= SDL_HWSURFACE; - else - Flags |= SDL_SWSURFACE; - - if(pInfo->blit_hw) // ignore_convention - Flags |= SDL_HWACCEL; - - if(g_Config.m_GfxFullscreen) - Flags |= SDL_FULLSCREEN; - - // set gl attributes - if(g_Config.m_GfxFsaaSamples) - { - SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); - SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, g_Config.m_GfxFsaaSamples); - } - else - { - SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0); - SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0); - } - - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, g_Config.m_GfxVsync); - - // set caption - SDL_WM_SetCaption("Teeworlds", "Teeworlds"); - - // create window - m_pScreenSurface = SDL_SetVideoMode(g_Config.m_GfxScreenWidth, g_Config.m_GfxScreenHeight, 0, Flags); - if(m_pScreenSurface == NULL) - { - dbg_msg("gfx", "unable to set video mode: %s", SDL_GetError()); - return -1; - } - - return 0; - } - - - int InitWindow() - { - if(TryInit() == 0) - return 0; - - // try disabling fsaa - while(g_Config.m_GfxFsaaSamples) - { - g_Config.m_GfxFsaaSamples--; - - if(g_Config.m_GfxFsaaSamples) - dbg_msg("gfx", "lowering FSAA to %d and trying again", g_Config.m_GfxFsaaSamples); - else - dbg_msg("gfx", "disabling FSAA and trying again"); - - if(TryInit() == 0) - return 0; - } - - // try lowering the resolution - if(g_Config.m_GfxScreenWidth != 640 || g_Config.m_GfxScreenHeight != 480) - { - dbg_msg("gfx", "setting resolution to 640x480 and trying again"); - g_Config.m_GfxScreenWidth = 640; - g_Config.m_GfxScreenHeight = 480; - - if(TryInit() == 0) - return 0; - } - - dbg_msg("gfx", "out of ideas. failed to init graphics"); - - return -1; - } - - int Init() - { - { - int Systems = SDL_INIT_VIDEO; - - if(g_Config.m_SndEnable) - Systems |= SDL_INIT_AUDIO; - - if(g_Config.m_ClEventthread) - Systems |= SDL_INIT_EVENTTHREAD; - - if(SDL_Init(Systems) < 0) - { - dbg_msg("gfx", "unable to init SDL: %s", SDL_GetError()); - return -1; - } - } - - - if(InitWindow() != 0) - return -1; - - SDL_ShowCursor(0); - - // set some default settings - glEnable(GL_BLEND); - glDisable(GL_CULL_FACE); - glDisable(GL_DEPTH_TEST); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - glAlphaFunc(GL_GREATER, 0); - glEnable(GL_ALPHA_TEST); - glDepthMask(0); - - return 0; - }*/ void Cmd_Init(const CCommandBuffer::SCommand_Init *pCommand) { @@ -445,14 +321,11 @@ class CCommandProcessorFragment_SDL dbg_msg("gfx", "unable to set video mode: %s", SDL_GetError()); *pCommand->m_pResult = -1; } - - - //*pCommand->m_pResult = Init(); } void Cmd_Shutdown(const CCommandBuffer::SCommand_Shutdown *pCommand) { - + SDL_Quit(); } void Cmd_Swap(const CCommandBuffer::SCommand_Swap *pCommand) @@ -1312,7 +1185,8 @@ bool CGraphics_Threaded::Init() void CGraphics_Threaded::Shutdown() { // TODO: SDL, is this correct? - SDL_Quit(); + + // } void CGraphics_Threaded::Minimize() diff --git a/src/engine/client/graphics_threaded.h b/src/engine/client/graphics_threaded.h index 54455d6df..1ea84ea90 100644 --- a/src/engine/client/graphics_threaded.h +++ b/src/engine/client/graphics_threaded.h @@ -78,6 +78,9 @@ public: // swap CMD_SWAP, + + // + CMD_SCREENSHOT, }; enum @@ -192,6 +195,13 @@ public: SVertex *m_pVertices; }; + struct SCommand_Screenshot : public SCommand + { + SCommand_Screenshot() : SCommand(CMD_SCREENSHOT) {} + + CImageInfo *m_pImage; // processor will fill this out + }; + struct SCommand_Swap : public SCommand { SCommand_Swap() : SCommand(CMD_SWAP) {} diff --git a/src/game/client/components/hud.cpp b/src/game/client/components/hud.cpp index 17365116a..df3afdeac 100644 --- a/src/game/client/components/hud.cpp +++ b/src/game/client/components/hud.cpp @@ -248,7 +248,7 @@ void CHud::RenderFps() if(g_Config.m_ClShowfps) { // calculate avg. fps - float FPS = 1.0f / Client()->FrameTime(); + float FPS = 1.0f / Client()->RenderFrameTime(); m_AverageFPS = (m_AverageFPS*(1.0f-(1.0f/m_AverageFPS))) + (FPS*(1.0f/m_AverageFPS)); char Buf[512]; str_format(Buf, sizeof(Buf), "%d", (int)m_AverageFPS); diff --git a/src/game/client/components/items.cpp b/src/game/client/components/items.cpp index 1c3389944..9f787a4b2 100644 --- a/src/game/client/components/items.cpp +++ b/src/game/client/components/items.cpp @@ -22,7 +22,6 @@ void CItems::OnReset() void CItems::RenderProjectile(const CNetObj_Projectile *pCurrent, int ItemID) { - // get positions float Curvature = 0; float Speed = 0; @@ -64,7 +63,6 @@ void CItems::RenderProjectile(const CNetObj_Projectile *pCurrent, int ItemID) if(pCurrent->m_Type == WEAPON_GRENADE) { m_pClient->m_pEffects->SmokeTrail(Pos, Vel*-1); - m_pClient->m_pFlow->Add(Pos, Vel*1000*Client()->FrameTime(), 10.0f); if(Client()->State() == IClient::STATE_DEMOPLAYBACK) { @@ -85,7 +83,6 @@ void CItems::RenderProjectile(const CNetObj_Projectile *pCurrent, int ItemID) else { m_pClient->m_pEffects->BulletTrail(Pos); - m_pClient->m_pFlow->Add(Pos, Vel*1000*Client()->FrameTime(), 10.0f); if(length(Vel) > 0.00001f) Graphics()->QuadsSetRotation(GetAngle(Vel)); From d7fe3ddaab7662cd53db7a1e272ad64c2ba861ae Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Sun, 1 Jan 2012 14:15:35 +0100 Subject: [PATCH 12/18] fixed shutdown and screenshot functionallity --- src/engine/client/graphics_threaded.cpp | 137 ++++++++++++++++-------- src/engine/client/graphics_threaded.h | 12 ++- 2 files changed, 98 insertions(+), 51 deletions(-) diff --git a/src/engine/client/graphics_threaded.cpp b/src/engine/client/graphics_threaded.cpp index 694ecc714..a87e4d623 100644 --- a/src/engine/client/graphics_threaded.cpp +++ b/src/engine/client/graphics_threaded.cpp @@ -203,6 +203,43 @@ class CCommandProcessorFragment_OpenGL }; } + void Cmd_Screenshot(const CCommandBuffer::SCommand_Screenshot *pCommand) + { + // fetch image data + GLint aViewport[4] = {0,0,0,0}; + glGetIntegerv(GL_VIEWPORT, aViewport); + + int w = aViewport[2]; + int h = aViewport[3]; + + dbg_msg("graphics", "grabbing %d x %d", w, h); + + // we allocate one more row to use when we are flipping the texture + unsigned char *pPixelData = (unsigned char *)mem_alloc(w*(h+1)*3, 1); + unsigned char *pTempRow = pPixelData+w*h*3; + + // fetch the pixels + GLint Alignment; + glGetIntegerv(GL_PACK_ALIGNMENT, &Alignment); + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glReadPixels(0,0, w, h, GL_RGB, GL_UNSIGNED_BYTE, pPixelData); + glPixelStorei(GL_PACK_ALIGNMENT, Alignment); + + // flip the pixel because opengl works from bottom left corner + for(int y = 0; y < h/2; y++) + { + mem_copy(pTempRow, pPixelData+y*w*3, w*3); + mem_copy(pPixelData+y*w*3, pPixelData+(h-y-1)*w*3, w*3); + mem_copy(pPixelData+(h-y-1)*w*3, pTempRow,w*3); + } + + // fill in the information + pCommand->m_pImage->m_Width = w; + pCommand->m_pImage->m_Height = h; + pCommand->m_pImage->m_Format = CImageInfo::FORMAT_RGB; + pCommand->m_pImage->m_pData = pPixelData; + } + public: CCommandProcessorFragment_OpenGL() { @@ -218,6 +255,7 @@ public: case CCommandBuffer::CMD_TEXTURE_UPDATE: Cmd_Texture_Update(static_cast(pBaseCommand)); break; case CCommandBuffer::CMD_CLEAR: Cmd_Clear(static_cast(pBaseCommand)); break; case CCommandBuffer::CMD_RENDER: Cmd_Render(static_cast(pBaseCommand)); break; + case CCommandBuffer::CMD_SCREENSHOT: Cmd_Screenshot(static_cast(pBaseCommand)); break; default: return false; } @@ -415,6 +453,14 @@ void CCommandProcessorHandler::Start(ICommandProcessor *pProcessor) m_BufferDone.signal(); } +void CCommandProcessorHandler::Stop() +{ + m_Shutdown = true; + m_Activity.signal(); + thread_wait(m_pThread); + thread_destroy(m_pThread); +} + void CCommandProcessorHandler::RunBuffer(CCommandBuffer *pBuffer) { WaitForIdle(); @@ -428,7 +474,7 @@ void CCommandProcessorHandler::WaitForIdle() m_BufferDone.wait(); } -void CGraphics_Threaded::Flush() +void CGraphics_Threaded::FlushVertices() { if(m_NumVertices == 0) return; @@ -464,7 +510,7 @@ void CGraphics_Threaded::AddVertices(int Count) { m_NumVertices += Count; if((m_NumVertices + Count) >= MAX_VERTICES) - Flush(); + FlushVertices(); } void CGraphics_Threaded::Rotate4(const CCommandBuffer::SPoint &rCenter, CCommandBuffer::SVertex *pPoints) @@ -622,7 +668,7 @@ void CGraphics_Threaded::LinesBegin() void CGraphics_Threaded::LinesEnd() { dbg_assert(m_Drawing == DRAWING_LINES, "called Graphics()->LinesEnd without begin"); - Flush(); + FlushVertices(); m_Drawing = 0; } @@ -828,34 +874,33 @@ int CGraphics_Threaded::LoadPNG(CImageInfo *pImg, const char *pFilename, int Sto return 1; } +void CGraphics_Threaded::KickCommandBuffer() +{ + m_Handler.RunBuffer(m_pCommandBuffer); + + // swap buffer + m_CurrentCommandBuffer ^= 1; + m_pCommandBuffer = m_apCommandBuffers[m_CurrentCommandBuffer]; + m_pCommandBuffer->Reset(); +} + void CGraphics_Threaded::ScreenshotDirect(const char *pFilename) { - // TODO: screenshot support - return; + // add swap command + CImageInfo Image; + mem_zero(&Image, sizeof(Image)); - /* - // fetch image data - int y; - int w = m_ScreenWidth; - int h = m_ScreenHeight; - unsigned char *pPixelData = (unsigned char *)mem_alloc(w*(h+1)*3, 1); - unsigned char *pTempRow = pPixelData+w*h*3; - GLint Alignment; - glGetIntegerv(GL_PACK_ALIGNMENT, &Alignment); - glPixelStorei(GL_PACK_ALIGNMENT, 1); - glReadPixels(0,0, w, h, GL_RGB, GL_UNSIGNED_BYTE, pPixelData); - glPixelStorei(GL_PACK_ALIGNMENT, Alignment); + CCommandBuffer::SCommand_Screenshot Cmd; + Cmd.m_pImage = &Image; + m_pCommandBuffer->AddCommand(Cmd); - // flip the pixel because opengl works from bottom left corner - for(y = 0; y < h/2; y++) - { - mem_copy(pTempRow, pPixelData+y*w*3, w*3); - mem_copy(pPixelData+y*w*3, pPixelData+(h-y-1)*w*3, w*3); - mem_copy(pPixelData+(h-y-1)*w*3, pTempRow,w*3); - } - - // find filename + // kick the buffer and wait for the result + KickCommandBuffer(); + WaitForIdle(); + + if(Image.m_pData) { + // find filename char aWholePath[1024]; png_t Png; // ignore_convention @@ -868,13 +913,11 @@ void CGraphics_Threaded::ScreenshotDirect(const char *pFilename) str_format(aBuf, sizeof(aBuf), "saved screenshot to '%s'", aWholePath); m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "client", aBuf); png_open_file_write(&Png, aWholePath); // ignore_convention - png_set_data(&Png, w, h, 8, PNG_TRUECOLOR, (unsigned char *)pPixelData); // ignore_convention + png_set_data(&Png, Image.m_Width, Image.m_Height, 8, PNG_TRUECOLOR, (unsigned char *)Image.m_pData); // ignore_convention png_close_file(&Png); // ignore_convention - } - // clean up - mem_free(pPixelData); - */ + mem_free(Image.m_pData); + } } void CGraphics_Threaded::TextureSet(int TextureID) @@ -906,7 +949,7 @@ void CGraphics_Threaded::QuadsBegin() void CGraphics_Threaded::QuadsEnd() { dbg_assert(m_Drawing == DRAWING_QUADS, "called Graphics()->QuadsEnd without begin"); - Flush(); + FlushVertices(); m_Drawing = 0; } @@ -1154,8 +1197,8 @@ bool CGraphics_Threaded::Init() m_aTextures[MAX_TEXTURES-1].m_Next = -1; // start the command processor - ICommandProcessor *pProcessor = new CCommandProcessor_SDL_OpenGL; - m_Handler.Start(pProcessor); + m_pProcessor = new CCommandProcessor_SDL_OpenGL; + m_Handler.Start(m_pProcessor); // create command buffers m_apCommandBuffers[0] = new CCommandBuffer(1024*512, 1024*1024); @@ -1184,9 +1227,16 @@ bool CGraphics_Threaded::Init() void CGraphics_Threaded::Shutdown() { - // TODO: SDL, is this correct? - - // + // add swap command + CCommandBuffer::SCommand_Shutdown Cmd; + m_pCommandBuffer->AddCommand(Cmd); + m_Handler.RunBuffer(m_pCommandBuffer); + + // wait for everything to process and then stop the command processor + m_Handler.WaitForIdle(); + m_Handler.Stop(); + delete m_pProcessor; + m_pProcessor = 0; } void CGraphics_Threaded::Minimize() @@ -1213,34 +1263,27 @@ int CGraphics_Threaded::WindowOpen() void CGraphics_Threaded::TakeScreenshot(const char *pFilename) { // TODO: screenshot support - return; - /* char aDate[20]; str_timestamp(aDate, sizeof(aDate)); str_format(m_aScreenshotName, sizeof(m_aScreenshotName), "screenshots/%s_%s.png", pFilename?pFilename:"screenshot", aDate); m_DoScreenshot = true; - */ } void CGraphics_Threaded::Swap() { // TODO: screenshot support - /* if(m_DoScreenshot) { ScreenshotDirect(m_aScreenshotName); m_DoScreenshot = false; - }*/ + } // add swap command CCommandBuffer::SCommand_Swap Cmd; m_pCommandBuffer->AddCommand(Cmd); - m_Handler.RunBuffer(m_pCommandBuffer); - // swap buffer - m_CurrentCommandBuffer ^= 1; - m_pCommandBuffer = m_apCommandBuffers[m_CurrentCommandBuffer]; - m_pCommandBuffer->Reset(); + // kick the command buffer + KickCommandBuffer(); } // syncronization diff --git a/src/engine/client/graphics_threaded.h b/src/engine/client/graphics_threaded.h index 1ea84ea90..1e69674de 100644 --- a/src/engine/client/graphics_threaded.h +++ b/src/engine/client/graphics_threaded.h @@ -124,7 +124,7 @@ public: SPoint m_Pos; STexCoord m_Tex; SColor m_Color; - } ; + }; struct SCommand { @@ -198,8 +198,7 @@ public: struct SCommand_Screenshot : public SCommand { SCommand_Screenshot() : SCommand(CMD_SCREENSHOT) {} - - CImageInfo *m_pImage; // processor will fill this out + CImageInfo *m_pImage; // processor will fill this out, the one who adds this command must free the data as well }; struct SCommand_Swap : public SCommand @@ -307,6 +306,8 @@ class CCommandProcessorHandler public: CCommandProcessorHandler(); void Start(ICommandProcessor *pProcessor); + void Stop(); + void RunBuffer(CCommandBuffer *pBuffer); bool IsIdle() const { return m_pBuffer == 0; } void WaitForIdle(); @@ -316,6 +317,7 @@ class CGraphics_Threaded : public IEngineGraphics { CCommandBuffer::SState m_State; CCommandProcessorHandler m_Handler; + ICommandProcessor *m_pProcessor; CCommandBuffer *m_apCommandBuffers[2]; CCommandBuffer *m_pCommandBuffer; @@ -361,13 +363,15 @@ class CGraphics_Threaded : public IEngineGraphics int m_FirstFreeTexture; int m_TextureMemoryUsage; - void Flush(); + void FlushVertices(); void AddVertices(int Count); void Rotate4(const CCommandBuffer::SPoint &rCenter, CCommandBuffer::SVertex *pPoints); static unsigned char Sample(int w, int h, const unsigned char *pData, int u, int v, int Offset, int ScaleW, int ScaleH, int Bpp); static unsigned char *Rescale(int Width, int Height, int NewWidth, int NewHeight, int Format, const unsigned char *pData); + void KickCommandBuffer(); + int IssueInit(); int InitWindow(); public: From c31c82a5840640b0c7f4b07eb90e681b88b0a330 Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Sun, 1 Jan 2012 14:30:45 +0100 Subject: [PATCH 13/18] added support for video modes --- src/engine/client/graphics_threaded.cpp | 82 +++++++++++++++---------- src/engine/client/graphics_threaded.h | 14 ++++- 2 files changed, 60 insertions(+), 36 deletions(-) diff --git a/src/engine/client/graphics_threaded.cpp b/src/engine/client/graphics_threaded.cpp index a87e4d623..947f39600 100644 --- a/src/engine/client/graphics_threaded.cpp +++ b/src/engine/client/graphics_threaded.cpp @@ -371,6 +371,39 @@ class CCommandProcessorFragment_SDL SDL_GL_SwapBuffers(); } + void Cmd_VideoModes(const CCommandBuffer::SCommand_VideoModes *pCommand) + { + // TODO: fix this code on osx or windows + SDL_Rect **ppModes = SDL_ListModes(NULL, SDL_OPENGL|SDL_GL_DOUBLEBUFFER|SDL_FULLSCREEN); + if(ppModes == NULL) + { + // no modes + *pCommand->m_pNumModes = 0; + } + else if(ppModes == (SDL_Rect**)-1) + { + // no modes + *pCommand->m_pNumModes = 0; + } + else + { + int NumModes = 0; + for(int i = 0; ppModes[i]; ++i) + { + if(NumModes == pCommand->m_MaxModes) + break; + pCommand->m_pModes[NumModes].m_Width = ppModes[i]->w; + pCommand->m_pModes[NumModes].m_Height = ppModes[i]->h; + pCommand->m_pModes[NumModes].m_Red = 8; + pCommand->m_pModes[NumModes].m_Green = 8; + pCommand->m_pModes[NumModes].m_Blue = 8; + NumModes++; + } + + *pCommand->m_pNumModes = NumModes; + } + } + public: CCommandProcessorFragment_SDL() { @@ -385,6 +418,7 @@ public: case CCommandBuffer::CMD_INIT: Cmd_Init(static_cast(pBaseCommand)); break; case CCommandBuffer::CMD_SHUTDOWN: Cmd_Shutdown(static_cast(pBaseCommand)); break; case CCommandBuffer::CMD_SWAP: Cmd_Swap(static_cast(pBaseCommand)); break; + case CCommandBuffer::CMD_VIDEOMODES: Cmd_VideoModes(static_cast(pBaseCommand)); break; default: return false; } @@ -1306,11 +1340,7 @@ void CGraphics_Threaded::WaitForIdle() int CGraphics_Threaded::GetVideoModes(CVideoMode *pModes, int MaxModes) { - // TODO: fix support for video modes, using fake modes for now - //int NumModes = sizeof(g_aFakeModes)/sizeof(CVideoMode); - //SDL_Rect **ppModes; - - //if(g_Config.m_GfxDisplayAllModes) + if(g_Config.m_GfxDisplayAllModes) { int Count = sizeof(g_aFakeModes)/sizeof(CVideoMode); mem_copy(pModes, g_aFakeModes, sizeof(g_aFakeModes)); @@ -1319,37 +1349,21 @@ int CGraphics_Threaded::GetVideoModes(CVideoMode *pModes, int MaxModes) return Count; } - // TODO: fix this code on osx or windows - /* + // add videomodes command + CImageInfo Image; + mem_zero(&Image, sizeof(Image)); - ppModes = SDL_ListModes(NULL, SDL_OPENGL|SDL_GL_DOUBLEBUFFER|SDL_FULLSCREEN); - if(ppModes == NULL) - { - // no modes - NumModes = 0; - } - else if(ppModes == (SDL_Rect**)-1) - { - // all modes - } - else - { - NumModes = 0; - for(int i = 0; ppModes[i]; ++i) - { - if(NumModes == MaxModes) - break; - pModes[NumModes].m_Width = ppModes[i]->w; - pModes[NumModes].m_Height = ppModes[i]->h; - pModes[NumModes].m_Red = 8; - pModes[NumModes].m_Green = 8; - pModes[NumModes].m_Blue = 8; - NumModes++; - } - } + int NumModes = 0; + CCommandBuffer::SCommand_VideoModes Cmd; + Cmd.m_pModes = pModes; + Cmd.m_MaxModes = MaxModes; + Cmd.m_pNumModes = &NumModes; + m_pCommandBuffer->AddCommand(Cmd); - return NumModes;*/ + // kick the buffer and wait for the result and return it + KickCommandBuffer(); + WaitForIdle(); + return NumModes; } - extern IEngineGraphics *CreateEngineGraphicsThreaded() { return new CGraphics_Threaded(); } diff --git a/src/engine/client/graphics_threaded.h b/src/engine/client/graphics_threaded.h index 1e69674de..8b1c772d8 100644 --- a/src/engine/client/graphics_threaded.h +++ b/src/engine/client/graphics_threaded.h @@ -79,8 +79,9 @@ public: // swap CMD_SWAP, - // + // misc CMD_SCREENSHOT, + CMD_VIDEOMODES, }; enum @@ -192,7 +193,7 @@ public: SState m_State; unsigned m_PrimType; unsigned m_PrimCount; - SVertex *m_pVertices; + SVertex *m_pVertices; // you should use the command buffer data to allocate vertices for this command }; struct SCommand_Screenshot : public SCommand @@ -201,6 +202,15 @@ public: CImageInfo *m_pImage; // processor will fill this out, the one who adds this command must free the data as well }; + struct SCommand_VideoModes : public SCommand + { + SCommand_VideoModes() : SCommand(CMD_VIDEOMODES) {} + + CVideoMode *m_pModes; // processor will fill this in + int m_MaxModes; // maximum of modes the processor can write to the m_pModes + int *m_pNumModes; // processor will write to this pointer + }; + struct SCommand_Swap : public SCommand { SCommand_Swap() : SCommand(CMD_SWAP) {} From e59b24d8db8a7a9ec21c654302d89e01903f4d96 Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Sun, 1 Jan 2012 15:56:28 +0100 Subject: [PATCH 14/18] fixed atomics and semaphore for windows. can't test it correctly however due to that I only have a virtual box machine --- src/base/system.c | 15 +++++++++ src/base/system.h | 17 ++++++++++ src/base/tl/threading.h | 72 ++++++++++++++++++++++++++++------------- 3 files changed, 82 insertions(+), 22 deletions(-) diff --git a/src/base/system.c b/src/base/system.c index 4ea6d4134..2026199b0 100644 --- a/src/base/system.c +++ b/src/base/system.c @@ -495,6 +495,21 @@ void lock_release(LOCK lock) #endif } +#if defined(CONF_FAMILY_UNIX) +void semaphore_init(SEMAPHORE *sem) { sem_init(sem, 0, 0); } +void semaphore_wait(SEMAPHORE *sem) { sem_wait(sem); } +void semaphore_signal(SEMAPHORE *sem) { sem_post(sem); } +void semaphore_destroy(SEMAPHORE *sem) { sem_destroy(sem); } +#elif defined(CONF_FAMILY_WINDOWS) +void semaphore_init(SEMAPHORE *sem) { (HANDLE)(*sem) = CreateSemaphore(0, 0, 10000, 0); } +void semaphore_wait(SEMAPHORE *sem) { WaitForSingleObject((HANDLE)*sem, 0L); } +void semaphore_signal(SEMAPHORE *sem) { ReleaseSemaphore((HANDLE)*sem, 1, NULL); } +void semaphore_destroy(SEMAPHORE *sem) { CloseHandle((HANDLE)*sem); } +#else + #error not implemented on this platform +#endif + + /* ----- time ----- */ int64 time_get() { diff --git a/src/base/system.h b/src/base/system.h index aaa5b43f2..ca93279f0 100644 --- a/src/base/system.h +++ b/src/base/system.h @@ -388,6 +388,23 @@ int lock_try(LOCK lock); void lock_wait(LOCK lock); void lock_release(LOCK lock); + +/* Group: Semaphores */ + +#if defined(CONF_FAMILY_UNIX) + #include + typedef sem_t SEMAPHORE; +#elif defined(CONF_FAMILY_WINDOWS) + typedef void* SEMAPHORE; +#else + #error missing sempahore implementation +#endif + +void semaphore_init(SEMAPHORE *sem); +void semaphore_wait(SEMAPHORE *sem); +void semaphore_signal(SEMAPHORE *sem); +void semaphore_destroy(SEMAPHORE *sem); + /* Group: Timer */ #ifdef __GNUC__ /* if compiled with -pedantic-errors it will complain about long diff --git a/src/base/tl/threading.h b/src/base/tl/threading.h index 66751ec4c..dbf788cdc 100644 --- a/src/base/tl/threading.h +++ b/src/base/tl/threading.h @@ -3,32 +3,60 @@ #include "../system.h" -inline unsigned atomic_inc(volatile unsigned *pValue) -{ - return __sync_fetch_and_add(pValue, 1); -} +/* + atomic_inc - should return the value after increment + atomic_dec - should return the value after decrement + atomic_compswap - should return the value before the eventual swap + +*/ -inline unsigned atomic_dec(volatile unsigned *pValue) -{ - return __sync_fetch_and_add(pValue, -1); -} +#if defined(__GNUC__) -inline unsigned atomic_compswap(volatile unsigned *pValue, unsigned comperand, unsigned value) -{ - return __sync_val_compare_and_swap(pValue, comperand, value); -} + inline unsigned atomic_inc(volatile unsigned *pValue) + { + return __sync_fetch_and_add(pValue, 1); + } -inline void sync_barrier() -{ - __sync_synchronize(); -} + inline unsigned atomic_dec(volatile unsigned *pValue) + { + return __sync_fetch_and_add(pValue, -1); + } -#include -typedef sem_t SEMAPHORE; -inline void semaphore_init(SEMAPHORE *sem) { sem_init(sem, 0, 0); } -inline void semaphore_wait(SEMAPHORE *sem) { sem_wait(sem); } -inline void semaphore_signal(SEMAPHORE *sem) { sem_post(sem); } -inline void semaphore_destroy(SEMAPHORE *sem) { sem_destroy(sem); } + inline unsigned atomic_compswap(volatile unsigned *pValue, unsigned comperand, unsigned value) + { + return __sync_val_compare_and_swap(pValue, comperand, value); + } + + inline void sync_barrier() + { + __sync_synchronize(); + } + +#elif defined(_MSC_VER) + #include + + inline unsigned atomic_inc(volatile unsigned *pValue) + { + return _InterlockedIncrement((volatile long *)pValue); + } + + inline unsigned atomic_dec(volatile unsigned *pValue) + { + return _InterlockedDecrement((volatile long *)pValue); + } + + inline unsigned atomic_compswap(volatile unsigned *pValue, unsigned comperand, unsigned value) + { + return _InterlockedCompareExchange((volatile long *)pValue, (long)value, (long)comperand); + } + + inline void sync_barrier() + { + _ReadWriteBarrier(); + } +#else + #error missing atomic implementation for this compiler +#endif class semaphore { From 50d872531aae6640f57da98e8dcf6dbae1f9cd82 Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Tue, 3 Jan 2012 21:39:10 +0100 Subject: [PATCH 15/18] cleaned up the code. fixed so that SDL is inited on main thread and then transfers the gl context to the render thread --- src/engine/client/backend_sdl.cpp | 495 ++++++++++++++++++++++ src/engine/client/backend_sdl.h | 180 ++++++++ src/engine/client/client.cpp | 23 +- src/engine/client/graphics.cpp | 12 +- src/engine/client/graphics.h | 4 +- src/engine/client/graphics_threaded.cpp | 539 ++---------------------- src/engine/client/graphics_threaded.h | 88 ++-- src/engine/client/sound.cpp | 7 + src/engine/graphics.h | 2 +- 9 files changed, 772 insertions(+), 578 deletions(-) create mode 100644 src/engine/client/backend_sdl.cpp create mode 100644 src/engine/client/backend_sdl.h diff --git a/src/engine/client/backend_sdl.cpp b/src/engine/client/backend_sdl.cpp new file mode 100644 index 000000000..691d193ca --- /dev/null +++ b/src/engine/client/backend_sdl.cpp @@ -0,0 +1,495 @@ + +#include "SDL.h" +#include "SDL_opengl.h" + +#include "graphics_threaded.h" +#include "backend_sdl.h" + +// ------------ CGraphicsBackend_Threaded + +void CGraphicsBackend_Threaded::ThreadFunc(void *pUser) +{ + CGraphicsBackend_Threaded *pThis = (CGraphicsBackend_Threaded *)pUser; + + while(!pThis->m_Shutdown) + { + pThis->m_Activity.wait(); + if(pThis->m_pBuffer) + { + pThis->m_pProcessor->RunBuffer(pThis->m_pBuffer); + sync_barrier(); + pThis->m_pBuffer = 0x0; + pThis->m_BufferDone.signal(); + } + } +} + +CGraphicsBackend_Threaded::CGraphicsBackend_Threaded() +{ + m_pBuffer = 0x0; + m_pProcessor = 0x0; + m_pThread = 0x0; +} + +void CGraphicsBackend_Threaded::StartProcessor(ICommandProcessor *pProcessor) +{ + m_Shutdown = false; + m_pProcessor = pProcessor; + m_pThread = thread_create(ThreadFunc, this); + m_BufferDone.signal(); +} + +void CGraphicsBackend_Threaded::StopProcessor() +{ + m_Shutdown = true; + m_Activity.signal(); + thread_wait(m_pThread); + thread_destroy(m_pThread); +} + +void CGraphicsBackend_Threaded::RunBuffer(CCommandBuffer *pBuffer) +{ + WaitForIdle(); + m_pBuffer = pBuffer; + m_Activity.signal(); +} + +bool CGraphicsBackend_Threaded::IsIdle() const +{ + return m_pBuffer == 0x0; +} + +void CGraphicsBackend_Threaded::WaitForIdle() +{ + while(m_pBuffer != 0x0) + m_BufferDone.wait(); +} + + +// ------------ CCommandProcessorFragment_General + +void CCommandProcessorFragment_General::Cmd_Signal(const CCommandBuffer::SCommand_Signal *pCommand) +{ + pCommand->m_pSemaphore->signal(); +} + +bool CCommandProcessorFragment_General::RunCommand(const CCommandBuffer::SCommand * pBaseCommand) +{ + switch(pBaseCommand->m_Cmd) + { + case CCommandBuffer::CMD_NOP: break; + case CCommandBuffer::CMD_SIGNAL: Cmd_Signal(static_cast(pBaseCommand)); break; + default: return false; + } + + return true; +} + +// ------------ CCommandProcessorFragment_OpenGL + +int CCommandProcessorFragment_OpenGL::TexFormatToOpenGLFormat(int TexFormat) +{ + if(TexFormat == CCommandBuffer::TEXFORMAT_RGB) return GL_RGB; + if(TexFormat == CCommandBuffer::TEXFORMAT_ALPHA) return GL_ALPHA; + if(TexFormat == CCommandBuffer::TEXFORMAT_RGBA) return GL_RGBA; + return GL_RGBA; +} + +void CCommandProcessorFragment_OpenGL::SetState(const CCommandBuffer::SState &State) +{ + // blend + switch(State.m_BlendMode) + { + case CCommandBuffer::BLEND_NONE: + glDisable(GL_BLEND); + break; + case CCommandBuffer::BLEND_ALPHA: + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + break; + case CCommandBuffer::BLEND_ADDITIVE: + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE); + break; + default: + dbg_msg("render", "unknown blendmode %d\n", State.m_BlendMode); + }; + + // clip + if(State.m_ClipEnable) + { + glScissor(State.m_ClipX, State.m_ClipY, State.m_ClipW, State.m_ClipH); + glEnable(GL_SCISSOR_TEST); + } + else + glDisable(GL_SCISSOR_TEST); + + // texture + if(State.m_Texture >= 0 && State.m_Texture < CCommandBuffer::MAX_TEXTURES) + { + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, m_aTextures[State.m_Texture]); + } + else + glDisable(GL_TEXTURE_2D); + + // screen mapping + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(State.m_ScreenTL.x, State.m_ScreenBR.x, State.m_ScreenBR.y, State.m_ScreenTL.y, 1.0f, 10.f); +} + +void CCommandProcessorFragment_OpenGL::Cmd_Texture_Update(const CCommandBuffer::SCommand_Texture_Update *pCommand) +{ + glBindTexture(GL_TEXTURE_2D, m_aTextures[pCommand->m_Slot]); + glTexSubImage2D(GL_TEXTURE_2D, 0, pCommand->m_X, pCommand->m_Y, pCommand->m_Width, pCommand->m_Height, + TexFormatToOpenGLFormat(pCommand->m_Format), GL_UNSIGNED_BYTE, pCommand->m_pData); + mem_free(pCommand->m_pData); +} + +void CCommandProcessorFragment_OpenGL::Cmd_Texture_Destroy(const CCommandBuffer::SCommand_Texture_Destroy *pCommand) +{ + glDeleteTextures(1, &m_aTextures[pCommand->m_Slot]); +} + +void CCommandProcessorFragment_OpenGL::Cmd_Texture_Create(const CCommandBuffer::SCommand_Texture_Create *pCommand) +{ + int Oglformat = TexFormatToOpenGLFormat(pCommand->m_Format); + int StoreOglformat = TexFormatToOpenGLFormat(pCommand->m_StoreFormat); + + glGenTextures(1, &m_aTextures[pCommand->m_Slot]); + glBindTexture(GL_TEXTURE_2D, m_aTextures[pCommand->m_Slot]); + + if(pCommand->m_Flags&CCommandBuffer::TEXFLAG_NOMIPMAPS) + { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, StoreOglformat, pCommand->m_Width, pCommand->m_Height, 0, Oglformat, GL_UNSIGNED_BYTE, pCommand->m_pData); + } + else + { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); + gluBuild2DMipmaps(GL_TEXTURE_2D, StoreOglformat, pCommand->m_Width, pCommand->m_Height, Oglformat, GL_UNSIGNED_BYTE, pCommand->m_pData); + } + + mem_free(pCommand->m_pData); +} + +void CCommandProcessorFragment_OpenGL::Cmd_Clear(const CCommandBuffer::SCommand_Clear *pCommand) +{ + glClearColor(pCommand->m_Color.r, pCommand->m_Color.g, pCommand->m_Color.b, 0.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +} + +void CCommandProcessorFragment_OpenGL::Cmd_Render(const CCommandBuffer::SCommand_Render *pCommand) +{ + SetState(pCommand->m_State); + + glVertexPointer(3, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices); + glTexCoordPointer(2, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices + sizeof(float)*3); + glColorPointer(4, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices + sizeof(float)*5); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); + + switch(pCommand->m_PrimType) + { + case CCommandBuffer::PRIMTYPE_QUADS: + glDrawArrays(GL_QUADS, 0, pCommand->m_PrimCount*4); + break; + case CCommandBuffer::PRIMTYPE_LINES: + glDrawArrays(GL_LINES, 0, pCommand->m_PrimCount*2); + break; + default: + dbg_msg("render", "unknown primtype %d\n", pCommand->m_Cmd); + }; +} + +void CCommandProcessorFragment_OpenGL::Cmd_Screenshot(const CCommandBuffer::SCommand_Screenshot *pCommand) +{ + // fetch image data + GLint aViewport[4] = {0,0,0,0}; + glGetIntegerv(GL_VIEWPORT, aViewport); + + int w = aViewport[2]; + int h = aViewport[3]; + + // we allocate one more row to use when we are flipping the texture + unsigned char *pPixelData = (unsigned char *)mem_alloc(w*(h+1)*3, 1); + unsigned char *pTempRow = pPixelData+w*h*3; + + // fetch the pixels + GLint Alignment; + glGetIntegerv(GL_PACK_ALIGNMENT, &Alignment); + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glReadPixels(0,0, w, h, GL_RGB, GL_UNSIGNED_BYTE, pPixelData); + glPixelStorei(GL_PACK_ALIGNMENT, Alignment); + + // flip the pixel because opengl works from bottom left corner + for(int y = 0; y < h/2; y++) + { + mem_copy(pTempRow, pPixelData+y*w*3, w*3); + mem_copy(pPixelData+y*w*3, pPixelData+(h-y-1)*w*3, w*3); + mem_copy(pPixelData+(h-y-1)*w*3, pTempRow,w*3); + } + + // fill in the information + pCommand->m_pImage->m_Width = w; + pCommand->m_pImage->m_Height = h; + pCommand->m_pImage->m_Format = CImageInfo::FORMAT_RGB; + pCommand->m_pImage->m_pData = pPixelData; +} + +CCommandProcessorFragment_OpenGL::CCommandProcessorFragment_OpenGL() +{ + mem_zero(m_aTextures, sizeof(m_aTextures)); +} + +bool CCommandProcessorFragment_OpenGL::RunCommand(const CCommandBuffer::SCommand * pBaseCommand) +{ + switch(pBaseCommand->m_Cmd) + { + case CCommandBuffer::CMD_TEXTURE_CREATE: Cmd_Texture_Create(static_cast(pBaseCommand)); break; + case CCommandBuffer::CMD_TEXTURE_DESTROY: Cmd_Texture_Destroy(static_cast(pBaseCommand)); break; + case CCommandBuffer::CMD_TEXTURE_UPDATE: Cmd_Texture_Update(static_cast(pBaseCommand)); break; + case CCommandBuffer::CMD_CLEAR: Cmd_Clear(static_cast(pBaseCommand)); break; + case CCommandBuffer::CMD_RENDER: Cmd_Render(static_cast(pBaseCommand)); break; + case CCommandBuffer::CMD_SCREENSHOT: Cmd_Screenshot(static_cast(pBaseCommand)); break; + default: return false; + } + + return true; +} + + +// ------------ CCommandProcessorFragment_SDL + +void CCommandProcessorFragment_SDL::Cmd_Init(const SCommand_Init *pCommand) +{ + m_GLContext = pCommand->m_Context; + GL_MakeCurrent(m_GLContext); + + // set some default settings + glEnable(GL_BLEND); + glDisable(GL_CULL_FACE); + glDisable(GL_DEPTH_TEST); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glAlphaFunc(GL_GREATER, 0); + glEnable(GL_ALPHA_TEST); + glDepthMask(0); +} + +void CCommandProcessorFragment_SDL::Cmd_Shutdown(const SCommand_Shutdown *pCommand) +{ + GL_ReleaseContext(m_GLContext); +} + +void CCommandProcessorFragment_SDL::Cmd_Swap(const CCommandBuffer::SCommand_Swap *pCommand) +{ + GL_SwapBuffers(m_GLContext); +} + +void CCommandProcessorFragment_SDL::Cmd_VideoModes(const CCommandBuffer::SCommand_VideoModes *pCommand) +{ + // TODO: fix this code on osx or windows + SDL_Rect **ppModes = SDL_ListModes(NULL, SDL_OPENGL|SDL_GL_DOUBLEBUFFER|SDL_FULLSCREEN); + if(ppModes == NULL) + { + // no modes + *pCommand->m_pNumModes = 0; + } + else if(ppModes == (SDL_Rect**)-1) + { + // no modes + *pCommand->m_pNumModes = 0; + } + else + { + int NumModes = 0; + for(int i = 0; ppModes[i]; ++i) + { + if(NumModes == pCommand->m_MaxModes) + break; + pCommand->m_pModes[NumModes].m_Width = ppModes[i]->w; + pCommand->m_pModes[NumModes].m_Height = ppModes[i]->h; + pCommand->m_pModes[NumModes].m_Red = 8; + pCommand->m_pModes[NumModes].m_Green = 8; + pCommand->m_pModes[NumModes].m_Blue = 8; + NumModes++; + } + + *pCommand->m_pNumModes = NumModes; + } +} + +CCommandProcessorFragment_SDL::CCommandProcessorFragment_SDL() +{ +} + +bool CCommandProcessorFragment_SDL::RunCommand(const CCommandBuffer::SCommand *pBaseCommand) +{ + switch(pBaseCommand->m_Cmd) + { + case CCommandBuffer::CMD_SWAP: Cmd_Swap(static_cast(pBaseCommand)); break; + case CCommandBuffer::CMD_VIDEOMODES: Cmd_VideoModes(static_cast(pBaseCommand)); break; + case CMD_INIT: Cmd_Init(static_cast(pBaseCommand)); break; + case CMD_SHUTDOWN: Cmd_Shutdown(static_cast(pBaseCommand)); break; + default: return false; + } + + return true; +} + +// ------------ CCommandProcessor_SDL_OpenGL + +void CCommandProcessor_SDL_OpenGL::RunBuffer(CCommandBuffer *pBuffer) +{ + unsigned CmdIndex = 0; + while(1) + { + const CCommandBuffer::SCommand *pBaseCommand = pBuffer->GetCommand(&CmdIndex); + if(pBaseCommand == 0x0) + break; + + if(m_OpenGL.RunCommand(pBaseCommand)) + continue; + + if(m_SDL.RunCommand(pBaseCommand)) + continue; + + if(m_General.RunCommand(pBaseCommand)) + continue; + + dbg_msg("graphics", "unknown command %d", pBaseCommand->m_Cmd); + } +} + +// ------------ CGraphicsBackend_SDL_OpenGL + +int CGraphicsBackend_SDL_OpenGL::Init(const char *pName, int Width, int Height, int FsaaSamples, int Flags) +{ + if(!SDL_WasInit(SDL_INIT_VIDEO)) + { + if(SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) + { + dbg_msg("gfx", "unable to init SDL video: %s", SDL_GetError()); + return -1; + } + + #ifdef CONF_FAMILY_WINDOWS + if(!getenv("SDL_VIDEO_WINDOW_POS") && !getenv("SDL_VIDEO_CENTERED")) // ignore_convention + putenv("SDL_VIDEO_WINDOW_POS=8,27"); // ignore_convention + #endif + } + + const SDL_VideoInfo *pInfo = SDL_GetVideoInfo(); + + // set flags + int SdlFlags = SDL_OPENGL; + if(Flags&IGraphicsBackend::INITFLAG_RESIZABLE) + SdlFlags |= SDL_RESIZABLE; + + if(pInfo->hw_available) // ignore_convention + SdlFlags |= SDL_HWSURFACE; + else + SdlFlags |= SDL_SWSURFACE; + + if(pInfo->blit_hw) // ignore_convention + SdlFlags |= SDL_HWACCEL; + + if(Flags&IGraphicsBackend::INITFLAG_FULLSCREEN) + SdlFlags |= SDL_FULLSCREEN; + + // set gl attributes + if(FsaaSamples) + { + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, FsaaSamples); + } + else + { + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0); + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0); + } + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, Flags&CCommandBuffer::INITFLAG_VSYNC ? 1 : 0); + + // set caption + SDL_WM_SetCaption(pName, pName); + + // create window + m_pScreenSurface = SDL_SetVideoMode(Width, Height, 0, SdlFlags); + if(!m_pScreenSurface) + { + dbg_msg("gfx", "unable to set video mode: %s", SDL_GetError()); + //*pCommand->m_pResult = -1; + return -1; + } + + SDL_ShowCursor(0); + + // fetch gl contexts and release the context from this thread + m_GLContext = GL_GetCurrentContext(); + GL_ReleaseContext(m_GLContext); + + // start the command processor + m_pProcessor = new CCommandProcessor_SDL_OpenGL; + StartProcessor(m_pProcessor); + + // issue a init command + CCommandBuffer CmdBuffer(1024, 512); + CCommandProcessorFragment_SDL::SCommand_Init Cmd; + Cmd.m_Context = m_GLContext; + CmdBuffer.AddCommand(Cmd); + RunBuffer(&CmdBuffer); + WaitForIdle(); + + // return + return 0; +} + +int CGraphicsBackend_SDL_OpenGL::Shutdown() +{ + // issue a shutdown command + CCommandBuffer CmdBuffer(1024, 512); + CCommandProcessorFragment_SDL::SCommand_Shutdown Cmd; + CmdBuffer.AddCommand(Cmd); + RunBuffer(&CmdBuffer); + WaitForIdle(); + + // stop and delete the processor + StopProcessor(); + delete m_pProcessor; + m_pProcessor = 0; + + SDL_QuitSubSystem(SDL_INIT_VIDEO); + return 0; +} + +void CGraphicsBackend_SDL_OpenGL::Minimize() +{ + SDL_WM_IconifyWindow(); +} + +void CGraphicsBackend_SDL_OpenGL::Maximize() +{ + // TODO: SDL +} + +int CGraphicsBackend_SDL_OpenGL::WindowActive() +{ + return SDL_GetAppState()&SDL_APPINPUTFOCUS; +} + +int CGraphicsBackend_SDL_OpenGL::WindowOpen() +{ + return SDL_GetAppState()&SDL_APPACTIVE; + +} + + +IGraphicsBackend *CreateGraphicsBackend() { return new CGraphicsBackend_SDL_OpenGL; } diff --git a/src/engine/client/backend_sdl.h b/src/engine/client/backend_sdl.h new file mode 100644 index 000000000..de0c6bd4a --- /dev/null +++ b/src/engine/client/backend_sdl.h @@ -0,0 +1,180 @@ + +#include "SDL.h" +#include "SDL_opengl.h" + +#include "graphics_threaded.h" + + + +// platform dependent implementations for transfering render context from the main thread to the graphics thread +// TODO: when SDL 1.3 comes, this can be removed +#if defined(CONF_FAMILY_WINDOWS) + struct SGLContext + { + HDC m_hDC; + HGLRC m_hGLRC; + }; + + static SGLContext GL_GetCurrentContext() + { + SGLContext Context; + Context.m_hDC = wglGetCurrentDC(); + Context.m_hGLRC = wglGetCurrentContext(); + return Context; + } + + static void GL_MakeCurrent(const SGLContext &Context) { wglMakeCurrent(Context.m_hDC, Context.m_hGLRC); } + static void GL_ReleaseContext(const SGLContext &Context) { wglMakeCurrent(Context.m_hDC, NULL); } + static void GL_SwapBuffers(const SGLContext &Context) { SwapBuffers(Context.m_hDC); } +#elif defined(CONF_PLATFORM_MACOSX) + #error missing implementation +#elif defined(CONF_FAMILY_UNIX) + + #include + + struct SGLContext + { + Display *m_pDisplay; + GLXDrawable m_Drawable; + GLXContext m_Context; + }; + + static SGLContext GL_GetCurrentContext() + { + SGLContext Context; + Context.m_pDisplay = glXGetCurrentDisplay(); + Context.m_Drawable = glXGetCurrentDrawable(); + Context.m_Context = glXGetCurrentContext(); + return Context; + } + + static void GL_MakeCurrent(const SGLContext &Context) { glXMakeCurrent(Context.m_pDisplay, Context.m_Drawable, Context.m_Context); } + static void GL_ReleaseContext(const SGLContext &Context) { glXMakeCurrent(Context.m_pDisplay, None, 0x0); } + static void GL_SwapBuffers(const SGLContext &Context) { glXSwapBuffers(Context.m_pDisplay, Context.m_Drawable); } +#else + #error missing implementation +#endif + + +// basic threaded backend, abstract, missing init and shutdown functions +class CGraphicsBackend_Threaded : public IGraphicsBackend +{ +public: + // constructed on the main thread, the rest of the functions is runned on the render thread + class ICommandProcessor + { + public: + virtual ~ICommandProcessor() {} + virtual void RunBuffer(CCommandBuffer *pBuffer) = 0; + }; + + CGraphicsBackend_Threaded(); + + virtual void RunBuffer(CCommandBuffer *pBuffer); + virtual bool IsIdle() const; + virtual void WaitForIdle(); + +protected: + void StartProcessor(ICommandProcessor *pProcessor); + void StopProcessor(); + +private: + ICommandProcessor *m_pProcessor; + CCommandBuffer * volatile m_pBuffer; + volatile bool m_Shutdown; + semaphore m_Activity; + semaphore m_BufferDone; + void *m_pThread; + + 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); +}; + +// takes care of opengl related rendering +class CCommandProcessorFragment_OpenGL +{ + GLuint m_aTextures[CCommandBuffer::MAX_TEXTURES]; + static int TexFormatToOpenGLFormat(int TexFormat); + + void SetState(const CCommandBuffer::SState &State); + + void Cmd_Texture_Update(const CCommandBuffer::SCommand_Texture_Update *pCommand); + void Cmd_Texture_Destroy(const CCommandBuffer::SCommand_Texture_Destroy *pCommand); + void Cmd_Texture_Create(const CCommandBuffer::SCommand_Texture_Create *pCommand); + void Cmd_Clear(const CCommandBuffer::SCommand_Clear *pCommand); + void Cmd_Render(const CCommandBuffer::SCommand_Render *pCommand); + void Cmd_Screenshot(const CCommandBuffer::SCommand_Screenshot *pCommand); + +public: + CCommandProcessorFragment_OpenGL(); + + bool RunCommand(const CCommandBuffer::SCommand * pBaseCommand); +}; + +// takes care of sdl related commands +class CCommandProcessorFragment_SDL +{ + // SDL stuff + SGLContext m_GLContext; +public: + enum + { + CMD_INIT = CCommandBuffer::CMDGROUP_PLATFORM, + CMD_SHUTDOWN, + }; + + struct SCommand_Init : public CCommandBuffer::SCommand + { + SCommand_Init() : SCommand(CMD_INIT) {} + SGLContext m_Context; + }; + + 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_VideoModes(const CCommandBuffer::SCommand_VideoModes *pCommand); +public: + CCommandProcessorFragment_SDL(); + + bool RunCommand(const CCommandBuffer::SCommand *pBaseCommand); +}; + +// command processor impelementation, uses the fragments to combine into one processor +class CCommandProcessor_SDL_OpenGL : public CGraphicsBackend_Threaded::ICommandProcessor +{ + CCommandProcessorFragment_OpenGL m_OpenGL; + CCommandProcessorFragment_SDL m_SDL; + CCommandProcessorFragment_General m_General; + public: + virtual void RunBuffer(CCommandBuffer *pBuffer); +}; + +// graphics backend implemented with SDL and OpenGL +class CGraphicsBackend_SDL_OpenGL : public CGraphicsBackend_Threaded +{ + SDL_Surface *m_pScreenSurface; + ICommandProcessor *m_pProcessor; + SGLContext m_GLContext; +public: + virtual int Init(const char *pName, int Width, int Height, int FsaaSamples, int Flags); + virtual int Shutdown(); + + virtual void Minimize(); + virtual void Maximize(); + virtual int WindowActive(); + virtual int WindowOpen(); +}; diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index b7244db4c..5067f8b29 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -1690,11 +1690,24 @@ void CClient::InitInterfaces() m_Friends.Init(); } +#include "SDL.h" + void CClient::Run() { m_LocalStartTime = time_get(); m_SnapshotParts = 0; + // init SDL + { + if(SDL_Init(0) < 0) + { + dbg_msg("client", "unable to init SDL base: %s", SDL_GetError()); + return; + } + + atexit(SDL_Quit); // ignore_convention + } + // init graphics { if(g_Config.m_GfxThreaded) @@ -1713,6 +1726,9 @@ void CClient::Run() } } + // init sound, allowed to fail + m_SoundInitFailed = Sound()->Init() != 0; + // open socket { NETADDR BindAddr; @@ -1737,8 +1753,6 @@ void CClient::Run() // init the editor //m_pEditor->Init(); - // init sound, allowed to fail - m_SoundInitFailed = Sound()->Init() != 0; // load data if(!LoadData()) @@ -1937,6 +1951,11 @@ void CClient::Run() m_pGraphics->Shutdown(); m_pSound->Shutdown(); + + // shutdown SDL + { + SDL_Quit(); + } } diff --git a/src/engine/client/graphics.cpp b/src/engine/client/graphics.cpp index d16f10fc4..ad3926f20 100644 --- a/src/engine/client/graphics.cpp +++ b/src/engine/client/graphics.cpp @@ -707,7 +707,7 @@ void CGraphics_OpenGL::QuadsText(float x, float y, float Size, float r, float g, QuadsEnd(); } -bool CGraphics_OpenGL::Init() +int CGraphics_OpenGL::Init() { m_pStorage = Kernel()->RequestInterface(); m_pConsole = Kernel()->RequestInterface(); @@ -743,7 +743,7 @@ bool CGraphics_OpenGL::Init() m_InvalidTexture = LoadTextureRaw(4,4,CImageInfo::FORMAT_RGBA,aNullTextureData,CImageInfo::FORMAT_RGBA,TEXLOAD_NORESAMPLE); - return true; + return 0; } int CGraphics_SDL::TryInit() @@ -841,7 +841,7 @@ CGraphics_SDL::CGraphics_SDL() m_pScreenSurface = 0; } -bool CGraphics_SDL::Init() +int CGraphics_SDL::Init() { { int Systems = SDL_INIT_VIDEO; @@ -855,7 +855,7 @@ bool CGraphics_SDL::Init() if(SDL_Init(Systems) < 0) { dbg_msg("gfx", "unable to init SDL: %s", SDL_GetError()); - return true; + return -1; } } @@ -867,14 +867,14 @@ bool CGraphics_SDL::Init() #endif if(InitWindow() != 0) - return true; + return -1; SDL_ShowCursor(0); CGraphics_OpenGL::Init(); MapScreen(0,0,g_Config.m_GfxScreenWidth, g_Config.m_GfxScreenHeight); - return false; + return 0; } void CGraphics_SDL::Shutdown() diff --git a/src/engine/client/graphics.h b/src/engine/client/graphics.h index 37276d36f..3ab550dcf 100644 --- a/src/engine/client/graphics.h +++ b/src/engine/client/graphics.h @@ -118,7 +118,7 @@ public: virtual void QuadsDrawFreeform(const CFreeformItem *pArray, int Num); virtual void QuadsText(float x, float y, float Size, float r, float g, float b, float a, const char *pText); - virtual bool Init(); + virtual int Init(); }; class CGraphics_SDL : public CGraphics_OpenGL @@ -130,7 +130,7 @@ class CGraphics_SDL : public CGraphics_OpenGL public: CGraphics_SDL(); - virtual bool Init(); + virtual int Init(); virtual void Shutdown(); virtual void Minimize(); diff --git a/src/engine/client/graphics_threaded.cpp b/src/engine/client/graphics_threaded.cpp index 947f39600..d1d09d380 100644 --- a/src/engine/client/graphics_threaded.cpp +++ b/src/engine/client/graphics_threaded.cpp @@ -4,9 +4,6 @@ #include #include -#include "SDL.h" -#include "SDL_opengl.h" - #include #include @@ -20,7 +17,6 @@ #include "graphics_threaded.h" - static CVideoMode g_aFakeModes[] = { {320,240,8,8,8}, {400,300,8,8,8}, {640,480,8,8,8}, {720,400,8,8,8}, {768,576,8,8,8}, {800,600,8,8,8}, @@ -45,469 +41,6 @@ static CVideoMode g_aFakeModes[] = { {2048,1536,5,6,5} }; -class CCommandProcessorFragment_General -{ -public: - void Cmd_Signal(const CCommandBuffer::SCommand_Signal *pCommand) - { - pCommand->m_pSemaphore->signal(); - } - - bool RunCommand(const CCommandBuffer::SCommand * pBaseCommand) - { - switch(pBaseCommand->m_Cmd) - { - case CCommandBuffer::CMD_NOP: break; - case CCommandBuffer::CMD_SIGNAL: Cmd_Signal(static_cast(pBaseCommand)); break; - default: return false; - } - - return true; - } -}; - -class CCommandProcessorFragment_OpenGL -{ - GLuint m_aTextures[CCommandBuffer::MAX_TEXTURES]; - - void SetState(const CCommandBuffer::SState &State) - { - // blend - switch(State.m_BlendMode) - { - case CCommandBuffer::BLEND_NONE: - glDisable(GL_BLEND); - break; - case CCommandBuffer::BLEND_ALPHA: - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - break; - case CCommandBuffer::BLEND_ADDITIVE: - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE); - break; - default: - dbg_msg("render", "unknown blendmode %d\n", State.m_BlendMode); - }; - - // clip - if(State.m_ClipEnable) - { - glScissor(State.m_ClipX, State.m_ClipY, State.m_ClipW, State.m_ClipH); - glEnable(GL_SCISSOR_TEST); - } - else - glDisable(GL_SCISSOR_TEST); - - // texture - if(State.m_Texture >= 0 && State.m_Texture < CCommandBuffer::MAX_TEXTURES) - { - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, m_aTextures[State.m_Texture]); - } - else - glDisable(GL_TEXTURE_2D); - - // screen mapping - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(State.m_ScreenTL.x, State.m_ScreenBR.x, State.m_ScreenBR.y, State.m_ScreenTL.y, 1.0f, 10.f); - } - - static int TexFormatToOpenGLFormat(int TexFormat) - { - if(TexFormat == CCommandBuffer::TEXFORMAT_RGB) return GL_RGB; - if(TexFormat == CCommandBuffer::TEXFORMAT_ALPHA) return GL_ALPHA; - if(TexFormat == CCommandBuffer::TEXFORMAT_RGBA) return GL_RGBA; - return GL_RGBA; - } - - void Cmd_Texture_Update(const CCommandBuffer::SCommand_Texture_Update *pCommand) - { - glBindTexture(GL_TEXTURE_2D, m_aTextures[pCommand->m_Slot]); - glTexSubImage2D(GL_TEXTURE_2D, 0, pCommand->m_X, pCommand->m_Y, pCommand->m_Width, pCommand->m_Height, - TexFormatToOpenGLFormat(pCommand->m_Format), GL_UNSIGNED_BYTE, pCommand->m_pData); - mem_free(pCommand->m_pData); - } - - void Cmd_Texture_Destroy(const CCommandBuffer::SCommand_Texture_Destroy *pCommand) - { - glDeleteTextures(1, &m_aTextures[pCommand->m_Slot]); - } - - void Cmd_Texture_Create(const CCommandBuffer::SCommand_Texture_Create *pCommand) - { - int Oglformat = TexFormatToOpenGLFormat(pCommand->m_Format); - - // upload texture - int StoreOglformat = Oglformat; - if(g_Config.m_GfxTextureCompression) - { - StoreOglformat = GL_COMPRESSED_RGBA_ARB; - if(pCommand->m_StoreFormat == CCommandBuffer::TEXFORMAT_RGB) - StoreOglformat = GL_COMPRESSED_RGB_ARB; - else if(Oglformat == CCommandBuffer::TEXFORMAT_ALPHA) - StoreOglformat = GL_COMPRESSED_ALPHA_ARB; - } - else - { - StoreOglformat = TexFormatToOpenGLFormat(pCommand->m_StoreFormat); - } - - glGenTextures(1, &m_aTextures[pCommand->m_Slot]); - glBindTexture(GL_TEXTURE_2D, m_aTextures[pCommand->m_Slot]); - - if(pCommand->m_Flags&CCommandBuffer::TEXFLAG_NOMIPMAPS) - { - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, StoreOglformat, pCommand->m_Width, pCommand->m_Height, 0, Oglformat, GL_UNSIGNED_BYTE, pCommand->m_pData); - } - else - { - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); - gluBuild2DMipmaps(GL_TEXTURE_2D, StoreOglformat, pCommand->m_Width, pCommand->m_Height, Oglformat, GL_UNSIGNED_BYTE, pCommand->m_pData); - } - - mem_free(pCommand->m_pData); - } - - void Cmd_Clear(const CCommandBuffer::SCommand_Clear *pCommand) - { - glClearColor(pCommand->m_Color.r, pCommand->m_Color.g, pCommand->m_Color.b, 0.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - } - - void Cmd_Render(const CCommandBuffer::SCommand_Render *pCommand) - { - SetState(pCommand->m_State); - - glVertexPointer(3, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices); - glTexCoordPointer(2, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices + sizeof(float)*3); - glColorPointer(4, GL_FLOAT, sizeof(CCommandBuffer::SVertex), (char*)pCommand->m_pVertices + sizeof(float)*5); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glEnableClientState(GL_COLOR_ARRAY); - - switch(pCommand->m_PrimType) - { - case CCommandBuffer::PRIMTYPE_QUADS: - glDrawArrays(GL_QUADS, 0, pCommand->m_PrimCount*4); - break; - case CCommandBuffer::PRIMTYPE_LINES: - glDrawArrays(GL_LINES, 0, pCommand->m_PrimCount*2); - break; - default: - dbg_msg("render", "unknown primtype %d\n", pCommand->m_Cmd); - }; - } - - void Cmd_Screenshot(const CCommandBuffer::SCommand_Screenshot *pCommand) - { - // fetch image data - GLint aViewport[4] = {0,0,0,0}; - glGetIntegerv(GL_VIEWPORT, aViewport); - - int w = aViewport[2]; - int h = aViewport[3]; - - dbg_msg("graphics", "grabbing %d x %d", w, h); - - // we allocate one more row to use when we are flipping the texture - unsigned char *pPixelData = (unsigned char *)mem_alloc(w*(h+1)*3, 1); - unsigned char *pTempRow = pPixelData+w*h*3; - - // fetch the pixels - GLint Alignment; - glGetIntegerv(GL_PACK_ALIGNMENT, &Alignment); - glPixelStorei(GL_PACK_ALIGNMENT, 1); - glReadPixels(0,0, w, h, GL_RGB, GL_UNSIGNED_BYTE, pPixelData); - glPixelStorei(GL_PACK_ALIGNMENT, Alignment); - - // flip the pixel because opengl works from bottom left corner - for(int y = 0; y < h/2; y++) - { - mem_copy(pTempRow, pPixelData+y*w*3, w*3); - mem_copy(pPixelData+y*w*3, pPixelData+(h-y-1)*w*3, w*3); - mem_copy(pPixelData+(h-y-1)*w*3, pTempRow,w*3); - } - - // fill in the information - pCommand->m_pImage->m_Width = w; - pCommand->m_pImage->m_Height = h; - pCommand->m_pImage->m_Format = CImageInfo::FORMAT_RGB; - pCommand->m_pImage->m_pData = pPixelData; - } - -public: - CCommandProcessorFragment_OpenGL() - { - mem_zero(m_aTextures, sizeof(m_aTextures)); - } - - bool RunCommand(const CCommandBuffer::SCommand * pBaseCommand) - { - switch(pBaseCommand->m_Cmd) - { - case CCommandBuffer::CMD_TEXTURE_CREATE: Cmd_Texture_Create(static_cast(pBaseCommand)); break; - case CCommandBuffer::CMD_TEXTURE_DESTROY: Cmd_Texture_Destroy(static_cast(pBaseCommand)); break; - case CCommandBuffer::CMD_TEXTURE_UPDATE: Cmd_Texture_Update(static_cast(pBaseCommand)); break; - case CCommandBuffer::CMD_CLEAR: Cmd_Clear(static_cast(pBaseCommand)); break; - case CCommandBuffer::CMD_RENDER: Cmd_Render(static_cast(pBaseCommand)); break; - case CCommandBuffer::CMD_SCREENSHOT: Cmd_Screenshot(static_cast(pBaseCommand)); break; - default: return false; - } - - return true; - } -}; - -class CCommandProcessorFragment_SDL -{ - // SDL stuff - SDL_Surface *m_pScreenSurface; - bool m_SystemInited; - - void Cmd_Init(const CCommandBuffer::SCommand_Init *pCommand) - { - if(!m_SystemInited) - { - int Systems = SDL_INIT_VIDEO; - - if(g_Config.m_SndEnable) // TODO: remove - Systems |= SDL_INIT_AUDIO; - - if(g_Config.m_ClEventthread) // TODO: remove - Systems |= SDL_INIT_EVENTTHREAD; - - if(SDL_Init(Systems) < 0) - { - dbg_msg("gfx", "unable to init SDL: %s", SDL_GetError()); - *pCommand->m_pResult = -1; - return; - } - - atexit(SDL_Quit); // ignore_convention - - #ifdef CONF_FAMILY_WINDOWS - if(!getenv("SDL_VIDEO_WINDOW_POS") && !getenv("SDL_VIDEO_CENTERED")) // ignore_convention - putenv("SDL_VIDEO_WINDOW_POS=8,27"); // ignore_convention - #endif - - m_SystemInited = true; - } - - const SDL_VideoInfo *pInfo = SDL_GetVideoInfo(); - SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE); - - // set flags - int Flags = SDL_OPENGL; - if(pCommand->m_Flags&CCommandBuffer::INITFLAG_RESIZABLE) - Flags |= SDL_RESIZABLE; - - if(pInfo->hw_available) // ignore_convention - Flags |= SDL_HWSURFACE; - else - Flags |= SDL_SWSURFACE; - - if(pInfo->blit_hw) // ignore_convention - Flags |= SDL_HWACCEL; - - if(pCommand->m_Flags&CCommandBuffer::INITFLAG_FULLSCREEN) - Flags |= SDL_FULLSCREEN; - - // set gl attributes - if(pCommand->m_FsaaSamples) - { - SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); - SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, pCommand->m_FsaaSamples); - } - else - { - SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0); - SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0); - } - - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, pCommand->m_Flags&CCommandBuffer::INITFLAG_VSYNC ? 1 : 0); - - // set caption - SDL_WM_SetCaption(pCommand->m_aName, pCommand->m_aName); - - // create window - m_pScreenSurface = SDL_SetVideoMode(pCommand->m_ScreenWidth, pCommand->m_ScreenHeight, 0, Flags); - if(m_pScreenSurface) - { - SDL_ShowCursor(0); - - // set some default settings - glEnable(GL_BLEND); - glDisable(GL_CULL_FACE); - glDisable(GL_DEPTH_TEST); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - glAlphaFunc(GL_GREATER, 0); - glEnable(GL_ALPHA_TEST); - glDepthMask(0); - - *pCommand->m_pResult = 0; - } - else - { - dbg_msg("gfx", "unable to set video mode: %s", SDL_GetError()); - *pCommand->m_pResult = -1; - } - } - - void Cmd_Shutdown(const CCommandBuffer::SCommand_Shutdown *pCommand) - { - SDL_Quit(); - } - - void Cmd_Swap(const CCommandBuffer::SCommand_Swap *pCommand) - { - SDL_GL_SwapBuffers(); - } - - void Cmd_VideoModes(const CCommandBuffer::SCommand_VideoModes *pCommand) - { - // TODO: fix this code on osx or windows - SDL_Rect **ppModes = SDL_ListModes(NULL, SDL_OPENGL|SDL_GL_DOUBLEBUFFER|SDL_FULLSCREEN); - if(ppModes == NULL) - { - // no modes - *pCommand->m_pNumModes = 0; - } - else if(ppModes == (SDL_Rect**)-1) - { - // no modes - *pCommand->m_pNumModes = 0; - } - else - { - int NumModes = 0; - for(int i = 0; ppModes[i]; ++i) - { - if(NumModes == pCommand->m_MaxModes) - break; - pCommand->m_pModes[NumModes].m_Width = ppModes[i]->w; - pCommand->m_pModes[NumModes].m_Height = ppModes[i]->h; - pCommand->m_pModes[NumModes].m_Red = 8; - pCommand->m_pModes[NumModes].m_Green = 8; - pCommand->m_pModes[NumModes].m_Blue = 8; - NumModes++; - } - - *pCommand->m_pNumModes = NumModes; - } - } - -public: - CCommandProcessorFragment_SDL() - { - m_SystemInited = false; - m_pScreenSurface = 0x0; - } - - bool RunCommand(const CCommandBuffer::SCommand *pBaseCommand) - { - switch(pBaseCommand->m_Cmd) - { - case CCommandBuffer::CMD_INIT: Cmd_Init(static_cast(pBaseCommand)); break; - case CCommandBuffer::CMD_SHUTDOWN: Cmd_Shutdown(static_cast(pBaseCommand)); break; - case CCommandBuffer::CMD_SWAP: Cmd_Swap(static_cast(pBaseCommand)); break; - case CCommandBuffer::CMD_VIDEOMODES: Cmd_VideoModes(static_cast(pBaseCommand)); break; - default: return false; - } - - return true; - } -}; - -class CCommandProcessor_SDL_OpenGL : public ICommandProcessor -{ - CCommandProcessorFragment_OpenGL m_OpenGL; - CCommandProcessorFragment_SDL m_SDL; - CCommandProcessorFragment_General m_General; - public: - virtual void RunBuffer(CCommandBuffer *pBuffer) - { - unsigned CmdIndex = 0; - while(1) - { - const CCommandBuffer::SCommand *pBaseCommand = pBuffer->GetCommand(&CmdIndex); - if(pBaseCommand == 0x0) - break; - - if(m_OpenGL.RunCommand(pBaseCommand)) - continue; - - if(m_SDL.RunCommand(pBaseCommand)) - continue; - - if(m_General.RunCommand(pBaseCommand)) - continue; - - dbg_msg("graphics", "unknown command %d", pBaseCommand->m_Cmd); - } - } -}; - -void CCommandProcessorHandler::ThreadFunc(void *pUser) -{ - CCommandProcessorHandler *pThis = (CCommandProcessorHandler *)pUser; - - while(!pThis->m_Shutdown) - { - pThis->m_Activity.wait(); - if(pThis->m_pBuffer) - { - pThis->m_pProcessor->RunBuffer(pThis->m_pBuffer); - sync_barrier(); - pThis->m_pBuffer = 0x0; - pThis->m_BufferDone.signal(); - } - } -} - -CCommandProcessorHandler::CCommandProcessorHandler() -{ - m_pBuffer = 0x0; - m_pProcessor = 0x0; - m_pThread = 0x0; -} - -void CCommandProcessorHandler::Start(ICommandProcessor *pProcessor) -{ - m_Shutdown = false; - m_pProcessor = pProcessor; - m_pThread = thread_create(ThreadFunc, this); - m_BufferDone.signal(); -} - -void CCommandProcessorHandler::Stop() -{ - m_Shutdown = true; - m_Activity.signal(); - thread_wait(m_pThread); - thread_destroy(m_pThread); -} - -void CCommandProcessorHandler::RunBuffer(CCommandBuffer *pBuffer) -{ - WaitForIdle(); - m_pBuffer = pBuffer; - m_Activity.signal(); -} - -void CCommandProcessorHandler::WaitForIdle() -{ - while(m_pBuffer != 0x0) - m_BufferDone.wait(); -} - void CGraphics_Threaded::FlushVertices() { if(m_NumVertices == 0) @@ -910,7 +443,7 @@ int CGraphics_Threaded::LoadPNG(CImageInfo *pImg, const char *pFilename, int Sto void CGraphics_Threaded::KickCommandBuffer() { - m_Handler.RunBuffer(m_pCommandBuffer); + m_pBackend->RunBuffer(m_pCommandBuffer); // swap buffer m_CurrentCommandBuffer ^= 1; @@ -1156,27 +689,12 @@ void CGraphics_Threaded::QuadsText(float x, float y, float Size, float r, float int CGraphics_Threaded::IssueInit() { - // issue init command - m_pCommandBuffer->Reset(); - - volatile int Result; - CCommandBuffer::SCommand_Init Cmd; - str_copy(Cmd.m_aName, "Teeworlds", sizeof(Cmd.m_aName)); - Cmd.m_pResult = &Result; - Cmd.m_ScreenWidth = g_Config.m_GfxScreenWidth; - Cmd.m_ScreenHeight = g_Config.m_GfxScreenHeight; - Cmd.m_FsaaSamples = g_Config.m_GfxFsaaSamples; - - Cmd.m_Flags = 0; - if(g_Config.m_GfxFullscreen) Cmd.m_Flags |= CCommandBuffer::INITFLAG_FULLSCREEN; - if(g_Config.m_GfxVsync) Cmd.m_Flags |= CCommandBuffer::INITFLAG_VSYNC; - if(g_Config.m_DbgResizable) Cmd.m_Flags |= CCommandBuffer::INITFLAG_RESIZABLE; + int Flags = 0; + if(g_Config.m_GfxFullscreen) Flags |= IGraphicsBackend::INITFLAG_FULLSCREEN; + if(g_Config.m_GfxVsync) Flags |= IGraphicsBackend::INITFLAG_VSYNC; + if(g_Config.m_DbgResizable) Flags |= IGraphicsBackend::INITFLAG_RESIZABLE; - m_pCommandBuffer->AddCommand(Cmd); - - m_Handler.RunBuffer(m_pCommandBuffer); - m_Handler.WaitForIdle(); - return Result; + return m_pBackend->Init("Teeworlds", g_Config.m_GfxScreenWidth, g_Config.m_GfxScreenHeight, g_Config.m_GfxFsaaSamples, Flags); } int CGraphics_Threaded::InitWindow() @@ -1214,7 +732,7 @@ int CGraphics_Threaded::InitWindow() return -1; } -bool CGraphics_Threaded::Init() +int CGraphics_Threaded::Init() { // fetch pointers m_pStorage = Kernel()->RequestInterface(); @@ -1230,23 +748,19 @@ bool CGraphics_Threaded::Init() m_aTextures[i].m_Next = i+1; m_aTextures[MAX_TEXTURES-1].m_Next = -1; - // start the command processor - m_pProcessor = new CCommandProcessor_SDL_OpenGL; - m_Handler.Start(m_pProcessor); + m_pBackend = CreateGraphicsBackend(); + if(InitWindow() != 0) + return -1; + + // fetch final resolusion + m_ScreenWidth = g_Config.m_GfxScreenWidth; + m_ScreenHeight = g_Config.m_GfxScreenHeight; // create command buffers m_apCommandBuffers[0] = new CCommandBuffer(1024*512, 1024*1024); m_apCommandBuffers[1] = new CCommandBuffer(1024*512, 1024*1024); m_pCommandBuffer = m_apCommandBuffers[0]; - // try to init the window - if(InitWindow() != 0) - return 0; - - // fetch final resolusion - m_ScreenWidth = g_Config.m_GfxScreenWidth; - m_ScreenHeight = g_Config.m_GfxScreenHeight; - // create null texture, will get id=0 static const unsigned char aNullTextureData[] = { 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0x00,0xff,0x00,0xff, 0x00,0xff,0x00,0xff, @@ -1261,36 +775,31 @@ bool CGraphics_Threaded::Init() void CGraphics_Threaded::Shutdown() { - // add swap command - CCommandBuffer::SCommand_Shutdown Cmd; - m_pCommandBuffer->AddCommand(Cmd); - m_Handler.RunBuffer(m_pCommandBuffer); - - // wait for everything to process and then stop the command processor - m_Handler.WaitForIdle(); - m_Handler.Stop(); - delete m_pProcessor; - m_pProcessor = 0; + // shutdown the backend + m_pBackend->Shutdown(); + delete m_pBackend; + m_pBackend = 0x0; } void CGraphics_Threaded::Minimize() { - SDL_WM_IconifyWindow(); + m_pBackend->Minimize(); } void CGraphics_Threaded::Maximize() { // TODO: SDL + m_pBackend->Maximize(); } int CGraphics_Threaded::WindowActive() { - return SDL_GetAppState()&SDL_APPINPUTFOCUS; + return m_pBackend->WindowActive(); } int CGraphics_Threaded::WindowOpen() { - return SDL_GetAppState()&SDL_APPACTIVE; + return m_pBackend->WindowOpen(); } @@ -1330,12 +839,12 @@ void CGraphics_Threaded::InsertSignal(semaphore *pSemaphore) bool CGraphics_Threaded::IsIdle() { - return m_Handler.IsIdle(); + return m_pBackend->IsIdle(); } void CGraphics_Threaded::WaitForIdle() { - m_Handler.WaitForIdle(); + m_pBackend->WaitForIdle(); } int CGraphics_Threaded::GetVideoModes(CVideoMode *pModes, int MaxModes) diff --git a/src/engine/client/graphics_threaded.h b/src/engine/client/graphics_threaded.h index 8b1c772d8..9f5f442d6 100644 --- a/src/engine/client/graphics_threaded.h +++ b/src/engine/client/graphics_threaded.h @@ -2,6 +2,8 @@ #include +#include + class CCommandBuffer { class CBuffer @@ -54,12 +56,12 @@ public: enum { - // - CMD_NOP = 0, + // commadn groups + CMDGROUP_CORE = 0, // commands that everyone has to implement + CMDGROUP_PLATFORM = 10000, // commands specific to a platform // - CMD_INIT, - CMD_SHUTDOWN, + CMD_NOP = CMDGROUP_CORE, // CMD_RUNBUFFER, @@ -82,6 +84,7 @@ public: // misc CMD_SCREENSHOT, CMD_VIDEOMODES, + }; enum @@ -155,25 +158,6 @@ public: SCommand_Clear() : SCommand(CMD_CLEAR) {} SColor m_Color; }; - - struct SCommand_Init : public SCommand - { - SCommand_Init() : SCommand(CMD_INIT) {} - - char m_aName[256]; - - int m_ScreenWidth; - int m_ScreenHeight; - int m_FsaaSamples; - int m_Flags; - - volatile int *m_pResult; - }; - - struct SCommand_Shutdown : public SCommand - { - SCommand_Shutdown() : SCommand(CMD_SHUTDOWN) {} - }; struct SCommand_Signal : public SCommand { @@ -269,10 +253,13 @@ public: template void AddCommand(const T &Command) { + // make sure that we don't do something stupid like ->AddCommand(&Cmd); + (void)static_cast(&Command); + + // allocate and copy the command into the buffer SCommand *pCmd = (SCommand *)m_CmdBuffer.Alloc(sizeof(Command)); if(!pCmd) return; - mem_copy(pCmd, &Command, sizeof(Command)); pCmd->m_Size = sizeof(Command); } @@ -294,40 +281,35 @@ public: } }; -class ICommandProcessor +// interface for the graphics backend +// all these functions are called on the main thread +class IGraphicsBackend { public: - virtual ~ICommandProcessor() {} + enum + { + INITFLAG_FULLSCREEN = 1, + INITFLAG_VSYNC = 2, + INITFLAG_RESIZABLE = 4, + }; + + virtual int Init(const char *pName, int Width, int Height, int FsaaSamples, int Flags) = 0; + virtual int Shutdown() = 0; + + virtual void Minimize() = 0; + virtual void Maximize() = 0; + virtual int WindowActive() = 0; + virtual int WindowOpen() = 0; + virtual void RunBuffer(CCommandBuffer *pBuffer) = 0; -}; - - -class CCommandProcessorHandler -{ - ICommandProcessor *m_pProcessor; - CCommandBuffer * volatile m_pBuffer; - volatile bool m_Shutdown; - semaphore m_Activity; - semaphore m_BufferDone; - void *m_pThread; - - static void ThreadFunc(void *pUser); - -public: - CCommandProcessorHandler(); - void Start(ICommandProcessor *pProcessor); - void Stop(); - - void RunBuffer(CCommandBuffer *pBuffer); - bool IsIdle() const { return m_pBuffer == 0; } - void WaitForIdle(); + virtual bool IsIdle() const = 0; + virtual void WaitForIdle() = 0; }; class CGraphics_Threaded : public IEngineGraphics { CCommandBuffer::SState m_State; - CCommandProcessorHandler m_Handler; - ICommandProcessor *m_pProcessor; + IGraphicsBackend *m_pBackend; CCommandBuffer *m_apCommandBuffers[2]; CCommandBuffer *m_pCommandBuffer; @@ -440,7 +422,7 @@ public: virtual int WindowActive(); virtual int WindowOpen(); - virtual bool Init(); + virtual int Init(); virtual void Shutdown(); virtual void TakeScreenshot(const char *pFilename); @@ -452,4 +434,6 @@ public: virtual void InsertSignal(semaphore *pSemaphore); virtual bool IsIdle(); virtual void WaitForIdle(); -}; \ No newline at end of file +}; + +extern IGraphicsBackend *CreateGraphicsBackend(); diff --git a/src/engine/client/sound.cpp b/src/engine/client/sound.cpp index 45404d18b..3f3d1a5d6 100644 --- a/src/engine/client/sound.cpp +++ b/src/engine/client/sound.cpp @@ -209,6 +209,12 @@ int CSound::Init() if(!g_Config.m_SndEnable) return 0; + if(SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) + { + dbg_msg("gfx", "unable to init SDL audio: %s", SDL_GetError()); + return -1; + } + m_MixingRate = g_Config.m_SndRate; // Set 16-bit stereo audio at 22Khz @@ -256,6 +262,7 @@ int CSound::Update() int CSound::Shutdown() { SDL_CloseAudio(); + SDL_QuitSubSystem(SDL_INIT_AUDIO); lock_destroy(m_SoundLock); return 0; } diff --git a/src/engine/graphics.h b/src/engine/graphics.h index 94d9c1a28..6d31060e6 100644 --- a/src/engine/graphics.h +++ b/src/engine/graphics.h @@ -146,7 +146,7 @@ class IEngineGraphics : public IGraphics { MACRO_INTERFACE("enginegraphics", 0) public: - virtual bool Init() = 0; + virtual int Init() = 0; virtual void Shutdown() = 0; virtual void Minimize() = 0; From 2991f4071e50cca641044376dc2196d9dcfff80d Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Tue, 3 Jan 2012 22:01:37 +0100 Subject: [PATCH 16/18] fixed kicking of command buffer if it's full when rendering. fixed compile error on windows --- src/engine/client/client.cpp | 6 +++-- src/engine/client/graphics_threaded.cpp | 20 ++++++++++++--- src/engine/client/graphics_threaded.h | 34 ++++++++++++++----------- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 5067f8b29..f126a09ce 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -49,6 +49,10 @@ #include #endif +#include "SDL.h" +#ifdef main +#undef main +#endif void CGraph::Init(float Min, float Max) { @@ -1690,8 +1694,6 @@ void CClient::InitInterfaces() m_Friends.Init(); } -#include "SDL.h" - void CClient::Run() { m_LocalStartTime = time_get(); diff --git a/src/engine/client/graphics_threaded.cpp b/src/engine/client/graphics_threaded.cpp index d1d09d380..33670f7bd 100644 --- a/src/engine/client/graphics_threaded.cpp +++ b/src/engine/client/graphics_threaded.cpp @@ -67,7 +67,17 @@ void CGraphics_Threaded::FlushVertices() Cmd.m_pVertices = (CCommandBuffer::SVertex *)m_pCommandBuffer->AllocData(sizeof(CCommandBuffer::SVertex)*NumVerts); if(Cmd.m_pVertices == 0x0) - return; + { + // kick command buffer and try again + KickCommandBuffer(); + + Cmd.m_pVertices = (CCommandBuffer::SVertex *)m_pCommandBuffer->AllocData(sizeof(CCommandBuffer::SVertex)*NumVerts); + if(Cmd.m_pVertices == 0x0) + { + dbg_msg("graphics", "failed to allocate data for vertices"); + return; + } + } mem_copy(Cmd.m_pVertices, m_aVertices, sizeof(CCommandBuffer::SVertex)*NumVerts); m_pCommandBuffer->AddCommand(Cmd); @@ -757,8 +767,8 @@ int CGraphics_Threaded::Init() m_ScreenHeight = g_Config.m_GfxScreenHeight; // create command buffers - m_apCommandBuffers[0] = new CCommandBuffer(1024*512, 1024*1024); - m_apCommandBuffers[1] = new CCommandBuffer(1024*512, 1024*1024); + for(int i = 0; i < NUM_CMDBUFFERS; i++) + m_apCommandBuffers[i] = new CCommandBuffer(128*1024, 2*1024*1024); m_pCommandBuffer = m_apCommandBuffers[0]; // create null texture, will get id=0 @@ -779,6 +789,10 @@ void CGraphics_Threaded::Shutdown() m_pBackend->Shutdown(); delete m_pBackend; m_pBackend = 0x0; + + // delete the command buffers + for(int i = 0; i < NUM_CMDBUFFERS; i++) + delete m_apCommandBuffers[i]; } void CGraphics_Threaded::Minimize() diff --git a/src/engine/client/graphics_threaded.h b/src/engine/client/graphics_threaded.h index 9f5f442d6..51148f0f2 100644 --- a/src/engine/client/graphics_threaded.h +++ b/src/engine/client/graphics_threaded.h @@ -46,9 +46,10 @@ class CCommandBuffer unsigned DataUsed() { return m_Used; } }; +public: CBuffer m_CmdBuffer; CBuffer m_DataBuffer; -public: + enum { MAX_TEXTURES=1024*4, @@ -251,7 +252,7 @@ public: } template - void AddCommand(const T &Command) + bool AddCommand(const T &Command) { // make sure that we don't do something stupid like ->AddCommand(&Cmd); (void)static_cast(&Command); @@ -259,9 +260,10 @@ public: // allocate and copy the command into the buffer SCommand *pCmd = (SCommand *)m_CmdBuffer.Alloc(sizeof(Command)); if(!pCmd) - return; + return false; mem_copy(pCmd, &Command, sizeof(Command)); pCmd->m_Size = sizeof(Command); + return true; } SCommand *GetCommand(unsigned *pIndex) @@ -278,7 +280,7 @@ public: { m_CmdBuffer.Reset(); m_DataBuffer.Reset(); - } + } }; // interface for the graphics backend @@ -308,19 +310,10 @@ public: class CGraphics_Threaded : public IEngineGraphics { - CCommandBuffer::SState m_State; - IGraphicsBackend *m_pBackend; - - CCommandBuffer *m_apCommandBuffers[2]; - CCommandBuffer *m_pCommandBuffer; - unsigned m_CurrentCommandBuffer; - - // - class IStorage *m_pStorage; - class IConsole *m_pConsole; - enum { + NUM_CMDBUFFERS = 2, + MAX_VERTICES = 32*1024, MAX_TEXTURES = 1024*4, @@ -328,6 +321,17 @@ class CGraphics_Threaded : public IEngineGraphics DRAWING_LINES=2 }; + CCommandBuffer::SState m_State; + IGraphicsBackend *m_pBackend; + + CCommandBuffer *m_apCommandBuffers[NUM_CMDBUFFERS]; + CCommandBuffer *m_pCommandBuffer; + unsigned m_CurrentCommandBuffer; + + // + class IStorage *m_pStorage; + class IConsole *m_pConsole; + CCommandBuffer::SVertex m_aVertices[MAX_VERTICES]; int m_NumVertices; From 666401e309e21b30285bc2545038a624c1c023eb Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Tue, 3 Jan 2012 22:49:31 +0100 Subject: [PATCH 17/18] added osx implementation. not tested, not even compiled once. hope it works --- src/engine/client/backend_sdl.h | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/engine/client/backend_sdl.h b/src/engine/client/backend_sdl.h index de0c6bd4a..ca3ae3e55 100644 --- a/src/engine/client/backend_sdl.h +++ b/src/engine/client/backend_sdl.h @@ -27,7 +27,26 @@ static void GL_ReleaseContext(const SGLContext &Context) { wglMakeCurrent(Context.m_hDC, NULL); } static void GL_SwapBuffers(const SGLContext &Context) { SwapBuffers(Context.m_hDC); } #elif defined(CONF_PLATFORM_MACOSX) - #error missing implementation + #warning Untested implementation. I have no Mac OS X machine to test on. Please test, verify, fix and then remove this warning + + struct SGLContext + { + AGLDrawable m_Drawable; + AGLContext m_Context; + }; + + static SGLContext GL_GetCurrentContext() + { + SGLContext Context; + Context.m_Drawable = aglGetCurrentDrawable(); + Context.m_Context = aglGetCurrentContext(); + return Context; + } + + static void GL_MakeCurrent(const SGLContext &Context) { aglMakeCurrent(Context.m_Drawable, Context.m_Context); } + static void GL_ReleaseContext(const SGLContext &Context) { aglMakeCurrent(Context.m_Drawable, NULL); } + static void GL_SwapBuffers(const SGLContext &Context) { aglSwapBuffers(Context.m_Drawable); } + #elif defined(CONF_FAMILY_UNIX) #include From 7393151b6d4cbfd8c4be29145344dcce22aa47d7 Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Tue, 3 Jan 2012 22:53:24 +0100 Subject: [PATCH 18/18] fixed the osx opengl make current thingie.. I think... --- src/engine/client/backend_sdl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/engine/client/backend_sdl.h b/src/engine/client/backend_sdl.h index ca3ae3e55..6e040510e 100644 --- a/src/engine/client/backend_sdl.h +++ b/src/engine/client/backend_sdl.h @@ -24,7 +24,7 @@ } static void GL_MakeCurrent(const SGLContext &Context) { wglMakeCurrent(Context.m_hDC, Context.m_hGLRC); } - static void GL_ReleaseContext(const SGLContext &Context) { wglMakeCurrent(Context.m_hDC, NULL); } + static void GL_ReleaseContext(const SGLContext &Context) { wglMakeCurrent(AGL_NONE, NULL); } static void GL_SwapBuffers(const SGLContext &Context) { SwapBuffers(Context.m_hDC); } #elif defined(CONF_PLATFORM_MACOSX) #warning Untested implementation. I have no Mac OS X machine to test on. Please test, verify, fix and then remove this warning