vbscript: Added support for title and type arguments of MsgBox.
[wine.git] / dlls / wined3d / volume.c
blob994cfc2fd7f3ee8214d9495341f7c6c2d08f09ce
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 * Copyright 2013 Stefan Dösinger for CodeWeavers
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.
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.
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
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
28 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
30 void volume_set_container(struct wined3d_volume *volume, struct wined3d_texture *container)
32 TRACE("volume %p, container %p.\n", volume, container);
34 volume->container = container;
37 static BOOL volume_prepare_system_memory(struct wined3d_volume *volume)
39 if (volume->resource.heap_memory)
40 return TRUE;
42 if (!wined3d_resource_allocate_sysmem(&volume->resource))
44 ERR("Failed to allocate system memory.\n");
45 return FALSE;
47 return TRUE;
50 /* Context activation is done by the caller. */
51 static void wined3d_volume_allocate_texture(struct wined3d_volume *volume,
52 const struct wined3d_context *context, BOOL srgb)
54 const struct wined3d_gl_info *gl_info = context->gl_info;
55 const struct wined3d_format *format = volume->resource.format;
56 void *mem = NULL;
58 if (gl_info->supported[APPLE_CLIENT_STORAGE] && !format->convert
59 && volume_prepare_system_memory(volume))
61 TRACE("Enabling GL_UNPACK_CLIENT_STORAGE_APPLE for volume %p\n", volume);
62 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
63 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
64 mem = volume->resource.heap_memory;
65 volume->flags |= WINED3D_VFLAG_CLIENT_STORAGE;
68 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, volume->texture_level,
69 srgb ? format->glGammaInternal : format->glInternal,
70 volume->resource.width, volume->resource.height, volume->resource.depth,
71 0, format->glFormat, format->glType, mem));
72 checkGLcall("glTexImage3D");
74 if (mem)
76 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
77 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
81 static void wined3d_volume_get_pitch(const struct wined3d_volume *volume, UINT *row_pitch,
82 UINT *slice_pitch)
84 const struct wined3d_format *format = volume->resource.format;
86 if (format->flags & WINED3DFMT_FLAG_BLOCKS)
88 /* Since compressed formats are block based, pitch means the amount of
89 * bytes to the next row of block rather than the next row of pixels. */
90 UINT row_block_count = (volume->resource.width + format->block_width - 1) / format->block_width;
91 UINT slice_block_count = (volume->resource.height + format->block_height - 1) / format->block_height;
92 *row_pitch = row_block_count * format->block_byte_count;
93 *slice_pitch = *row_pitch * slice_block_count;
95 else
97 unsigned char alignment = volume->resource.device->surface_alignment;
98 *row_pitch = format->byte_count * volume->resource.width; /* Bytes / row */
99 *row_pitch = (*row_pitch + alignment - 1) & ~(alignment - 1);
100 *slice_pitch = *row_pitch * volume->resource.height;
103 TRACE("Returning row pitch %u, slice pitch %u.\n", *row_pitch, *slice_pitch);
106 /* Context activation is done by the caller. */
107 void wined3d_volume_upload_data(struct wined3d_volume *volume, const struct wined3d_context *context,
108 const struct wined3d_bo_address *data)
110 const struct wined3d_gl_info *gl_info = context->gl_info;
111 const struct wined3d_format *format = volume->resource.format;
112 UINT width = volume->resource.width;
113 UINT height = volume->resource.height;
114 UINT depth = volume->resource.depth;
115 BYTE *mem = data->addr;
117 TRACE("volume %p, context %p, level %u, format %s (%#x).\n",
118 volume, context, volume->texture_level, debug_d3dformat(format->id),
119 format->id);
121 if (format->convert)
123 UINT dst_row_pitch, dst_slice_pitch;
124 UINT src_row_pitch, src_slice_pitch;
125 UINT alignment = volume->resource.device->surface_alignment;
127 if (data->buffer_object)
128 ERR("Loading a converted volume from a PBO.\n");
129 if (format->flags & WINED3DFMT_FLAG_BLOCKS)
130 ERR("Converting a block-based format.\n");
132 dst_row_pitch = width * format->conv_byte_count;
133 dst_row_pitch = (dst_row_pitch + alignment - 1) & ~(alignment - 1);
134 dst_slice_pitch = dst_row_pitch * height;
136 wined3d_volume_get_pitch(volume, &src_row_pitch, &src_slice_pitch);
138 mem = HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch * depth);
139 format->convert(data->addr, mem, src_row_pitch, src_slice_pitch,
140 dst_row_pitch, dst_slice_pitch, width, height, depth);
143 if (data->buffer_object)
145 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, data->buffer_object));
146 checkGLcall("glBindBufferARB");
149 GL_EXTCALL(glTexSubImage3DEXT(GL_TEXTURE_3D, volume->texture_level, 0, 0, 0,
150 width, height, depth,
151 format->glFormat, format->glType, mem));
152 checkGLcall("glTexSubImage3D");
154 if (data->buffer_object)
156 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
157 checkGLcall("glBindBufferARB");
160 if (mem != data->addr)
161 HeapFree(GetProcessHeap(), 0, mem);
164 static void wined3d_volume_validate_location(struct wined3d_volume *volume, DWORD location)
166 TRACE("Volume %p, setting %s.\n", volume, wined3d_debug_location(location));
167 volume->locations |= location;
168 TRACE("new location flags are %s.\n", wined3d_debug_location(volume->locations));
171 void wined3d_volume_invalidate_location(struct wined3d_volume *volume, DWORD location)
173 TRACE("Volume %p, clearing %s.\n", volume, wined3d_debug_location(location));
174 volume->locations &= ~location;
175 TRACE("new location flags are %s.\n", wined3d_debug_location(volume->locations));
178 /* Context activation is done by the caller. */
179 static void wined3d_volume_download_data(struct wined3d_volume *volume,
180 const struct wined3d_context *context, const struct wined3d_bo_address *data)
182 const struct wined3d_gl_info *gl_info = context->gl_info;
183 const struct wined3d_format *format = volume->resource.format;
185 if (format->convert)
187 FIXME("Attempting to download a converted volume, format %s.\n",
188 debug_d3dformat(format->id));
189 return;
192 if (data->buffer_object)
194 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, data->buffer_object));
195 checkGLcall("glBindBufferARB");
198 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, volume->texture_level,
199 format->glFormat, format->glType, data->addr);
200 checkGLcall("glGetTexImage");
202 if (data->buffer_object)
204 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
205 checkGLcall("glBindBufferARB");
210 static void wined3d_volume_evict_sysmem(struct wined3d_volume *volume)
212 wined3d_resource_free_sysmem(&volume->resource);
213 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_SYSMEM);
216 static DWORD volume_access_from_location(DWORD location)
218 switch (location)
220 case WINED3D_LOCATION_DISCARDED:
221 return 0;
223 case WINED3D_LOCATION_SYSMEM:
224 return WINED3D_RESOURCE_ACCESS_CPU;
226 case WINED3D_LOCATION_BUFFER:
227 case WINED3D_LOCATION_TEXTURE_RGB:
228 case WINED3D_LOCATION_TEXTURE_SRGB:
229 return WINED3D_RESOURCE_ACCESS_GPU;
231 default:
232 FIXME("Unhandled location %#x.\n", location);
233 return 0;
237 /* Context activation is done by the caller. */
238 static void wined3d_volume_srgb_transfer(struct wined3d_volume *volume,
239 struct wined3d_context *context, BOOL dest_is_srgb)
241 struct wined3d_bo_address data;
242 /* Optimizations are possible, but the effort should be put into either
243 * implementing EXT_SRGB_DECODE in the driver or finding out why we
244 * picked the wrong copy for the original upload and fixing that.
246 * Also keep in mind that we want to avoid using resource.heap_memory
247 * for DEFAULT pool surfaces. */
249 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
250 data.buffer_object = 0;
251 data.addr = HeapAlloc(GetProcessHeap(), 0, volume->resource.size);
252 if (!data.addr)
253 return;
255 wined3d_texture_bind_and_dirtify(volume->container, context, !dest_is_srgb);
256 wined3d_volume_download_data(volume, context, &data);
257 wined3d_texture_bind_and_dirtify(volume->container, context, dest_is_srgb);
258 wined3d_volume_upload_data(volume, context, &data);
260 HeapFree(GetProcessHeap(), 0, data.addr);
263 static BOOL wined3d_volume_can_evict(const struct wined3d_volume *volume)
265 if (volume->resource.pool != WINED3D_POOL_MANAGED)
266 return FALSE;
267 if (volume->download_count >= 10)
268 return FALSE;
269 if (volume->resource.format->convert)
270 return FALSE;
271 if (volume->flags & WINED3D_VFLAG_CLIENT_STORAGE)
272 return FALSE;
274 return TRUE;
276 /* Context activation is done by the caller. */
277 static void wined3d_volume_load_location(struct wined3d_volume *volume,
278 struct wined3d_context *context, DWORD location)
280 DWORD required_access = volume_access_from_location(location);
282 TRACE("Volume %p, loading %s, have %s.\n", volume, wined3d_debug_location(location),
283 wined3d_debug_location(volume->locations));
285 if ((volume->locations & location) == location)
287 TRACE("Location(s) already up to date.\n");
288 return;
291 if ((volume->resource.access_flags & required_access) != required_access)
293 ERR("Operation requires %#x access, but volume only has %#x.\n",
294 required_access, volume->resource.access_flags);
295 return;
298 switch (location)
300 case WINED3D_LOCATION_TEXTURE_RGB:
301 case WINED3D_LOCATION_TEXTURE_SRGB:
302 if ((location == WINED3D_LOCATION_TEXTURE_RGB
303 && !(volume->flags & WINED3D_VFLAG_ALLOCATED))
304 || (location == WINED3D_LOCATION_TEXTURE_SRGB
305 && !(volume->flags & WINED3D_VFLAG_SRGB_ALLOCATED)))
306 ERR("Trying to load (s)RGB texture without prior allocation.\n");
308 if (volume->locations & WINED3D_LOCATION_DISCARDED)
310 TRACE("Volume previously discarded, nothing to do.\n");
311 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
313 else if (volume->locations & WINED3D_LOCATION_SYSMEM)
315 struct wined3d_bo_address data = {0, volume->resource.heap_memory};
316 wined3d_volume_upload_data(volume, context, &data);
318 else if (volume->locations & WINED3D_LOCATION_BUFFER)
320 struct wined3d_bo_address data = {volume->pbo, NULL};
321 wined3d_volume_upload_data(volume, context, &data);
323 else if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
325 wined3d_volume_srgb_transfer(volume, context, TRUE);
327 else if (volume->locations & WINED3D_LOCATION_TEXTURE_SRGB)
329 wined3d_volume_srgb_transfer(volume, context, FALSE);
331 else
333 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume->locations));
334 return;
336 wined3d_volume_validate_location(volume, location);
338 if (wined3d_volume_can_evict(volume))
339 wined3d_volume_evict_sysmem(volume);
341 break;
343 case WINED3D_LOCATION_SYSMEM:
344 if (!volume->resource.heap_memory)
345 ERR("Trying to load WINED3D_LOCATION_SYSMEM without setting it up first.\n");
347 if (volume->locations & WINED3D_LOCATION_DISCARDED)
349 TRACE("Volume previously discarded, nothing to do.\n");
350 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
352 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
354 struct wined3d_bo_address data = {0, volume->resource.heap_memory};
356 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
357 wined3d_texture_bind_and_dirtify(volume->container, context, FALSE);
358 else
359 wined3d_texture_bind_and_dirtify(volume->container, context, TRUE);
361 volume->download_count++;
362 wined3d_volume_download_data(volume, context, &data);
364 else
366 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
367 wined3d_debug_location(volume->locations));
368 return;
370 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
371 break;
373 case WINED3D_LOCATION_BUFFER:
374 if (!volume->pbo || !(volume->flags & WINED3D_VFLAG_PBO))
375 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
377 if (volume->locations & WINED3D_LOCATION_DISCARDED)
379 TRACE("Volume previously discarded, nothing to do.\n");
380 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
382 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
384 struct wined3d_bo_address data = {volume->pbo, NULL};
386 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
387 wined3d_texture_bind_and_dirtify(volume->container, context, FALSE);
388 else
389 wined3d_texture_bind_and_dirtify(volume->container, context, TRUE);
391 wined3d_volume_download_data(volume, context, &data);
393 else
395 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
396 wined3d_debug_location(volume->locations));
397 return;
399 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
400 break;
402 default:
403 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
404 wined3d_debug_location(volume->locations));
408 /* Context activation is done by the caller. */
409 void wined3d_volume_load(struct wined3d_volume *volume, struct wined3d_context *context, BOOL srgb_mode)
411 wined3d_texture_bind_and_dirtify(volume->container, context, srgb_mode);
413 if (srgb_mode)
415 if (!(volume->flags & WINED3D_VFLAG_SRGB_ALLOCATED))
417 wined3d_volume_allocate_texture(volume, context, TRUE);
418 volume->flags |= WINED3D_VFLAG_SRGB_ALLOCATED;
421 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_TEXTURE_SRGB);
423 else
425 if (!(volume->flags & WINED3D_VFLAG_ALLOCATED))
427 wined3d_volume_allocate_texture(volume, context, FALSE);
428 volume->flags |= WINED3D_VFLAG_ALLOCATED;
431 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_TEXTURE_RGB);
435 /* Context activation is done by the caller. */
436 static void wined3d_volume_prepare_pbo(struct wined3d_volume *volume, struct wined3d_context *context)
438 const struct wined3d_gl_info *gl_info = context->gl_info;
440 if (volume->pbo)
441 return;
443 GL_EXTCALL(glGenBuffersARB(1, &volume->pbo));
444 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, volume->pbo));
445 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, volume->resource.size, NULL, GL_STREAM_DRAW_ARB));
446 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
447 checkGLcall("Create PBO");
449 TRACE("Created PBO %u for volume %p.\n", volume->pbo, volume);
452 static void wined3d_volume_free_pbo(struct wined3d_volume *volume)
454 struct wined3d_context *context = context_acquire(volume->resource.device, NULL);
455 const struct wined3d_gl_info *gl_info = context->gl_info;
457 TRACE("Deleting PBO %u belonging to volume %p.\n", volume->pbo, volume);
458 GL_EXTCALL(glDeleteBuffersARB(1, &volume->pbo));
459 checkGLcall("glDeleteBuffersARB");
460 volume->pbo = 0;
461 context_release(context);
464 static void volume_unload(struct wined3d_resource *resource)
466 struct wined3d_volume *volume = volume_from_resource(resource);
467 struct wined3d_device *device = volume->resource.device;
468 struct wined3d_context *context;
470 if (volume->resource.pool == WINED3D_POOL_DEFAULT)
471 ERR("Unloading DEFAULT pool volume.\n");
473 TRACE("texture %p.\n", resource);
475 if (volume_prepare_system_memory(volume))
477 context = context_acquire(device, NULL);
478 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
479 context_release(context);
480 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_SYSMEM);
482 else
484 ERR("Out of memory when unloading volume %p.\n", volume);
485 wined3d_volume_validate_location(volume, WINED3D_LOCATION_DISCARDED);
486 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_DISCARDED);
489 if (volume->pbo)
491 /* Should not happen because only dynamic default pool volumes
492 * have a buffer, and those are not evicted by device_evit_managed_resources
493 * and must be freed before a non-ex device reset. */
494 ERR("Unloading a volume with a buffer\n");
495 wined3d_volume_free_pbo(volume);
498 /* The texture name is managed by the container. */
499 volume->flags &= ~(WINED3D_VFLAG_ALLOCATED | WINED3D_VFLAG_SRGB_ALLOCATED
500 | WINED3D_VFLAG_CLIENT_STORAGE);
502 resource_unload(resource);
505 ULONG CDECL wined3d_volume_incref(struct wined3d_volume *volume)
507 ULONG refcount;
509 if (volume->container)
511 TRACE("Forwarding to container %p.\n", volume->container);
512 return wined3d_texture_incref(volume->container);
515 refcount = InterlockedIncrement(&volume->resource.ref);
517 TRACE("%p increasing refcount to %u.\n", volume, refcount);
519 return refcount;
522 ULONG CDECL wined3d_volume_decref(struct wined3d_volume *volume)
524 ULONG refcount;
526 if (volume->container)
528 TRACE("Forwarding to container %p.\n", volume->container);
529 return wined3d_texture_decref(volume->container);
532 refcount = InterlockedDecrement(&volume->resource.ref);
534 TRACE("%p decreasing refcount to %u.\n", volume, refcount);
536 if (!refcount)
538 if (volume->pbo)
539 wined3d_volume_free_pbo(volume);
541 resource_cleanup(&volume->resource);
542 volume->resource.parent_ops->wined3d_object_destroyed(volume->resource.parent);
543 HeapFree(GetProcessHeap(), 0, volume);
546 return refcount;
549 void * CDECL wined3d_volume_get_parent(const struct wined3d_volume *volume)
551 TRACE("volume %p.\n", volume);
553 return volume->resource.parent;
556 void CDECL wined3d_volume_preload(struct wined3d_volume *volume)
558 FIXME("volume %p stub!\n", volume);
561 struct wined3d_resource * CDECL wined3d_volume_get_resource(struct wined3d_volume *volume)
563 TRACE("volume %p.\n", volume);
565 return &volume->resource;
568 static BOOL volume_check_block_align(const struct wined3d_volume *volume,
569 const struct wined3d_box *box)
571 UINT width_mask, height_mask;
572 const struct wined3d_format *format = volume->resource.format;
574 if (!box)
575 return TRUE;
577 /* This assumes power of two block sizes, but NPOT block sizes would be
578 * silly anyway.
580 * This also assumes that the format's block depth is 1. */
581 width_mask = format->block_width - 1;
582 height_mask = format->block_height - 1;
584 if (box->left & width_mask)
585 return FALSE;
586 if (box->top & height_mask)
587 return FALSE;
588 if (box->right & width_mask && box->right != volume->resource.width)
589 return FALSE;
590 if (box->bottom & height_mask && box->bottom != volume->resource.height)
591 return FALSE;
593 return TRUE;
596 static BOOL wined3d_volume_check_box_dimensions(const struct wined3d_volume *volume,
597 const struct wined3d_box *box)
599 if (!box)
600 return TRUE;
602 if (box->left >= box->right)
603 return FALSE;
604 if (box->top >= box->bottom)
605 return FALSE;
606 if (box->front >= box->back)
607 return FALSE;
608 if (box->right > volume->resource.width)
609 return FALSE;
610 if (box->bottom > volume->resource.height)
611 return FALSE;
612 if (box->back > volume->resource.depth)
613 return FALSE;
615 return TRUE;
618 HRESULT CDECL wined3d_volume_map(struct wined3d_volume *volume,
619 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
621 struct wined3d_device *device = volume->resource.device;
622 struct wined3d_context *context;
623 const struct wined3d_gl_info *gl_info;
624 BYTE *base_memory;
625 const struct wined3d_format *format = volume->resource.format;
627 TRACE("volume %p, map_desc %p, box %p, flags %#x.\n",
628 volume, map_desc, box, flags);
630 map_desc->data = NULL;
631 if (!(volume->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU))
633 WARN("Volume %p is not CPU accessible.\n", volume);
634 return WINED3DERR_INVALIDCALL;
636 if (volume->resource.map_count)
638 WARN("Volume is already mapped.\n");
639 return WINED3DERR_INVALIDCALL;
641 if (!wined3d_volume_check_box_dimensions(volume, box))
643 WARN("Map box is invalid.\n");
644 return WINED3DERR_INVALIDCALL;
646 if ((format->flags & WINED3DFMT_FLAG_BLOCKS) && !volume_check_block_align(volume, box))
648 WARN("Map box is misaligned for %ux%u blocks.\n",
649 format->block_width, format->block_height);
650 return WINED3DERR_INVALIDCALL;
653 flags = wined3d_resource_sanitize_map_flags(&volume->resource, flags);
655 if (volume->flags & WINED3D_VFLAG_PBO)
657 context = context_acquire(device, NULL);
658 gl_info = context->gl_info;
660 wined3d_volume_prepare_pbo(volume, context);
661 if (flags & WINED3D_MAP_DISCARD)
662 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
663 else
664 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_BUFFER);
666 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, volume->pbo));
668 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
670 GLbitfield mapflags = wined3d_resource_gl_map_flags(flags);
671 mapflags &= ~GL_MAP_FLUSH_EXPLICIT_BIT;
672 base_memory = GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER_ARB,
673 0, volume->resource.size, mapflags));
675 else
677 GLenum access = wined3d_resource_gl_legacy_map_flags(flags);
678 base_memory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, access));
681 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
682 checkGLcall("Map PBO");
684 context_release(context);
686 else
688 if (!volume_prepare_system_memory(volume))
690 WARN("Out of memory.\n");
691 map_desc->data = NULL;
692 return E_OUTOFMEMORY;
695 if (flags & WINED3D_MAP_DISCARD)
697 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
699 else if (!(volume->locations & WINED3D_LOCATION_SYSMEM))
701 context = context_acquire(device, NULL);
702 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
703 context_release(context);
705 base_memory = volume->resource.heap_memory;
708 TRACE("Base memory pointer %p.\n", base_memory);
710 if (format->flags & WINED3DFMT_FLAG_BROKEN_PITCH)
712 map_desc->row_pitch = volume->resource.width * format->byte_count;
713 map_desc->slice_pitch = map_desc->row_pitch * volume->resource.height;
715 else
717 wined3d_volume_get_pitch(volume, &map_desc->row_pitch, &map_desc->slice_pitch);
720 if (!box)
722 TRACE("No box supplied - all is ok\n");
723 map_desc->data = base_memory;
725 else
727 TRACE("Lock Box (%p) = l %u, t %u, r %u, b %u, fr %u, ba %u\n",
728 box, box->left, box->top, box->right, box->bottom, box->front, box->back);
730 if ((format->flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
732 /* Compressed textures are block based, so calculate the offset of
733 * the block that contains the top-left pixel of the locked rectangle. */
734 map_desc->data = base_memory
735 + (box->front * map_desc->slice_pitch)
736 + ((box->top / format->block_height) * map_desc->row_pitch)
737 + ((box->left / format->block_width) * format->block_byte_count);
739 else
741 map_desc->data = base_memory
742 + (map_desc->slice_pitch * box->front)
743 + (map_desc->row_pitch * box->top)
744 + (box->left * volume->resource.format->byte_count);
748 if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
750 wined3d_texture_set_dirty(volume->container);
752 if (volume->flags & WINED3D_VFLAG_PBO)
753 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_BUFFER);
754 else
755 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_SYSMEM);
758 volume->resource.map_count++;
760 TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
761 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
763 return WINED3D_OK;
766 struct wined3d_volume * CDECL wined3d_volume_from_resource(struct wined3d_resource *resource)
768 return volume_from_resource(resource);
771 HRESULT CDECL wined3d_volume_unmap(struct wined3d_volume *volume)
773 TRACE("volume %p.\n", volume);
775 if (!volume->resource.map_count)
777 WARN("Trying to unlock an unlocked volume %p.\n", volume);
778 return WINED3DERR_INVALIDCALL;
781 if (volume->flags & WINED3D_VFLAG_PBO)
783 struct wined3d_device *device = volume->resource.device;
784 struct wined3d_context *context = context_acquire(device, NULL);
785 const struct wined3d_gl_info *gl_info = context->gl_info;
787 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, volume->pbo));
788 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
789 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
790 checkGLcall("Unmap PBO");
792 context_release(context);
795 volume->resource.map_count--;
797 return WINED3D_OK;
800 static const struct wined3d_resource_ops volume_resource_ops =
802 volume_unload,
805 static HRESULT volume_init(struct wined3d_volume *volume, struct wined3d_texture *container,
806 const struct wined3d_resource_desc *desc, UINT level)
808 struct wined3d_device *device = container->resource.device;
809 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
810 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format);
811 HRESULT hr;
812 UINT size;
814 if (!gl_info->supported[EXT_TEXTURE3D])
816 WARN("Volume cannot be created - no volume texture support.\n");
817 return WINED3DERR_INVALIDCALL;
819 /* TODO: Write tests for other resources and move this check
820 * to resource_init, if applicable. */
821 if (desc->usage & WINED3DUSAGE_DYNAMIC
822 && (desc->pool == WINED3D_POOL_MANAGED || desc->pool == WINED3D_POOL_SCRATCH))
824 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
825 return WINED3DERR_INVALIDCALL;
828 size = wined3d_format_calculate_size(format, device->surface_alignment, desc->width, desc->height, desc->depth);
830 if (FAILED(hr = resource_init(&volume->resource, device, WINED3D_RTYPE_VOLUME, format,
831 WINED3D_MULTISAMPLE_NONE, 0, desc->usage, desc->pool, desc->width, desc->height, desc->depth,
832 size, NULL, &wined3d_null_parent_ops, &volume_resource_ops)))
834 WARN("Failed to initialize resource, returning %#x.\n", hr);
835 return hr;
838 volume->texture_level = level;
839 volume->locations = WINED3D_LOCATION_DISCARDED;
841 if (desc->pool == WINED3D_POOL_DEFAULT && desc->usage & WINED3DUSAGE_DYNAMIC
842 && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
843 && !format->convert)
845 wined3d_resource_free_sysmem(&volume->resource);
846 volume->flags |= WINED3D_VFLAG_PBO;
849 volume_set_container(volume, container);
851 return WINED3D_OK;
854 HRESULT wined3d_volume_create(struct wined3d_texture *container, const struct wined3d_resource_desc *desc,
855 unsigned int level, struct wined3d_volume **volume)
857 struct wined3d_device_parent *device_parent = container->resource.device->device_parent;
858 const struct wined3d_parent_ops *parent_ops;
859 struct wined3d_volume *object;
860 void *parent;
861 HRESULT hr;
863 TRACE("container %p, width %u, height %u, depth %u, level %u, format %s, "
864 "usage %#x, pool %s, volume %p.\n",
865 container, desc->width, desc->height, desc->depth, level, debug_d3dformat(desc->format),
866 desc->usage, debug_d3dpool(desc->pool), volume);
868 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
869 return E_OUTOFMEMORY;
871 if (FAILED(hr = volume_init(object, container, desc, level)))
873 WARN("Failed to initialize volume, returning %#x.\n", hr);
874 HeapFree(GetProcessHeap(), 0, object);
875 return hr;
878 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
879 wined3d_texture_get_parent(container), object, &parent, &parent_ops)))
881 WARN("Failed to create volume parent, hr %#x.\n", hr);
882 volume_set_container(object, NULL);
883 wined3d_volume_decref(object);
884 return hr;
887 TRACE("Created volume %p, parent %p, parent_ops %p.\n", object, parent, parent_ops);
889 object->resource.parent = parent;
890 object->resource.parent_ops = parent_ops;
891 *volume = object;
893 return WINED3D_OK;