wined3d: Recognize the SM4 frc opcode.
[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 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((IWineD3DBaseTexture *)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, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
179     return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
180 }
181
182 static HRESULT WINAPI IWineD3DTextureImpl_GetPrivateData(IWineD3DTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
183     return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
184 }
185
186 static HRESULT WINAPI IWineD3DTextureImpl_FreePrivateData(IWineD3DTexture *iface, REFGUID refguid) {
187     return resource_free_private_data((IWineD3DResource *)iface, refguid);
188 }
189
190 static DWORD WINAPI IWineD3DTextureImpl_SetPriority(IWineD3DTexture *iface, DWORD PriorityNew) {
191     return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
192 }
193
194 static DWORD WINAPI IWineD3DTextureImpl_GetPriority(IWineD3DTexture *iface) {
195     return resource_get_priority((IWineD3DResource *)iface);
196 }
197
198 /* Do not call while under the GL lock. */
199 static void WINAPI IWineD3DTextureImpl_PreLoad(IWineD3DTexture *iface) {
200     texture_internal_preload((IWineD3DBaseTexture *) iface, SRGB_ANY);
201 }
202
203 /* Do not call while under the GL lock. */
204 static void WINAPI IWineD3DTextureImpl_UnLoad(IWineD3DTexture *iface) {
205     unsigned int i;
206     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
207     TRACE("(%p)\n", This);
208
209     /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
210      * surface before, this one will be a NOP and vice versa. Unloading an unloaded
211      * surface is fine
212      */
213     for (i = 0; i < This->baseTexture.level_count; ++i)
214     {
215         IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)This->baseTexture.sub_resources[i];
216         IWineD3DSurface_UnLoad((IWineD3DSurface *)surface);
217         surface_set_texture_name(surface, 0, FALSE); /* Delete rgb name */
218         surface_set_texture_name(surface, 0, TRUE); /* delete srgb name */
219     }
220
221     basetexture_unload((IWineD3DBaseTexture *)iface);
222 }
223
224 static WINED3DRESOURCETYPE WINAPI IWineD3DTextureImpl_GetType(IWineD3DTexture *iface) {
225     return resource_get_type((IWineD3DResource *)iface);
226 }
227
228 static void * WINAPI IWineD3DTextureImpl_GetParent(IWineD3DTexture *iface)
229 {
230     TRACE("iface %p.\n", iface);
231
232     return ((IWineD3DTextureImpl *)iface)->resource.parent;
233 }
234
235 /* ******************************************************
236    IWineD3DTexture IWineD3DBaseTexture parts follow
237    ****************************************************** */
238 static DWORD WINAPI IWineD3DTextureImpl_SetLOD(IWineD3DTexture *iface, DWORD LODNew) {
239     return basetexture_set_lod((IWineD3DBaseTexture *)iface, LODNew);
240 }
241
242 static DWORD WINAPI IWineD3DTextureImpl_GetLOD(IWineD3DTexture *iface) {
243     return basetexture_get_lod((IWineD3DBaseTexture *)iface);
244 }
245
246 static DWORD WINAPI IWineD3DTextureImpl_GetLevelCount(IWineD3DTexture *iface) {
247     return basetexture_get_level_count((IWineD3DBaseTexture *)iface);
248 }
249
250 static HRESULT WINAPI IWineD3DTextureImpl_SetAutoGenFilterType(IWineD3DTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
251   return basetexture_set_autogen_filter_type((IWineD3DBaseTexture *)iface, FilterType);
252 }
253
254 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DTextureImpl_GetAutoGenFilterType(IWineD3DTexture *iface) {
255   return basetexture_get_autogen_filter_type((IWineD3DBaseTexture *)iface);
256 }
257
258 static void WINAPI IWineD3DTextureImpl_GenerateMipSubLevels(IWineD3DTexture *iface) {
259     basetexture_generate_mipmaps((IWineD3DBaseTexture *)iface);
260 }
261
262 /* Internal function, No d3d mapping */
263 static BOOL WINAPI IWineD3DTextureImpl_SetDirty(IWineD3DTexture *iface, BOOL dirty) {
264     return basetexture_set_dirty((IWineD3DBaseTexture *)iface, dirty);
265 }
266
267 static BOOL WINAPI IWineD3DTextureImpl_GetDirty(IWineD3DTexture *iface) {
268     return basetexture_get_dirty((IWineD3DBaseTexture *)iface);
269 }
270
271 /* Context activation is done by the caller. */
272 static HRESULT WINAPI IWineD3DTextureImpl_BindTexture(IWineD3DTexture *iface, BOOL srgb) {
273     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
274     BOOL set_gl_texture_desc;
275     HRESULT hr;
276
277     TRACE("(%p) : relay to BaseTexture\n", This);
278
279     hr = basetexture_bind((IWineD3DBaseTexture *)iface, srgb, &set_gl_texture_desc);
280     if (set_gl_texture_desc && SUCCEEDED(hr)) {
281         UINT i;
282         struct gl_texture *gl_tex;
283
284         if(This->baseTexture.is_srgb) {
285             gl_tex = &This->baseTexture.texture_srgb;
286         } else {
287             gl_tex = &This->baseTexture.texture_rgb;
288         }
289
290         for (i = 0; i < This->baseTexture.level_count; ++i)
291         {
292             IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)This->baseTexture.sub_resources[i];
293             surface_set_texture_name(surface, gl_tex->name, This->baseTexture.is_srgb);
294         }
295
296         /* Conditinal non power of two textures use a different clamping
297          * default. If we're using the GL_WINE_normalized_texrect partial
298          * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
299          * has the address mode set to repeat - something that prevents us
300          * from hitting the accelerated codepath. Thus manually set the GL
301          * state. The same applies to filtering. Even if the texture has only
302          * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
303          * fallback on macos. */
304         if (IWineD3DBaseTexture_IsCondNP2(iface))
305         {
306             GLenum target = This->baseTexture.target;
307
308             ENTER_GL();
309             glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
310             checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
311             glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
312             checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
313             glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
314             checkGLcall("glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
315             glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
316             checkGLcall("glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
317             LEAVE_GL();
318             gl_tex->states[WINED3DTEXSTA_ADDRESSU]      = WINED3DTADDRESS_CLAMP;
319             gl_tex->states[WINED3DTEXSTA_ADDRESSV]      = WINED3DTADDRESS_CLAMP;
320             gl_tex->states[WINED3DTEXSTA_MAGFILTER]     = WINED3DTEXF_POINT;
321             gl_tex->states[WINED3DTEXSTA_MINFILTER]     = WINED3DTEXF_POINT;
322             gl_tex->states[WINED3DTEXSTA_MIPFILTER]     = WINED3DTEXF_NONE;
323         }
324     }
325
326     return hr;
327 }
328
329 static BOOL WINAPI IWineD3DTextureImpl_IsCondNP2(IWineD3DTexture *iface) {
330     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
331     TRACE("(%p)\n", This);
332
333     return This->cond_np2;
334 }
335
336 /* *******************************************
337    IWineD3DTexture IWineD3DTexture parts follow
338    ******************************************* */
339 static HRESULT WINAPI IWineD3DTextureImpl_GetLevelDesc(IWineD3DTexture *iface, UINT level, WINED3DSURFACE_DESC *desc)
340 {
341     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
342     IWineD3DSurface *surface;
343
344     TRACE("iface %p, level %u, desc %p.\n", iface, level, desc);
345
346     if (!(surface = (IWineD3DSurface *)basetexture_get_sub_resource(texture, 0, level)))
347     {
348         WARN("Failed to get sub-resource.\n");
349         return WINED3DERR_INVALIDCALL;
350     }
351
352     IWineD3DSurface_GetDesc(surface, desc);
353
354     return WINED3D_OK;
355 }
356
357 static HRESULT WINAPI IWineD3DTextureImpl_GetSurfaceLevel(IWineD3DTexture *iface,
358         UINT level, IWineD3DSurface **surface)
359 {
360     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
361     IWineD3DSurface *s;
362
363     TRACE("iface %p, level %u, surface %p.\n", iface, level, surface);
364
365     if (!(s = (IWineD3DSurface *)basetexture_get_sub_resource(texture, 0, level)))
366     {
367         WARN("Failed to get sub-resource.\n");
368         return WINED3DERR_INVALIDCALL;
369     }
370
371     IWineD3DSurface_AddRef(s);
372     *surface = s;
373
374     TRACE("Returning surface %p.\n", *surface);
375
376     return WINED3D_OK;
377 }
378
379 static HRESULT WINAPI IWineD3DTextureImpl_LockRect(IWineD3DTexture *iface,
380         UINT level, WINED3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
381 {
382     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
383     IWineD3DSurface *surface;
384
385     TRACE("iface %p, level %u, locked_rect %p, rect %s, flags %#x.\n",
386             iface, level, locked_rect, wine_dbgstr_rect(rect), flags);
387
388     if (!(surface = (IWineD3DSurface *)basetexture_get_sub_resource(texture, 0, level)))
389     {
390         WARN("Failed to get sub-resource.\n");
391         return WINED3DERR_INVALIDCALL;
392     }
393
394     return IWineD3DSurface_LockRect(surface, locked_rect, rect, flags);
395 }
396
397 static HRESULT WINAPI IWineD3DTextureImpl_UnlockRect(IWineD3DTexture *iface, UINT level)
398 {
399     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
400     IWineD3DSurface *surface;
401
402     TRACE("iface %p, level %u.\n", iface, level);
403
404     if (!(surface = (IWineD3DSurface *)basetexture_get_sub_resource(texture, 0, level)))
405     {
406         WARN("Failed to get sub-resource.\n");
407         return WINED3DERR_INVALIDCALL;
408     }
409
410     return IWineD3DSurface_UnlockRect(surface);
411 }
412
413 static HRESULT WINAPI IWineD3DTextureImpl_AddDirtyRect(IWineD3DTexture *iface, const RECT *dirty_rect)
414 {
415     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
416     IWineD3DSurfaceImpl *surface;
417
418     TRACE("iface %p, dirty_rect %s.\n", iface, wine_dbgstr_rect(dirty_rect));
419
420     if (!(surface = (IWineD3DSurfaceImpl *)basetexture_get_sub_resource(texture, 0, 0)))
421     {
422         WARN("Failed to get sub-resource.\n");
423         return WINED3DERR_INVALIDCALL;
424     }
425
426     texture->baseTexture.texture_rgb.dirty = TRUE;
427     texture->baseTexture.texture_srgb.dirty = TRUE;
428     surface_add_dirty_rect(surface, dirty_rect);
429
430     return WINED3D_OK;
431 }
432
433 static const IWineD3DTextureVtbl IWineD3DTexture_Vtbl =
434 {
435     /* IUnknown */
436     IWineD3DTextureImpl_QueryInterface,
437     IWineD3DTextureImpl_AddRef,
438     IWineD3DTextureImpl_Release,
439     /* IWineD3DResource */
440     IWineD3DTextureImpl_GetParent,
441     IWineD3DTextureImpl_SetPrivateData,
442     IWineD3DTextureImpl_GetPrivateData,
443     IWineD3DTextureImpl_FreePrivateData,
444     IWineD3DTextureImpl_SetPriority,
445     IWineD3DTextureImpl_GetPriority,
446     IWineD3DTextureImpl_PreLoad,
447     IWineD3DTextureImpl_UnLoad,
448     IWineD3DTextureImpl_GetType,
449     /* IWineD3DBaseTexture */
450     IWineD3DTextureImpl_SetLOD,
451     IWineD3DTextureImpl_GetLOD,
452     IWineD3DTextureImpl_GetLevelCount,
453     IWineD3DTextureImpl_SetAutoGenFilterType,
454     IWineD3DTextureImpl_GetAutoGenFilterType,
455     IWineD3DTextureImpl_GenerateMipSubLevels,
456     IWineD3DTextureImpl_SetDirty,
457     IWineD3DTextureImpl_GetDirty,
458     IWineD3DTextureImpl_BindTexture,
459     IWineD3DTextureImpl_IsCondNP2,
460     /* IWineD3DTexture */
461     IWineD3DTextureImpl_GetLevelDesc,
462     IWineD3DTextureImpl_GetSurfaceLevel,
463     IWineD3DTextureImpl_LockRect,
464     IWineD3DTextureImpl_UnlockRect,
465     IWineD3DTextureImpl_AddDirtyRect
466 };
467
468 HRESULT texture_init(IWineD3DTextureImpl *texture, UINT width, UINT height, UINT levels,
469         IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
470         void *parent, const struct wined3d_parent_ops *parent_ops)
471 {
472     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
473     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
474     UINT pow2_width, pow2_height;
475     UINT tmp_w, tmp_h;
476     unsigned int i;
477     HRESULT hr;
478
479     /* TODO: It should only be possible to create textures for formats
480      * that are reported as supported. */
481     if (WINED3DFMT_UNKNOWN >= format_id)
482     {
483         WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
484         return WINED3DERR_INVALIDCALL;
485     }
486
487     /* Non-power2 support. */
488     if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
489     {
490         pow2_width = width;
491         pow2_height = height;
492     }
493     else
494     {
495         /* Find the nearest pow2 match. */
496         pow2_width = pow2_height = 1;
497         while (pow2_width < width) pow2_width <<= 1;
498         while (pow2_height < height) pow2_height <<= 1;
499
500         if (pow2_width != width || pow2_height != height)
501         {
502             if (levels > 1)
503             {
504                 WARN("Attempted to create a mipmapped np2 texture without unconditional np2 support.\n");
505                 return WINED3DERR_INVALIDCALL;
506             }
507             levels = 1;
508         }
509     }
510
511     /* Calculate levels for mip mapping. */
512     if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
513     {
514         if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
515         {
516             WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
517             return WINED3DERR_INVALIDCALL;
518         }
519
520         if (levels > 1)
521         {
522             WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
523             return WINED3DERR_INVALIDCALL;
524         }
525
526         levels = 1;
527     }
528     else if (!levels)
529     {
530         levels = wined3d_log2i(max(width, height)) + 1;
531         TRACE("Calculated levels = %u.\n", levels);
532     }
533
534     texture->lpVtbl = &IWineD3DTexture_Vtbl;
535
536     hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, 1, levels,
537             WINED3DRTYPE_TEXTURE, device, 0, usage, format, pool, parent, parent_ops);
538     if (FAILED(hr))
539     {
540         WARN("Failed to initialize basetexture, returning %#x.\n", hr);
541         return hr;
542     }
543
544     /* Precalculated scaling for 'faked' non power of two texture coords.
545      * Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
546      * is used in combination with texture uploads (RTL_READTEX). The reason is that EXT_PALETTED_TEXTURE
547      * doesn't work in combination with ARB_TEXTURE_RECTANGLE. */
548     if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT] && (width != pow2_width || height != pow2_height))
549     {
550         texture->baseTexture.pow2Matrix[0] = 1.0f;
551         texture->baseTexture.pow2Matrix[5] = 1.0f;
552         texture->baseTexture.pow2Matrix[10] = 1.0f;
553         texture->baseTexture.pow2Matrix[15] = 1.0f;
554         texture->baseTexture.target = GL_TEXTURE_2D;
555         texture->cond_np2 = TRUE;
556         texture->baseTexture.minMipLookup = minMipLookup_noFilter;
557     }
558     else if (gl_info->supported[ARB_TEXTURE_RECTANGLE] && (width != pow2_width || height != pow2_height)
559             && !(format->id == WINED3DFMT_P8_UINT && gl_info->supported[EXT_PALETTED_TEXTURE]
560             && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
561     {
562         if ((width != 1) || (height != 1)) texture->baseTexture.pow2Matrix_identity = FALSE;
563
564         texture->baseTexture.pow2Matrix[0] = (float)width;
565         texture->baseTexture.pow2Matrix[5] = (float)height;
566         texture->baseTexture.pow2Matrix[10] = 1.0f;
567         texture->baseTexture.pow2Matrix[15] = 1.0f;
568         texture->baseTexture.target = GL_TEXTURE_RECTANGLE_ARB;
569         texture->cond_np2 = TRUE;
570
571         if(texture->resource.format->Flags & WINED3DFMT_FLAG_FILTERING)
572         {
573             texture->baseTexture.minMipLookup = minMipLookup_noMip;
574         }
575         else
576         {
577             texture->baseTexture.minMipLookup = minMipLookup_noFilter;
578         }
579     }
580     else
581     {
582         if ((width != pow2_width) || (height != pow2_height))
583         {
584             texture->baseTexture.pow2Matrix_identity = FALSE;
585             texture->baseTexture.pow2Matrix[0] = (((float)width) / ((float)pow2_width));
586             texture->baseTexture.pow2Matrix[5] = (((float)height) / ((float)pow2_height));
587         }
588         else
589         {
590             texture->baseTexture.pow2Matrix[0] = 1.0f;
591             texture->baseTexture.pow2Matrix[5] = 1.0f;
592         }
593
594         texture->baseTexture.pow2Matrix[10] = 1.0f;
595         texture->baseTexture.pow2Matrix[15] = 1.0f;
596         texture->baseTexture.target = GL_TEXTURE_2D;
597         texture->cond_np2 = FALSE;
598     }
599     TRACE("xf(%f) yf(%f)\n", texture->baseTexture.pow2Matrix[0], texture->baseTexture.pow2Matrix[5]);
600
601     /* Generate all the surfaces. */
602     tmp_w = width;
603     tmp_h = height;
604     for (i = 0; i < texture->baseTexture.level_count; ++i)
605     {
606         IWineD3DSurface *surface;
607
608         /* Use the callback to create the texture surface. */
609         hr = IWineD3DDeviceParent_CreateSurface(device->device_parent, parent, tmp_w, tmp_h,
610                 format->id, usage, pool, i, 0, &surface);
611         if (FAILED(hr))
612         {
613             FIXME("Failed to create surface %p, hr %#x\n", texture, hr);
614             texture_cleanup(texture);
615             return hr;
616         }
617
618         surface_set_container((IWineD3DSurfaceImpl *)surface, WINED3D_CONTAINER_TEXTURE, (IWineD3DBase *)texture);
619         surface_set_texture_target((IWineD3DSurfaceImpl *)surface, texture->baseTexture.target);
620         texture->baseTexture.sub_resources[i] = (IWineD3DResourceImpl *)surface;
621         TRACE("Created surface level %u @ %p.\n", i, surface);
622         /* Calculate the next mipmap level. */
623         tmp_w = max(1, tmp_w >> 1);
624         tmp_h = max(1, tmp_h >> 1);
625     }
626     texture->baseTexture.internal_preload = texture_internal_preload;
627
628     return WINED3D_OK;
629 }