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 /* Do not call while under the GL lock. */
30 static void volumetexture_internal_preload(IWineD3DBaseTexture
*iface
, enum WINED3DSRGB srgb
)
32 /* Override the IWineD3DResource Preload method. */
33 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
34 IWineD3DDeviceImpl
*device
= This
->resource
.device
;
35 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
36 struct wined3d_context
*context
= NULL
;
37 BOOL srgb_mode
= This
->baseTexture
.is_srgb
;
38 BOOL srgb_was_toggled
= FALSE
;
41 TRACE("(%p) : About to load texture.\n", This
);
43 if (!device
->isInDraw
) context
= context_acquire(device
, NULL
);
44 else if (gl_info
->supported
[EXT_TEXTURE_SRGB
] && This
->baseTexture
.bindCount
> 0)
46 srgb_mode
= device
->stateBlock
->state
.sampler_states
[This
->baseTexture
.sampler
][WINED3DSAMP_SRGBTEXTURE
];
47 srgb_was_toggled
= This
->baseTexture
.is_srgb
!= srgb_mode
;
48 This
->baseTexture
.is_srgb
= srgb_mode
;
51 /* If the texture is marked dirty or the srgb sampler setting has changed
52 * since the last load then reload the volumes. */
53 if (This
->baseTexture
.texture_rgb
.dirty
)
55 for (i
= 0; i
< This
->baseTexture
.level_count
; ++i
)
57 IWineD3DVolume
*volume
= (IWineD3DVolume
*)This
->baseTexture
.sub_resources
[i
];
58 IWineD3DVolume_LoadTexture(volume
, i
, srgb_mode
);
61 else if (srgb_was_toggled
)
63 for (i
= 0; i
< This
->baseTexture
.level_count
; ++i
)
65 IWineD3DVolume
*volume
= (IWineD3DVolume
*)This
->baseTexture
.sub_resources
[i
];
66 volume_add_dirty_box(volume
, NULL
);
67 IWineD3DVolume_LoadTexture(volume
, i
, srgb_mode
);
72 TRACE("(%p) Texture not dirty, nothing to do.\n", iface
);
75 if (context
) context_release(context
);
78 This
->baseTexture
.texture_rgb
.dirty
= FALSE
;
81 static void volumetexture_cleanup(IWineD3DVolumeTextureImpl
*This
)
85 TRACE("(%p) : Cleaning up.\n", This
);
87 for (i
= 0; i
< This
->baseTexture
.level_count
; ++i
)
89 IWineD3DVolumeImpl
*volume
= (IWineD3DVolumeImpl
*)This
->baseTexture
.sub_resources
[i
];
93 /* Cleanup the container. */
94 volume_set_container(volume
, NULL
);
95 IWineD3DVolume_Release((IWineD3DVolume
*)volume
);
98 basetexture_cleanup((IWineD3DBaseTexture
*)This
);
101 /* *******************************************
102 IWineD3DTexture IUnknown parts follow
103 ******************************************* */
105 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_QueryInterface(IWineD3DVolumeTexture
*iface
, REFIID riid
, LPVOID
*ppobj
)
107 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
108 TRACE("(%p)->(%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
109 if (IsEqualGUID(riid
, &IID_IUnknown
)
110 || IsEqualGUID(riid
, &IID_IWineD3DBase
)
111 || IsEqualGUID(riid
, &IID_IWineD3DResource
)
112 || IsEqualGUID(riid
, &IID_IWineD3DBaseTexture
)
113 || IsEqualGUID(riid
, &IID_IWineD3DVolumeTexture
)) {
114 IUnknown_AddRef(iface
);
119 return E_NOINTERFACE
;
122 static ULONG WINAPI
IWineD3DVolumeTextureImpl_AddRef(IWineD3DVolumeTexture
*iface
) {
123 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
124 TRACE("(%p) : AddRef increasing from %d\n", This
, This
->resource
.ref
);
125 return InterlockedIncrement(&This
->resource
.ref
);
128 /* Do not call while under the GL lock. */
129 static ULONG WINAPI
IWineD3DVolumeTextureImpl_Release(IWineD3DVolumeTexture
*iface
) {
130 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
132 TRACE("(%p) : Releasing from %d\n", This
, This
->resource
.ref
);
133 ref
= InterlockedDecrement(&This
->resource
.ref
);
136 volumetexture_cleanup(This
);
137 This
->resource
.parent_ops
->wined3d_object_destroyed(This
->resource
.parent
);
138 HeapFree(GetProcessHeap(), 0, This
);
143 /* ****************************************************
144 IWineD3DVolumeTexture IWineD3DResource parts follow
145 **************************************************** */
146 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_SetPrivateData(IWineD3DVolumeTexture
*iface
, REFGUID refguid
, CONST
void* pData
, DWORD SizeOfData
, DWORD Flags
) {
147 return resource_set_private_data((IWineD3DResource
*)iface
, refguid
, pData
, SizeOfData
, Flags
);
150 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_GetPrivateData(IWineD3DVolumeTexture
*iface
, REFGUID refguid
, void* pData
, DWORD
* pSizeOfData
) {
151 return resource_get_private_data((IWineD3DResource
*)iface
, refguid
, pData
, pSizeOfData
);
154 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_FreePrivateData(IWineD3DVolumeTexture
*iface
, REFGUID refguid
) {
155 return resource_free_private_data((IWineD3DResource
*)iface
, refguid
);
158 static DWORD WINAPI
IWineD3DVolumeTextureImpl_SetPriority(IWineD3DVolumeTexture
*iface
, DWORD PriorityNew
) {
159 return resource_set_priority((IWineD3DResource
*)iface
, PriorityNew
);
162 static DWORD WINAPI
IWineD3DVolumeTextureImpl_GetPriority(IWineD3DVolumeTexture
*iface
) {
163 return resource_get_priority((IWineD3DResource
*)iface
);
166 static void WINAPI
IWineD3DVolumeTextureImpl_PreLoad(IWineD3DVolumeTexture
*iface
) {
167 volumetexture_internal_preload((IWineD3DBaseTexture
*) iface
, SRGB_ANY
);
170 /* Do not call while under the GL lock. */
171 static void WINAPI
IWineD3DVolumeTextureImpl_UnLoad(IWineD3DVolumeTexture
*iface
) {
173 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
174 TRACE("(%p)\n", This
);
176 /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
177 * surface before, this one will be a NOP and vice versa. Unloading an unloaded
180 for (i
= 0; i
< This
->baseTexture
.level_count
; ++i
)
182 IWineD3DVolume_UnLoad((IWineD3DVolume
*)This
->baseTexture
.sub_resources
[i
]);
185 basetexture_unload((IWineD3DBaseTexture
*)iface
);
188 static WINED3DRESOURCETYPE WINAPI
IWineD3DVolumeTextureImpl_GetType(IWineD3DVolumeTexture
*iface
) {
189 return resource_get_type((IWineD3DResource
*)iface
);
192 static void * WINAPI
IWineD3DVolumeTextureImpl_GetParent(IWineD3DVolumeTexture
*iface
)
194 TRACE("iface %p\n", iface
);
196 return ((IWineD3DVolumeTextureImpl
*)iface
)->resource
.parent
;
199 /* ******************************************************
200 IWineD3DVolumeTexture IWineD3DBaseTexture parts follow
201 ****************************************************** */
202 static DWORD WINAPI
IWineD3DVolumeTextureImpl_SetLOD(IWineD3DVolumeTexture
*iface
, DWORD LODNew
) {
203 return basetexture_set_lod((IWineD3DBaseTexture
*)iface
, LODNew
);
206 static DWORD WINAPI
IWineD3DVolumeTextureImpl_GetLOD(IWineD3DVolumeTexture
*iface
) {
207 return basetexture_get_lod((IWineD3DBaseTexture
*)iface
);
210 static DWORD WINAPI
IWineD3DVolumeTextureImpl_GetLevelCount(IWineD3DVolumeTexture
*iface
) {
211 return basetexture_get_level_count((IWineD3DBaseTexture
*)iface
);
214 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_SetAutoGenFilterType(IWineD3DVolumeTexture
*iface
, WINED3DTEXTUREFILTERTYPE FilterType
) {
215 return basetexture_set_autogen_filter_type((IWineD3DBaseTexture
*)iface
, FilterType
);
218 static WINED3DTEXTUREFILTERTYPE WINAPI
IWineD3DVolumeTextureImpl_GetAutoGenFilterType(IWineD3DVolumeTexture
*iface
) {
219 return basetexture_get_autogen_filter_type((IWineD3DBaseTexture
*)iface
);
222 static void WINAPI
IWineD3DVolumeTextureImpl_GenerateMipSubLevels(IWineD3DVolumeTexture
*iface
) {
223 basetexture_generate_mipmaps((IWineD3DBaseTexture
*)iface
);
226 /* Internal function, No d3d mapping */
227 static BOOL WINAPI
IWineD3DVolumeTextureImpl_SetDirty(IWineD3DVolumeTexture
*iface
, BOOL dirty
) {
228 return basetexture_set_dirty((IWineD3DBaseTexture
*)iface
, dirty
);
231 static BOOL WINAPI
IWineD3DVolumeTextureImpl_GetDirty(IWineD3DVolumeTexture
*iface
) {
232 return basetexture_get_dirty((IWineD3DBaseTexture
*)iface
);
235 /* Context activation is done by the caller. */
236 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_BindTexture(IWineD3DVolumeTexture
*iface
, BOOL srgb
)
240 TRACE("iface %p, srgb %#x.\n", iface
, srgb
);
242 return basetexture_bind((IWineD3DBaseTexture
*)iface
, srgb
, &dummy
);
245 static BOOL WINAPI
IWineD3DVolumeTextureImpl_IsCondNP2(IWineD3DVolumeTexture
*iface
)
247 TRACE("iface %p.\n", iface
);
252 /* *******************************************
253 IWineD3DVolumeTexture IWineD3DVolumeTexture parts follow
254 ******************************************* */
255 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_GetLevelDesc(IWineD3DVolumeTexture
*iface
,
256 UINT level
, WINED3DVOLUME_DESC
*desc
)
258 IWineD3DBaseTextureImpl
*texture
= (IWineD3DBaseTextureImpl
*)iface
;
259 IWineD3DVolume
*volume
;
261 TRACE("iface %p, level %u, desc %p.\n", iface
, level
, desc
);
263 if (!(volume
= (IWineD3DVolume
*)basetexture_get_sub_resource(texture
, 0, level
)))
265 WARN("Failed to get sub-resource.\n");
266 return WINED3DERR_INVALIDCALL
;
269 IWineD3DVolume_GetDesc(volume
, desc
);
274 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_GetVolumeLevel(IWineD3DVolumeTexture
*iface
,
275 UINT level
, IWineD3DVolume
**volume
)
277 IWineD3DBaseTextureImpl
*texture
= (IWineD3DBaseTextureImpl
*)iface
;
280 TRACE("iface %p, level %u, volume %p.\n", iface
, level
, volume
);
282 if (!(v
= (IWineD3DVolume
*)basetexture_get_sub_resource(texture
, 0, level
)))
284 WARN("Failed to get sub-resource.\n");
285 return WINED3DERR_INVALIDCALL
;
288 IWineD3DVolume_AddRef(v
);
291 TRACE("Returning volume %p.\n", *volume
);
296 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_LockBox(IWineD3DVolumeTexture
*iface
,
297 UINT level
, WINED3DLOCKED_BOX
*locked_box
, const WINED3DBOX
*box
, DWORD flags
)
299 IWineD3DBaseTextureImpl
*texture
= (IWineD3DBaseTextureImpl
*)iface
;
300 IWineD3DVolume
*volume
;
302 TRACE("iface %p, level %u, locked_box %p, box %p, flags %#x.\n",
303 iface
, level
, locked_box
, box
, flags
);
305 if (!(volume
= (IWineD3DVolume
*)basetexture_get_sub_resource(texture
, 0, level
)))
307 WARN("Failed to get sub-resource.\n");
308 return WINED3DERR_INVALIDCALL
;
311 return IWineD3DVolume_LockBox(volume
, locked_box
, box
, flags
);
314 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_UnlockBox(IWineD3DVolumeTexture
*iface
, UINT level
)
316 IWineD3DBaseTextureImpl
*texture
= (IWineD3DBaseTextureImpl
*)iface
;
317 IWineD3DVolume
*volume
;
319 TRACE("iface %p, level %u.\n", iface
, level
);
321 if (!(volume
= (IWineD3DVolume
*)basetexture_get_sub_resource(texture
, 0, level
)))
323 WARN("Failed to get sub-resource.\n");
324 return WINED3DERR_INVALIDCALL
;
327 return IWineD3DVolume_UnlockBox(volume
);
330 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_AddDirtyBox(IWineD3DVolumeTexture
*iface
, const WINED3DBOX
*dirty_box
)
332 IWineD3DBaseTextureImpl
*texture
= (IWineD3DBaseTextureImpl
*)iface
;
333 IWineD3DVolume
*volume
;
335 TRACE("iface %p, dirty_box %p.\n", iface
, dirty_box
);
337 if (!(volume
= (IWineD3DVolume
*)basetexture_get_sub_resource(texture
, 0, 0)))
339 WARN("Failed to get sub-resource.\n");
340 return WINED3DERR_INVALIDCALL
;
343 texture
->baseTexture
.texture_rgb
.dirty
= TRUE
;
344 texture
->baseTexture
.texture_srgb
.dirty
= TRUE
;
345 volume_add_dirty_box(volume
, dirty_box
);
350 static const IWineD3DVolumeTextureVtbl IWineD3DVolumeTexture_Vtbl
=
353 IWineD3DVolumeTextureImpl_QueryInterface
,
354 IWineD3DVolumeTextureImpl_AddRef
,
355 IWineD3DVolumeTextureImpl_Release
,
357 IWineD3DVolumeTextureImpl_GetParent
,
358 IWineD3DVolumeTextureImpl_SetPrivateData
,
359 IWineD3DVolumeTextureImpl_GetPrivateData
,
360 IWineD3DVolumeTextureImpl_FreePrivateData
,
361 IWineD3DVolumeTextureImpl_SetPriority
,
362 IWineD3DVolumeTextureImpl_GetPriority
,
363 IWineD3DVolumeTextureImpl_PreLoad
,
364 IWineD3DVolumeTextureImpl_UnLoad
,
365 IWineD3DVolumeTextureImpl_GetType
,
367 IWineD3DVolumeTextureImpl_SetLOD
,
368 IWineD3DVolumeTextureImpl_GetLOD
,
369 IWineD3DVolumeTextureImpl_GetLevelCount
,
370 IWineD3DVolumeTextureImpl_SetAutoGenFilterType
,
371 IWineD3DVolumeTextureImpl_GetAutoGenFilterType
,
372 IWineD3DVolumeTextureImpl_GenerateMipSubLevels
,
373 IWineD3DVolumeTextureImpl_SetDirty
,
374 IWineD3DVolumeTextureImpl_GetDirty
,
376 IWineD3DVolumeTextureImpl_BindTexture
,
377 IWineD3DVolumeTextureImpl_IsCondNP2
,
379 IWineD3DVolumeTextureImpl_GetLevelDesc
,
380 IWineD3DVolumeTextureImpl_GetVolumeLevel
,
381 IWineD3DVolumeTextureImpl_LockBox
,
382 IWineD3DVolumeTextureImpl_UnlockBox
,
383 IWineD3DVolumeTextureImpl_AddDirtyBox
386 HRESULT
volumetexture_init(IWineD3DVolumeTextureImpl
*texture
, UINT width
, UINT height
,
387 UINT depth
, UINT levels
, IWineD3DDeviceImpl
*device
, DWORD usage
, enum wined3d_format_id format_id
,
388 WINED3DPOOL pool
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
390 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
391 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
);
392 UINT tmp_w
, tmp_h
, tmp_d
;
396 /* TODO: It should only be possible to create textures for formats
397 * that are reported as supported. */
398 if (WINED3DFMT_UNKNOWN
>= format_id
)
400 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
401 return WINED3DERR_INVALIDCALL
;
404 if (!gl_info
->supported
[EXT_TEXTURE3D
])
406 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
407 return WINED3DERR_INVALIDCALL
;
410 /* Calculate levels for mip mapping. */
411 if (usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
413 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
415 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
416 return WINED3DERR_INVALIDCALL
;
421 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
422 return WINED3DERR_INVALIDCALL
;
429 levels
= wined3d_log2i(max(max(width
, height
), depth
)) + 1;
430 TRACE("Calculated levels = %u.\n", levels
);
433 texture
->lpVtbl
= &IWineD3DVolumeTexture_Vtbl
;
435 hr
= basetexture_init((IWineD3DBaseTextureImpl
*)texture
, 1, levels
,
436 WINED3DRTYPE_VOLUMETEXTURE
, device
, 0, usage
, format
, pool
, parent
, parent_ops
);
439 WARN("Failed to initialize basetexture, returning %#x.\n", hr
);
443 /* Is NP2 support for volumes needed? */
444 texture
->baseTexture
.pow2Matrix
[0] = 1.0f
;
445 texture
->baseTexture
.pow2Matrix
[5] = 1.0f
;
446 texture
->baseTexture
.pow2Matrix
[10] = 1.0f
;
447 texture
->baseTexture
.pow2Matrix
[15] = 1.0f
;
448 texture
->baseTexture
.target
= GL_TEXTURE_3D
;
450 /* Generate all the surfaces. */
455 for (i
= 0; i
< texture
->baseTexture
.level_count
; ++i
)
457 IWineD3DVolume
*volume
;
459 /* Create the volume. */
460 hr
= IWineD3DDeviceParent_CreateVolume(device
->device_parent
, parent
,
461 tmp_w
, tmp_h
, tmp_d
, format_id
, pool
, usage
, &volume
);
464 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr
);
465 volumetexture_cleanup(texture
);
469 /* Set its container to this texture. */
470 volume_set_container((IWineD3DVolumeImpl
*)volume
, texture
);
471 texture
->baseTexture
.sub_resources
[i
] = (IWineD3DResourceImpl
*)volume
;
473 /* Calculate the next mipmap level. */
474 tmp_w
= max(1, tmp_w
>> 1);
475 tmp_h
= max(1, tmp_h
>> 1);
476 tmp_d
= max(1, tmp_d
>> 1);
478 texture
->baseTexture
.internal_preload
= volumetexture_internal_preload
;