wined3d: Pass around the context instead of gl_info.
[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(const struct wined3d_volume *volume, struct wined3d_context *context)
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 (context->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         device_invalidate_state(volume->resource.device, STATE_SAMPLER(active_sampler));
57
58     container->texture_ops->texture_bind(container, context, FALSE);
59 }
60
61 void volume_add_dirty_box(struct wined3d_volume *volume, const WINED3DBOX *dirty_box)
62 {
63     volume->dirty = TRUE;
64     if (dirty_box)
65     {
66         volume->lockedBox.Left = min(volume->lockedBox.Left, dirty_box->Left);
67         volume->lockedBox.Top = min(volume->lockedBox.Top, dirty_box->Top);
68         volume->lockedBox.Front = min(volume->lockedBox.Front, dirty_box->Front);
69         volume->lockedBox.Right = max(volume->lockedBox.Right, dirty_box->Right);
70         volume->lockedBox.Bottom = max(volume->lockedBox.Bottom, dirty_box->Bottom);
71         volume->lockedBox.Back = max(volume->lockedBox.Back, dirty_box->Back);
72     }
73     else
74     {
75         volume->lockedBox.Left = 0;
76         volume->lockedBox.Top = 0;
77         volume->lockedBox.Front = 0;
78         volume->lockedBox.Right = volume->resource.width;
79         volume->lockedBox.Bottom = volume->resource.height;
80         volume->lockedBox.Back = volume->resource.depth;
81     }
82 }
83
84 void volume_set_container(struct wined3d_volume *volume, struct wined3d_texture *container)
85 {
86     TRACE("volume %p, container %p.\n", volume, container);
87
88     volume->container = container;
89 }
90
91 /* Context activation is done by the caller. */
92 void volume_load(const struct wined3d_volume *volume, struct wined3d_context *context, UINT level, BOOL srgb_mode)
93 {
94     const struct wined3d_gl_info *gl_info = context->gl_info;
95     const struct wined3d_format *format = volume->resource.format;
96
97     TRACE("volume %p, context %p, level %u, srgb %#x, format %s (%#x).\n",
98             volume, context, level, srgb_mode, debug_d3dformat(format->id), format->id);
99
100     volume_bind_and_dirtify(volume, context);
101
102     ENTER_GL();
103     GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, level, format->glInternal,
104             volume->resource.width, volume->resource.height, volume->resource.depth,
105             0, format->glFormat, format->glType, volume->resource.allocatedMemory));
106     checkGLcall("glTexImage3D");
107     LEAVE_GL();
108
109     /* When adding code releasing volume->resource.allocatedMemory to save
110      * data keep in mind that GL_UNPACK_CLIENT_STORAGE_APPLE is enabled by
111      * default if supported(GL_APPLE_client_storage). Thus do not release
112      * volume->resource.allocatedMemory if GL_APPLE_client_storage is
113      * supported. */
114 }
115
116 /* Do not call while under the GL lock. */
117 static void volume_unload(struct wined3d_resource *resource)
118 {
119     TRACE("texture %p.\n", resource);
120
121     /* The whole content is shadowed on This->resource.allocatedMemory, and
122      * the texture name is managed by the VolumeTexture container. */
123
124     resource_unload(resource);
125 }
126
127 ULONG CDECL wined3d_volume_incref(struct wined3d_volume *volume)
128 {
129     ULONG refcount;
130
131     if (volume->container)
132     {
133         TRACE("Forwarding to container %p.\n", volume->container);
134         return wined3d_texture_incref(volume->container);
135     }
136
137     refcount = InterlockedIncrement(&volume->resource.ref);
138
139     TRACE("%p increasing refcount to %u.\n", volume, refcount);
140
141     return refcount;
142 }
143
144 /* Do not call while under the GL lock. */
145 ULONG CDECL wined3d_volume_decref(struct wined3d_volume *volume)
146 {
147     ULONG refcount;
148
149     if (volume->container)
150     {
151         TRACE("Forwarding to container %p.\n", volume->container);
152         return wined3d_texture_decref(volume->container);
153     }
154
155     refcount = InterlockedDecrement(&volume->resource.ref);
156
157     TRACE("%p decreasing refcount to %u.\n", volume, refcount);
158
159     if (!refcount)
160     {
161         resource_cleanup(&volume->resource);
162         volume->resource.parent_ops->wined3d_object_destroyed(volume->resource.parent);
163         HeapFree(GetProcessHeap(), 0, volume);
164     }
165
166     return refcount;
167 }
168
169 void * CDECL wined3d_volume_get_parent(const struct wined3d_volume *volume)
170 {
171     TRACE("volume %p.\n", volume);
172
173     return volume->resource.parent;
174 }
175
176 DWORD CDECL wined3d_volume_set_priority(struct wined3d_volume *volume, DWORD priority)
177 {
178     return resource_set_priority(&volume->resource, priority);
179 }
180
181 DWORD CDECL wined3d_volume_get_priority(const struct wined3d_volume *volume)
182 {
183     return resource_get_priority(&volume->resource);
184 }
185
186 /* Do not call while under the GL lock. */
187 void CDECL wined3d_volume_preload(struct wined3d_volume *volume)
188 {
189     FIXME("volume %p stub!\n", volume);
190 }
191
192 struct wined3d_resource * CDECL wined3d_volume_get_resource(struct wined3d_volume *volume)
193 {
194     TRACE("volume %p.\n", volume);
195
196     return &volume->resource;
197 }
198
199 HRESULT CDECL wined3d_volume_map(struct wined3d_volume *volume,
200         WINED3DLOCKED_BOX *locked_box, const WINED3DBOX *box, DWORD flags)
201 {
202     TRACE("volume %p, locked_box %p, box %p, flags %#x.\n",
203             volume, locked_box, box, flags);
204
205     if (!volume->resource.allocatedMemory)
206         volume->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, volume->resource.size);
207
208     TRACE("allocatedMemory %p.\n", volume->resource.allocatedMemory);
209
210     locked_box->RowPitch = volume->resource.format->byte_count * volume->resource.width; /* Bytes / row */
211     locked_box->SlicePitch = volume->resource.format->byte_count
212             * volume->resource.width * volume->resource.height; /* Bytes / slice */
213     if (!box)
214     {
215         TRACE("No box supplied - all is ok\n");
216         locked_box->pBits = volume->resource.allocatedMemory;
217         volume->lockedBox.Left   = 0;
218         volume->lockedBox.Top    = 0;
219         volume->lockedBox.Front  = 0;
220         volume->lockedBox.Right  = volume->resource.width;
221         volume->lockedBox.Bottom = volume->resource.height;
222         volume->lockedBox.Back   = volume->resource.depth;
223     }
224     else
225     {
226         TRACE("Lock Box (%p) = l %d, t %d, r %d, b %d, fr %d, ba %d\n",
227                 box, box->Left, box->Top, box->Right, box->Bottom, box->Front, box->Back);
228         locked_box->pBits = volume->resource.allocatedMemory
229                 + (locked_box->SlicePitch * box->Front)     /* FIXME: is front < back or vica versa? */
230                 + (locked_box->RowPitch * box->Top)
231                 + (box->Left * volume->resource.format->byte_count);
232         volume->lockedBox.Left   = box->Left;
233         volume->lockedBox.Top    = box->Top;
234         volume->lockedBox.Front  = box->Front;
235         volume->lockedBox.Right  = box->Right;
236         volume->lockedBox.Bottom = box->Bottom;
237         volume->lockedBox.Back   = box->Back;
238     }
239
240     if (!(flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)))
241     {
242         volume_add_dirty_box(volume, &volume->lockedBox);
243         wined3d_texture_set_dirty(volume->container, TRUE);
244     }
245
246     volume->locked = TRUE;
247
248     TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
249             locked_box->pBits, locked_box->RowPitch, locked_box->SlicePitch);
250
251     return WINED3D_OK;
252 }
253
254 struct wined3d_volume * CDECL wined3d_volume_from_resource(struct wined3d_resource *resource)
255 {
256     return volume_from_resource(resource);
257 }
258
259 HRESULT CDECL wined3d_volume_unmap(struct wined3d_volume *volume)
260 {
261     TRACE("volume %p.\n", volume);
262
263     if (!volume->locked)
264     {
265         WARN("Trying to unlock unlocked volume %p.\n", volume);
266         return WINED3DERR_INVALIDCALL;
267     }
268
269     volume->locked = FALSE;
270     memset(&volume->lockedBox, 0, sizeof(volume->lockedBox));
271
272     return WINED3D_OK;
273 }
274
275 static const struct wined3d_resource_ops volume_resource_ops =
276 {
277     volume_unload,
278 };
279
280 static HRESULT volume_init(struct wined3d_volume *volume, struct wined3d_device *device, UINT width,
281         UINT height, UINT depth, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
282         void *parent, const struct wined3d_parent_ops *parent_ops)
283 {
284     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
285     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
286     HRESULT hr;
287
288     if (!gl_info->supported[EXT_TEXTURE3D])
289     {
290         WARN("Volume cannot be created - no volume texture support.\n");
291         return WINED3DERR_INVALIDCALL;
292     }
293
294     hr = resource_init(&volume->resource, device, WINED3DRTYPE_VOLUME, format,
295             WINED3DMULTISAMPLE_NONE, 0, usage, pool, width, height, depth,
296             width * height * depth * format->byte_count, parent, parent_ops,
297             &volume_resource_ops);
298     if (FAILED(hr))
299     {
300         WARN("Failed to initialize resource, returning %#x.\n", hr);
301         return hr;
302     }
303
304     volume->lockable = TRUE;
305     volume->locked = FALSE;
306     memset(&volume->lockedBox, 0, sizeof(volume->lockedBox));
307     volume->dirty = TRUE;
308
309     volume_add_dirty_box(volume, NULL);
310
311     return WINED3D_OK;
312 }
313
314 HRESULT CDECL wined3d_volume_create(struct wined3d_device *device, UINT width, UINT height,
315         UINT depth, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool, void *parent,
316         const struct wined3d_parent_ops *parent_ops, struct wined3d_volume **volume)
317 {
318     struct wined3d_volume *object;
319     HRESULT hr;
320
321     TRACE("device %p, width %u, height %u, depth %u, usage %#x, format %s, pool %s\n",
322             device, width, height, depth, usage, debug_d3dformat(format_id), debug_d3dpool(pool));
323     TRACE("parent %p, parent_ops %p, volume %p.\n", parent, parent_ops, volume);
324
325     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
326     if (!object)
327     {
328         ERR("Out of memory\n");
329         *volume = NULL;
330         return WINED3DERR_OUTOFVIDEOMEMORY;
331     }
332
333     hr = volume_init(object, device, width, height, depth, usage, format_id, pool, parent, parent_ops);
334     if (FAILED(hr))
335     {
336         WARN("Failed to initialize volume, returning %#x.\n", hr);
337         HeapFree(GetProcessHeap(), 0, object);
338         return hr;
339     }
340
341     TRACE("Created volume %p.\n", object);
342     *volume = object;
343
344     return WINED3D_OK;
345 }