2 * IWineD3DTexture implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 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 /* Context activation is done by the caller. */
31 static HRESULT texture_bind(IWineD3DBaseTextureImpl *texture,
32 const struct wined3d_gl_info *gl_info, BOOL srgb)
34 BOOL set_gl_texture_desc;
37 TRACE("texture %p, gl_info %p, srgb %#x.\n", texture, gl_info, srgb);
39 hr = basetexture_bind(texture, gl_info, srgb, &set_gl_texture_desc);
40 if (set_gl_texture_desc && SUCCEEDED(hr))
42 BOOL srgb_tex = !gl_info->supported[EXT_TEXTURE_SRGB_DECODE] && texture->baseTexture.is_srgb;
43 struct gl_texture *gl_tex;
46 gl_tex = basetexture_get_gl_texture(texture, gl_info, srgb_tex);
48 for (i = 0; i < texture->baseTexture.level_count; ++i)
50 IWineD3DSurfaceImpl *surface = surface_from_resource(texture->baseTexture.sub_resources[i]);
51 surface_set_texture_name(surface, gl_tex->name, srgb_tex);
54 /* Conditinal non power of two textures use a different clamping
55 * default. If we're using the GL_WINE_normalized_texrect partial
56 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
57 * has the address mode set to repeat - something that prevents us
58 * from hitting the accelerated codepath. Thus manually set the GL
59 * state. The same applies to filtering. Even if the texture has only
60 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
61 * fallback on macos. */
62 if (IWineD3DBaseTexture_IsCondNP2((IWineD3DBaseTexture *)texture))
64 GLenum target = texture->baseTexture.target;
67 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
68 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
69 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
70 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
71 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
72 checkGLcall("glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
73 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
74 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
76 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3DTADDRESS_CLAMP;
77 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3DTADDRESS_CLAMP;
78 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
79 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
80 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
87 /* Do not call while under the GL lock. */
88 static void texture_preload(IWineD3DBaseTextureImpl *texture, enum WINED3DSRGB srgb)
90 IWineD3DDeviceImpl *device = texture->resource.device;
91 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
92 struct wined3d_context *context = NULL;
93 struct gl_texture *gl_tex;
97 TRACE("texture %p, srgb %#x.\n", texture, srgb);
106 texture_preload(texture, SRGB_RGB);
114 srgb_mode = texture->baseTexture.is_srgb;
118 gl_tex = basetexture_get_gl_texture(texture, gl_info, srgb_mode);
120 if (!device->isInDraw)
122 /* context_acquire() sets isInDraw to TRUE when loading a pbuffer into a texture,
123 * thus no danger of recursive calls. */
124 context = context_acquire(device, NULL);
127 if (texture->resource.format->id == WINED3DFMT_P8_UINT
128 || texture->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
130 for (i = 0; i < texture->baseTexture.level_count; ++i)
132 IWineD3DSurfaceImpl *surface = surface_from_resource(texture->baseTexture.sub_resources[i]);
133 if (palette9_changed(surface))
135 TRACE("Reloading surface because the d3d8/9 palette was changed.\n");
136 /* TODO: This is not necessarily needed with hw palettized texture support. */
137 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
138 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
139 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
144 /* If the texture is marked dirty or the srgb sampler setting has changed
145 * since the last load then reload the surfaces. */
148 for (i = 0; i < texture->baseTexture.level_count; ++i)
150 surface_load(surface_from_resource(texture->baseTexture.sub_resources[i]), srgb_mode);
155 TRACE("Texture %p not dirty, nothing to do.\n", texture);
158 if (context) context_release(context);
160 /* No longer dirty. */
161 gl_tex->dirty = FALSE;
164 /* Do not call while under the GL lock. */
165 static void texture_unload(struct wined3d_resource *resource)
167 IWineD3DBaseTextureImpl *texture = basetexture_from_resource(resource);
170 TRACE("texture %p.\n", texture);
172 for (i = 0; i < texture->baseTexture.level_count; ++i)
174 struct wined3d_resource *sub_resource = texture->baseTexture.sub_resources[i];
175 IWineD3DSurfaceImpl *surface = surface_from_resource(sub_resource);
177 sub_resource->resource_ops->resource_unload(sub_resource);
178 surface_set_texture_name(surface, 0, FALSE); /* Delete rgb name */
179 surface_set_texture_name(surface, 0, TRUE); /* delete srgb name */
182 basetexture_unload(texture);
185 static const struct wined3d_texture_ops texture_ops =
191 static const struct wined3d_resource_ops texture_resource_ops =
196 static void texture_cleanup(IWineD3DTextureImpl *This)
200 TRACE("(%p) : Cleaning up\n", This);
202 for (i = 0; i < This->baseTexture.level_count; ++i)
204 struct wined3d_resource *sub_resource = This->baseTexture.sub_resources[i];
208 IWineD3DSurfaceImpl *surface = surface_from_resource(sub_resource);
210 /* Clean out the texture name we gave to the surface so that the
211 * surface doesn't try and release it */
212 surface_set_texture_name(surface, 0, TRUE);
213 surface_set_texture_name(surface, 0, FALSE);
214 surface_set_texture_target(surface, 0);
215 surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
216 IWineD3DSurface_Release((IWineD3DSurface *)surface);
220 TRACE("(%p) : Cleaning up base texture\n", This);
221 basetexture_cleanup((IWineD3DBaseTextureImpl *)This);
224 /* *******************************************
225 IWineD3DTexture IUnknown parts follow
226 ******************************************* */
228 static HRESULT WINAPI IWineD3DTextureImpl_QueryInterface(IWineD3DTexture *iface, REFIID riid, LPVOID *ppobj)
230 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
231 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
232 if (IsEqualGUID(riid, &IID_IUnknown)
233 || IsEqualGUID(riid, &IID_IWineD3DBase)
234 || IsEqualGUID(riid, &IID_IWineD3DResource)
235 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
236 || IsEqualGUID(riid, &IID_IWineD3DTexture)){
237 IUnknown_AddRef(iface);
242 return E_NOINTERFACE;
245 static ULONG WINAPI IWineD3DTextureImpl_AddRef(IWineD3DTexture *iface) {
246 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
247 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
248 return InterlockedIncrement(&This->resource.ref);
251 /* Do not call while under the GL lock. */
252 static ULONG WINAPI IWineD3DTextureImpl_Release(IWineD3DTexture *iface) {
253 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
255 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
256 ref = InterlockedDecrement(&This->resource.ref);
259 texture_cleanup(This);
260 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
261 HeapFree(GetProcessHeap(), 0, This);
267 /* ****************************************************
268 IWineD3DTexture IWineD3DResource parts follow
269 **************************************************** */
270 static HRESULT WINAPI IWineD3DTextureImpl_SetPrivateData(IWineD3DTexture *iface,
271 REFGUID riid, const void *data, DWORD data_size, DWORD flags)
273 return resource_set_private_data(&((IWineD3DTextureImpl *)iface)->resource, riid, data, data_size, flags);
276 static HRESULT WINAPI IWineD3DTextureImpl_GetPrivateData(IWineD3DTexture *iface,
277 REFGUID guid, void *data, DWORD *data_size)
279 return resource_get_private_data(&((IWineD3DTextureImpl *)iface)->resource, guid, data, data_size);
282 static HRESULT WINAPI IWineD3DTextureImpl_FreePrivateData(IWineD3DTexture *iface, REFGUID refguid)
284 return resource_free_private_data(&((IWineD3DTextureImpl *)iface)->resource, refguid);
287 static DWORD WINAPI IWineD3DTextureImpl_SetPriority(IWineD3DTexture *iface, DWORD priority)
289 return resource_set_priority(&((IWineD3DTextureImpl *)iface)->resource, priority);
292 static DWORD WINAPI IWineD3DTextureImpl_GetPriority(IWineD3DTexture *iface)
294 return resource_get_priority(&((IWineD3DTextureImpl *)iface)->resource);
297 /* Do not call while under the GL lock. */
298 static void WINAPI IWineD3DTextureImpl_PreLoad(IWineD3DTexture *iface)
300 texture_preload((IWineD3DBaseTextureImpl *)iface, SRGB_ANY);
303 static WINED3DRESOURCETYPE WINAPI IWineD3DTextureImpl_GetType(IWineD3DTexture *iface)
305 return resource_get_type(&((IWineD3DTextureImpl *)iface)->resource);
308 static void * WINAPI IWineD3DTextureImpl_GetParent(IWineD3DTexture *iface)
310 TRACE("iface %p.\n", iface);
312 return ((IWineD3DTextureImpl *)iface)->resource.parent;
315 /* ******************************************************
316 IWineD3DTexture IWineD3DBaseTexture parts follow
317 ****************************************************** */
318 static DWORD WINAPI IWineD3DTextureImpl_SetLOD(IWineD3DTexture *iface, DWORD LODNew) {
319 return basetexture_set_lod((IWineD3DBaseTextureImpl *)iface, LODNew);
322 static DWORD WINAPI IWineD3DTextureImpl_GetLOD(IWineD3DTexture *iface) {
323 return basetexture_get_lod((IWineD3DBaseTextureImpl *)iface);
326 static DWORD WINAPI IWineD3DTextureImpl_GetLevelCount(IWineD3DTexture *iface)
328 return basetexture_get_level_count((IWineD3DBaseTextureImpl *)iface);
331 static HRESULT WINAPI IWineD3DTextureImpl_SetAutoGenFilterType(IWineD3DTexture *iface,
332 WINED3DTEXTUREFILTERTYPE FilterType)
334 return basetexture_set_autogen_filter_type((IWineD3DBaseTextureImpl *)iface, FilterType);
337 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DTextureImpl_GetAutoGenFilterType(IWineD3DTexture *iface)
339 return basetexture_get_autogen_filter_type((IWineD3DBaseTextureImpl *)iface);
342 static void WINAPI IWineD3DTextureImpl_GenerateMipSubLevels(IWineD3DTexture *iface)
344 basetexture_generate_mipmaps((IWineD3DBaseTextureImpl *)iface);
347 static BOOL WINAPI IWineD3DTextureImpl_IsCondNP2(IWineD3DTexture *iface) {
348 IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
349 TRACE("(%p)\n", This);
351 return This->cond_np2;
354 static struct wined3d_resource * WINAPI IWineD3DTextureImpl_GetSubResource(IWineD3DTexture *iface,
355 UINT sub_resource_idx)
357 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
359 TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
361 return basetexture_get_sub_resource(texture, sub_resource_idx);
364 static HRESULT WINAPI IWineD3DTextureImpl_AddDirtyRegion(IWineD3DTexture *iface,
365 UINT layer, const WINED3DBOX *dirty_region)
367 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
368 struct wined3d_resource *sub_resource;
370 TRACE("iface %p, layer %u, dirty_region %p.\n", iface, layer, dirty_region);
372 if (!(sub_resource = basetexture_get_sub_resource(texture, layer * texture->baseTexture.level_count)))
374 WARN("Failed to get sub-resource.\n");
375 return WINED3DERR_INVALIDCALL;
378 basetexture_set_dirty(texture, TRUE);
379 surface_add_dirty_rect(surface_from_resource(sub_resource), dirty_region);
384 static const IWineD3DTextureVtbl IWineD3DTexture_Vtbl =
387 IWineD3DTextureImpl_QueryInterface,
388 IWineD3DTextureImpl_AddRef,
389 IWineD3DTextureImpl_Release,
390 /* IWineD3DResource */
391 IWineD3DTextureImpl_GetParent,
392 IWineD3DTextureImpl_SetPrivateData,
393 IWineD3DTextureImpl_GetPrivateData,
394 IWineD3DTextureImpl_FreePrivateData,
395 IWineD3DTextureImpl_SetPriority,
396 IWineD3DTextureImpl_GetPriority,
397 IWineD3DTextureImpl_PreLoad,
398 IWineD3DTextureImpl_GetType,
399 /* IWineD3DBaseTexture */
400 IWineD3DTextureImpl_SetLOD,
401 IWineD3DTextureImpl_GetLOD,
402 IWineD3DTextureImpl_GetLevelCount,
403 IWineD3DTextureImpl_SetAutoGenFilterType,
404 IWineD3DTextureImpl_GetAutoGenFilterType,
405 IWineD3DTextureImpl_GenerateMipSubLevels,
406 IWineD3DTextureImpl_IsCondNP2,
407 IWineD3DTextureImpl_GetSubResource,
408 IWineD3DTextureImpl_AddDirtyRegion,
411 HRESULT texture_init(IWineD3DTextureImpl *texture, UINT width, UINT height, UINT levels,
412 IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
413 void *parent, const struct wined3d_parent_ops *parent_ops)
415 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
416 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
417 UINT pow2_width, pow2_height;
422 /* TODO: It should only be possible to create textures for formats
423 * that are reported as supported. */
424 if (WINED3DFMT_UNKNOWN >= format_id)
426 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
427 return WINED3DERR_INVALIDCALL;
430 /* Non-power2 support. */
431 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
434 pow2_height = height;
438 /* Find the nearest pow2 match. */
439 pow2_width = pow2_height = 1;
440 while (pow2_width < width) pow2_width <<= 1;
441 while (pow2_height < height) pow2_height <<= 1;
443 if (pow2_width != width || pow2_height != height)
447 WARN("Attempted to create a mipmapped np2 texture without unconditional np2 support.\n");
448 return WINED3DERR_INVALIDCALL;
454 /* Calculate levels for mip mapping. */
455 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
457 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
459 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
460 return WINED3DERR_INVALIDCALL;
465 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
466 return WINED3DERR_INVALIDCALL;
473 levels = wined3d_log2i(max(width, height)) + 1;
474 TRACE("Calculated levels = %u.\n", levels);
477 texture->lpVtbl = &IWineD3DTexture_Vtbl;
479 hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, &texture_ops,
480 1, levels, WINED3DRTYPE_TEXTURE, device, usage, format, pool,
481 parent, parent_ops, &texture_resource_ops);
484 WARN("Failed to initialize basetexture, returning %#x.\n", hr);
488 /* Precalculated scaling for 'faked' non power of two texture coords.
489 * Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
490 * is used in combination with texture uploads (RTL_READTEX). The reason is that EXT_PALETTED_TEXTURE
491 * doesn't work in combination with ARB_TEXTURE_RECTANGLE. */
492 if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT] && (width != pow2_width || height != pow2_height))
494 texture->baseTexture.pow2Matrix[0] = 1.0f;
495 texture->baseTexture.pow2Matrix[5] = 1.0f;
496 texture->baseTexture.pow2Matrix[10] = 1.0f;
497 texture->baseTexture.pow2Matrix[15] = 1.0f;
498 texture->baseTexture.target = GL_TEXTURE_2D;
499 texture->cond_np2 = TRUE;
500 texture->baseTexture.minMipLookup = minMipLookup_noFilter;
502 else if (gl_info->supported[ARB_TEXTURE_RECTANGLE] && (width != pow2_width || height != pow2_height)
503 && !(format->id == WINED3DFMT_P8_UINT && gl_info->supported[EXT_PALETTED_TEXTURE]
504 && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
506 if ((width != 1) || (height != 1)) texture->baseTexture.pow2Matrix_identity = FALSE;
508 texture->baseTexture.pow2Matrix[0] = (float)width;
509 texture->baseTexture.pow2Matrix[5] = (float)height;
510 texture->baseTexture.pow2Matrix[10] = 1.0f;
511 texture->baseTexture.pow2Matrix[15] = 1.0f;
512 texture->baseTexture.target = GL_TEXTURE_RECTANGLE_ARB;
513 texture->cond_np2 = TRUE;
515 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
517 texture->baseTexture.minMipLookup = minMipLookup_noMip;
521 texture->baseTexture.minMipLookup = minMipLookup_noFilter;
526 if ((width != pow2_width) || (height != pow2_height))
528 texture->baseTexture.pow2Matrix_identity = FALSE;
529 texture->baseTexture.pow2Matrix[0] = (((float)width) / ((float)pow2_width));
530 texture->baseTexture.pow2Matrix[5] = (((float)height) / ((float)pow2_height));
534 texture->baseTexture.pow2Matrix[0] = 1.0f;
535 texture->baseTexture.pow2Matrix[5] = 1.0f;
538 texture->baseTexture.pow2Matrix[10] = 1.0f;
539 texture->baseTexture.pow2Matrix[15] = 1.0f;
540 texture->baseTexture.target = GL_TEXTURE_2D;
541 texture->cond_np2 = FALSE;
543 TRACE("xf(%f) yf(%f)\n", texture->baseTexture.pow2Matrix[0], texture->baseTexture.pow2Matrix[5]);
545 /* Generate all the surfaces. */
548 for (i = 0; i < texture->baseTexture.level_count; ++i)
550 IWineD3DSurface *surface;
552 /* Use the callback to create the texture surface. */
553 hr = IWineD3DDeviceParent_CreateSurface(device->device_parent, parent, tmp_w, tmp_h,
554 format->id, usage, pool, i, 0, &surface);
557 FIXME("Failed to create surface %p, hr %#x\n", texture, hr);
558 texture_cleanup(texture);
562 surface_set_container((IWineD3DSurfaceImpl *)surface, WINED3D_CONTAINER_TEXTURE, (IWineD3DBase *)texture);
563 surface_set_texture_target((IWineD3DSurfaceImpl *)surface, texture->baseTexture.target);
564 texture->baseTexture.sub_resources[i] = &((IWineD3DSurfaceImpl *)surface)->resource;
565 TRACE("Created surface level %u @ %p.\n", i, surface);
566 /* Calculate the next mipmap level. */
567 tmp_w = max(1, tmp_w >> 1);
568 tmp_h = max(1, tmp_h >> 1);