wined3d: Do not activate vertex shaders needlessly.
[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->resource.wineD3DDevice)->wineD3D))->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     return IWineD3DResourceImpl_PreLoad((IWineD3DResource *)iface);
98 }
99
100 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeImpl_GetType(IWineD3DVolume *iface) {
101     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
102 }
103
104 /* *******************************************
105    IWineD3DVolume parts follow
106    ******************************************* */
107 static HRESULT WINAPI IWineD3DVolumeImpl_GetContainer(IWineD3DVolume *iface, REFIID riid, void** ppContainer) {
108     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
109
110     TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
111
112     if (!ppContainer) {
113         ERR("Called without a valid ppContainer.\n");
114     }
115
116     /* Although surfaces can be standalone, volumes can't */
117     if (!This->container) {
118         ERR("Volume without an container. Should not happen.\n");
119     }
120
121     TRACE("Relaying to QueryInterface\n");
122     return IUnknown_QueryInterface(This->container, riid, ppContainer);
123 }
124
125 static HRESULT WINAPI IWineD3DVolumeImpl_GetDesc(IWineD3DVolume *iface, WINED3DVOLUME_DESC* pDesc) {
126     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
127     TRACE("(%p) : copying into %p\n", This, pDesc);
128
129     *(pDesc->Format)  = This->resource.format;
130     *(pDesc->Type)    = This->resource.resourceType;
131     *(pDesc->Usage)   = This->resource.usage;
132     *(pDesc->Pool)    = This->resource.pool;
133     *(pDesc->Size)    = This->resource.size; /* dx8 only */
134     *(pDesc->Width)   = This->currentDesc.Width;
135     *(pDesc->Height)  = This->currentDesc.Height;
136     *(pDesc->Depth)   = This->currentDesc.Depth;
137     return WINED3D_OK;
138 }
139
140 static HRESULT WINAPI IWineD3DVolumeImpl_LockBox(IWineD3DVolume *iface, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
141     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
142     FIXME("(%p) : pBox=%p stub\n", This, pBox);
143
144     /* fixme: should we really lock as such? */
145     TRACE("(%p) : box=%p, output pbox=%p, allMem=%p\n", This, pBox, pLockedVolume, This->resource.allocatedMemory);
146
147     pLockedVolume->RowPitch   = This->bytesPerPixel * This->currentDesc.Width;                        /* Bytes / row   */
148     pLockedVolume->SlicePitch = This->bytesPerPixel * This->currentDesc.Width * This->currentDesc.Height;  /* Bytes / slice */
149     if (!pBox) {
150         TRACE("No box supplied - all is ok\n");
151         pLockedVolume->pBits = This->resource.allocatedMemory;
152         This->lockedBox.Left   = 0;
153         This->lockedBox.Top    = 0;
154         This->lockedBox.Front  = 0;
155         This->lockedBox.Right  = This->currentDesc.Width;
156         This->lockedBox.Bottom = This->currentDesc.Height;
157         This->lockedBox.Back   = This->currentDesc.Depth;
158     } else {
159         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);
160         pLockedVolume->pBits = This->resource.allocatedMemory +
161           (pLockedVolume->SlicePitch * pBox->Front) + /* FIXME: is front < back or vica versa? */
162           (pLockedVolume->RowPitch * pBox->Top) +
163           (pBox->Left * This->bytesPerPixel);
164         This->lockedBox.Left   = pBox->Left;
165         This->lockedBox.Top    = pBox->Top;
166         This->lockedBox.Front  = pBox->Front;
167         This->lockedBox.Right  = pBox->Right;
168         This->lockedBox.Bottom = pBox->Bottom;
169         This->lockedBox.Back   = pBox->Back;
170     }
171
172     if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
173       /* Don't dirtify */
174     } else {
175       /**
176        * Dirtify on lock
177        * as seen in msdn docs
178        */
179       IWineD3DVolume_AddDirtyBox(iface, &This->lockedBox);
180
181       /**  Dirtify Container if needed */
182       if (NULL != This->container) {
183
184         IWineD3DVolumeTexture *cont = (IWineD3DVolumeTexture*) This->container;
185         WINED3DRESOURCETYPE containerType = IWineD3DBaseTexture_GetType((IWineD3DBaseTexture *) cont);
186
187         if (containerType == WINED3DRTYPE_VOLUMETEXTURE) {
188           IWineD3DBaseTextureImpl* pTexture = (IWineD3DBaseTextureImpl*) cont;
189           pTexture->baseTexture.dirty = TRUE;
190         } else {
191           FIXME("Set dirty on container type %d\n", containerType);
192         }
193       }
194     }
195
196     This->locked = TRUE;
197     TRACE("returning memory@%p rpitch(%d) spitch(%d)\n", pLockedVolume->pBits, pLockedVolume->RowPitch, pLockedVolume->SlicePitch);
198     return WINED3D_OK;
199 }
200
201 static HRESULT WINAPI IWineD3DVolumeImpl_UnlockBox(IWineD3DVolume *iface) {
202     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
203     if (!This->locked) {
204       ERR("trying to lock unlocked volume@%p\n", This);
205       return WINED3DERR_INVALIDCALL;
206     }
207     TRACE("(%p) : unlocking volume\n", This);
208     This->locked = FALSE;
209     memset(&This->lockedBox, 0, sizeof(RECT));
210     return WINED3D_OK;
211 }
212
213 /* Internal use functions follow : */
214
215 static HRESULT WINAPI IWineD3DVolumeImpl_CleanDirtyBox(IWineD3DVolume *iface) {
216   IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
217   This->dirty = FALSE;
218   This->lockedBox.Left   = This->currentDesc.Width;
219   This->lockedBox.Top    = This->currentDesc.Height;
220   This->lockedBox.Front  = This->currentDesc.Depth;
221   This->lockedBox.Right  = 0;
222   This->lockedBox.Bottom = 0;
223   This->lockedBox.Back   = 0;
224   return WINED3D_OK;
225 }
226
227 static HRESULT WINAPI IWineD3DVolumeImpl_AddDirtyBox(IWineD3DVolume *iface, CONST WINED3DBOX* pDirtyBox) {
228   IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
229   This->dirty = TRUE;
230    if (NULL != pDirtyBox) {
231     This->lockedBox.Left   = min(This->lockedBox.Left,   pDirtyBox->Left);
232     This->lockedBox.Top    = min(This->lockedBox.Top,    pDirtyBox->Top);
233     This->lockedBox.Front  = min(This->lockedBox.Front,  pDirtyBox->Front);
234     This->lockedBox.Right  = max(This->lockedBox.Right,  pDirtyBox->Right);
235     This->lockedBox.Bottom = max(This->lockedBox.Bottom, pDirtyBox->Bottom);
236     This->lockedBox.Back   = max(This->lockedBox.Back,   pDirtyBox->Back);
237   } else {
238     This->lockedBox.Left   = 0;
239     This->lockedBox.Top    = 0;
240     This->lockedBox.Front  = 0;
241     This->lockedBox.Right  = This->currentDesc.Width;
242     This->lockedBox.Bottom = This->currentDesc.Height;
243     This->lockedBox.Back   = This->currentDesc.Depth;
244   }
245   return WINED3D_OK;
246 }
247
248 static HRESULT WINAPI IWineD3DVolumeImpl_SetContainer(IWineD3DVolume *iface, IWineD3DBase* container) {
249     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
250
251     TRACE("This %p, container %p\n", This, container);
252
253     /* We can't keep a reference to the container, since the container already keeps a reference to us. */
254
255     TRACE("Setting container to %p from %p\n", container, This->container);
256     This->container = container;
257
258     return WINED3D_OK;
259 }
260
261 static HRESULT WINAPI IWineD3DVolumeImpl_LoadTexture(IWineD3DVolume *iface, GLenum gl_level) {
262     IWineD3DVolumeImpl *This     = (IWineD3DVolumeImpl *)iface;
263     const PixelFormatDesc *formatEntry = getFormatDescEntry(This->resource.format);
264
265     if(GL_SUPPORT(EXT_TEXTURE3D)) {
266         TRACE("Calling glTexImage3D %x level=%d, intfmt=%x, w=%d, h=%d,d=%d, 0=%d, glFmt=%x, glType=%x, Mem=%p\n",
267                 GL_TEXTURE_3D,
268                 gl_level,
269                 formatEntry->glInternal,
270                 This->currentDesc.Width,
271                 This->currentDesc.Height,
272                 This->currentDesc.Depth,
273                 0,
274                 formatEntry->glFormat,
275                 formatEntry->glType,
276                 This->resource.allocatedMemory);
277         GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D,
278                     gl_level,
279                     formatEntry->glInternal,
280                     This->currentDesc.Width,
281                     This->currentDesc.Height,
282                     This->currentDesc.Depth,
283                     0,
284                     formatEntry->glFormat,
285                     formatEntry->glType,
286                     This->resource.allocatedMemory));
287         checkGLcall("glTexImage3D");
288     } else
289         WARN("This OpenGL implementation doesn't support 3D textures\n");
290     
291     return WINED3D_OK;
292
293 }
294
295 const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl =
296 {
297     /* IUnknown */
298     IWineD3DVolumeImpl_QueryInterface,
299     IWineD3DVolumeImpl_AddRef,
300     IWineD3DVolumeImpl_Release,
301     /* IWineD3DResource */
302     IWineD3DVolumeImpl_GetParent,
303     IWineD3DVolumeImpl_GetDevice,
304     IWineD3DVolumeImpl_SetPrivateData,
305     IWineD3DVolumeImpl_GetPrivateData,
306     IWineD3DVolumeImpl_FreePrivateData,
307     IWineD3DVolumeImpl_SetPriority,
308     IWineD3DVolumeImpl_GetPriority,
309     IWineD3DVolumeImpl_PreLoad,
310     IWineD3DVolumeImpl_GetType,
311     /* IWineD3DVolume */
312     IWineD3DVolumeImpl_GetContainer,
313     IWineD3DVolumeImpl_GetDesc,
314     IWineD3DVolumeImpl_LockBox,
315     IWineD3DVolumeImpl_UnlockBox,
316     /* Internal interface */
317     IWineD3DVolumeImpl_AddDirtyBox,
318     IWineD3DVolumeImpl_CleanDirtyBox,
319     IWineD3DVolumeImpl_LoadTexture,
320     IWineD3DVolumeImpl_SetContainer
321 };