wined3d: Update Nvidia Geforce6+ driver version.
[wine] / dlls / wined3d / texture.c
1 /*
2  * IWineD3DTexture 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 /* Context activation is done by the caller. */
31 static HRESULT texture_bind(IWineD3DBaseTextureImpl *texture,
32         const struct wined3d_gl_info *gl_info, BOOL srgb)
33 {
34     BOOL set_gl_texture_desc;
35     HRESULT hr;
36
37     TRACE("texture %p, gl_info %p, srgb %#x.\n", texture, gl_info, srgb);
38
39     hr = basetexture_bind(texture, gl_info, srgb, &set_gl_texture_desc);
40     if (set_gl_texture_desc && SUCCEEDED(hr))
41     {
42         BOOL srgb_tex = !gl_info->supported[EXT_TEXTURE_SRGB_DECODE] && texture->baseTexture.is_srgb;
43         struct gl_texture *gl_tex;
44         UINT i;
45
46         gl_tex = basetexture_get_gl_texture(texture, gl_info, srgb_tex);
47
48         for (i = 0; i < texture->baseTexture.level_count; ++i)
49         {
50             IWineD3DSurfaceImpl *surface = surface_from_resource(texture->baseTexture.sub_resources[i]);
51             surface_set_texture_name(surface, gl_tex->name, srgb_tex);
52         }
53
54         /* Conditinal non power of two textures use a different clamping
55          * default. If we're using the GL_WINE_normalized_texrect partial
56          * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
57          * has the address mode set to repeat - something that prevents us
58          * from hitting the accelerated codepath. Thus manually set the GL
59          * state. The same applies to filtering. Even if the texture has only
60          * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
61          * fallback on macos. */
62         if (IWineD3DBaseTexture_IsCondNP2((IWineD3DBaseTexture *)texture))
63         {
64             GLenum target = texture->baseTexture.target;
65
66             ENTER_GL();
67             glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
68             checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
69             glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
70             checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
71             glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
72             checkGLcall("glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
73             glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
74             checkGLcall("glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
75             LEAVE_GL();
76             gl_tex->states[WINED3DTEXSTA_ADDRESSU]      = WINED3DTADDRESS_CLAMP;
77             gl_tex->states[WINED3DTEXSTA_ADDRESSV]      = WINED3DTADDRESS_CLAMP;
78             gl_tex->states[WINED3DTEXSTA_MAGFILTER]     = WINED3DTEXF_POINT;
79             gl_tex->states[WINED3DTEXSTA_MINFILTER]     = WINED3DTEXF_POINT;
80             gl_tex->states[WINED3DTEXSTA_MIPFILTER]     = WINED3DTEXF_NONE;
81         }
82     }
83
84     return hr;
85 }
86
87 /* Do not call while under the GL lock. */
88 static void texture_preload(IWineD3DBaseTextureImpl *texture, enum WINED3DSRGB srgb)
89 {
90     IWineD3DDeviceImpl *device = texture->resource.device;
91     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
92     struct wined3d_context *context = NULL;
93     struct gl_texture *gl_tex;
94     unsigned int i;
95     BOOL srgb_mode;
96
97     TRACE("texture %p, srgb %#x.\n", texture, srgb);
98
99     switch (srgb)
100     {
101         case SRGB_RGB:
102             srgb_mode = FALSE;
103             break;
104
105         case SRGB_BOTH:
106             texture_preload(texture, SRGB_RGB);
107             /* Fallthrough */
108
109         case SRGB_SRGB:
110             srgb_mode = TRUE;
111             break;
112
113         default:
114             srgb_mode = texture->baseTexture.is_srgb;
115             break;
116     }
117
118     gl_tex = basetexture_get_gl_texture(texture, gl_info, srgb_mode);
119
120     if (!device->isInDraw)
121     {
122         /* context_acquire() sets isInDraw to TRUE when loading a pbuffer into a texture,
123          * thus no danger of recursive calls. */
124         context = context_acquire(device, NULL);
125     }
126
127     if (texture->resource.format->id == WINED3DFMT_P8_UINT
128             || texture->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
129     {
130         for (i = 0; i < texture->baseTexture.level_count; ++i)
131         {
132             IWineD3DSurfaceImpl *surface = surface_from_resource(texture->baseTexture.sub_resources[i]);
133             if (palette9_changed(surface))
134             {
135                 TRACE("Reloading surface because the d3d8/9 palette was changed.\n");
136                 /* TODO: This is not necessarily needed with hw palettized texture support. */
137                 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
138                 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
139                 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
140             }
141         }
142     }
143
144     /* If the texture is marked dirty or the srgb sampler setting has changed
145      * since the last load then reload the surfaces. */
146     if (gl_tex->dirty)
147     {
148         for (i = 0; i < texture->baseTexture.level_count; ++i)
149         {
150             surface_load(surface_from_resource(texture->baseTexture.sub_resources[i]), srgb_mode);
151         }
152     }
153     else
154     {
155         TRACE("Texture %p not dirty, nothing to do.\n", texture);
156     }
157
158     if (context) context_release(context);
159
160     /* No longer dirty. */
161     gl_tex->dirty = FALSE;
162 }
163
164 /* Do not call while under the GL lock. */
165 static void texture_unload(struct wined3d_resource *resource)
166 {
167     IWineD3DBaseTextureImpl *texture = basetexture_from_resource(resource);
168     unsigned int i;
169
170     TRACE("texture %p.\n", texture);
171
172     for (i = 0; i < texture->baseTexture.level_count; ++i)
173     {
174         struct wined3d_resource *sub_resource = texture->baseTexture.sub_resources[i];
175         IWineD3DSurfaceImpl *surface = surface_from_resource(sub_resource);
176
177         sub_resource->resource_ops->resource_unload(sub_resource);
178         surface_set_texture_name(surface, 0, FALSE); /* Delete rgb name */
179         surface_set_texture_name(surface, 0, TRUE); /* delete srgb name */
180     }
181
182     basetexture_unload(texture);
183 }
184
185 static const struct wined3d_texture_ops texture_ops =
186 {
187     texture_bind,
188     texture_preload,
189 };
190
191 static const struct wined3d_resource_ops texture_resource_ops =
192 {
193     texture_unload,
194 };
195
196 static void texture_cleanup(IWineD3DTextureImpl *This)
197 {
198     unsigned int i;
199
200     TRACE("(%p) : Cleaning up\n", This);
201
202     for (i = 0; i < This->baseTexture.level_count; ++i)
203     {
204         struct wined3d_resource *sub_resource = This->baseTexture.sub_resources[i];
205
206         if (sub_resource)
207         {
208             IWineD3DSurfaceImpl *surface = surface_from_resource(sub_resource);
209
210             /* Clean out the texture name we gave to the surface so that the
211              * surface doesn't try and release it */
212             surface_set_texture_name(surface, 0, TRUE);
213             surface_set_texture_name(surface, 0, FALSE);
214             surface_set_texture_target(surface, 0);
215             surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
216             IWineD3DSurface_Release((IWineD3DSurface *)surface);
217         }
218     }
219
220     TRACE("(%p) : Cleaning up base texture\n", This);
221     basetexture_cleanup((IWineD3DBaseTextureImpl *)This);
222 }
223
224 /* *******************************************
225    IWineD3DTexture IUnknown parts follow
226    ******************************************* */
227
228 static HRESULT WINAPI IWineD3DTextureImpl_QueryInterface(IWineD3DTexture *iface, REFIID riid, LPVOID *ppobj)
229 {
230     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
231     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
232     if (IsEqualGUID(riid, &IID_IUnknown)
233         || IsEqualGUID(riid, &IID_IWineD3DBase)
234         || IsEqualGUID(riid, &IID_IWineD3DResource)
235         || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
236         || IsEqualGUID(riid, &IID_IWineD3DTexture)){
237         IUnknown_AddRef(iface);
238         *ppobj = This;
239         return WINED3D_OK;
240     }
241     *ppobj = NULL;
242     return E_NOINTERFACE;
243 }
244
245 static ULONG WINAPI IWineD3DTextureImpl_AddRef(IWineD3DTexture *iface) {
246     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
247     TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
248     return InterlockedIncrement(&This->resource.ref);
249 }
250
251 /* Do not call while under the GL lock. */
252 static ULONG WINAPI IWineD3DTextureImpl_Release(IWineD3DTexture *iface) {
253     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
254     ULONG ref;
255     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
256     ref = InterlockedDecrement(&This->resource.ref);
257     if (!ref)
258     {
259         texture_cleanup(This);
260         This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
261         HeapFree(GetProcessHeap(), 0, This);
262     }
263     return ref;
264 }
265
266
267 /* ****************************************************
268    IWineD3DTexture IWineD3DResource parts follow
269    **************************************************** */
270 static HRESULT WINAPI IWineD3DTextureImpl_SetPrivateData(IWineD3DTexture *iface,
271         REFGUID riid, const void *data, DWORD data_size, DWORD flags)
272 {
273     return resource_set_private_data(&((IWineD3DTextureImpl *)iface)->resource, riid, data, data_size, flags);
274 }
275
276 static HRESULT WINAPI IWineD3DTextureImpl_GetPrivateData(IWineD3DTexture *iface,
277         REFGUID guid, void *data, DWORD *data_size)
278 {
279     return resource_get_private_data(&((IWineD3DTextureImpl *)iface)->resource, guid, data, data_size);
280 }
281
282 static HRESULT WINAPI IWineD3DTextureImpl_FreePrivateData(IWineD3DTexture *iface, REFGUID refguid)
283 {
284     return resource_free_private_data(&((IWineD3DTextureImpl *)iface)->resource, refguid);
285 }
286
287 static DWORD WINAPI IWineD3DTextureImpl_SetPriority(IWineD3DTexture *iface, DWORD priority)
288 {
289     return resource_set_priority(&((IWineD3DTextureImpl *)iface)->resource, priority);
290 }
291
292 static DWORD WINAPI IWineD3DTextureImpl_GetPriority(IWineD3DTexture *iface)
293 {
294     return resource_get_priority(&((IWineD3DTextureImpl *)iface)->resource);
295 }
296
297 /* Do not call while under the GL lock. */
298 static void WINAPI IWineD3DTextureImpl_PreLoad(IWineD3DTexture *iface)
299 {
300     texture_preload((IWineD3DBaseTextureImpl *)iface, SRGB_ANY);
301 }
302
303 static WINED3DRESOURCETYPE WINAPI IWineD3DTextureImpl_GetType(IWineD3DTexture *iface)
304 {
305     return resource_get_type(&((IWineD3DTextureImpl *)iface)->resource);
306 }
307
308 static void * WINAPI IWineD3DTextureImpl_GetParent(IWineD3DTexture *iface)
309 {
310     TRACE("iface %p.\n", iface);
311
312     return ((IWineD3DTextureImpl *)iface)->resource.parent;
313 }
314
315 /* ******************************************************
316    IWineD3DTexture IWineD3DBaseTexture parts follow
317    ****************************************************** */
318 static DWORD WINAPI IWineD3DTextureImpl_SetLOD(IWineD3DTexture *iface, DWORD LODNew) {
319     return basetexture_set_lod((IWineD3DBaseTextureImpl *)iface, LODNew);
320 }
321
322 static DWORD WINAPI IWineD3DTextureImpl_GetLOD(IWineD3DTexture *iface) {
323     return basetexture_get_lod((IWineD3DBaseTextureImpl *)iface);
324 }
325
326 static DWORD WINAPI IWineD3DTextureImpl_GetLevelCount(IWineD3DTexture *iface)
327 {
328     return basetexture_get_level_count((IWineD3DBaseTextureImpl *)iface);
329 }
330
331 static HRESULT WINAPI IWineD3DTextureImpl_SetAutoGenFilterType(IWineD3DTexture *iface,
332         WINED3DTEXTUREFILTERTYPE FilterType)
333 {
334   return basetexture_set_autogen_filter_type((IWineD3DBaseTextureImpl *)iface, FilterType);
335 }
336
337 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DTextureImpl_GetAutoGenFilterType(IWineD3DTexture *iface)
338 {
339   return basetexture_get_autogen_filter_type((IWineD3DBaseTextureImpl *)iface);
340 }
341
342 static void WINAPI IWineD3DTextureImpl_GenerateMipSubLevels(IWineD3DTexture *iface)
343 {
344     basetexture_generate_mipmaps((IWineD3DBaseTextureImpl *)iface);
345 }
346
347 static BOOL WINAPI IWineD3DTextureImpl_IsCondNP2(IWineD3DTexture *iface) {
348     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
349     TRACE("(%p)\n", This);
350
351     return This->cond_np2;
352 }
353
354 static struct wined3d_resource * WINAPI IWineD3DTextureImpl_GetSubResource(IWineD3DTexture *iface,
355         UINT sub_resource_idx)
356 {
357     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
358
359     TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
360
361     return basetexture_get_sub_resource(texture, sub_resource_idx);
362 }
363
364 static HRESULT WINAPI IWineD3DTextureImpl_AddDirtyRegion(IWineD3DTexture *iface,
365         UINT layer, const WINED3DBOX *dirty_region)
366 {
367     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
368     struct wined3d_resource *sub_resource;
369
370     TRACE("iface %p, layer %u, dirty_region %p.\n", iface, layer, dirty_region);
371
372     if (!(sub_resource = basetexture_get_sub_resource(texture, layer * texture->baseTexture.level_count)))
373     {
374         WARN("Failed to get sub-resource.\n");
375         return WINED3DERR_INVALIDCALL;
376     }
377
378     basetexture_set_dirty(texture, TRUE);
379     surface_add_dirty_rect(surface_from_resource(sub_resource), dirty_region);
380
381     return WINED3D_OK;
382 }
383
384 static const IWineD3DTextureVtbl IWineD3DTexture_Vtbl =
385 {
386     /* IUnknown */
387     IWineD3DTextureImpl_QueryInterface,
388     IWineD3DTextureImpl_AddRef,
389     IWineD3DTextureImpl_Release,
390     /* IWineD3DResource */
391     IWineD3DTextureImpl_GetParent,
392     IWineD3DTextureImpl_SetPrivateData,
393     IWineD3DTextureImpl_GetPrivateData,
394     IWineD3DTextureImpl_FreePrivateData,
395     IWineD3DTextureImpl_SetPriority,
396     IWineD3DTextureImpl_GetPriority,
397     IWineD3DTextureImpl_PreLoad,
398     IWineD3DTextureImpl_GetType,
399     /* IWineD3DBaseTexture */
400     IWineD3DTextureImpl_SetLOD,
401     IWineD3DTextureImpl_GetLOD,
402     IWineD3DTextureImpl_GetLevelCount,
403     IWineD3DTextureImpl_SetAutoGenFilterType,
404     IWineD3DTextureImpl_GetAutoGenFilterType,
405     IWineD3DTextureImpl_GenerateMipSubLevels,
406     IWineD3DTextureImpl_IsCondNP2,
407     IWineD3DTextureImpl_GetSubResource,
408     IWineD3DTextureImpl_AddDirtyRegion,
409 };
410
411 HRESULT texture_init(IWineD3DTextureImpl *texture, UINT width, UINT height, UINT levels,
412         IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
413         void *parent, const struct wined3d_parent_ops *parent_ops)
414 {
415     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
416     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
417     UINT pow2_width, pow2_height;
418     UINT tmp_w, tmp_h;
419     unsigned int i;
420     HRESULT hr;
421
422     /* TODO: It should only be possible to create textures for formats
423      * that are reported as supported. */
424     if (WINED3DFMT_UNKNOWN >= format_id)
425     {
426         WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
427         return WINED3DERR_INVALIDCALL;
428     }
429
430     /* Non-power2 support. */
431     if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
432     {
433         pow2_width = width;
434         pow2_height = height;
435     }
436     else
437     {
438         /* Find the nearest pow2 match. */
439         pow2_width = pow2_height = 1;
440         while (pow2_width < width) pow2_width <<= 1;
441         while (pow2_height < height) pow2_height <<= 1;
442
443         if (pow2_width != width || pow2_height != height)
444         {
445             if (levels > 1)
446             {
447                 WARN("Attempted to create a mipmapped np2 texture without unconditional np2 support.\n");
448                 return WINED3DERR_INVALIDCALL;
449             }
450             levels = 1;
451         }
452     }
453
454     /* Calculate levels for mip mapping. */
455     if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
456     {
457         if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
458         {
459             WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
460             return WINED3DERR_INVALIDCALL;
461         }
462
463         if (levels > 1)
464         {
465             WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
466             return WINED3DERR_INVALIDCALL;
467         }
468
469         levels = 1;
470     }
471     else if (!levels)
472     {
473         levels = wined3d_log2i(max(width, height)) + 1;
474         TRACE("Calculated levels = %u.\n", levels);
475     }
476
477     texture->lpVtbl = &IWineD3DTexture_Vtbl;
478
479     hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, &texture_ops,
480             1, levels, WINED3DRTYPE_TEXTURE, device, usage, format, pool,
481             parent, parent_ops, &texture_resource_ops);
482     if (FAILED(hr))
483     {
484         WARN("Failed to initialize basetexture, returning %#x.\n", hr);
485         return hr;
486     }
487
488     /* Precalculated scaling for 'faked' non power of two texture coords.
489      * Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
490      * is used in combination with texture uploads (RTL_READTEX). The reason is that EXT_PALETTED_TEXTURE
491      * doesn't work in combination with ARB_TEXTURE_RECTANGLE. */
492     if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT] && (width != pow2_width || height != pow2_height))
493     {
494         texture->baseTexture.pow2Matrix[0] = 1.0f;
495         texture->baseTexture.pow2Matrix[5] = 1.0f;
496         texture->baseTexture.pow2Matrix[10] = 1.0f;
497         texture->baseTexture.pow2Matrix[15] = 1.0f;
498         texture->baseTexture.target = GL_TEXTURE_2D;
499         texture->cond_np2 = TRUE;
500         texture->baseTexture.minMipLookup = minMipLookup_noFilter;
501     }
502     else if (gl_info->supported[ARB_TEXTURE_RECTANGLE] && (width != pow2_width || height != pow2_height)
503             && !(format->id == WINED3DFMT_P8_UINT && gl_info->supported[EXT_PALETTED_TEXTURE]
504             && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
505     {
506         if ((width != 1) || (height != 1)) texture->baseTexture.pow2Matrix_identity = FALSE;
507
508         texture->baseTexture.pow2Matrix[0] = (float)width;
509         texture->baseTexture.pow2Matrix[5] = (float)height;
510         texture->baseTexture.pow2Matrix[10] = 1.0f;
511         texture->baseTexture.pow2Matrix[15] = 1.0f;
512         texture->baseTexture.target = GL_TEXTURE_RECTANGLE_ARB;
513         texture->cond_np2 = TRUE;
514
515         if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
516         {
517             texture->baseTexture.minMipLookup = minMipLookup_noMip;
518         }
519         else
520         {
521             texture->baseTexture.minMipLookup = minMipLookup_noFilter;
522         }
523     }
524     else
525     {
526         if ((width != pow2_width) || (height != pow2_height))
527         {
528             texture->baseTexture.pow2Matrix_identity = FALSE;
529             texture->baseTexture.pow2Matrix[0] = (((float)width) / ((float)pow2_width));
530             texture->baseTexture.pow2Matrix[5] = (((float)height) / ((float)pow2_height));
531         }
532         else
533         {
534             texture->baseTexture.pow2Matrix[0] = 1.0f;
535             texture->baseTexture.pow2Matrix[5] = 1.0f;
536         }
537
538         texture->baseTexture.pow2Matrix[10] = 1.0f;
539         texture->baseTexture.pow2Matrix[15] = 1.0f;
540         texture->baseTexture.target = GL_TEXTURE_2D;
541         texture->cond_np2 = FALSE;
542     }
543     TRACE("xf(%f) yf(%f)\n", texture->baseTexture.pow2Matrix[0], texture->baseTexture.pow2Matrix[5]);
544
545     /* Generate all the surfaces. */
546     tmp_w = width;
547     tmp_h = height;
548     for (i = 0; i < texture->baseTexture.level_count; ++i)
549     {
550         IWineD3DSurface *surface;
551
552         /* Use the callback to create the texture surface. */
553         hr = IWineD3DDeviceParent_CreateSurface(device->device_parent, parent, tmp_w, tmp_h,
554                 format->id, usage, pool, i, 0, &surface);
555         if (FAILED(hr))
556         {
557             FIXME("Failed to create surface %p, hr %#x\n", texture, hr);
558             texture_cleanup(texture);
559             return hr;
560         }
561
562         surface_set_container((IWineD3DSurfaceImpl *)surface, WINED3D_CONTAINER_TEXTURE, (IWineD3DBase *)texture);
563         surface_set_texture_target((IWineD3DSurfaceImpl *)surface, texture->baseTexture.target);
564         texture->baseTexture.sub_resources[i] = &((IWineD3DSurfaceImpl *)surface)->resource;
565         TRACE("Created surface level %u @ %p.\n", i, surface);
566         /* Calculate the next mipmap level. */
567         tmp_w = max(1, tmp_w >> 1);
568         tmp_h = max(1, tmp_h >> 1);
569     }
570
571     return WINED3D_OK;
572 }