wined3d: Get rid of the IWineD3DBase requirement for surface_set_container().
[wine.git] / dlls / wined3d / cubetexture.c
blobaf4947cd113963e9e68f1ea33d5fb962232bdbac
1 /*
2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6 * Copyright 2009-2010 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
28 /* Context activation is done by the caller. */
29 static HRESULT cubetexture_bind(IWineD3DBaseTextureImpl *texture,
30 const struct wined3d_gl_info *gl_info, BOOL srgb)
32 BOOL set_gl_texture_desc;
33 HRESULT hr;
35 TRACE("texture %p, gl_info %p, srgb %#x.\n", texture, gl_info, srgb);
37 hr = basetexture_bind(texture, gl_info, srgb, &set_gl_texture_desc);
38 if (set_gl_texture_desc && SUCCEEDED(hr))
40 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
41 BOOL srgb_tex = !gl_info->supported[EXT_TEXTURE_SRGB_DECODE] && texture->baseTexture.is_srgb;
42 GLuint name = srgb_tex ? texture->baseTexture.texture_srgb.name : texture->baseTexture.texture_rgb.name;
43 UINT i;
45 for (i = 0; i < sub_count; ++i)
47 IWineD3DSurfaceImpl *surface = surface_from_resource(texture->baseTexture.sub_resources[i]);
48 surface_set_texture_name(surface, name, srgb_tex);
52 return hr;
55 /* Do not call while under the GL lock. */
56 static void cubetexture_preload(IWineD3DBaseTextureImpl *texture, enum WINED3DSRGB srgb)
58 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
59 IWineD3DDeviceImpl *device = texture->resource.device;
60 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
61 struct wined3d_context *context = NULL;
62 struct gl_texture *gl_tex;
63 BOOL srgb_mode;
64 UINT i;
66 TRACE("texture %p, srgb %#x.\n", texture, srgb);
68 switch (srgb)
70 case SRGB_RGB:
71 srgb_mode = FALSE;
72 break;
74 case SRGB_BOTH:
75 cubetexture_preload(texture, SRGB_RGB);
76 /* Fallthrough */
78 case SRGB_SRGB:
79 srgb_mode = TRUE;
80 break;
82 default:
83 srgb_mode = texture->baseTexture.is_srgb;
84 break;
87 gl_tex = basetexture_get_gl_texture(texture, gl_info, srgb_mode);
89 /* We only have to activate a context for gl when we're not drawing.
90 * In most cases PreLoad will be called during draw and a context was
91 * activated at the beginning of drawPrimitive. */
92 if (!device->isInDraw)
94 /* No danger of recursive calls, context_acquire() sets isInDraw to true
95 * when loading offscreen render targets into their texture. */
96 context = context_acquire(device, NULL);
99 if (texture->resource.format->id == WINED3DFMT_P8_UINT
100 || texture->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
102 for (i = 0; i < sub_count; ++i)
104 IWineD3DSurfaceImpl *surface = surface_from_resource(texture->baseTexture.sub_resources[i]);
106 if (palette9_changed(surface))
108 TRACE("Reloading surface %p because the d3d8/9 palette was changed.\n", surface);
109 /* TODO: This is not necessarily needed with hw palettized texture support. */
110 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
111 /* Make sure the texture is reloaded because of the palette change,
112 * this kills performance though :( */
113 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
118 /* If the texture is marked dirty or the srgb sampler setting has changed
119 * since the last load then reload the surfaces. */
120 if (gl_tex->dirty)
122 for (i = 0; i < sub_count; ++i)
124 surface_load(surface_from_resource(texture->baseTexture.sub_resources[i]), srgb_mode);
127 else
129 TRACE("Texture %p not dirty, nothing to do.\n" , texture);
132 /* No longer dirty. */
133 gl_tex->dirty = FALSE;
135 if (context) context_release(context);
138 static void cubetexture_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
139 const WINED3DBOX *dirty_region)
141 surface_add_dirty_rect(surface_from_resource(sub_resource), dirty_region);
144 static void cubetexture_sub_resource_cleanup(struct wined3d_resource *sub_resource)
146 IWineD3DSurfaceImpl *surface = surface_from_resource(sub_resource);
148 /* Clean out the texture name we gave to the surface so that the
149 * surface doesn't try and release it. */
150 surface_set_texture_name(surface, 0, TRUE);
151 surface_set_texture_name(surface, 0, FALSE);
152 surface_set_texture_target(surface, 0);
153 surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
154 IWineD3DSurface_Release((IWineD3DSurface *)surface);
157 /* Do not call while under the GL lock. */
158 static void cubetexture_unload(struct wined3d_resource *resource)
160 IWineD3DBaseTextureImpl *texture = basetexture_from_resource(resource);
161 UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
162 UINT i;
164 TRACE("texture %p.\n", texture);
166 for (i = 0; i < sub_count; ++i)
168 struct wined3d_resource *sub_resource = texture->baseTexture.sub_resources[i];
169 IWineD3DSurfaceImpl *surface = surface_from_resource(sub_resource);
171 sub_resource->resource_ops->resource_unload(sub_resource);
172 surface_set_texture_name(surface, 0, TRUE);
173 surface_set_texture_name(surface, 0, FALSE);
176 basetexture_unload(texture);
179 static const struct wined3d_texture_ops cubetexture_ops =
181 cubetexture_bind,
182 cubetexture_preload,
183 cubetexture_sub_resource_add_dirty_region,
184 cubetexture_sub_resource_cleanup,
187 static const struct wined3d_resource_ops cubetexture_resource_ops =
189 cubetexture_unload,
192 static HRESULT WINAPI IWineD3DCubeTextureImpl_QueryInterface(IWineD3DBaseTexture *iface, REFIID riid, LPVOID *ppobj)
194 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
195 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
196 if (IsEqualGUID(riid, &IID_IUnknown)
197 || IsEqualGUID(riid, &IID_IWineD3DBase)
198 || IsEqualGUID(riid, &IID_IWineD3DResource)
199 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture))
201 IUnknown_AddRef(iface);
202 *ppobj = This;
203 return S_OK;
205 *ppobj = NULL;
206 return E_NOINTERFACE;
209 static ULONG WINAPI IWineD3DCubeTextureImpl_AddRef(IWineD3DBaseTexture *iface)
211 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
212 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
213 return InterlockedIncrement(&This->resource.ref);
216 /* Do not call while under the GL lock. */
217 static ULONG WINAPI IWineD3DCubeTextureImpl_Release(IWineD3DBaseTexture *iface)
219 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
220 ULONG ref;
221 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
222 ref = InterlockedDecrement(&This->resource.ref);
223 if (!ref)
225 basetexture_cleanup(This);
226 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
227 HeapFree(GetProcessHeap(), 0, This);
229 return ref;
232 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetPrivateData(IWineD3DBaseTexture *iface,
233 REFGUID riid, const void *data, DWORD data_size, DWORD flags)
235 return resource_set_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, riid, data, data_size, flags);
238 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetPrivateData(IWineD3DBaseTexture *iface,
239 REFGUID guid, void *data, DWORD *data_size)
241 return resource_get_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, guid, data, data_size);
244 static HRESULT WINAPI IWineD3DCubeTextureImpl_FreePrivateData(IWineD3DBaseTexture *iface, REFGUID refguid)
246 return resource_free_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, refguid);
249 static DWORD WINAPI IWineD3DCubeTextureImpl_SetPriority(IWineD3DBaseTexture *iface, DWORD priority)
251 return resource_set_priority(&((IWineD3DBaseTextureImpl *)iface)->resource, priority);
254 static DWORD WINAPI IWineD3DCubeTextureImpl_GetPriority(IWineD3DBaseTexture *iface)
256 return resource_get_priority(&((IWineD3DBaseTextureImpl *)iface)->resource);
259 /* Do not call while under the GL lock. */
260 static void WINAPI IWineD3DCubeTextureImpl_PreLoad(IWineD3DBaseTexture *iface)
262 cubetexture_preload((IWineD3DBaseTextureImpl *)iface, SRGB_ANY);
265 static WINED3DRESOURCETYPE WINAPI IWineD3DCubeTextureImpl_GetType(IWineD3DBaseTexture *iface)
267 return resource_get_type(&((IWineD3DBaseTextureImpl *)iface)->resource);
270 static void * WINAPI IWineD3DCubeTextureImpl_GetParent(IWineD3DBaseTexture *iface)
272 TRACE("iface %p.\n", iface);
274 return ((IWineD3DBaseTextureImpl *)iface)->resource.parent;
277 static DWORD WINAPI IWineD3DCubeTextureImpl_SetLOD(IWineD3DBaseTexture *iface, DWORD LODNew)
279 return basetexture_set_lod((IWineD3DBaseTextureImpl *)iface, LODNew);
282 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLOD(IWineD3DBaseTexture *iface)
284 return basetexture_get_lod((IWineD3DBaseTextureImpl *)iface);
287 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLevelCount(IWineD3DBaseTexture *iface)
289 return basetexture_get_level_count((IWineD3DBaseTextureImpl *)iface);
292 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetAutoGenFilterType(IWineD3DBaseTexture *iface,
293 WINED3DTEXTUREFILTERTYPE FilterType)
295 return basetexture_set_autogen_filter_type((IWineD3DBaseTextureImpl *)iface, FilterType);
298 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DCubeTextureImpl_GetAutoGenFilterType(IWineD3DBaseTexture *iface)
300 return basetexture_get_autogen_filter_type((IWineD3DBaseTextureImpl *)iface);
303 static void WINAPI IWineD3DCubeTextureImpl_GenerateMipSubLevels(IWineD3DBaseTexture *iface)
305 basetexture_generate_mipmaps((IWineD3DBaseTextureImpl *)iface);
308 static struct wined3d_resource * WINAPI IWineD3DCubeTextureImpl_GetSubResource(IWineD3DBaseTexture *iface,
309 UINT sub_resource_idx)
311 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
313 TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
315 return basetexture_get_sub_resource(texture, sub_resource_idx);
318 static HRESULT WINAPI IWineD3DCubeTextureImpl_AddDirtyRegion(IWineD3DBaseTexture *iface,
319 UINT layer, const WINED3DBOX *dirty_region)
321 return basetexture_add_dirty_region((IWineD3DBaseTextureImpl *)iface, layer, dirty_region);
324 static const IWineD3DBaseTextureVtbl IWineD3DCubeTexture_Vtbl =
326 /* IUnknown */
327 IWineD3DCubeTextureImpl_QueryInterface,
328 IWineD3DCubeTextureImpl_AddRef,
329 IWineD3DCubeTextureImpl_Release,
330 /* IWineD3DResource */
331 IWineD3DCubeTextureImpl_GetParent,
332 IWineD3DCubeTextureImpl_SetPrivateData,
333 IWineD3DCubeTextureImpl_GetPrivateData,
334 IWineD3DCubeTextureImpl_FreePrivateData,
335 IWineD3DCubeTextureImpl_SetPriority,
336 IWineD3DCubeTextureImpl_GetPriority,
337 IWineD3DCubeTextureImpl_PreLoad,
338 IWineD3DCubeTextureImpl_GetType,
339 /* IWineD3DBaseTexture */
340 IWineD3DCubeTextureImpl_SetLOD,
341 IWineD3DCubeTextureImpl_GetLOD,
342 IWineD3DCubeTextureImpl_GetLevelCount,
343 IWineD3DCubeTextureImpl_SetAutoGenFilterType,
344 IWineD3DCubeTextureImpl_GetAutoGenFilterType,
345 IWineD3DCubeTextureImpl_GenerateMipSubLevels,
346 IWineD3DCubeTextureImpl_GetSubResource,
347 IWineD3DCubeTextureImpl_AddDirtyRegion,
350 HRESULT cubetexture_init(IWineD3DBaseTextureImpl *texture, UINT edge_length, UINT levels,
351 IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
352 void *parent, const struct wined3d_parent_ops *parent_ops)
354 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
355 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
356 UINT pow2_edge_length;
357 unsigned int i, j;
358 UINT tmp_w;
359 HRESULT hr;
361 /* TODO: It should only be possible to create textures for formats
362 * that are reported as supported. */
363 if (WINED3DFMT_UNKNOWN >= format_id)
365 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
366 return WINED3DERR_INVALIDCALL;
369 if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && pool != WINED3DPOOL_SCRATCH)
371 WARN("(%p) : Tried to create not supported cube texture.\n", texture);
372 return WINED3DERR_INVALIDCALL;
375 /* Calculate levels for mip mapping */
376 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
378 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
380 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
381 return WINED3DERR_INVALIDCALL;
384 if (levels > 1)
386 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
387 return WINED3DERR_INVALIDCALL;
390 levels = 1;
392 else if (!levels)
394 levels = wined3d_log2i(edge_length) + 1;
395 TRACE("Calculated levels = %u.\n", levels);
398 texture->lpVtbl = &IWineD3DCubeTexture_Vtbl;
400 hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, &cubetexture_ops,
401 6, levels, WINED3DRTYPE_CUBETEXTURE, device, usage, format, pool,
402 parent, parent_ops, &cubetexture_resource_ops);
403 if (FAILED(hr))
405 WARN("Failed to initialize basetexture, returning %#x\n", hr);
406 return hr;
409 /* Find the nearest pow2 match. */
410 pow2_edge_length = 1;
411 while (pow2_edge_length < edge_length) pow2_edge_length <<= 1;
413 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || (edge_length == pow2_edge_length))
415 /* Precalculated scaling for 'faked' non power of two texture coords. */
416 texture->baseTexture.pow2Matrix[0] = 1.0f;
417 texture->baseTexture.pow2Matrix[5] = 1.0f;
418 texture->baseTexture.pow2Matrix[10] = 1.0f;
419 texture->baseTexture.pow2Matrix[15] = 1.0f;
421 else
423 /* Precalculated scaling for 'faked' non power of two texture coords. */
424 texture->baseTexture.pow2Matrix[0] = ((float)edge_length) / ((float)pow2_edge_length);
425 texture->baseTexture.pow2Matrix[5] = ((float)edge_length) / ((float)pow2_edge_length);
426 texture->baseTexture.pow2Matrix[10] = ((float)edge_length) / ((float)pow2_edge_length);
427 texture->baseTexture.pow2Matrix[15] = 1.0f;
428 texture->baseTexture.pow2Matrix_identity = FALSE;
430 texture->baseTexture.target = GL_TEXTURE_CUBE_MAP_ARB;
432 /* Generate all the surfaces. */
433 tmp_w = edge_length;
434 for (i = 0; i < texture->baseTexture.level_count; ++i)
436 /* Create the 6 faces. */
437 for (j = 0; j < 6; ++j)
439 static const GLenum cube_targets[6] =
441 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
442 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
443 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
444 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
445 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
446 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
448 UINT idx = j * texture->baseTexture.level_count + i;
449 IWineD3DSurface *surface;
451 hr = IWineD3DDeviceParent_CreateSurface(device->device_parent, parent, tmp_w, tmp_w,
452 format_id, usage, pool, i /* Level */, j, &surface);
453 if (FAILED(hr))
455 FIXME("(%p) Failed to create surface, hr %#x.\n", texture, hr);
456 basetexture_cleanup(texture);
457 return hr;
460 surface_set_container((IWineD3DSurfaceImpl *)surface, WINED3D_CONTAINER_TEXTURE, texture);
461 surface_set_texture_target((IWineD3DSurfaceImpl *)surface, cube_targets[j]);
462 texture->baseTexture.sub_resources[idx] = &((IWineD3DSurfaceImpl *)surface)->resource;
463 TRACE("Created surface level %u @ %p.\n", i, surface);
465 tmp_w = max(1, tmp_w >> 1);
468 return WINED3D_OK;