- implemented render to surfaces (and render to new rendertargets)
[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\n", This);
139     for (i = 0; i < This->levels; i++) {
140       if (i == 0 && This->surfaces[0][i]->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][i]->textureName);
144 #else
145         glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, This->surfaces[0][i]->textureName);
146 #endif
147         checkGLcall("glBindTexture");
148         TRACE("Texture %p (level %d) given name %d\n", This->surfaces[0][i], i, This->surfaces[0][i]->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][i]->textureName == 0) {
154             glGenTextures(1, &This->surfaces[0][i]->textureName);
155             checkGLcall("glGenTextures");
156             TRACE("Texture %p (level %d) given name %d\n", This->surfaces[0][i], i, This->surfaces[0][i]->textureName);
157           }
158
159 #if defined(GL_VERSION_1_3)
160           glBindTexture(GL_TEXTURE_CUBE_MAP, This->surfaces[0][i]->textureName);
161 #else
162           glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, This->surfaces[0][i]->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_CreateGLTexture((LPDIRECT3DSURFACE8) This->surfaces[j][i], cube_targets[j], i); 
177 #if 0
178           TRACE("Calling glTexImage2D %x i=%d, intfmt=%x, w=%d, h=%d,d=%d, glFmt=%x, glType=%x, Mem=%p\n",
179                 cube_targets[j], 
180                 i, 
181                 fmt2glintFmt(This->format), 
182                 This->surfaces[j][i]->myDesc.Width, 
183                 This->surfaces[j][i]->myDesc.Height, 
184                 0, 
185                 fmt2glFmt(This->format), 
186                 fmt2glType(This->format),
187                 This->surfaces[j][i]->allocatedMemory);
188           glTexImage2D(cube_targets[j],
189                        i,
190                        fmt2glintFmt(This->format),
191                        This->surfaces[j][i]->myDesc.Width,
192                        This->surfaces[j][i]->myDesc.Height,
193                        0,
194                        fmt2glFmt(This->format),
195                        fmt2glType(This->format),
196                        This->surfaces[j][i]->allocatedMemory);
197           checkGLcall("glTexImage2D");
198 #endif
199         }
200         /* Removed glTexParameterf now TextureStageStates are initialized at startup */
201         This->Dirty = FALSE;
202       }
203     }
204     return ;
205 }
206
207 D3DRESOURCETYPE WINAPI IDirect3DCubeTexture8Impl_GetType(LPDIRECT3DCUBETEXTURE8 iface) {
208     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
209     TRACE("(%p) : returning %d\n", This, This->ResourceType);
210     return This->ResourceType;
211 }
212
213 /* IDirect3DCubeTexture8 (Inherited from IDirect3DBaseTexture8) */
214 DWORD    WINAPI        IDirect3DCubeTexture8Impl_SetLOD(LPDIRECT3DCUBETEXTURE8 iface, DWORD LODNew) {
215     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
216     FIXME("(%p) : stub\n", This);    
217     return 0;
218 }
219 DWORD    WINAPI        IDirect3DCubeTexture8Impl_GetLOD(LPDIRECT3DCUBETEXTURE8 iface) {
220     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
221     FIXME("(%p) : stub\n", This);    
222     return 0;
223 }
224
225 DWORD    WINAPI        IDirect3DCubeTexture8Impl_GetLevelCount(LPDIRECT3DCUBETEXTURE8 iface) {
226     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
227     TRACE("(%p) : returning %d\n", This, This->levels);
228     return This->levels;
229 }
230
231 /* IDirect3DCubeTexture8 */
232 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_GetLevelDesc(LPDIRECT3DCUBETEXTURE8 iface, UINT Level, D3DSURFACE_DESC* pDesc) {
233     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
234     if (Level < This->levels) {
235         TRACE("(%p) level (%d)\n", This, Level);
236         return IDirect3DSurface8Impl_GetDesc((LPDIRECT3DSURFACE8) This->surfaces[0][Level], pDesc);
237     } else {
238         FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->levels);
239         return D3DERR_INVALIDCALL;
240     }
241     return D3D_OK;
242 }
243 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_GetCubeMapSurface(LPDIRECT3DCUBETEXTURE8 iface, D3DCUBEMAP_FACES FaceType, UINT Level, IDirect3DSurface8** ppCubeMapSurface) {
244     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
245     if (Level < This->levels) {
246         *ppCubeMapSurface = (LPDIRECT3DSURFACE8) This->surfaces[FaceType][Level];
247         IDirect3DSurface8Impl_AddRef((LPDIRECT3DSURFACE8) *ppCubeMapSurface);
248         TRACE("(%p) -> faceType(%d) level(%d) returning surface@%p \n", This, FaceType, Level, This->surfaces[FaceType][Level]);
249     } else {
250         FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->levels);
251         return D3DERR_INVALIDCALL;
252     }
253     return D3D_OK;
254 }
255 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_LockRect(LPDIRECT3DCUBETEXTURE8 iface, D3DCUBEMAP_FACES FaceType, UINT Level, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
256     HRESULT hr;
257     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
258     if (Level < This->levels) {
259       /**
260        * Not dirtified while Surfaces don't notify dirtification
261        * This->Dirty = TRUE;
262        */
263       hr = IDirect3DSurface8_LockRect((LPDIRECT3DSURFACE8) This->surfaces[FaceType][Level], pLockedRect, pRect, Flags);
264       TRACE("(%p) -> faceType(%d) level(%d) returning memory@%p success(%lu)\n", This, FaceType, Level, pLockedRect->pBits, hr);
265     } else {
266       FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->levels);
267       return D3DERR_INVALIDCALL;
268     }
269     return hr;
270 }
271 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_UnlockRect(LPDIRECT3DCUBETEXTURE8 iface, D3DCUBEMAP_FACES FaceType, UINT Level) {
272     HRESULT hr;
273     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
274     if (Level < This->levels) {
275       hr = IDirect3DSurface8_UnlockRect((LPDIRECT3DSURFACE8) This->surfaces[FaceType][Level]);
276       FIXME("(%p) -> faceType(%d) level(%d) success(%lu)\n", This, FaceType, Level, hr);
277     } else {
278       FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->levels);
279       return D3DERR_INVALIDCALL;
280     }
281     return hr;
282 }
283 HRESULT  WINAPI        IDirect3DCubeTexture8Impl_AddDirtyRect(LPDIRECT3DCUBETEXTURE8 iface, D3DCUBEMAP_FACES FaceType, CONST RECT* pDirtyRect) {
284     ICOM_THIS(IDirect3DCubeTexture8Impl,iface);
285     This->Dirty = TRUE;
286     FIXME("(%p) : stub\n", This);
287     return D3D_OK;
288 }
289
290
291 ICOM_VTABLE(IDirect3DCubeTexture8) Direct3DCubeTexture8_Vtbl =
292 {
293     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
294     IDirect3DCubeTexture8Impl_QueryInterface,
295     IDirect3DCubeTexture8Impl_AddRef,
296     IDirect3DCubeTexture8Impl_Release,
297     IDirect3DCubeTexture8Impl_GetDevice,
298     IDirect3DCubeTexture8Impl_SetPrivateData,
299     IDirect3DCubeTexture8Impl_GetPrivateData,
300     IDirect3DCubeTexture8Impl_FreePrivateData,
301     IDirect3DCubeTexture8Impl_SetPriority,
302     IDirect3DCubeTexture8Impl_GetPriority,
303     IDirect3DCubeTexture8Impl_PreLoad,
304     IDirect3DCubeTexture8Impl_GetType,
305     IDirect3DCubeTexture8Impl_SetLOD,
306     IDirect3DCubeTexture8Impl_GetLOD,
307     IDirect3DCubeTexture8Impl_GetLevelCount,
308     IDirect3DCubeTexture8Impl_GetLevelDesc,
309     IDirect3DCubeTexture8Impl_GetCubeMapSurface,
310     IDirect3DCubeTexture8Impl_LockRect,
311     IDirect3DCubeTexture8Impl_UnlockRect,
312     IDirect3DCubeTexture8Impl_AddDirtyRect
313 };