diff --git a/dlls/wined3d/context.c b/dlls/wined3d/context.c
index 2aec7d6..72e1ae3 100644
--- a/dlls/wined3d/context.c
+++ b/dlls/wined3d/context.c
@@ -895,6 +895,12 @@ static void Context_MarkStateDirty(struct wined3d_context *context, DWORD state,
     DWORD idx;
     BYTE shift;
 
+    if (!rep)
+    {
+        ERR("Tried to make state %#x without rep dirty.\n", state);
+        DebugBreak();
+    }
+
     if (isStateDirty(context, rep)) return;
 
     context->dirtyArray[context->numDirtyEntries++] = rep;
diff --git a/dlls/wined3d/device.c b/dlls/wined3d/device.c
index 34495d3..4bc9702 100644
--- a/dlls/wined3d/device.c
+++ b/dlls/wined3d/device.c
@@ -3095,6 +3095,7 @@ static void device_map_fixed_function_samplers(IWineD3DDeviceImpl *This) {
 static void device_map_psamplers(IWineD3DDeviceImpl *This) {
     const WINED3DSAMPLER_TEXTURE_TYPE *sampler_type =
             ((IWineD3DPixelShaderImpl *)This->stateBlock->pixelShader)->baseShader.reg_maps.sampler_type;
+    const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
     unsigned int i;
 
     for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
@@ -3102,7 +3103,8 @@ static void device_map_psamplers(IWineD3DDeviceImpl *This) {
         {
             device_map_stage(This, i, i);
             IWineD3DDeviceImpl_MarkStateDirty(This, STATE_SAMPLER(i));
-            if (i < MAX_TEXTURES) {
+            if (i < gl_info->limits.texture_stages)
+            {
                 markTextureStagesDirty(This, i);
             }
         }
@@ -3806,11 +3808,14 @@ static HRESULT WINAPI IWineD3DDeviceImpl_ProcessVertices(IWineD3DDevice *iface,
 static HRESULT WINAPI IWineD3DDeviceImpl_SetTextureStageState(IWineD3DDevice *iface, DWORD Stage, WINED3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
     DWORD oldValue = This->updateStateBlock->textureState[Stage][Type];
+    const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
 
     TRACE("(%p) : Stage=%d, Type=%s(%d), Value=%d\n", This, Stage, debug_d3dtexturestate(Type), Type, Value);
 
-    if (Stage >= MAX_TEXTURES) {
-        WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring\n", Stage, MAX_TEXTURES - 1);
+    if (Stage >= gl_info->limits.texture_stages)
+    {
+        WARN("Attempting to set stage %u which is higher than the max stage %u, ignoring.\n",
+                Stage, gl_info->limits.texture_stages - 1);
         return WINED3D_OK;
     }
 
@@ -3891,6 +3896,7 @@ static HRESULT WINAPI IWineD3DDeviceImpl_SetTexture(IWineD3DDevice *iface,
         DWORD stage, IWineD3DBaseTexture *texture)
 {
     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
+    const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
     IWineD3DBaseTexture *prev;
 
     TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
@@ -3949,7 +3955,7 @@ static HRESULT WINAPI IWineD3DDeviceImpl_SetTexture(IWineD3DDevice *iface,
             IWineD3DDeviceImpl_MarkStateDirty(This, STATE_PIXELSHADER);
         }
 
-        if (!prev && stage < MAX_TEXTURES)
+        if (!prev && stage < gl_info->limits.texture_stages)
         {
             /* The source arguments for color and alpha ops have different
              * meanings when a NULL texture is bound, so the COLOROP and
@@ -3968,7 +3974,7 @@ static HRESULT WINAPI IWineD3DDeviceImpl_SetTexture(IWineD3DDevice *iface,
 
         IWineD3DBaseTexture_Release(prev);
 
-        if (!texture && stage < MAX_TEXTURES)
+        if (!texture && stage < gl_info->limits.texture_stages)
         {
             IWineD3DDeviceImpl_MarkStateDirty(This, STATE_TEXTURESTAGE(stage, WINED3DTSS_COLOROP));
             IWineD3DDeviceImpl_MarkStateDirty(This, STATE_TEXTURESTAGE(stage, WINED3DTSS_ALPHAOP));
@@ -7019,6 +7025,12 @@ void IWineD3DDeviceImpl_MarkStateDirty(IWineD3DDeviceImpl *This, DWORD state) {
     BYTE shift;
     UINT i;
 
+    if (!rep)
+    {
+        ERR("Tried to make state %#x without rep dirty.\n", state);
+        DebugBreak();
+    }
+
     for(i = 0; i < This->numContexts; i++) {
         context = This->contexts[i];
         if(isStateDirty(context, rep)) continue;
diff --git a/dlls/wined3d/state.c b/dlls/wined3d/state.c
index adb426b..43cb0df 100644
--- a/dlls/wined3d/state.c
+++ b/dlls/wined3d/state.c
@@ -3597,9 +3597,9 @@ void apply_pixelshader(DWORD state, IWineD3DStateBlockImpl *stateblock, struct w
         }
     } else {
         /* Disabled the pixel shader - color ops weren't applied
-         * while it was enabled, so re-apply them.
-         */
-        for(i=0; i < MAX_TEXTURES; i++) {
+         * while it was enabled, so re-apply them. */
+        for (i = 0; i < context->gl_info->limits.texture_stages; ++i)
+        {
             if(!isStateDirty(context, STATE_TEXTURESTAGE(i, WINED3DTSS_COLOROP))) {
                 device->StateTable[STATE_TEXTURESTAGE(i, WINED3DTSS_COLOROP)].apply
                         (STATE_TEXTURESTAGE(i, WINED3DTSS_COLOROP), stateblock, context);
@@ -5733,6 +5733,7 @@ HRESULT compile_state_table(struct StateEntry *StateTable, APPLYSTATEFUNC **dev_
     APPLYSTATEFUNC multistate_funcs[STATE_HIGHEST + 1][3];
     const struct StateEntryTemplate *cur;
     BOOL set[STATE_HIGHEST + 1];
+    unsigned int start, last;
 
     memset(multistate_funcs, 0, sizeof(multistate_funcs));
 
@@ -5824,6 +5825,14 @@ HRESULT compile_state_table(struct StateEntry *StateTable, APPLYSTATEFUNC **dev_
         }
     }
 
+    start = STATE_TEXTURESTAGE(gl_info->limits.texture_stages, 0);
+    last = STATE_TEXTURESTAGE(MAX_TEXTURES - 1, WINED3D_HIGHEST_TEXTURE_STATE);
+    for (i = start; i <= last; ++i)
+    {
+        StateTable[i].representative = 0;
+        StateTable[i].apply = state_undefined;
+    }
+
     return WINED3D_OK;
 
 out_of_mem:
