Fixed clip rectangle calculation when there is a viewport offset
authorSam Lantinga <slouken@libsdl.org>
Thu, 28 May 2015 18:57:10 -0700
changeset 96801d13a878b066
parent 9679 7fc4a8be47a8
child 9681 c2e49a5b4928
Fixed clip rectangle calculation when there is a viewport offset
src/render/direct3d/SDL_render_d3d.c
src/render/direct3d11/SDL_render_d3d11.c
src/render/opengl/SDL_render_gl.c
src/render/opengles/SDL_render_gles.c
src/render/opengles2/SDL_render_gles2.c
     1.1 --- a/src/render/direct3d/SDL_render_d3d.c	Thu May 28 12:55:01 2015 -0700
     1.2 +++ b/src/render/direct3d/SDL_render_d3d.c	Thu May 28 18:57:10 2015 -0700
     1.3 @@ -1269,10 +1269,10 @@
     1.4          HRESULT result;
     1.5  
     1.6          IDirect3DDevice9_SetRenderState(data->device, D3DRS_SCISSORTESTENABLE, TRUE);
     1.7 -        r.left = rect->x;
     1.8 -        r.top = rect->y;
     1.9 -        r.right = rect->x + rect->w;
    1.10 -        r.bottom = rect->y + rect->h;
    1.11 +        r.left = renderer->viewport.x + rect->x;
    1.12 +        r.top = renderer->viewport.y + rect->y;
    1.13 +        r.right = renderer->viewport.x + rect->x + rect->w;
    1.14 +        r.bottom = renderer->viewport.y + rect->y + rect->h;
    1.15  
    1.16          result = IDirect3DDevice9_SetScissorRect(data->device, &r);
    1.17          if (result != D3D_OK) {
     2.1 --- a/src/render/direct3d11/SDL_render_d3d11.c	Thu May 28 12:55:01 2015 -0700
     2.2 +++ b/src/render/direct3d11/SDL_render_d3d11.c	Thu May 28 18:57:10 2015 -0700
     2.3 @@ -1356,7 +1356,7 @@
     2.4  }
     2.5  
     2.6  static int
     2.7 -D3D11_GetViewportAlignedD3DRect(SDL_Renderer * renderer, const SDL_Rect * sdlRect, D3D11_RECT * outRect)
     2.8 +D3D11_GetViewportAlignedD3DRect(SDL_Renderer * renderer, const SDL_Rect * sdlRect, D3D11_RECT * outRect, BOOL includeViewportOffset)
     2.9  {
    2.10      D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
    2.11      const int rotation = D3D11_GetRotationForCurrentRenderTarget(renderer);
    2.12 @@ -1366,6 +1366,12 @@
    2.13              outRect->right = sdlRect->x + sdlRect->w;
    2.14              outRect->top = sdlRect->y;
    2.15              outRect->bottom = sdlRect->y + sdlRect->h;
    2.16 +            if (includeViewportOffset) {
    2.17 +                outRect->left += renderer->viewport.x;
    2.18 +                outRect->right += renderer->viewport.x;
    2.19 +                outRect->top += renderer->viewport.y;
    2.20 +                outRect->bottom += renderer->viewport.y;
    2.21 +            }
    2.22              break;
    2.23          case DXGI_MODE_ROTATION_ROTATE270:
    2.24              outRect->left = sdlRect->y;
    2.25 @@ -2280,7 +2286,7 @@
    2.26          ID3D11DeviceContext_RSSetScissorRects(data->d3dContext, 0, NULL);
    2.27      } else {
    2.28          D3D11_RECT scissorRect;
    2.29 -        if (D3D11_GetViewportAlignedD3DRect(renderer, &renderer->clip_rect, &scissorRect) != 0) {
    2.30 +        if (D3D11_GetViewportAlignedD3DRect(renderer, &renderer->clip_rect, &scissorRect, TRUE) != 0) {
    2.31              /* D3D11_GetViewportAlignedD3DRect will have set the SDL error */
    2.32              return -1;
    2.33          }
    2.34 @@ -2869,7 +2875,7 @@
    2.35      }
    2.36  
    2.37      /* Copy the desired portion of the back buffer to the staging texture: */
    2.38 -    if (D3D11_GetViewportAlignedD3DRect(renderer, rect, &srcRect) != 0) {
    2.39 +    if (D3D11_GetViewportAlignedD3DRect(renderer, rect, &srcRect, FALSE) != 0) {
    2.40          /* D3D11_GetViewportAlignedD3DRect will have set the SDL error */
    2.41          goto done;
    2.42      }
     3.1 --- a/src/render/opengl/SDL_render_gl.c	Thu May 28 12:55:01 2015 -0700
     3.2 +++ b/src/render/opengl/SDL_render_gl.c	Thu May 28 18:57:10 2015 -0700
     3.3 @@ -1046,7 +1046,14 @@
     3.4      if (renderer->clipping_enabled) {
     3.5          const SDL_Rect *rect = &renderer->clip_rect;
     3.6          data->glEnable(GL_SCISSOR_TEST);
     3.7 -        data->glScissor(rect->x, renderer->viewport.h - rect->y - rect->h, rect->w, rect->h);
     3.8 +        if (renderer->target) {
     3.9 +            data->glScissor(renderer->viewport.x + rect->x, renderer->viewport.y + rect->y, rect->w, rect->h);
    3.10 +        } else {
    3.11 +            int w, h;
    3.12 +
    3.13 +            SDL_GetRendererOutputSize(renderer, &w, &h);
    3.14 +            data->glScissor(renderer->viewport.x + rect->x, (h - renderer->viewport.y - renderer->viewport.h) + rect->y, rect->w, rect->h);
    3.15 +        }
    3.16      } else {
    3.17          data->glDisable(GL_SCISSOR_TEST);
    3.18      }
     4.1 --- a/src/render/opengles/SDL_render_gles.c	Thu May 28 12:55:01 2015 -0700
     4.2 +++ b/src/render/opengles/SDL_render_gles.c	Thu May 28 18:57:10 2015 -0700
     4.3 @@ -680,8 +680,16 @@
     4.4          return 0;
     4.5      }
     4.6  
     4.7 -    data->glViewport(renderer->viewport.x, renderer->viewport.y,
     4.8 -               renderer->viewport.w, renderer->viewport.h);
     4.9 +    if (renderer->target) {
    4.10 +        data->glViewport(renderer->viewport.x, renderer->viewport.y,
    4.11 +                         renderer->viewport.w, renderer->viewport.h);
    4.12 +    } else {
    4.13 +        int w, h;
    4.14 +
    4.15 +        SDL_GetRendererOutputSize(renderer, &w, &h);
    4.16 +        data->glViewport(renderer->viewport.x, (h - renderer->viewport.y - renderer->viewport.h),
    4.17 +                         renderer->viewport.w, renderer->viewport.h);
    4.18 +    }
    4.19  
    4.20      if (renderer->viewport.w && renderer->viewport.h) {
    4.21          data->glMatrixMode(GL_PROJECTION);
    4.22 @@ -707,7 +715,14 @@
    4.23      if (renderer->clipping_enabled) {
    4.24          const SDL_Rect *rect = &renderer->clip_rect;
    4.25          data->glEnable(GL_SCISSOR_TEST);
    4.26 -        data->glScissor(rect->x, renderer->viewport.h - rect->y - rect->h, rect->w, rect->h);
    4.27 +        if (renderer->target) {
    4.28 +            data->glScissor(renderer->viewport.x + rect->x, renderer->viewport.y + rect->y, rect->w, rect->h);
    4.29 +        } else {
    4.30 +            int w, h;
    4.31 +
    4.32 +            SDL_GetRendererOutputSize(renderer, &w, &h);
    4.33 +            data->glScissor(renderer->viewport.x + rect->x, (h - renderer->viewport.y - renderer->viewport.h) + rect->y, rect->w, rect->h);
    4.34 +        }
    4.35      } else {
    4.36          data->glDisable(GL_SCISSOR_TEST);
    4.37      }
     5.1 --- a/src/render/opengles2/SDL_render_gles2.c	Thu May 28 12:55:01 2015 -0700
     5.2 +++ b/src/render/opengles2/SDL_render_gles2.c	Thu May 28 18:57:10 2015 -0700
     5.3 @@ -382,8 +382,16 @@
     5.4          return 0;
     5.5      }
     5.6  
     5.7 -    data->glViewport(renderer->viewport.x, renderer->viewport.y,
     5.8 -               renderer->viewport.w, renderer->viewport.h);
     5.9 +    if (renderer->target) {
    5.10 +        data->glViewport(renderer->viewport.x, renderer->viewport.y,
    5.11 +                         renderer->viewport.w, renderer->viewport.h);
    5.12 +    } else {
    5.13 +        int w, h;
    5.14 +
    5.15 +        SDL_GetRendererOutputSize(renderer, &w, &h);
    5.16 +        data->glViewport(renderer->viewport.x, (h - renderer->viewport.y - renderer->viewport.h),
    5.17 +                         renderer->viewport.w, renderer->viewport.h);
    5.18 +    }
    5.19  
    5.20      if (data->current_program) {
    5.21          GLES2_SetOrthographicProjection(renderer);
    5.22 @@ -404,7 +412,14 @@
    5.23      if (renderer->clipping_enabled) {
    5.24          const SDL_Rect *rect = &renderer->clip_rect;
    5.25          data->glEnable(GL_SCISSOR_TEST);
    5.26 -        data->glScissor(rect->x, renderer->viewport.h - rect->y - rect->h, rect->w, rect->h);
    5.27 +        if (renderer->target) {
    5.28 +            data->glScissor(renderer->viewport.x + rect->x, renderer->viewport.y + rect->y, rect->w, rect->h);
    5.29 +        } else {
    5.30 +            int w, h;
    5.31 +
    5.32 +            SDL_GetRendererOutputSize(renderer, &w, &h);
    5.33 +            data->glScissor(renderer->viewport.x + rect->x, (h - renderer->viewport.y - renderer->viewport.h) + rect->y, rect->w, rect->h);
    5.34 +        }
    5.35      } else {
    5.36          data->glDisable(GL_SCISSOR_TEST);
    5.37      }