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, const struct wined3d_texture_ops *texture_ops,
31 UINT layer_count, UINT level_count, WINED3DRESOURCETYPE resource_type, IWineD3DDeviceImpl *device,
32 DWORD usage, const struct wined3d_format *format, WINED3DPOOL pool, void *parent,
33 const struct wined3d_parent_ops *parent_ops)
37 hr = resource_init((IWineD3DResourceImpl *)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.texture_ops = texture_ops;
46 texture->baseTexture.sub_resources = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
47 level_count * layer_count * sizeof(*texture->baseTexture.sub_resources));
48 if (!texture->baseTexture.sub_resources)
50 ERR("Failed to allocate sub-resource array.\n");
51 resource_cleanup((IWineD3DResourceImpl *)texture);
55 texture->baseTexture.layer_count = layer_count;
56 texture->baseTexture.level_count = level_count;
57 texture->baseTexture.filterType = (usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3DTEXF_LINEAR : WINED3DTEXF_NONE;
58 texture->baseTexture.LOD = 0;
59 texture->baseTexture.texture_rgb.dirty = TRUE;
60 texture->baseTexture.texture_srgb.dirty = TRUE;
61 texture->baseTexture.is_srgb = FALSE;
62 texture->baseTexture.pow2Matrix_identity = TRUE;
64 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
66 texture->baseTexture.minMipLookup = minMipLookup;
67 texture->baseTexture.magLookup = magLookup;
71 texture->baseTexture.minMipLookup = minMipLookup_noFilter;
72 texture->baseTexture.magLookup = magLookup_noFilter;
78 void basetexture_cleanup(IWineD3DBaseTextureImpl *texture)
80 basetexture_unload(texture);
81 HeapFree(GetProcessHeap(), 0, texture->baseTexture.sub_resources);
82 resource_cleanup((IWineD3DResourceImpl *)texture);
85 IWineD3DResourceImpl *basetexture_get_sub_resource(IWineD3DBaseTextureImpl *texture, UINT sub_resource_idx)
87 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
89 if (sub_resource_idx >= sub_count)
91 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
95 return texture->baseTexture.sub_resources[sub_resource_idx];
98 /* A GL context is provided by the caller */
99 static void gltexture_delete(struct gl_texture *tex)
102 glDeleteTextures(1, &tex->name);
107 void basetexture_unload(IWineD3DBaseTextureImpl *texture)
109 IWineD3DDeviceImpl *device = texture->resource.device;
110 struct wined3d_context *context = NULL;
112 if (texture->baseTexture.texture_rgb.name || texture->baseTexture.texture_srgb.name)
114 context = context_acquire(device, NULL);
117 if (texture->baseTexture.texture_rgb.name)
118 gltexture_delete(&texture->baseTexture.texture_rgb);
120 if (texture->baseTexture.texture_srgb.name)
121 gltexture_delete(&texture->baseTexture.texture_srgb);
123 if (context) context_release(context);
125 texture->baseTexture.texture_rgb.dirty = TRUE;
126 texture->baseTexture.texture_srgb.dirty = TRUE;
128 resource_unload((IWineD3DResourceImpl *)texture);
131 DWORD basetexture_set_lod(IWineD3DBaseTextureImpl *texture, DWORD lod)
133 DWORD old = texture->baseTexture.LOD;
135 TRACE("texture %p, lod %u.\n", texture, lod);
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 if (texture->resource.pool != WINED3DPOOL_MANAGED)
141 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
145 if (lod >= texture->baseTexture.level_count)
146 lod = texture->baseTexture.level_count - 1;
148 if (texture->baseTexture.LOD != lod)
150 texture->baseTexture.LOD = lod;
152 texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
153 texture->baseTexture.texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
154 if (texture->baseTexture.bindCount)
155 IWineD3DDeviceImpl_MarkStateDirty(texture->resource.device, STATE_SAMPLER(texture->baseTexture.sampler));
161 DWORD basetexture_get_lod(IWineD3DBaseTextureImpl *texture)
163 TRACE("texture %p, returning %u.\n", texture, texture->baseTexture.LOD);
165 return texture->baseTexture.LOD;
168 DWORD basetexture_get_level_count(IWineD3DBaseTextureImpl *texture)
170 TRACE("texture %p, returning %u.\n", texture, texture->baseTexture.level_count);
171 return texture->baseTexture.level_count;
174 HRESULT basetexture_set_autogen_filter_type(IWineD3DBaseTextureImpl *texture, WINED3DTEXTUREFILTERTYPE filter_type)
176 TRACE("texture %p, filter_type %s.\n", texture, debug_d3dtexturefiltertype(filter_type));
178 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
180 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
181 return WINED3DERR_INVALIDCALL;
184 if (texture->baseTexture.filterType != filter_type)
186 GLenum target = texture->baseTexture.target;
187 struct wined3d_context *context;
189 context = context_acquire(texture->resource.device, NULL);
192 glBindTexture(target, texture->baseTexture.texture_rgb.name);
193 checkGLcall("glBindTexture");
196 case WINED3DTEXF_NONE:
197 case WINED3DTEXF_POINT:
198 glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST);
199 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST)");
202 case WINED3DTEXF_LINEAR:
203 glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
204 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
208 WARN("Unexpected filter type %#x, setting to GL_NICEST.\n", filter_type);
209 glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
210 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
215 context_release(context);
217 texture->baseTexture.filterType = filter_type;
222 WINED3DTEXTUREFILTERTYPE basetexture_get_autogen_filter_type(IWineD3DBaseTextureImpl *texture)
224 TRACE("texture %p.\n", texture);
226 return texture->baseTexture.filterType;
229 void basetexture_generate_mipmaps(IWineD3DBaseTextureImpl *texture)
231 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
232 FIXME("texture %p stub!\n", texture);
235 BOOL basetexture_set_dirty(IWineD3DBaseTextureImpl *texture, BOOL dirty)
239 old = texture->baseTexture.texture_rgb.dirty || texture->baseTexture.texture_srgb.dirty;
240 texture->baseTexture.texture_rgb.dirty = dirty;
241 texture->baseTexture.texture_srgb.dirty = dirty;
246 /* Context activation is done by the caller. */
247 HRESULT basetexture_bind(IWineD3DBaseTextureImpl *texture, BOOL srgb, BOOL *set_surface_desc)
249 HRESULT hr = WINED3D_OK;
250 GLenum textureDimensions;
251 BOOL isNewTexture = FALSE;
252 struct gl_texture *gl_tex;
254 TRACE("texture %p, srgb %#x, set_surface_desc %p.\n", texture, srgb, set_surface_desc);
256 texture->baseTexture.is_srgb = srgb; /* SRGB mode cache for PreLoad calls outside drawprim */
258 gl_tex = &texture->baseTexture.texture_srgb;
260 gl_tex = &texture->baseTexture.texture_rgb;
262 textureDimensions = texture->baseTexture.target;
265 /* Generate a texture name if we don't already have one */
268 *set_surface_desc = TRUE;
269 glGenTextures(1, &gl_tex->name);
270 checkGLcall("glGenTextures");
271 TRACE("Generated texture %d\n", gl_tex->name);
272 if (texture->resource.pool == WINED3DPOOL_DEFAULT)
274 /* Tell opengl to try and keep this texture in video ram (well mostly) */
277 glPrioritizeTextures(1, &gl_tex->name, &tmp);
280 /* Initialise the state of the texture object
281 to the openGL defaults, not the directx defaults */
282 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3DTADDRESS_WRAP;
283 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3DTADDRESS_WRAP;
284 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = WINED3DTADDRESS_WRAP;
285 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = 0;
286 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_LINEAR;
287 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
288 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
289 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = 0;
290 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
291 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = 0;
292 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
293 basetexture_set_dirty(texture, TRUE);
296 if (texture->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
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)");
307 *set_surface_desc = FALSE;
310 /* Bind the texture */
313 glBindTexture(textureDimensions, gl_tex->name);
314 checkGLcall("glBindTexture");
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)
323 if (textureDimensions != GL_TEXTURE_RECTANGLE_ARB)
325 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->baseTexture.level_count - 1);
326 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, texture->baseTexture.level_count - 1);
327 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, texture->baseTexture.level_count)");
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);
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;
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)
351 if (d3d_wrap < WINED3DTADDRESS_WRAP || d3d_wrap > WINED3DTADDRESS_MIRRORONCE)
353 FIXME("Unrecognized or unsupported WINED3DTEXTUREADDRESS %#x.\n", d3d_wrap);
357 if (target == GL_TEXTURE_CUBE_MAP_ARB
358 || (cond_np2 && d3d_wrap == WINED3DTADDRESS_WRAP))
360 /* Cubemaps are always set to clamp, regardless of the sampler state. */
361 gl_wrap = GL_CLAMP_TO_EDGE;
365 gl_wrap = gl_info->wrap_lookup[d3d_wrap - WINED3DTADDRESS_WRAP];
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)");
373 /* GL locking is done by the caller (state handler) */
374 void basetexture_apply_state_changes(IWineD3DBaseTextureImpl *texture,
375 const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1],
376 const struct wined3d_gl_info *gl_info)
378 BOOL cond_np2 = IWineD3DBaseTexture_IsCondNP2((IWineD3DBaseTexture *)texture);
379 GLenum textureDimensions = texture->baseTexture.target;
382 struct gl_texture *gl_tex;
384 TRACE("texture %p, samplerStates %p\n", texture, samplerStates);
386 if (texture->baseTexture.is_srgb)
387 gl_tex = &texture->baseTexture.texture_srgb;
389 gl_tex = &texture->baseTexture.texture_rgb;
391 /* This function relies on the correct texture being bound and loaded. */
393 if(samplerStates[WINED3DSAMP_ADDRESSU] != gl_tex->states[WINED3DTEXSTA_ADDRESSU]) {
394 state = samplerStates[WINED3DSAMP_ADDRESSU];
395 apply_wrap(gl_info, textureDimensions, state, GL_TEXTURE_WRAP_S, cond_np2);
396 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
399 if(samplerStates[WINED3DSAMP_ADDRESSV] != gl_tex->states[WINED3DTEXSTA_ADDRESSV]) {
400 state = samplerStates[WINED3DSAMP_ADDRESSV];
401 apply_wrap(gl_info, textureDimensions, state, GL_TEXTURE_WRAP_T, cond_np2);
402 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
405 if(samplerStates[WINED3DSAMP_ADDRESSW] != gl_tex->states[WINED3DTEXSTA_ADDRESSW]) {
406 state = samplerStates[WINED3DSAMP_ADDRESSW];
407 apply_wrap(gl_info, textureDimensions, state, GL_TEXTURE_WRAP_R, cond_np2);
408 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
411 if(samplerStates[WINED3DSAMP_BORDERCOLOR] != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR]) {
414 state = samplerStates[WINED3DSAMP_BORDERCOLOR];
415 D3DCOLORTOGLFLOAT4(state, col);
416 TRACE("Setting border color for %u to %x\n", textureDimensions, state);
417 glTexParameterfv(textureDimensions, GL_TEXTURE_BORDER_COLOR, &col[0]);
418 checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
419 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
422 if(samplerStates[WINED3DSAMP_MAGFILTER] != gl_tex->states[WINED3DTEXSTA_MAGFILTER]) {
424 state = samplerStates[WINED3DSAMP_MAGFILTER];
425 if (state > WINED3DTEXF_ANISOTROPIC) {
426 FIXME("Unrecognized or unsupported MAGFILTER* value %d\n", state);
429 glValue = wined3d_gl_mag_filter(texture->baseTexture.magLookup,
430 min(max(state, WINED3DTEXF_POINT), WINED3DTEXF_LINEAR));
431 TRACE("ValueMAG=%d setting MAGFILTER to %x\n", state, glValue);
432 glTexParameteri(textureDimensions, GL_TEXTURE_MAG_FILTER, glValue);
434 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
437 if((samplerStates[WINED3DSAMP_MINFILTER] != gl_tex->states[WINED3DTEXSTA_MINFILTER] ||
438 samplerStates[WINED3DSAMP_MIPFILTER] != gl_tex->states[WINED3DTEXSTA_MIPFILTER] ||
439 samplerStates[WINED3DSAMP_MAXMIPLEVEL] != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL])) {
442 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = samplerStates[WINED3DSAMP_MIPFILTER];
443 gl_tex->states[WINED3DTEXSTA_MINFILTER] = samplerStates[WINED3DSAMP_MINFILTER];
444 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = samplerStates[WINED3DSAMP_MAXMIPLEVEL];
446 if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC
447 || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_ANISOTROPIC)
450 FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %d D3DSAMP_MIPFILTER value %d\n",
451 gl_tex->states[WINED3DTEXSTA_MINFILTER],
452 gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
454 glValue = wined3d_gl_min_mip_filter(texture->baseTexture.minMipLookup,
455 min(max(samplerStates[WINED3DSAMP_MINFILTER], WINED3DTEXF_POINT), WINED3DTEXF_LINEAR),
456 min(max(samplerStates[WINED3DSAMP_MIPFILTER], WINED3DTEXF_NONE), WINED3DTEXF_LINEAR));
458 TRACE("ValueMIN=%d, ValueMIP=%d, setting MINFILTER to %x\n",
459 samplerStates[WINED3DSAMP_MINFILTER],
460 samplerStates[WINED3DSAMP_MIPFILTER], glValue);
461 glTexParameteri(textureDimensions, GL_TEXTURE_MIN_FILTER, glValue);
462 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
466 if (gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE)
467 glValue = texture->baseTexture.LOD;
468 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= texture->baseTexture.level_count)
469 glValue = texture->baseTexture.level_count - 1;
470 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < texture->baseTexture.LOD)
471 /* baseTexture.LOD is already clamped in the setter */
472 glValue = texture->baseTexture.LOD;
474 glValue = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
476 /* Note that D3DSAMP_MAXMIPLEVEL specifies the biggest mipmap(default 0), while
477 * GL_TEXTURE_MAX_LEVEL specifies the smallest mimap used(default 1000).
478 * So D3DSAMP_MAXMIPLEVEL is the same as GL_TEXTURE_BASE_LEVEL.
480 glTexParameteri(textureDimensions, GL_TEXTURE_BASE_LEVEL, glValue);
484 if ((gl_tex->states[WINED3DTEXSTA_MAGFILTER] != WINED3DTEXF_ANISOTROPIC
485 && gl_tex->states[WINED3DTEXSTA_MINFILTER] != WINED3DTEXF_ANISOTROPIC
486 && gl_tex->states[WINED3DTEXSTA_MIPFILTER] != WINED3DTEXF_ANISOTROPIC)
493 aniso = samplerStates[WINED3DSAMP_MAXANISOTROPY];
496 if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
498 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
500 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
501 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
505 WARN("Anisotropic filtering not supported.\n");
507 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;
510 if (!(texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
511 != !gl_tex->states[WINED3DTEXSTA_SHADOW])
513 if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
515 glTexParameteri(textureDimensions, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
516 glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
517 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
518 gl_tex->states[WINED3DTEXSTA_SHADOW] = TRUE;
522 glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
523 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
524 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;