diff --git a/dlls/wined3d/drawprim.c b/dlls/wined3d/drawprim.c
index 83ed12d..f8490f2 100644
--- a/dlls/wined3d/drawprim.c
+++ b/dlls/wined3d/drawprim.c
@@ -180,6 +180,64 @@ static GLfloat invymat[16] = {
 	0.0f, 0.0f, 1.0f, 0.0f,
 	0.0f, 0.0f, 0.0f, 1.0f};
 
+void d3ddevice_set_ortho(IWineD3DDeviceImpl *This, BOOL dontclip) {
+    double X, Y, height, width, minZ, maxZ;
+
+    /* If the last draw was transformed as well, no need to reapply all the matrixes */
+    if ( (!This->last_was_rhw) || (This->viewport_changed) || (This->last_was_notclipped != dontclip) ) {
+        This->last_was_rhw = TRUE;
+        This->viewport_changed = FALSE;
+        This->last_was_notclipped = dontclip;
+
+        /* Transformed already into viewport coordinates, so we do not need transform
+            matrices. Reset all matrices to identity and leave the default matrix in world
+            mode.                                                                         */
+        glMatrixMode(GL_MODELVIEW);
+        checkGLcall("glMatrixMode(GL_MODELVIEW)");
+        glLoadIdentity();
+        checkGLcall("glLoadIdentity");
+
+        glMatrixMode(GL_PROJECTION);
+        checkGLcall("glMatrixMode(GL_PROJECTION)");
+        glLoadIdentity();
+        checkGLcall("glLoadIdentity");
+
+        /* Set up the viewport to be full viewport */
+        X      = This->stateBlock->viewport.X;
+        Y      = This->stateBlock->viewport.Y;
+        height = This->stateBlock->viewport.Height;
+        width  = This->stateBlock->viewport.Width;
+        minZ   = This->stateBlock->viewport.MinZ;
+        maxZ   = This->stateBlock->viewport.MaxZ;
+        if(!dontclip) {
+            TRACE("Calling glOrtho with %f, %f, %f, %f\n", width, height, 1.0, -1.0);
+            /* The Z coordinate must be passed to GL unchanged, otherwise the Z order
+               of transformed and untransformed vertices will be destroyed. This sets up
+               a z range from -1.0 to 1.0, reversed. The resulting transform matrix glOrtho
+               produces will have 0.0, 0.0, 1.0, 0.0 in the third row. (See glOrtho man page) */
+            glOrtho(X, X + width, Y + height, Y, 1.0, -1.0);
+        } else {
+            TRACE("Calling glOrtho with %f, %f, %f, %f\n", width, height, -minZ, -maxZ);
+            /* The app asked us not to clip the vertices. Disabling clipping isn't possible
+               in opengl, so we have to use some trick. This glOrtho setup transforms
+               the Z coordinates into the depth range, so they are not clipped due to
+               their z value. This used to be the default glOrtho setup, but some problems
+               arised with mixed transformed and untransformed scenes(see the !dontclip case) */
+             glOrtho(X, X + width, Y + height, Y, -minZ, -maxZ);
+        }
+        checkGLcall("glOrtho");
+
+        /* Window Coord 0 is the middle of the first pixel, so translate by half
+            a pixel (See comment above glTranslate below)                         */
+        glTranslatef(0.5, 0.5, 0);
+        checkGLcall("glTranslatef(0.5, 0.5, 0)");
+        if (This->renderUpsideDown) {
+            glMultMatrixf(invymat);
+            checkGLcall("glMultMatrixf(invymat)");
+        }
+    }
+}
+
 /* Setup views - Transformed & lit if RHW, else untransformed.
        Only unlit if Normals are supplied
     Returns: Whether to restore lighting afterwards           */
