wined3d: Remove IWineD3DBaseTexture::SetDirty() from the public interface.
[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(IWineD3DBaseTexture *iface)
78 {
79     basetexture_unload(iface);
80     HeapFree(GetProcessHeap(), 0, ((IWineD3DBaseTextureImpl *)iface)->baseTexture.sub_resources);
81     resource_cleanup((IWineD3DResource *)iface);
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(IWineD3DBaseTexture *iface)
107 {
108     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
109     IWineD3DDeviceImpl *device = This->resource.device;
110     struct wined3d_context *context = NULL;
111
112     if (This->baseTexture.texture_rgb.name || This->baseTexture.texture_srgb.name)
113     {
114         context = context_acquire(device, NULL);
115     }
116
117     if(This->baseTexture.texture_rgb.name) {
118         gltexture_delete(&This->baseTexture.texture_rgb);
119     }
120     if(This->baseTexture.texture_srgb.name) {
121         gltexture_delete(&This->baseTexture.texture_srgb);
122     }
123
124     if (context) context_release(context);
125
126     This->baseTexture.texture_rgb.dirty = TRUE;
127     This->baseTexture.texture_srgb.dirty = TRUE;
128
129     resource_unload((IWineD3DResourceImpl *)This);
130 }
131
132 DWORD basetexture_set_lod(IWineD3DBaseTexture *iface, DWORD LODNew)
133 {
134     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
135     DWORD old = This->baseTexture.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      */
140     if (This->resource.pool != WINED3DPOOL_MANAGED) {
141         TRACE("Ignoring SetLOD on %s texture, returning 0\n", debug_d3dpool(This->resource.pool));
142         return 0;
143     }
144
145     if (LODNew >= This->baseTexture.level_count)
146         LODNew = This->baseTexture.level_count - 1;
147
148     if(This->baseTexture.LOD != LODNew) {
149         This->baseTexture.LOD = LODNew;
150
151         This->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
152         This->baseTexture.texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
153         if(This->baseTexture.bindCount) {
154             IWineD3DDeviceImpl_MarkStateDirty(This->resource.device, STATE_SAMPLER(This->baseTexture.sampler));
155         }
156     }
157
158     TRACE("(%p) : set LOD to %d\n", This, This->baseTexture.LOD);
159
160     return old;
161 }
162
163 DWORD basetexture_get_lod(IWineD3DBaseTexture *iface)
164 {
165     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
166
167     TRACE("(%p) : returning %d\n", This, This->baseTexture.LOD);
168
169     return This->baseTexture.LOD;
170 }
171
172 DWORD basetexture_get_level_count(IWineD3DBaseTexture *iface)
173 {
174     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
175     TRACE("iface %p, returning %u.\n", iface, This->baseTexture.level_count);
176     return This->baseTexture.level_count;
177 }
178
179 HRESULT basetexture_set_autogen_filter_type(IWineD3DBaseTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType)
180 {
181   IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
182   IWineD3DDeviceImpl *device = This->resource.device;
183   GLenum textureDimensions = This->baseTexture.target;
184
185   if (!(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)) {
186       TRACE("(%p) : returning invalid call\n", This);
187       return WINED3DERR_INVALIDCALL;
188   }
189   if(This->baseTexture.filterType != FilterType) {
190       /* What about multithreading? Do we want all the context overhead just to set this value?
191        * Or should we delay the applying until the texture is used for drawing? For now, apply
192        * immediately.
193        */
194       struct wined3d_context *context = context_acquire(device, NULL);
195
196       ENTER_GL();
197       glBindTexture(textureDimensions, This->baseTexture.texture_rgb.name);
198       checkGLcall("glBindTexture");
199       switch(FilterType) {
200           case WINED3DTEXF_NONE:
201           case WINED3DTEXF_POINT:
202               glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST);
203               checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST)");
204
205               break;
206           case WINED3DTEXF_LINEAR:
207               glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
208               checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
209
210               break;
211           default:
212               WARN("Unexpected filter type %d, setting to GL_NICEST\n", FilterType);
213               glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
214               checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
215       }
216       LEAVE_GL();
217
218       context_release(context);
219   }
220   This->baseTexture.filterType = FilterType;
221   TRACE("(%p) :\n", This);
222   return WINED3D_OK;
223 }
224
225 WINED3DTEXTUREFILTERTYPE basetexture_get_autogen_filter_type(IWineD3DBaseTexture *iface)
226 {
227     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
228
229     FIXME("(%p) : stub\n", This);
230
231     return This->baseTexture.filterType;
232 }
233
234 void basetexture_generate_mipmaps(IWineD3DBaseTexture *iface)
235 {
236     /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
237     FIXME("iface %p stub!\n", iface);
238 }
239
240 BOOL basetexture_set_dirty(IWineD3DBaseTextureImpl *texture, BOOL dirty)
241 {
242     BOOL old;
243
244     old = texture->baseTexture.texture_rgb.dirty || texture->baseTexture.texture_srgb.dirty;
245     texture->baseTexture.texture_rgb.dirty = dirty;
246     texture->baseTexture.texture_srgb.dirty = dirty;
247
248     return old;
249 }
250
251 /* Context activation is done by the caller. */
252 HRESULT basetexture_bind(IWineD3DBaseTexture *iface, BOOL srgb, BOOL *set_surface_desc)
253 {
254     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
255     HRESULT hr = WINED3D_OK;
256     GLenum textureDimensions;
257     BOOL isNewTexture = FALSE;
258     struct gl_texture *gl_tex;
259     TRACE("(%p) : About to bind texture\n", This);
260
261     This->baseTexture.is_srgb = srgb; /* SRGB mode cache for PreLoad calls outside drawprim */
262     if(srgb) {
263         gl_tex = &This->baseTexture.texture_srgb;
264     } else {
265         gl_tex = &This->baseTexture.texture_rgb;
266     }
267
268     textureDimensions = This->baseTexture.target;
269     ENTER_GL();
270     /* Generate a texture name if we don't already have one */
271     if (!gl_tex->name)
272     {
273         *set_surface_desc = TRUE;
274         glGenTextures(1, &gl_tex->name);
275         checkGLcall("glGenTextures");
276         TRACE("Generated texture %d\n", gl_tex->name);
277         if (This->resource.pool == WINED3DPOOL_DEFAULT) {
278             /* Tell opengl to try and keep this texture in video ram (well mostly) */
279             GLclampf tmp;
280             tmp = 0.9f;
281             glPrioritizeTextures(1, &gl_tex->name, &tmp);
282
283         }
284         /* Initialise the state of the texture object
285         to the openGL defaults, not the directx defaults */
286         gl_tex->states[WINED3DTEXSTA_ADDRESSU]      = WINED3DTADDRESS_WRAP;
287         gl_tex->states[WINED3DTEXSTA_ADDRESSV]      = WINED3DTADDRESS_WRAP;
288         gl_tex->states[WINED3DTEXSTA_ADDRESSW]      = WINED3DTADDRESS_WRAP;
289         gl_tex->states[WINED3DTEXSTA_BORDERCOLOR]   = 0;
290         gl_tex->states[WINED3DTEXSTA_MAGFILTER]     = WINED3DTEXF_LINEAR;
291         gl_tex->states[WINED3DTEXSTA_MINFILTER]     = WINED3DTEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
292         gl_tex->states[WINED3DTEXSTA_MIPFILTER]     = WINED3DTEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
293         gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL]   = 0;
294         gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
295         gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE]   = 0;
296         gl_tex->states[WINED3DTEXSTA_SHADOW]        = FALSE;
297         basetexture_set_dirty(This, TRUE);
298         isNewTexture = TRUE;
299
300         if(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP) {
301             /* This means double binding the texture at creation, but keeps the code simpler all
302              * in all, and the run-time path free from additional checks
303              */
304             glBindTexture(textureDimensions, gl_tex->name);
305             checkGLcall("glBindTexture");
306             glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
307             checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
308         }
309     } else {
310         *set_surface_desc = FALSE;
311     }
312
313     /* Bind the texture */
314     if (gl_tex->name)
315     {
316         glBindTexture(textureDimensions, gl_tex->name);
317         checkGLcall("glBindTexture");
318         if (isNewTexture) {
319             /* For a new texture we have to set the textures levels after binding the texture.
320              * In theory this is all we should ever have to do, but because ATI's drivers are broken, we
321              * also need to set the texture dimensions before the texture is set
322              * Beware that texture rectangles do not support mipmapping, but set the maxmiplevel if we're
323              * relying on the partial GL_ARB_texture_non_power_of_two emulation with texture rectangles
324              * (ie, do not care for cond_np2 here, just look for GL_TEXTURE_RECTANGLE_ARB)
325              */
326             if (textureDimensions != GL_TEXTURE_RECTANGLE_ARB)
327             {
328                 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", This->baseTexture.level_count - 1);
329                 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.level_count - 1);
330                 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.level_count)");
331             }
332             if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
333                 /* Cubemaps are always set to clamp, regardless of the sampler state. */
334                 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
335                 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
336                 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
337             }
338         }
339     } else { /* this only happened if we've run out of openGL textures */
340         WARN("This texture doesn't have an openGL texture assigned to it\n");
341         hr =  WINED3DERR_INVALIDCALL;
342     }
343
344     LEAVE_GL();
345     return hr;
346 }
347
348 /* GL locking is done by the caller */
349 static void apply_wrap(const struct wined3d_gl_info *gl_info, GLenum target,
350         WINED3DTEXTUREADDRESS d3d_wrap, GLenum param, BOOL cond_np2)
351 {
352     GLint gl_wrap;
353
354     if (d3d_wrap < WINED3DTADDRESS_WRAP || d3d_wrap > WINED3DTADDRESS_MIRRORONCE)
355     {
356         FIXME("Unrecognized or unsupported WINED3DTEXTUREADDRESS %#x.\n", d3d_wrap);
357         return;
358     }
359
360     if (target == GL_TEXTURE_CUBE_MAP_ARB
361             || (cond_np2 && d3d_wrap == WINED3DTADDRESS_WRAP))
362     {
363         /* Cubemaps are always set to clamp, regardless of the sampler state. */
364         gl_wrap = GL_CLAMP_TO_EDGE;
365     }
366     else
367     {
368         gl_wrap = gl_info->wrap_lookup[d3d_wrap - WINED3DTADDRESS_WRAP];
369     }
370
371     TRACE("Setting param %#x to %#x for target %#x.\n", param, gl_wrap, target);
372     glTexParameteri(target, param, gl_wrap);
373     checkGLcall("glTexParameteri(target, param, gl_wrap)");
374 }
375
376 /* GL locking is done by the caller (state handler) */
377 void basetexture_apply_state_changes(IWineD3DBaseTexture *iface,
378         const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1],
379         const struct wined3d_gl_info *gl_info)
380 {
381     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
382     GLenum textureDimensions = This->baseTexture.target;
383     DWORD state;
384     BOOL cond_np2 = IWineD3DBaseTexture_IsCondNP2(iface);
385     DWORD aniso;
386     struct gl_texture *gl_tex;
387
388     TRACE("iface %p, samplerStates %p\n", iface, samplerStates);
389
390     if(This->baseTexture.is_srgb) {
391         gl_tex = &This->baseTexture.texture_srgb;
392     } else {
393         gl_tex = &This->baseTexture.texture_rgb;
394     }
395
396     /* This function relies on the correct texture being bound and loaded. */
397
398     if(samplerStates[WINED3DSAMP_ADDRESSU]      != gl_tex->states[WINED3DTEXSTA_ADDRESSU]) {
399         state = samplerStates[WINED3DSAMP_ADDRESSU];
400         apply_wrap(gl_info, textureDimensions, state, GL_TEXTURE_WRAP_S, cond_np2);
401         gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
402     }
403
404     if(samplerStates[WINED3DSAMP_ADDRESSV]      != gl_tex->states[WINED3DTEXSTA_ADDRESSV]) {
405         state = samplerStates[WINED3DSAMP_ADDRESSV];
406         apply_wrap(gl_info, textureDimensions, state, GL_TEXTURE_WRAP_T, cond_np2);
407         gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
408     }
409
410     if(samplerStates[WINED3DSAMP_ADDRESSW]      != gl_tex->states[WINED3DTEXSTA_ADDRESSW]) {
411         state = samplerStates[WINED3DSAMP_ADDRESSW];
412         apply_wrap(gl_info, textureDimensions, state, GL_TEXTURE_WRAP_R, cond_np2);
413         gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
414     }
415
416     if(samplerStates[WINED3DSAMP_BORDERCOLOR]   != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR]) {
417         float col[4];
418
419         state = samplerStates[WINED3DSAMP_BORDERCOLOR];
420         D3DCOLORTOGLFLOAT4(state, col);
421         TRACE("Setting border color for %u to %x\n", textureDimensions, state);
422         glTexParameterfv(textureDimensions, GL_TEXTURE_BORDER_COLOR, &col[0]);
423         checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
424         gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
425     }
426
427     if(samplerStates[WINED3DSAMP_MAGFILTER]     != gl_tex->states[WINED3DTEXSTA_MAGFILTER]) {
428         GLint glValue;
429         state = samplerStates[WINED3DSAMP_MAGFILTER];
430         if (state > WINED3DTEXF_ANISOTROPIC) {
431             FIXME("Unrecognized or unsupported MAGFILTER* value %d\n", state);
432         }
433
434         glValue = wined3d_gl_mag_filter(This->baseTexture.magLookup,
435                 min(max(state, WINED3DTEXF_POINT), WINED3DTEXF_LINEAR));
436         TRACE("ValueMAG=%d setting MAGFILTER to %x\n", state, glValue);
437         glTexParameteri(textureDimensions, GL_TEXTURE_MAG_FILTER, glValue);
438
439         gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
440     }
441
442     if((samplerStates[WINED3DSAMP_MINFILTER]     != gl_tex->states[WINED3DTEXSTA_MINFILTER] ||
443         samplerStates[WINED3DSAMP_MIPFILTER]     != gl_tex->states[WINED3DTEXSTA_MIPFILTER] ||
444         samplerStates[WINED3DSAMP_MAXMIPLEVEL]   != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL])) {
445         GLint glValue;
446
447         gl_tex->states[WINED3DTEXSTA_MIPFILTER] = samplerStates[WINED3DSAMP_MIPFILTER];
448         gl_tex->states[WINED3DTEXSTA_MINFILTER] = samplerStates[WINED3DSAMP_MINFILTER];
449         gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = samplerStates[WINED3DSAMP_MAXMIPLEVEL];
450
451         if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC
452             || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_ANISOTROPIC)
453         {
454
455             FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %d D3DSAMP_MIPFILTER value %d\n",
456                   gl_tex->states[WINED3DTEXSTA_MINFILTER],
457                   gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
458         }
459         glValue = wined3d_gl_min_mip_filter(This->baseTexture.minMipLookup,
460                 min(max(samplerStates[WINED3DSAMP_MINFILTER], WINED3DTEXF_POINT), WINED3DTEXF_LINEAR),
461                 min(max(samplerStates[WINED3DSAMP_MIPFILTER], WINED3DTEXF_NONE), WINED3DTEXF_LINEAR));
462
463         TRACE("ValueMIN=%d, ValueMIP=%d, setting MINFILTER to %x\n",
464               samplerStates[WINED3DSAMP_MINFILTER],
465               samplerStates[WINED3DSAMP_MIPFILTER], glValue);
466         glTexParameteri(textureDimensions, GL_TEXTURE_MIN_FILTER, glValue);
467         checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
468
469         if (!cond_np2)
470         {
471             if (gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE)
472                 glValue = This->baseTexture.LOD;
473             else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= This->baseTexture.level_count)
474                 glValue = This->baseTexture.level_count - 1;
475             else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < This->baseTexture.LOD)
476                 /* baseTexture.LOD is already clamped in the setter */
477                 glValue = This->baseTexture.LOD;
478             else
479                 glValue = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
480
481             /* Note that D3DSAMP_MAXMIPLEVEL specifies the biggest mipmap(default 0), while
482              * GL_TEXTURE_MAX_LEVEL specifies the smallest mimap used(default 1000).
483              * So D3DSAMP_MAXMIPLEVEL is the same as GL_TEXTURE_BASE_LEVEL.
484              */
485             glTexParameteri(textureDimensions, GL_TEXTURE_BASE_LEVEL, glValue);
486         }
487     }
488
489     if ((gl_tex->states[WINED3DTEXSTA_MAGFILTER] != WINED3DTEXF_ANISOTROPIC
490          && gl_tex->states[WINED3DTEXSTA_MINFILTER] != WINED3DTEXF_ANISOTROPIC
491          && gl_tex->states[WINED3DTEXSTA_MIPFILTER] != WINED3DTEXF_ANISOTROPIC)
492             || cond_np2)
493     {
494         aniso = 1;
495     }
496     else
497     {
498         aniso = samplerStates[WINED3DSAMP_MAXANISOTROPY];
499     }
500
501     if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
502     {
503         if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
504         {
505             glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
506             checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
507         }
508         else
509         {
510             WARN("Anisotropic filtering not supported.\n");
511         }
512         gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;
513     }
514
515     if (!(This->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
516             != !gl_tex->states[WINED3DTEXSTA_SHADOW])
517     {
518         if (This->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
519         {
520             glTexParameteri(textureDimensions, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
521             glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
522             checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
523             gl_tex->states[WINED3DTEXSTA_SHADOW] = TRUE;
524         }
525         else
526         {
527             glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
528             checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
529             gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
530         }
531     }
532 }