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