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