2 * IWineD3DVolumeTexture implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture
);
28 #define GLINFO_LOCATION (*gl_info)
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
.wineD3DDevice
;
35 const WineD3D_GL_Info
*gl_info
= &device
->adapter
->gl_info
;
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
)
44 ActivateContext(device
, device
->lastActiveRenderTarget
, CTXUSAGE_RESOURCELOAD
);
46 else if (GL_SUPPORT(EXT_TEXTURE_SRGB
) && This
->baseTexture
.bindCount
> 0)
48 srgb_mode
= device
->stateBlock
->samplerState
[This
->baseTexture
.sampler
][WINED3DSAMP_SRGBTEXTURE
];
49 srgb_was_toggled
= This
->baseTexture
.is_srgb
!= srgb_mode
;
50 This
->baseTexture
.is_srgb
= srgb_mode
;
53 /* If the texture is marked dirty or the srgb sampler setting has changed
54 * since the last load then reload the volumes. */
55 if (This
->baseTexture
.dirty
)
57 for (i
= 0; i
< This
->baseTexture
.levels
; ++i
)
59 IWineD3DVolume_LoadTexture(This
->volumes
[i
], i
, srgb_mode
);
62 else if (srgb_was_toggled
)
64 for (i
= 0; i
< This
->baseTexture
.levels
; ++i
)
66 volume_add_dirty_box(This
->volumes
[i
], NULL
);
67 IWineD3DVolume_LoadTexture(This
->volumes
[i
], i
, srgb_mode
);
72 TRACE("(%p) Texture not dirty, nothing to do.\n", iface
);
76 This
->baseTexture
.dirty
= FALSE
;
79 static void volumetexture_cleanup(IWineD3DVolumeTextureImpl
*This
, D3DCB_DESTROYVOLUMEFN volume_destroy_cb
)
83 TRACE("(%p) : Cleaning up.\n", This
);
85 for (i
= 0; i
< This
->baseTexture
.levels
; ++i
)
87 IWineD3DVolume
*volume
= This
->volumes
[i
];
91 /* Cleanup the container. */
92 IWineD3DVolume_SetContainer(volume
, NULL
);
93 volume_destroy_cb(volume
);
96 basetexture_cleanup((IWineD3DBaseTexture
*)This
);
99 HRESULT
volumetexture_init(IWineD3DVolumeTextureImpl
*texture
, UINT width
, UINT height
, UINT depth
, UINT levels
,
100 IWineD3DDeviceImpl
*device
, DWORD usage
, WINED3DFORMAT format
, WINED3DPOOL pool
, IUnknown
*parent
)
102 const WineD3D_GL_Info
*gl_info
= &device
->adapter
->gl_info
;
103 const struct GlPixelFormatDesc
*format_desc
= getFormatDescEntry(format
, gl_info
);
104 UINT tmp_w
, tmp_h
, tmp_d
;
108 /* TODO: It should only be possible to create textures for formats
109 * that are reported as supported. */
110 if (WINED3DFMT_UNKNOWN
>= format
)
112 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
113 return WINED3DERR_INVALIDCALL
;
116 if (!GL_SUPPORT(EXT_TEXTURE3D
))
118 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
119 return WINED3DERR_INVALIDCALL
;
122 /* Calculate levels for mip mapping. */
123 if (usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
125 if (!GL_SUPPORT(SGIS_GENERATE_MIPMAP
))
127 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
128 return WINED3DERR_INVALIDCALL
;
133 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
134 return WINED3DERR_INVALIDCALL
;
141 levels
= wined3d_log2i(max(max(width
, height
), depth
)) + 1;
142 TRACE("Calculated levels = %u.\n", levels
);
145 hr
= basetexture_init((IWineD3DBaseTextureImpl
*)texture
, levels
,
146 WINED3DRTYPE_VOLUMETEXTURE
, device
, 0, usage
, format_desc
, pool
, parent
);
149 WARN("Failed to initialize basetexture, returning %#x.\n", hr
);
153 if (texture
->resource
.format_desc
->Flags
& WINED3DFMT_FLAG_FILTERING
)
155 texture
->baseTexture
.minMipLookup
= minMipLookup
;
156 texture
->baseTexture
.magLookup
= magLookup
;
160 texture
->baseTexture
.minMipLookup
= minMipLookup_noFilter
;
161 texture
->baseTexture
.magLookup
= magLookup_noFilter
;
164 /* Is NP2 support for volumes needed? */
165 texture
->baseTexture
.pow2Matrix
[0] = 1.0;
166 texture
->baseTexture
.pow2Matrix
[5] = 1.0;
167 texture
->baseTexture
.pow2Matrix
[10] = 1.0;
168 texture
->baseTexture
.pow2Matrix
[15] = 1.0;
170 /* Generate all the surfaces. */
175 for (i
= 0; i
< texture
->baseTexture
.levels
; ++i
)
177 /* Create the volume. */
178 hr
= IWineD3DDeviceParent_CreateVolume(device
->device_parent
, parent
,
179 tmp_w
, tmp_h
, tmp_d
, format
, pool
, usage
, &texture
->volumes
[i
]);
182 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr
);
183 texture
->volumes
[i
] = NULL
;
184 volumetexture_cleanup(texture
, D3DCB_DefaultDestroyVolume
);
188 /* Set its container to this texture. */
189 IWineD3DVolume_SetContainer(texture
->volumes
[i
], (IWineD3DBase
*)texture
);
191 /* Calculate the next mipmap level. */
192 tmp_w
= max(1, tmp_w
>> 1);
193 tmp_h
= max(1, tmp_h
>> 1);
194 tmp_d
= max(1, tmp_d
>> 1);
196 texture
->baseTexture
.internal_preload
= volumetexture_internal_preload
;
201 #undef GLINFO_LOCATION
203 /* *******************************************
204 IWineD3DTexture IUnknown parts follow
205 ******************************************* */
207 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
209 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_QueryInterface(IWineD3DVolumeTexture
*iface
, REFIID riid
, LPVOID
*ppobj
)
211 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
212 TRACE("(%p)->(%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
213 if (IsEqualGUID(riid
, &IID_IUnknown
)
214 || IsEqualGUID(riid
, &IID_IWineD3DBase
)
215 || IsEqualGUID(riid
, &IID_IWineD3DResource
)
216 || IsEqualGUID(riid
, &IID_IWineD3DBaseTexture
)
217 || IsEqualGUID(riid
, &IID_IWineD3DVolumeTexture
)) {
218 IUnknown_AddRef(iface
);
223 return E_NOINTERFACE
;
226 static ULONG WINAPI
IWineD3DVolumeTextureImpl_AddRef(IWineD3DVolumeTexture
*iface
) {
227 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
228 TRACE("(%p) : AddRef increasing from %d\n", This
, This
->resource
.ref
);
229 return InterlockedIncrement(&This
->resource
.ref
);
232 static ULONG WINAPI
IWineD3DVolumeTextureImpl_Release(IWineD3DVolumeTexture
*iface
) {
233 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
235 TRACE("(%p) : Releasing from %d\n", This
, This
->resource
.ref
);
236 ref
= InterlockedDecrement(&This
->resource
.ref
);
238 IWineD3DVolumeTexture_Destroy(iface
, D3DCB_DefaultDestroyVolume
);
243 /* ****************************************************
244 IWineD3DVolumeTexture IWineD3DResource parts follow
245 **************************************************** */
246 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_GetDevice(IWineD3DVolumeTexture
*iface
, IWineD3DDevice
** ppDevice
) {
247 return resource_get_device((IWineD3DResource
*)iface
, ppDevice
);
250 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_SetPrivateData(IWineD3DVolumeTexture
*iface
, REFGUID refguid
, CONST
void* pData
, DWORD SizeOfData
, DWORD Flags
) {
251 return resource_set_private_data((IWineD3DResource
*)iface
, refguid
, pData
, SizeOfData
, Flags
);
254 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_GetPrivateData(IWineD3DVolumeTexture
*iface
, REFGUID refguid
, void* pData
, DWORD
* pSizeOfData
) {
255 return resource_get_private_data((IWineD3DResource
*)iface
, refguid
, pData
, pSizeOfData
);
258 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_FreePrivateData(IWineD3DVolumeTexture
*iface
, REFGUID refguid
) {
259 return resource_free_private_data((IWineD3DResource
*)iface
, refguid
);
262 static DWORD WINAPI
IWineD3DVolumeTextureImpl_SetPriority(IWineD3DVolumeTexture
*iface
, DWORD PriorityNew
) {
263 return resource_set_priority((IWineD3DResource
*)iface
, PriorityNew
);
266 static DWORD WINAPI
IWineD3DVolumeTextureImpl_GetPriority(IWineD3DVolumeTexture
*iface
) {
267 return resource_get_priority((IWineD3DResource
*)iface
);
270 static void WINAPI
IWineD3DVolumeTextureImpl_PreLoad(IWineD3DVolumeTexture
*iface
) {
271 volumetexture_internal_preload((IWineD3DBaseTexture
*) iface
, SRGB_ANY
);
274 static void WINAPI
IWineD3DVolumeTextureImpl_UnLoad(IWineD3DVolumeTexture
*iface
) {
276 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
277 TRACE("(%p)\n", This
);
279 /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
280 * surface before, this one will be a NOP and vice versa. Unloading an unloaded
283 for (i
= 0; i
< This
->baseTexture
.levels
; i
++) {
284 IWineD3DVolume_UnLoad(This
->volumes
[i
]);
287 basetexture_unload((IWineD3DBaseTexture
*)iface
);
290 static WINED3DRESOURCETYPE WINAPI
IWineD3DVolumeTextureImpl_GetType(IWineD3DVolumeTexture
*iface
) {
291 return resource_get_type((IWineD3DResource
*)iface
);
294 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_GetParent(IWineD3DVolumeTexture
*iface
, IUnknown
**pParent
) {
295 return resource_get_parent((IWineD3DResource
*)iface
, pParent
);
298 /* ******************************************************
299 IWineD3DVolumeTexture IWineD3DBaseTexture parts follow
300 ****************************************************** */
301 static DWORD WINAPI
IWineD3DVolumeTextureImpl_SetLOD(IWineD3DVolumeTexture
*iface
, DWORD LODNew
) {
302 return basetexture_set_lod((IWineD3DBaseTexture
*)iface
, LODNew
);
305 static DWORD WINAPI
IWineD3DVolumeTextureImpl_GetLOD(IWineD3DVolumeTexture
*iface
) {
306 return basetexture_get_lod((IWineD3DBaseTexture
*)iface
);
309 static DWORD WINAPI
IWineD3DVolumeTextureImpl_GetLevelCount(IWineD3DVolumeTexture
*iface
) {
310 return basetexture_get_level_count((IWineD3DBaseTexture
*)iface
);
313 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_SetAutoGenFilterType(IWineD3DVolumeTexture
*iface
, WINED3DTEXTUREFILTERTYPE FilterType
) {
314 return basetexture_set_autogen_filter_type((IWineD3DBaseTexture
*)iface
, FilterType
);
317 static WINED3DTEXTUREFILTERTYPE WINAPI
IWineD3DVolumeTextureImpl_GetAutoGenFilterType(IWineD3DVolumeTexture
*iface
) {
318 return basetexture_get_autogen_filter_type((IWineD3DBaseTexture
*)iface
);
321 static void WINAPI
IWineD3DVolumeTextureImpl_GenerateMipSubLevels(IWineD3DVolumeTexture
*iface
) {
322 basetexture_generate_mipmaps((IWineD3DBaseTexture
*)iface
);
325 /* Internal function, No d3d mapping */
326 static BOOL WINAPI
IWineD3DVolumeTextureImpl_SetDirty(IWineD3DVolumeTexture
*iface
, BOOL dirty
) {
327 return basetexture_set_dirty((IWineD3DBaseTexture
*)iface
, dirty
);
330 static BOOL WINAPI
IWineD3DVolumeTextureImpl_GetDirty(IWineD3DVolumeTexture
*iface
) {
331 return basetexture_get_dirty((IWineD3DBaseTexture
*)iface
);
334 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_BindTexture(IWineD3DVolumeTexture
*iface
, BOOL srgb
) {
335 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
337 TRACE("(%p) : relay to BaseTexture\n", This
);
338 return basetexture_bind((IWineD3DBaseTexture
*)iface
, srgb
, &dummy
);
341 static UINT WINAPI
IWineD3DVolumeTextureImpl_GetTextureDimensions(IWineD3DVolumeTexture
*iface
) {
342 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
343 TRACE("(%p)\n", This
);
344 return GL_TEXTURE_3D
;
347 static BOOL WINAPI
IWineD3DVolumeTextureImpl_IsCondNP2(IWineD3DVolumeTexture
*iface
) {
348 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
349 TRACE("(%p)\n", This
);
354 /* *******************************************
355 IWineD3DVolumeTexture IWineD3DVolumeTexture parts follow
356 ******************************************* */
357 static void WINAPI
IWineD3DVolumeTextureImpl_Destroy(IWineD3DVolumeTexture
*iface
, D3DCB_DESTROYVOLUMEFN D3DCB_DestroyVolume
) {
358 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
360 volumetexture_cleanup(This
, D3DCB_DestroyVolume
);
362 HeapFree(GetProcessHeap(), 0, This
);
365 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_GetLevelDesc(IWineD3DVolumeTexture
*iface
, UINT Level
,WINED3DVOLUME_DESC
*pDesc
) {
366 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
367 if (Level
< This
->baseTexture
.levels
) {
368 TRACE("(%p) Level (%d)\n", This
, Level
);
369 return IWineD3DVolume_GetDesc(This
->volumes
[Level
], pDesc
);
371 WARN("(%p) Level (%d)\n", This
, Level
);
375 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_GetVolumeLevel(IWineD3DVolumeTexture
*iface
, UINT Level
, IWineD3DVolume
** ppVolumeLevel
) {
376 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
377 if (Level
< This
->baseTexture
.levels
) {
378 *ppVolumeLevel
= This
->volumes
[Level
];
379 IWineD3DVolume_AddRef(*ppVolumeLevel
);
380 TRACE("(%p) -> level(%d) returning volume@%p\n", This
, Level
, *ppVolumeLevel
);
382 WARN("(%p) Level(%d) overflow Levels(%d)\n", This
, Level
, This
->baseTexture
.levels
);
383 return WINED3DERR_INVALIDCALL
;
388 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_LockBox(IWineD3DVolumeTexture
*iface
, UINT Level
, WINED3DLOCKED_BOX
* pLockedVolume
, CONST WINED3DBOX
* pBox
, DWORD Flags
) {
390 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
392 if (Level
< This
->baseTexture
.levels
) {
393 hr
= IWineD3DVolume_LockBox(This
->volumes
[Level
], pLockedVolume
, pBox
, Flags
);
394 TRACE("(%p) Level (%d) success(%u)\n", This
, Level
, hr
);
397 FIXME("(%p) level(%d) overflow Levels(%d)\n", This
, Level
, This
->baseTexture
.levels
);
398 return WINED3DERR_INVALIDCALL
;
403 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_UnlockBox(IWineD3DVolumeTexture
*iface
, UINT Level
) {
405 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
407 if (Level
< This
->baseTexture
.levels
) {
408 hr
= IWineD3DVolume_UnlockBox(This
->volumes
[Level
]);
409 TRACE("(%p) -> level(%d) success(%u)\n", This
, Level
, hr
);
412 FIXME("(%p) level(%d) overflow Levels(%d)\n", This
, Level
, This
->baseTexture
.levels
);
413 return WINED3DERR_INVALIDCALL
;
418 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_AddDirtyBox(IWineD3DVolumeTexture
*iface
, CONST WINED3DBOX
* pDirtyBox
) {
419 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
420 This
->baseTexture
.dirty
= TRUE
;
421 TRACE("(%p) : dirtyfication of volume Level (0)\n", This
);
422 volume_add_dirty_box(This
->volumes
[0], pDirtyBox
);
427 const IWineD3DVolumeTextureVtbl IWineD3DVolumeTexture_Vtbl
=
430 IWineD3DVolumeTextureImpl_QueryInterface
,
431 IWineD3DVolumeTextureImpl_AddRef
,
432 IWineD3DVolumeTextureImpl_Release
,
434 IWineD3DVolumeTextureImpl_GetParent
,
435 IWineD3DVolumeTextureImpl_GetDevice
,
436 IWineD3DVolumeTextureImpl_SetPrivateData
,
437 IWineD3DVolumeTextureImpl_GetPrivateData
,
438 IWineD3DVolumeTextureImpl_FreePrivateData
,
439 IWineD3DVolumeTextureImpl_SetPriority
,
440 IWineD3DVolumeTextureImpl_GetPriority
,
441 IWineD3DVolumeTextureImpl_PreLoad
,
442 IWineD3DVolumeTextureImpl_UnLoad
,
443 IWineD3DVolumeTextureImpl_GetType
,
445 IWineD3DVolumeTextureImpl_SetLOD
,
446 IWineD3DVolumeTextureImpl_GetLOD
,
447 IWineD3DVolumeTextureImpl_GetLevelCount
,
448 IWineD3DVolumeTextureImpl_SetAutoGenFilterType
,
449 IWineD3DVolumeTextureImpl_GetAutoGenFilterType
,
450 IWineD3DVolumeTextureImpl_GenerateMipSubLevels
,
451 IWineD3DVolumeTextureImpl_SetDirty
,
452 IWineD3DVolumeTextureImpl_GetDirty
,
454 IWineD3DVolumeTextureImpl_BindTexture
,
455 IWineD3DVolumeTextureImpl_GetTextureDimensions
,
456 IWineD3DVolumeTextureImpl_IsCondNP2
,
458 IWineD3DVolumeTextureImpl_Destroy
,
459 IWineD3DVolumeTextureImpl_GetLevelDesc
,
460 IWineD3DVolumeTextureImpl_GetVolumeLevel
,
461 IWineD3DVolumeTextureImpl_LockBox
,
462 IWineD3DVolumeTextureImpl_UnlockBox
,
463 IWineD3DVolumeTextureImpl_AddDirtyBox