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