wined3d: Store all the resource desc information in struct wined3d_resource.
[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 HRESULT WINAPI IWineD3DTextureImpl_GetSubResourceDesc(IWineD3DTexture *iface,
355         UINT sub_resource_idx, struct wined3d_resource_desc *desc)
356 {
357     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
358     struct wined3d_resource *sub_resource;
359
360     TRACE("iface %p, sub_resource_idx %u, desc %p.\n", iface, sub_resource_idx, desc);
361
362     if (!(sub_resource = basetexture_get_sub_resource(texture, sub_resource_idx)))
363     {
364         WARN("Failed to get sub-resource.\n");
365         return WINED3DERR_INVALIDCALL;
366     }
367
368     IWineD3DSurface_GetDesc((IWineD3DSurface *)surface_from_resource(sub_resource), desc);
369
370     return WINED3D_OK;
371 }
372
373 static HRESULT WINAPI IWineD3DTextureImpl_GetSurfaceLevel(IWineD3DTexture *iface,
374         UINT sub_resource_idx, IWineD3DSurface **surface)
375 {
376     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
377     struct wined3d_resource *sub_resource;
378
379     TRACE("iface %p, sub_resource_idx %u, surface %p.\n", iface, sub_resource_idx, surface);
380
381     if (!(sub_resource = basetexture_get_sub_resource(texture, sub_resource_idx)))
382     {
383         WARN("Failed to get sub-resource.\n");
384         return WINED3DERR_INVALIDCALL;
385     }
386
387     *surface = (IWineD3DSurface *)surface_from_resource(sub_resource);
388     IWineD3DSurface_AddRef(*surface);
389
390     TRACE("Returning surface %p.\n", *surface);
391
392     return WINED3D_OK;
393 }
394
395 static HRESULT WINAPI IWineD3DTextureImpl_Map(IWineD3DTexture *iface,
396         UINT sub_resource_idx, WINED3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
397 {
398     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
399     struct wined3d_resource *sub_resource;
400
401     TRACE("iface %p, sub_resource_idx %u, locked_rect %p, rect %s, flags %#x.\n",
402             iface, sub_resource_idx, locked_rect, wine_dbgstr_rect(rect), flags);
403
404     if (!(sub_resource = basetexture_get_sub_resource(texture, sub_resource_idx)))
405     {
406         WARN("Failed to get sub-resource.\n");
407         return WINED3DERR_INVALIDCALL;
408     }
409
410     return IWineD3DSurface_Map((IWineD3DSurface *)surface_from_resource(sub_resource), locked_rect, rect, flags);
411 }
412
413 static HRESULT WINAPI IWineD3DTextureImpl_Unmap(IWineD3DTexture *iface, UINT sub_resource_idx)
414 {
415     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
416     struct wined3d_resource *sub_resource;
417
418     TRACE("iface %p, sub_resource_idx %u.\n", iface, sub_resource_idx);
419
420     if (!(sub_resource = basetexture_get_sub_resource(texture, sub_resource_idx)))
421     {
422         WARN("Failed to get sub-resource.\n");
423         return WINED3DERR_INVALIDCALL;
424     }
425
426     return IWineD3DSurface_Unmap((IWineD3DSurface *)surface_from_resource(sub_resource));
427 }
428
429 static HRESULT WINAPI IWineD3DTextureImpl_AddDirtyRect(IWineD3DTexture *iface, const RECT *dirty_rect)
430 {
431     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
432     struct wined3d_resource *sub_resource;
433
434     TRACE("iface %p, dirty_rect %s.\n", iface, wine_dbgstr_rect(dirty_rect));
435
436     if (!(sub_resource = basetexture_get_sub_resource(texture, 0)))
437     {
438         WARN("Failed to get sub-resource.\n");
439         return WINED3DERR_INVALIDCALL;
440     }
441
442     basetexture_set_dirty(texture, TRUE);
443     surface_add_dirty_rect(surface_from_resource(sub_resource), dirty_rect);
444
445     return WINED3D_OK;
446 }
447
448 static const IWineD3DTextureVtbl IWineD3DTexture_Vtbl =
449 {
450     /* IUnknown */
451     IWineD3DTextureImpl_QueryInterface,
452     IWineD3DTextureImpl_AddRef,
453     IWineD3DTextureImpl_Release,
454     /* IWineD3DResource */
455     IWineD3DTextureImpl_GetParent,
456     IWineD3DTextureImpl_SetPrivateData,
457     IWineD3DTextureImpl_GetPrivateData,
458     IWineD3DTextureImpl_FreePrivateData,
459     IWineD3DTextureImpl_SetPriority,
460     IWineD3DTextureImpl_GetPriority,
461     IWineD3DTextureImpl_PreLoad,
462     IWineD3DTextureImpl_GetType,
463     /* IWineD3DBaseTexture */
464     IWineD3DTextureImpl_SetLOD,
465     IWineD3DTextureImpl_GetLOD,
466     IWineD3DTextureImpl_GetLevelCount,
467     IWineD3DTextureImpl_SetAutoGenFilterType,
468     IWineD3DTextureImpl_GetAutoGenFilterType,
469     IWineD3DTextureImpl_GenerateMipSubLevels,
470     IWineD3DTextureImpl_IsCondNP2,
471     IWineD3DTextureImpl_GetSubResourceDesc,
472     /* IWineD3DTexture */
473     IWineD3DTextureImpl_GetSurfaceLevel,
474     IWineD3DTextureImpl_Map,
475     IWineD3DTextureImpl_Unmap,
476     IWineD3DTextureImpl_AddDirtyRect
477 };
478
479 HRESULT texture_init(IWineD3DTextureImpl *texture, UINT width, UINT height, UINT levels,
480         IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
481         void *parent, const struct wined3d_parent_ops *parent_ops)
482 {
483     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
484     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
485     UINT pow2_width, pow2_height;
486     UINT tmp_w, tmp_h;
487     unsigned int i;
488     HRESULT hr;
489
490     /* TODO: It should only be possible to create textures for formats
491      * that are reported as supported. */
492     if (WINED3DFMT_UNKNOWN >= format_id)
493     {
494         WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
495         return WINED3DERR_INVALIDCALL;
496     }
497
498     /* Non-power2 support. */
499     if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
500     {
501         pow2_width = width;
502         pow2_height = height;
503     }
504     else
505     {
506         /* Find the nearest pow2 match. */
507         pow2_width = pow2_height = 1;
508         while (pow2_width < width) pow2_width <<= 1;
509         while (pow2_height < height) pow2_height <<= 1;
510
511         if (pow2_width != width || pow2_height != height)
512         {
513             if (levels > 1)
514             {
515                 WARN("Attempted to create a mipmapped np2 texture without unconditional np2 support.\n");
516                 return WINED3DERR_INVALIDCALL;
517             }
518             levels = 1;
519         }
520     }
521
522     /* Calculate levels for mip mapping. */
523     if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
524     {
525         if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
526         {
527             WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
528             return WINED3DERR_INVALIDCALL;
529         }
530
531         if (levels > 1)
532         {
533             WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
534             return WINED3DERR_INVALIDCALL;
535         }
536
537         levels = 1;
538     }
539     else if (!levels)
540     {
541         levels = wined3d_log2i(max(width, height)) + 1;
542         TRACE("Calculated levels = %u.\n", levels);
543     }
544
545     texture->lpVtbl = &IWineD3DTexture_Vtbl;
546
547     hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, &texture_ops,
548             1, levels, WINED3DRTYPE_TEXTURE, device, usage, format, pool,
549             parent, parent_ops, &texture_resource_ops);
550     if (FAILED(hr))
551     {
552         WARN("Failed to initialize basetexture, returning %#x.\n", hr);
553         return hr;
554     }
555
556     /* Precalculated scaling for 'faked' non power of two texture coords.
557      * Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
558      * is used in combination with texture uploads (RTL_READTEX). The reason is that EXT_PALETTED_TEXTURE
559      * doesn't work in combination with ARB_TEXTURE_RECTANGLE. */
560     if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT] && (width != pow2_width || height != pow2_height))
561     {
562         texture->baseTexture.pow2Matrix[0] = 1.0f;
563         texture->baseTexture.pow2Matrix[5] = 1.0f;
564         texture->baseTexture.pow2Matrix[10] = 1.0f;
565         texture->baseTexture.pow2Matrix[15] = 1.0f;
566         texture->baseTexture.target = GL_TEXTURE_2D;
567         texture->cond_np2 = TRUE;
568         texture->baseTexture.minMipLookup = minMipLookup_noFilter;
569     }
570     else if (gl_info->supported[ARB_TEXTURE_RECTANGLE] && (width != pow2_width || height != pow2_height)
571             && !(format->id == WINED3DFMT_P8_UINT && gl_info->supported[EXT_PALETTED_TEXTURE]
572             && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
573     {
574         if ((width != 1) || (height != 1)) texture->baseTexture.pow2Matrix_identity = FALSE;
575
576         texture->baseTexture.pow2Matrix[0] = (float)width;
577         texture->baseTexture.pow2Matrix[5] = (float)height;
578         texture->baseTexture.pow2Matrix[10] = 1.0f;
579         texture->baseTexture.pow2Matrix[15] = 1.0f;
580         texture->baseTexture.target = GL_TEXTURE_RECTANGLE_ARB;
581         texture->cond_np2 = TRUE;
582
583         if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
584         {
585             texture->baseTexture.minMipLookup = minMipLookup_noMip;
586         }
587         else
588         {
589             texture->baseTexture.minMipLookup = minMipLookup_noFilter;
590         }
591     }
592     else
593     {
594         if ((width != pow2_width) || (height != pow2_height))
595         {
596             texture->baseTexture.pow2Matrix_identity = FALSE;
597             texture->baseTexture.pow2Matrix[0] = (((float)width) / ((float)pow2_width));
598             texture->baseTexture.pow2Matrix[5] = (((float)height) / ((float)pow2_height));
599         }
600         else
601         {
602             texture->baseTexture.pow2Matrix[0] = 1.0f;
603             texture->baseTexture.pow2Matrix[5] = 1.0f;
604         }
605
606         texture->baseTexture.pow2Matrix[10] = 1.0f;
607         texture->baseTexture.pow2Matrix[15] = 1.0f;
608         texture->baseTexture.target = GL_TEXTURE_2D;
609         texture->cond_np2 = FALSE;
610     }
611     TRACE("xf(%f) yf(%f)\n", texture->baseTexture.pow2Matrix[0], texture->baseTexture.pow2Matrix[5]);
612
613     /* Generate all the surfaces. */
614     tmp_w = width;
615     tmp_h = height;
616     for (i = 0; i < texture->baseTexture.level_count; ++i)
617     {
618         IWineD3DSurface *surface;
619
620         /* Use the callback to create the texture surface. */
621         hr = IWineD3DDeviceParent_CreateSurface(device->device_parent, parent, tmp_w, tmp_h,
622                 format->id, usage, pool, i, 0, &surface);
623         if (FAILED(hr))
624         {
625             FIXME("Failed to create surface %p, hr %#x\n", texture, hr);
626             texture_cleanup(texture);
627             return hr;
628         }
629
630         surface_set_container((IWineD3DSurfaceImpl *)surface, WINED3D_CONTAINER_TEXTURE, (IWineD3DBase *)texture);
631         surface_set_texture_target((IWineD3DSurfaceImpl *)surface, texture->baseTexture.target);
632         texture->baseTexture.sub_resources[i] = &((IWineD3DSurfaceImpl *)surface)->resource;
633         TRACE("Created surface level %u @ %p.\n", i, surface);
634         /* Calculate the next mipmap level. */
635         tmp_w = max(1, tmp_w >> 1);
636         tmp_h = max(1, tmp_h >> 1);
637     }
638
639     return WINED3D_OK;
640 }