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