Makes sure any internal reference counting is passed onto the parent,
any objects referenced by the internal stateblock are released when
the stateblock is released (we don't reference count while a
stateblock is recording, so recorded stateblocks have no references to
clean up).

diff --git a/dlls/d3d9/stateblock.c b/dlls/d3d9/stateblock.c
index 870b6e4..8f3cfc3 100644
--- a/dlls/d3d9/stateblock.c
+++ b/dlls/d3d9/stateblock.c
@@ -112,7 +112,7 @@
    object->lpVtbl = &Direct3DStateBlock9_Vtbl;
    object->ref = 1;
    
-   hrc=IWineD3DDevice_CreateStateBlock(This->WineD3DDevice,Type,&object->wineD3DStateBlock,(IUnknown*)object);
+   hrc=IWineD3DDevice_CreateStateBlock(This->WineD3DDevice, (WINED3DSTATEBLOCKTYPE)Type, &object->wineD3DStateBlock, (IUnknown*)object);
    if(hrc != D3D_OK){
        FIXME("(%p) Call to IWineD3DDevice_CreateStateBlock failed.\n", This);
        HeapFree(GetProcessHeap(), 0, object);
diff --git a/dlls/wined3d/basetexture.c b/dlls/wined3d/basetexture.c
index 0de9596..abdd4e2 100644
--- a/dlls/wined3d/basetexture.c
+++ b/dlls/wined3d/basetexture.c
@@ -49,7 +49,6 @@
     ULONG ref = InterlockedIncrement(&This->resource.ref);
 
     TRACE("(%p) : AddRef increasing from %ld\n", This,ref - 1);
-    IUnknown_AddRef(This->resource.parent);
     return ref;
 }
 
@@ -60,8 +59,6 @@
     if (ref == 0) {
         IWineD3DBaseTextureImpl_CleanUp(iface);
         HeapFree(GetProcessHeap(), 0, This);
-    } else {
-        IUnknown_Release(This->resource.parent);  /* Released the reference to the d3dx object */
     }
     return ref;
 }
diff --git a/dlls/wined3d/device.c b/dlls/wined3d/device.c
index f8c2e63..90c6878 100644
--- a/dlls/wined3d/device.c
+++ b/dlls/wined3d/device.c
@@ -227,7 +227,7 @@
     BOOL changeTexture = TRUE;
 
     TRACE("-----------------------> Updating the texture at stage %ld to have new texture state information\n", Stage);
-    for (i = 1; i < HIGHEST_TEXTURE_STATE; i++) {
+    for (i = 1; i < WINED3D_HIGHEST_TEXTURE_STATE; i++) {
 
         BOOL skip = FALSE;
 
@@ -283,7 +283,7 @@
     }
 
     /* apply the sampler states to the texture */
-    for (i = 1; i <= HIGHEST_SAMPLER_STATE;i++) {
+    for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE;i++) {
       IWineD3DDevice_SetSamplerState(iface, Stage, i, This->stateBlock->samplerState[Stage][i]);
     }
 
@@ -419,7 +419,7 @@
     return D3D_OK;
 }
 
