oledlg: Call the hook proc if present.
[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 %d\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 %d\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 surface 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) {
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 &&
236            (~textureStates[WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED)) {
237
238             glMatrixMode(GL_TEXTURE);
239             memset(matrix, 0 , sizeof(matrix));
240
241             matrix[0] = This->pow2scalingFactor;
242             matrix[5] = This->pow2scalingFactor;
243             matrix[10] = This->pow2scalingFactor;
244 #if 0 /* Translation fixup is no longer required (here for reminder) */
245             matrix[12] = -0.25f / (float)This->edgeLength;
246             matrix[13] = -0.75f / (float)This->edgeLength;
247             matrix[14] = -0.25f / (float)This->edgeLength;
248 #endif
249             TRACE("(%p) Setup Matrix:\n", This);
250             TRACE(" %f %f %f %f\n", matrix[0], matrix[1], matrix[2], matrix[3]);
251             TRACE(" %f %f %f %f\n", matrix[4], matrix[5], matrix[6], matrix[7]);
252             TRACE(" %f %f %f %f\n", matrix[8], matrix[9], matrix[10], matrix[11]);
253             TRACE(" %f %f %f %f\n", matrix[12], matrix[13], matrix[14], matrix[15]);
254             TRACE("\n");
255             glMultMatrixf(matrix);
256         } else {
257             /* I don't expect nonpower 2 textures to be used with generated texture coordinates, but if they are present a fixme. */
258             FIXME("Non-power2 texture being used with generated texture coords\n");
259         }
260     }
261
262 }
263
264
265 /* *******************************************
266    IWineD3DCubeTexture IWineD3DCubeTexture parts follow
267    ******************************************* */
268 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetLevelDesc(IWineD3DCubeTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
269     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
270
271     if (Level < This->baseTexture.levels) {
272         TRACE("(%p) level (%d)\n", This, Level);
273         return IWineD3DSurface_GetDesc(This->surfaces[0][Level], pDesc);
274     }
275     FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
276     return WINED3DERR_INVALIDCALL;
277 }
278
279 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetCubeMapSurface(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level, IWineD3DSurface** ppCubeMapSurface) {
280     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
281     HRESULT hr = WINED3DERR_INVALIDCALL;
282
283     if (Level < This->baseTexture.levels && FaceType >= D3DCUBEMAP_FACE_POSITIVE_X && FaceType <= D3DCUBEMAP_FACE_NEGATIVE_Z) {
284         *ppCubeMapSurface = This->surfaces[FaceType][Level];
285         IWineD3DSurface_AddRef(*ppCubeMapSurface);
286
287         hr = WINED3D_OK;
288     }
289     if (WINED3D_OK == hr) {
290         TRACE("(%p) -> faceType(%d) level(%d) returning surface@%p\n", This, FaceType, Level, This->surfaces[FaceType][Level]);
291     } else {
292         WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
293     }
294
295     return hr;
296 }
297
298 static HRESULT WINAPI IWineD3DCubeTextureImpl_LockRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
299     HRESULT hr = WINED3DERR_INVALIDCALL;
300     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
301
302     if (Level < This->baseTexture.levels && FaceType >= D3DCUBEMAP_FACE_POSITIVE_X && FaceType <= D3DCUBEMAP_FACE_NEGATIVE_Z) {
303         hr = IWineD3DSurface_LockRect(This->surfaces[FaceType][Level], pLockedRect, pRect, Flags);
304     }
305
306     if (WINED3D_OK == hr) {
307         TRACE("(%p) -> faceType(%d) level(%d) returning memory@%p success(%u)\n", This, FaceType, Level, pLockedRect->pBits, hr);
308     } else {
309         WARN("(%p) level(%d) overflow Levels(%d)  Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
310     }
311
312     return hr;
313 }
314
315 static HRESULT WINAPI IWineD3DCubeTextureImpl_UnlockRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level) {
316     HRESULT hr = WINED3DERR_INVALIDCALL;
317     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
318
319     if (Level < This->baseTexture.levels && FaceType >= D3DCUBEMAP_FACE_POSITIVE_X && FaceType <= D3DCUBEMAP_FACE_NEGATIVE_Z) {
320         hr = IWineD3DSurface_UnlockRect(This->surfaces[FaceType][Level]);
321     }
322
323     if (WINED3D_OK == hr) {
324         TRACE("(%p) -> faceType(%d) level(%d) success(%u)\n", This, FaceType, Level, hr);
325     } else {
326         WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
327     }
328     return hr;
329 }
330
331 static HRESULT  WINAPI IWineD3DCubeTextureImpl_AddDirtyRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, CONST RECT* pDirtyRect) {
332     HRESULT hr = WINED3DERR_INVALIDCALL;
333     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
334     This->baseTexture.dirty = TRUE;
335     TRACE("(%p) : dirtyfication of faceType(%d) Level (0)\n", This, FaceType);
336     if (FaceType >= D3DCUBEMAP_FACE_POSITIVE_X && FaceType <= D3DCUBEMAP_FACE_NEGATIVE_Z) {
337         hr = IWineD3DSurface_AddDirtyRect(This->surfaces[FaceType][0], pDirtyRect);
338     } else {
339         WARN("(%p) overflow FaceType(%d)\n", This, FaceType);
340     }
341     return hr;
342 }
343
344
345 const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl =
346 {
347     /* IUnknown */
348     IWineD3DCubeTextureImpl_QueryInterface,
349     IWineD3DCubeTextureImpl_AddRef,
350     IWineD3DCubeTextureImpl_Release,
351     /* IWineD3DResource */
352     IWineD3DCubeTextureImpl_GetParent,
353     IWineD3DCubeTextureImpl_GetDevice,
354     IWineD3DCubeTextureImpl_SetPrivateData,
355     IWineD3DCubeTextureImpl_GetPrivateData,
356     IWineD3DCubeTextureImpl_FreePrivateData,
357     IWineD3DCubeTextureImpl_SetPriority,
358     IWineD3DCubeTextureImpl_GetPriority,
359     IWineD3DCubeTextureImpl_PreLoad,
360     IWineD3DCubeTextureImpl_GetType,
361     /* IWineD3DBaseTexture */
362     IWineD3DCubeTextureImpl_SetLOD,
363     IWineD3DCubeTextureImpl_GetLOD,
364     IWineD3DCubeTextureImpl_GetLevelCount,
365     IWineD3DCubeTextureImpl_SetAutoGenFilterType,
366     IWineD3DCubeTextureImpl_GetAutoGenFilterType,
367     IWineD3DCubeTextureImpl_GenerateMipSubLevels,
368     IWineD3DCubeTextureImpl_SetDirty,
369     IWineD3DCubeTextureImpl_GetDirty,
370     IWineD3DCubeTextureImpl_BindTexture,
371     IWineD3DCubeTextureImpl_UnBindTexture,
372     IWineD3DCubeTextureImpl_GetTextureDimensions,
373     IWineD3DCubeTextureImpl_ApplyStateChanges,
374     /* IWineD3DCubeTexture */
375     IWineD3DCubeTextureImpl_GetLevelDesc,
376     IWineD3DCubeTextureImpl_GetCubeMapSurface,
377     IWineD3DCubeTextureImpl_LockRect,
378     IWineD3DCubeTextureImpl_UnlockRect,
379     IWineD3DCubeTextureImpl_AddDirtyRect
380 };