Sync with trunk head (r49139)
[reactos.git] / dll / directx / wine / ddraw / device.c
index 9a4e5c7..9f0f631 100644 (file)
 #include "config.h"
 #include "wine/port.h"
 
-#include <assert.h>
-#include <stdarg.h>
-#include <string.h>
-#include <stdlib.h>
-
-#define COBJMACROS
-#define NONAMELESSUNION
-
-#include "windef.h"
-#include "winbase.h"
-#include "winerror.h"
-#include "wingdi.h"
-#include "wine/exception.h"
-
-#include "ddraw.h"
-#include "d3d.h"
-
 #include "ddraw_private.h"
-#include "wine/debug.h"
 
-WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
-WINE_DECLARE_DEBUG_CHANNEL(ddraw_thunk);
+WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
 
 /* The device ID */
 const GUID IID_D3DDEVICE_WineD3D = {
@@ -121,7 +102,8 @@ IDirect3DDeviceImpl_7_QueryInterface(IDirect3DDevice7 *iface,
                                      void **obj)
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
-    TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(refiid), obj);
+
+    TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
 
     /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
     *obj = NULL;
@@ -214,9 +196,9 @@ Thunk_IDirect3DDeviceImpl_3_QueryInterface(IDirect3DDevice3 *iface,
                                            REFIID riid,
                                            void **obj)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DDevice7 interface.\n", This, debugstr_guid(riid), obj);
-    return IDirect3DDevice7_QueryInterface((IDirect3DDevice7 *)This, riid, obj);
+    TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
+
+    return IDirect3DDevice7_QueryInterface((IDirect3DDevice7 *)device_from_device3(iface), riid, obj);
 }
 
 static HRESULT WINAPI
@@ -224,9 +206,9 @@ Thunk_IDirect3DDeviceImpl_2_QueryInterface(IDirect3DDevice2 *iface,
                                            REFIID riid,
                                            void **obj)
 {
-    IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DDevice7 interface.\n", This, debugstr_guid(riid), obj);
-    return IDirect3DDevice7_QueryInterface((IDirect3DDevice7 *)This, riid, obj);
+    TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
+
+    return IDirect3DDevice7_QueryInterface((IDirect3DDevice7 *)device_from_device2(iface), riid, obj);
 }
 
 static HRESULT WINAPI
@@ -234,9 +216,9 @@ Thunk_IDirect3DDeviceImpl_1_QueryInterface(IDirect3DDevice *iface,
                                            REFIID riid,
                                            void **obp)
 {
-    IDirect3DDeviceImpl *This = device_from_device1(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DDevice7 interface.\n", This, debugstr_guid(riid), obp);
-    return IDirect3DDevice7_QueryInterface((IDirect3DDevice7 *)This, riid, obp);
+    TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obp);
+
+    return IDirect3DDevice7_QueryInterface((IDirect3DDevice7 *)device_from_device1(iface), riid, obp);
 }
 
 /*****************************************************************************
@@ -257,7 +239,7 @@ IDirect3DDeviceImpl_7_AddRef(IDirect3DDevice7 *iface)
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     ULONG ref = InterlockedIncrement(&This->ref);
 
-    TRACE("(%p) : incrementing from %u.\n", This, ref -1);
+    TRACE("%p increasing refcount to %u.\n", This, ref);
 
     return ref;
 }
@@ -265,23 +247,24 @@ IDirect3DDeviceImpl_7_AddRef(IDirect3DDevice7 *iface)
 static ULONG WINAPI
 Thunk_IDirect3DDeviceImpl_3_AddRef(IDirect3DDevice3 *iface)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
-    return IDirect3DDevice7_AddRef((IDirect3DDevice7 *)This);
+    TRACE("iface %p.\n", iface);
+
+    return IDirect3DDevice7_AddRef((IDirect3DDevice7 *)device_from_device3(iface));
 }
 
 static ULONG WINAPI
 Thunk_IDirect3DDeviceImpl_2_AddRef(IDirect3DDevice2 *iface)
 {
-    IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
-    return IDirect3DDevice7_AddRef((IDirect3DDevice7 *)This);
+    TRACE("iface %p.\n", iface);
+
+    return IDirect3DDevice7_AddRef((IDirect3DDevice7 *)device_from_device2(iface));
 }
 
 static ULONG WINAPI
 Thunk_IDirect3DDeviceImpl_1_AddRef(IDirect3DDevice *iface)
 {
-    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", iface);
+    TRACE("iface %p.\n", iface);
+
     return IDirect3DDevice7_AddRef((IDirect3DDevice7 *)device_from_device1(iface));
 }
 
@@ -303,7 +286,7 @@ IDirect3DDeviceImpl_7_Release(IDirect3DDevice7 *iface)
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     ULONG ref = InterlockedDecrement(&This->ref);
 
-    TRACE("(%p)->() decrementing from %u.\n", This, ref +1);
+    TRACE("%p decreasing refcount to %u.\n", This, ref);
 
     /* This method doesn't destroy the WineD3DDevice, because it's still in use for
      * 2D rendering. IDirectDrawSurface7::Release will destroy the WineD3DDevice
@@ -317,10 +300,8 @@ IDirect3DDeviceImpl_7_Release(IDirect3DDevice7 *iface)
         EnterCriticalSection(&ddraw_cs);
         /* Free the index buffer. */
         IWineD3DDevice_SetIndexBuffer(This->wineD3DDevice, NULL, WINED3DFMT_UNKNOWN);
-        IWineD3DBuffer_GetParent(This->indexbuffer,
-                                 (IUnknown **) &IndexBufferParent);
-        IParent_Release(IndexBufferParent); /* Once for the getParent */
-        if( IParent_Release(IndexBufferParent) != 0)  /* And now to destroy it */
+        IndexBufferParent = IWineD3DBuffer_GetParent(This->indexbuffer);
+        if (IParent_Release(IndexBufferParent))
         {
             ERR(" (%p) Something is still holding the index buffer parent %p\n", This, IndexBufferParent);
         }
@@ -330,34 +311,10 @@ IDirect3DDeviceImpl_7_Release(IDirect3DDevice7 *iface)
          * IDirect3DVertexBuffer::Release will unset it.
          */
 
-        /* Restore the render targets */
-        if(This->OffScreenTarget)
-        {
-            WINED3DVIEWPORT vp;
-
-            vp.X = 0;
-            vp.Y = 0;
-            vp.Width = This->ddraw->d3d_target->surface_desc.dwWidth;
-            vp.Height = This->ddraw->d3d_target->surface_desc.dwHeight;
-            vp.MinZ = 0.0;
-            vp.MaxZ = 1.0;
-            IWineD3DDevice_SetViewport(This->wineD3DDevice,
-                                       &vp);
-
-            /* Set the device up to render to the front buffer since the back buffer will
-             * vanish soon.
-             */
-            IWineD3DDevice_SetRenderTarget(This->wineD3DDevice, 0,
-                                           This->ddraw->d3d_target->WineD3DSurface,
-                                           FALSE);
-            /* This->target is the offscreen target.
-             * This->ddraw->d3d_target is the target used by DDraw
-             */
-            TRACE("(%p) Release: Using %p as front buffer, %p as back buffer\n", This, This->ddraw->d3d_target, NULL);
-            IWineD3DDevice_SetFrontBackBuffers(This->wineD3DDevice,
-                                               This->ddraw->d3d_target->WineD3DSurface,
-                                               NULL);
-        }
+        /* Set the device up to render to the front buffer since the back
+         * buffer will vanish soon. */
+        IWineD3DDevice_SetRenderTarget(This->wineD3DDevice, 0,
+                This->ddraw->d3d_target->WineD3DSurface, TRUE);
 
         /* Release the WineD3DDevice. This won't destroy it */
         if(IWineD3DDevice_Release(This->wineD3DDevice) <= 0)
@@ -366,53 +323,55 @@ IDirect3DDeviceImpl_7_Release(IDirect3DDevice7 *iface)
         }
 
         /* The texture handles should be unset by now, but there might be some bits
-         * missing in our reference counting(needs test). Do a sanity check
-         */
-        for(i = 0; i < This->numHandles; i++)
+         * missing in our reference counting(needs test). Do a sanity check. */
+        for (i = 0; i < This->handle_table.entry_count; ++i)
         {
-            if(This->Handles[i].ptr)
+            struct ddraw_handle_entry *entry = &This->handle_table.entries[i];
+
+            switch (entry->type)
             {
-                switch(This->Handles[i].type)
-                {
-                    case DDrawHandle_Texture:
-                    {
-                        IDirectDrawSurfaceImpl *surf = This->Handles[i].ptr;
-                        FIXME("Texture Handle %d not unset properly\n", i + 1);
-                        surf->Handle = 0;
-                    }
+                case DDRAW_HANDLE_FREE:
                     break;
 
-                    case DDrawHandle_Material:
-                    {
-                        IDirect3DMaterialImpl *mat = This->Handles[i].ptr;
-                        FIXME("Material handle %d not unset properly\n", i + 1);
-                        mat->Handle = 0;
-                    }
+                case DDRAW_HANDLE_MATERIAL:
+                {
+                    IDirect3DMaterialImpl *m = entry->object;
+                    FIXME("Material handle %#x (%p) not unset properly.\n", i + 1, m);
+                    m->Handle = 0;
                     break;
+                }
 
-                    case DDrawHandle_Matrix:
-                    {
-                        /* No fixme here because this might happen because of sloppy apps */
-                        WARN("Leftover matrix handle %d, deleting\n", i + 1);
-                        IDirect3DDevice_DeleteMatrix((IDirect3DDevice *)&This->IDirect3DDevice_vtbl, i + 1);
-                    }
+                case DDRAW_HANDLE_MATRIX:
+                {
+                    /* No FIXME here because this might happen because of sloppy applications. */
+                    WARN("Leftover matrix handle %#x (%p), deleting.\n", i + 1, entry->object);
+                    IDirect3DDevice_DeleteMatrix((IDirect3DDevice *)&This->IDirect3DDevice_vtbl, i + 1);
                     break;
+                }
 
-                    case DDrawHandle_StateBlock:
-                    {
-                        /* No fixme here because this might happen because of sloppy apps */
-                        WARN("Leftover stateblock handle %d, deleting\n", i + 1);
-                        IDirect3DDevice7_DeleteStateBlock((IDirect3DDevice7 *)This, i + 1);
-                    }
+                case DDRAW_HANDLE_STATEBLOCK:
+                {
+                    /* No FIXME here because this might happen because of sloppy applications. */
+                    WARN("Leftover stateblock handle %#x (%p), deleting.\n", i + 1, entry->object);
+                    IDirect3DDevice7_DeleteStateBlock(iface, i + 1);
                     break;
+                }
 
-                    default:
-                        FIXME("Unknown handle %d not unset properly\n", i + 1);
+                case DDRAW_HANDLE_SURFACE:
+                {
+                    IDirectDrawSurfaceImpl *surf = entry->object;
+                    FIXME("Texture handle %#x (%p) not unset properly.\n", i + 1, surf);
+                    surf->Handle = 0;
+                    break;
                 }
+
+                default:
+                    FIXME("Handle %#x (%p) has unknown type %#x.\n", i + 1, entry->object, entry->type);
+                    break;
             }
         }
 
-        HeapFree(GetProcessHeap(), 0, This->Handles);
+        ddraw_handle_table_destroy(&This->handle_table);
 
         TRACE("Releasing target %p %p\n", This->target, This->ddraw->d3d_target);
         /* Release the render target and the WineD3D render target
@@ -436,25 +395,25 @@ IDirect3DDeviceImpl_7_Release(IDirect3DDevice7 *iface)
 static ULONG WINAPI
 Thunk_IDirect3DDeviceImpl_3_Release(IDirect3DDevice3 *iface)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
-    return IDirect3DDevice7_Release((IDirect3DDevice7 *)This);
+    TRACE("iface %p.\n", iface);
+
+    return IDirect3DDevice7_Release((IDirect3DDevice7 *)device_from_device3(iface));
 }
 
 static ULONG WINAPI
 Thunk_IDirect3DDeviceImpl_2_Release(IDirect3DDevice2 *iface)
 {
-    IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
-    return IDirect3DDevice7_Release((IDirect3DDevice7 *)This);
+    TRACE("iface %p.\n", iface);
+
+    return IDirect3DDevice7_Release((IDirect3DDevice7 *)device_from_device2(iface));
 }
 
 static ULONG WINAPI
 Thunk_IDirect3DDeviceImpl_1_Release(IDirect3DDevice *iface)
 {
-    IDirect3DDeviceImpl *This = device_from_device1(iface);
-    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
-    return IDirect3DDevice7_Release((IDirect3DDevice7 *)This);
+    TRACE("iface %p.\n", iface);
+
+    return IDirect3DDevice7_Release((IDirect3DDevice7 *)device_from_device1(iface));
 }
 
 /*****************************************************************************
@@ -480,12 +439,10 @@ IDirect3DDeviceImpl_1_Initialize(IDirect3DDevice *iface,
                                  IDirect3D *Direct3D, GUID *guid,
                                  D3DDEVICEDESC *Desc)
 {
-    IDirect3DDeviceImpl *This = device_from_device1(iface);
-
     /* It shouldn't be crucial, but print a FIXME, I'm interested if
-     * any game calls it and when
-     */
-    FIXME("(%p)->(%p,%p,%p): No-op!\n", This, Direct3D, guid, Desc);
+     * any game calls it and when. */
+    FIXME("iface %p, d3d %p, guid %s, device_desc %p nop!\n",
+            iface, Direct3D, debugstr_guid(guid), Desc);
 
     return D3D_OK;
 }