@@ -198,48 +256,7 @@ static BOOL primitiveInitState(IWineD3DD
     }
 
     if (!useVS && vtx_transformed) {
-
-        /* If the last draw was transformed as well, no need to reapply all the matrixes */
-        if ( (!This->last_was_rhw) || (This->viewport_changed) ) {
-
-            double X, Y, height, width, minZ, maxZ;
-            This->last_was_rhw = TRUE;
-            This->viewport_changed = FALSE;
-
-            /* Transformed already into viewport coordinates, so we do not need transform
-               matrices. Reset all matrices to identity and leave the default matrix in world
-               mode.                                                                         */
-            glMatrixMode(GL_MODELVIEW);
-            checkGLcall("glMatrixMode(GL_MODELVIEW)");
-            glLoadIdentity();
-            checkGLcall("glLoadIdentity");
-
-            glMatrixMode(GL_PROJECTION);
-            checkGLcall("glMatrixMode(GL_PROJECTION)");
-            glLoadIdentity();
-            checkGLcall("glLoadIdentity");
-
-            /* Set up the viewport to be full viewport */
-            X      = This->stateBlock->viewport.X;
-            Y      = This->stateBlock->viewport.Y;
-            height = This->stateBlock->viewport.Height;
-            width  = This->stateBlock->viewport.Width;
-            minZ   = This->stateBlock->viewport.MinZ;
-            maxZ   = This->stateBlock->viewport.MaxZ;
-            TRACE("Calling glOrtho with %f, %f, %f, %f\n", width, height, -minZ, -maxZ);
-            glOrtho(X, X + width, Y + height, Y, -minZ, -maxZ);
-            checkGLcall("glOrtho");
-
-            /* Window Coord 0 is the middle of the first pixel, so translate by half
-               a pixel (See comment above glTranslate below)                         */
-            glTranslatef(0.5, 0.5, 0);
-            checkGLcall("glTranslatef(0.5, 0.5, 0)");
-            if (This->renderUpsideDown) {
-                glMultMatrixf(invymat);
-                checkGLcall("glMultMatrixf(invymat)");
-            }
-        }
-
+            d3ddevice_set_ortho(This, FALSE);
     } else {
 
         /* Untransformed, so relies on the view and projection matrices */
diff --git a/dlls/wined3d/surface.c b/dlls/wined3d/surface.c
index 48b4e36..b00ed0f 100644
--- a/dlls/wined3d/surface.c
+++ b/dlls/wined3d/surface.c
@@ -752,41 +752,7 @@ HRESULT WINAPI IWineD3DSurfaceImpl_Unloc
             /* glDrawPixels transforms the raster position as though it was a vertex -
                we want to draw at screen position 0,0 - Set up ortho (rhw) mode as
                per drawprim (and leave set - it will sort itself out due to last_was_rhw */
-            if ( (!myDevice->last_was_rhw) || (myDevice->viewport_changed) ) {
-
-                double X, Y, height, width, minZ, maxZ;
-                myDevice->last_was_rhw = TRUE;
-                myDevice->viewport_changed = FALSE;
-
-                /* Transformed already into viewport coordinates, so we do not need transform
-                   matrices. Reset all matrices to identity and leave the default matrix in world
-                   mode.                                                                         */
-                glMatrixMode(GL_MODELVIEW);
-                checkGLcall("glMatrixMode");
-                glLoadIdentity();
-                checkGLcall("glLoadIdentity");
-
-                glMatrixMode(GL_PROJECTION);
-                checkGLcall("glMatrixMode");
-                glLoadIdentity();
-                checkGLcall("glLoadIdentity");
-
-                /* Set up the viewport to be full viewport */
-                X      = myDevice->stateBlock->viewport.X;
-                Y      = myDevice->stateBlock->viewport.Y;
-                height = myDevice->stateBlock->viewport.Height;
-                width  = myDevice->stateBlock->viewport.Width;
-                minZ   = myDevice->stateBlock->viewport.MinZ;
-                maxZ   = myDevice->stateBlock->viewport.MaxZ;
-                TRACE("Calling glOrtho with %f, %f, %f, %f\n", width, height, -minZ, -maxZ);
-                glOrtho(X, X + width, Y + height, Y, -minZ, -maxZ);
-                checkGLcall("glOrtho");
-
-                /* Window Coord 0 is the middle of the first pixel, so translate by half
-                   a pixel (See comment above glTranslate below)                         */
-                glTranslatef(0.5, 0.5, 0);
-                checkGLcall("glTranslatef(0.5, 0.5, 0)");
-            }
+            d3ddevice_set_ortho(myDevice, FALSE);
 
             if (iface ==  implSwapChain->backBuffer || iface == myDevice->renderTarget) {
                 glDrawBuffer(GL_BACK);
diff --git a/dlls/wined3d/wined3d_private.h b/dlls/wined3d/wined3d_private.h
index 25df4e2..8306d1b 100644
--- a/dlls/wined3d/wined3d_private.h
+++ b/dlls/wined3d/wined3d_private.h
@@ -508,6 +508,7 @@ typedef struct IWineD3DDeviceImpl
     UINT                    alphafunc;
     UINT                    stencilfunc;
     BOOL                    texture_shader_active;  /* TODO: Confirm use is correct */
+    BOOL                    last_was_notclipped;
 
     /* State block related */
     BOOL                    isRecordingState;
@@ -588,6 +589,9 @@ typedef struct PrivateData
     DWORD size;
 } PrivateData;
 
+/* OpenGL ortho matrix setup */
+void d3ddevice_set_ortho(IWineD3DDeviceImpl *This, BOOL dontclip);
+
 /*****************************************************************************
  * IWineD3DResource implementation structure
  */
