Sync trunk.
[reactos.git] / dll / directx / wine / wined3d / swapchain.c
index 2eaa812..b0d591a 100644 (file)
@@ -4,6 +4,7 @@
  *Copyright 2002-2003 Jason Edmeades
  *Copyright 2002-2003 Raphael Junqueira
  *Copyright 2005 Oliver Stieber
+ *Copyright 2007-2008 Stefan Dösinger for CodeWeavers
  *
  *This library is free software; you can redistribute it and/or
  *modify it under the terms of the GNU Lesser General Public
 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
 WINE_DECLARE_DEBUG_CHANNEL(fps);
 
-#define GLINFO_LOCATION This->wineD3DDevice->adapter->gl_info
-
-/* IDirect3DSwapChain IUnknown parts follow: */
-static ULONG WINAPI IWineD3DSwapChainImpl_AddRef(IWineD3DSwapChain *iface) {
-    IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
-    DWORD refCount = InterlockedIncrement(&This->ref);
-    TRACE("(%p) : AddRef increasing from %d\n", This, refCount - 1);
-    return refCount;
-}
-
-static HRESULT WINAPI IWineD3DSwapChainImpl_QueryInterface(IWineD3DSwapChain *iface, REFIID riid, LPVOID *ppobj)
+/*IWineD3DSwapChain parts follow: */
+static void WINAPI IWineD3DSwapChainImpl_Destroy(IWineD3DSwapChain *iface)
 {
     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
-    TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj);
-    if (IsEqualGUID(riid, &IID_IUnknown)
-        || IsEqualGUID(riid, &IID_IWineD3DBase)
-        || IsEqualGUID(riid, &IID_IWineD3DSwapChain)){
-        IWineD3DSwapChainImpl_AddRef(iface);
-        if(ppobj == NULL){
-            ERR("Query interface called but now data allocated\n");
-            return E_NOINTERFACE;
-        }
-        *ppobj = This;
-        return WINED3D_OK;
-    }
-    *ppobj = NULL;
-    return E_NOINTERFACE;
-}
-
-
-static ULONG WINAPI IWineD3DSwapChainImpl_Release(IWineD3DSwapChain *iface) {
-    IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
-    DWORD refCount;
-    refCount = InterlockedDecrement(&This->ref);
-    TRACE("(%p) : ReleaseRef to %d\n", This, refCount);
-    if (refCount == 0) {
-        IWineD3DSwapChain_Destroy(iface, D3DCB_DefaultDestroySurface);
-    }
-    return refCount;
-}
+    WINED3DDISPLAYMODE mode;
+    unsigned int i;
 
-static HRESULT WINAPI IWineD3DSwapChainImpl_GetParent(IWineD3DSwapChain *iface, IUnknown ** ppParent){
-    IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
-    *ppParent = This->parent;
-    IUnknown_AddRef(*ppParent);
-    TRACE("(%p) returning %p\n", This , *ppParent);
-    return WINED3D_OK;
-}
+    TRACE("Destroying swapchain %p\n", iface);
 
