Accept (shorter) date format in Win NT and Win 95.
[wine] / dlls / wined3d / cubetexture.c
1 /*
2  * IDirect3DCubeTexture9 implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  *                     Raphael Junqueira
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "wined3d_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
26 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->resource.wineD3DDevice)->wineD3D))->gl_info
27
28 static const GLenum cube_targets[6] = {
29 #if defined(GL_VERSION_1_3)
30   GL_TEXTURE_CUBE_MAP_POSITIVE_X,
31   GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
32   GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
33   GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
34   GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
35   GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
36 #else
37   GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
38   GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
39   GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
40   GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
41   GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
42   GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
43 #endif
44 };
45
46 /* *******************************************
47    IWineD3DCubeTexture IUnknown parts follow
48    ******************************************* */
49 HRESULT WINAPI IWineD3DCubeTextureImpl_QueryInterface(IWineD3DCubeTexture *iface, REFIID riid, LPVOID *ppobj)
50 {
51     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
52     WARN("(%p)->(%s,%p) should not be called\n",This,debugstr_guid(riid),ppobj);
53     return E_NOINTERFACE;
54 }
55
56 ULONG WINAPI IWineD3DCubeTextureImpl_AddRef(IWineD3DCubeTexture *iface) {
57     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
58     TRACE("(%p) : AddRef increasing from %ld\n", This, This->resource.ref);
59     IUnknown_AddRef(This->resource.parent);
60     return InterlockedIncrement(&This->resource.ref);
61 }
62
63 ULONG WINAPI IWineD3DCubeTextureImpl_Release(IWineD3DCubeTexture *iface) {
64     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
65     ULONG ref;
66     TRACE("(%p) : Releasing from %ld\n", This, This->resource.ref);
67     ref = InterlockedDecrement(&This->resource.ref);
68     if (ref == 0) {
69         int i,j;
70         for (i = 0; i < This->baseTexture.levels; i++) {
71           for (j = 0; j < 6; j++) { 
72             if (This->surfaces[j][i] != NULL) {
73               TRACE("(%p) : Releasing surface %p\n", This, This->surfaces[j][i]);
74               IWineD3DSurface_Release((IWineD3DSurface *) This->surfaces[j][i]);
75             }
76           }
77         }
78         IWineD3DDevice_Release((IWineD3DDevice *)This->resource.wineD3DDevice);
79         HeapFree(GetProcessHeap(), 0, This);
80     } else {
81         IUnknown_Release(This->resource.parent);  /* Released the reference to the d3dx object */
82     }
83     return ref;
84 }
85
86 /* ****************************************************
87    IWineD3DCubeTexture IWineD3DResource parts follow
88    **************************************************** */
89 HRESULT WINAPI IWineD3DCubeTextureImpl_GetDevice(IWineD3DCubeTexture *iface, IWineD3DDevice** ppDevice) {
90     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
91 }
92
93 HRESULT WINAPI IWineD3DCubeTextureImpl_SetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
94     return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
95 }
96
97 HRESULT WINAPI IWineD3DCubeTextureImpl_GetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
98     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
99 }
100
101 HRESULT WINAPI IWineD3DCubeTextureImpl_FreePrivateData(IWineD3DCubeTexture *iface, REFGUID refguid) {
102     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
103 }
104
105 DWORD WINAPI IWineD3DCubeTextureImpl_SetPriority(IWineD3DCubeTexture *iface, DWORD PriorityNew) {
106     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
107 }
108
109 DWORD WINAPI IWineD3DCubeTextureImpl_GetPriority(IWineD3DCubeTexture *iface) {
110     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
111 }
112
113 void WINAPI IWineD3DCubeTextureImpl_PreLoad(IWineD3DCubeTexture *iface) {
114     /* Override the IWineD3DResource Preload method */
115     unsigned int i,j;
116     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
117     
118     TRACE("(%p) : About to load texture: dirtified(%d)\n", This, This->baseTexture.dirty);
119
120     ENTER_GL();
121
122     for (i = 0; i < This->baseTexture.levels; i++) {
123       if (i == 0 && This->surfaces[0][0]->textureName != 0 && This->baseTexture.dirty == FALSE) {
124         glEnable(GL_TEXTURE_CUBE_MAP_ARB);
125         glBindTexture(GLTEXTURECUBEMAP, This->surfaces[0][0]->textureName);
126         checkGLcall("glBindTexture");
127         TRACE("Texture %p (level %d) given name %d\n", This->surfaces[0][0], i, This->surfaces[0][0]->textureName);
128         /* No need to walk through all mip-map levels, since already all assigned */
129         i = This->baseTexture.levels;
130
131       } else {
132         if (i == 0) {
133           if (This->surfaces[0][0]->textureName == 0) {
134             glGenTextures(1, &This->surfaces[0][0]->textureName);
135             checkGLcall("glGenTextures");
136             TRACE("Texture %p (level %d) given name %d\n", This->surfaces[0][i], i, This->surfaces[0][0]->textureName);
137           }
138
139           glBindTexture(GLTEXTURECUBEMAP, This->surfaces[0][0]->textureName);
140           checkGLcall("glBindTexture");
141
142           TRACE("Setting GL_TEXTURE_MAX_LEVEL to %d\n", This->baseTexture.levels - 1);
143           glTexParameteri(GLTEXTURECUBEMAP, GL_TEXTURE_MAX_LEVEL, This->baseTexture.levels - 1); 
144           checkGLcall("glTexParameteri(GL_TEXTURE_CUBE, GL_TEXTURE_MAX_LEVEL, This->levels - 1)");
145         }
146         
147         for (j = 0; j < 6; j++) {
148           IWineD3DSurface_LoadTexture((IWineD3DSurface *) This->surfaces[j][i], cube_targets[j], i); 
149 #if 0
150           static int gen = 0;
151           char buffer[4096];
152           snprintf(buffer, sizeof(buffer), "/tmp/cube%d_face%d_level%d_%d.png", This->surfaces[0][0]->textureName, j, i, ++gen);
153           IWineD3DSurfaceImpl_SaveSnapshot((IWineD3DSurface *) This->surfaces[j][i], buffer);
154 #endif
155         }
156         /* Removed glTexParameterf now TextureStageStates are initialized at startup */
157         This->baseTexture.dirty = FALSE;
158       }
159     }
160
161     LEAVE_GL();
162
163     return ;
164 }
165
166 D3DRESOURCETYPE WINAPI IWineD3DCubeTextureImpl_GetType(IWineD3DCubeTexture *iface) {
167     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
168 }
169
170 HRESULT WINAPI IWineD3DCubeTextureImpl_GetParent(IWineD3DCubeTexture *iface, IUnknown **pParent) {
171     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
172 }
173
174 /* ******************************************************
175    IWineD3DCubeTexture IWineD3DBaseTexture parts follow
176    ****************************************************** */
177 DWORD WINAPI IWineD3DCubeTextureImpl_SetLOD(IWineD3DCubeTexture *iface, DWORD LODNew) {
178     return IWineD3DBaseTextureImpl_SetLOD((IWineD3DBaseTexture *)iface, LODNew);
179 }
180
181 DWORD WINAPI IWineD3DCubeTextureImpl_GetLOD(IWineD3DCubeTexture *iface) {
182     return IWineD3DBaseTextureImpl_GetLOD((IWineD3DBaseTexture *)iface);
183 }
184
185 DWORD WINAPI IWineD3DCubeTextureImpl_GetLevelCount(IWineD3DCubeTexture *iface) {
186     return IWineD3DBaseTextureImpl_GetLevelCount((IWineD3DBaseTexture *)iface);
187 }
188
189 HRESULT WINAPI IWineD3DCubeTextureImpl_SetAutoGenFilterType(IWineD3DCubeTexture *iface, D3DTEXTUREFILTERTYPE FilterType) {
190   return IWineD3DBaseTextureImpl_SetAutoGenFilterType((IWineD3DBaseTexture *)iface, FilterType);
191 }
192
193 D3DTEXTUREFILTERTYPE WINAPI IWineD3DCubeTextureImpl_GetAutoGenFilterType(IWineD3DCubeTexture *iface) {
194   return IWineD3DBaseTextureImpl_GetAutoGenFilterType((IWineD3DBaseTexture *)iface);
195 }
196
197 void WINAPI IWineD3DCubeTextureImpl_GenerateMipSubLevels(IWineD3DCubeTexture *iface) {
198   return IWineD3DBaseTextureImpl_GenerateMipSubLevels((IWineD3DBaseTexture *)iface);
199 }
200
201 /* Internal function, No d3d mapping */
202 BOOL WINAPI IWineD3DCubeTextureImpl_SetDirty(IWineD3DCubeTexture *iface, BOOL dirty) {
203     return IWineD3DBaseTextureImpl_SetDirty((IWineD3DBaseTexture *)iface, TRUE);
204 }
205
206 BOOL WINAPI IWineD3DCubeTextureImpl_GetDirty(IWineD3DCubeTexture *iface) {
207     return IWineD3DBaseTextureImpl_GetDirty((IWineD3DBaseTexture *)iface);
208 }
209
210 /* *******************************************
211    IWineD3DCubeTexture IWineD3DCubeTexture parts follow
212    ******************************************* */
213 HRESULT WINAPI IWineD3DCubeTextureImpl_GetLevelDesc(IWineD3DCubeTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
214     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
215     
216     if (Level < This->baseTexture.levels) {
217         TRACE("(%p) level (%d)\n", This, Level);
218         return IWineD3DSurface_GetDesc((IWineD3DSurface *) This->surfaces[0][Level], pDesc);
219     }
220     FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
221     return D3DERR_INVALIDCALL;
222 }
223
224 HRESULT WINAPI IWineD3DCubeTextureImpl_GetCubeMapSurface(IWineD3DCubeTexture *iface, D3DCUBEMAP_FACES FaceType, UINT Level, IWineD3DSurface** ppCubeMapSurface) {
225     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
226     if (Level < This->baseTexture.levels) {
227         *ppCubeMapSurface = (IWineD3DSurface *) This->surfaces[FaceType][Level];
228         IWineD3DSurface_AddRef((IWineD3DSurface *) *ppCubeMapSurface);
229         TRACE("(%p) -> faceType(%d) level(%d) returning surface@%p \n", This, FaceType, Level, This->surfaces[FaceType][Level]);
230     } else {
231         FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
232         return D3DERR_INVALIDCALL;
233     }
234     return D3D_OK;
235 }
236
237 HRESULT WINAPI IWineD3DCubeTextureImpl_LockRect(IWineD3DCubeTexture *iface, D3DCUBEMAP_FACES FaceType, UINT Level, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
238     HRESULT hr;
239     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
240
241     if (Level < This->baseTexture.levels) {
242       hr = IWineD3DSurface_LockRect((IWineD3DSurface *) This->surfaces[FaceType][Level], pLockedRect, pRect, Flags);
243       TRACE("(%p) -> faceType(%d) level(%d) returning memory@%p success(%lu)\n", This, FaceType, Level, pLockedRect->pBits, hr);
244     } else {
245       FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
246       return D3DERR_INVALIDCALL;
247     }
248     return hr;
249 }
250
251 HRESULT WINAPI IWineD3DCubeTextureImpl_UnlockRect(IWineD3DCubeTexture *iface, D3DCUBEMAP_FACES FaceType, UINT Level) {
252     HRESULT hr;
253     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
254
255     if (Level < This->baseTexture.levels) {
256       hr = IWineD3DSurface_UnlockRect((IWineD3DSurface *) This->surfaces[FaceType][Level]);
257       TRACE("(%p) -> faceType(%d) level(%d) success(%lu)\n", This, FaceType, Level, hr);
258     } else {
259       FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
260       return D3DERR_INVALIDCALL;
261     }
262     return hr;
263 }
264
265 HRESULT  WINAPI IWineD3DCubeTextureImpl_AddDirtyRect(IWineD3DCubeTexture *iface, D3DCUBEMAP_FACES FaceType, CONST RECT* pDirtyRect) {
266     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
267     This->baseTexture.dirty = TRUE;
268     TRACE("(%p) : dirtyfication of faceType(%d) Level (0)\n", This, FaceType);    
269     return IWineD3DSurface_AddDirtyRect((IWineD3DSurface *) This->surfaces[FaceType][0], pDirtyRect);
270 }
271
272
273 IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl =
274 {
275     IWineD3DCubeTextureImpl_QueryInterface,
276     IWineD3DCubeTextureImpl_AddRef,
277     IWineD3DCubeTextureImpl_Release,
278     IWineD3DCubeTextureImpl_GetParent,
279     IWineD3DCubeTextureImpl_GetDevice,
280     IWineD3DCubeTextureImpl_SetPrivateData,
281     IWineD3DCubeTextureImpl_GetPrivateData,
282     IWineD3DCubeTextureImpl_FreePrivateData,
283     IWineD3DCubeTextureImpl_SetPriority,
284     IWineD3DCubeTextureImpl_GetPriority,
285     IWineD3DCubeTextureImpl_PreLoad,
286     IWineD3DCubeTextureImpl_GetType,
287     IWineD3DCubeTextureImpl_SetLOD,
288     IWineD3DCubeTextureImpl_GetLOD,
289     IWineD3DCubeTextureImpl_GetLevelCount,
290     IWineD3DCubeTextureImpl_SetAutoGenFilterType,
291     IWineD3DCubeTextureImpl_GetAutoGenFilterType,
292     IWineD3DCubeTextureImpl_GenerateMipSubLevels,
293     IWineD3DCubeTextureImpl_SetDirty,
294     IWineD3DCubeTextureImpl_GetDirty,
295     IWineD3DCubeTextureImpl_GetLevelDesc,
296     IWineD3DCubeTextureImpl_GetCubeMapSurface,
297     IWineD3DCubeTextureImpl_LockRect,
298     IWineD3DCubeTextureImpl_UnlockRect,
299     IWineD3DCubeTextureImpl_AddDirtyRect
300 };