wined3d: A volume's container is always a volume texture.
[wine/multimedia.git] / dlls / wined3d / volume.c
blob9b82fab5a81bf6de5e838aa03bf73141807668b7
1 /*
2 * IWineD3DVolume implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Copyright 2002-2005 Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2009 Henri Verbeet for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
29 /* Context activation is done by the caller. */
30 static void volume_bind_and_dirtify(IWineD3DVolume *iface) {
31 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
32 const struct wined3d_gl_info *gl_info = &This->resource.device->adapter->gl_info;
33 DWORD active_sampler;
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_info->supported[ARB_MULTITEXTURE])
48 GLint active_texture;
49 ENTER_GL();
50 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
51 LEAVE_GL();
52 active_sampler = This->resource.device->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
53 } else {
54 active_sampler = 0;
57 if (active_sampler != WINED3D_UNMAPPED_STAGE)
59 IWineD3DDeviceImpl_MarkStateDirty(This->resource.device, STATE_SAMPLER(active_sampler));
62 IWineD3DVolumeTexture_BindTexture((IWineD3DVolumeTexture *)This->container, FALSE);
65 void volume_add_dirty_box(IWineD3DVolume *iface, const WINED3DBOX *dirty_box)
67 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
69 This->dirty = TRUE;
70 if (dirty_box)
72 This->lockedBox.Left = min(This->lockedBox.Left, dirty_box->Left);
73 This->lockedBox.Top = min(This->lockedBox.Top, dirty_box->Top);
74 This->lockedBox.Front = min(This->lockedBox.Front, dirty_box->Front);
75 This->lockedBox.Right = max(This->lockedBox.Right, dirty_box->Right);
76 This->lockedBox.Bottom = max(This->lockedBox.Bottom, dirty_box->Bottom);
77 This->lockedBox.Back = max(This->lockedBox.Back, dirty_box->Back);
79 else
81 This->lockedBox.Left = 0;
82 This->lockedBox.Top = 0;
83 This->lockedBox.Front = 0;
84 This->lockedBox.Right = This->currentDesc.Width;
85 This->lockedBox.Bottom = This->currentDesc.Height;
86 This->lockedBox.Back = This->currentDesc.Depth;
90 void volume_set_container(IWineD3DVolumeImpl *volume, struct IWineD3DVolumeTextureImpl *container)
92 TRACE("volume %p, container %p.\n", volume, container);
94 volume->container = container;
97 /* *******************************************
98 IWineD3DVolume IUnknown parts follow
99 ******************************************* */
100 static HRESULT WINAPI IWineD3DVolumeImpl_QueryInterface(IWineD3DVolume *iface, REFIID riid, void **object)
102 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
104 if (IsEqualGUID(riid, &IID_IWineD3DVolume)
105 || IsEqualGUID(riid, &IID_IWineD3DResource)
106 || IsEqualGUID(riid, &IID_IWineD3DBase)
107 || IsEqualGUID(riid, &IID_IUnknown))
109 IUnknown_AddRef(iface);
110 *object = iface;
111 return S_OK;
114 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
116 *object = NULL;
117 return E_NOINTERFACE;
120 static ULONG WINAPI IWineD3DVolumeImpl_AddRef(IWineD3DVolume *iface) {
121 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
122 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
123 return InterlockedIncrement(&This->resource.ref);
126 static ULONG WINAPI IWineD3DVolumeImpl_Release(IWineD3DVolume *iface) {
127 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
128 ULONG ref;
129 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
130 ref = InterlockedDecrement(&This->resource.ref);
131 if (ref == 0) {
132 resource_cleanup((IWineD3DResource *)iface);
133 This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
134 HeapFree(GetProcessHeap(), 0, This);
136 return ref;
139 /* ****************************************************
140 IWineD3DVolume IWineD3DResource parts follow
141 **************************************************** */
142 static HRESULT WINAPI IWineD3DVolumeImpl_GetParent(IWineD3DVolume *iface, IUnknown **pParent) {
143 return resource_get_parent((IWineD3DResource *)iface, pParent);
146 static HRESULT WINAPI IWineD3DVolumeImpl_SetPrivateData(IWineD3DVolume *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
147 return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
150 static HRESULT WINAPI IWineD3DVolumeImpl_GetPrivateData(IWineD3DVolume *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
151 return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
154 static HRESULT WINAPI IWineD3DVolumeImpl_FreePrivateData(IWineD3DVolume *iface, REFGUID refguid) {
155 return resource_free_private_data((IWineD3DResource *)iface, refguid);
158 static DWORD WINAPI IWineD3DVolumeImpl_SetPriority(IWineD3DVolume *iface, DWORD PriorityNew) {
159 return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
162 static DWORD WINAPI IWineD3DVolumeImpl_GetPriority(IWineD3DVolume *iface) {
163 return resource_get_priority((IWineD3DResource *)iface);
166 static void WINAPI IWineD3DVolumeImpl_PreLoad(IWineD3DVolume *iface) {
167 FIXME("iface %p stub!\n", iface);
170 static void WINAPI IWineD3DVolumeImpl_UnLoad(IWineD3DVolume *iface)
172 TRACE("iface %p.\n", iface);
174 /* The whole content is shadowed on This->resource.allocatedMemory, and
175 * the texture name is managed by the VolumeTexture container. */
177 resource_unload((IWineD3DResourceImpl *)iface);
180 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeImpl_GetType(IWineD3DVolume *iface) {
181 return resource_get_type((IWineD3DResource *)iface);
184 /* *******************************************
185 IWineD3DVolume parts follow
186 ******************************************* */
187 static HRESULT WINAPI IWineD3DVolumeImpl_GetContainer(IWineD3DVolume *iface, REFIID riid, void** ppContainer) {
188 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
190 TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
192 if (!ppContainer) {
193 ERR("Called without a valid ppContainer.\n");
194 return E_FAIL;
197 /* Although surfaces can be standalone, volumes can't */
198 if (!This->container) {
199 ERR("Volume without an container. Should not happen.\n");
200 return E_FAIL;
203 TRACE("Relaying to QueryInterface\n");
204 return IUnknown_QueryInterface((IWineD3DVolumeTexture *)This->container, riid, ppContainer);
207 static HRESULT WINAPI IWineD3DVolumeImpl_GetDesc(IWineD3DVolume *iface, WINED3DVOLUME_DESC* pDesc) {
208 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
209 TRACE("(%p) : copying into %p\n", This, pDesc);
211 pDesc->Format = This->resource.format_desc->format;
212 pDesc->Type = This->resource.resourceType;
213 pDesc->Usage = This->resource.usage;
214 pDesc->Pool = This->resource.pool;
215 pDesc->Size = This->resource.size; /* dx8 only */
216 pDesc->Width = This->currentDesc.Width;
217 pDesc->Height = This->currentDesc.Height;
218 pDesc->Depth = This->currentDesc.Depth;
220 return WINED3D_OK;
223 static HRESULT WINAPI IWineD3DVolumeImpl_LockBox(IWineD3DVolume *iface, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
224 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
225 FIXME("(%p) : pBox=%p stub\n", This, pBox);
227 if(!This->resource.allocatedMemory) {
228 This->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size);
231 /* fixme: should we really lock as such? */
232 TRACE("(%p) : box=%p, output pbox=%p, allMem=%p\n", This, pBox, pLockedVolume, This->resource.allocatedMemory);
234 pLockedVolume->RowPitch = This->resource.format_desc->byte_count * This->currentDesc.Width; /* Bytes / row */
235 pLockedVolume->SlicePitch = This->resource.format_desc->byte_count
236 * This->currentDesc.Width * This->currentDesc.Height; /* Bytes / slice */
237 if (!pBox) {
238 TRACE("No box supplied - all is ok\n");
239 pLockedVolume->pBits = This->resource.allocatedMemory;
240 This->lockedBox.Left = 0;
241 This->lockedBox.Top = 0;
242 This->lockedBox.Front = 0;
243 This->lockedBox.Right = This->currentDesc.Width;
244 This->lockedBox.Bottom = This->currentDesc.Height;
245 This->lockedBox.Back = This->currentDesc.Depth;
246 } else {
247 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);
248 pLockedVolume->pBits = This->resource.allocatedMemory
249 + (pLockedVolume->SlicePitch * pBox->Front) /* FIXME: is front < back or vica versa? */
250 + (pLockedVolume->RowPitch * pBox->Top)
251 + (pBox->Left * This->resource.format_desc->byte_count);
252 This->lockedBox.Left = pBox->Left;
253 This->lockedBox.Top = pBox->Top;
254 This->lockedBox.Front = pBox->Front;
255 This->lockedBox.Right = pBox->Right;
256 This->lockedBox.Bottom = pBox->Bottom;
257 This->lockedBox.Back = pBox->Back;
260 if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
261 /* Don't dirtify */
263 else
265 volume_add_dirty_box(iface, &This->lockedBox);
266 This->container->baseTexture.texture_rgb.dirty = TRUE;
267 This->container->baseTexture.texture_srgb.dirty = TRUE;
270 This->locked = TRUE;
271 TRACE("returning memory@%p rpitch(%d) spitch(%d)\n", pLockedVolume->pBits, pLockedVolume->RowPitch, pLockedVolume->SlicePitch);
272 return WINED3D_OK;
275 static HRESULT WINAPI IWineD3DVolumeImpl_UnlockBox(IWineD3DVolume *iface) {
276 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
277 if (!This->locked)
279 WARN("Trying to unlock unlocked volume %p.\n", iface);
280 return WINED3DERR_INVALIDCALL;
282 TRACE("(%p) : unlocking volume\n", This);
283 This->locked = FALSE;
284 memset(&This->lockedBox, 0, sizeof(RECT));
285 return WINED3D_OK;
288 /* Internal use functions follow : */
290 /* Context activation is done by the caller. */
291 static HRESULT WINAPI IWineD3DVolumeImpl_LoadTexture(IWineD3DVolume *iface, int gl_level, BOOL srgb_mode)
293 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
294 const struct wined3d_gl_info *gl_info = &This->resource.device->adapter->gl_info;
295 const struct wined3d_format_desc *glDesc = This->resource.format_desc;
297 TRACE("(%p) : level %u, format %s (0x%08x)\n", This, gl_level, debug_d3dformat(glDesc->format), glDesc->format);
299 volume_bind_and_dirtify(iface);
301 TRACE("Calling glTexImage3D %x level=%d, intfmt=%x, w=%d, h=%d,d=%d, 0=%d, glFmt=%x, glType=%x, Mem=%p\n",
302 GL_TEXTURE_3D,
303 gl_level,
304 glDesc->glInternal,
305 This->currentDesc.Width,
306 This->currentDesc.Height,
307 This->currentDesc.Depth,
309 glDesc->glFormat,
310 glDesc->glType,
311 This->resource.allocatedMemory);
313 ENTER_GL();
314 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D,
315 gl_level,
316 glDesc->glInternal,
317 This->currentDesc.Width,
318 This->currentDesc.Height,
319 This->currentDesc.Depth,
321 glDesc->glFormat,
322 glDesc->glType,
323 This->resource.allocatedMemory));
324 checkGLcall("glTexImage3D");
325 LEAVE_GL();
327 /* When adding code releasing This->resource.allocatedMemory to save data keep in mind that
328 * GL_UNPACK_CLIENT_STORAGE_APPLE is enabled by default if supported(GL_APPLE_client_storage).
329 * Thus do not release This->resource.allocatedMemory if GL_APPLE_client_storage is supported.
331 return WINED3D_OK;
335 static const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl =
337 /* IUnknown */
338 IWineD3DVolumeImpl_QueryInterface,
339 IWineD3DVolumeImpl_AddRef,
340 IWineD3DVolumeImpl_Release,
341 /* IWineD3DResource */
342 IWineD3DVolumeImpl_GetParent,
343 IWineD3DVolumeImpl_SetPrivateData,
344 IWineD3DVolumeImpl_GetPrivateData,
345 IWineD3DVolumeImpl_FreePrivateData,
346 IWineD3DVolumeImpl_SetPriority,
347 IWineD3DVolumeImpl_GetPriority,
348 IWineD3DVolumeImpl_PreLoad,
349 IWineD3DVolumeImpl_UnLoad,
350 IWineD3DVolumeImpl_GetType,
351 /* IWineD3DVolume */
352 IWineD3DVolumeImpl_GetContainer,
353 IWineD3DVolumeImpl_GetDesc,
354 IWineD3DVolumeImpl_LockBox,
355 IWineD3DVolumeImpl_UnlockBox,
356 /* Internal interface */
357 IWineD3DVolumeImpl_LoadTexture,
360 HRESULT volume_init(IWineD3DVolumeImpl *volume, IWineD3DDeviceImpl *device, UINT width,
361 UINT height, UINT depth, DWORD usage, WINED3DFORMAT format, WINED3DPOOL pool,
362 IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
364 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
365 const struct wined3d_format_desc *format_desc = getFormatDescEntry(format, gl_info);
366 HRESULT hr;
368 if (!gl_info->supported[EXT_TEXTURE3D])
370 WARN("Volume cannot be created - no volume texture support.\n");
371 return WINED3DERR_INVALIDCALL;
374 volume->lpVtbl = &IWineD3DVolume_Vtbl;
376 hr = resource_init((IWineD3DResource *)volume, WINED3DRTYPE_VOLUME, device,
377 width * height * depth * format_desc->byte_count, usage, format_desc, pool, parent, parent_ops);
378 if (FAILED(hr))
380 WARN("Failed to initialize resource, returning %#x.\n", hr);
381 return hr;
384 volume->currentDesc.Width = width;
385 volume->currentDesc.Height = height;
386 volume->currentDesc.Depth = depth;
387 volume->lockable = TRUE;
388 volume->locked = FALSE;
389 memset(&volume->lockedBox, 0, sizeof(volume->lockedBox));
390 volume->dirty = TRUE;
392 volume_add_dirty_box((IWineD3DVolume *)volume, NULL);
394 return WINED3D_OK;