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, CTXUSAGE_RESOURCELOAD);
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.levels; ++i)
56 IWineD3DVolume_LoadTexture(This->volumes[i], i, srgb_mode);
59 else if (srgb_was_toggled)
61 for (i = 0; i < This->baseTexture.levels; ++i)
63 volume_add_dirty_box(This->volumes[i], NULL);
64 IWineD3DVolume_LoadTexture(This->volumes[i], i, srgb_mode);
69 TRACE("(%p) Texture not dirty, nothing to do.\n", iface);
72 if (context) context_release(context);
75 This->baseTexture.texture_rgb.dirty = FALSE;
78 static void volumetexture_cleanup(IWineD3DVolumeTextureImpl *This)
82 TRACE("(%p) : Cleaning up.\n", This);
84 for (i = 0; i < This->baseTexture.levels; ++i)
86 IWineD3DVolume *volume = This->volumes[i];
90 /* Cleanup the container. */
91 IWineD3DVolume_SetContainer(volume, NULL);
92 IWineD3DVolume_Release(volume);
95 basetexture_cleanup((IWineD3DBaseTexture *)This);
98 /* *******************************************
99 IWineD3DTexture IUnknown parts follow
100 ******************************************* */
102 static HRESULT WINAPI IWineD3DVolumeTextureImpl_QueryInterface(IWineD3DVolumeTexture *iface, REFIID riid, LPVOID *ppobj)
104 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
105 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
106 if (IsEqualGUID(riid, &IID_IUnknown)
107 || IsEqualGUID(riid, &IID_IWineD3DBase)
108 || IsEqualGUID(riid, &IID_IWineD3DResource)
109 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
110 || IsEqualGUID(riid, &IID_IWineD3DVolumeTexture)) {
111 IUnknown_AddRef(iface);
116 return E_NOINTERFACE;
119 static ULONG WINAPI IWineD3DVolumeTextureImpl_AddRef(IWineD3DVolumeTexture *iface) {
120 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
121 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
122 return InterlockedIncrement(&This->resource.ref);
125 static ULONG WINAPI IWineD3DVolumeTextureImpl_Release(IWineD3DVolumeTexture *iface) {
126 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
128 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
129 ref = InterlockedDecrement(&This->resource.ref);
132 volumetexture_cleanup(This);
133 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
134 HeapFree(GetProcessHeap(), 0, This);
139 /* ****************************************************
140 IWineD3DVolumeTexture IWineD3DResource parts follow
141 **************************************************** */
142 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetPrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
143 return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
146 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetPrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
147 return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
150 static HRESULT WINAPI IWineD3DVolumeTextureImpl_FreePrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid) {
151 return resource_free_private_data((IWineD3DResource *)iface, refguid);
154 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetPriority(IWineD3DVolumeTexture *iface, DWORD PriorityNew) {
155 return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
158 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetPriority(IWineD3DVolumeTexture *iface) {
159 return resource_get_priority((IWineD3DResource *)iface);
162 static void WINAPI IWineD3DVolumeTextureImpl_PreLoad(IWineD3DVolumeTexture *iface) {
163 volumetexture_internal_preload((IWineD3DBaseTexture *) iface, SRGB_ANY);
166 static void WINAPI IWineD3DVolumeTextureImpl_UnLoad(IWineD3DVolumeTexture *iface) {
168 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
169 TRACE("(%p)\n", This);
171 /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
172 * surface before, this one will be a NOP and vice versa. Unloading an unloaded
175 for (i = 0; i < This->baseTexture.levels; i++) {
176 IWineD3DVolume_UnLoad(This->volumes[i]);
179 basetexture_unload((IWineD3DBaseTexture *)iface);
182 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeTextureImpl_GetType(IWineD3DVolumeTexture *iface) {
183 return resource_get_type((IWineD3DResource *)iface);
186 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetParent(IWineD3DVolumeTexture *iface, IUnknown **pParent) {
187 return resource_get_parent((IWineD3DResource *)iface, pParent);
190 /* ******************************************************
191 IWineD3DVolumeTexture IWineD3DBaseTexture parts follow
192 ****************************************************** */
193 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetLOD(IWineD3DVolumeTexture *iface, DWORD LODNew) {
194 return basetexture_set_lod((IWineD3DBaseTexture *)iface, LODNew);
197 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLOD(IWineD3DVolumeTexture *iface) {
198 return basetexture_get_lod((IWineD3DBaseTexture *)iface);
201 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLevelCount(IWineD3DVolumeTexture *iface) {
202 return basetexture_get_level_count((IWineD3DBaseTexture *)iface);
205 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetAutoGenFilterType(IWineD3DVolumeTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
206 return basetexture_set_autogen_filter_type((IWineD3DBaseTexture *)iface, FilterType);
209 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DVolumeTextureImpl_GetAutoGenFilterType(IWineD3DVolumeTexture *iface) {
210 return basetexture_get_autogen_filter_type((IWineD3DBaseTexture *)iface);
213 static void WINAPI IWineD3DVolumeTextureImpl_GenerateMipSubLevels(IWineD3DVolumeTexture *iface) {
214 basetexture_generate_mipmaps((IWineD3DBaseTexture *)iface);
217 /* Internal function, No d3d mapping */
218 static BOOL WINAPI IWineD3DVolumeTextureImpl_SetDirty(IWineD3DVolumeTexture *iface, BOOL dirty) {
219 return basetexture_set_dirty((IWineD3DBaseTexture *)iface, dirty);
222 static BOOL WINAPI IWineD3DVolumeTextureImpl_GetDirty(IWineD3DVolumeTexture *iface) {
223 return basetexture_get_dirty((IWineD3DBaseTexture *)iface);
226 /* Context activation is done by the caller. */
227 static HRESULT WINAPI IWineD3DVolumeTextureImpl_BindTexture(IWineD3DVolumeTexture *iface, BOOL srgb)
231 TRACE("iface %p, srgb %#x.\n", iface, srgb);
233 return basetexture_bind((IWineD3DBaseTexture *)iface, srgb, &dummy);
236 static UINT WINAPI IWineD3DVolumeTextureImpl_GetTextureDimensions(IWineD3DVolumeTexture *iface)
238 TRACE("iface %p.\n", iface);
240 return GL_TEXTURE_3D;
243 static BOOL WINAPI IWineD3DVolumeTextureImpl_IsCondNP2(IWineD3DVolumeTexture *iface)
245 TRACE("iface %p.\n", iface);
250 /* *******************************************
251 IWineD3DVolumeTexture IWineD3DVolumeTexture parts follow
252 ******************************************* */
253 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetLevelDesc(IWineD3DVolumeTexture *iface, UINT Level,WINED3DVOLUME_DESC *pDesc) {
254 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
255 if (Level < This->baseTexture.levels) {
256 TRACE("(%p) Level (%d)\n", This, Level);
257 return IWineD3DVolume_GetDesc(This->volumes[Level], pDesc);
259 WARN("(%p) Level (%d)\n", This, Level);
263 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetVolumeLevel(IWineD3DVolumeTexture *iface, UINT Level, IWineD3DVolume** ppVolumeLevel) {
264 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
265 if (Level < This->baseTexture.levels) {
266 *ppVolumeLevel = This->volumes[Level];
267 IWineD3DVolume_AddRef(*ppVolumeLevel);
268 TRACE("(%p) -> level(%d) returning volume@%p\n", This, Level, *ppVolumeLevel);
270 WARN("(%p) Level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
271 return WINED3DERR_INVALIDCALL;
276 static HRESULT WINAPI IWineD3DVolumeTextureImpl_LockBox(IWineD3DVolumeTexture *iface, UINT Level, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
278 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
280 if (Level < This->baseTexture.levels) {
281 hr = IWineD3DVolume_LockBox(This->volumes[Level], pLockedVolume, pBox, Flags);
282 TRACE("(%p) Level (%d) success(%u)\n", This, Level, hr);
285 FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
286 return WINED3DERR_INVALIDCALL;
291 static HRESULT WINAPI IWineD3DVolumeTextureImpl_UnlockBox(IWineD3DVolumeTexture *iface, UINT Level) {
293 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
295 if (Level < This->baseTexture.levels) {
296 hr = IWineD3DVolume_UnlockBox(This->volumes[Level]);
297 TRACE("(%p) -> level(%d) success(%u)\n", This, Level, hr);
300 FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
301 return WINED3DERR_INVALIDCALL;
306 static HRESULT WINAPI IWineD3DVolumeTextureImpl_AddDirtyBox(IWineD3DVolumeTexture *iface, CONST WINED3DBOX* pDirtyBox) {
307 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
308 This->baseTexture.texture_rgb.dirty = TRUE;
309 This->baseTexture.texture_srgb.dirty = TRUE;
310 TRACE("(%p) : dirtyfication of volume Level (0)\n", This);
311 volume_add_dirty_box(This->volumes[0], pDirtyBox);
316 static const IWineD3DVolumeTextureVtbl IWineD3DVolumeTexture_Vtbl =
319 IWineD3DVolumeTextureImpl_QueryInterface,
320 IWineD3DVolumeTextureImpl_AddRef,
321 IWineD3DVolumeTextureImpl_Release,
323 IWineD3DVolumeTextureImpl_GetParent,
324 IWineD3DVolumeTextureImpl_SetPrivateData,
325 IWineD3DVolumeTextureImpl_GetPrivateData,
326 IWineD3DVolumeTextureImpl_FreePrivateData,
327 IWineD3DVolumeTextureImpl_SetPriority,
328 IWineD3DVolumeTextureImpl_GetPriority,
329 IWineD3DVolumeTextureImpl_PreLoad,
330 IWineD3DVolumeTextureImpl_UnLoad,
331 IWineD3DVolumeTextureImpl_GetType,
333 IWineD3DVolumeTextureImpl_SetLOD,
334 IWineD3DVolumeTextureImpl_GetLOD,
335 IWineD3DVolumeTextureImpl_GetLevelCount,
336 IWineD3DVolumeTextureImpl_SetAutoGenFilterType,
337 IWineD3DVolumeTextureImpl_GetAutoGenFilterType,
338 IWineD3DVolumeTextureImpl_GenerateMipSubLevels,
339 IWineD3DVolumeTextureImpl_SetDirty,
340 IWineD3DVolumeTextureImpl_GetDirty,
342 IWineD3DVolumeTextureImpl_BindTexture,
343 IWineD3DVolumeTextureImpl_GetTextureDimensions,
344 IWineD3DVolumeTextureImpl_IsCondNP2,
346 IWineD3DVolumeTextureImpl_GetLevelDesc,
347 IWineD3DVolumeTextureImpl_GetVolumeLevel,
348 IWineD3DVolumeTextureImpl_LockBox,
349 IWineD3DVolumeTextureImpl_UnlockBox,
350 IWineD3DVolumeTextureImpl_AddDirtyBox
353 HRESULT volumetexture_init(IWineD3DVolumeTextureImpl *texture, UINT width, UINT height,
354 UINT depth, UINT levels, IWineD3DDeviceImpl *device, DWORD usage, WINED3DFORMAT format,
355 WINED3DPOOL pool, IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
357 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
358 const struct wined3d_format_desc *format_desc = getFormatDescEntry(format, gl_info);
359 UINT tmp_w, tmp_h, tmp_d;
363 /* TODO: It should only be possible to create textures for formats
364 * that are reported as supported. */
365 if (WINED3DFMT_UNKNOWN >= format)
367 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
368 return WINED3DERR_INVALIDCALL;
371 if (!gl_info->supported[EXT_TEXTURE3D])
373 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
374 return WINED3DERR_INVALIDCALL;
377 /* Calculate levels for mip mapping. */
378 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
380 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
382 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
383 return WINED3DERR_INVALIDCALL;
388 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
389 return WINED3DERR_INVALIDCALL;
396 levels = wined3d_log2i(max(max(width, height), depth)) + 1;
397 TRACE("Calculated levels = %u.\n", levels);
400 texture->lpVtbl = &IWineD3DVolumeTexture_Vtbl;
402 hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, levels, WINED3DRTYPE_VOLUMETEXTURE,
403 device, 0, usage, format_desc, pool, parent, parent_ops);
406 WARN("Failed to initialize basetexture, returning %#x.\n", hr);
410 /* Is NP2 support for volumes needed? */
411 texture->baseTexture.pow2Matrix[0] = 1.0f;
412 texture->baseTexture.pow2Matrix[5] = 1.0f;
413 texture->baseTexture.pow2Matrix[10] = 1.0f;
414 texture->baseTexture.pow2Matrix[15] = 1.0f;
416 /* Generate all the surfaces. */
421 for (i = 0; i < texture->baseTexture.levels; ++i)
423 /* Create the volume. */
424 hr = IWineD3DDeviceParent_CreateVolume(device->device_parent, parent,
425 tmp_w, tmp_h, tmp_d, format, pool, usage, &texture->volumes[i]);
428 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
429 texture->volumes[i] = NULL;
430 volumetexture_cleanup(texture);
434 /* Set its container to this texture. */
435 IWineD3DVolume_SetContainer(texture->volumes[i], (IWineD3DBase *)texture);
437 /* Calculate the next mipmap level. */
438 tmp_w = max(1, tmp_w >> 1);
439 tmp_h = max(1, tmp_h >> 1);
440 tmp_d = max(1, tmp_d >> 1);
442 texture->baseTexture.internal_preload = volumetexture_internal_preload;