2 * IWineD3DVolumeTexture implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2009-2010 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 /* Context activation is done by the caller. */
30 static HRESULT
volumetexture_bind(IWineD3DBaseTextureImpl
*texture
, BOOL srgb
)
34 TRACE("texture %p, srgb %#x.\n", texture
, srgb
);
36 return basetexture_bind(texture
, srgb
, &dummy
);
39 /* Do not call while under the GL lock. */
40 static void volumetexture_preload(IWineD3DBaseTextureImpl
*texture
, enum WINED3DSRGB srgb
)
42 IWineD3DDeviceImpl
*device
= texture
->resource
.device
;
43 struct wined3d_context
*context
= NULL
;
44 BOOL srgb_mode
= texture
->baseTexture
.is_srgb
;
45 BOOL srgb_was_toggled
= FALSE
;
48 TRACE("texture %p, srgb %#x.\n", texture
, srgb
);
50 if (!device
->isInDraw
) context
= context_acquire(device
, NULL
);
51 else if (texture
->baseTexture
.bindCount
> 0)
53 srgb_mode
= device
->stateBlock
->state
.sampler_states
[texture
->baseTexture
.sampler
][WINED3DSAMP_SRGBTEXTURE
];
54 srgb_was_toggled
= texture
->baseTexture
.is_srgb
!= srgb_mode
;
55 texture
->baseTexture
.is_srgb
= srgb_mode
;
58 /* If the texture is marked dirty or the srgb sampler setting has changed
59 * since the last load then reload the volumes. */
60 if (texture
->baseTexture
.texture_rgb
.dirty
)
62 for (i
= 0; i
< texture
->baseTexture
.level_count
; ++i
)
64 volume_load(volume_from_resource(texture
->baseTexture
.sub_resources
[i
]), i
, srgb_mode
);
67 else if (srgb_was_toggled
)
69 for (i
= 0; i
< texture
->baseTexture
.level_count
; ++i
)
71 IWineD3DVolumeImpl
*volume
= volume_from_resource(texture
->baseTexture
.sub_resources
[i
]);
72 volume_add_dirty_box(volume
, NULL
);
73 volume_load(volume
, i
, srgb_mode
);
78 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
81 if (context
) context_release(context
);
84 texture
->baseTexture
.texture_rgb
.dirty
= FALSE
;
87 /* Do not call while under the GL lock. */
88 static void volumetexture_unload(struct wined3d_resource
*resource
)
90 IWineD3DBaseTextureImpl
*texture
= basetexture_from_resource(resource
);
93 TRACE("texture %p.\n", texture
);
95 for (i
= 0; i
< texture
->baseTexture
.level_count
; ++i
)
97 struct wined3d_resource
*sub_resource
= texture
->baseTexture
.sub_resources
[i
];
98 sub_resource
->resource_ops
->resource_unload(sub_resource
);
101 basetexture_unload(texture
);
104 static const struct wined3d_texture_ops volumetexture_ops
=
107 volumetexture_preload
,
110 static const struct wined3d_resource_ops volumetexture_resource_ops
=
112 volumetexture_unload
,
115 static void volumetexture_cleanup(IWineD3DVolumeTextureImpl
*This
)
119 TRACE("(%p) : Cleaning up.\n", This
);
121 for (i
= 0; i
< This
->baseTexture
.level_count
; ++i
)
123 IWineD3DVolumeImpl
*volume
= volume_from_resource(This
->baseTexture
.sub_resources
[i
]);
127 /* Cleanup the container. */
128 volume_set_container(volume
, NULL
);
129 IWineD3DVolume_Release((IWineD3DVolume
*)volume
);
132 basetexture_cleanup((IWineD3DBaseTextureImpl
*)This
);
135 /* *******************************************
136 IWineD3DTexture IUnknown parts follow
137 ******************************************* */
139 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_QueryInterface(IWineD3DVolumeTexture
*iface
, REFIID riid
, LPVOID
*ppobj
)
141 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
142 TRACE("(%p)->(%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
143 if (IsEqualGUID(riid
, &IID_IUnknown
)
144 || IsEqualGUID(riid
, &IID_IWineD3DBase
)
145 || IsEqualGUID(riid
, &IID_IWineD3DResource
)
146 || IsEqualGUID(riid
, &IID_IWineD3DBaseTexture
)
147 || IsEqualGUID(riid
, &IID_IWineD3DVolumeTexture
)) {
148 IUnknown_AddRef(iface
);
153 return E_NOINTERFACE
;
156 static ULONG WINAPI
IWineD3DVolumeTextureImpl_AddRef(IWineD3DVolumeTexture
*iface
) {
157 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
158 TRACE("(%p) : AddRef increasing from %d\n", This
, This
->resource
.ref
);
159 return InterlockedIncrement(&This
->resource
.ref
);
162 /* Do not call while under the GL lock. */
163 static ULONG WINAPI
IWineD3DVolumeTextureImpl_Release(IWineD3DVolumeTexture
*iface
) {
164 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
166 TRACE("(%p) : Releasing from %d\n", This
, This
->resource
.ref
);
167 ref
= InterlockedDecrement(&This
->resource
.ref
);
170 volumetexture_cleanup(This
);
171 This
->resource
.parent_ops
->wined3d_object_destroyed(This
->resource
.parent
);
172 HeapFree(GetProcessHeap(), 0, This
);
177 /* ****************************************************
178 IWineD3DVolumeTexture IWineD3DResource parts follow
179 **************************************************** */
180 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_SetPrivateData(IWineD3DVolumeTexture
*iface
,
181 REFGUID riid
, const void *data
, DWORD data_size
, DWORD flags
)
183 return resource_set_private_data(&((IWineD3DVolumeTextureImpl
*)iface
)->resource
, riid
, data
, data_size
, flags
);
186 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_GetPrivateData(IWineD3DVolumeTexture
*iface
,
187 REFGUID guid
, void *data
, DWORD
*data_size
)
189 return resource_get_private_data(&((IWineD3DVolumeTextureImpl
*)iface
)->resource
, guid
, data
, data_size
);
192 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_FreePrivateData(IWineD3DVolumeTexture
*iface
, REFGUID refguid
)
194 return resource_free_private_data(&((IWineD3DVolumeTextureImpl
*)iface
)->resource
, refguid
);
197 static DWORD WINAPI
IWineD3DVolumeTextureImpl_SetPriority(IWineD3DVolumeTexture
*iface
, DWORD priority
)
199 return resource_set_priority(&((IWineD3DVolumeTextureImpl
*)iface
)->resource
, priority
);
202 static DWORD WINAPI
IWineD3DVolumeTextureImpl_GetPriority(IWineD3DVolumeTexture
*iface
)
204 return resource_get_priority(&((IWineD3DVolumeTextureImpl
*)iface
)->resource
);
207 static void WINAPI
IWineD3DVolumeTextureImpl_PreLoad(IWineD3DVolumeTexture
*iface
)
209 volumetexture_preload((IWineD3DBaseTextureImpl
*)iface
, SRGB_ANY
);
212 static WINED3DRESOURCETYPE WINAPI
IWineD3DVolumeTextureImpl_GetType(IWineD3DVolumeTexture
*iface
)
214 return resource_get_type(&((IWineD3DVolumeTextureImpl
*)iface
)->resource
);
217 static void * WINAPI
IWineD3DVolumeTextureImpl_GetParent(IWineD3DVolumeTexture
*iface
)
219 TRACE("iface %p\n", iface
);
221 return ((IWineD3DVolumeTextureImpl
*)iface
)->resource
.parent
;
224 /* ******************************************************
225 IWineD3DVolumeTexture IWineD3DBaseTexture parts follow
226 ****************************************************** */
227 static DWORD WINAPI
IWineD3DVolumeTextureImpl_SetLOD(IWineD3DVolumeTexture
*iface
, DWORD LODNew
) {
228 return basetexture_set_lod((IWineD3DBaseTextureImpl
*)iface
, LODNew
);
231 static DWORD WINAPI
IWineD3DVolumeTextureImpl_GetLOD(IWineD3DVolumeTexture
*iface
) {
232 return basetexture_get_lod((IWineD3DBaseTextureImpl
*)iface
);
235 static DWORD WINAPI
IWineD3DVolumeTextureImpl_GetLevelCount(IWineD3DVolumeTexture
*iface
)
237 return basetexture_get_level_count((IWineD3DBaseTextureImpl
*)iface
);
240 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_SetAutoGenFilterType(IWineD3DVolumeTexture
*iface
,
241 WINED3DTEXTUREFILTERTYPE FilterType
)
243 return basetexture_set_autogen_filter_type((IWineD3DBaseTextureImpl
*)iface
, FilterType
);
246 static WINED3DTEXTUREFILTERTYPE WINAPI
IWineD3DVolumeTextureImpl_GetAutoGenFilterType(IWineD3DVolumeTexture
*iface
)
248 return basetexture_get_autogen_filter_type((IWineD3DBaseTextureImpl
*)iface
);
251 static void WINAPI
IWineD3DVolumeTextureImpl_GenerateMipSubLevels(IWineD3DVolumeTexture
*iface
)
253 basetexture_generate_mipmaps((IWineD3DBaseTextureImpl
*)iface
);
256 static BOOL WINAPI
IWineD3DVolumeTextureImpl_IsCondNP2(IWineD3DVolumeTexture
*iface
)
258 TRACE("iface %p.\n", iface
);
263 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_GetLevelDesc(IWineD3DVolumeTexture
*iface
,
264 UINT sub_resource_idx
, WINED3DVOLUME_DESC
*desc
)
266 IWineD3DBaseTextureImpl
*texture
= (IWineD3DBaseTextureImpl
*)iface
;
267 struct wined3d_resource
*sub_resource
;
269 TRACE("iface %p, sub_resource_idx %u, desc %p.\n", iface
, sub_resource_idx
, desc
);
271 if (!(sub_resource
= basetexture_get_sub_resource(texture
, sub_resource_idx
)))
273 WARN("Failed to get sub-resource.\n");
274 return WINED3DERR_INVALIDCALL
;
277 IWineD3DVolume_GetDesc((IWineD3DVolume
*)volume_from_resource(sub_resource
), desc
);
282 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_GetVolumeLevel(IWineD3DVolumeTexture
*iface
,
283 UINT sub_resource_idx
, IWineD3DVolume
**volume
)
285 IWineD3DBaseTextureImpl
*texture
= (IWineD3DBaseTextureImpl
*)iface
;
286 struct wined3d_resource
*sub_resource
;
288 TRACE("iface %p, sub_resource_idx %u, volume %p.\n", iface
, sub_resource_idx
, volume
);
290 if (!(sub_resource
= basetexture_get_sub_resource(texture
, sub_resource_idx
)))
292 WARN("Failed to get sub-resource.\n");
293 return WINED3DERR_INVALIDCALL
;
296 *volume
= (IWineD3DVolume
*)volume_from_resource(sub_resource
);
297 IWineD3DVolume_AddRef(*volume
);
299 TRACE("Returning volume %p.\n", *volume
);
304 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_Map(IWineD3DVolumeTexture
*iface
,
305 UINT sub_resource_idx
, WINED3DLOCKED_BOX
*locked_box
, const WINED3DBOX
*box
, DWORD flags
)
307 IWineD3DBaseTextureImpl
*texture
= (IWineD3DBaseTextureImpl
*)iface
;
308 struct wined3d_resource
*sub_resource
;
310 TRACE("iface %p, sub_resource_idx %u, locked_box %p, box %p, flags %#x.\n",
311 iface
, sub_resource_idx
, locked_box
, box
, flags
);
313 if (!(sub_resource
= basetexture_get_sub_resource(texture
, sub_resource_idx
)))
315 WARN("Failed to get sub-resource.\n");
316 return WINED3DERR_INVALIDCALL
;
319 return IWineD3DVolume_Map((IWineD3DVolume
*)volume_from_resource(sub_resource
),
320 locked_box
, box
, flags
);
323 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_Unmap(IWineD3DVolumeTexture
*iface
, UINT sub_resource_idx
)
325 IWineD3DBaseTextureImpl
*texture
= (IWineD3DBaseTextureImpl
*)iface
;
326 struct wined3d_resource
*sub_resource
;
328 TRACE("iface %p, sub_resource_idx %u.\n", iface
, sub_resource_idx
);
330 if (!(sub_resource
= basetexture_get_sub_resource(texture
, sub_resource_idx
)))
332 WARN("Failed to get sub-resource.\n");
333 return WINED3DERR_INVALIDCALL
;
336 return IWineD3DVolume_Unmap((IWineD3DVolume
*)volume_from_resource(sub_resource
));
339 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_AddDirtyBox(IWineD3DVolumeTexture
*iface
, const WINED3DBOX
*dirty_box
)
341 IWineD3DBaseTextureImpl
*texture
= (IWineD3DBaseTextureImpl
*)iface
;
342 struct wined3d_resource
*sub_resource
;
344 TRACE("iface %p, dirty_box %p.\n", iface
, dirty_box
);
346 if (!(sub_resource
= basetexture_get_sub_resource(texture
, 0)))
348 WARN("Failed to get sub-resource.\n");
349 return WINED3DERR_INVALIDCALL
;
352 basetexture_set_dirty(texture
, TRUE
);
353 volume_add_dirty_box(volume_from_resource(sub_resource
), dirty_box
);
358 static const IWineD3DVolumeTextureVtbl IWineD3DVolumeTexture_Vtbl
=
361 IWineD3DVolumeTextureImpl_QueryInterface
,
362 IWineD3DVolumeTextureImpl_AddRef
,
363 IWineD3DVolumeTextureImpl_Release
,
365 IWineD3DVolumeTextureImpl_GetParent
,
366 IWineD3DVolumeTextureImpl_SetPrivateData
,
367 IWineD3DVolumeTextureImpl_GetPrivateData
,
368 IWineD3DVolumeTextureImpl_FreePrivateData
,
369 IWineD3DVolumeTextureImpl_SetPriority
,
370 IWineD3DVolumeTextureImpl_GetPriority
,
371 IWineD3DVolumeTextureImpl_PreLoad
,
372 IWineD3DVolumeTextureImpl_GetType
,
374 IWineD3DVolumeTextureImpl_SetLOD
,
375 IWineD3DVolumeTextureImpl_GetLOD
,
376 IWineD3DVolumeTextureImpl_GetLevelCount
,
377 IWineD3DVolumeTextureImpl_SetAutoGenFilterType
,
378 IWineD3DVolumeTextureImpl_GetAutoGenFilterType
,
379 IWineD3DVolumeTextureImpl_GenerateMipSubLevels
,
381 IWineD3DVolumeTextureImpl_IsCondNP2
,
383 IWineD3DVolumeTextureImpl_GetLevelDesc
,
384 IWineD3DVolumeTextureImpl_GetVolumeLevel
,
385 IWineD3DVolumeTextureImpl_Map
,
386 IWineD3DVolumeTextureImpl_Unmap
,
387 IWineD3DVolumeTextureImpl_AddDirtyBox
390 HRESULT
volumetexture_init(IWineD3DVolumeTextureImpl
*texture
, UINT width
, UINT height
,
391 UINT depth
, UINT levels
, IWineD3DDeviceImpl
*device
, DWORD usage
, enum wined3d_format_id format_id
,
392 WINED3DPOOL pool
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
394 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
395 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
);
396 UINT tmp_w
, tmp_h
, tmp_d
;
400 /* TODO: It should only be possible to create textures for formats
401 * that are reported as supported. */
402 if (WINED3DFMT_UNKNOWN
>= format_id
)
404 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
405 return WINED3DERR_INVALIDCALL
;
408 if (!gl_info
->supported
[EXT_TEXTURE3D
])
410 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
411 return WINED3DERR_INVALIDCALL
;
414 /* Calculate levels for mip mapping. */
415 if (usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
417 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
419 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
420 return WINED3DERR_INVALIDCALL
;
425 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
426 return WINED3DERR_INVALIDCALL
;
433 levels
= wined3d_log2i(max(max(width
, height
), depth
)) + 1;
434 TRACE("Calculated levels = %u.\n", levels
);
437 texture
->lpVtbl
= &IWineD3DVolumeTexture_Vtbl
;
439 hr
= basetexture_init((IWineD3DBaseTextureImpl
*)texture
, &volumetexture_ops
,
440 1, levels
, WINED3DRTYPE_VOLUMETEXTURE
, device
, usage
, format
, pool
,
441 parent
, parent_ops
, &volumetexture_resource_ops
);
444 WARN("Failed to initialize basetexture, returning %#x.\n", hr
);
448 /* Is NP2 support for volumes needed? */
449 texture
->baseTexture
.pow2Matrix
[0] = 1.0f
;
450 texture
->baseTexture
.pow2Matrix
[5] = 1.0f
;
451 texture
->baseTexture
.pow2Matrix
[10] = 1.0f
;
452 texture
->baseTexture
.pow2Matrix
[15] = 1.0f
;
453 texture
->baseTexture
.target
= GL_TEXTURE_3D
;
455 /* Generate all the surfaces. */
460 for (i
= 0; i
< texture
->baseTexture
.level_count
; ++i
)
462 IWineD3DVolume
*volume
;
464 /* Create the volume. */
465 hr
= IWineD3DDeviceParent_CreateVolume(device
->device_parent
, parent
,
466 tmp_w
, tmp_h
, tmp_d
, format_id
, pool
, usage
, &volume
);
469 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr
);
470 volumetexture_cleanup(texture
);
474 /* Set its container to this texture. */
475 volume_set_container((IWineD3DVolumeImpl
*)volume
, texture
);
476 texture
->baseTexture
.sub_resources
[i
] = &((IWineD3DVolumeImpl
*)volume
)->resource
;
478 /* Calculate the next mipmap level. */
479 tmp_w
= max(1, tmp_w
>> 1);
480 tmp_h
= max(1, tmp_h
>> 1);
481 tmp_d
= max(1, tmp_d
>> 1);