wined3d: Cube texture named constants.
[wine] / dlls / wined3d / cubetexture.c
1 /*
2  * IWineD3DCubeTexture implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  * Copyright 2002-2005 Raphael Junqueira
6  * Copyright 2005 Oliver Stieber
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24 #include "wined3d_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->resource.wineD3DDevice)->wineD3D))->gl_info
28
29 static const GLenum cube_targets[6] = {
30 #if defined(GL_VERSION_1_3)
31   GL_TEXTURE_CUBE_MAP_POSITIVE_X,
32   GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
33   GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
34   GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
35   GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
36   GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
37 #else
38   GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
39   GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
40   GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
41   GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
42   GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
43   GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
44 #endif
45 };
46
47 /* *******************************************
48    IWineD3DCubeTexture IUnknown parts follow
49    ******************************************* */
50 HRESULT WINAPI IWineD3DCubeTextureImpl_QueryInterface(IWineD3DCubeTexture *iface, REFIID riid, LPVOID *ppobj)
51 {
52     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
53     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
54     if (IsEqualGUID(riid, &IID_IUnknown)
55         || IsEqualGUID(riid, &IID_IWineD3DResource)
56         || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
57         || IsEqualGUID(riid, &IID_IWineD3DTexture)) {
58         IUnknown_AddRef(iface);
59         *ppobj = This;
60         return D3D_OK;
61     }
62     return E_NOINTERFACE;
63 }
64
65 ULONG WINAPI IWineD3DCubeTextureImpl_AddRef(IWineD3DCubeTexture *iface) {
66     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
67     TRACE("(%p) : AddRef increasing from %ld\n", This, This->resource.ref);
68     return InterlockedIncrement(&This->resource.ref);
69 }
70
71 ULONG WINAPI IWineD3DCubeTextureImpl_Release(IWineD3DCubeTexture *iface) {
72     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
73     ULONG ref;
74     TRACE("(%p) : Releasing from %ld\n", This, This->resource.ref);
75     ref = InterlockedDecrement(&This->resource.ref);
76     if (ref == 0) {
77         int i,j;
78         TRACE("(%p) : Cleaning up\n",This);
79         for (i = 0; i < This->baseTexture.levels; i++) {
80             for (j = 0; j < 6; j++) {
81                 if (This->surfaces[j][i] != NULL) {
82                     /* Because the surfaces were created using a callback we need to release there parent otehrwise we leave the parent hanging */
83                     IUnknown* surfaceParent;
84                     /* Clean out the texture name we gave to the suface so that the surface doesn't try and release it */
85                     IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], 0, 0);
86                     TRACE("(%p) : Releasing surface%d %d  %p\n", This, j, i, This->surfaces[j][i]);
87                     IWineD3DSurface_GetParent(This->surfaces[j][i], &surfaceParent);
88                     IUnknown_Release(surfaceParent);
89                     IUnknown_Release(surfaceParent);
90                 }
91             }
92         }
93         IWineD3DBaseTextureImpl_CleanUp((IWineD3DBaseTexture *) iface);
94         /* finally delete the object */
95         HeapFree(GetProcessHeap(), 0, This);
96     }
97     return ref;
98 }
99
100 /* ****************************************************
101    IWineD3DCubeTexture IWineD3DResource parts follow
102    **************************************************** */
103 HRESULT WINAPI IWineD3DCubeTextureImpl_GetDevice(IWineD3DCubeTexture *iface, IWineD3DDevice** ppDevice) {
104     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
105 }
106
107 HRESULT WINAPI IWineD3DCubeTextureImpl_SetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
108     return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
109 }
110
111 HRESULT WINAPI IWineD3DCubeTextureImpl_GetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
112     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
113 }
114
115 HRESULT WINAPI IWineD3DCubeTextureImpl_FreePrivateData(IWineD3DCubeTexture *iface, REFGUID refguid) {
116     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
117 }
118
119 DWORD WINAPI IWineD3DCubeTextureImpl_SetPriority(IWineD3DCubeTexture *iface, DWORD PriorityNew) {
120     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
121 }
122
123 DWORD WINAPI IWineD3DCubeTextureImpl_GetPriority(IWineD3DCubeTexture *iface) {
124     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
125 }
126
127 void WINAPI IWineD3DCubeTextureImpl_PreLoad(IWineD3DCubeTexture *iface) {
128     /* Override the IWineD3DResource Preload method */
129     unsigned int i,j;
130     BOOL setGlTextureDesc = FALSE;
131     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
132
133     TRACE("(%p) : About to load texture: dirtified(%d)\n", This, This->baseTexture.dirty);
134
135     if (This->baseTexture.textureName == 0)  setGlTextureDesc = TRUE;
136
137     IWineD3DCubeTexture_BindTexture(iface);
138
139     ENTER_GL();
140     /* If were dirty then reload the surfaces */
141     if (This->baseTexture.dirty != FALSE) {
142         for (i = 0; i < This->baseTexture.levels; i++) {
143             for (j = D3DCUBEMAP_FACE_POSITIVE_X; j <= D3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
144                 if(setGlTextureDesc)
145                       IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], This->baseTexture.textureName, cube_targets[j]);
146                 IWineD3DSurface_LoadTexture(This->surfaces[j][i]);
147             }
148         }
149         /* No longer dirty */
150         This->baseTexture.dirty = FALSE;
151     }
152     LEAVE_GL();
153     return ;
154 }
155
156 D3DRESOURCETYPE WINAPI IWineD3DCubeTextureImpl_GetType(IWineD3DCubeTexture *iface) {
157     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
158 }
159
160 HRESULT WINAPI IWineD3DCubeTextureImpl_GetParent(IWineD3DCubeTexture *iface, IUnknown **pParent) {
161     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
162 }
163
164 /* ******************************************************
165    IWineD3DCubeTexture IWineD3DBaseTexture parts follow
166    ****************************************************** */
167 DWORD WINAPI IWineD3DCubeTextureImpl_SetLOD(IWineD3DCubeTexture *iface, DWORD LODNew) {
168     return IWineD3DBaseTextureImpl_SetLOD((IWineD3DBaseTexture *)iface, LODNew);
169 }
170
171 DWORD WINAPI IWineD3DCubeTextureImpl_GetLOD(IWineD3DCubeTexture *iface) {
172     return IWineD3DBaseTextureImpl_GetLOD((IWineD3DBaseTexture *)iface);
173 }
174
175 DWORD WINAPI IWineD3DCubeTextureImpl_GetLevelCount(IWineD3DCubeTexture *iface) {
176     return IWineD3DBaseTextureImpl_GetLevelCount((IWineD3DBaseTexture *)iface);
177 }
178
179 HRESULT WINAPI IWineD3DCubeTextureImpl_SetAutoGenFilterType(IWineD3DCubeTexture *iface, D3DTEXTUREFILTERTYPE FilterType) {
180   return IWineD3DBaseTextureImpl_SetAutoGenFilterType((IWineD3DBaseTexture *)iface, FilterType);
181 }
182
183 D3DTEXTUREFILTERTYPE WINAPI IWineD3DCubeTextureImpl_GetAutoGenFilterType(IWineD3DCubeTexture *iface) {
184   return IWineD3DBaseTextureImpl_GetAutoGenFilterType((IWineD3DBaseTexture *)iface);
185 }
186
187 void WINAPI IWineD3DCubeTextureImpl_GenerateMipSubLevels(IWineD3DCubeTexture *iface) {
188   return IWineD3DBaseTextureImpl_GenerateMipSubLevels((IWineD3DBaseTexture *)iface);
189 }
190
191 /* Internal function, No d3d mapping */
192 BOOL WINAPI IWineD3DCubeTextureImpl_SetDirty(IWineD3DCubeTexture *iface, BOOL dirty) {
193     return IWineD3DBaseTextureImpl_SetDirty((IWineD3DBaseTexture *)iface, dirty);
194 }
195
196 /* Internal function, No d3d mapping */
197 BOOL WINAPI IWineD3DCubeTextureImpl_GetDirty(IWineD3DCubeTexture *iface) {
198     return IWineD3DBaseTextureImpl_GetDirty((IWineD3DBaseTexture *)iface);
199 }
200
201 HRESULT WINAPI IWineD3DCubeTextureImpl_BindTexture(IWineD3DCubeTexture *iface) {
202     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
203     TRACE("(%p) : relay to BaseTexture\n", This);
204     return IWineD3DBaseTextureImpl_BindTexture((IWineD3DBaseTexture *)iface);
205 }
206
207 HRESULT WINAPI IWineD3DCubeTextureImpl_UnBindTexture(IWineD3DCubeTexture *iface) {
208     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
209     TRACE("(%p) : relay to BaseTexture\n", This);
210     return IWineD3DBaseTextureImpl_UnBindTexture((IWineD3DBaseTexture *)iface);
211 }
212
213 UINT WINAPI IWineD3DCubeTextureImpl_GetTextureDimensions(IWineD3DCubeTexture *iface){
214     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
215     TRACE("(%p)\n", This);
216
217     return GL_TEXTURE_CUBE_MAP_ARB;
218 }
219
220 void WINAPI IWineD3DCubeTextureImpl_ApplyStateChanges(IWineD3DCubeTexture *iface, 
221                                                         const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1], 
222                                                         const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1]) {
223     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
224     float matrix[16];
225     IWineD3DBaseTextureImpl_ApplyStateChanges((IWineD3DBaseTexture *)iface, textureStates, samplerStates);
226
227
228     /* Apply non-power2 mappings and texture offsets so long as the texture coords aren't projected or generated */
229     if(This->pow2scalingFactor != 1.0f) {
230         if((textureStates[WINED3DTSS_TEXCOORDINDEX] & 0xFFFF0000) == D3DTSS_TCI_PASSTHRU && (~textureStates[WINED3DTSS_TEXTURETRANSFORMFLAGS] & D3DTTFF_PROJECTED)) {
231             glMatrixMode(GL_TEXTURE);
232             memset(matrix, 0 , sizeof(matrix));
233
234             matrix[0] = This->pow2scalingFactor;
235             matrix[5] = This->pow2scalingFactor;
236             matrix[10] = This->pow2scalingFactor;
237 #if 0 /* Translation fixup is no longer required (here for reminder) */
238             matrix[12] = -0.25f / (float)This->edgeLength;
239             matrix[13] = -0.75f / (float)This->edgeLength;
240             matrix[14] = -0.25f / (float)This->edgeLength;
241 #endif
242             TRACE("(%p) Setup Matrix:\n", This);
243             TRACE(" %f %f %f %f\n", matrix[0], matrix[1], matrix[2], matrix[3]);
244             TRACE(" %f %f %f %f\n", matrix[4], matrix[5], matrix[6], matrix[7]);
245             TRACE(" %f %f %f %f\n", matrix[8], matrix[9], matrix[10], matrix[11]);
246             TRACE(" %f %f %f %f\n", matrix[12], matrix[13], matrix[14], matrix[15]);
247             TRACE("\n");
248             glMultMatrixf(matrix);
249         } else {
250             /* I don't expect nonpower 2 textures to be used with generated texture coordinates, but if they are present a fixme. */
251             FIXME("Non-power2 texture being used with generated texture coords\n");
252         }
253     }
254
255 }
256
257
258 /* *******************************************
259    IWineD3DCubeTexture IWineD3DCubeTexture parts follow
260    ******************************************* */
261 HRESULT WINAPI IWineD3DCubeTextureImpl_GetLevelDesc(IWineD3DCubeTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
262     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
263
264     if (Level < This->baseTexture.levels) {
265         TRACE("(%p) level (%d)\n", This, Level);
266         return IWineD3DSurface_GetDesc(This->surfaces[0][Level], pDesc);
267     }
268     FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
269     return D3DERR_INVALIDCALL;
270 }
271
272 HRESULT WINAPI IWineD3DCubeTextureImpl_GetCubeMapSurface(IWineD3DCubeTexture *iface, D3DCUBEMAP_FACES FaceType, UINT Level, IWineD3DSurface** ppCubeMapSurface) {
273     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
274     HRESULT hr = D3DERR_INVALIDCALL;
275
276     if (Level < This->baseTexture.levels && FaceType >= D3DCUBEMAP_FACE_POSITIVE_X && FaceType <= D3DCUBEMAP_FACE_NEGATIVE_Z) {
277         *ppCubeMapSurface = This->surfaces[FaceType][Level];
278         IWineD3DSurface_AddRef(*ppCubeMapSurface);
279
280         hr = D3D_OK;
281     }
282     if (D3D_OK == hr) {
283         TRACE("(%p) -> faceType(%d) level(%d) returning surface@%p\n", This, FaceType, Level, This->surfaces[FaceType][Level]);
284     } else {
285         WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
286     }
287
288     return hr;
289 }
290
291 HRESULT WINAPI IWineD3DCubeTextureImpl_LockRect(IWineD3DCubeTexture *iface, D3DCUBEMAP_FACES FaceType, UINT Level, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
292     HRESULT hr = D3DERR_INVALIDCALL;
293     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
294
295     if (Level < This->baseTexture.levels && FaceType >= D3DCUBEMAP_FACE_POSITIVE_X && FaceType <= D3DCUBEMAP_FACE_NEGATIVE_Z) {
296         hr = IWineD3DSurface_LockRect(This->surfaces[FaceType][Level], pLockedRect, pRect, Flags);
297     }
298
299     if (D3D_OK == hr) {
300         TRACE("(%p) -> faceType(%d) level(%d) returning memory@%p success(%lu)\n", This, FaceType, Level, pLockedRect->pBits, hr);
301     } else {
302         WARN("(%p) level(%d) overflow Levels(%d)  Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
303     }
304
305     return hr;
306 }
307
308 HRESULT WINAPI IWineD3DCubeTextureImpl_UnlockRect(IWineD3DCubeTexture *iface, D3DCUBEMAP_FACES FaceType, UINT Level) {
309     HRESULT hr = D3DERR_INVALIDCALL;
310     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
311
312     if (Level < This->baseTexture.levels && FaceType >= D3DCUBEMAP_FACE_POSITIVE_X && FaceType <= D3DCUBEMAP_FACE_NEGATIVE_Z) {
313         hr = IWineD3DSurface_UnlockRect(This->surfaces[FaceType][Level]);
314     }
315
316     if (D3D_OK == hr) {
317         TRACE("(%p) -> faceType(%d) level(%d) success(%lu)\n", This, FaceType, Level, hr);
318     } else {
319         WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
320     }
321     return hr;
322 }
323
324 HRESULT  WINAPI IWineD3DCubeTextureImpl_AddDirtyRect(IWineD3DCubeTexture *iface, D3DCUBEMAP_FACES FaceType, CONST RECT* pDirtyRect) {
325     HRESULT hr = D3DERR_INVALIDCALL;
326     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
327     This->baseTexture.dirty = TRUE;
328     TRACE("(%p) : dirtyfication of faceType(%d) Level (0)\n", This, FaceType);
329     if (FaceType >= D3DCUBEMAP_FACE_POSITIVE_X && FaceType <= D3DCUBEMAP_FACE_NEGATIVE_Z) {
330         hr = IWineD3DSurface_AddDirtyRect(This->surfaces[FaceType][0], pDirtyRect);
331     } else {
332         WARN("(%p) overflow FaceType(%d)\n", This, FaceType);
333     }
334     return hr;
335 }
336
337
338 const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl =
339 {
340     /* IUnknown */
341     IWineD3DCubeTextureImpl_QueryInterface,
342     IWineD3DCubeTextureImpl_AddRef,
343     IWineD3DCubeTextureImpl_Release,
344     /* IWineD3DResource */
345     IWineD3DCubeTextureImpl_GetParent,
346     IWineD3DCubeTextureImpl_GetDevice,
347     IWineD3DCubeTextureImpl_SetPrivateData,
348     IWineD3DCubeTextureImpl_GetPrivateData,
349     IWineD3DCubeTextureImpl_FreePrivateData,
350     IWineD3DCubeTextureImpl_SetPriority,
351     IWineD3DCubeTextureImpl_GetPriority,
352     IWineD3DCubeTextureImpl_PreLoad,
353     IWineD3DCubeTextureImpl_GetType,
354     /* IWineD3DBaseTexture */
355     IWineD3DCubeTextureImpl_SetLOD,
356     IWineD3DCubeTextureImpl_GetLOD,
357     IWineD3DCubeTextureImpl_GetLevelCount,
358     IWineD3DCubeTextureImpl_SetAutoGenFilterType,
359     IWineD3DCubeTextureImpl_GetAutoGenFilterType,
360     IWineD3DCubeTextureImpl_GenerateMipSubLevels,
361     IWineD3DCubeTextureImpl_SetDirty,
362     IWineD3DCubeTextureImpl_GetDirty,
363     IWineD3DCubeTextureImpl_BindTexture,
364     IWineD3DCubeTextureImpl_UnBindTexture,
365     IWineD3DCubeTextureImpl_GetTextureDimensions,
366     IWineD3DCubeTextureImpl_ApplyStateChanges,
367     /* IWineD3DCubeTexture */
368     IWineD3DCubeTextureImpl_GetLevelDesc,
369     IWineD3DCubeTextureImpl_GetCubeMapSurface,
370     IWineD3DCubeTextureImpl_LockRect,
371     IWineD3DCubeTextureImpl_UnlockRect,
372     IWineD3DCubeTextureImpl_AddDirtyRect
373 };