-HRESULT WINAPI IWineD3DDeviceImpl_CreateStateBlock(IWineD3DDevice* iface, D3DSTATEBLOCKTYPE Type, IWineD3DStateBlock** ppStateBlock, IUnknown *parent) {
+HRESULT WINAPI IWineD3DDeviceImpl_CreateStateBlock(IWineD3DDevice* iface, WINED3DSTATEBLOCKTYPE Type, IWineD3DStateBlock** ppStateBlock, IUnknown *parent) {
 
     IWineD3DDeviceImpl     *This = (IWineD3DDeviceImpl *)iface;
     IWineD3DStateBlockImpl *object;
@@ -430,7 +430,7 @@
 
     /* Special case - Used during initialization to produce a placeholder stateblock
           so other functions called can update a state block                         */
-    if (Type == (D3DSTATEBLOCKTYPE) 0) {
+    if (Type == WINED3DSBT_INIT) {
         /* Don't bother increasing the reference count otherwise a device will never
            be freed due to circular dependencies                                   */
         return D3D_OK;
@@ -452,10 +452,10 @@
 
     TRACE("Updating changed flags appropriate for type %d\n", Type);
 
-    if (Type == D3DSBT_ALL) {
+    if (Type == WINED3DSBT_ALL) {
         TRACE("ALL => Pretend everything has changed\n");
         memset(&object->changed, TRUE, sizeof(This->stateBlock->changed));
-    } else if (Type == D3DSBT_PIXELSTATE) {
+    } else if (Type == WINED3DSBT_PIXELSTATE) {
 
         memset(&object->changed, FALSE, sizeof(This->stateBlock->changed));
         /* TODO: Pixel Shader Constants */
@@ -475,7 +475,7 @@
                 object->changed.samplerState[j][SavedPixelStates_S[i]] = TRUE;
             }
         }
-    } else if (Type == D3DSBT_VERTEXSTATE) {
+    } else if (Type == WINED3DSBT_VERTEXSTATE) {
 
         memset(&object->changed, FALSE, sizeof(This->stateBlock->changed));
 
@@ -1500,8 +1500,17 @@
     }
 
     /* Not recording... */
-    if (oldSrc != NULL) IWineD3DVertexBuffer_Release(oldSrc);
-    if (pStreamData != NULL) IWineD3DVertexBuffer_AddRef(pStreamData);
+    if (pStreamData != NULL) {
+        IUnknown *newVertexBufferParent;
+        /* GetParent will add a ref, so leave it hanging until the vertex buffer is cleared */
+        IWineD3DVertexBuffer_GetParent(pStreamData, &newVertexBufferParent);
+    }
+    if (oldSrc != NULL) {
+        IUnknown *oldVertexBufferParent;
+        IWineD3DVertexBuffer_GetParent(oldSrc, &oldVertexBufferParent);
+        IUnknown_Release(oldVertexBufferParent);
+        IUnknown_Release(oldVertexBufferParent);
+    }
 
     return D3D_OK;
 }
@@ -2302,8 +2311,17 @@
         return D3D_OK;
     }
 
-    if (oldIdxs)    IWineD3DIndexBuffer_Release(oldIdxs);
-    if (pIndexData) IWineD3DIndexBuffer_AddRef(This->stateBlock->pIndexData);
+    if (pIndexData) {
+        IUnknown *indexBufferParent;
+        /* Getting the parent causes a addRef... it gets released when the indicies are clear */
+        IWineD3DIndexBuffer_GetParent(pIndexData, &indexBufferParent);
+    }
+    if (oldIdxs) {
+        IUnknown *indexBufferParent;
+        IWineD3DIndexBuffer_GetParent(oldIdxs, &indexBufferParent);
+        IUnknown_Release(indexBufferParent);
+        IUnknown_Release(indexBufferParent);
+    }
     return D3D_OK;
 }
 
@@ -3273,8 +3291,8 @@
 
     TRACE("(%p) Sampler(%ld), Type(%d) Value(%ld)\n",This, Sampler ,Type, Value);
 
