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 struct 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, NULL, 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 struct 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 /* Is NP2 support for volumes needed? */
154 texture->baseTexture.pow2Matrix[0] = 1.0f;
155 texture->baseTexture.pow2Matrix[5] = 1.0f;
156 texture->baseTexture.pow2Matrix[10] = 1.0f;
157 texture->baseTexture.pow2Matrix[15] = 1.0f;
159 /* Generate all the surfaces. */
164 for (i = 0; i < texture->baseTexture.levels; ++i)
166 /* Create the volume. */
167 hr = IWineD3DDeviceParent_CreateVolume(device->device_parent, parent,
168 tmp_w, tmp_h, tmp_d, format, pool, usage, &texture->volumes[i]);
171 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
172 texture->volumes[i] = NULL;
173 volumetexture_cleanup(texture, D3DCB_DefaultDestroyVolume);
177 /* Set its container to this texture. */
178 IWineD3DVolume_SetContainer(texture->volumes[i], (IWineD3DBase *)texture);
180 /* Calculate the next mipmap level. */
181 tmp_w = max(1, tmp_w >> 1);
182 tmp_h = max(1, tmp_h >> 1);
183 tmp_d = max(1, tmp_d >> 1);
185 texture->baseTexture.internal_preload = volumetexture_internal_preload;
190 #undef GLINFO_LOCATION
192 /* *******************************************
193 IWineD3DTexture IUnknown parts follow
194 ******************************************* */
196 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
198 static HRESULT WINAPI IWineD3DVolumeTextureImpl_QueryInterface(IWineD3DVolumeTexture *iface, REFIID riid, LPVOID *ppobj)
200 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
201 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
202 if (IsEqualGUID(riid, &IID_IUnknown)
203 || IsEqualGUID(riid, &IID_IWineD3DBase)
204 || IsEqualGUID(riid, &IID_IWineD3DResource)
205 || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
206 || IsEqualGUID(riid, &IID_IWineD3DVolumeTexture)) {
207 IUnknown_AddRef(iface);
212 return E_NOINTERFACE;
215 static ULONG WINAPI IWineD3DVolumeTextureImpl_AddRef(IWineD3DVolumeTexture *iface) {
216 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
217 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
218 return InterlockedIncrement(&This->resource.ref);
221 static ULONG WINAPI IWineD3DVolumeTextureImpl_Release(IWineD3DVolumeTexture *iface) {
222 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
224 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
225 ref = InterlockedDecrement(&This->resource.ref);
227 IWineD3DVolumeTexture_Destroy(iface, D3DCB_DefaultDestroyVolume);
232 /* ****************************************************
233 IWineD3DVolumeTexture IWineD3DResource parts follow
234 **************************************************** */
235 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetDevice(IWineD3DVolumeTexture *iface, IWineD3DDevice** ppDevice) {
236 return resource_get_device((IWineD3DResource *)iface, ppDevice);
239 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetPrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
240 return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
243 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetPrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
244 return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
247 static HRESULT WINAPI IWineD3DVolumeTextureImpl_FreePrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid) {
248 return resource_free_private_data((IWineD3DResource *)iface, refguid);
251 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetPriority(IWineD3DVolumeTexture *iface, DWORD PriorityNew) {
252 return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
255 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetPriority(IWineD3DVolumeTexture *iface) {
256 return resource_get_priority((IWineD3DResource *)iface);
259 static void WINAPI IWineD3DVolumeTextureImpl_PreLoad(IWineD3DVolumeTexture *iface) {
260 volumetexture_internal_preload((IWineD3DBaseTexture *) iface, SRGB_ANY);
263 static void WINAPI IWineD3DVolumeTextureImpl_UnLoad(IWineD3DVolumeTexture *iface) {
265 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
266 TRACE("(%p)\n", This);
268 /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
269 * surface before, this one will be a NOP and vice versa. Unloading an unloaded
272 for (i = 0; i < This->baseTexture.levels; i++) {
273 IWineD3DVolume_UnLoad(This->volumes[i]);
276 basetexture_unload((IWineD3DBaseTexture *)iface);
279 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeTextureImpl_GetType(IWineD3DVolumeTexture *iface) {
280 return resource_get_type((IWineD3DResource *)iface);
283 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetParent(IWineD3DVolumeTexture *iface, IUnknown **pParent) {
284 return resource_get_parent((IWineD3DResource *)iface, pParent);
287 /* ******************************************************
288 IWineD3DVolumeTexture IWineD3DBaseTexture parts follow
289 ****************************************************** */
290 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetLOD(IWineD3DVolumeTexture *iface, DWORD LODNew) {
291 return basetexture_set_lod((IWineD3DBaseTexture *)iface, LODNew);
294 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLOD(IWineD3DVolumeTexture *iface) {
295 return basetexture_get_lod((IWineD3DBaseTexture *)iface);
298 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLevelCount(IWineD3DVolumeTexture *iface) {
299 return basetexture_get_level_count((IWineD3DBaseTexture *)iface);
302 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetAutoGenFilterType(IWineD3DVolumeTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
303 return basetexture_set_autogen_filter_type((IWineD3DBaseTexture *)iface, FilterType);
306 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DVolumeTextureImpl_GetAutoGenFilterType(IWineD3DVolumeTexture *iface) {
307 return basetexture_get_autogen_filter_type((IWineD3DBaseTexture *)iface);
310 static void WINAPI IWineD3DVolumeTextureImpl_GenerateMipSubLevels(IWineD3DVolumeTexture *iface) {
311 basetexture_generate_mipmaps((IWineD3DBaseTexture *)iface);
314 /* Internal function, No d3d mapping */
315 static BOOL WINAPI IWineD3DVolumeTextureImpl_SetDirty(IWineD3DVolumeTexture *iface, BOOL dirty) {
316 return basetexture_set_dirty((IWineD3DBaseTexture *)iface, dirty);
319 static BOOL WINAPI IWineD3DVolumeTextureImpl_GetDirty(IWineD3DVolumeTexture *iface) {
320 return basetexture_get_dirty((IWineD3DBaseTexture *)iface);
323 /* Context activation is done by the caller. */
324 static HRESULT WINAPI IWineD3DVolumeTextureImpl_BindTexture(IWineD3DVolumeTexture *iface, BOOL srgb) {
325 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
327 TRACE("(%p) : relay to BaseTexture\n", This);
328 return basetexture_bind((IWineD3DBaseTexture *)iface, srgb, &dummy);
331 static UINT WINAPI IWineD3DVolumeTextureImpl_GetTextureDimensions(IWineD3DVolumeTexture *iface) {
332 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
333 TRACE("(%p)\n", This);
334 return GL_TEXTURE_3D;
337 static BOOL WINAPI IWineD3DVolumeTextureImpl_IsCondNP2(IWineD3DVolumeTexture *iface) {
338 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
339 TRACE("(%p)\n", This);
344 /* *******************************************
345 IWineD3DVolumeTexture IWineD3DVolumeTexture parts follow
346 ******************************************* */
347 static void WINAPI IWineD3DVolumeTextureImpl_Destroy(IWineD3DVolumeTexture *iface, D3DCB_DESTROYVOLUMEFN D3DCB_DestroyVolume) {
348 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
350 volumetexture_cleanup(This, D3DCB_DestroyVolume);
352 HeapFree(GetProcessHeap(), 0, This);
355 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetLevelDesc(IWineD3DVolumeTexture *iface, UINT Level,WINED3DVOLUME_DESC *pDesc) {
356 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
357 if (Level < This->baseTexture.levels) {
358 TRACE("(%p) Level (%d)\n", This, Level);
359 return IWineD3DVolume_GetDesc(This->volumes[Level], pDesc);
361 WARN("(%p) Level (%d)\n", This, Level);
365 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetVolumeLevel(IWineD3DVolumeTexture *iface, UINT Level, IWineD3DVolume** ppVolumeLevel) {
366 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
367 if (Level < This->baseTexture.levels) {
368 *ppVolumeLevel = This->volumes[Level];
369 IWineD3DVolume_AddRef(*ppVolumeLevel);
370 TRACE("(%p) -> level(%d) returning volume@%p\n", This, Level, *ppVolumeLevel);
372 WARN("(%p) Level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
373 return WINED3DERR_INVALIDCALL;
378 static HRESULT WINAPI IWineD3DVolumeTextureImpl_LockBox(IWineD3DVolumeTexture *iface, UINT Level, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
380 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
382 if (Level < This->baseTexture.levels) {
383 hr = IWineD3DVolume_LockBox(This->volumes[Level], pLockedVolume, pBox, Flags);
384 TRACE("(%p) Level (%d) success(%u)\n", This, Level, hr);
387 FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
388 return WINED3DERR_INVALIDCALL;
393 static HRESULT WINAPI IWineD3DVolumeTextureImpl_UnlockBox(IWineD3DVolumeTexture *iface, UINT Level) {
395 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
397 if (Level < This->baseTexture.levels) {
398 hr = IWineD3DVolume_UnlockBox(This->volumes[Level]);
399 TRACE("(%p) -> level(%d) success(%u)\n", This, Level, hr);
402 FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
403 return WINED3DERR_INVALIDCALL;
408 static HRESULT WINAPI IWineD3DVolumeTextureImpl_AddDirtyBox(IWineD3DVolumeTexture *iface, CONST WINED3DBOX* pDirtyBox) {
409 IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
410 This->baseTexture.dirty = TRUE;
411 TRACE("(%p) : dirtyfication of volume Level (0)\n", This);
412 volume_add_dirty_box(This->volumes[0], pDirtyBox);
417 const IWineD3DVolumeTextureVtbl IWineD3DVolumeTexture_Vtbl =
420 IWineD3DVolumeTextureImpl_QueryInterface,
421 IWineD3DVolumeTextureImpl_AddRef,
422 IWineD3DVolumeTextureImpl_Release,
424 IWineD3DVolumeTextureImpl_GetParent,
425 IWineD3DVolumeTextureImpl_GetDevice,
426 IWineD3DVolumeTextureImpl_SetPrivateData,
427 IWineD3DVolumeTextureImpl_GetPrivateData,
428 IWineD3DVolumeTextureImpl_FreePrivateData,
429 IWineD3DVolumeTextureImpl_SetPriority,
430 IWineD3DVolumeTextureImpl_GetPriority,
431 IWineD3DVolumeTextureImpl_PreLoad,
432 IWineD3DVolumeTextureImpl_UnLoad,
433 IWineD3DVolumeTextureImpl_GetType,
435 IWineD3DVolumeTextureImpl_SetLOD,
436 IWineD3DVolumeTextureImpl_GetLOD,
437 IWineD3DVolumeTextureImpl_GetLevelCount,
438 IWineD3DVolumeTextureImpl_SetAutoGenFilterType,
439 IWineD3DVolumeTextureImpl_GetAutoGenFilterType,
440 IWineD3DVolumeTextureImpl_GenerateMipSubLevels,
441 IWineD3DVolumeTextureImpl_SetDirty,
442 IWineD3DVolumeTextureImpl_GetDirty,
444 IWineD3DVolumeTextureImpl_BindTexture,
445 IWineD3DVolumeTextureImpl_GetTextureDimensions,
446 IWineD3DVolumeTextureImpl_IsCondNP2,
448 IWineD3DVolumeTextureImpl_Destroy,
449 IWineD3DVolumeTextureImpl_GetLevelDesc,
450 IWineD3DVolumeTextureImpl_GetVolumeLevel,
451 IWineD3DVolumeTextureImpl_LockBox,
452 IWineD3DVolumeTextureImpl_UnlockBox,
453 IWineD3DVolumeTextureImpl_AddDirtyBox