msi: Correctly parse double quotes in the token value.
[wine/multimedia.git] / dlls / wined3d / volume.c
blob2daa55e2c483d262f7c2ff71df279a39bb54a96a
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 BOOL volume_prepare_system_memory(struct wined3d_volume *volume)
32 if (volume->resource.heap_memory)
33 return TRUE;
35 if (!wined3d_resource_allocate_sysmem(&volume->resource))
37 ERR("Failed to allocate system memory.\n");
38 return FALSE;
40 return TRUE;
43 void wined3d_volume_get_pitch(const struct wined3d_volume *volume, UINT *row_pitch, UINT *slice_pitch)
45 const struct wined3d_format *format = volume->resource.format;
47 if (volume->container->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
49 /* Since compressed formats are block based, pitch means the amount of
50 * bytes to the next row of block rather than the next row of pixels. */
51 UINT row_block_count = (volume->resource.width + format->block_width - 1) / format->block_width;
52 UINT slice_block_count = (volume->resource.height + format->block_height - 1) / format->block_height;
53 *row_pitch = row_block_count * format->block_byte_count;
54 *slice_pitch = *row_pitch * slice_block_count;
56 else
58 unsigned char alignment = volume->resource.device->surface_alignment;
59 *row_pitch = format->byte_count * volume->resource.width; /* Bytes / row */
60 *row_pitch = (*row_pitch + alignment - 1) & ~(alignment - 1);
61 *slice_pitch = *row_pitch * volume->resource.height;
64 TRACE("Returning row pitch %u, slice pitch %u.\n", *row_pitch, *slice_pitch);
67 /* This call just uploads data, the caller is responsible for binding the
68 * correct texture. */
69 /* Context activation is done by the caller. */
70 void wined3d_volume_upload_data(struct wined3d_volume *volume, const struct wined3d_context *context,
71 const struct wined3d_const_bo_address *data)
73 const struct wined3d_gl_info *gl_info = context->gl_info;
74 const struct wined3d_format *format = volume->resource.format;
75 UINT width = volume->resource.width;
76 UINT height = volume->resource.height;
77 UINT depth = volume->resource.depth;
78 const void *mem = data->addr;
79 void *converted_mem = NULL;
81 TRACE("volume %p, context %p, level %u, format %s (%#x).\n",
82 volume, context, volume->texture_level, debug_d3dformat(format->id),
83 format->id);
85 if (format->convert)
87 UINT dst_row_pitch, dst_slice_pitch;
88 UINT src_row_pitch, src_slice_pitch;
90 if (data->buffer_object)
91 ERR("Loading a converted volume from a PBO.\n");
92 if (volume->container->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
93 ERR("Converting a block-based format.\n");
95 dst_row_pitch = width * format->conv_byte_count;
96 dst_slice_pitch = dst_row_pitch * height;
98 wined3d_volume_get_pitch(volume, &src_row_pitch, &src_slice_pitch);
100 converted_mem = HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch * depth);
101 format->convert(data->addr, converted_mem, src_row_pitch, src_slice_pitch,
102 dst_row_pitch, dst_slice_pitch, width, height, depth);
103 mem = converted_mem;
106 if (data->buffer_object)
108 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
109 checkGLcall("glBindBuffer");
112 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D, volume->texture_level, 0, 0, 0,
113 width, height, depth,
114 format->glFormat, format->glType, mem));
115 checkGLcall("glTexSubImage3D");
117 if (data->buffer_object)
119 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
120 checkGLcall("glBindBuffer");
123 HeapFree(GetProcessHeap(), 0, converted_mem);
126 void wined3d_volume_validate_location(struct wined3d_volume *volume, DWORD location)
128 TRACE("Volume %p, setting %s.\n", volume, wined3d_debug_location(location));
129 volume->locations |= location;
130 TRACE("new location flags are %s.\n", wined3d_debug_location(volume->locations));
133 void wined3d_volume_invalidate_location(struct wined3d_volume *volume, DWORD location)
135 TRACE("Volume %p, clearing %s.\n", volume, wined3d_debug_location(location));
136 volume->locations &= ~location;
137 TRACE("new location flags are %s.\n", wined3d_debug_location(volume->locations));
140 /* Context activation is done by the caller. */
141 static void wined3d_volume_download_data(struct wined3d_volume *volume,
142 const struct wined3d_context *context, const struct wined3d_bo_address *data)
144 const struct wined3d_gl_info *gl_info = context->gl_info;
145 const struct wined3d_format *format = volume->resource.format;
147 if (format->convert)
149 FIXME("Attempting to download a converted volume, format %s.\n",
150 debug_d3dformat(format->id));
151 return;
154 if (data->buffer_object)
156 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
157 checkGLcall("glBindBuffer");
160 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, volume->texture_level,
161 format->glFormat, format->glType, data->addr);
162 checkGLcall("glGetTexImage");
164 if (data->buffer_object)
166 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
167 checkGLcall("glBindBuffer");
172 static void wined3d_volume_evict_sysmem(struct wined3d_volume *volume)
174 wined3d_resource_free_sysmem(&volume->resource);
175 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_SYSMEM);
178 static DWORD volume_access_from_location(DWORD location)
180 switch (location)
182 case WINED3D_LOCATION_DISCARDED:
183 return 0;
185 case WINED3D_LOCATION_SYSMEM:
186 return WINED3D_RESOURCE_ACCESS_CPU;
188 case WINED3D_LOCATION_BUFFER:
189 case WINED3D_LOCATION_TEXTURE_RGB:
190 case WINED3D_LOCATION_TEXTURE_SRGB:
191 return WINED3D_RESOURCE_ACCESS_GPU;
193 default:
194 FIXME("Unhandled location %#x.\n", location);
195 return 0;
199 /* Context activation is done by the caller. */
200 static void wined3d_volume_srgb_transfer(struct wined3d_volume *volume,
201 struct wined3d_context *context, BOOL dest_is_srgb)
203 struct wined3d_bo_address data;
204 /* Optimizations are possible, but the effort should be put into either
205 * implementing EXT_SRGB_DECODE in the driver or finding out why we
206 * picked the wrong copy for the original upload and fixing that.
208 * Also keep in mind that we want to avoid using resource.heap_memory
209 * for DEFAULT pool surfaces. */
211 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
212 data.buffer_object = 0;
213 data.addr = HeapAlloc(GetProcessHeap(), 0, volume->resource.size);
214 if (!data.addr)
215 return;
217 wined3d_texture_bind_and_dirtify(volume->container, context, !dest_is_srgb);
218 wined3d_volume_download_data(volume, context, &data);
219 wined3d_texture_bind_and_dirtify(volume->container, context, dest_is_srgb);
220 wined3d_volume_upload_data(volume, context, wined3d_const_bo_address(&data));
222 HeapFree(GetProcessHeap(), 0, data.addr);
225 static BOOL wined3d_volume_can_evict(const struct wined3d_volume *volume)
227 if (volume->resource.pool != WINED3D_POOL_MANAGED)
228 return FALSE;
229 if (volume->download_count >= 10)
230 return FALSE;
231 if (volume->resource.format->convert)
232 return FALSE;
233 if (volume->flags & WINED3D_VFLAG_CLIENT_STORAGE)
234 return FALSE;
236 return TRUE;
238 /* Context activation is done by the caller. */
239 static void wined3d_volume_load_location(struct wined3d_volume *volume,
240 struct wined3d_context *context, DWORD location)
242 DWORD required_access = volume_access_from_location(location);
244 TRACE("Volume %p, loading %s, have %s.\n", volume, wined3d_debug_location(location),
245 wined3d_debug_location(volume->locations));
247 if ((volume->locations & location) == location)
249 TRACE("Location(s) already up to date.\n");
250 return;
253 if ((volume->resource.access_flags & required_access) != required_access)
255 ERR("Operation requires %#x access, but volume only has %#x.\n",
256 required_access, volume->resource.access_flags);
257 return;
260 switch (location)
262 case WINED3D_LOCATION_TEXTURE_RGB:
263 case WINED3D_LOCATION_TEXTURE_SRGB:
264 if ((location == WINED3D_LOCATION_TEXTURE_RGB
265 && !(volume->container->flags & WINED3D_TEXTURE_RGB_ALLOCATED))
266 || (location == WINED3D_LOCATION_TEXTURE_SRGB
267 && !(volume->container->flags & WINED3D_TEXTURE_SRGB_ALLOCATED)))
268 ERR("Trying to load (s)RGB texture without prior allocation.\n");
270 if (volume->locations & WINED3D_LOCATION_DISCARDED)
272 TRACE("Volume previously discarded, nothing to do.\n");
273 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
275 else if (volume->locations & WINED3D_LOCATION_SYSMEM)
277 struct wined3d_const_bo_address data = {0, volume->resource.heap_memory};
278 wined3d_texture_bind_and_dirtify(volume->container, context,
279 location == WINED3D_LOCATION_TEXTURE_SRGB);
280 wined3d_volume_upload_data(volume, context, &data);
282 else if (volume->locations & WINED3D_LOCATION_BUFFER)
284 struct wined3d_const_bo_address data = {volume->pbo, NULL};
285 wined3d_texture_bind_and_dirtify(volume->container, context,
286 location == WINED3D_LOCATION_TEXTURE_SRGB);
287 wined3d_volume_upload_data(volume, context, &data);
289 else if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
291 wined3d_volume_srgb_transfer(volume, context, TRUE);
293 else if (volume->locations & WINED3D_LOCATION_TEXTURE_SRGB)
295 wined3d_volume_srgb_transfer(volume, context, FALSE);
297 else
299 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume->locations));
300 return;
302 wined3d_volume_validate_location(volume, location);
304 if (wined3d_volume_can_evict(volume))
305 wined3d_volume_evict_sysmem(volume);
307 break;
309 case WINED3D_LOCATION_SYSMEM:
310 if (!volume->resource.heap_memory)
311 ERR("Trying to load WINED3D_LOCATION_SYSMEM without setting it up first.\n");
313 if (volume->locations & WINED3D_LOCATION_DISCARDED)
315 TRACE("Volume previously discarded, nothing to do.\n");
316 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
318 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
320 struct wined3d_bo_address data = {0, volume->resource.heap_memory};
322 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
323 wined3d_texture_bind_and_dirtify(volume->container, context, FALSE);
324 else
325 wined3d_texture_bind_and_dirtify(volume->container, context, TRUE);
327 volume->download_count++;
328 wined3d_volume_download_data(volume, context, &data);
330 else
332 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
333 wined3d_debug_location(volume->locations));
334 return;
336 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
337 break;
339 case WINED3D_LOCATION_BUFFER:
340 if (!volume->pbo)
341 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
343 if (volume->locations & WINED3D_LOCATION_DISCARDED)
345 TRACE("Volume previously discarded, nothing to do.\n");
346 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
348 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
350 struct wined3d_bo_address data = {volume->pbo, NULL};
352 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
353 wined3d_texture_bind_and_dirtify(volume->container, context, FALSE);
354 else
355 wined3d_texture_bind_and_dirtify(volume->container, context, TRUE);
357 wined3d_volume_download_data(volume, context, &data);
359 else
361 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
362 wined3d_debug_location(volume->locations));
363 return;
365 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
366 break;
368 default:
369 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
370 wined3d_debug_location(volume->locations));
374 /* Context activation is done by the caller. */
375 void wined3d_volume_load(struct wined3d_volume *volume, struct wined3d_context *context, BOOL srgb_mode)
377 wined3d_texture_prepare_texture(volume->container, context, srgb_mode);
378 wined3d_volume_load_location(volume, context,
379 srgb_mode ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB);
382 /* Context activation is done by the caller. */
383 static void wined3d_volume_prepare_pbo(struct wined3d_volume *volume, struct wined3d_context *context)
385 const struct wined3d_gl_info *gl_info = context->gl_info;
387 if (volume->pbo)
388 return;
390 GL_EXTCALL(glGenBuffers(1, &volume->pbo));
391 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, volume->pbo));
392 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER, volume->resource.size, NULL, GL_STREAM_DRAW));
393 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
394 checkGLcall("Create PBO");
396 TRACE("Created PBO %u for volume %p.\n", volume->pbo, volume);
399 static void wined3d_volume_free_pbo(struct wined3d_volume *volume)
401 struct wined3d_context *context = context_acquire(volume->resource.device, NULL);
402 const struct wined3d_gl_info *gl_info = context->gl_info;
404 TRACE("Deleting PBO %u belonging to volume %p.\n", volume->pbo, volume);
405 GL_EXTCALL(glDeleteBuffers(1, &volume->pbo));
406 checkGLcall("glDeleteBuffers");
407 volume->pbo = 0;
408 context_release(context);
411 void wined3d_volume_destroy(struct wined3d_volume *volume)
413 TRACE("volume %p.\n", volume);
415 if (volume->pbo)
416 wined3d_volume_free_pbo(volume);
418 resource_cleanup(&volume->resource);
419 volume->resource.parent_ops->wined3d_object_destroyed(volume->resource.parent);
420 HeapFree(GetProcessHeap(), 0, volume);
423 static void volume_unload(struct wined3d_resource *resource)
425 struct wined3d_volume *volume = volume_from_resource(resource);
426 struct wined3d_device *device = volume->resource.device;
427 struct wined3d_context *context;
429 if (volume->resource.pool == WINED3D_POOL_DEFAULT)
430 ERR("Unloading DEFAULT pool volume.\n");
432 TRACE("texture %p.\n", resource);
434 if (volume_prepare_system_memory(volume))
436 context = context_acquire(device, NULL);
437 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
438 context_release(context);
439 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_SYSMEM);
441 else
443 ERR("Out of memory when unloading volume %p.\n", volume);
444 wined3d_volume_validate_location(volume, WINED3D_LOCATION_DISCARDED);
445 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_DISCARDED);
448 if (volume->pbo)
450 /* Should not happen because only dynamic default pool volumes
451 * have a buffer, and those are not evicted by device_evit_managed_resources
452 * and must be freed before a non-ex device reset. */
453 ERR("Unloading a volume with a buffer\n");
454 wined3d_volume_free_pbo(volume);
457 /* The texture name is managed by the container. */
458 volume->flags &= ~WINED3D_VFLAG_CLIENT_STORAGE;
460 resource_unload(resource);
463 static ULONG CDECL wined3d_volume_incref(struct wined3d_volume *volume)
465 TRACE("Forwarding to container %p.\n", volume->container);
467 return wined3d_texture_incref(volume->container);
470 static ULONG CDECL wined3d_volume_decref(struct wined3d_volume *volume)
472 TRACE("Forwarding to container %p.\n", volume->container);
474 return wined3d_texture_decref(volume->container);
477 struct wined3d_resource * CDECL wined3d_volume_get_resource(struct wined3d_volume *volume)
479 TRACE("volume %p.\n", volume);
481 return &volume->resource;
484 static BOOL volume_check_block_align(const struct wined3d_volume *volume,
485 const struct wined3d_box *box)
487 UINT width_mask, height_mask;
488 const struct wined3d_format *format = volume->resource.format;
490 if (!box)
491 return TRUE;
493 /* This assumes power of two block sizes, but NPOT block sizes would be
494 * silly anyway.
496 * This also assumes that the format's block depth is 1. */
497 width_mask = format->block_width - 1;
498 height_mask = format->block_height - 1;
500 if (box->left & width_mask)
501 return FALSE;
502 if (box->top & height_mask)
503 return FALSE;
504 if (box->right & width_mask && box->right != volume->resource.width)
505 return FALSE;
506 if (box->bottom & height_mask && box->bottom != volume->resource.height)
507 return FALSE;
509 return TRUE;
512 static BOOL wined3d_volume_check_box_dimensions(const struct wined3d_volume *volume,
513 const struct wined3d_box *box)
515 if (!box)
516 return TRUE;
518 if (box->left >= box->right)
519 return FALSE;
520 if (box->top >= box->bottom)
521 return FALSE;
522 if (box->front >= box->back)
523 return FALSE;
524 if (box->right > volume->resource.width)
525 return FALSE;
526 if (box->bottom > volume->resource.height)
527 return FALSE;
528 if (box->back > volume->resource.depth)
529 return FALSE;
531 return TRUE;
534 HRESULT CDECL wined3d_volume_map(struct wined3d_volume *volume,
535 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
537 struct wined3d_device *device = volume->resource.device;
538 struct wined3d_context *context;
539 const struct wined3d_gl_info *gl_info;
540 BYTE *base_memory;
541 const struct wined3d_format *format = volume->resource.format;
542 const unsigned int fmt_flags = volume->container->resource.format_flags;
544 TRACE("volume %p, map_desc %p, box %p, flags %#x.\n",
545 volume, map_desc, box, flags);
547 map_desc->data = NULL;
548 if (!(volume->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU))
550 WARN("Volume %p is not CPU accessible.\n", volume);
551 return WINED3DERR_INVALIDCALL;
553 if (volume->resource.map_count)
555 WARN("Volume is already mapped.\n");
556 return WINED3DERR_INVALIDCALL;
558 if (!wined3d_volume_check_box_dimensions(volume, box))
560 WARN("Map box is invalid.\n");
561 return WINED3DERR_INVALIDCALL;
563 if ((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && !volume_check_block_align(volume, box))
565 WARN("Map box is misaligned for %ux%u blocks.\n",
566 format->block_width, format->block_height);
567 return WINED3DERR_INVALIDCALL;
570 flags = wined3d_resource_sanitize_map_flags(&volume->resource, flags);
572 if (volume->resource.map_binding == WINED3D_LOCATION_BUFFER)
574 context = context_acquire(device, NULL);
575 gl_info = context->gl_info;
577 wined3d_volume_prepare_pbo(volume, context);
578 if (flags & WINED3D_MAP_DISCARD)
579 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
580 else
581 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_BUFFER);
583 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, volume->pbo));
585 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
587 GLbitfield mapflags = wined3d_resource_gl_map_flags(flags);
588 mapflags &= ~GL_MAP_FLUSH_EXPLICIT_BIT;
589 base_memory = GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER,
590 0, volume->resource.size, mapflags));
592 else
594 GLenum access = wined3d_resource_gl_legacy_map_flags(flags);
595 base_memory = GL_EXTCALL(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, access));
598 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
599 checkGLcall("Map PBO");
601 context_release(context);
603 else
605 if (!volume_prepare_system_memory(volume))
607 WARN("Out of memory.\n");
608 map_desc->data = NULL;
609 return E_OUTOFMEMORY;
612 if (flags & WINED3D_MAP_DISCARD)
614 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
616 else if (!(volume->locations & WINED3D_LOCATION_SYSMEM))
618 context = context_acquire(device, NULL);
619 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
620 context_release(context);
622 base_memory = volume->resource.heap_memory;
625 TRACE("Base memory pointer %p.\n", base_memory);
627 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
629 map_desc->row_pitch = volume->resource.width * format->byte_count;
630 map_desc->slice_pitch = map_desc->row_pitch * volume->resource.height;
632 else
634 wined3d_volume_get_pitch(volume, &map_desc->row_pitch, &map_desc->slice_pitch);
637 if (!box)
639 TRACE("No box supplied - all is ok\n");
640 map_desc->data = base_memory;
642 else
644 TRACE("Lock Box (%p) = l %u, t %u, r %u, b %u, fr %u, ba %u\n",
645 box, box->left, box->top, box->right, box->bottom, box->front, box->back);
647 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
649 /* Compressed textures are block based, so calculate the offset of
650 * the block that contains the top-left pixel of the locked rectangle. */
651 map_desc->data = base_memory
652 + (box->front * map_desc->slice_pitch)
653 + ((box->top / format->block_height) * map_desc->row_pitch)
654 + ((box->left / format->block_width) * format->block_byte_count);
656 else
658 map_desc->data = base_memory
659 + (map_desc->slice_pitch * box->front)
660 + (map_desc->row_pitch * box->top)
661 + (box->left * volume->resource.format->byte_count);
665 if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
667 wined3d_texture_set_dirty(volume->container);
668 wined3d_volume_invalidate_location(volume, ~volume->resource.map_binding);
671 volume->resource.map_count++;
673 TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
674 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
676 return WINED3D_OK;
679 struct wined3d_volume * CDECL wined3d_volume_from_resource(struct wined3d_resource *resource)
681 return volume_from_resource(resource);
684 HRESULT CDECL wined3d_volume_unmap(struct wined3d_volume *volume)
686 TRACE("volume %p.\n", volume);
688 if (!volume->resource.map_count)
690 WARN("Trying to unlock an unlocked volume %p.\n", volume);
691 return WINED3DERR_INVALIDCALL;
694 if (volume->resource.map_binding == WINED3D_LOCATION_BUFFER)
696 struct wined3d_device *device = volume->resource.device;
697 struct wined3d_context *context = context_acquire(device, NULL);
698 const struct wined3d_gl_info *gl_info = context->gl_info;
700 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, volume->pbo));
701 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER));
702 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
703 checkGLcall("Unmap PBO");
705 context_release(context);
708 volume->resource.map_count--;
710 return WINED3D_OK;
713 static ULONG volume_resource_incref(struct wined3d_resource *resource)
715 return wined3d_volume_incref(volume_from_resource(resource));
718 static ULONG volume_resource_decref(struct wined3d_resource *resource)
720 return wined3d_volume_decref(volume_from_resource(resource));
723 static const struct wined3d_resource_ops volume_resource_ops =
725 volume_resource_incref,
726 volume_resource_decref,
727 volume_unload,
730 static HRESULT volume_init(struct wined3d_volume *volume, struct wined3d_texture *container,
731 const struct wined3d_resource_desc *desc, UINT level)
733 struct wined3d_device *device = container->resource.device;
734 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
735 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format);
736 HRESULT hr;
737 UINT size;
739 /* TODO: Write tests for other resources and move this check
740 * to resource_init, if applicable. */
741 if (desc->usage & WINED3DUSAGE_DYNAMIC
742 && (desc->pool == WINED3D_POOL_MANAGED || desc->pool == WINED3D_POOL_SCRATCH))
744 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
745 return WINED3DERR_INVALIDCALL;
748 size = wined3d_format_calculate_size(format, device->surface_alignment, desc->width, desc->height, desc->depth);
750 if (FAILED(hr = resource_init(&volume->resource, device, WINED3D_RTYPE_VOLUME, format,
751 WINED3D_MULTISAMPLE_NONE, 0, desc->usage, desc->pool, desc->width, desc->height, desc->depth,
752 size, NULL, &wined3d_null_parent_ops, &volume_resource_ops)))
754 WARN("Failed to initialize resource, returning %#x.\n", hr);
755 return hr;
758 volume->texture_level = level;
759 volume->locations = WINED3D_LOCATION_DISCARDED;
761 if (desc->pool == WINED3D_POOL_DEFAULT && desc->usage & WINED3DUSAGE_DYNAMIC
762 && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
763 && !format->convert)
765 wined3d_resource_free_sysmem(&volume->resource);
766 volume->resource.map_binding = WINED3D_LOCATION_BUFFER;
769 volume->container = container;
771 return WINED3D_OK;
774 HRESULT wined3d_volume_create(struct wined3d_texture *container, const struct wined3d_resource_desc *desc,
775 unsigned int level, struct wined3d_volume **volume)
777 struct wined3d_device_parent *device_parent = container->resource.device->device_parent;
778 const struct wined3d_parent_ops *parent_ops;
779 struct wined3d_volume *object;
780 void *parent;
781 HRESULT hr;
783 TRACE("container %p, width %u, height %u, depth %u, level %u, format %s, "
784 "usage %#x, pool %s, volume %p.\n",
785 container, desc->width, desc->height, desc->depth, level, debug_d3dformat(desc->format),
786 desc->usage, debug_d3dpool(desc->pool), volume);
788 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
789 return E_OUTOFMEMORY;
791 if (FAILED(hr = volume_init(object, container, desc, level)))
793 WARN("Failed to initialize volume, returning %#x.\n", hr);
794 HeapFree(GetProcessHeap(), 0, object);
795 return hr;
798 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
799 wined3d_texture_get_parent(container), object, &parent, &parent_ops)))
801 WARN("Failed to create volume parent, hr %#x.\n", hr);
802 wined3d_volume_destroy(object);
803 return hr;
806 TRACE("Created volume %p, parent %p, parent_ops %p.\n", object, parent, parent_ops);
808 object->resource.parent = parent;
809 object->resource.parent_ops = parent_ops;
810 *volume = object;
812 return WINED3D_OK;