gdi32: Check the region handle in GetClipRgn later.
[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 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 #define GLINFO_LOCATION This->resource.device->adapter->gl_info
29
30 /* Context activation is done by the caller. */
31 static void volume_bind_and_dirtify(IWineD3DVolume *iface) {
32     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
33     const struct wined3d_gl_info *gl_info = &This->resource.device->adapter->gl_info;
34     IWineD3DVolumeTexture *texture;
35     DWORD active_sampler;
36
37     /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
38      * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
39      * gl states. The current texture unit should always be a valid one.
40      *
41      * To be more specific, this is tricky because we can implicitly be called
42      * from sampler() in state.c. This means we can't touch anything other than
43      * whatever happens to be the currently active texture, or we would risk
44      * marking already applied sampler states dirty again.
45      *
46      * TODO: Track the current active texture per GL context instead of using glGet
47      */
48     if (gl_info->supported[ARB_MULTITEXTURE])
49     {
50         GLint active_texture;
51         ENTER_GL();
52         glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
53         LEAVE_GL();
54         active_sampler = This->resource.device->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
55     } else {
56         active_sampler = 0;
57     }
58
59     if (active_sampler != WINED3D_UNMAPPED_STAGE)
60     {
61         IWineD3DDeviceImpl_MarkStateDirty(This->resource.device, STATE_SAMPLER(active_sampler));
62     }
63
64     if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DVolumeTexture, (void **)&texture))) {
65         IWineD3DVolumeTexture_BindTexture(texture, FALSE);
66         IWineD3DVolumeTexture_Release(texture);
67     } else {
68         ERR("Volume should be part of a volume texture\n");
69     }
70 }
71
72 void volume_add_dirty_box(IWineD3DVolume *iface, const WINED3DBOX *dirty_box)
73 {
74     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
75
76     This->dirty = TRUE;
77     if (dirty_box)
78     {
79         This->lockedBox.Left = min(This->lockedBox.Left, dirty_box->Left);
80         This->lockedBox.Top = min(This->lockedBox.Top, dirty_box->Top);
81         This->lockedBox.Front = min(This->lockedBox.Front, dirty_box->Front);
82         This->lockedBox.Right = max(This->lockedBox.Right, dirty_box->Right);
83         This->lockedBox.Bottom = max(This->lockedBox.Bottom, dirty_box->Bottom);
84         This->lockedBox.Back = max(This->lockedBox.Back, dirty_box->Back);
85     }
86     else
87     {
88         This->lockedBox.Left = 0;
89         This->lockedBox.Top = 0;
90         This->lockedBox.Front = 0;
91         This->lockedBox.Right = This->currentDesc.Width;
92         This->lockedBox.Bottom = This->currentDesc.Height;
93         This->lockedBox.Back = This->currentDesc.Depth;
94     }
95 }
96
97 /* *******************************************
98    IWineD3DVolume IUnknown parts follow
99    ******************************************* */
100 static HRESULT WINAPI IWineD3DVolumeImpl_QueryInterface(IWineD3DVolume *iface, REFIID riid, void **object)
101 {
102     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
103
104     if (IsEqualGUID(riid, &IID_IWineD3DVolume)
105             || IsEqualGUID(riid, &IID_IWineD3DResource)
106             || IsEqualGUID(riid, &IID_IWineD3DBase)
107             || IsEqualGUID(riid, &IID_IUnknown))
108     {
109         IUnknown_AddRef(iface);
110         *object = iface;
111         return S_OK;
112     }
113
114     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
115
116     *object = NULL;
117     return E_NOINTERFACE;
118 }
119
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);
124 }
125
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);
135     }
136     return ref;
137 }
138
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);
144 }
145
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);
148 }
149
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);
152 }
153
154 static HRESULT WINAPI IWineD3DVolumeImpl_FreePrivateData(IWineD3DVolume *iface, REFGUID refguid) {
155     return resource_free_private_data((IWineD3DResource *)iface, refguid);
156 }
157
158 static DWORD WINAPI IWineD3DVolumeImpl_SetPriority(IWineD3DVolume *iface, DWORD PriorityNew) {
159     return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
160 }
161
162 static DWORD WINAPI IWineD3DVolumeImpl_GetPriority(IWineD3DVolume *iface) {
163     return resource_get_priority((IWineD3DResource *)iface);
164 }
165
166 static void WINAPI IWineD3DVolumeImpl_PreLoad(IWineD3DVolume *iface) {
167     FIXME("iface %p stub!\n", iface);
168 }
169
170 static void WINAPI IWineD3DVolumeImpl_UnLoad(IWineD3DVolume *iface) {
171     /* The whole content is shadowed on This->resource.allocatedMemory, and the
172      * texture name is managed by the VolumeTexture container
173      */
174     TRACE("(%p): Nothing to do\n", iface);
175 }
176
177 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeImpl_GetType(IWineD3DVolume *iface) {
178     return resource_get_type((IWineD3DResource *)iface);
179 }
180
181 /* *******************************************
182    IWineD3DVolume parts follow
183    ******************************************* */
184 static HRESULT WINAPI IWineD3DVolumeImpl_GetContainer(IWineD3DVolume *iface, REFIID riid, void** ppContainer) {
185     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
186
187     TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
188
189     if (!ppContainer) {
190         ERR("Called without a valid ppContainer.\n");
191         return E_FAIL;
192     }
193
194     /* Although surfaces can be standalone, volumes can't */
195     if (!This->container) {
196         ERR("Volume without an container. Should not happen.\n");
197         return E_FAIL;
198     }
199
200     TRACE("Relaying to QueryInterface\n");
201     return IUnknown_QueryInterface(This->container, riid, ppContainer);
202 }
203
204 static HRESULT WINAPI IWineD3DVolumeImpl_GetDesc(IWineD3DVolume *iface, WINED3DVOLUME_DESC* pDesc) {
205     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
206     TRACE("(%p) : copying into %p\n", This, pDesc);
207
208     pDesc->Format = This->resource.format_desc->format;
209     pDesc->Type = This->resource.resourceType;
210     pDesc->Usage = This->resource.usage;
211     pDesc->Pool = This->resource.pool;
212     pDesc->Size = This->resource.size; /* dx8 only */
213     pDesc->Width = This->currentDesc.Width;
214     pDesc->Height = This->currentDesc.Height;
215     pDesc->Depth = This->currentDesc.Depth;
216
217     return WINED3D_OK;
218 }
219
220 static HRESULT WINAPI IWineD3DVolumeImpl_LockBox(IWineD3DVolume *iface, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
221     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
222     FIXME("(%p) : pBox=%p stub\n", This, pBox);
223
224     if(!This->resource.allocatedMemory) {
225         This->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size);
226     }
227
228     /* fixme: should we really lock as such? */
229     TRACE("(%p) : box=%p, output pbox=%p, allMem=%p\n", This, pBox, pLockedVolume, This->resource.allocatedMemory);
230
231     pLockedVolume->RowPitch = This->resource.format_desc->byte_count * This->currentDesc.Width; /* Bytes / row   */
232     pLockedVolume->SlicePitch = This->resource.format_desc->byte_count
233             * This->currentDesc.Width * This->currentDesc.Height;                               /* Bytes / slice */
234     if (!pBox) {
235         TRACE("No box supplied - all is ok\n");
236         pLockedVolume->pBits = This->resource.allocatedMemory;
237         This->lockedBox.Left   = 0;
238         This->lockedBox.Top    = 0;
239         This->lockedBox.Front  = 0;
240         This->lockedBox.Right  = This->currentDesc.Width;
241         This->lockedBox.Bottom = This->currentDesc.Height;
242         This->lockedBox.Back   = This->currentDesc.Depth;
243     } else {
244         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);
245         pLockedVolume->pBits = This->resource.allocatedMemory
246                 + (pLockedVolume->SlicePitch * pBox->Front)     /* FIXME: is front < back or vica versa? */
247                 + (pLockedVolume->RowPitch * pBox->Top)
248                 + (pBox->Left * This->resource.format_desc->byte_count);
249         This->lockedBox.Left   = pBox->Left;
250         This->lockedBox.Top    = pBox->Top;
251         This->lockedBox.Front  = pBox->Front;
252         This->lockedBox.Right  = pBox->Right;
253         This->lockedBox.Bottom = pBox->Bottom;
254         This->lockedBox.Back   = pBox->Back;
255     }
256
257     if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
258       /* Don't dirtify */
259     } else {
260       /**
261        * Dirtify on lock
262        * as seen in msdn docs
263        */
264       volume_add_dirty_box(iface, &This->lockedBox);
265
266       /**  Dirtify Container if needed */
267       if (NULL != This->container) {
268
269         IWineD3DVolumeTexture *cont = (IWineD3DVolumeTexture*) This->container;
270         WINED3DRESOURCETYPE containerType = IWineD3DBaseTexture_GetType((IWineD3DBaseTexture *) cont);
271
272         if (containerType == WINED3DRTYPE_VOLUMETEXTURE) {
273           IWineD3DBaseTextureImpl* pTexture = (IWineD3DBaseTextureImpl*) cont;
274           pTexture->baseTexture.texture_rgb.dirty = TRUE;
275           pTexture->baseTexture.texture_srgb.dirty = TRUE;
276         } else {
277           FIXME("Set dirty on container type %d\n", containerType);
278         }
279       }
280     }
281
282     This->locked = TRUE;
283     TRACE("returning memory@%p rpitch(%d) spitch(%d)\n", pLockedVolume->pBits, pLockedVolume->RowPitch, pLockedVolume->SlicePitch);
284     return WINED3D_OK;
285 }
286
287 static HRESULT WINAPI IWineD3DVolumeImpl_UnlockBox(IWineD3DVolume *iface) {
288     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
289     if (!This->locked)
290     {
291         WARN("Trying to unlock unlocked volume %p.\n", iface);
292         return WINED3DERR_INVALIDCALL;
293     }
294     TRACE("(%p) : unlocking volume\n", This);
295     This->locked = FALSE;
296     memset(&This->lockedBox, 0, sizeof(RECT));
297     return WINED3D_OK;
298 }
299
300 /* Internal use functions follow : */
301
302 static HRESULT WINAPI IWineD3DVolumeImpl_SetContainer(IWineD3DVolume *iface, IWineD3DBase* container) {
303     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
304
305     TRACE("This %p, container %p\n", This, container);
306
307     /* We can't keep a reference to the container, since the container already keeps a reference to us. */
308
309     TRACE("Setting container to %p from %p\n", container, This->container);
310     This->container = container;
311
312     return WINED3D_OK;
313 }
314
315 /* Context activation is done by the caller. */
316 static HRESULT WINAPI IWineD3DVolumeImpl_LoadTexture(IWineD3DVolume *iface, int gl_level, BOOL srgb_mode) {
317     IWineD3DVolumeImpl *This     = (IWineD3DVolumeImpl *)iface;
318     const struct GlPixelFormatDesc *glDesc = This->resource.format_desc;
319
320     TRACE("(%p) : level %u, format %s (0x%08x)\n", This, gl_level, debug_d3dformat(glDesc->format), glDesc->format);
321
322     volume_bind_and_dirtify(iface);
323
324     TRACE("Calling glTexImage3D %x level=%d, intfmt=%x, w=%d, h=%d,d=%d, 0=%d, glFmt=%x, glType=%x, Mem=%p\n",
325             GL_TEXTURE_3D,
326             gl_level,
327             glDesc->glInternal,
328             This->currentDesc.Width,
329             This->currentDesc.Height,
330             This->currentDesc.Depth,
331             0,
332             glDesc->glFormat,
333             glDesc->glType,
334             This->resource.allocatedMemory);
335
336     ENTER_GL();
337     GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D,
338                 gl_level,
339                 glDesc->glInternal,
340                 This->currentDesc.Width,
341                 This->currentDesc.Height,
342                 This->currentDesc.Depth,
343                 0,
344                 glDesc->glFormat,
345                 glDesc->glType,
346                 This->resource.allocatedMemory));
347     checkGLcall("glTexImage3D");
348     LEAVE_GL();
349
350     /* When adding code releasing This->resource.allocatedMemory to save data keep in mind that
351      * GL_UNPACK_CLIENT_STORAGE_APPLE is enabled by default if supported(GL_APPLE_client_storage).
352      * Thus do not release This->resource.allocatedMemory if GL_APPLE_client_storage is supported.
353      */
354     return WINED3D_OK;
355
356 }
357
358 static const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl =
359 {
360     /* IUnknown */
361     IWineD3DVolumeImpl_QueryInterface,
362     IWineD3DVolumeImpl_AddRef,
363     IWineD3DVolumeImpl_Release,
364     /* IWineD3DResource */
365     IWineD3DVolumeImpl_GetParent,
366     IWineD3DVolumeImpl_SetPrivateData,
367     IWineD3DVolumeImpl_GetPrivateData,
368     IWineD3DVolumeImpl_FreePrivateData,
369     IWineD3DVolumeImpl_SetPriority,
370     IWineD3DVolumeImpl_GetPriority,
371     IWineD3DVolumeImpl_PreLoad,
372     IWineD3DVolumeImpl_UnLoad,
373     IWineD3DVolumeImpl_GetType,
374     /* IWineD3DVolume */
375     IWineD3DVolumeImpl_GetContainer,
376     IWineD3DVolumeImpl_GetDesc,
377     IWineD3DVolumeImpl_LockBox,
378     IWineD3DVolumeImpl_UnlockBox,
379     /* Internal interface */
380     IWineD3DVolumeImpl_LoadTexture,
381     IWineD3DVolumeImpl_SetContainer
382 };
383
384 HRESULT volume_init(IWineD3DVolumeImpl *volume, IWineD3DDeviceImpl *device, UINT width,
385         UINT height, UINT depth, DWORD usage, WINED3DFORMAT format, WINED3DPOOL pool,
386         IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
387 {
388     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
389     const struct GlPixelFormatDesc *format_desc = getFormatDescEntry(format, gl_info);
390     HRESULT hr;
391
392     if (!gl_info->supported[EXT_TEXTURE3D])
393     {
394         WARN("Volume cannot be created - no volume texture support.\n");
395         return WINED3DERR_INVALIDCALL;
396     }
397
398     volume->lpVtbl = &IWineD3DVolume_Vtbl;
399
400     hr = resource_init((IWineD3DResource *)volume, WINED3DRTYPE_VOLUME, device,
401             width * height * depth * format_desc->byte_count, usage, format_desc, pool, parent, parent_ops);
402     if (FAILED(hr))
403     {
404         WARN("Failed to initialize resource, returning %#x.\n", hr);
405         return hr;
406     }
407
408     volume->currentDesc.Width = width;
409     volume->currentDesc.Height = height;
410     volume->currentDesc.Depth = depth;
411     volume->lockable = TRUE;
412     volume->locked = FALSE;
413     memset(&volume->lockedBox, 0, sizeof(volume->lockedBox));
414     volume->dirty = TRUE;
415
416     volume_add_dirty_box((IWineD3DVolume *)volume, NULL);
417
418     return WINED3D_OK;
419 }