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