wined3d: Get rid of IWineD3DBaseTexture::IsCondNP2().
[wine.git] / dlls / wined3d / volumetexture.c
blob3831ef1e282db3b3ebe2e18bd2d421d3df80b26a
1 /*
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
22 #include "config.h"
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)
31 BOOL dummy;
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;
45 unsigned int i;
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);
75 else
77 TRACE("Texture %p not dirty, nothing to do.\n", texture);
80 if (context) context_release(context);
82 /* No longer dirty */
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);
90 unsigned int i;
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 =
105 volumetexture_bind,
106 volumetexture_preload,
109 static const struct wined3d_resource_ops volumetexture_resource_ops =
111 volumetexture_unload,
114 static void volumetexture_cleanup(IWineD3DBaseTextureImpl *This)
116 unsigned int i;
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];
124 if (sub_resource)
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);
150 *ppobj = This;
151 return S_OK;
153 *ppobj = NULL;
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;
168 ULONG ref;
169 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
170 ref = InterlockedDecrement(&This->resource.ref);
171 if (!ref)
173 volumetexture_cleanup(This);
174 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
175 HeapFree(GetProcessHeap(), 0, This);
177 return ref;
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 struct wined3d_resource * WINAPI IWineD3DVolumeTextureImpl_GetSubResource(IWineD3DBaseTexture *iface,
256 UINT sub_resource_idx)
258 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
260 TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
262 return basetexture_get_sub_resource(texture, sub_resource_idx);
265 static HRESULT WINAPI IWineD3DVolumeTextureImpl_AddDirtyRegion(IWineD3DBaseTexture *iface,
266 UINT layer, const WINED3DBOX *dirty_region)
268 IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
269 struct wined3d_resource *sub_resource;
271 TRACE("iface %p, layer %u, dirty_region %p.\n", iface, layer, dirty_region);
273 if (!(sub_resource = basetexture_get_sub_resource(texture, layer * texture->baseTexture.level_count)))
275 WARN("Failed to get sub-resource.\n");
276 return WINED3DERR_INVALIDCALL;
279 basetexture_set_dirty(texture, TRUE);
280 volume_add_dirty_box(volume_from_resource(sub_resource), dirty_region);
282 return WINED3D_OK;
285 static const IWineD3DBaseTextureVtbl IWineD3DVolumeTexture_Vtbl =
287 /* IUnknown */
288 IWineD3DVolumeTextureImpl_QueryInterface,
289 IWineD3DVolumeTextureImpl_AddRef,
290 IWineD3DVolumeTextureImpl_Release,
291 /* resource */
292 IWineD3DVolumeTextureImpl_GetParent,
293 IWineD3DVolumeTextureImpl_SetPrivateData,
294 IWineD3DVolumeTextureImpl_GetPrivateData,
295 IWineD3DVolumeTextureImpl_FreePrivateData,
296 IWineD3DVolumeTextureImpl_SetPriority,
297 IWineD3DVolumeTextureImpl_GetPriority,
298 IWineD3DVolumeTextureImpl_PreLoad,
299 IWineD3DVolumeTextureImpl_GetType,
300 /* BaseTexture */
301 IWineD3DVolumeTextureImpl_SetLOD,
302 IWineD3DVolumeTextureImpl_GetLOD,
303 IWineD3DVolumeTextureImpl_GetLevelCount,
304 IWineD3DVolumeTextureImpl_SetAutoGenFilterType,
305 IWineD3DVolumeTextureImpl_GetAutoGenFilterType,
306 IWineD3DVolumeTextureImpl_GenerateMipSubLevels,
307 IWineD3DVolumeTextureImpl_GetSubResource,
308 IWineD3DVolumeTextureImpl_AddDirtyRegion,
311 HRESULT volumetexture_init(IWineD3DBaseTextureImpl *texture, UINT width, UINT height,
312 UINT depth, UINT levels, IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id,
313 WINED3DPOOL pool, void *parent, const struct wined3d_parent_ops *parent_ops)
315 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
316 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
317 UINT tmp_w, tmp_h, tmp_d;
318 unsigned int i;
319 HRESULT hr;
321 /* TODO: It should only be possible to create textures for formats
322 * that are reported as supported. */
323 if (WINED3DFMT_UNKNOWN >= format_id)
325 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
326 return WINED3DERR_INVALIDCALL;
329 if (!gl_info->supported[EXT_TEXTURE3D])
331 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
332 return WINED3DERR_INVALIDCALL;
335 /* Calculate levels for mip mapping. */
336 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
338 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
340 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
341 return WINED3DERR_INVALIDCALL;
344 if (levels > 1)
346 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
347 return WINED3DERR_INVALIDCALL;
350 levels = 1;
352 else if (!levels)
354 levels = wined3d_log2i(max(max(width, height), depth)) + 1;
355 TRACE("Calculated levels = %u.\n", levels);
358 texture->lpVtbl = &IWineD3DVolumeTexture_Vtbl;
360 hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, &volumetexture_ops,
361 1, levels, WINED3DRTYPE_VOLUMETEXTURE, device, usage, format, pool,
362 parent, parent_ops, &volumetexture_resource_ops);
363 if (FAILED(hr))
365 WARN("Failed to initialize basetexture, returning %#x.\n", hr);
366 return hr;
369 /* Is NP2 support for volumes needed? */
370 texture->baseTexture.pow2Matrix[0] = 1.0f;
371 texture->baseTexture.pow2Matrix[5] = 1.0f;
372 texture->baseTexture.pow2Matrix[10] = 1.0f;
373 texture->baseTexture.pow2Matrix[15] = 1.0f;
374 texture->baseTexture.target = GL_TEXTURE_3D;
376 /* Generate all the surfaces. */
377 tmp_w = width;
378 tmp_h = height;
379 tmp_d = depth;
381 for (i = 0; i < texture->baseTexture.level_count; ++i)
383 IWineD3DVolume *volume;
385 /* Create the volume. */
386 hr = IWineD3DDeviceParent_CreateVolume(device->device_parent, parent,
387 tmp_w, tmp_h, tmp_d, format_id, pool, usage, &volume);
388 if (FAILED(hr))
390 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
391 volumetexture_cleanup(texture);
392 return hr;
395 /* Set its container to this texture. */
396 volume_set_container((IWineD3DVolumeImpl *)volume, texture);
397 texture->baseTexture.sub_resources[i] = &((IWineD3DVolumeImpl *)volume)->resource;
399 /* Calculate the next mipmap level. */
400 tmp_w = max(1, tmp_w >> 1);
401 tmp_h = max(1, tmp_h >> 1);
402 tmp_d = max(1, tmp_d >> 1);
405 return WINED3D_OK;