mshtml: Test return value of OnStateChange.
[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);
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         IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)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((IWineD3DSurface *)surface, NULL);
123             IWineD3DSurface_Release((IWineD3DSurface *)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         IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)This->baseTexture.sub_resources[i];
212         IWineD3DSurface_UnLoad((IWineD3DSurface *)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             IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)This->baseTexture.sub_resources[i];
286             surface_set_texture_name(surface, gl_tex->name, This->baseTexture.is_srgb);
287         }
288
289         /* Conditinal non power of two textures use a different clamping
290          * default. If we're using the GL_WINE_normalized_texrect partial
291          * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
292          * has the address mode set to repeat - something that prevents us
293          * from hitting the accelerated codepath. Thus manually set the GL
294          * state. The same applies to filtering. Even if the texture has only
295          * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
296          * fallback on macos. */
297         if(IWineD3DBaseTexture_IsCondNP2(iface)) {
298             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 *desc)
337 {
338     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
339     IWineD3DSurface *surface;
340
341     TRACE("iface %p, level %u, desc %p.\n", iface, level, desc);
342
343     if (!(surface = (IWineD3DSurface *)basetexture_get_sub_resource(texture, 0, level)))
344     {
345         WARN("Failed to get sub-resource.\n");
346         return WINED3DERR_INVALIDCALL;
347     }
348
349     return IWineD3DSurface_GetDesc(surface, desc);
350 }
351
352 static HRESULT WINAPI IWineD3DTextureImpl_GetSurfaceLevel(IWineD3DTexture *iface,
353         UINT level, IWineD3DSurface **surface)
354 {
355     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
356     IWineD3DSurface *s;
357
358     TRACE("iface %p, level %u, surface %p.\n", iface, level, surface);
359
360     if (!(s = (IWineD3DSurface *)basetexture_get_sub_resource(texture, 0, level)))
361     {
362         WARN("Failed to get sub-resource.\n");
363         return WINED3DERR_INVALIDCALL;
364     }
365
366     IWineD3DSurface_AddRef(s);
367     *surface = s;
368
369     TRACE("Returning surface %p.\n", *surface);
370
371     return WINED3D_OK;
372 }
373
374 static HRESULT WINAPI IWineD3DTextureImpl_LockRect(IWineD3DTexture *iface,
375         UINT level, WINED3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
376 {
377     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
378     IWineD3DSurface *surface;
379
380     TRACE("iface %p, level %u, locked_rect %p, rect %s, flags %#x.\n",
381             iface, level, locked_rect, wine_dbgstr_rect(rect), flags);
382
383     if (!(surface = (IWineD3DSurface *)basetexture_get_sub_resource(texture, 0, level)))
384     {
385         WARN("Failed to get sub-resource.\n");
386         return WINED3DERR_INVALIDCALL;
387     }
388
389     return IWineD3DSurface_LockRect(surface, locked_rect, rect, flags);
390 }
391
392 static HRESULT WINAPI IWineD3DTextureImpl_UnlockRect(IWineD3DTexture *iface, UINT level)
393 {
394     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
395     IWineD3DSurface *surface;
396
397     TRACE("iface %p, level %u.\n", iface, level);
398
399     if (!(surface = (IWineD3DSurface *)basetexture_get_sub_resource(texture, 0, level)))
400     {
401         WARN("Failed to get sub-resource.\n");
402         return WINED3DERR_INVALIDCALL;
403     }
404
405     return IWineD3DSurface_UnlockRect(surface);
406 }
407
408 static HRESULT WINAPI IWineD3DTextureImpl_AddDirtyRect(IWineD3DTexture *iface, const RECT *dirty_rect)
409 {
410     IWineD3DBaseTextureImpl *texture = (IWineD3DBaseTextureImpl *)iface;
411     IWineD3DSurfaceImpl *surface;
412
413     TRACE("iface %p, dirty_rect %s.\n", iface, wine_dbgstr_rect(dirty_rect));
414
415     if (!(surface = (IWineD3DSurfaceImpl *)basetexture_get_sub_resource(texture, 0, 0)))
416     {
417         WARN("Failed to get sub-resource.\n");
418         return WINED3DERR_INVALIDCALL;
419     }
420
421     texture->baseTexture.texture_rgb.dirty = TRUE;
422     texture->baseTexture.texture_srgb.dirty = TRUE;
423     surface_add_dirty_rect(surface, dirty_rect);
424
425     return WINED3D_OK;
426 }
427
428 static const IWineD3DTextureVtbl IWineD3DTexture_Vtbl =
429 {
430     /* IUnknown */
431     IWineD3DTextureImpl_QueryInterface,
432     IWineD3DTextureImpl_AddRef,
433     IWineD3DTextureImpl_Release,
434     /* IWineD3DResource */
435     IWineD3DTextureImpl_GetParent,
436     IWineD3DTextureImpl_SetPrivateData,
437     IWineD3DTextureImpl_GetPrivateData,
438     IWineD3DTextureImpl_FreePrivateData,
439     IWineD3DTextureImpl_SetPriority,
440     IWineD3DTextureImpl_GetPriority,
441     IWineD3DTextureImpl_PreLoad,
442     IWineD3DTextureImpl_UnLoad,
443     IWineD3DTextureImpl_GetType,
444     /* IWineD3DBaseTexture */
445     IWineD3DTextureImpl_SetLOD,
446     IWineD3DTextureImpl_GetLOD,
447     IWineD3DTextureImpl_GetLevelCount,
448     IWineD3DTextureImpl_SetAutoGenFilterType,
449     IWineD3DTextureImpl_GetAutoGenFilterType,
450     IWineD3DTextureImpl_GenerateMipSubLevels,
451     IWineD3DTextureImpl_SetDirty,
452     IWineD3DTextureImpl_GetDirty,
453     IWineD3DTextureImpl_BindTexture,
454     IWineD3DTextureImpl_GetTextureDimensions,
455     IWineD3DTextureImpl_IsCondNP2,
456     /* IWineD3DTexture */
457     IWineD3DTextureImpl_GetLevelDesc,
458     IWineD3DTextureImpl_GetSurfaceLevel,
459     IWineD3DTextureImpl_LockRect,
460     IWineD3DTextureImpl_UnlockRect,
461     IWineD3DTextureImpl_AddDirtyRect
462 };
463
464 HRESULT texture_init(IWineD3DTextureImpl *texture, UINT width, UINT height, UINT levels,
465         IWineD3DDeviceImpl *device, DWORD usage, WINED3DFORMAT format, WINED3DPOOL pool,
466         IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
467 {
468     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
469     const struct wined3d_format_desc *format_desc = getFormatDescEntry(format, gl_info);
470     UINT pow2_width, pow2_height;
471     UINT tmp_w, tmp_h;
472     unsigned int i;
473     HRESULT hr;
474
475     /* TODO: It should only be possible to create textures for formats
476      * that are reported as supported. */
477     if (WINED3DFMT_UNKNOWN >= format)
478     {
479         WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
480         return WINED3DERR_INVALIDCALL;
481     }
482
483     /* Non-power2 support. */
484     if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
485     {
486         pow2_width = width;
487         pow2_height = height;
488     }
489     else
490     {
491         /* Find the nearest pow2 match. */
492         pow2_width = pow2_height = 1;
493         while (pow2_width < width) pow2_width <<= 1;
494         while (pow2_height < height) pow2_height <<= 1;
495
496         if (pow2_width != width || pow2_height != height)
497         {
498             if (levels > 1)
499             {
500                 WARN("Attempted to create a mipmapped np2 texture without unconditional np2 support.\n");
501                 return WINED3DERR_INVALIDCALL;
502             }
503             levels = 1;
504         }
505     }
506
507     /* Calculate levels for mip mapping. */
508     if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
509     {
510         if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
511         {
512             WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
513             return WINED3DERR_INVALIDCALL;
514         }
515
516         if (levels > 1)
517         {
518             WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
519             return WINED3DERR_INVALIDCALL;
520         }
521
522         levels = 1;
523     }
524     else if (!levels)
525     {
526         levels = wined3d_log2i(max(width, height)) + 1;
527         TRACE("Calculated levels = %u.\n", levels);
528     }
529
530     texture->lpVtbl = &IWineD3DTexture_Vtbl;
531
532     hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, 1, levels,
533             WINED3DRTYPE_TEXTURE, device, 0, usage, format_desc, pool, parent, parent_ops);
534     if (FAILED(hr))
535     {
536         WARN("Failed to initialize basetexture, returning %#x.\n", hr);
537         return hr;
538     }
539
540     /* Precalculated scaling for 'faked' non power of two texture coords.
541      * Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
542      * is used in combination with texture uploads (RTL_READTEX). The reason is that EXT_PALETTED_TEXTURE
543      * doesn't work in combination with ARB_TEXTURE_RECTANGLE. */
544     if (gl_info->supported[WINE_NORMALIZED_TEXRECT] && (width != pow2_width || height != pow2_height))
545     {
546         texture->baseTexture.pow2Matrix[0] = 1.0f;
547         texture->baseTexture.pow2Matrix[5] = 1.0f;
548         texture->baseTexture.pow2Matrix[10] = 1.0f;
549         texture->baseTexture.pow2Matrix[15] = 1.0f;
550         texture->target = GL_TEXTURE_2D;
551         texture->cond_np2 = TRUE;
552         texture->baseTexture.minMipLookup = minMipLookup_noFilter;
553     }
554     else if (gl_info->supported[ARB_TEXTURE_RECTANGLE] && (width != pow2_width || height != pow2_height)
555             && !(format_desc->format == WINED3DFMT_P8_UINT && gl_info->supported[EXT_PALETTED_TEXTURE]
556             && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
557     {
558         if ((width != 1) || (height != 1)) texture->baseTexture.pow2Matrix_identity = FALSE;
559
560         texture->baseTexture.pow2Matrix[0] = (float)width;
561         texture->baseTexture.pow2Matrix[5] = (float)height;
562         texture->baseTexture.pow2Matrix[10] = 1.0f;
563         texture->baseTexture.pow2Matrix[15] = 1.0f;
564         texture->target = GL_TEXTURE_RECTANGLE_ARB;
565         texture->cond_np2 = TRUE;
566
567         if(texture->resource.format_desc->Flags & WINED3DFMT_FLAG_FILTERING)
568         {
569             texture->baseTexture.minMipLookup = minMipLookup_noMip;
570         }
571         else
572         {
573             texture->baseTexture.minMipLookup = minMipLookup_noFilter;
574         }
575     }
576     else
577     {
578         if ((width != pow2_width) || (height != pow2_height))
579         {
580             texture->baseTexture.pow2Matrix_identity = FALSE;
581             texture->baseTexture.pow2Matrix[0] = (((float)width) / ((float)pow2_width));
582             texture->baseTexture.pow2Matrix[5] = (((float)height) / ((float)pow2_height));
583         }
584         else
585         {
586             texture->baseTexture.pow2Matrix[0] = 1.0f;
587             texture->baseTexture.pow2Matrix[5] = 1.0f;
588         }
589
590         texture->baseTexture.pow2Matrix[10] = 1.0f;
591         texture->baseTexture.pow2Matrix[15] = 1.0f;
592         texture->target = GL_TEXTURE_2D;
593         texture->cond_np2 = FALSE;
594     }
595     TRACE("xf(%f) yf(%f)\n", texture->baseTexture.pow2Matrix[0], texture->baseTexture.pow2Matrix[5]);
596
597     /* Generate all the surfaces. */
598     tmp_w = width;
599     tmp_h = height;
600     for (i = 0; i < texture->baseTexture.level_count; ++i)
601     {
602         IWineD3DSurface *surface;
603
604         /* Use the callback to create the texture surface. */
605         hr = IWineD3DDeviceParent_CreateSurface(device->device_parent, parent, tmp_w, tmp_h, format_desc->format,
606                 usage, pool, i, 0, &surface);
607         if (FAILED(hr))
608         {
609             FIXME("Failed to create surface %p, hr %#x\n", texture, hr);
610             texture_cleanup(texture);
611             return hr;
612         }
613
614         IWineD3DSurface_SetContainer(surface, (IWineD3DBase *)texture);
615         surface_set_texture_target((IWineD3DSurfaceImpl *)surface, texture->target);
616         texture->baseTexture.sub_resources[i] = (IWineD3DResourceImpl *)surface;
617         TRACE("Created surface level %u @ %p.\n", i, surface);
618         /* Calculate the next mipmap level. */
619         tmp_w = max(1, tmp_w >> 1);
620         tmp_h = max(1, tmp_h >> 1);
621     }
622     texture->baseTexture.internal_preload = texture_internal_preload;
623
624     return WINED3D_OK;
625 }