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