oleaut32/tests: Add test for tlibattr->lcid.
[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  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wined3d_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
27 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
28
29 /* Context activation is done by the caller. */
30 static void volume_bind_and_dirtify(IWineD3DVolume *iface) {
31     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
32     IWineD3DVolumeTexture *texture;
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_SUPPORT(ARB_MULTITEXTURE)) {
47         GLint active_texture;
48         ENTER_GL();
49         glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
50         LEAVE_GL();
51         active_sampler = This->resource.wineD3DDevice->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
52     } else {
53         active_sampler = 0;
54     }
55
56     if (active_sampler != WINED3D_UNMAPPED_STAGE)
57     {
58         IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(active_sampler));
59     }
60
61     if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DVolumeTexture, (void **)&texture))) {
62         IWineD3DVolumeTexture_BindTexture(texture, FALSE);
63         IWineD3DVolumeTexture_Release(texture);
64     } else {
65         ERR("Volume should be part of a volume texture\n");
66     }
67 }
68
69 void volume_add_dirty_box(IWineD3DVolume *iface, const WINED3DBOX *dirty_box)
70 {
71     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
72
73     This->dirty = TRUE;
74     if (dirty_box)
75     {
76         This->lockedBox.Left = min(This->lockedBox.Left, dirty_box->Left);
77         This->lockedBox.Top = min(This->lockedBox.Top, dirty_box->Top);
78         This->lockedBox.Front = min(This->lockedBox.Front, dirty_box->Front);
79         This->lockedBox.Right = max(This->lockedBox.Right, dirty_box->Right);
80         This->lockedBox.Bottom = max(This->lockedBox.Bottom, dirty_box->Bottom);
81         This->lockedBox.Back = max(This->lockedBox.Back, dirty_box->Back);
82     }
83     else
84     {
85         This->lockedBox.Left = 0;
86         This->lockedBox.Top = 0;
87         This->lockedBox.Front = 0;
88         This->lockedBox.Right = This->currentDesc.Width;
89         This->lockedBox.Bottom = This->currentDesc.Height;
90         This->lockedBox.Back = This->currentDesc.Depth;
91     }
92 }
93
94 /* *******************************************
95    IWineD3DVolume IUnknown parts follow
96    ******************************************* */
97 static HRESULT WINAPI IWineD3DVolumeImpl_QueryInterface(IWineD3DVolume *iface, REFIID riid, LPVOID *ppobj)
98 {
99     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
100     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
101     if (IsEqualGUID(riid, &IID_IUnknown)
102         || IsEqualGUID(riid, &IID_IWineD3DBase)
103         || IsEqualGUID(riid, &IID_IWineD3DVolume)){
104         IUnknown_AddRef(iface);
105         *ppobj = This;
106         return S_OK;
107     }
108     *ppobj = NULL;
109     return E_NOINTERFACE;
110 }
111
112 static ULONG WINAPI IWineD3DVolumeImpl_AddRef(IWineD3DVolume *iface) {
113     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
114     TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
115     return InterlockedIncrement(&This->resource.ref);
116 }
117
118 static ULONG WINAPI IWineD3DVolumeImpl_Release(IWineD3DVolume *iface) {
119     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
120     ULONG ref;
121     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
122     ref = InterlockedDecrement(&This->resource.ref);
123     if (ref == 0) {
124         resource_cleanup((IWineD3DResource *)iface);
125         HeapFree(GetProcessHeap(), 0, This);
126     }
127     return ref;
128 }
129
130 /* ****************************************************
131    IWineD3DVolume IWineD3DResource parts follow
132    **************************************************** */
133 static HRESULT WINAPI IWineD3DVolumeImpl_GetParent(IWineD3DVolume *iface, IUnknown **pParent) {
134     return resource_get_parent((IWineD3DResource *)iface, pParent);
135 }
136
137 static HRESULT WINAPI IWineD3DVolumeImpl_GetDevice(IWineD3DVolume *iface, IWineD3DDevice** ppDevice) {
138     return resource_get_device((IWineD3DResource *)iface, ppDevice);
139 }
140
141 static HRESULT WINAPI IWineD3DVolumeImpl_SetPrivateData(IWineD3DVolume *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
142     return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
143 }
144
145 static HRESULT WINAPI IWineD3DVolumeImpl_GetPrivateData(IWineD3DVolume *iface, REFGUID  refguid, void* pData, DWORD* pSizeOfData) {
146     return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
147 }
148
149 static HRESULT WINAPI IWineD3DVolumeImpl_FreePrivateData(IWineD3DVolume *iface, REFGUID refguid) {
150     return resource_free_private_data((IWineD3DResource *)iface, refguid);
151 }
152
153 static DWORD WINAPI IWineD3DVolumeImpl_SetPriority(IWineD3DVolume *iface, DWORD PriorityNew) {
154     return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
155 }
156
157 static DWORD WINAPI IWineD3DVolumeImpl_GetPriority(IWineD3DVolume *iface) {
158     return resource_get_priority((IWineD3DResource *)iface);
159 }
160
161 static void WINAPI IWineD3DVolumeImpl_PreLoad(IWineD3DVolume *iface) {
162     FIXME("iface %p stub!\n", iface);
163 }
164
165 static void WINAPI IWineD3DVolumeImpl_UnLoad(IWineD3DVolume *iface) {
166     /* The whole content is shadowed on This->resource.allocatedMemory, and the
167      * texture name is managed by the VolumeTexture container
168      */
169     TRACE("(%p): Nothing to do\n", iface);
170 }
171
172 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeImpl_GetType(IWineD3DVolume *iface) {
173     return resource_get_type((IWineD3DResource *)iface);
174 }
175
176 /* *******************************************
177    IWineD3DVolume parts follow
178    ******************************************* */
179 static HRESULT WINAPI IWineD3DVolumeImpl_GetContainer(IWineD3DVolume *iface, REFIID riid, void** ppContainer) {
180     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
181
182     TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
183
184     if (!ppContainer) {
185         ERR("Called without a valid ppContainer.\n");
186     }
187
188     /* Although surfaces can be standalone, volumes can't */
189     if (!This->container) {
190         ERR("Volume without an container. Should not happen.\n");
191     }
192
193     TRACE("Relaying to QueryInterface\n");
194     return IUnknown_QueryInterface(This->container, riid, ppContainer);
195 }
196
197 static HRESULT WINAPI IWineD3DVolumeImpl_GetDesc(IWineD3DVolume *iface, WINED3DVOLUME_DESC* pDesc) {
198     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
199     TRACE("(%p) : copying into %p\n", This, pDesc);
200
201     pDesc->Format = This->resource.format_desc->format;
202     pDesc->Type = This->resource.resourceType;
203     pDesc->Usage = This->resource.usage;
204     pDesc->Pool = This->resource.pool;
205     pDesc->Size = This->resource.size; /* dx8 only */
206     pDesc->Width = This->currentDesc.Width;
207     pDesc->Height = This->currentDesc.Height;
208     pDesc->Depth = This->currentDesc.Depth;
209
210     return WINED3D_OK;
211 }
212
213 static HRESULT WINAPI IWineD3DVolumeImpl_LockBox(IWineD3DVolume *iface, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
214     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
215     FIXME("(%p) : pBox=%p stub\n", This, pBox);
216
217     if(!This->resource.allocatedMemory) {
218         This->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size);
219     }
220
221     /* fixme: should we really lock as such? */
222     TRACE("(%p) : box=%p, output pbox=%p, allMem=%p\n", This, pBox, pLockedVolume, This->resource.allocatedMemory);
223
224     pLockedVolume->RowPitch = This->resource.format_desc->byte_count * This->currentDesc.Width; /* Bytes / row   */
225     pLockedVolume->SlicePitch = This->resource.format_desc->byte_count
226             * This->currentDesc.Width * This->currentDesc.Height;                               /* Bytes / slice */
227     if (!pBox) {
228         TRACE("No box supplied - all is ok\n");
229         pLockedVolume->pBits = This->resource.allocatedMemory;
230         This->lockedBox.Left   = 0;
231         This->lockedBox.Top    = 0;
232         This->lockedBox.Front  = 0;
233         This->lockedBox.Right  = This->currentDesc.Width;
234         This->lockedBox.Bottom = This->currentDesc.Height;
235         This->lockedBox.Back   = This->currentDesc.Depth;
236     } else {
237         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);
238         pLockedVolume->pBits = This->resource.allocatedMemory
239                 + (pLockedVolume->SlicePitch * pBox->Front)     /* FIXME: is front < back or vica versa? */
240                 + (pLockedVolume->RowPitch * pBox->Top)
241                 + (pBox->Left * This->resource.format_desc->byte_count);
242         This->lockedBox.Left   = pBox->Left;
243         This->lockedBox.Top    = pBox->Top;
244         This->lockedBox.Front  = pBox->Front;
245         This->lockedBox.Right  = pBox->Right;
246         This->lockedBox.Bottom = pBox->Bottom;
247         This->lockedBox.Back   = pBox->Back;
248     }
249
250     if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
251       /* Don't dirtify */
252     } else {
253       /**
254        * Dirtify on lock
255        * as seen in msdn docs
256        */
257       volume_add_dirty_box(iface, &This->lockedBox);
258
259       /**  Dirtify Container if needed */
260       if (NULL != This->container) {
261
262         IWineD3DVolumeTexture *cont = (IWineD3DVolumeTexture*) This->container;
263         WINED3DRESOURCETYPE containerType = IWineD3DBaseTexture_GetType((IWineD3DBaseTexture *) cont);
264
265         if (containerType == WINED3DRTYPE_VOLUMETEXTURE) {
266           IWineD3DBaseTextureImpl* pTexture = (IWineD3DBaseTextureImpl*) cont;
267           pTexture->baseTexture.dirty = TRUE;
268         } else {
269           FIXME("Set dirty on container type %d\n", containerType);
270         }
271       }
272     }
273
274     This->locked = TRUE;
275     TRACE("returning memory@%p rpitch(%d) spitch(%d)\n", pLockedVolume->pBits, pLockedVolume->RowPitch, pLockedVolume->SlicePitch);
276     return WINED3D_OK;
277 }
278
279 static HRESULT WINAPI IWineD3DVolumeImpl_UnlockBox(IWineD3DVolume *iface) {
280     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
281     if (!This->locked) {
282       ERR("trying to lock unlocked volume@%p\n", This);
283       return WINED3DERR_INVALIDCALL;
284     }
285     TRACE("(%p) : unlocking volume\n", This);
286     This->locked = FALSE;
287     memset(&This->lockedBox, 0, sizeof(RECT));
288     return WINED3D_OK;
289 }
290
291 /* Internal use functions follow : */
292
293 static HRESULT WINAPI IWineD3DVolumeImpl_SetContainer(IWineD3DVolume *iface, IWineD3DBase* container) {
294     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
295
296     TRACE("This %p, container %p\n", This, container);
297
298     /* We can't keep a reference to the container, since the container already keeps a reference to us. */
299
300     TRACE("Setting container to %p from %p\n", container, This->container);
301     This->container = container;
302
303     return WINED3D_OK;
304 }
305
306 /* Context activation is done by the caller. */
307 static HRESULT WINAPI IWineD3DVolumeImpl_LoadTexture(IWineD3DVolume *iface, int gl_level, BOOL srgb_mode) {
308     IWineD3DVolumeImpl *This     = (IWineD3DVolumeImpl *)iface;
309     const struct GlPixelFormatDesc *glDesc = This->resource.format_desc;
310
311     TRACE("(%p) : level %u, format %s (0x%08x)\n", This, gl_level, debug_d3dformat(glDesc->format), glDesc->format);
312
313     volume_bind_and_dirtify(iface);
314
315     TRACE("Calling glTexImage3D %x level=%d, intfmt=%x, w=%d, h=%d,d=%d, 0=%d, glFmt=%x, glType=%x, Mem=%p\n",
316             GL_TEXTURE_3D,
317             gl_level,
318             glDesc->glInternal,
319             This->currentDesc.Width,
320             This->currentDesc.Height,
321             This->currentDesc.Depth,
322             0,
323             glDesc->glFormat,
324             glDesc->glType,
325             This->resource.allocatedMemory);
326
327     ENTER_GL();
328     GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D,
329                 gl_level,
330                 glDesc->glInternal,
331                 This->currentDesc.Width,
332                 This->currentDesc.Height,
333                 This->currentDesc.Depth,
334                 0,
335                 glDesc->glFormat,
336                 glDesc->glType,
337                 This->resource.allocatedMemory));
338     checkGLcall("glTexImage3D");
339     LEAVE_GL();
340
341     /* When adding code releasing This->resource.allocatedMemory to save data keep in mind that
342      * GL_UNPACK_CLIENT_STORAGE_APPLE is enabled by default if supported(GL_APPLE_client_storage).
343      * Thus do not release This->resource.allocatedMemory if GL_APPLE_client_storage is supported.
344      */
345     return WINED3D_OK;
346
347 }
348
349 const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl =
350 {
351     /* IUnknown */
352     IWineD3DVolumeImpl_QueryInterface,
353     IWineD3DVolumeImpl_AddRef,
354     IWineD3DVolumeImpl_Release,
355     /* IWineD3DResource */
356     IWineD3DVolumeImpl_GetParent,
357     IWineD3DVolumeImpl_GetDevice,
358     IWineD3DVolumeImpl_SetPrivateData,
359     IWineD3DVolumeImpl_GetPrivateData,
360     IWineD3DVolumeImpl_FreePrivateData,
361     IWineD3DVolumeImpl_SetPriority,
362     IWineD3DVolumeImpl_GetPriority,
363     IWineD3DVolumeImpl_PreLoad,
364     IWineD3DVolumeImpl_UnLoad,
365     IWineD3DVolumeImpl_GetType,
366     /* IWineD3DVolume */
367     IWineD3DVolumeImpl_GetContainer,
368     IWineD3DVolumeImpl_GetDesc,
369     IWineD3DVolumeImpl_LockBox,
370     IWineD3DVolumeImpl_UnlockBox,
371     /* Internal interface */
372     IWineD3DVolumeImpl_LoadTexture,
373     IWineD3DVolumeImpl_SetContainer
374 };