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
,
31 const struct wined3d_gl_info
*gl_info
, BOOL srgb
)
35 TRACE("texture %p, gl_info %p, srgb %#x.\n", texture
, gl_info
, srgb
);
37 return basetexture_bind(texture
, gl_info
, srgb
, &dummy
);
40 /* Do not call while under the GL lock. */
41 static void volumetexture_preload(IWineD3DBaseTextureImpl
*texture
, enum WINED3DSRGB srgb
)
43 IWineD3DDeviceImpl
*device
= texture
->resource
.device
;
44 struct wined3d_context
*context
= NULL
;
45 BOOL srgb_mode
= texture
->baseTexture
.is_srgb
;
46 BOOL srgb_was_toggled
= FALSE
;
49 TRACE("texture %p, srgb %#x.\n", texture
, srgb
);
51 if (!device
->isInDraw
) context
= context_acquire(device
, NULL
);
52 else if (texture
->baseTexture
.bindCount
> 0)
54 srgb_mode
= device
->stateBlock
->state
.sampler_states
[texture
->baseTexture
.sampler
][WINED3DSAMP_SRGBTEXTURE
];
55 srgb_was_toggled
= texture
->baseTexture
.is_srgb
!= srgb_mode
;
56 texture
->baseTexture
.is_srgb
= srgb_mode
;
59 /* If the texture is marked dirty or the srgb sampler setting has changed
60 * since the last load then reload the volumes. */
61 if (texture
->baseTexture
.texture_rgb
.dirty
)
63 for (i
= 0; i
< texture
->baseTexture
.level_count
; ++i
)
65 volume_load(volume_from_resource(texture
->baseTexture
.sub_resources
[i
]), i
, srgb_mode
);
68 else if (srgb_was_toggled
)
70 for (i
= 0; i
< texture
->baseTexture
.level_count
; ++i
)
72 IWineD3DVolumeImpl
*volume
= volume_from_resource(texture
->baseTexture
.sub_resources
[i
]);
73 volume_add_dirty_box(volume
, NULL
);
74 volume_load(volume
, i
, srgb_mode
);
79 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
82 if (context
) context_release(context
);
85 texture
->baseTexture
.texture_rgb
.dirty
= FALSE
;
88 /* Do not call while under the GL lock. */
89 static void volumetexture_unload(struct wined3d_resource
*resource
)
91 IWineD3DBaseTextureImpl
*texture
= basetexture_from_resource(resource
);
94 TRACE("texture %p.\n", texture
);
96 for (i
= 0; i
< texture
->baseTexture
.level_count
; ++i
)
98 struct wined3d_resource
*sub_resource
= texture
->baseTexture
.sub_resources
[i
];
99 sub_resource
->resource_ops
->resource_unload(sub_resource
);
102 basetexture_unload(texture
);
105 static const struct wined3d_texture_ops volumetexture_ops
=
108 volumetexture_preload
,
111 static const struct wined3d_resource_ops volumetexture_resource_ops
=
113 volumetexture_unload
,
116 static void volumetexture_cleanup(IWineD3DVolumeTextureImpl
*This
)
120 TRACE("(%p) : Cleaning up.\n", This
);
122 for (i
= 0; i
< This
->baseTexture
.level_count
; ++i
)
124 struct wined3d_resource
*sub_resource
= This
->baseTexture
.sub_resources
[i
];
128 IWineD3DVolumeImpl
*volume
= volume_from_resource(sub_resource
);
130 /* Cleanup the container. */
131 volume_set_container(volume
, NULL
);
132 IWineD3DVolume_Release((IWineD3DVolume
*)volume
);
135 basetexture_cleanup((IWineD3DBaseTextureImpl
*)This
);
138 /* *******************************************
139 IWineD3DTexture IUnknown parts follow
140 ******************************************* */
142 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_QueryInterface(IWineD3DVolumeTexture
*iface
, REFIID riid
, LPVOID
*ppobj
)
144 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
145 TRACE("(%p)->(%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
146 if (IsEqualGUID(riid
, &IID_IUnknown
)
147 || IsEqualGUID(riid
, &IID_IWineD3DBase
)
148 || IsEqualGUID(riid
, &IID_IWineD3DResource
)
149 || IsEqualGUID(riid
, &IID_IWineD3DBaseTexture
)
150 || IsEqualGUID(riid
, &IID_IWineD3DVolumeTexture
)) {
151 IUnknown_AddRef(iface
);
156 return E_NOINTERFACE
;
159 static ULONG WINAPI
IWineD3DVolumeTextureImpl_AddRef(IWineD3DVolumeTexture
*iface
) {
160 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
161 TRACE("(%p) : AddRef increasing from %d\n", This
, This
->resource
.ref
);
162 return InterlockedIncrement(&This
->resource
.ref
);
165 /* Do not call while under the GL lock. */
166 static ULONG WINAPI
IWineD3DVolumeTextureImpl_Release(IWineD3DVolumeTexture
*iface
) {
167 IWineD3DVolumeTextureImpl
*This
= (IWineD3DVolumeTextureImpl
*)iface
;
169 TRACE("(%p) : Releasing from %d\n", This
, This
->resource
.ref
);
170 ref
= InterlockedDecrement(&This
->resource
.ref
);
173 volumetexture_cleanup(This
);
174 This
->resource
.parent_ops
->wined3d_object_destroyed(This
->resource
.parent
);
175 HeapFree(GetProcessHeap(), 0, This
);
180 /* ****************************************************
181 IWineD3DVolumeTexture IWineD3DResource parts follow
182 **************************************************** */
183 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_SetPrivateData(IWineD3DVolumeTexture
*iface
,
184 REFGUID riid
, const void *data
, DWORD data_size
, DWORD flags
)
186 return resource_set_private_data(&((IWineD3DVolumeTextureImpl
*)iface
)->resource
, riid
, data
, data_size
, flags
);
189 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_GetPrivateData(IWineD3DVolumeTexture
*iface
,
190 REFGUID guid
, void *data
, DWORD
*data_size
)
192 return resource_get_private_data(&((IWineD3DVolumeTextureImpl
*)iface
)->resource
, guid
, data
, data_size
);
195 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_FreePrivateData(IWineD3DVolumeTexture
*iface
, REFGUID refguid
)
197 return resource_free_private_data(&((IWineD3DVolumeTextureImpl
*)iface
)->resource
, refguid
);
200 static DWORD WINAPI
IWineD3DVolumeTextureImpl_SetPriority(IWineD3DVolumeTexture
*iface
, DWORD priority
)
202 return resource_set_priority(&((IWineD3DVolumeTextureImpl
*)iface
)->resource
, priority
);
205 static DWORD WINAPI
IWineD3DVolumeTextureImpl_GetPriority(IWineD3DVolumeTexture
*iface
)
207 return resource_get_priority(&((IWineD3DVolumeTextureImpl
*)iface
)->resource
);
210 static void WINAPI
IWineD3DVolumeTextureImpl_PreLoad(IWineD3DVolumeTexture
*iface
)
212 volumetexture_preload((IWineD3DBaseTextureImpl
*)iface
, SRGB_ANY
);
215 static WINED3DRESOURCETYPE WINAPI
IWineD3DVolumeTextureImpl_GetType(IWineD3DVolumeTexture
*iface
)
217 return resource_get_type(&((IWineD3DVolumeTextureImpl
*)iface
)->resource
);
220 static void * WINAPI
IWineD3DVolumeTextureImpl_GetParent(IWineD3DVolumeTexture
*iface
)
222 TRACE("iface %p\n", iface
);
224 return ((IWineD3DVolumeTextureImpl
*)iface
)->resource
.parent
;
227 /* ******************************************************
228 IWineD3DVolumeTexture IWineD3DBaseTexture parts follow
229 ****************************************************** */
230 static DWORD WINAPI
IWineD3DVolumeTextureImpl_SetLOD(IWineD3DVolumeTexture
*iface
, DWORD LODNew
) {
231 return basetexture_set_lod((IWineD3DBaseTextureImpl
*)iface
, LODNew
);
234 static DWORD WINAPI
IWineD3DVolumeTextureImpl_GetLOD(IWineD3DVolumeTexture
*iface
) {
235 return basetexture_get_lod((IWineD3DBaseTextureImpl
*)iface
);
238 static DWORD WINAPI
IWineD3DVolumeTextureImpl_GetLevelCount(IWineD3DVolumeTexture
*iface
)
240 return basetexture_get_level_count((IWineD3DBaseTextureImpl
*)iface
);
243 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_SetAutoGenFilterType(IWineD3DVolumeTexture
*iface
,
244 WINED3DTEXTUREFILTERTYPE FilterType
)
246 return basetexture_set_autogen_filter_type((IWineD3DBaseTextureImpl
*)iface
, FilterType
);
249 static WINED3DTEXTUREFILTERTYPE WINAPI
IWineD3DVolumeTextureImpl_GetAutoGenFilterType(IWineD3DVolumeTexture
*iface
)
251 return basetexture_get_autogen_filter_type((IWineD3DBaseTextureImpl
*)iface
);
254 static void WINAPI
IWineD3DVolumeTextureImpl_GenerateMipSubLevels(IWineD3DVolumeTexture
*iface
)
256 basetexture_generate_mipmaps((IWineD3DBaseTextureImpl
*)iface
);
259 static BOOL WINAPI
IWineD3DVolumeTextureImpl_IsCondNP2(IWineD3DVolumeTexture
*iface
)
261 TRACE("iface %p.\n", iface
);
266 static struct wined3d_resource
* WINAPI
IWineD3DVolumeTextureImpl_GetSubResource(IWineD3DVolumeTexture
*iface
,
267 UINT sub_resource_idx
)
269 IWineD3DBaseTextureImpl
*texture
= (IWineD3DBaseTextureImpl
*)iface
;
271 TRACE("iface %p, sub_resource_idx %u.\n", iface
, sub_resource_idx
);
273 return basetexture_get_sub_resource(texture
, sub_resource_idx
);
276 static HRESULT WINAPI
IWineD3DVolumeTextureImpl_AddDirtyBox(IWineD3DVolumeTexture
*iface
, const WINED3DBOX
*dirty_box
)
278 IWineD3DBaseTextureImpl
*texture
= (IWineD3DBaseTextureImpl
*)iface
;
279 struct wined3d_resource
*sub_resource
;
281 TRACE("iface %p, dirty_box %p.\n", iface
, dirty_box
);
283 if (!(sub_resource
= basetexture_get_sub_resource(texture
, 0)))
285 WARN("Failed to get sub-resource.\n");
286 return WINED3DERR_INVALIDCALL
;
289 basetexture_set_dirty(texture
, TRUE
);
290 volume_add_dirty_box(volume_from_resource(sub_resource
), dirty_box
);
295 static const IWineD3DVolumeTextureVtbl IWineD3DVolumeTexture_Vtbl
=
298 IWineD3DVolumeTextureImpl_QueryInterface
,
299 IWineD3DVolumeTextureImpl_AddRef
,
300 IWineD3DVolumeTextureImpl_Release
,
302 IWineD3DVolumeTextureImpl_GetParent
,
303 IWineD3DVolumeTextureImpl_SetPrivateData
,
304 IWineD3DVolumeTextureImpl_GetPrivateData
,
305 IWineD3DVolumeTextureImpl_FreePrivateData
,
306 IWineD3DVolumeTextureImpl_SetPriority
,
307 IWineD3DVolumeTextureImpl_GetPriority
,
308 IWineD3DVolumeTextureImpl_PreLoad
,
309 IWineD3DVolumeTextureImpl_GetType
,
311 IWineD3DVolumeTextureImpl_SetLOD
,
312 IWineD3DVolumeTextureImpl_GetLOD
,
313 IWineD3DVolumeTextureImpl_GetLevelCount
,
314 IWineD3DVolumeTextureImpl_SetAutoGenFilterType
,
315 IWineD3DVolumeTextureImpl_GetAutoGenFilterType
,
316 IWineD3DVolumeTextureImpl_GenerateMipSubLevels
,
317 IWineD3DVolumeTextureImpl_IsCondNP2
,
318 IWineD3DVolumeTextureImpl_GetSubResource
,
320 IWineD3DVolumeTextureImpl_AddDirtyBox
323 HRESULT
volumetexture_init(IWineD3DVolumeTextureImpl
*texture
, UINT width
, UINT height
,
324 UINT depth
, UINT levels
, IWineD3DDeviceImpl
*device
, DWORD usage
, enum wined3d_format_id format_id
,
325 WINED3DPOOL pool
, void *parent
, const struct wined3d_parent_ops
*parent_ops
)
327 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
328 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
);
329 UINT tmp_w
, tmp_h
, tmp_d
;
333 /* TODO: It should only be possible to create textures for formats
334 * that are reported as supported. */
335 if (WINED3DFMT_UNKNOWN
>= format_id
)
337 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture
);
338 return WINED3DERR_INVALIDCALL
;
341 if (!gl_info
->supported
[EXT_TEXTURE3D
])
343 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture
);
344 return WINED3DERR_INVALIDCALL
;
347 /* Calculate levels for mip mapping. */
348 if (usage
& WINED3DUSAGE_AUTOGENMIPMAP
)
350 if (!gl_info
->supported
[SGIS_GENERATE_MIPMAP
])
352 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
353 return WINED3DERR_INVALIDCALL
;
358 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
359 return WINED3DERR_INVALIDCALL
;
366 levels
= wined3d_log2i(max(max(width
, height
), depth
)) + 1;
367 TRACE("Calculated levels = %u.\n", levels
);
370 texture
->lpVtbl
= &IWineD3DVolumeTexture_Vtbl
;
372 hr
= basetexture_init((IWineD3DBaseTextureImpl
*)texture
, &volumetexture_ops
,
373 1, levels
, WINED3DRTYPE_VOLUMETEXTURE
, device
, usage
, format
, pool
,
374 parent
, parent_ops
, &volumetexture_resource_ops
);
377 WARN("Failed to initialize basetexture, returning %#x.\n", hr
);
381 /* Is NP2 support for volumes needed? */
382 texture
->baseTexture
.pow2Matrix
[0] = 1.0f
;
383 texture
->baseTexture
.pow2Matrix
[5] = 1.0f
;
384 texture
->baseTexture
.pow2Matrix
[10] = 1.0f
;
385 texture
->baseTexture
.pow2Matrix
[15] = 1.0f
;
386 texture
->baseTexture
.target
= GL_TEXTURE_3D
;
388 /* Generate all the surfaces. */
393 for (i
= 0; i
< texture
->baseTexture
.level_count
; ++i
)
395 IWineD3DVolume
*volume
;
397 /* Create the volume. */
398 hr
= IWineD3DDeviceParent_CreateVolume(device
->device_parent
, parent
,
399 tmp_w
, tmp_h
, tmp_d
, format_id
, pool
, usage
, &volume
);
402 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr
);
403 volumetexture_cleanup(texture
);
407 /* Set its container to this texture. */
408 volume_set_container((IWineD3DVolumeImpl
*)volume
, texture
);
409 texture
->baseTexture
.sub_resources
[i
] = &((IWineD3DVolumeImpl
*)volume
)->resource
;
411 /* Calculate the next mipmap level. */
412 tmp_w
= max(1, tmp_w
>> 1);
413 tmp_h
= max(1, tmp_h
>> 1);
414 tmp_d
= max(1, tmp_d
>> 1);