| 1 | // This file can be included several times. |
| 2 | #if (!defined(BACKEND_AS_OPENGL_ES) && !defined(ENGINE_CLIENT_BACKEND_OPENGL_BACKEND_OPENGL_H)) || \ |
| 3 | (defined(BACKEND_AS_OPENGL_ES) && !defined(ENGINE_CLIENT_BACKEND_OPENGL_BACKEND_OPENGL_H_AS_ES)) |
| 4 | |
| 5 | #if !defined(BACKEND_AS_OPENGL_ES) && !defined(ENGINE_CLIENT_BACKEND_OPENGL_BACKEND_OPENGL_H) |
| 6 | #define ENGINE_CLIENT_BACKEND_OPENGL_BACKEND_OPENGL_H |
| 7 | #endif |
| 8 | |
| 9 | #if defined(BACKEND_AS_OPENGL_ES) && !defined(ENGINE_CLIENT_BACKEND_OPENGL_BACKEND_OPENGL_H_AS_ES) |
| 10 | #define ENGINE_CLIENT_BACKEND_OPENGL_BACKEND_OPENGL_H_AS_ES |
| 11 | #endif |
| 12 | |
| 13 | #include <base/system.h> |
| 14 | |
| 15 | #include <engine/client/backend/backend_base.h> |
| 16 | #include <engine/client/graphics_defines.h> |
| 17 | |
| 18 | class CGLSLTWProgram; |
| 19 | class CGLSLPrimitiveProgram; |
| 20 | class CGLSLTileProgram; |
| 21 | |
| 22 | #if defined(BACKEND_AS_OPENGL_ES) && defined(CONF_BACKEND_OPENGL_ES3) |
| 23 | #define BACKEND_GL_MODERN_API 1 |
| 24 | #endif |
| 25 | |
| 26 | // takes care of opengl related rendering |
| 27 | class CCommandProcessorFragment_OpenGL : public CCommandProcessorFragment_GLBase |
| 28 | { |
| 29 | protected: |
| 30 | struct CTexture |
| 31 | { |
| 32 | CTexture() : |
| 33 | m_Tex(0), m_Tex2DArray(0), m_Sampler(0), m_Sampler2DArray(0), m_LastWrapMode(EWrapMode::REPEAT), m_MemSize(0), m_Width(0), m_Height(0), m_RescaleCount(0), m_ResizeWidth(0), m_ResizeHeight(0) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | TWGLuint m_Tex; |
| 38 | TWGLuint m_Tex2DArray; // or 3D texture as fallback |
| 39 | TWGLuint m_Sampler; |
| 40 | TWGLuint m_Sampler2DArray; // or 3D texture as fallback |
| 41 | EWrapMode m_LastWrapMode; |
| 42 | |
| 43 | int m_MemSize; |
| 44 | |
| 45 | int m_Width; |
| 46 | int m_Height; |
| 47 | int m_RescaleCount; |
| 48 | float m_ResizeWidth; |
| 49 | float m_ResizeHeight; |
| 50 | }; |
| 51 | std::vector<CTexture> m_vTextures; |
| 52 | std::atomic<uint64_t> *m_pTextureMemoryUsage; |
| 53 | |
| 54 | uint32_t m_CanvasWidth = 0; |
| 55 | uint32_t m_CanvasHeight = 0; |
| 56 | |
| 57 | TWGLint m_MaxTexSize; |
| 58 | |
| 59 | bool m_Has2DArrayTextures; |
| 60 | bool m_Has2DArrayTexturesAsExtension; |
| 61 | TWGLenum m_2DArrayTarget; |
| 62 | bool m_Has3DTextures; |
| 63 | bool m_HasMipMaps; |
| 64 | bool m_HasNPOTTextures; |
| 65 | |
| 66 | bool m_HasShaders; |
| 67 | EBlendMode m_LastBlendMode; // avoid all possible opengl state changes |
| 68 | bool m_LastClipEnable; |
| 69 | |
| 70 | int m_OpenGLTextureLodBIAS; |
| 71 | |
| 72 | bool m_IsOpenGLES; |
| 73 | |
| 74 | bool IsTexturedState(const CCommandBuffer::SState &State); |
| 75 | |
| 76 | bool InitOpenGL(const SCommand_Init *pCommand); |
| 77 | |
| 78 | void SetState(const CCommandBuffer::SState &State, bool Use2DArrayTexture = false); |
| 79 | virtual bool IsNewApi() { return false; } |
| 80 | void DestroyTexture(int Slot); |
| 81 | |
| 82 | bool GetPresentedImageData(uint32_t &Width, uint32_t &Height, CImageInfo::EImageFormat &Format, std::vector<uint8_t> &vDstData) override; |
| 83 | |
| 84 | static size_t GLFormatToPixelSize(int GLFormat); |
| 85 | |
| 86 | void TextureUpdate(int Slot, int X, int Y, int Width, int Height, int GLFormat, uint8_t *pTexData); |
| 87 | void TextureCreate(int Slot, int Width, int Height, int GLFormat, int GLStoreFormat, int Flags, uint8_t *pTexData); |
| 88 | |
| 89 | virtual bool Cmd_Init(const SCommand_Init *pCommand); |
| 90 | virtual void Cmd_Shutdown(const SCommand_Shutdown *pCommand) {} |
| 91 | virtual void Cmd_Texture_Destroy(const CCommandBuffer::SCommand_Texture_Destroy *pCommand); |
| 92 | virtual void Cmd_Texture_Create(const CCommandBuffer::SCommand_Texture_Create *pCommand); |
| 93 | virtual void Cmd_TextTexture_Update(const CCommandBuffer::SCommand_TextTexture_Update *pCommand); |
| 94 | virtual void Cmd_TextTextures_Destroy(const CCommandBuffer::SCommand_TextTextures_Destroy *pCommand); |
| 95 | virtual void Cmd_TextTextures_Create(const CCommandBuffer::SCommand_TextTextures_Create *pCommand); |
| 96 | virtual void Cmd_Clear(const CCommandBuffer::SCommand_Clear *pCommand); |
| 97 | virtual void Cmd_Render(const CCommandBuffer::SCommand_Render *pCommand); |
| 98 | virtual void Cmd_RenderTex3D(const CCommandBuffer::SCommand_RenderTex3D *pCommand) { dbg_assert_failed("Call of unsupported Cmd_RenderTex3D" ); } |
| 99 | virtual void Cmd_ReadPixel(const CCommandBuffer::SCommand_TrySwapAndReadPixel *pCommand); |
| 100 | virtual void Cmd_Screenshot(const CCommandBuffer::SCommand_TrySwapAndScreenshot *pCommand); |
| 101 | |
| 102 | virtual void Cmd_Update_Viewport(const CCommandBuffer::SCommand_Update_Viewport *pCommand); |
| 103 | |
| 104 | virtual void Cmd_CreateBufferObject(const CCommandBuffer::SCommand_CreateBufferObject *pCommand) { dbg_assert_failed("Call of unsupported Cmd_CreateBufferObject" ); } |
| 105 | virtual void Cmd_RecreateBufferObject(const CCommandBuffer::SCommand_RecreateBufferObject *pCommand) { dbg_assert_failed("Call of unsupported Cmd_RecreateBufferObject" ); } |
| 106 | virtual void Cmd_UpdateBufferObject(const CCommandBuffer::SCommand_UpdateBufferObject *pCommand) { dbg_assert_failed("Call of unsupported Cmd_UpdateBufferObject" ); } |
| 107 | virtual void Cmd_CopyBufferObject(const CCommandBuffer::SCommand_CopyBufferObject *pCommand) { dbg_assert_failed("Call of unsupported Cmd_CopyBufferObject" ); } |
| 108 | virtual void Cmd_DeleteBufferObject(const CCommandBuffer::SCommand_DeleteBufferObject *pCommand) { dbg_assert_failed("Call of unsupported Cmd_DeleteBufferObject" ); } |
| 109 | |
| 110 | virtual void Cmd_CreateBufferContainer(const CCommandBuffer::SCommand_CreateBufferContainer *pCommand) { dbg_assert_failed("Call of unsupported Cmd_CreateBufferContainer" ); } |
| 111 | virtual void Cmd_UpdateBufferContainer(const CCommandBuffer::SCommand_UpdateBufferContainer *pCommand) { dbg_assert_failed("Call of unsupported Cmd_UpdateBufferContainer" ); } |
| 112 | virtual void Cmd_DeleteBufferContainer(const CCommandBuffer::SCommand_DeleteBufferContainer *pCommand) { dbg_assert_failed("Call of unsupported Cmd_DeleteBufferContainer" ); } |
| 113 | virtual void Cmd_IndicesRequiredNumNotify(const CCommandBuffer::SCommand_IndicesRequiredNumNotify *pCommand) { dbg_assert_failed("Call of unsupported Cmd_IndicesRequiredNumNotify" ); } |
| 114 | |
| 115 | virtual void Cmd_RenderTileLayer(const CCommandBuffer::SCommand_RenderTileLayer *pCommand) { dbg_assert_failed("Call of unsupported Cmd_RenderTileLayer" ); } |
| 116 | virtual void Cmd_RenderBorderTile(const CCommandBuffer::SCommand_RenderBorderTile *pCommand) { dbg_assert_failed("Call of unsupported Cmd_RenderBorderTile" ); } |
| 117 | virtual void Cmd_RenderQuadLayer(const CCommandBuffer::SCommand_RenderQuadLayer *pCommand, bool Grouped) { dbg_assert_failed("Call of unsupported Cmd_RenderQuadLayer" ); } |
| 118 | virtual void Cmd_RenderText(const CCommandBuffer::SCommand_RenderText *pCommand) { dbg_assert_failed("Call of unsupported Cmd_RenderText" ); } |
| 119 | virtual void Cmd_RenderQuadContainer(const CCommandBuffer::SCommand_RenderQuadContainer *pCommand) { dbg_assert_failed("Call of unsupported Cmd_RenderQuadContainer" ); } |
| 120 | virtual void Cmd_RenderQuadContainerEx(const CCommandBuffer::SCommand_RenderQuadContainerEx *pCommand) { dbg_assert_failed("Call of unsupported Cmd_RenderQuadContainerEx" ); } |
| 121 | virtual void Cmd_RenderQuadContainerAsSpriteMultiple(const CCommandBuffer::SCommand_RenderQuadContainerAsSpriteMultiple *pCommand) { dbg_assert_failed("Call of unsupported Cmd_RenderQuadContainerAsSpriteMultiple" ); } |
| 122 | |
| 123 | public: |
| 124 | CCommandProcessorFragment_OpenGL(); |
| 125 | |
| 126 | ERunCommandReturnTypes RunCommand(const CCommandBuffer::SCommand *pBaseCommand) override; |
| 127 | }; |
| 128 | |
| 129 | class CCommandProcessorFragment_OpenGL2 : public CCommandProcessorFragment_OpenGL |
| 130 | { |
| 131 | struct SBufferContainer |
| 132 | { |
| 133 | SBufferContainerInfo m_ContainerInfo; |
| 134 | }; |
| 135 | std::vector<SBufferContainer> m_vBufferContainers; |
| 136 | |
| 137 | #ifndef BACKEND_AS_OPENGL_ES |
| 138 | GL_SVertexTex3D m_aStreamVertices[1024 * 4]; |
| 139 | #endif |
| 140 | |
| 141 | struct SBufferObject |
| 142 | { |
| 143 | SBufferObject(TWGLuint BufferObjectId) : |
| 144 | m_BufferObjectId(BufferObjectId) |
| 145 | { |
| 146 | m_pData = NULL; |
| 147 | m_DataSize = 0; |
| 148 | } |
| 149 | TWGLuint m_BufferObjectId; |
| 150 | uint8_t *m_pData; |
| 151 | size_t m_DataSize; |
| 152 | }; |
| 153 | |
| 154 | std::vector<SBufferObject> m_vBufferObjectIndices; |
| 155 | |
| 156 | #ifndef BACKEND_GL_MODERN_API |
| 157 | bool DoAnalyzeStep(size_t CheckCount, size_t VerticesCount, uint8_t aFakeTexture[], size_t SingleImageSize); |
| 158 | bool IsTileMapAnalysisSucceeded(); |
| 159 | #endif |
| 160 | |
| 161 | void UseProgram(CGLSLTWProgram *pProgram); |
| 162 | |
| 163 | protected: |
| 164 | void SetState(const CCommandBuffer::SState &State, CGLSLTWProgram *pProgram, bool Use2DArrayTextures = false); |
| 165 | |
| 166 | #ifndef BACKEND_GL_MODERN_API |
| 167 | bool Cmd_Init(const SCommand_Init *pCommand) override; |
| 168 | void Cmd_Shutdown(const SCommand_Shutdown *pCommand) override; |
| 169 | |
| 170 | void Cmd_RenderTex3D(const CCommandBuffer::SCommand_RenderTex3D *pCommand) override; |
| 171 | |
| 172 | void Cmd_CreateBufferObject(const CCommandBuffer::SCommand_CreateBufferObject *pCommand) override; |
| 173 | void Cmd_RecreateBufferObject(const CCommandBuffer::SCommand_RecreateBufferObject *pCommand) override; |
| 174 | void Cmd_UpdateBufferObject(const CCommandBuffer::SCommand_UpdateBufferObject *pCommand) override; |
| 175 | void Cmd_CopyBufferObject(const CCommandBuffer::SCommand_CopyBufferObject *pCommand) override; |
| 176 | void Cmd_DeleteBufferObject(const CCommandBuffer::SCommand_DeleteBufferObject *pCommand) override; |
| 177 | |
| 178 | void Cmd_CreateBufferContainer(const CCommandBuffer::SCommand_CreateBufferContainer *pCommand) override; |
| 179 | void Cmd_UpdateBufferContainer(const CCommandBuffer::SCommand_UpdateBufferContainer *pCommand) override; |
| 180 | void Cmd_DeleteBufferContainer(const CCommandBuffer::SCommand_DeleteBufferContainer *pCommand) override; |
| 181 | void Cmd_IndicesRequiredNumNotify(const CCommandBuffer::SCommand_IndicesRequiredNumNotify *pCommand) override; |
| 182 | |
| 183 | void Cmd_RenderTileLayer(const CCommandBuffer::SCommand_RenderTileLayer *pCommand) override; |
| 184 | void Cmd_RenderBorderTile(const CCommandBuffer::SCommand_RenderBorderTile *pCommand) override; |
| 185 | #endif |
| 186 | |
| 187 | CGLSLTileProgram *m_pTileProgram; |
| 188 | CGLSLTileProgram *m_pTileProgramTextured; |
| 189 | CGLSLTileProgram *m_pBorderTileProgram; |
| 190 | CGLSLTileProgram *m_pBorderTileProgramTextured; |
| 191 | CGLSLPrimitiveProgram *m_pPrimitive3DProgram; |
| 192 | CGLSLPrimitiveProgram *m_pPrimitive3DProgramTextured; |
| 193 | }; |
| 194 | |
| 195 | class CCommandProcessorFragment_OpenGL3 : public CCommandProcessorFragment_OpenGL2 |
| 196 | { |
| 197 | }; |
| 198 | |
| 199 | #if defined(BACKEND_AS_OPENGL_ES) && defined(CONF_BACKEND_OPENGL_ES3) |
| 200 | #undef BACKEND_GL_MODERN_API |
| 201 | #endif |
| 202 | |
| 203 | #endif |
| 204 | |