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 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 levels, WINED3DRESOURCETYPE resource_type,
31 IWineD3DDeviceImpl *device, UINT size, DWORD usage, const struct wined3d_format_desc *format_desc,
32 WINED3DPOOL pool, IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
36 hr = resource_init((IWineD3DResource *)texture, resource_type, device,
37 size, usage, format_desc, pool, parent, parent_ops);
40 WARN("Failed to initialize resource, returning %#x\n", hr);
44 texture->baseTexture.levels = levels;
45 texture->baseTexture.filterType = (usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3DTEXF_LINEAR : WINED3DTEXF_NONE;
46 texture->baseTexture.LOD = 0;
47 texture->baseTexture.texture_rgb.dirty = TRUE;
48 texture->baseTexture.texture_srgb.dirty = TRUE;
49 texture->baseTexture.is_srgb = FALSE;
50 texture->baseTexture.pow2Matrix_identity = TRUE;
52 if (texture->resource.format_desc->Flags & WINED3DFMT_FLAG_FILTERING)
54 texture->baseTexture.minMipLookup = minMipLookup;
55 texture->baseTexture.magLookup = magLookup;
59 texture->baseTexture.minMipLookup = minMipLookup_noFilter;
60 texture->baseTexture.magLookup = magLookup_noFilter;
66 void basetexture_cleanup(IWineD3DBaseTexture *iface)
68 basetexture_unload(iface);
69 resource_cleanup((IWineD3DResource *)iface);
72 /* A GL context is provided by the caller */
73 static void gltexture_delete(struct gl_texture *tex)
76 glDeleteTextures(1, &tex->name);
81 void basetexture_unload(IWineD3DBaseTexture *iface)
83 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
84 IWineD3DDeviceImpl *device = This->resource.device;
85 struct wined3d_context *context = NULL;
87 if (This->baseTexture.texture_rgb.name || This->baseTexture.texture_srgb.name)
89 context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
92 if(This->baseTexture.texture_rgb.name) {
93 gltexture_delete(&This->baseTexture.texture_rgb);
95 if(This->baseTexture.texture_srgb.name) {
96 gltexture_delete(&This->baseTexture.texture_srgb);
99 if (context) context_release(context);
101 This->baseTexture.texture_rgb.dirty = TRUE;
102 This->baseTexture.texture_srgb.dirty = TRUE;
105 DWORD basetexture_set_lod(IWineD3DBaseTexture *iface, DWORD LODNew)
107 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
108 DWORD old = This->baseTexture.LOD;
110 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
111 * textures. The call always returns 0, and GetLOD always returns 0
113 if (This->resource.pool != WINED3DPOOL_MANAGED) {
114 TRACE("Ignoring SetLOD on %s texture, returning 0\n", debug_d3dpool(This->resource.pool));
118 if(LODNew >= This->baseTexture.levels)
119 LODNew = This->baseTexture.levels - 1;
121 if(This->baseTexture.LOD != LODNew) {
122 This->baseTexture.LOD = LODNew;
124 This->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
125 This->baseTexture.texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
126 if(This->baseTexture.bindCount) {
127 IWineD3DDeviceImpl_MarkStateDirty(This->resource.device, STATE_SAMPLER(This->baseTexture.sampler));
131 TRACE("(%p) : set LOD to %d\n", This, This->baseTexture.LOD);
136 DWORD basetexture_get_lod(IWineD3DBaseTexture *iface)
138 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
140 TRACE("(%p) : returning %d\n", This, This->baseTexture.LOD);
142 return This->baseTexture.LOD;
145 DWORD basetexture_get_level_count(IWineD3DBaseTexture *iface)
147 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
148 TRACE("(%p) : returning %d\n", This, This->baseTexture.levels);
149 return This->baseTexture.levels;
152 HRESULT basetexture_set_autogen_filter_type(IWineD3DBaseTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType)
154 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
155 IWineD3DDeviceImpl *device = This->resource.device;
156 UINT textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
158 if (!(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)) {
159 TRACE("(%p) : returning invalid call\n", This);
160 return WINED3DERR_INVALIDCALL;
162 if(This->baseTexture.filterType != FilterType) {
163 /* What about multithreading? Do we want all the context overhead just to set this value?
164 * Or should we delay the applying until the texture is used for drawing? For now, apply
167 struct wined3d_context *context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
170 glBindTexture(textureDimensions, This->baseTexture.texture_rgb.name);
171 checkGLcall("glBindTexture");
173 case WINED3DTEXF_NONE:
174 case WINED3DTEXF_POINT:
175 glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST);
176 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST)");
179 case WINED3DTEXF_LINEAR:
180 glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
181 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
185 WARN("Unexpected filter type %d, setting to GL_NICEST\n", FilterType);
186 glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
187 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
191 context_release(context);
193 This->baseTexture.filterType = FilterType;
194 TRACE("(%p) :\n", This);
198 WINED3DTEXTUREFILTERTYPE basetexture_get_autogen_filter_type(IWineD3DBaseTexture *iface)
200 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
202 FIXME("(%p) : stub\n", This);
204 return This->baseTexture.filterType;
207 void basetexture_generate_mipmaps(IWineD3DBaseTexture *iface)
209 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
210 FIXME("iface %p stub!\n", iface);
213 BOOL basetexture_set_dirty(IWineD3DBaseTexture *iface, BOOL dirty)
216 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
217 old = This->baseTexture.texture_rgb.dirty || This->baseTexture.texture_srgb.dirty;
218 This->baseTexture.texture_rgb.dirty = dirty;
219 This->baseTexture.texture_srgb.dirty = dirty;
223 BOOL basetexture_get_dirty(IWineD3DBaseTexture *iface)
225 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
226 return This->baseTexture.texture_rgb.dirty || This->baseTexture.texture_srgb.dirty;
229 /* Context activation is done by the caller. */
230 HRESULT basetexture_bind(IWineD3DBaseTexture *iface, BOOL srgb, BOOL *set_surface_desc)
232 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
233 HRESULT hr = WINED3D_OK;
234 UINT textureDimensions;
235 BOOL isNewTexture = FALSE;
236 struct gl_texture *gl_tex;
237 TRACE("(%p) : About to bind texture\n", This);
239 This->baseTexture.is_srgb = srgb; /* SRGB mode cache for PreLoad calls outside drawprim */
241 gl_tex = &This->baseTexture.texture_srgb;
243 gl_tex = &This->baseTexture.texture_rgb;
246 textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
248 /* Generate a texture name if we don't already have one */
249 if (gl_tex->name == 0) {
250 *set_surface_desc = TRUE;
251 glGenTextures(1, &gl_tex->name);
252 checkGLcall("glGenTextures");
253 TRACE("Generated texture %d\n", gl_tex->name);
254 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
255 /* Tell opengl to try and keep this texture in video ram (well mostly) */
258 glPrioritizeTextures(1, &gl_tex->name, &tmp);
261 /* Initialise the state of the texture object
262 to the openGL defaults, not the directx defaults */
263 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3DTADDRESS_WRAP;
264 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3DTADDRESS_WRAP;
265 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = WINED3DTADDRESS_WRAP;
266 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = 0;
267 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_LINEAR;
268 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
269 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
270 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = 0;
271 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
272 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = 0;
273 gl_tex->states[WINED3DTEXSTA_ELEMENTINDEX] = 0;
274 gl_tex->states[WINED3DTEXSTA_DMAPOFFSET] = 0;
275 gl_tex->states[WINED3DTEXSTA_TSSADDRESSW] = WINED3DTADDRESS_WRAP;
276 IWineD3DBaseTexture_SetDirty(iface, TRUE);
279 if(This->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP) {
280 /* This means double binding the texture at creation, but keeps the code simpler all
281 * in all, and the run-time path free from additional checks
283 glBindTexture(textureDimensions, gl_tex->name);
284 checkGLcall("glBindTexture");
285 glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
286 checkGLcall("glTexParameteri(textureDimensions, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
289 *set_surface_desc = FALSE;
292 /* Bind the texture */
293 if (gl_tex->name != 0) {
294 glBindTexture(textureDimensions, gl_tex->name);
295 checkGLcall("glBindTexture");
297 /* For a new texture we have to set the textures levels after binding the texture.
298 * In theory this is all we should ever have to do, but because ATI's drivers are broken, we
299 * also need to set the texture dimensions before the texture is set
300 * Beware that texture rectangles do not support mipmapping, but set the maxmiplevel if we're
301 * relying on the partial GL_ARB_texture_non_power_of_two emulation with texture rectangles
302 * (ie, do not care for cond_np2 here, just look for GL_TEXTURE_RECTANGLE_ARB)
304 if(textureDimensions != GL_TEXTURE_RECTANGLE_ARB) {
305 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->baseTexture.levels - 1);
306 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels - 1);
307 checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels)");
309 if(textureDimensions==GL_TEXTURE_CUBE_MAP_ARB) {
310 /* Cubemaps are always set to clamp, regardless of the sampler state. */
311 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
312 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
313 glTexParameteri(textureDimensions, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
316 } else { /* this only happened if we've run out of openGL textures */
317 WARN("This texture doesn't have an openGL texture assigned to it\n");
318 hr = WINED3DERR_INVALIDCALL;
325 /* GL locking is done by the caller */
326 static void apply_wrap(const struct wined3d_gl_info *gl_info, GLenum target,
327 WINED3DTEXTUREADDRESS d3d_wrap, GLenum param, BOOL cond_np2)
331 if (d3d_wrap < WINED3DTADDRESS_WRAP || d3d_wrap > WINED3DTADDRESS_MIRRORONCE)
333 FIXME("Unrecognized or unsupported WINED3DTEXTUREADDRESS %#x.\n", d3d_wrap);
337 if (target == GL_TEXTURE_CUBE_MAP_ARB
338 || (cond_np2 && d3d_wrap == WINED3DTADDRESS_WRAP))
340 /* Cubemaps are always set to clamp, regardless of the sampler state. */
341 gl_wrap = GL_CLAMP_TO_EDGE;
345 gl_wrap = gl_info->wrap_lookup[d3d_wrap - WINED3DTADDRESS_WRAP];
348 TRACE("Setting param %#x to %#x for target %#x.\n", param, gl_wrap, target);
349 glTexParameteri(target, param, gl_wrap);
350 checkGLcall("glTexParameteri(target, param, gl_wrap)");
353 /* GL locking is done by the caller (state handler) */
354 void basetexture_apply_state_changes(IWineD3DBaseTexture *iface,
355 const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1],
356 const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1],
357 const struct wined3d_gl_info *gl_info)
359 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
361 GLint textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
362 BOOL cond_np2 = IWineD3DBaseTexture_IsCondNP2(iface);
364 struct gl_texture *gl_tex;
366 TRACE("iface %p, textureStates %p, samplerStates %p\n", iface, textureStates, samplerStates);
368 if(This->baseTexture.is_srgb) {
369 gl_tex = &This->baseTexture.texture_srgb;
371 gl_tex = &This->baseTexture.texture_rgb;
374 /* This function relies on the correct texture being bound and loaded. */
376 if(samplerStates[WINED3DSAMP_ADDRESSU] != gl_tex->states[WINED3DTEXSTA_ADDRESSU]) {
377 state = samplerStates[WINED3DSAMP_ADDRESSU];
378 apply_wrap(gl_info, textureDimensions, state, GL_TEXTURE_WRAP_S, cond_np2);
379 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
382 if(samplerStates[WINED3DSAMP_ADDRESSV] != gl_tex->states[WINED3DTEXSTA_ADDRESSV]) {
383 state = samplerStates[WINED3DSAMP_ADDRESSV];
384 apply_wrap(gl_info, textureDimensions, state, GL_TEXTURE_WRAP_T, cond_np2);
385 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
388 if(samplerStates[WINED3DSAMP_ADDRESSW] != gl_tex->states[WINED3DTEXSTA_ADDRESSW]) {
389 state = samplerStates[WINED3DSAMP_ADDRESSW];
390 apply_wrap(gl_info, textureDimensions, state, GL_TEXTURE_WRAP_R, cond_np2);
391 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
394 if(samplerStates[WINED3DSAMP_BORDERCOLOR] != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR]) {
397 state = samplerStates[WINED3DSAMP_BORDERCOLOR];
398 D3DCOLORTOGLFLOAT4(state, col);
399 TRACE("Setting border color for %u to %x\n", textureDimensions, state);
400 glTexParameterfv(textureDimensions, GL_TEXTURE_BORDER_COLOR, &col[0]);
401 checkGLcall("glTexParameteri(..., GL_TEXTURE_BORDER_COLOR, ...)");
402 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
405 if(samplerStates[WINED3DSAMP_MAGFILTER] != gl_tex->states[WINED3DTEXSTA_MAGFILTER]) {
407 state = samplerStates[WINED3DSAMP_MAGFILTER];
408 if (state > WINED3DTEXF_ANISOTROPIC) {
409 FIXME("Unrecognized or unsupported MAGFILTER* value %d\n", state);
412 glValue = wined3d_gl_mag_filter(This->baseTexture.magLookup,
413 min(max(state, WINED3DTEXF_POINT), WINED3DTEXF_LINEAR));
414 TRACE("ValueMAG=%d setting MAGFILTER to %x\n", state, glValue);
415 glTexParameteri(textureDimensions, GL_TEXTURE_MAG_FILTER, glValue);
417 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
420 if((samplerStates[WINED3DSAMP_MINFILTER] != gl_tex->states[WINED3DTEXSTA_MINFILTER] ||
421 samplerStates[WINED3DSAMP_MIPFILTER] != gl_tex->states[WINED3DTEXSTA_MIPFILTER] ||
422 samplerStates[WINED3DSAMP_MAXMIPLEVEL] != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL])) {
425 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = samplerStates[WINED3DSAMP_MIPFILTER];
426 gl_tex->states[WINED3DTEXSTA_MINFILTER] = samplerStates[WINED3DSAMP_MINFILTER];
427 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = samplerStates[WINED3DSAMP_MAXMIPLEVEL];
429 if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC
430 || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_ANISOTROPIC)
433 FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %d D3DSAMP_MIPFILTER value %d\n",
434 gl_tex->states[WINED3DTEXSTA_MINFILTER],
435 gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
437 glValue = wined3d_gl_min_mip_filter(This->baseTexture.minMipLookup,
438 min(max(samplerStates[WINED3DSAMP_MINFILTER], WINED3DTEXF_POINT), WINED3DTEXF_LINEAR),
439 min(max(samplerStates[WINED3DSAMP_MIPFILTER], WINED3DTEXF_NONE), WINED3DTEXF_LINEAR));
441 TRACE("ValueMIN=%d, ValueMIP=%d, setting MINFILTER to %x\n",
442 samplerStates[WINED3DSAMP_MINFILTER],
443 samplerStates[WINED3DSAMP_MIPFILTER], glValue);
444 glTexParameteri(textureDimensions, GL_TEXTURE_MIN_FILTER, glValue);
445 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
448 if(gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE) {
449 glValue = This->baseTexture.LOD;
450 } else if(gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= This->baseTexture.levels) {
451 glValue = This->baseTexture.levels - 1;
452 } else if(gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < This->baseTexture.LOD) {
453 /* baseTexture.LOD is already clamped in the setter */
454 glValue = This->baseTexture.LOD;
456 glValue = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
458 /* Note that D3DSAMP_MAXMIPLEVEL specifies the biggest mipmap(default 0), while
459 * GL_TEXTURE_MAX_LEVEL specifies the smallest mimap used(default 1000).
460 * So D3DSAMP_MAXMIPLEVEL is the same as GL_TEXTURE_BASE_LEVEL.
462 glTexParameteri(textureDimensions, GL_TEXTURE_BASE_LEVEL, glValue);
466 if ((gl_tex->states[WINED3DTEXSTA_MAGFILTER] != WINED3DTEXF_ANISOTROPIC
467 && gl_tex->states[WINED3DTEXSTA_MINFILTER] != WINED3DTEXF_ANISOTROPIC
468 && gl_tex->states[WINED3DTEXSTA_MIPFILTER] != WINED3DTEXF_ANISOTROPIC)
475 aniso = samplerStates[WINED3DSAMP_MAXANISOTROPY];
478 if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
480 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
482 glTexParameteri(textureDimensions, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
483 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
487 WARN("Anisotropic filtering not supported.\n");
489 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;