SDL: render: Remove the logical presentation render target.

From 54459def695b2fea70d9ef01585cae5e162f3f72 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Mon, 16 Sep 2024 13:33:16 -0400
Subject: [PATCH] render: Remove the logical presentation render target.

Now we render directly to the window, scaling as appropriate. This fixes some
concerns the render target introduced, like the quality of the final scaled
output, how to step outside of the logical size temporarily to draw some
things sharply at the native resolution, and loss of sub-pixel precision.

Fixes #8736.
---
 build-scripts/SDL_migration.cocci             |   4 +-
 examples/renderer/02-primitives/primitives.c  |   2 +-
 .../renderer/17-read-pixels/read-pixels.c     |  38 +-
 include/SDL3/SDL_render.h                     |  25 +-
 include/SDL3/SDL_test_common.h                |   1 -
 src/dynapi/SDL_dynapi_procs.h                 |   4 +-
 src/render/SDL_render.c                       | 465 ++++++++----------
 src/render/SDL_sysrender.h                    |   7 +-
 src/render/opengl/SDL_render_gl.c             |  12 +-
 src/render/opengles2/SDL_render_gles2.c       |  11 +-
 src/render/vitagxm/SDL_render_vita_gxm.c      |  11 +-
 src/test/SDL_test_common.c                    |  39 +-
 test/testautomation_render.c                  |  56 +--
 test/testcontroller.c                         |   3 +-
 test/testime.c                                |   2 +-
 test/testspriteminimal.c                      |   4 +-
 16 files changed, 295 insertions(+), 389 deletions(-)

diff --git a/build-scripts/SDL_migration.cocci b/build-scripts/SDL_migration.cocci
index 0483a40bb2ff2..9e3a4d6a24a0b 100644
--- a/build-scripts/SDL_migration.cocci
+++ b/build-scripts/SDL_migration.cocci
@@ -1761,10 +1761,10 @@ expression e2;
 @@
 (
 - SDL_RenderSetLogicalSize(renderer, 0, 0)
-+ SDL_SetRenderLogicalPresentation(renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED, SDL_ScaleModeNearest)
++ SDL_SetRenderLogicalPresentation(renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED)
 |
 - SDL_RenderSetLogicalSize(renderer, e1, e2)
-+ SDL_SetRenderLogicalPresentation(renderer, e1, e2, SDL_LOGICAL_PRESENTATION_LETTERBOX, SDL_ScaleModeLinear)
++ SDL_SetRenderLogicalPresentation(renderer, e1, e2, SDL_LOGICAL_PRESENTATION_LETTERBOX)
 )
 @@
 @@
diff --git a/examples/renderer/02-primitives/primitives.c b/examples/renderer/02-primitives/primitives.c
index 1222b86f14e7c..28cba4396c9e6 100644
--- a/examples/renderer/02-primitives/primitives.c
+++ b/examples/renderer/02-primitives/primitives.c
@@ -55,7 +55,7 @@ SDL_AppResult SDL_AppIterate(void *appstate)
     SDL_FRect rect;
 
     /* as you can see from this, rendering draws over whatever was drawn before it. */
-    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);  /* black, full alpha */
+    SDL_SetRenderDrawColor(renderer, 33, 33, 33, 255);  /* dark gray, full alpha */
     SDL_RenderClear(renderer);  /* start with a blank canvas. */
 
     /* draw a filled rectangle in the middle of the canvas. */
diff --git a/examples/renderer/17-read-pixels/read-pixels.c b/examples/renderer/17-read-pixels/read-pixels.c
index 476588f66b932..35af871e6ed75 100644
--- a/examples/renderer/17-read-pixels/read-pixels.c
+++ b/examples/renderer/17-read-pixels/read-pixels.c
@@ -19,9 +19,11 @@
 static SDL_Window *window = NULL;
 static SDL_Renderer *renderer = NULL;
 static SDL_Texture *texture = NULL;
-static SDL_Texture *converted_texture = NULL;
 static int texture_width = 0;
 static int texture_height = 0;
+static SDL_Texture *converted_texture = NULL;
+static int converted_texture_width = 0;
+static int converted_texture_height = 0;
 
 #define WINDOW_WIDTH 640
 #define WINDOW_HEIGHT 480
@@ -68,12 +70,6 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
 
     SDL_DestroySurface(surface);  /* done with this, the texture has a copy of the pixels now. */
 
-    converted_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, WINDOW_WIDTH, WINDOW_HEIGHT);
-    if (!texture) {
-        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create conversion texture!", SDL_GetError(), NULL);
-        return SDL_APP_FAILURE;
-    }
-
     return SDL_APP_CONTINUE;  /* carry on with the program! */
 }
 
