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
) {
228 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
230 TRACE("(%p) : relay to BaseTexture\n", This
);
231 return basetexture_bind((IWineD3DBaseTexture
*)iface
, srgb
, &dummy
);
234 static UINT WINAPI
IWineD3DVolumeTextureImpl_GetTextureDimensions(IWineD3DVolumeTexture
*iface
) {
235 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
236 TRACE("(%p)\n", This
);
237 return GL_TEXTURE_3D
;
240 static BOOL WINAPI
IWineD3DVolumeTextureImpl_IsCondNP2(IWineD3DVolumeTexture
*iface
) {
241 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
242 TRACE("(%p)\n", This
);
247 /* *******************************************
248 IWineD3DVolumeTexture IWineD3DVolumeTexture parts follow
249 ******************************************* */
250 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_GetLevelDesc(IWineD3DVolumeTexture
*iface
, UINT Level
,WINED3DVOLUME_DESC
*pDesc
) {
251 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
252 if (Level
< This
->baseTexture
.levels
) {
253 TRACE("(%p) Level (%d)\n", This
, Level
);
254 return IWineD3DVolume_GetDesc(This
->volumes
[Level
], pDesc
);
256 WARN("(%p) Level (%d)\n", This
, Level
);
260 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_GetVolumeLevel(IWineD3DVolumeTexture
*iface
, UINT Level
, IWineD3DVolume
** ppVolumeLevel
) {
261 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
262 if (Level
< This
->baseTexture
.levels
) {
263 *ppVolumeLevel
= This
->volumes
[Level
];
264 IWineD3DVolume_AddRef(*ppVolumeLevel
);
265 TRACE("(%p) -> level(%d) returning volume@%p\n", This
, Level
, *ppVolumeLevel
);
267 WARN("(%p) Level(%d) overflow Levels(%d)\n", This
, Level
, This
->baseTexture
.levels
);
268 return WINED3DERR_INVALIDCALL
;
273 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_LockBox(IWineD3DVolumeTexture
*iface
, UINT Level
, WINED3DLOCKED_BOX
* pLockedVolume
, CONST WINED3DBOX
* pBox
, DWORD Flags
) {
275 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
277 if (Level
< This
->baseTexture
.levels
) {
278 hr
= IWineD3DVolume_LockBox(This
->volumes
[Level
], pLockedVolume
, pBox
, Flags
);
279 TRACE("(%p) Level (%d) success(%u)\n", This
, Level
, hr
);
282 FIXME("(%p) level(%d) overflow Levels(%d)\n", This
, Level
, This
->baseTexture
.levels
);
283 return WINED3DERR_INVALIDCALL
;
288 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_UnlockBox(IWineD3DVolumeTexture
*iface
, UINT Level
) {
290 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
292 if (Level
< This
->baseTexture
.levels
) {
293 hr
= IWineD3DVolume_UnlockBox(This
->volumes
[Level
]);
294 TRACE("(%p) -> level(%d) success(%u)\n", This
, Level
, hr
);
297 FIXME("(%p) level(%d) overflow Levels(%d)\n", This
, Level
, This
->baseTexture
.levels
);
298 return WINED3DERR_INVALIDCALL
;
303 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_AddDirtyBox(IWineD3DVolumeTexture
*iface
, CONST WINED3DBOX
* pDirtyBox
) {
304 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
305 This
->baseTexture
.texture_rgb
.dirty
= TRUE
;
306 This
->baseTexture
.texture_srgb
.dirty
= TRUE
;
307 TRACE("(%p) : dirtyfication of volume Level (0)\n", This
);
308 volume_add_dirty_box(This
->volumes
[0], pDirtyBox
);
313 static const IWineD3DVolumeTextureVtbl IWineD3DVolumeTexture_Vtbl
=
316 IWineD3DVolumeTextureImpl_QueryInterface
,
317 IWineD3DVolumeTextureImpl_AddRef
,
318 IWineD3DVolumeTextureImpl_Release
,
320 IWineD3DVolumeTextureImpl_GetParent
,
321 IWineD3DVolumeTextureImpl_SetPrivateData
,
322 IWineD3DVolumeTextureImpl_GetPrivateData
,
323 IWineD3DVolumeTextureImpl_FreePrivateData
,
324 IWineD3DVolumeTextureImpl_SetPriority
,
325 IWineD3DVolumeTextureImpl_GetPriority
,
326 IWineD3DVolumeTextureImpl_PreLoad
,
327 IWineD3DVolumeTextureImpl_UnLoad
,
328 IWineD3DVolumeTextureImpl_GetType
,
330 IWineD3DVolumeTextureImpl_SetLOD
,
331 IWineD3DVolumeTextureImpl_GetLOD
,
332 IWineD3DVolumeTextureImpl_GetLevelCount
,
333 IWineD3DVolumeTextureImpl_SetAutoGenFilterType
,
334 IWineD3DVolumeTextureImpl_GetAutoGenFilterType
,
335 IWineD3DVolumeTextureImpl_GenerateMipSubLevels
,
336 IWineD3DVolumeTextureImpl_SetDirty
,
337 IWineD3DVolumeTextureImpl_GetDirty
,
339 IWineD3DVolumeTextureImpl_BindTexture
,
340 IWineD3DVolumeTextureImpl_GetTextureDimensions
,
341 IWineD3DVolumeTextureImpl_IsCondNP2
,
343 IWineD3DVolumeTextureImpl_GetLevelDesc
,
344 IWineD3DVolumeTextureImpl_GetVolumeLevel
,
345 IWineD3DVolumeTextureImpl_LockBox
,
346 IWineD3DVolumeTextureImpl_UnlockBox
,
347 IWineD3DVolumeTextureImpl_AddDirtyBox
350 HRESULT
volumetexture_init(IWineD3DVolumeTextureImpl
*texture
, UINT width
, UINT height
,
351 UINT depth
, UINT levels
, IWineD3DDeviceImpl
*device
, DWORD usage
, WINED3DFORMAT format
,
352 WINED3DPOOL pool
, IUnknown
*parent
, const struct wined3d_parent_ops
*parent_ops
)
354 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
355 const struct GlPixelFormatDesc
*format_desc
= getFormatDescEntry(format
, gl_info
);
356 UINT tmp_w
, tmp_h
, tmp_d
;
360 /* TODO: It should only be possible to create textures for formats
361 * that are reported as supported. */
362 if (WINED3DFMT_UNKNOWN
>= format
)
364 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
365 return WINED3DERR_INVALIDCALL
;
368 if (!gl_info
->supported
[EXT_TEXTURE3D
])
370 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
371 return WINED3DERR_INVALIDCALL
;
374 /* Calculate levels for mip mapping. */
375 if (usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
377 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
379 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
380 return WINED3DERR_INVALIDCALL
;
385 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
386 return WINED3DERR_INVALIDCALL
;
393 levels
= wined3d_log2i(max(max(width
, height
), depth
)) + 1;
394 TRACE("Calculated levels = %u.\n", levels
);
397 texture
->lpVtbl
= &IWineD3DVolumeTexture_Vtbl
;
399 hr
= basetexture_init((IWineD3DBaseTextureImpl
*)texture
, levels
, WINED3DRTYPE_VOLUMETEXTURE
,
400 device
, 0, usage
, format_desc
, pool
, parent
, parent_ops
);
403 WARN("Failed to initialize basetexture, returning %#x.\n", hr
);
407 /* Is NP2 support for volumes needed? */
408 texture
->baseTexture
.pow2Matrix
[0] = 1.0f
;
409 texture
->baseTexture
.pow2Matrix
[5] = 1.0f
;
410 texture
->baseTexture
.pow2Matrix
[10] = 1.0f
;
411 texture
->baseTexture
.pow2Matrix
[15] = 1.0f
;
413 /* Generate all the surfaces. */
418 for (i
= 0; i
< texture
->baseTexture
.levels
; ++i
)
420 /* Create the volume. */
421 hr
= IWineD3DDeviceParent_CreateVolume(device
->device_parent
, parent
,
422 tmp_w
, tmp_h
, tmp_d
, format
, pool
, usage
, &texture
->volumes
[i
]);
425 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr
);
426 texture
->volumes
[i
] = NULL
;
427 volumetexture_cleanup(texture
);
431 /* Set its container to this texture. */
432 IWineD3DVolume_SetContainer(texture
->volumes
[i
], (IWineD3DBase
*)texture
);
434 /* Calculate the next mipmap level. */
435 tmp_w
= max(1, tmp_w
>> 1);
436 tmp_h
= max(1, tmp_h
>> 1);
437 tmp_d
= max(1, tmp_d
>> 1);
439 texture
->baseTexture
.internal_preload
= volumetexture_internal_preload
;