wined3d: Retrieve the GL texture through a function.
[wine/multimedia.git] / dlls / wined3d / cubetexture.c
blob0d6d20e1e337d95bd83b3b4ec0a74ee410beffa8
1 /*
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
25 #include "config.h"
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;
34 HRESULT hr;
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;
42 UINT i;
44 for (i = 0; i < sub_count; ++i)
46 IWineD3DSurfaceImpl *surface = surface_from_resource(texture->baseTexture.sub_resources[i]);
48 if (texture->baseTexture.is_srgb)
49 surface_set_texture_name(surface, texture->baseTexture.texture_srgb.name, TRUE);
50 else
51 surface_set_texture_name(surface, texture->baseTexture.texture_rgb.name, FALSE);
55 return hr;
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;
64 struct gl_texture *gl_tex;
65 BOOL srgb_mode;
66 UINT i;
68 TRACE("texture %p, srgb %#x.\n", texture, srgb);
70 switch (srgb)
72 case SRGB_RGB:
73 srgb_mode = FALSE;
74 break;
76 case SRGB_BOTH:
77 cubetexture_preload(texture, SRGB_RGB);
78 /* Fallthrough */
80 case SRGB_SRGB:
81 srgb_mode = TRUE;
82 break;
84 default:
85 srgb_mode = texture->baseTexture.is_srgb;
86 break;
89 gl_tex = basetexture_get_gl_texture(texture, srgb_mode);
91 /* We only have to activate a context for gl when we're not drawing.
92 * In most cases PreLoad will be called during draw and a context was
93 * activated at the beginning of drawPrimitive. */
94 if (!device->isInDraw)
96 /* No danger of recursive calls, context_acquire() sets isInDraw to true
97 * when loading offscreen render targets into their texture. */
98 context = context_acquire(device, NULL);
101 if (texture->resource.format->id == WINED3DFMT_P8_UINT
102 || texture->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
104 for (i = 0; i < sub_count; ++i)
106 IWineD3DSurfaceImpl *surface = surface_from_resource(texture->baseTexture.sub_resources[i]);
108 if (palette9_changed(surface))
110 TRACE("Reloading surface %p because the d3d8/9 palette was changed.\n", surface);
111 /* TODO: This is not necessarily needed with hw palettized texture support. */
112 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
113 /* Make sure the texture is reloaded because of the palette change,
114 * this kills performance though :( */
115 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
120 /* If the texture is marked dirty or the srgb sampler setting has changed
121 * since the last load then reload the surfaces. */
122 if (gl_tex->dirty)
124 for (i = 0; i < sub_count; ++i)
126 surface_load(surface_from_resource(texture->baseTexture.sub_resources[i]), srgb_mode);
129 else
131 TRACE("Texture %p not dirty, nothing to do.\n" , texture);
134 /* No longer dirty. */
135 gl_tex->dirty = FALSE;
137 if (context) context_release(context);
140 /* Do not call while under the GL lock. */
141 static void cubetexture_unload(struct wined3d_resource *resource)
143 IWineD3DBaseTextureImpl *texture = basetexture_from_resource(resource);
144 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
145 UINT i;
147 TRACE("texture %p.\n", texture);
149 for (i = 0; i < sub_count; ++i)
151 struct wined3d_resource *sub_resource = texture->baseTexture.sub_resources[i];
152 IWineD3DSurfaceImpl *surface = surface_from_resource(sub_resource);
154 resource->resource_ops->resource_unload(sub_resource);
155 surface_set_texture_name(surface, 0, TRUE);
156 surface_set_texture_name(surface, 0, FALSE);
159 basetexture_unload(texture);
162 static const struct wined3d_texture_ops cubetexture_ops =
164 cubetexture_bind,
165 cubetexture_preload,
168 static const struct wined3d_resource_ops cubetexture_resource_ops =
170 cubetexture_unload,
173 static void cubetexture_cleanup(IWineD3DCubeTextureImpl *This)
175 UINT sub_count = This->baseTexture.level_count * This->baseTexture.layer_count;
176 UINT i;
178 TRACE("(%p) : Cleaning up.\n", This);
180 for (i = 0; i < sub_count; ++i)
182 IWineD3DSurfaceImpl *surface = surface_from_resource(This->baseTexture.sub_resources[i]);
184 if (surface)
186 /* Clean out the texture name we gave to the surface so that the
187 * surface doesn't try and release it. */
188 surface_set_texture_name(surface, 0, TRUE);
189 surface_set_texture_name(surface, 0, FALSE);
190 surface_set_texture_target(surface, 0);
191 surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
192 IWineD3DSurface_Release((IWineD3DSurface *)surface);
195 basetexture_cleanup((IWineD3DBaseTextureImpl *)This);
198 /* *******************************************
199 IWineD3DCubeTexture IUnknown parts follow
200 ******************************************* */
202 static HRESULT WINAPI IWineD3DCubeTextureImpl_QueryInterface(IWineD3DCubeTexture *iface, REFIID riid, LPVOID *ppobj)
204 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
205 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
206 if (IsEqualGUID(riid, &IID_IUnknown)
207 || IsEqualGUID(riid, &IID_IWineD3DBase)
208 || IsEqualGUID(riid, &IID_IWineD3DResource)
209 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
210 || IsEqualGUID(riid, &IID_IWineD3DCubeTexture)) {
211 IUnknown_AddRef(iface);
212 *ppobj = This;
213 return S_OK;
215 *ppobj = NULL;
216 return E_NOINTERFACE;
219 static ULONG WINAPI IWineD3DCubeTextureImpl_AddRef(IWineD3DCubeTexture *iface) {
220 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
221 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
222 return InterlockedIncrement(&This->resource.ref);
225 /* Do not call while under the GL lock. */
226 static ULONG WINAPI IWineD3DCubeTextureImpl_Release(IWineD3DCubeTexture *iface) {
227 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
228 ULONG ref;
229 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
230 ref = InterlockedDecrement(&This->resource.ref);
231 if (!ref)
233 cubetexture_cleanup(This);
234 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
235 HeapFree(GetProcessHeap(), 0, This);
237 return ref;
240 /* ****************************************************
241 IWineD3DCubeTexture IWineD3DResource parts follow
242 **************************************************** */
243 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetPrivateData(IWineD3DCubeTexture *iface,
244 REFGUID riid, const void *data, DWORD data_size, DWORD flags)
246 return resource_set_private_data(&((IWineD3DCubeTextureImpl *)iface)->resource, riid, data, data_size, flags);
249 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetPrivateData(IWineD3DCubeTexture *iface,
250 REFGUID guid, void *data, DWORD *data_size)
252 return resource_get_private_data(&((IWineD3DCubeTextureImpl *)iface)->resource, guid, data, data_size);
255 static HRESULT WINAPI IWineD3DCubeTextureImpl_FreePrivateData(IWineD3DCubeTexture *iface, REFGUID refguid)
257 return resource_free_private_data(&((IWineD3DCubeTextureImpl *)iface)->resource, refguid);
260 static DWORD WINAPI IWineD3DCubeTextureImpl_SetPriority(IWineD3DCubeTexture *iface, DWORD priority)
262 return resource_set_priority(&((IWineD3DCubeTextureImpl *)iface)->resource, priority);
265 static DWORD WINAPI IWineD3DCubeTextureImpl_GetPriority(IWineD3DCubeTexture *iface)
267 return resource_get_priority(&((IWineD3DCubeTextureImpl *)iface)->resource);
270 /* Do not call while under the GL lock. */
271 static void WINAPI IWineD3DCubeTextureImpl_PreLoad(IWineD3DCubeTexture *iface)
273 cubetexture_preload((IWineD3DBaseTextureImpl *)iface, SRGB_ANY);
276 static WINED3DRESOURCETYPE WINAPI IWineD3DCubeTextureImpl_GetType(IWineD3DCubeTexture *iface)
278 return resource_get_type(&((IWineD3DCubeTextureImpl *)iface)->resource);
281 static void * WINAPI IWineD3DCubeTextureImpl_GetParent(IWineD3DCubeTexture *iface)
283 TRACE("iface %p.\n", iface);
285 return ((IWineD3DCubeTextureImpl *)iface)->resource.parent;
288 /* ******************************************************
289 IWineD3DCubeTexture IWineD3DBaseTexture parts follow
290 ****************************************************** */
291 static DWORD WINAPI IWineD3DCubeTextureImpl_SetLOD(IWineD3DCubeTexture *iface, DWORD LODNew) {
292 return basetexture_set_lod((IWineD3DBaseTextureImpl *)iface, LODNew);
295 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLOD(IWineD3DCubeTexture *iface) {
296 return basetexture_get_lod((IWineD3DBaseTextureImpl *)iface);
299 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLevelCount(IWineD3DCubeTexture *iface)
301 return basetexture_get_level_count((IWineD3DBaseTextureImpl *)iface);
304 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetAutoGenFilterType(IWineD3DCubeTexture *iface,
305 WINED3DTEXTUREFILTERTYPE FilterType)
307 return basetexture_set_autogen_filter_type((IWineD3DBaseTextureImpl *)iface, FilterType);
310 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DCubeTextureImpl_GetAutoGenFilterType(IWineD3DCubeTexture *iface)
312 return basetexture_get_autogen_filter_type((IWineD3DBaseTextureImpl *)iface);
315 static void WINAPI IWineD3DCubeTextureImpl_GenerateMipSubLevels(IWineD3DCubeTexture *iface)
317 basetexture_generate_mipmaps((IWineD3DBaseTextureImpl *)iface);
320 static BOOL WINAPI IWineD3DCubeTextureImpl_IsCondNP2(IWineD3DCubeTexture *iface)
322 TRACE("iface %p.\n", iface);
324 return FALSE;
327 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetLevelDesc(IWineD3DCubeTexture *iface,
328 UINT sub_resource_idx, WINED3DSURFACE_DESC *desc)
330 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
331 struct wined3d_resource *sub_resource;
333 TRACE("iface %p, sub_resource_idx %u, desc %p.\n", iface, sub_resource_idx, desc);
335 if (!(sub_resource = basetexture_get_sub_resource(texture, sub_resource_idx)))
337 WARN("Failed to get sub-resource.\n");
338 return WINED3DERR_INVALIDCALL;
341 IWineD3DSurface_GetDesc((IWineD3DSurface *)surface_from_resource(sub_resource), desc);
343 return WINED3D_OK;
346 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetCubeMapSurface(IWineD3DCubeTexture *iface,
347 UINT sub_resource_idx, IWineD3DSurface **surface)
349 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
350 struct wined3d_resource *sub_resource;
352 TRACE("iface %p, sub_resource_idx %u, surface %p.\n",
353 iface, sub_resource_idx, surface);
355 if (!(sub_resource = basetexture_get_sub_resource(texture, sub_resource_idx)))
357 WARN("Failed to get sub-resource.\n");
358 return WINED3DERR_INVALIDCALL;
361 *surface = (IWineD3DSurface *)surface_from_resource(sub_resource);
362 IWineD3DSurface_AddRef(*surface);
364 TRACE("Returning surface %p.\n", *surface);
366 return WINED3D_OK;
369 static HRESULT WINAPI IWineD3DCubeTextureImpl_Map(IWineD3DCubeTexture *iface,
370 UINT sub_resource_idx, WINED3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
372 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
373 struct wined3d_resource *sub_resource;
375 TRACE("iface %p, sub_resource_idx %u, locked_rect %p, rect %s, flags %#x.\n",
376 iface, sub_resource_idx, locked_rect, wine_dbgstr_rect(rect), flags);
378 if (!(sub_resource = basetexture_get_sub_resource(texture, sub_resource_idx)))
380 WARN("Failed to get sub-resource.\n");
381 return WINED3DERR_INVALIDCALL;
384 return IWineD3DSurface_Map((IWineD3DSurface *)surface_from_resource(sub_resource),
385 locked_rect, rect, flags);
388 static HRESULT WINAPI IWineD3DCubeTextureImpl_Unmap(IWineD3DCubeTexture *iface,
389 UINT sub_resource_idx)
391 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
392 struct wined3d_resource *sub_resource;
394 TRACE("iface %p, sub_resource_idx %u.\n",
395 iface, sub_resource_idx);
397 if (!(sub_resource = basetexture_get_sub_resource(texture, sub_resource_idx)))
399 WARN("Failed to get sub-resource.\n");
400 return WINED3DERR_INVALIDCALL;
403 return IWineD3DSurface_Unmap((IWineD3DSurface *)surface_from_resource(sub_resource));
406 static HRESULT WINAPI IWineD3DCubeTextureImpl_AddDirtyRect(IWineD3DCubeTexture *iface,
407 WINED3DCUBEMAP_FACES face, const RECT *dirty_rect)
409 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
410 UINT sub_resource_idx = face * texture->baseTexture.level_count;
411 struct wined3d_resource *sub_resource;
413 TRACE("iface %p, face %u, dirty_rect %s.\n",
414 iface, face, wine_dbgstr_rect(dirty_rect));
416 if (!(sub_resource = basetexture_get_sub_resource(texture, sub_resource_idx)))
418 WARN("Failed to get sub-resource.\n");
419 return WINED3DERR_INVALIDCALL;
422 basetexture_set_dirty(texture, TRUE);
423 surface_add_dirty_rect(surface_from_resource(sub_resource), dirty_rect);
425 return WINED3D_OK;
428 static const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl =
430 /* IUnknown */
431 IWineD3DCubeTextureImpl_QueryInterface,
432 IWineD3DCubeTextureImpl_AddRef,
433 IWineD3DCubeTextureImpl_Release,
434 /* IWineD3DResource */
435 IWineD3DCubeTextureImpl_GetParent,
436 IWineD3DCubeTextureImpl_SetPrivateData,
437 IWineD3DCubeTextureImpl_GetPrivateData,
438 IWineD3DCubeTextureImpl_FreePrivateData,
439 IWineD3DCubeTextureImpl_SetPriority,
440 IWineD3DCubeTextureImpl_GetPriority,
441 IWineD3DCubeTextureImpl_PreLoad,
442 IWineD3DCubeTextureImpl_GetType,
443 /* IWineD3DBaseTexture */
444 IWineD3DCubeTextureImpl_SetLOD,
445 IWineD3DCubeTextureImpl_GetLOD,
446 IWineD3DCubeTextureImpl_GetLevelCount,
447 IWineD3DCubeTextureImpl_SetAutoGenFilterType,
448 IWineD3DCubeTextureImpl_GetAutoGenFilterType,
449 IWineD3DCubeTextureImpl_GenerateMipSubLevels,
450 IWineD3DCubeTextureImpl_IsCondNP2,
451 /* IWineD3DCubeTexture */
452 IWineD3DCubeTextureImpl_GetLevelDesc,
453 IWineD3DCubeTextureImpl_GetCubeMapSurface,
454 IWineD3DCubeTextureImpl_Map,
455 IWineD3DCubeTextureImpl_Unmap,
456 IWineD3DCubeTextureImpl_AddDirtyRect
459 HRESULT cubetexture_init(IWineD3DCubeTextureImpl *texture, UINT edge_length, UINT levels,
460 IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
461 void *parent, const struct wined3d_parent_ops *parent_ops)
463 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
464 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
465 UINT pow2_edge_length;
466 unsigned int i, j;
467 UINT tmp_w;
468 HRESULT hr;
470 /* TODO: It should only be possible to create textures for formats
471 * that are reported as supported. */
472 if (WINED3DFMT_UNKNOWN >= format_id)
474 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
475 return WINED3DERR_INVALIDCALL;
478 if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && pool != WINED3DPOOL_SCRATCH)
480 WARN("(%p) : Tried to create not supported cube texture.\n", texture);
481 return WINED3DERR_INVALIDCALL;
484 /* Calculate levels for mip mapping */
485 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
487 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
489 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
490 return WINED3DERR_INVALIDCALL;
493 if (levels > 1)
495 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
496 return WINED3DERR_INVALIDCALL;
499 levels = 1;
501 else if (!levels)
503 levels = wined3d_log2i(edge_length) + 1;
504 TRACE("Calculated levels = %u.\n", levels);
507 texture->lpVtbl = &IWineD3DCubeTexture_Vtbl;
509 hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, &cubetexture_ops,
510 6, levels, WINED3DRTYPE_CUBETEXTURE, device, usage, format, pool,
511 parent, parent_ops, &cubetexture_resource_ops);
512 if (FAILED(hr))
514 WARN("Failed to initialize basetexture, returning %#x\n", hr);
515 return hr;
518 /* Find the nearest pow2 match. */
519 pow2_edge_length = 1;
520 while (pow2_edge_length < edge_length) pow2_edge_length <<= 1;
522 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || (edge_length == pow2_edge_length))
524 /* Precalculated scaling for 'faked' non power of two texture coords. */
525 texture->baseTexture.pow2Matrix[0] = 1.0f;
526 texture->baseTexture.pow2Matrix[5] = 1.0f;
527 texture->baseTexture.pow2Matrix[10] = 1.0f;
528 texture->baseTexture.pow2Matrix[15] = 1.0f;
530 else
532 /* Precalculated scaling for 'faked' non power of two texture coords. */
533 texture->baseTexture.pow2Matrix[0] = ((float)edge_length) / ((float)pow2_edge_length);
534 texture->baseTexture.pow2Matrix[5] = ((float)edge_length) / ((float)pow2_edge_length);
535 texture->baseTexture.pow2Matrix[10] = ((float)edge_length) / ((float)pow2_edge_length);
536 texture->baseTexture.pow2Matrix[15] = 1.0f;
537 texture->baseTexture.pow2Matrix_identity = FALSE;
539 texture->baseTexture.target = GL_TEXTURE_CUBE_MAP_ARB;
541 /* Generate all the surfaces. */
542 tmp_w = edge_length;
543 for (i = 0; i < texture->baseTexture.level_count; ++i)
545 /* Create the 6 faces. */
546 for (j = 0; j < 6; ++j)
548 static const GLenum cube_targets[6] =
550 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
551 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
552 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
553 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
554 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
555 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
557 UINT idx = j * texture->baseTexture.level_count + i;
558 IWineD3DSurface *surface;
560 hr = IWineD3DDeviceParent_CreateSurface(device->device_parent, parent, tmp_w, tmp_w,
561 format_id, usage, pool, i /* Level */, j, &surface);
562 if (FAILED(hr))
564 FIXME("(%p) Failed to create surface, hr %#x.\n", texture, hr);
565 cubetexture_cleanup(texture);
566 return hr;
569 surface_set_container((IWineD3DSurfaceImpl *)surface, WINED3D_CONTAINER_TEXTURE, (IWineD3DBase *)texture);
570 surface_set_texture_target((IWineD3DSurfaceImpl *)surface, cube_targets[j]);
571 texture->baseTexture.sub_resources[idx] = &((IWineD3DSurfaceImpl *)surface)->resource;
572 TRACE("Created surface level %u @ %p.\n", i, surface);
574 tmp_w = max(1, tmp_w >> 1);
577 return WINED3D_OK;