@@ -111,8 +107,13 @@ SDL_AppResult SDL_AppIterate(void *appstate)
     center.y = texture_height / 2.0f;
     SDL_RenderTextureRotated(renderer, texture, NULL, &dst_rect, rotation, &center, SDL_FLIP_NONE);
 
-    /* this whole thing is _super_ expensive. Seriously, don't do this in real life. */
+    /* this next whole thing is _super_ expensive. Seriously, don't do this in real life. */
+
+    /* Download the pixels of what has just been rendered. This has to wait for the GPU to finish rendering it and everything before it,
+       and then make an expensive copy from the GPU to system RAM! */
     surface = SDL_RenderReadPixels(renderer, NULL);
+
+    /* This is also expensive, but easier: convert the pixels to a format we want. */
     if (surface && (surface->format != SDL_PIXELFORMAT_RGBA8888) && (surface->format != SDL_PIXELFORMAT_BGRA8888)) {
         SDL_Surface *converted = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA8888);
         SDL_DestroySurface(surface);
@@ -120,6 +121,18 @@ SDL_AppResult SDL_AppIterate(void *appstate)
     }
 
     if (surface) {
+        /* Rebuild converted_texture if the dimensions have changed (window resized, etc). */
+        if ((surface->w != converted_texture_width) || (surface->h != converted_texture_height)) {
+            SDL_DestroyTexture(converted_texture);
+            converted_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, surface->w, surface->h);
+            if (!converted_texture) {
+                SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't (re)create conversion texture!", SDL_GetError(), NULL);
+                return SDL_APP_FAILURE;
+            }
+            converted_texture_width = surface->w;
+            converted_texture_height = surface->h;
+        }
+
         /* Turn each pixel into either black or white. This is a lousy technique but it works here.
            In real life, something like Floyd-Steinberg dithering might work
            better: https://en.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering*/
@@ -130,9 +143,10 @@ SDL_AppResult SDL_AppIterate(void *appstate)
                 Uint8 *p = (Uint8 *) (&pixels[x]);
                 const Uint32 average = (((Uint32) p[1]) + ((Uint32) p[2]) + ((Uint32) p[3])) / 3;
                 if (average == 0) {
-                    p[0] = 0;  // turn off alpha for pure black pixels.
+                    p[0] = p[3] = 0xFF; p[1] = p[2] = 0;  /* make pure black pixels red. */
+                } else {
+                    p[1] = p[2] = p[3] = (average > 50) ? 0xFF : 0x00;  /* make everything else either black or white. */
                 }
-                p[1] = p[2] = p[3] = (average > 50) ? 0xFF : 0x00;
             }
         }
 
@@ -142,8 +156,8 @@ SDL_AppResult SDL_AppIterate(void *appstate)
 
         /* draw the texture to the top-left of the screen. */
         dst_rect.x = dst_rect.y = 0.0f;
-        dst_rect.w = ((float) WINDOW_WIDTH) / 2.0f;
-        dst_rect.h = ((float) WINDOW_WIDTH) / 2.0f;
+        dst_rect.w = ((float) WINDOW_WIDTH) / 4.0f;
+        dst_rect.h = ((float) WINDOW_HEIGHT) / 4.0f;
         SDL_RenderTexture(renderer, converted_texture, NULL, &dst_rect);
     }
 
