wined3d: Track overlay surfaces in the overlayed surface.
[wine] / dlls / wined3d / volume.c
1 /*
2  * IWineD3DVolume 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 /* *******************************************
30    IWineD3DVolume IUnknown parts follow
31    ******************************************* */
32 static HRESULT WINAPI IWineD3DVolumeImpl_QueryInterface(IWineD3DVolume *iface, REFIID riid, LPVOID *ppobj)
33 {
34     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
35     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
36     if (IsEqualGUID(riid, &IID_IUnknown)
37         || IsEqualGUID(riid, &IID_IWineD3DBase)
38         || IsEqualGUID(riid, &IID_IWineD3DVolume)){
39         IUnknown_AddRef(iface);
40         *ppobj = This;
41         return S_OK;
42     }
43     *ppobj = NULL;
44     return E_NOINTERFACE;
45 }
46
47 static ULONG WINAPI IWineD3DVolumeImpl_AddRef(IWineD3DVolume *iface) {
48     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
49     TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
50     return InterlockedIncrement(&This->resource.ref);
51 }
52
53 static ULONG WINAPI IWineD3DVolumeImpl_Release(IWineD3DVolume *iface) {
54     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
55     ULONG ref;
56     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
57     ref = InterlockedDecrement(&This->resource.ref);
58     if (ref == 0) {
59         IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
60         HeapFree(GetProcessHeap(), 0, This);
61     }
62     return ref;
63 }
64
65 /* ****************************************************
66    IWineD3DVolume IWineD3DResource parts follow
67    **************************************************** */
68 static HRESULT WINAPI IWineD3DVolumeImpl_GetParent(IWineD3DVolume *iface, IUnknown **pParent) {
69     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
70 }
71
72 static HRESULT WINAPI IWineD3DVolumeImpl_GetDevice(IWineD3DVolume *iface, IWineD3DDevice** ppDevice) {
73     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
74 }
75
76 static HRESULT WINAPI IWineD3DVolumeImpl_SetPrivateData(IWineD3DVolume *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
77     return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
78 }
79
80 static HRESULT WINAPI IWineD3DVolumeImpl_GetPrivateData(IWineD3DVolume *iface, REFGUID  refguid, void* pData, DWORD* pSizeOfData) {
81     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
82 }
83
84 static HRESULT WINAPI IWineD3DVolumeImpl_FreePrivateData(IWineD3DVolume *iface, REFGUID refguid) {
85     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
86 }
87
88 static DWORD WINAPI IWineD3DVolumeImpl_SetPriority(IWineD3DVolume *iface, DWORD PriorityNew) {
89     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
90 }
91
92 static DWORD WINAPI IWineD3DVolumeImpl_GetPriority(IWineD3DVolume *iface) {
93     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
94 }
95
96 static void WINAPI IWineD3DVolumeImpl_PreLoad(IWineD3DVolume *iface) {
97     IWineD3DResourceImpl_PreLoad((IWineD3DResource *)iface);
98 }
99
100 static void WINAPI IWineD3DVolumeImpl_UnLoad(IWineD3DVolume *iface) {
101     /* The whole content is shadowed on This->resource.allocatedMemory, and the
102      * texture name is managed by the VolumeTexture container
103      */
104     TRACE("(%p): Nothing to do\n", iface);
105 }
106
107 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeImpl_GetType(IWineD3DVolume *iface) {
108     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
109 }
110
111 /* *******************************************
112    IWineD3DVolume parts follow
113    ******************************************* */
114 static HRESULT WINAPI IWineD3DVolumeImpl_GetContainer(IWineD3DVolume *iface, REFIID riid, void** ppContainer) {
115     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
116
117     TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
118
119     if (!ppContainer) {
120         ERR("Called without a valid ppContainer.\n");
121     }
122
123     /* Although surfaces can be standalone, volumes can't */
124     if (!This->container) {
125         ERR("Volume without an container. Should not happen.\n");
126     }
127
128     TRACE("Relaying to QueryInterface\n");
129     return IUnknown_QueryInterface(This->container, riid, ppContainer);
130 }
131
132 static HRESULT WINAPI IWineD3DVolumeImpl_GetDesc(IWineD3DVolume *iface, WINED3DVOLUME_DESC* pDesc) {
133     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
134     TRACE("(%p) : copying into %p\n", This, pDesc);
135
136     *(pDesc->Format)  = This->resource.format;
137     *(pDesc->Type)    = This->resource.resourceType;
138     *(pDesc->Usage)   = This->resource.usage;
139     *(pDesc->Pool)    = This->resource.pool;
140     *(pDesc->Size)    = This->resource.size; /* dx8 only */
141     *(pDesc->Width)   = This->currentDesc.Width;
142     *(pDesc->Height)  = This->currentDesc.Height;
143     *(pDesc->Depth)   = This->currentDesc.Depth;
144     return WINED3D_OK;
145 }
146
147 static HRESULT WINAPI IWineD3DVolumeImpl_LockBox(IWineD3DVolume *iface, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
148     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
149     FIXME("(%p) : pBox=%p stub\n", This, pBox);
150
151     if(!This->resource.allocatedMemory) {
152         This->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size);
153     }
154
155     /* fixme: should we really lock as such? */
156     TRACE("(%p) : box=%p, output pbox=%p, allMem=%p\n", This, pBox, pLockedVolume, This->resource.allocatedMemory);
157
158     pLockedVolume->RowPitch   = This->bytesPerPixel * This->currentDesc.Width;                        /* Bytes / row   */
159     pLockedVolume->SlicePitch = This->bytesPerPixel * This->currentDesc.Width * This->currentDesc.Height;  /* Bytes / slice */
160     if (!pBox) {
161         TRACE("No box supplied - all is ok\n");
162         pLockedVolume->pBits = This->resource.allocatedMemory;
163         This->lockedBox.Left   = 0;
164         This->lockedBox.Top    = 0;
165         This->lockedBox.Front  = 0;
166         This->lockedBox.Right  = This->currentDesc.Width;
167         This->lockedBox.Bottom = This->currentDesc.Height;
168         This->lockedBox.Back   = This->currentDesc.Depth;
169     } else {
170         TRACE("Lock Box (%p) = l %d, t %d, r %d, b %d, fr %d, ba %d\n", pBox, pBox->Left, pBox->Top, pBox->Right, pBox->Bottom, pBox->Front, pBox->Back);
171         pLockedVolume->pBits = This->resource.allocatedMemory +
172           (pLockedVolume->SlicePitch * pBox->Front) + /* FIXME: is front < back or vica versa? */
173           (pLockedVolume->RowPitch * pBox->Top) +
174           (pBox->Left * This->bytesPerPixel);
175         This->lockedBox.Left   = pBox->Left;
176         This->lockedBox.Top    = pBox->Top;
177         This->lockedBox.Front  = pBox->Front;
178         This->lockedBox.Right  = pBox->Right;
179         This->lockedBox.Bottom = pBox->Bottom;
180         This->lockedBox.Back   = pBox->Back;
181     }
182
183     if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
184       /* Don't dirtify */
185     } else {
186       /**
187        * Dirtify on lock
188        * as seen in msdn docs
189        */
190       IWineD3DVolume_AddDirtyBox(iface, &This->lockedBox);
191
192       /**  Dirtify Container if needed */
193       if (NULL != This->container) {
194
195         IWineD3DVolumeTexture *cont = (IWineD3DVolumeTexture*) This->container;
196         WINED3DRESOURCETYPE containerType = IWineD3DBaseTexture_GetType((IWineD3DBaseTexture *) cont);
197
198         if (containerType == WINED3DRTYPE_VOLUMETEXTURE) {
199           IWineD3DBaseTextureImpl* pTexture = (IWineD3DBaseTextureImpl*) cont;
200           pTexture->baseTexture.dirty = TRUE;
201         } else {
202           FIXME("Set dirty on container type %d\n", containerType);
203         }
204       }
205     }
206
207     This->locked = TRUE;
208     TRACE("returning memory@%p rpitch(%d) spitch(%d)\n", pLockedVolume->pBits, pLockedVolume->RowPitch, pLockedVolume->SlicePitch);
209     return WINED3D_OK;
210 }
211
212 static HRESULT WINAPI IWineD3DVolumeImpl_UnlockBox(IWineD3DVolume *iface) {
213     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
214     if (!This->locked) {
215       ERR("trying to lock unlocked volume@%p\n", This);
216       return WINED3DERR_INVALIDCALL;
217     }
218     TRACE("(%p) : unlocking volume\n", This);
219     This->locked = FALSE;
220     memset(&This->lockedBox, 0, sizeof(RECT));
221     return WINED3D_OK;
222 }
223
224 /* Internal use functions follow : */
225
226 static HRESULT WINAPI IWineD3DVolumeImpl_CleanDirtyBox(IWineD3DVolume *iface) {
227   IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
228   This->dirty = FALSE;
229   This->lockedBox.Left   = This->currentDesc.Width;
230   This->lockedBox.Top    = This->currentDesc.Height;
231   This->lockedBox.Front  = This->currentDesc.Depth;
232   This->lockedBox.Right  = 0;
233   This->lockedBox.Bottom = 0;
234   This->lockedBox.Back   = 0;
235   return WINED3D_OK;
236 }
237
238 static HRESULT WINAPI IWineD3DVolumeImpl_AddDirtyBox(IWineD3DVolume *iface, CONST WINED3DBOX* pDirtyBox) {
239   IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
240   This->dirty = TRUE;
241    if (NULL != pDirtyBox) {
242     This->lockedBox.Left   = min(This->lockedBox.Left,   pDirtyBox->Left);
243     This->lockedBox.Top    = min(This->lockedBox.Top,    pDirtyBox->Top);
244     This->lockedBox.Front  = min(This->lockedBox.Front,  pDirtyBox->Front);
245     This->lockedBox.Right  = max(This->lockedBox.Right,  pDirtyBox->Right);
246     This->lockedBox.Bottom = max(This->lockedBox.Bottom, pDirtyBox->Bottom);
247     This->lockedBox.Back   = max(This->lockedBox.Back,   pDirtyBox->Back);
248   } else {
249     This->lockedBox.Left   = 0;
250     This->lockedBox.Top    = 0;
251     This->lockedBox.Front  = 0;
252     This->lockedBox.Right  = This->currentDesc.Width;
253     This->lockedBox.Bottom = This->currentDesc.Height;
254     This->lockedBox.Back   = This->currentDesc.Depth;
255   }
256   return WINED3D_OK;
257 }
258
259 static HRESULT WINAPI IWineD3DVolumeImpl_SetContainer(IWineD3DVolume *iface, IWineD3DBase* container) {
260     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
261
262     TRACE("This %p, container %p\n", This, container);
263
264     /* We can't keep a reference to the container, since the container already keeps a reference to us. */
265
266     TRACE("Setting container to %p from %p\n", container, This->container);
267     This->container = container;
268
269     return WINED3D_OK;
270 }
271
272 static HRESULT WINAPI IWineD3DVolumeImpl_LoadTexture(IWineD3DVolume *iface, int gl_level, BOOL srgb_mode) {
273     IWineD3DVolumeImpl *This     = (IWineD3DVolumeImpl *)iface;
274     WINED3DFORMAT format = This->resource.format;
275     const GlPixelFormatDesc *glDesc;
276     getFormatDescEntry(format, &GLINFO_LOCATION, &glDesc);
277
278     TRACE("(%p) : level %u, format %s (0x%08x)\n", This, gl_level, debug_d3dformat(format), format);
279
280     TRACE("Calling glTexImage3D %x level=%d, intfmt=%x, w=%d, h=%d,d=%d, 0=%d, glFmt=%x, glType=%x, Mem=%p\n",
281             GL_TEXTURE_3D,
282             gl_level,
283             glDesc->glInternal,
284             This->currentDesc.Width,
285             This->currentDesc.Height,
286             This->currentDesc.Depth,
287             0,
288             glDesc->glFormat,
289             glDesc->glType,
290             This->resource.allocatedMemory);
291     GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D,
292                 gl_level,
293                 glDesc->glInternal,
294                 This->currentDesc.Width,
295                 This->currentDesc.Height,
296                 This->currentDesc.Depth,
297                 0,
298                 glDesc->glFormat,
299                 glDesc->glType,
300                 This->resource.allocatedMemory));
301     checkGLcall("glTexImage3D");
302
303     /* When adding code releasing This->resource.allocatedMemory to save data keep in mind that
304      * GL_UNPACK_CLIENT_STORAGE_APPLE is enabled by default if supported(GL_APPLE_client_storage).
305      * Thus do not release This->resource.allocatedMemory if GL_APPLE_client_storage is supported.
306      */
307     return WINED3D_OK;
308
309 }
310
311 const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl =
312 {
313     /* IUnknown */
314     IWineD3DVolumeImpl_QueryInterface,
315     IWineD3DVolumeImpl_AddRef,
316     IWineD3DVolumeImpl_Release,
317     /* IWineD3DResource */
318     IWineD3DVolumeImpl_GetParent,
319     IWineD3DVolumeImpl_GetDevice,
320     IWineD3DVolumeImpl_SetPrivateData,
321     IWineD3DVolumeImpl_GetPrivateData,
322     IWineD3DVolumeImpl_FreePrivateData,
323     IWineD3DVolumeImpl_SetPriority,
324     IWineD3DVolumeImpl_GetPriority,
325     IWineD3DVolumeImpl_PreLoad,
326     IWineD3DVolumeImpl_UnLoad,
327     IWineD3DVolumeImpl_GetType,
328     /* IWineD3DVolume */
329     IWineD3DVolumeImpl_GetContainer,
330     IWineD3DVolumeImpl_GetDesc,
331     IWineD3DVolumeImpl_LockBox,
332     IWineD3DVolumeImpl_UnlockBox,
333     /* Internal interface */
334     IWineD3DVolumeImpl_AddDirtyBox,
335     IWineD3DVolumeImpl_CleanDirtyBox,
336     IWineD3DVolumeImpl_LoadTexture,
337     IWineD3DVolumeImpl_SetContainer
338 };