2 * IWineD3DCubeTexture 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 cubetexture_bind(IWineD3DBaseTextureImpl *texture, BOOL srgb)
33 BOOL set_gl_texture_desc;
36 TRACE("texture %p, srgb %#x.\n", texture, srgb);
38 hr = basetexture_bind(texture, srgb, &set_gl_texture_desc);
39 if (set_gl_texture_desc && SUCCEEDED(hr))
41 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
44 for (i = 0; i < sub_count; ++i)
46 IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)texture->baseTexture.sub_resources[i];
48 if (texture->baseTexture.is_srgb)
49 surface_set_texture_name(surface, texture->baseTexture.texture_srgb.name, TRUE);
51 surface_set_texture_name(surface, texture->baseTexture.texture_rgb.name, FALSE);
58 /* Do not call while under the GL lock. */
59 static void cubetexture_preload(IWineD3DBaseTextureImpl *texture, enum WINED3DSRGB srgb)
61 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
62 IWineD3DDeviceImpl *device = texture->resource.device;
63 struct wined3d_context *context = NULL;
68 TRACE("texture %p, srgb %#x.\n", texture, srgb);
77 cubetexture_preload(texture, SRGB_RGB);
85 srgb_mode = texture->baseTexture.is_srgb;
88 dirty = srgb_mode ? &texture->baseTexture.texture_srgb.dirty : &texture->baseTexture.texture_rgb.dirty;
90 /* We only have to activate a context for gl when we're not drawing.
91 * In most cases PreLoad will be called during draw and a context was
92 * activated at the beginning of drawPrimitive. */
93 if (!device->isInDraw)
95 /* No danger of recursive calls, context_acquire() sets isInDraw to true
96 * when loading offscreen render targets into their texture. */
97 context = context_acquire(device, NULL);
100 if (texture->resource.format->id == WINED3DFMT_P8_UINT
101 || texture->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
103 for (i = 0; i < sub_count; ++i)
105 IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)texture->baseTexture.sub_resources[i];
107 if (palette9_changed(surface))
109 TRACE("Reloading surface %p because the d3d8/9 palette was changed.\n", surface);
110 /* TODO: This is not necessarily needed with hw palettized texture support. */
111 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
112 /* Make sure the texture is reloaded because of the palette change,
113 * this kills performance though :( */
114 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
119 /* If the texture is marked dirty or the srgb sampler setting has changed
120 * since the last load then reload the surfaces. */
123 for (i = 0; i < sub_count; ++i)
125 surface_load((IWineD3DSurfaceImpl *)texture->baseTexture.sub_resources[i], srgb_mode);
130 TRACE("Texture %p not dirty, nothing to do.\n" , texture);
133 /* No longer dirty. */
136 if (context) context_release(context);
139 /* Do not call while under the GL lock. */
140 static void cubetexture_unload(IWineD3DResourceImpl *resource)
142 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)resource;
143 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
146 TRACE("texture %p.\n", texture);
148 for (i = 0; i < sub_count; ++i)
150 IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)texture->baseTexture.sub_resources[i];
152 surface->resource.resource_ops->resource_unload((IWineD3DResourceImpl *)surface);
153 surface_set_texture_name(surface, 0, TRUE);
154 surface_set_texture_name(surface, 0, FALSE);
157 basetexture_unload(texture);
160 static const struct wined3d_texture_ops cubetexture_ops =
166 static const struct wined3d_resource_ops cubetexture_resource_ops =
171 static void cubetexture_cleanup(IWineD3DCubeTextureImpl *This)
173 UINT sub_count = This->baseTexture.level_count * This->baseTexture.layer_count;
176 TRACE("(%p) : Cleaning up.\n", This);
178 for (i = 0; i < sub_count; ++i)
180 IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)This->baseTexture.sub_resources[i];
184 /* Clean out the texture name we gave to the surface so that the
185 * surface doesn't try and release it. */
186 surface_set_texture_name(surface, 0, TRUE);
187 surface_set_texture_name(surface, 0, FALSE);
188 surface_set_texture_target(surface, 0);
189 surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
190 IWineD3DSurface_Release((IWineD3DSurface *)surface);
193 basetexture_cleanup((IWineD3DBaseTextureImpl *)This);
196 /* *******************************************
197 IWineD3DCubeTexture IUnknown parts follow
198 ******************************************* */
200 static HRESULT WINAPI IWineD3DCubeTextureImpl_QueryInterface(IWineD3DCubeTexture *iface, REFIID riid, LPVOID *ppobj)
202 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
203 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
204 if (IsEqualGUID(riid, &IID_IUnknown)
205 || IsEqualGUID(riid, &IID_IWineD3DBase)
206 || IsEqualGUID(riid, &IID_IWineD3DResource)
207 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
208 || IsEqualGUID(riid, &IID_IWineD3DCubeTexture)) {
209 IUnknown_AddRef(iface);
214 return E_NOINTERFACE;
217 static ULONG WINAPI IWineD3DCubeTextureImpl_AddRef(IWineD3DCubeTexture *iface) {
218 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
219 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
220 return InterlockedIncrement(&This->resource.ref);
223 /* Do not call while under the GL lock. */
224 static ULONG WINAPI IWineD3DCubeTextureImpl_Release(IWineD3DCubeTexture *iface) {
225 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
227 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
228 ref = InterlockedDecrement(&This->resource.ref);
231 cubetexture_cleanup(This);
232 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
233 HeapFree(GetProcessHeap(), 0, This);
238 /* ****************************************************
239 IWineD3DCubeTexture IWineD3DResource parts follow
240 **************************************************** */
241 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetPrivateData(IWineD3DCubeTexture *iface,
242 REFGUID riid, const void *data, DWORD data_size, DWORD flags)
244 return resource_set_private_data((IWineD3DResourceImpl *)iface, riid, data, data_size, flags);
247 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetPrivateData(IWineD3DCubeTexture *iface,
248 REFGUID guid, void *data, DWORD *data_size)
250 return resource_get_private_data((IWineD3DResourceImpl *)iface, guid, data, data_size);
253 static HRESULT WINAPI IWineD3DCubeTextureImpl_FreePrivateData(IWineD3DCubeTexture *iface, REFGUID refguid)
255 return resource_free_private_data((IWineD3DResourceImpl *)iface, refguid);
258 static DWORD WINAPI IWineD3DCubeTextureImpl_SetPriority(IWineD3DCubeTexture *iface, DWORD priority)
260 return resource_set_priority((IWineD3DResourceImpl *)iface, priority);
263 static DWORD WINAPI IWineD3DCubeTextureImpl_GetPriority(IWineD3DCubeTexture *iface)
265 return resource_get_priority((IWineD3DResourceImpl *)iface);
268 /* Do not call while under the GL lock. */
269 static void WINAPI IWineD3DCubeTextureImpl_PreLoad(IWineD3DCubeTexture *iface)
271 cubetexture_preload((IWineD3DBaseTextureImpl *)iface, SRGB_ANY);
274 static WINED3DRESOURCETYPE WINAPI IWineD3DCubeTextureImpl_GetType(IWineD3DCubeTexture *iface)
276 return resource_get_type((IWineD3DResourceImpl *)iface);
279 static void * WINAPI IWineD3DCubeTextureImpl_GetParent(IWineD3DCubeTexture *iface)
281 TRACE("iface %p.\n", iface);
283 return ((IWineD3DCubeTextureImpl *)iface)->resource.parent;
286 /* ******************************************************
287 IWineD3DCubeTexture IWineD3DBaseTexture parts follow
288 ****************************************************** */
289 static DWORD WINAPI IWineD3DCubeTextureImpl_SetLOD(IWineD3DCubeTexture *iface, DWORD LODNew) {
290 return basetexture_set_lod((IWineD3DBaseTextureImpl *)iface, LODNew);
293 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLOD(IWineD3DCubeTexture *iface) {
294 return basetexture_get_lod((IWineD3DBaseTextureImpl *)iface);
297 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLevelCount(IWineD3DCubeTexture *iface)
299 return basetexture_get_level_count((IWineD3DBaseTextureImpl *)iface);
302 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetAutoGenFilterType(IWineD3DCubeTexture *iface,
303 WINED3DTEXTUREFILTERTYPE FilterType)
305 return basetexture_set_autogen_filter_type((IWineD3DBaseTextureImpl *)iface, FilterType);
308 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DCubeTextureImpl_GetAutoGenFilterType(IWineD3DCubeTexture *iface)
310 return basetexture_get_autogen_filter_type((IWineD3DBaseTextureImpl *)iface);
313 static void WINAPI IWineD3DCubeTextureImpl_GenerateMipSubLevels(IWineD3DCubeTexture *iface)
315 basetexture_generate_mipmaps((IWineD3DBaseTextureImpl *)iface);
318 static BOOL WINAPI IWineD3DCubeTextureImpl_IsCondNP2(IWineD3DCubeTexture *iface)
320 TRACE("iface %p.\n", iface);
325 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetLevelDesc(IWineD3DCubeTexture *iface,
326 UINT sub_resource_idx, WINED3DSURFACE_DESC *desc)
328 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
329 IWineD3DSurface *surface;
331 TRACE("iface %p, sub_resource_idx %u, desc %p.\n", iface, sub_resource_idx, desc);
333 if (!(surface = (IWineD3DSurface *)basetexture_get_sub_resource(texture, sub_resource_idx)))
335 WARN("Failed to get sub-resource.\n");
336 return WINED3DERR_INVALIDCALL;
339 IWineD3DSurface_GetDesc(surface, desc);
344 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetCubeMapSurface(IWineD3DCubeTexture *iface,
345 UINT sub_resource_idx, IWineD3DSurface **surface)
347 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
350 TRACE("iface %p, sub_resource_idx %u, surface %p.\n",
351 iface, sub_resource_idx, surface);
353 if (!(s = (IWineD3DSurface *)basetexture_get_sub_resource(texture, sub_resource_idx)))
355 WARN("Failed to get sub-resource.\n");
356 return WINED3DERR_INVALIDCALL;
359 IWineD3DSurface_AddRef(s);
362 TRACE("Returning surface %p.\n", *surface);
367 static HRESULT WINAPI IWineD3DCubeTextureImpl_Map(IWineD3DCubeTexture *iface,
368 UINT sub_resource_idx, WINED3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
370 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
371 IWineD3DSurface *surface;
373 TRACE("iface %p, sub_resource_idx %u, locked_rect %p, rect %s, flags %#x.\n",
374 iface, sub_resource_idx, locked_rect, wine_dbgstr_rect(rect), flags);
376 if (!(surface = (IWineD3DSurface *)basetexture_get_sub_resource(texture, sub_resource_idx)))
378 WARN("Failed to get sub-resource.\n");
379 return WINED3DERR_INVALIDCALL;
382 return IWineD3DSurface_Map(surface, locked_rect, rect, flags);
385 static HRESULT WINAPI IWineD3DCubeTextureImpl_Unmap(IWineD3DCubeTexture *iface,
386 UINT sub_resource_idx)
388 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
389 IWineD3DSurface *surface;
391 TRACE("iface %p, sub_resource_idx %u.\n",
392 iface, sub_resource_idx);
394 if (!(surface = (IWineD3DSurface *)basetexture_get_sub_resource(texture, sub_resource_idx)))
396 WARN("Failed to get sub-resource.\n");
397 return WINED3DERR_INVALIDCALL;
400 return IWineD3DSurface_Unmap(surface);
403 static HRESULT WINAPI IWineD3DCubeTextureImpl_AddDirtyRect(IWineD3DCubeTexture *iface,
404 WINED3DCUBEMAP_FACES face, const RECT *dirty_rect)
406 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
407 UINT sub_resource_idx = face * texture->baseTexture.level_count;
408 IWineD3DSurfaceImpl *surface;
410 TRACE("iface %p, face %u, dirty_rect %s.\n",
411 iface, face, wine_dbgstr_rect(dirty_rect));
413 if (!(surface = (IWineD3DSurfaceImpl *)basetexture_get_sub_resource(texture, sub_resource_idx)))
415 WARN("Failed to get sub-resource.\n");
416 return WINED3DERR_INVALIDCALL;
419 texture->baseTexture.texture_rgb.dirty = TRUE;
420 texture->baseTexture.texture_srgb.dirty = TRUE;
421 surface_add_dirty_rect(surface, dirty_rect);
426 static const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl =
429 IWineD3DCubeTextureImpl_QueryInterface,
430 IWineD3DCubeTextureImpl_AddRef,
431 IWineD3DCubeTextureImpl_Release,
432 /* IWineD3DResource */
433 IWineD3DCubeTextureImpl_GetParent,
434 IWineD3DCubeTextureImpl_SetPrivateData,
435 IWineD3DCubeTextureImpl_GetPrivateData,
436 IWineD3DCubeTextureImpl_FreePrivateData,
437 IWineD3DCubeTextureImpl_SetPriority,
438 IWineD3DCubeTextureImpl_GetPriority,
439 IWineD3DCubeTextureImpl_PreLoad,
440 IWineD3DCubeTextureImpl_GetType,
441 /* IWineD3DBaseTexture */
442 IWineD3DCubeTextureImpl_SetLOD,
443 IWineD3DCubeTextureImpl_GetLOD,
444 IWineD3DCubeTextureImpl_GetLevelCount,
445 IWineD3DCubeTextureImpl_SetAutoGenFilterType,
446 IWineD3DCubeTextureImpl_GetAutoGenFilterType,
447 IWineD3DCubeTextureImpl_GenerateMipSubLevels,
448 IWineD3DCubeTextureImpl_IsCondNP2,
449 /* IWineD3DCubeTexture */
450 IWineD3DCubeTextureImpl_GetLevelDesc,
451 IWineD3DCubeTextureImpl_GetCubeMapSurface,
452 IWineD3DCubeTextureImpl_Map,
453 IWineD3DCubeTextureImpl_Unmap,
454 IWineD3DCubeTextureImpl_AddDirtyRect
457 HRESULT cubetexture_init(IWineD3DCubeTextureImpl *texture, UINT edge_length, UINT levels,
458 IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
459 void *parent, const struct wined3d_parent_ops *parent_ops)
461 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
462 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
463 UINT pow2_edge_length;
468 /* TODO: It should only be possible to create textures for formats
469 * that are reported as supported. */
470 if (WINED3DFMT_UNKNOWN >= format_id)
472 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
473 return WINED3DERR_INVALIDCALL;
476 if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && pool != WINED3DPOOL_SCRATCH)
478 WARN("(%p) : Tried to create not supported cube texture.\n", texture);
479 return WINED3DERR_INVALIDCALL;
482 /* Calculate levels for mip mapping */
483 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
485 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
487 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
488 return WINED3DERR_INVALIDCALL;
493 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
494 return WINED3DERR_INVALIDCALL;
501 levels = wined3d_log2i(edge_length) + 1;
502 TRACE("Calculated levels = %u.\n", levels);
505 texture->lpVtbl = &IWineD3DCubeTexture_Vtbl;
507 hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, &cubetexture_ops,
508 6, levels, WINED3DRTYPE_CUBETEXTURE, device, usage, format, pool,
509 parent, parent_ops, &cubetexture_resource_ops);
512 WARN("Failed to initialize basetexture, returning %#x\n", hr);
516 /* Find the nearest pow2 match. */
517 pow2_edge_length = 1;
518 while (pow2_edge_length < edge_length) pow2_edge_length <<= 1;
520 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || (edge_length == pow2_edge_length))
522 /* Precalculated scaling for 'faked' non power of two texture coords. */
523 texture->baseTexture.pow2Matrix[0] = 1.0f;
524 texture->baseTexture.pow2Matrix[5] = 1.0f;
525 texture->baseTexture.pow2Matrix[10] = 1.0f;
526 texture->baseTexture.pow2Matrix[15] = 1.0f;
530 /* Precalculated scaling for 'faked' non power of two texture coords. */
531 texture->baseTexture.pow2Matrix[0] = ((float)edge_length) / ((float)pow2_edge_length);
532 texture->baseTexture.pow2Matrix[5] = ((float)edge_length) / ((float)pow2_edge_length);
533 texture->baseTexture.pow2Matrix[10] = ((float)edge_length) / ((float)pow2_edge_length);
534 texture->baseTexture.pow2Matrix[15] = 1.0f;
535 texture->baseTexture.pow2Matrix_identity = FALSE;
537 texture->baseTexture.target = GL_TEXTURE_CUBE_MAP_ARB;
539 /* Generate all the surfaces. */
541 for (i = 0; i < texture->baseTexture.level_count; ++i)
543 /* Create the 6 faces. */
544 for (j = 0; j < 6; ++j)
546 static const GLenum cube_targets[6] =
548 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
549 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
550 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
551 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
552 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
553 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
555 UINT idx = j * texture->baseTexture.level_count + i;
556 IWineD3DSurface *surface;
558 hr = IWineD3DDeviceParent_CreateSurface(device->device_parent, parent, tmp_w, tmp_w,
559 format_id, usage, pool, i /* Level */, j, &surface);
562 FIXME("(%p) Failed to create surface, hr %#x.\n", texture, hr);
563 cubetexture_cleanup(texture);
567 surface_set_container((IWineD3DSurfaceImpl *)surface, WINED3D_CONTAINER_TEXTURE, (IWineD3DBase *)texture);
568 surface_set_texture_target((IWineD3DSurfaceImpl *)surface, cube_targets[j]);
569 texture->baseTexture.sub_resources[idx] = (IWineD3DResourceImpl *)surface;
570 TRACE("Created surface level %u @ %p.\n", i, surface);
572 tmp_w = max(1, tmp_w >> 1);