-/*IWineD3DSwapChain parts follow: */
-static void WINAPI IWineD3DSwapChainImpl_Destroy(IWineD3DSwapChain *iface, D3DCB_DESTROYSURFACEFN D3DCB_DestroyRenderTarget) {
-    IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
-    WINED3DDISPLAYMODE mode;
-    int i;
+    IWineD3DSwapChain_SetGammaRamp(iface, 0, &This->orig_gamma);
 
-    /* release the ref to the front and back buffer parents */
-    if(This->frontBuffer) {
-        IWineD3DSurface_SetContainer(This->frontBuffer, 0);
-        if(D3DCB_DestroyRenderTarget(This->frontBuffer) > 0) {
-            FIXME("(%p) Something's still holding the front buffer\n",This);
+    /* Release the swapchain's draw buffers. Make sure This->back_buffers[0] is
+     * the last buffer to be destroyed, FindContext() depends on that. */
+    if (This->front_buffer)
+    {
+        IWineD3DSurface_SetContainer((IWineD3DSurface *)This->front_buffer, NULL);
+        if (IWineD3DSurface_Release((IWineD3DSurface *)This->front_buffer))
+        {
+            WARN("(%p) Something's still holding the front buffer (%p).\n",
+                    This, This->front_buffer);
         }
+        This->front_buffer = NULL;
     }
 
-    if(This->backBuffer) {
-        int i;
-        for(i = 0; i < This->presentParms.BackBufferCount; i++) {
-            IWineD3DSurface_SetContainer(This->backBuffer[i], 0);
-            if(D3DCB_DestroyRenderTarget(This->backBuffer[i]) > 0) {
-                FIXME("(%p) Something's still holding the back buffer\n",This);
-            }
+    if (This->back_buffers)
+    {
+        UINT i = This->presentParms.BackBufferCount;
+
+        while (i--)
+        {
+            IWineD3DSurface_SetContainer((IWineD3DSurface *)This->back_buffers[i], NULL);
+            if (IWineD3DSurface_Release((IWineD3DSurface *)This->back_buffers[i]))
+                WARN("(%p) Something's still holding back buffer %u (%p).\n",
+                        This, i, This->back_buffers[i]);
         }
-        HeapFree(GetProcessHeap(), 0, This->backBuffer);
+        HeapFree(GetProcessHeap(), 0, This->back_buffers);
+        This->back_buffers = NULL;
     }
 
-    for(i = 0; i < This->num_contexts; i++) {
-        DestroyContext(This->wineD3DDevice, This->context[i]);
+    for (i = 0; i < This->num_contexts; ++i)
+    {
+        context_destroy(This->device, This->context[i]);
     }
     /* Restore the screen resolution if we rendered in fullscreen
      * This will restore the screen resolution to what it was before creating the swapchain. In case of d3d8 and d3d9
      * this will be the original desktop resolution. In case of d3d7 this will be a NOP because ddraw sets the resolution
      * before starting up Direct3D, thus orig_width and orig_height will be equal to the modes in the presentation params
      */
-    if(This->presentParms.Windowed == FALSE) {
+    if(This->presentParms.Windowed == FALSE && This->presentParms.AutoRestoreDisplayMode) {
         mode.Width = This->orig_width;
         mode.Height = This->orig_height;
         mode.RefreshRate = 0;
         mode.Format = This->orig_fmt;
-        IWineD3DDevice_SetDisplayMode((IWineD3DDevice *) This->wineD3DDevice, 0, &mode);
+        IWineD3DDevice_SetDisplayMode((IWineD3DDevice *)This->device, 0, &mode);
     }
-    HeapFree(GetProcessHeap(), 0, This->context);
 
+    HeapFree(GetProcessHeap(), 0, This->context);
     HeapFree(GetProcessHeap(), 0, This);
 }
 
+/* A GL context is provided by the caller */
+static void swapchain_blit(IWineD3DSwapChainImpl *This, struct wined3d_context *context,
+        const RECT *src_rect, const RECT *dst_rect)
+{
+    IWineD3DDeviceImpl *device = This->device;
+    IWineD3DSurfaceImpl *backbuffer = This->back_buffers[0];
+    UINT src_w = src_rect->right - src_rect->left;
+    UINT src_h = src_rect->bottom - src_rect->top;
+    GLenum gl_filter;
+    const struct wined3d_gl_info *gl_info = context->gl_info;
+    RECT win_rect;
+    UINT win_h;
+
+    TRACE("swapchain %p, context %p, src_rect %s, dst_rect %s.\n",
+            This, context, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect));
+
+    if (src_w == dst_rect->right - dst_rect->left && src_h == dst_rect->bottom - dst_rect->top)
+        gl_filter = GL_NEAREST;
+    else
+        gl_filter = GL_LINEAR;
+
+    GetClientRect(This->win_handle, &win_rect);
+    win_h = win_rect.bottom - win_rect.top;
+
+    if (gl_info->fbo_ops.glBlitFramebuffer && is_identity_fixup(backbuffer->resource.format_desc->color_fixup))
+    {
+        ENTER_GL();
+        context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, backbuffer, NULL);
+        glReadBuffer(GL_COLOR_ATTACHMENT0);
+
+        context_bind_fbo(context, GL_DRAW_FRAMEBUFFER, NULL);
+        context_set_draw_buffer(context, GL_BACK);
+
+        glDisable(GL_SCISSOR_TEST);
+        IWineD3DDeviceImpl_MarkStateDirty(This->device, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE));
+
+        /* Note that the texture is upside down */
+        gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
+                dst_rect->left, win_h - dst_rect->top, dst_rect->right, win_h - dst_rect->bottom,
+                GL_COLOR_BUFFER_BIT, gl_filter);
+        checkGLcall("Swapchain present blit(EXT_framebuffer_blit)\n");
+        LEAVE_GL();
+    }
+    else
+    {
+        struct wined3d_context *context2;
+        float tex_left = src_rect->left;
+        float tex_top = src_rect->top;
+        float tex_right = src_rect->right;
+        float tex_bottom = src_rect->bottom;
+
+        context2 = context_acquire(This->device, This->back_buffers[0]);
+        context_apply_blit_state(context2, device);
+
+        if(backbuffer->Flags & SFLAG_NORMCOORD)
+        {
+            tex_left /= src_w;
+            tex_right /= src_w;
+            tex_top /= src_h;
+            tex_bottom /= src_h;
+        }
+
+        if (is_complex_fixup(backbuffer->resource.format_desc->color_fixup))
+            gl_filter = GL_NEAREST;
+
+        ENTER_GL();
+        context_bind_fbo(context2, GL_DRAW_FRAMEBUFFER, NULL);
+
+        /* Set up the texture. The surface is not in a IWineD3D*Texture container,
+         * so there are no d3d texture settings to dirtify
+         */
+        device->blitter->set_shader((IWineD3DDevice *) device, backbuffer);
+        glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MIN_FILTER, gl_filter);
+        glTexParameteri(backbuffer->texture_target, GL_TEXTURE_MAG_FILTER, gl_filter);
+
+        context_set_draw_buffer(context, GL_BACK);
+
+        /* Set the viewport to the destination rectandle, disable any projection
+         * transformation set up by context_apply_blit_state(), and draw a
+         * (-1,-1)-(1,1) quad.
+         *
+         * Back up viewport and matrix to avoid breaking last_was_blit
+         *
+         * Note that context_apply_blit_state() set up viewport and ortho to
+         * match the surface size - we want the GL drawable(=window) size. */
+        glPushAttrib(GL_VIEWPORT_BIT);
+        glViewport(dst_rect->left, win_h - dst_rect->bottom, dst_rect->right, win_h - dst_rect->top);
+        glMatrixMode(GL_PROJECTION);
+        glPushMatrix();
+        glLoadIdentity();
+
+        glBegin(GL_QUADS);
+            /* bottom left */
+            glTexCoord2f(tex_left, tex_bottom);
+            glVertex2i(-1, -1);
+
+            /* top left */
+            glTexCoord2f(tex_left, tex_top);
+            glVertex2i(-1, 1);
+
+            /* top right */
+            glTexCoord2f(tex_right, tex_top);
+            glVertex2i(1, 1);
+
+            /* bottom right */
+            glTexCoord2f(tex_right, tex_bottom);
+            glVertex2i(1, -1);
+        glEnd();
+
+        glPopMatrix();
+        glPopAttrib();
+
+        device->blitter->unset_shader((IWineD3DDevice *) device);
+        checkGLcall("Swapchain present blit(manual)\n");
+        LEAVE_GL();
+
+        context_release(context2);
+    }
+}
+
 static HRESULT WINAPI IWineD3DSwapChainImpl_Present(IWineD3DSwapChain *iface, CONST RECT *pSourceRect, CONST RECT *pDestRect, HWND hDestWindowOverride, CONST RGNDATA *pDirtyRegion, DWORD dwFlags) {
     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
+    const struct wined3d_gl_info *gl_info;
+    struct wined3d_context *context;
+    RECT src_rect, dst_rect;
+    BOOL render_to_fbo;
     unsigned int sync;
     int retval;
 
+    IWineD3DSwapChain_SetDestWindowOverride(iface, hDestWindowOverride);
 
-    ActivateContext(This->wineD3DDevice, This->backBuffer[0], CTXUSAGE_RESOURCELOAD);
+    context = context_acquire(This->device, This->back_buffers[0]);
+    if (!context->valid)
+    {
+        context_release(context);
+        WARN("Invalid context, skipping present.\n");
+        return WINED3D_OK;
+    }
+
+    gl_info = context->gl_info;
 
     /* Render the cursor onto the back buffer, using our nifty directdraw blitting code :-) */
-    if(This->wineD3DDevice->bCursorVisible && This->wineD3DDevice->cursorTexture) {
+    if (This->device->bCursorVisible && This->device->cursorTexture)
+    {
         IWineD3DSurfaceImpl cursor;
-        RECT destRect = {This->wineD3DDevice->xScreenSpace - This->wineD3DDevice->xHotSpot,
-                         This->wineD3DDevice->yScreenSpace - This->wineD3DDevice->yHotSpot,
-                         This->wineD3DDevice->xScreenSpace + This->wineD3DDevice->cursorWidth - This->wineD3DDevice->xHotSpot,
-                         This->wineD3DDevice->yScreenSpace + This->wineD3DDevice->cursorHeight - This->wineD3DDevice->yHotSpot};
+        RECT destRect =
+        {
+            This->device->xScreenSpace - This->device->xHotSpot,
+            This->device->yScreenSpace - This->device->yHotSpot,
+            This->device->xScreenSpace + This->device->cursorWidth - This->device->xHotSpot,
+            This->device->yScreenSpace + This->device->cursorHeight - This->device->yHotSpot,
+        };
         TRACE("Rendering the cursor. Creating fake surface at %p\n", &cursor);
         /* Build a fake surface to call the Blitting code. It is not possible to use the interface passed by
          * the application because we are only supposed to copy the information out. Using a fake surface
@@ -149,19 +253,15 @@ static HRESULT WINAPI IWineD3DSwapChainImpl_Present(IWineD3DSwapChain *iface, CO
         memset(&cursor, 0, sizeof(cursor));
         cursor.lpVtbl = &IWineD3DSurface_Vtbl;
         cursor.resource.ref = 1;
-        cursor.resource.wineD3DDevice = This->wineD3DDevice;
+        cursor.resource.device = This->device;
         cursor.resource.pool = WINED3DPOOL_SCRATCH;
-        cursor.resource.format = WINED3DFMT_A8R8G8B8;
+        cursor.resource.format_desc = getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM, gl_info);
         cursor.resource.resourceType = WINED3DRTYPE_SURFACE;
-        cursor.glDescription.textureName = This->wineD3DDevice->cursorTexture;
-        cursor.glDescription.target = GL_TEXTURE_2D;
-        cursor.glDescription.level = 0;
-        cursor.currentDesc.Width = This->wineD3DDevice->cursorWidth;
-        cursor.currentDesc.Height = This->wineD3DDevice->cursorHeight;
-        cursor.glRect.left = 0;
-        cursor.glRect.top = 0;
-        cursor.glRect.right = cursor.currentDesc.Width;
-        cursor.glRect.bottom = cursor.currentDesc.Height;
+        cursor.texture_name = This->device->cursorTexture;
+        cursor.texture_target = GL_TEXTURE_2D;
+        cursor.texture_level = 0;
+        cursor.currentDesc.Width = This->device->cursorWidth;
+        cursor.currentDesc.Height = This->device->cursorHeight;
         /* The cursor must have pow2 sizes */
         cursor.pow2Width = cursor.currentDesc.Width;
         cursor.pow2Height = cursor.currentDesc.Height;
@@ -173,52 +273,85 @@ static HRESULT WINAPI IWineD3DSwapChainImpl_Present(IWineD3DSwapChain *iface, CO
         if (This->presentParms.Windowed) {
             MapWindowPoints(NULL, This->win_handle, (LPPOINT)&destRect, 2);
         }
-        IWineD3DSurface_Blt(This->backBuffer[0], &destRect, (IWineD3DSurface *) &cursor, NULL, WINEDDBLT_KEYSRC, NULL, WINED3DTEXF_NONE);
+        IWineD3DSurface_Blt((IWineD3DSurface *)This->back_buffers[0], &destRect, (IWineD3DSurface *)&cursor,
+                NULL, WINEDDBLT_KEYSRC, NULL, WINED3DTEXF_POINT);
     }
-    if(This->wineD3DDevice->logo_surface) {
-        /* Blit the logo into the upper left corner of the drawable */
-        IWineD3DSurface_BltFast(This->backBuffer[0], 0, 0, This->wineD3DDevice->logo_surface, NULL, WINEDDBLTFAST_SRCCOLORKEY);
+
+    if (This->device->logo_surface)
+    {
+        /* Blit the logo into the upper left corner of the drawable. */
+        IWineD3DSurface_BltFast((IWineD3DSurface *)This->back_buffers[0], 0, 0,
+                This->device->logo_surface, NULL, WINEDDBLTFAST_SRCCOLORKEY);
     }
 
-    if (pSourceRect || pDestRect) FIXME("Unhandled present options %p/%p\n", pSourceRect, pDestRect);
-    /* TODO: If only source rect or dest rect are supplied then clip the window to match */
-    TRACE("preseting HDC %p\n", This->context[0]->hdc);
+    TRACE("Presenting HDC %p.\n", context->hdc);
 
-    /* Don't call checkGLcall, as glGetError is not applicable here */
-    if (hDestWindowOverride && This->win_handle != hDestWindowOverride) {
-        WINED3DLOCKED_RECT r;
-        BYTE *mem;
+    render_to_fbo = This->render_to_fbo;
 
-        TRACE("Performing dest override of swapchain %p from window %p to %p\n", This, This->win_handle, hDestWindowOverride);
-        if(This->context[0] == This->wineD3DDevice->contexts[0]) {
-            /* The primary context 'owns' all the opengl resources. Destroying and recreating that context would require downloading
-             * all opengl resources, deleting the gl resources, destroying all other contexts, then recreating all other contexts
-             * and reload the resources
-             */
-            ERR("Cannot change the destination window of the owner of the primary context\n");
-        } else {
-            This->win_handle             = hDestWindowOverride;
+    if (pSourceRect)
+    {
+        src_rect = *pSourceRect;
+        if (!render_to_fbo && (src_rect.left || src_rect.top
+                || src_rect.right != This->presentParms.BackBufferWidth
+                || src_rect.bottom != This->presentParms.BackBufferHeight))
+        {
+            render_to_fbo = TRUE;
+        }
+    }
+    else
+    {
+        src_rect.left = 0;
+        src_rect.top = 0;
+        src_rect.right = This->presentParms.BackBufferWidth;
+        src_rect.bottom = This->presentParms.BackBufferHeight;
+    }
 
-            /* The old back buffer has to be copied over to the new back buffer. A lockrect - switchcontext - unlockrect
-             * would suffice in theory, but it is rather nasty and may cause troubles with future changes of the locking code
-             * So lock read only, copy the surface out, then lock with the discard flag and write back
-             */
-            IWineD3DSurface_LockRect(This->backBuffer[0], &r, NULL, WINED3DLOCK_READONLY);
-            mem = HeapAlloc(GetProcessHeap(), 0, r.Pitch * ((IWineD3DSurfaceImpl *) This->backBuffer[0])->currentDesc.Height);
-            memcpy(mem, r.pBits, r.Pitch * ((IWineD3DSurfaceImpl *) This->backBuffer[0])->currentDesc.Height);
-            IWineD3DSurface_UnlockRect(This->backBuffer[0]);
-
-            DestroyContext(This->wineD3DDevice, This->context[0]);
-            This->context[0] = CreateContext(This->wineD3DDevice, (IWineD3DSurfaceImpl *) This->frontBuffer, This->win_handle, FALSE /* pbuffer */, &This->presentParms);
-
-            IWineD3DSurface_LockRect(This->backBuffer[0], &r, NULL, WINED3DLOCK_DISCARD);
-            memcpy(r.pBits, mem, r.Pitch * ((IWineD3DSurfaceImpl *) This->backBuffer[0])->currentDesc.Height);
-            HeapFree(GetProcessHeap(), 0, mem);
-            IWineD3DSurface_UnlockRect(This->backBuffer[0]);
+    if (pDestRect) dst_rect = *pDestRect;
+    else GetClientRect(This->win_handle, &dst_rect);
+
+    if (!render_to_fbo && (dst_rect.left || dst_rect.top
+            || dst_rect.right != This->presentParms.BackBufferWidth
+            || dst_rect.bottom != This->presentParms.BackBufferHeight))
+    {
+        render_to_fbo = TRUE;
+    }
+
+    /* Rendering to a window of different size, presenting partial rectangles,
+     * or rendering to a different window needs help from FBO_blit or a textured
+     * draw. Render the swapchain to a FBO in the future.
+     *
+     * Note that FBO_blit from the backbuffer to the frontbuffer cannot solve
+     * all these issues - this fails if the window is smaller than the backbuffer.
+     */
+    if (!This->render_to_fbo && render_to_fbo && wined3d_settings.offscreen_rendering_mode == ORM_FBO)
+    {
+        IWineD3DSurface_LoadLocation((IWineD3DSurface *)This->back_buffers[0], SFLAG_INTEXTURE, NULL);
+        IWineD3DSurface_ModifyLocation((IWineD3DSurface *)This->back_buffers[0], SFLAG_INDRAWABLE, FALSE);
+        This->render_to_fbo = TRUE;
+
+        /* Force the context manager to update the render target configuration next draw. */
+        context->current_rt = NULL;
+    }
+
+    if(This->render_to_fbo)
+    {
+        /* This codepath should only be hit with the COPY swapeffect. Otherwise a backbuffer-
+         * window size mismatch is impossible(fullscreen) and src and dst rectangles are
+         * not allowed(they need the COPY swapeffect)
+         *
+         * The DISCARD swap effect is ok as well since any backbuffer content is allowed after
+         * the swap
+         */
+        if(This->presentParms.SwapEffect == WINED3DSWAPEFFECT_FLIP )
+        {
+            FIXME("Render-to-fbo with WINED3DSWAPEFFECT_FLIP\n");
         }
+
+        swapchain_blit(This, context, &src_rect, &dst_rect);
     }
 
-    SwapBuffers(This->context[0]->hdc); /* TODO: cycle through the swapchain buffers */
+    if (This->num_contexts > 1) wglFinish();
+    SwapBuffers(context->hdc); /* TODO: cycle through the swapchain buffers */
 
     TRACE("SwapBuffers called, Starting new frame\n");
     /* FPS support */
@@ -274,98 +407,87 @@ static HRESULT WINAPI IWineD3DSwapChainImpl_Present(IWineD3DSwapChain *iface, CO
 }
 #endif
 
-    if (This->presentParms.SwapEffect == WINED3DSWAPEFFECT_DISCARD) {
-        TRACE("Clearing the color buffer with pink color\n");
+    /* This is disabled, but the code left in for debug purposes.
+     *
+     * Since we're allowed to modify the new back buffer on a D3DSWAPEFFECT_DISCARD flip,
+     * we can clear it with some ugly color to make bad drawing visible and ease debugging.
+     * The Debug runtime does the same on Windows. However, a few games do not redraw the
+     * screen properly, like Max Payne 2, which leaves a few pixels undefined.
+     *
+     * Tests show that the content of the back buffer after a discard flip is indeed not
+     * reliable, so no game can depend on the exact content. However, it resembles the
+     * old contents in some way, for example by showing fragments at other locations. In
+     * general, the color theme is still intact. So Max payne, which draws rather dark scenes
+     * gets a dark background image. If we clear it with a bright ugly color, the game's
+     * bug shows up much more than it does on Windows, and the players see single pixels
+     * with wrong colors.
+     * (The Max Payne bug has been confirmed on Windows with the debug runtime)
+     */
+    if (FALSE && This->presentParms.SwapEffect == WINED3DSWAPEFFECT_DISCARD) {
+        TRACE("Clearing the color buffer with cyan color\n");
 
-        IWineD3DDevice_Clear((IWineD3DDevice*)This->wineD3DDevice, 0, NULL,
-                              WINED3DCLEAR_TARGET, 0xff00ffff, 1.0, 0);
+        IWineD3DDevice_Clear((IWineD3DDevice *)This->device, 0, NULL,
+                WINED3DCLEAR_TARGET, 0xff00ffff, 1.0f, 0);
     }
 
-    if(((IWineD3DSurfaceImpl *) This->frontBuffer)->Flags   & SFLAG_INSYSMEM ||
-       ((IWineD3DSurfaceImpl *) This->backBuffer[0])->Flags & SFLAG_INSYSMEM ) {
-        /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying */
-        IWineD3DSurfaceImpl *front = (IWineD3DSurfaceImpl *) This->frontBuffer;
-        IWineD3DSurfaceImpl *back = (IWineD3DSurfaceImpl *) This->backBuffer[0];
-        BOOL frontuptodate = front->Flags & SFLAG_INSYSMEM;
-        BOOL backuptodate = back->Flags & SFLAG_INSYSMEM;
+    if (!This->render_to_fbo && ((This->front_buffer->Flags & SFLAG_INSYSMEM)
+            || (This->back_buffers[0]->Flags & SFLAG_INSYSMEM)))
+    {
+        /* Both memory copies of the surfaces are ok, flip them around too instead of dirtifying
+         * Doesn't work with render_to_fbo because we're not flipping
+         */
+        IWineD3DSurfaceImpl *front = This->front_buffer;
+        IWineD3DSurfaceImpl *back = This->back_buffers[0];
 
         if(front->resource.size == back->resource.size) {
-            /* Flip the DC */
-            {
-                HDC tmp;
-                tmp = front->hDC;
-                front->hDC = back->hDC;
-                back->hDC = tmp;
-            }
-
-            /* Flip the DIBsection */
-            {
-                HBITMAP tmp;
-                BOOL hasDib = front->Flags & SFLAG_DIBSECTION;
-                tmp = front->dib.DIBsection;
-                front->dib.DIBsection = back->dib.DIBsection;
-                back->dib.DIBsection = tmp;
-
-                if(back->Flags & SFLAG_DIBSECTION) front->Flags |= SFLAG_DIBSECTION;
-                else front->Flags &= ~SFLAG_DIBSECTION;
-                if(hasDib) back->Flags |= SFLAG_DIBSECTION;
-                else back->Flags &= ~SFLAG_DIBSECTION;
-            }
+            DWORD fbflags;
+            flip_surface(front, back);
 
-            /* Flip the surface data */
-            {
-                void* tmp;
-
-                tmp = front->dib.bitmap_data;
-                front->dib.bitmap_data = back->dib.bitmap_data;
-                back->dib.bitmap_data = tmp;
-
-                tmp = front->resource.allocatedMemory;
-                front->resource.allocatedMemory = back->resource.allocatedMemory;
-                back->resource.allocatedMemory = tmp;
-
-                tmp = front->resource.heapMemory;
-                front->resource.heapMemory = back->resource.heapMemory;
-                back->resource.heapMemory = tmp;
-            }
-
-            /* Flip the PBO */
-            {
-                DWORD tmp_flags = front->Flags;
-
-                GLuint tmp_pbo = front->pbo;
-                front->pbo = back->pbo;
-                back->pbo = tmp_pbo;
-
-                if(back->Flags & SFLAG_PBO)
-                    front->Flags |= SFLAG_PBO;
-                else
-                    front->Flags &= ~SFLAG_PBO;
-
-                if(tmp_flags & SFLAG_PBO)
-                    back->Flags |= SFLAG_PBO;
-                else
-                    back->Flags &= ~SFLAG_PBO;
-            }
-
-            /* client_memory should not be different, but just in case */
-            {
-                BOOL tmp;
-                tmp = front->dib.client_memory;
-                front->dib.client_memory = back->dib.client_memory;
-                back->dib.client_memory = tmp;
-            }
-            if(frontuptodate) back->Flags |= SFLAG_INSYSMEM;
-            else back->Flags &= ~SFLAG_INSYSMEM;
-            if(backuptodate) front->Flags |= SFLAG_INSYSMEM;
-            else front->Flags &= ~SFLAG_INSYSMEM;
+            /* Tell the front buffer surface that is has been modified. However,
+             * the other locations were preserved during that, so keep the flags.
+             * This serves to update the emulated overlay, if any
+             */
+            fbflags = front->Flags;
+            IWineD3DSurface_ModifyLocation((IWineD3DSurface *)front, SFLAG_INDRAWABLE, TRUE);
+            front->Flags = fbflags;
         } else {
             IWineD3DSurface_ModifyLocation((IWineD3DSurface *) front, SFLAG_INDRAWABLE, TRUE);
             IWineD3DSurface_ModifyLocation((IWineD3DSurface *) back, SFLAG_INDRAWABLE, TRUE);
         }
     }
+    else
+    {
+        IWineD3DSurface_ModifyLocation((IWineD3DSurface *)This->front_buffer, SFLAG_INDRAWABLE, TRUE);
+        /* If the swapeffect is DISCARD, the back buffer is undefined. That means the SYSMEM
+         * and INTEXTURE copies can keep their old content if they have any defined content.
+         * If the swapeffect is COPY, the content remains the same. If it is FLIP however,
+         * the texture / sysmem copy needs to be reloaded from the drawable
+         */
+        if (This->presentParms.SwapEffect == WINED3DSWAPEFFECT_FLIP)
+        {
+            IWineD3DSurface_ModifyLocation((IWineD3DSurface *)This->back_buffers[0], SFLAG_INDRAWABLE, TRUE);
+        }
+    }
 
-    if(This->presentParms.PresentationInterval != WINED3DPRESENT_INTERVAL_IMMEDIATE && GL_SUPPORT(SGI_VIDEO_SYNC)) {
+    if (This->device->depth_stencil)
+    {
+        if (This->presentParms.Flags & WINED3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
+                || This->device->depth_stencil->Flags & SFLAG_DISCARD)
+        {
+            surface_modify_ds_location(This->device->depth_stencil, SFLAG_DS_DISCARDED,
+                    This->device->depth_stencil->currentDesc.Width,
+                    This->device->depth_stencil->currentDesc.Height);
+            if (This->device->depth_stencil == This->device->onscreen_depth_stencil)
+            {
+                IWineD3DSurface_Release((IWineD3DSurface *)This->device->onscreen_depth_stencil);
+                This->device->onscreen_depth_stencil = NULL;
+            }
+        }
+    }
+
+    if (This->presentParms.PresentationInterval != WINED3DPRESENT_INTERVAL_IMMEDIATE
+            && gl_info->supported[SGI_VIDEO_SYNC])
+    {
         retval = GL_EXTCALL(glXGetVideoSyncSGI(&sync));
         if(retval != 0) {
             ERR("glXGetVideoSyncSGI failed(retval = %d\n", retval);
@@ -406,174 +528,451 @@ static HRESULT WINAPI IWineD3DSwapChainImpl_Present(IWineD3DSwapChain *iface, CO
         }
     }
 
+    context_release(context);
+
     TRACE("returning\n");
     return WINED3D_OK;
 }
 
-static HRESULT WINAPI IWineD3DSwapChainImpl_GetFrontBufferData(IWineD3DSwapChain *iface, IWineD3DSurface *pDestSurface) {
-    IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
-    POINT start;
+static HRESULT WINAPI IWineD3DSwapChainImpl_SetDestWindowOverride(IWineD3DSwapChain *iface, HWND window)
+{
+    IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)iface;
 
-    TRACE("(%p) : iface(%p) pDestSurface(%p)\n", This, iface, pDestSurface);
+    if (!window) window = swapchain->device_window;
+    if (window == swapchain->win_handle) return WINED3D_OK;
 
-    start.x = 0;
-    start.y = 0;
+    TRACE("Setting swapchain %p window from %p to %p\n", swapchain, swapchain->win_handle, window);
+    swapchain->win_handle = window;
 
-    if (This->presentParms.Windowed) {
-        MapWindowPoints(This->win_handle, NULL, &start, 1);
-    }
-
-    IWineD3DSurface_BltFast(pDestSurface, start.x, start.y, This->frontBuffer, NULL, 0);
     return WINED3D_OK;
 }
 
-static HRESULT WINAPI IWineD3DSwapChainImpl_GetBackBuffer(IWineD3DSwapChain *iface, UINT iBackBuffer, WINED3DBACKBUFFER_TYPE Type, IWineD3DSurface **ppBackBuffer) {
+static const IWineD3DSwapChainVtbl IWineD3DSwapChain_Vtbl =
+{
+    /* IUnknown */
+    IWineD3DBaseSwapChainImpl_QueryInterface,
+    IWineD3DBaseSwapChainImpl_AddRef,
+    IWineD3DBaseSwapChainImpl_Release,
+    /* IWineD3DSwapChain */
+    IWineD3DBaseSwapChainImpl_GetParent,
+    IWineD3DSwapChainImpl_Destroy,
+    IWineD3DBaseSwapChainImpl_GetDevice,
+    IWineD3DSwapChainImpl_Present,
+    IWineD3DSwapChainImpl_SetDestWindowOverride,
+    IWineD3DBaseSwapChainImpl_GetFrontBufferData,
+    IWineD3DBaseSwapChainImpl_GetBackBuffer,
+    IWineD3DBaseSwapChainImpl_GetRasterStatus,
+    IWineD3DBaseSwapChainImpl_GetDisplayMode,
+    IWineD3DBaseSwapChainImpl_GetPresentParameters,
+    IWineD3DBaseSwapChainImpl_SetGammaRamp,
+    IWineD3DBaseSwapChainImpl_GetGammaRamp
+};
+
+static LONG fullscreen_style(LONG style)
+{
+    /* Make sure the window is managed, otherwise we won't get keyboard input. */
+    style |= WS_POPUP | WS_SYSMENU;
+    style &= ~(WS_CAPTION | WS_THICKFRAME);
 
-    IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
+    return style;
+}
 
-    if (iBackBuffer > This->presentParms.BackBufferCount - 1) {
-        TRACE("Back buffer count out of range\n");
-        /* Native d3d9 doesn't set NULL here, just as wine's d3d9. But set it here
-         * in wined3d to avoid problems in other libs
-         */
-        *ppBackBuffer = NULL;
-        return WINED3DERR_INVALIDCALL;
+static LONG fullscreen_exstyle(LONG exstyle)
+{
+    /* Filter out window decorations. */
+    exstyle &= ~(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
+
+    return exstyle;
+}
+
+void swapchain_setup_fullscreen_window(IWineD3DSwapChainImpl *swapchain, UINT w, UINT h)
+{
+    IWineD3DDeviceImpl *device = swapchain->device;
+    HWND window = swapchain->device_window;
+    BOOL filter_messages;
+    LONG style, exstyle;
+
+    TRACE("Setting up window %p for fullscreen mode.\n", window);
+
+    if (device->style || device->exStyle)
+    {
+        ERR("Changing the window style for window %p, but another style (%08x, %08x) is already stored.\n",
+                window, device->style, device->exStyle);
     }
 
-    *ppBackBuffer = This->backBuffer[iBackBuffer];
-    TRACE("(%p) : BackBuf %d Type %d  returning %p\n", This, iBackBuffer, Type, *ppBackBuffer);
+    device->style = GetWindowLongW(window, GWL_STYLE);
+    device->exStyle = GetWindowLongW(window, GWL_EXSTYLE);
 
-    /* Note inc ref on returned surface */
-    if(*ppBackBuffer) IWineD3DSurface_AddRef(*ppBackBuffer);
-    return WINED3D_OK;
+    style = fullscreen_style(device->style);
+    exstyle = fullscreen_exstyle(device->exStyle);
 
+    TRACE("Old style was %08x, %08x, setting to %08x, %08x.\n",
+            device->style, device->exStyle, style, exstyle);
+
+    filter_messages = device->filter_messages;
+    device->filter_messages = TRUE;
+
+    SetWindowLongW(window, GWL_STYLE, style);
+    SetWindowLongW(window, GWL_EXSTYLE, exstyle);
+    SetWindowPos(window, HWND_TOP, 0, 0, w, h, SWP_FRAMECHANGED | SWP_SHOWWINDOW | SWP_NOACTIVATE);
+
+    device->filter_messages = filter_messages;
 }
 
-static HRESULT WINAPI IWineD3DSwapChainImpl_GetRasterStatus(IWineD3DSwapChain *iface, WINED3DRASTER_STATUS *pRasterStatus) {
-    IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
-    static BOOL showFixmes = TRUE;
-    pRasterStatus->InVBlank = TRUE;
-    pRasterStatus->ScanLine = 0;
-    /* No openGL equivalent */
-    if(showFixmes) {
-        FIXME("(%p) : stub (once)\n", This);
-        showFixmes = FALSE;
+void swapchain_restore_fullscreen_window(IWineD3DSwapChainImpl *swapchain)
+{
+    IWineD3DDeviceImpl *device = swapchain->device;
+    HWND window = swapchain->device_window;
+    BOOL filter_messages;
+    LONG style, exstyle;
+
+    if (!device->style && !device->exStyle) return;
+
+    TRACE("Restoring window style of window %p to %08x, %08x.\n",
+            window, device->style, device->exStyle);
+
+    style = GetWindowLongW(window, GWL_STYLE);
+    exstyle = GetWindowLongW(window, GWL_EXSTYLE);
+
+    filter_messages = device->filter_messages;
+    device->filter_messages = TRUE;
+
+    /* Only restore the style if the application didn't modify it during the
+     * fullscreen phase. Some applications change it before calling Reset()
+     * when switching between windowed and fullscreen modes (HL2), some
+     * depend on the original style (Eve Online). */
+    if (style == fullscreen_style(device->style) && exstyle == fullscreen_exstyle(device->exStyle))
+    {
+        SetWindowLongW(window, GWL_STYLE, device->style);
+        SetWindowLongW(window, GWL_EXSTYLE, device->exStyle);
     }
-    return WINED3D_OK;
+    SetWindowPos(window, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
+
+    device->filter_messages = filter_messages;
+
+    /* Delete the old values. */
+    device->style = 0;
+    device->exStyle = 0;
 }
 
-static HRESULT WINAPI IWineD3DSwapChainImpl_GetDisplayMode(IWineD3DSwapChain *iface, WINED3DDISPLAYMODE*pMode) {
-    IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
-    HDC                 hdc;
-    int                 bpp = 0;
 
-    pMode->Width        = GetSystemMetrics(SM_CXSCREEN);
-    pMode->Height       = GetSystemMetrics(SM_CYSCREEN);
-    pMode->RefreshRate  = 85; /* FIXME: How to identify? */
+HRESULT swapchain_init(IWineD3DSwapChainImpl *swapchain, WINED3DSURFTYPE surface_type,
+        IWineD3DDeviceImpl *device, WINED3DPRESENT_PARAMETERS *present_parameters, IUnknown *parent)
+{
+    const struct wined3d_adapter *adapter = device->adapter;
+    const struct wined3d_format_desc *format_desc;
+    BOOL displaymode_set = FALSE;
+    WINED3DDISPLAYMODE mode;
+    RECT client_rect;
+    HWND window;
+    HRESULT hr;
+    UINT i;
 
-    hdc = GetDC(0);
-    bpp = GetDeviceCaps(hdc, BITSPIXEL);
-    ReleaseDC(0, hdc);
+    if (present_parameters->BackBufferCount > WINED3DPRESENT_BACK_BUFFER_MAX)
+    {
+        FIXME("The application requested %u back buffers, this is not supported.\n",
+                present_parameters->BackBufferCount);
+        return WINED3DERR_INVALIDCALL;
+    }
 
-    switch (bpp) {
-    case  8: pMode->Format       = WINED3DFMT_R8G8B8; break;
-    case 16: pMode->Format       = WINED3DFMT_R5G6B5; break;
-    case 24: /*pMode->Format       = WINED3DFMT_R8G8B8; break; */ /* 32bpp and 24bpp can be aliased for X */
-    case 32: pMode->Format       = WINED3DFMT_A8R8G8B8; break;
-    default:
-       FIXME("Unrecognized display mode format\n");
-       pMode->Format       = WINED3DFMT_UNKNOWN;
+    if (present_parameters->BackBufferCount > 1)
+    {
+        FIXME("The application requested more than one back buffer, this is not properly supported.\n"
+                "Please configure the application to use double buffering (1 back buffer) if possible.\n");
     }
 
-    TRACE("(%p) : returning w(%d) h(%d) rr(%d) fmt(%u,%s)\n", This, pMode->Width, pMode->Height, pMode->RefreshRate,
-    pMode->Format, debug_d3dformat(pMode->Format));
-    return WINED3D_OK;
-}
+    switch (surface_type)
+    {
+        case SURFACE_GDI:
+            swapchain->lpVtbl = &IWineGDISwapChain_Vtbl;
+            break;
 
-static HRESULT WINAPI IWineD3DSwapChainImpl_GetDevice(IWineD3DSwapChain *iface, IWineD3DDevice**ppDevice) {
-    IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
+        case SURFACE_OPENGL:
+            swapchain->lpVtbl = &IWineD3DSwapChain_Vtbl;
+            break;
 
-    *ppDevice = (IWineD3DDevice *) This->wineD3DDevice;
+        case SURFACE_UNKNOWN:
+            FIXME("Caller tried to create a SURFACE_UNKNOWN swapchain.\n");
+            return WINED3DERR_INVALIDCALL;
+    }
 
-    /* Note  Calling this method will increase the internal reference count
-       on the IDirect3DDevice9 interface. */
-    IWineD3DDevice_AddRef(*ppDevice);
-    TRACE("(%p) : returning %p\n", This, *ppDevice);
-    return WINED3D_OK;
-}
+    window = present_parameters->hDeviceWindow ? present_parameters->hDeviceWindow : device->createParms.hFocusWindow;
 
-static HRESULT WINAPI IWineD3DSwapChainImpl_GetPresentParameters(IWineD3DSwapChain *iface, WINED3DPRESENT_PARAMETERS *pPresentationParameters) {
-    IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
-    TRACE("(%p)\n", This);
+    swapchain->device = device;
+    swapchain->parent = parent;
+    swapchain->ref = 1;
+    swapchain->win_handle = window;
+    swapchain->device_window = window;
 
-    *pPresentationParameters = This->presentParms;
+    if (!present_parameters->Windowed && window)
+    {
+        swapchain_setup_fullscreen_window(swapchain, present_parameters->BackBufferWidth,
+                present_parameters->BackBufferHeight);
+    }
 
-    return WINED3D_OK;
-}
+    IWineD3D_GetAdapterDisplayMode(device->wined3d, adapter->ordinal, &mode);
+    swapchain->orig_width = mode.Width;
+    swapchain->orig_height = mode.Height;
+    swapchain->orig_fmt = mode.Format;
+    format_desc = getFormatDescEntry(mode.Format, &adapter->gl_info);
 
-static HRESULT WINAPI IWineD3DSwapChainImpl_SetGammaRamp(IWineD3DSwapChain *iface, DWORD Flags, CONST WINED3DGAMMARAMP *pRamp){
+    GetClientRect(window, &client_rect);
+    if (present_parameters->Windowed
+            && (!present_parameters->BackBufferWidth || !present_parameters->BackBufferHeight
+            || present_parameters->BackBufferFormat == WINED3DFMT_UNKNOWN))
+    {
 
-    IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
-    HDC hDC;
-    TRACE("(%p) : pRamp@%p flags(%d)\n", This, pRamp, Flags);
-    hDC = GetDC(This->win_handle);
-    SetDeviceGammaRamp(hDC, (LPVOID)pRamp);
-    ReleaseDC(This->win_handle, hDC);
-    return WINED3D_OK;
+        if (!present_parameters->BackBufferWidth)
+        {
+            present_parameters->BackBufferWidth = client_rect.right;
+            TRACE("Updating width to %u.\n", present_parameters->BackBufferWidth);
+        }
 
-}
+        if (!present_parameters->BackBufferHeight)
+        {
+            present_parameters->BackBufferHeight = client_rect.bottom;
+            TRACE("Updating height to %u.\n", present_parameters->BackBufferHeight);
+        }
 
-static HRESULT WINAPI IWineD3DSwapChainImpl_GetGammaRamp(IWineD3DSwapChain *iface, WINED3DGAMMARAMP *pRamp){
+        if (present_parameters->BackBufferFormat == WINED3DFMT_UNKNOWN)
+        {
+            present_parameters->BackBufferFormat = swapchain->orig_fmt;
+            TRACE("Updating format to %s.\n", debug_d3dformat(swapchain->orig_fmt));
+        }
+    }
+    swapchain->presentParms = *present_parameters;
+
+    if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
+            && present_parameters->BackBufferCount
+            && (present_parameters->BackBufferWidth != client_rect.right
+            || present_parameters->BackBufferHeight != client_rect.bottom))
+    {
+        TRACE("Rendering to FBO. Backbuffer %ux%u, window %ux%u.\n",
+                present_parameters->BackBufferWidth,
+                present_parameters->BackBufferHeight,
+                client_rect.right, client_rect.bottom);
+        swapchain->render_to_fbo = TRUE;
+    }
+
+    TRACE("Creating front buffer.\n");
+    hr = IWineD3DDeviceParent_CreateRenderTarget(device->device_parent, parent,
+            swapchain->presentParms.BackBufferWidth, swapchain->presentParms.BackBufferHeight,
+            swapchain->presentParms.BackBufferFormat, swapchain->presentParms.MultiSampleType,
+            swapchain->presentParms.MultiSampleQuality, TRUE /* Lockable */,
+            (IWineD3DSurface **)&swapchain->front_buffer);
+    if (FAILED(hr))
+    {
+        WARN("Failed to create front buffer, hr %#x.\n", hr);
+        goto err;
+    }
+
+    IWineD3DSurface_SetContainer((IWineD3DSurface *)swapchain->front_buffer, (IWineD3DBase *)swapchain);
+    swapchain->front_buffer->Flags |= SFLAG_SWAPCHAIN;
+    if (surface_type == SURFACE_OPENGL)
+    {
+        IWineD3DSurface_ModifyLocation((IWineD3DSurface *)swapchain->front_buffer, SFLAG_INDRAWABLE, TRUE);
+    }
+
+    /* MSDN says we're only allowed a single fullscreen swapchain per device,
+     * so we should really check to see if there is a fullscreen swapchain
+     * already. Does a single head count as full screen? */
+
+    if (!present_parameters->Windowed)
+    {
+        WINED3DDISPLAYMODE mode;
+
+        /* Change the display settings */
+        mode.Width = present_parameters->BackBufferWidth;
+        mode.Height = present_parameters->BackBufferHeight;
+        mode.Format = present_parameters->BackBufferFormat;
+        mode.RefreshRate = present_parameters->FullScreen_RefreshRateInHz;
+
+        hr = IWineD3DDevice_SetDisplayMode((IWineD3DDevice *)device, 0, &mode);
+        if (FAILED(hr))
+        {
+            WARN("Failed to set display mode, hr %#x.\n", hr);
+            goto err;
+        }
+        displaymode_set = TRUE;
+    }
+
+    swapchain->context = HeapAlloc(GetProcessHeap(), 0, sizeof(swapchain->context));
+    if (!swapchain->context)
+    {
+        ERR("Failed to create the context array.\n");
+        hr = E_OUTOFMEMORY;
+        goto err;
+    }
+    swapchain->num_contexts = 1;
+
+    if (surface_type == SURFACE_OPENGL)
+    {
+        WINED3DFORMAT formats[] =
+        {
+            WINED3DFMT_D24_UNORM_S8_UINT,
+            WINED3DFMT_D32_UNORM,
+            WINED3DFMT_R24_UNORM_X8_TYPELESS,
+            WINED3DFMT_D16_UNORM,
+            WINED3DFMT_S1_UINT_D15_UNORM
+        };
+
+        const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
+
+        /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
+         * You are able to add a depth + stencil surface at a later stage when you need it.
+         * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
+         * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
+         * context, need torecreate shaders, textures and other resources.
+         *
+         * The context manager already takes care of the state problem and for the other tasks code from Reset
+         * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
+         * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
+         * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
+         * issue needs to be fixed. */
+        for (i = 0; i < (sizeof(formats) / sizeof(*formats)); i++)
+        {
+            swapchain->ds_format = getFormatDescEntry(formats[i], gl_info);
+            swapchain->context[0] = context_create(swapchain, swapchain->front_buffer, swapchain->ds_format);
+            if (swapchain->context[0]) break;
+            TRACE("Depth stencil format %s is not supported, trying next format\n",
+                  debug_d3dformat(formats[i]));
+        }
+
+        if (!swapchain->context[0])
+        {
+            WARN("Failed to create context.\n");
+            hr = WINED3DERR_NOTAVAILABLE;
+            goto err;
+        }
+
+        if (!present_parameters->EnableAutoDepthStencil
+                || swapchain->presentParms.AutoDepthStencilFormat != swapchain->ds_format->format)
+        {
+            FIXME("Add OpenGL context recreation support to context_validate_onscreen_formats\n");
+        }
+        context_release(swapchain->context[0]);
+    }
+    else
+    {
+        swapchain->context[0] = NULL;
+    }
+
+    if (swapchain->presentParms.BackBufferCount > 0)
+    {
+        swapchain->back_buffers = HeapAlloc(GetProcessHeap(), 0,
+                sizeof(*swapchain->back_buffers) * swapchain->presentParms.BackBufferCount);
+        if (!swapchain->back_buffers)
+        {
+            ERR("Failed to allocate backbuffer array memory.\n");
+            hr = E_OUTOFMEMORY;
+            goto err;
+        }
+
+        for (i = 0; i < swapchain->presentParms.BackBufferCount; ++i)
+        {
+            TRACE("Creating back buffer %u.\n", i);
+            hr = IWineD3DDeviceParent_CreateRenderTarget(device->device_parent, parent,
+                    swapchain->presentParms.BackBufferWidth, swapchain->presentParms.BackBufferHeight,
+                    swapchain->presentParms.BackBufferFormat, swapchain->presentParms.MultiSampleType,
+                    swapchain->presentParms.MultiSampleQuality, TRUE /* Lockable */,
+                    (IWineD3DSurface **)&swapchain->back_buffers[i]);
+            if (FAILED(hr))
+            {
+                WARN("Failed to create back buffer %u, hr %#x.\n", i, hr);
+                goto err;
+            }
+
+            IWineD3DSurface_SetContainer((IWineD3DSurface *)swapchain->back_buffers[i], (IWineD3DBase *)swapchain);
+            swapchain->back_buffers[i]->Flags |= SFLAG_SWAPCHAIN;
+        }
+    }
+
+    /* Swapchains share the depth/stencil buffer, so only create a single depthstencil surface. */
+    if (present_parameters->EnableAutoDepthStencil && surface_type == SURFACE_OPENGL)
+    {
+        TRACE("Creating depth/stencil buffer.\n");
+        if (!device->auto_depth_stencil)
+        {
+            hr = IWineD3DDeviceParent_CreateDepthStencilSurface(device->device_parent, parent,
+                    swapchain->presentParms.BackBufferWidth, swapchain->presentParms.BackBufferHeight,
+                    swapchain->presentParms.AutoDepthStencilFormat, swapchain->presentParms.MultiSampleType,
+                    swapchain->presentParms.MultiSampleQuality, FALSE /* FIXME: Discard */,
+                    (IWineD3DSurface **)&device->auto_depth_stencil);
+            if (FAILED(hr))
+            {
+                WARN("Failed to create the auto depth stencil, hr %#x.\n", hr);
+                goto err;
+            }
+
+            IWineD3DSurface_SetContainer((IWineD3DSurface *)device->auto_depth_stencil, NULL);
+        }
+    }
+
+    IWineD3DSwapChain_GetGammaRamp((IWineD3DSwapChain *)swapchain, &swapchain->orig_gamma);
 
-    IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *)iface;
-    HDC hDC;
-    TRACE("(%p) : pRamp@%p\n", This, pRamp);
-    hDC = GetDC(This->win_handle);
-    GetDeviceGammaRamp(hDC, pRamp);
-    ReleaseDC(This->win_handle, hDC);
     return WINED3D_OK;
 
-}
+err:
+    if (displaymode_set)
+    {
+        DEVMODEW devmode;
+
+        ClipCursor(NULL);
+
+        /* Change the display settings */
+        memset(&devmode, 0, sizeof(devmode));
+        devmode.dmSize = sizeof(devmode);
+        devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
+        devmode.dmBitsPerPel = format_desc->byte_count * 8;
+        devmode.dmPelsWidth = swapchain->orig_width;
+        devmode.dmPelsHeight = swapchain->orig_height;
+        ChangeDisplaySettingsExW(adapter->DeviceName, &devmode, NULL, CDS_FULLSCREEN, NULL);
+    }
 
+    if (swapchain->back_buffers)
+    {
+        for (i = 0; i < swapchain->presentParms.BackBufferCount; ++i)
+        {
+            if (swapchain->back_buffers[i]) IWineD3DSurface_Release((IWineD3DSurface *)swapchain->back_buffers[i]);
+        }
+        HeapFree(GetProcessHeap(), 0, swapchain->back_buffers);
+    }
 
-const IWineD3DSwapChainVtbl IWineD3DSwapChain_Vtbl =
-{
-    /* IUnknown */
-    IWineD3DSwapChainImpl_QueryInterface,
-    IWineD3DSwapChainImpl_AddRef,
-    IWineD3DSwapChainImpl_Release,
-    /* IWineD3DSwapChain */
-    IWineD3DSwapChainImpl_GetParent,
-    IWineD3DSwapChainImpl_Destroy,
-    IWineD3DSwapChainImpl_GetDevice,
-    IWineD3DSwapChainImpl_Present,
-    IWineD3DSwapChainImpl_GetFrontBufferData,
-    IWineD3DSwapChainImpl_GetBackBuffer,
-    IWineD3DSwapChainImpl_GetRasterStatus,
-    IWineD3DSwapChainImpl_GetDisplayMode,
-    IWineD3DSwapChainImpl_GetPresentParameters,
-    IWineD3DSwapChainImpl_SetGammaRamp,
-    IWineD3DSwapChainImpl_GetGammaRamp
-};
+    if (swapchain->context)
+    {
+        if (swapchain->context[0])
+        {
+            context_release(swapchain->context[0]);
+            context_destroy(device, swapchain->context[0]);
+            swapchain->num_contexts = 0;
+        }
+        HeapFree(GetProcessHeap(), 0, swapchain->context);
+    }
+
+    if (swapchain->front_buffer) IWineD3DSurface_Release((IWineD3DSurface *)swapchain->front_buffer);
 
-WineD3DContext *IWineD3DSwapChainImpl_CreateContextForThread(IWineD3DSwapChain *iface) {
-    WineD3DContext *ctx;
+    return hr;
+}
+
+struct wined3d_context *swapchain_create_context_for_thread(IWineD3DSwapChain *iface)
+{
     IWineD3DSwapChainImpl *This = (IWineD3DSwapChainImpl *) iface;
-    WineD3DContext **newArray;
+    struct wined3d_context **newArray;
+    struct wined3d_context *ctx;
 
     TRACE("Creating a new context for swapchain %p, thread %d\n", This, GetCurrentThreadId());
 
-    ctx = CreateContext(This->wineD3DDevice, (IWineD3DSurfaceImpl *) This->frontBuffer,
-                        This->context[0]->win_handle, FALSE /* pbuffer */, &This->presentParms);
-    if(!ctx) {
+    if (!(ctx = context_create(This, This->front_buffer, This->ds_format)))
+    {
         ERR("Failed to create a new context for the swapchain\n");
         return NULL;
     }
+    context_release(ctx);
 
     newArray = HeapAlloc(GetProcessHeap(), 0, sizeof(*newArray) * This->num_contexts + 1);
     if(!newArray) {
         ERR("Out of memory when trying to allocate a new context array\n");
-        DestroyContext(This->wineD3DDevice, ctx);
+        context_destroy(This->device, ctx);
         return NULL;
     }
     memcpy(newArray, This->context, sizeof(*newArray) * This->num_contexts);
@@ -585,3 +984,11 @@ WineD3DContext *IWineD3DSwapChainImpl_CreateContextForThread(IWineD3DSwapChain *
     TRACE("Returning context %p\n", ctx);
     return ctx;
 }
+
+void get_drawable_size_swapchain(struct wined3d_context *context, UINT *width, UINT *height)
+{
+    /* The drawable size of an onscreen drawable is the surface size.
+     * (Actually: The window size, but the surface is created in window size) */
+    *width = context->current_rt->currentDesc.Width;
+    *height = context->current_rt->currentDesc.Height;
+}