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