wined3d: Pass an IWineD3DBaseTextureImpl pointer to basetexture_get_lod().
[wine] / dlls / wined3d / volumetexture.c
1 /*
2  * IWineD3DVolumeTexture implementation
3  *
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
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include "config.h"
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
28
29 /* Do not call while under the GL lock. */
30 static void volumetexture_internal_preload(IWineD3DBaseTexture *iface, enum WINED3DSRGB srgb)
31 {
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;
39     unsigned int i;
40
41     TRACE("(%p) : About to load texture.\n", This);
42
43     if (!device->isInDraw) context = context_acquire(device, NULL);
44     else if (gl_info->supported[EXT_TEXTURE_SRGB] && This->baseTexture.bindCount > 0)
45     {
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;
49     }
50
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)
54     {
55         for (i = 0; i < This->baseTexture.level_count; ++i)
56         {
57             IWineD3DVolume *volume = (IWineD3DVolume *)This->baseTexture.sub_resources[i];
58             IWineD3DVolume_LoadTexture(volume, i, srgb_mode);
59         }
60     }
61     else if (srgb_was_toggled)
62     {
63         for (i = 0; i < This->baseTexture.level_count; ++i)
64         {
65             IWineD3DVolume *volume = (IWineD3DVolume *)This->baseTexture.sub_resources[i];
66             volume_add_dirty_box(volume, NULL);
67             IWineD3DVolume_LoadTexture(volume, i, srgb_mode);
68         }
69     }
70     else
71     {
72         TRACE("(%p) Texture not dirty, nothing to do.\n", iface);
73     }
74
75     if (context) context_release(context);
76
77     /* No longer dirty */
78     This->baseTexture.texture_rgb.dirty = FALSE;
79 }
80
81 static void volumetexture_cleanup(IWineD3DVolumeTextureImpl *This)
82 {
83     unsigned int i;
84
85     TRACE("(%p) : Cleaning up.\n", This);
86
87     for (i = 0; i < This->baseTexture.level_count; ++i)
88     {
89         IWineD3DVolumeImpl *volume = (IWineD3DVolumeImpl *)This->baseTexture.sub_resources[i];
90
91         if (volume)
92         {
93             /* Cleanup the container. */
94             volume_set_container(volume, NULL);
95             IWineD3DVolume_Release((IWineD3DVolume *)volume);
96         }
97     }
98     basetexture_cleanup((IWineD3DBaseTextureImpl *)This);
99 }
100
101 /* *******************************************
102    IWineD3DTexture IUnknown parts follow
103    ******************************************* */
104
105 static HRESULT WINAPI IWineD3DVolumeTextureImpl_QueryInterface(IWineD3DVolumeTexture *iface, REFIID riid, LPVOID *ppobj)
106 {
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);
115         *ppobj = This;
116         return S_OK;
117     }
118     *ppobj = NULL;
119     return E_NOINTERFACE;
120 }
121
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);
126 }
127
128 /* Do not call while under the GL lock. */
129 static ULONG WINAPI IWineD3DVolumeTextureImpl_Release(IWineD3DVolumeTexture *iface) {
130     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
131     ULONG ref;
132     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
133     ref = InterlockedDecrement(&This->resource.ref);
134     if (!ref)
135     {
136         volumetexture_cleanup(This);
137         This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
138         HeapFree(GetProcessHeap(), 0, This);
139     }
140     return ref;
141 }
142
143 /* ****************************************************
144    IWineD3DVolumeTexture IWineD3DResource parts follow
145    **************************************************** */
146 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetPrivateData(IWineD3DVolumeTexture *iface,
147         REFGUID riid, const void *data, DWORD data_size, DWORD flags)
148 {
149     return resource_set_private_data((IWineD3DResource *)iface, riid, data, data_size, flags);
150 }
151
152 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetPrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
153     return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
154 }
155
156 static HRESULT WINAPI IWineD3DVolumeTextureImpl_FreePrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid) {
157     return resource_free_private_data((IWineD3DResource *)iface, refguid);
158 }
159
160 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetPriority(IWineD3DVolumeTexture *iface, DWORD PriorityNew) {
161     return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
162 }
163
164 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetPriority(IWineD3DVolumeTexture *iface) {
165     return resource_get_priority((IWineD3DResource *)iface);
166 }
167
168 static void WINAPI IWineD3DVolumeTextureImpl_PreLoad(IWineD3DVolumeTexture *iface) {
169     volumetexture_internal_preload((IWineD3DBaseTexture *) iface, SRGB_ANY);
170 }
171
172 /* Do not call while under the GL lock. */
173 static void WINAPI IWineD3DVolumeTextureImpl_UnLoad(IWineD3DVolumeTexture *iface) {
174     unsigned int i;
175     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
176     TRACE("(%p)\n", This);
177
178     /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
179      * surface before, this one will be a NOP and vice versa. Unloading an unloaded
180      * surface is fine
181      */
182     for (i = 0; i < This->baseTexture.level_count; ++i)
183     {
184         IWineD3DVolume_UnLoad((IWineD3DVolume *)This->baseTexture.sub_resources[i]);
185     }
186
187     basetexture_unload((IWineD3DBaseTextureImpl *)This);
188 }
189
190 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeTextureImpl_GetType(IWineD3DVolumeTexture *iface) {
191     return resource_get_type((IWineD3DResource *)iface);
192 }
193
194 static void * WINAPI IWineD3DVolumeTextureImpl_GetParent(IWineD3DVolumeTexture *iface)
195 {
196     TRACE("iface %p\n", iface);
197
198     return ((IWineD3DVolumeTextureImpl *)iface)->resource.parent;
199 }
200
201 /* ******************************************************
202    IWineD3DVolumeTexture IWineD3DBaseTexture parts follow
203    ****************************************************** */
204 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetLOD(IWineD3DVolumeTexture *iface, DWORD LODNew) {
205     return basetexture_set_lod((IWineD3DBaseTextureImpl *)iface, LODNew);
206 }
207
208 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLOD(IWineD3DVolumeTexture *iface) {
209     return basetexture_get_lod((IWineD3DBaseTextureImpl *)iface);
210 }
211
212 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLevelCount(IWineD3DVolumeTexture *iface)
213 {
214     return basetexture_get_level_count((IWineD3DBaseTextureImpl *)iface);
215 }
216
217 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetAutoGenFilterType(IWineD3DVolumeTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
218   return basetexture_set_autogen_filter_type((IWineD3DBaseTexture *)iface, FilterType);
219 }
220
221 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DVolumeTextureImpl_GetAutoGenFilterType(IWineD3DVolumeTexture *iface) {
222   return basetexture_get_autogen_filter_type((IWineD3DBaseTexture *)iface);
223 }
224
225 static void WINAPI IWineD3DVolumeTextureImpl_GenerateMipSubLevels(IWineD3DVolumeTexture *iface) {
226     basetexture_generate_mipmaps((IWineD3DBaseTexture *)iface);
227 }
228
229 /* Context activation is done by the caller. */
230 static HRESULT WINAPI IWineD3DVolumeTextureImpl_BindTexture(IWineD3DVolumeTexture *iface, BOOL srgb)
231 {
232     BOOL dummy;
233
234     TRACE("iface %p, srgb %#x.\n", iface, srgb);
235
236     return basetexture_bind((IWineD3DBaseTexture *)iface, srgb, &dummy);
237 }
238
239 static BOOL WINAPI IWineD3DVolumeTextureImpl_IsCondNP2(IWineD3DVolumeTexture *iface)
240 {
241     TRACE("iface %p.\n", iface);
242
243     return FALSE;
244 }
245
246 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetLevelDesc(IWineD3DVolumeTexture *iface,
247         UINT sub_resource_idx, WINED3DVOLUME_DESC *desc)
248 {
249     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
250     IWineD3DVolume *volume;
251
252     TRACE("iface %p, sub_resource_idx %u, desc %p.\n", iface, sub_resource_idx, desc);
253
254     if (!(volume = (IWineD3DVolume *)basetexture_get_sub_resource(texture, sub_resource_idx)))
255     {
256         WARN("Failed to get sub-resource.\n");
257         return WINED3DERR_INVALIDCALL;
258     }
259
260     IWineD3DVolume_GetDesc(volume, desc);
261
262     return WINED3D_OK;
263 }
264
265 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetVolumeLevel(IWineD3DVolumeTexture *iface,
266         UINT sub_resource_idx, IWineD3DVolume **volume)
267 {
268     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
269     IWineD3DVolume *v;
270
271     TRACE("iface %p, sub_resource_idx %u, volume %p.\n", iface, sub_resource_idx, volume);
272
273     if (!(v= (IWineD3DVolume *)basetexture_get_sub_resource(texture, sub_resource_idx)))
274     {
275         WARN("Failed to get sub-resource.\n");
276         return WINED3DERR_INVALIDCALL;
277     }
278
279     IWineD3DVolume_AddRef(v);
280     *volume = v;
281
282     TRACE("Returning volume %p.\n", *volume);
283
284     return WINED3D_OK;
285 }
286
287 static HRESULT WINAPI IWineD3DVolumeTextureImpl_Map(IWineD3DVolumeTexture *iface,
288         UINT sub_resource_idx, WINED3DLOCKED_BOX *locked_box, const WINED3DBOX *box, DWORD flags)
289 {
290     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
291     IWineD3DVolume *volume;
292
293     TRACE("iface %p, sub_resource_idx %u, locked_box %p, box %p, flags %#x.\n",
294             iface, sub_resource_idx, locked_box, box, flags);
295
296     if (!(volume = (IWineD3DVolume *)basetexture_get_sub_resource(texture, sub_resource_idx)))
297     {
298         WARN("Failed to get sub-resource.\n");
299         return WINED3DERR_INVALIDCALL;
300     }
301
302     return IWineD3DVolume_Map(volume, locked_box, box, flags);
303 }
304
305 static HRESULT WINAPI IWineD3DVolumeTextureImpl_Unmap(IWineD3DVolumeTexture *iface, UINT sub_resource_idx)
306 {
307     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
308     IWineD3DVolume *volume;
309
310     TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
311
312     if (!(volume = (IWineD3DVolume *)basetexture_get_sub_resource(texture, sub_resource_idx)))
313     {
314         WARN("Failed to get sub-resource.\n");
315         return WINED3DERR_INVALIDCALL;
316     }
317
318     return IWineD3DVolume_Unmap(volume);
319 }
320
321 static HRESULT WINAPI IWineD3DVolumeTextureImpl_AddDirtyBox(IWineD3DVolumeTexture *iface, const WINED3DBOX *dirty_box)
322 {
323     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
324     IWineD3DVolume *volume;
325
326     TRACE("iface %p, dirty_box %p.\n", iface, dirty_box);
327
328     if (!(volume = (IWineD3DVolume *)basetexture_get_sub_resource(texture, 0)))
329     {
330         WARN("Failed to get sub-resource.\n");
331         return WINED3DERR_INVALIDCALL;
332     }
333
334     texture->baseTexture.texture_rgb.dirty = TRUE;
335     texture->baseTexture.texture_srgb.dirty = TRUE;
336     volume_add_dirty_box(volume, dirty_box);
337
338     return WINED3D_OK;
339 }
340
341 static const IWineD3DVolumeTextureVtbl IWineD3DVolumeTexture_Vtbl =
342 {
343     /* IUnknown */
344     IWineD3DVolumeTextureImpl_QueryInterface,
345     IWineD3DVolumeTextureImpl_AddRef,
346     IWineD3DVolumeTextureImpl_Release,
347     /* resource */
348     IWineD3DVolumeTextureImpl_GetParent,
349     IWineD3DVolumeTextureImpl_SetPrivateData,
350     IWineD3DVolumeTextureImpl_GetPrivateData,
351     IWineD3DVolumeTextureImpl_FreePrivateData,
352     IWineD3DVolumeTextureImpl_SetPriority,
353     IWineD3DVolumeTextureImpl_GetPriority,
354     IWineD3DVolumeTextureImpl_PreLoad,
355     IWineD3DVolumeTextureImpl_UnLoad,
356     IWineD3DVolumeTextureImpl_GetType,
357     /* BaseTexture */
358     IWineD3DVolumeTextureImpl_SetLOD,
359     IWineD3DVolumeTextureImpl_GetLOD,
360     IWineD3DVolumeTextureImpl_GetLevelCount,
361     IWineD3DVolumeTextureImpl_SetAutoGenFilterType,
362     IWineD3DVolumeTextureImpl_GetAutoGenFilterType,
363     IWineD3DVolumeTextureImpl_GenerateMipSubLevels,
364     /* not in d3d */
365     IWineD3DVolumeTextureImpl_BindTexture,
366     IWineD3DVolumeTextureImpl_IsCondNP2,
367     /* volume texture */
368     IWineD3DVolumeTextureImpl_GetLevelDesc,
369     IWineD3DVolumeTextureImpl_GetVolumeLevel,
370     IWineD3DVolumeTextureImpl_Map,
371     IWineD3DVolumeTextureImpl_Unmap,
372     IWineD3DVolumeTextureImpl_AddDirtyBox
373 };
374
375 HRESULT volumetexture_init(IWineD3DVolumeTextureImpl *texture, UINT width, UINT height,
376         UINT depth, UINT levels, IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id,
377         WINED3DPOOL pool, void *parent, const struct wined3d_parent_ops *parent_ops)
378 {
379     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
380     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
381     UINT tmp_w, tmp_h, tmp_d;
382     unsigned int i;
383     HRESULT hr;
384
385     /* TODO: It should only be possible to create textures for formats
386      * that are reported as supported. */
387     if (WINED3DFMT_UNKNOWN >= format_id)
388     {
389         WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
390         return WINED3DERR_INVALIDCALL;
391     }
392
393     if (!gl_info->supported[EXT_TEXTURE3D])
394     {
395         WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
396         return WINED3DERR_INVALIDCALL;
397     }
398
399     /* Calculate levels for mip mapping. */
400     if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
401     {
402         if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
403         {
404             WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
405             return WINED3DERR_INVALIDCALL;
406         }
407
408         if (levels > 1)
409         {
410             WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
411             return WINED3DERR_INVALIDCALL;
412         }
413
414         levels = 1;
415     }
416     else if (!levels)
417     {
418         levels = wined3d_log2i(max(max(width, height), depth)) + 1;
419         TRACE("Calculated levels = %u.\n", levels);
420     }
421
422     texture->lpVtbl = &IWineD3DVolumeTexture_Vtbl;
423
424     hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, 1, levels,
425             WINED3DRTYPE_VOLUMETEXTURE, device, usage, format, pool, parent, parent_ops);
426     if (FAILED(hr))
427     {
428         WARN("Failed to initialize basetexture, returning %#x.\n", hr);
429         return hr;
430     }
431
432     /* Is NP2 support for volumes needed? */
433     texture->baseTexture.pow2Matrix[0] = 1.0f;
434     texture->baseTexture.pow2Matrix[5] = 1.0f;
435     texture->baseTexture.pow2Matrix[10] = 1.0f;
436     texture->baseTexture.pow2Matrix[15] = 1.0f;
437     texture->baseTexture.target = GL_TEXTURE_3D;
438
439     /* Generate all the surfaces. */
440     tmp_w = width;
441     tmp_h = height;
442     tmp_d = depth;
443
444     for (i = 0; i < texture->baseTexture.level_count; ++i)
445     {
446         IWineD3DVolume *volume;
447
448         /* Create the volume. */
449         hr = IWineD3DDeviceParent_CreateVolume(device->device_parent, parent,
450                 tmp_w, tmp_h, tmp_d, format_id, pool, usage, &volume);
451         if (FAILED(hr))
452         {
453             ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
454             volumetexture_cleanup(texture);
455             return hr;
456         }
457
458         /* Set its container to this texture. */
459         volume_set_container((IWineD3DVolumeImpl *)volume, texture);
460         texture->baseTexture.sub_resources[i] = (IWineD3DResourceImpl *)volume;
461
462         /* Calculate the next mipmap level. */
463         tmp_w = max(1, tmp_w >> 1);
464         tmp_h = max(1, tmp_h >> 1);
465         tmp_d = max(1, tmp_d >> 1);
466     }
467     texture->baseTexture.internal_preload = volumetexture_internal_preload;
468
469     return WINED3D_OK;
470 }