wined3d: Override the default filter of conditional np2 textures.
[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  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include "config.h"
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
28 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
29
30 /* *******************************************
31    IWineD3DTexture IUnknown parts follow
32    ******************************************* */
33 static HRESULT WINAPI IWineD3DTextureImpl_QueryInterface(IWineD3DTexture *iface, REFIID riid, LPVOID *ppobj)
34 {
35     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
36     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
37     if (IsEqualGUID(riid, &IID_IUnknown)
38         || IsEqualGUID(riid, &IID_IWineD3DBase)
39         || IsEqualGUID(riid, &IID_IWineD3DResource)
40         || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
41         || IsEqualGUID(riid, &IID_IWineD3DTexture)){
42         IUnknown_AddRef(iface);
43         *ppobj = This;
44         return WINED3D_OK;
45     }
46     *ppobj = NULL;
47     return E_NOINTERFACE;
48 }
49
50 static ULONG WINAPI IWineD3DTextureImpl_AddRef(IWineD3DTexture *iface) {
51     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
52     TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
53     return InterlockedIncrement(&This->resource.ref);
54 }
55
56 static ULONG WINAPI IWineD3DTextureImpl_Release(IWineD3DTexture *iface) {
57     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
58     ULONG ref;
59     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
60     ref = InterlockedDecrement(&This->resource.ref);
61     if (ref == 0) {
62         IWineD3DTexture_Destroy(iface, D3DCB_DefaultDestroySurface);
63     }
64     return ref;
65 }
66
67
68 /* ****************************************************
69    IWineD3DTexture IWineD3DResource parts follow
70    **************************************************** */
71 static HRESULT WINAPI IWineD3DTextureImpl_GetDevice(IWineD3DTexture *iface, IWineD3DDevice** ppDevice) {
72     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
73 }
74
75 static HRESULT WINAPI IWineD3DTextureImpl_SetPrivateData(IWineD3DTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
76     return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
77 }
78
79 static HRESULT WINAPI IWineD3DTextureImpl_GetPrivateData(IWineD3DTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
80     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
81 }
82
83 static HRESULT WINAPI IWineD3DTextureImpl_FreePrivateData(IWineD3DTexture *iface, REFGUID refguid) {
84     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
85 }
86
87 static DWORD WINAPI IWineD3DTextureImpl_SetPriority(IWineD3DTexture *iface, DWORD PriorityNew) {
88     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
89 }
90
91 static DWORD WINAPI IWineD3DTextureImpl_GetPriority(IWineD3DTexture *iface) {
92     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
93 }
94
95 static void WINAPI IWineD3DTextureImpl_PreLoad(IWineD3DTexture *iface) {
96
97     /* Override the IWineD3DResource PreLoad method */
98     unsigned int i;
99     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
100     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
101     BOOL srgb_mode = This->baseTexture.is_srgb;
102     BOOL srgb_was_toggled = FALSE;
103
104     TRACE("(%p) : About to load texture\n", This);
105
106     if(!device->isInDraw) {
107         /* ActivateContext sets isInDraw to TRUE when loading a pbuffer into a texture, thus no danger of
108          * recursive calls
109          */
110         ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
111     } else if (GL_SUPPORT(EXT_TEXTURE_SRGB) && This->baseTexture.bindCount > 0) {
112         srgb_mode = device->stateBlock->samplerState[This->baseTexture.sampler][WINED3DSAMP_SRGBTEXTURE];
113         srgb_was_toggled = This->baseTexture.is_srgb != srgb_mode;
114         This->baseTexture.is_srgb = srgb_mode;
115     }
116
117     IWineD3DTexture_BindTexture(iface);
118
119     if (This->resource.format == WINED3DFMT_P8 || This->resource.format == WINED3DFMT_A8P8) {
120         for (i = 0; i < This->baseTexture.levels; i++) {
121             if(palette9_changed((IWineD3DSurfaceImpl *)This->surfaces[i])) {
122                 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
123                 /* TODO: This is not necessarily needed with hw palettized texture support */
124                 IWineD3DSurface_LoadLocation(This->surfaces[i], SFLAG_INSYSMEM, NULL);
125                 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
126                 IWineD3DSurface_ModifyLocation(This->surfaces[i], SFLAG_INTEXTURE, FALSE);
127             }
128         }
129     }
130     /* If the texture is marked dirty or the srgb sampler setting has changed since the last load then reload the surfaces */
131     if (This->baseTexture.dirty) {
132         for (i = 0; i < This->baseTexture.levels; i++) {
133             IWineD3DSurface_LoadTexture(This->surfaces[i], srgb_mode);
134         }
135     } else if (srgb_was_toggled) {
136         if (This->baseTexture.srgb_mode_change_count < 20)
137             ++This->baseTexture.srgb_mode_change_count;
138         else
139             FIXME("Texture (%p) has been reloaded at least 20 times due to WINED3DSAMP_SRGBTEXTURE changes on it\'s sampler\n", This);
140
141         for (i = 0; i < This->baseTexture.levels; i++) {
142             IWineD3DSurface_AddDirtyRect(This->surfaces[i], NULL);
143             IWineD3DSurface_SetGlTextureDesc(This->surfaces[i], This->baseTexture.textureName, IWineD3DTexture_GetTextureDimensions(iface));
144             IWineD3DSurface_LoadTexture(This->surfaces[i], srgb_mode);
145         }
146     } else {
147         TRACE("(%p) Texture not dirty, nothing to do\n" , iface);
148     }
149
150     /* No longer dirty */
151     This->baseTexture.dirty = FALSE;
152
153     return ;
154 }
155
156 static void WINAPI IWineD3DTextureImpl_UnLoad(IWineD3DTexture *iface) {
157     unsigned int i;
158     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
159     TRACE("(%p)\n", This);
160
161     /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
162      * surface before, this one will be a NOP and vice versa. Unloading an unloaded
163      * surface is fine
164      */
165     for (i = 0; i < This->baseTexture.levels; i++) {
166         IWineD3DSurface_UnLoad(This->surfaces[i]);
167         IWineD3DSurface_SetGlTextureDesc(This->surfaces[i], 0, IWineD3DTexture_GetTextureDimensions(iface));
168     }
169
170     IWineD3DBaseTextureImpl_UnLoad((IWineD3DBaseTexture *) iface);
171 }
172
173 static WINED3DRESOURCETYPE WINAPI IWineD3DTextureImpl_GetType(IWineD3DTexture *iface) {
174     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
175 }
176
177 static HRESULT WINAPI IWineD3DTextureImpl_GetParent(IWineD3DTexture *iface, IUnknown **pParent) {
178     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
179 }
180
181 /* ******************************************************
182    IWineD3DTexture IWineD3DBaseTexture parts follow
183    ****************************************************** */
184 static DWORD WINAPI IWineD3DTextureImpl_SetLOD(IWineD3DTexture *iface, DWORD LODNew) {
185     return IWineD3DBaseTextureImpl_SetLOD((IWineD3DBaseTexture *)iface, LODNew);
186 }
187
188 static DWORD WINAPI IWineD3DTextureImpl_GetLOD(IWineD3DTexture *iface) {
189     return IWineD3DBaseTextureImpl_GetLOD((IWineD3DBaseTexture *)iface);
190 }
191
192 static DWORD WINAPI IWineD3DTextureImpl_GetLevelCount(IWineD3DTexture *iface) {
193     return IWineD3DBaseTextureImpl_GetLevelCount((IWineD3DBaseTexture *)iface);
194 }
195
196 static HRESULT WINAPI IWineD3DTextureImpl_SetAutoGenFilterType(IWineD3DTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
197   return IWineD3DBaseTextureImpl_SetAutoGenFilterType((IWineD3DBaseTexture *)iface, FilterType);
198 }
199
200 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DTextureImpl_GetAutoGenFilterType(IWineD3DTexture *iface) {
201   return IWineD3DBaseTextureImpl_GetAutoGenFilterType((IWineD3DBaseTexture *)iface);
202 }
203
204 static void WINAPI IWineD3DTextureImpl_GenerateMipSubLevels(IWineD3DTexture *iface) {
205     IWineD3DBaseTextureImpl_GenerateMipSubLevels((IWineD3DBaseTexture *)iface);
206 }
207
208 /* Internal function, No d3d mapping */
209 static BOOL WINAPI IWineD3DTextureImpl_SetDirty(IWineD3DTexture *iface, BOOL dirty) {
210     return IWineD3DBaseTextureImpl_SetDirty((IWineD3DBaseTexture *)iface, dirty);
211 }
212
213 static BOOL WINAPI IWineD3DTextureImpl_GetDirty(IWineD3DTexture *iface) {
214     return IWineD3DBaseTextureImpl_GetDirty((IWineD3DBaseTexture *)iface);
215 }
216
217 static HRESULT WINAPI IWineD3DTextureImpl_BindTexture(IWineD3DTexture *iface) {
218     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
219     BOOL set_gl_texture_desc = This->baseTexture.textureName == 0;
220     HRESULT hr;
221
222     TRACE("(%p) : relay to BaseTexture\n", This);
223
224     hr = IWineD3DBaseTextureImpl_BindTexture((IWineD3DBaseTexture *)iface);
225     if (set_gl_texture_desc && SUCCEEDED(hr)) {
226         UINT i;
227         for (i = 0; i < This->baseTexture.levels; ++i) {
228             IWineD3DSurface_SetGlTextureDesc(This->surfaces[i], This->baseTexture.textureName, IWineD3DTexture_GetTextureDimensions(iface));
229         }
230         /* Conditinal non power of two textures use a different clamping default. If we're using the GL_WINE_normalized_texrect
231          * partial driver emulation, we're dealing with a GL_TEXTURE_2D texture which has the address mode set to repeat - something
232          * that prevents us from hitting the accelerated codepath. Thus manually set the GL state. The same applies to filtering.
233          * Even if the texture has only one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW fallback on macos.
234          */
235         if(IWineD3DBaseTexture_IsCondNP2(iface)) {
236             ENTER_GL();
237             glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
238             checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
239             glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
240             checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
241             glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_MIN_FILTER, GL_NEAREST);
242             checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
243             glTexParameteri(IWineD3DTexture_GetTextureDimensions(iface), GL_TEXTURE_MAG_FILTER, GL_NEAREST);
244             checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
245             LEAVE_GL();
246             This->baseTexture.states[WINED3DTEXSTA_ADDRESSU]      = WINED3DTADDRESS_CLAMP;
247             This->baseTexture.states[WINED3DTEXSTA_ADDRESSV]      = WINED3DTADDRESS_CLAMP;
248             This->baseTexture.states[WINED3DTEXSTA_MAGFILTER]     = WINED3DTEXF_POINT;
249             This->baseTexture.states[WINED3DTEXSTA_MINFILTER]     = WINED3DTEXF_POINT;
250             This->baseTexture.states[WINED3DTEXSTA_MIPFILTER]     = WINED3DTEXF_NONE;
251         }
252     }
253
254     return hr;
255 }
256
257 static HRESULT WINAPI IWineD3DTextureImpl_UnBindTexture(IWineD3DTexture *iface) {
258     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
259     TRACE("(%p) : relay to BaseTexture\n", This);
260     return IWineD3DBaseTextureImpl_UnBindTexture((IWineD3DBaseTexture *)iface);
261 }
262
263 static UINT WINAPI IWineD3DTextureImpl_GetTextureDimensions(IWineD3DTexture *iface) {
264     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
265     TRACE("(%p)\n", This);
266
267     return This->target;
268 }
269
270 static BOOL WINAPI IWineD3DTextureImpl_IsCondNP2(IWineD3DTexture *iface) {
271     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
272     TRACE("(%p)\n", This);
273
274     return This->cond_np2;
275 }
276
277 static void WINAPI IWineD3DTextureImpl_ApplyStateChanges(IWineD3DTexture *iface,
278                                                    const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1],
279                                                    const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1]) {
280     TRACE("(%p) : relay to BaseTexture\n", iface);
281     IWineD3DBaseTextureImpl_ApplyStateChanges((IWineD3DBaseTexture *)iface, textureStates, samplerStates);
282 }
283
284 /* *******************************************
285    IWineD3DTexture IWineD3DTexture parts follow
286    ******************************************* */
287 static void WINAPI IWineD3DTextureImpl_Destroy(IWineD3DTexture *iface, D3DCB_DESTROYSURFACEFN D3DCB_DestroySurface) {
288     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
289     int i;
290
291     TRACE("(%p) : Cleaning up\n",This);
292     for (i = 0; i < This->baseTexture.levels; i++) {
293         if (This->surfaces[i] != NULL) {
294             /* Clean out the texture name we gave to the surface so that the surface doesn't try and release it */
295             IWineD3DSurface_SetGlTextureDesc(This->surfaces[i], 0, 0);
296             IWineD3DSurface_SetContainer(This->surfaces[i], 0);
297             D3DCB_DestroySurface(This->surfaces[i]);
298         }
299     }
300     TRACE("(%p) : cleaning up base texture\n", This);
301     IWineD3DBaseTextureImpl_CleanUp((IWineD3DBaseTexture *)iface);
302     /* free the object */
303     HeapFree(GetProcessHeap(), 0, This);
304 }
305
306 static HRESULT WINAPI IWineD3DTextureImpl_GetLevelDesc(IWineD3DTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
307     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
308
309     if (Level < This->baseTexture.levels) {
310         TRACE("(%p) Level (%d)\n", This, Level);
311         return IWineD3DSurface_GetDesc(This->surfaces[Level], pDesc);
312     }
313     FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
314     return WINED3DERR_INVALIDCALL;
315 }
316
317 static HRESULT WINAPI IWineD3DTextureImpl_GetSurfaceLevel(IWineD3DTexture *iface, UINT Level, IWineD3DSurface** ppSurfaceLevel) {
318     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
319     HRESULT hr = WINED3DERR_INVALIDCALL;
320
321     if (Level < This->baseTexture.levels) {
322         *ppSurfaceLevel = This->surfaces[Level];
323         IWineD3DSurface_AddRef(This->surfaces[Level]);
324         hr = WINED3D_OK;
325         TRACE("(%p) : returning %p for level %d\n", This, *ppSurfaceLevel, Level);
326     }
327     if (WINED3D_OK != hr) {
328         WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
329         *ppSurfaceLevel = NULL; /* Just to be on the safe side.. */
330     }
331     return hr;
332 }
333
334 static HRESULT WINAPI IWineD3DTextureImpl_LockRect(IWineD3DTexture *iface, UINT Level, WINED3DLOCKED_RECT *pLockedRect,
335                                             CONST RECT *pRect, DWORD Flags) {
336     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
337     HRESULT hr = WINED3DERR_INVALIDCALL;
338
339     if (Level < This->baseTexture.levels) {
340         hr = IWineD3DSurface_LockRect(This->surfaces[Level], pLockedRect, pRect, Flags);
341     }
342     if (WINED3D_OK == hr) {
343         TRACE("(%p) Level (%d) success\n", This, Level);
344     } else {
345         WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
346     }
347
348     return hr;
349 }
350
351 static HRESULT WINAPI IWineD3DTextureImpl_UnlockRect(IWineD3DTexture *iface, UINT Level) {
352    IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
353     HRESULT hr = WINED3DERR_INVALIDCALL;
354
355     if (Level < This->baseTexture.levels) {
356         hr = IWineD3DSurface_UnlockRect(This->surfaces[Level]);
357     }
358     if ( WINED3D_OK == hr) {
359         TRACE("(%p) Level (%d) success\n", This, Level);
360     } else {
361         WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
362     }
363     return hr;
364 }
365
366 static HRESULT WINAPI IWineD3DTextureImpl_AddDirtyRect(IWineD3DTexture *iface, CONST RECT* pDirtyRect) {
367     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
368     This->baseTexture.dirty = TRUE;
369     TRACE("(%p) : dirtyfication of surface Level (0)\n", This);
370     return IWineD3DSurface_AddDirtyRect(This->surfaces[0], pDirtyRect);
371 }
372
373 const IWineD3DTextureVtbl IWineD3DTexture_Vtbl =
374 {
375     /* IUnknown */
376     IWineD3DTextureImpl_QueryInterface,
377     IWineD3DTextureImpl_AddRef,
378     IWineD3DTextureImpl_Release,
379     /* IWineD3DResource */
380     IWineD3DTextureImpl_GetParent,
381     IWineD3DTextureImpl_GetDevice,
382     IWineD3DTextureImpl_SetPrivateData,
383     IWineD3DTextureImpl_GetPrivateData,
384     IWineD3DTextureImpl_FreePrivateData,
385     IWineD3DTextureImpl_SetPriority,
386     IWineD3DTextureImpl_GetPriority,
387     IWineD3DTextureImpl_PreLoad,
388     IWineD3DTextureImpl_UnLoad,
389     IWineD3DTextureImpl_GetType,
390     /* IWineD3DBaseTexture */
391     IWineD3DTextureImpl_SetLOD,
392     IWineD3DTextureImpl_GetLOD,
393     IWineD3DTextureImpl_GetLevelCount,
394     IWineD3DTextureImpl_SetAutoGenFilterType,
395     IWineD3DTextureImpl_GetAutoGenFilterType,
396     IWineD3DTextureImpl_GenerateMipSubLevels,
397     IWineD3DTextureImpl_SetDirty,
398     IWineD3DTextureImpl_GetDirty,
399     IWineD3DTextureImpl_BindTexture,
400     IWineD3DTextureImpl_UnBindTexture,
401     IWineD3DTextureImpl_GetTextureDimensions,
402     IWineD3DTextureImpl_IsCondNP2,
403     IWineD3DTextureImpl_ApplyStateChanges,
404     /* IWineD3DTexture */
405     IWineD3DTextureImpl_Destroy,
406     IWineD3DTextureImpl_GetLevelDesc,
407     IWineD3DTextureImpl_GetSurfaceLevel,
408     IWineD3DTextureImpl_LockRect,
409     IWineD3DTextureImpl_UnlockRect,
410     IWineD3DTextureImpl_AddDirtyRect
411 };