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