wined3d: Generalize GetSurfaceLevel() / GetVolumeLevel() to GetSubResource().
[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  * Copyright 2009-2010 Henri Verbeet for CodeWeavers
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include "config.h"
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
28
29 /* Context activation is done by the caller. */
30 static void volume_bind_and_dirtify(struct IWineD3DVolumeImpl *volume, const struct wined3d_gl_info *gl_info)
31 {
32     IWineD3DBaseTextureImpl *container = (IWineD3DBaseTextureImpl *)volume->container;
33     DWORD active_sampler;
34
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.
38      *
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.
43      *
44      * TODO: Track the current active texture per GL context instead of using glGet
45      */
46     if (gl_info->supported[ARB_MULTITEXTURE])
47     {
48         GLint active_texture;
49         ENTER_GL();
50         glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
51         LEAVE_GL();
52         active_sampler = volume->resource.device->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
53     } else {
54         active_sampler = 0;
55     }
56
57     if (active_sampler != WINED3D_UNMAPPED_STAGE)
58     {
59         IWineD3DDeviceImpl_MarkStateDirty(volume->resource.device, STATE_SAMPLER(active_sampler));
60     }
61
62     container->baseTexture.texture_ops->texture_bind(container, gl_info, FALSE);
63 }
64
65 void volume_add_dirty_box(struct IWineD3DVolumeImpl *volume, const WINED3DBOX *dirty_box)
66 {
67     volume->dirty = TRUE;
68     if (dirty_box)
69     {
70         volume->lockedBox.Left = min(volume->lockedBox.Left, dirty_box->Left);
71         volume->lockedBox.Top = min(volume->lockedBox.Top, dirty_box->Top);
72         volume->lockedBox.Front = min(volume->lockedBox.Front, dirty_box->Front);
73         volume->lockedBox.Right = max(volume->lockedBox.Right, dirty_box->Right);
74         volume->lockedBox.Bottom = max(volume->lockedBox.Bottom, dirty_box->Bottom);
75         volume->lockedBox.Back = max(volume->lockedBox.Back, dirty_box->Back);
76     }
77     else
78     {
79         volume->lockedBox.Left = 0;
80         volume->lockedBox.Top = 0;
81         volume->lockedBox.Front = 0;
82         volume->lockedBox.Right = volume->resource.width;
83         volume->lockedBox.Bottom = volume->resource.height;
84         volume->lockedBox.Back = volume->resource.depth;
85     }
86 }
87
88 void volume_set_container(IWineD3DVolumeImpl *volume, struct IWineD3DVolumeTextureImpl *container)
89 {
90     TRACE("volume %p, container %p.\n", volume, container);
91
92     volume->container = container;
93 }
94
95 /* Context activation is done by the caller. */
96 void volume_load(IWineD3DVolumeImpl *volume, UINT level, BOOL srgb_mode)
97 {
98     const struct wined3d_gl_info *gl_info = &volume->resource.device->adapter->gl_info;
99     const struct wined3d_format *format = volume->resource.format;
100
101     TRACE("volume %p, level %u, srgb %#x, format %s (%#x).\n",
102             volume, level, srgb_mode, debug_d3dformat(format->id), format->id);
103
104     volume_bind_and_dirtify(volume, gl_info);
105
106     ENTER_GL();
107     GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, level, format->glInternal,
108             volume->resource.width, volume->resource.height, volume->resource.depth,
109             0, format->glFormat, format->glType, volume->resource.allocatedMemory));
110     checkGLcall("glTexImage3D");
111     LEAVE_GL();
112
113     /* When adding code releasing volume->resource.allocatedMemory to save
114      * data keep in mind that GL_UNPACK_CLIENT_STORAGE_APPLE is enabled by
115      * default if supported(GL_APPLE_client_storage). Thus do not release
116      * volume->resource.allocatedMemory if GL_APPLE_client_storage is
117      * supported. */
118 }
119
120 /* Do not call while under the GL lock. */
121 static void volume_unload(struct wined3d_resource *resource)
122 {
123     TRACE("texture %p.\n", resource);
124
125     /* The whole content is shadowed on This->resource.allocatedMemory, and
126      * the texture name is managed by the VolumeTexture container. */
127
128     resource_unload(resource);
129 }
130
131 /* *******************************************
132    IWineD3DVolume IUnknown parts follow
133    ******************************************* */
134 static HRESULT WINAPI IWineD3DVolumeImpl_QueryInterface(IWineD3DVolume *iface, REFIID riid, void **object)
135 {
136     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
137
138     if (IsEqualGUID(riid, &IID_IWineD3DVolume)
139             || IsEqualGUID(riid, &IID_IWineD3DResource)
140             || IsEqualGUID(riid, &IID_IWineD3DBase)
141             || IsEqualGUID(riid, &IID_IUnknown))
142     {
143         IUnknown_AddRef(iface);
144         *object = iface;
145         return S_OK;
146     }
147
148     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
149
150     *object = NULL;
151     return E_NOINTERFACE;
152 }
153
154 static ULONG WINAPI IWineD3DVolumeImpl_AddRef(IWineD3DVolume *iface) {
155     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
156     TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
157     return InterlockedIncrement(&This->resource.ref);
158 }
159
160 /* Do not call while under the GL lock. */
161 static ULONG WINAPI IWineD3DVolumeImpl_Release(IWineD3DVolume *iface) {
162     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
163     ULONG ref;
164     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
165     ref = InterlockedDecrement(&This->resource.ref);
166
167     if (!ref)
168     {
169         resource_cleanup(&This->resource);
170         This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
171         HeapFree(GetProcessHeap(), 0, This);
172     }
173     return ref;
174 }
175
176 /* ****************************************************
177    IWineD3DVolume IWineD3DResource parts follow
178    **************************************************** */
179 static void * WINAPI IWineD3DVolumeImpl_GetParent(IWineD3DVolume *iface)
180 {
181     TRACE("iface %p.\n", iface);
182
183     return ((IWineD3DVolumeImpl *)iface)->resource.parent;
184 }
185
186 static HRESULT WINAPI IWineD3DVolumeImpl_SetPrivateData(IWineD3DVolume *iface,
187         REFGUID riid, const void *data, DWORD data_size, DWORD flags)
188 {
189     return resource_set_private_data(&((IWineD3DVolumeImpl *)iface)->resource, riid, data, data_size, flags);
190 }
191
192 static HRESULT WINAPI IWineD3DVolumeImpl_GetPrivateData(IWineD3DVolume *iface,
193         REFGUID guid, void *data, DWORD *data_size)
194 {
195     return resource_get_private_data(&((IWineD3DVolumeImpl *)iface)->resource, guid, data, data_size);
196 }
197
198 static HRESULT WINAPI IWineD3DVolumeImpl_FreePrivateData(IWineD3DVolume *iface, REFGUID refguid)
199 {
200     return resource_free_private_data(&((IWineD3DVolumeImpl *)iface)->resource, refguid);
201 }
202
203 static DWORD WINAPI IWineD3DVolumeImpl_SetPriority(IWineD3DVolume *iface, DWORD priority)
204 {
205     return resource_set_priority(&((IWineD3DVolumeImpl *)iface)->resource, priority);
206 }
207
208 static DWORD WINAPI IWineD3DVolumeImpl_GetPriority(IWineD3DVolume *iface)
209 {
210     return resource_get_priority(&((IWineD3DVolumeImpl *)iface)->resource);
211 }
212
213 /* Do not call while under the GL lock. */
214 static void WINAPI IWineD3DVolumeImpl_PreLoad(IWineD3DVolume *iface) {
215     FIXME("iface %p stub!\n", iface);
216 }
217
218 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeImpl_GetType(IWineD3DVolume *iface)
219 {
220     return resource_get_type(&((IWineD3DVolumeImpl *)iface)->resource);
221 }
222
223 static void WINAPI IWineD3DVolumeImpl_GetDesc(IWineD3DVolume *iface, struct wined3d_resource_desc *desc)
224 {
225     IWineD3DVolumeImpl *volume = (IWineD3DVolumeImpl *)iface;
226
227     TRACE("iface %p, desc %p.\n", iface, desc);
228
229     wined3d_resource_get_desc(&volume->resource, desc);
230 }
231
232 static HRESULT WINAPI IWineD3DVolumeImpl_Map(IWineD3DVolume *iface,
233         WINED3DLOCKED_BOX *pLockedVolume, const WINED3DBOX *pBox, DWORD flags)
234 {
235     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
236     FIXME("(%p) : pBox=%p stub\n", This, pBox);
237
238     if(!This->resource.allocatedMemory) {
239         This->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size);
240     }
241
242     /* fixme: should we really lock as such? */
243     TRACE("(%p) : box=%p, output pbox=%p, allMem=%p\n", This, pBox, pLockedVolume, This->resource.allocatedMemory);
244
245     pLockedVolume->RowPitch = This->resource.format->byte_count * This->resource.width; /* Bytes / row */
246     pLockedVolume->SlicePitch = This->resource.format->byte_count
247             * This->resource.width * This->resource.height; /* Bytes / slice */
248     if (!pBox) {
249         TRACE("No box supplied - all is ok\n");
250         pLockedVolume->pBits = This->resource.allocatedMemory;
251         This->lockedBox.Left   = 0;
252         This->lockedBox.Top    = 0;
253         This->lockedBox.Front  = 0;
254         This->lockedBox.Right  = This->resource.width;
255         This->lockedBox.Bottom = This->resource.height;
256         This->lockedBox.Back   = This->resource.depth;
257     } else {
258         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);
259         pLockedVolume->pBits = This->resource.allocatedMemory
260                 + (pLockedVolume->SlicePitch * pBox->Front)     /* FIXME: is front < back or vica versa? */
261                 + (pLockedVolume->RowPitch * pBox->Top)
262                 + (pBox->Left * This->resource.format->byte_count);
263         This->lockedBox.Left   = pBox->Left;
264         This->lockedBox.Top    = pBox->Top;
265         This->lockedBox.Front  = pBox->Front;
266         This->lockedBox.Right  = pBox->Right;
267         This->lockedBox.Bottom = pBox->Bottom;
268         This->lockedBox.Back   = pBox->Back;
269     }
270
271     if (!(flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)))
272     {
273         volume_add_dirty_box(This, &This->lockedBox);
274         basetexture_set_dirty((IWineD3DBaseTextureImpl *)This->container, TRUE);
275     }
276
277     This->locked = TRUE;
278     TRACE("returning memory@%p rpitch(%d) spitch(%d)\n", pLockedVolume->pBits, pLockedVolume->RowPitch, pLockedVolume->SlicePitch);
279     return WINED3D_OK;
280 }
281
282 static HRESULT WINAPI IWineD3DVolumeImpl_Unmap(IWineD3DVolume *iface)
283 {
284     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
285     if (!This->locked)
286     {
287         WARN("Trying to unlock unlocked volume %p.\n", iface);
288         return WINED3DERR_INVALIDCALL;
289     }
290     TRACE("(%p) : unlocking volume\n", This);
291     This->locked = FALSE;
292     memset(&This->lockedBox, 0, sizeof(This->lockedBox));
293     return WINED3D_OK;
294 }
295
296 static const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl =
297 {
298     /* IUnknown */
299     IWineD3DVolumeImpl_QueryInterface,
300     IWineD3DVolumeImpl_AddRef,
301     IWineD3DVolumeImpl_Release,
302     /* IWineD3DResource */
303     IWineD3DVolumeImpl_GetParent,
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_GetDesc,
313     IWineD3DVolumeImpl_Map,
314     IWineD3DVolumeImpl_Unmap,
315 };
316
317 static const struct wined3d_resource_ops volume_resource_ops =
318 {
319     volume_unload,
320 };
321
322 HRESULT volume_init(IWineD3DVolumeImpl *volume, IWineD3DDeviceImpl *device, UINT width,
323         UINT height, UINT depth, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
324         void *parent, const struct wined3d_parent_ops *parent_ops)
325 {
326     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
327     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
328     HRESULT hr;
329
330     if (!gl_info->supported[EXT_TEXTURE3D])
331     {
332         WARN("Volume cannot be created - no volume texture support.\n");
333         return WINED3DERR_INVALIDCALL;
334     }
335
336     volume->lpVtbl = &IWineD3DVolume_Vtbl;
337
338     hr = resource_init(&volume->resource, device, WINED3DRTYPE_VOLUME, format,
339             WINED3DMULTISAMPLE_NONE, 0, usage, pool, width, height, depth,
340             width * height * depth * format->byte_count, parent, parent_ops,
341             &volume_resource_ops);
342     if (FAILED(hr))
343     {
344         WARN("Failed to initialize resource, returning %#x.\n", hr);
345         return hr;
346     }
347
348     volume->lockable = TRUE;
349     volume->locked = FALSE;
350     memset(&volume->lockedBox, 0, sizeof(volume->lockedBox));
351     volume->dirty = TRUE;
352
353     volume_add_dirty_box(volume, NULL);
354
355     return WINED3D_OK;
356 }