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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
27 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
29 static void volume_bind_and_dirtify(IWineD3DVolume *iface) {
30 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
31 IWineD3DVolumeTexture *texture;
34 /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
35 * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
36 * gl states. The current texture unit should always be a valid one.
38 * To be more specific, this is tricky because we can implicitly be called
39 * from sampler() in state.c. This means we can't touch anything other than
40 * whatever happens to be the currently active texture, or we would risk
41 * marking already applied sampler states dirty again.
43 * TODO: Track the current active texture per GL context instead of using glGet
45 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
48 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
50 active_sampler = This->resource.wineD3DDevice->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
55 if (active_sampler != -1) {
56 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(active_sampler));
59 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DVolumeTexture, (void **)&texture))) {
60 IWineD3DVolumeTexture_BindTexture(texture);
61 IWineD3DVolumeTexture_Release(texture);
63 ERR("Volume should be part of a volume texture\n");
67 /* *******************************************
68 IWineD3DVolume IUnknown parts follow
69 ******************************************* */
70 static HRESULT WINAPI IWineD3DVolumeImpl_QueryInterface(IWineD3DVolume *iface, REFIID riid, LPVOID *ppobj)
72 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
73 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
74 if (IsEqualGUID(riid, &IID_IUnknown)
75 || IsEqualGUID(riid, &IID_IWineD3DBase)
76 || IsEqualGUID(riid, &IID_IWineD3DVolume)){
77 IUnknown_AddRef(iface);
85 static ULONG WINAPI IWineD3DVolumeImpl_AddRef(IWineD3DVolume *iface) {
86 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
87 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
88 return InterlockedIncrement(&This->resource.ref);
91 static ULONG WINAPI IWineD3DVolumeImpl_Release(IWineD3DVolume *iface) {
92 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
94 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
95 ref = InterlockedDecrement(&This->resource.ref);
97 IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
98 HeapFree(GetProcessHeap(), 0, This);
103 /* ****************************************************
104 IWineD3DVolume IWineD3DResource parts follow
105 **************************************************** */
106 static HRESULT WINAPI IWineD3DVolumeImpl_GetParent(IWineD3DVolume *iface, IUnknown **pParent) {
107 return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
110 static HRESULT WINAPI IWineD3DVolumeImpl_GetDevice(IWineD3DVolume *iface, IWineD3DDevice** ppDevice) {
111 return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
114 static HRESULT WINAPI IWineD3DVolumeImpl_SetPrivateData(IWineD3DVolume *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
115 return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
118 static HRESULT WINAPI IWineD3DVolumeImpl_GetPrivateData(IWineD3DVolume *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
119 return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
122 static HRESULT WINAPI IWineD3DVolumeImpl_FreePrivateData(IWineD3DVolume *iface, REFGUID refguid) {
123 return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
126 static DWORD WINAPI IWineD3DVolumeImpl_SetPriority(IWineD3DVolume *iface, DWORD PriorityNew) {
127 return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
130 static DWORD WINAPI IWineD3DVolumeImpl_GetPriority(IWineD3DVolume *iface) {
131 return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
134 static void WINAPI IWineD3DVolumeImpl_PreLoad(IWineD3DVolume *iface) {
135 IWineD3DResourceImpl_PreLoad((IWineD3DResource *)iface);
138 static void WINAPI IWineD3DVolumeImpl_UnLoad(IWineD3DVolume *iface) {
139 /* The whole content is shadowed on This->resource.allocatedMemory, and the
140 * texture name is managed by the VolumeTexture container
142 TRACE("(%p): Nothing to do\n", iface);
145 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeImpl_GetType(IWineD3DVolume *iface) {
146 return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
149 /* *******************************************
150 IWineD3DVolume parts follow
151 ******************************************* */
152 static HRESULT WINAPI IWineD3DVolumeImpl_GetContainer(IWineD3DVolume *iface, REFIID riid, void** ppContainer) {
153 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
155 TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
158 ERR("Called without a valid ppContainer.\n");
161 /* Although surfaces can be standalone, volumes can't */
162 if (!This->container) {
163 ERR("Volume without an container. Should not happen.\n");
166 TRACE("Relaying to QueryInterface\n");
167 return IUnknown_QueryInterface(This->container, riid, ppContainer);
170 static HRESULT WINAPI IWineD3DVolumeImpl_GetDesc(IWineD3DVolume *iface, WINED3DVOLUME_DESC* pDesc) {
171 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
172 TRACE("(%p) : copying into %p\n", This, pDesc);
174 *(pDesc->Format) = This->resource.format;
175 *(pDesc->Type) = This->resource.resourceType;
176 *(pDesc->Usage) = This->resource.usage;
177 *(pDesc->Pool) = This->resource.pool;
178 *(pDesc->Size) = This->resource.size; /* dx8 only */
179 *(pDesc->Width) = This->currentDesc.Width;
180 *(pDesc->Height) = This->currentDesc.Height;
181 *(pDesc->Depth) = This->currentDesc.Depth;
185 static HRESULT WINAPI IWineD3DVolumeImpl_LockBox(IWineD3DVolume *iface, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
186 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
187 FIXME("(%p) : pBox=%p stub\n", This, pBox);
189 if(!This->resource.allocatedMemory) {
190 This->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size);
193 /* fixme: should we really lock as such? */
194 TRACE("(%p) : box=%p, output pbox=%p, allMem=%p\n", This, pBox, pLockedVolume, This->resource.allocatedMemory);
196 pLockedVolume->RowPitch = This->bytesPerPixel * This->currentDesc.Width; /* Bytes / row */
197 pLockedVolume->SlicePitch = This->bytesPerPixel * This->currentDesc.Width * This->currentDesc.Height; /* Bytes / slice */
199 TRACE("No box supplied - all is ok\n");
200 pLockedVolume->pBits = This->resource.allocatedMemory;
201 This->lockedBox.Left = 0;
202 This->lockedBox.Top = 0;
203 This->lockedBox.Front = 0;
204 This->lockedBox.Right = This->currentDesc.Width;
205 This->lockedBox.Bottom = This->currentDesc.Height;
206 This->lockedBox.Back = This->currentDesc.Depth;
208 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);
209 pLockedVolume->pBits = This->resource.allocatedMemory +
210 (pLockedVolume->SlicePitch * pBox->Front) + /* FIXME: is front < back or vica versa? */
211 (pLockedVolume->RowPitch * pBox->Top) +
212 (pBox->Left * This->bytesPerPixel);
213 This->lockedBox.Left = pBox->Left;
214 This->lockedBox.Top = pBox->Top;
215 This->lockedBox.Front = pBox->Front;
216 This->lockedBox.Right = pBox->Right;
217 This->lockedBox.Bottom = pBox->Bottom;
218 This->lockedBox.Back = pBox->Back;
221 if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
226 * as seen in msdn docs
228 IWineD3DVolume_AddDirtyBox(iface, &This->lockedBox);
230 /** Dirtify Container if needed */
231 if (NULL != This->container) {
233 IWineD3DVolumeTexture *cont = (IWineD3DVolumeTexture*) This->container;
234 WINED3DRESOURCETYPE containerType = IWineD3DBaseTexture_GetType((IWineD3DBaseTexture *) cont);
236 if (containerType == WINED3DRTYPE_VOLUMETEXTURE) {
237 IWineD3DBaseTextureImpl* pTexture = (IWineD3DBaseTextureImpl*) cont;
238 pTexture->baseTexture.dirty = TRUE;
240 FIXME("Set dirty on container type %d\n", containerType);
246 TRACE("returning memory@%p rpitch(%d) spitch(%d)\n", pLockedVolume->pBits, pLockedVolume->RowPitch, pLockedVolume->SlicePitch);
250 static HRESULT WINAPI IWineD3DVolumeImpl_UnlockBox(IWineD3DVolume *iface) {
251 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
253 ERR("trying to lock unlocked volume@%p\n", This);
254 return WINED3DERR_INVALIDCALL;
256 TRACE("(%p) : unlocking volume\n", This);
257 This->locked = FALSE;
258 memset(&This->lockedBox, 0, sizeof(RECT));
262 /* Internal use functions follow : */
264 static HRESULT WINAPI IWineD3DVolumeImpl_CleanDirtyBox(IWineD3DVolume *iface) {
265 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
267 This->lockedBox.Left = This->currentDesc.Width;
268 This->lockedBox.Top = This->currentDesc.Height;
269 This->lockedBox.Front = This->currentDesc.Depth;
270 This->lockedBox.Right = 0;
271 This->lockedBox.Bottom = 0;
272 This->lockedBox.Back = 0;
276 static HRESULT WINAPI IWineD3DVolumeImpl_AddDirtyBox(IWineD3DVolume *iface, CONST WINED3DBOX* pDirtyBox) {
277 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
279 if (NULL != pDirtyBox) {
280 This->lockedBox.Left = min(This->lockedBox.Left, pDirtyBox->Left);
281 This->lockedBox.Top = min(This->lockedBox.Top, pDirtyBox->Top);
282 This->lockedBox.Front = min(This->lockedBox.Front, pDirtyBox->Front);
283 This->lockedBox.Right = max(This->lockedBox.Right, pDirtyBox->Right);
284 This->lockedBox.Bottom = max(This->lockedBox.Bottom, pDirtyBox->Bottom);
285 This->lockedBox.Back = max(This->lockedBox.Back, pDirtyBox->Back);
287 This->lockedBox.Left = 0;
288 This->lockedBox.Top = 0;
289 This->lockedBox.Front = 0;
290 This->lockedBox.Right = This->currentDesc.Width;
291 This->lockedBox.Bottom = This->currentDesc.Height;
292 This->lockedBox.Back = This->currentDesc.Depth;
297 static HRESULT WINAPI IWineD3DVolumeImpl_SetContainer(IWineD3DVolume *iface, IWineD3DBase* container) {
298 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
300 TRACE("This %p, container %p\n", This, container);
302 /* We can't keep a reference to the container, since the container already keeps a reference to us. */
304 TRACE("Setting container to %p from %p\n", container, This->container);
305 This->container = container;
310 static HRESULT WINAPI IWineD3DVolumeImpl_LoadTexture(IWineD3DVolume *iface, int gl_level, BOOL srgb_mode) {
311 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
312 WINED3DFORMAT format = This->resource.format;
313 const GlPixelFormatDesc *glDesc;
314 getFormatDescEntry(format, &GLINFO_LOCATION, &glDesc);
316 TRACE("(%p) : level %u, format %s (0x%08x)\n", This, gl_level, debug_d3dformat(format), format);
318 volume_bind_and_dirtify(iface);
320 TRACE("Calling glTexImage3D %x level=%d, intfmt=%x, w=%d, h=%d,d=%d, 0=%d, glFmt=%x, glType=%x, Mem=%p\n",
324 This->currentDesc.Width,
325 This->currentDesc.Height,
326 This->currentDesc.Depth,
330 This->resource.allocatedMemory);
331 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D,
334 This->currentDesc.Width,
335 This->currentDesc.Height,
336 This->currentDesc.Depth,
340 This->resource.allocatedMemory));
341 checkGLcall("glTexImage3D");
343 /* When adding code releasing This->resource.allocatedMemory to save data keep in mind that
344 * GL_UNPACK_CLIENT_STORAGE_APPLE is enabled by default if supported(GL_APPLE_client_storage).
345 * Thus do not release This->resource.allocatedMemory if GL_APPLE_client_storage is supported.
351 const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl =
354 IWineD3DVolumeImpl_QueryInterface,
355 IWineD3DVolumeImpl_AddRef,
356 IWineD3DVolumeImpl_Release,
357 /* IWineD3DResource */
358 IWineD3DVolumeImpl_GetParent,
359 IWineD3DVolumeImpl_GetDevice,
360 IWineD3DVolumeImpl_SetPrivateData,
361 IWineD3DVolumeImpl_GetPrivateData,
362 IWineD3DVolumeImpl_FreePrivateData,
363 IWineD3DVolumeImpl_SetPriority,
364 IWineD3DVolumeImpl_GetPriority,
365 IWineD3DVolumeImpl_PreLoad,
366 IWineD3DVolumeImpl_UnLoad,
367 IWineD3DVolumeImpl_GetType,
369 IWineD3DVolumeImpl_GetContainer,
370 IWineD3DVolumeImpl_GetDesc,
371 IWineD3DVolumeImpl_LockBox,
372 IWineD3DVolumeImpl_UnlockBox,
373 /* Internal interface */
374 IWineD3DVolumeImpl_AddDirtyBox,
375 IWineD3DVolumeImpl_CleanDirtyBox,
376 IWineD3DVolumeImpl_LoadTexture,
377 IWineD3DVolumeImpl_SetContainer