- declare a new debug chanel for vertex shaders
[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   GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
118   GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
119   GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
120   GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
121   GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
122   GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
123 };
124
125 void     WINAPI        IDirect3DCubeTexture8Impl_PreLoad(LPDIRECT3DCUBETEXTURE8 iface) {
126     int i;
127     int j;
128     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
129     TRACE("(%p) : About to load texture\n", This);
130     for (i = 0; i < This->levels; i++) {
131       if (i == 0 && This->surfaces[0][i]->textureName != 0 && This->Dirty == FALSE) {
132         glBindTexture(GL_TEXTURE_CUBE_MAP, This->surfaces[0][i]->textureName);
133         checkGLcall("glBindTexture");
134         TRACE("Texture %p (level %d) given name %d\n", This->surfaces[0][i], i, This->surfaces[0][i]->textureName);
135         /* No need to walk through all mip-map levels, since already all assigned */
136         i = This->levels;
137       } else {
138         if (i == 0) {
139           if (This->surfaces[0][i]->textureName == 0) {
140             glGenTextures(1, &This->surfaces[0][i]->textureName);
141             checkGLcall("glGenTextures");
142             TRACE("Texture %p (level %d) given name %d\n", This->surfaces[0][i], i, This->surfaces[0][i]->textureName);
143           }
144           
145           glBindTexture(GL_TEXTURE_CUBE_MAP, This->surfaces[0][i]->textureName);
146           checkGLcall("glBindTexture");
147           
148           TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->levels - 1);
149           glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, This->levels - 1); 
150           checkGLcall("glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, This->levels - 1)");
151         }
152         
153         for (j = 0; j < 6; j++) {
154           TRACE("Calling glTexImage2D %x i=%d, intfmt=%x, w=%d, h=%d,d=%d, glFmt=%x, glType=%lx, Mem=%p\n",
155                 cube_targets[j], i, fmt2glintFmt(This->format), 
156                 This->surfaces[j][i]->myDesc.Width, This->surfaces[j][i]->myDesc.Height, 
157                 0, fmt2glFmt(This->format), fmt2glType(This->format),
158                 This->surfaces[j][i]->allocatedMemory);
159           glTexImage2D(cube_targets[j],
160                        i,
161                        fmt2glintFmt(This->format),
162                        This->surfaces[j][i]->myDesc.Width,
163                        This->surfaces[j][i]->myDesc.Height,
164                        0,
165                        fmt2glFmt(This->format),
166                        fmt2glType(This->format),
167                        This->surfaces[j][i]->allocatedMemory);
168           checkGLcall("glTexImage2D");
169         }
170         /* Removed glTexParameterf now TextureStageStates are initialized at startup */
171         This->Dirty = FALSE;
172       }
173     }
174     return ;
175 }
176
177 D3DRESOURCETYPE WINAPI IDirect3DCubeTexture8Impl_GetType(LPDIRECT3DCUBETEXTURE8 iface) {
178     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
179     TRACE("(%p) : returning %d\n", This, This->ResourceType);
180     return This->ResourceType;
181 }
182
183 /* IDirect3DCubeTexture8 (Inherited from IDirect3DBaseTexture8) */
184 DWORD    WINAPI        IDirect3DCubeTexture8Impl_SetLOD(LPDIRECT3DCUBETEXTURE8 iface, DWORD LODNew) {
185     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
186     FIXME("(%p) : stub\n", This);    
187     return 0;
188 }
189 DWORD    WINAPI        IDirect3DCubeTexture8Impl_GetLOD(LPDIRECT3DCUBETEXTURE8 iface) {
190     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
191     FIXME("(%p) : stub\n", This);    
192     return 0;
193 }
194
195 DWORD    WINAPI        IDirect3DCubeTexture8Impl_GetLevelCount(LPDIRECT3DCUBETEXTURE8 iface) {
196     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
197     TRACE("(%p) : returning %d\n", This, This->levels);
198     return This->levels;
199 }
200
201 /* IDirect3DCubeTexture8 */
202 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_GetLevelDesc(LPDIRECT3DCUBETEXTURE8 iface, UINT Level, D3DSURFACE_DESC* pDesc) {
203     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
204     if (Level < This->levels) {
205         TRACE("(%p) level (%d)\n", This, Level);
206         return IDirect3DSurface8Impl_GetDesc((LPDIRECT3DSURFACE8) This->surfaces[0][Level], pDesc);
207     } else {
208         FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->levels);
209         return D3DERR_INVALIDCALL;
210     }
211     return D3D_OK;
212 }
213 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_GetCubeMapSurface(LPDIRECT3DCUBETEXTURE8 iface, D3DCUBEMAP_FACES FaceType, UINT Level, IDirect3DSurface8** ppCubeMapSurface) {
214     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
215     if (Level < This->levels) {
216         *ppCubeMapSurface = (LPDIRECT3DSURFACE8) This->surfaces[FaceType][Level];
217         IDirect3DSurface8Impl_AddRef((LPDIRECT3DSURFACE8) *ppCubeMapSurface);
218         TRACE("(%p) -> faceType(%d) level(%d) returning surface@%p \n", This, FaceType, Level, This->surfaces[FaceType][Level]);
219     } else {
220         FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->levels);
221         return D3DERR_INVALIDCALL;
222     }
223     return D3D_OK;
224 }
225 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_LockRect(LPDIRECT3DCUBETEXTURE8 iface, D3DCUBEMAP_FACES FaceType, UINT Level, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
226     HRESULT hr;
227     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
228     if (Level < This->levels) {
229       /**
230        * Not dirtified while Surfaces don't notify dirtification
231        * This->Dirty = TRUE;
232        */
233       hr = IDirect3DSurface8_LockRect((LPDIRECT3DSURFACE8) This->surfaces[FaceType][Level], pLockedRect, pRect, Flags);
234       TRACE("(%p) -> faceType(%d) level(%d) returning memory@%p success(%lu)\n", This, FaceType, Level, pLockedRect->pBits, hr);
235     } else {
236       FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->levels);
237       return D3DERR_INVALIDCALL;
238     }
239     return hr;
240 }
241 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_UnlockRect(LPDIRECT3DCUBETEXTURE8 iface, D3DCUBEMAP_FACES FaceType, UINT Level) {
242     HRESULT hr;
243     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
244     if (Level < This->levels) {
245       hr = IDirect3DSurface8_UnlockRect((LPDIRECT3DSURFACE8) This->surfaces[FaceType][Level]);
246       FIXME("(%p) -> faceType(%d) level(%d) success(%lu)\n", This, FaceType, Level, hr);
247     } else {
248       FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->levels);
249       return D3DERR_INVALIDCALL;
250     }
251     return hr;
252 }
253 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_AddDirtyRect(LPDIRECT3DCUBETEXTURE8 iface, D3DCUBEMAP_FACES FaceType, CONST RECT* pDirtyRect) {
254     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
255     This->Dirty = TRUE;
256     FIXME("(%p) : stub\n", This);
257     return D3D_OK;
258 }
259
260
261 ICOM_VTABLE(IDirect3DCubeTexture8) Direct3DCubeTexture8_Vtbl =
262 {
263     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
264     IDirect3DCubeTexture8Impl_QueryInterface,
265     IDirect3DCubeTexture8Impl_AddRef,
266     IDirect3DCubeTexture8Impl_Release,
267     IDirect3DCubeTexture8Impl_GetDevice,
268     IDirect3DCubeTexture8Impl_SetPrivateData,
269     IDirect3DCubeTexture8Impl_GetPrivateData,
270     IDirect3DCubeTexture8Impl_FreePrivateData,
271     IDirect3DCubeTexture8Impl_SetPriority,
272     IDirect3DCubeTexture8Impl_GetPriority,
273     IDirect3DCubeTexture8Impl_PreLoad,
274     IDirect3DCubeTexture8Impl_GetType,
275     IDirect3DCubeTexture8Impl_SetLOD,
276     IDirect3DCubeTexture8Impl_GetLOD,
277     IDirect3DCubeTexture8Impl_GetLevelCount,
278     IDirect3DCubeTexture8Impl_GetLevelDesc,
279     IDirect3DCubeTexture8Impl_GetCubeMapSurface,
280     IDirect3DCubeTexture8Impl_LockRect,
281     IDirect3DCubeTexture8Impl_UnlockRect,
282     IDirect3DCubeTexture8Impl_AddDirtyRect
283 };