Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/device.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/device.c	(revision 32460)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/device.c	(revision 32461)
@@ -462,7 +462,11 @@
     return ret;
 }
-
+#ifdef VBOXWDDM
+static HRESULT IDirect3DDevice9Impl_DoCreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
+        D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
+#else
 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
         D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
+#endif
 {
     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
@@ -493,4 +497,33 @@
     return D3D_OK;
 }
+
+#ifdef VBOXWDDM
+static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
+        D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
+{
+    IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
+    IDirect3DSwapChain9Impl *newSwapchain;
+    HRESULT hr = IDirect3DDevice9Impl_DoCreateAdditionalSwapChain(iface, present_parameters, &newSwapchain);
+    if (FAILED(hr))
+    {
+        ERR("Failed to create additional swapchain, hr %#x.\n", hr);
+        return hr;
+    }
+
+    /* add swapchain to the swapchain list */
+    wined3d_mutex_lock();
+    hr = IWineD3DDevice_AddSwapChain(This->WineD3DDevice, newSwapchain->wineD3DSwapChain);
+    wined3d_mutex_unlock();
+    if (FAILED(hr))
+    {
+        ERR("Failed to add additional swapchain, hr %#x.\n", hr);
+        IUnknown_Release(newSwapchain);
+        return hr;
+    }
+
+    *swapchain = (IDirect3DSwapChain9 *)newSwapchain;
+    return D3D_OK;
+}
+#endif
 
 static HRESULT WINAPI reset_enum_callback(IWineD3DResource *resource, void *data) {
@@ -2898,6 +2931,11 @@
     local_parameters.PresentationInterval = present_parameters->PresentationInterval;
 
+#ifdef VBOXWDDM
+    hr = IDirect3DDevice9Impl_DoCreateAdditionalSwapChain((IDirect3DDevice9Ex *)This,
+            &local_parameters, (IDirect3DSwapChain9 **)&d3d_swapchain);
+#else
     hr = IDirect3DDevice9Impl_CreateAdditionalSwapChain((IDirect3DDevice9Ex *)This,
             &local_parameters, (IDirect3DSwapChain9 **)&d3d_swapchain);
+#endif
     if (FAILED(hr))
     {
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/include/wine/wined3d.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/include/wine/wined3d.h	(revision 32460)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/include/wine/wined3d.h	(revision 32461)
@@ -7302,4 +7302,11 @@
         ) = 0;
 
+#ifdef VBOXWDDM
+    virtual HRESULT STDMETHODCALLTYPE Flush(
+        ) = 0;
+
+    virtual HRESULT STDMETHODCALLTYPE AddSwapChain(
+        IWineD3DSwapChain *swapchain) = 0;
+#endif
 };
 #else
@@ -8065,4 +8072,8 @@
     HRESULT (STDMETHODCALLTYPE *Flush)(
         IWineD3DDevice* This);
+
+    HRESULT (STDMETHODCALLTYPE *AddSwapChain)(
+        IWineD3DDevice* This,
+        IWineD3DSwapChain *swapchain);
 #endif
 
@@ -8224,4 +8235,5 @@
 #ifdef VBOXWDDM
 #define IWineD3DDevice_Flush(This) (This)->lpVtbl->Flush(This)
