Encapsulate texture, surface encapsulated.
[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
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                 IWineD3DSurface_GetParent(This->surfaces[i], &surfaceParent);
67                 IUnknown_Release(surfaceParent);
68                 IUnknown_Release(surfaceParent);
69             }
70         }
71         if (This->baseTexture.textureName != 0) {
72             ENTER_GL();
73             TRACE("Deleting texture %d\n", This->baseTexture.textureName);
74             glDeleteTextures(1, &This->baseTexture.textureName);
75             LEAVE_GL();
76         }
77         IWineD3DDevice_Release((IWineD3DDevice *)This->resource.wineD3DDevice);
78         HeapFree(GetProcessHeap(), 0, This);
79     } else {
80         IUnknown_Release(This->resource.parent);  /* Released the reference to the d3dx object */
81     }
82     return ref;
83 }
84
85 /* ****************************************************
86    IWineD3DTexture IWineD3DResource parts follow
87    **************************************************** */
88 HRESULT WINAPI IWineD3DTextureImpl_GetDevice(IWineD3DTexture *iface, IWineD3DDevice** ppDevice) {
89     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
90 }
91
92 HRESULT WINAPI IWineD3DTextureImpl_SetPrivateData(IWineD3DTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
93     return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
94 }
95
96 HRESULT WINAPI IWineD3DTextureImpl_GetPrivateData(IWineD3DTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
97     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
98 }
99
100 HRESULT WINAPI IWineD3DTextureImpl_FreePrivateData(IWineD3DTexture *iface, REFGUID refguid) {
101     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
102 }
103
104 DWORD WINAPI IWineD3DTextureImpl_SetPriority(IWineD3DTexture *iface, DWORD PriorityNew) {
105     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
106 }
107
108 DWORD WINAPI IWineD3DTextureImpl_GetPriority(IWineD3DTexture *iface) {
109     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
110 }
111
112 void WINAPI IWineD3DTextureImpl_PreLoad(IWineD3DTexture *iface) {
113     /* Override the IWineD3DResource Preload method */
114     unsigned int i;
115     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
116     
117     TRACE("(%p) : About to load texture\n", This);
118
119     ENTER_GL();
120
121 #if 0 /* TODO: context manager support */
122      IWineD3DContextManager_PushState(This->contextManager, GL_TEXTURE_2D, ENABLED, NOW /* make sure the state is applied now */);
123 #endif
124     
125     /* Generate a texture name if we don't already have one */    
126     if (This->baseTexture.textureName == 0) {
127         glGenTextures(1, &This->baseTexture.textureName);
128         checkGLcall("glGenTextures");
129         TRACE("Generated texture %d\n", This->baseTexture.textureName);
130          if (This->baseTexture.pool == D3DPOOL_DEFAULT) {
131             /* Tell opengl to try and keep this texture in video ram (well mostly) */
132             GLclampf tmp;
133             tmp = 0.9f;
134             glPrioritizeTextures(1, &This->baseTexture.textureName, &tmp);
135          }        
136     }
137
138     /* Bind the texture */    
139     if (This->baseTexture.textureName != 0) {
140         glBindTexture(GL_TEXTURE_2D, This->baseTexture.textureName);
141         checkGLcall("glBindTexture");
142     } else { /* this only happened if we've run out of openGL textures */
143         WARN("This texture doesn't have an openGL texture assigned to it\n");
144         return;
145     }
146
147     /* If were dirty then reload the surfaces */
148     if(This->baseTexture.dirty != FALSE) {
149         for (i = 0; i < This->baseTexture.levels; i++) {
150              IWineD3DSurface_LoadTexture(This->surfaces[i], GL_TEXTURE_2D, i);
151         }
152         
153         /* No longer dirty */
154         This->baseTexture.dirty = FALSE;        
155     }
156
157     /* Always need to reset the number of mipmap levels when rebinding as it is
158        a property of the active texture unit, and another texture may have set it
159        to a different value                                                       */
160     TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->baseTexture.levels - 1);   
161     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels - 1);
162     checkGLcall("glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, This->levels)");
163     /* TODO: disable texture support, if it wastn't enabled when we entered. */
164 #if 0 /* TODO: context manager support */
165      IWineD3DContextManager_PopState(This->contextManager, GL_TEXTURE_2D, DISABLED,DELAYED
166               /* we don't care when the state is disabled(if atall) */);
167 #endif    
168
169     LEAVE_GL();
170
171     return ;
172 }
173
174 D3DRESOURCETYPE WINAPI IWineD3DTextureImpl_GetType(IWineD3DTexture *iface) {
175     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
176 }
177
178 HRESULT WINAPI IWineD3DTextureImpl_GetParent(IWineD3DTexture *iface, IUnknown **pParent) {
179     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
180 }
181
182 /* ******************************************************
183    IWineD3DTexture IWineD3DBaseTexture parts follow
184    ****************************************************** */
185 DWORD WINAPI IWineD3DTextureImpl_SetLOD(IWineD3DTexture *iface, DWORD LODNew) {
186     return IWineD3DBaseTextureImpl_SetLOD((IWineD3DBaseTexture *)iface, LODNew);
187 }
188
189 DWORD WINAPI IWineD3DTextureImpl_GetLOD(IWineD3DTexture *iface) {
190     return IWineD3DBaseTextureImpl_GetLOD((IWineD3DBaseTexture *)iface);
191 }
192
193 DWORD WINAPI IWineD3DTextureImpl_GetLevelCount(IWineD3DTexture *iface) {
194     return IWineD3DBaseTextureImpl_GetLevelCount((IWineD3DBaseTexture *)iface);
195 }
196
197 HRESULT WINAPI IWineD3DTextureImpl_SetAutoGenFilterType(IWineD3DTexture *iface, D3DTEXTUREFILTERTYPE FilterType) {
198   return IWineD3DBaseTextureImpl_SetAutoGenFilterType((IWineD3DBaseTexture *)iface, FilterType);
199 }
200
201 D3DTEXTUREFILTERTYPE WINAPI IWineD3DTextureImpl_GetAutoGenFilterType(IWineD3DTexture *iface) {
202   return IWineD3DBaseTextureImpl_GetAutoGenFilterType((IWineD3DBaseTexture *)iface);
203 }
204
205 void WINAPI IWineD3DTextureImpl_GenerateMipSubLevels(IWineD3DTexture *iface) {
206   return IWineD3DBaseTextureImpl_GenerateMipSubLevels((IWineD3DBaseTexture *)iface);
207 }
208
209 /* Internal function, No d3d mapping */
210 BOOL WINAPI IWineD3DTextureImpl_SetDirty(IWineD3DTexture *iface, BOOL dirty) {
211     return IWineD3DBaseTextureImpl_SetDirty((IWineD3DBaseTexture *)iface, TRUE);
212 }
213
214 BOOL WINAPI IWineD3DTextureImpl_GetDirty(IWineD3DTexture *iface) {
215     return IWineD3DBaseTextureImpl_GetDirty((IWineD3DBaseTexture *)iface);
216 }
217
218 HRESULT WINAPI IWineD3DTextureImpl_BindTexture(IWineD3DTexture *iface) {
219     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
220     TRACE("(%p) : %d \n", This, This->baseTexture.textureName);
221 #if 0 /* TODO: context manager support */
222      IWineD3DContextManager_PushState(This->contextManager, GL_TEXTURE_2D, ENABLED, NOW /* make sure the state is applied now */);
223 #endif
224     ENTER_GL();
225     IWineD3DTexture_PreLoad(iface); /* make sure the textures is preloaded */
226 #if 1 /* TODO: context manager support */
227     glEnable(GL_TEXTURE_2D);    /* all this enable disable stuff is a bit of a mess */
228 #endif
229     glBindTexture(GL_TEXTURE_2D, This->baseTexture.textureName);
230     LEAVE_GL();
231     return D3D_OK;
232 }
233
234 HRESULT WINAPI IWineD3DTextureImpl_UnBindTexture(IWineD3DTexture *iface) {
235     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
236     TRACE("(%p) \n", This);
237 #if 0 /* TODO: context manager support */
238     IWineD3DContextManager_PopState(This->contextManager, GL_TEXTURE_2D, DISABLED, DELAYED);
239 #endif
240     ENTER_GL();
241 #if 1 /* TODO: context manager support */
242     glBindTexture(GL_TEXTURE_2D, 0);
243 #endif
244     glDisable(GL_TEXTURE_2D); /* This should be queued! so that we don't keep enabling and disabling states. */
245     LEAVE_GL();
246     return D3D_OK;
247 }
248
249 UINT WINAPI IWineD3DTextureImpl_GetTextureDimensions(IWineD3DTexture *iface) {
250     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
251     TRACE("(%p) \n", This);
252
253     return GL_TEXTURE_2D;
254 }
255
256
257 /* *******************************************
258    IWineD3DTexture IWineD3DTexture parts follow
259    ******************************************* */
260 HRESULT WINAPI IWineD3DTextureImpl_GetLevelDesc(IWineD3DTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
261     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
262
263     if (Level < This->baseTexture.levels) {
264         TRACE("(%p) Level (%d)\n", This, Level);
265         return IWineD3DSurface_GetDesc((IWineD3DSurface *) This->surfaces[Level], pDesc);
266     }
267     FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
268     return D3DERR_INVALIDCALL;
269 }
270
271 HRESULT WINAPI IWineD3DTextureImpl_GetSurfaceLevel(IWineD3DTexture *iface, UINT Level, IWineD3DSurface** ppSurfaceLevel) {
272     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
273     *ppSurfaceLevel = (IWineD3DSurface *) This->surfaces[Level];
274     IWineD3DSurface_AddRef((IWineD3DSurface *) This->surfaces[Level]);
275     TRACE("(%p) : returning %p for level %d\n", This, *ppSurfaceLevel, Level);
276     return D3D_OK;
277 }
278
279 HRESULT WINAPI IWineD3DTextureImpl_LockRect(IWineD3DTexture *iface, UINT Level, D3DLOCKED_RECT *pLockedRect, 
280                                             CONST RECT *pRect, DWORD Flags) {
281     HRESULT hr;
282     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
283
284     if (Level < This->baseTexture.levels) {
285
286         hr = IWineD3DSurface_LockRect((IWineD3DSurface *) This->surfaces[Level], pLockedRect, pRect, Flags);
287         TRACE("(%p) Level (%d) success(%lu)\n", This, Level, hr);
288     } else {
289         FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
290         return D3DERR_INVALIDCALL;
291     }
292     return hr;
293 }
294
295 HRESULT WINAPI IWineD3DTextureImpl_UnlockRect(IWineD3DTexture *iface, UINT Level) {
296     HRESULT hr;
297     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
298
299     if (Level < This->baseTexture.levels) {
300         hr = IWineD3DSurface_UnlockRect((IWineD3DSurface *) This->surfaces[Level]);
301         TRACE("(%p) Level (%d) success(%lu)\n", This, Level, hr);
302     } else {
303         FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
304         return D3DERR_INVALIDCALL;
305     }
306     return hr;
307 }
308
309 HRESULT WINAPI IWineD3DTextureImpl_AddDirtyRect(IWineD3DTexture *iface, CONST RECT* pDirtyRect) {
310     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
311     This->baseTexture.dirty = TRUE;
312     TRACE("(%p) : dirtyfication of surface Level (0)\n", This);    
313     return IWineD3DSurface_AddDirtyRect((IWineD3DSurface *)This->surfaces[0], pDirtyRect);
314 }
315
316 IWineD3DTextureVtbl IWineD3DTexture_Vtbl =
317 {
318     /* IUnknown */
319     IWineD3DTextureImpl_QueryInterface,
320     IWineD3DTextureImpl_AddRef,
321     IWineD3DTextureImpl_Release,
322     /* IWineD3DResource */    
323     IWineD3DTextureImpl_GetParent,
324     IWineD3DTextureImpl_GetDevice,
325     IWineD3DTextureImpl_SetPrivateData,
326     IWineD3DTextureImpl_GetPrivateData,
327     IWineD3DTextureImpl_FreePrivateData,
328     IWineD3DTextureImpl_SetPriority,
329     IWineD3DTextureImpl_GetPriority,
330     IWineD3DTextureImpl_PreLoad,
331     IWineD3DTextureImpl_GetType,
332     /* IWineD3DBaseTexture */    
333     IWineD3DTextureImpl_SetLOD,
334     IWineD3DTextureImpl_GetLOD,
335     IWineD3DTextureImpl_GetLevelCount,
336     IWineD3DTextureImpl_SetAutoGenFilterType,
337     IWineD3DTextureImpl_GetAutoGenFilterType,
338     IWineD3DTextureImpl_GenerateMipSubLevels,
339     IWineD3DTextureImpl_SetDirty,
340     IWineD3DTextureImpl_GetDirty,
341     IWineD3DTextureImpl_BindTexture,
342     IWineD3DTextureImpl_UnBindTexture,
343     IWineD3DTextureImpl_GetTextureDimensions,    
344     /* IWineD3DTexture */    
345     IWineD3DTextureImpl_GetLevelDesc,
346     IWineD3DTextureImpl_GetSurfaceLevel,
347     IWineD3DTextureImpl_LockRect,
348     IWineD3DTextureImpl_UnlockRect,
349     IWineD3DTextureImpl_AddDirtyRect
350 };