- cleaning of volume.c/volumetexture.c as done previously for
[wine] / dlls / d3d8 / cubetexture.c
1 /*
2  * IDirect3DCubeTexture8 implementation
3  *
4  * Copyright 2002 Jason Edmeades
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/debug.h"
28
29 #include "d3d8_private.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
32
33 /* IDirect3DCubeTexture8 IUnknown parts follow: */
34 HRESULT WINAPI IDirect3DCubeTexture8Impl_QueryInterface(LPDIRECT3DCUBETEXTURE8 iface,REFIID riid,LPVOID *ppobj)
35 {
36     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
37     TRACE("(%p) : QueryInterface\n", This);
38     if (IsEqualGUID(riid, &IID_IUnknown)
39         || IsEqualGUID(riid, &IID_IDirect3DResource8)
40         || IsEqualGUID(riid, &IID_IDirect3DBaseTexture8)
41         || IsEqualGUID(riid, &IID_IDirect3DCubeTexture8)) {
42         IDirect3DCubeTexture8Impl_AddRef(iface);
43         *ppobj = This;
44         return D3D_OK;
45     }
46
47     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
48     return E_NOINTERFACE;
49 }
50
51 ULONG WINAPI IDirect3DCubeTexture8Impl_AddRef(LPDIRECT3DCUBETEXTURE8 iface) {
52     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
53     TRACE("(%p) : AddRef from %ld\n", This, This->ref);
54     return ++(This->ref);
55 }
56
57 ULONG WINAPI IDirect3DCubeTexture8Impl_Release(LPDIRECT3DCUBETEXTURE8 iface) {
58     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
59     ULONG ref = --This->ref;
60     int i;
61     int j;
62
63     TRACE("(%p) : ReleaseRef to %ld\n", This, This->ref);
64     if (ref == 0) {
65         for (i = 0; i < This->levels; i++) {
66           for (j = 0; j < 6; j++) { 
67             if (This->surfaces[j][i] != NULL) {
68               TRACE("(%p) : Releasing surface %p\n", This, This->surfaces[j][i]);
69               IDirect3DSurface8Impl_Release((LPDIRECT3DSURFACE8) This->surfaces[j][i]);
70             }
71           }
72         }
73         HeapFree(GetProcessHeap(), 0, This);
74     }
75     return ref;
76 }
77
78 /* IDirect3DCubeTexture8 (Inherited from IDirect3DResource8) */
79 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_GetDevice(LPDIRECT3DCUBETEXTURE8 iface, IDirect3DDevice8** ppDevice) {
80     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
81     TRACE("(%p) : returning %p\n", This, This->Device);
82     *ppDevice = (LPDIRECT3DDEVICE8) This->Device;
83     /**
84      * Note  Calling this method will increase the internal reference count 
85      * on the IDirect3DDevice8 interface. 
86      */
87     IDirect3DDevice8Impl_AddRef(*ppDevice);
88     return D3D_OK;
89 }
90 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_SetPrivateData(LPDIRECT3DCUBETEXTURE8 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
91     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
92     FIXME("(%p) : stub\n", This);    
93     return D3D_OK;
94 }
95 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_GetPrivateData(LPDIRECT3DCUBETEXTURE8 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
96     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
97     FIXME("(%p) : stub\n", This);    
98     return D3D_OK;
99 }
100 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_FreePrivateData(LPDIRECT3DCUBETEXTURE8 iface, REFGUID refguid) {
101     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
102     FIXME("(%p) : stub\n", This);    
103     return D3D_OK;
104 }
105 DWORD    WINAPI        IDirect3DCubeTexture8Impl_SetPriority(LPDIRECT3DCUBETEXTURE8 iface, DWORD PriorityNew) {
106     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
107     FIXME("(%p) : stub\n", This);    
108     return 0;
109 }
110 DWORD    WINAPI        IDirect3DCubeTexture8Impl_GetPriority(LPDIRECT3DCUBETEXTURE8 iface) {
111     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
112     FIXME("(%p) : stub\n", This);    
113     return 0;
114 }
115
116 static const GLenum cube_targets[6] = {
117 #if defined(GL_VERSION_1_3)
118   GL_TEXTURE_CUBE_MAP_POSITIVE_X,
119   GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
120   GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
121   GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
122   GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
123   GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
124 #else
125   GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
126   GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
127   GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
128   GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
129   GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
130   GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
131 #endif
132 };
133
134 void     WINAPI        IDirect3DCubeTexture8Impl_PreLoad(LPDIRECT3DCUBETEXTURE8 iface) {
135     int i;
136     int j;
137     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
138     TRACE("(%p) : About to load texture: dirtified(%d)\n", This, This->Dirty);
139     for (i = 0; i < This->levels; i++) {
140       if (i == 0 && This->surfaces[0][0]->textureName != 0 && This->Dirty == FALSE) {
141         glEnable(GL_TEXTURE_CUBE_MAP_ARB);
142 #if defined(GL_VERSION_1_3)
143         glBindTexture(GL_TEXTURE_CUBE_MAP, This->surfaces[0][0]->textureName);
144 #else
145         glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, This->surfaces[0][0]->textureName);
146 #endif
147         checkGLcall("glBindTexture");
148         TRACE("Texture %p (level %d) given name %d\n", This->surfaces[0][0], i, This->surfaces[0][0]->textureName);
149         /* No need to walk through all mip-map levels, since already all assigned */
150         i = This->levels;
151       } else {
152         if (i == 0) {
153           if (This->surfaces[0][0]->textureName == 0) {
154             glGenTextures(1, &This->surfaces[0][0]->textureName);
155             checkGLcall("glGenTextures");
156             TRACE("Texture %p (level %d) given name %d\n", This->surfaces[0][i], i, This->surfaces[0][0]->textureName);
157           }
158
159 #if defined(GL_VERSION_1_3)
160           glBindTexture(GL_TEXTURE_CUBE_MAP, This->surfaces[0][0]->textureName);
161 #else
162           glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, This->surfaces[0][0]->textureName);
163 #endif
164           checkGLcall("glBindTexture");
165
166           TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->levels - 1);
167 #if defined(GL_VERSION_1_3)
168           glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, This->levels - 1); 
169 #else
170           glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAX_LEVEL, This->levels - 1); 
171 #endif
172           checkGLcall("glTexParameteri(GL_TEXTURE_CUBE, GL_TEXTURE_MAX_LEVEL, This->levels - 1)");
173         }
174         
175         for (j = 0; j < 6; j++) {
176           IDirect3DSurface8Impl_LoadTexture((LPDIRECT3DSURFACE8) This->surfaces[j][i], cube_targets[j], i); 
177 #if 0
178           static int gen = 0;
179           char buffer[4096];
180           snprintf(buffer, sizeof(buffer), "/tmp/cube%d_face%d_level%d_%d.png", This->surfaces[0][0]->textureName, j, i, ++gen);
181           IDirect3DSurface8Impl_SaveSnapshot((LPDIRECT3DSURFACE8) This->surfaces[j][i], buffer);
182 #endif
183         }
184         /* Removed glTexParameterf now TextureStageStates are initialized at startup */
185         This->Dirty = FALSE;
186       }
187     }
188     return ;
189 }
190
191 D3DRESOURCETYPE WINAPI IDirect3DCubeTexture8Impl_GetType(LPDIRECT3DCUBETEXTURE8 iface) {
192     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
193     TRACE("(%p) : returning %d\n", This, This->ResourceType);
194     return This->ResourceType;
195 }
196
197 /* IDirect3DCubeTexture8 (Inherited from IDirect3DBaseTexture8) */
198 DWORD    WINAPI        IDirect3DCubeTexture8Impl_SetLOD(LPDIRECT3DCUBETEXTURE8 iface, DWORD LODNew) {
199     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
200     FIXME("(%p) : stub\n", This);    
201     return 0;
202 }
203 DWORD    WINAPI        IDirect3DCubeTexture8Impl_GetLOD(LPDIRECT3DCUBETEXTURE8 iface) {
204     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
205     FIXME("(%p) : stub\n", This);    
206     return 0;
207 }
208
209 DWORD    WINAPI        IDirect3DCubeTexture8Impl_GetLevelCount(LPDIRECT3DCUBETEXTURE8 iface) {
210     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
211     TRACE("(%p) : returning %d\n", This, This->levels);
212     return This->levels;
213 }
214
215 /* IDirect3DCubeTexture8 */
216 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_GetLevelDesc(LPDIRECT3DCUBETEXTURE8 iface, UINT Level, D3DSURFACE_DESC* pDesc) {
217     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
218     if (Level < This->levels) {
219         TRACE("(%p) level (%d)\n", This, Level);
220         return IDirect3DSurface8Impl_GetDesc((LPDIRECT3DSURFACE8) This->surfaces[0][Level], pDesc);
221     } else {
222         FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->levels);
223         return D3DERR_INVALIDCALL;
224     }
225     return D3D_OK;
226 }
227 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_GetCubeMapSurface(LPDIRECT3DCUBETEXTURE8 iface, D3DCUBEMAP_FACES FaceType, UINT Level, IDirect3DSurface8** ppCubeMapSurface) {
228     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
229     if (Level < This->levels) {
230         *ppCubeMapSurface = (LPDIRECT3DSURFACE8) This->surfaces[FaceType][Level];
231         IDirect3DSurface8Impl_AddRef((LPDIRECT3DSURFACE8) *ppCubeMapSurface);
232         TRACE("(%p) -> faceType(%d) level(%d) returning surface@%p \n", This, FaceType, Level, This->surfaces[FaceType][Level]);
233     } else {
234         FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->levels);
235         return D3DERR_INVALIDCALL;
236     }
237     return D3D_OK;
238 }
239 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_LockRect(LPDIRECT3DCUBETEXTURE8 iface, D3DCUBEMAP_FACES FaceType, UINT Level, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
240     HRESULT hr;
241     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
242     if (Level < This->levels) {
243       /**
244        * Not dirtified while Surfaces don't notify dirtification
245        * This->Dirty = TRUE;
246        */
247       hr = IDirect3DSurface8Impl_LockRect((LPDIRECT3DSURFACE8) This->surfaces[FaceType][Level], pLockedRect, pRect, Flags);
248       TRACE("(%p) -> faceType(%d) level(%d) returning memory@%p success(%lu)\n", This, FaceType, Level, pLockedRect->pBits, hr);
249     } else {
250       FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->levels);
251       return D3DERR_INVALIDCALL;
252     }
253     return hr;
254 }
255 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_UnlockRect(LPDIRECT3DCUBETEXTURE8 iface, D3DCUBEMAP_FACES FaceType, UINT Level) {
256     HRESULT hr;
257     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
258     if (Level < This->levels) {
259       hr = IDirect3DSurface8Impl_UnlockRect((LPDIRECT3DSURFACE8) This->surfaces[FaceType][Level]);
260       TRACE("(%p) -> faceType(%d) level(%d) success(%lu)\n", This, FaceType, Level, hr);
261     } else {
262       FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->levels);
263       return D3DERR_INVALIDCALL;
264     }
265     return hr;
266 }
267 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_AddDirtyRect(LPDIRECT3DCUBETEXTURE8 iface, D3DCUBEMAP_FACES FaceType, CONST RECT* pDirtyRect) {
268     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
269     This->Dirty = TRUE;
270     TRACE("(%p) : dirtyfication of faceType(%d) Level (0)\n", This, FaceType);    
271     return IDirect3DSurface8Impl_AddDirtyRect((LPDIRECT3DSURFACE8) This->surfaces[FaceType][0], pDirtyRect);
272 }
273
274
275 ICOM_VTABLE(IDirect3DCubeTexture8) Direct3DCubeTexture8_Vtbl =
276 {
277     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
278     IDirect3DCubeTexture8Impl_QueryInterface,
279     IDirect3DCubeTexture8Impl_AddRef,
280     IDirect3DCubeTexture8Impl_Release,
281     IDirect3DCubeTexture8Impl_GetDevice,
282     IDirect3DCubeTexture8Impl_SetPrivateData,
283     IDirect3DCubeTexture8Impl_GetPrivateData,
284     IDirect3DCubeTexture8Impl_FreePrivateData,
285     IDirect3DCubeTexture8Impl_SetPriority,
286     IDirect3DCubeTexture8Impl_GetPriority,
287     IDirect3DCubeTexture8Impl_PreLoad,
288     IDirect3DCubeTexture8Impl_GetType,
289     IDirect3DCubeTexture8Impl_SetLOD,
290     IDirect3DCubeTexture8Impl_GetLOD,
291     IDirect3DCubeTexture8Impl_GetLevelCount,
292     IDirect3DCubeTexture8Impl_GetLevelDesc,
293     IDirect3DCubeTexture8Impl_GetCubeMapSurface,
294     IDirect3DCubeTexture8Impl_LockRect,
295     IDirect3DCubeTexture8Impl_UnlockRect,
296     IDirect3DCubeTexture8Impl_AddDirtyRect
297 };