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