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