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 static void volumetexture_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
87 const WINED3DBOX *dirty_region)
89 volume_add_dirty_box(volume_from_resource(sub_resource), dirty_region);
92 static void volumetexture_sub_resource_cleanup(struct wined3d_resource *sub_resource)
94 IWineD3DVolumeImpl *volume = volume_from_resource(sub_resource);
96 /* Cleanup the container. */
97 volume_set_container(volume, NULL);
98 IWineD3DVolume_Release((IWineD3DVolume *)volume);
101 /* Do not call while under the GL lock. */
102 static void volumetexture_unload(struct wined3d_resource *resource)
104 IWineD3DBaseTextureImpl *texture = basetexture_from_resource(resource);
107 TRACE("texture %p.\n", texture);
109 for (i = 0; i < texture->baseTexture.level_count; ++i)
111 struct wined3d_resource *sub_resource = texture->baseTexture.sub_resources[i];
112 sub_resource->resource_ops->resource_unload(sub_resource);
115 basetexture_unload(texture);
118 static const struct wined3d_texture_ops volumetexture_ops =
121 volumetexture_preload,
122 volumetexture_sub_resource_add_dirty_region,
123 volumetexture_sub_resource_cleanup,
126 static const struct wined3d_resource_ops volumetexture_resource_ops =
128 volumetexture_unload,
131 static HRESULT WINAPI IWineD3DVolumeTextureImpl_QueryInterface(IWineD3DBaseTexture *iface, REFIID riid, LPVOID *ppobj)
133 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
134 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
135 if (IsEqualGUID(riid, &IID_IUnknown)
136 || IsEqualGUID(riid, &IID_IWineD3DBase)
137 || IsEqualGUID(riid, &IID_IWineD3DResource)
138 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture))
140 IUnknown_AddRef(iface);
145 return E_NOINTERFACE;
148 static ULONG WINAPI IWineD3DVolumeTextureImpl_AddRef(IWineD3DBaseTexture *iface)
150 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
151 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
152 return InterlockedIncrement(&This->resource.ref);
155 /* Do not call while under the GL lock. */
156 static ULONG WINAPI IWineD3DVolumeTextureImpl_Release(IWineD3DBaseTexture *iface)
158 IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
160 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
161 ref = InterlockedDecrement(&This->resource.ref);
164 basetexture_cleanup(This);
165 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
166 HeapFree(GetProcessHeap(), 0, This);
171 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetPrivateData(IWineD3DBaseTexture *iface,
172 REFGUID riid, const void *data, DWORD data_size, DWORD flags)
174 return resource_set_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, riid, data, data_size, flags);
177 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetPrivateData(IWineD3DBaseTexture *iface,
178 REFGUID guid, void *data, DWORD *data_size)
180 return resource_get_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, guid, data, data_size);
183 static HRESULT WINAPI IWineD3DVolumeTextureImpl_FreePrivateData(IWineD3DBaseTexture *iface, REFGUID refguid)
185 return resource_free_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, refguid);
188 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetPriority(IWineD3DBaseTexture *iface, DWORD priority)
190 return resource_set_priority(&((IWineD3DBaseTextureImpl *)iface)->resource, priority);
193 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetPriority(IWineD3DBaseTexture *iface)
195 return resource_get_priority(&((IWineD3DBaseTextureImpl *)iface)->resource);
198 static void WINAPI IWineD3DVolumeTextureImpl_PreLoad(IWineD3DBaseTexture *iface)
200 volumetexture_preload((IWineD3DBaseTextureImpl *)iface, SRGB_ANY);
203 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeTextureImpl_GetType(IWineD3DBaseTexture *iface)
205 return resource_get_type(&((IWineD3DBaseTextureImpl *)iface)->resource);
208 static void * WINAPI IWineD3DVolumeTextureImpl_GetParent(IWineD3DBaseTexture *iface)
210 TRACE("iface %p\n", iface);
212 return ((IWineD3DBaseTextureImpl *)iface)->resource.parent;
215 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetLOD(IWineD3DBaseTexture *iface, DWORD LODNew)
217 return basetexture_set_lod((IWineD3DBaseTextureImpl *)iface, LODNew);
220 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLOD(IWineD3DBaseTexture *iface)
222 return basetexture_get_lod((IWineD3DBaseTextureImpl *)iface);
225 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLevelCount(IWineD3DBaseTexture *iface)
227 return basetexture_get_level_count((IWineD3DBaseTextureImpl *)iface);
230 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetAutoGenFilterType(IWineD3DBaseTexture *iface,
231 WINED3DTEXTUREFILTERTYPE FilterType)
233 return basetexture_set_autogen_filter_type((IWineD3DBaseTextureImpl *)iface, FilterType);
236 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DVolumeTextureImpl_GetAutoGenFilterType(IWineD3DBaseTexture *iface)
238 return basetexture_get_autogen_filter_type((IWineD3DBaseTextureImpl *)iface);
241 static void WINAPI IWineD3DVolumeTextureImpl_GenerateMipSubLevels(IWineD3DBaseTexture *iface)
243 basetexture_generate_mipmaps((IWineD3DBaseTextureImpl *)iface);
246 static struct wined3d_resource * WINAPI IWineD3DVolumeTextureImpl_GetSubResource(IWineD3DBaseTexture *iface,
247 UINT sub_resource_idx)
249 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
251 TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
253 return basetexture_get_sub_resource(texture, sub_resource_idx);
256 static HRESULT WINAPI IWineD3DVolumeTextureImpl_AddDirtyRegion(IWineD3DBaseTexture *iface,
257 UINT layer, const WINED3DBOX *dirty_region)
259 return basetexture_add_dirty_region((IWineD3DBaseTextureImpl *)iface, layer, dirty_region);
262 static const IWineD3DBaseTextureVtbl IWineD3DVolumeTexture_Vtbl =
265 IWineD3DVolumeTextureImpl_QueryInterface,
266 IWineD3DVolumeTextureImpl_AddRef,
267 IWineD3DVolumeTextureImpl_Release,
269 IWineD3DVolumeTextureImpl_GetParent,
270 IWineD3DVolumeTextureImpl_SetPrivateData,
271 IWineD3DVolumeTextureImpl_GetPrivateData,
272 IWineD3DVolumeTextureImpl_FreePrivateData,
273 IWineD3DVolumeTextureImpl_SetPriority,
274 IWineD3DVolumeTextureImpl_GetPriority,
275 IWineD3DVolumeTextureImpl_PreLoad,
276 IWineD3DVolumeTextureImpl_GetType,
278 IWineD3DVolumeTextureImpl_SetLOD,
279 IWineD3DVolumeTextureImpl_GetLOD,
280 IWineD3DVolumeTextureImpl_GetLevelCount,
281 IWineD3DVolumeTextureImpl_SetAutoGenFilterType,
282 IWineD3DVolumeTextureImpl_GetAutoGenFilterType,
283 IWineD3DVolumeTextureImpl_GenerateMipSubLevels,
284 IWineD3DVolumeTextureImpl_GetSubResource,
285 IWineD3DVolumeTextureImpl_AddDirtyRegion,
288 HRESULT volumetexture_init(IWineD3DBaseTextureImpl *texture, UINT width, UINT height,
289 UINT depth, UINT levels, IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id,
290 WINED3DPOOL pool, void *parent, const struct wined3d_parent_ops *parent_ops)
292 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
293 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
294 UINT tmp_w, tmp_h, tmp_d;
298 /* TODO: It should only be possible to create textures for formats
299 * that are reported as supported. */
300 if (WINED3DFMT_UNKNOWN >= format_id)
302 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
303 return WINED3DERR_INVALIDCALL;
306 if (!gl_info->supported[EXT_TEXTURE3D])
308 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
309 return WINED3DERR_INVALIDCALL;
312 /* Calculate levels for mip mapping. */
313 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
315 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
317 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
318 return WINED3DERR_INVALIDCALL;
323 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
324 return WINED3DERR_INVALIDCALL;
331 levels = wined3d_log2i(max(max(width, height), depth)) + 1;
332 TRACE("Calculated levels = %u.\n", levels);
335 texture->lpVtbl = &IWineD3DVolumeTexture_Vtbl;
337 hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, &volumetexture_ops,
338 1, levels, WINED3DRTYPE_VOLUMETEXTURE, device, usage, format, pool,
339 parent, parent_ops, &volumetexture_resource_ops);
342 WARN("Failed to initialize basetexture, returning %#x.\n", hr);
346 /* Is NP2 support for volumes needed? */
347 texture->baseTexture.pow2Matrix[0] = 1.0f;
348 texture->baseTexture.pow2Matrix[5] = 1.0f;
349 texture->baseTexture.pow2Matrix[10] = 1.0f;
350 texture->baseTexture.pow2Matrix[15] = 1.0f;
351 texture->baseTexture.target = GL_TEXTURE_3D;
353 /* Generate all the surfaces. */
358 for (i = 0; i < texture->baseTexture.level_count; ++i)
360 IWineD3DVolume *volume;
362 /* Create the volume. */
363 hr = IWineD3DDeviceParent_CreateVolume(device->device_parent, parent,
364 tmp_w, tmp_h, tmp_d, format_id, pool, usage, &volume);
367 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
368 basetexture_cleanup(texture);
372 /* Set its container to this texture. */
373 volume_set_container((IWineD3DVolumeImpl *)volume, texture);
374 texture->baseTexture.sub_resources[i] = &((IWineD3DVolumeImpl *)volume)->resource;
376 /* Calculate the next mipmap level. */
377 tmp_w = max(1, tmp_w >> 1);
378 tmp_h = max(1, tmp_h >> 1);
379 tmp_d = max(1, tmp_d >> 1);