Fixed clip rectangle calculation when there is a viewport offset
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
diff --git a/src/render/direct3d/SDL_render_d3d.c b/src/render/direct3d/SDL_render_d3d.c
index 92d2c3a..6ebef67 100644
--- a/src/render/direct3d/SDL_render_d3d.c
+++ b/src/render/direct3d/SDL_render_d3d.c
@@ -1269,10 +1269,10 @@ D3D_UpdateClipRect(SDL_Renderer * renderer)
HRESULT result;
IDirect3DDevice9_SetRenderState(data->device, D3DRS_SCISSORTESTENABLE, TRUE);
- r.left = rect->x;
- r.top = rect->y;
- r.right = rect->x + rect->w;
- r.bottom = rect->y + rect->h;
+ r.left = renderer->viewport.x + rect->x;
+ r.top = renderer->viewport.y + rect->y;
+ r.right = renderer->viewport.x + rect->x + rect->w;
+ r.bottom = renderer->viewport.y + rect->y + rect->h;
result = IDirect3DDevice9_SetScissorRect(data->device, &r);
if (result != D3D_OK) {
diff --git a/src/render/direct3d11/SDL_render_d3d11.c b/src/render/direct3d11/SDL_render_d3d11.c
index deb023e..78d6714 100644
--- a/src/render/direct3d11/SDL_render_d3d11.c
+++ b/src/render/direct3d11/SDL_render_d3d11.c
@@ -1356,7 +1356,7 @@ D3D11_GetRotationForCurrentRenderTarget(SDL_Renderer * renderer)
}
static int
-D3D11_GetViewportAlignedD3DRect(SDL_Renderer * renderer, const SDL_Rect * sdlRect, D3D11_RECT * outRect)
+D3D11_GetViewportAlignedD3DRect(SDL_Renderer * renderer, const SDL_Rect * sdlRect, D3D11_RECT * outRect, BOOL includeViewportOffset)
{
D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
const int rotation = D3D11_GetRotationForCurrentRenderTarget(renderer);
@@ -1366,6 +1366,12 @@ D3D11_GetViewportAlignedD3DRect(SDL_Renderer * renderer, const SDL_Rect * sdlRec
outRect->right = sdlRect->x + sdlRect->w;
outRect->top = sdlRect->y;
outRect->bottom = sdlRect->y + sdlRect->h;
+ if (includeViewportOffset) {
+ outRect->left += renderer->viewport.x;
+ outRect->right += renderer->viewport.x;
+ outRect->top += renderer->viewport.y;
+ outRect->bottom += renderer->viewport.y;
+ }
break;
case DXGI_MODE_ROTATION_ROTATE270:
outRect->left = sdlRect->y;
@@ -2280,7 +2286,7 @@ D3D11_UpdateClipRect(SDL_Renderer * renderer)
ID3D11DeviceContext_RSSetScissorRects(data->d3dContext, 0, NULL);
} else {
D3D11_RECT scissorRect;
- if (D3D11_GetViewportAlignedD3DRect(renderer, &renderer->clip_rect, &scissorRect) != 0) {
+ if (D3D11_GetViewportAlignedD3DRect(renderer, &renderer->clip_rect, &scissorRect, TRUE) != 0) {
/* D3D11_GetViewportAlignedD3DRect will have set the SDL error */
return -1;
}
@@ -2869,7 +2875,7 @@ D3D11_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
}
/* Copy the desired portion of the back buffer to the staging texture: */
- if (D3D11_GetViewportAlignedD3DRect(renderer, rect, &srcRect) != 0) {
+ if (D3D11_GetViewportAlignedD3DRect(renderer, rect, &srcRect, FALSE) != 0) {
/* D3D11_GetViewportAlignedD3DRect will have set the SDL error */
goto done;
}
diff --git a/src/render/opengl/SDL_render_gl.c b/src/render/opengl/SDL_render_gl.c
index f9ce30c..f5ac96d 100644
--- a/src/render/opengl/SDL_render_gl.c
+++ b/src/render/opengl/SDL_render_gl.c
@@ -1046,7 +1046,14 @@ GL_UpdateClipRect(SDL_Renderer * renderer)
if (renderer->clipping_enabled) {
const SDL_Rect *rect = &renderer->clip_rect;
data->glEnable(GL_SCISSOR_TEST);
- data->glScissor(rect->x, renderer->viewport.h - rect->y - rect->h, rect->w, rect->h);
+ if (renderer->target) {
+ data->glScissor(renderer->viewport.x + rect->x, renderer->viewport.y + rect->y, rect->w, rect->h);
+ } else {
+ int w, h;
+
+ SDL_GetRendererOutputSize(renderer, &w, &h);
+ data->glScissor(renderer->viewport.x + rect->x, (h - renderer->viewport.y - renderer->viewport.h) + rect->y, rect->w, rect->h);
+ }
} else {
data->glDisable(GL_SCISSOR_TEST);
}
diff --git a/src/render/opengles/SDL_render_gles.c b/src/render/opengles/SDL_render_gles.c
index 81674b4..b35fed7 100644
--- a/src/render/opengles/SDL_render_gles.c
+++ b/src/render/opengles/SDL_render_gles.c
@@ -680,8 +680,16 @@ GLES_UpdateViewport(SDL_Renderer * renderer)
return 0;
}
- data->glViewport(renderer->viewport.x, renderer->viewport.y,
- renderer->viewport.w, renderer->viewport.h);
+ if (renderer->target) {
+ data->glViewport(renderer->viewport.x, renderer->viewport.y,
+ renderer->viewport.w, renderer->viewport.h);
+ } else {
+ int w, h;
+
+ SDL_GetRendererOutputSize(renderer, &w, &h);
+ data->glViewport(renderer->viewport.x, (h - renderer->viewport.y - renderer->viewport.h),
+ renderer->viewport.w, renderer->viewport.h);
+ }
if (renderer->viewport.w && renderer->viewport.h) {
data->glMatrixMode(GL_PROJECTION);
@@ -707,7 +715,14 @@ GLES_UpdateClipRect(SDL_Renderer * renderer)
if (renderer->clipping_enabled) {
const SDL_Rect *rect = &renderer->clip_rect;
data->glEnable(GL_SCISSOR_TEST);
- data->glScissor(rect->x, renderer->viewport.h - rect->y - rect->h, rect->w, rect->h);
+ if (renderer->target) {
+ data->glScissor(renderer->viewport.x + rect->x, renderer->viewport.y + rect->y, rect->w, rect->h);
+ } else {
+ int w, h;
+
+ SDL_GetRendererOutputSize(renderer, &w, &h);
+ data->glScissor(renderer->viewport.x + rect->x, (h - renderer->viewport.y - renderer->viewport.h) + rect->y, rect->w, rect->h);
+ }
} else {
data->glDisable(GL_SCISSOR_TEST);
}
diff --git a/src/render/opengles2/SDL_render_gles2.c b/src/render/opengles2/SDL_render_gles2.c
index be16a4c..c36539e 100644
--- a/src/render/opengles2/SDL_render_gles2.c
+++ b/src/render/opengles2/SDL_render_gles2.c
@@ -382,8 +382,16 @@ GLES2_UpdateViewport(SDL_Renderer * renderer)
return 0;
}
- data->glViewport(renderer->viewport.x, renderer->viewport.y,
- renderer->viewport.w, renderer->viewport.h);
+ if (renderer->target) {
+ data->glViewport(renderer->viewport.x, renderer->viewport.y,
+ renderer->viewport.w, renderer->viewport.h);
+ } else {
+ int w, h;
+
+ SDL_GetRendererOutputSize(renderer, &w, &h);
+ data->glViewport(renderer->viewport.x, (h - renderer->viewport.y - renderer->viewport.h),
+ renderer->viewport.w, renderer->viewport.h);
+ }
if (data->current_program) {
GLES2_SetOrthographicProjection(renderer);
@@ -404,7 +412,14 @@ GLES2_UpdateClipRect(SDL_Renderer * renderer)
if (renderer->clipping_enabled) {
const SDL_Rect *rect = &renderer->clip_rect;
data->glEnable(GL_SCISSOR_TEST);
- data->glScissor(rect->x, renderer->viewport.h - rect->y - rect->h, rect->w, rect->h);
+ if (renderer->target) {
+ data->glScissor(renderer->viewport.x + rect->x, renderer->viewport.y + rect->y, rect->w, rect->h);
+ } else {
+ int w, h;
+
+ SDL_GetRendererOutputSize(renderer, &w, &h);
+ data->glScissor(renderer->viewport.x + rect->x, (h - renderer->viewport.y - renderer->viewport.h) + rect->y, rect->w, rect->h);
+ }
} else {
data->glDisable(GL_SCISSOR_TEST);
}