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