wined3d: Fix the relation between volumes and their container.
[wine/multimedia.git] / dlls / wined3d / volume.c
blob151ade3a89d1c47218b8b970bcfff2c541c59b5a
1 /*
2 * IWineD3DVolume implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
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.
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.
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
23 #include "config.h"
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->wineD3DDevice)->wineD3D))->gl_info
29 /* *******************************************
30 IWineD3DVolume IUnknown parts follow
31 ******************************************* */
32 HRESULT WINAPI IWineD3DVolumeImpl_QueryInterface(IWineD3DVolume *iface, REFIID riid, LPVOID *ppobj)
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;
43 return E_NOINTERFACE;
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);
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);
61 return ref;
64 /* ****************************************************
65 IWineD3DVolume IWineD3DResource parts follow
66 **************************************************** */
67 HRESULT WINAPI IWineD3DVolumeImpl_GetParent(IWineD3DVolume *iface, IUnknown **pParent) {
68 return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
71 HRESULT WINAPI IWineD3DVolumeImpl_GetDevice(IWineD3DVolume *iface, IWineD3DDevice** ppDevice) {
72 return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
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);
79 HRESULT WINAPI IWineD3DVolumeImpl_GetPrivateData(IWineD3DVolume *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
80 return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
83 HRESULT WINAPI IWineD3DVolumeImpl_FreePrivateData(IWineD3DVolume *iface, REFGUID refguid) {
84 return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
87 DWORD WINAPI IWineD3DVolumeImpl_SetPriority(IWineD3DVolume *iface, DWORD PriorityNew) {
88 return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
91 DWORD WINAPI IWineD3DVolumeImpl_GetPriority(IWineD3DVolume *iface) {
92 return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
95 void WINAPI IWineD3DVolumeImpl_PreLoad(IWineD3DVolume *iface) {
96 return IWineD3DResourceImpl_PreLoad((IWineD3DResource *)iface);
99 D3DRESOURCETYPE WINAPI IWineD3DVolumeImpl_GetType(IWineD3DVolume *iface) {
100 return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
103 /* *******************************************
104 IWineD3DVolume parts follow
105 ******************************************* */
106 HRESULT WINAPI IWineD3DVolumeImpl_GetContainerParent(IWineD3DVolume *iface, IUnknown **ppContainerParent) {
107 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
109 TRACE("(%p) : ppContainerParent %p\n", This, ppContainerParent);
111 if (!ppContainerParent) {
112 ERR("(%p) : Called without a valid ppContainerParent\n", This);
115 if (This->container) {
116 IWineD3DBase_GetParent(This->container, ppContainerParent);
117 if (!ppContainerParent) {
118 /* WineD3D objects should always have a parent */
119 ERR("(%p) : GetParent returned NULL\n", This);
121 IUnknown_Release(*ppContainerParent); /* GetParent adds a reference; we want just the pointer */
122 } else {
123 *ppContainerParent = NULL;
126 return D3D_OK;
129 HRESULT WINAPI IWineD3DVolumeImpl_GetContainer(IWineD3DVolume *iface, REFIID riid, void** ppContainer) {
130 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
132 TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
134 if (!ppContainer) {
135 ERR("Called without a valid ppContainer.\n");
138 /* Although surfaces can be standalone, volumes can't */
139 if (!This->container) {
140 ERR("Volume without an container. Should not happen.\n");
143 TRACE("Relaying to QueryInterface\n");
144 return IUnknown_QueryInterface(This->container, riid, ppContainer);
147 HRESULT WINAPI IWineD3DVolumeImpl_GetDesc(IWineD3DVolume *iface, WINED3DVOLUME_DESC* pDesc) {
148 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
149 TRACE("(%p) : copying into %p\n", This, pDesc);
151 *(pDesc->Format) = This->resource.format;
152 *(pDesc->Type) = This->resource.resourceType;
153 *(pDesc->Usage) = This->resource.usage;
154 *(pDesc->Pool) = This->resource.pool;
155 *(pDesc->Size) = This->resource.size; /* dx8 only */
156 *(pDesc->Width) = This->currentDesc.Width;
157 *(pDesc->Height) = This->currentDesc.Height;
158 *(pDesc->Depth) = This->currentDesc.Depth;
159 return D3D_OK;
162 HRESULT WINAPI IWineD3DVolumeImpl_LockBox(IWineD3DVolume *iface, D3DLOCKED_BOX* pLockedVolume, CONST D3DBOX* pBox, DWORD Flags) {
163 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
164 FIXME("(%p) : pBox=%p stub\n", This, pBox);
166 /* fixme: should we really lock as such? */
167 TRACE("(%p) : box=%p, output pbox=%p, allMem=%p\n", This, pBox, pLockedVolume, This->resource.allocatedMemory);
169 pLockedVolume->RowPitch = This->bytesPerPixel * This->currentDesc.Width; /* Bytes / row */
170 pLockedVolume->SlicePitch = This->bytesPerPixel * This->currentDesc.Width * This->currentDesc.Height; /* Bytes / slice */
171 if (!pBox) {
172 TRACE("No box supplied - all is ok\n");
173 pLockedVolume->pBits = This->resource.allocatedMemory;
174 This->lockedBox.Left = 0;
175 This->lockedBox.Top = 0;
176 This->lockedBox.Front = 0;
177 This->lockedBox.Right = This->currentDesc.Width;
178 This->lockedBox.Bottom = This->currentDesc.Height;
179 This->lockedBox.Back = This->currentDesc.Depth;
180 } else {
181 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);
182 pLockedVolume->pBits = This->resource.allocatedMemory +
183 (pLockedVolume->SlicePitch * pBox->Front) + /* FIXME: is front < back or vica versa? */
184 (pLockedVolume->RowPitch * pBox->Top) +
185 (pBox->Left * This->bytesPerPixel);
186 This->lockedBox.Left = pBox->Left;
187 This->lockedBox.Top = pBox->Top;
188 This->lockedBox.Front = pBox->Front;
189 This->lockedBox.Right = pBox->Right;
190 This->lockedBox.Bottom = pBox->Bottom;
191 This->lockedBox.Back = pBox->Back;
194 if (Flags & (D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_READONLY)) {
195 /* Don't dirtify */
196 } else {
198 * Dirtify on lock
199 * as seen in msdn docs
201 IWineD3DVolume_AddDirtyBox(iface, &This->lockedBox);
203 /** Dirtify Container if needed */
204 if (NULL != This->container) {
206 IWineD3DVolumeTexture *cont = (IWineD3DVolumeTexture*) This->container;
207 D3DRESOURCETYPE containerType = IWineD3DBaseTexture_GetType((IWineD3DBaseTexture *) cont);
209 if (containerType == D3DRTYPE_VOLUMETEXTURE) {
210 IWineD3DBaseTextureImpl* pTexture = (IWineD3DBaseTextureImpl*) cont;
211 pTexture->baseTexture.dirty = TRUE;
212 } else {
213 FIXME("Set dirty on container type %d\n", containerType);
218 This->locked = TRUE;
219 TRACE("returning memory@%p rpitch(%d) spitch(%d)\n", pLockedVolume->pBits, pLockedVolume->RowPitch, pLockedVolume->SlicePitch);
220 return D3D_OK;
223 HRESULT WINAPI IWineD3DVolumeImpl_UnlockBox(IWineD3DVolume *iface) {
224 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
225 if (FALSE == This->locked) {
226 ERR("trying to lock unlocked volume@%p\n", This);
227 return D3DERR_INVALIDCALL;
229 TRACE("(%p) : unlocking volume\n", This);
230 This->locked = FALSE;
231 memset(&This->lockedBox, 0, sizeof(RECT));
232 return D3D_OK;
235 /* Internal use functions follow : */
237 HRESULT WINAPI IWineD3DVolumeImpl_CleanDirtyBox(IWineD3DVolume *iface) {
238 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
239 This->dirty = FALSE;
240 This->lockedBox.Left = This->currentDesc.Width;
241 This->lockedBox.Top = This->currentDesc.Height;
242 This->lockedBox.Front = This->currentDesc.Depth;
243 This->lockedBox.Right = 0;
244 This->lockedBox.Bottom = 0;
245 This->lockedBox.Back = 0;
246 return D3D_OK;
249 HRESULT WINAPI IWineD3DVolumeImpl_AddDirtyBox(IWineD3DVolume *iface, CONST D3DBOX* pDirtyBox) {
250 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
251 This->dirty = TRUE;
252 if (NULL != pDirtyBox) {
253 This->lockedBox.Left = min(This->lockedBox.Left, pDirtyBox->Left);
254 This->lockedBox.Top = min(This->lockedBox.Top, pDirtyBox->Top);
255 This->lockedBox.Front = min(This->lockedBox.Front, pDirtyBox->Front);
256 This->lockedBox.Right = max(This->lockedBox.Right, pDirtyBox->Right);
257 This->lockedBox.Bottom = max(This->lockedBox.Bottom, pDirtyBox->Bottom);
258 This->lockedBox.Back = max(This->lockedBox.Back, pDirtyBox->Back);
259 } else {
260 This->lockedBox.Left = 0;
261 This->lockedBox.Top = 0;
262 This->lockedBox.Front = 0;
263 This->lockedBox.Right = This->currentDesc.Width;
264 This->lockedBox.Bottom = This->currentDesc.Height;
265 This->lockedBox.Back = This->currentDesc.Depth;
267 return D3D_OK;
270 HRESULT WINAPI IWineD3DVolumeImpl_SetContainer(IWineD3DVolume *iface, IWineD3DBase* container) {
271 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
273 TRACE("This %p, container %p\n", This, container);
275 /* We can't keep a reference to the container, since the container already keeps a reference to us. */
277 TRACE("Setting container to %p from %p\n", container, This->container);
278 This->container = container;
280 return D3D_OK;
283 HRESULT WINAPI IWineD3DVolumeImpl_LoadTexture(IWineD3DVolume *iface, GLenum gl_level) {
284 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
285 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
287 TRACE("Calling glTexImage3D %x level=%d, intfmt=%x, w=%d, h=%d,d=%d, 0=%d, glFmt=%x, glType=%x, Mem=%p\n",
288 GL_TEXTURE_3D,
289 gl_level,
290 D3DFmt2GLIntFmt(myDevice, This->currentDesc.Format),
291 This->currentDesc.Width,
292 This->currentDesc.Height,
293 This->currentDesc.Depth,
295 D3DFmt2GLFmt(myDevice, This->currentDesc.Format),
296 D3DFmt2GLType(myDevice, This->currentDesc.Format),
297 This->resource.allocatedMemory);
298 glTexImage3D(GL_TEXTURE_3D,
299 gl_level,
300 D3DFmt2GLIntFmt(myDevice, This->currentDesc.Format),
301 This->currentDesc.Width,
302 This->currentDesc.Height,
303 This->currentDesc.Depth,
305 D3DFmt2GLFmt(myDevice, This->currentDesc.Format),
306 D3DFmt2GLType(myDevice, This->currentDesc.Format),
307 This->resource.allocatedMemory);
308 checkGLcall("glTexImage3D");
309 return D3D_OK;
313 const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl =
315 /* IUnknown */
316 IWineD3DVolumeImpl_QueryInterface,
317 IWineD3DVolumeImpl_AddRef,
318 IWineD3DVolumeImpl_Release,
319 /* IWineD3DResource */
320 IWineD3DVolumeImpl_GetParent,
321 IWineD3DVolumeImpl_GetDevice,
322 IWineD3DVolumeImpl_SetPrivateData,
323 IWineD3DVolumeImpl_GetPrivateData,
324 IWineD3DVolumeImpl_FreePrivateData,
325 IWineD3DVolumeImpl_SetPriority,
326 IWineD3DVolumeImpl_GetPriority,
327 IWineD3DVolumeImpl_PreLoad,
328 IWineD3DVolumeImpl_GetType,
329 /* IWineD3DVolume */
330 IWineD3DVolumeImpl_GetContainerParent,
331 IWineD3DVolumeImpl_GetContainer,
332 IWineD3DVolumeImpl_GetDesc,
333 IWineD3DVolumeImpl_LockBox,
334 IWineD3DVolumeImpl_UnlockBox,
335 /* Internal interface */
336 IWineD3DVolumeImpl_AddDirtyBox,
337 IWineD3DVolumeImpl_CleanDirtyBox,
338 IWineD3DVolumeImpl_LoadTexture,
339 IWineD3DVolumeImpl_SetContainer