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