diff --git a/include/SDL3/SDL_render.h b/include/SDL3/SDL_render.h
index f0fd89733fae2..3d7167ea22f08 100644
--- a/include/SDL3/SDL_render.h
+++ b/include/SDL3/SDL_render.h
@@ -1266,13 +1266,23 @@ extern SDL_DECLSPEC SDL_Texture * SDLCALL SDL_GetRenderTarget(SDL_Renderer *rend
 /**
  * Set a device independent resolution and presentation mode for rendering.
  *
- * This function sets the width and height of the logical rendering output. A
- * render target is created at the specified size and used for rendering and
- * then copied to the output during presentation.
+ * This function sets the width and height of the logical rendering output.
+ * The renderer will act as if the window is always the requested dimensions,
+ * scaling to the actual window resolution as necessary.
+ *
+ * This can be useful for games that expect a fixed size, but would like to
+ * scale the output to whatever is available, regardless of how a user resizes
+ * a window, or if the display is high DPI.
  *
  * You can disable logical coordinates by setting the mode to
  * SDL_LOGICAL_PRESENTATION_DISABLED, and in that case you get the full pixel
- * resolution of the output window.
+ * resolution of the output window; it is safe to toggle logical presentation
+ * during the rendering of a frame: perhaps most of the rendering is done to
+ * specific dimensions but to make fonts look sharp, the app turns off logical
+ * presentation while drawing text.
+ *
+ * Letterboxing will only happen if logical presentation is enabled during
+ * SDL_RenderPresent; be sure to reenable it first if you were using it.
  *
  * You can convert coordinates in an event into rendering coordinates using
  * SDL_ConvertEventToRenderCoordinates().
@@ -1281,7 +1291,6 @@ extern SDL_DECLSPEC SDL_Texture * SDLCALL SDL_GetRenderTarget(SDL_Renderer *rend
  * \param w the width of the logical resolution.
  * \param h the height of the logical resolution.
  * \param mode the presentation mode used.
- * \param scale_mode the scale mode used.
  * \returns true on success or false on failure; call SDL_GetError() for more
  *          information.
  *
@@ -1291,7 +1300,7 @@ extern SDL_DECLSPEC SDL_Texture * SDLCALL SDL_GetRenderTarget(SDL_Renderer *rend
  * \sa SDL_GetRenderLogicalPresentation
  * \sa SDL_GetRenderLogicalPresentationRect
  */
-extern SDL_DECLSPEC bool SDLCALL SDL_SetRenderLogicalPresentation(SDL_Renderer *renderer, int w, int h, SDL_RendererLogicalPresentation mode, SDL_ScaleMode scale_mode);
+extern SDL_DECLSPEC bool SDLCALL SDL_SetRenderLogicalPresentation(SDL_Renderer *renderer, int w, int h, SDL_RendererLogicalPresentation mode);
 
 /**
  * Get device independent resolution and presentation mode for rendering.
@@ -1302,8 +1311,6 @@ extern SDL_DECLSPEC bool SDLCALL SDL_SetRenderLogicalPresentation(SDL_Renderer *
  * \param renderer the rendering context.
  * \param w an int to be filled with the width.
  * \param h an int to be filled with the height.
- * \param mode a pointer filled in with the presentation mode.
- * \param scale_mode a pointer filled in with the scale mode.
  * \returns true on success or false on failure; call SDL_GetError() for more
  *          information.
  *
@@ -1311,7 +1318,7 @@ extern SDL_DECLSPEC bool SDLCALL SDL_SetRenderLogicalPresentation(SDL_Renderer *
  *
  * \sa SDL_SetRenderLogicalPresentation
  */
-extern SDL_DECLSPEC bool SDLCALL SDL_GetRenderLogicalPresentation(SDL_Renderer *renderer, int *w, int *h, SDL_RendererLogicalPresentation *mode, SDL_ScaleMode *scale_mode);
+extern SDL_DECLSPEC bool SDLCALL SDL_GetRenderLogicalPresentation(SDL_Renderer *renderer, int *w, int *h, SDL_RendererLogicalPresentation *mode);
 
 /**
  * Get the final presentation rectangle for rendering.
diff --git a/include/SDL3/SDL_test_common.h b/include/SDL3/SDL_test_common.h
index 69f63765f4fc5..0e7e1accdb857 100644
--- a/include/SDL3/SDL_test_common.h
+++ b/include/SDL3/SDL_test_common.h
@@ -103,7 +103,6 @@ typedef struct
     int logical_h;
     bool auto_scale_content;
     SDL_RendererLogicalPresentation logical_presentation;
-    SDL_ScaleMode logical_scale_mode;
     float scale;
     int depth;
     float refresh_rate;
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index 051f0b9ce4bbe..ae93bf3e0ece3 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -499,7 +499,7 @@ SDL_DYNAPI_PROC(bool,SDL_GetRenderDrawBlendMode,(SDL_Renderer *a, SDL_BlendMode
 SDL_DYNAPI_PROC(bool,SDL_GetRenderDrawColor,(SDL_Renderer *a, Uint8 *b, Uint8 *c, Uint8 *d, Uint8 *e),(a,b,c,d,e),return)
 SDL_DYNAPI_PROC(bool,SDL_GetRenderDrawColorFloat,(SDL_Renderer *a, float *b, float *c, float *d, float *e),(a,b,c,d,e),return)
 SDL_DYNAPI_PROC(const char*,SDL_GetRenderDriver,(int a),(a),return)
-SDL_DYNAPI_PROC(bool,SDL_GetRenderLogicalPresentation,(SDL_Renderer *a, int *b, int *c, SDL_RendererLogicalPresentation *d, SDL_ScaleMode *e),(a,b,c,d,e),return)
+SDL_DYNAPI_PROC(bool,SDL_GetRenderLogicalPresentation,(SDL_Renderer *a, int *b, int *c, SDL_RendererLogicalPresentation *d),(a,b,c,d),return)
 SDL_DYNAPI_PROC(bool,SDL_GetRenderLogicalPresentationRect,(SDL_Renderer *a, SDL_FRect *b),(a,b),return)
 SDL_DYNAPI_PROC(void*,SDL_GetRenderMetalCommandEncoder,(SDL_Renderer *a),(a),return)
 SDL_DYNAPI_PROC(void*,SDL_GetRenderMetalLayer,(SDL_Renderer *a),(a),return)
@@ -879,7 +879,7 @@ SDL_DYNAPI_PROC(bool,SDL_SetRenderColorScale,(SDL_Renderer *a, float b),(a,b),re
 SDL_DYNAPI_PROC(bool,SDL_SetRenderDrawBlendMode,(SDL_Renderer *a, SDL_BlendMode b),(a,b),return)
 SDL_DYNAPI_PROC(bool,SDL_SetRenderDrawColor,(SDL_Renderer *a, Uint8 b, Uint8 c, Uint8 d, Uint8 e),(a,b,c,d,e),return)
 SDL_DYNAPI_PROC(bool,SDL_SetRenderDrawColorFloat,(SDL_Renderer *a, float b, float c, float d, float e),(a,b,c,d,e),return)
-SDL_DYNAPI_PROC(bool,SDL_SetRenderLogicalPresentation,(SDL_Renderer *a, int b, int c, SDL_RendererLogicalPresentation d, SDL_ScaleMode e),(a,b,c,d,e),return)
+SDL_DYNAPI_PROC(bool,SDL_SetRenderLogicalPresentation,(SDL_Renderer *a, int b, int c, SDL_RendererLogicalPresentation d),(a,b,c,d),return)
 SDL_DYNAPI_PROC(bool,SDL_SetRenderScale,(SDL_Renderer *a, float b, float c),(a,b,c),return)
 SDL_DYNAPI_PROC(bool,SDL_SetRenderTarget,(SDL_Renderer *a, SDL_Texture *b),(a,b),return)
 SDL_DYNAPI_PROC(bool,SDL_SetRenderVSync,(SDL_Renderer *a, int b),(a,b),return)
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index a17365aa289d6..a7c534fdea0df 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -415,15 +415,15 @@ static SDL_RenderCommand *AllocateRenderCommand(SDL_Renderer *renderer)
 
 static void UpdatePixelViewport(SDL_Renderer *renderer, SDL_RenderViewState *view)
 {
-    view->pixel_viewport.x = (int)SDL_floorf(view->viewport.x * view->scale.x);
-    view->pixel_viewport.y = (int)SDL_floorf(view->viewport.y * view->scale.y);
+    view->pixel_viewport.x = (int)SDL_floorf((view->viewport.x * view->current_scale.x) + view->logical_offset.x);
+    view->pixel_viewport.y = (int)SDL_floorf((view->viewport.y * view->current_scale.y) + view->logical_offset.y);
     if (view->viewport.w >= 0) {
-        view->pixel_viewport.w = (int)SDL_ceilf(view->viewport.w * view->scale.x);
+        view->pixel_viewport.w = (int)SDL_ceilf(view->viewport.w * view->current_scale.x);
     } else {
         view->pixel_viewport.w = view->pixel_w;
     }
     if (view->viewport.h >= 0) {
-        view->pixel_viewport.h = (int)SDL_ceilf(view->viewport.h * view->scale.y);
+        view->pixel_viewport.h = (int)SDL_ceilf(view->viewport.h * view->current_scale.y);
     } else {
         view->pixel_viewport.h = view->pixel_h;
     }
@@ -431,10 +431,9 @@ static void UpdatePixelViewport(SDL_Renderer *renderer, SDL_RenderViewState *vie
 
 static bool QueueCmdSetViewport(SDL_Renderer *renderer)
 {
-    SDL_Rect viewport;
     bool result = true;
 
-    viewport = renderer->view->pixel_viewport;
+    SDL_Rect viewport = renderer->view->pixel_viewport;
 
     if (!renderer->viewport_queued ||
         SDL_memcmp(&viewport, &renderer->last_queued_viewport, sizeof(viewport)) != 0) {
@@ -459,19 +458,19 @@ static bool QueueCmdSetViewport(SDL_Renderer *renderer)
 
 static void UpdatePixelClipRect(SDL_Renderer *renderer, SDL_RenderViewState *view)
 {
-    view->pixel_clip_rect.x = (int)SDL_floorf(view->clip_rect.x * view->scale.x);
-    view->pixel_clip_rect.y = (int)SDL_floorf(view->clip_rect.y * view->scale.y);
-    view->pixel_clip_rect.w = (int)SDL_ceilf(view->clip_rect.w * view->scale.x);
-    view->pixel_clip_rect.h = (int)SDL_ceilf(view->clip_rect.h * view->scale.y);
+    const float scale_x = view->current_scale.x;
+    const float scale_y = view->current_scale.y;
+    view->pixel_clip_rect.x = (int)SDL_floorf((view->clip_rect.x * scale_x) + view->logical_offset.x);
+    view->pixel_clip_rect.y = (int)SDL_floorf((view->clip_rect.y * scale_y) + view->logical_offset.y);
+    view->pixel_clip_rect.w = (int)SDL_ceilf(view->clip_rect.w * scale_x);
+    view->pixel_clip_rect.h = (int)SDL_ceilf(view->clip_rect.h * scale_y);
 }
 
 static bool QueueCmdSetClipRect(SDL_Renderer *renderer)
 {
-    SDL_Rect clip_rect;
     bool result = true;
 
-    clip_rect = renderer->view->pixel_clip_rect;
-
+    SDL_Rect clip_rect = renderer->view->pixel_clip_rect;
     if (!renderer->cliprect_queued ||
         renderer->view->clipping_enabled != renderer->last_queued_cliprect_enabled ||
         SDL_memcmp(&clip_rect, &renderer->last_queued_cliprect, sizeof(clip_rect)) != 0) {
@@ -718,7 +717,6 @@ static bool QueueCmdGeometry(SDL_Renderer *renderer, SDL_Texture *texture,
     cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_GEOMETRY, texture);
     if (cmd) {
         cmd->data.draw.texture_address_mode = texture_address_mode;
-
         result = renderer->QueueGeometry(renderer, cmd, texture,
                                          xy, xy_stride,
                                          color, color_stride, uv, uv_stride,
@@ -794,7 +792,7 @@ static void UpdateHDRProperties(SDL_Renderer *renderer)
     UpdateColorScale(renderer);
 }
 
-static bool UpdateLogicalPresentation(SDL_Renderer *renderer);
+static void UpdateLogicalPresentation(SDL_Renderer *renderer);
 
 
 int SDL_GetNumRenderDrivers(void)
@@ -835,7 +833,6 @@ static bool SDLCALL SDL_RendererEventWatch(void *userdata, SDL_Event *event)
             if (event->type == SDL_EVENT_WINDOW_RESIZED ||
                 event->type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED ||
                 event->type == SDL_EVENT_WINDOW_METAL_VIEW_RESIZED) {
-                UpdateMainViewDimensions(renderer);
                 UpdateLogicalPresentation(renderer);
             } else if (event->type == SDL_EVENT_WINDOW_HIDDEN) {
                 renderer->hidden = true;
@@ -1057,6 +1054,10 @@ SDL_Renderer *SDL_CreateRendererWithProperties(SDL_PropertiesID props)
     renderer->main_view.viewport.h = -1;
     renderer->main_view.scale.x = 1.0f;
     renderer->main_view.scale.y = 1.0f;
+    renderer->main_view.logical_scale.x = 1.0f;
+    renderer->main_view.logical_scale.y = 1.0f;
+    renderer->main_view.current_scale.x = 1.0f;
+    renderer->main_view.current_scale.y = 1.0f;
     renderer->view = &renderer->main_view;
     renderer->dpi_scale.x = 1.0f;
     renderer->dpi_scale.y = 1.0f;
@@ -1387,6 +1388,10 @@ SDL_Texture *SDL_CreateTextureWithProperties(SDL_Renderer *renderer, SDL_Propert
     texture->view.viewport.h = -1;
     texture->view.scale.x = 1.0f;
     texture->view.scale.y = 1.0f;
+    texture->view.logical_scale.x = 1.0f;
+    texture->view.logical_scale.y = 1.0f;
+    texture->view.current_scale.x = 1.0f;
+    texture->view.current_scale.y = 1.0f;
     texture->renderer = renderer;
     texture->next = renderer->textures;
     if (renderer->textures) {
@@ -2485,7 +2490,7 @@ void SDL_UnlockTexture(SDL_Texture *texture)
     texture->locked_surface = NULL;
 }
 
-static bool SDL_SetRenderTargetInternal(SDL_Renderer *renderer, SDL_Texture *texture)
+bool SDL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
 {
     // texture == NULL is valid and means reset the target to the window
     if (texture) {
@@ -2537,46 +2542,35 @@ static bool SDL_SetRenderTargetInternal(SDL_Renderer *renderer, SDL_Texture *tex
     return true;
 }
 
-bool SDL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
-{
-    if (!texture && renderer->logical_target) {
-        return SDL_SetRenderTargetInternal(renderer, renderer->logical_target);
-    } else {
-        return SDL_SetRenderTargetInternal(renderer, texture);
-    }
-}
-
 SDL_Texture *SDL_GetRenderTarget(SDL_Renderer *renderer)
 {
     CHECK_RENDERER_MAGIC(renderer, NULL);
-
-    if (renderer->target == renderer->logical_target) {
+    if (!renderer->target) {
         return NULL;
-    } else {
-        return (SDL_Texture *)SDL_GetPointerProperty(SDL_GetTextureProperties(renderer->target), SDL_PROP_TEXTURE_PARENT_POINTER, renderer->target);
     }
+    return (SDL_Texture *) SDL_GetPointerProperty(SDL_GetTextureProperties(renderer->target), SDL_PROP_TEXTURE_PARENT_POINTER, renderer->target);
 }
 
-static bool UpdateLogicalPresentation(SDL_Renderer *renderer)
+static void UpdateLogicalPresentation(SDL_Renderer *renderer)
 {
-    float logical_w = 1.0f, logical_h = 1.0f;
-    float output_w = (float)renderer->main_view.pixel_w;
-    float output_h = (float)renderer->main_view.pixel_h;
-    float want_aspect = 1.0f;
-    float real_aspect = 1.0f;
-    float scale;
-
     if (renderer->logical_presentation_mode == SDL_LOGICAL_PRESENTATION_DISABLED) {
-        // All done!
-        return true;
-    }
-
-    if (!SDL_GetTextureSize(renderer->logical_target, &logical_w, &logical_h)) {
-        goto error;
-    }
-
-    want_aspect = logical_w / logical_h;
-    real_aspect = output_w / output_h;
+        renderer->main_view.logical_offset.x = renderer->main_view.logical_offset.y = 0.0f;
+        renderer->main_view.logical_scale.x = renderer->main_view.logical_scale.y = 1.0f;
+        renderer->main_view.current_scale.x = renderer->main_view.scale.x;  // skip the multiplications against 1.0f.
+        renderer->main_view.current_scale.y = renderer->main_view.scale.y;
+        UpdateMainViewDimensions(renderer);
+        UpdatePixelClipRect(renderer, &renderer->main_view);
+        return;  // All done!
+    }
+
+    int iwidth, iheight;
+    SDL_GetRenderOutputSize(renderer, &iwidth, &iheight);
+    const float output_w = (float)iwidth;
+    const float output_h = (float)iheight;
+    const float logical_w = renderer->logical_w;
+    const float logical_h = renderer->logical_h;
+    const float want_aspect = logical_w / logical_h;
+    const float real_aspect = output_w / output_h;
 
     renderer->logical_src_rect.x = 0.0f;
     renderer->logical_src_rect.y = 0.0f;
@@ -2584,6 +2578,7 @@ static bool UpdateLogicalPresentation(SDL_Renderer *renderer)
     renderer->logical_src_rect.h = logical_h;
 
     if (renderer->logical_presentation_mode == SDL_LOGICAL_PRESENTATION_INTEGER_SCALE) {
+        float scale;
         if (want_aspect > real_aspect) {
             scale = (float)((int)output_w / (int)logical_w); // This an integer division!
         } else {
@@ -2609,7 +2604,7 @@ static bool UpdateLogicalPresentation(SDL_Renderer *renderer)
     } else if (want_aspect > real_aspect) {
         if (renderer->logical_presentation_mode == SDL_LOGICAL_PRESENTATION_LETTERBOX) {
             // We want a wider aspect ratio than is available - letterbox it
-            scale = output_w / logical_w;
+            const float scale = output_w / logical_w;
             renderer->logical_dst_rect.x = 0.0f;
             renderer->logical_dst_rect.w = output_w;
             renderer->logical_dst_rect.h = SDL_floorf(logical_h * scale);
@@ -2619,7 +2614,7 @@ static bool UpdateLogicalPresentation(SDL_Renderer *renderer)
                zoom so logical height matches the real height
                and the width will grow off the screen
              */
-            scale = output_h / logical_h;
+            const float scale = output_h / logical_h;
             renderer->logical_dst_rect.y = 0.0f;
             renderer->logical_dst_rect.h = output_h;
             renderer->logical_dst_rect.w = SDL_floorf(logical_w * scale);
@@ -2628,7 +2623,7 @@ static bool UpdateLogicalPresentation(SDL_Renderer *renderer)
     } else {
         if (renderer->logical_presentation_mode == SDL_LOGICAL_PRESENTATION_LETTERBOX) {
             // We want a narrower aspect ratio than is available - use side-bars
-            scale = output_h / logical_h;
+            const float scale = output_h / logical_h;
             renderer->logical_dst_rect.y = 0.0f;
             renderer->logical_dst_rect.h = output_h;
             renderer->logical_dst_rect.w = SDL_floorf(logical_w * scale);
@@ -2638,7 +2633,7 @@ static bool UpdateLogicalPresentation(SDL_Renderer *renderer)
                zoom so logical width matches the real width
                and the height will grow off the screen
              */
-            scale = output_w / logical_w;
+            const float scale = output_w / logical_w;
             renderer->logical_dst_rect.x = 0.0f;
             renderer->logical_dst_rect.w = output_w;
             renderer->logical_dst_rect.h = SDL_floorf(logical_h * scale);
@@ -2646,97 +2641,48 @@ static bool UpdateLogicalPresentation(SDL_Renderer *renderer)
         }
     }
 