+#define IWineD3DDevice_AddSwapChain(This,swapchain) (This)->lpVtbl->AddSwapChain(This,swapchain)
 #endif
 #endif
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/arb_program_shader.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/arb_program_shader.c	(revision 32460)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/arb_program_shader.c	(revision 32461)
@@ -586,5 +586,9 @@
 static void shader_arb_load_constants(const struct wined3d_context *context, char usePixelShader, char useVertexShader)
 {
+#ifdef VBOXWDDM
+    IWineD3DDeviceImpl *device = context->device;
+#else
     IWineD3DDeviceImpl *device = context->swapchain->device;
+#endif
     IWineD3DStateBlockImpl* stateBlock = device->stateBlock;
     const struct wined3d_gl_info *gl_info = context->gl_info;
@@ -616,5 +620,9 @@
     /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
      * context. On a context switch the old context will be fully dirtified */
+#ifdef VBOXWDDM
+    if (!context || context->device != This) return;
+#else
     if (!context || context->swapchain->device != This) return;
+#endif
 
     memset(context->vshader_const_dirty + start, 1, sizeof(*context->vshader_const_dirty) * count);
@@ -629,5 +637,9 @@
     /* We don't want shader constant dirtification to be an O(contexts), so just dirtify the active
      * context. On a context switch the old context will be fully dirtified */
+#ifdef VBOXWDDM
+    if (!context || context->device != This) return;
+#else
     if (!context || context->swapchain->device != This) return;
+#endif
 
     memset(context->pshader_const_dirty + start, 1, sizeof(*context->pshader_const_dirty) * count);
@@ -4349,5 +4361,9 @@
 static void shader_arb_select(const struct wined3d_context *context, BOOL usePS, BOOL useVS)
 {
+#ifdef VBOXWDDM
+    IWineD3DDeviceImpl *This = context->device;
+#else
     IWineD3DDeviceImpl *This = context->swapchain->device;
+#endif
     struct shader_arb_priv *priv = This->shader_priv;
     const struct wined3d_gl_info *gl_info = context->gl_info;
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/context.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/context.c	(revision 32460)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/context.c	(revision 32461)
@@ -331,5 +331,9 @@
 {
     const struct wined3d_gl_info *gl_info = context->gl_info;
+#ifdef VBOXWDDM
+    IWineD3DDeviceImpl *device = context->device;
+#else
     IWineD3DDeviceImpl *device = context->swapchain->device;
+#endif
     struct fbo_entry *entry;
 
@@ -348,5 +352,9 @@
 {
     const struct wined3d_gl_info *gl_info = context->gl_info;
+#ifdef VBOXWDDM
+    IWineD3DDeviceImpl *device = context->device;
+#else
     IWineD3DDeviceImpl *device = context->swapchain->device;
+#endif
 
     context_bind_fbo(context, GL_FRAMEBUFFER, &entry->id);
@@ -377,5 +385,9 @@
 {
     const struct wined3d_gl_info *gl_info = context->gl_info;
+#ifdef VBOXWDDM
+    IWineD3DDeviceImpl *device = context->device;
+#else
     IWineD3DDeviceImpl *device = context->swapchain->device;
+#endif
     struct fbo_entry *entry;
 
@@ -413,5 +425,9 @@
 {
     const struct wined3d_gl_info *gl_info = context->gl_info;
+#ifdef VBOXWDDM
+    IWineD3DDeviceImpl *device = context->device;
+#else
     IWineD3DDeviceImpl *device = context->swapchain->device;
+#endif
     unsigned int i;
 
@@ -734,8 +750,17 @@
 }
 
-static void context_update_window(struct wined3d_context *context)
-{
+static void context_update_window(struct wined3d_context *context
+#ifdef VBOXWDDM
+        , IWineD3DSwapChainImpl *swapchain
+#endif
+        )
+{
+#ifdef VBOXWDDM
+    TRACE("Updating context %p window from %p to %p.\n",
+            context, context->win_handle, swapchain->win_handle);
+#else
     TRACE("Updating context %p window from %p to %p.\n",
             context, context->win_handle, context->swapchain->win_handle);
+#endif
 
     if (context->valid)
@@ -749,4 +774,9 @@
     else context->valid = 1;
 
+#ifdef VBOXWDDM
+    context->win_handle = swapchain->win_handle;
+    context->hdc = GetDC(context->win_handle);
+    context->currentSwapchain = swapchain;
+#else
     context->win_handle = context->swapchain->win_handle;
 
@@ -756,4 +786,5 @@
         goto err;
     }
+#endif
 
     if (!context_set_pixel_format(context->gl_info, context->hdc, context->pixel_format))
@@ -777,5 +808,9 @@
 }
 
-static void context_validate(struct wined3d_context *context)
+static void context_validate(struct wined3d_context *context
+#ifdef VBOXWDDM
+        , IWineD3DSwapChainImpl *swapchain
+#endif
+        )
 {
     HWND wnd = WindowFromDC(context->hdc);
@@ -788,6 +823,18 @@
     }
 
-    if (context->win_handle != context->swapchain->win_handle)
-        context_update_window(context);
+    if (
+#ifdef VBOXWDDM
+            swapchain && context->win_handle != swapchain->win_handle
+#else
+            context->win_handle != context->swapchain->win_handle
+#endif
+            )
+    {
+        context_update_window(context
+#ifdef VBOXWDDM
+                , swapchain
+#endif
+                );
+    }
 }
 
@@ -805,5 +852,9 @@
     restore_dc = pwglGetCurrentDC();
 
-    context_validate(context);
+    context_validate(context
+#ifdef VBOXWDDM
+            , NULL//context->device->swapchains[0]
+#endif
+            );
     if (context->valid && restore_ctx != context->glCtx) pwglMakeCurrent(context->hdc, context->glCtx);
     else restore_ctx = NULL;
@@ -1210,4 +1261,7 @@
 }
 
+
+
+
 /*****************************************************************************
  * context_create
@@ -1361,5 +1415,10 @@
     }
 
+#ifdef VBOXWDDM
+    ret->device = device;
+    ret->currentSwapchain = swapchain;
+#else
     ret->swapchain = swapchain;
+#endif
     ret->current_rt = (IWineD3DSurface *)target;
     ret->tid = GetCurrentThreadId();
@@ -1529,4 +1588,60 @@
     return NULL;
 }
+
+#ifdef VBOXWDDM
+static void context_setup_target(IWineD3DDeviceImpl *device, struct wined3d_context *context, IWineD3DSurface *target);
+
+struct wined3d_context *context_find_create(IWineD3DDeviceImpl *device, IWineD3DSwapChainImpl *swapchain, IWineD3DSurfaceImpl *target,
+        const struct wined3d_format_desc *ds_format_desc)
+{
+    IWineD3DSwapChainImpl *swapChain = NULL;
+    int i, j;
+    int swapchains = IWineD3DDevice_GetNumberOfSwapChains(device);
+    DWORD tid = GetCurrentThreadId();
+    struct wined3d_context *context = NULL;
+
+    Assert(0);
+
+    for(i = 0 ; i < swapchains ; i ++)
+    {
+        IWineD3DDevice_GetSwapChain(device, i, &swapChain);
+        if (swapChain->ds_format == ds_format_desc)
+        {
+            for(j = 0; j < swapChain->num_contexts; j++) {
+                if(swapChain->context[j]->tid == tid
+                        && swapChain->context[j]->tid == tid) {
+                    context = swapChain->context[j];
+                    break;
+                }
+            }
+        }
+        else
+        {
+            Assert(0);
+        }
+
+        if (context)
+            break;
+        IWineD3DSwapChain_Release(swapChain);
+    }
+
+    if (!context)
+    {
+        Assert(!swapchains);
+        context = context_create(swapchain, target, ds_format_desc);
+    }
+    else
+    {
+        Assert(swapChain);
+        context_validate(context, swapChain);
+        context_setup_target(device, context, target);
+        context_enter(context);
+        Assert(context->valid);
+        IWineD3DSwapChain_Release(swapChain);
+    }
+
+    return context;
+}
+#endif
 
 /*****************************************************************************
@@ -1807,6 +1922,9 @@
             return ((IWineD3DSwapChainImpl *) swapchain)->context[i];
         }
-
-    }
+    }
+
+#ifdef VBOXWDDM
+    Assert(0);
+#endif
 
     /* Create a new context for the thread */
@@ -1839,5 +1957,10 @@
         if (current_context
                 && current_context->current_rt
-                && current_context->swapchain->device == This)
+#ifdef VBOXWDDM
+                && current_context->device == This
+#else
+                && current_context->swapchain->device == This
+#endif
+                )
         {
             target = current_context->current_rt;
@@ -1853,5 +1976,9 @@
     if (current_context && current_context->current_rt == target)
     {
-        context_validate(current_context);
+        context_validate(current_context
+#ifdef VBOXWDDM
+                , NULL// current_context->device->swapchains[0]
+#endif
+                                                    );
         return current_context;
     }
@@ -1862,4 +1989,7 @@
         context = findThreadContextForSwapChain(swapchain, tid);
         IWineD3DSwapChain_Release(swapchain);
+#ifdef VBOXWDDM
+        context_validate(context, swapchain);
+#endif
     }
     else
@@ -1868,5 +1998,11 @@
 
         /* Stay with the currently active context. */
-        if (current_context && current_context->swapchain->device == This)
+        if (current_context
+#ifdef VBOXWDDM
+                && current_context->device == This
+#else
+                && current_context->swapchain->device == This
+#endif
+                )
         {
             context = current_context;
@@ -1882,7 +2018,14 @@
             context = findThreadContextForSwapChain(This->swapchains[0], tid);
         }
-    }
-
+#ifdef VBOXWDDM
+        context_validate(context,
+                NULL//This->swapchains[0]
+                                 );
+#endif
+    }
+
+#ifndef VBOXWDDM
     context_validate(context);
