Remove function prototypes, make functions static.
[wine] / dlls / wined3d / cubetexture.c
1 /*
2  * IWineD3DCubeTexture 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 static const GLenum cube_targets[6] = {
30 #if defined(GL_VERSION_1_3)
31   GL_TEXTURE_CUBE_MAP_POSITIVE_X,
32   GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
33   GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
34   GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
35   GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
36   GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
37 #else
38   GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
39   GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
40   GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
41   GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
42   GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
43   GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
44 #endif
45 };
46
47 /* *******************************************
48    IWineD3DCubeTexture IUnknown parts follow
49    ******************************************* */
50 HRESULT WINAPI IWineD3DCubeTextureImpl_QueryInterface(IWineD3DCubeTexture *iface, REFIID riid, LPVOID *ppobj)
51 {
52     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
53     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
54     if (IsEqualGUID(riid, &IID_IUnknown)
55         || IsEqualGUID(riid, &IID_IWineD3DResource)
56         || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
57         || IsEqualGUID(riid, &IID_IWineD3DTexture)) {
58         IUnknown_AddRef(iface);
59         *ppobj = This;
60         return D3D_OK;
61     }
62     return E_NOINTERFACE;
63 }
64
65 ULONG WINAPI IWineD3DCubeTextureImpl_AddRef(IWineD3DCubeTexture *iface) {
66     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
67     TRACE("(%p) : AddRef increasing from %ld\n", This, This->resource.ref);
68     IUnknown_AddRef(This->resource.parent);
69     return InterlockedIncrement(&This->resource.ref);
70 }
71
72 ULONG WINAPI IWineD3DCubeTextureImpl_Release(IWineD3DCubeTexture *iface) {
73     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
74     ULONG ref;
75     TRACE("(%p) : Releasing from %ld\n", This, This->resource.ref);
76     ref = InterlockedDecrement(&This->resource.ref);
77     if (ref == 0) {
78         int i,j;
79         for (i = 0; i < This->baseTexture.levels; i++) {
80           for (j = 0; j < 6; j++) { 
81             if (This->surfaces[j][i] != NULL) {
82               TRACE("(%p) : Releasing surface%d %d  %p\n", This, j, i, This->surfaces[j][i]);
83               IWineD3DSurface_Release((IWineD3DSurface *) This->surfaces[j][i]);
84             }
85           }
86         }
87         if(This->baseTexture.textureName != 0){
88             ENTER_GL();
89             TRACE("Deleting texture %d\n", This->baseTexture.textureName);
90             glDeleteTextures(1, &This->baseTexture.textureName);
91             LEAVE_GL(); 
92         }
93         HeapFree(GetProcessHeap(), 0, This);
94     } else {
95         IUnknown_Release(This->resource.parent);  /* Released the reference to the d3dx object */
96     }
97     return ref;
98 }
99
100 /* ****************************************************
101    IWineD3DCubeTexture IWineD3DResource parts follow
102    **************************************************** */
103 HRESULT WINAPI IWineD3DCubeTextureImpl_GetDevice(IWineD3DCubeTexture *iface, IWineD3DDevice** ppDevice) {
104     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
105 }
106
107 HRESULT WINAPI IWineD3DCubeTextureImpl_SetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
108     return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
109 }
110
111 HRESULT WINAPI IWineD3DCubeTextureImpl_GetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
112     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
113 }
114
115 HRESULT WINAPI IWineD3DCubeTextureImpl_FreePrivateData(IWineD3DCubeTexture *iface, REFGUID refguid) {
116     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
117 }
118
119 DWORD WINAPI IWineD3DCubeTextureImpl_SetPriority(IWineD3DCubeTexture *iface, DWORD PriorityNew) {
120     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
121 }
122
123 DWORD WINAPI IWineD3DCubeTextureImpl_GetPriority(IWineD3DCubeTexture *iface) {
124     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
125 }
126
127 void WINAPI IWineD3DCubeTextureImpl_PreLoad(IWineD3DCubeTexture *iface) {
128     /* Override the IWineD3DResource Preload method */
129     unsigned int i,j;
130     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
131     
132     TRACE("(%p) : About to load texture: dirtified(%d)\n", This, This->baseTexture.dirty);
133
134     ENTER_GL();
135 #if 0 /* TODO: context manager support */
136      IWineD3DContextManager_PushState(This->contextManager, GL_TEXTURE_CUBE_MAP_ARB, ENABLED, NOW /* make sure the state is applied now */);
137 #else    
138     glEnable(GL_TEXTURE_CUBE_MAP_ARB);
139 #endif
140
141     /* Generate a texture name if we don't already have one */    
142     if (This->baseTexture.textureName == 0) {
143         glGenTextures(1, &This->baseTexture.textureName);
144         checkGLcall("glGenTextures");
145         TRACE("Generated texture %d\n", This->baseTexture.textureName);
146          if (This->baseTexture.pool == D3DPOOL_DEFAULT) {
147             /* Tell opengl to try and keep this texture in video ram (well mostly) */
148             GLclampf tmp;
149             tmp = 0.9f;
150             glPrioritizeTextures(1, &This->baseTexture.textureName, &tmp);
151          }        
152     }
153
154     /* Bind the texture */
155     if (This->baseTexture.textureName != 0) {
156         glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, This->baseTexture.textureName);
157         checkGLcall("glBindTexture");
158     } else { /* this only happened if we've run out of openGL textures */
159         WARN("This texture doesn't have an openGL texture assigned to it\n");
160         return;
161     }
162     
163     /* If were dirty then reload the surfaces */
164     if(This->baseTexture.dirty != FALSE) {
165         for (i = 0; i < This->baseTexture.levels; i++) {
166           for (j = 0; j < 6; j++)
167               IWineD3DSurface_LoadTexture((IWineD3DSurface *) This->surfaces[j][i], cube_targets[j], i);
168         }
169         /* No longer dirty */
170         This->baseTexture.dirty = FALSE;        
171        
172     }
173
174     /* Always need to reset the number of mipmap levels when rebinding as it is
175        a property of the active texture unit, and another texture may have set it
176        to a different value                                                       */
177     TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->baseTexture.levels - 1);
178     glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels - 1);
179     checkGLcall("glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels)");
180     
181 #if 0 /* TODO: context manager support */
182      IWineD3DContextManager_PopState(This->contextManager, GL_TEXTURE_CUBE_MAP_ARB, DISABLED, DELAYED);
183 #endif
184     LEAVE_GL();    
185
186     return ;
187 }
188
189 D3DRESOURCETYPE WINAPI IWineD3DCubeTextureImpl_GetType(IWineD3DCubeTexture *iface) {
190     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
191 }
192
193 HRESULT WINAPI IWineD3DCubeTextureImpl_GetParent(IWineD3DCubeTexture *iface, IUnknown **pParent) {
194     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
195 }
196
197 /* ******************************************************
198    IWineD3DCubeTexture IWineD3DBaseTexture parts follow
199    ****************************************************** */
200 DWORD WINAPI IWineD3DCubeTextureImpl_SetLOD(IWineD3DCubeTexture *iface, DWORD LODNew) {
201     return IWineD3DBaseTextureImpl_SetLOD((IWineD3DBaseTexture *)iface, LODNew);
202 }
203
204 DWORD WINAPI IWineD3DCubeTextureImpl_GetLOD(IWineD3DCubeTexture *iface) {
205     return IWineD3DBaseTextureImpl_GetLOD((IWineD3DBaseTexture *)iface);
206 }
207
208 DWORD WINAPI IWineD3DCubeTextureImpl_GetLevelCount(IWineD3DCubeTexture *iface) {
209     return IWineD3DBaseTextureImpl_GetLevelCount((IWineD3DBaseTexture *)iface);
210 }
211
212 HRESULT WINAPI IWineD3DCubeTextureImpl_SetAutoGenFilterType(IWineD3DCubeTexture *iface, D3DTEXTUREFILTERTYPE FilterType) {
213   return IWineD3DBaseTextureImpl_SetAutoGenFilterType((IWineD3DBaseTexture *)iface, FilterType);
214 }
215
216 D3DTEXTUREFILTERTYPE WINAPI IWineD3DCubeTextureImpl_GetAutoGenFilterType(IWineD3DCubeTexture *iface) {
217   return IWineD3DBaseTextureImpl_GetAutoGenFilterType((IWineD3DBaseTexture *)iface);
218 }
219
220 void WINAPI IWineD3DCubeTextureImpl_GenerateMipSubLevels(IWineD3DCubeTexture *iface) {
221   return IWineD3DBaseTextureImpl_GenerateMipSubLevels((IWineD3DBaseTexture *)iface);
222 }
223
224 /* Internal function, No d3d mapping */
225 BOOL WINAPI IWineD3DCubeTextureImpl_SetDirty(IWineD3DCubeTexture *iface, BOOL dirty) {
226     return IWineD3DBaseTextureImpl_SetDirty((IWineD3DBaseTexture *)iface, dirty);
227 }
228
229 /* Internal function, No d3d mapping */
230 BOOL WINAPI IWineD3DCubeTextureImpl_GetDirty(IWineD3DCubeTexture *iface) {
231     return IWineD3DBaseTextureImpl_GetDirty((IWineD3DBaseTexture *)iface);
232 }
233
234 HRESULT WINAPI IWineD3DCubeTextureImpl_BindTexture(IWineD3DCubeTexture *iface) {
235     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
236     TRACE("(%p) : %d \n", This, This->baseTexture.textureName);
237     ENTER_GL();
238 #if 0 /* TODO: context manager support */
239      IWineD3DContextManager_PushState(This->contextManager, GL_TEXTURE_CUBE_MAP_ARB, ENABLED, NOW /* make sure the state is applied now */);
240 #else    
241     glEnable(GL_TEXTURE_CUBE_MAP_ARB);
242 #endif 
243     glBindTexture(GLTEXTURECUBEMAP, This->baseTexture.textureName);
244     LEAVE_GL();
245     return D3D_OK;
246 }
247
248 HRESULT WINAPI IWineD3DCubeTextureImpl_UnBindTexture(IWineD3DCubeTexture *iface) {
249     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
250     TRACE("(%p) \n", This);    
251     ENTER_GL();
252     
253     glBindTexture(GLTEXTURECUBEMAP, 0);
254 #if 0 /* TODO: context manager support */
255      IWineD3DContextManager_PopState(This->contextManager, GL_TEXTURE_CUBE_MAP_ARB, ENABLED, NOW /* make sure the state is applied now */);
256 #else    
257     glDisable(GL_TEXTURE_CUBE_MAP_ARB);
258 #endif     
259
260     LEAVE_GL();
261     return D3D_OK;
262 }
263
264 UINT WINAPI IWineD3DCubeTextureImpl_GetTextureDimensions(IWineD3DCubeTexture *iface){
265     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
266     TRACE("(%p) \n", This);
267     
268     return GLTEXTURECUBEMAP;
269 }
270
271 /* *******************************************
272    IWineD3DCubeTexture IWineD3DCubeTexture parts follow
273    ******************************************* */
274 HRESULT WINAPI IWineD3DCubeTextureImpl_GetLevelDesc(IWineD3DCubeTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
275     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
276     
277     if (Level < This->baseTexture.levels) {
278         TRACE("(%p) level (%d)\n", This, Level);
279         return IWineD3DSurface_GetDesc((IWineD3DSurface *) This->surfaces[0][Level], pDesc);
280     }
281     FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
282     return D3DERR_INVALIDCALL;
283 }
284
285 HRESULT WINAPI IWineD3DCubeTextureImpl_GetCubeMapSurface(IWineD3DCubeTexture *iface, D3DCUBEMAP_FACES FaceType, UINT Level, IWineD3DSurface** ppCubeMapSurface) {
286     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
287     if (Level < This->baseTexture.levels) {
288         *ppCubeMapSurface = (IWineD3DSurface *) This->surfaces[FaceType][Level];
289         IWineD3DSurface_AddRef((IWineD3DSurface *) *ppCubeMapSurface);
290         TRACE("(%p) -> faceType(%d) level(%d) returning surface@%p \n", This, FaceType, Level, This->surfaces[FaceType][Level]);
291     } else {
292         FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
293         return D3DERR_INVALIDCALL;
294     }
295     return D3D_OK;
296 }
297
298 HRESULT WINAPI IWineD3DCubeTextureImpl_LockRect(IWineD3DCubeTexture *iface, D3DCUBEMAP_FACES FaceType, UINT Level, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
299     HRESULT hr;
300     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
301
302     if (Level < This->baseTexture.levels) {
303       hr = IWineD3DSurface_LockRect((IWineD3DSurface *) This->surfaces[FaceType][Level], pLockedRect, pRect, Flags);
304       TRACE("(%p) -> faceType(%d) level(%d) returning memory@%p success(%lu)\n", This, FaceType, Level, pLockedRect->pBits, hr);
305     } else {
306       FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
307       return D3DERR_INVALIDCALL;
308     }
309     return hr;
310 }
311
312 HRESULT WINAPI IWineD3DCubeTextureImpl_UnlockRect(IWineD3DCubeTexture *iface, D3DCUBEMAP_FACES FaceType, UINT Level) {
313     HRESULT hr;
314     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
315
316     if (Level < This->baseTexture.levels) {
317       hr = IWineD3DSurface_UnlockRect((IWineD3DSurface *) This->surfaces[FaceType][Level]);
318       TRACE("(%p) -> faceType(%d) level(%d) success(%lu)\n", This, FaceType, Level, hr);
319     } else {
320       FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
321       return D3DERR_INVALIDCALL;
322     }
323     return hr;
324 }
325
326 HRESULT  WINAPI IWineD3DCubeTextureImpl_AddDirtyRect(IWineD3DCubeTexture *iface, D3DCUBEMAP_FACES FaceType, CONST RECT* pDirtyRect) {
327     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
328     This->baseTexture.dirty = TRUE;
329     TRACE("(%p) : dirtyfication of faceType(%d) Level (0)\n", This, FaceType);    
330     return IWineD3DSurface_AddDirtyRect((IWineD3DSurface *) This->surfaces[FaceType][0], pDirtyRect);
331 }
332
333
334 IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl =
335 {
336     /* IUnknown */
337     IWineD3DCubeTextureImpl_QueryInterface,
338     IWineD3DCubeTextureImpl_AddRef,
339     IWineD3DCubeTextureImpl_Release,
340     /* IWineD3DResource */
341     IWineD3DCubeTextureImpl_GetParent,
342     IWineD3DCubeTextureImpl_GetDevice,
343     IWineD3DCubeTextureImpl_SetPrivateData,
344     IWineD3DCubeTextureImpl_GetPrivateData,
345     IWineD3DCubeTextureImpl_FreePrivateData,
346     IWineD3DCubeTextureImpl_SetPriority,
347     IWineD3DCubeTextureImpl_GetPriority,
348     IWineD3DCubeTextureImpl_PreLoad,
349     IWineD3DCubeTextureImpl_GetType,
350     /*base texture */
351     IWineD3DCubeTextureImpl_SetLOD,
352     IWineD3DCubeTextureImpl_GetLOD,
353     IWineD3DCubeTextureImpl_GetLevelCount,
354     IWineD3DCubeTextureImpl_SetAutoGenFilterType,
355     IWineD3DCubeTextureImpl_GetAutoGenFilterType,
356     IWineD3DCubeTextureImpl_GenerateMipSubLevels,
357     IWineD3DCubeTextureImpl_SetDirty,
358     IWineD3DCubeTextureImpl_GetDirty,
359     IWineD3DCubeTextureImpl_BindTexture,
360     IWineD3DCubeTextureImpl_UnBindTexture,
361     IWineD3DCubeTextureImpl_GetTextureDimensions,    
362     /* cube texture */    
363     IWineD3DCubeTextureImpl_GetLevelDesc,
364     IWineD3DCubeTextureImpl_GetCubeMapSurface,
365     IWineD3DCubeTextureImpl_LockRect,
366     IWineD3DCubeTextureImpl_UnlockRect,
367     IWineD3DCubeTextureImpl_AddDirtyRect
368 };