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