-    SDL_SetTextureScaleMode(renderer->logical_target, renderer->logical_scale_mode);
+    renderer->main_view.logical_scale.x = (logical_w != 0.0f) ? renderer->logical_dst_rect.w / logical_w : 0.0f;
+    renderer->main_view.logical_scale.y = (logical_h != 0.0f) ? renderer->logical_dst_rect.h / logical_h : 0.0f;
+    renderer->main_view.current_scale.x = renderer->main_view.scale.x * renderer->main_view.logical_scale.x;
+    renderer->main_view.current_scale.y = renderer->main_view.scale.y * renderer->main_view.logical_scale.y;
+    renderer->main_view.logical_offset.x = renderer->logical_dst_rect.x;
+    renderer->main_view.logical_offset.y = renderer->logical_dst_rect.y;
 
-    if (!renderer->target) {
-        SDL_SetRenderTarget(renderer, renderer->logical_target);
-    }
-
-    return true;
-
-error:
-    SDL_SetRenderLogicalPresentation(renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED, SDL_SCALEMODE_NEAREST);
-    return false;
+    UpdateMainViewDimensions(renderer);  // this will replace pixel_w and pixel_h while making sure the dpi_scale is right.
+    renderer->main_view.pixel_w = (int) renderer->logical_dst_rect.w;
+    renderer->main_view.pixel_h = (int) renderer->logical_dst_rect.h;
+    UpdatePixelViewport(renderer, &renderer->main_view);
+    UpdatePixelClipRect(renderer, &renderer->main_view);
 }
 
