wined3d: Explicitly pass shader_data and reg_maps to reserved_vs_const().
[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     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_info->supported[ARB_MULTITEXTURE])
47     {
48         GLint active_texture;
49         ENTER_GL();
50         glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
51         LEAVE_GL();
52         active_sampler = This->resource.device->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.device, STATE_SAMPLER(active_sampler));
60     }
61
62     IWineD3DVolumeTexture_BindTexture((IWineD3DVolumeTexture *)This->container, FALSE);
63 }
64
65 void volume_add_dirty_box(IWineD3DVolume *iface, const WINED3DBOX *dirty_box)
66 {
67     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
68
69     This->dirty = TRUE;
70     if (dirty_box)
71     {
72         This->lockedBox.Left = min(This->lockedBox.Left, dirty_box->Left);
73         This->lockedBox.Top = min(This->lockedBox.Top, dirty_box->Top);
74         This->lockedBox.Front = min(This->lockedBox.Front, dirty_box->Front);
75         This->lockedBox.Right = max(This->lockedBox.Right, dirty_box->Right);
76         This->lockedBox.Bottom = max(This->lockedBox.Bottom, dirty_box->Bottom);
77         This->lockedBox.Back = max(This->lockedBox.Back, dirty_box->Back);
78     }
79     else
80     {
81         This->lockedBox.Left = 0;
82         This->lockedBox.Top = 0;
83         This->lockedBox.Front = 0;
84         This->lockedBox.Right = This->currentDesc.Width;
85         This->lockedBox.Bottom = This->currentDesc.Height;
86         This->lockedBox.Back = This->currentDesc.Depth;
87     }
88 }
89
90 void volume_set_container(IWineD3DVolumeImpl *volume, struct IWineD3DVolumeTextureImpl *container)
91 {
92     TRACE("volume %p, container %p.\n", volume, container);
93
94     volume->container = container;
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 /* Do not call while under the GL lock. */
127 static ULONG WINAPI IWineD3DVolumeImpl_Release(IWineD3DVolume *iface) {
128     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
129     ULONG ref;
130     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
131     ref = InterlockedDecrement(&This->resource.ref);
132
133     if (!ref)
134     {
135         resource_cleanup((IWineD3DResource *)iface);
136         This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
137         HeapFree(GetProcessHeap(), 0, This);
138     }
139     return ref;
140 }
141
142 /* ****************************************************
143    IWineD3DVolume IWineD3DResource parts follow
144    **************************************************** */
145 static void * WINAPI IWineD3DVolumeImpl_GetParent(IWineD3DVolume *iface)
146 {
147     TRACE("iface %p.\n", iface);
148
149     return ((IWineD3DVolumeImpl *)iface)->resource.parent;
150 }
151
152 static HRESULT WINAPI IWineD3DVolumeImpl_SetPrivateData(IWineD3DVolume *iface,
153         REFGUID riid, const void *data, DWORD data_size, DWORD flags)
154 {
155     return resource_set_private_data((IWineD3DResource *)iface, riid, data, data_size, flags);
156 }
157
158 static HRESULT WINAPI IWineD3DVolumeImpl_GetPrivateData(IWineD3DVolume *iface, REFGUID  refguid, void* pData, DWORD* pSizeOfData) {
159     return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
160 }
161
162 static HRESULT WINAPI IWineD3DVolumeImpl_FreePrivateData(IWineD3DVolume *iface, REFGUID refguid) {
163     return resource_free_private_data((IWineD3DResource *)iface, refguid);
164 }
165
166 static DWORD WINAPI IWineD3DVolumeImpl_SetPriority(IWineD3DVolume *iface, DWORD PriorityNew) {
167     return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
168 }
169
170 static DWORD WINAPI IWineD3DVolumeImpl_GetPriority(IWineD3DVolume *iface) {
171     return resource_get_priority((IWineD3DResource *)iface);
172 }
173
174 /* Do not call while under the GL lock. */
175 static void WINAPI IWineD3DVolumeImpl_PreLoad(IWineD3DVolume *iface) {
176     FIXME("iface %p stub!\n", iface);
177 }
178
179 /* Do not call while under the GL lock. */
180 static void WINAPI IWineD3DVolumeImpl_UnLoad(IWineD3DVolume *iface)
181 {
182     TRACE("iface %p.\n", iface);
183
184     /* The whole content is shadowed on This->resource.allocatedMemory, and
185      * the texture name is managed by the VolumeTexture container. */
186
187     resource_unload((IWineD3DResourceImpl *)iface);
188 }
189
190 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeImpl_GetType(IWineD3DVolume *iface) {
191     return resource_get_type((IWineD3DResource *)iface);
192 }
193
194 static void WINAPI IWineD3DVolumeImpl_GetDesc(IWineD3DVolume *iface, WINED3DVOLUME_DESC *desc)
195 {
196     IWineD3DVolumeImpl *volume = (IWineD3DVolumeImpl *)iface;
197
198     TRACE("iface %p, desc %p.\n", iface, desc);
199
200     desc->Format = volume->resource.format->id;
201     desc->Type = volume->resource.resourceType;
202     desc->Usage = volume->resource.usage;
203     desc->Pool = volume->resource.pool;
204     desc->Size = volume->resource.size; /* dx8 only */
205     desc->Width = volume->currentDesc.Width;
206     desc->Height = volume->currentDesc.Height;
207     desc->Depth = volume->currentDesc.Depth;
208 }
209
210 static HRESULT WINAPI IWineD3DVolumeImpl_Map(IWineD3DVolume *iface,
211         WINED3DLOCKED_BOX *pLockedVolume, const WINED3DBOX *pBox, DWORD flags)
212 {
213     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
214     FIXME("(%p) : pBox=%p stub\n", This, pBox);
215
216     if(!This->resource.allocatedMemory) {
217         This->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size);
218     }
219
220     /* fixme: should we really lock as such? */
221     TRACE("(%p) : box=%p, output pbox=%p, allMem=%p\n", This, pBox, pLockedVolume, This->resource.allocatedMemory);
222
223     pLockedVolume->RowPitch = This->resource.format->byte_count * This->currentDesc.Width; /* Bytes / row   */
224     pLockedVolume->SlicePitch = This->resource.format->byte_count
225             * This->currentDesc.Width * This->currentDesc.Height;                               /* Bytes / slice */
226     if (!pBox) {
227         TRACE("No box supplied - all is ok\n");
228         pLockedVolume->pBits = This->resource.allocatedMemory;
229         This->lockedBox.Left   = 0;
230         This->lockedBox.Top    = 0;
231         This->lockedBox.Front  = 0;
232         This->lockedBox.Right  = This->currentDesc.Width;
233         This->lockedBox.Bottom = This->currentDesc.Height;
234         This->lockedBox.Back   = This->currentDesc.Depth;
235     } else {
236         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);
237         pLockedVolume->pBits = This->resource.allocatedMemory
238                 + (pLockedVolume->SlicePitch * pBox->Front)     /* FIXME: is front < back or vica versa? */
239                 + (pLockedVolume->RowPitch * pBox->Top)
240                 + (pBox->Left * This->resource.format->byte_count);
241         This->lockedBox.Left   = pBox->Left;
242         This->lockedBox.Top    = pBox->Top;
243         This->lockedBox.Front  = pBox->Front;
244         This->lockedBox.Right  = pBox->Right;
245         This->lockedBox.Bottom = pBox->Bottom;
246         This->lockedBox.Back   = pBox->Back;
247     }
248
249     if (!(flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)))
250     {
251         volume_add_dirty_box(iface, &This->lockedBox);
252         This->container->baseTexture.texture_rgb.dirty = TRUE;
253         This->container->baseTexture.texture_srgb.dirty = TRUE;
254     }
255
256     This->locked = TRUE;
257     TRACE("returning memory@%p rpitch(%d) spitch(%d)\n", pLockedVolume->pBits, pLockedVolume->RowPitch, pLockedVolume->SlicePitch);
258     return WINED3D_OK;
259 }
260
261 static HRESULT WINAPI IWineD3DVolumeImpl_Unmap(IWineD3DVolume *iface)
262 {
263     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
264     if (!This->locked)
265     {
266         WARN("Trying to unlock unlocked volume %p.\n", iface);
267         return WINED3DERR_INVALIDCALL;
268     }
269     TRACE("(%p) : unlocking volume\n", This);
270     This->locked = FALSE;
271     memset(&This->lockedBox, 0, sizeof(RECT));
272     return WINED3D_OK;
273 }
274
275 /* Internal use functions follow : */
276
277 /* Context activation is done by the caller. */
278 static HRESULT WINAPI IWineD3DVolumeImpl_LoadTexture(IWineD3DVolume *iface, int gl_level, BOOL srgb_mode)
279 {
280     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
281     const struct wined3d_gl_info *gl_info = &This->resource.device->adapter->gl_info;
282     const struct wined3d_format *format = This->resource.format;
283
284     TRACE("iface %p, level %u, srgb %#x, format %s (%#x).\n",
285             iface, gl_level, srgb_mode, debug_d3dformat(format->id), format->id);
286
287     volume_bind_and_dirtify(iface);
288
289     TRACE("Calling glTexImage3D %x level=%d, intfmt=%x, w=%d, h=%d,d=%d, 0=%d, glFmt=%x, glType=%x, Mem=%p\n",
290             GL_TEXTURE_3D, gl_level, format->glInternal, This->currentDesc.Width, This->currentDesc.Height,
291             This->currentDesc.Depth, 0, format->glFormat, format->glType, This->resource.allocatedMemory);
292
293     ENTER_GL();
294     GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, gl_level, format->glInternal,
295             This->currentDesc.Width, This->currentDesc.Height, This->currentDesc.Depth,
296             0, format->glFormat, format->glType, This->resource.allocatedMemory));
297     checkGLcall("glTexImage3D");
298     LEAVE_GL();
299
300     /* When adding code releasing This->resource.allocatedMemory to save data keep in mind that
301      * GL_UNPACK_CLIENT_STORAGE_APPLE is enabled by default if supported(GL_APPLE_client_storage).
302      * Thus do not release This->resource.allocatedMemory if GL_APPLE_client_storage is supported.
303      */
304     return WINED3D_OK;
305
306 }
307
308 static const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl =
309 {
310     /* IUnknown */
311     IWineD3DVolumeImpl_QueryInterface,
312     IWineD3DVolumeImpl_AddRef,
313     IWineD3DVolumeImpl_Release,
314     /* IWineD3DResource */
315     IWineD3DVolumeImpl_GetParent,
316     IWineD3DVolumeImpl_SetPrivateData,
317     IWineD3DVolumeImpl_GetPrivateData,
318     IWineD3DVolumeImpl_FreePrivateData,
319     IWineD3DVolumeImpl_SetPriority,
320     IWineD3DVolumeImpl_GetPriority,
321     IWineD3DVolumeImpl_PreLoad,
322     IWineD3DVolumeImpl_UnLoad,
323     IWineD3DVolumeImpl_GetType,
324     /* IWineD3DVolume */
325     IWineD3DVolumeImpl_GetDesc,
326     IWineD3DVolumeImpl_Map,
327     IWineD3DVolumeImpl_Unmap,
328     /* Internal interface */
329     IWineD3DVolumeImpl_LoadTexture,
330 };
331
332 HRESULT volume_init(IWineD3DVolumeImpl *volume, IWineD3DDeviceImpl *device, UINT width,
333         UINT height, UINT depth, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
334         void *parent, const struct wined3d_parent_ops *parent_ops)
335 {
336     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
337     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
338     HRESULT hr;
339
340     if (!gl_info->supported[EXT_TEXTURE3D])
341     {
342         WARN("Volume cannot be created - no volume texture support.\n");
343         return WINED3DERR_INVALIDCALL;
344     }
345
346     volume->lpVtbl = &IWineD3DVolume_Vtbl;
347
348     hr = resource_init((IWineD3DResource *)volume, WINED3DRTYPE_VOLUME, device,
349             width * height * depth * format->byte_count, usage, format, pool, parent, parent_ops);
350     if (FAILED(hr))
351     {
352         WARN("Failed to initialize resource, returning %#x.\n", hr);
353         return hr;
354     }
355
356     volume->currentDesc.Width = width;
357     volume->currentDesc.Height = height;
358     volume->currentDesc.Depth = depth;
359     volume->lockable = TRUE;
360     volume->locked = FALSE;
361     memset(&volume->lockedBox, 0, sizeof(volume->lockedBox));
362     volume->dirty = TRUE;
363
364     volume_add_dirty_box((IWineD3DVolume *)volume, NULL);
365
366     return WINED3D_OK;
367 }