wined3d: Use the core version of the gl*Buffer* functions.
[wine/multimedia.git] / dlls / wined3d / volume.c
blobf2dd9e90df0a53786a50f007127553e1a7c779a9
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 (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 /* Context activation is done by the caller. */
68 void wined3d_volume_upload_data(struct wined3d_volume *volume, const struct wined3d_context *context,
69 const struct wined3d_const_bo_address *data)
71 const struct wined3d_gl_info *gl_info = context->gl_info;
72 const struct wined3d_format *format = volume->resource.format;
73 UINT width = volume->resource.width;
74 UINT height = volume->resource.height;
75 UINT depth = volume->resource.depth;
76 const void *mem = data->addr;
77 void *converted_mem = NULL;
79 TRACE("volume %p, context %p, level %u, format %s (%#x).\n",
80 volume, context, volume->texture_level, debug_d3dformat(format->id),
81 format->id);
83 if (format->convert)
85 UINT dst_row_pitch, dst_slice_pitch;
86 UINT src_row_pitch, src_slice_pitch;
87 UINT alignment = volume->resource.device->surface_alignment;
89 if (data->buffer_object)
90 ERR("Loading a converted volume from a PBO.\n");
91 if (format->flags & WINED3DFMT_FLAG_BLOCKS)
92 ERR("Converting a block-based format.\n");
94 dst_row_pitch = width * format->conv_byte_count;
95 dst_row_pitch = (dst_row_pitch + alignment - 1) & ~(alignment - 1);
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(glTexSubImage3DEXT(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_volume_upload_data(volume, context, &data);
280 else if (volume->locations & WINED3D_LOCATION_BUFFER)
282 struct wined3d_const_bo_address data = {volume->pbo, NULL};
283 wined3d_volume_upload_data(volume, context, &data);
285 else if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
287 wined3d_volume_srgb_transfer(volume, context, TRUE);
289 else if (volume->locations & WINED3D_LOCATION_TEXTURE_SRGB)
291 wined3d_volume_srgb_transfer(volume, context, FALSE);
293 else
295 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume->locations));
296 return;
298 wined3d_volume_validate_location(volume, location);
300 if (wined3d_volume_can_evict(volume))
301 wined3d_volume_evict_sysmem(volume);
303 break;
305 case WINED3D_LOCATION_SYSMEM:
306 if (!volume->resource.heap_memory)
307 ERR("Trying to load WINED3D_LOCATION_SYSMEM without setting it up first.\n");
309 if (volume->locations & WINED3D_LOCATION_DISCARDED)
311 TRACE("Volume previously discarded, nothing to do.\n");
312 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
314 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
316 struct wined3d_bo_address data = {0, volume->resource.heap_memory};
318 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
319 wined3d_texture_bind_and_dirtify(volume->container, context, FALSE);
320 else
321 wined3d_texture_bind_and_dirtify(volume->container, context, TRUE);
323 volume->download_count++;
324 wined3d_volume_download_data(volume, context, &data);
326 else
328 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
329 wined3d_debug_location(volume->locations));
330 return;
332 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
333 break;
335 case WINED3D_LOCATION_BUFFER:
336 if (!volume->pbo)
337 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
339 if (volume->locations & WINED3D_LOCATION_DISCARDED)
341 TRACE("Volume previously discarded, nothing to do.\n");
342 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
344 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
346 struct wined3d_bo_address data = {volume->pbo, NULL};
348 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
349 wined3d_texture_bind_and_dirtify(volume->container, context, FALSE);
350 else
351 wined3d_texture_bind_and_dirtify(volume->container, context, TRUE);
353 wined3d_volume_download_data(volume, context, &data);
355 else
357 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
358 wined3d_debug_location(volume->locations));
359 return;
361 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
362 break;
364 default:
365 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
366 wined3d_debug_location(volume->locations));
370 /* Context activation is done by the caller. */
371 void wined3d_volume_load(struct wined3d_volume *volume, struct wined3d_context *context, BOOL srgb_mode)
373 wined3d_texture_prepare_texture(volume->container, context, srgb_mode);
374 wined3d_volume_load_location(volume, context,
375 srgb_mode ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB);
378 /* Context activation is done by the caller. */
379 static void wined3d_volume_prepare_pbo(struct wined3d_volume *volume, struct wined3d_context *context)
381 const struct wined3d_gl_info *gl_info = context->gl_info;
383 if (volume->pbo)
384 return;
386 GL_EXTCALL(glGenBuffers(1, &volume->pbo));
387 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, volume->pbo));
388 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER, volume->resource.size, NULL, GL_STREAM_DRAW));
389 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
390 checkGLcall("Create PBO");
392 TRACE("Created PBO %u for volume %p.\n", volume->pbo, volume);
395 static void wined3d_volume_free_pbo(struct wined3d_volume *volume)
397 struct wined3d_context *context = context_acquire(volume->resource.device, NULL);
398 const struct wined3d_gl_info *gl_info = context->gl_info;
400 TRACE("Deleting PBO %u belonging to volume %p.\n", volume->pbo, volume);
401 GL_EXTCALL(glDeleteBuffers(1, &volume->pbo));
402 checkGLcall("glDeleteBuffers");
403 volume->pbo = 0;
404 context_release(context);
407 void wined3d_volume_destroy(struct wined3d_volume *volume)
409 TRACE("volume %p.\n", volume);
411 if (volume->pbo)
412 wined3d_volume_free_pbo(volume);
414 resource_cleanup(&volume->resource);
415 volume->resource.parent_ops->wined3d_object_destroyed(volume->resource.parent);
416 HeapFree(GetProcessHeap(), 0, volume);
419 static void volume_unload(struct wined3d_resource *resource)
421 struct wined3d_volume *volume = volume_from_resource(resource);
422 struct wined3d_device *device = volume->resource.device;
423 struct wined3d_context *context;
425 if (volume->resource.pool == WINED3D_POOL_DEFAULT)
426 ERR("Unloading DEFAULT pool volume.\n");
428 TRACE("texture %p.\n", resource);
430 if (volume_prepare_system_memory(volume))
432 context = context_acquire(device, NULL);
433 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
434 context_release(context);
435 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_SYSMEM);
437 else
439 ERR("Out of memory when unloading volume %p.\n", volume);
440 wined3d_volume_validate_location(volume, WINED3D_LOCATION_DISCARDED);
441 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_DISCARDED);
444 if (volume->pbo)
446 /* Should not happen because only dynamic default pool volumes
447 * have a buffer, and those are not evicted by device_evit_managed_resources
448 * and must be freed before a non-ex device reset. */
449 ERR("Unloading a volume with a buffer\n");
450 wined3d_volume_free_pbo(volume);
453 /* The texture name is managed by the container. */
454 volume->flags &= ~WINED3D_VFLAG_CLIENT_STORAGE;
456 resource_unload(resource);
459 ULONG CDECL wined3d_volume_incref(struct wined3d_volume *volume)
461 TRACE("Forwarding to container %p.\n", volume->container);
463 return wined3d_texture_incref(volume->container);
466 ULONG CDECL wined3d_volume_decref(struct wined3d_volume *volume)
468 TRACE("Forwarding to container %p.\n", volume->container);
470 return wined3d_texture_decref(volume->container);
473 void * CDECL wined3d_volume_get_parent(const struct wined3d_volume *volume)
475 TRACE("volume %p.\n", volume);
477 return volume->resource.parent;
480 void CDECL wined3d_volume_preload(struct wined3d_volume *volume)
482 FIXME("volume %p stub!\n", volume);
485 struct wined3d_resource * CDECL wined3d_volume_get_resource(struct wined3d_volume *volume)
487 TRACE("volume %p.\n", volume);
489 return &volume->resource;
492 static BOOL volume_check_block_align(const struct wined3d_volume *volume,
493 const struct wined3d_box *box)
495 UINT width_mask, height_mask;
496 const struct wined3d_format *format = volume->resource.format;
498 if (!box)
499 return TRUE;
501 /* This assumes power of two block sizes, but NPOT block sizes would be
502 * silly anyway.
504 * This also assumes that the format's block depth is 1. */
505 width_mask = format->block_width - 1;
506 height_mask = format->block_height - 1;
508 if (box->left & width_mask)
509 return FALSE;
510 if (box->top & height_mask)
511 return FALSE;
512 if (box->right & width_mask && box->right != volume->resource.width)
513 return FALSE;
514 if (box->bottom & height_mask && box->bottom != volume->resource.height)
515 return FALSE;
517 return TRUE;
520 static BOOL wined3d_volume_check_box_dimensions(const struct wined3d_volume *volume,
521 const struct wined3d_box *box)
523 if (!box)
524 return TRUE;
526 if (box->left >= box->right)
527 return FALSE;
528 if (box->top >= box->bottom)
529 return FALSE;
530 if (box->front >= box->back)
531 return FALSE;
532 if (box->right > volume->resource.width)
533 return FALSE;
534 if (box->bottom > volume->resource.height)
535 return FALSE;
536 if (box->back > volume->resource.depth)
537 return FALSE;
539 return TRUE;
542 HRESULT CDECL wined3d_volume_map(struct wined3d_volume *volume,
543 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
545 struct wined3d_device *device = volume->resource.device;
546 struct wined3d_context *context;
547 const struct wined3d_gl_info *gl_info;
548 BYTE *base_memory;
549 const struct wined3d_format *format = volume->resource.format;
551 TRACE("volume %p, map_desc %p, box %p, flags %#x.\n",
552 volume, map_desc, box, flags);
554 map_desc->data = NULL;
555 if (!(volume->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU))
557 WARN("Volume %p is not CPU accessible.\n", volume);
558 return WINED3DERR_INVALIDCALL;
560 if (volume->resource.map_count)
562 WARN("Volume is already mapped.\n");
563 return WINED3DERR_INVALIDCALL;
565 if (!wined3d_volume_check_box_dimensions(volume, box))
567 WARN("Map box is invalid.\n");
568 return WINED3DERR_INVALIDCALL;
570 if ((format->flags & WINED3DFMT_FLAG_BLOCKS) && !volume_check_block_align(volume, box))
572 WARN("Map box is misaligned for %ux%u blocks.\n",
573 format->block_width, format->block_height);
574 return WINED3DERR_INVALIDCALL;
577 flags = wined3d_resource_sanitize_map_flags(&volume->resource, flags);
579 if (volume->resource.map_binding == WINED3D_LOCATION_BUFFER)
581 context = context_acquire(device, NULL);
582 gl_info = context->gl_info;
584 wined3d_volume_prepare_pbo(volume, context);
585 if (flags & WINED3D_MAP_DISCARD)
586 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
587 else
588 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_BUFFER);
590 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, volume->pbo));
592 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
594 GLbitfield mapflags = wined3d_resource_gl_map_flags(flags);
595 mapflags &= ~GL_MAP_FLUSH_EXPLICIT_BIT;
596 base_memory = GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER,
597 0, volume->resource.size, mapflags));
599 else
601 GLenum access = wined3d_resource_gl_legacy_map_flags(flags);
602 base_memory = GL_EXTCALL(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, access));
605 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
606 checkGLcall("Map PBO");
608 context_release(context);
610 else
612 if (!volume_prepare_system_memory(volume))
614 WARN("Out of memory.\n");
615 map_desc->data = NULL;
616 return E_OUTOFMEMORY;
619 if (flags & WINED3D_MAP_DISCARD)
621 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
623 else if (!(volume->locations & WINED3D_LOCATION_SYSMEM))
625 context = context_acquire(device, NULL);
626 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
627 context_release(context);
629 base_memory = volume->resource.heap_memory;
632 TRACE("Base memory pointer %p.\n", base_memory);
634 if (format->flags & WINED3DFMT_FLAG_BROKEN_PITCH)
636 map_desc->row_pitch = volume->resource.width * format->byte_count;
637 map_desc->slice_pitch = map_desc->row_pitch * volume->resource.height;
639 else
641 wined3d_volume_get_pitch(volume, &map_desc->row_pitch, &map_desc->slice_pitch);
644 if (!box)
646 TRACE("No box supplied - all is ok\n");
647 map_desc->data = base_memory;
649 else
651 TRACE("Lock Box (%p) = l %u, t %u, r %u, b %u, fr %u, ba %u\n",
652 box, box->left, box->top, box->right, box->bottom, box->front, box->back);
654 if ((format->flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
656 /* Compressed textures are block based, so calculate the offset of
657 * the block that contains the top-left pixel of the locked rectangle. */
658 map_desc->data = base_memory
659 + (box->front * map_desc->slice_pitch)
660 + ((box->top / format->block_height) * map_desc->row_pitch)
661 + ((box->left / format->block_width) * format->block_byte_count);
663 else
665 map_desc->data = base_memory
666 + (map_desc->slice_pitch * box->front)
667 + (map_desc->row_pitch * box->top)
668 + (box->left * volume->resource.format->byte_count);
672 if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
674 wined3d_texture_set_dirty(volume->container);
675 wined3d_volume_invalidate_location(volume, ~volume->resource.map_binding);
678 volume->resource.map_count++;
680 TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
681 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
683 return WINED3D_OK;
686 struct wined3d_volume * CDECL wined3d_volume_from_resource(struct wined3d_resource *resource)
688 return volume_from_resource(resource);
691 HRESULT CDECL wined3d_volume_unmap(struct wined3d_volume *volume)
693 TRACE("volume %p.\n", volume);
695 if (!volume->resource.map_count)
697 WARN("Trying to unlock an unlocked volume %p.\n", volume);
698 return WINED3DERR_INVALIDCALL;
701 if (volume->resource.map_binding == WINED3D_LOCATION_BUFFER)
703 struct wined3d_device *device = volume->resource.device;
704 struct wined3d_context *context = context_acquire(device, NULL);
705 const struct wined3d_gl_info *gl_info = context->gl_info;
707 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, volume->pbo));
708 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER));
709 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
710 checkGLcall("Unmap PBO");
712 context_release(context);
715 volume->resource.map_count--;
717 return WINED3D_OK;
720 static ULONG volume_resource_incref(struct wined3d_resource *resource)
722 return wined3d_volume_incref(volume_from_resource(resource));
725 static ULONG volume_resource_decref(struct wined3d_resource *resource)
727 return wined3d_volume_decref(volume_from_resource(resource));
730 static const struct wined3d_resource_ops volume_resource_ops =
732 volume_resource_incref,
733 volume_resource_decref,
734 volume_unload,
737 static HRESULT volume_init(struct wined3d_volume *volume, struct wined3d_texture *container,
738 const struct wined3d_resource_desc *desc, UINT level)
740 struct wined3d_device *device = container->resource.device;
741 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
742 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format);
743 HRESULT hr;
744 UINT size;
746 if (!gl_info->supported[EXT_TEXTURE3D])
748 WARN("Volume cannot be created - no volume texture support.\n");
749 return WINED3DERR_INVALIDCALL;
751 /* TODO: Write tests for other resources and move this check
752 * to resource_init, if applicable. */
753 if (desc->usage & WINED3DUSAGE_DYNAMIC
754 && (desc->pool == WINED3D_POOL_MANAGED || desc->pool == WINED3D_POOL_SCRATCH))
756 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
757 return WINED3DERR_INVALIDCALL;
760 size = wined3d_format_calculate_size(format, device->surface_alignment, desc->width, desc->height, desc->depth);
762 if (FAILED(hr = resource_init(&volume->resource, device, WINED3D_RTYPE_VOLUME, format,
763 WINED3D_MULTISAMPLE_NONE, 0, desc->usage, desc->pool, desc->width, desc->height, desc->depth,
764 size, NULL, &wined3d_null_parent_ops, &volume_resource_ops)))
766 WARN("Failed to initialize resource, returning %#x.\n", hr);
767 return hr;
770 volume->texture_level = level;
771 volume->locations = WINED3D_LOCATION_DISCARDED;
773 if (desc->pool == WINED3D_POOL_DEFAULT && desc->usage & WINED3DUSAGE_DYNAMIC
774 && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
775 && !format->convert)
777 wined3d_resource_free_sysmem(&volume->resource);
778 volume->resource.map_binding = WINED3D_LOCATION_BUFFER;
781 volume->container = container;
783 return WINED3D_OK;
786 HRESULT wined3d_volume_create(struct wined3d_texture *container, const struct wined3d_resource_desc *desc,
787 unsigned int level, struct wined3d_volume **volume)
789 struct wined3d_device_parent *device_parent = container->resource.device->device_parent;
790 const struct wined3d_parent_ops *parent_ops;
791 struct wined3d_volume *object;
792 void *parent;
793 HRESULT hr;
795 TRACE("container %p, width %u, height %u, depth %u, level %u, format %s, "
796 "usage %#x, pool %s, volume %p.\n",
797 container, desc->width, desc->height, desc->depth, level, debug_d3dformat(desc->format),
798 desc->usage, debug_d3dpool(desc->pool), volume);
800 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
801 return E_OUTOFMEMORY;
803 if (FAILED(hr = volume_init(object, container, desc, level)))
805 WARN("Failed to initialize volume, returning %#x.\n", hr);
806 HeapFree(GetProcessHeap(), 0, object);
807 return hr;
810 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
811 wined3d_texture_get_parent(container), object, &parent, &parent_ops)))
813 WARN("Failed to create volume parent, hr %#x.\n", hr);
814 wined3d_volume_destroy(object);
815 return hr;
818 TRACE("Created volume %p, parent %p, parent_ops %p.\n", object, parent, parent_ops);
820 object->resource.parent = parent;
821 object->resource.parent_ops = parent_ops;
822 *volume = object;
824 return WINED3D_OK;