-    if(Sampler >  GL_LIMITS(samplers) || Sampler < 0 || Type > HIGHEST_SAMPLER_STATE || Type < 0) {
-        FIXME("out of range %d %d sampler %ld type %u\n", GL_LIMITS(samplers), HIGHEST_SAMPLER_STATE, Sampler, Type);
+    if(Sampler >  GL_LIMITS(samplers) || Sampler < 0 || Type > WINED3D_HIGHEST_SAMPLER_STATE || Type < 0) {
+        FIXME("out of range %d %d sampler %ld type %u\n", GL_LIMITS(samplers), WINED3D_HIGHEST_SAMPLER_STATE, Sampler, Type);
         return D3DERR_INVALIDCALL;
     }
 
@@ -4148,8 +4166,13 @@
     * and the the application nust set the texture back to null (or have a leaky application),
     * This means we should pass the refcount upto the parent
      *******************************/
-    if (NULL != oldTexture) {
+    if (NULL != This->updateStateBlock->textures[Stage]) {
+        IUnknown *textureParent;
+        IWineD3DBaseTexture_GetParent(This->updateStateBlock->textures[Stage], (IUnknown **)&textureParent);
+        /** NOTE: GetParent will increase the ref count for me, I won't clean up untill the texture is set to NULL **/
+    }
 
+    if (NULL != oldTexture) {
         IUnknown *textureParent;
         IWineD3DBaseTexture_GetParent(oldTexture, (IUnknown **)&textureParent);
         IUnknown_Release(textureParent);
@@ -4159,10 +4182,6 @@
 
 
     if (NULL != pTexture) {
-        IUnknown *textureParent;
-        IWineD3DBaseTexture_GetParent(This->updateStateBlock->textures[Stage], (IUnknown **)&textureParent);
-        /** NOTE: GetParent will increase the ref count for me, I won't clean up untill the texture is set to NULL **/
-
         /* Now setup the texture appropraitly */
         textureType = IWineD3DBaseTexture_GetType(pTexture);
 
@@ -4327,7 +4346,7 @@
     object->wineD3DDevice= This;
     /** FIXME: object->parent       = parent; **/
     object->parent       = NULL;
-    object->blockType    = D3DSBT_ALL;
+    object->blockType    = WINED3DSBT_ALL;
     object->ref          = 1;
     object->lpVtbl       = &IWineD3DStateBlock_Vtbl;
 
@@ -4596,7 +4615,12 @@
              debug_d3dprimitivetype(PrimitiveType),
              PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
 
-    if (This->stateBlock->streamSource[0] != NULL) IWineD3DVertexBuffer_Release(This->stateBlock->streamSource[0]);
+    if (This->stateBlock->streamSource[0] != NULL) {
+        IUnknown *vertexBufferParent;
+        IWineD3DVertexBuffer_GetParent(This->stateBlock->streamSource[0], &vertexBufferParent);
+        IUnknown_Release(vertexBufferParent);
+        IUnknown_Release(vertexBufferParent);
+    }
 
     /* Note in the following, it's not this type, but that's the purpose of streamIsUP */
     This->stateBlock->streamSource[0] = (IWineD3DVertexBuffer *)pVertexStreamZeroData;
@@ -4623,14 +4647,28 @@
              MinVertexIndex, NumVertexIndices, PrimitiveCount, pIndexData,
              IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
 
-    if (This->stateBlock->streamSource[0] != NULL) IWineD3DVertexBuffer_Release(This->stateBlock->streamSource[0]);
-
     if (IndexDataFormat == WINED3DFMT_INDEX16) {
         idxStride = 2;
     } else {
         idxStride = 4;
     }
 
+    if (This->stateBlock->streamSource[0] != NULL) {
+        IUnknown *vertexBufferParent;
+        IWineD3DVertexBuffer_GetParent(This->stateBlock->streamSource[0], &vertexBufferParent);
+        This->stateBlock->streamSource[0] = NULL;
+        IUnknown_Release(vertexBufferParent);
+        IUnknown_Release(vertexBufferParent);
+    }
+
+    if (This->stateBlock->pIndexData) {
+        IUnknown *indexBufferParent;
+        IWineD3DIndexBuffer_GetParent(This->stateBlock->pIndexData, &indexBufferParent);
+        This->stateBlock->pIndexData = NULL;
+        IUnknown_Release(indexBufferParent);
+        IUnknown_Release(indexBufferParent);
+    }
+
     /* Note in the following, it's not this type, but that's the purpose of streamIsUP */
     This->stateBlock->streamSource[0] = (IWineD3DVertexBuffer *)pVertexStreamZeroData;
     This->stateBlock->streamIsUP = TRUE;
@@ -4641,7 +4679,6 @@
     /* stream zero settings set to null at end as per the msdn */
     This->stateBlock->streamSource[0] = NULL;
     This->stateBlock->streamStride[0] = 0;
-    IWineD3DDevice_SetIndices(iface, NULL, 0);
 
     return D3D_OK;
 }
diff --git a/dlls/wined3d/directx.c b/dlls/wined3d/directx.c
index b0377e4..a5f2eb3 100644
--- a/dlls/wined3d/directx.c
+++ b/dlls/wined3d/directx.c
@@ -1572,12 +1572,16 @@
     /* FIXME: Use for dx8 code eventually too! */
     /* Deliberately no indentation here, as this if will be removed when dx8 support merged in */
     if (This->dxVersion > 8) {
-
+        TRACE("(%p) : Creating stateblock\n", This);
         /* Creating the startup stateBlock - Note Special Case: 0 => Don't fill in yet! */
-        IWineD3DDevice_CreateStateBlock((IWineD3DDevice *)object,
-                                        (D3DSTATEBLOCKTYPE) 0,
+        if (D3D_OK != IWineD3DDevice_CreateStateBlock((IWineD3DDevice *)object,
+                                         WINED3DSBT_INIT,
                                         (IWineD3DStateBlock **)&object->stateBlock,
-                                        NULL);   /* Note: No parent needed for initial internal stateblock */
+                                        NULL)  || NULL == object->stateBlock) {   /* Note: No parent needed for initial internal stateblock */
+            WARN("Failed to create stateblock\n");
+            goto create_device_error;
+        }
+        TRACE("(%p) : Created stateblock (%p) \n", This, object->stateBlock);
         object->updateStateBlock = object->stateBlock;
         IWineD3DStateBlock_AddRef((IWineD3DStateBlock*)object->updateStateBlock);
         /* Setup surfaces for the backbuffer, frontbuffer and depthstencil buffer */
@@ -1585,71 +1589,93 @@
         /* Setup the implicit swapchain */
         TRACE("Creating implicit swapchain\n");
 
-        if (D3D_OK == D3DCB_CreateAdditionalSwapChain((IUnknown *) object->parent, pPresentationParameters, (IWineD3DSwapChain **)&swapchain) && swapchain != NULL) {
-
-            object->renderTarget = swapchain->backBuffer;
-            IWineD3DSurface_AddRef(object->renderTarget);
-            /* Depth Stencil support */
-            object->stencilBufferTarget = object->depthStencilBuffer;
-            if (NULL != object->stencilBufferTarget) {
-                IWineD3DSurface_AddRef(object->stencilBufferTarget);
-            }
-
-            /* Set up some starting GL setup */
-
-            ENTER_GL();
-            /*
-            * Initialize openGL extension related variables
-            *  with Default values
-            */
-
-            This->isGLInfoValid = IWineD3DImpl_FillGLCaps(&This->gl_info, swapchain->display);
-            /* Setup all the devices defaults */
-            IWineD3DStateBlock_InitStartupStateBlock((IWineD3DStateBlock *)object->stateBlock);
-#if 0
-            IWineD3DImpl_CheckGraphicsMemory();
-#endif
-            LEAVE_GL();
-
-            { /* Set a default viewport */
-                D3DVIEWPORT9 vp;
-                vp.X      = 0;
-                vp.Y      = 0;
-                vp.Width  = *(pPresentationParameters->BackBufferWidth);
-                vp.Height = *(pPresentationParameters->BackBufferHeight);
-                vp.MinZ   = 0.0f;
-                vp.MaxZ   = 1.0f;
-                IWineD3DDevice_SetViewport((IWineD3DDevice *)object, &vp);
-            }
-
-
-            /* Initialize the current view state */
-            object->modelview_valid = 1;
-            object->proj_valid = 0;
-            object->view_ident = 1;
-            object->last_was_rhw = 0;
-            glGetIntegerv(GL_MAX_LIGHTS, &object->maxConcurrentLights);
-            TRACE("(%p,%d) All defaults now set up, leaving CreateDevice with %p\n", This, Adapter, object);
-
-            /* Clear the screen */
-            IWineD3DDevice_Clear((IWineD3DDevice *) object, 0, NULL, D3DCLEAR_STENCIL|D3DCLEAR_ZBUFFER|D3DCLEAR_TARGET, 0x00, 1.0, 0);
-        } else { /* couldn't create swapchain */
-            IWineD3DStateBlock_Release((IWineD3DStateBlock *)object->updateStateBlock);
-            object->updateStateBlock = NULL;
-            IWineD3DStateBlock_Release((IWineD3DStateBlock *)object->stateBlock);
-            object->stateBlock = NULL;
-            HeapFree(GetProcessHeap(), 0, object);
-            *ppReturnedDeviceInterface = NULL;
-            return D3DERR_INVALIDCALL;
+        if (D3D_OK != D3DCB_CreateAdditionalSwapChain((IUnknown *) object->parent, pPresentationParameters, (IWineD3DSwapChain **)&swapchain) || swapchain == NULL) {
+            WARN("Failed to create implicite swapchain\n");
+            goto create_device_error;
         }
 
+        object->renderTarget = swapchain->backBuffer;
+        IWineD3DSurface_AddRef(object->renderTarget);
+        /* Depth Stencil support */
+        object->stencilBufferTarget = object->depthStencilBuffer;
+        if (NULL != object->stencilBufferTarget) {
+            IWineD3DSurface_AddRef(object->stencilBufferTarget);
+        }
+
+        /* Set up some starting GL setup */
+        ENTER_GL();
+        /*
+        * Initialize openGL extension related variables
+        *  with Default values
+        */
+
+        This->isGLInfoValid = IWineD3DImpl_FillGLCaps(&This->gl_info, swapchain->display);
+        /* Setup all the devices defaults */
+        IWineD3DStateBlock_InitStartupStateBlock((IWineD3DStateBlock *)object->stateBlock);
+#if 0
+        IWineD3DImpl_CheckGraphicsMemory();
+#endif
+        LEAVE_GL();
+
+        { /* Set a default viewport */
+            D3DVIEWPORT9 vp;
+            vp.X      = 0;
+            vp.Y      = 0;
+            vp.Width  = *(pPresentationParameters->BackBufferWidth);
+            vp.Height = *(pPresentationParameters->BackBufferHeight);
+            vp.MinZ   = 0.0f;
+            vp.MaxZ   = 1.0f;
+            IWineD3DDevice_SetViewport((IWineD3DDevice *)object, &vp);
+        }
+
+
+        /* Initialize the current view state */
+        object->modelview_valid = 1;
+        object->proj_valid = 0;
+        object->view_ident = 1;
+        object->last_was_rhw = 0;
+        glGetIntegerv(GL_MAX_LIGHTS, &object->maxConcurrentLights);
+        TRACE("(%p,%d) All defaults now set up, leaving CreateDevice with %p\n", This, Adapter, object);
+
+        /* Clear the screen */
+        IWineD3DDevice_Clear((IWineD3DDevice *) object, 0, NULL, D3DCLEAR_STENCIL|D3DCLEAR_ZBUFFER|D3DCLEAR_TARGET, 0x00, 1.0, 0);
+
     } else { /* End of FIXME: remove when dx8 merged in */
 
-     FIXME("(%p) Incomplete stub for d3d8\n", This);
+        FIXME("(%p) Incomplete stub for d3d8\n", This);
 
     }
 
     return D3D_OK;
+create_device_error:
+    if (object->updateStateBlock != NULL) {
+        IWineD3DStateBlock_Release((IWineD3DStateBlock *)object->updateStateBlock);
+        object->updateStateBlock = NULL;
+    }
+    if (object->stateBlock != NULL) {
+        IWineD3DStateBlock_Release((IWineD3DStateBlock *)object->stateBlock);
+        object->stateBlock = NULL;
+    }
+    if (object->renderTarget != NULL) {
+        IWineD3DSurface_Release(object->renderTarget);
+        object->renderTarget = NULL;
+    }
+    if (object->stencilBufferTarget != NULL) {
+        IWineD3DSurface_Release(object->stencilBufferTarget);
+        object->stencilBufferTarget = NULL;
+    }
+    if (object->stencilBufferTarget != NULL) {
+        IWineD3DSurface_Release(object->stencilBufferTarget);
+        object->stencilBufferTarget = NULL;
+    }
+    if (swapchain != NULL) {
+        IWineD3DSwapChain_Release((IWineD3DSwapChain *)swapchain);
+        swapchain = NULL;
+    }
+    HeapFree(GetProcessHeap(), 0, object);
+    *ppReturnedDeviceInterface = NULL;
+    return D3DERR_INVALIDCALL;
+
 }
 
 HRESULT WINAPI IWineD3DImpl_GetParent(IWineD3D *iface, IUnknown **pParent) {
diff --git a/dlls/wined3d/stateblock.c b/dlls/wined3d/stateblock.c
index 1502e45..60a5131 100644
--- a/dlls/wined3d/stateblock.c
+++ b/dlls/wined3d/stateblock.c
@@ -57,6 +57,48 @@
     TRACE("(%p) : Releasing from %ld\n", This, refCount + 1);
 
     if (!refCount) {
+        /* type 0 represents the primary stateblock, so free all the resources */
+        if (This->blockType == WINED3DSBT_INIT) {
+            int counter;
+            FIXME("Releasing primary stateblock\n");
+            /* Free any streams still bound */
+            for (counter = 0 ; counter < MAX_STREAMS ; counter++) {
+                if (This->streamSource[counter] != NULL) {
+                    IUnknown *vertexBufferParent;
+                    IWineD3DVertexBuffer_GetParent(This->streamSource[counter], &vertexBufferParent);
+                    /* Set to NULL here so that Device_ResourceReleased can give a warning if This->streamSource[counter] == ResourceReleased */
+                    This->streamSource[counter] = NULL;
+                    IUnknown_Release(vertexBufferParent);
+                    IUnknown_Release(vertexBufferParent);
+                }
+            }
+
+            /* free any index data */
+            if (This->pIndexData) {
+                IUnknown *indexBufferParent;
+                IWineD3DIndexBuffer_GetParent(This->pIndexData, &indexBufferParent);
+                This->pIndexData = NULL;
+                TRACE("Releasing index buffer %p p(%p)", This->pIndexData, indexBufferParent);
+                IUnknown_Release(indexBufferParent);
+                IUnknown_Release(indexBufferParent);
+            }
+
+            /* NOTE: according to MSDN: The applicaion is responsible for making sure the texture references are cleared down */
+            for (counter = 0; counter < GL_LIMITS(textures); counter++) {
+                if (This->textures[counter]) {
+                    IUnknown *textureParent;
+                    IWineD3DBaseTexture_GetParent(This->textures[counter], &textureParent);
+                    /* FIXME: Were not using internal counting properly, so were making up for it here by releasing the object anyway */
+
+                    IUnknown_Release(textureParent);
+                    /* release our 'internal' hold on the texture */
+                    if(0 != IUnknown_Release(textureParent)) {
+                        TRACE("Texture still referenced by stateblock, applications has leaked Stage = %u Texture = %p Parent = %p\n", counter, This->textures[counter], textureParent);
+                    }
+                }
+            }
+
+        }
         HeapFree(GetProcessHeap(), 0, This);
     }
     return refCount;
@@ -242,7 +284,7 @@
         /* FIXME: textures are upto MAX_SAMPLERS for d3d9? */
         /* Texture */
         for (j = 0; j < GL_LIMITS(textures); j++) {
-            for (i = 1; i <= HIGHEST_TEXTURE_STATE ; i++) {
+            for (i = 1; i <= WINED3D_HIGHEST_TEXTURE_STATE ; i++) {
 
                 if (This->set.textureState[j][i] && (This->textureState[j][i] !=
                                                                     targetStateBlock->textureState[j][i])) {
@@ -263,7 +305,7 @@
 
         /* Samplers */
         for (j = 0 ; j < GL_LIMITS(samplers); j++){
-            for (i = 1; i <= HIGHEST_SAMPLER_STATE ; i++){ /* States are 1 based */
+            for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE ; i++){ /* States are 1 based */
                 if (This->set.samplerState[j][i] && (This->samplerState[j][i] !=
                                                         targetStateBlock->samplerState[j][i])) {
                     TRACE("Updating sampler state %d,%d to %ld (was %ld)\n",
@@ -381,7 +423,7 @@
             if (This->set.textures[j] && This->changed.textures[j]) {
                 IWineD3DDevice_SetTexture(pDevice, j, This->textures[j]);
             }
-            for (i = 1; i <= HIGHEST_TEXTURE_STATE; i++) {
+            for (i = 1; i <= WINED3D_HIGHEST_TEXTURE_STATE; i++) {
                 if (This->set.textureState[j][i] && This->changed.textureState[j][i]) {
                     IWineD3DDevice_SetTextureStageState(pDevice, j, i, This->textureState[j][i]);
                 }
@@ -390,7 +432,7 @@
 
         /* Samplers */
         for (j = 0 ; j < GL_LIMITS(samplers); j++){
-            for (i = 1; i <= HIGHEST_SAMPLER_STATE; i++){
+            for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE; i++){
                  if (This->set.samplerState[j][i] && This->changed.samplerState[j][i] && This->samplerState[j][i] != 0) {
                         IWineD3DDevice_SetSamplerState(pDevice, j, i, This->samplerState[j][i]);
                  }
@@ -456,10 +498,11 @@
     /* Note this may have a large overhead but it should only be executed
        once, in order to initialize the complete state of the device and
        all opengl equivalents                                            */
-    TRACE("-----------------------> Setting up device defaults...\n");
-    This->blockType = D3DSBT_ALL;
+    TRACE("(%p) -----------------------> Setting up device defaults... %p \n", This, This->wineD3DDevice);
+    /* TODO: make a special stateblock type for the primary stateblock (it never gets applied so it doesn't need a real type) */
+    This->blockType = WINED3DSBT_INIT;
 
-    /* FIXME: Set some of the defaults for lights, transforms etc */
+    /* Set some of the defaults for lights, transforms etc */
     memcpy(&This->transforms[D3DTS_PROJECTION], &identity, sizeof(identity));
     memcpy(&This->transforms[D3DTS_VIEW], &identity, sizeof(identity));
     for (i = 0; i < 256; ++i) {
@@ -633,7 +676,7 @@
     }
 
         /* Sampler states*/
-    for (i = 0 ; i <  MAX_SAMPLERS; i++) {
+    for (i = 0 ; i <  GL_LIMITS(samplers); i++) {
         TRACE("Setting up default samplers states for sampler %d\n", i);
         This->samplerState[i][WINED3DSAMP_ADDRESSU         ] = D3DTADDRESS_WRAP;
         This->samplerState[i][WINED3DSAMP_ADDRESSV         ] = D3DTADDRESS_WRAP;
diff --git a/dlls/wined3d/volumetexture.c b/dlls/wined3d/volumetexture.c
index 3ca1bdb..2204c88 100644
--- a/dlls/wined3d/volumetexture.c
+++ b/dlls/wined3d/volumetexture.c
@@ -47,7 +47,6 @@
 ULONG WINAPI IWineD3DVolumeTextureImpl_AddRef(IWineD3DVolumeTexture *iface) {
     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
     TRACE("(%p) : AddRef increasing from %ld\n", This, This->resource.ref);
-    IUnknown_AddRef(This->resource.parent);
     return InterlockedIncrement(&This->resource.ref);
 }
 
@@ -66,8 +65,6 @@
         }
         IWineD3DBaseTextureImpl_CleanUp((IWineD3DBaseTexture *) iface);
         HeapFree(GetProcessHeap(), 0, This);
-    } else {
-        IUnknown_Release(This->resource.parent);  /* Released the reference to the d3dx object */
     }
     return ref;
 }
diff --git a/dlls/wined3d/wined3d_private.h b/dlls/wined3d/wined3d_private.h
index 5f8de97..10d753d 100644
--- a/dlls/wined3d/wined3d_private.h
+++ b/dlls/wined3d/wined3d_private.h
@@ -170,10 +170,6 @@
                          /* Maximum number of constants provided to the shaders */
 #define HIGHEST_TRANSFORMSTATE 512 
                          /* Highest value in D3DTRANSFORMSTATETYPE */
-#define HIGHEST_TEXTURE_STATE   D3DTSS_CONSTANT
-                         /* Highest D3DTSS_ value                  */
-#define HIGHEST_SAMPLER_STATE   D3DSAMP_DMAPOFFSET
-                         /* Maximum number of constants provided to the shaders */
 #define MAX_CLIPPLANES  D3DMAXUSERCLIPPLANES
 
 #define MAX_PALETTES      256
@@ -781,9 +777,9 @@
         BOOL                      transform[HIGHEST_TRANSFORMSTATE + 1];
         BOOL                      viewport;
         BOOL                      renderState[WINEHIGHEST_RENDER_STATE + 1];
-        BOOL                      textureState[MAX_TEXTURES][HIGHEST_TEXTURE_STATE + 1];
+        BOOL                      textureState[MAX_TEXTURES][WINED3D_HIGHEST_TEXTURE_STATE + 1];
         BOOL                      clipplane[MAX_CLIPPLANES];
-        BOOL                      samplerState[MAX_SAMPLERS][HIGHEST_SAMPLER_STATE + 1];
+        BOOL                      samplerState[MAX_SAMPLERS][WINED3D_HIGHEST_SAMPLER_STATE + 1];
         BOOL                      vertexDecl;
         BOOL                      pixelShader;
         BOOL                      vertexShader;
@@ -798,7 +794,7 @@
     /* IWineD3DStateBlock information */
     IUnknown                 *parent;
     IWineD3DDeviceImpl       *wineD3DDevice;
-    D3DSTATEBLOCKTYPE         blockType;
+    WINED3DSTATEBLOCKTYPE     blockType;
 
     /* Array indicating whether things have been set or changed */
     SAVEDSTATES               changed;
@@ -854,9 +850,9 @@
     int                       textureDimensions[MAX_SAMPLERS];
 
     /* Texture State Stage */
-    DWORD                     textureState[MAX_TEXTURES][HIGHEST_TEXTURE_STATE + 1];
+    DWORD                     textureState[MAX_TEXTURES][WINED3D_HIGHEST_TEXTURE_STATE + 1];
     /* Sampler States */
-    DWORD                     samplerState[MAX_SAMPLERS][HIGHEST_SAMPLER_STATE + 1];
+    DWORD                     samplerState[MAX_SAMPLERS][WINED3D_HIGHEST_SAMPLER_STATE + 1];
 
 };
 
diff --git a/include/wine/wined3d_interface.h b/include/wine/wined3d_interface.h
index 4f0c37b..dfc5acb 100644
--- a/include/wine/wined3d_interface.h
+++ b/include/wine/wined3d_interface.h
@@ -273,7 +273,7 @@
     STDMETHOD(GetParent)(THIS_ IUnknown **pParent) PURE;
     STDMETHOD(CreateVertexBuffer)(THIS_ UINT  Length,DWORD  Usage,DWORD  FVF,D3DPOOL  Pool,struct IWineD3DVertexBuffer **ppVertexBuffer, HANDLE *sharedHandle, IUnknown *parent) PURE;
     STDMETHOD(CreateIndexBuffer)(THIS_ UINT Length, DWORD Usage, WINED3DFORMAT Format, D3DPOOL Pool, struct IWineD3DIndexBuffer** ppIndexBuffer, HANDLE* pSharedHandle, IUnknown *parent) PURE;
-    STDMETHOD(CreateStateBlock)(THIS_ D3DSTATEBLOCKTYPE Type, struct IWineD3DStateBlock **ppStateBlock, IUnknown *parent) PURE;
+    STDMETHOD(CreateStateBlock)(THIS_ WINED3DSTATEBLOCKTYPE Type, struct IWineD3DStateBlock **ppStateBlock, IUnknown *parent) PURE;
     STDMETHOD(CreateSurface)(THIS_ UINT Width, UINT Height, WINED3DFORMAT Format,  BOOL Lockable, BOOL Discard, UINT Level,  struct IWineD3DSurface** ppSurface, D3DRESOURCETYPE Type, DWORD Usage, D3DPOOL Pool, D3DMULTISAMPLE_TYPE MultiSample ,DWORD MultisampleQuality,  HANDLE* pSharedHandle, IUnknown *parent) PURE;    
     STDMETHOD(CreateTexture)(THIS_ UINT Width, UINT Height, UINT Levels, DWORD Usage, WINED3DFORMAT Format, D3DPOOL Pool, struct IWineD3DTexture** ppTexture, HANDLE* pSharedHandle, IUnknown *parent, D3DCB_CREATESURFACEFN pFn) PURE;
     STDMETHOD(CreateVolumeTexture)(THIS_ UINT Width, UINT Height, UINT Depth, UINT Levels, DWORD Usage, WINED3DFORMAT Format, D3DPOOL Pool, struct IWineD3DVolumeTexture** ppVolumeTexture, HANDLE* pSharedHandle, IUnknown *parent, D3DCB_CREATEVOLUMEFN pFn) PURE;
diff --git a/include/wine/wined3d_types.h b/include/wine/wined3d_types.h
index 5bbd955..dd4f1fd 100644
--- a/include/wine/wined3d_types.h
+++ b/include/wine/wined3d_types.h
@@ -343,9 +343,10 @@
     WINED3DSAMP_SRGBTEXTURE    = 11,
     WINED3DSAMP_ELEMENTINDEX   = 12,
     WINED3DSAMP_DMAPOFFSET     = 13,
-                                
+
     WINED3DSAMP_FORCE_DWORD   = 0x7fffffff,
 } WINED3DSAMPLERSTATETYPE;
+#define WINED3D_HIGHEST_SAMPLER_STATE WINED3DSAMP_DMAPOFFSET
 
 typedef enum _WINED3DTEXTURESTAGESTATETYPE {
     WINED3DTSS_COLOROP               =  1,
@@ -371,6 +372,8 @@
     WINED3DTSS_FORCE_DWORD           = 0x7fffffff
 } WINED3DTEXTURESTAGESTATETYPE;
 
+#define WINED3D_HIGHEST_TEXTURE_STATE WINED3DTSS_CONSTANT
+
 typedef struct _WINEDD3DRECTPATCH_INFO {
     UINT                StartVertexOffsetWidth;
     UINT                StartVertexOffsetHeight;
@@ -674,6 +677,15 @@
 
 } WINED3DCAPS;
 
+typedef enum _WINED3DSTATEBLOCKTYPE {
+    WINED3DSBT_INIT          = 0,
+    WINED3DSBT_ALL           = 1,
+    WINED3DSBT_PIXELSTATE    = 2,
+    WINED3DSBT_VERTEXSTATE   = 3,
+
+    WINED3DSBT_FORCE_DWORD   = 0xffffffff
+} WINED3DSTATEBLOCKTYPE;
+
 typedef struct glDescriptor {
     UINT          textureName;
     int           level;
@@ -683,4 +695,6 @@
     int/*GLenum*/ glType;
 } glDescriptor;
 
+
+
 #endif