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