wined3d: Consistently store format bitcounts in BYTEs.
[wine] / dlls / wined3d / volume.c
1 /*
2  * Copyright 2002-2005 Jason Edmeades
3  * Copyright 2002-2005 Raphael Junqueira
4  * Copyright 2005 Oliver Stieber
5  * Copyright 2009-2011 Henri Verbeet for CodeWeavers
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wined3d_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
26
27 /* Context activation is done by the caller. */
28 static void volume_bind_and_dirtify(struct wined3d_volume *volume, const struct wined3d_gl_info *gl_info)
29 {
30     struct wined3d_texture *container = volume->container;
31     DWORD active_sampler;
32
33     /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
34      * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
35      * gl states. The current texture unit should always be a valid one.
36      *
37      * To be more specific, this is tricky because we can implicitly be called
38      * from sampler() in state.c. This means we can't touch anything other than
39      * whatever happens to be the currently active texture, or we would risk
40      * marking already applied sampler states dirty again.
41      *
42      * TODO: Track the current active texture per GL context instead of using glGet
43      */
44     if (gl_info->supported[ARB_MULTITEXTURE])
45     {
46         GLint active_texture;
47         ENTER_GL();
48         glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
49         LEAVE_GL();
50         active_sampler = volume->resource.device->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
51     } else {
52         active_sampler = 0;
53     }
54
55     if (active_sampler != WINED3D_UNMAPPED_STAGE)
56     {
57         IWineD3DDeviceImpl_MarkStateDirty(volume->resource.device, STATE_SAMPLER(active_sampler));
58     }
59
60     container->texture_ops->texture_bind(container, gl_info, FALSE);
61 }
62
63 void volume_add_dirty_box(struct wined3d_volume *volume, const WINED3DBOX *dirty_box)
64 {
65     volume->dirty = TRUE;
66     if (dirty_box)
67     {
68         volume->lockedBox.Left = min(volume->lockedBox.Left, dirty_box->Left);
69         volume->lockedBox.Top = min(volume->lockedBox.Top, dirty_box->Top);
70         volume->lockedBox.Front = min(volume->lockedBox.Front, dirty_box->Front);
71         volume->lockedBox.Right = max(volume->lockedBox.Right, dirty_box->Right);
72         volume->lockedBox.Bottom = max(volume->lockedBox.Bottom, dirty_box->Bottom);
73         volume->lockedBox.Back = max(volume->lockedBox.Back, dirty_box->Back);
74     }
75     else
76     {
77         volume->lockedBox.Left = 0;
78         volume->lockedBox.Top = 0;
79         volume->lockedBox.Front = 0;
80         volume->lockedBox.Right = volume->resource.width;
81         volume->lockedBox.Bottom = volume->resource.height;
82         volume->lockedBox.Back = volume->resource.depth;
83     }
84 }
85
86 void volume_set_container(struct wined3d_volume *volume, struct wined3d_texture *container)
87 {
88     TRACE("volume %p, container %p.\n", volume, container);
89
90     volume->container = container;
91 }
92
93 /* Context activation is done by the caller. */
94 void volume_load(struct wined3d_volume *volume, UINT level, BOOL srgb_mode)
95 {
96     const struct wined3d_gl_info *gl_info = &volume->resource.device->adapter->gl_info;
97     const struct wined3d_format *format = volume->resource.format;
98
99     TRACE("volume %p, level %u, srgb %#x, format %s (%#x).\n",
100             volume, level, srgb_mode, debug_d3dformat(format->id), format->id);
101
102     volume_bind_and_dirtify(volume, gl_info);
103
104     ENTER_GL();
105     GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, level, format->glInternal,
106             volume->resource.width, volume->resource.height, volume->resource.depth,
107             0, format->glFormat, format->glType, volume->resource.allocatedMemory));
108     checkGLcall("glTexImage3D");
109     LEAVE_GL();
110
111     /* When adding code releasing volume->resource.allocatedMemory to save
112      * data keep in mind that GL_UNPACK_CLIENT_STORAGE_APPLE is enabled by
113      * default if supported(GL_APPLE_client_storage). Thus do not release
114      * volume->resource.allocatedMemory if GL_APPLE_client_storage is
115      * supported. */
116 }
117
118 /* Do not call while under the GL lock. */
119 static void volume_unload(struct wined3d_resource *resource)
120 {
121     TRACE("texture %p.\n", resource);
122
123     /* The whole content is shadowed on This->resource.allocatedMemory, and
124      * the texture name is managed by the VolumeTexture container. */
125
126     resource_unload(resource);
127 }
128
129 ULONG CDECL wined3d_volume_incref(struct wined3d_volume *volume)
130 {
131     ULONG refcount;
132
133     if (volume->container)
134     {
135         TRACE("Forwarding to container %p.\n", volume->container);
136         return wined3d_texture_incref(volume->container);
137     }
138
139     refcount = InterlockedIncrement(&volume->resource.ref);
140
141     TRACE("%p increasing refcount to %u.\n", volume, refcount);
142
143     return refcount;
144 }
145
146 /* Do not call while under the GL lock. */
147 ULONG CDECL wined3d_volume_decref(struct wined3d_volume *volume)
148 {
149     ULONG refcount;
150
151     if (volume->container)
152     {
153         TRACE("Forwarding to container %p.\n", volume->container);
154         return wined3d_texture_decref(volume->container);
155     }
156
157     refcount = InterlockedDecrement(&volume->resource.ref);
158
159     TRACE("%p decreasing refcount to %u.\n", volume, refcount);
160
161     if (!refcount)
162     {
163         resource_cleanup(&volume->resource);
164         volume->resource.parent_ops->wined3d_object_destroyed(volume->resource.parent);
165         HeapFree(GetProcessHeap(), 0, volume);
166     }
167
168     return refcount;
169 }
170
171 void * CDECL wined3d_volume_get_parent(const struct wined3d_volume *volume)
172 {
173     TRACE("volume %p.\n", volume);
174
175     return volume->resource.parent;
176 }
177
178 HRESULT CDECL wined3d_volume_set_private_data(struct wined3d_volume *volume,
179         REFGUID guid, const void *data, DWORD data_size, DWORD flags)
180 {
181     return resource_set_private_data(&volume->resource, guid, data, data_size, flags);
182 }
183
184 HRESULT CDECL wined3d_volume_get_private_data(const struct wined3d_volume *volume,
185         REFGUID guid, void *data, DWORD *data_size)
186 {
187     return resource_get_private_data(&volume->resource, guid, data, data_size);
188 }
189
190 HRESULT CDECL wined3d_volume_free_private_data(struct wined3d_volume *volume, REFGUID guid)
191 {
192     return resource_free_private_data(&volume->resource, guid);
193 }
194
195 DWORD CDECL wined3d_volume_set_priority(struct wined3d_volume *volume, DWORD priority)
196 {
197     return resource_set_priority(&volume->resource, priority);
198 }
199
200 DWORD CDECL wined3d_volume_get_priority(const struct wined3d_volume *volume)
201 {
202     return resource_get_priority(&volume->resource);
203 }
204
205 /* Do not call while under the GL lock. */
206 void CDECL wined3d_volume_preload(struct wined3d_volume *volume)
207 {
208     FIXME("volume %p stub!\n", volume);
209 }
210
211 struct wined3d_resource * CDECL wined3d_volume_get_resource(struct wined3d_volume *volume)
212 {
213     TRACE("volume %p.\n", volume);
214
215     return &volume->resource;
216 }
217
218 HRESULT CDECL wined3d_volume_map(struct wined3d_volume *volume,
219         WINED3DLOCKED_BOX *locked_box, const WINED3DBOX *box, DWORD flags)
220 {
221     TRACE("volume %p, locked_box %p, box %p, flags %#x.\n",
222             volume, locked_box, box, flags);
223
224     if (!volume->resource.allocatedMemory)
225         volume->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, volume->resource.size);
226
227     TRACE("allocatedMemory %p.\n", volume->resource.allocatedMemory);
228
229     locked_box->RowPitch = volume->resource.format->byte_count * volume->resource.width; /* Bytes / row */
230     locked_box->SlicePitch = volume->resource.format->byte_count
231             * volume->resource.width * volume->resource.height; /* Bytes / slice */
232     if (!box)
233     {
234         TRACE("No box supplied - all is ok\n");
235         locked_box->pBits = volume->resource.allocatedMemory;
236         volume->lockedBox.Left   = 0;
237         volume->lockedBox.Top    = 0;
238         volume->lockedBox.Front  = 0;
239         volume->lockedBox.Right  = volume->resource.width;
240         volume->lockedBox.Bottom = volume->resource.height;
241         volume->lockedBox.Back   = volume->resource.depth;
242     }
243     else
244     {
245         TRACE("Lock Box (%p) = l %d, t %d, r %d, b %d, fr %d, ba %d\n",
246                 box, box->Left, box->Top, box->Right, box->Bottom, box->Front, box->Back);
247         locked_box->pBits = volume->resource.allocatedMemory
248                 + (locked_box->SlicePitch * box->Front)     /* FIXME: is front < back or vica versa? */
249                 + (locked_box->RowPitch * box->Top)
250                 + (box->Left * volume->resource.format->byte_count);
251         volume->lockedBox.Left   = box->Left;
252         volume->lockedBox.Top    = box->Top;
253         volume->lockedBox.Front  = box->Front;
254         volume->lockedBox.Right  = box->Right;
255         volume->lockedBox.Bottom = box->Bottom;
256         volume->lockedBox.Back   = box->Back;
257     }
258
259     if (!(flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)))
260     {
261         volume_add_dirty_box(volume, &volume->lockedBox);
262         wined3d_texture_set_dirty(volume->container, TRUE);
263     }
264
265     volume->locked = TRUE;
266
267     TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
268             locked_box->pBits, locked_box->RowPitch, locked_box->SlicePitch);
269
270     return WINED3D_OK;
271 }
272
273 struct wined3d_volume * CDECL wined3d_volume_from_resource(struct wined3d_resource *resource)
274 {
275     return volume_from_resource(resource);
276 }
277
278 HRESULT CDECL wined3d_volume_unmap(struct wined3d_volume *volume)
279 {
280     TRACE("volume %p.\n", volume);
281
282     if (!volume->locked)
283     {
284         WARN("Trying to unlock unlocked volume %p.\n", volume);
285         return WINED3DERR_INVALIDCALL;
286     }
287
288     volume->locked = FALSE;
289     memset(&volume->lockedBox, 0, sizeof(volume->lockedBox));
290
291     return WINED3D_OK;
292 }
293
294 static const struct wined3d_resource_ops volume_resource_ops =
295 {
296     volume_unload,
297 };
298
299 static HRESULT volume_init(struct wined3d_volume *volume, IWineD3DDeviceImpl *device, UINT width,
300         UINT height, UINT depth, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
301         void *parent, const struct wined3d_parent_ops *parent_ops)
302 {
303     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
304     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
305     HRESULT hr;
306
307     if (!gl_info->supported[EXT_TEXTURE3D])
308     {
309         WARN("Volume cannot be created - no volume texture support.\n");
310         return WINED3DERR_INVALIDCALL;
311     }
312
313     hr = resource_init(&volume->resource, device, WINED3DRTYPE_VOLUME, format,
314             WINED3DMULTISAMPLE_NONE, 0, usage, pool, width, height, depth,
315             width * height * depth * format->byte_count, parent, parent_ops,
316             &volume_resource_ops);
317     if (FAILED(hr))
318     {
319         WARN("Failed to initialize resource, returning %#x.\n", hr);
320         return hr;
321     }
322
323     volume->lockable = TRUE;
324     volume->locked = FALSE;
325     memset(&volume->lockedBox, 0, sizeof(volume->lockedBox));
326     volume->dirty = TRUE;
327
328     volume_add_dirty_box(volume, NULL);
329
330     return WINED3D_OK;
331 }
332
333 HRESULT CDECL wined3d_volume_create(IWineD3DDevice *iface, UINT width, UINT height,
334         UINT depth, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool, void *parent,
335         const struct wined3d_parent_ops *parent_ops, struct wined3d_volume **volume)
336 {
337     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)iface;
338     struct wined3d_volume *object;
339     HRESULT hr;
340
341     TRACE("device %p, width %u, height %u, depth %u, usage %#x, format %s, pool %s\n",
342             device, width, height, depth, usage, debug_d3dformat(format_id), debug_d3dpool(pool));
343     TRACE("parent %p, parent_ops %p, volume %p.\n", parent, parent_ops, volume);
344
345     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
346     if (!object)
347     {
348         ERR("Out of memory\n");
349         *volume = NULL;
350         return WINED3DERR_OUTOFVIDEOMEMORY;
351     }
352
353     hr = volume_init(object, device, width, height, depth, usage, format_id, pool, parent, parent_ops);
354     if (FAILED(hr))
355     {
356         WARN("Failed to initialize volume, returning %#x.\n", hr);
357         HeapFree(GetProcessHeap(), 0, object);
358         return hr;
359     }
360
361     TRACE("Created volume %p.\n", object);
362     *volume = object;
363
364     return WINED3D_OK;
365 }