mshtml: Implement IHTMLDOMNode previousSibling.
[wine] / dlls / wined3d / cubetexture.c
1 /*
2  * Copyright 2002-2005 Jason Edmeades
3  * Copyright 2002-2005 Raphael Junqueira
4  * Copyright 2005 Oliver Stieber
5  * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6  * Copyright 2009-2010 Henri Verbeet for CodeWeavers
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wined3d_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
27
28 /* Context activation is done by the caller. */
29 static HRESULT cubetexture_bind(IWineD3DBaseTextureImpl *texture,
30         const struct wined3d_gl_info *gl_info, BOOL srgb)
31 {
32     BOOL set_gl_texture_desc;
33     HRESULT hr;
34
35     TRACE("texture %p, gl_info %p, srgb %#x.\n", texture, gl_info, srgb);
36
37     hr = basetexture_bind(texture, gl_info, srgb, &set_gl_texture_desc);
38     if (set_gl_texture_desc && SUCCEEDED(hr))
39     {
40         UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
41         BOOL srgb_tex = !gl_info->supported[EXT_TEXTURE_SRGB_DECODE] && texture->baseTexture.is_srgb;
42         GLuint name = srgb_tex ? texture->baseTexture.texture_srgb.name : texture->baseTexture.texture_rgb.name;
43         UINT i;
44
45         for (i = 0; i < sub_count; ++i)
46         {
47             IWineD3DSurfaceImpl *surface = surface_from_resource(texture->baseTexture.sub_resources[i]);
48             surface_set_texture_name(surface, name, srgb_tex);
49         }
50     }
51
52     return hr;
53 }
54
55 /* Do not call while under the GL lock. */
56 static void cubetexture_preload(IWineD3DBaseTextureImpl *texture, enum WINED3DSRGB srgb)
57 {
58     UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
59     IWineD3DDeviceImpl *device = texture->resource.device;
60     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
61     struct wined3d_context *context = NULL;
62     struct gl_texture *gl_tex;
63     BOOL srgb_mode;
64     UINT i;
65
66     TRACE("texture %p, srgb %#x.\n", texture, srgb);
67
68     switch (srgb)
69     {
70         case SRGB_RGB:
71             srgb_mode = FALSE;
72             break;
73
74         case SRGB_BOTH:
75             cubetexture_preload(texture, SRGB_RGB);
76             /* Fallthrough */
77
78         case SRGB_SRGB:
79             srgb_mode = TRUE;
80             break;
81
82         default:
83             srgb_mode = texture->baseTexture.is_srgb;
84             break;
85     }
86
87     gl_tex = basetexture_get_gl_texture(texture, gl_info, srgb_mode);
88
89     /* We only have to activate a context for gl when we're not drawing.
90      * In most cases PreLoad will be called during draw and a context was
91      * activated at the beginning of drawPrimitive. */
92     if (!device->isInDraw)
93     {
94         /* No danger of recursive calls, context_acquire() sets isInDraw to true
95          * when loading offscreen render targets into their texture. */
96         context = context_acquire(device, NULL);
97     }
98
99     if (texture->resource.format->id == WINED3DFMT_P8_UINT
100             || texture->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
101     {
102         for (i = 0; i < sub_count; ++i)
103         {
104             IWineD3DSurfaceImpl *surface = surface_from_resource(texture->baseTexture.sub_resources[i]);
105
106             if (palette9_changed(surface))
107             {
108                 TRACE("Reloading surface %p because the d3d8/9 palette was changed.\n", surface);
109                 /* TODO: This is not necessarily needed with hw palettized texture support. */
110                 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
111                 /* Make sure the texture is reloaded because of the palette change,
112                  * this kills performance though :( */
113                 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
114             }
115         }
116     }
117
118     /* If the texture is marked dirty or the srgb sampler setting has changed
119      * since the last load then reload the surfaces. */
120     if (gl_tex->dirty)
121     {
122         for (i = 0; i < sub_count; ++i)
123         {
124             surface_load(surface_from_resource(texture->baseTexture.sub_resources[i]), srgb_mode);
125         }
126     }
127     else
128     {
129         TRACE("Texture %p not dirty, nothing to do.\n" , texture);
130     }
131
132     /* No longer dirty. */
133     gl_tex->dirty = FALSE;
134
135     if (context) context_release(context);
136 }
137
138 static void cubetexture_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
139         const WINED3DBOX *dirty_region)
140 {
141     surface_add_dirty_rect(surface_from_resource(sub_resource), dirty_region);
142 }
143
144 static void cubetexture_sub_resource_cleanup(struct wined3d_resource *sub_resource)
145 {
146     IWineD3DSurfaceImpl *surface = surface_from_resource(sub_resource);
147
148     /* Clean out the texture name we gave to the surface so that the
149      * surface doesn't try and release it. */
150     surface_set_texture_name(surface, 0, TRUE);
151     surface_set_texture_name(surface, 0, FALSE);
152     surface_set_texture_target(surface, 0);
153     surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
154     IWineD3DSurface_Release((IWineD3DSurface *)surface);
155 }
156
157 /* Do not call while under the GL lock. */
158 static void cubetexture_unload(struct wined3d_resource *resource)
159 {
160     IWineD3DBaseTextureImpl *texture = basetexture_from_resource(resource);
161     UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
162     UINT i;
163
164     TRACE("texture %p.\n", texture);
165
166     for (i = 0; i < sub_count; ++i)
167     {
168         struct wined3d_resource *sub_resource = texture->baseTexture.sub_resources[i];
169         IWineD3DSurfaceImpl *surface = surface_from_resource(sub_resource);
170
171         sub_resource->resource_ops->resource_unload(sub_resource);
172         surface_set_texture_name(surface, 0, TRUE);
173         surface_set_texture_name(surface, 0, FALSE);
174     }
175
176     basetexture_unload(texture);
177 }
178
179 static const struct wined3d_texture_ops cubetexture_ops =
180 {
181     cubetexture_bind,
182     cubetexture_preload,
183     cubetexture_sub_resource_add_dirty_region,
184     cubetexture_sub_resource_cleanup,
185 };
186
187 static const struct wined3d_resource_ops cubetexture_resource_ops =
188 {
189     cubetexture_unload,
190 };
191
192 static HRESULT WINAPI IWineD3DCubeTextureImpl_QueryInterface(IWineD3DBaseTexture *iface, REFIID riid, LPVOID *ppobj)
193 {
194     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
195     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
196     if (IsEqualGUID(riid, &IID_IUnknown)
197         || IsEqualGUID(riid, &IID_IWineD3DBase)
198         || IsEqualGUID(riid, &IID_IWineD3DResource)
199         || IsEqualGUID(riid, &IID_IWineD3DBaseTexture))
200     {
201         IUnknown_AddRef(iface);
202         *ppobj = This;
203         return S_OK;
204     }
205     *ppobj = NULL;
206     return E_NOINTERFACE;
207 }
208
209 static ULONG WINAPI IWineD3DCubeTextureImpl_AddRef(IWineD3DBaseTexture *iface)
210 {
211     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
212     TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
213     return InterlockedIncrement(&This->resource.ref);
214 }
215
216 /* Do not call while under the GL lock. */
217 static ULONG WINAPI IWineD3DCubeTextureImpl_Release(IWineD3DBaseTexture *iface)
218 {
219     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
220     ULONG ref;
221     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
222     ref = InterlockedDecrement(&This->resource.ref);
223     if (!ref)
224     {
225         basetexture_cleanup(This);
226         This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
227         HeapFree(GetProcessHeap(), 0, This);
228     }
229     return ref;
230 }
231
232 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetPrivateData(IWineD3DBaseTexture *iface,
233         REFGUID riid, const void *data, DWORD data_size, DWORD flags)
234 {
235     return resource_set_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, riid, data, data_size, flags);
236 }
237
238 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetPrivateData(IWineD3DBaseTexture *iface,
239         REFGUID guid, void *data, DWORD *data_size)
240 {
241     return resource_get_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, guid, data, data_size);
242 }
243
244 static HRESULT WINAPI IWineD3DCubeTextureImpl_FreePrivateData(IWineD3DBaseTexture *iface, REFGUID refguid)
245 {
246     return resource_free_private_data(&((IWineD3DBaseTextureImpl *)iface)->resource, refguid);
247 }
248
249 static DWORD WINAPI IWineD3DCubeTextureImpl_SetPriority(IWineD3DBaseTexture *iface, DWORD priority)
250 {
251     return resource_set_priority(&((IWineD3DBaseTextureImpl *)iface)->resource, priority);
252 }
253
254 static DWORD WINAPI IWineD3DCubeTextureImpl_GetPriority(IWineD3DBaseTexture *iface)
255 {
256     return resource_get_priority(&((IWineD3DBaseTextureImpl *)iface)->resource);
257 }
258
259 /* Do not call while under the GL lock. */
260 static void WINAPI IWineD3DCubeTextureImpl_PreLoad(IWineD3DBaseTexture *iface)
261 {
262     cubetexture_preload((IWineD3DBaseTextureImpl *)iface, SRGB_ANY);
263 }
264
265 static WINED3DRESOURCETYPE WINAPI IWineD3DCubeTextureImpl_GetType(IWineD3DBaseTexture *iface)
266 {
267     return resource_get_type(&((IWineD3DBaseTextureImpl *)iface)->resource);
268 }
269
270 static void * WINAPI IWineD3DCubeTextureImpl_GetParent(IWineD3DBaseTexture *iface)
271 {
272     TRACE("iface %p.\n", iface);
273
274     return ((IWineD3DBaseTextureImpl *)iface)->resource.parent;
275 }
276
277 static DWORD WINAPI IWineD3DCubeTextureImpl_SetLOD(IWineD3DBaseTexture *iface, DWORD LODNew)
278 {
279     return basetexture_set_lod((IWineD3DBaseTextureImpl *)iface, LODNew);
280 }
281
282 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLOD(IWineD3DBaseTexture *iface)
283 {
284     return basetexture_get_lod((IWineD3DBaseTextureImpl *)iface);
285 }
286
287 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLevelCount(IWineD3DBaseTexture *iface)
288 {
289     return basetexture_get_level_count((IWineD3DBaseTextureImpl *)iface);
290 }
291
292 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetAutoGenFilterType(IWineD3DBaseTexture *iface,
293         WINED3DTEXTUREFILTERTYPE FilterType)
294 {
295   return basetexture_set_autogen_filter_type((IWineD3DBaseTextureImpl *)iface, FilterType);
296 }
297
298 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DCubeTextureImpl_GetAutoGenFilterType(IWineD3DBaseTexture *iface)
299 {
300   return basetexture_get_autogen_filter_type((IWineD3DBaseTextureImpl *)iface);
301 }
302
303 static void WINAPI IWineD3DCubeTextureImpl_GenerateMipSubLevels(IWineD3DBaseTexture *iface)
304 {
305     basetexture_generate_mipmaps((IWineD3DBaseTextureImpl *)iface);
306 }
307
308 static struct wined3d_resource * WINAPI IWineD3DCubeTextureImpl_GetSubResource(IWineD3DBaseTexture *iface,
309         UINT sub_resource_idx)
310 {
311     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
312
313     TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
314
315     return basetexture_get_sub_resource(texture, sub_resource_idx);
316 }
317
318 static HRESULT WINAPI IWineD3DCubeTextureImpl_AddDirtyRegion(IWineD3DBaseTexture *iface,
319         UINT layer, const WINED3DBOX *dirty_region)
320 {
321     return basetexture_add_dirty_region((IWineD3DBaseTextureImpl *)iface, layer, dirty_region);
322 }
323
324 static const IWineD3DBaseTextureVtbl IWineD3DCubeTexture_Vtbl =
325 {
326     /* IUnknown */
327     IWineD3DCubeTextureImpl_QueryInterface,
328     IWineD3DCubeTextureImpl_AddRef,
329     IWineD3DCubeTextureImpl_Release,
330     /* IWineD3DResource */
331     IWineD3DCubeTextureImpl_GetParent,
332     IWineD3DCubeTextureImpl_SetPrivateData,
333     IWineD3DCubeTextureImpl_GetPrivateData,
334     IWineD3DCubeTextureImpl_FreePrivateData,
335     IWineD3DCubeTextureImpl_SetPriority,
336     IWineD3DCubeTextureImpl_GetPriority,
337     IWineD3DCubeTextureImpl_PreLoad,
338     IWineD3DCubeTextureImpl_GetType,
339     /* IWineD3DBaseTexture */
340     IWineD3DCubeTextureImpl_SetLOD,
341     IWineD3DCubeTextureImpl_GetLOD,
342     IWineD3DCubeTextureImpl_GetLevelCount,
343     IWineD3DCubeTextureImpl_SetAutoGenFilterType,
344     IWineD3DCubeTextureImpl_GetAutoGenFilterType,
345     IWineD3DCubeTextureImpl_GenerateMipSubLevels,
346     IWineD3DCubeTextureImpl_GetSubResource,
347     IWineD3DCubeTextureImpl_AddDirtyRegion,
348 };
349
350 HRESULT cubetexture_init(IWineD3DBaseTextureImpl *texture, UINT edge_length, UINT levels,
351         IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
352         void *parent, const struct wined3d_parent_ops *parent_ops)
353 {
354     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
355     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
356     UINT pow2_edge_length;
357     unsigned int i, j;
358     UINT tmp_w;
359     HRESULT hr;
360
361     /* TODO: It should only be possible to create textures for formats
362      * that are reported as supported. */
363     if (WINED3DFMT_UNKNOWN >= format_id)
364     {
365         WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
366         return WINED3DERR_INVALIDCALL;
367     }
368
369     if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && pool != WINED3DPOOL_SCRATCH)
370     {
371         WARN("(%p) : Tried to create not supported cube texture.\n", texture);
372         return WINED3DERR_INVALIDCALL;
373     }
374
375     /* Calculate levels for mip mapping */
376     if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
377     {
378         if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
379         {
380             WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
381             return WINED3DERR_INVALIDCALL;
382         }
383
384         if (levels > 1)
385         {
386             WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
387             return WINED3DERR_INVALIDCALL;
388         }
389
390         levels = 1;
391     }
392     else if (!levels)
393     {
394         levels = wined3d_log2i(edge_length) + 1;
395         TRACE("Calculated levels = %u.\n", levels);
396     }
397
398     texture->lpVtbl = &IWineD3DCubeTexture_Vtbl;
399
400     hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, &cubetexture_ops,
401             6, levels, WINED3DRTYPE_CUBETEXTURE, device, usage, format, pool,
402             parent, parent_ops, &cubetexture_resource_ops);
403     if (FAILED(hr))
404     {
405         WARN("Failed to initialize basetexture, returning %#x\n", hr);
406         return hr;
407     }
408
409     /* Find the nearest pow2 match. */
410     pow2_edge_length = 1;
411     while (pow2_edge_length < edge_length) pow2_edge_length <<= 1;
412
413     if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || (edge_length == pow2_edge_length))
414     {
415         /* Precalculated scaling for 'faked' non power of two texture coords. */
416         texture->baseTexture.pow2Matrix[0] = 1.0f;
417         texture->baseTexture.pow2Matrix[5] = 1.0f;
418         texture->baseTexture.pow2Matrix[10] = 1.0f;
419         texture->baseTexture.pow2Matrix[15] = 1.0f;
420     }
421     else
422     {
423         /* Precalculated scaling for 'faked' non power of two texture coords. */
424         texture->baseTexture.pow2Matrix[0] = ((float)edge_length) / ((float)pow2_edge_length);
425         texture->baseTexture.pow2Matrix[5] = ((float)edge_length) / ((float)pow2_edge_length);
426         texture->baseTexture.pow2Matrix[10] = ((float)edge_length) / ((float)pow2_edge_length);
427         texture->baseTexture.pow2Matrix[15] = 1.0f;
428         texture->baseTexture.pow2Matrix_identity = FALSE;
429     }
430     texture->baseTexture.target = GL_TEXTURE_CUBE_MAP_ARB;
431
432     /* Generate all the surfaces. */
433     tmp_w = edge_length;
434     for (i = 0; i < texture->baseTexture.level_count; ++i)
435     {
436         /* Create the 6 faces. */
437         for (j = 0; j < 6; ++j)
438         {
439             static const GLenum cube_targets[6] =
440             {
441                 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
442                 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
443                 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
444                 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
445                 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
446                 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
447             };
448             UINT idx = j * texture->baseTexture.level_count + i;
449             IWineD3DSurface *surface;
450
451             hr = IWineD3DDeviceParent_CreateSurface(device->device_parent, parent, tmp_w, tmp_w,
452                     format_id, usage, pool, i /* Level */, j, &surface);
453             if (FAILED(hr))
454             {
455                 FIXME("(%p) Failed to create surface, hr %#x.\n", texture, hr);
456                 basetexture_cleanup(texture);
457                 return hr;
458             }
459
460             surface_set_container((IWineD3DSurfaceImpl *)surface, WINED3D_CONTAINER_TEXTURE, (IWineD3DBase *)texture);
461             surface_set_texture_target((IWineD3DSurfaceImpl *)surface, cube_targets[j]);
462             texture->baseTexture.sub_resources[idx] = &((IWineD3DSurfaceImpl *)surface)->resource;
463             TRACE("Created surface level %u @ %p.\n", i, surface);
464         }
465         tmp_w = max(1, tmp_w >> 1);
466     }
467
468     return WINED3D_OK;
469 }