ntdll: Use the getdents64 system call on all Linux platforms.
[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, UINT layer_count, UINT level_count,
31         WINED3DRESOURCETYPE resource_type, IWineD3DDeviceImpl *device, DWORD usage,
32         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((IWineD3DResource *)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.sub_resources = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
46             level_count * layer_count * sizeof(*texture->baseTexture.sub_resources));
47     if (!texture->baseTexture.sub_resources)
48     {
49         ERR("Failed to allocate sub-resource array.\n");
50         resource_cleanup((IWineD3DResource *)texture);
51         return E_OUTOFMEMORY;
52     }
53
54     texture->baseTexture.layer_count = layer_count;
55     texture->baseTexture.level_count = level_count;
56     texture->baseTexture.filterType = (usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3DTEXF_LINEAR : WINED3DTEXF_NONE;
57     texture->baseTexture.LOD = 0;
58     texture->baseTexture.texture_rgb.dirty = TRUE;
59     texture->baseTexture.texture_srgb.dirty = TRUE;
60     texture->baseTexture.is_srgb = FALSE;
61     texture->baseTexture.pow2Matrix_identity = TRUE;
62
63     if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
64     {
65         texture->baseTexture.minMipLookup = minMipLookup;
66         texture->baseTexture.magLookup = magLookup;
67     }
68     else
69     {
70         texture->baseTexture.minMipLookup = minMipLookup_noFilter;
71         texture->baseTexture.magLookup = magLookup_noFilter;
72     }
73
74     return WINED3D_OK;
75 }
76
77 void basetexture_cleanup(IWineD3DBaseTextureImpl *texture)
78 {
79     basetexture_unload(texture);
80     HeapFree(GetProcessHeap(), 0, texture->baseTexture.sub_resources);
81     resource_cleanup((IWineD3DResource *)texture);
82 }
83
84 IWineD3DResourceImpl *basetexture_get_sub_resource(IWineD3DBaseTextureImpl *texture, UINT sub_resource_idx)
85 {
86     UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
87
88     if (sub_resource_idx >= sub_count)
89     {
90         WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
91         return NULL;
92     }
93
94     return texture->baseTexture.sub_resources[sub_resource_idx];
95 }
96
97 /* A GL context is provided by the caller */
98 static void gltexture_delete(struct gl_texture *tex)
99 {
100     ENTER_GL();
101     glDeleteTextures(1, &tex->name);
102     LEAVE_GL();
103     tex->name = 0;
104 }
105
106 void basetexture_unload(IWineD3DBaseTextureImpl *texture)
107 {
108     IWineD3DDeviceImpl *device = texture->resource.device;
109     struct wined3d_context *context = NULL;
110
111     if (texture->baseTexture.texture_rgb.name || texture->baseTexture.texture_srgb.name)
112     {
113         context = context_acquire(device, NULL);
114     }
115
116     if (texture->baseTexture.texture_rgb.name)
117         gltexture_delete(&texture->baseTexture.texture_rgb);
118
119     if (texture->baseTexture.texture_srgb.name)
120         gltexture_delete(&texture->baseTexture.texture_srgb);
121
122     if (context) context_release(context);
123
124     texture->baseTexture.texture_rgb.dirty = TRUE;
125     texture->baseTexture.texture_srgb.dirty = TRUE;
126
127     resource_unload((IWineD3DResourceImpl *)texture);
128 }
129
130 DWORD basetexture_set_lod(IWineD3DBaseTexture *iface, DWORD LODNew)
131 {
132     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
133     DWORD old = This->baseTexture.LOD;
134
135     /* The d3d9:texture test shows that SetLOD is ignored on non-managed
136      * textures. The call always returns 0, and GetLOD always returns 0
137      */
138     if (This->resource.pool != WINED3DPOOL_MANAGED) {
139         TRACE("Ignoring SetLOD on %s texture, returning 0\n", debug_d3dpool(This->resource.pool));
140         return 0;
141     }
142
143     if (LODNew >= This->baseTexture.level_count)
144         LODNew = This->baseTexture.level_count - 1;
145
146     if(This->baseTexture.LOD != LODNew) {
147         This->baseTexture.LOD = LODNew;
148
149         This->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
150         This->baseTexture.texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
151         if(This->baseTexture.bindCount) {
152             IWineD3DDeviceImpl_MarkStateDirty(This->resource.device, STATE_SAMPLER(This->baseTexture.sampler));
153         }
154     }
155
156     TRACE("(%p) : set LOD to %d\n", This, This->baseTexture.LOD);
157
158     return old;
159 }
160
161 DWORD basetexture_get_lod(IWineD3DBaseTexture *iface)
162 {
163     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
164
165     TRACE("(%p) : returning %d\n", This, This->baseTexture.LOD);
166
167     return This->baseTexture.LOD;
168 }
169
170 DWORD basetexture_get_level_count(IWineD3DBaseTextureImpl *texture)
171 {
172     TRACE("texture %p, returning %u.\n", texture, texture->baseTexture.level_count);
173     return texture->baseTexture.level_count;
174 }
175
176 HRESULT basetexture_set_autogen_filter_type(IWineD3DBaseTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType)
177 {
178   IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
179   IWineD3DDeviceImpl *device = This->resource.device;
180   GLenum textureDimensions = This->baseTexture.target;
181
182   if (!(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)) {
183       TRACE("(%p) : returning invalid call\n", This);
184       return WINED3DERR_INVALIDCALL;
185   }
186   if(This->baseTexture.filterType != FilterType) {
187       /* What about multithreading? Do we want all the context overhead just to set this value?
188        * Or should we delay the applying until the texture is used for drawing? For now, apply
189        * immediately.
190        */
191       struct wined3d_context *context = context_acquire(device, NULL);
192
193       ENTER_GL();
194       glBindTexture(textureDimensions, This->baseTexture.texture_rgb.name);
195       checkGLcall("glBindTexture");
196       switch(FilterType) {
197           case WINED3DTEXF_NONE:
198           case WINED3DTEXF_POINT:
199               glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST);
200               checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST)");
201
202               break;
203           case WINED3DTEXF_LINEAR:
204               glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
205               checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
206
207               break;
208           default:
209               WARN("Unexpected filter type %d, setting to GL_NICEST\n", FilterType);
210               glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
211               checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
212       }
213       LEAVE_GL();
214
215       context_release(context);
216   }
217   This->baseTexture.filterType = FilterType;
218   TRACE("(%p) :\n", This);
219   return WINED3D_OK;
220 }
221
222 WINED3DTEXTUREFILTERTYPE basetexture_get_autogen_filter_type(IWineD3DBaseTexture *iface)
223 {
224     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
225
226     FIXME("(%p) : stub\n", This);
227
228     return This->baseTexture.filterType;
229 }
230
231 void basetexture_generate_mipmaps(IWineD3DBaseTexture *iface)
232 {
233     /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
234     FIXME("iface %p stub!\n", iface);
235 }
236
237 BOOL basetexture_set_dirty(IWineD3DBaseTextureImpl *texture, BOOL dirty)
238 {
239     BOOL old;
240
241     old = texture->baseTexture.texture_rgb.dirty || texture->baseTexture.texture_srgb.dirty;
242     texture->baseTexture.texture_rgb.dirty = dirty;
243     texture->baseTexture.texture_srgb.dirty = dirty;
244
245     return old;
246 }
247
248 /* Context activation is done by the caller. */
249 HRESULT basetexture_bind(IWineD3DBaseTexture *iface, BOOL srgb, BOOL *set_surface_desc)
250 {
251     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
252     HRESULT hr = WINED3D_OK;
253     GLenum textureDimensions;
254     BOOL isNewTexture = FALSE;
255     struct gl_texture *gl_tex;
256     TRACE("(%p) : About to bind texture\n", This);
257
258     This->baseTexture.is_srgb = srgb; /* SRGB mode cache for PreLoad calls outside drawprim */
259     if(srgb) {
260         gl_tex = &This->baseTexture.texture_srgb;
261     } else {
262         gl_tex = &This->baseTexture.texture_rgb;
263     }
264
265     textureDimensions = This->baseTexture.target;
266     ENTER_GL();
267     /* Generate a texture name if we don't already have one */
268     if (!gl_tex->name)
269     {
270         *set_surface_desc = TRUE;
271         glGenTextures(1, &gl_tex->name);
272         checkGLcall("glGenTextures");
273         TRACE("Generated texture %d\n", gl_tex->name);
274         if (This->resource.pool == WINED3DPOOL_DEFAULT) {
275             /* Tell opengl to try and keep this texture in video ram (well mostly) */
276             GLclampf tmp;
277             tmp = 0.9f;
278             glPrioritizeTextures(1, &gl_tex->name, &tmp);
279
280         }
281         /* Initialise the state of the texture object
282         to the openGL defaults, not the directx defaults */
283         gl_tex->states[WINED3DTEXSTA_ADDRESSU]      = WINED3DTADDRESS_WRAP;
284         gl_tex->states[WINED3DTEXSTA_ADDRESSV]      = WINED3DTADDRESS_WRAP;
285         gl_tex->states[WINED3DTEXSTA_ADDRESSW]      = WINED3DTADDRESS_WRAP;
286         gl_tex->states[WINED3DTEXSTA_BORDERCOLOR]   = 0;
287         gl_tex->states[WINED3DTEXSTA_MAGFILTER]     = WINED3DTEXF_LINEAR;
288         gl_tex->states[WINED3DTEXSTA_MINFILTER]     = WINED3DTEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
289         gl_tex->states[WINED3DTEXSTA_MIPFILTER]     = WINED3DTEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
290         gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL]   = 0;
291         gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
292         gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE]   = 0;
293         gl_tex->states[WINED3DTEXSTA_SHADOW]        = FALSE;
294         basetexture_set_dirty(This, TRUE);
295         isNewTexture = TRUE;
296
297         if(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP) {
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", This->baseTexture.level_count - 1);
326                 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.level_count - 1);
327                 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->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(IWineD3DBaseTexture *iface,
375         const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1],
376         const struct wined3d_gl_info *gl_info)
377 {
378     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
379     GLenum textureDimensions = This->baseTexture.target;
380     DWORD state;
381     BOOL cond_np2 = IWineD3DBaseTexture_IsCondNP2(iface);
382     DWORD aniso;
383     struct gl_texture *gl_tex;
384
385     TRACE("iface %p, samplerStates %p\n", iface, samplerStates);
386
387     if(This->baseTexture.is_srgb) {
388         gl_tex = &This->baseTexture.texture_srgb;
389     } else {
390         gl_tex = &This->baseTexture.texture_rgb;
391     }
392
393     /* This function relies on the correct texture being bound and loaded. */
394
395     if(samplerStates[WINED3DSAMP_ADDRESSU]      != gl_tex->states[WINED3DTEXSTA_ADDRESSU]) {
396         state = samplerStates[WINED3DSAMP_ADDRESSU];
397         apply_wrap(gl_info, textureDimensions, state, GL_TEXTURE_WRAP_S, cond_np2);
398         gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
399     }
400
401     if(samplerStates[WINED3DSAMP_ADDRESSV]      != gl_tex->states[WINED3DTEXSTA_ADDRESSV]) {
402         state = samplerStates[WINED3DSAMP_ADDRESSV];
403         apply_wrap(gl_info, textureDimensions, state, GL_TEXTURE_WRAP_T, cond_np2);
404         gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
405     }
406
407     if(samplerStates[WINED3DSAMP_ADDRESSW]      != gl_tex->states[WINED3DTEXSTA_ADDRESSW]) {
408         state = samplerStates[WINED3DSAMP_ADDRESSW];
409         apply_wrap(gl_info, textureDimensions, state, GL_TEXTURE_WRAP_R, cond_np2);
410         gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
411     }
412
413     if(samplerStates[WINED3DSAMP_BORDERCOLOR]   != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR]) {
414         float col[4];
415
416         state = samplerStates[WINED3DSAMP_BORDERCOLOR];
417         D3DCOLORTOGLFLOAT4(state, col);
418         TRACE("Setting border color for %u to %x\n", textureDimensions, state);
419         glTexParameterfv(textureDimensions, GL_TEXTURE_BORDER_COLOR, &col[0]);
420         checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
421         gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
422     }
423
424     if(samplerStates[WINED3DSAMP_MAGFILTER]     != gl_tex->states[WINED3DTEXSTA_MAGFILTER]) {
425         GLint glValue;
426         state = samplerStates[WINED3DSAMP_MAGFILTER];
427         if (state > WINED3DTEXF_ANISOTROPIC) {
428             FIXME("Unrecognized or unsupported MAGFILTER* value %d\n", state);
429         }
430
431         glValue = wined3d_gl_mag_filter(This->baseTexture.magLookup,
432                 min(max(state, WINED3DTEXF_POINT), WINED3DTEXF_LINEAR));
433         TRACE("ValueMAG=%d setting MAGFILTER to %x\n", state, glValue);
434         glTexParameteri(textureDimensions, GL_TEXTURE_MAG_FILTER, glValue);
435
436         gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
437     }
438
439     if((samplerStates[WINED3DSAMP_MINFILTER]     != gl_tex->states[WINED3DTEXSTA_MINFILTER] ||
440         samplerStates[WINED3DSAMP_MIPFILTER]     != gl_tex->states[WINED3DTEXSTA_MIPFILTER] ||
441         samplerStates[WINED3DSAMP_MAXMIPLEVEL]   != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL])) {
442         GLint glValue;
443
444         gl_tex->states[WINED3DTEXSTA_MIPFILTER] = samplerStates[WINED3DSAMP_MIPFILTER];
445         gl_tex->states[WINED3DTEXSTA_MINFILTER] = samplerStates[WINED3DSAMP_MINFILTER];
446         gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = samplerStates[WINED3DSAMP_MAXMIPLEVEL];
447
448         if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC
449             || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_ANISOTROPIC)
450         {
451
452             FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %d D3DSAMP_MIPFILTER value %d\n",
453                   gl_tex->states[WINED3DTEXSTA_MINFILTER],
454                   gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
455         }
456         glValue = wined3d_gl_min_mip_filter(This->baseTexture.minMipLookup,
457                 min(max(samplerStates[WINED3DSAMP_MINFILTER], WINED3DTEXF_POINT), WINED3DTEXF_LINEAR),
458                 min(max(samplerStates[WINED3DSAMP_MIPFILTER], WINED3DTEXF_NONE), WINED3DTEXF_LINEAR));
459
460         TRACE("ValueMIN=%d, ValueMIP=%d, setting MINFILTER to %x\n",
461               samplerStates[WINED3DSAMP_MINFILTER],
462               samplerStates[WINED3DSAMP_MIPFILTER], glValue);
463         glTexParameteri(textureDimensions, GL_TEXTURE_MIN_FILTER, glValue);
464         checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
465
466         if (!cond_np2)
467         {
468             if (gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE)
469                 glValue = This->baseTexture.LOD;
470             else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= This->baseTexture.level_count)
471                 glValue = This->baseTexture.level_count - 1;
472             else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < This->baseTexture.LOD)
473                 /* baseTexture.LOD is already clamped in the setter */
474                 glValue = This->baseTexture.LOD;
475             else
476                 glValue = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
477
478             /* Note that D3DSAMP_MAXMIPLEVEL specifies the biggest mipmap(default 0), while
479              * GL_TEXTURE_MAX_LEVEL specifies the smallest mimap used(default 1000).
480              * So D3DSAMP_MAXMIPLEVEL is the same as GL_TEXTURE_BASE_LEVEL.
481              */
482             glTexParameteri(textureDimensions, GL_TEXTURE_BASE_LEVEL, glValue);
483         }
484     }
485
486     if ((gl_tex->states[WINED3DTEXSTA_MAGFILTER] != WINED3DTEXF_ANISOTROPIC
487          && gl_tex->states[WINED3DTEXSTA_MINFILTER] != WINED3DTEXF_ANISOTROPIC
488          && gl_tex->states[WINED3DTEXSTA_MIPFILTER] != WINED3DTEXF_ANISOTROPIC)
489             || cond_np2)
490     {
491         aniso = 1;
492     }
493     else
494     {
495         aniso = samplerStates[WINED3DSAMP_MAXANISOTROPY];
496     }
497
498     if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
499     {
500         if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
501         {
502             glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
503             checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
504         }
505         else
506         {
507             WARN("Anisotropic filtering not supported.\n");
508         }
509         gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;
510     }
511
512     if (!(This->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
513             != !gl_tex->states[WINED3DTEXSTA_SHADOW])
514     {
515         if (This->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
516         {
517             glTexParameteri(textureDimensions, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
518             glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
519             checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
520             gl_tex->states[WINED3DTEXSTA_SHADOW] = TRUE;
521         }
522         else
523         {
524             glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
525             checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
526             gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
527         }
528     }
529 }