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