+#endif
 
     return context;
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/device.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/device.c	(revision 32460)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/device.c	(revision 32461)
@@ -6857,4 +6857,18 @@
 }
 
+static HRESULT WINAPI IWineD3DDeviceImpl_AddSwapChain(IWineD3DDevice *iface, IWineD3DSwapChain *swapchain)
+{
+    IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
+    VOID *pvNewBuf = HeapReAlloc(GetProcessHeap(), 0, This->swapchains, (This->NumberOfSwapChains + 1) * sizeof(IWineD3DSwapChain *));
+    if(!pvNewBuf) {
+        ERR("Out of memory!\n");
+        return E_OUTOFMEMORY;
+    }
+    IUnknown_AddRef(swapchain);
+    This->swapchains = (IWineD3DSwapChain *)pvNewBuf;
+    This->swapchains[This->NumberOfSwapChains] = swapchain;
+    ++This->NumberOfSwapChains;
+    return WINED3D_OK;
+}
 #endif
 
@@ -7014,4 +7028,5 @@
     /* VBox WDDM extensions */
     IWineD3DDeviceImpl_Flush,
+    IWineD3DDeviceImpl_AddSwapChain,
 #endif
 };
@@ -7121,5 +7136,9 @@
 void get_drawable_size_backbuffer(struct wined3d_context *context, UINT *width, UINT *height)
 {
+#ifdef VBOXWDDM
+    IWineD3DSwapChainImpl *swapchain = context->currentSwapchain;
+#else
     IWineD3DSwapChainImpl *swapchain = context->swapchain;
+#endif
     /* The drawable size of a backbuffer / aux buffer offscreen target is the size of the
      * current context's drawable, which is the size of the back buffer of the swapchain
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/glsl_shader.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/glsl_shader.c	(revision 32460)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/glsl_shader.c	(revision 32461)
@@ -773,5 +773,9 @@
 {
     const struct wined3d_gl_info *gl_info = context->gl_info;
+#ifdef VBOXWDDM
+    IWineD3DDeviceImpl *device = context->device;
+#else
     IWineD3DDeviceImpl *device = context->swapchain->device;
+#endif
     IWineD3DStateBlockImpl* stateBlock = device->stateBlock;
     struct shader_glsl_priv *priv = device->shader_priv;
@@ -4616,5 +4620,9 @@
 {
     const struct wined3d_gl_info *gl_info = context->gl_info;
+#ifdef VBOXWDDM
+    IWineD3DDeviceImpl *device = context->device;
+#else
     IWineD3DDeviceImpl *device = context->swapchain->device;
+#endif
     struct shader_glsl_priv *priv = device->shader_priv;
     GLhandleARB program_id = 0;
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain.c	(revision 32460)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain.c	(revision 32461)
@@ -842,6 +842,12 @@
         }
         swapchain->ds_format = getFormatDescEntry(WINED3DFMT_D24_UNORM_S8_UINT, gl_info);
+
+#ifdef VBOXWDDM
+        swapchain->context[0] = context_find_create(device, swapchain, (IWineD3DSurfaceImpl *)swapchain->frontBuffer,
+                swapchain->ds_format);
+#else
         swapchain->context[0] = context_create(swapchain, (IWineD3DSurfaceImpl *)swapchain->frontBuffer,
                 swapchain->ds_format);
+#endif
         if (!swapchain->context[0])
         {
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h	(revision 32460)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h	(revision 32461)
@@ -1074,5 +1074,11 @@
     DWORD isStateDirty[STATE_HIGHEST / (sizeof(DWORD) * CHAR_BIT) + 1]; /* Bitmap to find out quickly if a state is dirty */
 
+#ifdef VBOXWDDM
+    IWineD3DDeviceImpl    *device;
+    IWineD3DSwapChainImpl *currentSwapchain;
+#else
     IWineD3DSwapChainImpl *swapchain;
+#endif
+
     IWineD3DSurface *current_rt;
     DWORD                   tid;    /* Thread ID which owns this context at the moment */
@@ -1240,4 +1246,8 @@
 struct wined3d_context *context_create(IWineD3DSwapChainImpl *swapchain, IWineD3DSurfaceImpl *target,
         const struct wined3d_format_desc *ds_format_desc) DECLSPEC_HIDDEN;
+#ifdef VBOXWDDM
+struct wined3d_context *context_find_create(IWineD3DDeviceImpl *device, IWineD3DSwapChainImpl *swapchain, IWineD3DSurfaceImpl *target,
+        const struct wined3d_format_desc *ds_format_desc) DECLSPEC_HIDDEN;
+#endif
 void context_destroy(IWineD3DDeviceImpl *This, struct wined3d_context *context) DECLSPEC_HIDDEN;
 void context_free_event_query(struct wined3d_event_query *query) DECLSPEC_HIDDEN;
