wined3d: Get rid of IWineD3DBaseTexture::IsCondNP2().
[wine.git] / dlls / wined3d / cubetexture.c
blobaed36748b1083dd56a260735af74504b1f034cb4
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,
32 const struct wined3d_gl_info *gl_info, BOOL srgb)
34 BOOL set_gl_texture_desc;
35 HRESULT hr;
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 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
43 BOOL srgb_tex = !gl_info->supported[EXT_TEXTURE_SRGB_DECODE] && texture->baseTexture.is_srgb;
44 GLuint name = srgb_tex ? texture->baseTexture.texture_srgb.name : texture->baseTexture.texture_rgb.name;
45 UINT i;
47 for (i = 0; i < sub_count; ++i)
49 IWineD3DSurfaceImpl *surface = surface_from_resource(texture->baseTexture.sub_resources[i]);
50 surface_set_texture_name(surface, name, srgb_tex);
54 return hr;
57 /* Do not call while under the GL lock. */
58 static void cubetexture_preload(IWineD3DBaseTextureImpl *texture, enum WINED3DSRGB srgb)
60 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
61 IWineD3DDeviceImpl *device = texture->resource.device;
62 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
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, gl_info, 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 sub_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 struct wined3d_resource *sub_resource = This->baseTexture.sub_resources[i];
184 if (sub_resource)
186 IWineD3DSurfaceImpl *surface = surface_from_resource(sub_resource);
188 /* Clean out the texture name we gave to the surface so that the
189 * surface doesn't try and release it. */
190 surface_set_texture_name(surface, 0, TRUE);
191 surface_set_texture_name(surface, 0, FALSE);
192 surface_set_texture_target(surface, 0);
193 surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
194 IWineD3DSurface_Release((IWineD3DSurface *)surface);
197 basetexture_cleanup((IWineD3DBaseTextureImpl *)This);
200 /* *******************************************
201 IWineD3DCubeTexture IUnknown parts follow
202 ******************************************* */
204 static HRESULT WINAPI IWineD3DCubeTextureImpl_QueryInterface(IWineD3DCubeTexture *iface, REFIID riid, LPVOID *ppobj)
206 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
207 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
208 if (IsEqualGUID(riid, &IID_IUnknown)
209 || IsEqualGUID(riid, &IID_IWineD3DBase)
210 || IsEqualGUID(riid, &IID_IWineD3DResource)
211 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
212 || IsEqualGUID(riid, &IID_IWineD3DCubeTexture)) {
213 IUnknown_AddRef(iface);
214 *ppobj = This;
215 return S_OK;
217 *ppobj = NULL;
218 return E_NOINTERFACE;
221 static ULONG WINAPI IWineD3DCubeTextureImpl_AddRef(IWineD3DCubeTexture *iface) {
222 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
223 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
224 return InterlockedIncrement(&This->resource.ref);
227 /* Do not call while under the GL lock. */
228 static ULONG WINAPI IWineD3DCubeTextureImpl_Release(IWineD3DCubeTexture *iface) {
229 IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
230 ULONG ref;
231 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
232 ref = InterlockedDecrement(&This->resource.ref);
233 if (!ref)
235 cubetexture_cleanup(This);
236 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
237 HeapFree(GetProcessHeap(), 0, This);
239 return ref;
242 /* ****************************************************
243 IWineD3DCubeTexture IWineD3DResource parts follow
244 **************************************************** */
245 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetPrivateData(IWineD3DCubeTexture *iface,
246 REFGUID riid, const void *data, DWORD data_size, DWORD flags)
248 return resource_set_private_data(&((IWineD3DCubeTextureImpl *)iface)->resource, riid, data, data_size, flags);
251 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetPrivateData(IWineD3DCubeTexture *iface,
252 REFGUID guid, void *data, DWORD *data_size)
254 return resource_get_private_data(&((IWineD3DCubeTextureImpl *)iface)->resource, guid, data, data_size);
257 static HRESULT WINAPI IWineD3DCubeTextureImpl_FreePrivateData(IWineD3DCubeTexture *iface, REFGUID refguid)
259 return resource_free_private_data(&((IWineD3DCubeTextureImpl *)iface)->resource, refguid);
262 static DWORD WINAPI IWineD3DCubeTextureImpl_SetPriority(IWineD3DCubeTexture *iface, DWORD priority)
264 return resource_set_priority(&((IWineD3DCubeTextureImpl *)iface)->resource, priority);
267 static DWORD WINAPI IWineD3DCubeTextureImpl_GetPriority(IWineD3DCubeTexture *iface)
269 return resource_get_priority(&((IWineD3DCubeTextureImpl *)iface)->resource);
272 /* Do not call while under the GL lock. */
273 static void WINAPI IWineD3DCubeTextureImpl_PreLoad(IWineD3DCubeTexture *iface)
275 cubetexture_preload((IWineD3DBaseTextureImpl *)iface, SRGB_ANY);
278 static WINED3DRESOURCETYPE WINAPI IWineD3DCubeTextureImpl_GetType(IWineD3DCubeTexture *iface)
280 return resource_get_type(&((IWineD3DCubeTextureImpl *)iface)->resource);
283 static void * WINAPI IWineD3DCubeTextureImpl_GetParent(IWineD3DCubeTexture *iface)
285 TRACE("iface %p.\n", iface);
287 return ((IWineD3DCubeTextureImpl *)iface)->resource.parent;
290 /* ******************************************************
291 IWineD3DCubeTexture IWineD3DBaseTexture parts follow
292 ****************************************************** */
293 static DWORD WINAPI IWineD3DCubeTextureImpl_SetLOD(IWineD3DCubeTexture *iface, DWORD LODNew) {
294 return basetexture_set_lod((IWineD3DBaseTextureImpl *)iface, LODNew);
297 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLOD(IWineD3DCubeTexture *iface) {
298 return basetexture_get_lod((IWineD3DBaseTextureImpl *)iface);
301 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLevelCount(IWineD3DCubeTexture *iface)
303 return basetexture_get_level_count((IWineD3DBaseTextureImpl *)iface);
306 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetAutoGenFilterType(IWineD3DCubeTexture *iface,
307 WINED3DTEXTUREFILTERTYPE FilterType)
309 return basetexture_set_autogen_filter_type((IWineD3DBaseTextureImpl *)iface, FilterType);
312 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DCubeTextureImpl_GetAutoGenFilterType(IWineD3DCubeTexture *iface)
314 return basetexture_get_autogen_filter_type((IWineD3DBaseTextureImpl *)iface);
317 static void WINAPI IWineD3DCubeTextureImpl_GenerateMipSubLevels(IWineD3DCubeTexture *iface)
319 basetexture_generate_mipmaps((IWineD3DBaseTextureImpl *)iface);
322 static struct wined3d_resource * WINAPI IWineD3DCubeTextureImpl_GetSubResource(IWineD3DCubeTexture *iface,
323 UINT sub_resource_idx)
325 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
327 TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
329 return basetexture_get_sub_resource(texture, sub_resource_idx);
332 static HRESULT WINAPI IWineD3DCubeTextureImpl_AddDirtyRegion(IWineD3DCubeTexture *iface,
333 UINT layer, const WINED3DBOX *dirty_region)
335 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
336 struct wined3d_resource *sub_resource;
338 TRACE("iface %p, layer %u, dirty_region %p.\n", iface, layer, dirty_region);
340 if (!(sub_resource = basetexture_get_sub_resource(texture, layer * texture->baseTexture.level_count)))
342 WARN("Failed to get sub-resource.\n");
343 return WINED3DERR_INVALIDCALL;
346 basetexture_set_dirty(texture, TRUE);
347 surface_add_dirty_rect(surface_from_resource(sub_resource), dirty_region);
349 return WINED3D_OK;
352 static const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl =
354 /* IUnknown */
355 IWineD3DCubeTextureImpl_QueryInterface,
356 IWineD3DCubeTextureImpl_AddRef,
357 IWineD3DCubeTextureImpl_Release,
358 /* IWineD3DResource */
359 IWineD3DCubeTextureImpl_GetParent,
360 IWineD3DCubeTextureImpl_SetPrivateData,
361 IWineD3DCubeTextureImpl_GetPrivateData,
362 IWineD3DCubeTextureImpl_FreePrivateData,
363 IWineD3DCubeTextureImpl_SetPriority,
364 IWineD3DCubeTextureImpl_GetPriority,
365 IWineD3DCubeTextureImpl_PreLoad,
366 IWineD3DCubeTextureImpl_GetType,
367 /* IWineD3DBaseTexture */
368 IWineD3DCubeTextureImpl_SetLOD,
369 IWineD3DCubeTextureImpl_GetLOD,
370 IWineD3DCubeTextureImpl_GetLevelCount,
371 IWineD3DCubeTextureImpl_SetAutoGenFilterType,
372 IWineD3DCubeTextureImpl_GetAutoGenFilterType,
373 IWineD3DCubeTextureImpl_GenerateMipSubLevels,
374 IWineD3DCubeTextureImpl_GetSubResource,
375 IWineD3DCubeTextureImpl_AddDirtyRegion,
378 HRESULT cubetexture_init(IWineD3DCubeTextureImpl *texture, UINT edge_length, UINT levels,
379 IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
380 void *parent, const struct wined3d_parent_ops *parent_ops)
382 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
383 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
384 UINT pow2_edge_length;
385 unsigned int i, j;
386 UINT tmp_w;
387 HRESULT hr;
389 /* TODO: It should only be possible to create textures for formats
390 * that are reported as supported. */
391 if (WINED3DFMT_UNKNOWN >= format_id)
393 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
394 return WINED3DERR_INVALIDCALL;
397 if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && pool != WINED3DPOOL_SCRATCH)
399 WARN("(%p) : Tried to create not supported cube texture.\n", texture);
400 return WINED3DERR_INVALIDCALL;
403 /* Calculate levels for mip mapping */
404 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
406 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
408 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
409 return WINED3DERR_INVALIDCALL;
412 if (levels > 1)
414 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
415 return WINED3DERR_INVALIDCALL;
418 levels = 1;
420 else if (!levels)
422 levels = wined3d_log2i(edge_length) + 1;
423 TRACE("Calculated levels = %u.\n", levels);
426 texture->lpVtbl = &IWineD3DCubeTexture_Vtbl;
428 hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, &cubetexture_ops,
429 6, levels, WINED3DRTYPE_CUBETEXTURE, device, usage, format, pool,
430 parent, parent_ops, &cubetexture_resource_ops);
431 if (FAILED(hr))
433 WARN("Failed to initialize basetexture, returning %#x\n", hr);
434 return hr;
437 /* Find the nearest pow2 match. */
438 pow2_edge_length = 1;
439 while (pow2_edge_length < edge_length) pow2_edge_length <<= 1;
441 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || (edge_length == pow2_edge_length))
443 /* Precalculated scaling for 'faked' non power of two texture coords. */
444 texture->baseTexture.pow2Matrix[0] = 1.0f;
445 texture->baseTexture.pow2Matrix[5] = 1.0f;
446 texture->baseTexture.pow2Matrix[10] = 1.0f;
447 texture->baseTexture.pow2Matrix[15] = 1.0f;
449 else
451 /* Precalculated scaling for 'faked' non power of two texture coords. */
452 texture->baseTexture.pow2Matrix[0] = ((float)edge_length) / ((float)pow2_edge_length);
453 texture->baseTexture.pow2Matrix[5] = ((float)edge_length) / ((float)pow2_edge_length);
454 texture->baseTexture.pow2Matrix[10] = ((float)edge_length) / ((float)pow2_edge_length);
455 texture->baseTexture.pow2Matrix[15] = 1.0f;
456 texture->baseTexture.pow2Matrix_identity = FALSE;
458 texture->baseTexture.target = GL_TEXTURE_CUBE_MAP_ARB;
460 /* Generate all the surfaces. */
461 tmp_w = edge_length;
462 for (i = 0; i < texture->baseTexture.level_count; ++i)
464 /* Create the 6 faces. */
465 for (j = 0; j < 6; ++j)
467 static const GLenum cube_targets[6] =
469 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
470 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
471 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
472 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
473 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
474 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
476 UINT idx = j * texture->baseTexture.level_count + i;
477 IWineD3DSurface *surface;
479 hr = IWineD3DDeviceParent_CreateSurface(device->device_parent, parent, tmp_w, tmp_w,
480 format_id, usage, pool, i /* Level */, j, &surface);
481 if (FAILED(hr))
483 FIXME("(%p) Failed to create surface, hr %#x.\n", texture, hr);
484 cubetexture_cleanup(texture);
485 return hr;
488 surface_set_container((IWineD3DSurfaceImpl *)surface, WINED3D_CONTAINER_TEXTURE, (IWineD3DBase *)texture);
489 surface_set_texture_target((IWineD3DSurfaceImpl *)surface, cube_targets[j]);
490 texture->baseTexture.sub_resources[idx] = &((IWineD3DSurfaceImpl *)surface)->resource;
491 TRACE("Created surface level %u @ %p.\n", i, surface);
493 tmp_w = max(1, tmp_w >> 1);
496 return WINED3D_OK;