Fix gcc 4.0 -Wpointer-sign warnings.
[wine] / dlls / wined3d / texture.c
1 /*
2  * IDirect3DTexture9 implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  * Copyright 2002-2005 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    IWineD3DTexture IUnknown parts follow
31    ******************************************* */
32 HRESULT WINAPI IWineD3DTextureImpl_QueryInterface(IWineD3DTexture *iface, REFIID riid, LPVOID *ppobj)
33 {
34     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
35     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
36     if (IsEqualGUID(riid, &IID_IUnknown)
37         || IsEqualGUID(riid, &IID_IWineD3DResource)
38         || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
39         || IsEqualGUID(riid, &IID_IWineD3DTexture)){
40         IUnknown_AddRef(iface);
41         *ppobj = This;
42         return D3D_OK;
43     }
44     return E_NOINTERFACE;
45 }
46
47 ULONG WINAPI IWineD3DTextureImpl_AddRef(IWineD3DTexture *iface) {
48     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
49     TRACE("(%p) : AddRef increasing from %ld\n", This, This->resource.ref);
50     IUnknown_AddRef(This->resource.parent);
51     return InterlockedIncrement(&This->resource.ref);
52 }
53
54 ULONG WINAPI IWineD3DTextureImpl_Release(IWineD3DTexture *iface) {
55     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
56     ULONG ref;
57     TRACE("(%p) : Releasing from %ld\n", This, This->resource.ref);
58     ref = InterlockedDecrement(&This->resource.ref);
59     if (ref == 0) {
60         int i;
61         for (i = 0; i < This->baseTexture.levels; i++) {
62             if (This->surfaces[i] != NULL) {
63                 /* Because the surfaces were created using a callback we need to release there parent otehrwise we leave the parent hanging */
64                 IUnknown* surfaceParent;
65                 IWineD3DSurface_GetParent(This->surfaces[i], &surfaceParent);
66                 IUnknown_Release(surfaceParent);
67                 IUnknown_Release(surfaceParent);
68             }
69         }
70
71         IWineD3DBaseTextureImpl_CleanUp((IWineD3DBaseTexture *)iface);
72         HeapFree(GetProcessHeap(), 0, This);
73     } else {
74         IUnknown_Release(This->resource.parent);  /* Released the reference to the d3dx object */
75     }
76     return ref;
77 }
78
79 /* ****************************************************
80    IWineD3DTexture IWineD3DResource parts follow
81    **************************************************** */
82 HRESULT WINAPI IWineD3DTextureImpl_GetDevice(IWineD3DTexture *iface, IWineD3DDevice** ppDevice) {
83     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
84 }
85
86 HRESULT WINAPI IWineD3DTextureImpl_SetPrivateData(IWineD3DTexture *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 IWineD3DTextureImpl_GetPrivateData(IWineD3DTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
91     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
92 }
93
94 HRESULT WINAPI IWineD3DTextureImpl_FreePrivateData(IWineD3DTexture *iface, REFGUID refguid) {
95     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
96 }
97
98 DWORD WINAPI IWineD3DTextureImpl_SetPriority(IWineD3DTexture *iface, DWORD PriorityNew) {
99     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
100 }
101
102 DWORD WINAPI IWineD3DTextureImpl_GetPriority(IWineD3DTexture *iface) {
103     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
104 }
105
106 void WINAPI IWineD3DTextureImpl_PreLoad(IWineD3DTexture *iface) {
107
108     /* Override the IWineD3DResource PreLoad method */
109     unsigned int i;
110     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
111
112     TRACE("(%p) : About to load texture\n", This);
113
114     IWineD3DTexture_BindTexture(iface);
115     ENTER_GL();
116     /* If were dirty then reload the surfaces */
117     if(This->baseTexture.dirty != FALSE) {
118         for (i = 0; i < This->baseTexture.levels; i++) {
119              IWineD3DSurface_LoadTexture(This->surfaces[i], GL_TEXTURE_2D, i);
120         }
121
122         /* No longer dirty */
123         This->baseTexture.dirty = FALSE;
124     }
125     LEAVE_GL();
126
127     return ;
128 }
129
130 D3DRESOURCETYPE WINAPI IWineD3DTextureImpl_GetType(IWineD3DTexture *iface) {
131     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
132 }
133
134 HRESULT WINAPI IWineD3DTextureImpl_GetParent(IWineD3DTexture *iface, IUnknown **pParent) {
135     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
136 }
137
138 /* ******************************************************
139    IWineD3DTexture IWineD3DBaseTexture parts follow
140    ****************************************************** */
141 DWORD WINAPI IWineD3DTextureImpl_SetLOD(IWineD3DTexture *iface, DWORD LODNew) {
142     return IWineD3DBaseTextureImpl_SetLOD((IWineD3DBaseTexture *)iface, LODNew);
143 }
144
145 DWORD WINAPI IWineD3DTextureImpl_GetLOD(IWineD3DTexture *iface) {
146     return IWineD3DBaseTextureImpl_GetLOD((IWineD3DBaseTexture *)iface);
147 }
148
149 DWORD WINAPI IWineD3DTextureImpl_GetLevelCount(IWineD3DTexture *iface) {
150     return IWineD3DBaseTextureImpl_GetLevelCount((IWineD3DBaseTexture *)iface);
151 }
152
153 HRESULT WINAPI IWineD3DTextureImpl_SetAutoGenFilterType(IWineD3DTexture *iface, D3DTEXTUREFILTERTYPE FilterType) {
154   return IWineD3DBaseTextureImpl_SetAutoGenFilterType((IWineD3DBaseTexture *)iface, FilterType);
155 }
156
157 D3DTEXTUREFILTERTYPE WINAPI IWineD3DTextureImpl_GetAutoGenFilterType(IWineD3DTexture *iface) {
158   return IWineD3DBaseTextureImpl_GetAutoGenFilterType((IWineD3DBaseTexture *)iface);
159 }
160
161 void WINAPI IWineD3DTextureImpl_GenerateMipSubLevels(IWineD3DTexture *iface) {
162   return IWineD3DBaseTextureImpl_GenerateMipSubLevels((IWineD3DBaseTexture *)iface);
163 }
164
165 /* Internal function, No d3d mapping */
166 BOOL WINAPI IWineD3DTextureImpl_SetDirty(IWineD3DTexture *iface, BOOL dirty) {
167     return IWineD3DBaseTextureImpl_SetDirty((IWineD3DBaseTexture *)iface, TRUE);
168 }
169
170 BOOL WINAPI IWineD3DTextureImpl_GetDirty(IWineD3DTexture *iface) {
171     return IWineD3DBaseTextureImpl_GetDirty((IWineD3DBaseTexture *)iface);
172 }
173
174 HRESULT WINAPI IWineD3DTextureImpl_BindTexture(IWineD3DTexture *iface) {
175     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
176     TRACE("(%p) : relay to BaseTexture \n", This);
177     return IWineD3DBaseTextureImpl_BindTexture((IWineD3DBaseTexture *)iface);
178 }
179
180 HRESULT WINAPI IWineD3DTextureImpl_UnBindTexture(IWineD3DTexture *iface) {
181     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
182     TRACE("(%p) : relay to BaseTexture \n", This);
183     return IWineD3DBaseTextureImpl_UnBindTexture((IWineD3DBaseTexture *)iface);
184 }
185
186 UINT WINAPI IWineD3DTextureImpl_GetTextureDimensions(IWineD3DTexture *iface) {
187     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
188     TRACE("(%p) \n", This);
189
190     return GL_TEXTURE_2D;
191 }
192
193
194 /* *******************************************
195    IWineD3DTexture IWineD3DTexture parts follow
196    ******************************************* */
197 HRESULT WINAPI IWineD3DTextureImpl_GetLevelDesc(IWineD3DTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
198     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
199
200     if (Level < This->baseTexture.levels) {
201         TRACE("(%p) Level (%d)\n", This, Level);
202         return IWineD3DSurface_GetDesc((IWineD3DSurface *) This->surfaces[Level], pDesc);
203     }
204     FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
205     return D3DERR_INVALIDCALL;
206 }
207
208 HRESULT WINAPI IWineD3DTextureImpl_GetSurfaceLevel(IWineD3DTexture *iface, UINT Level, IWineD3DSurface** ppSurfaceLevel) {
209     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
210     HRESULT hr = D3DERR_INVALIDCALL;
211
212     if (Level < This->baseTexture.levels) {
213         *ppSurfaceLevel = This->surfaces[Level];
214         IWineD3DSurface_AddRef((IWineD3DSurface*) This->surfaces[Level]);
215         hr = D3D_OK;
216         TRACE("(%p) : returning %p for level %d\n", This, *ppSurfaceLevel, Level);
217     }
218     if (D3D_OK != hr) {
219         WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
220         *ppSurfaceLevel = NULL; /* Just to be on the safe side.. */
221     }
222     return hr;
223 }
224
225 HRESULT WINAPI IWineD3DTextureImpl_LockRect(IWineD3DTexture *iface, UINT Level, D3DLOCKED_RECT *pLockedRect, 
226                                             CONST RECT *pRect, DWORD Flags) {
227     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
228     HRESULT hr = D3DERR_INVALIDCALL;
229
230     if (Level < This->baseTexture.levels) {
231         hr = IWineD3DSurface_LockRect((IWineD3DSurface *) This->surfaces[Level], pLockedRect, pRect, Flags);
232     }
233     if (D3D_OK == hr) {
234         TRACE("(%p) Level (%d) success\n", This, Level);
235     } else {
236         WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
237     }
238
239     return hr;
240 }
241
242 HRESULT WINAPI IWineD3DTextureImpl_UnlockRect(IWineD3DTexture *iface, UINT Level) {
243    IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
244     HRESULT hr = D3DERR_INVALIDCALL;
245
246     if (Level < This->baseTexture.levels) {
247         hr = IWineD3DSurface_UnlockRect(This->surfaces[Level]);
248     }
249     if ( D3D_OK == hr) {
250         TRACE("(%p) Level (%d) success\n", This, Level);
251     } else {
252         WARN("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
253     }
254     return hr;
255 }
256
257 HRESULT WINAPI IWineD3DTextureImpl_AddDirtyRect(IWineD3DTexture *iface, CONST RECT* pDirtyRect) {
258     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
259     This->baseTexture.dirty = TRUE;
260     TRACE("(%p) : dirtyfication of surface Level (0)\n", This);    
261     return IWineD3DSurface_AddDirtyRect((IWineD3DSurface *)This->surfaces[0], pDirtyRect);
262 }
263
264 const IWineD3DTextureVtbl IWineD3DTexture_Vtbl =
265 {
266     /* IUnknown */
267     IWineD3DTextureImpl_QueryInterface,
268     IWineD3DTextureImpl_AddRef,
269     IWineD3DTextureImpl_Release,
270     /* IWineD3DResource */
271     IWineD3DTextureImpl_GetParent,
272     IWineD3DTextureImpl_GetDevice,
273     IWineD3DTextureImpl_SetPrivateData,
274     IWineD3DTextureImpl_GetPrivateData,
275     IWineD3DTextureImpl_FreePrivateData,
276     IWineD3DTextureImpl_SetPriority,
277     IWineD3DTextureImpl_GetPriority,
278     IWineD3DTextureImpl_PreLoad,
279     IWineD3DTextureImpl_GetType,
280     /* IWineD3DBaseTexture */
281     IWineD3DTextureImpl_SetLOD,
282     IWineD3DTextureImpl_GetLOD,
283     IWineD3DTextureImpl_GetLevelCount,
284     IWineD3DTextureImpl_SetAutoGenFilterType,
285     IWineD3DTextureImpl_GetAutoGenFilterType,
286     IWineD3DTextureImpl_GenerateMipSubLevels,
287     IWineD3DTextureImpl_SetDirty,
288     IWineD3DTextureImpl_GetDirty,
289     IWineD3DTextureImpl_BindTexture,
290     IWineD3DTextureImpl_UnBindTexture,
291     IWineD3DTextureImpl_GetTextureDimensions,
292     /* IWineD3DTexture */
293     IWineD3DTextureImpl_GetLevelDesc,
294     IWineD3DTextureImpl_GetSurfaceLevel,
295     IWineD3DTextureImpl_LockRect,
296     IWineD3DTextureImpl_UnlockRect,
297     IWineD3DTextureImpl_AddDirtyRect
298 };