2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2009-2010 Henri Verbeet for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wined3d_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
27 /* Context activation is done by the caller. */
28 static HRESULT volumetexture_bind(IWineD3DBaseTextureImpl *texture,
29 const struct wined3d_gl_info *gl_info, BOOL srgb)
33 TRACE("texture %p, gl_info %p, srgb %#x.\n", texture, gl_info, srgb);
35 return basetexture_bind(texture, gl_info, srgb, &dummy);
38 /* Do not call while under the GL lock. */
39 static void volumetexture_preload(IWineD3DBaseTextureImpl *texture, enum WINED3DSRGB srgb)
41 IWineD3DDeviceImpl *device = texture->resource.device;
42 struct wined3d_context *context = NULL;
43 BOOL srgb_mode = texture->baseTexture.is_srgb;
44 BOOL srgb_was_toggled = FALSE;
47 TRACE("texture %p, srgb %#x.\n", texture, srgb);
49 if (!device->isInDraw) context = context_acquire(device, NULL);
50 else if (texture->baseTexture.bindCount > 0)
52 srgb_mode = device->stateBlock->state.sampler_states[texture->baseTexture.sampler][WINED3DSAMP_SRGBTEXTURE];
53 srgb_was_toggled = texture->baseTexture.is_srgb != srgb_mode;
54 texture->baseTexture.is_srgb = srgb_mode;
57 /* If the texture is marked dirty or the srgb sampler setting has changed
58 * since the last load then reload the volumes. */
59 if (texture->baseTexture.texture_rgb.dirty)
61 for (i = 0; i < texture->baseTexture.level_count; ++i)
63 volume_load(volume_from_resource(texture->baseTexture.sub_resources[i]), i, srgb_mode);
66 else if (srgb_was_toggled)
68 for (i = 0; i < texture->baseTexture.level_count; ++i)
70 IWineD3DVolumeImpl *volume = volume_from_resource(texture->baseTexture.sub_resources[i]);
71 volume_add_dirty_box(volume, NULL);
72 volume_load(volume, i, srgb_mode);
77 TRACE("Texture %p not dirty, nothing to do.\n", texture);
80 if (context) context_release(context);
83 texture->baseTexture.texture_rgb.dirty = FALSE;
86 /* Do not call while under the GL lock. */
87 static void volumetexture_unload(struct wined3d_resource *resource)
89 IWineD3DBaseTextureImpl *texture = basetexture_from_resource(resource);
92 TRACE("texture %p.\n", texture);
94 for (i = 0; i < texture->baseTexture.level_count; ++i)
96 struct wined3d_resource *sub_resource = texture->baseTexture.sub_resources[i];
97 sub_resource->resource_ops->resource_unload(sub_resource);
100 basetexture_unload(texture);
103 static const struct wined3d_texture_ops volumetexture_ops =
106 volumetexture_preload,
109 static const struct wined3d_resource_ops volumetexture_resource_ops =
111 volumetexture_unload,
114 static void volumetexture_cleanup(IWineD3DBaseTextureImpl *This)
118 TRACE("(%p) : Cleaning up.\n", This);
120 for (i = 0; i < This->baseTexture.level_count; ++i)
122 struct wined3d_resource *sub_resource = This->baseTexture.sub_resources[i];
126 IWineD3DVolumeImpl *volume = volume_from_resource(sub_resource);
128 /* Cleanup the container. */
129 volume_set_container(volume, NULL);
130 IWineD3DVolume_Release((IWineD3DVolume *)volume);
133 basetexture_cleanup((IWineD3DBaseTextureImpl *)This);
136 /* *******************************************
137 IWineD3DTexture IUnknown parts follow
138 ******************************************* */
140 static HRESULT WINAPI IWineD3DVolumeTextureImpl_QueryInterface(IWineD3DBaseTexture *iface, REFIID riid, LPVOID *ppobj)
142 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
143 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
144 if (IsEqualGUID(riid, &IID_IUnknown)
145 || IsEqualGUID(riid, &IID_IWineD3DBase)
146 || IsEqualGUID(riid, &IID_IWineD3DResource)
147 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture))
149 IUnknown_AddRef(iface);
154 return E_NOINTERFACE;
157 static ULONG WINAPI IWineD3DVolumeTextureImpl_AddRef(IWineD3DBaseTexture *iface)
159 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
160 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
161 return InterlockedIncrement(&This->resource.ref);
164 /* Do not call while under the GL lock. */
165 static ULONG WINAPI IWineD3DVolumeTextureImpl_Release(IWineD3DBaseTexture *iface)
167 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
169 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
170 ref = InterlockedDecrement(&This->resource.ref);
173 volumetexture_cleanup(This);
174 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
175 HeapFree(GetProcessHeap(), 0, This);
180 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetPrivateData(IWineD3DBaseTexture *iface,
181 REFGUID riid, const void *data, DWORD data_size, DWORD flags)
183 return resource_set_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, riid, data, data_size, flags);
186 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetPrivateData(IWineD3DBaseTexture *iface,
187 REFGUID guid, void *data, DWORD *data_size)
189 return resource_get_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, guid, data, data_size);
192 static HRESULT WINAPI IWineD3DVolumeTextureImpl_FreePrivateData(IWineD3DBaseTexture *iface, REFGUID refguid)
194 return resource_free_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, refguid);
197 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetPriority(IWineD3DBaseTexture *iface, DWORD priority)
199 return resource_set_priority(&((IWineD3DBaseTextureImpl *)iface)->resource, priority);
202 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetPriority(IWineD3DBaseTexture *iface)
204 return resource_get_priority(&((IWineD3DBaseTextureImpl *)iface)->resource);
207 static void WINAPI IWineD3DVolumeTextureImpl_PreLoad(IWineD3DBaseTexture *iface)
209 volumetexture_preload((IWineD3DBaseTextureImpl *)iface, SRGB_ANY);
212 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeTextureImpl_GetType(IWineD3DBaseTexture *iface)
214 return resource_get_type(&((IWineD3DBaseTextureImpl *)iface)->resource);
217 static void * WINAPI IWineD3DVolumeTextureImpl_GetParent(IWineD3DBaseTexture *iface)
219 TRACE("iface %p\n", iface);
221 return ((IWineD3DBaseTextureImpl *)iface)->resource.parent;
224 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetLOD(IWineD3DBaseTexture *iface, DWORD LODNew)
226 return basetexture_set_lod((IWineD3DBaseTextureImpl *)iface, LODNew);
229 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLOD(IWineD3DBaseTexture *iface)
231 return basetexture_get_lod((IWineD3DBaseTextureImpl *)iface);
234 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLevelCount(IWineD3DBaseTexture *iface)
236 return basetexture_get_level_count((IWineD3DBaseTextureImpl *)iface);
239 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetAutoGenFilterType(IWineD3DBaseTexture *iface,
240 WINED3DTEXTUREFILTERTYPE FilterType)
242 return basetexture_set_autogen_filter_type((IWineD3DBaseTextureImpl *)iface, FilterType);
245 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DVolumeTextureImpl_GetAutoGenFilterType(IWineD3DBaseTexture *iface)
247 return basetexture_get_autogen_filter_type((IWineD3DBaseTextureImpl *)iface);
250 static void WINAPI IWineD3DVolumeTextureImpl_GenerateMipSubLevels(IWineD3DBaseTexture *iface)
252 basetexture_generate_mipmaps((IWineD3DBaseTextureImpl *)iface);
255 static BOOL WINAPI IWineD3DVolumeTextureImpl_IsCondNP2(IWineD3DBaseTexture *iface)
257 TRACE("iface %p.\n", iface);
262 static struct wined3d_resource * WINAPI IWineD3DVolumeTextureImpl_GetSubResource(IWineD3DBaseTexture *iface,
263 UINT sub_resource_idx)
265 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
267 TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
269 return basetexture_get_sub_resource(texture, sub_resource_idx);
272 static HRESULT WINAPI IWineD3DVolumeTextureImpl_AddDirtyRegion(IWineD3DBaseTexture *iface,
273 UINT layer, const WINED3DBOX *dirty_region)
275 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
276 struct wined3d_resource *sub_resource;
278 TRACE("iface %p, layer %u, dirty_region %p.\n", iface, layer, dirty_region);
280 if (!(sub_resource = basetexture_get_sub_resource(texture, layer * texture->baseTexture.level_count)))
282 WARN("Failed to get sub-resource.\n");
283 return WINED3DERR_INVALIDCALL;
286 basetexture_set_dirty(texture, TRUE);
287 volume_add_dirty_box(volume_from_resource(sub_resource), dirty_region);
292 static const IWineD3DBaseTextureVtbl IWineD3DVolumeTexture_Vtbl =
295 IWineD3DVolumeTextureImpl_QueryInterface,
296 IWineD3DVolumeTextureImpl_AddRef,
297 IWineD3DVolumeTextureImpl_Release,
299 IWineD3DVolumeTextureImpl_GetParent,
300 IWineD3DVolumeTextureImpl_SetPrivateData,
301 IWineD3DVolumeTextureImpl_GetPrivateData,
302 IWineD3DVolumeTextureImpl_FreePrivateData,
303 IWineD3DVolumeTextureImpl_SetPriority,
304 IWineD3DVolumeTextureImpl_GetPriority,
305 IWineD3DVolumeTextureImpl_PreLoad,
306 IWineD3DVolumeTextureImpl_GetType,
308 IWineD3DVolumeTextureImpl_SetLOD,
309 IWineD3DVolumeTextureImpl_GetLOD,
310 IWineD3DVolumeTextureImpl_GetLevelCount,
311 IWineD3DVolumeTextureImpl_SetAutoGenFilterType,
312 IWineD3DVolumeTextureImpl_GetAutoGenFilterType,
313 IWineD3DVolumeTextureImpl_GenerateMipSubLevels,
314 IWineD3DVolumeTextureImpl_IsCondNP2,
315 IWineD3DVolumeTextureImpl_GetSubResource,
316 IWineD3DVolumeTextureImpl_AddDirtyRegion,
319 HRESULT volumetexture_init(IWineD3DBaseTextureImpl *texture, UINT width, UINT height,
320 UINT depth, UINT levels, IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id,
321 WINED3DPOOL pool, void *parent, const struct wined3d_parent_ops *parent_ops)
323 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
324 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
325 UINT tmp_w, tmp_h, tmp_d;
329 /* TODO: It should only be possible to create textures for formats
330 * that are reported as supported. */
331 if (WINED3DFMT_UNKNOWN >= format_id)
333 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
334 return WINED3DERR_INVALIDCALL;
337 if (!gl_info->supported[EXT_TEXTURE3D])
339 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
340 return WINED3DERR_INVALIDCALL;
343 /* Calculate levels for mip mapping. */
344 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
346 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
348 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
349 return WINED3DERR_INVALIDCALL;
354 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
355 return WINED3DERR_INVALIDCALL;
362 levels = wined3d_log2i(max(max(width, height), depth)) + 1;
363 TRACE("Calculated levels = %u.\n", levels);
366 texture->lpVtbl = &IWineD3DVolumeTexture_Vtbl;
368 hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, &volumetexture_ops,
369 1, levels, WINED3DRTYPE_VOLUMETEXTURE, device, usage, format, pool,
370 parent, parent_ops, &volumetexture_resource_ops);
373 WARN("Failed to initialize basetexture, returning %#x.\n", hr);
377 /* Is NP2 support for volumes needed? */
378 texture->baseTexture.pow2Matrix[0] = 1.0f;
379 texture->baseTexture.pow2Matrix[5] = 1.0f;
380 texture->baseTexture.pow2Matrix[10] = 1.0f;
381 texture->baseTexture.pow2Matrix[15] = 1.0f;
382 texture->baseTexture.target = GL_TEXTURE_3D;
384 /* Generate all the surfaces. */
389 for (i = 0; i < texture->baseTexture.level_count; ++i)
391 IWineD3DVolume *volume;
393 /* Create the volume. */
394 hr = IWineD3DDeviceParent_CreateVolume(device->device_parent, parent,
395 tmp_w, tmp_h, tmp_d, format_id, pool, usage, &volume);
398 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
399 volumetexture_cleanup(texture);
403 /* Set its container to this texture. */
404 volume_set_container((IWineD3DVolumeImpl *)volume, texture);
405 texture->baseTexture.sub_resources[i] = &((IWineD3DVolumeImpl *)volume)->resource;
407 /* Calculate the next mipmap level. */
408 tmp_w = max(1, tmp_w >> 1);
409 tmp_h = max(1, tmp_h >> 1);
410 tmp_d = max(1, tmp_d >> 1);