include: Fix a dsound define.
[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.wineD3DDevice;
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.levels; ++i)
73         {
74             if (palette9_changed((IWineD3DSurfaceImpl *)This->surfaces[i]))
75             {
76                 TRACE("Reloading surface because the d3d8/9 palette was changed.\n");
77                 /* TODO: This is not necessarily needed with hw palettized texture support. */
78                 IWineD3DSurface_LoadLocation(This->surfaces[i], SFLAG_INSYSMEM, NULL);
79                 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
80                 IWineD3DSurface_ModifyLocation(This->surfaces[i], SFLAG_INTEXTURE, FALSE);
81             }
82         }
83     }
84
85     /* If the texture is marked dirty or the srgb sampler setting has changed
86      * since the last load then reload the surfaces. */
87     if (*dirty)
88     {
89         for (i = 0; i < This->baseTexture.levels; ++i)
90         {
91             IWineD3DSurface_LoadTexture(This->surfaces[i], srgb_mode);
92         }
93     }
94     else
95     {
96         TRACE("(%p) Texture not dirty, nothing to do.\n", iface);
97     }
98
99     if (context) context_release(context);
100
101     /* No longer dirty. */
102     *dirty = FALSE;
103 }
104
105 static void texture_cleanup(IWineD3DTextureImpl *This)
106 {
107     unsigned int i;
108
109     TRACE("(%p) : Cleaning up\n", This);
110
111     for (i = 0; i < This->baseTexture.levels; ++i)
112     {
113         if (This->surfaces[i])
114         {
115             /* Clean out the texture name we gave to the surface so that the
116              * surface doesn't try and release it */
117             surface_set_texture_name(This->surfaces[i], 0, TRUE);
118             surface_set_texture_name(This->surfaces[i], 0, FALSE);
119             surface_set_texture_target(This->surfaces[i], 0);
120             IWineD3DSurface_SetContainer(This->surfaces[i], 0);
121             IWineD3DSurface_Release(This->surfaces[i]);
122         }
123     }
124
125     TRACE("(%p) : Cleaning up base texture\n", This);
126     basetexture_cleanup((IWineD3DBaseTexture *)This);
127 }
128
129 /* *******************************************
130    IWineD3DTexture IUnknown parts follow
131    ******************************************* */
132
133 static HRESULT WINAPI IWineD3DTextureImpl_QueryInterface(IWineD3DTexture *iface, REFIID riid, LPVOID *ppobj)
134 {
135     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
136     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
137     if (IsEqualGUID(riid, &IID_IUnknown)
138         || IsEqualGUID(riid, &IID_IWineD3DBase)
139         || IsEqualGUID(riid, &IID_IWineD3DResource)
140         || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
141         || IsEqualGUID(riid, &IID_IWineD3DTexture)){
142         IUnknown_AddRef(iface);
143         *ppobj = This;
144         return WINED3D_OK;
145     }
146     *ppobj = NULL;
147     return E_NOINTERFACE;
148 }
149
150 static ULONG WINAPI IWineD3DTextureImpl_AddRef(IWineD3DTexture *iface) {
151     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
152     TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
153     return InterlockedIncrement(&This->resource.ref);
154 }
155
156 static ULONG WINAPI IWineD3DTextureImpl_Release(IWineD3DTexture *iface) {
157     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
158     ULONG ref;
159     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
160     ref = InterlockedDecrement(&This->resource.ref);
161     if (!ref)
162     {
163         texture_cleanup(This);
164         This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
165         HeapFree(GetProcessHeap(), 0, This);
166     }
167     return ref;
168 }
169
170
171 /* ****************************************************
172    IWineD3DTexture IWineD3DResource parts follow
173    **************************************************** */
174 static HRESULT WINAPI IWineD3DTextureImpl_GetDevice(IWineD3DTexture *iface, IWineD3DDevice** ppDevice) {
175     return resource_get_device((IWineD3DResource *)iface, ppDevice);
176 }
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 static void WINAPI IWineD3DTextureImpl_PreLoad(IWineD3DTexture *iface) {
199     texture_internal_preload((IWineD3DBaseTexture *) iface, SRGB_ANY);
200 }
201
202 static void WINAPI IWineD3DTextureImpl_UnLoad(IWineD3DTexture *iface) {
203     unsigned int i;
204     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
205     TRACE("(%p)\n", This);
206
207     /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
208      * surface before, this one will be a NOP and vice versa. Unloading an unloaded
209      * surface is fine
210      */
211     for (i = 0; i < This->baseTexture.levels; i++) {
212         IWineD3DSurface_UnLoad(This->surfaces[i]);
213         surface_set_texture_name(This->surfaces[i], 0, FALSE); /* Delete rgb name */
214         surface_set_texture_name(This->surfaces[i], 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.levels; ++i) {
284             surface_set_texture_name(This->surfaces[i], gl_tex->name, This->baseTexture.is_srgb);
285         }
286         /* Conditinal non power of two textures use a different clamping default. If we're using the GL_WINE_normalized_texrect
287          * partial driver emulation, we're dealing with a GL_TEXTURE_2D texture which has the address mode set to repeat - something
288          * that prevents us from hitting the accelerated codepath. Thus manually set the GL state. The same applies to filtering.
289          * Even if the texture has only one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW fallback on macos.
290          */
291         if(IWineD3DBaseTexture_IsCondNP2(iface)) {
292             ENTER_GL();
293             glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
294             checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
295             glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
296             checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
297             glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_MIN_FILTER, GL_NEAREST);
298             checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
299             glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_MAG_FILTER, GL_NEAREST);
300             checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
301             LEAVE_GL();
302             gl_tex->states[WINED3DTEXSTA_ADDRESSU]      = WINED3DTADDRESS_CLAMP;
303             gl_tex->states[WINED3DTEXSTA_ADDRESSV]      = WINED3DTADDRESS_CLAMP;
304             gl_tex->states[WINED3DTEXSTA_MAGFILTER]     = WINED3DTEXF_POINT;
305             gl_tex->states[WINED3DTEXSTA_MINFILTER]     = WINED3DTEXF_POINT;
306             gl_tex->states[WINED3DTEXSTA_MIPFILTER]     = WINED3DTEXF_NONE;
307         }
308     }
309
310     return hr;
311 }
312
313 static UINT WINAPI IWineD3DTextureImpl_GetTextureDimensions(IWineD3DTexture *iface) {
314     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
315     TRACE("(%p)\n", This);
316
317     return This->target;
318 }
319
320 static BOOL WINAPI IWineD3DTextureImpl_IsCondNP2(IWineD3DTexture *iface) {
321     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
322     TRACE("(%p)\n", This);
323
324     return This->cond_np2;
325 }
326
327 /* *******************************************
328    IWineD3DTexture IWineD3DTexture parts follow
329    ******************************************* */
330 static HRESULT WINAPI IWineD3DTextureImpl_GetLevelDesc(IWineD3DTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
331     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
332
333     if (Level < This->baseTexture.levels) {
334         TRACE("(%p) Level (%d)\n", This, Level);
335         return IWineD3DSurface_GetDesc(This->surfaces[Level], pDesc);
336     }
337     WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
338     return WINED3DERR_INVALIDCALL;
339 }
340
341 static HRESULT WINAPI IWineD3DTextureImpl_GetSurfaceLevel(IWineD3DTexture *iface, UINT Level, IWineD3DSurface** ppSurfaceLevel) {
342     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
343     HRESULT hr = WINED3DERR_INVALIDCALL;
344
345     if (Level < This->baseTexture.levels) {
346         *ppSurfaceLevel = This->surfaces[Level];
347         IWineD3DSurface_AddRef(This->surfaces[Level]);
348         hr = WINED3D_OK;
349         TRACE("(%p) : returning %p for level %d\n", This, *ppSurfaceLevel, Level);
350     }
351     if (WINED3D_OK != hr) {
352         WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
353         *ppSurfaceLevel = NULL; /* Just to be on the safe side.. */
354     }
355     return hr;
356 }
357
358 static HRESULT WINAPI IWineD3DTextureImpl_LockRect(IWineD3DTexture *iface, UINT Level, WINED3DLOCKED_RECT *pLockedRect,
359                                             CONST RECT *pRect, DWORD Flags) {
360     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
361     HRESULT hr = WINED3DERR_INVALIDCALL;
362
363     if (Level < This->baseTexture.levels) {
364         hr = IWineD3DSurface_LockRect(This->surfaces[Level], pLockedRect, pRect, Flags);
365     }
366     if (WINED3D_OK == hr) {
367         TRACE("(%p) Level (%d) success\n", This, Level);
368     } else {
369         WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
370     }
371
372     return hr;
373 }
374
375 static HRESULT WINAPI IWineD3DTextureImpl_UnlockRect(IWineD3DTexture *iface, UINT Level) {
376    IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
377     HRESULT hr = WINED3DERR_INVALIDCALL;
378
379     if (Level < This->baseTexture.levels) {
380         hr = IWineD3DSurface_UnlockRect(This->surfaces[Level]);
381     }
382     if ( WINED3D_OK == hr) {
383         TRACE("(%p) Level (%d) success\n", This, Level);
384     } else {
385         WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
386     }
387     return hr;
388 }
389
390 static HRESULT WINAPI IWineD3DTextureImpl_AddDirtyRect(IWineD3DTexture *iface, CONST RECT* pDirtyRect) {
391     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
392     This->baseTexture.texture_rgb.dirty = TRUE;
393     This->baseTexture.texture_srgb.dirty = TRUE;
394     TRACE("(%p) : dirtyfication of surface Level (0)\n", This);
395     surface_add_dirty_rect(This->surfaces[0], pDirtyRect);
396
397     return WINED3D_OK;
398 }
399
400 static const IWineD3DTextureVtbl IWineD3DTexture_Vtbl =
401 {
402     /* IUnknown */
403     IWineD3DTextureImpl_QueryInterface,
404     IWineD3DTextureImpl_AddRef,
405     IWineD3DTextureImpl_Release,
406     /* IWineD3DResource */
407     IWineD3DTextureImpl_GetParent,
408     IWineD3DTextureImpl_GetDevice,
409     IWineD3DTextureImpl_SetPrivateData,
410     IWineD3DTextureImpl_GetPrivateData,
411     IWineD3DTextureImpl_FreePrivateData,
412     IWineD3DTextureImpl_SetPriority,
413     IWineD3DTextureImpl_GetPriority,
414     IWineD3DTextureImpl_PreLoad,
415     IWineD3DTextureImpl_UnLoad,
416     IWineD3DTextureImpl_GetType,
417     /* IWineD3DBaseTexture */
418     IWineD3DTextureImpl_SetLOD,
419     IWineD3DTextureImpl_GetLOD,
420     IWineD3DTextureImpl_GetLevelCount,
421     IWineD3DTextureImpl_SetAutoGenFilterType,
422     IWineD3DTextureImpl_GetAutoGenFilterType,
423     IWineD3DTextureImpl_GenerateMipSubLevels,
424     IWineD3DTextureImpl_SetDirty,
425     IWineD3DTextureImpl_GetDirty,
426     IWineD3DTextureImpl_BindTexture,
427     IWineD3DTextureImpl_GetTextureDimensions,
428     IWineD3DTextureImpl_IsCondNP2,
429     /* IWineD3DTexture */
430     IWineD3DTextureImpl_GetLevelDesc,
431     IWineD3DTextureImpl_GetSurfaceLevel,
432     IWineD3DTextureImpl_LockRect,
433     IWineD3DTextureImpl_UnlockRect,
434     IWineD3DTextureImpl_AddDirtyRect
435 };
436
437 HRESULT texture_init(IWineD3DTextureImpl *texture, UINT width, UINT height, UINT levels,
438         IWineD3DDeviceImpl *device, DWORD usage, WINED3DFORMAT format, WINED3DPOOL pool,
439         IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
440 {
441     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
442     const struct GlPixelFormatDesc *format_desc = getFormatDescEntry(format, gl_info);
443     UINT pow2_width, pow2_height;
444     UINT tmp_w, tmp_h;
445     unsigned int i;
446     HRESULT hr;
447
448     /* TODO: It should only be possible to create textures for formats
449      * that are reported as supported. */
450     if (WINED3DFMT_UNKNOWN >= format)
451     {
452         WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
453         return WINED3DERR_INVALIDCALL;
454     }
455
456     /* Non-power2 support. */
457     if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
458     {
459         pow2_width = width;
460         pow2_height = height;
461     }
462     else
463     {
464         /* Find the nearest pow2 match. */
465         pow2_width = pow2_height = 1;
466         while (pow2_width < width) pow2_width <<= 1;
467         while (pow2_height < height) pow2_height <<= 1;
468
469         if (pow2_width != width || pow2_height != height)
470         {
471             if (levels > 1)
472             {
473                 WARN("Attempted to create a mipmapped np2 texture without unconditional np2 support.\n");
474                 return WINED3DERR_INVALIDCALL;
475             }
476             levels = 1;
477         }
478     }
479
480     /* Calculate levels for mip mapping. */
481     if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
482     {
483         if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
484         {
485             WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
486             return WINED3DERR_INVALIDCALL;
487         }
488
489         if (levels > 1)
490         {
491             WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
492             return WINED3DERR_INVALIDCALL;
493         }
494
495         levels = 1;
496     }
497     else if (!levels)
498     {
499         levels = wined3d_log2i(max(width, height)) + 1;
500         TRACE("Calculated levels = %u.\n", levels);
501     }
502
503     texture->lpVtbl = &IWineD3DTexture_Vtbl;
504
505     hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, levels, WINED3DRTYPE_TEXTURE,
506             device, 0, usage, format_desc, pool, parent, parent_ops);
507     if (FAILED(hr))
508     {
509         WARN("Failed to initialize basetexture, returning %#x.\n", hr);
510         return hr;
511     }
512
513     /* Precalculated scaling for 'faked' non power of two texture coords.
514      * Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
515      * is used in combination with texture uploads (RTL_READTEX). The reason is that EXT_PALETTED_TEXTURE
516      * doesn't work in combination with ARB_TEXTURE_RECTANGLE. */
517     if (gl_info->supported[WINE_NORMALIZED_TEXRECT] && (width != pow2_width || height != pow2_height))
518     {
519         texture->baseTexture.pow2Matrix[0] = 1.0f;
520         texture->baseTexture.pow2Matrix[5] = 1.0f;
521         texture->baseTexture.pow2Matrix[10] = 1.0f;
522         texture->baseTexture.pow2Matrix[15] = 1.0f;
523         texture->target = GL_TEXTURE_2D;
524         texture->cond_np2 = TRUE;
525         texture->baseTexture.minMipLookup = minMipLookup_noFilter;
526     }
527     else if (gl_info->supported[ARB_TEXTURE_RECTANGLE] && (width != pow2_width || height != pow2_height)
528             && !(format_desc->format == WINED3DFMT_P8_UINT && gl_info->supported[EXT_PALETTED_TEXTURE]
529             && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
530     {
531         if ((width != 1) || (height != 1)) texture->baseTexture.pow2Matrix_identity = FALSE;
532
533         texture->baseTexture.pow2Matrix[0] = (float)width;
534         texture->baseTexture.pow2Matrix[5] = (float)height;
535         texture->baseTexture.pow2Matrix[10] = 1.0f;
536         texture->baseTexture.pow2Matrix[15] = 1.0f;
537         texture->target = GL_TEXTURE_RECTANGLE_ARB;
538         texture->cond_np2 = TRUE;
539
540         if(texture->resource.format_desc->Flags & WINED3DFMT_FLAG_FILTERING)
541         {
542             texture->baseTexture.minMipLookup = minMipLookup_noMip;
543         }
544         else
545         {
546             texture->baseTexture.minMipLookup = minMipLookup_noFilter;
547         }
548     }
549     else
550     {
551         if ((width != pow2_width) || (height != pow2_height))
552         {
553             texture->baseTexture.pow2Matrix_identity = FALSE;
554             texture->baseTexture.pow2Matrix[0] = (((float)width) / ((float)pow2_width));
555             texture->baseTexture.pow2Matrix[5] = (((float)height) / ((float)pow2_height));
556         }
557         else
558         {
559             texture->baseTexture.pow2Matrix[0] = 1.0f;
560             texture->baseTexture.pow2Matrix[5] = 1.0f;
561         }
562
563         texture->baseTexture.pow2Matrix[10] = 1.0f;
564         texture->baseTexture.pow2Matrix[15] = 1.0f;
565         texture->target = GL_TEXTURE_2D;
566         texture->cond_np2 = FALSE;
567     }
568     TRACE("xf(%f) yf(%f)\n", texture->baseTexture.pow2Matrix[0], texture->baseTexture.pow2Matrix[5]);
569
570     /* Generate all the surfaces. */
571     tmp_w = width;
572     tmp_h = height;
573     for (i = 0; i < texture->baseTexture.levels; ++i)
574     {
575         /* Use the callback to create the texture surface. */
576         hr = IWineD3DDeviceParent_CreateSurface(device->device_parent, parent, tmp_w, tmp_h, format_desc->format,
577                 usage, pool, i, WINED3DCUBEMAP_FACE_POSITIVE_X, &texture->surfaces[i]);
578         if (FAILED(hr) || ((IWineD3DSurfaceImpl *)texture->surfaces[i])->Flags & SFLAG_OVERSIZE)
579         {
580             FIXME("Failed to create surface %p, hr %#x\n", texture, hr);
581             texture->surfaces[i] = NULL;
582             texture_cleanup(texture);
583             return hr;
584         }
585
586         IWineD3DSurface_SetContainer(texture->surfaces[i], (IWineD3DBase *)texture);
587         TRACE("Created surface level %u @ %p.\n", i, texture->surfaces[i]);
588         surface_set_texture_target(texture->surfaces[i], texture->target);
589         /* Calculate the next mipmap level. */
590         tmp_w = max(1, tmp_w >> 1);
591         tmp_h = max(1, tmp_h >> 1);
592     }
593     texture->baseTexture.internal_preload = texture_internal_preload;
594
595     return WINED3D_OK;
596 }