wined3d: Set GL_UNPACK_ALIGNMENT to 1.
[wine/multimedia.git] / dlls / wined3d / volume.c
blob91325dc25be30b8c812dd594e6c56af0e116579b
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;
88 if (data->buffer_object)
89 ERR("Loading a converted volume from a PBO.\n");
90 if (format->flags & WINED3DFMT_FLAG_BLOCKS)
91 ERR("Converting a block-based format.\n");
93 dst_row_pitch = width * format->conv_byte_count;
94 dst_slice_pitch = dst_row_pitch * height;
96 wined3d_volume_get_pitch(volume, &src_row_pitch, &src_slice_pitch);
98 converted_mem = HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch * depth);
99 format->convert(data->addr, converted_mem, src_row_pitch, src_slice_pitch,
100 dst_row_pitch, dst_slice_pitch, width, height, depth);
101 mem = converted_mem;
104 if (data->buffer_object)
106 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
107 checkGLcall("glBindBuffer");
110 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D, volume->texture_level, 0, 0, 0,
111 width, height, depth,
112 format->glFormat, format->glType, mem));
113 checkGLcall("glTexSubImage3D");
115 if (data->buffer_object)
117 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
118 checkGLcall("glBindBuffer");
121 HeapFree(GetProcessHeap(), 0, converted_mem);
124 void wined3d_volume_validate_location(struct wined3d_volume *volume, DWORD location)
126 TRACE("Volume %p, setting %s.\n", volume, wined3d_debug_location(location));
127 volume->locations |= location;
128 TRACE("new location flags are %s.\n", wined3d_debug_location(volume->locations));
131 void wined3d_volume_invalidate_location(struct wined3d_volume *volume, DWORD location)
133 TRACE("Volume %p, clearing %s.\n", volume, wined3d_debug_location(location));
134 volume->locations &= ~location;
135 TRACE("new location flags are %s.\n", wined3d_debug_location(volume->locations));
138 /* Context activation is done by the caller. */
139 static void wined3d_volume_download_data(struct wined3d_volume *volume,
140 const struct wined3d_context *context, const struct wined3d_bo_address *data)
142 const struct wined3d_gl_info *gl_info = context->gl_info;
143 const struct wined3d_format *format = volume->resource.format;
145 if (format->convert)
147 FIXME("Attempting to download a converted volume, format %s.\n",
148 debug_d3dformat(format->id));
149 return;
152 if (data->buffer_object)
154 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
155 checkGLcall("glBindBuffer");
158 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, volume->texture_level,
159 format->glFormat, format->glType, data->addr);
160 checkGLcall("glGetTexImage");
162 if (data->buffer_object)
164 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
165 checkGLcall("glBindBuffer");
170 static void wined3d_volume_evict_sysmem(struct wined3d_volume *volume)
172 wined3d_resource_free_sysmem(&volume->resource);
173 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_SYSMEM);
176 static DWORD volume_access_from_location(DWORD location)
178 switch (location)
180 case WINED3D_LOCATION_DISCARDED:
181 return 0;
183 case WINED3D_LOCATION_SYSMEM:
184 return WINED3D_RESOURCE_ACCESS_CPU;
186 case WINED3D_LOCATION_BUFFER:
187 case WINED3D_LOCATION_TEXTURE_RGB:
188 case WINED3D_LOCATION_TEXTURE_SRGB:
189 return WINED3D_RESOURCE_ACCESS_GPU;
191 default:
192 FIXME("Unhandled location %#x.\n", location);
193 return 0;
197 /* Context activation is done by the caller. */
198 static void wined3d_volume_srgb_transfer(struct wined3d_volume *volume,
199 struct wined3d_context *context, BOOL dest_is_srgb)
201 struct wined3d_bo_address data;
202 /* Optimizations are possible, but the effort should be put into either
203 * implementing EXT_SRGB_DECODE in the driver or finding out why we
204 * picked the wrong copy for the original upload and fixing that.
206 * Also keep in mind that we want to avoid using resource.heap_memory
207 * for DEFAULT pool surfaces. */
209 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
210 data.buffer_object = 0;
211 data.addr = HeapAlloc(GetProcessHeap(), 0, volume->resource.size);
212 if (!data.addr)
213 return;
215 wined3d_texture_bind_and_dirtify(volume->container, context, !dest_is_srgb);
216 wined3d_volume_download_data(volume, context, &data);
217 wined3d_texture_bind_and_dirtify(volume->container, context, dest_is_srgb);
218 wined3d_volume_upload_data(volume, context, wined3d_const_bo_address(&data));
220 HeapFree(GetProcessHeap(), 0, data.addr);
223 static BOOL wined3d_volume_can_evict(const struct wined3d_volume *volume)
225 if (volume->resource.pool != WINED3D_POOL_MANAGED)
226 return FALSE;
227 if (volume->download_count >= 10)
228 return FALSE;
229 if (volume->resource.format->convert)
230 return FALSE;
231 if (volume->flags & WINED3D_VFLAG_CLIENT_STORAGE)
232 return FALSE;
234 return TRUE;
236 /* Context activation is done by the caller. */
237 static void wined3d_volume_load_location(struct wined3d_volume *volume,
238 struct wined3d_context *context, DWORD location)
240 DWORD required_access = volume_access_from_location(location);
242 TRACE("Volume %p, loading %s, have %s.\n", volume, wined3d_debug_location(location),
243 wined3d_debug_location(volume->locations));
245 if ((volume->locations & location) == location)
247 TRACE("Location(s) already up to date.\n");
248 return;
251 if ((volume->resource.access_flags & required_access) != required_access)
253 ERR("Operation requires %#x access, but volume only has %#x.\n",
254 required_access, volume->resource.access_flags);
255 return;
258 switch (location)
260 case WINED3D_LOCATION_TEXTURE_RGB:
261 case WINED3D_LOCATION_TEXTURE_SRGB:
262 if ((location == WINED3D_LOCATION_TEXTURE_RGB
263 && !(volume->container->flags & WINED3D_TEXTURE_RGB_ALLOCATED))
264 || (location == WINED3D_LOCATION_TEXTURE_SRGB
265 && !(volume->container->flags & WINED3D_TEXTURE_SRGB_ALLOCATED)))
266 ERR("Trying to load (s)RGB texture without prior allocation.\n");
268 if (volume->locations & WINED3D_LOCATION_DISCARDED)
270 TRACE("Volume previously discarded, nothing to do.\n");
271 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
273 else if (volume->locations & WINED3D_LOCATION_SYSMEM)
275 struct wined3d_const_bo_address data = {0, volume->resource.heap_memory};
276 wined3d_volume_upload_data(volume, context, &data);
278 else if (volume->locations & WINED3D_LOCATION_BUFFER)
280 struct wined3d_const_bo_address data = {volume->pbo, NULL};
281 wined3d_volume_upload_data(volume, context, &data);
283 else if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
285 wined3d_volume_srgb_transfer(volume, context, TRUE);
287 else if (volume->locations & WINED3D_LOCATION_TEXTURE_SRGB)
289 wined3d_volume_srgb_transfer(volume, context, FALSE);
291 else
293 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume->locations));
294 return;
296 wined3d_volume_validate_location(volume, location);
298 if (wined3d_volume_can_evict(volume))
299 wined3d_volume_evict_sysmem(volume);
301 break;
303 case WINED3D_LOCATION_SYSMEM:
304 if (!volume->resource.heap_memory)
305 ERR("Trying to load WINED3D_LOCATION_SYSMEM without setting it up first.\n");
307 if (volume->locations & WINED3D_LOCATION_DISCARDED)
309 TRACE("Volume previously discarded, nothing to do.\n");
310 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
312 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
314 struct wined3d_bo_address data = {0, volume->resource.heap_memory};
316 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
317 wined3d_texture_bind_and_dirtify(volume->container, context, FALSE);
318 else
319 wined3d_texture_bind_and_dirtify(volume->container, context, TRUE);
321 volume->download_count++;
322 wined3d_volume_download_data(volume, context, &data);
324 else
326 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
327 wined3d_debug_location(volume->locations));
328 return;
330 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
331 break;
333 case WINED3D_LOCATION_BUFFER:
334 if (!volume->pbo)
335 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
337 if (volume->locations & WINED3D_LOCATION_DISCARDED)
339 TRACE("Volume previously discarded, nothing to do.\n");
340 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
342 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
344 struct wined3d_bo_address data = {volume->pbo, NULL};
346 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
347 wined3d_texture_bind_and_dirtify(volume->container, context, FALSE);
348 else
349 wined3d_texture_bind_and_dirtify(volume->container, context, TRUE);
351 wined3d_volume_download_data(volume, context, &data);
353 else
355 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
356 wined3d_debug_location(volume->locations));
357 return;
359 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
360 break;
362 default:
363 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
364 wined3d_debug_location(volume->locations));
368 /* Context activation is done by the caller. */
369 void wined3d_volume_load(struct wined3d_volume *volume, struct wined3d_context *context, BOOL srgb_mode)
371 wined3d_texture_prepare_texture(volume->container, context, srgb_mode);
372 wined3d_volume_load_location(volume, context,
373 srgb_mode ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB);
376 /* Context activation is done by the caller. */
377 static void wined3d_volume_prepare_pbo(struct wined3d_volume *volume, struct wined3d_context *context)
379 const struct wined3d_gl_info *gl_info = context->gl_info;
381 if (volume->pbo)
382 return;
384 GL_EXTCALL(glGenBuffers(1, &volume->pbo));
385 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, volume->pbo));
386 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER, volume->resource.size, NULL, GL_STREAM_DRAW));
387 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
388 checkGLcall("Create PBO");
390 TRACE("Created PBO %u for volume %p.\n", volume->pbo, volume);
393 static void wined3d_volume_free_pbo(struct wined3d_volume *volume)
395 struct wined3d_context *context = context_acquire(volume->resource.device, NULL);
396 const struct wined3d_gl_info *gl_info = context->gl_info;
398 TRACE("Deleting PBO %u belonging to volume %p.\n", volume->pbo, volume);
399 GL_EXTCALL(glDeleteBuffers(1, &volume->pbo));
400 checkGLcall("glDeleteBuffers");
401 volume->pbo = 0;
402 context_release(context);
405 void wined3d_volume_destroy(struct wined3d_volume *volume)
407 TRACE("volume %p.\n", volume);
409 if (volume->pbo)
410 wined3d_volume_free_pbo(volume);
412 resource_cleanup(&volume->resource);
413 volume->resource.parent_ops->wined3d_object_destroyed(volume->resource.parent);
414 HeapFree(GetProcessHeap(), 0, volume);
417 static void volume_unload(struct wined3d_resource *resource)
419 struct wined3d_volume *volume = volume_from_resource(resource);
420 struct wined3d_device *device = volume->resource.device;
421 struct wined3d_context *context;
423 if (volume->resource.pool == WINED3D_POOL_DEFAULT)
424 ERR("Unloading DEFAULT pool volume.\n");
426 TRACE("texture %p.\n", resource);
428 if (volume_prepare_system_memory(volume))
430 context = context_acquire(device, NULL);
431 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
432 context_release(context);
433 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_SYSMEM);
435 else
437 ERR("Out of memory when unloading volume %p.\n", volume);
438 wined3d_volume_validate_location(volume, WINED3D_LOCATION_DISCARDED);
439 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_DISCARDED);
442 if (volume->pbo)
444 /* Should not happen because only dynamic default pool volumes
445 * have a buffer, and those are not evicted by device_evit_managed_resources
446 * and must be freed before a non-ex device reset. */
447 ERR("Unloading a volume with a buffer\n");
448 wined3d_volume_free_pbo(volume);
451 /* The texture name is managed by the container. */
452 volume->flags &= ~WINED3D_VFLAG_CLIENT_STORAGE;
454 resource_unload(resource);
457 ULONG CDECL wined3d_volume_incref(struct wined3d_volume *volume)
459 TRACE("Forwarding to container %p.\n", volume->container);
461 return wined3d_texture_incref(volume->container);
464 ULONG CDECL wined3d_volume_decref(struct wined3d_volume *volume)
466 TRACE("Forwarding to container %p.\n", volume->container);
468 return wined3d_texture_decref(volume->container);
471 void * CDECL wined3d_volume_get_parent(const struct wined3d_volume *volume)
473 TRACE("volume %p.\n", volume);
475 return volume->resource.parent;
478 void CDECL wined3d_volume_preload(struct wined3d_volume *volume)
480 FIXME("volume %p stub!\n", volume);
483 struct wined3d_resource * CDECL wined3d_volume_get_resource(struct wined3d_volume *volume)
485 TRACE("volume %p.\n", volume);
487 return &volume->resource;
490 static BOOL volume_check_block_align(const struct wined3d_volume *volume,
491 const struct wined3d_box *box)
493 UINT width_mask, height_mask;
494 const struct wined3d_format *format = volume->resource.format;
496 if (!box)
497 return TRUE;
499 /* This assumes power of two block sizes, but NPOT block sizes would be
500 * silly anyway.
502 * This also assumes that the format's block depth is 1. */
503 width_mask = format->block_width - 1;
504 height_mask = format->block_height - 1;
506 if (box->left & width_mask)
507 return FALSE;
508 if (box->top & height_mask)
509 return FALSE;
510 if (box->right & width_mask && box->right != volume->resource.width)
511 return FALSE;
512 if (box->bottom & height_mask && box->bottom != volume->resource.height)
513 return FALSE;
515 return TRUE;
518 static BOOL wined3d_volume_check_box_dimensions(const struct wined3d_volume *volume,
519 const struct wined3d_box *box)
521 if (!box)
522 return TRUE;
524 if (box->left >= box->right)
525 return FALSE;
526 if (box->top >= box->bottom)
527 return FALSE;
528 if (box->front >= box->back)
529 return FALSE;
530 if (box->right > volume->resource.width)
531 return FALSE;
532 if (box->bottom > volume->resource.height)
533 return FALSE;
534 if (box->back > volume->resource.depth)
535 return FALSE;
537 return TRUE;
540 HRESULT CDECL wined3d_volume_map(struct wined3d_volume *volume,
541 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
543 struct wined3d_device *device = volume->resource.device;
544 struct wined3d_context *context;
545 const struct wined3d_gl_info *gl_info;
546 BYTE *base_memory;
547 const struct wined3d_format *format = volume->resource.format;
549 TRACE("volume %p, map_desc %p, box %p, flags %#x.\n",
550 volume, map_desc, box, flags);
552 map_desc->data = NULL;
553 if (!(volume->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU))
555 WARN("Volume %p is not CPU accessible.\n", volume);
556 return WINED3DERR_INVALIDCALL;
558 if (volume->resource.map_count)
560 WARN("Volume is already mapped.\n");
561 return WINED3DERR_INVALIDCALL;
563 if (!wined3d_volume_check_box_dimensions(volume, box))
565 WARN("Map box is invalid.\n");
566 return WINED3DERR_INVALIDCALL;
568 if ((format->flags & WINED3DFMT_FLAG_BLOCKS) && !volume_check_block_align(volume, box))
570 WARN("Map box is misaligned for %ux%u blocks.\n",
571 format->block_width, format->block_height);
572 return WINED3DERR_INVALIDCALL;
575 flags = wined3d_resource_sanitize_map_flags(&volume->resource, flags);
577 if (volume->resource.map_binding == WINED3D_LOCATION_BUFFER)
579 context = context_acquire(device, NULL);
580 gl_info = context->gl_info;
582 wined3d_volume_prepare_pbo(volume, context);
583 if (flags & WINED3D_MAP_DISCARD)
584 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
585 else
586 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_BUFFER);
588 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, volume->pbo));
590 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
592 GLbitfield mapflags = wined3d_resource_gl_map_flags(flags);
593 mapflags &= ~GL_MAP_FLUSH_EXPLICIT_BIT;
594 base_memory = GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER,
595 0, volume->resource.size, mapflags));
597 else
599 GLenum access = wined3d_resource_gl_legacy_map_flags(flags);
600 base_memory = GL_EXTCALL(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, access));
603 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
604 checkGLcall("Map PBO");
606 context_release(context);
608 else
610 if (!volume_prepare_system_memory(volume))
612 WARN("Out of memory.\n");
613 map_desc->data = NULL;
614 return E_OUTOFMEMORY;
617 if (flags & WINED3D_MAP_DISCARD)
619 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
621 else if (!(volume->locations & WINED3D_LOCATION_SYSMEM))
623 context = context_acquire(device, NULL);
624 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
625 context_release(context);
627 base_memory = volume->resource.heap_memory;
630 TRACE("Base memory pointer %p.\n", base_memory);
632 if (format->flags & WINED3DFMT_FLAG_BROKEN_PITCH)
634 map_desc->row_pitch = volume->resource.width * format->byte_count;
635 map_desc->slice_pitch = map_desc->row_pitch * volume->resource.height;
637 else
639 wined3d_volume_get_pitch(volume, &map_desc->row_pitch, &map_desc->slice_pitch);
642 if (!box)
644 TRACE("No box supplied - all is ok\n");
645 map_desc->data = base_memory;
647 else
649 TRACE("Lock Box (%p) = l %u, t %u, r %u, b %u, fr %u, ba %u\n",
650 box, box->left, box->top, box->right, box->bottom, box->front, box->back);
652 if ((format->flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
654 /* Compressed textures are block based, so calculate the offset of
655 * the block that contains the top-left pixel of the locked rectangle. */
656 map_desc->data = base_memory
657 + (box->front * map_desc->slice_pitch)
658 + ((box->top / format->block_height) * map_desc->row_pitch)
659 + ((box->left / format->block_width) * format->block_byte_count);
661 else
663 map_desc->data = base_memory
664 + (map_desc->slice_pitch * box->front)
665 + (map_desc->row_pitch * box->top)
666 + (box->left * volume->resource.format->byte_count);
670 if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
672 wined3d_texture_set_dirty(volume->container);
673 wined3d_volume_invalidate_location(volume, ~volume->resource.map_binding);
676 volume->resource.map_count++;
678 TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
679 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
681 return WINED3D_OK;
684 struct wined3d_volume * CDECL wined3d_volume_from_resource(struct wined3d_resource *resource)
686 return volume_from_resource(resource);
689 HRESULT CDECL wined3d_volume_unmap(struct wined3d_volume *volume)
691 TRACE("volume %p.\n", volume);
693 if (!volume->resource.map_count)
695 WARN("Trying to unlock an unlocked volume %p.\n", volume);
696 return WINED3DERR_INVALIDCALL;
699 if (volume->resource.map_binding == WINED3D_LOCATION_BUFFER)
701 struct wined3d_device *device = volume->resource.device;
702 struct wined3d_context *context = context_acquire(device, NULL);
703 const struct wined3d_gl_info *gl_info = context->gl_info;
705 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, volume->pbo));
706 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER));
707 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
708 checkGLcall("Unmap PBO");
710 context_release(context);
713 volume->resource.map_count--;
715 return WINED3D_OK;
718 static ULONG volume_resource_incref(struct wined3d_resource *resource)
720 return wined3d_volume_incref(volume_from_resource(resource));
723 static ULONG volume_resource_decref(struct wined3d_resource *resource)
725 return wined3d_volume_decref(volume_from_resource(resource));
728 static const struct wined3d_resource_ops volume_resource_ops =
730 volume_resource_incref,
731 volume_resource_decref,
732 volume_unload,
735 static HRESULT volume_init(struct wined3d_volume *volume, struct wined3d_texture *container,
736 const struct wined3d_resource_desc *desc, UINT level)
738 struct wined3d_device *device = container->resource.device;
739 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
740 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format);
741 HRESULT hr;
742 UINT size;
744 if (!gl_info->supported[EXT_TEXTURE3D])
746 WARN("Volume cannot be created - no volume texture support.\n");
747 return WINED3DERR_INVALIDCALL;
749 /* TODO: Write tests for other resources and move this check
750 * to resource_init, if applicable. */
751 if (desc->usage & WINED3DUSAGE_DYNAMIC
752 && (desc->pool == WINED3D_POOL_MANAGED || desc->pool == WINED3D_POOL_SCRATCH))
754 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
755 return WINED3DERR_INVALIDCALL;
758 size = wined3d_format_calculate_size(format, device->surface_alignment, desc->width, desc->height, desc->depth);
760 if (FAILED(hr = resource_init(&volume->resource, device, WINED3D_RTYPE_VOLUME, format,
761 WINED3D_MULTISAMPLE_NONE, 0, desc->usage, desc->pool, desc->width, desc->height, desc->depth,
762 size, NULL, &wined3d_null_parent_ops, &volume_resource_ops)))
764 WARN("Failed to initialize resource, returning %#x.\n", hr);
765 return hr;
768 volume->texture_level = level;
769 volume->locations = WINED3D_LOCATION_DISCARDED;
771 if (desc->pool == WINED3D_POOL_DEFAULT && desc->usage & WINED3DUSAGE_DYNAMIC
772 && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
773 && !format->convert)
775 wined3d_resource_free_sysmem(&volume->resource);
776 volume->resource.map_binding = WINED3D_LOCATION_BUFFER;
779 volume->container = container;
781 return WINED3D_OK;
784 HRESULT wined3d_volume_create(struct wined3d_texture *container, const struct wined3d_resource_desc *desc,
785 unsigned int level, struct wined3d_volume **volume)
787 struct wined3d_device_parent *device_parent = container->resource.device->device_parent;
788 const struct wined3d_parent_ops *parent_ops;
789 struct wined3d_volume *object;
790 void *parent;
791 HRESULT hr;
793 TRACE("container %p, width %u, height %u, depth %u, level %u, format %s, "
794 "usage %#x, pool %s, volume %p.\n",
795 container, desc->width, desc->height, desc->depth, level, debug_d3dformat(desc->format),
796 desc->usage, debug_d3dpool(desc->pool), volume);
798 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
799 return E_OUTOFMEMORY;
801 if (FAILED(hr = volume_init(object, container, desc, level)))
803 WARN("Failed to initialize volume, returning %#x.\n", hr);
804 HeapFree(GetProcessHeap(), 0, object);
805 return hr;
808 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
809 wined3d_texture_get_parent(container), object, &parent, &parent_ops)))
811 WARN("Failed to create volume parent, hr %#x.\n", hr);
812 wined3d_volume_destroy(object);
813 return hr;
816 TRACE("Created volume %p, parent %p, parent_ops %p.\n", object, parent, parent_ops);
818 object->resource.parent = parent;
819 object->resource.parent_ops = parent_ops;
820 *volume = object;
822 return WINED3D_OK;