-bool SDL_SetRenderLogicalPresentation(SDL_Renderer *renderer, int w, int h, SDL_RendererLogicalPresentation mode, SDL_ScaleMode scale_mode)
+bool SDL_SetRenderLogicalPresentation(SDL_Renderer *renderer, int w, int h, SDL_RendererLogicalPresentation mode)
 {
     CHECK_RENDERER_MAGIC(renderer, false);
 
-    if (mode == SDL_LOGICAL_PRESENTATION_DISABLED) {
-        if (renderer->logical_target) {
-            SDL_DestroyTexture(renderer->logical_target);
-        }
-    } else {
-        if (renderer->logical_target) {
-            SDL_PropertiesID props = SDL_GetTextureProperties(renderer->logical_target);
-            if (!props) {
-                goto error;
-            }
-
-            int existing_w = (int)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_WIDTH_NUMBER, 0);
-            int existing_h = (int)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_HEIGHT_NUMBER, 0);
-
-            if (w != existing_w || h != existing_h) {
-                SDL_DestroyTexture(renderer->logical_target);
-            }
-        }
-        if (!renderer->logical_target) {
-            renderer->logical_target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_UNKNOWN, SDL_TEXTUREACCESS_TARGET, w, h);
-            if (!renderer->logical_target) {
-                goto error;
-            }
-            SDL_SetTextureBlendMode(renderer->logical_target, SDL_BLENDMODE_NONE);
-        }
-    }
-
     renderer->logical_presentation_mode = mode;
