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