wined3d: Don't free D3D volume textures until the wined3d volume texture is destroyed.
[wine.git] / dlls / wined3d / volumetexture.c
blob252de28acb0ed7f280b086d29846f6e49f07a138
1 /*
2 * IWineD3DVolumeTexture implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2009 Henri Verbeet for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
29 #define GLINFO_LOCATION (*gl_info)
31 static void volumetexture_internal_preload(IWineD3DBaseTexture *iface, enum WINED3DSRGB srgb)
33 /* Override the IWineD3DResource Preload method. */
34 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
35 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
36 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
37 BOOL srgb_mode = This->baseTexture.is_srgb;
38 BOOL srgb_was_toggled = FALSE;
39 unsigned int i;
41 TRACE("(%p) : About to load texture.\n", This);
43 if (!device->isInDraw)
45 ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
47 else if (GL_SUPPORT(EXT_TEXTURE_SRGB) && This->baseTexture.bindCount > 0)
49 srgb_mode = device->stateBlock->samplerState[This->baseTexture.sampler][WINED3DSAMP_SRGBTEXTURE];
50 srgb_was_toggled = This->baseTexture.is_srgb != srgb_mode;
51 This->baseTexture.is_srgb = srgb_mode;
54 /* If the texture is marked dirty or the srgb sampler setting has changed
55 * since the last load then reload the volumes. */
56 if (This->baseTexture.dirty)
58 for (i = 0; i < This->baseTexture.levels; ++i)
60 IWineD3DVolume_LoadTexture(This->volumes[i], i, srgb_mode);
63 else if (srgb_was_toggled)
65 for (i = 0; i < This->baseTexture.levels; ++i)
67 volume_add_dirty_box(This->volumes[i], NULL);
68 IWineD3DVolume_LoadTexture(This->volumes[i], i, srgb_mode);
71 else
73 TRACE("(%p) Texture not dirty, nothing to do.\n", iface);
76 /* No longer dirty */
77 This->baseTexture.dirty = FALSE;
80 static void volumetexture_cleanup(IWineD3DVolumeTextureImpl *This)
82 unsigned int i;
84 TRACE("(%p) : Cleaning up.\n", This);
86 for (i = 0; i < This->baseTexture.levels; ++i)
88 IWineD3DVolume *volume = This->volumes[i];
90 if (volume)
92 /* Cleanup the container. */
93 IWineD3DVolume_SetContainer(volume, NULL);
94 IWineD3DVolume_Release(volume);
97 basetexture_cleanup((IWineD3DBaseTexture *)This);
100 #undef GLINFO_LOCATION
102 /* *******************************************
103 IWineD3DTexture IUnknown parts follow
104 ******************************************* */
106 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
108 static HRESULT WINAPI IWineD3DVolumeTextureImpl_QueryInterface(IWineD3DVolumeTexture *iface, REFIID riid, LPVOID *ppobj)
110 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
111 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
112 if (IsEqualGUID(riid, &IID_IUnknown)
113 || IsEqualGUID(riid, &IID_IWineD3DBase)
114 || IsEqualGUID(riid, &IID_IWineD3DResource)
115 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
116 || IsEqualGUID(riid, &IID_IWineD3DVolumeTexture)) {
117 IUnknown_AddRef(iface);
118 *ppobj = This;
119 return S_OK;
121 *ppobj = NULL;
122 return E_NOINTERFACE;
125 static ULONG WINAPI IWineD3DVolumeTextureImpl_AddRef(IWineD3DVolumeTexture *iface) {
126 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
127 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
128 return InterlockedIncrement(&This->resource.ref);
131 static ULONG WINAPI IWineD3DVolumeTextureImpl_Release(IWineD3DVolumeTexture *iface) {
132 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
133 ULONG ref;
134 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
135 ref = InterlockedDecrement(&This->resource.ref);
136 if (!ref)
138 volumetexture_cleanup(This);
139 This->parent_ops->wined3d_object_destroyed(This->resource.parent);
140 HeapFree(GetProcessHeap(), 0, This);
142 return ref;
145 /* ****************************************************
146 IWineD3DVolumeTexture IWineD3DResource parts follow
147 **************************************************** */
148 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetDevice(IWineD3DVolumeTexture *iface, IWineD3DDevice** ppDevice) {
149 return resource_get_device((IWineD3DResource *)iface, ppDevice);
152 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetPrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
153 return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
156 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetPrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
157 return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
160 static HRESULT WINAPI IWineD3DVolumeTextureImpl_FreePrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid) {
161 return resource_free_private_data((IWineD3DResource *)iface, refguid);
164 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetPriority(IWineD3DVolumeTexture *iface, DWORD PriorityNew) {
165 return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
168 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetPriority(IWineD3DVolumeTexture *iface) {
169 return resource_get_priority((IWineD3DResource *)iface);
172 static void WINAPI IWineD3DVolumeTextureImpl_PreLoad(IWineD3DVolumeTexture *iface) {
173 volumetexture_internal_preload((IWineD3DBaseTexture *) iface, SRGB_ANY);
176 static void WINAPI IWineD3DVolumeTextureImpl_UnLoad(IWineD3DVolumeTexture *iface) {
177 unsigned int i;
178 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
179 TRACE("(%p)\n", This);
181 /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
182 * surface before, this one will be a NOP and vice versa. Unloading an unloaded
183 * surface is fine
185 for (i = 0; i < This->baseTexture.levels; i++) {
186 IWineD3DVolume_UnLoad(This->volumes[i]);
189 basetexture_unload((IWineD3DBaseTexture *)iface);
192 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeTextureImpl_GetType(IWineD3DVolumeTexture *iface) {
193 return resource_get_type((IWineD3DResource *)iface);
196 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetParent(IWineD3DVolumeTexture *iface, IUnknown **pParent) {
197 return resource_get_parent((IWineD3DResource *)iface, pParent);
200 /* ******************************************************
201 IWineD3DVolumeTexture IWineD3DBaseTexture parts follow
202 ****************************************************** */
203 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetLOD(IWineD3DVolumeTexture *iface, DWORD LODNew) {
204 return basetexture_set_lod((IWineD3DBaseTexture *)iface, LODNew);
207 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLOD(IWineD3DVolumeTexture *iface) {
208 return basetexture_get_lod((IWineD3DBaseTexture *)iface);
211 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLevelCount(IWineD3DVolumeTexture *iface) {
212 return basetexture_get_level_count((IWineD3DBaseTexture *)iface);
215 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetAutoGenFilterType(IWineD3DVolumeTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
216 return basetexture_set_autogen_filter_type((IWineD3DBaseTexture *)iface, FilterType);
219 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DVolumeTextureImpl_GetAutoGenFilterType(IWineD3DVolumeTexture *iface) {
220 return basetexture_get_autogen_filter_type((IWineD3DBaseTexture *)iface);
223 static void WINAPI IWineD3DVolumeTextureImpl_GenerateMipSubLevels(IWineD3DVolumeTexture *iface) {
224 basetexture_generate_mipmaps((IWineD3DBaseTexture *)iface);
227 /* Internal function, No d3d mapping */
228 static BOOL WINAPI IWineD3DVolumeTextureImpl_SetDirty(IWineD3DVolumeTexture *iface, BOOL dirty) {
229 return basetexture_set_dirty((IWineD3DBaseTexture *)iface, dirty);
232 static BOOL WINAPI IWineD3DVolumeTextureImpl_GetDirty(IWineD3DVolumeTexture *iface) {
233 return basetexture_get_dirty((IWineD3DBaseTexture *)iface);
236 /* Context activation is done by the caller. */
237 static HRESULT WINAPI IWineD3DVolumeTextureImpl_BindTexture(IWineD3DVolumeTexture *iface, BOOL srgb) {
238 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
239 BOOL dummy;
240 TRACE("(%p) : relay to BaseTexture\n", This);
241 return basetexture_bind((IWineD3DBaseTexture *)iface, srgb, &dummy);
244 static UINT WINAPI IWineD3DVolumeTextureImpl_GetTextureDimensions(IWineD3DVolumeTexture *iface) {
245 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
246 TRACE("(%p)\n", This);
247 return GL_TEXTURE_3D;
250 static BOOL WINAPI IWineD3DVolumeTextureImpl_IsCondNP2(IWineD3DVolumeTexture *iface) {
251 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
252 TRACE("(%p)\n", This);
254 return FALSE;
257 /* *******************************************
258 IWineD3DVolumeTexture IWineD3DVolumeTexture parts follow
259 ******************************************* */
260 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetLevelDesc(IWineD3DVolumeTexture *iface, UINT Level,WINED3DVOLUME_DESC *pDesc) {
261 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
262 if (Level < This->baseTexture.levels) {
263 TRACE("(%p) Level (%d)\n", This, Level);
264 return IWineD3DVolume_GetDesc(This->volumes[Level], pDesc);
265 } else {
266 WARN("(%p) Level (%d)\n", This, Level);
268 return WINED3D_OK;
270 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetVolumeLevel(IWineD3DVolumeTexture *iface, UINT Level, IWineD3DVolume** ppVolumeLevel) {
271 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
272 if (Level < This->baseTexture.levels) {
273 *ppVolumeLevel = This->volumes[Level];
274 IWineD3DVolume_AddRef(*ppVolumeLevel);
275 TRACE("(%p) -> level(%d) returning volume@%p\n", This, Level, *ppVolumeLevel);
276 } else {
277 WARN("(%p) Level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
278 return WINED3DERR_INVALIDCALL;
280 return WINED3D_OK;
283 static HRESULT WINAPI IWineD3DVolumeTextureImpl_LockBox(IWineD3DVolumeTexture *iface, UINT Level, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
284 HRESULT hr;
285 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
287 if (Level < This->baseTexture.levels) {
288 hr = IWineD3DVolume_LockBox(This->volumes[Level], pLockedVolume, pBox, Flags);
289 TRACE("(%p) Level (%d) success(%u)\n", This, Level, hr);
291 } else {
292 FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
293 return WINED3DERR_INVALIDCALL;
295 return hr;
298 static HRESULT WINAPI IWineD3DVolumeTextureImpl_UnlockBox(IWineD3DVolumeTexture *iface, UINT Level) {
299 HRESULT hr;
300 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
302 if (Level < This->baseTexture.levels) {
303 hr = IWineD3DVolume_UnlockBox(This->volumes[Level]);
304 TRACE("(%p) -> level(%d) success(%u)\n", This, Level, hr);
306 } else {
307 FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
308 return WINED3DERR_INVALIDCALL;
310 return hr;
313 static HRESULT WINAPI IWineD3DVolumeTextureImpl_AddDirtyBox(IWineD3DVolumeTexture *iface, CONST WINED3DBOX* pDirtyBox) {
314 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
315 This->baseTexture.dirty = TRUE;
316 TRACE("(%p) : dirtyfication of volume Level (0)\n", This);
317 volume_add_dirty_box(This->volumes[0], pDirtyBox);
319 return WINED3D_OK;
322 static const IWineD3DVolumeTextureVtbl IWineD3DVolumeTexture_Vtbl =
324 /* IUnknown */
325 IWineD3DVolumeTextureImpl_QueryInterface,
326 IWineD3DVolumeTextureImpl_AddRef,
327 IWineD3DVolumeTextureImpl_Release,
328 /* resource */
329 IWineD3DVolumeTextureImpl_GetParent,
330 IWineD3DVolumeTextureImpl_GetDevice,
331 IWineD3DVolumeTextureImpl_SetPrivateData,
332 IWineD3DVolumeTextureImpl_GetPrivateData,
333 IWineD3DVolumeTextureImpl_FreePrivateData,
334 IWineD3DVolumeTextureImpl_SetPriority,
335 IWineD3DVolumeTextureImpl_GetPriority,
336 IWineD3DVolumeTextureImpl_PreLoad,
337 IWineD3DVolumeTextureImpl_UnLoad,
338 IWineD3DVolumeTextureImpl_GetType,
339 /* BaseTexture */
340 IWineD3DVolumeTextureImpl_SetLOD,
341 IWineD3DVolumeTextureImpl_GetLOD,
342 IWineD3DVolumeTextureImpl_GetLevelCount,
343 IWineD3DVolumeTextureImpl_SetAutoGenFilterType,
344 IWineD3DVolumeTextureImpl_GetAutoGenFilterType,
345 IWineD3DVolumeTextureImpl_GenerateMipSubLevels,
346 IWineD3DVolumeTextureImpl_SetDirty,
347 IWineD3DVolumeTextureImpl_GetDirty,
348 /* not in d3d */
349 IWineD3DVolumeTextureImpl_BindTexture,
350 IWineD3DVolumeTextureImpl_GetTextureDimensions,
351 IWineD3DVolumeTextureImpl_IsCondNP2,
352 /* volume texture */
353 IWineD3DVolumeTextureImpl_GetLevelDesc,
354 IWineD3DVolumeTextureImpl_GetVolumeLevel,
355 IWineD3DVolumeTextureImpl_LockBox,
356 IWineD3DVolumeTextureImpl_UnlockBox,
357 IWineD3DVolumeTextureImpl_AddDirtyBox
360 HRESULT volumetexture_init(IWineD3DVolumeTextureImpl *texture, UINT width, UINT height,
361 UINT depth, UINT levels, IWineD3DDeviceImpl *device, DWORD usage, WINED3DFORMAT format,
362 WINED3DPOOL pool, IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
364 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
365 const struct GlPixelFormatDesc *format_desc = getFormatDescEntry(format, gl_info);
366 UINT tmp_w, tmp_h, tmp_d;
367 unsigned int i;
368 HRESULT hr;
370 /* TODO: It should only be possible to create textures for formats
371 * that are reported as supported. */
372 if (WINED3DFMT_UNKNOWN >= format)
374 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
375 return WINED3DERR_INVALIDCALL;
378 if (!gl_info->supported[EXT_TEXTURE3D])
380 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
381 return WINED3DERR_INVALIDCALL;
384 /* Calculate levels for mip mapping. */
385 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
387 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
389 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
390 return WINED3DERR_INVALIDCALL;
393 if (levels > 1)
395 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
396 return WINED3DERR_INVALIDCALL;
399 levels = 1;
401 else if (!levels)
403 levels = wined3d_log2i(max(max(width, height), depth)) + 1;
404 TRACE("Calculated levels = %u.\n", levels);
407 texture->lpVtbl = &IWineD3DVolumeTexture_Vtbl;
409 hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, levels,
410 WINED3DRTYPE_VOLUMETEXTURE, device, 0, usage, format_desc, pool, parent);
411 if (FAILED(hr))
413 WARN("Failed to initialize basetexture, returning %#x.\n", hr);
414 return hr;
417 texture->parent_ops = parent_ops;
419 /* Is NP2 support for volumes needed? */
420 texture->baseTexture.pow2Matrix[0] = 1.0f;
421 texture->baseTexture.pow2Matrix[5] = 1.0f;
422 texture->baseTexture.pow2Matrix[10] = 1.0f;
423 texture->baseTexture.pow2Matrix[15] = 1.0f;
425 /* Generate all the surfaces. */
426 tmp_w = width;
427 tmp_h = height;
428 tmp_d = depth;
430 for (i = 0; i < texture->baseTexture.levels; ++i)
432 /* Create the volume. */
433 hr = IWineD3DDeviceParent_CreateVolume(device->device_parent, parent,
434 tmp_w, tmp_h, tmp_d, format, pool, usage, &texture->volumes[i]);
435 if (FAILED(hr))
437 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
438 texture->volumes[i] = NULL;
439 volumetexture_cleanup(texture);
440 return hr;
443 /* Set its container to this texture. */
444 IWineD3DVolume_SetContainer(texture->volumes[i], (IWineD3DBase *)texture);
446 /* Calculate the next mipmap level. */
447 tmp_w = max(1, tmp_w >> 1);
448 tmp_h = max(1, tmp_h >> 1);
449 tmp_d = max(1, tmp_d >> 1);
451 texture->baseTexture.internal_preload = volumetexture_internal_preload;
453 return WINED3D_OK;