rpcrt4: Add functions for generating and parsing the upper floors in endpoint-mapper...
[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 ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->wineD3DDevice)->wineD3D))->gl_info
28
29 /* *******************************************
30    IWineD3DVolume IUnknown parts follow
31    ******************************************* */
32 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 ULONG WINAPI IWineD3DVolumeImpl_AddRef(IWineD3DVolume *iface) {
48     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
49     TRACE("(%p) : AddRef increasing from %ld\n", This, This->resource.ref);
50     return InterlockedIncrement(&This->resource.ref);
51 }
52
53 ULONG WINAPI IWineD3DVolumeImpl_Release(IWineD3DVolume *iface) {
54     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
55     ULONG ref;
56     TRACE("(%p) : Releasing from %ld\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 HRESULT WINAPI IWineD3DVolumeImpl_GetParent(IWineD3DVolume *iface, IUnknown **pParent) {
69     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
70 }
71
72 HRESULT WINAPI IWineD3DVolumeImpl_GetDevice(IWineD3DVolume *iface, IWineD3DDevice** ppDevice) {
73     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
74 }
75
76 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 HRESULT WINAPI IWineD3DVolumeImpl_GetPrivateData(IWineD3DVolume *iface, REFGUID  refguid, void* pData, DWORD* pSizeOfData) {
81     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
82 }
83
84 HRESULT WINAPI IWineD3DVolumeImpl_FreePrivateData(IWineD3DVolume *iface, REFGUID refguid) {
85     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
86 }
87
88 DWORD WINAPI IWineD3DVolumeImpl_SetPriority(IWineD3DVolume *iface, DWORD PriorityNew) {
89     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
90 }
91
92 DWORD WINAPI IWineD3DVolumeImpl_GetPriority(IWineD3DVolume *iface) {
93     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
94 }
95
96 void WINAPI IWineD3DVolumeImpl_PreLoad(IWineD3DVolume *iface) {
97     return IWineD3DResourceImpl_PreLoad((IWineD3DResource *)iface);
98 }
99
100 WINED3DRESOURCETYPE WINAPI IWineD3DVolumeImpl_GetType(IWineD3DVolume *iface) {
101     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
102 }
103
104 /* *******************************************
105    IWineD3DVolume parts follow
106    ******************************************* */
107 HRESULT WINAPI IWineD3DVolumeImpl_GetContainerParent(IWineD3DVolume *iface, IUnknown **ppContainerParent) {
108     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
109
110     TRACE("(%p) : ppContainerParent %p\n", This, ppContainerParent);
111
112     if (!ppContainerParent) {
113         ERR("(%p) : Called without a valid ppContainerParent\n", This);
114     }
115
116     if (This->container) {
117         IWineD3DBase_GetParent(This->container, ppContainerParent);
118         if (!ppContainerParent) {
119             /* WineD3D objects should always have a parent */
120             ERR("(%p) : GetParent returned NULL\n", This);
121         }
122         IUnknown_Release(*ppContainerParent); /* GetParent adds a reference; we want just the pointer */
123     } else {
124         *ppContainerParent = NULL;
125     }
126
127     return WINED3D_OK;
128 }
129
130 HRESULT WINAPI IWineD3DVolumeImpl_GetContainer(IWineD3DVolume *iface, REFIID riid, void** ppContainer) {
131     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
132
133     TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
134
135     if (!ppContainer) {
136         ERR("Called without a valid ppContainer.\n");
137     }
138
139     /* Although surfaces can be standalone, volumes can't */
140     if (!This->container) {
141         ERR("Volume without an container. Should not happen.\n");
142     }
143
144     TRACE("Relaying to QueryInterface\n");
145     if (IUnknown_QueryInterface(This->container, riid, ppContainer) != S_OK)
146         return WINED3DERR_INVALIDCALL;
147
148     return WINED3D_OK;
149 }
150
151 HRESULT WINAPI IWineD3DVolumeImpl_GetDesc(IWineD3DVolume *iface, WINED3DVOLUME_DESC* pDesc) {
152     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
153     TRACE("(%p) : copying into %p\n", This, pDesc);
154
155     *(pDesc->Format)  = This->resource.format;
156     *(pDesc->Type)    = This->resource.resourceType;
157     *(pDesc->Usage)   = This->resource.usage;
158     *(pDesc->Pool)    = This->resource.pool;
159     *(pDesc->Size)    = This->resource.size; /* dx8 only */
160     *(pDesc->Width)   = This->currentDesc.Width;
161     *(pDesc->Height)  = This->currentDesc.Height;
162     *(pDesc->Depth)   = This->currentDesc.Depth;
163     return WINED3D_OK;
164 }
165
166 HRESULT WINAPI IWineD3DVolumeImpl_LockBox(IWineD3DVolume *iface, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
167     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
168     FIXME("(%p) : pBox=%p stub\n", This, pBox);
169
170     /* fixme: should we really lock as such? */
171     TRACE("(%p) : box=%p, output pbox=%p, allMem=%p\n", This, pBox, pLockedVolume, This->resource.allocatedMemory);
172
173     pLockedVolume->RowPitch   = This->bytesPerPixel * This->currentDesc.Width;                        /* Bytes / row   */
174     pLockedVolume->SlicePitch = This->bytesPerPixel * This->currentDesc.Width * This->currentDesc.Height;  /* Bytes / slice */
175     if (!pBox) {
176         TRACE("No box supplied - all is ok\n");
177         pLockedVolume->pBits = This->resource.allocatedMemory;
178         This->lockedBox.Left   = 0;
179         This->lockedBox.Top    = 0;
180         This->lockedBox.Front  = 0;
181         This->lockedBox.Right  = This->currentDesc.Width;
182         This->lockedBox.Bottom = This->currentDesc.Height;
183         This->lockedBox.Back   = This->currentDesc.Depth;
184     } else {
185         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);
186         pLockedVolume->pBits = This->resource.allocatedMemory +
187           (pLockedVolume->SlicePitch * pBox->Front) + /* FIXME: is front < back or vica versa? */
188           (pLockedVolume->RowPitch * pBox->Top) +
189           (pBox->Left * This->bytesPerPixel);
190         This->lockedBox.Left   = pBox->Left;
191         This->lockedBox.Top    = pBox->Top;
192         This->lockedBox.Front  = pBox->Front;
193         This->lockedBox.Right  = pBox->Right;
194         This->lockedBox.Bottom = pBox->Bottom;
195         This->lockedBox.Back   = pBox->Back;
196     }
197
198     if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
199       /* Don't dirtify */
200     } else {
201       /**
202        * Dirtify on lock
203        * as seen in msdn docs
204        */
205       IWineD3DVolume_AddDirtyBox(iface, &This->lockedBox);
206
207       /**  Dirtify Container if needed */
208       if (NULL != This->container) {
209
210         IWineD3DVolumeTexture *cont = (IWineD3DVolumeTexture*) This->container;
211         WINED3DRESOURCETYPE containerType = IWineD3DBaseTexture_GetType((IWineD3DBaseTexture *) cont);
212
213         if (containerType == WINED3DRTYPE_VOLUMETEXTURE) {
214           IWineD3DBaseTextureImpl* pTexture = (IWineD3DBaseTextureImpl*) cont;
215           pTexture->baseTexture.dirty = TRUE;
216         } else {
217           FIXME("Set dirty on container type %d\n", containerType);
218         }
219       }
220     }
221
222     This->locked = TRUE;
223     TRACE("returning memory@%p rpitch(%d) spitch(%d)\n", pLockedVolume->pBits, pLockedVolume->RowPitch, pLockedVolume->SlicePitch);
224     return WINED3D_OK;
225 }
226
227 HRESULT WINAPI IWineD3DVolumeImpl_UnlockBox(IWineD3DVolume *iface) {
228     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
229     if (FALSE == This->locked) {
230       ERR("trying to lock unlocked volume@%p\n", This);
231       return WINED3DERR_INVALIDCALL;
232     }
233     TRACE("(%p) : unlocking volume\n", This);
234     This->locked = FALSE;
235     memset(&This->lockedBox, 0, sizeof(RECT));
236     return WINED3D_OK;
237 }
238
239 /* Internal use functions follow : */
240
241 HRESULT WINAPI IWineD3DVolumeImpl_CleanDirtyBox(IWineD3DVolume *iface) {
242   IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
243   This->dirty = FALSE;
244   This->lockedBox.Left   = This->currentDesc.Width;
245   This->lockedBox.Top    = This->currentDesc.Height;
246   This->lockedBox.Front  = This->currentDesc.Depth;
247   This->lockedBox.Right  = 0;
248   This->lockedBox.Bottom = 0;
249   This->lockedBox.Back   = 0;
250   return WINED3D_OK;
251 }
252
253 HRESULT WINAPI IWineD3DVolumeImpl_AddDirtyBox(IWineD3DVolume *iface, CONST WINED3DBOX* pDirtyBox) {
254   IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
255   This->dirty = TRUE;
256    if (NULL != pDirtyBox) {
257     This->lockedBox.Left   = min(This->lockedBox.Left,   pDirtyBox->Left);
258     This->lockedBox.Top    = min(This->lockedBox.Top,    pDirtyBox->Top);
259     This->lockedBox.Front  = min(This->lockedBox.Front,  pDirtyBox->Front);
260     This->lockedBox.Right  = max(This->lockedBox.Right,  pDirtyBox->Right);
261     This->lockedBox.Bottom = max(This->lockedBox.Bottom, pDirtyBox->Bottom);
262     This->lockedBox.Back   = max(This->lockedBox.Back,   pDirtyBox->Back);
263   } else {
264     This->lockedBox.Left   = 0;
265     This->lockedBox.Top    = 0;
266     This->lockedBox.Front  = 0;
267     This->lockedBox.Right  = This->currentDesc.Width;
268     This->lockedBox.Bottom = This->currentDesc.Height;
269     This->lockedBox.Back   = This->currentDesc.Depth;
270   }
271   return WINED3D_OK;
272 }
273
274 HRESULT WINAPI IWineD3DVolumeImpl_SetContainer(IWineD3DVolume *iface, IWineD3DBase* container) {
275     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
276
277     TRACE("This %p, container %p\n", This, container);
278
279     /* We can't keep a reference to the container, since the container already keeps a reference to us. */
280
281     TRACE("Setting container to %p from %p\n", container, This->container);
282     This->container = container;
283
284     return WINED3D_OK;
285 }
286
287 HRESULT WINAPI IWineD3DVolumeImpl_LoadTexture(IWineD3DVolume *iface, GLenum gl_level) {
288     IWineD3DVolumeImpl *This     = (IWineD3DVolumeImpl *)iface;
289     IWineD3DDeviceImpl  *myDevice = This->resource.wineD3DDevice;
290
291     TRACE("Calling glTexImage3D %x level=%d, intfmt=%x, w=%d, h=%d,d=%d, 0=%d, glFmt=%x, glType=%x, Mem=%p\n",
292             GL_TEXTURE_3D,
293             gl_level,
294             D3DFmt2GLIntFmt(myDevice, This->resource.format),
295             This->currentDesc.Width,
296             This->currentDesc.Height,
297             This->currentDesc.Depth,
298             0,
299             D3DFmt2GLFmt(myDevice, This->resource.format),
300             D3DFmt2GLType(myDevice, This->resource.format),
301             This->resource.allocatedMemory);
302     glTexImage3D(GL_TEXTURE_3D,
303                     gl_level,
304                     D3DFmt2GLIntFmt(myDevice, This->resource.format),
305                     This->currentDesc.Width,
306                     This->currentDesc.Height,
307                     This->currentDesc.Depth,
308                     0,
309                     D3DFmt2GLFmt(myDevice, This->resource.format),
310                     D3DFmt2GLType(myDevice, This->resource.format),
311                     This->resource.allocatedMemory);
312     checkGLcall("glTexImage3D");
313     return WINED3D_OK;
314
315 }
316
317 const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl =
318 {
319     /* IUnknown */
320     IWineD3DVolumeImpl_QueryInterface,
321     IWineD3DVolumeImpl_AddRef,
322     IWineD3DVolumeImpl_Release,
323     /* IWineD3DResource */
324     IWineD3DVolumeImpl_GetParent,
325     IWineD3DVolumeImpl_GetDevice,
326     IWineD3DVolumeImpl_SetPrivateData,
327     IWineD3DVolumeImpl_GetPrivateData,
328     IWineD3DVolumeImpl_FreePrivateData,
329     IWineD3DVolumeImpl_SetPriority,
330     IWineD3DVolumeImpl_GetPriority,
331     IWineD3DVolumeImpl_PreLoad,
332     IWineD3DVolumeImpl_GetType,
333     /* IWineD3DVolume */
334     IWineD3DVolumeImpl_GetContainerParent,
335     IWineD3DVolumeImpl_GetContainer,
336     IWineD3DVolumeImpl_GetDesc,
337     IWineD3DVolumeImpl_LockBox,
338     IWineD3DVolumeImpl_UnlockBox,
339     /* Internal interface */
340     IWineD3DVolumeImpl_AddDirtyBox,
341     IWineD3DVolumeImpl_CleanDirtyBox,
342     IWineD3DVolumeImpl_LoadTexture,
343     IWineD3DVolumeImpl_SetContainer
344 };