Validate that the texture is no larger than the graphics cards largest
[wine] / dlls / wined3d / basetexture.c
1 /*
2  * IWineD3DBaseTexture Implementation
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  * Copyright 2002-2004 Raphael Junqueira
6  * Copyright 2005 Oliver Stieber
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24 #include "wined3d_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->resource.wineD3DDevice)->wineD3D))->gl_info
28
29 /* *******************************************
30    IWineD3DBaseTexture IUnknown parts follow
31    ******************************************* */
32 HRESULT WINAPI IWineD3DBaseTextureImpl_QueryInterface(IWineD3DBaseTexture *iface, REFIID riid, LPVOID *ppobj)
33 {
34     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
35     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
36     /* FIXME: This needs to extend an IWineD3DBaseObject */
37     if (IsEqualGUID(riid, &IID_IUnknown)
38         || IsEqualGUID(riid, &IID_IWineD3DResource)
39         || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)) {
40         IUnknown_AddRef(iface);
41         *ppobj = This;
42         return D3D_OK;
43     }
44     return E_NOINTERFACE;
45 }
46
47 ULONG WINAPI IWineD3DBaseTextureImpl_AddRef(IWineD3DBaseTexture *iface) {
48     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
49     ULONG ref = InterlockedIncrement(&This->resource.ref);
50
51     TRACE("(%p) : AddRef increasing from %ld\n", This,ref - 1);
52     return ref;
53 }
54
55 ULONG WINAPI IWineD3DBaseTextureImpl_Release(IWineD3DBaseTexture *iface) {
56     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
57     ULONG ref = InterlockedDecrement(&This->resource.ref);
58     TRACE("(%p) : Releasing from %ld\n", This, ref + 1);
59     if (ref == 0) {
60         IWineD3DBaseTextureImpl_CleanUp(iface);
61         HeapFree(GetProcessHeap(), 0, This);
62     }
63     return ref;
64 }
65
66 /* class static */
67 void IWineD3DBaseTextureImpl_CleanUp(IWineD3DBaseTexture *iface) {
68     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
69     TRACE("(%p) : textureName(%d)\n", This, This->baseTexture.textureName);
70     if (This->baseTexture.textureName != 0) {
71         ENTER_GL();
72         TRACE("(%p) : Deleting texture %d\n", This, This->baseTexture.textureName);
73         glDeleteTextures(1, &This->baseTexture.textureName);
74         LEAVE_GL();
75     }
76     IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
77 }
78
79 /* ****************************************************
80    IWineD3DBaseTexture IWineD3DResource parts follow
81    **************************************************** */
82 HRESULT WINAPI IWineD3DBaseTextureImpl_GetDevice(IWineD3DBaseTexture *iface, IWineD3DDevice** ppDevice) {
83     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
84 }
85
86 HRESULT WINAPI IWineD3DBaseTextureImpl_SetPrivateData(IWineD3DBaseTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
87     return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
88 }
89
90 HRESULT WINAPI IWineD3DBaseTextureImpl_GetPrivateData(IWineD3DBaseTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
91     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
92 }
93
94 HRESULT WINAPI IWineD3DBaseTextureImpl_FreePrivateData(IWineD3DBaseTexture *iface, REFGUID refguid) {
95     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
96 }
97
98 DWORD    WINAPI        IWineD3DBaseTextureImpl_SetPriority(IWineD3DBaseTexture *iface, DWORD PriorityNew) {
99     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
100 }
101
102 DWORD    WINAPI        IWineD3DBaseTextureImpl_GetPriority(IWineD3DBaseTexture *iface) {
103     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
104 }
105
106 void     WINAPI        IWineD3DBaseTextureImpl_PreLoad(IWineD3DBaseTexture *iface) {
107     return IWineD3DResourceImpl_PreLoad((IWineD3DResource *)iface);
108 }
109
110 D3DRESOURCETYPE WINAPI IWineD3DBaseTextureImpl_GetType(IWineD3DBaseTexture *iface) {
111     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
112 }
113
114 HRESULT WINAPI IWineD3DBaseTextureImpl_GetParent(IWineD3DBaseTexture *iface, IUnknown **pParent) {
115     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
116 }
117
118 /* ******************************************************
119    IWineD3DBaseTexture IWineD3DBaseTexture parts follow
120    ****************************************************** */
121
122 /* There is no OpenGL equivilent of setLOD, getLOD, all they do it priortise testure loading
123  * so just pretend that they work unless something really needs a failure. */
124 DWORD WINAPI IWineD3DBaseTextureImpl_SetLOD(IWineD3DBaseTexture *iface, DWORD LODNew) {
125     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
126
127     if (This->resource.pool != D3DPOOL_MANAGED) {
128         return  D3DERR_INVALIDCALL;
129     }
130
131     if(LODNew >= This->baseTexture.levels)
132         LODNew = This->baseTexture.levels - 1;
133      This->baseTexture.LOD = LODNew;
134
135     TRACE("(%p) : set bogus LOD to %d \n", This, This->baseTexture.LOD);
136
137     return This->baseTexture.LOD;
138 }
139
140 DWORD WINAPI IWineD3DBaseTextureImpl_GetLOD(IWineD3DBaseTexture *iface) {
141     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
142
143     if (This->resource.pool != D3DPOOL_MANAGED) {
144         return  D3DERR_INVALIDCALL;
145     }
146
147     TRACE("(%p) : returning %d \n", This, This->baseTexture.LOD);
148
149     return This->baseTexture.LOD;
150 }
151
152 DWORD WINAPI IWineD3DBaseTextureImpl_GetLevelCount(IWineD3DBaseTexture *iface) {
153     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
154     TRACE("(%p) : returning %d\n", This, This->baseTexture.levels);
155     return This->baseTexture.levels;
156 }
157
158 HRESULT WINAPI IWineD3DBaseTextureImpl_SetAutoGenFilterType(IWineD3DBaseTexture *iface, D3DTEXTUREFILTERTYPE FilterType) {
159   IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
160
161   if (!(This->baseTexture.usage & D3DUSAGE_AUTOGENMIPMAP)) {
162       TRACE("(%p) : returning invalid call\n", This);
163       return D3DERR_INVALIDCALL;
164   }
165   This->baseTexture.filterType = FilterType;
166   TRACE("(%p) : \n", This);
167   return D3D_OK;
168 }
169
170 D3DTEXTUREFILTERTYPE WINAPI IWineD3DBaseTextureImpl_GetAutoGenFilterType(IWineD3DBaseTexture *iface) {
171   IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
172   FIXME("(%p) : stub\n", This);
173   if (!(This->baseTexture.usage & D3DUSAGE_AUTOGENMIPMAP)) {
174      return D3DTEXF_NONE;
175   }
176   return This->baseTexture.filterType;
177   return D3DTEXF_LINEAR; /* default */
178 }
179
180 void WINAPI IWineD3DBaseTextureImpl_GenerateMipSubLevels(IWineD3DBaseTexture *iface) {
181   IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
182   /* TODO: implement filters using GL_SGI_generate_mipmaps http://oss.sgi.com/projects/ogl-sample/registry/SGIS/generate_mipmap.txt */
183   FIXME("(%p) : stub\n", This);
184   return ;
185 }
186
187 /* Internal function, No d3d mapping */
188 BOOL WINAPI IWineD3DBaseTextureImpl_SetDirty(IWineD3DBaseTexture *iface, BOOL dirty) {
189     BOOL old;
190     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
191     old = This->baseTexture.dirty;
192     This->baseTexture.dirty = dirty;
193     return old;
194 }
195
196 BOOL WINAPI IWineD3DBaseTextureImpl_GetDirty(IWineD3DBaseTexture *iface) {
197     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
198     return This->baseTexture.dirty;
199 }
200
201 HRESULT WINAPI IWineD3DBaseTextureImpl_BindTexture(IWineD3DBaseTexture *iface) {
202     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
203     HRESULT hr = D3D_OK;
204     UINT textureDimensions;
205     BOOL isNewTexture = FALSE;
206     TRACE("(%p) : About to bind texture\n", This);
207
208     textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
209     ENTER_GL();
210 #if 0 /* TODO: context manager support */
211      IWineD3DContextManager_PushState(This->contextManager, textureDimensions, ENABLED, NOW /* make sure the state is applied now */);
212 #else
213     glEnable(textureDimensions);
214 #endif
215
216     /* Generate a texture name if we don't already have one */
217     if (This->baseTexture.textureName == 0) {
218         glGenTextures(1, &This->baseTexture.textureName);
219         checkGLcall("glGenTextures");
220         TRACE("Generated texture %d\n", This->baseTexture.textureName);
221          if (This->resource.pool == D3DPOOL_DEFAULT) {
222             /* Tell opengl to try and keep this texture in video ram (well mostly) */
223             GLclampf tmp;
224             tmp = 0.9f;
225             glPrioritizeTextures(1, &This->baseTexture.textureName, &tmp);
226          }
227         IWineD3DBaseTexture_SetDirty(iface, TRUE);
228         isNewTexture = TRUE;
229     }
230
231     /* Bind the texture */
232     if (This->baseTexture.textureName != 0) {
233         /* Always need to reset the number of mipmap levels when rebinding as it is
234         a property of the active texture unit, and another texture may have set it
235         to a different value                                                       */
236         if (This->baseTexture.levels > 0) {
237             TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->baseTexture.levels - 1);
238             glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels - 1);
239             checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels)");
240         } else {
241            glTexParameteri(textureDimensions, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
242         }
243         glBindTexture(textureDimensions, This->baseTexture.textureName);
244         checkGLcall("glBindTexture");
245         if (isNewTexture) {
246             /* For a new texture we have to set the textures levels after binding the texture,
247             * in theory this is all we should ever have to dom, but because ATI's drivers are broken we
248             * also need to set the texture dimensins before the texture is is set */
249             TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->baseTexture.levels - 1);
250             glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels - 1);
251             checkGLcall("glTexParameteri(textureDimensions, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels)");
252         }
253     } else { /* this only happened if we've run out of openGL textures */
254         WARN("This texture doesn't have an openGL texture assigned to it\n");
255         hr =  D3DERR_INVALIDCALL;
256     }
257
258     LEAVE_GL();
259     return hr;
260 }
261
262 HRESULT WINAPI IWineD3DBaseTextureImpl_UnBindTexture(IWineD3DBaseTexture *iface) {
263     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
264     UINT textureDimensions;
265
266     TRACE("(%p) : About to bind texture\n", This);
267     textureDimensions = IWineD3DBaseTexture_GetTextureDimensions(iface);
268
269     ENTER_GL();
270
271     glBindTexture(textureDimensions, 0);
272 #if 0 /* TODO: context manager support */
273      IWineD3DContextManager_PopState(This->contextManager, textureDimensions, ENABLED, NOW /* make sure the state is applied now */);
274 #else
275     glDisable(textureDimensions);
276 #endif
277
278     LEAVE_GL();
279     return D3D_OK;
280 }
281
282 UINT WINAPI IWineD3DBaseTextureImpl_GetTextureDimensions(IWineD3DBaseTexture *iface){
283     IWineD3DBaseTextureImpl *This = (IWineD3DBaseTextureImpl *)iface;
284     FIXME("(%p) : This shouldn't be called\n", This);
285     return D3D_OK;
286 }
287
288 static const IWineD3DBaseTextureVtbl IWineD3DBaseTexture_Vtbl =
289 {
290     /* IUnknown */
291     IWineD3DBaseTextureImpl_QueryInterface,
292     IWineD3DBaseTextureImpl_AddRef,
293     IWineD3DBaseTextureImpl_Release,
294     /* IWineD3DResource */
295     IWineD3DBaseTextureImpl_GetParent,
296     IWineD3DBaseTextureImpl_GetDevice,
297     IWineD3DBaseTextureImpl_SetPrivateData,
298     IWineD3DBaseTextureImpl_GetPrivateData,
299     IWineD3DBaseTextureImpl_FreePrivateData,
300     IWineD3DBaseTextureImpl_SetPriority,
301     IWineD3DBaseTextureImpl_GetPriority,
302     IWineD3DBaseTextureImpl_PreLoad,
303     IWineD3DBaseTextureImpl_GetType,
304     /*IWineD3DBaseTexture*/
305     IWineD3DBaseTextureImpl_SetLOD,
306     IWineD3DBaseTextureImpl_GetLOD,
307     IWineD3DBaseTextureImpl_GetLevelCount,
308     IWineD3DBaseTextureImpl_SetAutoGenFilterType,
309     IWineD3DBaseTextureImpl_GetAutoGenFilterType,
310     IWineD3DBaseTextureImpl_GenerateMipSubLevels,
311     IWineD3DBaseTextureImpl_SetDirty,
312     IWineD3DBaseTextureImpl_GetDirty,
313     /* internal */
314     IWineD3DBaseTextureImpl_BindTexture,
315     IWineD3DBaseTextureImpl_UnBindTexture,
316     IWineD3DBaseTextureImpl_GetTextureDimensions
317
318 };