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