@@ -512,7 +469,8 @@ IDirect3DDeviceImpl_7_GetCaps(IDirect3DDevice7 *iface,
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     D3DDEVICEDESC OldDesc;
-    TRACE("(%p)->(%p)\n", This, Desc);
+
+    TRACE("iface %p, device_desc %p.\n", iface, Desc);
 
     /* Call the same function used by IDirect3D, this saves code */
     return IDirect3DImpl_GetCaps(This->ddraw->wineD3D, &OldDesc, Desc);
@@ -563,7 +521,8 @@ IDirect3DDeviceImpl_3_GetCaps(IDirect3DDevice3 *iface,
     IDirect3DDeviceImpl *This = device_from_device3(iface);
     D3DDEVICEDESC7 newDesc;
     HRESULT hr;
-    TRACE("(%p)->(%p,%p)\n", iface, HWDesc, HelDesc);
+
+    TRACE("iface %p, hw_desc %p, hel_desc %p.\n", iface, HWDesc, HelDesc);
 
     hr = IDirect3DImpl_GetCaps(This->ddraw->wineD3D, HWDesc, &newDesc);
     if(hr != D3D_OK) return hr;
@@ -578,7 +537,7 @@ Thunk_IDirect3DDeviceImpl_2_GetCaps(IDirect3DDevice2 *iface,
                                     D3DDEVICEDESC *D3DHELDevDesc)
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DDevice3 interface.\n", This, D3DHWDevDesc, D3DHELDevDesc);
+    TRACE("iface %p, hw_desc %p, hel_desc %p.\n", iface, D3DHWDevDesc, D3DHELDevDesc);
     return IDirect3DDevice3_GetCaps((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl, D3DHWDevDesc, D3DHELDevDesc);
 }
 
@@ -588,7 +547,7 @@ Thunk_IDirect3DDeviceImpl_1_GetCaps(IDirect3DDevice *iface,
                                     D3DDEVICEDESC *D3DHELDevDesc)
 {
     IDirect3DDeviceImpl *This = device_from_device1(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DDevice3 interface.\n", This, D3DHWDevDesc, D3DHELDevDesc);
+    TRACE("iface %p, hw_desc %p, hel_desc %p.\n", iface, D3DHWDevDesc, D3DHELDevDesc);
     return IDirect3DDevice3_GetCaps((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl, D3DHWDevDesc, D3DHELDevDesc);
 }
 
@@ -610,18 +569,21 @@ IDirect3DDeviceImpl_2_SwapTextureHandles(IDirect3DDevice2 *iface,
                                          IDirect3DTexture2 *Tex2)
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
-    DWORD swap;
     IDirectDrawSurfaceImpl *surf1 = surface_from_texture2(Tex1);
     IDirectDrawSurfaceImpl *surf2 = surface_from_texture2(Tex2);
-    TRACE("(%p)->(%p,%p)\n", This, surf1, surf2);
+    DWORD h1, h2;
+
+    TRACE("iface %p, tex1 %p, tex2 %p.\n", iface, Tex1, Tex2);
 
     EnterCriticalSection(&ddraw_cs);
-    This->Handles[surf1->Handle - 1].ptr = surf2;
-    This->Handles[surf2->Handle - 1].ptr = surf1;
 
-    swap = surf2->Handle;
-    surf2->Handle = surf1->Handle;
-    surf1->Handle = swap;
+    h1 = surf1->Handle - 1;
+    h2 = surf2->Handle - 1;
+    This->handle_table.entries[h1].object = surf2;
+    This->handle_table.entries[h2].object = surf1;
+    surf2->Handle = h1 + 1;
+    surf1->Handle = h2 + 1;
+
     LeaveCriticalSection(&ddraw_cs);
 
     return D3D_OK;
@@ -637,7 +599,9 @@ Thunk_IDirect3DDeviceImpl_1_SwapTextureHandles(IDirect3DDevice *iface,
     IDirectDrawSurfaceImpl *surf2 = surface_from_texture1(D3DTex2);
     IDirect3DTexture2 *t1 = surf1 ? (IDirect3DTexture2 *)&surf1->IDirect3DTexture2_vtbl : NULL;
     IDirect3DTexture2 *t2 = surf2 ? (IDirect3DTexture2 *)&surf2->IDirect3DTexture2_vtbl : NULL;
-    TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DDevice2 interface.\n", This, surf1, surf2);
+
+    TRACE("iface %p, tex1 %p, tex2 %p.\n", iface, D3DTex1, D3DTex2);
+
     return IDirect3DDevice2_SwapTextureHandles((IDirect3DDevice2 *)&This->IDirect3DDevice2_vtbl, t1, t2);
 }
 
@@ -663,8 +627,7 @@ static HRESULT WINAPI
 IDirect3DDeviceImpl_3_GetStats(IDirect3DDevice3 *iface,
                                D3DSTATS *Stats)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
-    FIXME("(%p)->(%p): Stub!\n", This, Stats);
+    FIXME("iface %p, stats %p stub!\n", iface, Stats);
 
     if(!Stats)
         return DDERR_INVALIDPARAMS;
@@ -684,7 +647,9 @@ Thunk_IDirect3DDeviceImpl_2_GetStats(IDirect3DDevice2 *iface,
                                      D3DSTATS *Stats)
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, Stats);
+
+    TRACE("iface %p, stats %p.\n", iface, Stats);
+
     return IDirect3DDevice3_GetStats((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl, Stats);
 }
 
@@ -693,7 +658,9 @@ Thunk_IDirect3DDeviceImpl_1_GetStats(IDirect3DDevice *iface,
                                      D3DSTATS *Stats)
 {
     IDirect3DDeviceImpl *This = device_from_device1(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, Stats);
+
+    TRACE("iface %p, stats %p.\n", iface, Stats);
+
     return IDirect3DDevice3_GetStats((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl, Stats);
 }
 
@@ -725,7 +692,10 @@ IDirect3DDeviceImpl_1_CreateExecuteBuffer(IDirect3DDevice *iface,
 {
     IDirect3DDeviceImpl *This = device_from_device1(iface);
     IDirect3DExecuteBufferImpl* object;
-    TRACE("(%p)->(%p,%p,%p)!\n", This, Desc, ExecuteBuffer, UnkOuter);
+    HRESULT hr;
+
+    TRACE("iface %p, buffer_desc %p, buffer %p, outer_unknown %p.\n",
+            iface, Desc, ExecuteBuffer, UnkOuter);
 
     if(UnkOuter)
         return CLASS_E_NOAGGREGATION;
@@ -738,46 +708,14 @@ IDirect3DDeviceImpl_1_CreateExecuteBuffer(IDirect3DDevice *iface,
         return DDERR_OUTOFMEMORY;
     }
 
-    object->lpVtbl = &IDirect3DExecuteBuffer_Vtbl;
-    object->ref = 1;
-    object->d3ddev = This;
-
-    /* Initializes memory */
-    memcpy(&object->desc, Desc, Desc->dwSize);
-
-    /* No buffer given */
-    if ((object->desc.dwFlags & D3DDEB_LPDATA) == 0)
-        object->desc.lpData = NULL;
-
-    /* No buffer size given */
-    if ((object->desc.dwFlags & D3DDEB_BUFSIZE) == 0)
-        object->desc.dwBufferSize = 0;
-
-    /* Create buffer if asked */
-    if ((object->desc.lpData == NULL) && (object->desc.dwBufferSize > 0))
+    hr = d3d_execute_buffer_init(object, This, Desc);
+    if (FAILED(hr))
     {
-        object->need_free = TRUE;
-        object->desc.lpData = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,object->desc.dwBufferSize);
-        if(!object->desc.lpData)
-        {
-            ERR("Out of memory when allocating the execute buffer data\n");
-            HeapFree(GetProcessHeap(), 0, object);
-            return DDERR_OUTOFMEMORY;
-        }
-    }
-    else
-    {
-        object->need_free = FALSE;
+        WARN("Failed to initialize execute buffer, hr %#x.\n", hr);
+        HeapFree(GetProcessHeap(), 0, object);
+        return hr;
     }
 
-    /* No vertices for the moment */
-    object->vertex_data = NULL;
-
-    object->desc.dwFlags |= D3DDEB_LPDATA;
-
-    object->indices = NULL;
-    object->nb_indices = 0;
-
     *ExecuteBuffer = (IDirect3DExecuteBuffer *)object;
 
     TRACE(" Returning IDirect3DExecuteBuffer at %p, implementation is at %p\n", *ExecuteBuffer, object);
@@ -810,7 +748,7 @@ IDirect3DDeviceImpl_1_Execute(IDirect3DDevice *iface,
     IDirect3DExecuteBufferImpl *Direct3DExecuteBufferImpl = (IDirect3DExecuteBufferImpl *)ExecuteBuffer;
     IDirect3DViewportImpl *Direct3DViewportImpl = (IDirect3DViewportImpl *)Viewport;
 
-    TRACE("(%p)->(%p,%p,%08x)\n", This, Direct3DExecuteBufferImpl, Direct3DViewportImpl, Flags);
+    TRACE("iface %p, buffer %p, viewport %p, flags %#x.\n", iface, ExecuteBuffer, Viewport, Flags);
 
     if(!Direct3DExecuteBufferImpl)
         return DDERR_INVALIDPARAMS;
@@ -847,7 +785,7 @@ IDirect3DDeviceImpl_3_AddViewport(IDirect3DDevice3 *iface,
     IDirect3DDeviceImpl *This = device_from_device3(iface);
     IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *)Viewport;
 
-    TRACE("(%p)->(%p)\n", This, vp);
+    TRACE("iface %p, viewport %p.\n", iface, Viewport);
 
     /* Sanity check */
     if(!vp)
@@ -869,7 +807,9 @@ Thunk_IDirect3DDeviceImpl_2_AddViewport(IDirect3DDevice2 *iface,
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
     IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *)Direct3DViewport2;
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, vp);
+
+    TRACE("iface %p, viewport %p.\n", iface, Direct3DViewport2);
+
     return IDirect3DDevice3_AddViewport((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl, (IDirect3DViewport3 *)vp);
 }
 
@@ -879,7 +819,9 @@ Thunk_IDirect3DDeviceImpl_1_AddViewport(IDirect3DDevice *iface,
 {
     IDirect3DDeviceImpl *This = device_from_device1(iface);
     IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *)Direct3DViewport;
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, vp);
+
+    TRACE("iface %p, viewport %p.\n", iface, Direct3DViewport);
+
     return IDirect3DDevice3_AddViewport((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl, (IDirect3DViewport3 *)vp);
 }
 
@@ -907,7 +849,7 @@ IDirect3DDeviceImpl_3_DeleteViewport(IDirect3DDevice3 *iface,
     IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *) Viewport;
     IDirect3DViewportImpl *cur_viewport, *prev_viewport = NULL;
 
-    TRACE("(%p)->(%p)\n", This, vp);
+    TRACE("iface %p, viewport %p.\n", iface, Viewport);
 
     EnterCriticalSection(&ddraw_cs);
     cur_viewport = This->viewport_list;
@@ -935,7 +877,9 @@ Thunk_IDirect3DDeviceImpl_2_DeleteViewport(IDirect3DDevice2 *iface,
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
     IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *)Direct3DViewport2;
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, vp);
+
+    TRACE("iface %p, viewport %p.\n", iface, Direct3DViewport2);
+
     return IDirect3DDevice3_DeleteViewport((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl, (IDirect3DViewport3 *)vp);
 }
 
@@ -945,7 +889,9 @@ Thunk_IDirect3DDeviceImpl_1_DeleteViewport(IDirect3DDevice *iface,
 {
     IDirect3DDeviceImpl *This = device_from_device1(iface);
     IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *)Direct3DViewport;
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, vp);
+
+    TRACE("iface %p, viewport %p.\n", iface, Direct3DViewport);
+
     return IDirect3DDevice3_DeleteViewport((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl, (IDirect3DViewport3 *)vp);
 }
 
@@ -977,7 +923,8 @@ IDirect3DDeviceImpl_3_NextViewport(IDirect3DDevice3 *iface,
     IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *)Viewport3;
     IDirect3DViewportImpl *res = NULL;
 
-    TRACE("(%p)->(%p,%p,%08x)\n", This, vp, lplpDirect3DViewport3, Flags);
+    TRACE("iface %p, viewport %p, next %p, flags %#x.\n",
+            iface, Viewport3, lplpDirect3DViewport3, Flags);
 
     if(!vp)
     {
@@ -1030,7 +977,10 @@ Thunk_IDirect3DDeviceImpl_2_NextViewport(IDirect3DDevice2 *iface,
     IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *)Viewport2;
     IDirect3DViewport3 *res;
     HRESULT hr;
-    TRACE_(ddraw_thunk)("(%p)->(%p,%p,%08x) thunking to IDirect3DDevice3 interface.\n", This, vp, lplpDirect3DViewport2, Flags);
+
+    TRACE("iface %p, viewport %p, next %p, flags %#x.\n",
+            iface, Viewport2, lplpDirect3DViewport2, Flags);
+
     hr = IDirect3DDevice3_NextViewport((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl,
             (IDirect3DViewport3 *)vp, &res, Flags);
     *lplpDirect3DViewport2 = (IDirect3DViewport2 *)res;
@@ -1047,7 +997,10 @@ Thunk_IDirect3DDeviceImpl_1_NextViewport(IDirect3DDevice *iface,
     IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *)Viewport;
     IDirect3DViewport3 *res;
     HRESULT hr;
-    TRACE_(ddraw_thunk)("(%p)->(%p,%p,%08x) thunking to IDirect3DDevice3 interface.\n", This, vp, lplpDirect3DViewport, Flags);
+
+    TRACE("iface %p, viewport %p, next %p, flags %#x.\n",
+            iface, Viewport, lplpDirect3DViewport, Flags);
+
     hr = IDirect3DDevice3_NextViewport((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl,
             (IDirect3DViewport3 *)vp, &res, Flags);
     *lplpDirect3DViewport = (IDirect3DViewport *)res;
@@ -1082,10 +1035,8 @@ IDirect3DDeviceImpl_1_Pick(IDirect3DDevice *iface,
                            DWORD Flags,
                            D3DRECT *Rect)
 {
-    IDirect3DDeviceImpl *This = device_from_device1(iface);
-    IDirect3DExecuteBufferImpl *execbuf = (IDirect3DExecuteBufferImpl *)ExecuteBuffer;
-    IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *)Viewport;
-    FIXME("(%p)->(%p,%p,%08x,%p): stub!\n", This, execbuf, vp, Flags, Rect);
+    FIXME("iface %p, buffer %p, viewport %p, flags %#x, rect %s stub!\n",
+            iface, ExecuteBuffer, Viewport, Flags, wine_dbgstr_rect((RECT *)Rect));
 
     return D3D_OK;
 }
@@ -1111,8 +1062,7 @@ IDirect3DDeviceImpl_1_GetPickRecords(IDirect3DDevice *iface,
                                      DWORD *Count,
                                      D3DPICKRECORD *D3DPickRec)
 {
-    IDirect3DDeviceImpl *This = device_from_device1(iface);
-    FIXME("(%p)->(%p,%p): stub!\n", This, Count, D3DPickRec);
+    FIXME("iface %p, count %p, records %p stub!\n", iface, Count, D3DPickRec);
 
     return D3D_OK;
 }
@@ -1146,7 +1096,8 @@ IDirect3DDeviceImpl_7_EnumTextureFormats(IDirect3DDevice7 *iface,
     WINED3DDISPLAYMODE mode;
     unsigned int i;
 
-    WINED3DFORMAT FormatList[] = {
+    static const enum wined3d_format_id FormatList[] =
+    {
         /* 32 bit */
         WINED3DFMT_B8G8R8A8_UNORM,
         WINED3DFMT_B8G8R8X8_UNORM,
@@ -1166,17 +1117,17 @@ IDirect3DDeviceImpl_7_EnumTextureFormats(IDirect3DDevice7 *iface,
         WINED3DFMT_DXT5,
     };
 
-    WINED3DFORMAT BumpFormatList[] = {
+    static const enum wined3d_format_id BumpFormatList[] =
+    {
         WINED3DFMT_R8G8_SNORM,
         WINED3DFMT_R5G5_SNORM_L6_UNORM,
         WINED3DFMT_R8G8_SNORM_L8X8_UNORM,
-        WINED3DFMT_R8G8B8A8_SNORM,
         WINED3DFMT_R16G16_SNORM,
         WINED3DFMT_R10G11B11_SNORM,
         WINED3DFMT_R10G10B10_SNORM_A2_UNORM
     };
 
-    TRACE("(%p)->(%p,%p): Relay\n", This, Callback, Arg);
+    TRACE("iface %p, callback %p, context %p.\n", iface, Callback, Arg);
 
     if(!Callback)
         return DDERR_INVALIDPARAMS;
@@ -1193,7 +1144,7 @@ IDirect3DDeviceImpl_7_EnumTextureFormats(IDirect3DDevice7 *iface,
         return hr;
     }
 
-    for(i = 0; i < sizeof(FormatList) / sizeof(WINED3DFORMAT); i++)
+    for (i = 0; i < sizeof(FormatList) / sizeof(*FormatList); ++i)
     {
         hr = IWineD3D_CheckDeviceFormat(This->ddraw->wineD3D,
                                         WINED3DADAPTER_DEFAULT,
@@ -1222,7 +1173,7 @@ IDirect3DDeviceImpl_7_EnumTextureFormats(IDirect3DDevice7 *iface,
         }
     }
 
-    for(i = 0; i < sizeof(BumpFormatList) / sizeof(WINED3DFORMAT); i++)
+    for (i = 0; i < sizeof(BumpFormatList) / sizeof(*BumpFormatList); ++i)
     {
         hr = IWineD3D_CheckDeviceFormat(This->ddraw->wineD3D,
                                         WINED3DADAPTER_DEFAULT,
@@ -1284,7 +1235,9 @@ Thunk_IDirect3DDeviceImpl_3_EnumTextureFormats(IDirect3DDevice3 *iface,
                                                void *Arg)
 {
     IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DDevice7 interface.\n", This, Callback, Arg);
+
+    TRACE("iface %p, callback %p, context %p.\n", iface, Callback, Arg);
+
     return IDirect3DDevice7_EnumTextureFormats((IDirect3DDevice7 *)This, Callback, Arg);
 }
 
@@ -1308,7 +1261,8 @@ IDirect3DDeviceImpl_2_EnumTextureFormats(IDirect3DDevice2 *iface,
     unsigned int i;
     WINED3DDISPLAYMODE mode;
 
-    WINED3DFORMAT FormatList[] = {
+    static const enum wined3d_format_id FormatList[] =
+    {
         /* 32 bit */
         WINED3DFMT_B8G8R8A8_UNORM,
         WINED3DFMT_B8G8R8X8_UNORM,
@@ -1325,7 +1279,7 @@ IDirect3DDeviceImpl_2_EnumTextureFormats(IDirect3DDevice2 *iface,
         /* FOURCC codes - Not in this version*/
     };
 
-    TRACE("(%p)->(%p,%p): Relay\n", This, Callback, Arg);
+    TRACE("iface %p, callback %p, context %p.\n", iface, Callback, Arg);
 
     if(!Callback)
         return DDERR_INVALIDPARAMS;
@@ -1342,7 +1296,7 @@ IDirect3DDeviceImpl_2_EnumTextureFormats(IDirect3DDevice2 *iface,
         return hr;
     }
 
-    for(i = 0; i < sizeof(FormatList) / sizeof(WINED3DFORMAT); i++)
+    for (i = 0; i < sizeof(FormatList) / sizeof(*FormatList); ++i)
     {
         hr = IWineD3D_CheckDeviceFormat(This->ddraw->wineD3D,
                                         0 /* Adapter */,
@@ -1384,7 +1338,9 @@ Thunk_IDirect3DDeviceImpl_1_EnumTextureFormats(IDirect3DDevice *iface,
                                                void *Arg)
 {
     IDirect3DDeviceImpl *This = device_from_device1(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DDevice2 interface.\n", This, Callback, Arg);
+
+    TRACE("iface %p, callback %p, context %p.\n", iface, Callback, Arg);
+
     return IDirect3DDevice2_EnumTextureFormats((IDirect3DDevice2 *)&This->IDirect3DDevice2_vtbl, Callback, Arg);
 }
 
@@ -1409,7 +1365,9 @@ IDirect3DDeviceImpl_1_CreateMatrix(IDirect3DDevice *iface, D3DMATRIXHANDLE *D3DM
 {
     IDirect3DDeviceImpl *This = device_from_device1(iface);
     D3DMATRIX *Matrix;
-    TRACE("(%p)->(%p)\n", This, D3DMatHandle);
+    DWORD h;
+
+    TRACE("iface %p, matrix_handle %p.\n", iface, D3DMatHandle);
 
     if(!D3DMatHandle)
         return DDERR_INVALIDPARAMS;
@@ -1422,16 +1380,18 @@ IDirect3DDeviceImpl_1_CreateMatrix(IDirect3DDevice *iface, D3DMATRIXHANDLE *D3DM
     }
 
     EnterCriticalSection(&ddraw_cs);
-    *D3DMatHandle = IDirect3DDeviceImpl_CreateHandle(This);
-    if(!(*D3DMatHandle))
+
+    h = ddraw_allocate_handle(&This->handle_table, Matrix, DDRAW_HANDLE_MATRIX);
+    if (h == DDRAW_INVALID_HANDLE)
     {
-        ERR("Failed to create a matrix handle\n");
+        ERR("Failed to allocate a matrix handle.\n");
         HeapFree(GetProcessHeap(), 0, Matrix);
         LeaveCriticalSection(&ddraw_cs);
         return DDERR_OUTOFMEMORY;
     }
-    This->Handles[*D3DMatHandle - 1].ptr = Matrix;
-    This->Handles[*D3DMatHandle - 1].type = DDrawHandle_Matrix;
+
+    *D3DMatHandle = h + 1;
+
     TRACE(" returning matrix handle %d\n", *D3DMatHandle);
 
     LeaveCriticalSection(&ddraw_cs);
@@ -1462,29 +1422,26 @@ IDirect3DDeviceImpl_1_SetMatrix(IDirect3DDevice *iface,
                                 D3DMATRIX *D3DMatrix)
 {
     IDirect3DDeviceImpl *This = device_from_device1(iface);
-    TRACE("(%p)->(%08x,%p)\n", This, D3DMatHandle, D3DMatrix);
+    D3DMATRIX *m;
 
-    if( (!D3DMatHandle) || (!D3DMatrix) )
-        return DDERR_INVALIDPARAMS;
+    TRACE("iface %p, matrix_handle %#x, matrix %p.\n", iface, D3DMatHandle, D3DMatrix);
+
+    if (!D3DMatrix) return DDERR_INVALIDPARAMS;
 
     EnterCriticalSection(&ddraw_cs);
-    if(D3DMatHandle > This->numHandles)
-    {
-        ERR("Handle %d out of range\n", D3DMatHandle);
-        LeaveCriticalSection(&ddraw_cs);
-        return DDERR_INVALIDPARAMS;
-    }
-    else if(This->Handles[D3DMatHandle - 1].type != DDrawHandle_Matrix)
+
+    m = ddraw_get_object(&This->handle_table, D3DMatHandle - 1, DDRAW_HANDLE_MATRIX);
+    if (!m)
     {
-        ERR("Handle %d is not a matrix handle\n", D3DMatHandle);
+        WARN("Invalid matrix handle.\n");
         LeaveCriticalSection(&ddraw_cs);
         return DDERR_INVALIDPARAMS;
     }
 
-    if (TRACE_ON(d3d7))
+    if (TRACE_ON(ddraw))
         dump_D3DMATRIX(D3DMatrix);
 
-    *((D3DMATRIX *) This->Handles[D3DMatHandle - 1].ptr) = *D3DMatrix;
+    *m = *D3DMatrix;
 
     if(This->world == D3DMatHandle)
     {
@@ -1510,7 +1467,7 @@ IDirect3DDeviceImpl_1_SetMatrix(IDirect3DDevice *iface,
 }
 
 /*****************************************************************************
- * IDirect3DDevice::SetMatrix
+ * IDirect3DDevice::GetMatrix
  *
  * Returns the content of a D3DMATRIX handle
  *
@@ -1531,29 +1488,23 @@ IDirect3DDeviceImpl_1_GetMatrix(IDirect3DDevice *iface,
                                 D3DMATRIX *D3DMatrix)
 {
     IDirect3DDeviceImpl *This = device_from_device1(iface);
-    TRACE("(%p)->(%08x,%p)\n", This, D3DMatHandle, D3DMatrix);
+    D3DMATRIX *m;
 
-    if(!D3DMatrix)
-        return DDERR_INVALIDPARAMS;
-    if(!D3DMatHandle)
-        return DDERR_INVALIDPARAMS;
+    TRACE("iface %p, matrix_handle %#x, matrix %p.\n", iface, D3DMatHandle, D3DMatrix);
+
+    if (!D3DMatrix) return DDERR_INVALIDPARAMS;
 
     EnterCriticalSection(&ddraw_cs);
-    if(D3DMatHandle > This->numHandles)
-    {
-        ERR("Handle %d out of range\n", D3DMatHandle);
-        LeaveCriticalSection(&ddraw_cs);
-        return DDERR_INVALIDPARAMS;
-    }
-    else if(This->Handles[D3DMatHandle - 1].type != DDrawHandle_Matrix)
+
+    m = ddraw_get_object(&This->handle_table, D3DMatHandle - 1, DDRAW_HANDLE_MATRIX);
+    if (!m)
     {
-        ERR("Handle %d is not a matrix handle\n", D3DMatHandle);
+        WARN("Invalid matrix handle.\n");
         LeaveCriticalSection(&ddraw_cs);
         return DDERR_INVALIDPARAMS;
     }
 
-    /* The handle is simply a pointer to a D3DMATRIX structure */
-    *D3DMatrix = *((D3DMATRIX *) This->Handles[D3DMatHandle - 1].ptr);
+    *D3DMatrix = *m;
 
     LeaveCriticalSection(&ddraw_cs);
     return D3D_OK;
@@ -1579,30 +1530,24 @@ IDirect3DDeviceImpl_1_DeleteMatrix(IDirect3DDevice *iface,
                                    D3DMATRIXHANDLE D3DMatHandle)
 {
     IDirect3DDeviceImpl *This = device_from_device1(iface);
-    TRACE("(%p)->(%08x)\n", This, D3DMatHandle);
+    D3DMATRIX *m;
 
-    if(!D3DMatHandle)
-        return DDERR_INVALIDPARAMS;
+    TRACE("iface %p, matrix_handle %#x.\n", iface, D3DMatHandle);
 
     EnterCriticalSection(&ddraw_cs);
-    if(D3DMatHandle > This->numHandles)
-    {
-        ERR("Handle %d out of range\n", D3DMatHandle);
-        LeaveCriticalSection(&ddraw_cs);
-        return DDERR_INVALIDPARAMS;
-    }
-    else if(This->Handles[D3DMatHandle - 1].type != DDrawHandle_Matrix)
+
+    m = ddraw_free_handle(&This->handle_table, D3DMatHandle - 1, DDRAW_HANDLE_MATRIX);
+    if (!m)
     {
-        ERR("Handle %d is not a matrix handle\n", D3DMatHandle);
+        WARN("Invalid matrix handle.\n");
         LeaveCriticalSection(&ddraw_cs);
         return DDERR_INVALIDPARAMS;
     }
 
-    HeapFree(GetProcessHeap(), 0, This->Handles[D3DMatHandle - 1].ptr);
-    This->Handles[D3DMatHandle - 1].ptr = NULL;
-    This->Handles[D3DMatHandle - 1].type = DDrawHandle_Unknown;
-
     LeaveCriticalSection(&ddraw_cs);
+
+    HeapFree(GetProcessHeap(), 0, m);
+
     return D3D_OK;
 }
 
@@ -1625,7 +1570,8 @@ IDirect3DDeviceImpl_7_BeginScene(IDirect3DDevice7 *iface)
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
-    TRACE("(%p): Relay\n", This);
+
+    TRACE("iface %p.\n", iface);
 
     EnterCriticalSection(&ddraw_cs);
     hr = IWineD3DDevice_BeginScene(This->wineD3DDevice);
@@ -1656,25 +1602,25 @@ IDirect3DDeviceImpl_7_BeginScene_FPUPreserve(IDirect3DDevice7 *iface)
 static HRESULT WINAPI
 Thunk_IDirect3DDeviceImpl_3_BeginScene(IDirect3DDevice3 *iface)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
-    return IDirect3DDevice7_BeginScene((IDirect3DDevice7 *)This);
+    TRACE("iface %p.\n", iface);
+
+    return IDirect3DDevice7_BeginScene((IDirect3DDevice7 *)device_from_device3(iface));
 }
 
 static HRESULT WINAPI
 Thunk_IDirect3DDeviceImpl_2_BeginScene(IDirect3DDevice2 *iface)
 {
-    IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
-    return IDirect3DDevice7_BeginScene((IDirect3DDevice7 *)This);
+    TRACE("iface %p.\n", iface);
+
+    return IDirect3DDevice7_BeginScene((IDirect3DDevice7 *)device_from_device2(iface));
 }
 
 static HRESULT WINAPI
 Thunk_IDirect3DDeviceImpl_1_BeginScene(IDirect3DDevice *iface)
 {
-    IDirect3DDeviceImpl *This = device_from_device1(iface);
-    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
-    return IDirect3DDevice7_BeginScene((IDirect3DDevice7 *)This);
+    TRACE("iface %p.\n", iface);
+
+    return IDirect3DDevice7_BeginScene((IDirect3DDevice7 *)device_from_device1(iface));
 }
 
 /*****************************************************************************
@@ -1696,7 +1642,8 @@ IDirect3DDeviceImpl_7_EndScene(IDirect3DDevice7 *iface)
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
-    TRACE("(%p): Relay\n", This);
+
+    TRACE("iface %p.\n", iface);
 
     EnterCriticalSection(&ddraw_cs);
     hr = IWineD3DDevice_EndScene(This->wineD3DDevice);
@@ -1727,25 +1674,25 @@ IDirect3DDeviceImpl_7_EndScene_FPUPreserve(IDirect3DDevice7 *iface)
 static HRESULT WINAPI DECLSPEC_HOTPATCH
 Thunk_IDirect3DDeviceImpl_3_EndScene(IDirect3DDevice3 *iface)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
-    return IDirect3DDevice7_EndScene((IDirect3DDevice7 *)This);
+    TRACE("iface %p.\n", iface);
+
+    return IDirect3DDevice7_EndScene((IDirect3DDevice7 *)device_from_device3(iface));
 }
 
 static HRESULT WINAPI DECLSPEC_HOTPATCH
 Thunk_IDirect3DDeviceImpl_2_EndScene(IDirect3DDevice2 *iface)
 {
-    IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
-    return IDirect3DDevice7_EndScene((IDirect3DDevice7 *)This);
+    TRACE("iface %p.\n", iface);
+
+    return IDirect3DDevice7_EndScene((IDirect3DDevice7 *)device_from_device2(iface));
 }
 
 static HRESULT WINAPI DECLSPEC_HOTPATCH
 Thunk_IDirect3DDeviceImpl_1_EndScene(IDirect3DDevice *iface)
 {
-    IDirect3DDeviceImpl *This = device_from_device1(iface);
-    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
-    return IDirect3DDevice7_EndScene((IDirect3DDevice7 *)This);
+    TRACE("iface %p.\n", iface);
+
+    return IDirect3DDevice7_EndScene((IDirect3DDevice7 *)device_from_device1(iface));
 }
 
 /*****************************************************************************
@@ -1767,7 +1714,8 @@ IDirect3DDeviceImpl_7_GetDirect3D(IDirect3DDevice7 *iface,
                                   IDirect3D7 **Direct3D7)
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
-    TRACE("(%p)->(%p)\n", This, Direct3D7);
+
+    TRACE("iface %p, d3d %p.\n", iface, Direct3D7);
 
     if(!Direct3D7)
         return DDERR_INVALIDPARAMS;
@@ -1787,7 +1735,8 @@ Thunk_IDirect3DDeviceImpl_3_GetDirect3D(IDirect3DDevice3 *iface,
     HRESULT ret;
     IDirect3D7 *ret_ptr;
 
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, Direct3D3);
+    TRACE("iface %p, d3d %p.\n", iface, Direct3D3);
+
     ret = IDirect3DDevice7_GetDirect3D((IDirect3DDevice7 *)This, &ret_ptr);
     if(ret != D3D_OK)
         return ret;
@@ -1804,7 +1753,8 @@ Thunk_IDirect3DDeviceImpl_2_GetDirect3D(IDirect3DDevice2 *iface,
     HRESULT ret;
     IDirect3D7 *ret_ptr;
 
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, Direct3D2);
+    TRACE("iface %p, d3d %p.\n", iface, Direct3D2);
+
     ret = IDirect3DDevice7_GetDirect3D((IDirect3DDevice7 *)This, &ret_ptr);
     if(ret != D3D_OK)
         return ret;
@@ -1821,7 +1771,8 @@ Thunk_IDirect3DDeviceImpl_1_GetDirect3D(IDirect3DDevice *iface,
     HRESULT ret;
     IDirect3D7 *ret_ptr;
 
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, Direct3D);
+    TRACE("iface %p, d3d %p.\n", iface, Direct3D);
+
     ret = IDirect3DDevice7_GetDirect3D((IDirect3DDevice7 *)This, &ret_ptr);
     if(ret != D3D_OK)
         return ret;
@@ -1852,7 +1803,8 @@ IDirect3DDeviceImpl_3_SetCurrentViewport(IDirect3DDevice3 *iface,
 {
     IDirect3DDeviceImpl *This = device_from_device3(iface);
     IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *)Direct3DViewport3;
-    TRACE("(%p)->(%p)\n", This, Direct3DViewport3);
+
+    TRACE("iface %p, viewport %p.\n", iface, Direct3DViewport3);
 
     EnterCriticalSection(&ddraw_cs);
     /* Do nothing if the specified viewport is the same as the current one */
@@ -1878,7 +1830,7 @@ IDirect3DDeviceImpl_3_SetCurrentViewport(IDirect3DDevice3 *iface,
 
     /* Activate this viewport */
     This->current_viewport->active_device = This;
-    This->current_viewport->activate(This->current_viewport, FALSE);
+    viewport_activate(This->current_viewport, FALSE);
 
     LeaveCriticalSection(&ddraw_cs);
     return D3D_OK;
@@ -1890,7 +1842,9 @@ Thunk_IDirect3DDeviceImpl_2_SetCurrentViewport(IDirect3DDevice2 *iface,
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
     IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *)Direct3DViewport2;
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, vp);
+
+    TRACE("iface %p, viewport %p.\n", iface, Direct3DViewport2);
+
     return IDirect3DDevice3_SetCurrentViewport((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl,
             (IDirect3DViewport3 *)vp);
 }
@@ -1915,7 +1869,8 @@ IDirect3DDeviceImpl_3_GetCurrentViewport(IDirect3DDevice3 *iface,
                                          IDirect3DViewport3 **Direct3DViewport3)
 {
     IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE("(%p)->(%p)\n", This, Direct3DViewport3);
+
+    TRACE("iface %p, viewport %p.\n", iface, Direct3DViewport3);
 
     if(!Direct3DViewport3)
         return DDERR_INVALIDPARAMS;
@@ -1938,7 +1893,9 @@ Thunk_IDirect3DDeviceImpl_2_GetCurrentViewport(IDirect3DDevice2 *iface,
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
     HRESULT hr;
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, Direct3DViewport2);
+
+    TRACE("iface %p, viewport %p.\n", iface, Direct3DViewport2);
+
     hr = IDirect3DDevice3_GetCurrentViewport((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl,
             (IDirect3DViewport3 **)Direct3DViewport2);
     if(hr != D3D_OK) return hr;
@@ -1971,7 +1928,8 @@ IDirect3DDeviceImpl_7_SetRenderTarget(IDirect3DDevice7 *iface,
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     IDirectDrawSurfaceImpl *Target = (IDirectDrawSurfaceImpl *)NewTarget;
     HRESULT hr;
-    TRACE("(%p)->(%p,%08x): Relay\n", This, NewTarget, Flags);
+
+    TRACE("iface %p, target %p, flags %#x.\n", iface, NewTarget, Flags);
 
     EnterCriticalSection(&ddraw_cs);
     /* Flags: Not used */
@@ -2030,7 +1988,9 @@ Thunk_IDirect3DDeviceImpl_3_SetRenderTarget(IDirect3DDevice3 *iface,
 {
     IDirect3DDeviceImpl *This = device_from_device3(iface);
     IDirectDrawSurfaceImpl *Target = (IDirectDrawSurfaceImpl *)NewRenderTarget;
-    TRACE_(ddraw_thunk)("(%p)->(%p,%08x) thunking to IDirect3DDevice7 interface.\n", This, Target, Flags);
+
+    TRACE("iface %p, target %p, flags %#x.\n", iface, NewRenderTarget, Flags);
+
     return IDirect3DDevice7_SetRenderTarget((IDirect3DDevice7 *)This, (IDirectDrawSurface7 *)Target, Flags);
 }
 
@@ -2041,7 +2001,9 @@ Thunk_IDirect3DDeviceImpl_2_SetRenderTarget(IDirect3DDevice2 *iface,
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
     IDirectDrawSurfaceImpl *Target = (IDirectDrawSurfaceImpl *)NewRenderTarget;
-    TRACE_(ddraw_thunk)("(%p)->(%p,%08x) thunking to IDirect3DDevice7 interface.\n", This, Target, Flags);
+
+    TRACE("iface %p, target %p, flags %#x.\n", iface, NewRenderTarget, Flags);
+
     return IDirect3DDevice7_SetRenderTarget((IDirect3DDevice7 *)This, (IDirectDrawSurface7 *)Target, Flags);
 }
 
@@ -2067,7 +2029,8 @@ IDirect3DDeviceImpl_7_GetRenderTarget(IDirect3DDevice7 *iface,
                                       IDirectDrawSurface7 **RenderTarget)
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
-    TRACE("(%p)->(%p): Relay\n", This, RenderTarget);
+
+    TRACE("iface %p, target %p.\n", iface, RenderTarget);
 
     if(!RenderTarget)
         return DDERR_INVALIDPARAMS;
@@ -2086,7 +2049,9 @@ Thunk_IDirect3DDeviceImpl_3_GetRenderTarget(IDirect3DDevice3 *iface,
 {
     IDirect3DDeviceImpl *This = device_from_device3(iface);
     HRESULT hr;
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, RenderTarget);
+
+    TRACE("iface %p, target %p.\n", iface, RenderTarget);
+
     hr = IDirect3DDevice7_GetRenderTarget((IDirect3DDevice7 *)This, (IDirectDrawSurface7 **)RenderTarget);
     if(hr != D3D_OK) return hr;
     return D3D_OK;
@@ -2098,7 +2063,9 @@ Thunk_IDirect3DDeviceImpl_2_GetRenderTarget(IDirect3DDevice2 *iface,
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
     HRESULT hr;
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, RenderTarget);
+
+    TRACE("iface %p, target %p.\n", iface, RenderTarget);
+
     hr = IDirect3DDevice7_GetRenderTarget((IDirect3DDevice7 *)This, (IDirectDrawSurface7 **)RenderTarget);
     if(hr != D3D_OK) return hr;
     *RenderTarget = *RenderTarget ?
@@ -2131,7 +2098,9 @@ IDirect3DDeviceImpl_3_Begin(IDirect3DDevice3 *iface,
                             DWORD Flags)
 {
     IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE("(%p)->(%d,%d,%08x)\n", This, PrimitiveType, VertexTypeDesc, Flags);
+
+    TRACE("iface %p, primitive_type %#x, FVF %#x, flags %#x.\n",
+            iface, PrimitiveType, VertexTypeDesc, Flags);
 
     EnterCriticalSection(&ddraw_cs);
     This->primitive_type = PrimitiveType;
@@ -2152,7 +2121,9 @@ Thunk_IDirect3DDeviceImpl_2_Begin(IDirect3DDevice2 *iface,
 {
     DWORD FVF;
     IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p/%p)->(%08x,%08x,%08x): Thunking to IDirect3DDevice3\n", This, iface, d3dpt, dwVertexTypeDesc, dwFlags);
+
+    TRACE("iface %p, primitive_type %#x, vertex_type %#x, flags %#x.\n",
+            iface, d3dpt, dwVertexTypeDesc, dwFlags);
 
     switch(dwVertexTypeDesc)
     {
@@ -2194,8 +2165,9 @@ IDirect3DDeviceImpl_3_BeginIndexed(IDirect3DDevice3 *iface,
                                    DWORD NumVertices,
                                    DWORD Flags)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
-    FIXME("(%p)->(%08x,%08x,%p,%08x,%08x): stub!\n", This, PrimitiveType, VertexType, Vertices, NumVertices, Flags);
+    FIXME("iface %p, primitive_type %#x, FVF %#x, vertices %p, vertex_count %u, flags %#x stub!\n",
+            iface, PrimitiveType, VertexType, Vertices, NumVertices, Flags);
+
     return D3D_OK;
 }
 
@@ -2210,7 +2182,9 @@ Thunk_IDirect3DDeviceImpl_2_BeginIndexed(IDirect3DDevice2 *iface,
 {
     DWORD FVF;
     IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p/%p)->(%08x,%08x,%p,%08x,%08x): Thunking to IDirect3DDevice3\n", This, iface, d3dptPrimitiveType, d3dvtVertexType, lpvVertices, dwNumVertices, dwFlags);
+
+    TRACE("iface %p, primitive_type %#x, vertex_type %#x, vertices %p, vertex_count %u, flags %#x stub!\n",
+            iface, d3dptPrimitiveType, d3dvtVertexType, lpvVertices, dwNumVertices, dwFlags);
 
     switch(d3dvtVertexType)
     {
@@ -2248,7 +2222,8 @@ IDirect3DDeviceImpl_3_Vertex(IDirect3DDevice3 *iface,
                              void *Vertex)
 {
     IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE("(%p)->(%p)\n", This, Vertex);
+
+    TRACE("iface %p, vertex %p.\n", iface, Vertex);
 
     if(!Vertex)
         return DDERR_INVALIDPARAMS;
@@ -2278,7 +2253,9 @@ Thunk_IDirect3DDeviceImpl_2_Vertex(IDirect3DDevice2 *iface,
                                    void *lpVertexType)
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, lpVertexType);
+
+    TRACE("iface %p, vertex %p.\n", iface, lpVertexType);
+
     return IDirect3DDevice3_Vertex((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl, lpVertexType);
 }
 
@@ -2299,8 +2276,8 @@ static HRESULT WINAPI
 IDirect3DDeviceImpl_3_Index(IDirect3DDevice3 *iface,
                             WORD VertexIndex)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
-    FIXME("(%p)->(%04x): stub!\n", This, VertexIndex);
+    FIXME("iface %p, index %#x stub!\n", iface, VertexIndex);
+
     return D3D_OK;
 }
 
@@ -2309,7 +2286,9 @@ Thunk_IDirect3DDeviceImpl_2_Index(IDirect3DDevice2 *iface,
                                   WORD wVertexIndex)
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%04x) thunking to IDirect3DDevice3 interface.\n", This, wVertexIndex);
+
+    TRACE("iface %p, index %#x.\n", iface, wVertexIndex);
+
     return IDirect3DDevice3_Index((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl, wVertexIndex);
 }
 
@@ -2336,7 +2315,8 @@ IDirect3DDeviceImpl_3_End(IDirect3DDevice3 *iface,
                           DWORD Flags)
 {
     IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE("(%p)->(%08x)\n", This, Flags);
+
+    TRACE("iface %p, flags %#x.\n", iface, Flags);
 
     return IDirect3DDevice7_DrawPrimitive((IDirect3DDevice7 *)This, This->primitive_type,
             This->vertex_type, This->vertex_buffer, This->nb_vertices, This->render_flags);
@@ -2347,7 +2327,9 @@ Thunk_IDirect3DDeviceImpl_2_End(IDirect3DDevice2 *iface,
                                 DWORD dwFlags)
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%08x) thunking to IDirect3DDevice3 interface.\n", This, dwFlags);
+
+    TRACE("iface %p, flags %#x.\n", iface, dwFlags);
+
     return IDirect3DDevice3_End((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl, dwFlags);
 }
 
@@ -2375,7 +2357,8 @@ IDirect3DDeviceImpl_7_GetRenderState(IDirect3DDevice7 *iface,
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
-    TRACE("(%p)->(%08x,%p): Relay\n", This, RenderStateType, Value);
+
+    TRACE("iface %p, state %#x, value %p.\n", iface, RenderStateType, Value);
 
     if(!Value)
         return DDERR_INVALIDPARAMS;
@@ -2484,6 +2467,12 @@ IDirect3DDeviceImpl_7_GetRenderState(IDirect3DDevice7 *iface,
             hr = E_NOTIMPL;
             break;
 
+        case D3DRENDERSTATE_TEXTUREHANDLE:
+        case D3DRENDERSTATE_TEXTUREMAPBLEND:
+            WARN("Render state %#x is invalid in d3d7.\n", RenderStateType);
+            hr = DDERR_INVALIDPARAMS;
+            break;
+
         default:
             if (RenderStateType >= D3DRENDERSTATE_STIPPLEPATTERN00
                     && RenderStateType <= D3DRENDERSTATE_STIPPLEPATTERN31)
@@ -2531,7 +2520,8 @@ IDirect3DDeviceImpl_3_GetRenderState(IDirect3DDevice3 *iface,
 {
     IDirect3DDeviceImpl *This = device_from_device3(iface);
     HRESULT hr;
-    TRACE("(%p)->(%08x,%p)\n", This, dwRenderStateType, lpdwRenderState);
+
+    TRACE("iface %p, state %#x, value %p.\n", iface, dwRenderStateType, lpdwRenderState);
 
     switch(dwRenderStateType)
     {
@@ -2545,24 +2535,13 @@ IDirect3DDeviceImpl_3_GetRenderState(IDirect3DDevice3 *iface,
 
             EnterCriticalSection(&ddraw_cs);
 
-            hr = IWineD3DDevice_GetTexture(This->wineD3DDevice,
-                                           0,
-                                           &tex);
-
-            if(hr == WINED3D_OK && tex)
+            hr = IWineD3DDevice_GetTexture(This->wineD3DDevice, 0, &tex);
+            if (SUCCEEDED(hr) && tex)
             {
-                IDirectDrawSurface7 *parent = NULL;
-                hr = IWineD3DBaseTexture_GetParent(tex,
-                                                   (IUnknown **) &parent);
-                if(parent)
-                {
-                    /* The parent of the texture is the IDirectDrawSurface7 interface
-                     * of the ddraw surface
-                     */
-                    IDirectDrawSurfaceImpl *texImpl = (IDirectDrawSurfaceImpl *)parent;
-                    *lpdwRenderState = texImpl->Handle;
-                    IDirectDrawSurface7_Release(parent);
-                }
+                /* The parent of the texture is the IDirectDrawSurface7
+                 * interface of the ddraw surface. */
+                IDirectDrawSurfaceImpl *parent = IWineD3DBaseTexture_GetParent(tex);
+                if (parent) *lpdwRenderState = parent->Handle;
                 IWineD3DBaseTexture_Release(tex);
             }
 
@@ -2655,7 +2634,9 @@ Thunk_IDirect3DDeviceImpl_2_GetRenderState(IDirect3DDevice2 *iface,
                                            DWORD *lpdwRenderState)
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice3 interface.\n", This, dwRenderStateType, lpdwRenderState);
+
+    TRACE("iface %p, state %#x, value %p.\n", iface, dwRenderStateType, lpdwRenderState);
+
     return IDirect3DDevice3_GetRenderState((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl,
             dwRenderStateType, lpdwRenderState);
 }
@@ -2684,7 +2665,8 @@ IDirect3DDeviceImpl_7_SetRenderState(IDirect3DDevice7 *iface,
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
-    TRACE("(%p)->(%08x,%d): Relay\n", This, RenderStateType, Value);
+
+    TRACE("iface %p, state %#x, value %#x.\n", iface, RenderStateType, Value);
 
     EnterCriticalSection(&ddraw_cs);
     /* Some render states need special care */
@@ -2801,6 +2783,12 @@ IDirect3DDeviceImpl_7_SetRenderState(IDirect3DDevice7 *iface,
             hr = E_NOTIMPL;
             break;
 
+        case D3DRENDERSTATE_TEXTUREHANDLE:
+        case D3DRENDERSTATE_TEXTUREMAPBLEND:
+            WARN("Render state %#x is invalid in d3d7.\n", RenderStateType);
+            hr = DDERR_INVALIDPARAMS;
+            break;
+
         default:
             if (RenderStateType >= D3DRENDERSTATE_STIPPLEPATTERN00
                     && RenderStateType <= D3DRENDERSTATE_STIPPLEPATTERN31)
@@ -2869,7 +2857,8 @@ IDirect3DDeviceImpl_3_SetRenderState(IDirect3DDevice3 *iface,
 
     HRESULT hr;
     IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE("(%p)->(%08x,%d)\n", This, RenderStateType, Value);
+
+    TRACE("iface %p, state %#x, value %#x.\n", iface, RenderStateType, Value);
 
     EnterCriticalSection(&ddraw_cs);
 
@@ -2877,6 +2866,8 @@ IDirect3DDeviceImpl_3_SetRenderState(IDirect3DDevice3 *iface,
     {
         case D3DRENDERSTATE_TEXTUREHANDLE:
         {
+            IDirectDrawSurfaceImpl *surf;
+
             if(Value == 0)
             {
                 hr = IWineD3DDevice_SetTexture(This->wineD3DDevice,
@@ -2885,25 +2876,16 @@ IDirect3DDeviceImpl_3_SetRenderState(IDirect3DDevice3 *iface,
                 break;
             }
 
-            if(Value > This->numHandles)
+            surf = ddraw_get_object(&This->handle_table, Value - 1, DDRAW_HANDLE_SURFACE);
+            if (!surf)
             {
-                FIXME("Specified handle %d out of range\n", Value);
+                WARN("Invalid texture handle.\n");
                 hr = DDERR_INVALIDPARAMS;
                 break;
             }
-            if(This->Handles[Value - 1].type != DDrawHandle_Texture)
-            {
-                FIXME("Handle %d isn't a texture handle\n", Value);
-                hr = DDERR_INVALIDPARAMS;
-                break;
-            }
-            else
-            {
-                IDirectDrawSurfaceImpl *surf = This->Handles[Value - 1].ptr;
-                IDirect3DTexture2 *tex = surf ? (IDirect3DTexture2 *)&surf->IDirect3DTexture2_vtbl : NULL;
-                hr = IDirect3DDevice3_SetTexture(iface, 0, tex);
-                break;
-            }
+
+            hr = IDirect3DDevice3_SetTexture(iface, 0, (IDirect3DTexture2 *)&surf->IDirect3DTexture2_vtbl);
+            break;
         }
 
         case D3DRENDERSTATE_TEXTUREMAPBLEND:
@@ -3007,7 +2989,9 @@ Thunk_IDirect3DDeviceImpl_2_SetRenderState(IDirect3DDevice2 *iface,
                                            DWORD Value)
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%d) thunking to IDirect3DDevice3 interface.\n", This, RenderStateType, Value);
+
+    TRACE("iface %p, state %#x, value %#x.\n", iface, RenderStateType, Value);
+
     return IDirect3DDevice3_SetRenderState((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl, RenderStateType, Value);
 }
 
@@ -3037,7 +3021,7 @@ IDirect3DDeviceImpl_3_SetLightState(IDirect3DDevice3 *iface,
     IDirect3DDeviceImpl *This = device_from_device3(iface);
     HRESULT hr;
 
-    TRACE("(%p)->(%08x,%08x)\n", This, LightStateType, Value);
+    TRACE("iface %p, state %#x, value %#x.\n", iface, LightStateType, Value);
 
     if (!LightStateType || (LightStateType > D3DLIGHTSTATE_COLORVERTEX))
     {
@@ -3048,35 +3032,17 @@ IDirect3DDeviceImpl_3_SetLightState(IDirect3DDevice3 *iface,
     EnterCriticalSection(&ddraw_cs);
     if (LightStateType == D3DLIGHTSTATE_MATERIAL /* 1 */)
     {
-        IDirect3DMaterialImpl *mat;
-
-        if(Value == 0) mat = NULL;
-        else if(Value > This->numHandles)
+        IDirect3DMaterialImpl *m = ddraw_get_object(&This->handle_table, Value - 1, DDRAW_HANDLE_MATERIAL);
+        if (!m)
         {
-            ERR("Material handle out of range(%d)\n", Value);
+            WARN("Invalid material handle.\n");
             LeaveCriticalSection(&ddraw_cs);
             return DDERR_INVALIDPARAMS;
         }
-        else if(This->Handles[Value - 1].type != DDrawHandle_Material)
-        {
-            ERR("Invalid handle %d\n", Value);
-            LeaveCriticalSection(&ddraw_cs);
-            return DDERR_INVALIDPARAMS;
-        }
-        else
-        {
-            mat = This->Handles[Value - 1].ptr;
-        }
 
-        if (mat != NULL)
-        {
-            TRACE(" activating material %p.\n", mat);
-            mat->activate(mat);
-        }
-        else
-        {
-            FIXME(" D3DLIGHTSTATE_MATERIAL called with NULL material !!!\n");
-        }
+        TRACE(" activating material %p.\n", m);
+        material_activate(m);
+
         This->material = Value;
     }
     else if (LightStateType == D3DLIGHTSTATE_COLORMODEL /* 3 */)
@@ -3103,7 +3069,7 @@ IDirect3DDeviceImpl_3_SetLightState(IDirect3DDevice3 *iface,
         {
             case D3DLIGHTSTATE_AMBIENT:       /* 2 */
                 rs = D3DRENDERSTATE_AMBIENT;
-                break;         
+                break;
             case D3DLIGHTSTATE_FOGMODE:       /* 4 */
                 rs = D3DRENDERSTATE_FOGVERTEXMODE;
                 break;
@@ -3140,7 +3106,9 @@ Thunk_IDirect3DDeviceImpl_2_SetLightState(IDirect3DDevice2 *iface,
                                           DWORD Value)
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x) thunking to IDirect3DDevice3 interface.\n", This, LightStateType, Value);
+
+    TRACE("iface %p, state %#x, value %#x.\n", iface, LightStateType, Value);
+
     return IDirect3DDevice3_SetLightState((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl, LightStateType, Value);
 }
 
@@ -3170,7 +3138,7 @@ IDirect3DDeviceImpl_3_GetLightState(IDirect3DDevice3 *iface,
     IDirect3DDeviceImpl *This = device_from_device3(iface);
     HRESULT hr;
 
-    TRACE("(%p)->(%08x,%p)\n", This, LightStateType, Value);
+    TRACE("iface %p, state %#x, value %p.\n", iface, LightStateType, Value);
 
     if (!LightStateType || (LightStateType > D3DLIGHTSTATE_COLORVERTEX))
     {
@@ -3197,7 +3165,7 @@ IDirect3DDeviceImpl_3_GetLightState(IDirect3DDevice3 *iface,
         {
             case D3DLIGHTSTATE_AMBIENT:       /* 2 */
                 rs = D3DRENDERSTATE_AMBIENT;
-                break;         
+                break;
             case D3DLIGHTSTATE_FOGMODE:       /* 4 */
                 rs = D3DRENDERSTATE_FOGVERTEXMODE;
                 break;
@@ -3234,7 +3202,9 @@ Thunk_IDirect3DDeviceImpl_2_GetLightState(IDirect3DDevice2 *iface,
                                           DWORD *Value)
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice3 interface.\n", This, LightStateType, Value);
+
+    TRACE("iface %p, state %#x, value %p.\n", iface, LightStateType, Value);
+
     return IDirect3DDevice3_GetLightState((IDirect3DDevice3 *)&This->IDirect3DDevice3_vtbl, LightStateType, Value);
 }
 
@@ -3267,7 +3237,8 @@ IDirect3DDeviceImpl_7_SetTransform(IDirect3DDevice7 *iface,
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     D3DTRANSFORMSTATETYPE type;
     HRESULT hr;
-    TRACE("(%p)->(%08x,%p): Relay\n", This, TransformStateType, Matrix);
+
+    TRACE("iface %p, state %#x, matrix %p.\n", iface, TransformStateType, Matrix);
 
     switch(TransformStateType)
     {
@@ -3319,7 +3290,9 @@ Thunk_IDirect3DDeviceImpl_3_SetTransform(IDirect3DDevice3 *iface,
                                          D3DMATRIX *D3DMatrix)
 {
     IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
+
+    TRACE("iface %p, state %#x, matrix %p.\n", iface, TransformStateType, D3DMatrix);
+
     return IDirect3DDevice7_SetTransform((IDirect3DDevice7 *)This, TransformStateType, D3DMatrix);
 }
 
@@ -3329,7 +3302,9 @@ Thunk_IDirect3DDeviceImpl_2_SetTransform(IDirect3DDevice2 *iface,
                                          D3DMATRIX *D3DMatrix)
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
+
+    TRACE("iface %p, state %#x, matrix %p.\n", iface, TransformStateType, D3DMatrix);
+
     return IDirect3DDevice7_SetTransform((IDirect3DDevice7 *)This, TransformStateType, D3DMatrix);
 }
 
@@ -3358,7 +3333,8 @@ IDirect3DDeviceImpl_7_GetTransform(IDirect3DDevice7 *iface,
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     D3DTRANSFORMSTATETYPE type;
     HRESULT hr;
-    TRACE("(%p)->(%08x,%p): Relay\n", This, TransformStateType, Matrix);
+
+    TRACE("iface %p, state %#x, matrix %p.\n", iface, TransformStateType, Matrix);
 
     switch(TransformStateType)
     {
@@ -3408,7 +3384,9 @@ Thunk_IDirect3DDeviceImpl_3_GetTransform(IDirect3DDevice3 *iface,
                                          D3DMATRIX *D3DMatrix)
 {
     IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
+
+    TRACE("iface %p, state %#x, matrix %p.\n", iface, TransformStateType, D3DMatrix);
+
     return IDirect3DDevice7_GetTransform((IDirect3DDevice7 *)This, TransformStateType, D3DMatrix);
 }
 
@@ -3418,7 +3396,9 @@ Thunk_IDirect3DDeviceImpl_2_GetTransform(IDirect3DDevice2 *iface,
                                          D3DMATRIX *D3DMatrix)
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
+
+    TRACE("iface %p, state %#x, matrix %p.\n", iface, TransformStateType, D3DMatrix);
+
     return IDirect3DDevice7_GetTransform((IDirect3DDevice7 *)This, TransformStateType, D3DMatrix);
 }
 
@@ -3448,7 +3428,8 @@ IDirect3DDeviceImpl_7_MultiplyTransform(IDirect3DDevice7 *iface,
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
     D3DTRANSFORMSTATETYPE type;
-    TRACE("(%p)->(%08x,%p): Relay\n", This, TransformStateType, D3DMatrix);
+
+    TRACE("iface %p, state %#x, matrix %p.\n", iface, TransformStateType, D3DMatrix);
 
     switch(TransformStateType)
     {
@@ -3497,7 +3478,9 @@ Thunk_IDirect3DDeviceImpl_3_MultiplyTransform(IDirect3DDevice3 *iface,
                                               D3DMATRIX *D3DMatrix)
 {
     IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
+
+    TRACE("iface %p, state %#x, matrix %p.\n", iface, TransformStateType, D3DMatrix);
+
     return IDirect3DDevice7_MultiplyTransform((IDirect3DDevice7 *)This, TransformStateType, D3DMatrix);
 }
 
@@ -3507,7 +3490,9 @@ Thunk_IDirect3DDeviceImpl_2_MultiplyTransform(IDirect3DDevice2 *iface,
                                               D3DMATRIX *D3DMatrix)
 {
     IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
+
+    TRACE("iface %p, state %#x, matrix %p.\n", iface, TransformStateType, D3DMatrix);
+
     return IDirect3DDevice7_MultiplyTransform((IDirect3DDevice7 *)This, TransformStateType, D3DMatrix);
 }
 
@@ -3543,7 +3528,9 @@ IDirect3DDeviceImpl_7_DrawPrimitive(IDirect3DDevice7 *iface,
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     UINT stride;
     HRESULT hr;
-    TRACE("(%p)->(%08x,%08x,%p,%08x,%08x): Relay!\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
+
+    TRACE("iface %p, primitive_type %#x, FVF %#x, vertices %p, vertex_count %u, flags %#x.\n",
+            iface, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
 
     if(!Vertices)
         return DDERR_INVALIDPARAMS;
@@ -3553,8 +3540,7 @@ IDirect3DDeviceImpl_7_DrawPrimitive(IDirect3DDevice7 *iface,
 
     /* Set the FVF */
     EnterCriticalSection(&ddraw_cs);
-    hr = IWineD3DDevice_SetVertexDeclaration(This->wineD3DDevice,
-                                             IDirectDrawImpl_FindDecl(This->ddraw, VertexType));
+    hr = IWineD3DDevice_SetVertexDeclaration(This->wineD3DDevice, ddraw_find_decl(This->ddraw, VertexType));
     if(hr != D3D_OK)
     {
         LeaveCriticalSection(&ddraw_cs);
@@ -3605,9 +3591,10 @@ Thunk_IDirect3DDeviceImpl_3_DrawPrimitive(IDirect3DDevice3 *iface,
                                           DWORD VertexCount,
                                           DWORD Flags)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
-    return IDirect3DDevice7_DrawPrimitive((IDirect3DDevice7 *)This,
+    TRACE("iface %p, primitive_type %#x, FVF %#x, vertices %p, vertex_count %u, flags %#x.\n",
+            iface, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
+
+    return IDirect3DDevice7_DrawPrimitive((IDirect3DDevice7 *)device_from_device3(iface),
             PrimitiveType, VertexType, Vertices, VertexCount, Flags);
 }
 
@@ -3619,9 +3606,10 @@ Thunk_IDirect3DDeviceImpl_2_DrawPrimitive(IDirect3DDevice2 *iface,
                                           DWORD VertexCount,
                                           DWORD Flags)
 {
-    IDirect3DDeviceImpl *This = device_from_device2(iface);
     DWORD FVF;
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
+
+    TRACE("iface %p, primitive_type %#x, vertex_type %#x, vertices %p, vertex_count %u, flags %#x.\n",
+            iface, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
 
     switch(VertexType)
     {
@@ -3633,7 +3621,8 @@ Thunk_IDirect3DDeviceImpl_2_DrawPrimitive(IDirect3DDevice2 *iface,
             return DDERR_INVALIDPARAMS;  /* Should never happen */
     }
 
-    return IDirect3DDevice7_DrawPrimitive((IDirect3DDevice7 *)This, PrimitiveType, FVF, Vertices, VertexCount, Flags);
+    return IDirect3DDevice7_DrawPrimitive((IDirect3DDevice7 *)device_from_device2(iface),
+            PrimitiveType, FVF, Vertices, VertexCount, Flags);
 }
 
 /*****************************************************************************
@@ -3672,12 +3661,13 @@ IDirect3DDeviceImpl_7_DrawIndexedPrimitive(IDirect3DDevice7 *iface,
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
-    TRACE("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x): Relay!\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
+
+    TRACE("iface %p, primitive_type %#x, FVF %#x, vertices %p, vertex_count %u, indices %p, index_count %u, flags %#x.\n",
+            iface, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
 
     /* Set the D3DDevice's FVF */
     EnterCriticalSection(&ddraw_cs);
-    hr = IWineD3DDevice_SetVertexDeclaration(This->wineD3DDevice,
-                                             IDirectDrawImpl_FindDecl(This->ddraw, VertexType));
+    hr = IWineD3DDevice_SetVertexDeclaration(This->wineD3DDevice, ddraw_find_decl(This->ddraw, VertexType));
     if(FAILED(hr))
     {
         ERR(" (%p) Setting the FVF failed, hr = %x!\n", This, hr);
@@ -3735,9 +3725,10 @@ Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitive(IDirect3DDevice3 *iface,
                                                  DWORD IndexCount,
                                                  DWORD Flags)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
-    return IDirect3DDevice7_DrawIndexedPrimitive((IDirect3DDevice7 *)This,
+    TRACE("iface %p, primitive_type %#x, FVF %#x, vertices %p, vertex_count %u, indices %p, index_count %u, flags %#x.\n",
+            iface, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
+
+    return IDirect3DDevice7_DrawIndexedPrimitive((IDirect3DDevice7 *)device_from_device3(iface),
             PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
 }
 
@@ -3752,8 +3743,9 @@ Thunk_IDirect3DDeviceImpl_2_DrawIndexedPrimitive(IDirect3DDevice2 *iface,
                                                  DWORD Flags)
 {
     DWORD FVF;
-    IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
+
+    TRACE("iface %p, primitive_type %#x, vertex_type %#x, vertices %p, vertex_count %u, indices %p, index_count %u, flags %#x.\n",
+            iface, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
 
     switch(VertexType)
     {
@@ -3765,7 +3757,7 @@ Thunk_IDirect3DDeviceImpl_2_DrawIndexedPrimitive(IDirect3DDevice2 *iface,
             return DDERR_INVALIDPARAMS;  /* Should never happen */
     }
 
-    return IDirect3DDevice7_DrawIndexedPrimitive((IDirect3DDevice7 *)This,
+    return IDirect3DDevice7_DrawIndexedPrimitive((IDirect3DDevice7 *)device_from_device2(iface),
             PrimitiveType, FVF, Vertices, VertexCount, Indices, IndexCount, Flags);
 }
 
@@ -3789,8 +3781,7 @@ static HRESULT WINAPI
 IDirect3DDeviceImpl_7_SetClipStatus(IDirect3DDevice7 *iface,
                                     D3DCLIPSTATUS *ClipStatus)
 {
-    IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
-    FIXME("(%p)->(%p): Stub!\n", This, ClipStatus);
+    FIXME("iface %p, clip_status %p stub!\n", iface, ClipStatus);
 
     /* D3DCLIPSTATUS and WINED3DCLIPSTATUS are different. I don't know how to convert them
      * Perhaps this needs a new data type and an additional IWineD3DDevice method
@@ -3803,18 +3794,18 @@ static HRESULT WINAPI
 Thunk_IDirect3DDeviceImpl_3_SetClipStatus(IDirect3DDevice3 *iface,
                                           D3DCLIPSTATUS *ClipStatus)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
-    return IDirect3DDevice7_SetClipStatus((IDirect3DDevice7 *)This, ClipStatus);
+    TRACE("iface %p, clip_status %p.\n", iface, ClipStatus);
+
+    return IDirect3DDevice7_SetClipStatus((IDirect3DDevice7 *)device_from_device3(iface), ClipStatus);
 }
 
 static HRESULT WINAPI
 Thunk_IDirect3DDeviceImpl_2_SetClipStatus(IDirect3DDevice2 *iface,
                                           D3DCLIPSTATUS *ClipStatus)
 {
-    IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
-    return IDirect3DDevice7_SetClipStatus((IDirect3DDevice7 *)This, ClipStatus);
+    TRACE("iface %p, clip_status %p.\n", iface, ClipStatus);
+
+    return IDirect3DDevice7_SetClipStatus((IDirect3DDevice7 *)device_from_device2(iface), ClipStatus);
 }
 
 /*****************************************************************************
@@ -3833,8 +3824,7 @@ static HRESULT WINAPI
 IDirect3DDeviceImpl_7_GetClipStatus(IDirect3DDevice7 *iface,
                                     D3DCLIPSTATUS *ClipStatus)
 {
-    IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
-    FIXME("(%p)->(%p): Stub!\n", This, ClipStatus);
+    FIXME("iface %p, clip_status %p stub!\n", iface, ClipStatus);
 
     /* D3DCLIPSTATUS and WINED3DCLIPSTATUS are different. I don't know how to convert them */
     /* return IWineD3DDevice_GetClipStatus(This->wineD3DDevice, ClipStatus);*/
@@ -3845,18 +3835,18 @@ static HRESULT WINAPI
 Thunk_IDirect3DDeviceImpl_3_GetClipStatus(IDirect3DDevice3 *iface,
                                           D3DCLIPSTATUS *ClipStatus)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
-    return IDirect3DDevice7_GetClipStatus((IDirect3DDevice7 *)This, ClipStatus);
+    TRACE("iface %p, clip_status %p.\n", iface, ClipStatus);
+
+    return IDirect3DDevice7_GetClipStatus((IDirect3DDevice7 *)device_from_device3(iface), ClipStatus);
 }
 
 static HRESULT WINAPI
 Thunk_IDirect3DDeviceImpl_2_GetClipStatus(IDirect3DDevice2 *iface,
                                           D3DCLIPSTATUS *ClipStatus)
 {
-    IDirect3DDeviceImpl *This = device_from_device2(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
-    return IDirect3DDevice7_GetClipStatus((IDirect3DDevice7 *)This, ClipStatus);
+    TRACE("iface %p, clip_status %p.\n", iface, ClipStatus);
+
+    return IDirect3DDevice7_GetClipStatus((IDirect3DDevice7 *)device_from_device2(iface), ClipStatus);
 }
 
 /*****************************************************************************
@@ -3893,7 +3883,8 @@ IDirect3DDeviceImpl_7_DrawPrimitiveStrided(IDirect3DDevice7 *iface,
     DWORD i;
     HRESULT hr;
 
-    TRACE("(%p)->(%08x,%08x,%p,%08x,%08x): stub!\n", This, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
+    TRACE("iface %p, primitive_type %#x, FVF %#x, strided_data %p, vertex_count %u, flags %#x.\n",
+            iface, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
 
     memset(&WineD3DStrided, 0, sizeof(WineD3DStrided));
     /* Get the strided data right. the wined3d structure is a bit bigger
@@ -3996,9 +3987,10 @@ Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveStrided(IDirect3DDevice3 *iface,
                                                  DWORD VertexCount,
                                                  DWORD Flags)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
-    return IDirect3DDevice7_DrawPrimitiveStrided((IDirect3DDevice7 *)This,
+    TRACE("iface %p, primitive_type %#x, FVF %#x, strided_data %p, vertex_count %u, flags %#x.\n",
+            iface, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
+
+    return IDirect3DDevice7_DrawPrimitiveStrided((IDirect3DDevice7 *)device_from_device3(iface),
             PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
 }
 
@@ -4034,7 +4026,8 @@ IDirect3DDeviceImpl_7_DrawIndexedPrimitiveStrided(IDirect3DDevice7 *iface,
     DWORD i;
     HRESULT hr;
 
-    TRACE("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x)\n", This, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
+    TRACE("iface %p, primitive_type %#x, FVF %#x, strided_data %p, vertex_count %u, indices %p, index_count %u, flags %#x.\n",
+            iface, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
 
     memset(&WineD3DStrided, 0, sizeof(WineD3DStrided));
     /* Get the strided data right. the wined3d structure is a bit bigger
@@ -4144,10 +4137,11 @@ Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveStrided(IDirect3DDevice3 *iface,
                                                         DWORD IndexCount,
                                                         DWORD Flags)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", iface, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
-    return IDirect3DDevice7_DrawIndexedPrimitiveStrided((IDirect3DDevice7 *)This, PrimitiveType,
-            VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
+    TRACE("iface %p, primitive_type %#x, FVF %#x, strided_data %p, vertex_count %u, indices %p, index_count %u, flags %#x.\n",
+            iface, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
+
+    return IDirect3DDevice7_DrawIndexedPrimitiveStrided((IDirect3DDevice7 *)device_from_device3(iface),
+            PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
 }
 
 /*****************************************************************************
@@ -4182,7 +4176,8 @@ IDirect3DDeviceImpl_7_DrawPrimitiveVB(IDirect3DDevice7 *iface,
     HRESULT hr;
     DWORD stride;
 
-    TRACE("(%p)->(%08x,%p,%08x,%08x,%08x)\n", This, PrimitiveType, D3DVertexBuf, StartVertex, NumVertices, Flags);
+    TRACE("iface %p, primitive_type %#x, vb %p, start_vertex %u, vertex_count %u, flags %#x.\n",
+            iface, PrimitiveType, D3DVertexBuf, StartVertex, NumVertices, Flags);
 
     /* Sanity checks */
     if(!vb)
@@ -4259,11 +4254,13 @@ Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveVB(IDirect3DDevice3 *iface,
                                             DWORD NumVertices,
                                             DWORD Flags)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
     IDirect3DVertexBufferImpl *vb = D3DVertexBuf ? vb_from_vb1(D3DVertexBuf) : NULL;
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%p,%08x,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This,  PrimitiveType, vb, StartVertex, NumVertices, Flags);
-    return IDirect3DDevice7_DrawPrimitiveVB((IDirect3DDevice7 *)This, PrimitiveType,
-            (IDirect3DVertexBuffer7 *)vb, StartVertex, NumVertices, Flags);
+
+    TRACE("iface %p, primitive_type %#x, vb %p, start_vertex %u, vertex_count %u, flags %#x.\n",
+            iface, PrimitiveType, D3DVertexBuf, StartVertex, NumVertices, Flags);
+
+    return IDirect3DDevice7_DrawPrimitiveVB((IDirect3DDevice7 *)device_from_device3(iface),
+            PrimitiveType, (IDirect3DVertexBuffer7 *)vb, StartVertex, NumVertices, Flags);
 }
 
 
@@ -4299,8 +4296,10 @@ IDirect3DDeviceImpl_7_DrawIndexedPrimitiveVB(IDirect3DDevice7 *iface,
     DWORD stride = get_flexible_vertex_size(vb->fvf);
     WORD *LockedIndices;
     HRESULT hr;
+    WINED3DBUFFER_DESC desc;
 
-    TRACE("(%p)->(%08x,%p,%d,%d,%p,%d,%08x)\n", This, PrimitiveType, vb, StartVertex, NumVertices, Indices, IndexCount, Flags);
+    TRACE("iface %p, primitive_type %#x, vb %p, start_vertex %u, vertex_count %u, indices %p, index_count %u, flags %#x.\n",
+            iface, PrimitiveType, D3DVertexBuf, StartVertex, NumVertices, Indices, IndexCount, Flags);
 
     /* Steps:
      * 1) Upload the Indices to the index buffer
@@ -4320,6 +4319,33 @@ IDirect3DDeviceImpl_7_DrawIndexedPrimitiveVB(IDirect3DDevice7 *iface,
         return hr;
     }
 
+    /* check that the buffer is large enough to hold the indices,
+     * reallocate if necessary. */
+    IWineD3DBuffer_GetDesc(This->indexbuffer, &desc);
+    if (desc.Size < IndexCount * sizeof(WORD))
+    {
+        UINT size = max(desc.Size * 2, IndexCount * sizeof(WORD));
+        IWineD3DBuffer *buffer;
+        IParentImpl *parent;
+
+        TRACE("Growing index buffer to %u bytes\n", size);
+
+        parent = IWineD3DBuffer_GetParent(This->indexbuffer);
+        hr = IWineD3DDevice_CreateIndexBuffer(This->wineD3DDevice, size, WINED3DUSAGE_DYNAMIC /* Usage */,
+                WINED3DPOOL_DEFAULT, parent, &ddraw_null_wined3d_parent_ops, &buffer);
+        if (FAILED(hr))
+        {
+            ERR("(%p) IWineD3DDevice::CreateIndexBuffer failed with hr = %08x\n", This, hr);
+            LeaveCriticalSection(&ddraw_cs);
+            return hr;
+        }
+
+        IWineD3DBuffer_Release(This->indexbuffer);
+        This->indexbuffer = buffer;
+
+        parent->child = (IUnknown *)buffer;
+    }
+
     /* copy the index stream into the index buffer.
      * A new IWineD3DDevice method could be created
      * which takes an user pointer containing the indices
@@ -4331,7 +4357,6 @@ IDirect3DDeviceImpl_7_DrawIndexedPrimitiveVB(IDirect3DDevice7 *iface,
                             IndexCount * sizeof(WORD),
                             (BYTE **) &LockedIndices,
                             0 /* Flags */);
-    assert(IndexCount < 0x100000);
     if(hr != D3D_OK)
     {
         ERR("(%p) IWineD3DBuffer::Map failed with hr = %08x\n", This, hr);
@@ -4414,12 +4439,13 @@ Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveVB(IDirect3DDevice3 *iface,
                                                    DWORD IndexCount,
                                                    DWORD Flags)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
     IDirect3DVertexBufferImpl *VB = vb_from_vb1(D3DVertexBuf);
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%p,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VB, Indices, IndexCount, Flags);
 
-    return IDirect3DDevice7_DrawIndexedPrimitiveVB((IDirect3DDevice7 *)This, PrimitiveType,
-            (IDirect3DVertexBuffer7 *)VB, 0, IndexCount, Indices, IndexCount, Flags);
+    TRACE("iface %p, primitive_type %#x, vb %p, indices %p, index_count %u, flags %#x.\n",
+            iface, PrimitiveType, D3DVertexBuf, Indices, IndexCount, Flags);
+
+    return IDirect3DDevice7_DrawIndexedPrimitiveVB((IDirect3DDevice7 *)device_from_device3(iface),
+            PrimitiveType, (IDirect3DVertexBuffer7 *)VB, 0, IndexCount, Indices, IndexCount, Flags);
 }
 
 /*****************************************************************************
@@ -4475,7 +4501,8 @@ IDirect3DDeviceImpl_7_ComputeSphereVisibility(IDirect3DDevice7 *iface,
     HRESULT hr;
     UINT i, j;
 
-    TRACE("(%p)->(%p,%p,%08x,%08x,%p)\n", iface, Centers, Radii, NumSpheres, Flags, ReturnValues);
+    TRACE("iface %p, centers %p, radii %p, sphere_count %u, flags %#x, return_values %p.\n",
+            iface, Centers, Radii, NumSpheres, Flags, ReturnValues);
 
     hr = IDirect3DDeviceImpl_7_GetTransform(iface, D3DTRANSFORMSTATE_WORLD, &m);
     if ( hr != DD_OK ) return DDERR_INVALIDPARAMS;
@@ -4540,9 +4567,10 @@ Thunk_IDirect3DDeviceImpl_3_ComputeSphereVisibility(IDirect3DDevice3 *iface,
                                                     DWORD Flags,
                                                     DWORD *ReturnValues)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%p,%p,%08x,%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, Centers, Radii, NumSpheres, Flags, ReturnValues);
-    return IDirect3DDevice7_ComputeSphereVisibility((IDirect3DDevice7 *)This,
+    TRACE("iface %p, centers %p, radii %p, sphere_count %u, flags %#x, return_values %p.\n",
+            iface, Centers, Radii, NumSpheres, Flags, ReturnValues);
+
+    return IDirect3DDevice7_ComputeSphereVisibility((IDirect3DDevice7 *)device_from_device3(iface),
             Centers, Radii, NumSpheres, Flags, ReturnValues);
 }
 
@@ -4573,7 +4601,8 @@ IDirect3DDeviceImpl_7_GetTexture(IDirect3DDevice7 *iface,
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     IWineD3DBaseTexture *Surf;
     HRESULT hr;
-    TRACE("(%p)->(%d,%p): Relay\n", This, Stage, Texture);
+
+    TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, Texture);
 
     if(!Texture)
     {
@@ -4590,11 +4619,8 @@ IDirect3DDeviceImpl_7_GetTexture(IDirect3DDevice7 *iface,
         return hr;
     }
 
-    /* GetParent AddRef()s, which is perfectly OK.
-     * We have passed the IDirectDrawSurface7 interface to WineD3D, so that's OK too.
-     */
-    hr = IWineD3DBaseTexture_GetParent(Surf,
-                                       (IUnknown **) Texture);
+    *Texture = IWineD3DBaseTexture_GetParent(Surf);
+    IDirectDrawSurface7_AddRef(*Texture);
     LeaveCriticalSection(&ddraw_cs);
     return hr;
 }
@@ -4627,16 +4653,16 @@ Thunk_IDirect3DDeviceImpl_3_GetTexture(IDirect3DDevice3 *iface,
                                        DWORD Stage,
                                        IDirect3DTexture2 **Texture2)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
     HRESULT ret;
     IDirectDrawSurface7 *ret_val;
 
-    TRACE_(ddraw_thunk)("(%p)->(%d,%p) thunking to IDirect3DDevice7 interface.\n", This, Stage, Texture2);
-    ret = IDirect3DDevice7_GetTexture((IDirect3DDevice7 *)This, Stage, &ret_val);
+    TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, Texture2);
+
+    ret = IDirect3DDevice7_GetTexture((IDirect3DDevice7 *)device_from_device3(iface), Stage, &ret_val);
 
     *Texture2 = ret_val ? (IDirect3DTexture2 *)&((IDirectDrawSurfaceImpl *)ret_val)->IDirect3DTexture2_vtbl : NULL;
 
-    TRACE_(ddraw_thunk)(" returning interface %p.\n", *Texture2);
+    TRACE("Returning texture %p.\n", *Texture2);
 
     return ret;
 }
@@ -4665,7 +4691,8 @@ IDirect3DDeviceImpl_7_SetTexture(IDirect3DDevice7 *iface,
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     IDirectDrawSurfaceImpl *surf = (IDirectDrawSurfaceImpl *)Texture;
     HRESULT hr;
-    TRACE("(%p)->(%08x,%p): Relay!\n", This, Stage, surf);
+
+    TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, Texture);
 
     /* Texture may be NULL here */
     EnterCriticalSection(&ddraw_cs);
@@ -4708,7 +4735,8 @@ IDirect3DDeviceImpl_3_SetTexture(IDirect3DDevice3 *iface,
     IDirectDrawSurfaceImpl *tex = Texture2 ? surface_from_texture2(Texture2) : NULL;
     DWORD texmapblend;
     HRESULT hr;
-    TRACE("(%p)->(%d,%p)\n", This, Stage, tex);
+
+    TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, Texture2);
 
     EnterCriticalSection(&ddraw_cs);
 
@@ -4817,8 +4845,10 @@ IDirect3DDeviceImpl_7_GetTextureStageState(IDirect3DDevice7 *iface,
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
-    const struct tss_lookup *l = &tss_lookup[TexStageStateType];
-    TRACE("(%p)->(%08x,%08x,%p): Relay!\n", This, Stage, TexStageStateType, State);
+    const struct tss_lookup *l;
+
+    TRACE("iface %p, stage %u, state %#x, value %p.\n",
+            iface, Stage, TexStageStateType, State);
 
     if(!State)
         return DDERR_INVALIDPARAMS;
@@ -4826,10 +4856,11 @@ IDirect3DDeviceImpl_7_GetTextureStageState(IDirect3DDevice7 *iface,
     if (TexStageStateType > D3DTSS_TEXTURETRANSFORMFLAGS)
     {
         WARN("Invalid TexStageStateType %#x passed.\n", TexStageStateType);
-        *State = 0;
         return DD_OK;
     }
 
+    l = &tss_lookup[TexStageStateType];
+
     EnterCriticalSection(&ddraw_cs);
 
     if (l->sampler_state)
@@ -4916,9 +4947,11 @@ Thunk_IDirect3DDeviceImpl_3_GetTextureStageState(IDirect3DDevice3 *iface,
                                                  D3DTEXTURESTAGESTATETYPE TexStageStateType,
                                                  DWORD *State)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, Stage, TexStageStateType, State);
-    return IDirect3DDevice7_GetTextureStageState((IDirect3DDevice7 *)This, Stage, TexStageStateType, State);
+    TRACE("iface %p, stage %u, state %#x, value %p.\n",
+            iface, Stage, TexStageStateType, State);
+
+    return IDirect3DDevice7_GetTextureStageState((IDirect3DDevice7 *)device_from_device3(iface),
+            Stage, TexStageStateType, State);
 }
 
 /*****************************************************************************
@@ -4946,9 +4979,11 @@ IDirect3DDeviceImpl_7_SetTextureStageState(IDirect3DDevice7 *iface,
                                            DWORD State)
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
-    const struct tss_lookup *l = &tss_lookup[TexStageStateType];
+    const struct tss_lookup *l;
     HRESULT hr;
-    TRACE("(%p)->(%08x,%08x,%08x): Relay!\n", This, Stage, TexStageStateType, State);
+
+    TRACE("iface %p, stage %u, state %#x, value %#x.\n",
+            iface, Stage, TexStageStateType, State);
 
     if (TexStageStateType > D3DTSS_TEXTURETRANSFORMFLAGS)
     {
@@ -4956,6 +4991,8 @@ IDirect3DDeviceImpl_7_SetTextureStageState(IDirect3DDevice7 *iface,
         return DD_OK;
     }
 
+    l = &tss_lookup[TexStageStateType];
+
     EnterCriticalSection(&ddraw_cs);
 
     if (l->sampler_state)
@@ -5047,9 +5084,11 @@ Thunk_IDirect3DDeviceImpl_3_SetTextureStageState(IDirect3DDevice3 *iface,
                                                  D3DTEXTURESTAGESTATETYPE TexStageStateType,
                                                  DWORD State)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, Stage, TexStageStateType, State);
-    return IDirect3DDevice7_SetTextureStageState((IDirect3DDevice7 *)This, Stage, TexStageStateType, State);
+    TRACE("iface %p, stage %u, state %#x, value %#x.\n",
+            iface, Stage, TexStageStateType, State);
+
+    return IDirect3DDevice7_SetTextureStageState((IDirect3DDevice7 *)device_from_device3(iface),
+            Stage, TexStageStateType, State);
 }
 
 /*****************************************************************************
@@ -5076,7 +5115,8 @@ IDirect3DDeviceImpl_7_ValidateDevice(IDirect3DDevice7 *iface,
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
-    TRACE("(%p)->(%p): Relay\n", This, NumPasses);
+
+    TRACE("iface %p, pass_count %p.\n", iface, NumPasses);
 
     EnterCriticalSection(&ddraw_cs);
     hr = IWineD3DDevice_ValidateDevice(This->wineD3DDevice, NumPasses);
@@ -5109,9 +5149,9 @@ static HRESULT WINAPI
 Thunk_IDirect3DDeviceImpl_3_ValidateDevice(IDirect3DDevice3 *iface,
                                            DWORD *Passes)
 {
-    IDirect3DDeviceImpl *This = device_from_device3(iface);
-    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, Passes);
-    return IDirect3DDevice7_ValidateDevice((IDirect3DDevice7 *)This, Passes);
+    TRACE("iface %p, pass_count %p.\n", iface, Passes);
+
+    return IDirect3DDevice7_ValidateDevice((IDirect3DDevice7 *)device_from_device3(iface), Passes);
 }
 
 /*****************************************************************************
@@ -5146,11 +5186,12 @@ IDirect3DDeviceImpl_7_Clear(IDirect3DDevice7 *iface,
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
-    TRACE("(%p)->(%08x,%p,%08x,%08x,%f,%08x): Relay\n", This, Count, Rects, Flags, Color, Z, Stencil);
 
-    /* Note; D3DRECT is compatible with WINED3DRECT */
+    TRACE("iface %p, count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %#x.\n",
+            iface, Count, Rects, Flags, Color, Z, Stencil);
+
     EnterCriticalSection(&ddraw_cs);
-    hr = IWineD3DDevice_Clear(This->wineD3DDevice, Count, (WINED3DRECT*) Rects, Flags, Color, Z, Stencil);
+    hr = IWineD3DDevice_Clear(This->wineD3DDevice, Count, (RECT *)Rects, Flags, Color, Z, Stencil);
     LeaveCriticalSection(&ddraw_cs);
     return hr;
 }
@@ -5209,7 +5250,8 @@ IDirect3DDeviceImpl_7_SetViewport(IDirect3DDevice7 *iface,
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
-    TRACE("(%p)->(%p) Relay!\n", This, Data);
+
+    TRACE("iface %p, viewport %p.\n", iface, Data);
 
     if(!Data)
         return DDERR_INVALIDPARAMS;
@@ -5265,7 +5307,8 @@ IDirect3DDeviceImpl_7_GetViewport(IDirect3DDevice7 *iface,
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
-    TRACE("(%p)->(%p) Relay!\n", This, Data);
+
+    TRACE("iface %p, viewport %p.\n", iface, Data);
 
     if(!Data)
         return DDERR_INVALIDPARAMS;
@@ -5322,7 +5365,8 @@ IDirect3DDeviceImpl_7_SetMaterial(IDirect3DDevice7 *iface,
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
-    TRACE("(%p)->(%p): Relay!\n", This, Mat);
+
+    TRACE("iface %p, material %p.\n", iface, Mat);
 
     if (!Mat) return DDERR_INVALIDPARAMS;
     /* Note: D3DMATERIAL7 is compatible with WINED3DMATERIAL */
@@ -5376,7 +5420,8 @@ IDirect3DDeviceImpl_7_GetMaterial(IDirect3DDevice7 *iface,
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
-    TRACE("(%p)->(%p): Relay!\n", This, Mat);
+
+    TRACE("iface %p, material %p.\n", iface, Mat);
 
     EnterCriticalSection(&ddraw_cs);
     /* Note: D3DMATERIAL7 is compatible with WINED3DMATERIAL */
@@ -5430,7 +5475,8 @@ IDirect3DDeviceImpl_7_SetLight(IDirect3DDevice7 *iface,
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
-    TRACE("(%p)->(%08x,%p): Relay!\n", This, LightIndex, Light);
+
+    TRACE("iface %p, light_idx %u, light %p.\n", iface, LightIndex, Light);
 
     EnterCriticalSection(&ddraw_cs);
     /* Note: D3DLIGHT7 is compatible with WINED3DLIGHT */
@@ -5485,7 +5531,8 @@ IDirect3DDeviceImpl_7_GetLight(IDirect3DDevice7 *iface,
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT rc;
-    TRACE("(%p)->(%08x,%p): Relay!\n", This, LightIndex, Light);
+
+    TRACE("iface %p, light_idx %u, light %p.\n", iface, LightIndex, Light);
 
     EnterCriticalSection(&ddraw_cs);
     /* Note: D3DLIGHT7 is compatible with WINED3DLIGHT */
@@ -5538,7 +5585,8 @@ IDirect3DDeviceImpl_7_BeginStateBlock(IDirect3DDevice7 *iface)
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
-    TRACE("(%p)->(): Relay!\n", This);
+
+    TRACE("iface %p.\n", iface);
 
     EnterCriticalSection(&ddraw_cs);
     hr = IWineD3DDevice_BeginStateBlock(This->wineD3DDevice);
@@ -5587,8 +5635,11 @@ IDirect3DDeviceImpl_7_EndStateBlock(IDirect3DDevice7 *iface,
                                     DWORD *BlockHandle)
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
+    IWineD3DStateBlock *wined3d_sb;
     HRESULT hr;
-    TRACE("(%p)->(%p): Relay!\n", This, BlockHandle);
+    DWORD h;
+
+    TRACE("iface %p, stateblock %p.\n", iface, BlockHandle);
 
     if(!BlockHandle)
     {
@@ -5597,17 +5648,29 @@ IDirect3DDeviceImpl_7_EndStateBlock(IDirect3DDevice7 *iface,
     }
 
     EnterCriticalSection(&ddraw_cs);
-    *BlockHandle = IDirect3DDeviceImpl_CreateHandle(This);
-    if(!*BlockHandle)
+
+    hr = IWineD3DDevice_EndStateBlock(This->wineD3DDevice, &wined3d_sb);
+    if (FAILED(hr))
     {
-        ERR("Cannot get a handle number for the stateblock\n");
+        WARN("Failed to end stateblock, hr %#x.\n", hr);
         LeaveCriticalSection(&ddraw_cs);
+        *BlockHandle = 0;
+        return hr_ddraw_from_wined3d(hr);
+    }
+
+    h = ddraw_allocate_handle(&This->handle_table, wined3d_sb, DDRAW_HANDLE_STATEBLOCK);
+    if (h == DDRAW_INVALID_HANDLE)
+    {
+        ERR("Failed to allocate a stateblock handle.\n");
+        IWineD3DStateBlock_Release(wined3d_sb);
+        LeaveCriticalSection(&ddraw_cs);
+        *BlockHandle = 0;
         return DDERR_OUTOFMEMORY;
     }
-    This->Handles[*BlockHandle - 1].type = DDrawHandle_StateBlock;
-    hr = IWineD3DDevice_EndStateBlock(This->wineD3DDevice,
-                                      (IWineD3DStateBlock **) &This->Handles[*BlockHandle - 1].ptr);
+
     LeaveCriticalSection(&ddraw_cs);
+    *BlockHandle = h + 1;
+
     return hr_ddraw_from_wined3d(hr);
 }
 
@@ -5653,10 +5716,9 @@ static HRESULT
 IDirect3DDeviceImpl_7_PreLoad(IDirect3DDevice7 *iface,
                               IDirectDrawSurface7 *Texture)
 {
-    IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     IDirectDrawSurfaceImpl *surf = (IDirectDrawSurfaceImpl *)Texture;
 
-    TRACE("(%p)->(%p): Relay!\n", This, surf);
+    TRACE("iface %p, texture %p.\n", iface, Texture);
 
     if(!Texture)
         return DDERR_INVALIDPARAMS;
@@ -5706,25 +5768,24 @@ IDirect3DDeviceImpl_7_ApplyStateBlock(IDirect3DDevice7 *iface,
                                       DWORD BlockHandle)
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
+    IWineD3DStateBlock *wined3d_sb;
     HRESULT hr;
-    TRACE("(%p)->(%08x): Relay!\n", This, BlockHandle);
+
+    TRACE("iface %p, stateblock %#x.\n", iface, BlockHandle);
 
     EnterCriticalSection(&ddraw_cs);
-    if(!BlockHandle || BlockHandle > This->numHandles)
-    {
-        WARN("Out of range handle %d, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
-        LeaveCriticalSection(&ddraw_cs);
-        return D3DERR_INVALIDSTATEBLOCK;
-    }
-    if(This->Handles[BlockHandle - 1].type != DDrawHandle_StateBlock)
+
+    wined3d_sb = ddraw_get_object(&This->handle_table, BlockHandle - 1, DDRAW_HANDLE_STATEBLOCK);
+    if (!wined3d_sb)
     {
-        WARN("Handle %d is not a stateblock, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
+        WARN("Invalid stateblock handle.\n");
         LeaveCriticalSection(&ddraw_cs);
         return D3DERR_INVALIDSTATEBLOCK;
     }
 
-    hr = IWineD3DStateBlock_Apply((IWineD3DStateBlock *) This->Handles[BlockHandle - 1].ptr);
+    hr = IWineD3DStateBlock_Apply(wined3d_sb);
     LeaveCriticalSection(&ddraw_cs);
+
     return hr_ddraw_from_wined3d(hr);
 }
 
@@ -5770,24 +5831,22 @@ IDirect3DDeviceImpl_7_CaptureStateBlock(IDirect3DDevice7 *iface,
                                         DWORD BlockHandle)
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
+    IWineD3DStateBlock *wined3d_sb;
     HRESULT hr;
-    TRACE("(%p)->(%08x): Relay!\n", This, BlockHandle);
+
+    TRACE("iface %p, stateblock %#x.\n", iface, BlockHandle);
 
     EnterCriticalSection(&ddraw_cs);
-    if(BlockHandle == 0 || BlockHandle > This->numHandles)
-    {
-        WARN("Out of range handle %d, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
-        LeaveCriticalSection(&ddraw_cs);
-        return D3DERR_INVALIDSTATEBLOCK;
-    }
-    if(This->Handles[BlockHandle - 1].type != DDrawHandle_StateBlock)
+
+    wined3d_sb = ddraw_get_object(&This->handle_table, BlockHandle - 1, DDRAW_HANDLE_STATEBLOCK);
+    if (!wined3d_sb)
     {
-        WARN("Handle %d is not a stateblock, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
+        WARN("Invalid stateblock handle.\n");
         LeaveCriticalSection(&ddraw_cs);
         return D3DERR_INVALIDSTATEBLOCK;
     }
 
-    hr = IWineD3DStateBlock_Capture((IWineD3DStateBlock *) This->Handles[BlockHandle - 1].ptr);
+    hr = IWineD3DStateBlock_Capture(wined3d_sb);
     LeaveCriticalSection(&ddraw_cs);
     return hr_ddraw_from_wined3d(hr);
 }
@@ -5833,30 +5892,25 @@ IDirect3DDeviceImpl_7_DeleteStateBlock(IDirect3DDevice7 *iface,
                                        DWORD BlockHandle)
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
+    IWineD3DStateBlock *wined3d_sb;
     ULONG ref;
-    TRACE("(%p)->(%08x): Relay!\n", This, BlockHandle);
+
+    TRACE("iface %p, stateblock %#x.\n", iface, BlockHandle);
 
     EnterCriticalSection(&ddraw_cs);
-    if(BlockHandle == 0 || BlockHandle > This->numHandles)
-    {
-        WARN("Out of range handle %d, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
-        LeaveCriticalSection(&ddraw_cs);
-        return D3DERR_INVALIDSTATEBLOCK;
-    }
-    if(This->Handles[BlockHandle - 1].type != DDrawHandle_StateBlock)
+
+    wined3d_sb = ddraw_free_handle(&This->handle_table, BlockHandle - 1, DDRAW_HANDLE_STATEBLOCK);
+    if (!wined3d_sb)
     {
-        WARN("Handle %d is not a stateblock, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
+        WARN("Invalid stateblock handle.\n");
         LeaveCriticalSection(&ddraw_cs);
         return D3DERR_INVALIDSTATEBLOCK;
     }
 
-    ref = IWineD3DStateBlock_Release((IWineD3DStateBlock *) This->Handles[BlockHandle - 1].ptr);
-    if(ref)
+    if ((ref = IWineD3DStateBlock_Release(wined3d_sb)))
     {
-        ERR("Something is still holding the stateblock %p(Handle %d). Ref = %d\n", This->Handles[BlockHandle - 1].ptr, BlockHandle, ref);
+        ERR("Something is still holding stateblock %p (refcount %u).\n", wined3d_sb, ref);
     }
-    This->Handles[BlockHandle - 1].ptr = NULL;
-    This->Handles[BlockHandle - 1].type = DDrawHandle_Unknown;
 
     LeaveCriticalSection(&ddraw_cs);
     return D3D_OK;
@@ -5905,8 +5959,11 @@ IDirect3DDeviceImpl_7_CreateStateBlock(IDirect3DDevice7 *iface,
                                        DWORD *BlockHandle)
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
+    IWineD3DStateBlock *wined3d_sb;
     HRESULT hr;
-    TRACE("(%p)->(%08x,%p)!\n", This, Type, BlockHandle);
+    DWORD h;
+
+    TRACE("iface %p, type %#x, stateblock %p.\n", iface, Type, BlockHandle);
 
     if(!BlockHandle)
     {
@@ -5920,21 +5977,28 @@ IDirect3DDeviceImpl_7_CreateStateBlock(IDirect3DDevice7 *iface,
     }
 
     EnterCriticalSection(&ddraw_cs);
-    *BlockHandle = IDirect3DDeviceImpl_CreateHandle(This);
-    if(!*BlockHandle)
+
+    /* The D3DSTATEBLOCKTYPE enum is fine here. */
+    hr = IWineD3DDevice_CreateStateBlock(This->wineD3DDevice, Type, &wined3d_sb);
+    if (FAILED(hr))
     {
-        ERR("Cannot get a handle number for the stateblock\n");
+        WARN("Failed to create stateblock, hr %#x.\n", hr);
+        LeaveCriticalSection(&ddraw_cs);
+        return hr_ddraw_from_wined3d(hr);
+    }
+
+    h = ddraw_allocate_handle(&This->handle_table, wined3d_sb, DDRAW_HANDLE_STATEBLOCK);
+    if (h == DDRAW_INVALID_HANDLE)
+    {
+        ERR("Failed to allocate stateblock handle.\n");
+        IWineD3DStateBlock_Release(wined3d_sb);
         LeaveCriticalSection(&ddraw_cs);
         return DDERR_OUTOFMEMORY;
     }
-    This->Handles[*BlockHandle - 1].type = DDrawHandle_StateBlock;
 
-    /* The D3DSTATEBLOCKTYPE enum is fine here */
-    hr = IWineD3DDevice_CreateStateBlock(This->wineD3DDevice,
-                                         Type,
-                                         (IWineD3DStateBlock **) &This->Handles[*BlockHandle - 1].ptr,
-                                         NULL /* Parent, hope that works */);
+    *BlockHandle = h + 1;
     LeaveCriticalSection(&ddraw_cs);
+
     return hr_ddraw_from_wined3d(hr);
 }
 
@@ -6074,7 +6138,7 @@ static void copy_mipmap_chain(IDirect3DDeviceImpl *device,
             /* Try UpdateSurface that may perform a more direct opengl loading. But skip this if destination is paletted texture and has no palette.
              * Some games like Sacrifice set palette after Load, and it is a waste of effort to try to load texture without palette and generates
              * warnings in wined3d. */
-           if (!palette_missing)
+            if (!palette_missing)
                 hr = IWineD3DDevice_UpdateSurface(device->wineD3DDevice, src_level->WineD3DSurface, &rect, dest_level->WineD3DSurface,
                                 &point);
 
@@ -6154,7 +6218,9 @@ IDirect3DDeviceImpl_7_Load(IDirect3DDevice7 *iface,
     IDirectDrawSurfaceImpl *src = (IDirectDrawSurfaceImpl *)SrcTex;
     POINT destpoint;
     RECT srcrect;
-    TRACE("(%p)->(%p,%p,%p,%p,%08x)\n", This, dest, DestPoint, src, SrcRect, Flags);
+
+    TRACE("iface %p, dst_texture %p, dst_pos %s, src_texture %p, src_rect %s, flags %#x.\n",
+            iface, DestTex, wine_dbgstr_point(DestPoint), SrcTex, wine_dbgstr_rect(SrcRect), Flags);
 
     if( (!src) || (!dest) )
         return DDERR_INVALIDPARAMS;
@@ -6364,7 +6430,8 @@ IDirect3DDeviceImpl_7_LightEnable(IDirect3DDevice7 *iface,
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
-    TRACE("(%p)->(%08x,%d): Relay!\n", This, LightIndex, Enable);
+
+    TRACE("iface %p, light_idx %u, enabled %#x.\n", iface, LightIndex, Enable);
 
     EnterCriticalSection(&ddraw_cs);
     hr = IWineD3DDevice_SetLightEnable(This->wineD3DDevice, LightIndex, Enable);
@@ -6419,7 +6486,8 @@ IDirect3DDeviceImpl_7_GetLightEnable(IDirect3DDevice7 *iface,
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
-    TRACE("(%p)->(%08x,%p): Relay\n", This, LightIndex, Enable);
+
+    TRACE("iface %p, light_idx %u, enabled %p.\n", iface, LightIndex, Enable);
 
     if(!Enable)
         return DDERR_INVALIDPARAMS;
@@ -6477,7 +6545,8 @@ IDirect3DDeviceImpl_7_SetClipPlane(IDirect3DDevice7 *iface,
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
-    TRACE("(%p)->(%08x,%p): Relay!\n", This, Index, PlaneEquation);
+
+    TRACE("iface %p, idx %u, plane %p.\n", iface, Index, PlaneEquation);
 
     if(!PlaneEquation)
         return DDERR_INVALIDPARAMS;
@@ -6533,7 +6602,8 @@ IDirect3DDeviceImpl_7_GetClipPlane(IDirect3DDevice7 *iface,
 {
     IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
     HRESULT hr;
-    TRACE("(%p)->(%d,%p): Relay!\n", This, Index, PlaneEquation);
+
+    TRACE("iface %p, idx %u, plane %p.\n", iface, Index, PlaneEquation);
 
     if(!PlaneEquation)
         return DDERR_INVALIDPARAMS;
@@ -6589,10 +6659,10 @@ IDirect3DDeviceImpl_7_GetInfo(IDirect3DDevice7 *iface,
                               void *DevInfoStruct,
                               DWORD Size)
 {
-    IDirect3DDeviceImpl *This = (IDirect3DDeviceImpl *)iface;
-    TRACE("(%p)->(%08x,%p,%08x)\n", This, DevInfoID, DevInfoStruct, Size);
+    TRACE("iface %p, info_id %#x, info %p, info_size %u.\n",
+            iface, DevInfoID, DevInfoStruct, Size);
 
-    if (TRACE_ON(d3d7))
+    if (TRACE_ON(ddraw))
     {
         TRACE(" info requested : ");
         switch (DevInfoID)
@@ -6617,7 +6687,7 @@ IDirect3DDeviceImpl_7_GetInfo(IDirect3DDevice7 *iface,
  * Device created with DDSCL_FPUPRESERVE - resets and restores FPU mode when necessary in
  * d3d calls (FPU may be in a mode non-suitable for d3d when the app calls d3d). Required
  * by Sacrifice (game). */
-const IDirect3DDevice7Vtbl IDirect3DDevice7_FPUSetup_Vtbl =
+static const struct IDirect3DDevice7Vtbl d3d_device7_fpu_setup_vtbl =
 {
     /*** IUnknown Methods ***/
     IDirect3DDeviceImpl_7_QueryInterface,
@@ -6672,7 +6742,7 @@ const IDirect3DDevice7Vtbl IDirect3DDevice7_FPUSetup_Vtbl =
     IDirect3DDeviceImpl_7_GetInfo
 };
 
-const IDirect3DDevice7Vtbl IDirect3DDevice7_FPUPreserve_Vtbl =
+static const struct IDirect3DDevice7Vtbl d3d_device7_fpu_preserve_vtbl =
 {
     /*** IUnknown Methods ***/
     IDirect3DDeviceImpl_7_QueryInterface,
@@ -6727,7 +6797,7 @@ const IDirect3DDevice7Vtbl IDirect3DDevice7_FPUPreserve_Vtbl =
     IDirect3DDeviceImpl_7_GetInfo
 };
 
-const IDirect3DDevice3Vtbl IDirect3DDevice3_Vtbl =
+static const struct IDirect3DDevice3Vtbl d3d_device3_vtbl =
 {
     /*** IUnknown Methods ***/
     Thunk_IDirect3DDeviceImpl_3_QueryInterface,
@@ -6775,7 +6845,7 @@ const IDirect3DDevice3Vtbl IDirect3DDevice3_Vtbl =
     Thunk_IDirect3DDeviceImpl_3_ValidateDevice
 };
 
-const IDirect3DDevice2Vtbl IDirect3DDevice2_Vtbl =
+static const struct IDirect3DDevice2Vtbl d3d_device2_vtbl =
 {
     /*** IUnknown Methods ***/
     Thunk_IDirect3DDeviceImpl_2_QueryInterface,
@@ -6814,7 +6884,7 @@ const IDirect3DDevice2Vtbl IDirect3DDevice2_Vtbl =
     Thunk_IDirect3DDeviceImpl_2_GetClipStatus
 };
 
-const IDirect3DDeviceVtbl IDirect3DDevice1_Vtbl =
+static const struct IDirect3DDeviceVtbl d3d_device1_vtbl =
 {
     /*** IUnknown Methods ***/
     Thunk_IDirect3DDeviceImpl_1_QueryInterface,
@@ -6842,71 +6912,6 @@ const IDirect3DDeviceVtbl IDirect3DDevice1_Vtbl =
     Thunk_IDirect3DDeviceImpl_1_GetDirect3D
 };
 
-/*****************************************************************************
- * IDirect3DDeviceImpl_CreateHandle
- *
- * Not called from the VTable
- *
- * Some older interface versions operate with handles, which are basically
- * DWORDs which identify an interface, for example
- * IDirect3DDevice::SetRenderState with DIRECT3DRENDERSTATE_TEXTUREHANDLE
- *
- * Those handle could be just casts to the interface pointers or vice versa,
- * but that is not 64 bit safe and would mean blindly derefering a DWORD
- * passed by the app. Instead there is a dynamic array in the device which
- * keeps a DWORD to pointer information and a type for the handle.
- *
- * Basically this array only grows, when a handle is freed its pointer is
- * just set to NULL. There will be much more reads from the array than
- * insertion operations, so a dynamic array is fine.
- *
- * Params:
- *  This: D3DDevice implementation for which this handle should be created
- *
- * Returns:
- *  A free handle on success
- *  0 on failure
- *
- *****************************************************************************/
-DWORD
-IDirect3DDeviceImpl_CreateHandle(IDirect3DDeviceImpl *This)
-{
-    DWORD i;
-    struct HandleEntry *oldHandles = This->Handles;
-
-    TRACE("(%p)\n", This);
-
-    for(i = 0; i < This->numHandles; i++)
-    {
-        if(This->Handles[i].ptr == NULL &&
-           This->Handles[i].type == DDrawHandle_Unknown)
-        {
-            TRACE("Reusing freed handle %d\n", i + 1);
-            return i + 1;
-        }
-    }
-
-    TRACE("Growing the handle array\n");
-
-    This->numHandles++;
-    This->Handles = HeapAlloc(GetProcessHeap(), 0, sizeof(struct HandleEntry) * This->numHandles);
-    if(!This->Handles)
-    {
-        ERR("Out of memory\n");
-        This->Handles = oldHandles;
-        This->numHandles--;
-        return 0;
-    }
-    if(oldHandles)
-    {
-        memcpy(This->Handles, oldHandles, (This->numHandles - 1) * sizeof(struct HandleEntry));
-        HeapFree(GetProcessHeap(), 0, oldHandles);
-    }
-
-    TRACE("Returning %d\n", This->numHandles);
-    return This->numHandles;
-}
-
 /*****************************************************************************
  * IDirect3DDeviceImpl_UpdateDepthStencil
  *
@@ -6941,3 +6946,85 @@ IDirect3DDeviceImpl_UpdateDepthStencil(IDirect3DDeviceImpl *This)
     IDirectDrawSurface7_Release(depthStencil);
     return WINED3DZB_TRUE;
 }
+
+HRESULT d3d_device_init(IDirect3DDeviceImpl *device, IDirectDrawImpl *ddraw, IDirectDrawSurfaceImpl *target)
+{
+    IParentImpl *index_buffer_parent;
+    HRESULT hr;
+
+    if (ddraw->cooperative_level & DDSCL_FPUPRESERVE)
+        device->lpVtbl = &d3d_device7_fpu_preserve_vtbl;
+    else
+        device->lpVtbl = &d3d_device7_fpu_setup_vtbl;
+
+    device->IDirect3DDevice3_vtbl = &d3d_device3_vtbl;
+    device->IDirect3DDevice2_vtbl = &d3d_device2_vtbl;
+    device->IDirect3DDevice_vtbl = &d3d_device1_vtbl;
+    device->ref = 1;
+    device->ddraw = ddraw;
+    device->target = target;
+
+    if (!ddraw_handle_table_init(&device->handle_table, 64))
+    {
+        ERR("Failed to initialize handle table.\n");
+        return DDERR_OUTOFMEMORY;
+    }
+
+    device->legacyTextureBlending = FALSE;
+
+    /* Create an index buffer, it's needed for indexed drawing */
+    index_buffer_parent = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*index_buffer_parent));
+    if (!index_buffer_parent)
+    {
+        ERR("Failed to allocate index buffer parent memory.\n");
+        ddraw_handle_table_destroy(&device->handle_table);
+        return DDERR_OUTOFMEMORY;
+    }
+
+    ddraw_parent_init(index_buffer_parent);
+
+    hr = IWineD3DDevice_CreateIndexBuffer(ddraw->wineD3DDevice, 0x40000 /* Length. Don't know how long it should be */,
+            WINED3DUSAGE_DYNAMIC /* Usage */, WINED3DPOOL_DEFAULT, index_buffer_parent,
+            &ddraw_null_wined3d_parent_ops, &device->indexbuffer);
+    if (FAILED(hr))
+    {
+        ERR("Failed to create an index buffer, hr %#x.\n", hr);
+        HeapFree(GetProcessHeap(), 0, index_buffer_parent);
+        ddraw_handle_table_destroy(&device->handle_table);
+        return hr;
+    }
+    index_buffer_parent->child = (IUnknown *)device->indexbuffer;
+
+    /* This is for convenience. */
+    device->wineD3DDevice = ddraw->wineD3DDevice;
+    IWineD3DDevice_AddRef(ddraw->wineD3DDevice);
+
+    /* Render to the back buffer */
+    hr = IWineD3DDevice_SetRenderTarget(ddraw->wineD3DDevice, 0, target->WineD3DSurface, TRUE);
+    if (FAILED(hr))
+    {
+        ERR("Failed to set render target, hr %#x.\n", hr);
+        IParent_Release((IParent *)index_buffer_parent);
+        ddraw_handle_table_destroy(&device->handle_table);
+        return hr;
+    }
+
+    /* FIXME: This is broken. The target AddRef() makes some sense, because
+     * we store a pointer during initialization, but then that's also where
+     * the AddRef() should be. We don't store ddraw->d3d_target anywhere. */
+    /* AddRef the render target. Also AddRef the render target from ddraw,
+     * because if it is released before the app releases the D3D device, the
+     * D3D capabilities of wined3d will be uninitialized, which has bad effects.
+     *
+     * In most cases, those surfaces are the same anyway, but this will simply
+     * add another ref which is released when the device is destroyed. */
+    IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)target);
+    IDirectDrawSurface7_AddRef((IDirectDrawSurface7 *)ddraw->d3d_target);
+
+    ddraw->d3ddevice = device;
+
+    IWineD3DDevice_SetRenderState(ddraw->wineD3DDevice, WINED3DRS_ZENABLE,
+            IDirect3DDeviceImpl_UpdateDepthStencil(device));
+
+    return D3D_OK;
+}