Commit 77305d47c2bc45068427d6a501d312fbba4db026

Sam Lantinga 2016-10-01T11:46:32

Fixed bug 3345 - SDL_RenderClear inconsistency with ClipRect Simon Hug The description of the SDL_RenderClear function in the SDL_render.h header says the following: "This function clears the entire rendering target, ignoring the viewport." The word "entire" implies that the clipping rectangle set with SDL_RenderSetClipRect also gets ignored. This is left somewhat ambiguous if only the viewport is mentioned. Minor thing, but let's see what the implementations actually do. The software renderer ignores the clipping rectangle when clearing. It even has a comment on this: /* By definition the clear ignores the clip rect */ Most other render drivers (opengl, opengles, opengles2, direct3d, and psp [I assume. Can't test it.]) use the scissor test for the ClipRect and don't disable it when clearing. Clearing will only happen within the clipping rectangle for these drivers. An exception is direct3d11 which uses a clear function that ignores the scissor test.

diff --git a/include/SDL_render.h b/include/SDL_render.h
index 6e3daf5..60c87b6 100644
--- a/include/SDL_render.h
+++ b/include/SDL_render.h
@@ -682,7 +682,8 @@ extern DECLSPEC int SDLCALL SDL_GetRenderDrawBlendMode(SDL_Renderer * renderer,
 /**
  *  \brief Clear the current rendering target with the drawing color
  *
- *  This function clears the entire rendering target, ignoring the viewport.
+ *  This function clears the entire rendering target, ignoring the viewport and
+ *  the clip rectangle.
  *
  *  \return 0 on success, or -1 on error
  */
diff --git a/src/render/direct3d/SDL_render_d3d.c b/src/render/direct3d/SDL_render_d3d.c
index da8c1af..c54897f 100644
--- a/src/render/direct3d/SDL_render_d3d.c
+++ b/src/render/direct3d/SDL_render_d3d.c
@@ -1311,6 +1311,10 @@ D3D_RenderClear(SDL_Renderer * renderer)
         BackBufferHeight = data->pparams.BackBufferHeight;
     }
 
+    if (renderer->clipping_enabled) {
+        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SCISSORTESTENABLE, FALSE);
+    }
+
     /* Don't reset the viewport if we don't have to! */
     if (!renderer->viewport.x && !renderer->viewport.y &&
         renderer->viewport.w == BackBufferWidth &&
@@ -1340,6 +1344,10 @@ D3D_RenderClear(SDL_Renderer * renderer)
         IDirect3DDevice9_SetViewport(data->device, &viewport);
     }
 
+    if (renderer->clipping_enabled) {
+        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SCISSORTESTENABLE, TRUE);
+    }
+
     if (FAILED(result)) {
         return D3D_SetError("Clear()", result);
     }
diff --git a/src/render/opengl/SDL_render_gl.c b/src/render/opengl/SDL_render_gl.c
index 7c6f80d..0276f28 100644
--- a/src/render/opengl/SDL_render_gl.c
+++ b/src/render/opengl/SDL_render_gl.c
@@ -1146,8 +1146,16 @@ GL_RenderClear(SDL_Renderer * renderer)
                        (GLfloat) renderer->b * inv255f,
                        (GLfloat) renderer->a * inv255f);
 
+    if (renderer->clipping_enabled) {
+        data->glDisable(GL_SCISSOR_TEST);
+    }
+
     data->glClear(GL_COLOR_BUFFER_BIT);
 
+    if (renderer->clipping_enabled) {
+        data->glEnable(GL_SCISSOR_TEST);
+    }
+
     return 0;
 }
 
diff --git a/src/render/opengles/SDL_render_gles.c b/src/render/opengles/SDL_render_gles.c
index b8b8c1d..56cbad6 100644
--- a/src/render/opengles/SDL_render_gles.c
+++ b/src/render/opengles/SDL_render_gles.c
@@ -815,9 +815,17 @@ GLES_RenderClear(SDL_Renderer * renderer)
                  (GLfloat) renderer->g * inv255f,
                  (GLfloat) renderer->b * inv255f,
                  (GLfloat) renderer->a * inv255f);
+    
+    if (renderer->clipping_enabled) {
+        data->glDisable(GL_SCISSOR_TEST);
+    }
 
     data->glClear(GL_COLOR_BUFFER_BIT);
 
+    if (renderer->clipping_enabled) {
+        data->glEnable(GL_SCISSOR_TEST);
+    }
+
     return 0;
 }
 
diff --git a/src/render/opengles2/SDL_render_gles2.c b/src/render/opengles2/SDL_render_gles2.c
index edefa64..30d761a 100644
--- a/src/render/opengles2/SDL_render_gles2.c
+++ b/src/render/opengles2/SDL_render_gles2.c
@@ -1327,8 +1327,16 @@ GLES2_RenderClear(SDL_Renderer * renderer)
         data->clear_a = renderer->a;
     }
 
+    if (renderer->clipping_enabled) {
+        data->glDisable(GL_SCISSOR_TEST);
+    }
+
     data->glClear(GL_COLOR_BUFFER_BIT);
 
+    if (renderer->clipping_enabled) {
+        data->glEnable(GL_SCISSOR_TEST);
+    }
+
     return 0;
 }