dinput/tests: Skip acquire tests when not running in the foreground.
[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(IWineD3DVolume *iface) {
31     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
32     const struct wined3d_gl_info *gl_info = &This->resource.device->adapter->gl_info;
33     IWineD3DBaseTextureImpl *container = (IWineD3DBaseTextureImpl *)This->container;
34     DWORD active_sampler;
35
36     /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
37      * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
38      * gl states. The current texture unit should always be a valid one.
39      *
40      * To be more specific, this is tricky because we can implicitly be called
41      * from sampler() in state.c. This means we can't touch anything other than
42      * whatever happens to be the currently active texture, or we would risk
43      * marking already applied sampler states dirty again.
44      *
45      * TODO: Track the current active texture per GL context instead of using glGet
46      */
47     if (gl_info->supported[ARB_MULTITEXTURE])
48     {
49         GLint active_texture;
50         ENTER_GL();
51         glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
52         LEAVE_GL();
53         active_sampler = This->resource.device->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
54     } else {
55         active_sampler = 0;
56     }
57
58     if (active_sampler != WINED3D_UNMAPPED_STAGE)
59     {
60         IWineD3DDeviceImpl_MarkStateDirty(This->resource.device, STATE_SAMPLER(active_sampler));
61     }
62
63     container->baseTexture.texture_ops->texture_bind(container, FALSE);
64 }
65
66 void volume_add_dirty_box(IWineD3DVolume *iface, const WINED3DBOX *dirty_box)
67 {
68     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
69
70     This->dirty = TRUE;
71     if (dirty_box)
72     {
73         This->lockedBox.Left = min(This->lockedBox.Left, dirty_box->Left);
74         This->lockedBox.Top = min(This->lockedBox.Top, dirty_box->Top);
75         This->lockedBox.Front = min(This->lockedBox.Front, dirty_box->Front);
76         This->lockedBox.Right = max(This->lockedBox.Right, dirty_box->Right);
77         This->lockedBox.Bottom = max(This->lockedBox.Bottom, dirty_box->Bottom);
78         This->lockedBox.Back = max(This->lockedBox.Back, dirty_box->Back);
79     }
80     else
81     {
82         This->lockedBox.Left = 0;
83         This->lockedBox.Top = 0;
84         This->lockedBox.Front = 0;
85         This->lockedBox.Right = This->currentDesc.Width;
86         This->lockedBox.Bottom = This->currentDesc.Height;
87         This->lockedBox.Back = This->currentDesc.Depth;
88     }
89 }
90
91 void volume_set_container(IWineD3DVolumeImpl *volume, struct IWineD3DVolumeTextureImpl *container)
92 {
93     TRACE("volume %p, container %p.\n", volume, container);
94
95     volume->container = container;
96 }
97
98 /* *******************************************
99    IWineD3DVolume IUnknown parts follow
100    ******************************************* */
101 static HRESULT WINAPI IWineD3DVolumeImpl_QueryInterface(IWineD3DVolume *iface, REFIID riid, void **object)
102 {
103     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
104
105     if (IsEqualGUID(riid, &IID_IWineD3DVolume)
106             || IsEqualGUID(riid, &IID_IWineD3DResource)
107             || IsEqualGUID(riid, &IID_IWineD3DBase)
108             || IsEqualGUID(riid, &IID_IUnknown))
109     {
110         IUnknown_AddRef(iface);
111         *object = iface;
112         return S_OK;
113     }
114
115     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
116
117     *object = NULL;
118     return E_NOINTERFACE;
119 }
120
121 static ULONG WINAPI IWineD3DVolumeImpl_AddRef(IWineD3DVolume *iface) {
122     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
123     TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
124     return InterlockedIncrement(&This->resource.ref);
125 }
126
127 /* Do not call while under the GL lock. */
128 static ULONG WINAPI IWineD3DVolumeImpl_Release(IWineD3DVolume *iface) {
129     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
130     ULONG ref;
131     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
132     ref = InterlockedDecrement(&This->resource.ref);
133
134     if (!ref)
135     {
136         resource_cleanup((IWineD3DResourceImpl *)iface);
137         This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
138         HeapFree(GetProcessHeap(), 0, This);
139     }
140     return ref;
141 }
142
143 /* ****************************************************
144    IWineD3DVolume IWineD3DResource parts follow
145    **************************************************** */
146 static void * WINAPI IWineD3DVolumeImpl_GetParent(IWineD3DVolume *iface)
147 {
148     TRACE("iface %p.\n", iface);
149
150     return ((IWineD3DVolumeImpl *)iface)->resource.parent;
151 }
152
153 static HRESULT WINAPI IWineD3DVolumeImpl_SetPrivateData(IWineD3DVolume *iface,
154         REFGUID riid, const void *data, DWORD data_size, DWORD flags)
155 {
156     return resource_set_private_data((IWineD3DResource *)iface, riid, data, data_size, flags);
157 }
158
159 static HRESULT WINAPI IWineD3DVolumeImpl_GetPrivateData(IWineD3DVolume *iface,
160         REFGUID guid, void *data, DWORD *data_size)
161 {
162     return resource_get_private_data((IWineD3DResourceImpl *)iface, guid, data, data_size);
163 }
164
165 static HRESULT WINAPI IWineD3DVolumeImpl_FreePrivateData(IWineD3DVolume *iface, REFGUID refguid)
166 {
167     return resource_free_private_data((IWineD3DResourceImpl *)iface, refguid);
168 }
169
170 static DWORD WINAPI IWineD3DVolumeImpl_SetPriority(IWineD3DVolume *iface, DWORD PriorityNew) {
171     return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
172 }
173
174 static DWORD WINAPI IWineD3DVolumeImpl_GetPriority(IWineD3DVolume *iface) {
175     return resource_get_priority((IWineD3DResource *)iface);
176 }
177
178 /* Do not call while under the GL lock. */
179 static void WINAPI IWineD3DVolumeImpl_PreLoad(IWineD3DVolume *iface) {
180     FIXME("iface %p stub!\n", iface);
181 }
182
183 /* Do not call while under the GL lock. */
184 static void WINAPI IWineD3DVolumeImpl_UnLoad(IWineD3DVolume *iface)
185 {
186     TRACE("iface %p.\n", iface);
187
188     /* The whole content is shadowed on This->resource.allocatedMemory, and
189      * the texture name is managed by the VolumeTexture container. */
190
191     resource_unload((IWineD3DResourceImpl *)iface);
192 }
193
194 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeImpl_GetType(IWineD3DVolume *iface) {
195     return resource_get_type((IWineD3DResource *)iface);
196 }
197
198 static void WINAPI IWineD3DVolumeImpl_GetDesc(IWineD3DVolume *iface, WINED3DVOLUME_DESC *desc)
199 {
200     IWineD3DVolumeImpl *volume = (IWineD3DVolumeImpl *)iface;
201
202     TRACE("iface %p, desc %p.\n", iface, desc);
203
204     desc->Format = volume->resource.format->id;
205     desc->Type = volume->resource.resourceType;
206     desc->Usage = volume->resource.usage;
207     desc->Pool = volume->resource.pool;
208     desc->Size = volume->resource.size; /* dx8 only */
209     desc->Width = volume->currentDesc.Width;
210     desc->Height = volume->currentDesc.Height;
211     desc->Depth = volume->currentDesc.Depth;
212 }
213
214 static HRESULT WINAPI IWineD3DVolumeImpl_Map(IWineD3DVolume *iface,
215         WINED3DLOCKED_BOX *pLockedVolume, const WINED3DBOX *pBox, DWORD flags)
216 {
217     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
218     FIXME("(%p) : pBox=%p stub\n", This, pBox);
219
220     if(!This->resource.allocatedMemory) {
221         This->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size);
222     }
223
224     /* fixme: should we really lock as such? */
225     TRACE("(%p) : box=%p, output pbox=%p, allMem=%p\n", This, pBox, pLockedVolume, This->resource.allocatedMemory);
226
227     pLockedVolume->RowPitch = This->resource.format->byte_count * This->currentDesc.Width; /* Bytes / row   */
228     pLockedVolume->SlicePitch = This->resource.format->byte_count
229             * This->currentDesc.Width * This->currentDesc.Height;                               /* Bytes / slice */
230     if (!pBox) {
231         TRACE("No box supplied - all is ok\n");
232         pLockedVolume->pBits = This->resource.allocatedMemory;
233         This->lockedBox.Left   = 0;
234         This->lockedBox.Top    = 0;
235         This->lockedBox.Front  = 0;
236         This->lockedBox.Right  = This->currentDesc.Width;
237         This->lockedBox.Bottom = This->currentDesc.Height;
238         This->lockedBox.Back   = This->currentDesc.Depth;
239     } else {
240         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);
241         pLockedVolume->pBits = This->resource.allocatedMemory
242                 + (pLockedVolume->SlicePitch * pBox->Front)     /* FIXME: is front < back or vica versa? */
243                 + (pLockedVolume->RowPitch * pBox->Top)
244                 + (pBox->Left * This->resource.format->byte_count);
245         This->lockedBox.Left   = pBox->Left;
246         This->lockedBox.Top    = pBox->Top;
247         This->lockedBox.Front  = pBox->Front;
248         This->lockedBox.Right  = pBox->Right;
249         This->lockedBox.Bottom = pBox->Bottom;
250         This->lockedBox.Back   = pBox->Back;
251     }
252
253     if (!(flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)))
254     {
255         volume_add_dirty_box(iface, &This->lockedBox);
256         This->container->baseTexture.texture_rgb.dirty = TRUE;
257         This->container->baseTexture.texture_srgb.dirty = TRUE;
258     }
259
260     This->locked = TRUE;
261     TRACE("returning memory@%p rpitch(%d) spitch(%d)\n", pLockedVolume->pBits, pLockedVolume->RowPitch, pLockedVolume->SlicePitch);
262     return WINED3D_OK;
263 }
264
265 static HRESULT WINAPI IWineD3DVolumeImpl_Unmap(IWineD3DVolume *iface)
266 {
267     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
268     if (!This->locked)
269     {
270         WARN("Trying to unlock unlocked volume %p.\n", iface);
271         return WINED3DERR_INVALIDCALL;
272     }
273     TRACE("(%p) : unlocking volume\n", This);
274     This->locked = FALSE;
275     memset(&This->lockedBox, 0, sizeof(This->lockedBox));
276     return WINED3D_OK;
277 }
278
279 /* Internal use functions follow : */
280
281 /* Context activation is done by the caller. */
282 static HRESULT WINAPI IWineD3DVolumeImpl_LoadTexture(IWineD3DVolume *iface, int gl_level, BOOL srgb_mode)
283 {
284     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
285     const struct wined3d_gl_info *gl_info = &This->resource.device->adapter->gl_info;
286     const struct wined3d_format *format = This->resource.format;
287
288     TRACE("iface %p, level %u, srgb %#x, format %s (%#x).\n",
289             iface, gl_level, srgb_mode, debug_d3dformat(format->id), format->id);
290
291     volume_bind_and_dirtify(iface);
292
293     TRACE("Calling glTexImage3D %x level=%d, intfmt=%x, w=%d, h=%d,d=%d, 0=%d, glFmt=%x, glType=%x, Mem=%p\n",
294             GL_TEXTURE_3D, gl_level, format->glInternal, This->currentDesc.Width, This->currentDesc.Height,
295             This->currentDesc.Depth, 0, format->glFormat, format->glType, This->resource.allocatedMemory);
296
297     ENTER_GL();
298     GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, gl_level, format->glInternal,
299             This->currentDesc.Width, This->currentDesc.Height, This->currentDesc.Depth,
300             0, format->glFormat, format->glType, This->resource.allocatedMemory));
301     checkGLcall("glTexImage3D");
302     LEAVE_GL();
303
304     /* When adding code releasing This->resource.allocatedMemory to save data keep in mind that
305      * GL_UNPACK_CLIENT_STORAGE_APPLE is enabled by default if supported(GL_APPLE_client_storage).
306      * Thus do not release This->resource.allocatedMemory if GL_APPLE_client_storage is supported.
307      */
308     return WINED3D_OK;
309
310 }
311
312 static const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl =
313 {
314     /* IUnknown */
315     IWineD3DVolumeImpl_QueryInterface,
316     IWineD3DVolumeImpl_AddRef,
317     IWineD3DVolumeImpl_Release,
318     /* IWineD3DResource */
319     IWineD3DVolumeImpl_GetParent,
320     IWineD3DVolumeImpl_SetPrivateData,
321     IWineD3DVolumeImpl_GetPrivateData,
322     IWineD3DVolumeImpl_FreePrivateData,
323     IWineD3DVolumeImpl_SetPriority,
324     IWineD3DVolumeImpl_GetPriority,
325     IWineD3DVolumeImpl_PreLoad,
326     IWineD3DVolumeImpl_UnLoad,
327     IWineD3DVolumeImpl_GetType,
328     /* IWineD3DVolume */
329     IWineD3DVolumeImpl_GetDesc,
330     IWineD3DVolumeImpl_Map,
331     IWineD3DVolumeImpl_Unmap,
332     /* Internal interface */
333     IWineD3DVolumeImpl_LoadTexture,
334 };
335
336 HRESULT volume_init(IWineD3DVolumeImpl *volume, IWineD3DDeviceImpl *device, UINT width,
337         UINT height, UINT depth, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
338         void *parent, const struct wined3d_parent_ops *parent_ops)
339 {
340     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
341     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
342     HRESULT hr;
343
344     if (!gl_info->supported[EXT_TEXTURE3D])
345     {
346         WARN("Volume cannot be created - no volume texture support.\n");
347         return WINED3DERR_INVALIDCALL;
348     }
349
350     volume->lpVtbl = &IWineD3DVolume_Vtbl;
351
352     hr = resource_init((IWineD3DResourceImpl *)volume, WINED3DRTYPE_VOLUME, device,
353             width * height * depth * format->byte_count, usage, format, pool, parent, parent_ops);
354     if (FAILED(hr))
355     {
356         WARN("Failed to initialize resource, returning %#x.\n", hr);
357         return hr;
358     }
359
360     volume->currentDesc.Width = width;
361     volume->currentDesc.Height = height;
362     volume->currentDesc.Depth = depth;
363     volume->lockable = TRUE;
364     volume->locked = FALSE;
365     memset(&volume->lockedBox, 0, sizeof(volume->lockedBox));
366     volume->dirty = TRUE;
367
368     volume_add_dirty_box((IWineD3DVolume *)volume, NULL);
369
370     return WINED3D_OK;
371 }