.gitignore: Added wildcards to ignore generated resource files.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 static 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_IWineD3DBase)
56         || IsEqualGUID(riid, &IID_IWineD3DResource)
57         || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
58         || IsEqualGUID(riid, &IID_IWineD3DTexture)) {
59         IUnknown_AddRef(iface);
60         *ppobj = This;
61         return S_OK;
62     }
63     *ppobj = NULL;
64     return E_NOINTERFACE;
65 }
66
67 static ULONG WINAPI IWineD3DCubeTextureImpl_AddRef(IWineD3DCubeTexture *iface) {
68     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
69     TRACE("(%p) : AddRef increasing from %ld\n", This, This->resource.ref);
70     return InterlockedIncrement(&This->resource.ref);
71 }
72
73 static ULONG WINAPI IWineD3DCubeTextureImpl_Release(IWineD3DCubeTexture *iface) {
74     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
75     ULONG ref;
76     TRACE("(%p) : Releasing from %ld\n", This, This->resource.ref);
77     ref = InterlockedDecrement(&This->resource.ref);
78     if (ref == 0) {
79         int i,j;
80         TRACE("(%p) : Cleaning up\n",This);
81         for (i = 0; i < This->baseTexture.levels; i++) {
82             for (j = 0; j < 6; j++) {
83                 if (This->surfaces[j][i] != NULL) {
84                     /* Because the surfaces were created using a callback we need to release there parent otehrwise we leave the parent hanging */
85                     IUnknown* surfaceParent;
86                     /* Clean out the texture name we gave to the suface so that the surface doesn't try and release it */
87                     IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], 0, 0);
88                     /* Cleanup the container */
89                     IWineD3DSurface_SetContainer(This->surfaces[j][i], 0);
90                     /* Now, release the parent, which will take care of cleaning up the surface for us */
91                     TRACE("(%p) : Releasing surface%d %d  %p\n", This, j, i, This->surfaces[j][i]);
92                     IWineD3DSurface_GetParent(This->surfaces[j][i], &surfaceParent);
93                     IUnknown_Release(surfaceParent);
94                     IUnknown_Release(surfaceParent);
95                 }
96             }
97         }
98         IWineD3DBaseTextureImpl_CleanUp((IWineD3DBaseTexture *) iface);
99         /* finally delete the object */
100         HeapFree(GetProcessHeap(), 0, This);
101     }
102     return ref;
103 }
104
105 /* ****************************************************
106    IWineD3DCubeTexture IWineD3DResource parts follow
107    **************************************************** */
108 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetDevice(IWineD3DCubeTexture *iface, IWineD3DDevice** ppDevice) {
109     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
110 }
111
112 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
113     return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
114 }
115
116 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetPrivateData(IWineD3DCubeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
117     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
118 }
119
120 static HRESULT WINAPI IWineD3DCubeTextureImpl_FreePrivateData(IWineD3DCubeTexture *iface, REFGUID refguid) {
121     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
122 }
123
124 static DWORD WINAPI IWineD3DCubeTextureImpl_SetPriority(IWineD3DCubeTexture *iface, DWORD PriorityNew) {
125     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
126 }
127
128 static DWORD WINAPI IWineD3DCubeTextureImpl_GetPriority(IWineD3DCubeTexture *iface) {
129     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
130 }
131
132 static void WINAPI IWineD3DCubeTextureImpl_PreLoad(IWineD3DCubeTexture *iface) {
133     /* Override the IWineD3DResource Preload method */
134     unsigned int i,j;
135     BOOL setGlTextureDesc = FALSE;
136     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
137
138     TRACE("(%p) : About to load texture: dirtified(%d)\n", This, This->baseTexture.dirty);
139
140     if (This->baseTexture.textureName == 0)  setGlTextureDesc = TRUE;
141
142     IWineD3DCubeTexture_BindTexture(iface);
143
144     ENTER_GL();
145     /* If were dirty then reload the surfaces */
146     if (This->baseTexture.dirty != FALSE) {
147         for (i = 0; i < This->baseTexture.levels; i++) {
148             for (j = D3DCUBEMAP_FACE_POSITIVE_X; j <= D3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
149                 if(setGlTextureDesc)
150                       IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], This->baseTexture.textureName, cube_targets[j]);
151                 IWineD3DSurface_LoadTexture(This->surfaces[j][i]);
152             }
153         }
154         /* No longer dirty */
155         This->baseTexture.dirty = FALSE;
156     }
157     LEAVE_GL();
158     return ;
159 }
160
161 static WINED3DRESOURCETYPE WINAPI IWineD3DCubeTextureImpl_GetType(IWineD3DCubeTexture *iface) {
162     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
163 }
164
165 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetParent(IWineD3DCubeTexture *iface, IUnknown **pParent) {
166     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
167 }
168
169 /* ******************************************************
170    IWineD3DCubeTexture IWineD3DBaseTexture parts follow
171    ****************************************************** */
172 static DWORD WINAPI IWineD3DCubeTextureImpl_SetLOD(IWineD3DCubeTexture *iface, DWORD LODNew) {
173     return IWineD3DBaseTextureImpl_SetLOD((IWineD3DBaseTexture *)iface, LODNew);
174 }
175
176 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLOD(IWineD3DCubeTexture *iface) {
177     return IWineD3DBaseTextureImpl_GetLOD((IWineD3DBaseTexture *)iface);
178 }
179
180 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLevelCount(IWineD3DCubeTexture *iface) {
181     return IWineD3DBaseTextureImpl_GetLevelCount((IWineD3DBaseTexture *)iface);
182 }
183
184 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetAutoGenFilterType(IWineD3DCubeTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
185   return IWineD3DBaseTextureImpl_SetAutoGenFilterType((IWineD3DBaseTexture *)iface, FilterType);
186 }
187
188 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DCubeTextureImpl_GetAutoGenFilterType(IWineD3DCubeTexture *iface) {
189   return IWineD3DBaseTextureImpl_GetAutoGenFilterType((IWineD3DBaseTexture *)iface);
190 }
191
192 static void WINAPI IWineD3DCubeTextureImpl_GenerateMipSubLevels(IWineD3DCubeTexture *iface) {
193   return IWineD3DBaseTextureImpl_GenerateMipSubLevels((IWineD3DBaseTexture *)iface);
194 }
195
196 /* Internal function, No d3d mapping */
197 static BOOL WINAPI IWineD3DCubeTextureImpl_SetDirty(IWineD3DCubeTexture *iface, BOOL dirty) {
198     return IWineD3DBaseTextureImpl_SetDirty((IWineD3DBaseTexture *)iface, dirty);
199 }
200
201 /* Internal function, No d3d mapping */
202 static BOOL WINAPI IWineD3DCubeTextureImpl_GetDirty(IWineD3DCubeTexture *iface) {
203     return IWineD3DBaseTextureImpl_GetDirty((IWineD3DBaseTexture *)iface);
204 }
205
206 static HRESULT WINAPI IWineD3DCubeTextureImpl_BindTexture(IWineD3DCubeTexture *iface) {
207     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
208     TRACE("(%p) : relay to BaseTexture\n", This);
209     return IWineD3DBaseTextureImpl_BindTexture((IWineD3DBaseTexture *)iface);
210 }
211
212 static HRESULT WINAPI IWineD3DCubeTextureImpl_UnBindTexture(IWineD3DCubeTexture *iface) {
213     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
214     TRACE("(%p) : relay to BaseTexture\n", This);
215     return IWineD3DBaseTextureImpl_UnBindTexture((IWineD3DBaseTexture *)iface);
216 }
217
218 static UINT WINAPI IWineD3DCubeTextureImpl_GetTextureDimensions(IWineD3DCubeTexture *iface){
219     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
220     TRACE("(%p)\n", This);
221
222     return GL_TEXTURE_CUBE_MAP_ARB;
223 }
224
225 static void WINAPI IWineD3DCubeTextureImpl_ApplyStateChanges(IWineD3DCubeTexture *iface, 
226                                                         const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1], 
227                                                         const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1]) {
228     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
229     float matrix[16];
230     IWineD3DBaseTextureImpl_ApplyStateChanges((IWineD3DBaseTexture *)iface, textureStates, samplerStates);
231
232
233     /* Apply non-power2 mappings and texture offsets so long as the texture coords aren't projected or generated */
234     if(This->pow2scalingFactor != 1.0f) {
235         if((textureStates[WINED3DTSS_TEXCOORDINDEX] & 0xFFFF0000) == D3DTSS_TCI_PASSTHRU && (~textureStates[WINED3DTSS_TEXTURETRANSFORMFLAGS] & D3DTTFF_PROJECTED)) {
236             glMatrixMode(GL_TEXTURE);
237             memset(matrix, 0 , sizeof(matrix));
238
239             matrix[0] = This->pow2scalingFactor;
240             matrix[5] = This->pow2scalingFactor;
241             matrix[10] = This->pow2scalingFactor;
242 #if 0 /* Translation fixup is no longer required (here for reminder) */
243             matrix[12] = -0.25f / (float)This->edgeLength;
244             matrix[13] = -0.75f / (float)This->edgeLength;
245             matrix[14] = -0.25f / (float)This->edgeLength;
246 #endif
247             TRACE("(%p) Setup Matrix:\n", This);
248             TRACE(" %f %f %f %f\n", matrix[0], matrix[1], matrix[2], matrix[3]);
249             TRACE(" %f %f %f %f\n", matrix[4], matrix[5], matrix[6], matrix[7]);
250             TRACE(" %f %f %f %f\n", matrix[8], matrix[9], matrix[10], matrix[11]);
251             TRACE(" %f %f %f %f\n", matrix[12], matrix[13], matrix[14], matrix[15]);
252             TRACE("\n");
253             glMultMatrixf(matrix);
254         } else {
255             /* I don't expect nonpower 2 textures to be used with generated texture coordinates, but if they are present a fixme. */
256             FIXME("Non-power2 texture being used with generated texture coords\n");
257         }
258     }
259
260 }
261
262
263 /* *******************************************
264    IWineD3DCubeTexture IWineD3DCubeTexture parts follow
265    ******************************************* */
266 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetLevelDesc(IWineD3DCubeTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
267     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
268
269     if (Level < This->baseTexture.levels) {
270         TRACE("(%p) level (%d)\n", This, Level);
271         return IWineD3DSurface_GetDesc(This->surfaces[0][Level], pDesc);
272     }
273     FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
274     return WINED3DERR_INVALIDCALL;
275 }
276
277 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetCubeMapSurface(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level, IWineD3DSurface** ppCubeMapSurface) {
278     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
279     HRESULT hr = WINED3DERR_INVALIDCALL;
280
281     if (Level < This->baseTexture.levels && FaceType >= D3DCUBEMAP_FACE_POSITIVE_X && FaceType <= D3DCUBEMAP_FACE_NEGATIVE_Z) {
282         *ppCubeMapSurface = This->surfaces[FaceType][Level];
283         IWineD3DSurface_AddRef(*ppCubeMapSurface);
284
285         hr = WINED3D_OK;
286     }
287     if (WINED3D_OK == hr) {
288         TRACE("(%p) -> faceType(%d) level(%d) returning surface@%p\n", This, FaceType, Level, This->surfaces[FaceType][Level]);
289     } else {
290         WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
291     }
292
293     return hr;
294 }
295
296 static HRESULT WINAPI IWineD3DCubeTextureImpl_LockRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
297     HRESULT hr = WINED3DERR_INVALIDCALL;
298     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
299
300     if (Level < This->baseTexture.levels && FaceType >= D3DCUBEMAP_FACE_POSITIVE_X && FaceType <= D3DCUBEMAP_FACE_NEGATIVE_Z) {
301         hr = IWineD3DSurface_LockRect(This->surfaces[FaceType][Level], pLockedRect, pRect, Flags);
302     }
303
304     if (WINED3D_OK == hr) {
305         TRACE("(%p) -> faceType(%d) level(%d) returning memory@%p success(%lu)\n", This, FaceType, Level, pLockedRect->pBits, hr);
306     } else {
307         WARN("(%p) level(%d) overflow Levels(%d)  Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
308     }
309
310     return hr;
311 }
312
313 static HRESULT WINAPI IWineD3DCubeTextureImpl_UnlockRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level) {
314     HRESULT hr = WINED3DERR_INVALIDCALL;
315     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
316
317     if (Level < This->baseTexture.levels && FaceType >= D3DCUBEMAP_FACE_POSITIVE_X && FaceType <= D3DCUBEMAP_FACE_NEGATIVE_Z) {
318         hr = IWineD3DSurface_UnlockRect(This->surfaces[FaceType][Level]);
319     }
320
321     if (WINED3D_OK == hr) {
322         TRACE("(%p) -> faceType(%d) level(%d) success(%lu)\n", This, FaceType, Level, hr);
323     } else {
324         WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
325     }
326     return hr;
327 }
328
329 static HRESULT  WINAPI IWineD3DCubeTextureImpl_AddDirtyRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, CONST RECT* pDirtyRect) {
330     HRESULT hr = WINED3DERR_INVALIDCALL;
331     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
332     This->baseTexture.dirty = TRUE;
333     TRACE("(%p) : dirtyfication of faceType(%d) Level (0)\n", This, FaceType);
334     if (FaceType >= D3DCUBEMAP_FACE_POSITIVE_X && FaceType <= D3DCUBEMAP_FACE_NEGATIVE_Z) {
335         hr = IWineD3DSurface_AddDirtyRect(This->surfaces[FaceType][0], pDirtyRect);
336     } else {
337         WARN("(%p) overflow FaceType(%d)\n", This, FaceType);
338     }
339     return hr;
340 }
341
342
343 const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl =
344 {
345     /* IUnknown */
346     IWineD3DCubeTextureImpl_QueryInterface,
347     IWineD3DCubeTextureImpl_AddRef,
348     IWineD3DCubeTextureImpl_Release,
349     /* IWineD3DResource */
350     IWineD3DCubeTextureImpl_GetParent,
351     IWineD3DCubeTextureImpl_GetDevice,
352     IWineD3DCubeTextureImpl_SetPrivateData,
353     IWineD3DCubeTextureImpl_GetPrivateData,
354     IWineD3DCubeTextureImpl_FreePrivateData,
355     IWineD3DCubeTextureImpl_SetPriority,
356     IWineD3DCubeTextureImpl_GetPriority,
357     IWineD3DCubeTextureImpl_PreLoad,
358     IWineD3DCubeTextureImpl_GetType,
359     /* IWineD3DBaseTexture */
360     IWineD3DCubeTextureImpl_SetLOD,
361     IWineD3DCubeTextureImpl_GetLOD,
362     IWineD3DCubeTextureImpl_GetLevelCount,
363     IWineD3DCubeTextureImpl_SetAutoGenFilterType,
364     IWineD3DCubeTextureImpl_GetAutoGenFilterType,
365     IWineD3DCubeTextureImpl_GenerateMipSubLevels,
366     IWineD3DCubeTextureImpl_SetDirty,
367     IWineD3DCubeTextureImpl_GetDirty,
368     IWineD3DCubeTextureImpl_BindTexture,
369     IWineD3DCubeTextureImpl_UnBindTexture,
370     IWineD3DCubeTextureImpl_GetTextureDimensions,
371     IWineD3DCubeTextureImpl_ApplyStateChanges,
372     /* IWineD3DCubeTexture */
373     IWineD3DCubeTextureImpl_GetLevelDesc,
374     IWineD3DCubeTextureImpl_GetCubeMapSurface,
375     IWineD3DCubeTextureImpl_LockRect,
376     IWineD3DCubeTextureImpl_UnlockRect,
377     IWineD3DCubeTextureImpl_AddDirtyRect
378 };