wined3d: Track GL texture states in a separate structure.
[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 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 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
30
31 HRESULT basetexture_init(IWineD3DBaseTextureImpl *texture, UINT levels, WINED3DRESOURCETYPE resource_type,
32         IWineD3DDeviceImpl *device, UINT size, DWORD usage, const struct GlPixelFormatDesc *format_desc,
33         WINED3DPOOL pool, IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
34 {
35     HRESULT hr;
36
37     hr = resource_init((IWineD3DResource *)texture, resource_type, device,
38             size, usage, format_desc, 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.levels = levels;
46     texture->baseTexture.filterType = (usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3DTEXF_LINEAR : WINED3DTEXF_NONE;
47     texture->baseTexture.LOD = 0;
48     texture->baseTexture.texture_rgb.dirty = TRUE;
49     texture->baseTexture.texture_srgb.dirty = TRUE;
50     texture->baseTexture.is_srgb = FALSE;
51     texture->baseTexture.pow2Matrix_identity = TRUE;
52
53     if (texture->resource.format_desc->Flags & WINED3DFMT_FLAG_FILTERING)
54     {
55         texture->baseTexture.minMipLookup = minMipLookup;
56         texture->baseTexture.magLookup = magLookup;
57     }
58     else
59     {
60         texture->baseTexture.minMipLookup = minMipLookup_noFilter;
61         texture->baseTexture.magLookup = magLookup_noFilter;
62     }
63
64     return WINED3D_OK;
65 }
66
67 void basetexture_cleanup(IWineD3DBaseTexture *iface)
68 {
69     basetexture_unload(iface);
70     resource_cleanup((IWineD3DResource *)iface);
71 }
72
73 void basetexture_unload(IWineD3DBaseTexture *iface)
74 {
75     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
76     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
77
78     if(This->baseTexture.texture_rgb.name) {
79         ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
80         ENTER_GL();
81         glDeleteTextures(1, &This->baseTexture.texture_rgb.name);
82         This->baseTexture.texture_rgb.name = 0;
83         LEAVE_GL();
84     }
85
86     if(This->baseTexture.texture_srgb.name) {
87         ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
88         ENTER_GL();
89         glDeleteTextures(1, &This->baseTexture.texture_srgb.name);
90         This->baseTexture.texture_srgb.name = 0;
91         LEAVE_GL();
92     }
93     This->baseTexture.texture_rgb.dirty = TRUE;
94     This->baseTexture.texture_srgb.dirty = TRUE;
95 }
96
97 DWORD basetexture_set_lod(IWineD3DBaseTexture *iface, DWORD LODNew)
98 {
99     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
100     DWORD old = This->baseTexture.LOD;
101
102     /* The d3d9:texture test shows that SetLOD is ignored on non-managed
103      * textures. The call always returns 0, and GetLOD always returns 0
104      */
105     if (This->resource.pool != WINED3DPOOL_MANAGED) {
106         TRACE("Ignoring SetLOD on %s texture, returning 0\n", debug_d3dpool(This->resource.pool));
107         return 0;
108     }
109
110     if(LODNew >= This->baseTexture.levels)
111         LODNew = This->baseTexture.levels - 1;
112
113     if(This->baseTexture.LOD != LODNew) {
114         This->baseTexture.LOD = LODNew;
115
116         This->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
117         This->baseTexture.texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
118         if(This->baseTexture.bindCount) {
119             IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(This->baseTexture.sampler));
120         }
121     }
122
123     TRACE("(%p) : set LOD to %d\n", This, This->baseTexture.LOD);
124
125     return old;
126 }
127
128 DWORD basetexture_get_lod(IWineD3DBaseTexture *iface)
129 {
130     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
131
132     TRACE("(%p) : returning %d\n", This, This->baseTexture.LOD);
133
134     return This->baseTexture.LOD;
135 }
136
137 DWORD basetexture_get_level_count(IWineD3DBaseTexture *iface)
138 {
139     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
140     TRACE("(%p) : returning %d\n", This, This->baseTexture.levels);
141     return This->baseTexture.levels;
142 }
143
144 HRESULT basetexture_set_autogen_filter_type(IWineD3DBaseTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType)
145 {
146   IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
147   IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
148   UINT textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
149
150   if (!(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)) {
151       TRACE("(%p) : returning invalid call\n", This);
152       return WINED3DERR_INVALIDCALL;
153   }
154   if(This->baseTexture.filterType != FilterType) {
155       /* What about multithreading? Do we want all the context overhead just to set this value?
156        * Or should we delay the applying until the texture is used for drawing? For now, apply
157        * immediately.
158        */
159       ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
160       ENTER_GL();
161       glBindTexture(textureDimensions, This->baseTexture.texture_rgb.name);
162       checkGLcall("glBindTexture");
163       switch(FilterType) {
164           case WINED3DTEXF_NONE:
165           case WINED3DTEXF_POINT:
166               glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST);
167               checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST)");
168
169               break;
170           case WINED3DTEXF_LINEAR:
171               glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
172               checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
173
174               break;
175           default:
176               WARN("Unexpected filter type %d, setting to GL_NICEST\n", FilterType);
177               glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
178               checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
179       }
180       LEAVE_GL();
181   }
182   This->baseTexture.filterType = FilterType;
183   TRACE("(%p) :\n", This);
184   return WINED3D_OK;
185 }
186
187 WINED3DTEXTUREFILTERTYPE basetexture_get_autogen_filter_type(IWineD3DBaseTexture *iface)
188 {
189     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
190
191     FIXME("(%p) : stub\n", This);
192
193     return This->baseTexture.filterType;
194 }
195
196 void basetexture_generate_mipmaps(IWineD3DBaseTexture *iface)
197 {
198   IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
199   /* TODO: implement filters using GL_SGI_generate_mipmaps http://oss.sgi.com/projects/ogl-sample/registry/SGIS/generate_mipmap.txt */
200   FIXME("(%p) : stub\n", This);
201   return ;
202 }
203
204 BOOL basetexture_set_dirty(IWineD3DBaseTexture *iface, BOOL dirty)
205 {
206     BOOL old;
207     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
208     old = This->baseTexture.texture_rgb.dirty || This->baseTexture.texture_srgb.dirty;
209     This->baseTexture.texture_rgb.dirty = dirty;
210     This->baseTexture.texture_srgb.dirty = dirty;
211     return old;
212 }
213
214 BOOL basetexture_get_dirty(IWineD3DBaseTexture *iface)
215 {
216     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
217     return This->baseTexture.texture_rgb.dirty || This->baseTexture.texture_srgb.dirty;
218 }
219
220 /* Context activation is done by the caller. */
221 HRESULT basetexture_bind(IWineD3DBaseTexture *iface, BOOL srgb, BOOL *set_surface_desc)
222 {
223     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
224     HRESULT hr = WINED3D_OK;
225     UINT textureDimensions;
226     BOOL isNewTexture = FALSE;
227     struct gl_texture *gl_tex;
228     TRACE("(%p) : About to bind texture\n", This);
229
230     This->baseTexture.is_srgb = srgb; /* SRGB mode cache for PreLoad calls outside drawprim */
231     if(srgb) {
232         gl_tex = &This->baseTexture.texture_srgb;
233     } else {
234         gl_tex = &This->baseTexture.texture_rgb;
235     }
236
237     textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
238     ENTER_GL();
239     /* Generate a texture name if we don't already have one */
240     if (gl_tex->name == 0) {
241         *set_surface_desc = TRUE;
242         glGenTextures(1, &gl_tex->name);
243         checkGLcall("glGenTextures");
244         TRACE("Generated texture %d\n", gl_tex->name);
245         if (This->resource.pool == WINED3DPOOL_DEFAULT) {
246             /* Tell opengl to try and keep this texture in video ram (well mostly) */
247             GLclampf tmp;
248             tmp = 0.9f;
249             glPrioritizeTextures(1, &gl_tex->name, &tmp);
250
251         }
252         /* Initialise the state of the texture object
253         to the openGL defaults, not the directx defaults */
254         gl_tex->states[WINED3DTEXSTA_ADDRESSU]      = WINED3DTADDRESS_WRAP;
255         gl_tex->states[WINED3DTEXSTA_ADDRESSV]      = WINED3DTADDRESS_WRAP;
256         gl_tex->states[WINED3DTEXSTA_ADDRESSW]      = WINED3DTADDRESS_WRAP;
257         gl_tex->states[WINED3DTEXSTA_BORDERCOLOR]   = 0;
258         gl_tex->states[WINED3DTEXSTA_MAGFILTER]     = WINED3DTEXF_LINEAR;
259         gl_tex->states[WINED3DTEXSTA_MINFILTER]     = WINED3DTEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
260         gl_tex->states[WINED3DTEXSTA_MIPFILTER]     = WINED3DTEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
261         gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL]   = 0;
262         gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
263         gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE]   = 0;
264         gl_tex->states[WINED3DTEXSTA_ELEMENTINDEX]  = 0;
265         gl_tex->states[WINED3DTEXSTA_DMAPOFFSET]    = 0;
266         gl_tex->states[WINED3DTEXSTA_TSSADDRESSW]   = WINED3DTADDRESS_WRAP;
267         IWineD3DBaseTexture_SetDirty(iface, TRUE);
268         isNewTexture = TRUE;
269
270         if(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP) {
271             /* This means double binding the texture at creation, but keeps the code simpler all
272              * in all, and the run-time path free from additional checks
273              */
274             glBindTexture(textureDimensions, gl_tex->name);
275             checkGLcall("glBindTexture");
276             glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
277             checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
278         }
279     } else {
280         *set_surface_desc = FALSE;
281     }
282
283     /* Bind the texture */
284     if (gl_tex->name != 0) {
285         glBindTexture(textureDimensions, gl_tex->name);
286         checkGLcall("glBindTexture");
287         if (isNewTexture) {
288             /* For a new texture we have to set the textures levels after binding the texture.
289              * In theory this is all we should ever have to do, but because ATI's drivers are broken, we
290              * also need to set the texture dimensions before the texture is set
291              * Beware that texture rectangles do not support mipmapping, but set the maxmiplevel if we're
292              * relying on the partial GL_ARB_texture_non_power_of_two emulation with texture rectangles
293              * (ie, do not care for cond_np2 here, just look for GL_TEXTURE_RECTANGLE_ARB)
294              */
295             if(textureDimensions != GL_TEXTURE_RECTANGLE_ARB) {
296                 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->baseTexture.levels - 1);
297                 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels - 1);
298                 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels)");
299             }
300             if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
301                 /* Cubemaps are always set to clamp, regardless of the sampler state. */
302                 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
303                 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
304                 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
305             }
306         }
307     } else { /* this only happened if we've run out of openGL textures */
308         WARN("This texture doesn't have an openGL texture assigned to it\n");
309         hr =  WINED3DERR_INVALIDCALL;
310     }
311
312     LEAVE_GL();
313     return hr;
314 }
315
316 /* GL locking is done by the caller */
317 static inline void apply_wrap(const GLint textureDimensions, const DWORD state, const GLint type,
318                               BOOL cond_np2) {
319     GLint wrapParm;
320
321     if (state < minLookup[WINELOOKUP_WARPPARAM] || state > maxLookup[WINELOOKUP_WARPPARAM]) {
322         FIXME("Unrecognized or unsupported WINED3DTADDRESS_U value %d\n", state);
323     } else {
324         if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
325             /* Cubemaps are always set to clamp, regardless of the sampler state. */
326             wrapParm = GL_CLAMP_TO_EDGE;
327         } else if(cond_np2) {
328             if(state == WINED3DTADDRESS_WRAP) {
329                 wrapParm = GL_CLAMP_TO_EDGE;
330             } else {
331                 wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
332             }
333         } else {
334             wrapParm = stateLookup[WINELOOKUP_WARPPARAM][state - minLookup[WINELOOKUP_WARPPARAM]];
335         }
336         TRACE("Setting WRAP_S to %d for %x\n", wrapParm, textureDimensions);
337         glTexParameteri(textureDimensions, type, wrapParm);
338         checkGLcall("glTexParameteri(..., type, wrapParm)");
339     }
340 }
341
342 /* GL locking is done by the caller (state handler) */
343 void basetexture_apply_state_changes(IWineD3DBaseTexture *iface,
344         const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1],
345         const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1])
346 {
347     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
348     DWORD state;
349     GLint textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
350     BOOL cond_np2 = IWineD3DBaseTexture_IsCondNP2(iface);
351     DWORD aniso;
352     struct gl_texture *gl_tex;
353
354     TRACE("iface %p, textureStates %p, samplerStates %p\n", iface, textureStates, samplerStates);
355
356     if(This->baseTexture.is_srgb) {
357         gl_tex = &This->baseTexture.texture_srgb;
358     } else {
359         gl_tex = &This->baseTexture.texture_rgb;
360     }
361
362     /* This function relies on the correct texture being bound and loaded. */
363
364     if(samplerStates[WINED3DSAMP_ADDRESSU]      != gl_tex->states[WINED3DTEXSTA_ADDRESSU]) {
365         state = samplerStates[WINED3DSAMP_ADDRESSU];
366         apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_S, cond_np2);
367         gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
368     }
369
370     if(samplerStates[WINED3DSAMP_ADDRESSV]      != gl_tex->states[WINED3DTEXSTA_ADDRESSV]) {
371         state = samplerStates[WINED3DSAMP_ADDRESSV];
372         apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_T, cond_np2);
373         gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
374     }
375
376     if(samplerStates[WINED3DSAMP_ADDRESSW]      != gl_tex->states[WINED3DTEXSTA_ADDRESSW]) {
377         state = samplerStates[WINED3DSAMP_ADDRESSW];
378         apply_wrap(textureDimensions, state, GL_TEXTURE_WRAP_R, cond_np2);
379         gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
380     }
381
382     if(samplerStates[WINED3DSAMP_BORDERCOLOR]   != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR]) {
383         float col[4];
384
385         state = samplerStates[WINED3DSAMP_BORDERCOLOR];
386         D3DCOLORTOGLFLOAT4(state, col);
387         TRACE("Setting border color for %u to %x\n", textureDimensions, state);
388         glTexParameterfv(textureDimensions, GL_TEXTURE_BORDER_COLOR, &col[0]);
389         checkGLcall("glTexParameteri(..., GL_TEXTURE_BORDER_COLOR, ...)");
390         gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
391     }
392
393     if(samplerStates[WINED3DSAMP_MAGFILTER]     != gl_tex->states[WINED3DTEXSTA_MAGFILTER]) {
394         GLint glValue;
395         state = samplerStates[WINED3DSAMP_MAGFILTER];
396         if (state > WINED3DTEXF_ANISOTROPIC) {
397             FIXME("Unrecognized or unsupported MAGFILTER* value %d\n", state);
398         }
399
400         glValue = wined3d_gl_mag_filter(This->baseTexture.magLookup,
401                 min(max(state, WINED3DTEXF_POINT), WINED3DTEXF_LINEAR));
402         TRACE("ValueMAG=%d setting MAGFILTER to %x\n", state, glValue);
403         glTexParameteri(textureDimensions, GL_TEXTURE_MAG_FILTER, glValue);
404
405         gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
406     }
407
408     if((samplerStates[WINED3DSAMP_MINFILTER]     != gl_tex->states[WINED3DTEXSTA_MINFILTER] ||
409         samplerStates[WINED3DSAMP_MIPFILTER]     != gl_tex->states[WINED3DTEXSTA_MIPFILTER] ||
410         samplerStates[WINED3DSAMP_MAXMIPLEVEL]   != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL])) {
411         GLint glValue;
412
413         gl_tex->states[WINED3DTEXSTA_MIPFILTER] = samplerStates[WINED3DSAMP_MIPFILTER];
414         gl_tex->states[WINED3DTEXSTA_MINFILTER] = samplerStates[WINED3DSAMP_MINFILTER];
415         gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = samplerStates[WINED3DSAMP_MAXMIPLEVEL];
416
417         if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC
418             || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_ANISOTROPIC)
419         {
420
421             FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %d D3DSAMP_MIPFILTER value %d\n",
422                   gl_tex->states[WINED3DTEXSTA_MINFILTER],
423                   gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
424         }
425         glValue = wined3d_gl_min_mip_filter(This->baseTexture.minMipLookup,
426                 min(max(samplerStates[WINED3DSAMP_MINFILTER], WINED3DTEXF_POINT), WINED3DTEXF_LINEAR),
427                 min(max(samplerStates[WINED3DSAMP_MIPFILTER], WINED3DTEXF_NONE), WINED3DTEXF_LINEAR));
428
429         TRACE("ValueMIN=%d, ValueMIP=%d, setting MINFILTER to %x\n",
430               samplerStates[WINED3DSAMP_MINFILTER],
431               samplerStates[WINED3DSAMP_MIPFILTER], glValue);
432         glTexParameteri(textureDimensions, GL_TEXTURE_MIN_FILTER, glValue);
433         checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
434
435         if(!cond_np2) {
436             if(gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE) {
437                 glValue = This->baseTexture.LOD;
438             } else if(gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= This->baseTexture.levels) {
439                 glValue = This->baseTexture.levels - 1;
440             } else if(gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < This->baseTexture.LOD) {
441                 /* baseTexture.LOD is already clamped in the setter */
442                 glValue = This->baseTexture.LOD;
443             } else {
444                 glValue = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
445             }
446             /* Note that D3DSAMP_MAXMIPLEVEL specifies the biggest mipmap(default 0), while
447              * GL_TEXTURE_MAX_LEVEL specifies the smallest mimap used(default 1000).
448              * So D3DSAMP_MAXMIPLEVEL is the same as GL_TEXTURE_BASE_LEVEL.
449              */
450             glTexParameteri(textureDimensions, GL_TEXTURE_BASE_LEVEL, glValue);
451         }
452     }
453
454     if ((gl_tex->states[WINED3DSAMP_MAGFILTER] != WINED3DTEXF_ANISOTROPIC
455          && gl_tex->states[WINED3DSAMP_MINFILTER] != WINED3DTEXF_ANISOTROPIC
456          && gl_tex->states[WINED3DSAMP_MIPFILTER] != WINED3DTEXF_ANISOTROPIC)
457             || cond_np2)
458     {
459         aniso = 1;
460     }
461     else
462     {
463         aniso = samplerStates[WINED3DSAMP_MAXANISOTROPY];
464     }
465
466     if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
467     {
468         if (GL_SUPPORT(EXT_TEXTURE_FILTER_ANISOTROPIC))
469         {
470             glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
471             checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
472         }
473         else
474         {
475             WARN("Anisotropic filtering not supported.\n");
476         }
477         gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;
478     }
479 }