wined3d: Merge device_unload_resource() and reset_unload_resources().
[wine] / dlls / wined3d / basetexture.c
1 /*
2  * IWineD3DBaseTexture Implementation
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  * Copyright 2002-2004 Raphael Junqueira
6  * Copyright 2005 Oliver Stieber
7  * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8  * Copyright 2009-2010 Henri Verbeet for CodeWeavers
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26 #include "wined3d_private.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
29
30 HRESULT basetexture_init(IWineD3DBaseTextureImpl *texture, const struct wined3d_texture_ops *texture_ops,
31         UINT layer_count, UINT level_count, WINED3DRESOURCETYPE resource_type, IWineD3DDeviceImpl *device,
32         DWORD usage, const struct wined3d_format *format, WINED3DPOOL pool, void *parent,
33         const struct wined3d_parent_ops *parent_ops)
34 {
35     HRESULT hr;
36
37     hr = resource_init((IWineD3DResourceImpl *)texture, resource_type, device,
38             0, usage, format, pool, parent, parent_ops);
39     if (FAILED(hr))
40     {
41         WARN("Failed to initialize resource, returning %#x\n", hr);
42         return hr;
43     }
44
45     texture->baseTexture.texture_ops = texture_ops;
46     texture->baseTexture.sub_resources = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
47             level_count * layer_count * sizeof(*texture->baseTexture.sub_resources));
48     if (!texture->baseTexture.sub_resources)
49     {
50         ERR("Failed to allocate sub-resource array.\n");
51         resource_cleanup((IWineD3DResourceImpl *)texture);
52         return E_OUTOFMEMORY;
53     }
54
55     texture->baseTexture.layer_count = layer_count;
56     texture->baseTexture.level_count = level_count;
57     texture->baseTexture.filterType = (usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3DTEXF_LINEAR : WINED3DTEXF_NONE;
58     texture->baseTexture.LOD = 0;
59     texture->baseTexture.texture_rgb.dirty = TRUE;
60     texture->baseTexture.texture_srgb.dirty = TRUE;
61     texture->baseTexture.is_srgb = FALSE;
62     texture->baseTexture.pow2Matrix_identity = TRUE;
63
64     if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
65     {
66         texture->baseTexture.minMipLookup = minMipLookup;
67         texture->baseTexture.magLookup = magLookup;
68     }
69     else
70     {
71         texture->baseTexture.minMipLookup = minMipLookup_noFilter;
72         texture->baseTexture.magLookup = magLookup_noFilter;
73     }
74
75     return WINED3D_OK;
76 }
77
78 void basetexture_cleanup(IWineD3DBaseTextureImpl *texture)
79 {
80     basetexture_unload(texture);
81     HeapFree(GetProcessHeap(), 0, texture->baseTexture.sub_resources);
82     resource_cleanup((IWineD3DResourceImpl *)texture);
83 }
84
85 IWineD3DResourceImpl *basetexture_get_sub_resource(IWineD3DBaseTextureImpl *texture, UINT sub_resource_idx)
86 {
87     UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
88
89     if (sub_resource_idx >= sub_count)
90     {
91         WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
92         return NULL;
93     }
94
95     return texture->baseTexture.sub_resources[sub_resource_idx];
96 }
97
98 /* A GL context is provided by the caller */
99 static void gltexture_delete(struct gl_texture *tex)
100 {
101     ENTER_GL();
102     glDeleteTextures(1, &tex->name);
103     LEAVE_GL();
104     tex->name = 0;
105 }
106
107 void basetexture_unload(IWineD3DBaseTextureImpl *texture)
108 {
109     IWineD3DDeviceImpl *device = texture->resource.device;
110     struct wined3d_context *context = NULL;
111
112     if (texture->baseTexture.texture_rgb.name || texture->baseTexture.texture_srgb.name)
113     {
114         context = context_acquire(device, NULL);
115     }
116
117     if (texture->baseTexture.texture_rgb.name)
118         gltexture_delete(&texture->baseTexture.texture_rgb);
119
120     if (texture->baseTexture.texture_srgb.name)
121         gltexture_delete(&texture->baseTexture.texture_srgb);
122
123     if (context) context_release(context);
124
125     texture->baseTexture.texture_rgb.dirty = TRUE;
126     texture->baseTexture.texture_srgb.dirty = TRUE;
127
128     resource_unload((IWineD3DResourceImpl *)texture);
129 }
130
131 DWORD basetexture_set_lod(IWineD3DBaseTextureImpl *texture, DWORD lod)
132 {
133     DWORD old = texture->baseTexture.LOD;
134
135     TRACE("texture %p, lod %u.\n", texture, lod);
136
137     /* The d3d9:texture test shows that SetLOD is ignored on non-managed
138      * textures. The call always returns 0, and GetLOD always returns 0. */
139     if (texture->resource.pool != WINED3DPOOL_MANAGED)
140     {
141         TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
142         return 0;
143     }
144
145     if (lod >= texture->baseTexture.level_count)
146         lod = texture->baseTexture.level_count - 1;
147
148     if (texture->baseTexture.LOD != lod)
149     {
150         texture->baseTexture.LOD = lod;
151
152         texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
153         texture->baseTexture.texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
154         if (texture->baseTexture.bindCount)
155             IWineD3DDeviceImpl_MarkStateDirty(texture->resource.device, STATE_SAMPLER(texture->baseTexture.sampler));
156     }
157
158     return old;
159 }
160
161 DWORD basetexture_get_lod(IWineD3DBaseTextureImpl *texture)
162 {
163     TRACE("texture %p, returning %u.\n", texture, texture->baseTexture.LOD);
164
165     return texture->baseTexture.LOD;
166 }
167
168 DWORD basetexture_get_level_count(IWineD3DBaseTextureImpl *texture)
169 {
170     TRACE("texture %p, returning %u.\n", texture, texture->baseTexture.level_count);
171     return texture->baseTexture.level_count;
172 }
173
174 HRESULT basetexture_set_autogen_filter_type(IWineD3DBaseTextureImpl *texture, WINED3DTEXTUREFILTERTYPE filter_type)
175 {
176     TRACE("texture %p, filter_type %s.\n", texture, debug_d3dtexturefiltertype(filter_type));
177
178     if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
179     {
180         WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
181         return WINED3DERR_INVALIDCALL;
182     }
183
184     if (texture->baseTexture.filterType != filter_type)
185     {
186         GLenum target = texture->baseTexture.target;
187         struct wined3d_context *context;
188
189         context = context_acquire(texture->resource.device, NULL);
190
191         ENTER_GL();
192         glBindTexture(target, texture->baseTexture.texture_rgb.name);
193         checkGLcall("glBindTexture");
194         switch (filter_type)
195         {
196             case WINED3DTEXF_NONE:
197             case WINED3DTEXF_POINT:
198                 glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST);
199                 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST)");
200                 break;
201
202             case WINED3DTEXF_LINEAR:
203                 glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
204                 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
205                 break;
206
207             default:
208                 WARN("Unexpected filter type %#x, setting to GL_NICEST.\n", filter_type);
209                 glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
210                 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
211                 break;
212         }
213         LEAVE_GL();
214
215         context_release(context);
216     }
217     texture->baseTexture.filterType = filter_type;
218
219     return WINED3D_OK;
220 }
221
222 WINED3DTEXTUREFILTERTYPE basetexture_get_autogen_filter_type(IWineD3DBaseTextureImpl *texture)
223 {
224     TRACE("texture %p.\n", texture);
225
226     return texture->baseTexture.filterType;
227 }
228
229 void basetexture_generate_mipmaps(IWineD3DBaseTextureImpl *texture)
230 {
231     /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
232     FIXME("texture %p stub!\n", texture);
233 }
234
235 BOOL basetexture_set_dirty(IWineD3DBaseTextureImpl *texture, BOOL dirty)
236 {
237     BOOL old;
238
239     old = texture->baseTexture.texture_rgb.dirty || texture->baseTexture.texture_srgb.dirty;
240     texture->baseTexture.texture_rgb.dirty = dirty;
241     texture->baseTexture.texture_srgb.dirty = dirty;
242
243     return old;
244 }
245
246 /* Context activation is done by the caller. */
247 HRESULT basetexture_bind(IWineD3DBaseTextureImpl *texture, BOOL srgb, BOOL *set_surface_desc)
248 {
249     HRESULT hr = WINED3D_OK;
250     GLenum textureDimensions;
251     BOOL isNewTexture = FALSE;
252     struct gl_texture *gl_tex;
253
254     TRACE("texture %p, srgb %#x, set_surface_desc %p.\n", texture, srgb, set_surface_desc);
255
256     texture->baseTexture.is_srgb = srgb; /* SRGB mode cache for PreLoad calls outside drawprim */
257     if (srgb)
258         gl_tex = &texture->baseTexture.texture_srgb;
259     else
260         gl_tex = &texture->baseTexture.texture_rgb;
261
262     textureDimensions = texture->baseTexture.target;
263
264     ENTER_GL();
265     /* Generate a texture name if we don't already have one */
266     if (!gl_tex->name)
267     {
268         *set_surface_desc = TRUE;
269         glGenTextures(1, &gl_tex->name);
270         checkGLcall("glGenTextures");
271         TRACE("Generated texture %d\n", gl_tex->name);
272         if (texture->resource.pool == WINED3DPOOL_DEFAULT)
273         {
274             /* Tell opengl to try and keep this texture in video ram (well mostly) */
275             GLclampf tmp;
276             tmp = 0.9f;
277             glPrioritizeTextures(1, &gl_tex->name, &tmp);
278
279         }
280         /* Initialise the state of the texture object
281         to the openGL defaults, not the directx defaults */
282         gl_tex->states[WINED3DTEXSTA_ADDRESSU]      = WINED3DTADDRESS_WRAP;
283         gl_tex->states[WINED3DTEXSTA_ADDRESSV]      = WINED3DTADDRESS_WRAP;
284         gl_tex->states[WINED3DTEXSTA_ADDRESSW]      = WINED3DTADDRESS_WRAP;
285         gl_tex->states[WINED3DTEXSTA_BORDERCOLOR]   = 0;
286         gl_tex->states[WINED3DTEXSTA_MAGFILTER]     = WINED3DTEXF_LINEAR;
287         gl_tex->states[WINED3DTEXSTA_MINFILTER]     = WINED3DTEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
288         gl_tex->states[WINED3DTEXSTA_MIPFILTER]     = WINED3DTEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
289         gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL]   = 0;
290         gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
291         gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE]   = 0;
292         gl_tex->states[WINED3DTEXSTA_SHADOW]        = FALSE;
293         basetexture_set_dirty(texture, TRUE);
294         isNewTexture = TRUE;
295
296         if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
297         {
298             /* This means double binding the texture at creation, but keeps the code simpler all
299              * in all, and the run-time path free from additional checks
300              */
301             glBindTexture(textureDimensions, gl_tex->name);
302             checkGLcall("glBindTexture");
303             glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
304             checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
305         }
306     } else {
307         *set_surface_desc = FALSE;
308     }
309
310     /* Bind the texture */
311     if (gl_tex->name)
312     {
313         glBindTexture(textureDimensions, gl_tex->name);
314         checkGLcall("glBindTexture");
315         if (isNewTexture) {
316             /* For a new texture we have to set the textures levels after binding the texture.
317              * In theory this is all we should ever have to do, but because ATI's drivers are broken, we
318              * also need to set the texture dimensions before the texture is set
319              * Beware that texture rectangles do not support mipmapping, but set the maxmiplevel if we're
320              * relying on the partial GL_ARB_texture_non_power_of_two emulation with texture rectangles
321              * (ie, do not care for cond_np2 here, just look for GL_TEXTURE_RECTANGLE_ARB)
322              */
323             if (textureDimensions != GL_TEXTURE_RECTANGLE_ARB)
324             {
325                 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->baseTexture.level_count - 1);
326                 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, texture->baseTexture.level_count - 1);
327                 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, texture->baseTexture.level_count)");
328             }
329             if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
330                 /* Cubemaps are always set to clamp, regardless of the sampler state. */
331                 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
332                 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
333                 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
334             }
335         }
336     } else { /* this only happened if we've run out of openGL textures */
337         WARN("This texture doesn't have an openGL texture assigned to it\n");
338         hr =  WINED3DERR_INVALIDCALL;
339     }
340
341     LEAVE_GL();
342     return hr;
343 }
344
345 /* GL locking is done by the caller */
346 static void apply_wrap(const struct wined3d_gl_info *gl_info, GLenum target,
347         WINED3DTEXTUREADDRESS d3d_wrap, GLenum param, BOOL cond_np2)
348 {
349     GLint gl_wrap;
350
351     if (d3d_wrap < WINED3DTADDRESS_WRAP || d3d_wrap > WINED3DTADDRESS_MIRRORONCE)
352     {
353         FIXME("Unrecognized or unsupported WINED3DTEXTUREADDRESS %#x.\n", d3d_wrap);
354         return;
355     }
356
357     if (target == GL_TEXTURE_CUBE_MAP_ARB
358             || (cond_np2 && d3d_wrap == WINED3DTADDRESS_WRAP))
359     {
360         /* Cubemaps are always set to clamp, regardless of the sampler state. */
361         gl_wrap = GL_CLAMP_TO_EDGE;
362     }
363     else
364     {
365         gl_wrap = gl_info->wrap_lookup[d3d_wrap - WINED3DTADDRESS_WRAP];
366     }
367
368     TRACE("Setting param %#x to %#x for target %#x.\n", param, gl_wrap, target);
369     glTexParameteri(target, param, gl_wrap);
370     checkGLcall("glTexParameteri(target, param, gl_wrap)");
371 }
372
373 /* GL locking is done by the caller (state handler) */
374 void basetexture_apply_state_changes(IWineD3DBaseTextureImpl *texture,
375         const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1],
376         const struct wined3d_gl_info *gl_info)
377 {
378     BOOL cond_np2 = IWineD3DBaseTexture_IsCondNP2((IWineD3DBaseTexture *)texture);
379     GLenum textureDimensions = texture->baseTexture.target;
380     DWORD state;
381     DWORD aniso;
382     struct gl_texture *gl_tex;
383
384     TRACE("texture %p, samplerStates %p\n", texture, samplerStates);
385
386     if (texture->baseTexture.is_srgb)
387         gl_tex = &texture->baseTexture.texture_srgb;
388     else
389         gl_tex = &texture->baseTexture.texture_rgb;
390
391     /* This function relies on the correct texture being bound and loaded. */
392
393     if(samplerStates[WINED3DSAMP_ADDRESSU]      != gl_tex->states[WINED3DTEXSTA_ADDRESSU]) {
394         state = samplerStates[WINED3DSAMP_ADDRESSU];
395         apply_wrap(gl_info, textureDimensions, state, GL_TEXTURE_WRAP_S, cond_np2);
396         gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
397     }
398
399     if(samplerStates[WINED3DSAMP_ADDRESSV]      != gl_tex->states[WINED3DTEXSTA_ADDRESSV]) {
400         state = samplerStates[WINED3DSAMP_ADDRESSV];
401         apply_wrap(gl_info, textureDimensions, state, GL_TEXTURE_WRAP_T, cond_np2);
402         gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
403     }
404
405     if(samplerStates[WINED3DSAMP_ADDRESSW]      != gl_tex->states[WINED3DTEXSTA_ADDRESSW]) {
406         state = samplerStates[WINED3DSAMP_ADDRESSW];
407         apply_wrap(gl_info, textureDimensions, state, GL_TEXTURE_WRAP_R, cond_np2);
408         gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
409     }
410
411     if(samplerStates[WINED3DSAMP_BORDERCOLOR]   != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR]) {
412         float col[4];
413
414         state = samplerStates[WINED3DSAMP_BORDERCOLOR];
415         D3DCOLORTOGLFLOAT4(state, col);
416         TRACE("Setting border color for %u to %x\n", textureDimensions, state);
417         glTexParameterfv(textureDimensions, GL_TEXTURE_BORDER_COLOR, &col[0]);
418         checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
419         gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
420     }
421
422     if(samplerStates[WINED3DSAMP_MAGFILTER]     != gl_tex->states[WINED3DTEXSTA_MAGFILTER]) {
423         GLint glValue;
424         state = samplerStates[WINED3DSAMP_MAGFILTER];
425         if (state > WINED3DTEXF_ANISOTROPIC) {
426             FIXME("Unrecognized or unsupported MAGFILTER* value %d\n", state);
427         }
428
429         glValue = wined3d_gl_mag_filter(texture->baseTexture.magLookup,
430                 min(max(state, WINED3DTEXF_POINT), WINED3DTEXF_LINEAR));
431         TRACE("ValueMAG=%d setting MAGFILTER to %x\n", state, glValue);
432         glTexParameteri(textureDimensions, GL_TEXTURE_MAG_FILTER, glValue);
433
434         gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
435     }
436
437     if((samplerStates[WINED3DSAMP_MINFILTER]     != gl_tex->states[WINED3DTEXSTA_MINFILTER] ||
438         samplerStates[WINED3DSAMP_MIPFILTER]     != gl_tex->states[WINED3DTEXSTA_MIPFILTER] ||
439         samplerStates[WINED3DSAMP_MAXMIPLEVEL]   != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL])) {
440         GLint glValue;
441
442         gl_tex->states[WINED3DTEXSTA_MIPFILTER] = samplerStates[WINED3DSAMP_MIPFILTER];
443         gl_tex->states[WINED3DTEXSTA_MINFILTER] = samplerStates[WINED3DSAMP_MINFILTER];
444         gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = samplerStates[WINED3DSAMP_MAXMIPLEVEL];
445
446         if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC
447             || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_ANISOTROPIC)
448         {
449
450             FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %d D3DSAMP_MIPFILTER value %d\n",
451                   gl_tex->states[WINED3DTEXSTA_MINFILTER],
452                   gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
453         }
454         glValue = wined3d_gl_min_mip_filter(texture->baseTexture.minMipLookup,
455                 min(max(samplerStates[WINED3DSAMP_MINFILTER], WINED3DTEXF_POINT), WINED3DTEXF_LINEAR),
456                 min(max(samplerStates[WINED3DSAMP_MIPFILTER], WINED3DTEXF_NONE), WINED3DTEXF_LINEAR));
457
458         TRACE("ValueMIN=%d, ValueMIP=%d, setting MINFILTER to %x\n",
459               samplerStates[WINED3DSAMP_MINFILTER],
460               samplerStates[WINED3DSAMP_MIPFILTER], glValue);
461         glTexParameteri(textureDimensions, GL_TEXTURE_MIN_FILTER, glValue);
462         checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
463
464         if (!cond_np2)
465         {
466             if (gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE)
467                 glValue = texture->baseTexture.LOD;
468             else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= texture->baseTexture.level_count)
469                 glValue = texture->baseTexture.level_count - 1;
470             else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < texture->baseTexture.LOD)
471                 /* baseTexture.LOD is already clamped in the setter */
472                 glValue = texture->baseTexture.LOD;
473             else
474                 glValue = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
475
476             /* Note that D3DSAMP_MAXMIPLEVEL specifies the biggest mipmap(default 0), while
477              * GL_TEXTURE_MAX_LEVEL specifies the smallest mimap used(default 1000).
478              * So D3DSAMP_MAXMIPLEVEL is the same as GL_TEXTURE_BASE_LEVEL.
479              */
480             glTexParameteri(textureDimensions, GL_TEXTURE_BASE_LEVEL, glValue);
481         }
482     }
483
484     if ((gl_tex->states[WINED3DTEXSTA_MAGFILTER] != WINED3DTEXF_ANISOTROPIC
485          && gl_tex->states[WINED3DTEXSTA_MINFILTER] != WINED3DTEXF_ANISOTROPIC
486          && gl_tex->states[WINED3DTEXSTA_MIPFILTER] != WINED3DTEXF_ANISOTROPIC)
487             || cond_np2)
488     {
489         aniso = 1;
490     }
491     else
492     {
493         aniso = samplerStates[WINED3DSAMP_MAXANISOTROPY];
494     }
495
496     if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
497     {
498         if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
499         {
500             glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
501             checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
502         }
503         else
504         {
505             WARN("Anisotropic filtering not supported.\n");
506         }
507         gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;
508     }
509
510     if (!(texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
511             != !gl_tex->states[WINED3DTEXSTA_SHADOW])
512     {
513         if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
514         {
515             glTexParameteri(textureDimensions, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
516             glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
517             checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
518             gl_tex->states[WINED3DTEXSTA_SHADOW] = TRUE;
519         }
520         else
521         {
522             glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
523             checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
524             gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
525         }
526     }
527 }