wined3d: Add a new IWineD3DResource::UnLoad method.
[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 This->resource.wineD3DDevice->adapter->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     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
106     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
107     BOOL srgb_mode = This->baseTexture.is_srgb;
108     BOOL srgb_was_toggled = FALSE;
109
110     TRACE("(%p) : About to load texture: dirtified(%d)\n", This, This->baseTexture.dirty);
111
112     /* We only have to activate a context for gl when we're not drawing. In most cases PreLoad will be called during draw
113      * and a context was activated at the beginning of drawPrimitive
114      */
115     if(!device->isInDraw) {
116         /* No danger of recursive calls, ActivateContext sets isInDraw to true when loading
117          * offscreen render targets into their texture
118          */
119         ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
120     } else if (GL_SUPPORT(EXT_TEXTURE_SRGB) && This->baseTexture.bindCount > 0) {
121         srgb_mode = device->stateBlock->samplerState[This->baseTexture.sampler][WINED3DSAMP_SRGBTEXTURE];
122         srgb_was_toggled = (This->baseTexture.is_srgb != srgb_mode);
123         This->baseTexture.is_srgb = srgb_mode;
124     }
125     IWineD3DCubeTexture_BindTexture(iface);
126
127     ENTER_GL();
128     /* If the texture is marked dirty or the srgb sampler setting has changed since the last load then reload the surfaces */
129     if (This->baseTexture.dirty) {
130         for (i = 0; i < This->baseTexture.levels; i++) {
131             for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
132                 IWineD3DSurface_LoadTexture(This->surfaces[j][i], srgb_mode);
133             }
134         }
135     } else if (srgb_was_toggled) {
136         /* Loop is repeated in the else block with the extra AddDirtyRect line to avoid the alternative of
137          * checking srgb_was_toggled in every iteration, even when the texture is just dirty
138          */
139         if (This->baseTexture.srgb_mode_change_count < 20)
140             ++This->baseTexture.srgb_mode_change_count;
141         else
142             FIXME("Cubetexture (%p) has been reloaded at least 20 times due to WINED3DSAMP_SRGBTEXTURE changes on it\'s sampler\n", This);
143
144         for (i = 0; i < This->baseTexture.levels; i++) {
145             for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z ; j++) {
146                 IWineD3DSurface_AddDirtyRect(This->surfaces[j][i], NULL);
147                 IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], This->baseTexture.textureName, cube_targets[j]);
148                 IWineD3DSurface_LoadTexture(This->surfaces[j][i], srgb_mode);
149             }
150         }
151     } else {
152         TRACE("(%p) Texture not dirty, nothing to do\n" , iface);
153     }
154     LEAVE_GL();
155
156     /* No longer dirty */
157     This->baseTexture.dirty = FALSE;
158     return ;
159 }
160
161 static void WINAPI IWineD3DCubeTextureImpl_UnLoad(IWineD3DCubeTexture *iface) {
162     IWineD3DResourceImpl_UnLoad((IWineD3DResource *)iface);
163 }
164
165 static WINED3DRESOURCETYPE WINAPI IWineD3DCubeTextureImpl_GetType(IWineD3DCubeTexture *iface) {
166     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
167 }
168
169 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetParent(IWineD3DCubeTexture *iface, IUnknown **pParent) {
170     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
171 }
172
173 /* ******************************************************
174    IWineD3DCubeTexture IWineD3DBaseTexture parts follow
175    ****************************************************** */
176 static DWORD WINAPI IWineD3DCubeTextureImpl_SetLOD(IWineD3DCubeTexture *iface, DWORD LODNew) {
177     return IWineD3DBaseTextureImpl_SetLOD((IWineD3DBaseTexture *)iface, LODNew);
178 }
179
180 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLOD(IWineD3DCubeTexture *iface) {
181     return IWineD3DBaseTextureImpl_GetLOD((IWineD3DBaseTexture *)iface);
182 }
183
184 static DWORD WINAPI IWineD3DCubeTextureImpl_GetLevelCount(IWineD3DCubeTexture *iface) {
185     return IWineD3DBaseTextureImpl_GetLevelCount((IWineD3DBaseTexture *)iface);
186 }
187
188 static HRESULT WINAPI IWineD3DCubeTextureImpl_SetAutoGenFilterType(IWineD3DCubeTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
189   return IWineD3DBaseTextureImpl_SetAutoGenFilterType((IWineD3DBaseTexture *)iface, FilterType);
190 }
191
192 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DCubeTextureImpl_GetAutoGenFilterType(IWineD3DCubeTexture *iface) {
193   return IWineD3DBaseTextureImpl_GetAutoGenFilterType((IWineD3DBaseTexture *)iface);
194 }
195
196 static void WINAPI IWineD3DCubeTextureImpl_GenerateMipSubLevels(IWineD3DCubeTexture *iface) {
197     IWineD3DBaseTextureImpl_GenerateMipSubLevels((IWineD3DBaseTexture *)iface);
198 }
199
200 /* Internal function, No d3d mapping */
201 static BOOL WINAPI IWineD3DCubeTextureImpl_SetDirty(IWineD3DCubeTexture *iface, BOOL dirty) {
202     return IWineD3DBaseTextureImpl_SetDirty((IWineD3DBaseTexture *)iface, dirty);
203 }
204
205 /* Internal function, No d3d mapping */
206 static BOOL WINAPI IWineD3DCubeTextureImpl_GetDirty(IWineD3DCubeTexture *iface) {
207     return IWineD3DBaseTextureImpl_GetDirty((IWineD3DBaseTexture *)iface);
208 }
209
210 static HRESULT WINAPI IWineD3DCubeTextureImpl_BindTexture(IWineD3DCubeTexture *iface) {
211     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
212     BOOL set_gl_texture_desc = This->baseTexture.textureName == 0;
213     HRESULT hr;
214
215     TRACE("(%p) : relay to BaseTexture\n", This);
216
217     hr = IWineD3DBaseTextureImpl_BindTexture((IWineD3DBaseTexture *)iface);
218     if (set_gl_texture_desc && SUCCEEDED(hr)) {
219         UINT i, j;
220         for (i = 0; i < This->baseTexture.levels; ++i) {
221             for (j = WINED3DCUBEMAP_FACE_POSITIVE_X; j <= WINED3DCUBEMAP_FACE_NEGATIVE_Z; ++j) {
222                 IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], This->baseTexture.textureName, cube_targets[j]);
223             }
224         }
225     }
226
227     return hr;
228 }
229
230 static HRESULT WINAPI IWineD3DCubeTextureImpl_UnBindTexture(IWineD3DCubeTexture *iface) {
231     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
232     TRACE("(%p) : relay to BaseTexture\n", This);
233     return IWineD3DBaseTextureImpl_UnBindTexture((IWineD3DBaseTexture *)iface);
234 }
235
236 static UINT WINAPI IWineD3DCubeTextureImpl_GetTextureDimensions(IWineD3DCubeTexture *iface){
237     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
238     TRACE("(%p)\n", This);
239
240     return GL_TEXTURE_CUBE_MAP_ARB;
241 }
242
243 static void WINAPI IWineD3DCubeTextureImpl_ApplyStateChanges(IWineD3DCubeTexture *iface, 
244                                                         const DWORD textureStates[WINED3D_HIGHEST_TEXTURE_STATE + 1], 
245                                                         const DWORD samplerStates[WINED3D_HIGHEST_SAMPLER_STATE + 1]) {
246     TRACE("(%p) : relay to BaseTexture\n", iface);
247     IWineD3DBaseTextureImpl_ApplyStateChanges((IWineD3DBaseTexture *)iface, textureStates, samplerStates);
248 }
249
250
251 /* *******************************************
252    IWineD3DCubeTexture IWineD3DCubeTexture parts follow
253    ******************************************* */
254 static void WINAPI IWineD3DCubeTextureImpl_Destroy(IWineD3DCubeTexture *iface, D3DCB_DESTROYSURFACEFN D3DCB_DestroySurface) {
255     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
256     unsigned int i,j;
257     TRACE("(%p) : Cleaning up\n",This);
258     for (i = 0; i < This->baseTexture.levels; i++) {
259         for (j = 0; j < 6; j++) {
260             if (This->surfaces[j][i] != NULL) {
261                 /* Clean out the texture name we gave to the surface so that the surface doesn't try and release it */
262                 IWineD3DSurface_SetGlTextureDesc(This->surfaces[j][i], 0, 0);
263                 /* Cleanup the container */
264                 IWineD3DSurface_SetContainer(This->surfaces[j][i], 0);
265                 D3DCB_DestroySurface(This->surfaces[j][i]);
266             }
267         }
268     }
269     IWineD3DBaseTextureImpl_CleanUp((IWineD3DBaseTexture *) iface);
270     /* finally delete the object */
271     HeapFree(GetProcessHeap(), 0, This);
272 }
273
274 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetLevelDesc(IWineD3DCubeTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
275     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
276
277     if (Level < This->baseTexture.levels) {
278         TRACE("(%p) level (%d)\n", This, Level);
279         return IWineD3DSurface_GetDesc(This->surfaces[0][Level], pDesc);
280     }
281     FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
282     return WINED3DERR_INVALIDCALL;
283 }
284
285 static HRESULT WINAPI IWineD3DCubeTextureImpl_GetCubeMapSurface(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level, IWineD3DSurface** ppCubeMapSurface) {
286     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
287     HRESULT hr = WINED3DERR_INVALIDCALL;
288
289     if (Level < This->baseTexture.levels && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
290         *ppCubeMapSurface = This->surfaces[FaceType][Level];
291         IWineD3DSurface_AddRef(*ppCubeMapSurface);
292
293         hr = WINED3D_OK;
294     }
295     if (WINED3D_OK == hr) {
296         TRACE("(%p) -> faceType(%d) level(%d) returning surface@%p\n", This, FaceType, Level, This->surfaces[FaceType][Level]);
297     } else {
298         WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
299     }
300
301     return hr;
302 }
303
304 static HRESULT WINAPI IWineD3DCubeTextureImpl_LockRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
305     HRESULT hr = WINED3DERR_INVALIDCALL;
306     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
307
308     if (Level < This->baseTexture.levels && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
309         hr = IWineD3DSurface_LockRect(This->surfaces[FaceType][Level], pLockedRect, pRect, Flags);
310     }
311
312     if (WINED3D_OK == hr) {
313         TRACE("(%p) -> faceType(%d) level(%d) returning memory@%p success(%u)\n", This, FaceType, Level, pLockedRect->pBits, hr);
314     } else {
315         WARN("(%p) level(%d) overflow Levels(%d)  Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
316     }
317
318     return hr;
319 }
320
321 static HRESULT WINAPI IWineD3DCubeTextureImpl_UnlockRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, UINT Level) {
322     HRESULT hr = WINED3DERR_INVALIDCALL;
323     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
324
325     if (Level < This->baseTexture.levels && FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
326         hr = IWineD3DSurface_UnlockRect(This->surfaces[FaceType][Level]);
327     }
328
329     if (WINED3D_OK == hr) {
330         TRACE("(%p) -> faceType(%d) level(%d) success(%u)\n", This, FaceType, Level, hr);
331     } else {
332         WARN("(%p) level(%d) overflow Levels(%d) Or FaceType(%d)\n", This, Level, This->baseTexture.levels, FaceType);
333     }
334     return hr;
335 }
336
337 static HRESULT  WINAPI IWineD3DCubeTextureImpl_AddDirtyRect(IWineD3DCubeTexture *iface, WINED3DCUBEMAP_FACES FaceType, CONST RECT* pDirtyRect) {
338     HRESULT hr = WINED3DERR_INVALIDCALL;
339     IWineD3DCubeTextureImpl *This = (IWineD3DCubeTextureImpl *)iface;
340     This->baseTexture.dirty = TRUE;
341     TRACE("(%p) : dirtyfication of faceType(%d) Level (0)\n", This, FaceType);
342     if (FaceType <= WINED3DCUBEMAP_FACE_NEGATIVE_Z) {
343         hr = IWineD3DSurface_AddDirtyRect(This->surfaces[FaceType][0], pDirtyRect);
344     } else {
345         WARN("(%p) overflow FaceType(%d)\n", This, FaceType);
346     }
347     return hr;
348 }
349
350
351 const IWineD3DCubeTextureVtbl IWineD3DCubeTexture_Vtbl =
352 {
353     /* IUnknown */
354     IWineD3DCubeTextureImpl_QueryInterface,
355     IWineD3DCubeTextureImpl_AddRef,
356     IWineD3DCubeTextureImpl_Release,
357     /* IWineD3DResource */
358     IWineD3DCubeTextureImpl_GetParent,
359     IWineD3DCubeTextureImpl_GetDevice,
360     IWineD3DCubeTextureImpl_SetPrivateData,
361     IWineD3DCubeTextureImpl_GetPrivateData,
362     IWineD3DCubeTextureImpl_FreePrivateData,
363     IWineD3DCubeTextureImpl_SetPriority,
364     IWineD3DCubeTextureImpl_GetPriority,
365     IWineD3DCubeTextureImpl_PreLoad,
366     IWineD3DCubeTextureImpl_UnLoad,
367     IWineD3DCubeTextureImpl_GetType,
368     /* IWineD3DBaseTexture */
369     IWineD3DCubeTextureImpl_SetLOD,
370     IWineD3DCubeTextureImpl_GetLOD,
371     IWineD3DCubeTextureImpl_GetLevelCount,
372     IWineD3DCubeTextureImpl_SetAutoGenFilterType,
373     IWineD3DCubeTextureImpl_GetAutoGenFilterType,
374     IWineD3DCubeTextureImpl_GenerateMipSubLevels,
375     IWineD3DCubeTextureImpl_SetDirty,
376     IWineD3DCubeTextureImpl_GetDirty,
377     IWineD3DCubeTextureImpl_BindTexture,
378     IWineD3DCubeTextureImpl_UnBindTexture,
379     IWineD3DCubeTextureImpl_GetTextureDimensions,
380     IWineD3DCubeTextureImpl_ApplyStateChanges,
381     /* IWineD3DCubeTexture */
382     IWineD3DCubeTextureImpl_Destroy,
383     IWineD3DCubeTextureImpl_GetLevelDesc,
384     IWineD3DCubeTextureImpl_GetCubeMapSurface,
385     IWineD3DCubeTextureImpl_LockRect,
386     IWineD3DCubeTextureImpl_UnlockRect,
387     IWineD3DCubeTextureImpl_AddDirtyRect
388 };