|
18 | 18 | static EGLDisplay g_eglDisplay = EGL_NO_DISPLAY; |
19 | 19 | static EGLContext g_eglContext = EGL_NO_CONTEXT; |
20 | 20 | static EGLSurface g_eglSurface = EGL_NO_SURFACE; |
| 21 | +static EGLConfig g_eglConfig = nullptr; // saved so the pbuffer can be recreated on resize |
21 | 22 | static void* g_metalLayer = nullptr; // CAMetalLayer attached to the window's NSView |
22 | 23 | static int g_pbufW = 1280; // pbuffer (default framebuffer) dimensions |
23 | 24 | static int g_pbufH = 720; |
@@ -162,6 +163,7 @@ static bool InitEGLContext(SDL_Window* window, int major, int minor) { |
162 | 163 | (int)cfgOk, numConfigs, eglGetError()); |
163 | 164 | if (!cfgOk || numConfigs == 0) |
164 | 165 | return false; |
| 166 | + g_eglConfig = eglConfig; // remember for pbuffer recreation on window resize |
165 | 167 |
|
166 | 168 | void* nativeView = GetNSViewFromSDLWindow(window); |
167 | 169 | g_metalLayer = nativeView; // CAMetalLayer for the manual present path |
@@ -314,6 +316,52 @@ static void DestroyEGLContext() { |
314 | 316 | g_eglContext = EGL_NO_CONTEXT; |
315 | 317 | g_eglSurface = EGL_NO_SURFACE; |
316 | 318 | } |
| 319 | + |
| 320 | +// Recreate the pbuffer (the engine's default framebuffer) at the window's |
| 321 | +// current pixel size so the window resizes at true resolution instead of |
| 322 | +// scaling a fixed-size buffer. Called from ReadWindowPosAndSize on resize, |
| 323 | +// before winSize is bound to g_pbufW. No-op if the size is unchanged or the |
| 324 | +// EGL context is not up yet. The CAMetalLayer drawable auto-follows because |
| 325 | +// the present path sizes it from g_pbufW/g_pbufH each frame. |
| 326 | +static void ResizeEGLSurfaceIfNeeded(SDL_Window* window) { |
| 327 | + if (g_eglDisplay == EGL_NO_DISPLAY || g_eglContext == EGL_NO_CONTEXT || g_eglConfig == nullptr) |
| 328 | + return; |
| 329 | + |
| 330 | + int winW = 0, winH = 0; |
| 331 | + SDL_GetWindowSize(window, &winW, &winH); |
| 332 | + if (winW <= 0 || winH <= 0) |
| 333 | + return; |
| 334 | + |
| 335 | + const bool noRetina = (getenv("SPRING_MAC_NO_RETINA") != nullptr); |
| 336 | + const double bsf = noRetina ? 1.0 : GetBackingScaleFactor(window); |
| 337 | + const int pxW = std::max(1, int(std::lround(double(winW) * bsf))); |
| 338 | + const int pxH = std::max(1, int(std::lround(double(winH) * bsf))); |
| 339 | + |
| 340 | + if (pxW == g_pbufW && pxH == g_pbufH) |
| 341 | + return; // size unchanged |
| 342 | + |
| 343 | + EGLint pbAttribs[] = { EGL_WIDTH, pxW, EGL_HEIGHT, pxH, EGL_NONE }; |
| 344 | + EGLSurface newSurface = eglCreatePbufferSurface(g_eglDisplay, g_eglConfig, pbAttribs); |
| 345 | + if (newSurface == EGL_NO_SURFACE) { |
| 346 | + fprintf(stderr, "[EGL] resize: eglCreatePbufferSurface %dx%d failed (0x%x); keeping %dx%d\n", |
| 347 | + pxW, pxH, eglGetError(), g_pbufW, g_pbufH); |
| 348 | + return; // keep the old surface rather than lose the context |
| 349 | + } |
| 350 | + |
| 351 | + if (!eglMakeCurrent(g_eglDisplay, newSurface, newSurface, g_eglContext)) { |
| 352 | + fprintf(stderr, "[EGL] resize: eglMakeCurrent failed (0x%x); reverting\n", eglGetError()); |
| 353 | + eglMakeCurrent(g_eglDisplay, g_eglSurface, g_eglSurface, g_eglContext); |
| 354 | + eglDestroySurface(g_eglDisplay, newSurface); |
| 355 | + return; |
| 356 | + } |
| 357 | + |
| 358 | + if (g_eglSurface != EGL_NO_SURFACE) |
| 359 | + eglDestroySurface(g_eglDisplay, g_eglSurface); |
| 360 | + g_eglSurface = newSurface; |
| 361 | + g_pbufW = pxW; |
| 362 | + g_pbufH = pxH; |
| 363 | + fprintf(stderr, "[EGL] resize: pbuffer -> %dx%d px (window %dx%d * %.2f)\n", pxW, pxH, winW, winH, bsf); |
| 364 | +} |
317 | 365 | #endif // __APPLE__ && !HEADLESS |
318 | 366 |
|
319 | 367 | #include "GlobalRenderingInfo.h" |
@@ -2249,6 +2297,12 @@ void CGlobalRendering::ReadWindowPosAndSize() |
2249 | 2297 | SDL_GetWindowSize(sdlWindow, &winSizeX, &winSizeY); |
2250 | 2298 | #if defined(__APPLE__) && !defined(HEADLESS) |
2251 | 2299 | { |
| 2300 | + // True dynamic-resolution resize: recreate the pbuffer FBO at the |
| 2301 | + // window's current pixel size, so winSize below binds to the NEW size |
| 2302 | + // and the engine renders at the new resolution (rather than scaling a |
| 2303 | + // fixed startup buffer). No-op if unchanged or pre-EGL-init. |
| 2304 | + ResizeEGLSurfaceIfNeeded(sdlWindow); |
| 2305 | + |
2252 | 2306 | // Engine contract: winSize/viewSize must match the pbuffer FBO the |
2253 | 2307 | // engine actually renders into — *not* the CAMetalLayer drawableSize. |
2254 | 2308 | // They are equal by accident at full Retina (drawable == backing == |
|
0 commit comments