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