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