mshtml: Initialize OLEINPLACEFRAMEINFO.cb for IOleInPlaceSite::GetWindowContext.
[wine] / dlls / wined3d / volumetexture.c
1 /*
2  * Copyright 2002-2005 Jason Edmeades
3  * Copyright 2002-2005 Raphael Junqueira
4  * Copyright 2005 Oliver Stieber
5  * Copyright 2009-2010 Henri Verbeet for CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wined3d_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
26
27 /* Context activation is done by the caller. */
28 static HRESULT volumetexture_bind(IWineD3DBaseTextureImpl *texture,
29         const struct wined3d_gl_info *gl_info, BOOL srgb)
30 {
31     BOOL dummy;
32
33     TRACE("texture %p, gl_info %p, srgb %#x.\n", texture, gl_info, srgb);
34
35     return basetexture_bind(texture, gl_info, srgb, &dummy);
36 }
37
38 /* Do not call while under the GL lock. */
39 static void volumetexture_preload(IWineD3DBaseTextureImpl *texture, enum WINED3DSRGB srgb)
40 {
41     IWineD3DDeviceImpl *device = texture->resource.device;
42     struct wined3d_context *context = NULL;
43     BOOL srgb_mode = texture->baseTexture.is_srgb;
44     BOOL srgb_was_toggled = FALSE;
45     unsigned int i;
46
47     TRACE("texture %p, srgb %#x.\n", texture, srgb);
48
49     if (!device->isInDraw) context = context_acquire(device, NULL);
50     else if (texture->baseTexture.bindCount > 0)
51     {
52         srgb_mode = device->stateBlock->state.sampler_states[texture->baseTexture.sampler][WINED3DSAMP_SRGBTEXTURE];
53         srgb_was_toggled = texture->baseTexture.is_srgb != srgb_mode;
54         texture->baseTexture.is_srgb = srgb_mode;
55     }
56
57     /* If the texture is marked dirty or the srgb sampler setting has changed
58      * since the last load then reload the volumes. */
59     if (texture->baseTexture.texture_rgb.dirty)
60     {
61         for (i = 0; i < texture->baseTexture.level_count; ++i)
62         {
63             volume_load(volume_from_resource(texture->baseTexture.sub_resources[i]), i, srgb_mode);
64         }
65     }
66     else if (srgb_was_toggled)
67     {
68         for (i = 0; i < texture->baseTexture.level_count; ++i)
69         {
70             IWineD3DVolumeImpl *volume = volume_from_resource(texture->baseTexture.sub_resources[i]);
71             volume_add_dirty_box(volume, NULL);
72             volume_load(volume, i, srgb_mode);
73         }
74     }
75     else
76     {
77         TRACE("Texture %p not dirty, nothing to do.\n", texture);
78     }
79
80     if (context) context_release(context);
81
82     /* No longer dirty */
83     texture->baseTexture.texture_rgb.dirty = FALSE;
84 }
85
86 static void volumetexture_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
87         const WINED3DBOX *dirty_region)
88 {
89     volume_add_dirty_box(volume_from_resource(sub_resource), dirty_region);
90 }
91
92 static void volumetexture_sub_resource_cleanup(struct wined3d_resource *sub_resource)
93 {
94     IWineD3DVolumeImpl *volume = volume_from_resource(sub_resource);
95
96     /* Cleanup the container. */
97     volume_set_container(volume, NULL);
98     IWineD3DVolume_Release((IWineD3DVolume *)volume);
99 }
100
101 /* Do not call while under the GL lock. */
102 static void volumetexture_unload(struct wined3d_resource *resource)
103 {
104     IWineD3DBaseTextureImpl *texture = basetexture_from_resource(resource);
105     unsigned int i;
106
107     TRACE("texture %p.\n", texture);
108
109     for (i = 0; i < texture->baseTexture.level_count; ++i)
110     {
111         struct wined3d_resource *sub_resource = texture->baseTexture.sub_resources[i];
112         sub_resource->resource_ops->resource_unload(sub_resource);
113     }
114
115     basetexture_unload(texture);
116 }
117
118 static const struct wined3d_texture_ops volumetexture_ops =
119 {
120     volumetexture_bind,
121     volumetexture_preload,
122     volumetexture_sub_resource_add_dirty_region,
123     volumetexture_sub_resource_cleanup,
124 };
125
126 static const struct wined3d_resource_ops volumetexture_resource_ops =
127 {
128     volumetexture_unload,
129 };
130
131 static HRESULT WINAPI IWineD3DVolumeTextureImpl_QueryInterface(IWineD3DBaseTexture *iface, REFIID riid, LPVOID *ppobj)
132 {
133     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
134     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
135     if (IsEqualGUID(riid, &IID_IUnknown)
136         || IsEqualGUID(riid, &IID_IWineD3DBase)
137         || IsEqualGUID(riid, &IID_IWineD3DResource)
138         || IsEqualGUID(riid, &IID_IWineD3DBaseTexture))
139     {
140         IUnknown_AddRef(iface);
141         *ppobj = This;
142         return S_OK;
143     }
144     *ppobj = NULL;
145     return E_NOINTERFACE;
146 }
147
148 static ULONG WINAPI IWineD3DVolumeTextureImpl_AddRef(IWineD3DBaseTexture *iface)
149 {
150     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
151     TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
152     return InterlockedIncrement(&This->resource.ref);
153 }
154
155 /* Do not call while under the GL lock. */
156 static ULONG WINAPI IWineD3DVolumeTextureImpl_Release(IWineD3DBaseTexture *iface)
157 {
158     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
159     ULONG ref;
160     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
161     ref = InterlockedDecrement(&This->resource.ref);
162     if (!ref)
163     {
164         basetexture_cleanup(This);
165         This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
166         HeapFree(GetProcessHeap(), 0, This);
167     }
168     return ref;
169 }
170
171 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetPrivateData(IWineD3DBaseTexture *iface,
172         REFGUID riid, const void *data, DWORD data_size, DWORD flags)
173 {
174     return resource_set_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, riid, data, data_size, flags);
175 }
176
177 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetPrivateData(IWineD3DBaseTexture *iface,
178         REFGUID guid, void *data, DWORD *data_size)
179 {
180     return resource_get_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, guid, data, data_size);
181 }
182
183 static HRESULT WINAPI IWineD3DVolumeTextureImpl_FreePrivateData(IWineD3DBaseTexture *iface, REFGUID refguid)
184 {
185     return resource_free_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, refguid);
186 }
187
188 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetPriority(IWineD3DBaseTexture *iface, DWORD priority)
189 {
190     return resource_set_priority(&((IWineD3DBaseTextureImpl *)iface)->resource, priority);
191 }
192
193 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetPriority(IWineD3DBaseTexture *iface)
194 {
195     return resource_get_priority(&((IWineD3DBaseTextureImpl *)iface)->resource);
196 }
197
198 static void WINAPI IWineD3DVolumeTextureImpl_PreLoad(IWineD3DBaseTexture *iface)
199 {
200     volumetexture_preload((IWineD3DBaseTextureImpl *)iface, SRGB_ANY);
201 }
202
203 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeTextureImpl_GetType(IWineD3DBaseTexture *iface)
204 {
205     return resource_get_type(&((IWineD3DBaseTextureImpl *)iface)->resource);
206 }
207
208 static void * WINAPI IWineD3DVolumeTextureImpl_GetParent(IWineD3DBaseTexture *iface)
209 {
210     TRACE("iface %p\n", iface);
211
212     return ((IWineD3DBaseTextureImpl *)iface)->resource.parent;
213 }
214
215 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetLOD(IWineD3DBaseTexture *iface, DWORD LODNew)
216 {
217     return basetexture_set_lod((IWineD3DBaseTextureImpl *)iface, LODNew);
218 }
219
220 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLOD(IWineD3DBaseTexture *iface)
221 {
222     return basetexture_get_lod((IWineD3DBaseTextureImpl *)iface);
223 }
224
225 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLevelCount(IWineD3DBaseTexture *iface)
226 {
227     return basetexture_get_level_count((IWineD3DBaseTextureImpl *)iface);
228 }
229
230 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetAutoGenFilterType(IWineD3DBaseTexture *iface,
231         WINED3DTEXTUREFILTERTYPE FilterType)
232 {
233   return basetexture_set_autogen_filter_type((IWineD3DBaseTextureImpl *)iface, FilterType);
234 }
235
236 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DVolumeTextureImpl_GetAutoGenFilterType(IWineD3DBaseTexture *iface)
237 {
238   return basetexture_get_autogen_filter_type((IWineD3DBaseTextureImpl *)iface);
239 }
240
241 static void WINAPI IWineD3DVolumeTextureImpl_GenerateMipSubLevels(IWineD3DBaseTexture *iface)
242 {
243     basetexture_generate_mipmaps((IWineD3DBaseTextureImpl *)iface);
244 }
245
246 static struct wined3d_resource * WINAPI IWineD3DVolumeTextureImpl_GetSubResource(IWineD3DBaseTexture *iface,
247         UINT sub_resource_idx)
248 {
249     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
250
251     TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
252
253     return basetexture_get_sub_resource(texture, sub_resource_idx);
254 }
255
256 static HRESULT WINAPI IWineD3DVolumeTextureImpl_AddDirtyRegion(IWineD3DBaseTexture *iface,
257         UINT layer, const WINED3DBOX *dirty_region)
258 {
259     return basetexture_add_dirty_region((IWineD3DBaseTextureImpl *)iface, layer, dirty_region);
260 }
261
262 static const IWineD3DBaseTextureVtbl IWineD3DVolumeTexture_Vtbl =
263 {
264     /* IUnknown */
265     IWineD3DVolumeTextureImpl_QueryInterface,
266     IWineD3DVolumeTextureImpl_AddRef,
267     IWineD3DVolumeTextureImpl_Release,
268     /* resource */
269     IWineD3DVolumeTextureImpl_GetParent,
270     IWineD3DVolumeTextureImpl_SetPrivateData,
271     IWineD3DVolumeTextureImpl_GetPrivateData,
272     IWineD3DVolumeTextureImpl_FreePrivateData,
273     IWineD3DVolumeTextureImpl_SetPriority,
274     IWineD3DVolumeTextureImpl_GetPriority,
275     IWineD3DVolumeTextureImpl_PreLoad,
276     IWineD3DVolumeTextureImpl_GetType,
277     /* BaseTexture */
278     IWineD3DVolumeTextureImpl_SetLOD,
279     IWineD3DVolumeTextureImpl_GetLOD,
280     IWineD3DVolumeTextureImpl_GetLevelCount,
281     IWineD3DVolumeTextureImpl_SetAutoGenFilterType,
282     IWineD3DVolumeTextureImpl_GetAutoGenFilterType,
283     IWineD3DVolumeTextureImpl_GenerateMipSubLevels,
284     IWineD3DVolumeTextureImpl_GetSubResource,
285     IWineD3DVolumeTextureImpl_AddDirtyRegion,
286 };
287
288 HRESULT volumetexture_init(IWineD3DBaseTextureImpl *texture, UINT width, UINT height,
289         UINT depth, UINT levels, IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id,
290         WINED3DPOOL pool, void *parent, const struct wined3d_parent_ops *parent_ops)
291 {
292     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
293     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
294     UINT tmp_w, tmp_h, tmp_d;
295     unsigned int i;
296     HRESULT hr;
297
298     /* TODO: It should only be possible to create textures for formats
299      * that are reported as supported. */
300     if (WINED3DFMT_UNKNOWN >= format_id)
301     {
302         WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
303         return WINED3DERR_INVALIDCALL;
304     }
305
306     if (!gl_info->supported[EXT_TEXTURE3D])
307     {
308         WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
309         return WINED3DERR_INVALIDCALL;
310     }
311
312     /* Calculate levels for mip mapping. */
313     if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
314     {
315         if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
316         {
317             WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
318             return WINED3DERR_INVALIDCALL;
319         }
320
321         if (levels > 1)
322         {
323             WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
324             return WINED3DERR_INVALIDCALL;
325         }
326
327         levels = 1;
328     }
329     else if (!levels)
330     {
331         levels = wined3d_log2i(max(max(width, height), depth)) + 1;
332         TRACE("Calculated levels = %u.\n", levels);
333     }
334
335     texture->lpVtbl = &IWineD3DVolumeTexture_Vtbl;
336
337     hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, &volumetexture_ops,
338             1, levels, WINED3DRTYPE_VOLUMETEXTURE, device, usage, format, pool,
339             parent, parent_ops, &volumetexture_resource_ops);
340     if (FAILED(hr))
341     {
342         WARN("Failed to initialize basetexture, returning %#x.\n", hr);
343         return hr;
344     }
345
346     /* Is NP2 support for volumes needed? */
347     texture->baseTexture.pow2Matrix[0] = 1.0f;
348     texture->baseTexture.pow2Matrix[5] = 1.0f;
349     texture->baseTexture.pow2Matrix[10] = 1.0f;
350     texture->baseTexture.pow2Matrix[15] = 1.0f;
351     texture->baseTexture.target = GL_TEXTURE_3D;
352
353     /* Generate all the surfaces. */
354     tmp_w = width;
355     tmp_h = height;
356     tmp_d = depth;
357
358     for (i = 0; i < texture->baseTexture.level_count; ++i)
359     {
360         IWineD3DVolume *volume;
361
362         /* Create the volume. */
363         hr = IWineD3DDeviceParent_CreateVolume(device->device_parent, parent,
364                 tmp_w, tmp_h, tmp_d, format_id, pool, usage, &volume);
365         if (FAILED(hr))
366         {
367             ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
368             basetexture_cleanup(texture);
369             return hr;
370         }
371
372         /* Set its container to this texture. */
373         volume_set_container((IWineD3DVolumeImpl *)volume, texture);
374         texture->baseTexture.sub_resources[i] = &((IWineD3DVolumeImpl *)volume)->resource;
375
376         /* Calculate the next mipmap level. */
377         tmp_w = max(1, tmp_w >> 1);
378         tmp_h = max(1, tmp_h >> 1);
379         tmp_d = max(1, tmp_d >> 1);
380     }
381
382     return WINED3D_OK;
383 }