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 static const struct wined3d_texture_ops cubetexture_ops =
145 static void cubetexture_cleanup(IWineD3DCubeTextureImpl *This)
147 UINT sub_count = This->baseTexture.level_count * This->baseTexture.layer_count;
150 TRACE("(%p) : Cleaning up.\n", This);
152 for (i = 0; i < sub_count; ++i)
154 IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)This->baseTexture.sub_resources[i];
158 /* Clean out the texture name we gave to the surface so that the
159 * surface doesn't try and release it. */
160 surface_set_texture_name(surface, 0, TRUE);
161 surface_set_texture_name(surface, 0, FALSE);
162 surface_set_texture_target(surface, 0);
163 surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
164 IWineD3DSurface_Release((IWineD3DSurface *)surface);
167 basetexture_cleanup((IWineD3DBaseTextureImpl *)This);
170 /* *******************************************
171 IWineD3DCubeTexture IUnknown parts follow
172 ******************************************* */
174 static HRESULT WINAPI IWineD3DCubeTextureImpl_QueryInterface(IWineD3DCubeTexture *iface, REFIID riid, LPVOID *ppobj)
176 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
177 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
178 if (IsEqualGUID(riid, &IID_IUnknown)
179 || IsEqualGUID(riid, &IID_IWineD3DBase)
180 || IsEqualGUID(riid, &IID_IWineD3DResource)
181 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
182 || IsEqualGUID(riid, &IID_IWineD3DCubeTexture)) {
183 IUnknown_AddRef(iface);
188 return E_NOINTERFACE;
191 static ULONG WINAPI IWineD3DCubeTextureImpl_AddRef(IWineD3DCubeTexture *iface) {
192 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
193 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
194 return InterlockedIncrement(&This->resource.ref);
197 /* Do not call while under the GL lock. */
198 static ULONG WINAPI IWineD3DCubeTextureImpl_Release(IWineD3DCubeTexture *iface) {
199 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
201 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
202 ref = InterlockedDecrement(&This->resource.ref);
205 cubetexture_cleanup(This);
206 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
207 HeapFree(GetProcessHeap(), 0, This);
212 /* ****************************************************
213 IWineD3DCubeTexture IWineD3DResource parts follow
214 **************************************************** */
215 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetPrivateData(IWineD3DCubeTexture *iface,
216 REFGUID riid, const void *data, DWORD data_size, DWORD flags)
218 return resource_set_private_data((IWineD3DResourceImpl *)iface, riid, data, data_size, flags);
221 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetPrivateData(IWineD3DCubeTexture *iface,
222 REFGUID guid, void *data, DWORD *data_size)
224 return resource_get_private_data((IWineD3DResourceImpl *)iface, guid, data, data_size);
227 static HRESULT WINAPI IWineD3DCubeTextureImpl_FreePrivateData(IWineD3DCubeTexture *iface, REFGUID refguid)
229 return resource_free_private_data((IWineD3DResourceImpl *)iface, refguid);
232 static DWORD WINAPI IWineD3DCubeTextureImpl_SetPriority(IWineD3DCubeTexture *iface, DWORD priority)
234 return resource_set_priority((IWineD3DResourceImpl *)iface, priority);
237 static DWORD WINAPI IWineD3DCubeTextureImpl_GetPriority(IWineD3DCubeTexture *iface)
239 return resource_get_priority((IWineD3DResourceImpl *)iface);
242 /* Do not call while under the GL lock. */
243 static void WINAPI IWineD3DCubeTextureImpl_PreLoad(IWineD3DCubeTexture *iface)
245 cubetexture_preload((IWineD3DBaseTextureImpl *)iface, SRGB_ANY);
248 /* Do not call while under the GL lock. */
249 static void WINAPI IWineD3DCubeTextureImpl_UnLoad(IWineD3DCubeTexture *iface)
251 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
252 UINT sub_count = This->baseTexture.level_count * This->baseTexture.layer_count;
255 TRACE("iface %p.\n", iface);
257 /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
258 * surface before, this one will be a NOP and vice versa. Unloading an unloaded
259 * surface is fine. */
261 for (i = 0; i < sub_count; ++i)
263 IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)This->baseTexture.sub_resources[i];
265 IWineD3DSurface_UnLoad((IWineD3DSurface *)surface);
266 surface_set_texture_name(surface, 0, TRUE);
267 surface_set_texture_name(surface, 0, FALSE);
270 basetexture_unload((IWineD3DBaseTextureImpl *)This);
273 static WINED3DRESOURCETYPE WINAPI IWineD3DCubeTextureImpl_GetType(IWineD3DCubeTexture *iface)
275 return resource_get_type((IWineD3DResourceImpl *)iface);
278 static void * WINAPI IWineD3DCubeTextureImpl_GetParent(IWineD3DCubeTexture *iface)
280 TRACE("iface %p.\n", iface);
282 return ((IWineD3DCubeTextureImpl *)iface)->resource.parent;
285 /* ******************************************************
286 IWineD3DCubeTexture IWineD3DBaseTexture parts follow
287 ****************************************************** */
288 static DWORD WINAPI IWineD3DCubeTextureImpl_SetLOD(IWineD3DCubeTexture *iface, DWORD LODNew) {
289 return basetexture_set_lod((IWineD3DBaseTextureImpl *)iface, LODNew);
292 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLOD(IWineD3DCubeTexture *iface) {
293 return basetexture_get_lod((IWineD3DBaseTextureImpl *)iface);
296 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLevelCount(IWineD3DCubeTexture *iface)
298 return basetexture_get_level_count((IWineD3DBaseTextureImpl *)iface);
301 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetAutoGenFilterType(IWineD3DCubeTexture *iface,
302 WINED3DTEXTUREFILTERTYPE FilterType)
304 return basetexture_set_autogen_filter_type((IWineD3DBaseTextureImpl *)iface, FilterType);
307 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DCubeTextureImpl_GetAutoGenFilterType(IWineD3DCubeTexture *iface)
309 return basetexture_get_autogen_filter_type((IWineD3DBaseTextureImpl *)iface);
312 static void WINAPI IWineD3DCubeTextureImpl_GenerateMipSubLevels(IWineD3DCubeTexture *iface)
314 basetexture_generate_mipmaps((IWineD3DBaseTextureImpl *)iface);
317 static BOOL WINAPI IWineD3DCubeTextureImpl_IsCondNP2(IWineD3DCubeTexture *iface)
319 TRACE("iface %p.\n", iface);
324 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetLevelDesc(IWineD3DCubeTexture *iface,
325 UINT sub_resource_idx, WINED3DSURFACE_DESC *desc)
327 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
328 IWineD3DSurface *surface;
330 TRACE("iface %p, sub_resource_idx %u, desc %p.\n", iface, sub_resource_idx, desc);
332 if (!(surface = (IWineD3DSurface *)basetexture_get_sub_resource(texture, sub_resource_idx)))
334 WARN("Failed to get sub-resource.\n");
335 return WINED3DERR_INVALIDCALL;
338 IWineD3DSurface_GetDesc(surface, desc);
343 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetCubeMapSurface(IWineD3DCubeTexture *iface,
344 UINT sub_resource_idx, IWineD3DSurface **surface)
346 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
349 TRACE("iface %p, sub_resource_idx %u, surface %p.\n",
350 iface, sub_resource_idx, surface);
352 if (!(s = (IWineD3DSurface *)basetexture_get_sub_resource(texture, sub_resource_idx)))
354 WARN("Failed to get sub-resource.\n");
355 return WINED3DERR_INVALIDCALL;
358 IWineD3DSurface_AddRef(s);
361 TRACE("Returning surface %p.\n", *surface);
366 static HRESULT WINAPI IWineD3DCubeTextureImpl_Map(IWineD3DCubeTexture *iface,
367 UINT sub_resource_idx, WINED3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
369 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
370 IWineD3DSurface *surface;
372 TRACE("iface %p, sub_resource_idx %u, locked_rect %p, rect %s, flags %#x.\n",
373 iface, sub_resource_idx, locked_rect, wine_dbgstr_rect(rect), flags);
375 if (!(surface = (IWineD3DSurface *)basetexture_get_sub_resource(texture, sub_resource_idx)))
377 WARN("Failed to get sub-resource.\n");
378 return WINED3DERR_INVALIDCALL;
381 return IWineD3DSurface_Map(surface, locked_rect, rect, flags);
384 static HRESULT WINAPI IWineD3DCubeTextureImpl_Unmap(IWineD3DCubeTexture *iface,
385 UINT sub_resource_idx)
387 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
388 IWineD3DSurface *surface;
390 TRACE("iface %p, sub_resource_idx %u.\n",
391 iface, sub_resource_idx);
393 if (!(surface = (IWineD3DSurface *)basetexture_get_sub_resource(texture, sub_resource_idx)))
395 WARN("Failed to get sub-resource.\n");
396 return WINED3DERR_INVALIDCALL;
399 return IWineD3DSurface_Unmap(surface);
402 static HRESULT WINAPI IWineD3DCubeTextureImpl_AddDirtyRect(IWineD3DCubeTexture *iface,
403 WINED3DCUBEMAP_FACES face, const RECT *dirty_rect)
405 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
406 UINT sub_resource_idx = face * texture->baseTexture.level_count;
407 IWineD3DSurfaceImpl *surface;
409 TRACE("iface %p, face %u, dirty_rect %s.\n",
410 iface, face, wine_dbgstr_rect(dirty_rect));
412 if (!(surface = (IWineD3DSurfaceImpl *)basetexture_get_sub_resource(texture, sub_resource_idx)))
414 WARN("Failed to get sub-resource.\n");
415 return WINED3DERR_INVALIDCALL;
418 texture->baseTexture.texture_rgb.dirty = TRUE;
419 texture->baseTexture.texture_srgb.dirty = TRUE;
420 surface_add_dirty_rect(surface, dirty_rect);
425 static const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl =
428 IWineD3DCubeTextureImpl_QueryInterface,
429 IWineD3DCubeTextureImpl_AddRef,
430 IWineD3DCubeTextureImpl_Release,
431 /* IWineD3DResource */
432 IWineD3DCubeTextureImpl_GetParent,
433 IWineD3DCubeTextureImpl_SetPrivateData,
434 IWineD3DCubeTextureImpl_GetPrivateData,
435 IWineD3DCubeTextureImpl_FreePrivateData,
436 IWineD3DCubeTextureImpl_SetPriority,
437 IWineD3DCubeTextureImpl_GetPriority,
438 IWineD3DCubeTextureImpl_PreLoad,
439 IWineD3DCubeTextureImpl_UnLoad,
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,
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);