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
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
29 static void volumetexture_internal_preload(IWineD3DBaseTexture *iface, enum WINED3DSRGB srgb)
31 /* Override the IWineD3DResource Preload method. */
32 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
33 IWineD3DDeviceImpl *device = This->resource.device;
34 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
35 struct wined3d_context *context = NULL;
36 BOOL srgb_mode = This->baseTexture.is_srgb;
37 BOOL srgb_was_toggled = FALSE;
40 TRACE("(%p) : About to load texture.\n", This);
42 if (!device->isInDraw) context = context_acquire(device, NULL);
43 else if (gl_info->supported[EXT_TEXTURE_SRGB] && This->baseTexture.bindCount > 0)
45 srgb_mode = device->stateBlock->samplerState[This->baseTexture.sampler][WINED3DSAMP_SRGBTEXTURE];
46 srgb_was_toggled = This->baseTexture.is_srgb != srgb_mode;
47 This->baseTexture.is_srgb = srgb_mode;
50 /* If the texture is marked dirty or the srgb sampler setting has changed
51 * since the last load then reload the volumes. */
52 if (This->baseTexture.texture_rgb.dirty)
54 for (i = 0; i < This->baseTexture.level_count; ++i)
56 IWineD3DVolume *volume = (IWineD3DVolume *)This->baseTexture.sub_resources[i];
57 IWineD3DVolume_LoadTexture(volume, i, srgb_mode);
60 else if (srgb_was_toggled)
62 for (i = 0; i < This->baseTexture.level_count; ++i)
64 IWineD3DVolume *volume = (IWineD3DVolume *)This->baseTexture.sub_resources[i];
65 volume_add_dirty_box(volume, NULL);
66 IWineD3DVolume_LoadTexture(volume, i, srgb_mode);
71 TRACE("(%p) Texture not dirty, nothing to do.\n", iface);
74 if (context) context_release(context);
77 This->baseTexture.texture_rgb.dirty = FALSE;
80 static void volumetexture_cleanup(IWineD3DVolumeTextureImpl *This)
84 TRACE("(%p) : Cleaning up.\n", This);
86 for (i = 0; i < This->baseTexture.level_count; ++i)
88 IWineD3DVolume *volume = (IWineD3DVolume *)This->baseTexture.sub_resources[i];
92 /* Cleanup the container. */
93 IWineD3DVolume_SetContainer(volume, NULL);
94 IWineD3DVolume_Release(volume);
97 basetexture_cleanup((IWineD3DBaseTexture *)This);
100 /* *******************************************
101 IWineD3DTexture IUnknown parts follow
102 ******************************************* */
104 static HRESULT WINAPI IWineD3DVolumeTextureImpl_QueryInterface(IWineD3DVolumeTexture *iface, REFIID riid, LPVOID *ppobj)
106 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
107 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
108 if (IsEqualGUID(riid, &IID_IUnknown)
109 || IsEqualGUID(riid, &IID_IWineD3DBase)
110 || IsEqualGUID(riid, &IID_IWineD3DResource)
111 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
112 || IsEqualGUID(riid, &IID_IWineD3DVolumeTexture)) {
113 IUnknown_AddRef(iface);
118 return E_NOINTERFACE;
121 static ULONG WINAPI IWineD3DVolumeTextureImpl_AddRef(IWineD3DVolumeTexture *iface) {
122 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
123 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
124 return InterlockedIncrement(&This->resource.ref);
127 static ULONG WINAPI IWineD3DVolumeTextureImpl_Release(IWineD3DVolumeTexture *iface) {
128 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
130 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
131 ref = InterlockedDecrement(&This->resource.ref);
134 volumetexture_cleanup(This);
135 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
136 HeapFree(GetProcessHeap(), 0, This);
141 /* ****************************************************
142 IWineD3DVolumeTexture IWineD3DResource parts follow
143 **************************************************** */
144 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetPrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
145 return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
148 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetPrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
149 return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
152 static HRESULT WINAPI IWineD3DVolumeTextureImpl_FreePrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid) {
153 return resource_free_private_data((IWineD3DResource *)iface, refguid);
156 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetPriority(IWineD3DVolumeTexture *iface, DWORD PriorityNew) {
157 return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
160 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetPriority(IWineD3DVolumeTexture *iface) {
161 return resource_get_priority((IWineD3DResource *)iface);
164 static void WINAPI IWineD3DVolumeTextureImpl_PreLoad(IWineD3DVolumeTexture *iface) {
165 volumetexture_internal_preload((IWineD3DBaseTexture *) iface, SRGB_ANY);
168 static void WINAPI IWineD3DVolumeTextureImpl_UnLoad(IWineD3DVolumeTexture *iface) {
170 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
171 TRACE("(%p)\n", This);
173 /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
174 * surface before, this one will be a NOP and vice versa. Unloading an unloaded
177 for (i = 0; i < This->baseTexture.level_count; ++i)
179 IWineD3DVolume_UnLoad((IWineD3DVolume *)This->baseTexture.sub_resources[i]);
182 basetexture_unload((IWineD3DBaseTexture *)iface);
185 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeTextureImpl_GetType(IWineD3DVolumeTexture *iface) {
186 return resource_get_type((IWineD3DResource *)iface);
189 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetParent(IWineD3DVolumeTexture *iface, IUnknown **pParent) {
190 return resource_get_parent((IWineD3DResource *)iface, pParent);
193 /* ******************************************************
194 IWineD3DVolumeTexture IWineD3DBaseTexture parts follow
195 ****************************************************** */
196 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetLOD(IWineD3DVolumeTexture *iface, DWORD LODNew) {
197 return basetexture_set_lod((IWineD3DBaseTexture *)iface, LODNew);
200 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLOD(IWineD3DVolumeTexture *iface) {
201 return basetexture_get_lod((IWineD3DBaseTexture *)iface);
204 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLevelCount(IWineD3DVolumeTexture *iface) {
205 return basetexture_get_level_count((IWineD3DBaseTexture *)iface);
208 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetAutoGenFilterType(IWineD3DVolumeTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
209 return basetexture_set_autogen_filter_type((IWineD3DBaseTexture *)iface, FilterType);
212 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DVolumeTextureImpl_GetAutoGenFilterType(IWineD3DVolumeTexture *iface) {
213 return basetexture_get_autogen_filter_type((IWineD3DBaseTexture *)iface);
216 static void WINAPI IWineD3DVolumeTextureImpl_GenerateMipSubLevels(IWineD3DVolumeTexture *iface) {
217 basetexture_generate_mipmaps((IWineD3DBaseTexture *)iface);
220 /* Internal function, No d3d mapping */
221 static BOOL WINAPI IWineD3DVolumeTextureImpl_SetDirty(IWineD3DVolumeTexture *iface, BOOL dirty) {
222 return basetexture_set_dirty((IWineD3DBaseTexture *)iface, dirty);
225 static BOOL WINAPI IWineD3DVolumeTextureImpl_GetDirty(IWineD3DVolumeTexture *iface) {
226 return basetexture_get_dirty((IWineD3DBaseTexture *)iface);
229 /* Context activation is done by the caller. */
230 static HRESULT WINAPI IWineD3DVolumeTextureImpl_BindTexture(IWineD3DVolumeTexture *iface, BOOL srgb)
234 TRACE("iface %p, srgb %#x.\n", iface, srgb);
236 return basetexture_bind((IWineD3DBaseTexture *)iface, srgb, &dummy);
239 static UINT WINAPI IWineD3DVolumeTextureImpl_GetTextureDimensions(IWineD3DVolumeTexture *iface)
241 TRACE("iface %p.\n", iface);
243 return GL_TEXTURE_3D;
246 static BOOL WINAPI IWineD3DVolumeTextureImpl_IsCondNP2(IWineD3DVolumeTexture *iface)
248 TRACE("iface %p.\n", iface);
253 /* *******************************************
254 IWineD3DVolumeTexture IWineD3DVolumeTexture parts follow
255 ******************************************* */
256 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetLevelDesc(IWineD3DVolumeTexture *iface,
257 UINT level, WINED3DVOLUME_DESC *desc)
259 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
260 IWineD3DVolume *volume;
262 TRACE("iface %p, level %u, desc %p.\n", iface, level, desc);
264 if (!(volume = (IWineD3DVolume *)basetexture_get_sub_resource(texture, 0, level)))
266 WARN("Failed to get sub-resource.\n");
267 return WINED3DERR_INVALIDCALL;
270 return IWineD3DVolume_GetDesc(volume, desc);
273 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetVolumeLevel(IWineD3DVolumeTexture *iface,
274 UINT level, IWineD3DVolume **volume)
276 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
279 TRACE("iface %p, level %u, volume %p.\n", iface, level, volume);
281 if (!(v = (IWineD3DVolume *)basetexture_get_sub_resource(texture, 0, level)))
283 WARN("Failed to get sub-resource.\n");
284 return WINED3DERR_INVALIDCALL;
287 IWineD3DVolume_AddRef(v);
290 TRACE("Returning volume %p.\n", *volume);
295 static HRESULT WINAPI IWineD3DVolumeTextureImpl_LockBox(IWineD3DVolumeTexture *iface,
296 UINT level, WINED3DLOCKED_BOX *locked_box, const WINED3DBOX *box, DWORD flags)
298 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
299 IWineD3DVolume *volume;
301 TRACE("iface %p, level %u, locked_box %p, box %p, flags %#x.\n",
302 iface, level, locked_box, box, flags);
304 if (!(volume = (IWineD3DVolume *)basetexture_get_sub_resource(texture, 0, level)))
306 WARN("Failed to get sub-resource.\n");
307 return WINED3DERR_INVALIDCALL;
310 return IWineD3DVolume_LockBox(volume, locked_box, box, flags);
313 static HRESULT WINAPI IWineD3DVolumeTextureImpl_UnlockBox(IWineD3DVolumeTexture *iface, UINT level)
315 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
316 IWineD3DVolume *volume;
318 TRACE("iface %p, level %u.\n", iface, level);
320 if (!(volume = (IWineD3DVolume *)basetexture_get_sub_resource(texture, 0, level)))
322 WARN("Failed to get sub-resource.\n");
323 return WINED3DERR_INVALIDCALL;
326 return IWineD3DVolume_UnlockBox(volume);
329 static HRESULT WINAPI IWineD3DVolumeTextureImpl_AddDirtyBox(IWineD3DVolumeTexture *iface, const WINED3DBOX *dirty_box)
331 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
332 IWineD3DVolume *volume;
334 TRACE("iface %p, dirty_box %p.\n", iface, dirty_box);
336 if (!(volume = (IWineD3DVolume *)basetexture_get_sub_resource(texture, 0, 0)))
338 WARN("Failed to get sub-resource.\n");
339 return WINED3DERR_INVALIDCALL;
342 texture->baseTexture.texture_rgb.dirty = TRUE;
343 texture->baseTexture.texture_srgb.dirty = TRUE;
344 volume_add_dirty_box(volume, dirty_box);
349 static const IWineD3DVolumeTextureVtbl IWineD3DVolumeTexture_Vtbl =
352 IWineD3DVolumeTextureImpl_QueryInterface,
353 IWineD3DVolumeTextureImpl_AddRef,
354 IWineD3DVolumeTextureImpl_Release,
356 IWineD3DVolumeTextureImpl_GetParent,
357 IWineD3DVolumeTextureImpl_SetPrivateData,
358 IWineD3DVolumeTextureImpl_GetPrivateData,
359 IWineD3DVolumeTextureImpl_FreePrivateData,
360 IWineD3DVolumeTextureImpl_SetPriority,
361 IWineD3DVolumeTextureImpl_GetPriority,
362 IWineD3DVolumeTextureImpl_PreLoad,
363 IWineD3DVolumeTextureImpl_UnLoad,
364 IWineD3DVolumeTextureImpl_GetType,
366 IWineD3DVolumeTextureImpl_SetLOD,
367 IWineD3DVolumeTextureImpl_GetLOD,
368 IWineD3DVolumeTextureImpl_GetLevelCount,
369 IWineD3DVolumeTextureImpl_SetAutoGenFilterType,
370 IWineD3DVolumeTextureImpl_GetAutoGenFilterType,
371 IWineD3DVolumeTextureImpl_GenerateMipSubLevels,
372 IWineD3DVolumeTextureImpl_SetDirty,
373 IWineD3DVolumeTextureImpl_GetDirty,
375 IWineD3DVolumeTextureImpl_BindTexture,
376 IWineD3DVolumeTextureImpl_GetTextureDimensions,
377 IWineD3DVolumeTextureImpl_IsCondNP2,
379 IWineD3DVolumeTextureImpl_GetLevelDesc,
380 IWineD3DVolumeTextureImpl_GetVolumeLevel,
381 IWineD3DVolumeTextureImpl_LockBox,
382 IWineD3DVolumeTextureImpl_UnlockBox,
383 IWineD3DVolumeTextureImpl_AddDirtyBox
386 HRESULT volumetexture_init(IWineD3DVolumeTextureImpl *texture, UINT width, UINT height,
387 UINT depth, UINT levels, IWineD3DDeviceImpl *device, DWORD usage, WINED3DFORMAT format,
388 WINED3DPOOL pool, IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
390 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
391 const struct wined3d_format_desc *format_desc = getFormatDescEntry(format, gl_info);
392 UINT tmp_w, tmp_h, tmp_d;
396 /* TODO: It should only be possible to create textures for formats
397 * that are reported as supported. */
398 if (WINED3DFMT_UNKNOWN >= format)
400 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
401 return WINED3DERR_INVALIDCALL;
404 if (!gl_info->supported[EXT_TEXTURE3D])
406 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
407 return WINED3DERR_INVALIDCALL;
410 /* Calculate levels for mip mapping. */
411 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
413 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
415 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
416 return WINED3DERR_INVALIDCALL;
421 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
422 return WINED3DERR_INVALIDCALL;
429 levels = wined3d_log2i(max(max(width, height), depth)) + 1;
430 TRACE("Calculated levels = %u.\n", levels);
433 texture->lpVtbl = &IWineD3DVolumeTexture_Vtbl;
435 hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, 1, levels,
436 WINED3DRTYPE_VOLUMETEXTURE, device, 0, usage, format_desc, pool, parent, parent_ops);
439 WARN("Failed to initialize basetexture, returning %#x.\n", hr);
443 /* Is NP2 support for volumes needed? */
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 /* Generate all the surfaces. */
454 for (i = 0; i < texture->baseTexture.level_count; ++i)
456 IWineD3DVolume *volume;
458 /* Create the volume. */
459 hr = IWineD3DDeviceParent_CreateVolume(device->device_parent, parent,
460 tmp_w, tmp_h, tmp_d, format, pool, usage, &volume);
463 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
464 volumetexture_cleanup(texture);
468 /* Set its container to this texture. */
469 IWineD3DVolume_SetContainer(volume, (IWineD3DBase *)texture);
470 texture->baseTexture.sub_resources[i] = (IWineD3DResourceImpl *)volume;
472 /* Calculate the next mipmap level. */
473 tmp_w = max(1, tmp_w >> 1);
474 tmp_h = max(1, tmp_h >> 1);
475 tmp_d = max(1, tmp_d >> 1);
477 texture->baseTexture.internal_preload = volumetexture_internal_preload;