-    renderer->logical_scale_mode = scale_mode;
+    renderer->logical_w = w;
+    renderer->logical_h = h;
 
-    return UpdateLogicalPresentation(renderer);
+    UpdateLogicalPresentation(renderer);
 
-error:
-    SDL_SetRenderLogicalPresentation(renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED, SDL_SCALEMODE_NEAREST);
-    return false;
+    return true;
 }
 
-bool SDL_GetRenderLogicalPresentation(SDL_Renderer *renderer, int *w, int *h, SDL_RendererLogicalPresentation *mode, SDL_ScaleMode *scale_mode)
+bool SDL_GetRenderLogicalPresentation(SDL_Renderer *renderer, int *w, int *h, SDL_RendererLogicalPresentation *mode)
 {
-    if (w) {
-        *w = 0;
-    }
-    if (h) {
-        *h = 0;
-    }
-    if (mode) {
-        *mode = SDL_LOGICAL_PRESENTATION_DISABLED;
-    }
-    if (scale_mode) {
-        *scale_mode = SDL_SCALEMODE_NEAREST;
-    }
+    #define SETVAL(ptr, val) if (ptr) { *ptr = val; }
 
-    CHECK_RENDERER_MAGIC(renderer, false);
+    SETVAL(w, 0);
+    SETVAL(h, 0);
+    SETVAL(mode, SDL_LOGICAL_PRESENTATION_DISABLED);
 
-    if (renderer->logical_target) {
-        SDL_PropertiesID props = SDL_GetTextureProperties(renderer->logical_target);
-        if (!props) {
-            return false;
-        }
+    CHECK_RENDERER_MAGIC(renderer, false);
 
-        if (w) {
-            *w = (int)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_WIDTH_NUMBER, 0);
-        }
-        if (h) {
-            *h = (int)SDL_GetNumberProperty(props, SDL_PROP_TEXTURE_HEIGHT_NUMBER, 0);
-        }
-    }
+    SETVAL(w, renderer->logical_w);
+    SETVAL(h, renderer->logical_h);
+    SETVAL(mode, renderer->logical_presentation_mode);
 
