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 /* Context activation is done by the caller. */
30 static void volume_bind_and_dirtify(IWineD3DVolume *iface) {
31 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
32 IWineD3DVolumeTexture *texture;
35 /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
36 * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
37 * gl states. The current texture unit should always be a valid one.
39 * To be more specific, this is tricky because we can implicitly be called
40 * from sampler() in state.c. This means we can't touch anything other than
41 * whatever happens to be the currently active texture, or we would risk
42 * marking already applied sampler states dirty again.
44 * TODO: Track the current active texture per GL context instead of using glGet
46 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
49 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
51 active_sampler = This->resource.wineD3DDevice->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
56 if (active_sampler != WINED3D_UNMAPPED_STAGE)
58 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(active_sampler));
61 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DVolumeTexture, (void **)&texture))) {
62 IWineD3DVolumeTexture_BindTexture(texture, FALSE);
63 IWineD3DVolumeTexture_Release(texture);
65 ERR("Volume should be part of a volume texture\n");
69 void volume_add_dirty_box(IWineD3DVolume *iface, const WINED3DBOX *dirty_box)
71 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
76 This->lockedBox.Left = min(This->lockedBox.Left, dirty_box->Left);
77 This->lockedBox.Top = min(This->lockedBox.Top, dirty_box->Top);
78 This->lockedBox.Front = min(This->lockedBox.Front, dirty_box->Front);
79 This->lockedBox.Right = max(This->lockedBox.Right, dirty_box->Right);
80 This->lockedBox.Bottom = max(This->lockedBox.Bottom, dirty_box->Bottom);
81 This->lockedBox.Back = max(This->lockedBox.Back, dirty_box->Back);
85 This->lockedBox.Left = 0;
86 This->lockedBox.Top = 0;
87 This->lockedBox.Front = 0;
88 This->lockedBox.Right = This->currentDesc.Width;
89 This->lockedBox.Bottom = This->currentDesc.Height;
90 This->lockedBox.Back = This->currentDesc.Depth;
94 /* *******************************************
95 IWineD3DVolume IUnknown parts follow
96 ******************************************* */
97 static HRESULT WINAPI IWineD3DVolumeImpl_QueryInterface(IWineD3DVolume *iface, REFIID riid, LPVOID *ppobj)
99 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
100 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
101 if (IsEqualGUID(riid, &IID_IUnknown)
102 || IsEqualGUID(riid, &IID_IWineD3DBase)
103 || IsEqualGUID(riid, &IID_IWineD3DVolume)){
104 IUnknown_AddRef(iface);
109 return E_NOINTERFACE;
112 static ULONG WINAPI IWineD3DVolumeImpl_AddRef(IWineD3DVolume *iface) {
113 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
114 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
115 return InterlockedIncrement(&This->resource.ref);
118 static ULONG WINAPI IWineD3DVolumeImpl_Release(IWineD3DVolume *iface) {
119 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
121 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
122 ref = InterlockedDecrement(&This->resource.ref);
124 resource_cleanup((IWineD3DResource *)iface);
125 HeapFree(GetProcessHeap(), 0, This);
130 /* ****************************************************
131 IWineD3DVolume IWineD3DResource parts follow
132 **************************************************** */
133 static HRESULT WINAPI IWineD3DVolumeImpl_GetParent(IWineD3DVolume *iface, IUnknown **pParent) {
134 return resource_get_parent((IWineD3DResource *)iface, pParent);
137 static HRESULT WINAPI IWineD3DVolumeImpl_GetDevice(IWineD3DVolume *iface, IWineD3DDevice** ppDevice) {
138 return resource_get_device((IWineD3DResource *)iface, ppDevice);
141 static HRESULT WINAPI IWineD3DVolumeImpl_SetPrivateData(IWineD3DVolume *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
142 return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
145 static HRESULT WINAPI IWineD3DVolumeImpl_GetPrivateData(IWineD3DVolume *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
146 return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
149 static HRESULT WINAPI IWineD3DVolumeImpl_FreePrivateData(IWineD3DVolume *iface, REFGUID refguid) {
150 return resource_free_private_data((IWineD3DResource *)iface, refguid);
153 static DWORD WINAPI IWineD3DVolumeImpl_SetPriority(IWineD3DVolume *iface, DWORD PriorityNew) {
154 return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
157 static DWORD WINAPI IWineD3DVolumeImpl_GetPriority(IWineD3DVolume *iface) {
158 return resource_get_priority((IWineD3DResource *)iface);
161 static void WINAPI IWineD3DVolumeImpl_PreLoad(IWineD3DVolume *iface) {
162 FIXME("iface %p stub!\n", iface);
165 static void WINAPI IWineD3DVolumeImpl_UnLoad(IWineD3DVolume *iface) {
166 /* The whole content is shadowed on This->resource.allocatedMemory, and the
167 * texture name is managed by the VolumeTexture container
169 TRACE("(%p): Nothing to do\n", iface);
172 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeImpl_GetType(IWineD3DVolume *iface) {
173 return resource_get_type((IWineD3DResource *)iface);
176 /* *******************************************
177 IWineD3DVolume parts follow
178 ******************************************* */
179 static HRESULT WINAPI IWineD3DVolumeImpl_GetContainer(IWineD3DVolume *iface, REFIID riid, void** ppContainer) {
180 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
182 TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
185 ERR("Called without a valid ppContainer.\n");
188 /* Although surfaces can be standalone, volumes can't */
189 if (!This->container) {
190 ERR("Volume without an container. Should not happen.\n");
193 TRACE("Relaying to QueryInterface\n");
194 return IUnknown_QueryInterface(This->container, riid, ppContainer);
197 static HRESULT WINAPI IWineD3DVolumeImpl_GetDesc(IWineD3DVolume *iface, WINED3DVOLUME_DESC* pDesc) {
198 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
199 TRACE("(%p) : copying into %p\n", This, pDesc);
201 pDesc->Format = This->resource.format_desc->format;
202 pDesc->Type = This->resource.resourceType;
203 pDesc->Usage = This->resource.usage;
204 pDesc->Pool = This->resource.pool;
205 pDesc->Size = This->resource.size; /* dx8 only */
206 pDesc->Width = This->currentDesc.Width;
207 pDesc->Height = This->currentDesc.Height;
208 pDesc->Depth = This->currentDesc.Depth;
213 static HRESULT WINAPI IWineD3DVolumeImpl_LockBox(IWineD3DVolume *iface, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
214 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
215 FIXME("(%p) : pBox=%p stub\n", This, pBox);
217 if(!This->resource.allocatedMemory) {
218 This->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size);
221 /* fixme: should we really lock as such? */
222 TRACE("(%p) : box=%p, output pbox=%p, allMem=%p\n", This, pBox, pLockedVolume, This->resource.allocatedMemory);
224 pLockedVolume->RowPitch = This->resource.format_desc->byte_count * This->currentDesc.Width; /* Bytes / row */
225 pLockedVolume->SlicePitch = This->resource.format_desc->byte_count
226 * This->currentDesc.Width * This->currentDesc.Height; /* Bytes / slice */
228 TRACE("No box supplied - all is ok\n");
229 pLockedVolume->pBits = This->resource.allocatedMemory;
230 This->lockedBox.Left = 0;
231 This->lockedBox.Top = 0;
232 This->lockedBox.Front = 0;
233 This->lockedBox.Right = This->currentDesc.Width;
234 This->lockedBox.Bottom = This->currentDesc.Height;
235 This->lockedBox.Back = This->currentDesc.Depth;
237 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);
238 pLockedVolume->pBits = This->resource.allocatedMemory
239 + (pLockedVolume->SlicePitch * pBox->Front) /* FIXME: is front < back or vica versa? */
240 + (pLockedVolume->RowPitch * pBox->Top)
241 + (pBox->Left * This->resource.format_desc->byte_count);
242 This->lockedBox.Left = pBox->Left;
243 This->lockedBox.Top = pBox->Top;
244 This->lockedBox.Front = pBox->Front;
245 This->lockedBox.Right = pBox->Right;
246 This->lockedBox.Bottom = pBox->Bottom;
247 This->lockedBox.Back = pBox->Back;
250 if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
255 * as seen in msdn docs
257 volume_add_dirty_box(iface, &This->lockedBox);
259 /** Dirtify Container if needed */
260 if (NULL != This->container) {
262 IWineD3DVolumeTexture *cont = (IWineD3DVolumeTexture*) This->container;
263 WINED3DRESOURCETYPE containerType = IWineD3DBaseTexture_GetType((IWineD3DBaseTexture *) cont);
265 if (containerType == WINED3DRTYPE_VOLUMETEXTURE) {
266 IWineD3DBaseTextureImpl* pTexture = (IWineD3DBaseTextureImpl*) cont;
267 pTexture->baseTexture.dirty = TRUE;
269 FIXME("Set dirty on container type %d\n", containerType);
275 TRACE("returning memory@%p rpitch(%d) spitch(%d)\n", pLockedVolume->pBits, pLockedVolume->RowPitch, pLockedVolume->SlicePitch);
279 static HRESULT WINAPI IWineD3DVolumeImpl_UnlockBox(IWineD3DVolume *iface) {
280 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
282 ERR("trying to lock unlocked volume@%p\n", This);
283 return WINED3DERR_INVALIDCALL;
285 TRACE("(%p) : unlocking volume\n", This);
286 This->locked = FALSE;
287 memset(&This->lockedBox, 0, sizeof(RECT));
291 /* Internal use functions follow : */
293 static HRESULT WINAPI IWineD3DVolumeImpl_SetContainer(IWineD3DVolume *iface, IWineD3DBase* container) {
294 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
296 TRACE("This %p, container %p\n", This, container);
298 /* We can't keep a reference to the container, since the container already keeps a reference to us. */
300 TRACE("Setting container to %p from %p\n", container, This->container);
301 This->container = container;
306 /* Context activation is done by the caller. */
307 static HRESULT WINAPI IWineD3DVolumeImpl_LoadTexture(IWineD3DVolume *iface, int gl_level, BOOL srgb_mode) {
308 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
309 const struct GlPixelFormatDesc *glDesc = This->resource.format_desc;
311 TRACE("(%p) : level %u, format %s (0x%08x)\n", This, gl_level, debug_d3dformat(glDesc->format), glDesc->format);
313 volume_bind_and_dirtify(iface);
315 TRACE("Calling glTexImage3D %x level=%d, intfmt=%x, w=%d, h=%d,d=%d, 0=%d, glFmt=%x, glType=%x, Mem=%p\n",
319 This->currentDesc.Width,
320 This->currentDesc.Height,
321 This->currentDesc.Depth,
325 This->resource.allocatedMemory);
328 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D,
331 This->currentDesc.Width,
332 This->currentDesc.Height,
333 This->currentDesc.Depth,
337 This->resource.allocatedMemory));
338 checkGLcall("glTexImage3D");
341 /* When adding code releasing This->resource.allocatedMemory to save data keep in mind that
342 * GL_UNPACK_CLIENT_STORAGE_APPLE is enabled by default if supported(GL_APPLE_client_storage).
343 * Thus do not release This->resource.allocatedMemory if GL_APPLE_client_storage is supported.
349 const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl =
352 IWineD3DVolumeImpl_QueryInterface,
353 IWineD3DVolumeImpl_AddRef,
354 IWineD3DVolumeImpl_Release,
355 /* IWineD3DResource */
356 IWineD3DVolumeImpl_GetParent,
357 IWineD3DVolumeImpl_GetDevice,
358 IWineD3DVolumeImpl_SetPrivateData,
359 IWineD3DVolumeImpl_GetPrivateData,
360 IWineD3DVolumeImpl_FreePrivateData,
361 IWineD3DVolumeImpl_SetPriority,
362 IWineD3DVolumeImpl_GetPriority,
363 IWineD3DVolumeImpl_PreLoad,
364 IWineD3DVolumeImpl_UnLoad,
365 IWineD3DVolumeImpl_GetType,
367 IWineD3DVolumeImpl_GetContainer,
368 IWineD3DVolumeImpl_GetDesc,
369 IWineD3DVolumeImpl_LockBox,
370 IWineD3DVolumeImpl_UnlockBox,
371 /* Internal interface */
372 IWineD3DVolumeImpl_LoadTexture,
373 IWineD3DVolumeImpl_SetContainer