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