-    if (mode) {
-        *mode = renderer->logical_presentation_mode;
-    }
-    if (scale_mode) {
-        *scale_mode = renderer->logical_scale_mode;
-    }
+    #undef SETVAL
 
     return true;
 }
@@ -2814,17 +2760,48 @@ static void SDL_RenderLogicalBorders(SDL_Renderer *renderer)
 
 static void SDL_RenderLogicalPresentation(SDL_Renderer *renderer)
 {
-    SDL_assert(renderer->target == NULL);
-    SDL_SetRenderViewport(renderer, NULL);
-    SDL_SetRenderClipRect(renderer, NULL);
-    SDL_SetRenderScale(renderer, 1.0f, 1.0f);
-    SDL_RenderLogicalBorders(renderer);
-    SDL_RenderTexture(renderer, renderer->logical_target, &renderer->logical_src_rect, &renderer->logical_dst_rect);
+    const SDL_RendererLogicalPresentation mode = renderer->logical_presentation_mode;
+    if (mode == SDL_LOGICAL_PRESENTATION_LETTERBOX) {
+        // save off some state we're going to trample.
+        SDL_assert(renderer->view == &renderer->main_view);
+        SDL_RenderViewState *view = &renderer->main_view;
+        const int logical_w = renderer->logical_w;
+        const int logical_h = renderer->logical_h;
+        const float scale_x = view->scale.x;
+        const float scale_y = view->scale.y;
+        const bool clipping_enabled = view->clipping_enabled;
+        SDL_Rect orig_viewport, orig_cliprect;
+
+        SDL_copyp(&orig_viewport, &view->viewport);
+        if (clipping_enabled) {
+            SDL_copyp(&orig_cliprect, &view->clip_rect);
+        }
+
+        // trample some state.
+        SDL_SetRenderLogicalPresentation(renderer, logical_w, logical_h, SDL_LOGICAL_PRESENTATION_DISABLED);
+        SDL_SetRenderViewport(renderer, NULL);
+        if (clipping_enabled) {
+            SDL_SetRenderClipRect(renderer, NULL);
+        }
+        SDL_SetRenderScale(renderer, 1.0f, 1.0f);
+
+        // draw the borders.
+        SDL_RenderLogicalBorders(renderer);
+
+        // now set everything back.
+        renderer->logical_presentation_mode = mode;
+        SDL_SetRenderViewport(renderer, &orig_viewport);
+        if (clipping_enabled) {
+            SDL_SetRenderClipRect(renderer, &orig_cliprect);
+        }
+        SDL_SetRenderScale(renderer, scale_x, scale_y);
+
+        SDL_SetRenderLogicalPresentation(renderer, logical_w, logical_h, mode);
+    }
 }
 
 bool SDL_RenderCoordinatesFromWindow(SDL_Renderer *renderer, float window_x, float window_y, float *x, float *y)
 

(Patch may be truncated, please check the link at the top of this post.)