2 * IWineD3DBaseTexture Implementation
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
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.
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.
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
26 #include "wined3d_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
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)
37 hr = resource_init((IWineD3DResource *)texture, resource_type, device,
38 0, usage, format, pool, parent, parent_ops);
41 WARN("Failed to initialize resource, returning %#x\n", hr);
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)
49 ERR("Failed to allocate sub-resource array.\n");
50 resource_cleanup((IWineD3DResource *)texture);
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;
63 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
65 texture->baseTexture.minMipLookup = minMipLookup;
66 texture->baseTexture.magLookup = magLookup;
70 texture->baseTexture.minMipLookup = minMipLookup_noFilter;
71 texture->baseTexture.magLookup = magLookup_noFilter;
77 void basetexture_cleanup(IWineD3DBaseTextureImpl *texture)
79 basetexture_unload(texture);
80 HeapFree(GetProcessHeap(), 0, texture->baseTexture.sub_resources);
81 resource_cleanup((IWineD3DResource *)texture);
84 IWineD3DResourceImpl *basetexture_get_sub_resource(IWineD3DBaseTextureImpl *texture, UINT sub_resource_idx)
86 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
88 if (sub_resource_idx >= sub_count)
90 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
94 return texture->baseTexture.sub_resources[sub_resource_idx];
97 /* A GL context is provided by the caller */
98 static void gltexture_delete(struct gl_texture *tex)
101 glDeleteTextures(1, &tex->name);
106 void basetexture_unload(IWineD3DBaseTextureImpl *texture)
108 IWineD3DDeviceImpl *device = texture->resource.device;
109 struct wined3d_context *context = NULL;
111 if (texture->baseTexture.texture_rgb.name || texture->baseTexture.texture_srgb.name)
113 context = context_acquire(device, NULL);
116 if (texture->baseTexture.texture_rgb.name)
117 gltexture_delete(&texture->baseTexture.texture_rgb);
119 if (texture->baseTexture.texture_srgb.name)
120 gltexture_delete(&texture->baseTexture.texture_srgb);
122 if (context) context_release(context);
124 texture->baseTexture.texture_rgb.dirty = TRUE;
125 texture->baseTexture.texture_srgb.dirty = TRUE;
127 resource_unload((IWineD3DResourceImpl *)texture);
130 DWORD basetexture_set_lod(IWineD3DBaseTextureImpl *texture, DWORD lod)
132 DWORD old = texture->baseTexture.LOD;
134 TRACE("texture %p, lod %u.\n", texture, lod);
136 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
137 * textures. The call always returns 0, and GetLOD always returns 0. */
138 if (texture->resource.pool != WINED3DPOOL_MANAGED)
140 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
144 if (lod >= texture->baseTexture.level_count)
145 lod = texture->baseTexture.level_count - 1;
147 if (texture->baseTexture.LOD != lod)
149 texture->baseTexture.LOD = lod;
151 texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
152 texture->baseTexture.texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
153 if (texture->baseTexture.bindCount)
154 IWineD3DDeviceImpl_MarkStateDirty(texture->resource.device, STATE_SAMPLER(texture->baseTexture.sampler));
160 DWORD basetexture_get_lod(IWineD3DBaseTextureImpl *texture)
162 TRACE("texture %p, returning %u.\n", texture, texture->baseTexture.LOD);
164 return texture->baseTexture.LOD;
167 DWORD basetexture_get_level_count(IWineD3DBaseTextureImpl *texture)
169 TRACE("texture %p, returning %u.\n", texture, texture->baseTexture.level_count);
170 return texture->baseTexture.level_count;
173 HRESULT basetexture_set_autogen_filter_type(IWineD3DBaseTextureImpl *texture, WINED3DTEXTUREFILTERTYPE filter_type)
175 TRACE("texture %p, filter_type %s.\n", texture, debug_d3dtexturefiltertype(filter_type));
177 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
179 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
180 return WINED3DERR_INVALIDCALL;
183 if (texture->baseTexture.filterType != filter_type)
185 GLenum target = texture->baseTexture.target;
186 struct wined3d_context *context;
188 context = context_acquire(texture->resource.device, NULL);
191 glBindTexture(target, texture->baseTexture.texture_rgb.name);
192 checkGLcall("glBindTexture");
195 case WINED3DTEXF_NONE:
196 case WINED3DTEXF_POINT:
197 glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST);
198 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST)");
201 case WINED3DTEXF_LINEAR:
202 glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
203 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
207 WARN("Unexpected filter type %#x, setting to GL_NICEST.\n", filter_type);
208 glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
209 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
214 context_release(context);
216 texture->baseTexture.filterType = filter_type;
221 WINED3DTEXTUREFILTERTYPE basetexture_get_autogen_filter_type(IWineD3DBaseTextureImpl *texture)
223 TRACE("texture %p.\n", texture);
225 return texture->baseTexture.filterType;
228 void basetexture_generate_mipmaps(IWineD3DBaseTextureImpl *texture)
230 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
231 FIXME("texture %p stub!\n", texture);
234 BOOL basetexture_set_dirty(IWineD3DBaseTextureImpl *texture, BOOL dirty)
238 old = texture->baseTexture.texture_rgb.dirty || texture->baseTexture.texture_srgb.dirty;
239 texture->baseTexture.texture_rgb.dirty = dirty;
240 texture->baseTexture.texture_srgb.dirty = dirty;
245 /* Context activation is done by the caller. */
246 HRESULT basetexture_bind(IWineD3DBaseTextureImpl *texture, BOOL srgb, BOOL *set_surface_desc)
248 HRESULT hr = WINED3D_OK;
249 GLenum textureDimensions;
250 BOOL isNewTexture = FALSE;
251 struct gl_texture *gl_tex;
253 TRACE("texture %p, srgb %#x, set_surface_desc %p.\n", texture, srgb, set_surface_desc);
255 texture->baseTexture.is_srgb = srgb; /* SRGB mode cache for PreLoad calls outside drawprim */
257 gl_tex = &texture->baseTexture.texture_srgb;
259 gl_tex = &texture->baseTexture.texture_rgb;
261 textureDimensions = texture->baseTexture.target;
264 /* Generate a texture name if we don't already have one */
267 *set_surface_desc = TRUE;
268 glGenTextures(1, &gl_tex->name);
269 checkGLcall("glGenTextures");
270 TRACE("Generated texture %d\n", gl_tex->name);
271 if (texture->resource.pool == WINED3DPOOL_DEFAULT)
273 /* Tell opengl to try and keep this texture in video ram (well mostly) */
276 glPrioritizeTextures(1, &gl_tex->name, &tmp);
279 /* Initialise the state of the texture object
280 to the openGL defaults, not the directx defaults */
281 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3DTADDRESS_WRAP;
282 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3DTADDRESS_WRAP;
283 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = WINED3DTADDRESS_WRAP;
284 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = 0;
285 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_LINEAR;
286 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
287 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
288 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = 0;
289 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
290 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = 0;
291 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
292 basetexture_set_dirty(texture, TRUE);
295 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
297 /* This means double binding the texture at creation, but keeps the code simpler all
298 * in all, and the run-time path free from additional checks
300 glBindTexture(textureDimensions, gl_tex->name);
301 checkGLcall("glBindTexture");
302 glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
303 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
306 *set_surface_desc = FALSE;
309 /* Bind the texture */
312 glBindTexture(textureDimensions, gl_tex->name);
313 checkGLcall("glBindTexture");
315 /* For a new texture we have to set the textures levels after binding the texture.
316 * In theory this is all we should ever have to do, but because ATI's drivers are broken, we
317 * also need to set the texture dimensions before the texture is set
318 * Beware that texture rectangles do not support mipmapping, but set the maxmiplevel if we're
319 * relying on the partial GL_ARB_texture_non_power_of_two emulation with texture rectangles
320 * (ie, do not care for cond_np2 here, just look for GL_TEXTURE_RECTANGLE_ARB)
322 if (textureDimensions != GL_TEXTURE_RECTANGLE_ARB)
324 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->baseTexture.level_count - 1);
325 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, texture->baseTexture.level_count - 1);
326 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, texture->baseTexture.level_count)");
328 if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
329 /* Cubemaps are always set to clamp, regardless of the sampler state. */
330 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
331 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
332 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
335 } else { /* this only happened if we've run out of openGL textures */
336 WARN("This texture doesn't have an openGL texture assigned to it\n");
337 hr = WINED3DERR_INVALIDCALL;
344 /* GL locking is done by the caller */
345 static void apply_wrap(const struct wined3d_gl_info *gl_info, GLenum target,
346 WINED3DTEXTUREADDRESS d3d_wrap, GLenum param, BOOL cond_np2)
350 if (d3d_wrap < WINED3DTADDRESS_WRAP || d3d_wrap > WINED3DTADDRESS_MIRRORONCE)
352 FIXME("Unrecognized or unsupported WINED3DTEXTUREADDRESS %#x.\n", d3d_wrap);
356 if (target == GL_TEXTURE_CUBE_MAP_ARB
357 || (cond_np2 && d3d_wrap == WINED3DTADDRESS_WRAP))
359 /* Cubemaps are always set to clamp, regardless of the sampler state. */
360 gl_wrap = GL_CLAMP_TO_EDGE;
364 gl_wrap = gl_info->wrap_lookup[d3d_wrap - WINED3DTADDRESS_WRAP];
367 TRACE("Setting param %#x to %#x for target %#x.\n", param, gl_wrap, target);
368 glTexParameteri(target, param, gl_wrap);
369 checkGLcall("glTexParameteri(target, param, gl_wrap)");
372 /* GL locking is done by the caller (state handler) */
373 void basetexture_apply_state_changes(IWineD3DBaseTextureImpl *texture,
374 const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1],
375 const struct wined3d_gl_info *gl_info)
377 BOOL cond_np2 = IWineD3DBaseTexture_IsCondNP2((IWineD3DBaseTexture *)texture);
378 GLenum textureDimensions = texture->baseTexture.target;
381 struct gl_texture *gl_tex;
383 TRACE("texture %p, samplerStates %p\n", texture, samplerStates);
385 if (texture->baseTexture.is_srgb)
386 gl_tex = &texture->baseTexture.texture_srgb;
388 gl_tex = &texture->baseTexture.texture_rgb;
390 /* This function relies on the correct texture being bound and loaded. */
392 if(samplerStates[WINED3DSAMP_ADDRESSU] != gl_tex->states[WINED3DTEXSTA_ADDRESSU]) {
393 state = samplerStates[WINED3DSAMP_ADDRESSU];
394 apply_wrap(gl_info, textureDimensions, state, GL_TEXTURE_WRAP_S, cond_np2);
395 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
398 if(samplerStates[WINED3DSAMP_ADDRESSV] != gl_tex->states[WINED3DTEXSTA_ADDRESSV]) {
399 state = samplerStates[WINED3DSAMP_ADDRESSV];
400 apply_wrap(gl_info, textureDimensions, state, GL_TEXTURE_WRAP_T, cond_np2);
401 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
404 if(samplerStates[WINED3DSAMP_ADDRESSW] != gl_tex->states[WINED3DTEXSTA_ADDRESSW]) {
405 state = samplerStates[WINED3DSAMP_ADDRESSW];
406 apply_wrap(gl_info, textureDimensions, state, GL_TEXTURE_WRAP_R, cond_np2);
407 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
410 if(samplerStates[WINED3DSAMP_BORDERCOLOR] != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR]) {
413 state = samplerStates[WINED3DSAMP_BORDERCOLOR];
414 D3DCOLORTOGLFLOAT4(state, col);
415 TRACE("Setting border color for %u to %x\n", textureDimensions, state);
416 glTexParameterfv(textureDimensions, GL_TEXTURE_BORDER_COLOR, &col[0]);
417 checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
418 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
421 if(samplerStates[WINED3DSAMP_MAGFILTER] != gl_tex->states[WINED3DTEXSTA_MAGFILTER]) {
423 state = samplerStates[WINED3DSAMP_MAGFILTER];
424 if (state > WINED3DTEXF_ANISOTROPIC) {
425 FIXME("Unrecognized or unsupported MAGFILTER* value %d\n", state);
428 glValue = wined3d_gl_mag_filter(texture->baseTexture.magLookup,
429 min(max(state, WINED3DTEXF_POINT), WINED3DTEXF_LINEAR));
430 TRACE("ValueMAG=%d setting MAGFILTER to %x\n", state, glValue);
431 glTexParameteri(textureDimensions, GL_TEXTURE_MAG_FILTER, glValue);
433 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
436 if((samplerStates[WINED3DSAMP_MINFILTER] != gl_tex->states[WINED3DTEXSTA_MINFILTER] ||
437 samplerStates[WINED3DSAMP_MIPFILTER] != gl_tex->states[WINED3DTEXSTA_MIPFILTER] ||
438 samplerStates[WINED3DSAMP_MAXMIPLEVEL] != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL])) {
441 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = samplerStates[WINED3DSAMP_MIPFILTER];
442 gl_tex->states[WINED3DTEXSTA_MINFILTER] = samplerStates[WINED3DSAMP_MINFILTER];
443 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = samplerStates[WINED3DSAMP_MAXMIPLEVEL];
445 if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC
446 || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_ANISOTROPIC)
449 FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %d D3DSAMP_MIPFILTER value %d\n",
450 gl_tex->states[WINED3DTEXSTA_MINFILTER],
451 gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
453 glValue = wined3d_gl_min_mip_filter(texture->baseTexture.minMipLookup,
454 min(max(samplerStates[WINED3DSAMP_MINFILTER], WINED3DTEXF_POINT), WINED3DTEXF_LINEAR),
455 min(max(samplerStates[WINED3DSAMP_MIPFILTER], WINED3DTEXF_NONE), WINED3DTEXF_LINEAR));
457 TRACE("ValueMIN=%d, ValueMIP=%d, setting MINFILTER to %x\n",
458 samplerStates[WINED3DSAMP_MINFILTER],
459 samplerStates[WINED3DSAMP_MIPFILTER], glValue);
460 glTexParameteri(textureDimensions, GL_TEXTURE_MIN_FILTER, glValue);
461 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
465 if (gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE)
466 glValue = texture->baseTexture.LOD;
467 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= texture->baseTexture.level_count)
468 glValue = texture->baseTexture.level_count - 1;
469 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < texture->baseTexture.LOD)
470 /* baseTexture.LOD is already clamped in the setter */
471 glValue = texture->baseTexture.LOD;
473 glValue = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
475 /* Note that D3DSAMP_MAXMIPLEVEL specifies the biggest mipmap(default 0), while
476 * GL_TEXTURE_MAX_LEVEL specifies the smallest mimap used(default 1000).
477 * So D3DSAMP_MAXMIPLEVEL is the same as GL_TEXTURE_BASE_LEVEL.
479 glTexParameteri(textureDimensions, GL_TEXTURE_BASE_LEVEL, glValue);
483 if ((gl_tex->states[WINED3DTEXSTA_MAGFILTER] != WINED3DTEXF_ANISOTROPIC
484 && gl_tex->states[WINED3DTEXSTA_MINFILTER] != WINED3DTEXF_ANISOTROPIC
485 && gl_tex->states[WINED3DTEXSTA_MIPFILTER] != WINED3DTEXF_ANISOTROPIC)
492 aniso = samplerStates[WINED3DSAMP_MAXANISOTROPY];
495 if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
497 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
499 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
500 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
504 WARN("Anisotropic filtering not supported.\n");
506 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;
509 if (!(texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
510 != !gl_tex->states[WINED3DTEXSTA_SHADOW])
512 if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
514 glTexParameteri(textureDimensions, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
515 glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
516 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
517 gl_tex->states[WINED3DTEXSTA_SHADOW] = TRUE;
521 glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
522 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
523 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;