1#include "backend_opengl.h"
2
3#include <base/dbg.h>
4#include <base/detect.h>
5#include <base/log.h>
6#include <base/mem.h>
7#include <base/str.h>
8
9#include <engine/client/backend_sdl.h>
10#include <engine/graphics.h>
11
12#include <cstdint>
13
14#if defined(BACKEND_AS_OPENGL_ES) || !defined(CONF_BACKEND_OPENGL_ES)
15
16#include <engine/client/backend/glsl_shader_compiler.h>
17#include <engine/client/backend/opengl/opengl_sl.h>
18#include <engine/client/backend/opengl/opengl_sl_program.h>
19#include <engine/client/blocklist_driver.h>
20#include <engine/gfx/image_manipulation.h>
21
22#ifndef BACKEND_AS_OPENGL_ES
23#include <GL/glew.h>
24#else
25#include <GLES3/gl3.h>
26#define GL_TEXTURE_2D_ARRAY_EXT GL_TEXTURE_2D_ARRAY
27// GLES doesn't support GL_QUADS, but the code is also never executed
28#define GL_QUADS GL_TRIANGLES
29#ifndef CONF_BACKEND_OPENGL_ES3
30#include <GLES/gl.h>
31#define glOrtho glOrthof
32#else
33#define BACKEND_GL_MODERN_API 1
34#endif
35#endif
36
37// ------------ CCommandProcessorFragment_OpenGL
38void CCommandProcessorFragment_OpenGL::Cmd_Update_Viewport(const CCommandBuffer::SCommand_Update_Viewport *pCommand)
39{
40 if(pCommand->m_ByResize)
41 {
42 m_CanvasWidth = (uint32_t)pCommand->m_Width;
43 m_CanvasHeight = (uint32_t)pCommand->m_Height;
44 }
45 glViewport(x: pCommand->m_X, y: pCommand->m_Y, width: pCommand->m_Width, height: pCommand->m_Height);
46}
47
48size_t CCommandProcessorFragment_OpenGL::GLFormatToPixelSize(int GLFormat)
49{
50 switch(GLFormat)
51 {
52 case GL_RGBA: return 4;
53 case GL_RGB: return 3;
54 case GL_RED: return 1;
55 case GL_ALPHA: return 1;
56 default: return 4;
57 }
58}
59
60bool CCommandProcessorFragment_OpenGL::IsTexturedState(const CCommandBuffer::SState &State)
61{
62 return State.m_Texture >= 0 && State.m_Texture < (int)m_vTextures.size();
63}
64
65void CCommandProcessorFragment_OpenGL::SetState(const CCommandBuffer::SState &State, bool Use2DArrayTextures)
66{
67#ifndef BACKEND_GL_MODERN_API
68 // blend
69 switch(State.m_BlendMode)
70 {
71 case EBlendMode::NONE:
72 glDisable(GL_BLEND);
73 break;
74 case EBlendMode::ALPHA:
75 glEnable(GL_BLEND);
76 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
77 break;
78 case EBlendMode::ADDITIVE:
79 glEnable(GL_BLEND);
80 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
81 break;
82 default:
83 dbg_assert_failed("Invalid blend mode: %d", (int)State.m_BlendMode);
84 };
85 m_LastBlendMode = State.m_BlendMode;
86
87 // clip
88 if(State.m_ClipEnable)
89 {
90 glScissor(x: State.m_ClipX, y: State.m_ClipY, width: State.m_ClipW, height: State.m_ClipH);
91 glEnable(GL_SCISSOR_TEST);
92 m_LastClipEnable = true;
93 }
94 else if(m_LastClipEnable)
95 {
96 // Don't disable it always
97 glDisable(GL_SCISSOR_TEST);
98 m_LastClipEnable = false;
99 }
100
101 glDisable(GL_TEXTURE_2D);
102 if(!m_HasShaders)
103 {
104 if(m_Has3DTextures)
105 glDisable(GL_TEXTURE_3D);
106 if(m_Has2DArrayTextures)
107 {
108 glDisable(cap: m_2DArrayTarget);
109 }
110 }
111
112 if(m_HasShaders && IsNewApi())
113 {
114 glBindSampler(0, 0);
115 }
116
117 // texture
118 if(IsTexturedState(State))
119 {
120 if(!Use2DArrayTextures)
121 {
122 glEnable(GL_TEXTURE_2D);
123 glBindTexture(GL_TEXTURE_2D, texture: m_vTextures[State.m_Texture].m_Tex);
124
125 if(m_vTextures[State.m_Texture].m_LastWrapMode != State.m_WrapMode)
126 {
127 switch(State.m_WrapMode)
128 {
129 case EWrapMode::REPEAT:
130 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
131 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
132 break;
133 case EWrapMode::CLAMP:
134 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
135 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
136 break;
137 default:
138 dbg_assert_failed("Invalid wrap mode: %d", (int)State.m_WrapMode);
139 };
140 m_vTextures[State.m_Texture].m_LastWrapMode = State.m_WrapMode;
141 }
142 }
143 else if(m_Has2DArrayTextures)
144 {
145 if(!m_HasShaders)
146 glEnable(cap: m_2DArrayTarget);
147 glBindTexture(target: m_2DArrayTarget, texture: m_vTextures[State.m_Texture].m_Tex2DArray);
148 }
149 else if(m_Has3DTextures)
150 {
151 if(!m_HasShaders)
152 glEnable(GL_TEXTURE_3D);
153 glBindTexture(GL_TEXTURE_3D, texture: m_vTextures[State.m_Texture].m_Tex2DArray);
154 }
155 else
156 {
157 dbg_assert_failed("Should have either 2D, 3D or no texture array support");
158 }
159 }
160
161 // screen mapping
162 glMatrixMode(GL_PROJECTION);
163 glLoadIdentity();
164 glOrtho(left: State.m_ScreenTL.x, right: State.m_ScreenBR.x, bottom: State.m_ScreenBR.y, top: State.m_ScreenTL.y, zNear: -10.0f, zFar: 10.f);
165#endif
166}
167
168static void ParseVersionString(EBackendType BackendType, const char *pStr, int &VersionMajor, int &VersionMinor, int &VersionPatch)
169{
170 // If the backend is GLES, the version string starts with `OpenGL ES ` or `OpenGL ES-CM ` for older contexts, rest is the same.
171 if(BackendType == BACKEND_TYPE_OPENGL_ES)
172 {
173 const char *pSkippedPrefix;
174 if((pSkippedPrefix = str_startswith(str: pStr, prefix: "OpenGL ES ")) != nullptr ||
175 (pSkippedPrefix = str_startswith(str: pStr, prefix: "OpenGL ES-CM ")) != nullptr)
176 {
177 pStr = pSkippedPrefix;
178 }
179 }
180
181 char aCurNumberStr[10];
182 size_t CurNumberStrLen = 0;
183 size_t TotalNumbersPassed = 0;
184 int aNumbers[3] = {0};
185 bool LastWasNumber = false;
186 bool Error = false;
187 while(true)
188 {
189 if(str_isnum(c: *pStr))
190 {
191 if(CurNumberStrLen >= std::size(aCurNumberStr) - 1)
192 {
193 Error = true;
194 break;
195 }
196 aCurNumberStr[CurNumberStrLen++] = *pStr;
197 LastWasNumber = true;
198 }
199 else if(LastWasNumber && (*pStr == '.' || *pStr == ' ' || *pStr == '\0'))
200 {
201 aCurNumberStr[CurNumberStrLen] = '\0';
202 aNumbers[TotalNumbersPassed] = str_toint(str: aCurNumberStr);
203 CurNumberStrLen = 0;
204 TotalNumbersPassed++;
205 LastWasNumber = false;
206 if(TotalNumbersPassed == std::size(aNumbers) || *pStr != '.')
207 {
208 break;
209 }
210 }
211 else
212 {
213 break;
214 }
215 ++pStr;
216 }
217
218 if(Error || TotalNumbersPassed == 0)
219 {
220 // Use the newest supported OpenGL version if the version string could not be parsed.
221 // We assume that the format was changed in a future driver that supports all OpenGL
222 // capabilities that we use.
223 VersionMajor = 3;
224 VersionMinor = BackendType == BACKEND_TYPE_OPENGL_ES ? 0 : 3;
225 VersionPatch = 0;
226 }
227 else
228 {
229 VersionMajor = aNumbers[0];
230 VersionMinor = aNumbers[1];
231 VersionPatch = aNumbers[2];
232 }
233}
234
235#ifndef BACKEND_AS_OPENGL_ES
236static LEVEL GetLogSeverity(GLenum Severity)
237{
238 switch(Severity)
239 {
240 case GL_DEBUG_SEVERITY_HIGH: return LEVEL_ERROR;
241 case GL_DEBUG_SEVERITY_MEDIUM: return LEVEL_WARN;
242 case GL_DEBUG_SEVERITY_LOW: return LEVEL_INFO;
243 case GL_DEBUG_SEVERITY_NOTIFICATION: return LEVEL_DEBUG;
244 default: dbg_assert_failed("Severity invalid: %d", (int)Severity);
245 }
246}
247
248static const char *GetErrorName(GLenum Type)
249{
250 switch(Type)
251 {
252 case GL_DEBUG_TYPE_ERROR: return "ERROR";
253 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: return "DEPRECATED BEHAVIOR";
254 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: return "UNDEFINED BEHAVIOR";
255 case GL_DEBUG_TYPE_PORTABILITY: return "PORTABILITY";
256 case GL_DEBUG_TYPE_PERFORMANCE: return "PERFORMANCE";
257 case GL_DEBUG_TYPE_OTHER: return "OTHER";
258 case GL_DEBUG_TYPE_MARKER: return "MARKER";
259 case GL_DEBUG_TYPE_PUSH_GROUP: return "PUSH_GROUP";
260 case GL_DEBUG_TYPE_POP_GROUP: return "POP_GROUP";
261 default: return "UNKNOWN";
262 }
263}
264
265static const char *GetSeverityString(GLenum Severity)
266{
267 switch(Severity)
268 {
269 // All OpenGL Errors, shader compilation/linking errors, or highly-dangerous undefined behavior
270 case GL_DEBUG_SEVERITY_HIGH: return "high";
271 // Major performance warnings, shader compilation/linking warnings, or the use of deprecated functionality
272 case GL_DEBUG_SEVERITY_MEDIUM: return "medium";
273 // Redundant state change performance warning, or unimportant undefined behavior
274 case GL_DEBUG_SEVERITY_LOW: return "low";
275 // Anything that isn't an error or performance issue.
276 case GL_DEBUG_SEVERITY_NOTIFICATION: return "notification";
277 default: dbg_assert_failed("Severity invalid: %d", (int)Severity);
278 }
279}
280
281static void GLAPIENTRY
282GfxOpenGLMessageCallback(GLenum Source,
283 GLenum Type,
284 GLuint Id,
285 GLenum Severity,
286 GLsizei Length,
287 const GLchar *pMsg,
288 const void *pUserParam)
289{
290 log_log(level: GetLogSeverity(Severity), sys: "gfx/opengl", fmt: "[%s] (importance: %s) %s", GetErrorName(Type), GetSeverityString(Severity), pMsg);
291}
292#endif
293
294bool CCommandProcessorFragment_OpenGL::GetPresentedImageData(uint32_t &Width, uint32_t &Height, CImageInfo::EImageFormat &Format, std::vector<uint8_t> &vDstData)
295{
296 if(m_CanvasWidth == 0 || m_CanvasHeight == 0)
297 {
298 return false;
299 }
300 else
301 {
302 Width = m_CanvasWidth;
303 Height = m_CanvasHeight;
304 Format = CImageInfo::FORMAT_RGBA;
305 vDstData.resize(sz: (size_t)Width * (Height + 1) * 4); // +1 for flipping image
306 glReadBuffer(GL_FRONT);
307 GLint Alignment;
308 glGetIntegerv(GL_PACK_ALIGNMENT, params: &Alignment);
309 glPixelStorei(GL_PACK_ALIGNMENT, param: 1);
310 glReadPixels(x: 0, y: 0, width: m_CanvasWidth, height: m_CanvasHeight, GL_RGBA, GL_UNSIGNED_BYTE, pixels: vDstData.data());
311 glPixelStorei(GL_PACK_ALIGNMENT, param: Alignment);
312
313 uint8_t *pTempRow = vDstData.data() + Width * Height * 4;
314 for(uint32_t Y = 0; Y < Height / 2; ++Y)
315 {
316 mem_copy(dest: pTempRow, source: vDstData.data() + Y * Width * 4, size: Width * 4);
317 mem_copy(dest: vDstData.data() + Y * Width * 4, source: vDstData.data() + ((Height - Y) - 1) * Width * 4, size: Width * 4);
318 mem_copy(dest: vDstData.data() + ((Height - Y) - 1) * Width * 4, source: pTempRow, size: Width * 4);
319 }
320
321 return true;
322 }
323}
324
325bool CCommandProcessorFragment_OpenGL::InitOpenGL(const SCommand_Init *pCommand)
326{
327 m_IsOpenGLES = pCommand->m_RequestedBackend == BACKEND_TYPE_OPENGL_ES;
328
329 *pCommand->m_pReadPresentedImageDataFunc = [this](uint32_t &Width, uint32_t &Height, CImageInfo::EImageFormat &Format, std::vector<uint8_t> &vDstData) {
330 return GetPresentedImageData(Width, Height, Format, vDstData);
331 };
332
333 const char *pVendorString = (const char *)glGetString(GL_VENDOR);
334 dbg_assert(pVendorString != nullptr, "glGetString(GL_VENDOR) failure");
335 log_info("gfx/opengl", "Vendor string: %s", pVendorString);
336
337 // check what this context can do
338 const char *pVersionString = (const char *)glGetString(GL_VERSION);
339 dbg_assert(pVersionString != nullptr, "glGetString(GL_VERSION) failure");
340 log_info("gfx/opengl", "Version string: %s", pVersionString);
341
342 const char *pRendererString = (const char *)glGetString(GL_RENDERER);
343 dbg_assert(pRendererString != nullptr, "glGetString(GL_RENDERER) failure");
344
345 str_copy(dst: pCommand->m_pVendorString, src: pVendorString, dst_size: GPU_INFO_STRING_SIZE);
346 str_copy(dst: pCommand->m_pVersionString, src: pVersionString, dst_size: GPU_INFO_STRING_SIZE);
347 str_copy(dst: pCommand->m_pRendererString, src: pRendererString, dst_size: GPU_INFO_STRING_SIZE);
348
349 // parse version string
350 ParseVersionString(BackendType: pCommand->m_RequestedBackend, pStr: pVersionString, VersionMajor&: pCommand->m_pCapabilities->m_ContextMajor, VersionMinor&: pCommand->m_pCapabilities->m_ContextMinor, VersionPatch&: pCommand->m_pCapabilities->m_ContextPatch);
351
352 *pCommand->m_pInitError = 0;
353
354 int BlocklistMajor = -1, BlocklistMinor = -1, BlocklistPatch = -1;
355 bool RequiresWarning = false;
356 const char *pErrString = ParseBlocklistDriverVersions(pVendorStr: pVendorString, pVersionStr: pVersionString, BlocklistMajor, BlocklistMinor, BlocklistPatch, RequiresWarning);
357 // if the driver is buggy, and the requested GL version is the default, fallback
358 if(pErrString != nullptr && pCommand->m_RequestedMajor == 3 && pCommand->m_RequestedMinor == 0 && pCommand->m_RequestedPatch == 0)
359 {
360 // if not already in the error state, set the GL version
361 if(g_Config.m_GfxDriverIsBlocked == 0)
362 {
363 // fallback to known good GL version
364 pCommand->m_pCapabilities->m_ContextMajor = BlocklistMajor;
365 pCommand->m_pCapabilities->m_ContextMinor = BlocklistMinor;
366 pCommand->m_pCapabilities->m_ContextPatch = BlocklistPatch;
367
368 // set backend error string
369 if(RequiresWarning)
370 *pCommand->m_pErrStringPtr = pErrString;
371 *pCommand->m_pInitError = -2;
372
373 g_Config.m_GfxDriverIsBlocked = 1;
374 }
375 }
376 // if the driver was in a blocked error state, but is not anymore, reset all config variables
377 else if(pErrString == nullptr && g_Config.m_GfxDriverIsBlocked == 1)
378 {
379 pCommand->m_pCapabilities->m_ContextMajor = 3;
380 pCommand->m_pCapabilities->m_ContextMinor = 0;
381 pCommand->m_pCapabilities->m_ContextPatch = 0;
382
383 // tell the caller to reinitialize the context
384 *pCommand->m_pInitError = -2;
385
386 g_Config.m_GfxDriverIsBlocked = 0;
387 }
388
389 int MajorV = pCommand->m_pCapabilities->m_ContextMajor;
390
391 if(pCommand->m_RequestedBackend == BACKEND_TYPE_OPENGL)
392 {
393#ifndef BACKEND_AS_OPENGL_ES
394 int MinorV = pCommand->m_pCapabilities->m_ContextMinor;
395 if(*pCommand->m_pInitError == 0)
396 {
397 if(MajorV < pCommand->m_RequestedMajor)
398 {
399 *pCommand->m_pInitError = -2;
400 }
401 else if(MajorV == pCommand->m_RequestedMajor)
402 {
403 if(MinorV < pCommand->m_RequestedMinor)
404 {
405 *pCommand->m_pInitError = -2;
406 }
407 else if(MinorV == pCommand->m_RequestedMinor)
408 {
409 int PatchV = pCommand->m_pCapabilities->m_ContextPatch;
410 if(PatchV < pCommand->m_RequestedPatch)
411 {
412 *pCommand->m_pInitError = -2;
413 }
414 }
415 }
416 }
417
418 if(*pCommand->m_pInitError == 0)
419 {
420 MajorV = pCommand->m_RequestedMajor;
421 MinorV = pCommand->m_RequestedMinor;
422
423 pCommand->m_pCapabilities->m_2DArrayTexturesAsExtension = false;
424 pCommand->m_pCapabilities->m_NPOTTextures = true;
425 pCommand->m_pCapabilities->m_TrianglesAsQuads = false;
426
427 if(MajorV >= 4 || (MajorV == 3 && MinorV == 3))
428 {
429 pCommand->m_pCapabilities->m_TileBuffering = true;
430 pCommand->m_pCapabilities->m_QuadBuffering = true;
431 pCommand->m_pCapabilities->m_TextBuffering = true;
432 pCommand->m_pCapabilities->m_QuadContainerBuffering = true;
433 pCommand->m_pCapabilities->m_ShaderSupport = true;
434
435 pCommand->m_pCapabilities->m_MipMapping = true;
436 pCommand->m_pCapabilities->m_3DTextures = true;
437 pCommand->m_pCapabilities->m_2DArrayTextures = true;
438
439 pCommand->m_pCapabilities->m_TrianglesAsQuads = true;
440 }
441 else if(MajorV == 3)
442 {
443 pCommand->m_pCapabilities->m_MipMapping = true;
444 // check for context native 2D array texture size
445 pCommand->m_pCapabilities->m_3DTextures = false;
446 pCommand->m_pCapabilities->m_2DArrayTextures = false;
447 pCommand->m_pCapabilities->m_ShaderSupport = true;
448
449 int TextureLayers = 0;
450 glGetIntegerv(GL_MAX_ARRAY_TEXTURE_LAYERS, params: &TextureLayers);
451 if(TextureLayers >= 256)
452 {
453 pCommand->m_pCapabilities->m_2DArrayTextures = true;
454 }
455
456 int Texture3DSize = 0;
457 glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, params: &Texture3DSize);
458 if(Texture3DSize >= 256)
459 {
460 pCommand->m_pCapabilities->m_3DTextures = true;
461 }
462
463 if(!pCommand->m_pCapabilities->m_3DTextures && !pCommand->m_pCapabilities->m_2DArrayTextures)
464 {
465 *pCommand->m_pInitError = -2;
466 pCommand->m_pCapabilities->m_ContextMajor = 1;
467 pCommand->m_pCapabilities->m_ContextMinor = 5;
468 pCommand->m_pCapabilities->m_ContextPatch = 0;
469 }
470
471 pCommand->m_pCapabilities->m_TileBuffering = pCommand->m_pCapabilities->m_2DArrayTextures;
472 pCommand->m_pCapabilities->m_QuadBuffering = false;
473 pCommand->m_pCapabilities->m_TextBuffering = false;
474 pCommand->m_pCapabilities->m_QuadContainerBuffering = false;
475 }
476 else if(MajorV == 2)
477 {
478 pCommand->m_pCapabilities->m_MipMapping = true;
479 // check for context extension: 2D array texture and its max size
480 pCommand->m_pCapabilities->m_3DTextures = false;
481 pCommand->m_pCapabilities->m_2DArrayTextures = false;
482
483 pCommand->m_pCapabilities->m_ShaderSupport = false;
484
485 int Texture3DSize = 0;
486 glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, params: &Texture3DSize);
487 if(Texture3DSize >= 256)
488 {
489 pCommand->m_pCapabilities->m_3DTextures = true;
490 }
491
492 pCommand->m_pCapabilities->m_TileBuffering = false;
493 pCommand->m_pCapabilities->m_QuadBuffering = false;
494 pCommand->m_pCapabilities->m_TextBuffering = false;
495 pCommand->m_pCapabilities->m_QuadContainerBuffering = false;
496
497 pCommand->m_pCapabilities->m_NPOTTextures = GLEW_ARB_texture_non_power_of_two || pCommand->m_GlewMajor > 2;
498
499 if(!pCommand->m_pCapabilities->m_NPOTTextures || (!pCommand->m_pCapabilities->m_3DTextures && !pCommand->m_pCapabilities->m_2DArrayTextures))
500 {
501 *pCommand->m_pInitError = -2;
502 pCommand->m_pCapabilities->m_ContextMajor = 1;
503 pCommand->m_pCapabilities->m_ContextMinor = 5;
504 pCommand->m_pCapabilities->m_ContextPatch = 0;
505 }
506 }
507 else if(MajorV < 2)
508 {
509 pCommand->m_pCapabilities->m_TileBuffering = false;
510 pCommand->m_pCapabilities->m_QuadBuffering = false;
511 pCommand->m_pCapabilities->m_TextBuffering = false;
512 pCommand->m_pCapabilities->m_QuadContainerBuffering = false;
513 pCommand->m_pCapabilities->m_ShaderSupport = false;
514
515 pCommand->m_pCapabilities->m_MipMapping = false;
516 pCommand->m_pCapabilities->m_3DTextures = false;
517 pCommand->m_pCapabilities->m_2DArrayTextures = false;
518 pCommand->m_pCapabilities->m_NPOTTextures = false;
519 }
520 }
521#endif
522 }
523 else if(pCommand->m_RequestedBackend == BACKEND_TYPE_OPENGL_ES)
524 {
525 if(MajorV < 3)
526 {
527 pCommand->m_pCapabilities->m_TileBuffering = false;
528 pCommand->m_pCapabilities->m_QuadBuffering = false;
529 pCommand->m_pCapabilities->m_TextBuffering = false;
530 pCommand->m_pCapabilities->m_QuadContainerBuffering = false;
531 pCommand->m_pCapabilities->m_ShaderSupport = false;
532
533 pCommand->m_pCapabilities->m_MipMapping = false;
534 pCommand->m_pCapabilities->m_3DTextures = false;
535 pCommand->m_pCapabilities->m_2DArrayTextures = false;
536 pCommand->m_pCapabilities->m_NPOTTextures = false;
537
538 pCommand->m_pCapabilities->m_TrianglesAsQuads = false;
539 }
540 else
541 {
542 pCommand->m_pCapabilities->m_TileBuffering = true;
543 pCommand->m_pCapabilities->m_QuadBuffering = true;
544 pCommand->m_pCapabilities->m_TextBuffering = true;
545 pCommand->m_pCapabilities->m_QuadContainerBuffering = true;
546 pCommand->m_pCapabilities->m_ShaderSupport = true;
547
548 pCommand->m_pCapabilities->m_MipMapping = true;
549 pCommand->m_pCapabilities->m_3DTextures = true;
550 pCommand->m_pCapabilities->m_2DArrayTextures = true;
551 pCommand->m_pCapabilities->m_NPOTTextures = true;
552
553 pCommand->m_pCapabilities->m_TrianglesAsQuads = true;
554 }
555 }
556
557 if(*pCommand->m_pInitError != -2)
558 {
559 // set some default settings
560 glEnable(GL_BLEND);
561 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
562 glDisable(GL_CULL_FACE);
563 glDisable(GL_DEPTH_TEST);
564
565#ifndef BACKEND_GL_MODERN_API
566 if(!IsNewApi())
567 {
568 glAlphaFunc(GL_GREATER, ref: 0);
569 glEnable(GL_ALPHA_TEST);
570 }
571#endif
572
573 glDepthMask(flag: 0);
574
575#ifndef BACKEND_AS_OPENGL_ES
576 if(g_Config.m_DbgGfx != DEBUG_GFX_MODE_NONE)
577 {
578 if(GLEW_KHR_debug || GLEW_ARB_debug_output)
579 {
580 // During init, enable debug output
581 if(GLEW_KHR_debug)
582 {
583 glEnable(GL_DEBUG_OUTPUT);
584 glDebugMessageCallback((GLDEBUGPROC)GfxOpenGLMessageCallback, nullptr);
585 }
586 else if(GLEW_ARB_debug_output)
587 {
588 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
589 glDebugMessageCallbackARB((GLDEBUGPROC)GfxOpenGLMessageCallback, nullptr);
590 }
591 log_info("gfx/opengl", "Enabled OpenGL debug mode");
592 }
593 else
594 {
595 log_warn("gfx/opengl", "Requested OpenGL debug mode, but the driver does not support the required extension");
596 }
597 }
598#endif
599
600 return true;
601 }
602 else
603 return false;
604}
605
606bool CCommandProcessorFragment_OpenGL::Cmd_Init(const SCommand_Init *pCommand)
607{
608 if(!InitOpenGL(pCommand))
609 return false;
610
611 m_pTextureMemoryUsage = pCommand->m_pTextureMemoryUsage;
612 m_pTextureMemoryUsage->store(i: 0, m: std::memory_order_relaxed);
613 m_MaxTexSize = -1;
614
615 m_OpenGLTextureLodBIAS = 0;
616
617 m_Has2DArrayTextures = pCommand->m_pCapabilities->m_2DArrayTextures;
618 if(pCommand->m_pCapabilities->m_2DArrayTexturesAsExtension)
619 {
620 m_Has2DArrayTexturesAsExtension = true;
621 m_2DArrayTarget = GL_TEXTURE_2D_ARRAY_EXT;
622 }
623 else
624 {
625 m_Has2DArrayTexturesAsExtension = false;
626 m_2DArrayTarget = GL_TEXTURE_2D_ARRAY;
627 }
628
629 m_Has3DTextures = pCommand->m_pCapabilities->m_3DTextures;
630 m_HasMipMaps = pCommand->m_pCapabilities->m_MipMapping;
631 m_HasNPOTTextures = pCommand->m_pCapabilities->m_NPOTTextures;
632
633 m_LastBlendMode = EBlendMode::ALPHA;
634 m_LastClipEnable = false;
635
636 return true;
637}
638
639void CCommandProcessorFragment_OpenGL::TextureUpdate(int Slot, int X, int Y, int Width, int Height, int GLFormat, uint8_t *pTexData)
640{
641 glBindTexture(GL_TEXTURE_2D, texture: m_vTextures[Slot].m_Tex);
642
643 if(!m_HasNPOTTextures)
644 {
645 float ResizeW = m_vTextures[Slot].m_ResizeWidth;
646 float ResizeH = m_vTextures[Slot].m_ResizeHeight;
647 if(ResizeW > 0 && ResizeH > 0)
648 {
649 int ResizedW = (int)(Width * ResizeW);
650 int ResizedH = (int)(Height * ResizeH);
651
652 uint8_t *pTmpData = ResizeImage(pImageData: pTexData, Width, Height, NewWidth: ResizedW, NewHeight: ResizedH, BPP: GLFormatToPixelSize(GLFormat));
653 free(ptr: pTexData);
654 pTexData = pTmpData;
655
656 Width = ResizedW;
657 Height = ResizedH;
658 }
659 }
660
661 if(m_vTextures[Slot].m_RescaleCount > 0)
662 {
663 int OldWidth = Width;
664 int OldHeight = Height;
665 for(int i = 0; i < m_vTextures[Slot].m_RescaleCount; ++i)
666 {
667 Width >>= 1;
668 Height >>= 1;
669
670 X /= 2;
671 Y /= 2;
672 }
673
674 uint8_t *pTmpData = ResizeImage(pImageData: pTexData, Width: OldWidth, Height: OldHeight, NewWidth: Width, NewHeight: Height, BPP: GLFormatToPixelSize(GLFormat));
675 free(ptr: pTexData);
676 pTexData = pTmpData;
677 }
678
679 glTexSubImage2D(GL_TEXTURE_2D, level: 0, xoffset: X, yoffset: Y, width: Width, height: Height, format: GLFormat, GL_UNSIGNED_BYTE, pixels: pTexData);
680 free(ptr: pTexData);
681}
682
683void CCommandProcessorFragment_OpenGL::DestroyTexture(int Slot)
684{
685 m_pTextureMemoryUsage->store(i: m_pTextureMemoryUsage->load(m: std::memory_order_relaxed) - m_vTextures[Slot].m_MemSize, m: std::memory_order_relaxed);
686
687 if(m_vTextures[Slot].m_Tex != 0)
688 {
689 glDeleteTextures(n: 1, textures: &m_vTextures[Slot].m_Tex);
690 }
691
692 if(m_vTextures[Slot].m_Tex2DArray != 0)
693 {
694 glDeleteTextures(n: 1, textures: &m_vTextures[Slot].m_Tex2DArray);
695 }
696
697 if(IsNewApi())
698 {
699 if(m_vTextures[Slot].m_Sampler != 0)
700 {
701 glDeleteSamplers(1, &m_vTextures[Slot].m_Sampler);
702 }
703 if(m_vTextures[Slot].m_Sampler2DArray != 0)
704 {
705 glDeleteSamplers(1, &m_vTextures[Slot].m_Sampler2DArray);
706 }
707 }
708
709 m_vTextures[Slot].m_Tex = 0;
710 m_vTextures[Slot].m_Sampler = 0;
711 m_vTextures[Slot].m_Tex2DArray = 0;
712 m_vTextures[Slot].m_Sampler2DArray = 0;
713 m_vTextures[Slot].m_LastWrapMode = EWrapMode::REPEAT;
714}
715
716void CCommandProcessorFragment_OpenGL::Cmd_Texture_Destroy(const CCommandBuffer::SCommand_Texture_Destroy *pCommand)
717{
718 DestroyTexture(Slot: pCommand->m_Slot);
719}
720
721void CCommandProcessorFragment_OpenGL::TextureCreate(int Slot, int Width, int Height, int GLFormat, int GLStoreFormat, int Flags, uint8_t *pTexData)
722{
723#ifndef BACKEND_GL_MODERN_API
724
725 if(m_MaxTexSize == -1)
726 {
727 // fix the alignment to allow even 1byte changes, e.g. for alpha components
728 glPixelStorei(GL_UNPACK_ALIGNMENT, param: 1);
729 glGetIntegerv(GL_MAX_TEXTURE_SIZE, params: &m_MaxTexSize);
730 }
731
732 while(Slot >= (int)m_vTextures.size())
733 m_vTextures.resize(sz: m_vTextures.size() * 2);
734
735 m_vTextures[Slot].m_ResizeWidth = -1.f;
736 m_vTextures[Slot].m_ResizeHeight = -1.f;
737
738 if(!m_HasNPOTTextures)
739 {
740 int PowerOfTwoWidth = HighestBit(OfVar: Width);
741 int PowerOfTwoHeight = HighestBit(OfVar: Height);
742 if(Width != PowerOfTwoWidth || Height != PowerOfTwoHeight)
743 {
744 uint8_t *pTmpData = ResizeImage(pImageData: pTexData, Width, Height, NewWidth: PowerOfTwoWidth, NewHeight: PowerOfTwoHeight, BPP: GLFormatToPixelSize(GLFormat));
745 free(ptr: pTexData);
746 pTexData = pTmpData;
747
748 m_vTextures[Slot].m_ResizeWidth = (float)PowerOfTwoWidth / (float)Width;
749 m_vTextures[Slot].m_ResizeHeight = (float)PowerOfTwoHeight / (float)Height;
750
751 Width = PowerOfTwoWidth;
752 Height = PowerOfTwoHeight;
753 }
754 }
755
756 int RescaleCount = 0;
757 if(GLFormat == GL_RGBA)
758 {
759 int OldWidth = Width;
760 int OldHeight = Height;
761 bool NeedsResize = false;
762
763 if(Width > m_MaxTexSize || Height > m_MaxTexSize)
764 {
765 do
766 {
767 Width >>= 1;
768 Height >>= 1;
769 ++RescaleCount;
770 } while(Width > m_MaxTexSize || Height > m_MaxTexSize);
771 NeedsResize = true;
772 }
773
774 if(NeedsResize)
775 {
776 uint8_t *pTmpData = ResizeImage(pImageData: pTexData, Width: OldWidth, Height: OldHeight, NewWidth: Width, NewHeight: Height, BPP: GLFormatToPixelSize(GLFormat));
777 free(ptr: pTexData);
778 pTexData = pTmpData;
779 }
780 }
781 m_vTextures[Slot].m_Width = Width;
782 m_vTextures[Slot].m_Height = Height;
783 m_vTextures[Slot].m_RescaleCount = RescaleCount;
784
785 const size_t PixelSize = GLFormatToPixelSize(GLFormat);
786
787 if((Flags & TextureFlag::NO_2D_TEXTURE) == 0)
788 {
789 glGenTextures(n: 1, textures: &m_vTextures[Slot].m_Tex);
790 glBindTexture(GL_TEXTURE_2D, texture: m_vTextures[Slot].m_Tex);
791 }
792
793 if(Flags & TextureFlag::NO_MIPMAPS || !m_HasMipMaps)
794 {
795 if((Flags & TextureFlag::NO_2D_TEXTURE) == 0)
796 {
797 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
798 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
799 glTexImage2D(GL_TEXTURE_2D, level: 0, internalformat: GLStoreFormat, width: Width, height: Height, border: 0, format: GLFormat, GL_UNSIGNED_BYTE, pixels: pTexData);
800 }
801 }
802 else
803 {
804 if((Flags & TextureFlag::NO_2D_TEXTURE) == 0)
805 {
806 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
807 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
808 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
809
810#ifndef BACKEND_AS_OPENGL_ES
811 if(m_OpenGLTextureLodBIAS != 0 && !m_IsOpenGLES)
812 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, param: ((GLfloat)m_OpenGLTextureLodBIAS / 1000.0f));
813#endif
814
815 glTexImage2D(GL_TEXTURE_2D, level: 0, internalformat: GLStoreFormat, width: Width, height: Height, border: 0, format: GLFormat, GL_UNSIGNED_BYTE, pixels: pTexData);
816 }
817
818 int Flag2DArrayTexture = TextureFlag::TO_2D_ARRAY_TEXTURE;
819 int Flag3DTexture = TextureFlag::TO_3D_TEXTURE;
820 if((Flags & (Flag2DArrayTexture | Flag3DTexture)) != 0)
821 {
822 bool Is3DTexture = (Flags & Flag3DTexture) != 0;
823
824 glGenTextures(n: 1, textures: &m_vTextures[Slot].m_Tex2DArray);
825
826 GLenum Target = GL_TEXTURE_3D;
827
828 if(Is3DTexture)
829 {
830 Target = GL_TEXTURE_3D;
831 }
832 else
833 {
834 Target = m_2DArrayTarget;
835 }
836
837 glBindTexture(target: Target, texture: m_vTextures[Slot].m_Tex2DArray);
838
839 if(IsNewApi())
840 {
841 glGenSamplers(1, &m_vTextures[Slot].m_Sampler2DArray);
842 glBindSampler(0, m_vTextures[Slot].m_Sampler2DArray);
843 }
844
845 glTexParameteri(target: Target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
846 if(Is3DTexture)
847 {
848 glTexParameteri(target: Target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
849 if(IsNewApi())
850 glSamplerParameteri(m_vTextures[Slot].m_Sampler2DArray, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
851 }
852 else
853 {
854 glTexParameteri(target: Target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
855 glTexParameteri(target: Target, GL_GENERATE_MIPMAP, GL_TRUE);
856 if(IsNewApi())
857 glSamplerParameteri(m_vTextures[Slot].m_Sampler2DArray, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
858 }
859
860 glTexParameteri(target: Target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
861 glTexParameteri(target: Target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
862 glTexParameteri(target: Target, GL_TEXTURE_WRAP_R, GL_MIRRORED_REPEAT);
863
864#ifndef BACKEND_AS_OPENGL_ES
865 if(m_OpenGLTextureLodBIAS != 0 && !m_IsOpenGLES)
866 glTexParameterf(target: Target, GL_TEXTURE_LOD_BIAS, param: ((GLfloat)m_OpenGLTextureLodBIAS / 1000.0f));
867#endif
868
869 if(IsNewApi())
870 {
871 glSamplerParameteri(m_vTextures[Slot].m_Sampler2DArray, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
872 glSamplerParameteri(m_vTextures[Slot].m_Sampler2DArray, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
873 glSamplerParameteri(m_vTextures[Slot].m_Sampler2DArray, GL_TEXTURE_WRAP_R, GL_MIRRORED_REPEAT);
874
875#ifndef BACKEND_AS_OPENGL_ES
876 if(m_OpenGLTextureLodBIAS != 0 && !m_IsOpenGLES)
877 glSamplerParameterf(m_vTextures[Slot].m_Sampler2DArray, GL_TEXTURE_LOD_BIAS, ((GLfloat)m_OpenGLTextureLodBIAS / 1000.0f));
878#endif
879
880 glBindSampler(0, 0);
881 }
882
883 int ConvertWidth = Width;
884 int ConvertHeight = Height;
885
886 if(ConvertWidth == 0 || (ConvertWidth % 16) != 0 || ConvertHeight == 0 || (ConvertHeight % 16) != 0)
887 {
888 int NewWidth = maximum<int>(a: HighestBit(OfVar: ConvertWidth), b: 16);
889 int NewHeight = maximum<int>(a: HighestBit(OfVar: ConvertHeight), b: 16);
890 uint8_t *pNewTexData = ResizeImage(pImageData: pTexData, Width: ConvertWidth, Height: ConvertHeight, NewWidth, NewHeight, BPP: GLFormatToPixelSize(GLFormat));
891 log_debug("gfx/opengl", "3D/2D array texture was resized. Slot=%d Size=(%d, %d) Resized=(%d, %d)", Slot, ConvertWidth, ConvertHeight, NewWidth, NewHeight);
892
893 ConvertWidth = NewWidth;
894 ConvertHeight = NewHeight;
895
896 free(ptr: pTexData);
897 pTexData = pNewTexData;
898 }
899
900 int Image3DWidth, Image3DHeight;
901 uint8_t *pImageData3D = static_cast<uint8_t *>(malloc(size: (size_t)PixelSize * ConvertWidth * ConvertHeight));
902 Texture2DTo3D(pImageBuffer: pTexData, ImageWidth: ConvertWidth, ImageHeight: ConvertHeight, PixelSize, SplitCountWidth: 16, SplitCountHeight: 16, pTarget3DImageData: pImageData3D, Target3DImageWidth&: Image3DWidth, Target3DImageHeight&: Image3DHeight);
903 glTexImage3D(Target, 0, GLStoreFormat, Image3DWidth, Image3DHeight, 256, 0, GLFormat, GL_UNSIGNED_BYTE, pImageData3D);
904 free(ptr: pImageData3D);
905 }
906 }
907
908 // This is the initial value for the wrap modes
909 m_vTextures[Slot].m_LastWrapMode = EWrapMode::REPEAT;
910
911 // calculate memory usage
912 m_vTextures[Slot].m_MemSize = (size_t)Width * Height * PixelSize;
913 while(Width > 2 && Height > 2)
914 {
915 Width >>= 1;
916 Height >>= 1;
917 m_vTextures[Slot].m_MemSize += (size_t)Width * Height * PixelSize;
918 }
919 m_pTextureMemoryUsage->store(i: m_pTextureMemoryUsage->load(m: std::memory_order_relaxed) + m_vTextures[Slot].m_MemSize, m: std::memory_order_relaxed);
920
921 free(ptr: pTexData);
922#endif
923}
924
925void CCommandProcessorFragment_OpenGL::Cmd_Texture_Create(const CCommandBuffer::SCommand_Texture_Create *pCommand)
926{
927 TextureCreate(Slot: pCommand->m_Slot, Width: pCommand->m_Width, Height: pCommand->m_Height, GL_RGBA, GL_RGBA, Flags: pCommand->m_Flags, pTexData: pCommand->m_pData);
928}
929
930void CCommandProcessorFragment_OpenGL::Cmd_TextTexture_Update(const CCommandBuffer::SCommand_TextTexture_Update *pCommand)
931{
932 TextureUpdate(Slot: pCommand->m_Slot, X: pCommand->m_X, Y: pCommand->m_Y, Width: pCommand->m_Width, Height: pCommand->m_Height, GL_ALPHA, pTexData: pCommand->m_pData);
933}
934
935void CCommandProcessorFragment_OpenGL::Cmd_TextTextures_Destroy(const CCommandBuffer::SCommand_TextTextures_Destroy *pCommand)
936{
937 DestroyTexture(Slot: pCommand->m_Slot);
938 DestroyTexture(Slot: pCommand->m_SlotOutline);
939}
940
941void CCommandProcessorFragment_OpenGL::Cmd_TextTextures_Create(const CCommandBuffer::SCommand_TextTextures_Create *pCommand)
942{
943 TextureCreate(Slot: pCommand->m_Slot, Width: pCommand->m_Width, Height: pCommand->m_Height, GL_ALPHA, GL_ALPHA, Flags: TextureFlag::NO_MIPMAPS, pTexData: pCommand->m_pTextData);
944 TextureCreate(Slot: pCommand->m_SlotOutline, Width: pCommand->m_Width, Height: pCommand->m_Height, GL_ALPHA, GL_ALPHA, Flags: TextureFlag::NO_MIPMAPS, pTexData: pCommand->m_pTextOutlineData);
945}
946
947void CCommandProcessorFragment_OpenGL::Cmd_Clear(const CCommandBuffer::SCommand_Clear *pCommand)
948{
949 // if clip is still active, force disable it for clearing, enable it again afterwards
950 bool ClipWasEnabled = m_LastClipEnable;
951 if(ClipWasEnabled)
952 {
953 glDisable(GL_SCISSOR_TEST);
954 }
955 glClearColor(red: pCommand->m_Color.r, green: pCommand->m_Color.g, blue: pCommand->m_Color.b, alpha: 0.0f);
956 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
957 if(ClipWasEnabled)
958 {
959 glEnable(GL_SCISSOR_TEST);
960 }
961}
962
963void CCommandProcessorFragment_OpenGL::Cmd_Render(const CCommandBuffer::SCommand_Render *pCommand)
964{
965#ifndef BACKEND_GL_MODERN_API
966 SetState(State: pCommand->m_State);
967
968 glVertexPointer(size: 2, GL_FLOAT, stride: sizeof(CCommandBuffer::SVertex), pointer: (char *)pCommand->m_pVertices);
969 glTexCoordPointer(size: 2, GL_FLOAT, stride: sizeof(CCommandBuffer::SVertex), pointer: (char *)pCommand->m_pVertices + sizeof(float) * 2);
970 glColorPointer(size: 4, GL_UNSIGNED_BYTE, stride: sizeof(CCommandBuffer::SVertex), pointer: (char *)pCommand->m_pVertices + sizeof(float) * 4);
971 glEnableClientState(GL_VERTEX_ARRAY);
972 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
973 glEnableClientState(GL_COLOR_ARRAY);
974
975 switch(pCommand->m_PrimType)
976 {
977 case EPrimitiveType::QUADS:
978#ifndef BACKEND_AS_OPENGL_ES
979 glDrawArrays(GL_QUADS, first: 0, count: pCommand->m_PrimCount * 4);
980#endif
981 break;
982 case EPrimitiveType::LINES:
983 glDrawArrays(GL_LINES, first: 0, count: pCommand->m_PrimCount * 2);
984 break;
985 case EPrimitiveType::TRIANGLES:
986 glDrawArrays(GL_TRIANGLES, first: 0, count: pCommand->m_PrimCount * 3);
987 break;
988 default:
989 dbg_assert_failed("Invalid primitive type: %d", (int)pCommand->m_PrimType);
990 };
991#endif
992}
993
994void CCommandProcessorFragment_OpenGL::Cmd_ReadPixel(const CCommandBuffer::SCommand_TrySwapAndReadPixel *pCommand)
995{
996 // get size of viewport
997 GLint aViewport[4] = {0, 0, 0, 0};
998 glGetIntegerv(GL_VIEWPORT, params: aViewport);
999 const int h = aViewport[3];
1000
1001 // fetch the pixel
1002 uint8_t aPixelData[3];
1003 GLint Alignment;
1004 glGetIntegerv(GL_PACK_ALIGNMENT, params: &Alignment);
1005 glPixelStorei(GL_PACK_ALIGNMENT, param: 1);
1006 glReadPixels(x: pCommand->m_Position.x, y: h - 1 - pCommand->m_Position.y, width: 1, height: 1, GL_RGB, GL_UNSIGNED_BYTE, pixels: aPixelData);
1007 glPixelStorei(GL_PACK_ALIGNMENT, param: Alignment);
1008
1009 // fill in the information
1010 *pCommand->m_pColor = ColorRGBA(aPixelData[0] / 255.0f, aPixelData[1] / 255.0f, aPixelData[2] / 255.0f, 1.0f);
1011}
1012
1013void CCommandProcessorFragment_OpenGL::Cmd_Screenshot(const CCommandBuffer::SCommand_TrySwapAndScreenshot *pCommand)
1014{
1015 // fetch image data
1016 GLint aViewport[4] = {0, 0, 0, 0};
1017 glGetIntegerv(GL_VIEWPORT, params: aViewport);
1018
1019 int w = aViewport[2];
1020 int h = aViewport[3];
1021
1022 pCommand->m_pImage->m_Width = w;
1023 pCommand->m_pImage->m_Height = h;
1024 pCommand->m_pImage->m_Format = CImageInfo::FORMAT_RGBA;
1025 pCommand->m_pImage->Allocate();
1026
1027 uint8_t *pPixelData = pCommand->m_pImage->m_pData;
1028
1029 // we create a tmp row to use when we are flipping the texture
1030 std::vector<uint8_t> vTmpRow(w * 4);
1031 uint8_t *pTempRow = vTmpRow.data();
1032
1033 // fetch the pixels
1034 GLint Alignment;
1035 glGetIntegerv(GL_PACK_ALIGNMENT, params: &Alignment);
1036 glPixelStorei(GL_PACK_ALIGNMENT, param: 1);
1037 glReadPixels(x: 0, y: 0, width: w, height: h, GL_RGBA, GL_UNSIGNED_BYTE, pixels: pPixelData);
1038 glPixelStorei(GL_PACK_ALIGNMENT, param: Alignment);
1039
1040 // flip the pixel because opengl works from bottom left corner
1041 for(int y = 0; y < h / 2; y++)
1042 {
1043 mem_copy(dest: pTempRow, source: pPixelData + y * w * 4, size: w * 4);
1044 mem_copy(dest: pPixelData + y * w * 4, source: pPixelData + (h - y - 1) * w * 4, size: w * 4);
1045 mem_copy(dest: pPixelData + (h - y - 1) * w * 4, source: pTempRow, size: w * 4);
1046 for(int x = 0; x < w; x++)
1047 {
1048 pPixelData[y * w * 4 + x * 4 + 3] = 255;
1049 pPixelData[(h - y - 1) * w * 4 + x * 4 + 3] = 255;
1050 }
1051 }
1052}
1053
1054CCommandProcessorFragment_OpenGL::CCommandProcessorFragment_OpenGL()
1055{
1056 m_vTextures.resize(sz: CCommandBuffer::MAX_TEXTURES);
1057 m_HasShaders = false;
1058}
1059
1060ERunCommandReturnTypes CCommandProcessorFragment_OpenGL::RunCommand(const CCommandBuffer::SCommand *pBaseCommand)
1061{
1062 switch(pBaseCommand->m_Cmd)
1063 {
1064 case CCommandProcessorFragment_OpenGL::CMD_INIT:
1065 Cmd_Init(pCommand: static_cast<const SCommand_Init *>(pBaseCommand));
1066 break;
1067 case CCommandProcessorFragment_OpenGL::CMD_SHUTDOWN:
1068 Cmd_Shutdown(pCommand: static_cast<const SCommand_Shutdown *>(pBaseCommand));
1069 break;
1070 case CCommandBuffer::CMD_TEXTURE_CREATE:
1071 Cmd_Texture_Create(pCommand: static_cast<const CCommandBuffer::SCommand_Texture_Create *>(pBaseCommand));
1072 break;
1073 case CCommandBuffer::CMD_TEXTURE_DESTROY:
1074 Cmd_Texture_Destroy(pCommand: static_cast<const CCommandBuffer::SCommand_Texture_Destroy *>(pBaseCommand));
1075 break;
1076 case CCommandBuffer::CMD_TEXT_TEXTURES_CREATE:
1077 Cmd_TextTextures_Create(pCommand: static_cast<const CCommandBuffer::SCommand_TextTextures_Create *>(pBaseCommand));
1078 break;
1079 case CCommandBuffer::CMD_TEXT_TEXTURES_DESTROY:
1080 Cmd_TextTextures_Destroy(pCommand: static_cast<const CCommandBuffer::SCommand_TextTextures_Destroy *>(pBaseCommand));
1081 break;
1082 case CCommandBuffer::CMD_TEXT_TEXTURE_UPDATE:
1083 Cmd_TextTexture_Update(pCommand: static_cast<const CCommandBuffer::SCommand_TextTexture_Update *>(pBaseCommand));
1084 break;
1085 case CCommandBuffer::CMD_CLEAR:
1086 Cmd_Clear(pCommand: static_cast<const CCommandBuffer::SCommand_Clear *>(pBaseCommand));
1087 break;
1088 case CCommandBuffer::CMD_RENDER:
1089 Cmd_Render(pCommand: static_cast<const CCommandBuffer::SCommand_Render *>(pBaseCommand));
1090 break;
1091 case CCommandBuffer::CMD_RENDER_TEX3D:
1092 Cmd_RenderTex3D(pCommand: static_cast<const CCommandBuffer::SCommand_RenderTex3D *>(pBaseCommand));
1093 break;
1094 case CCommandBuffer::CMD_TRY_SWAP_AND_READ_PIXEL:
1095 Cmd_ReadPixel(pCommand: static_cast<const CCommandBuffer::SCommand_TrySwapAndReadPixel *>(pBaseCommand));
1096 break;
1097 case CCommandBuffer::CMD_TRY_SWAP_AND_SCREENSHOT:
1098 Cmd_Screenshot(pCommand: static_cast<const CCommandBuffer::SCommand_TrySwapAndScreenshot *>(pBaseCommand));
1099 break;
1100 case CCommandBuffer::CMD_UPDATE_VIEWPORT:
1101 Cmd_Update_Viewport(pCommand: static_cast<const CCommandBuffer::SCommand_Update_Viewport *>(pBaseCommand));
1102 break;
1103
1104 case CCommandBuffer::CMD_CREATE_BUFFER_OBJECT: Cmd_CreateBufferObject(pCommand: static_cast<const CCommandBuffer::SCommand_CreateBufferObject *>(pBaseCommand)); break;
1105 case CCommandBuffer::CMD_UPDATE_BUFFER_OBJECT: Cmd_UpdateBufferObject(pCommand: static_cast<const CCommandBuffer::SCommand_UpdateBufferObject *>(pBaseCommand)); break;
1106 case CCommandBuffer::CMD_RECREATE_BUFFER_OBJECT: Cmd_RecreateBufferObject(pCommand: static_cast<const CCommandBuffer::SCommand_RecreateBufferObject *>(pBaseCommand)); break;
1107 case CCommandBuffer::CMD_COPY_BUFFER_OBJECT: Cmd_CopyBufferObject(pCommand: static_cast<const CCommandBuffer::SCommand_CopyBufferObject *>(pBaseCommand)); break;
1108 case CCommandBuffer::CMD_DELETE_BUFFER_OBJECT: Cmd_DeleteBufferObject(pCommand: static_cast<const CCommandBuffer::SCommand_DeleteBufferObject *>(pBaseCommand)); break;
1109
1110 case CCommandBuffer::CMD_CREATE_BUFFER_CONTAINER: Cmd_CreateBufferContainer(pCommand: static_cast<const CCommandBuffer::SCommand_CreateBufferContainer *>(pBaseCommand)); break;
1111 case CCommandBuffer::CMD_UPDATE_BUFFER_CONTAINER: Cmd_UpdateBufferContainer(pCommand: static_cast<const CCommandBuffer::SCommand_UpdateBufferContainer *>(pBaseCommand)); break;
1112 case CCommandBuffer::CMD_DELETE_BUFFER_CONTAINER: Cmd_DeleteBufferContainer(pCommand: static_cast<const CCommandBuffer::SCommand_DeleteBufferContainer *>(pBaseCommand)); break;
1113 case CCommandBuffer::CMD_INDICES_REQUIRED_NUM_NOTIFY: Cmd_IndicesRequiredNumNotify(pCommand: static_cast<const CCommandBuffer::SCommand_IndicesRequiredNumNotify *>(pBaseCommand)); break;
1114
1115 case CCommandBuffer::CMD_RENDER_TILE_LAYER: Cmd_RenderTileLayer(pCommand: static_cast<const CCommandBuffer::SCommand_RenderTileLayer *>(pBaseCommand)); break;
1116 case CCommandBuffer::CMD_RENDER_BORDER_TILE: Cmd_RenderBorderTile(pCommand: static_cast<const CCommandBuffer::SCommand_RenderBorderTile *>(pBaseCommand)); break;
1117 case CCommandBuffer::CMD_RENDER_QUAD_LAYER: Cmd_RenderQuadLayer(pCommand: static_cast<const CCommandBuffer::SCommand_RenderQuadLayer *>(pBaseCommand), Grouped: false); break;
1118 case CCommandBuffer::CMD_RENDER_QUAD_LAYER_GROUPED: Cmd_RenderQuadLayer(pCommand: static_cast<const CCommandBuffer::SCommand_RenderQuadLayer *>(pBaseCommand), Grouped: true); break;
1119 case CCommandBuffer::CMD_RENDER_TEXT: Cmd_RenderText(pCommand: static_cast<const CCommandBuffer::SCommand_RenderText *>(pBaseCommand)); break;
1120 case CCommandBuffer::CMD_RENDER_QUAD_CONTAINER: Cmd_RenderQuadContainer(pCommand: static_cast<const CCommandBuffer::SCommand_RenderQuadContainer *>(pBaseCommand)); break;
1121 case CCommandBuffer::CMD_RENDER_QUAD_CONTAINER_EX: Cmd_RenderQuadContainerEx(pCommand: static_cast<const CCommandBuffer::SCommand_RenderQuadContainerEx *>(pBaseCommand)); break;
1122 case CCommandBuffer::CMD_RENDER_QUAD_CONTAINER_SPRITE_MULTIPLE: Cmd_RenderQuadContainerAsSpriteMultiple(pCommand: static_cast<const CCommandBuffer::SCommand_RenderQuadContainerAsSpriteMultiple *>(pBaseCommand)); break;
1123 default: return ERunCommandReturnTypes::RUN_COMMAND_COMMAND_UNHANDLED;
1124 }
1125
1126 return ERunCommandReturnTypes::RUN_COMMAND_COMMAND_HANDLED;
1127}
1128
1129// ------------ CCommandProcessorFragment_OpenGL2
1130
1131void CCommandProcessorFragment_OpenGL2::UseProgram(CGLSLTWProgram *pProgram)
1132{
1133 pProgram->UseProgram();
1134}
1135
1136void CCommandProcessorFragment_OpenGL2::SetState(const CCommandBuffer::SState &State, CGLSLTWProgram *pProgram, bool Use2DArrayTextures)
1137{
1138 if(m_LastBlendMode == EBlendMode::NONE)
1139 {
1140 m_LastBlendMode = EBlendMode::ALPHA;
1141 glEnable(GL_BLEND);
1142 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1143 }
1144 if(State.m_BlendMode != m_LastBlendMode && State.m_BlendMode != EBlendMode::NONE)
1145 {
1146 // blend
1147 switch(State.m_BlendMode)
1148 {
1149 case EBlendMode::NONE:
1150 // We don't really need this anymore
1151 // glDisable(GL_BLEND);
1152 break;
1153 case EBlendMode::ALPHA:
1154 // glEnable(GL_BLEND);
1155 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1156 break;
1157 case EBlendMode::ADDITIVE:
1158 // glEnable(GL_BLEND);
1159 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
1160 break;
1161 default:
1162 dbg_assert_failed("Invalid blend mode: %d", (int)State.m_BlendMode);
1163 };
1164
1165 m_LastBlendMode = State.m_BlendMode;
1166 }
1167
1168 // clip
1169 if(State.m_ClipEnable)
1170 {
1171 glScissor(x: State.m_ClipX, y: State.m_ClipY, width: State.m_ClipW, height: State.m_ClipH);
1172 glEnable(GL_SCISSOR_TEST);
1173 m_LastClipEnable = true;
1174 }
1175 else if(m_LastClipEnable)
1176 {
1177 // Don't disable it always
1178 glDisable(GL_SCISSOR_TEST);
1179 m_LastClipEnable = false;
1180 }
1181
1182 if(!IsNewApi())
1183 {
1184 glDisable(GL_TEXTURE_2D);
1185 if(!m_HasShaders)
1186 {
1187 if(m_Has3DTextures)
1188 glDisable(GL_TEXTURE_3D);
1189 if(m_Has2DArrayTextures)
1190 {
1191 glDisable(cap: m_2DArrayTarget);
1192 }
1193 }
1194 }
1195
1196 // texture
1197 if(IsTexturedState(State))
1198 {
1199 int Slot = 0;
1200 if(!Use2DArrayTextures)
1201 {
1202 if(!IsNewApi() && !m_HasShaders)
1203 glEnable(GL_TEXTURE_2D);
1204 glBindTexture(GL_TEXTURE_2D, texture: m_vTextures[State.m_Texture].m_Tex);
1205 if(IsNewApi())
1206 glBindSampler(Slot, m_vTextures[State.m_Texture].m_Sampler);
1207 }
1208 else
1209 {
1210 if(!m_Has2DArrayTextures)
1211 {
1212 if(!IsNewApi() && !m_HasShaders)
1213 glEnable(GL_TEXTURE_3D);
1214 glBindTexture(GL_TEXTURE_3D, texture: m_vTextures[State.m_Texture].m_Tex2DArray);
1215 if(IsNewApi())
1216 glBindSampler(Slot, m_vTextures[State.m_Texture].m_Sampler2DArray);
1217 }
1218 else
1219 {
1220 if(!IsNewApi() && !m_HasShaders)
1221 glEnable(cap: m_2DArrayTarget);
1222 glBindTexture(target: m_2DArrayTarget, texture: m_vTextures[State.m_Texture].m_Tex2DArray);
1223 if(IsNewApi())
1224 glBindSampler(Slot, m_vTextures[State.m_Texture].m_Sampler2DArray);
1225 }
1226 }
1227
1228 if(pProgram->m_LastTextureSampler != Slot)
1229 {
1230 pProgram->SetUniform(Loc: pProgram->m_LocTextureSampler, Value: Slot);
1231 pProgram->m_LastTextureSampler = Slot;
1232 }
1233
1234 if(m_vTextures[State.m_Texture].m_LastWrapMode != State.m_WrapMode && !Use2DArrayTextures)
1235 {
1236 switch(State.m_WrapMode)
1237 {
1238 case EWrapMode::REPEAT:
1239 if(IsNewApi())
1240 {
1241 glSamplerParameteri(m_vTextures[State.m_Texture].m_Sampler, GL_TEXTURE_WRAP_S, GL_REPEAT);
1242 glSamplerParameteri(m_vTextures[State.m_Texture].m_Sampler, GL_TEXTURE_WRAP_T, GL_REPEAT);
1243 }
1244 break;
1245 case EWrapMode::CLAMP:
1246 if(IsNewApi())
1247 {
1248 glSamplerParameteri(m_vTextures[State.m_Texture].m_Sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1249 glSamplerParameteri(m_vTextures[State.m_Texture].m_Sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1250 }
1251 break;
1252 default:
1253 dbg_assert_failed("Invalid wrap mode: %d", (int)State.m_WrapMode);
1254 };
1255 m_vTextures[State.m_Texture].m_LastWrapMode = State.m_WrapMode;
1256 }
1257 }
1258
1259 if(pProgram->m_LastScreenTL != State.m_ScreenTL || pProgram->m_LastScreenBR != State.m_ScreenBR)
1260 {
1261 pProgram->m_LastScreenTL = State.m_ScreenTL;
1262 pProgram->m_LastScreenBR = State.m_ScreenBR;
1263
1264 // screen mapping
1265 // orthographic projection matrix
1266 // the z coordinate is the same for every vertex, so just ignore the z coordinate and set it in the shaders
1267 float m[2 * 4] = {
1268 2.f / (State.m_ScreenBR.x - State.m_ScreenTL.x),
1269 0,
1270 0,
1271 -((State.m_ScreenBR.x + State.m_ScreenTL.x) / (State.m_ScreenBR.x - State.m_ScreenTL.x)),
1272 0,
1273 (2.f / (State.m_ScreenTL.y - State.m_ScreenBR.y)),
1274 0,
1275 -((State.m_ScreenTL.y + State.m_ScreenBR.y) / (State.m_ScreenTL.y - State.m_ScreenBR.y)),
1276 };
1277
1278 // transpose bcs of column-major order of opengl
1279 glUniformMatrix4x2fv(pProgram->m_LocPos, 1, true, (float *)&m);
1280 }
1281}
1282
1283#ifndef BACKEND_GL_MODERN_API
1284bool CCommandProcessorFragment_OpenGL2::DoAnalyzeStep(size_t CheckCount, size_t VerticesCount, uint8_t aFakeTexture[], size_t SingleImageSize)
1285{
1286 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1287
1288 int Slot = 0;
1289 if(m_HasShaders)
1290 {
1291 CGLSLTWProgram *pProgram = m_pPrimitive3DProgramTextured;
1292 UseProgram(pProgram);
1293
1294 pProgram->SetUniform(Loc: pProgram->m_LocTextureSampler, Value: Slot);
1295
1296 float m[2 * 4] = {
1297 1, 0, 0, 0,
1298 0, 1, 0, 0};
1299
1300 // transpose bcs of column-major order of opengl
1301 glUniformMatrix4x2fv(pProgram->m_LocPos, 1, true, (float *)&m);
1302 }
1303 else
1304 {
1305 glMatrixMode(GL_PROJECTION);
1306 glLoadIdentity();
1307 glOrtho(left: -1, right: 1, bottom: -1, top: 1, zNear: -10.0f, zFar: 10.f);
1308 }
1309
1310 glEnableClientState(GL_VERTEX_ARRAY);
1311 glEnableClientState(GL_COLOR_ARRAY);
1312 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1313
1314 glVertexPointer(size: 2, GL_FLOAT, stride: sizeof(m_aStreamVertices[0]), pointer: m_aStreamVertices);
1315 glColorPointer(size: 4, GL_FLOAT, stride: sizeof(m_aStreamVertices[0]), pointer: (uint8_t *)m_aStreamVertices + (ptrdiff_t)(sizeof(vec2)));
1316 glTexCoordPointer(size: 3, GL_FLOAT, stride: sizeof(m_aStreamVertices[0]), pointer: (uint8_t *)m_aStreamVertices + (ptrdiff_t)(sizeof(vec2) + sizeof(vec4)));
1317
1318 glDrawArrays(GL_QUADS, first: 0, count: VerticesCount);
1319
1320 glDisableClientState(GL_VERTEX_ARRAY);
1321 glDisableClientState(GL_COLOR_ARRAY);
1322 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1323
1324 if(m_HasShaders)
1325 {
1326 glUseProgram(0);
1327 }
1328
1329 glFinish();
1330
1331 GLint aViewport[4] = {0, 0, 0, 0};
1332 glGetIntegerv(GL_VIEWPORT, params: aViewport);
1333
1334 int w = aViewport[2];
1335 int h = aViewport[3];
1336
1337 size_t PixelDataSize = (size_t)w * h * 3;
1338 if(PixelDataSize == 0)
1339 return false;
1340 uint8_t *pPixelData = (uint8_t *)malloc(size: PixelDataSize);
1341
1342 // fetch the pixels
1343 GLint Alignment;
1344 glGetIntegerv(GL_PACK_ALIGNMENT, params: &Alignment);
1345 glPixelStorei(GL_PACK_ALIGNMENT, param: 1);
1346 glReadPixels(x: 0, y: 0, width: w, height: h, GL_RGB, GL_UNSIGNED_BYTE, pixels: pPixelData);
1347 glPixelStorei(GL_PACK_ALIGNMENT, param: Alignment);
1348
1349 // now analyse the image data
1350 bool CheckFailed = false;
1351 int WidthTile = w / 16;
1352 int HeightTile = h / 16;
1353 int StartX = WidthTile / 2;
1354 int StartY = HeightTile / 2;
1355 for(size_t d = 0; d < CheckCount; ++d)
1356 {
1357 int CurX = (int)d % 16;
1358 int CurY = (int)d / 16;
1359
1360 int CheckX = StartX + CurX * WidthTile;
1361 int CheckY = StartY + CurY * HeightTile;
1362
1363 ptrdiff_t OffsetPixelData = (CheckY * (w * 3)) + (CheckX * 3);
1364 ptrdiff_t OffsetFakeTexture = SingleImageSize * d;
1365 OffsetPixelData = std::clamp<ptrdiff_t>(val: OffsetPixelData, lo: 0, hi: (ptrdiff_t)PixelDataSize);
1366 OffsetFakeTexture = std::clamp<ptrdiff_t>(val: OffsetFakeTexture, lo: 0, hi: (ptrdiff_t)(SingleImageSize * CheckCount));
1367 uint8_t *pPixel = pPixelData + OffsetPixelData;
1368 uint8_t *pPixelTex = aFakeTexture + OffsetFakeTexture;
1369 for(size_t i = 0; i < 3; ++i)
1370 {
1371 if((pPixel[i] < pPixelTex[i] - 25) || (pPixel[i] > pPixelTex[i] + 25))
1372 {
1373 CheckFailed = true;
1374 break;
1375 }
1376 }
1377 }
1378
1379 free(ptr: pPixelData);
1380 return !CheckFailed;
1381}
1382
1383bool CCommandProcessorFragment_OpenGL2::IsTileMapAnalysisSucceeded()
1384{
1385 glClearColor(red: 0, green: 0, blue: 0, alpha: 1);
1386
1387 // create fake texture 1024x1024
1388 const size_t ImageWidth = 1024;
1389 const size_t ImageHeight = 1024;
1390 uint8_t *pFakeTexture = (uint8_t *)malloc(size: sizeof(uint8_t) * ImageWidth * ImageHeight * 4);
1391 // fill by colors stepping by 50 => (255 / 50 ~ 5) => 5 times 3(color channels) = 5 ^ 3 = 125 possibilities to check
1392 size_t CheckCount = 5 * 5 * 5;
1393 // always fill 4 pixels of the texture, so the sampling is accurate
1394 int aCurColor[4] = {25, 25, 25, 255};
1395 const size_t SingleImageWidth = 64;
1396 const size_t SingleImageHeight = 64;
1397 size_t SingleImageSize = SingleImageWidth * SingleImageHeight * 4;
1398 for(size_t d = 0; d < CheckCount; ++d)
1399 {
1400 uint8_t *pCurFakeTexture = pFakeTexture + (ptrdiff_t)(SingleImageSize * d);
1401
1402 uint8_t aCurColorUint8[SingleImageWidth * SingleImageHeight * 4];
1403 for(size_t y = 0; y < SingleImageHeight; ++y)
1404 {
1405 for(size_t x = 0; x < SingleImageWidth; ++x)
1406 {
1407 for(size_t i = 0; i < 4; ++i)
1408 {
1409 aCurColorUint8[(y * SingleImageWidth * 4) + (x * 4) + i] = (uint8_t)aCurColor[i];
1410 }
1411 }
1412 }
1413 mem_copy(dest: pCurFakeTexture, source: aCurColorUint8, size: sizeof(aCurColorUint8));
1414
1415 aCurColor[2] += 50;
1416 if(aCurColor[2] > 225)
1417 {
1418 aCurColor[2] -= 250;
1419 aCurColor[1] += 50;
1420 }
1421 if(aCurColor[1] > 225)
1422 {
1423 aCurColor[1] -= 250;
1424 aCurColor[0] += 50;
1425 }
1426 if(aCurColor[0] > 225)
1427 {
1428 break;
1429 }
1430 }
1431
1432 // upload the texture
1433 GLuint FakeTexture;
1434 glGenTextures(n: 1, textures: &FakeTexture);
1435
1436 GLenum Target = GL_TEXTURE_3D;
1437 if(m_Has2DArrayTextures)
1438 {
1439 Target = m_2DArrayTarget;
1440 }
1441
1442 glBindTexture(target: Target, texture: FakeTexture);
1443 glTexParameteri(target: Target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1444 if(!m_Has2DArrayTextures)
1445 {
1446 glTexParameteri(target: Target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1447 }
1448 else
1449 {
1450 glTexParameteri(target: Target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
1451 glTexParameteri(target: Target, GL_GENERATE_MIPMAP, GL_TRUE);
1452 }
1453
1454 glTexParameteri(target: Target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1455 glTexParameteri(target: Target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1456 glTexParameteri(target: Target, GL_TEXTURE_WRAP_R, GL_MIRRORED_REPEAT);
1457
1458 glTexImage3D(Target, 0, GL_RGBA, ImageWidth / 16, ImageHeight / 16, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, pFakeTexture);
1459
1460 glEnable(GL_BLEND);
1461 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1462 glDisable(GL_SCISSOR_TEST);
1463
1464 if(!m_HasShaders)
1465 {
1466 glDisable(GL_TEXTURE_2D);
1467 if(m_Has3DTextures)
1468 glDisable(GL_TEXTURE_3D);
1469 if(m_Has2DArrayTextures)
1470 {
1471 glDisable(cap: m_2DArrayTarget);
1472 }
1473
1474 if(!m_Has2DArrayTextures)
1475 {
1476 glEnable(GL_TEXTURE_3D);
1477 glBindTexture(GL_TEXTURE_3D, texture: FakeTexture);
1478 }
1479 else
1480 {
1481 glEnable(cap: m_2DArrayTarget);
1482 glBindTexture(target: m_2DArrayTarget, texture: FakeTexture);
1483 }
1484 }
1485
1486 static_assert(sizeof(m_aStreamVertices) / sizeof(m_aStreamVertices[0]) >= 256 * 4, "Keep the number of stream vertices >= 256 * 4.");
1487
1488 size_t VertexCount = 0;
1489 for(size_t i = 0; i < CheckCount; ++i)
1490 {
1491 float XPos = (float)(i % 16);
1492 float YPos = (float)(i / 16);
1493
1494 GL_SVertexTex3D *pVertex = &m_aStreamVertices[VertexCount++];
1495 GL_SVertexTex3D *pVertexBefore = pVertex;
1496 pVertex->m_Pos.x = XPos / 16.f;
1497 pVertex->m_Pos.y = YPos / 16.f;
1498 pVertex->m_Color.r = 1;
1499 pVertex->m_Color.g = 1;
1500 pVertex->m_Color.b = 1;
1501 pVertex->m_Color.a = 1;
1502 pVertex->m_Tex.u = 0;
1503 pVertex->m_Tex.v = 0;
1504
1505 pVertex = &m_aStreamVertices[VertexCount++];
1506 pVertex->m_Pos.x = XPos / 16.f + 1.f / 16.f;
1507 pVertex->m_Pos.y = YPos / 16.f;
1508 pVertex->m_Color.r = 1;
1509 pVertex->m_Color.g = 1;
1510 pVertex->m_Color.b = 1;
1511 pVertex->m_Color.a = 1;
1512 pVertex->m_Tex.u = 1;
1513 pVertex->m_Tex.v = 0;
1514
1515 pVertex = &m_aStreamVertices[VertexCount++];
1516 pVertex->m_Pos.x = XPos / 16.f + 1.f / 16.f;
1517 pVertex->m_Pos.y = YPos / 16.f + 1.f / 16.f;
1518 pVertex->m_Color.r = 1;
1519 pVertex->m_Color.g = 1;
1520 pVertex->m_Color.b = 1;
1521 pVertex->m_Color.a = 1;
1522 pVertex->m_Tex.u = 1;
1523 pVertex->m_Tex.v = 1;
1524
1525 pVertex = &m_aStreamVertices[VertexCount++];
1526 pVertex->m_Pos.x = XPos / 16.f;
1527 pVertex->m_Pos.y = YPos / 16.f + 1.f / 16.f;
1528 pVertex->m_Color.r = 1;
1529 pVertex->m_Color.g = 1;
1530 pVertex->m_Color.b = 1;
1531 pVertex->m_Color.a = 1;
1532 pVertex->m_Tex.u = 0;
1533 pVertex->m_Tex.v = 1;
1534
1535 for(size_t n = 0; n < 4; ++n)
1536 {
1537 pVertexBefore[n].m_Pos.x *= 2;
1538 pVertexBefore[n].m_Pos.x -= 1;
1539 pVertexBefore[n].m_Pos.y *= 2;
1540 pVertexBefore[n].m_Pos.y -= 1;
1541 if(m_Has2DArrayTextures)
1542 {
1543 pVertexBefore[n].m_Tex.w = i;
1544 }
1545 else
1546 {
1547 pVertexBefore[n].m_Tex.w = (i + 0.5f) / 256.f;
1548 }
1549 }
1550 }
1551
1552 // everything build up, now do the analyze steps
1553 bool NoError = DoAnalyzeStep(CheckCount, VerticesCount: VertexCount, aFakeTexture: pFakeTexture, SingleImageSize);
1554
1555 glDeleteTextures(n: 1, textures: &FakeTexture);
1556 free(ptr: pFakeTexture);
1557
1558 return NoError;
1559}
1560
1561bool CCommandProcessorFragment_OpenGL2::Cmd_Init(const SCommand_Init *pCommand)
1562{
1563 if(!CCommandProcessorFragment_OpenGL::Cmd_Init(pCommand))
1564 return false;
1565
1566 m_pTileProgram = nullptr;
1567 m_pTileProgramTextured = nullptr;
1568 m_pPrimitive3DProgram = nullptr;
1569 m_pPrimitive3DProgramTextured = nullptr;
1570
1571 m_OpenGLTextureLodBIAS = g_Config.m_GfxGLTextureLODBIAS;
1572
1573 m_HasShaders = pCommand->m_pCapabilities->m_ShaderSupport;
1574
1575 bool HasAllFunc = true;
1576#ifndef BACKEND_AS_OPENGL_ES
1577 if(m_HasShaders)
1578 {
1579 HasAllFunc &= (glUniformMatrix4x2fv != nullptr) && (glGenBuffers != nullptr);
1580 HasAllFunc &= (glBindBuffer != nullptr) && (glBufferData != nullptr);
1581 HasAllFunc &= (glEnableVertexAttribArray != nullptr) && (glVertexAttribPointer != nullptr) && (glVertexAttribIPointer != nullptr);
1582 HasAllFunc &= (glDisableVertexAttribArray != nullptr) && (glDeleteBuffers != nullptr);
1583 HasAllFunc &= (glUseProgram != nullptr) && (glTexImage3D != nullptr);
1584 HasAllFunc &= (glBindAttribLocation != nullptr) && (glTexImage3D != nullptr);
1585 HasAllFunc &= (glBufferSubData != nullptr) && (glGetUniformLocation != nullptr);
1586 HasAllFunc &= (glUniform1i != nullptr) && (glUniform1f != nullptr);
1587 HasAllFunc &= (glUniform1ui != nullptr) && (glUniform1i != nullptr);
1588 HasAllFunc &= (glUniform1fv != nullptr) && (glUniform2fv != nullptr);
1589 HasAllFunc &= (glUniform4fv != nullptr) && (glGetAttachedShaders != nullptr);
1590 HasAllFunc &= (glGetProgramInfoLog != nullptr) && (glGetProgramiv != nullptr);
1591 HasAllFunc &= (glLinkProgram != nullptr) && (glDetachShader != nullptr);
1592 HasAllFunc &= (glAttachShader != nullptr) && (glDeleteProgram != nullptr);
1593 HasAllFunc &= (glCreateProgram != nullptr) && (glShaderSource != nullptr);
1594 HasAllFunc &= (glCompileShader != nullptr) && (glGetShaderiv != nullptr);
1595 HasAllFunc &= (glGetShaderInfoLog != nullptr) && (glDeleteShader != nullptr);
1596 HasAllFunc &= (glCreateShader != nullptr);
1597 }
1598#endif
1599
1600 bool AnalysisCorrect = true;
1601 if(HasAllFunc)
1602 {
1603 if(m_HasShaders)
1604 {
1605 m_pTileProgram = new CGLSLTileProgram;
1606 m_pTileProgramTextured = new CGLSLTileProgram;
1607 m_pBorderTileProgram = new CGLSLTileProgram;
1608 m_pBorderTileProgramTextured = new CGLSLTileProgram;
1609 m_pPrimitive3DProgram = new CGLSLPrimitiveProgram;
1610 m_pPrimitive3DProgramTextured = new CGLSLPrimitiveProgram;
1611
1612 CGLSLCompiler ShaderCompiler(g_Config.m_GfxGLMajor, g_Config.m_GfxGLMinor, g_Config.m_GfxGLPatch, m_IsOpenGLES, m_OpenGLTextureLodBIAS / 1000.0f);
1613 ShaderCompiler.SetHasTextureArray(pCommand->m_pCapabilities->m_2DArrayTextures);
1614
1615 if(pCommand->m_pCapabilities->m_2DArrayTextures)
1616 ShaderCompiler.SetTextureReplaceType(CGLSLCompiler::GLSL_COMPILER_TEXTURE_REPLACE_TYPE_2D_ARRAY);
1617 else
1618 ShaderCompiler.SetTextureReplaceType(CGLSLCompiler::GLSL_COMPILER_TEXTURE_REPLACE_TYPE_3D);
1619 {
1620 CGLSL PrimitiveVertexShader;
1621 CGLSL PrimitiveFragmentShader;
1622 PrimitiveVertexShader.LoadShader(pCompiler: &ShaderCompiler, pStorage: pCommand->m_pStorage, pFile: "shader/pipeline.vert", GL_VERTEX_SHADER);
1623 PrimitiveFragmentShader.LoadShader(pCompiler: &ShaderCompiler, pStorage: pCommand->m_pStorage, pFile: "shader/pipeline.frag", GL_FRAGMENT_SHADER);
1624
1625 m_pPrimitive3DProgram->CreateProgram();
1626 m_pPrimitive3DProgram->AddShader(pShader: &PrimitiveVertexShader);
1627 m_pPrimitive3DProgram->AddShader(pShader: &PrimitiveFragmentShader);
1628 m_pPrimitive3DProgram->LinkProgram();
1629
1630 UseProgram(pProgram: m_pPrimitive3DProgram);
1631
1632 m_pPrimitive3DProgram->m_LocPos = m_pPrimitive3DProgram->GetUniformLoc(pName: "gPos");
1633 }
1634
1635 if(pCommand->m_pCapabilities->m_2DArrayTextures)
1636 ShaderCompiler.SetTextureReplaceType(CGLSLCompiler::GLSL_COMPILER_TEXTURE_REPLACE_TYPE_2D_ARRAY);
1637 else
1638 ShaderCompiler.SetTextureReplaceType(CGLSLCompiler::GLSL_COMPILER_TEXTURE_REPLACE_TYPE_3D);
1639 {
1640 CGLSL PrimitiveVertexShader;
1641 CGLSL PrimitiveFragmentShader;
1642 ShaderCompiler.AddDefine(pDefineName: "TW_TEXTURED", pDefineValue: "");
1643 if(!pCommand->m_pCapabilities->m_2DArrayTextures)
1644 ShaderCompiler.AddDefine(pDefineName: "TW_3D_TEXTURED", pDefineValue: "");
1645 PrimitiveVertexShader.LoadShader(pCompiler: &ShaderCompiler, pStorage: pCommand->m_pStorage, pFile: "shader/pipeline.vert", GL_VERTEX_SHADER);
1646 PrimitiveFragmentShader.LoadShader(pCompiler: &ShaderCompiler, pStorage: pCommand->m_pStorage, pFile: "shader/pipeline.frag", GL_FRAGMENT_SHADER);
1647 ShaderCompiler.ClearDefines();
1648
1649 m_pPrimitive3DProgramTextured->CreateProgram();
1650 m_pPrimitive3DProgramTextured->AddShader(pShader: &PrimitiveVertexShader);
1651 m_pPrimitive3DProgramTextured->AddShader(pShader: &PrimitiveFragmentShader);
1652 m_pPrimitive3DProgramTextured->LinkProgram();
1653
1654 UseProgram(pProgram: m_pPrimitive3DProgramTextured);
1655
1656 m_pPrimitive3DProgramTextured->m_LocPos = m_pPrimitive3DProgramTextured->GetUniformLoc(pName: "gPos");
1657 m_pPrimitive3DProgramTextured->m_LocTextureSampler = m_pPrimitive3DProgramTextured->GetUniformLoc(pName: "gTextureSampler");
1658 }
1659 if(pCommand->m_pCapabilities->m_2DArrayTextures)
1660 ShaderCompiler.SetTextureReplaceType(CGLSLCompiler::GLSL_COMPILER_TEXTURE_REPLACE_TYPE_2D_ARRAY);
1661 else
1662 ShaderCompiler.SetTextureReplaceType(CGLSLCompiler::GLSL_COMPILER_TEXTURE_REPLACE_TYPE_3D);
1663 {
1664 CGLSL VertexShader;
1665 CGLSL FragmentShader;
1666 VertexShader.LoadShader(pCompiler: &ShaderCompiler, pStorage: pCommand->m_pStorage, pFile: "shader/tile.vert", GL_VERTEX_SHADER);
1667 FragmentShader.LoadShader(pCompiler: &ShaderCompiler, pStorage: pCommand->m_pStorage, pFile: "shader/tile.frag", GL_FRAGMENT_SHADER);
1668
1669 m_pTileProgram->CreateProgram();
1670 m_pTileProgram->AddShader(pShader: &VertexShader);
1671 m_pTileProgram->AddShader(pShader: &FragmentShader);
1672
1673 glBindAttribLocation(m_pTileProgram->GetProgramId(), 0, "inVertex");
1674
1675 m_pTileProgram->LinkProgram();
1676
1677 UseProgram(pProgram: m_pTileProgram);
1678
1679 m_pTileProgram->m_LocPos = m_pTileProgram->GetUniformLoc(pName: "gPos");
1680 m_pTileProgram->m_LocColor = m_pTileProgram->GetUniformLoc(pName: "gVertColor");
1681 }
1682 if(pCommand->m_pCapabilities->m_2DArrayTextures)
1683 ShaderCompiler.SetTextureReplaceType(CGLSLCompiler::GLSL_COMPILER_TEXTURE_REPLACE_TYPE_2D_ARRAY);
1684 else
1685 ShaderCompiler.SetTextureReplaceType(CGLSLCompiler::GLSL_COMPILER_TEXTURE_REPLACE_TYPE_3D);
1686 {
1687 CGLSL VertexShader;
1688 CGLSL FragmentShader;
1689 ShaderCompiler.AddDefine(pDefineName: "TW_TILE_TEXTURED", pDefineValue: "");
1690 if(!pCommand->m_pCapabilities->m_2DArrayTextures)
1691 ShaderCompiler.AddDefine(pDefineName: "TW_TILE_3D_TEXTURED", pDefineValue: "");
1692 VertexShader.LoadShader(pCompiler: &ShaderCompiler, pStorage: pCommand->m_pStorage, pFile: "shader/tile.vert", GL_VERTEX_SHADER);
1693 FragmentShader.LoadShader(pCompiler: &ShaderCompiler, pStorage: pCommand->m_pStorage, pFile: "shader/tile.frag", GL_FRAGMENT_SHADER);
1694 ShaderCompiler.ClearDefines();
1695
1696 m_pTileProgramTextured->CreateProgram();
1697 m_pTileProgramTextured->AddShader(pShader: &VertexShader);
1698 m_pTileProgramTextured->AddShader(pShader: &FragmentShader);
1699
1700 glBindAttribLocation(m_pTileProgram->GetProgramId(), 0, "inVertex");
1701 glBindAttribLocation(m_pTileProgram->GetProgramId(), 1, "inVertexTexCoord");
1702
1703 m_pTileProgramTextured->LinkProgram();
1704
1705 UseProgram(pProgram: m_pTileProgramTextured);
1706
1707 m_pTileProgramTextured->m_LocPos = m_pTileProgramTextured->GetUniformLoc(pName: "gPos");
1708 m_pTileProgramTextured->m_LocTextureSampler = m_pTileProgramTextured->GetUniformLoc(pName: "gTextureSampler");
1709 m_pTileProgramTextured->m_LocColor = m_pTileProgramTextured->GetUniformLoc(pName: "gVertColor");
1710 }
1711 if(pCommand->m_pCapabilities->m_2DArrayTextures)
1712 ShaderCompiler.SetTextureReplaceType(CGLSLCompiler::GLSL_COMPILER_TEXTURE_REPLACE_TYPE_2D_ARRAY);
1713 else
1714 ShaderCompiler.SetTextureReplaceType(CGLSLCompiler::GLSL_COMPILER_TEXTURE_REPLACE_TYPE_3D);
1715 {
1716 CGLSL VertexShader;
1717 CGLSL FragmentShader;
1718 VertexShader.LoadShader(pCompiler: &ShaderCompiler, pStorage: pCommand->m_pStorage, pFile: "shader/tile_border.vert", GL_VERTEX_SHADER);
1719 FragmentShader.LoadShader(pCompiler: &ShaderCompiler, pStorage: pCommand->m_pStorage, pFile: "shader/tile_border.frag", GL_FRAGMENT_SHADER);
1720 ShaderCompiler.ClearDefines();
1721
1722 m_pBorderTileProgram->CreateProgram();
1723 m_pBorderTileProgram->AddShader(pShader: &VertexShader);
1724 m_pBorderTileProgram->AddShader(pShader: &FragmentShader);
1725
1726 glBindAttribLocation(m_pBorderTileProgram->GetProgramId(), 0, "inVertex");
1727
1728 m_pBorderTileProgram->LinkProgram();
1729
1730 UseProgram(pProgram: m_pBorderTileProgram);
1731
1732 m_pBorderTileProgram->m_LocPos = m_pBorderTileProgram->GetUniformLoc(pName: "gPos");
1733 m_pBorderTileProgram->m_LocColor = m_pBorderTileProgram->GetUniformLoc(pName: "gVertColor");
1734 m_pBorderTileProgram->m_LocOffset = m_pBorderTileProgram->GetUniformLoc(pName: "gOffset");
1735 m_pBorderTileProgram->m_LocScale = m_pBorderTileProgram->GetUniformLoc(pName: "gScale");
1736 }
1737 if(pCommand->m_pCapabilities->m_2DArrayTextures)
1738 ShaderCompiler.SetTextureReplaceType(CGLSLCompiler::GLSL_COMPILER_TEXTURE_REPLACE_TYPE_2D_ARRAY);
1739 else
1740 ShaderCompiler.SetTextureReplaceType(CGLSLCompiler::GLSL_COMPILER_TEXTURE_REPLACE_TYPE_3D);
1741 {
1742 CGLSL VertexShader;
1743 CGLSL FragmentShader;
1744 ShaderCompiler.AddDefine(pDefineName: "TW_TILE_TEXTURED", pDefineValue: "");
1745 if(!pCommand->m_pCapabilities->m_2DArrayTextures)
1746 ShaderCompiler.AddDefine(pDefineName: "TW_TILE_3D_TEXTURED", pDefineValue: "");
1747 VertexShader.LoadShader(pCompiler: &ShaderCompiler, pStorage: pCommand->m_pStorage, pFile: "shader/tile_border.vert", GL_VERTEX_SHADER);
1748 FragmentShader.LoadShader(pCompiler: &ShaderCompiler, pStorage: pCommand->m_pStorage, pFile: "shader/tile_border.frag", GL_FRAGMENT_SHADER);
1749 ShaderCompiler.ClearDefines();
1750
1751 m_pBorderTileProgramTextured->CreateProgram();
1752 m_pBorderTileProgramTextured->AddShader(pShader: &VertexShader);
1753 m_pBorderTileProgramTextured->AddShader(pShader: &FragmentShader);
1754
1755 glBindAttribLocation(m_pBorderTileProgramTextured->GetProgramId(), 0, "inVertex");
1756 glBindAttribLocation(m_pBorderTileProgramTextured->GetProgramId(), 1, "inVertexTexCoord");
1757
1758 m_pBorderTileProgramTextured->LinkProgram();
1759
1760 UseProgram(pProgram: m_pBorderTileProgramTextured);
1761
1762 m_pBorderTileProgramTextured->m_LocPos = m_pBorderTileProgramTextured->GetUniformLoc(pName: "gPos");
1763 m_pBorderTileProgramTextured->m_LocTextureSampler = m_pBorderTileProgramTextured->GetUniformLoc(pName: "gTextureSampler");
1764 m_pBorderTileProgramTextured->m_LocColor = m_pBorderTileProgramTextured->GetUniformLoc(pName: "gVertColor");
1765 m_pBorderTileProgramTextured->m_LocOffset = m_pBorderTileProgramTextured->GetUniformLoc(pName: "gOffset");
1766 m_pBorderTileProgramTextured->m_LocScale = m_pBorderTileProgramTextured->GetUniformLoc(pName: "gScale");
1767 }
1768
1769 glUseProgram(0);
1770 }
1771
1772 if(g_Config.m_Gfx3DTextureAnalysisRan == 0 || str_comp(a: g_Config.m_Gfx3DTextureAnalysisRenderer, b: pCommand->m_pRendererString) != 0 || str_comp(a: g_Config.m_Gfx3DTextureAnalysisVersion, b: pCommand->m_pVersionString) != 0)
1773 {
1774 AnalysisCorrect = IsTileMapAnalysisSucceeded();
1775 if(AnalysisCorrect)
1776 {
1777 g_Config.m_Gfx3DTextureAnalysisRan = 1;
1778 str_copy(dst&: g_Config.m_Gfx3DTextureAnalysisRenderer, src: pCommand->m_pRendererString);
1779 str_copy(dst&: g_Config.m_Gfx3DTextureAnalysisVersion, src: pCommand->m_pVersionString);
1780 }
1781 }
1782 }
1783
1784 if(!AnalysisCorrect || !HasAllFunc)
1785 {
1786 // downgrade to opengl 1.5
1787 *pCommand->m_pInitError = -2;
1788 pCommand->m_pCapabilities->m_ContextMajor = 1;
1789 pCommand->m_pCapabilities->m_ContextMinor = 5;
1790 pCommand->m_pCapabilities->m_ContextPatch = 0;
1791
1792 return false;
1793 }
1794
1795 return true;
1796}
1797
1798void CCommandProcessorFragment_OpenGL2::Cmd_Shutdown(const SCommand_Shutdown *pCommand)
1799{
1800 // TODO: cleanup the OpenGL context too
1801 delete m_pTileProgram;
1802 delete m_pTileProgramTextured;
1803 delete m_pPrimitive3DProgram;
1804 delete m_pPrimitive3DProgramTextured;
1805 for(auto &BufferObject : m_vBufferObjectIndices)
1806 free(ptr: BufferObject.m_pData);
1807}
1808
1809void CCommandProcessorFragment_OpenGL2::Cmd_RenderTex3D(const CCommandBuffer::SCommand_RenderTex3D *pCommand)
1810{
1811 if(m_HasShaders)
1812 {
1813 CGLSLPrimitiveProgram *pProgram = nullptr;
1814 if(IsTexturedState(State: pCommand->m_State))
1815 {
1816 pProgram = m_pPrimitive3DProgramTextured;
1817 }
1818 else
1819 pProgram = m_pPrimitive3DProgram;
1820
1821 UseProgram(pProgram);
1822
1823 SetState(State: pCommand->m_State, pProgram, Use2DArrayTextures: true);
1824 }
1825 else
1826 {
1827 CCommandProcessorFragment_OpenGL::SetState(State: pCommand->m_State, Use2DArrayTextures: true);
1828 }
1829
1830 glEnableClientState(GL_VERTEX_ARRAY);
1831 glEnableClientState(GL_COLOR_ARRAY);
1832 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1833
1834 glVertexPointer(size: 2, GL_FLOAT, stride: sizeof(pCommand->m_pVertices[0]), pointer: pCommand->m_pVertices);
1835 glColorPointer(size: 4, GL_UNSIGNED_BYTE, stride: sizeof(pCommand->m_pVertices[0]), pointer: (uint8_t *)pCommand->m_pVertices + (ptrdiff_t)(sizeof(vec2)));
1836 glTexCoordPointer(size: 3, GL_FLOAT, stride: sizeof(pCommand->m_pVertices[0]), pointer: (uint8_t *)pCommand->m_pVertices + (ptrdiff_t)(sizeof(vec2) + sizeof(unsigned char) * 4));
1837
1838 switch(pCommand->m_PrimType)
1839 {
1840 case EPrimitiveType::QUADS:
1841 glDrawArrays(GL_QUADS, first: 0, count: pCommand->m_PrimCount * 4);
1842 break;
1843 case EPrimitiveType::TRIANGLES:
1844 glDrawArrays(GL_TRIANGLES, first: 0, count: pCommand->m_PrimCount * 3);
1845 break;
1846 default:
1847 dbg_assert_failed("Invalid primitive type: %d", (int)pCommand->m_PrimType);
1848 };
1849
1850 glDisableClientState(GL_VERTEX_ARRAY);
1851 glDisableClientState(GL_COLOR_ARRAY);
1852 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1853
1854 if(m_HasShaders)
1855 {
1856 glUseProgram(0);
1857 }
1858}
1859
1860void CCommandProcessorFragment_OpenGL2::Cmd_CreateBufferObject(const CCommandBuffer::SCommand_CreateBufferObject *pCommand)
1861{
1862 void *pUploadData = pCommand->m_pUploadData;
1863 const int Index = pCommand->m_BufferIndex;
1864 // create necessary space
1865 if((size_t)Index >= m_vBufferObjectIndices.size())
1866 {
1867 m_vBufferObjectIndices.resize(sz: Index + 1, c: 0);
1868 }
1869
1870 GLuint VertBufferId = 0;
1871
1872 glGenBuffers(1, &VertBufferId);
1873 glBindBuffer(GL_ARRAY_BUFFER, VertBufferId);
1874 glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)(pCommand->m_DataSize), pUploadData, GL_STATIC_DRAW);
1875 glBindBuffer(GL_ARRAY_BUFFER, 0);
1876
1877 SBufferObject &BufferObject = m_vBufferObjectIndices[Index];
1878 BufferObject.m_BufferObjectId = VertBufferId;
1879 BufferObject.m_DataSize = pCommand->m_DataSize;
1880 BufferObject.m_pData = static_cast<uint8_t *>(malloc(size: pCommand->m_DataSize));
1881 if(pUploadData)
1882 mem_copy(dest: BufferObject.m_pData, source: pUploadData, size: pCommand->m_DataSize);
1883
1884 if(pCommand->m_DeletePointer)
1885 free(ptr: pUploadData);
1886}
1887
1888void CCommandProcessorFragment_OpenGL2::Cmd_RecreateBufferObject(const CCommandBuffer::SCommand_RecreateBufferObject *pCommand)
1889{
1890 void *pUploadData = pCommand->m_pUploadData;
1891 int Index = pCommand->m_BufferIndex;
1892 SBufferObject &BufferObject = m_vBufferObjectIndices[Index];
1893
1894 glBindBuffer(GL_ARRAY_BUFFER, BufferObject.m_BufferObjectId);
1895 glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)(pCommand->m_DataSize), pUploadData, GL_STATIC_DRAW);
1896 glBindBuffer(GL_ARRAY_BUFFER, 0);
1897
1898 BufferObject.m_DataSize = pCommand->m_DataSize;
1899 free(ptr: BufferObject.m_pData);
1900 BufferObject.m_pData = static_cast<uint8_t *>(malloc(size: pCommand->m_DataSize));
1901 if(pUploadData)
1902 mem_copy(dest: BufferObject.m_pData, source: pUploadData, size: pCommand->m_DataSize);
1903
1904 if(pCommand->m_DeletePointer)
1905 free(ptr: pUploadData);
1906}
1907
1908void CCommandProcessorFragment_OpenGL2::Cmd_UpdateBufferObject(const CCommandBuffer::SCommand_UpdateBufferObject *pCommand)
1909{
1910 void *pUploadData = pCommand->m_pUploadData;
1911 int Index = pCommand->m_BufferIndex;
1912 SBufferObject &BufferObject = m_vBufferObjectIndices[Index];
1913
1914 glBindBuffer(GL_ARRAY_BUFFER, BufferObject.m_BufferObjectId);
1915 glBufferSubData(GL_ARRAY_BUFFER, (GLintptr)(pCommand->m_pOffset), (GLsizeiptr)(pCommand->m_DataSize), pUploadData);
1916 glBindBuffer(GL_ARRAY_BUFFER, 0);
1917
1918 if(pUploadData)
1919 mem_copy(dest: BufferObject.m_pData + (ptrdiff_t)pCommand->m_pOffset, source: pUploadData, size: pCommand->m_DataSize);
1920
1921 if(pCommand->m_DeletePointer)
1922 free(ptr: pUploadData);
1923}
1924
1925void CCommandProcessorFragment_OpenGL2::Cmd_CopyBufferObject(const CCommandBuffer::SCommand_CopyBufferObject *pCommand)
1926{
1927 int WriteIndex = pCommand->m_WriteBufferIndex;
1928 int ReadIndex = pCommand->m_ReadBufferIndex;
1929
1930 SBufferObject &ReadBufferObject = m_vBufferObjectIndices[ReadIndex];
1931 SBufferObject &WriteBufferObject = m_vBufferObjectIndices[WriteIndex];
1932
1933 mem_copy(dest: WriteBufferObject.m_pData + (ptrdiff_t)pCommand->m_WriteOffset, source: ReadBufferObject.m_pData + (ptrdiff_t)pCommand->m_ReadOffset, size: pCommand->m_CopySize);
1934
1935 glBindBuffer(GL_ARRAY_BUFFER, WriteBufferObject.m_BufferObjectId);
1936 glBufferSubData(GL_ARRAY_BUFFER, (GLintptr)(pCommand->m_WriteOffset), (GLsizeiptr)(pCommand->m_CopySize), WriteBufferObject.m_pData + (ptrdiff_t)pCommand->m_WriteOffset);
1937 glBindBuffer(GL_ARRAY_BUFFER, 0);
1938}
1939
1940void CCommandProcessorFragment_OpenGL2::Cmd_DeleteBufferObject(const CCommandBuffer::SCommand_DeleteBufferObject *pCommand)
1941{
1942 int Index = pCommand->m_BufferIndex;
1943 SBufferObject &BufferObject = m_vBufferObjectIndices[Index];
1944
1945 glDeleteBuffers(1, &BufferObject.m_BufferObjectId);
1946
1947 free(ptr: BufferObject.m_pData);
1948 BufferObject.m_pData = nullptr;
1949}
1950
1951void CCommandProcessorFragment_OpenGL2::Cmd_CreateBufferContainer(const CCommandBuffer::SCommand_CreateBufferContainer *pCommand)
1952{
1953 const int Index = pCommand->m_BufferContainerIndex;
1954 // create necessary space
1955 if((size_t)Index >= m_vBufferContainers.size())
1956 {
1957 SBufferContainer Container;
1958 Container.m_ContainerInfo.m_Stride = 0;
1959 Container.m_ContainerInfo.m_VertBufferBindingIndex = -1;
1960 m_vBufferContainers.resize(sz: Index + 1, c: Container);
1961 }
1962
1963 SBufferContainer &BufferContainer = m_vBufferContainers[Index];
1964
1965 for(size_t i = 0; i < pCommand->m_AttrCount; ++i)
1966 {
1967 BufferContainer.m_ContainerInfo.m_vAttributes.push_back(x: pCommand->m_pAttributes[i]);
1968 }
1969
1970 BufferContainer.m_ContainerInfo.m_Stride = pCommand->m_Stride;
1971 BufferContainer.m_ContainerInfo.m_VertBufferBindingIndex = pCommand->m_VertBufferBindingIndex;
1972}
1973
1974void CCommandProcessorFragment_OpenGL2::Cmd_UpdateBufferContainer(const CCommandBuffer::SCommand_UpdateBufferContainer *pCommand)
1975{
1976 SBufferContainer &BufferContainer = m_vBufferContainers[pCommand->m_BufferContainerIndex];
1977
1978 BufferContainer.m_ContainerInfo.m_vAttributes.clear();
1979
1980 for(size_t i = 0; i < pCommand->m_AttrCount; ++i)
1981 {
1982 BufferContainer.m_ContainerInfo.m_vAttributes.push_back(x: pCommand->m_pAttributes[i]);
1983 }
1984
1985 BufferContainer.m_ContainerInfo.m_Stride = pCommand->m_Stride;
1986 BufferContainer.m_ContainerInfo.m_VertBufferBindingIndex = pCommand->m_VertBufferBindingIndex;
1987}
1988
1989void CCommandProcessorFragment_OpenGL2::Cmd_DeleteBufferContainer(const CCommandBuffer::SCommand_DeleteBufferContainer *pCommand)
1990{
1991 SBufferContainer &BufferContainer = m_vBufferContainers[pCommand->m_BufferContainerIndex];
1992
1993 if(pCommand->m_DestroyAllBO)
1994 {
1995 int VertBufferId = BufferContainer.m_ContainerInfo.m_VertBufferBindingIndex;
1996 if(VertBufferId != -1)
1997 {
1998 glDeleteBuffers(1, &m_vBufferObjectIndices[VertBufferId].m_BufferObjectId);
1999
2000 free(ptr: m_vBufferObjectIndices[VertBufferId].m_pData);
2001 m_vBufferObjectIndices[VertBufferId].m_pData = nullptr;
2002 }
2003 }
2004
2005 BufferContainer.m_ContainerInfo.m_vAttributes.clear();
2006}
2007
2008void CCommandProcessorFragment_OpenGL2::Cmd_IndicesRequiredNumNotify(const CCommandBuffer::SCommand_IndicesRequiredNumNotify *pCommand)
2009{
2010}
2011
2012void CCommandProcessorFragment_OpenGL2::Cmd_RenderBorderTile(const CCommandBuffer::SCommand_RenderBorderTile *pCommand)
2013{
2014 int Index = pCommand->m_BufferContainerIndex;
2015 // if space not there return
2016 if((size_t)Index >= m_vBufferContainers.size())
2017 return;
2018
2019 SBufferContainer &BufferContainer = m_vBufferContainers[Index];
2020
2021 CGLSLTileProgram *pProgram = nullptr;
2022 if(IsTexturedState(State: pCommand->m_State))
2023 pProgram = m_pBorderTileProgramTextured;
2024 else
2025 pProgram = m_pBorderTileProgram;
2026 UseProgram(pProgram);
2027
2028 SetState(State: pCommand->m_State, pProgram, Use2DArrayTextures: true);
2029 pProgram->SetUniformVec4(Loc: pProgram->m_LocColor, Count: 1, pValue: (float *)&pCommand->m_Color);
2030
2031 pProgram->SetUniformVec2(Loc: pProgram->m_LocOffset, Count: 1, pValue: (float *)&pCommand->m_Offset);
2032 pProgram->SetUniformVec2(Loc: pProgram->m_LocScale, Count: 1, pValue: (float *)&pCommand->m_Scale);
2033
2034 bool IsTextured = BufferContainer.m_ContainerInfo.m_vAttributes.size() == 2;
2035
2036 SBufferObject &BufferObject = m_vBufferObjectIndices[(size_t)BufferContainer.m_ContainerInfo.m_VertBufferBindingIndex];
2037
2038 glBindBuffer(GL_ARRAY_BUFFER, BufferObject.m_BufferObjectId);
2039
2040 glEnableVertexAttribArray(0);
2041 glVertexAttribPointer(0, 2, GL_FLOAT, false, BufferContainer.m_ContainerInfo.m_Stride, BufferContainer.m_ContainerInfo.m_vAttributes[0].m_pOffset);
2042 if(IsTextured)
2043 {
2044 glEnableVertexAttribArray(1);
2045 glVertexAttribIPointer(1, 4, GL_UNSIGNED_BYTE, BufferContainer.m_ContainerInfo.m_Stride, BufferContainer.m_ContainerInfo.m_vAttributes[1].m_pOffset);
2046 }
2047
2048 size_t RealDrawCount = pCommand->m_DrawNum * 4;
2049 GLint RealOffset = (GLint)((((size_t)(uintptr_t)(pCommand->m_pIndicesOffset)) / (6 * sizeof(unsigned int))) * 4);
2050 glDrawArrays(GL_QUADS, first: RealOffset, count: RealDrawCount);
2051
2052 glDisableVertexAttribArray(0);
2053 if(IsTextured)
2054 glDisableVertexAttribArray(1);
2055 glBindBuffer(GL_ARRAY_BUFFER, 0);
2056 glUseProgram(0);
2057}
2058
2059void CCommandProcessorFragment_OpenGL2::Cmd_RenderTileLayer(const CCommandBuffer::SCommand_RenderTileLayer *pCommand)
2060{
2061 int Index = pCommand->m_BufferContainerIndex;
2062 // if space not there return
2063 if((size_t)Index >= m_vBufferContainers.size())
2064 return;
2065
2066 SBufferContainer &BufferContainer = m_vBufferContainers[Index];
2067
2068 if(pCommand->m_IndicesDrawNum == 0)
2069 {
2070 return; // nothing to draw
2071 }
2072
2073 CGLSLTileProgram *pProgram = nullptr;
2074 if(IsTexturedState(State: pCommand->m_State))
2075 {
2076 pProgram = m_pTileProgramTextured;
2077 }
2078 else
2079 pProgram = m_pTileProgram;
2080
2081 UseProgram(pProgram);
2082
2083 SetState(State: pCommand->m_State, pProgram, Use2DArrayTextures: true);
2084 pProgram->SetUniformVec4(Loc: pProgram->m_LocColor, Count: 1, pValue: (float *)&pCommand->m_Color);
2085
2086 bool IsTextured = BufferContainer.m_ContainerInfo.m_vAttributes.size() == 2;
2087
2088 SBufferObject &BufferObject = m_vBufferObjectIndices[(size_t)BufferContainer.m_ContainerInfo.m_VertBufferBindingIndex];
2089
2090 glBindBuffer(GL_ARRAY_BUFFER, BufferObject.m_BufferObjectId);
2091
2092 glEnableVertexAttribArray(0);
2093 glVertexAttribPointer(0, 2, GL_FLOAT, false, BufferContainer.m_ContainerInfo.m_Stride, BufferContainer.m_ContainerInfo.m_vAttributes[0].m_pOffset);
2094 if(IsTextured)
2095 {
2096 glEnableVertexAttribArray(1);
2097 glVertexAttribIPointer(1, 4, GL_UNSIGNED_BYTE, BufferContainer.m_ContainerInfo.m_Stride, BufferContainer.m_ContainerInfo.m_vAttributes[1].m_pOffset);
2098 }
2099
2100 for(int i = 0; i < pCommand->m_IndicesDrawNum; ++i)
2101 {
2102 size_t RealDrawCount = (pCommand->m_pDrawCount[i] / 6) * 4;
2103 GLint RealOffset = (GLint)((((size_t)(uintptr_t)(pCommand->m_pIndicesOffsets[i])) / (6 * sizeof(unsigned int))) * 4);
2104 glDrawArrays(GL_QUADS, first: RealOffset, count: RealDrawCount);
2105 }
2106
2107 glDisableVertexAttribArray(0);
2108 if(IsTextured)
2109 glDisableVertexAttribArray(1);
2110 glBindBuffer(GL_ARRAY_BUFFER, 0);
2111 glUseProgram(0);
2112}
2113
2114#undef BACKEND_GL_MODERN_API
2115
2116#endif
2117
2118#endif
2119