wined3d: Handle slice pitch and alignment as well in wined3d_format_calculate_pitch().
[wine.git] / dlls / wined3d / volume.c
blob733b46868fffcdaacdba51122389a21a63d83f7e
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 wined3d_format_calculate_pitch(volume->resource.format, volume->resource.device->surface_alignment,
46 volume->resource.width, volume->resource.height, row_pitch, slice_pitch);
49 /* This call just uploads data, the caller is responsible for binding the
50 * correct texture. */
51 /* Context activation is done by the caller. */
52 void wined3d_volume_upload_data(struct wined3d_volume *volume, const struct wined3d_context *context,
53 const struct wined3d_const_bo_address *data)
55 const struct wined3d_gl_info *gl_info = context->gl_info;
56 const struct wined3d_format *format = volume->resource.format;
57 UINT width = volume->resource.width;
58 UINT height = volume->resource.height;
59 UINT depth = volume->resource.depth;
60 const void *mem = data->addr;
61 void *converted_mem = NULL;
63 TRACE("volume %p, context %p, level %u, format %s (%#x).\n",
64 volume, context, volume->texture_level, debug_d3dformat(format->id),
65 format->id);
67 if (format->convert)
69 UINT dst_row_pitch, dst_slice_pitch;
70 UINT src_row_pitch, src_slice_pitch;
72 if (data->buffer_object)
73 ERR("Loading a converted volume from a PBO.\n");
74 if (volume->container->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
75 ERR("Converting a block-based format.\n");
77 dst_row_pitch = width * format->conv_byte_count;
78 dst_slice_pitch = dst_row_pitch * height;
80 wined3d_volume_get_pitch(volume, &src_row_pitch, &src_slice_pitch);
82 converted_mem = HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch * depth);
83 format->convert(data->addr, converted_mem, src_row_pitch, src_slice_pitch,
84 dst_row_pitch, dst_slice_pitch, width, height, depth);
85 mem = converted_mem;
88 if (data->buffer_object)
90 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
91 checkGLcall("glBindBuffer");
94 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D, volume->texture_level, 0, 0, 0,
95 width, height, depth,
96 format->glFormat, format->glType, mem));
97 checkGLcall("glTexSubImage3D");
99 if (data->buffer_object)
101 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
102 checkGLcall("glBindBuffer");
105 HeapFree(GetProcessHeap(), 0, converted_mem);
108 void wined3d_volume_validate_location(struct wined3d_volume *volume, DWORD location)
110 TRACE("Volume %p, setting %s.\n", volume, wined3d_debug_location(location));
111 volume->locations |= location;
112 TRACE("new location flags are %s.\n", wined3d_debug_location(volume->locations));
115 void wined3d_volume_invalidate_location(struct wined3d_volume *volume, DWORD location)
117 TRACE("Volume %p, clearing %s.\n", volume, wined3d_debug_location(location));
118 volume->locations &= ~location;
119 TRACE("new location flags are %s.\n", wined3d_debug_location(volume->locations));
122 /* Context activation is done by the caller. */
123 static void wined3d_volume_download_data(struct wined3d_volume *volume,
124 const struct wined3d_context *context, const struct wined3d_bo_address *data)
126 const struct wined3d_gl_info *gl_info = context->gl_info;
127 const struct wined3d_format *format = volume->resource.format;
129 if (format->convert)
131 FIXME("Attempting to download a converted volume, format %s.\n",
132 debug_d3dformat(format->id));
133 return;
136 if (data->buffer_object)
138 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
139 checkGLcall("glBindBuffer");
142 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, volume->texture_level,
143 format->glFormat, format->glType, data->addr);
144 checkGLcall("glGetTexImage");
146 if (data->buffer_object)
148 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
149 checkGLcall("glBindBuffer");
154 static void wined3d_volume_evict_sysmem(struct wined3d_volume *volume)
156 wined3d_resource_free_sysmem(&volume->resource);
157 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_SYSMEM);
160 static DWORD volume_access_from_location(DWORD location)
162 switch (location)
164 case WINED3D_LOCATION_DISCARDED:
165 return 0;
167 case WINED3D_LOCATION_SYSMEM:
168 return WINED3D_RESOURCE_ACCESS_CPU;
170 case WINED3D_LOCATION_BUFFER:
171 case WINED3D_LOCATION_TEXTURE_RGB:
172 case WINED3D_LOCATION_TEXTURE_SRGB:
173 return WINED3D_RESOURCE_ACCESS_GPU;
175 default:
176 FIXME("Unhandled location %#x.\n", location);
177 return 0;
181 /* Context activation is done by the caller. */
182 static void wined3d_volume_srgb_transfer(struct wined3d_volume *volume,
183 struct wined3d_context *context, BOOL dest_is_srgb)
185 struct wined3d_bo_address data;
186 /* Optimizations are possible, but the effort should be put into either
187 * implementing EXT_SRGB_DECODE in the driver or finding out why we
188 * picked the wrong copy for the original upload and fixing that.
190 * Also keep in mind that we want to avoid using resource.heap_memory
191 * for DEFAULT pool surfaces. */
193 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
194 data.buffer_object = 0;
195 data.addr = HeapAlloc(GetProcessHeap(), 0, volume->resource.size);
196 if (!data.addr)
197 return;
199 wined3d_texture_bind_and_dirtify(volume->container, context, !dest_is_srgb);
200 wined3d_volume_download_data(volume, context, &data);
201 wined3d_texture_bind_and_dirtify(volume->container, context, dest_is_srgb);
202 wined3d_volume_upload_data(volume, context, wined3d_const_bo_address(&data));
204 HeapFree(GetProcessHeap(), 0, data.addr);
207 static BOOL wined3d_volume_can_evict(const struct wined3d_volume *volume)
209 if (volume->resource.pool != WINED3D_POOL_MANAGED)
210 return FALSE;
211 if (volume->download_count >= 10)
212 return FALSE;
213 if (volume->resource.format->convert)
214 return FALSE;
216 return TRUE;
218 /* Context activation is done by the caller. */
219 static void wined3d_volume_load_location(struct wined3d_volume *volume,
220 struct wined3d_context *context, DWORD location)
222 DWORD required_access = volume_access_from_location(location);
224 TRACE("Volume %p, loading %s, have %s.\n", volume, wined3d_debug_location(location),
225 wined3d_debug_location(volume->locations));
227 if ((volume->locations & location) == location)
229 TRACE("Location(s) already up to date.\n");
230 return;
233 if ((volume->resource.access_flags & required_access) != required_access)
235 ERR("Operation requires %#x access, but volume only has %#x.\n",
236 required_access, volume->resource.access_flags);
237 return;
240 switch (location)
242 case WINED3D_LOCATION_TEXTURE_RGB:
243 case WINED3D_LOCATION_TEXTURE_SRGB:
244 if ((location == WINED3D_LOCATION_TEXTURE_RGB
245 && !(volume->container->flags & WINED3D_TEXTURE_RGB_ALLOCATED))
246 || (location == WINED3D_LOCATION_TEXTURE_SRGB
247 && !(volume->container->flags & WINED3D_TEXTURE_SRGB_ALLOCATED)))
248 ERR("Trying to load (s)RGB texture without prior allocation.\n");
250 if (volume->locations & WINED3D_LOCATION_DISCARDED)
252 TRACE("Volume previously discarded, nothing to do.\n");
253 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
255 else if (volume->locations & WINED3D_LOCATION_SYSMEM)
257 struct wined3d_const_bo_address data = {0, volume->resource.heap_memory};
258 wined3d_texture_bind_and_dirtify(volume->container, context,
259 location == WINED3D_LOCATION_TEXTURE_SRGB);
260 wined3d_volume_upload_data(volume, context, &data);
262 else if (volume->locations & WINED3D_LOCATION_BUFFER)
264 struct wined3d_const_bo_address data = {volume->pbo, NULL};
265 wined3d_texture_bind_and_dirtify(volume->container, context,
266 location == WINED3D_LOCATION_TEXTURE_SRGB);
267 wined3d_volume_upload_data(volume, context, &data);
269 else if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
271 wined3d_volume_srgb_transfer(volume, context, TRUE);
273 else if (volume->locations & WINED3D_LOCATION_TEXTURE_SRGB)
275 wined3d_volume_srgb_transfer(volume, context, FALSE);
277 else
279 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume->locations));
280 return;
282 wined3d_volume_validate_location(volume, location);
284 if (wined3d_volume_can_evict(volume))
285 wined3d_volume_evict_sysmem(volume);
287 break;
289 case WINED3D_LOCATION_SYSMEM:
290 if (!volume->resource.heap_memory)
291 ERR("Trying to load WINED3D_LOCATION_SYSMEM without setting it up first.\n");
293 if (volume->locations & WINED3D_LOCATION_DISCARDED)
295 TRACE("Volume previously discarded, nothing to do.\n");
296 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
298 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
300 struct wined3d_bo_address data = {0, volume->resource.heap_memory};
302 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
303 wined3d_texture_bind_and_dirtify(volume->container, context, FALSE);
304 else
305 wined3d_texture_bind_and_dirtify(volume->container, context, TRUE);
307 volume->download_count++;
308 wined3d_volume_download_data(volume, context, &data);
310 else
312 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
313 wined3d_debug_location(volume->locations));
314 return;
316 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
317 break;
319 case WINED3D_LOCATION_BUFFER:
320 if (!volume->pbo)
321 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
323 if (volume->locations & WINED3D_LOCATION_DISCARDED)
325 TRACE("Volume previously discarded, nothing to do.\n");
326 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
328 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
330 struct wined3d_bo_address data = {volume->pbo, NULL};
332 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
333 wined3d_texture_bind_and_dirtify(volume->container, context, FALSE);
334 else
335 wined3d_texture_bind_and_dirtify(volume->container, context, TRUE);
337 wined3d_volume_download_data(volume, context, &data);
339 else
341 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
342 wined3d_debug_location(volume->locations));
343 return;
345 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
346 break;
348 default:
349 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
350 wined3d_debug_location(volume->locations));
354 /* Context activation is done by the caller. */
355 void wined3d_volume_load(struct wined3d_volume *volume, struct wined3d_context *context, BOOL srgb_mode)
357 wined3d_texture_prepare_texture(volume->container, context, srgb_mode);
358 wined3d_volume_load_location(volume, context,
359 srgb_mode ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB);
362 /* Context activation is done by the caller. */
363 static void wined3d_volume_prepare_pbo(struct wined3d_volume *volume, struct wined3d_context *context)
365 const struct wined3d_gl_info *gl_info = context->gl_info;
367 if (volume->pbo)
368 return;
370 GL_EXTCALL(glGenBuffers(1, &volume->pbo));
371 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, volume->pbo));
372 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER, volume->resource.size, NULL, GL_STREAM_DRAW));
373 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
374 checkGLcall("Create PBO");
376 TRACE("Created PBO %u for volume %p.\n", volume->pbo, volume);
379 static void wined3d_volume_free_pbo(struct wined3d_volume *volume)
381 struct wined3d_context *context = context_acquire(volume->resource.device, NULL);
382 const struct wined3d_gl_info *gl_info = context->gl_info;
384 TRACE("Deleting PBO %u belonging to volume %p.\n", volume->pbo, volume);
385 GL_EXTCALL(glDeleteBuffers(1, &volume->pbo));
386 checkGLcall("glDeleteBuffers");
387 volume->pbo = 0;
388 context_release(context);
391 void wined3d_volume_destroy(struct wined3d_volume *volume)
393 TRACE("volume %p.\n", volume);
395 if (volume->pbo)
396 wined3d_volume_free_pbo(volume);
398 resource_cleanup(&volume->resource);
399 volume->resource.parent_ops->wined3d_object_destroyed(volume->resource.parent);
400 HeapFree(GetProcessHeap(), 0, volume);
403 static void volume_unload(struct wined3d_resource *resource)
405 struct wined3d_volume *volume = volume_from_resource(resource);
406 struct wined3d_device *device = volume->resource.device;
407 struct wined3d_context *context;
409 if (volume->resource.pool == WINED3D_POOL_DEFAULT)
410 ERR("Unloading DEFAULT pool volume.\n");
412 TRACE("texture %p.\n", resource);
414 if (volume_prepare_system_memory(volume))
416 context = context_acquire(device, NULL);
417 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
418 context_release(context);
419 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_SYSMEM);
421 else
423 ERR("Out of memory when unloading volume %p.\n", volume);
424 wined3d_volume_validate_location(volume, WINED3D_LOCATION_DISCARDED);
425 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_DISCARDED);
428 if (volume->pbo)
430 /* Should not happen because only dynamic default pool volumes
431 * have a buffer, and those are not evicted by device_evit_managed_resources
432 * and must be freed before a non-ex device reset. */
433 ERR("Unloading a volume with a buffer\n");
434 wined3d_volume_free_pbo(volume);
437 /* The texture name is managed by the container. */
439 resource_unload(resource);
442 static BOOL volume_check_block_align(const struct wined3d_volume *volume,
443 const struct wined3d_box *box)
445 UINT width_mask, height_mask;
446 const struct wined3d_format *format = volume->resource.format;
448 if (!box)
449 return TRUE;
451 /* This assumes power of two block sizes, but NPOT block sizes would be
452 * silly anyway.
454 * This also assumes that the format's block depth is 1. */
455 width_mask = format->block_width - 1;
456 height_mask = format->block_height - 1;
458 if (box->left & width_mask)
459 return FALSE;
460 if (box->top & height_mask)
461 return FALSE;
462 if (box->right & width_mask && box->right != volume->resource.width)
463 return FALSE;
464 if (box->bottom & height_mask && box->bottom != volume->resource.height)
465 return FALSE;
467 return TRUE;
470 static BOOL wined3d_volume_check_box_dimensions(const struct wined3d_volume *volume,
471 const struct wined3d_box *box)
473 if (!box)
474 return TRUE;
476 if (box->left >= box->right)
477 return FALSE;
478 if (box->top >= box->bottom)
479 return FALSE;
480 if (box->front >= box->back)
481 return FALSE;
482 if (box->right > volume->resource.width)
483 return FALSE;
484 if (box->bottom > volume->resource.height)
485 return FALSE;
486 if (box->back > volume->resource.depth)
487 return FALSE;
489 return TRUE;
492 HRESULT wined3d_volume_map(struct wined3d_volume *volume,
493 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
495 struct wined3d_device *device = volume->resource.device;
496 struct wined3d_context *context;
497 const struct wined3d_gl_info *gl_info;
498 BYTE *base_memory;
499 const struct wined3d_format *format = volume->resource.format;
500 const unsigned int fmt_flags = volume->container->resource.format_flags;
502 TRACE("volume %p, map_desc %p, box %s, flags %#x.\n",
503 volume, map_desc, debug_box(box), flags);
505 map_desc->data = NULL;
506 if (!(volume->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU))
508 WARN("Volume %p is not CPU accessible.\n", volume);
509 return WINED3DERR_INVALIDCALL;
511 if (volume->resource.map_count)
513 WARN("Volume is already mapped.\n");
514 return WINED3DERR_INVALIDCALL;
516 if (!wined3d_volume_check_box_dimensions(volume, box))
518 WARN("Map box is invalid.\n");
519 return WINED3DERR_INVALIDCALL;
521 if ((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && !volume_check_block_align(volume, box))
523 WARN("Map box %s is misaligned for %ux%u blocks.\n",
524 debug_box(box), format->block_width, format->block_height);
525 return WINED3DERR_INVALIDCALL;
528 flags = wined3d_resource_sanitize_map_flags(&volume->resource, flags);
530 if (volume->resource.map_binding == WINED3D_LOCATION_BUFFER)
532 context = context_acquire(device, NULL);
533 gl_info = context->gl_info;
535 wined3d_volume_prepare_pbo(volume, context);
536 if (flags & WINED3D_MAP_DISCARD)
537 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
538 else
539 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_BUFFER);
541 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, volume->pbo));
543 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
545 GLbitfield mapflags = wined3d_resource_gl_map_flags(flags);
546 mapflags &= ~GL_MAP_FLUSH_EXPLICIT_BIT;
547 base_memory = GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER,
548 0, volume->resource.size, mapflags));
550 else
552 GLenum access = wined3d_resource_gl_legacy_map_flags(flags);
553 base_memory = GL_EXTCALL(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, access));
556 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
557 checkGLcall("Map PBO");
559 context_release(context);
561 else
563 if (!volume_prepare_system_memory(volume))
565 WARN("Out of memory.\n");
566 map_desc->data = NULL;
567 return E_OUTOFMEMORY;
570 if (flags & WINED3D_MAP_DISCARD)
572 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
574 else if (!(volume->locations & WINED3D_LOCATION_SYSMEM))
576 context = context_acquire(device, NULL);
577 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
578 context_release(context);
580 base_memory = volume->resource.heap_memory;
583 TRACE("Base memory pointer %p.\n", base_memory);
585 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
587 map_desc->row_pitch = volume->resource.width * format->byte_count;
588 map_desc->slice_pitch = map_desc->row_pitch * volume->resource.height;
590 else
592 wined3d_volume_get_pitch(volume, &map_desc->row_pitch, &map_desc->slice_pitch);
595 if (!box)
597 map_desc->data = base_memory;
599 else
601 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
603 /* Compressed textures are block based, so calculate the offset of
604 * the block that contains the top-left pixel of the locked rectangle. */
605 map_desc->data = base_memory
606 + (box->front * map_desc->slice_pitch)
607 + ((box->top / format->block_height) * map_desc->row_pitch)
608 + ((box->left / format->block_width) * format->block_byte_count);
610 else
612 map_desc->data = base_memory
613 + (map_desc->slice_pitch * box->front)
614 + (map_desc->row_pitch * box->top)
615 + (box->left * volume->resource.format->byte_count);
619 if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
621 wined3d_texture_set_dirty(volume->container);
622 wined3d_volume_invalidate_location(volume, ~volume->resource.map_binding);
625 volume->resource.map_count++;
627 TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
628 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
630 return WINED3D_OK;
633 HRESULT wined3d_volume_unmap(struct wined3d_volume *volume)
635 TRACE("volume %p.\n", volume);
637 if (!volume->resource.map_count)
639 WARN("Trying to unlock an unlocked volume %p.\n", volume);
640 return WINED3DERR_INVALIDCALL;
643 if (volume->resource.map_binding == WINED3D_LOCATION_BUFFER)
645 struct wined3d_device *device = volume->resource.device;
646 struct wined3d_context *context = context_acquire(device, NULL);
647 const struct wined3d_gl_info *gl_info = context->gl_info;
649 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, volume->pbo));
650 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER));
651 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
652 checkGLcall("Unmap PBO");
654 context_release(context);
657 volume->resource.map_count--;
659 return WINED3D_OK;
662 static ULONG volume_resource_incref(struct wined3d_resource *resource)
664 struct wined3d_volume *volume = volume_from_resource(resource);
665 TRACE("Forwarding to container %p.\n", volume->container);
667 return wined3d_texture_incref(volume->container);
670 static ULONG volume_resource_decref(struct wined3d_resource *resource)
672 struct wined3d_volume *volume = volume_from_resource(resource);
673 TRACE("Forwarding to container %p.\n", volume->container);
675 return wined3d_texture_decref(volume->container);
678 static HRESULT volume_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
679 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
681 ERR("Not supported on sub-resources.\n");
682 return WINED3DERR_INVALIDCALL;
685 static HRESULT volume_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
687 ERR("Not supported on sub-resources.\n");
688 return WINED3DERR_INVALIDCALL;
691 static const struct wined3d_resource_ops volume_resource_ops =
693 volume_resource_incref,
694 volume_resource_decref,
695 volume_unload,
696 volume_resource_sub_resource_map,
697 volume_resource_sub_resource_unmap,
700 static HRESULT volume_init(struct wined3d_volume *volume, struct wined3d_texture *container,
701 const struct wined3d_resource_desc *desc, UINT level)
703 struct wined3d_device *device = container->resource.device;
704 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
705 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format);
706 HRESULT hr;
707 UINT size;
709 /* TODO: Write tests for other resources and move this check
710 * to resource_init, if applicable. */
711 if (desc->usage & WINED3DUSAGE_DYNAMIC
712 && (desc->pool == WINED3D_POOL_MANAGED || desc->pool == WINED3D_POOL_SCRATCH))
714 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
715 return WINED3DERR_INVALIDCALL;
718 size = wined3d_format_calculate_size(format, device->surface_alignment, desc->width, desc->height, desc->depth);
720 if (FAILED(hr = resource_init(&volume->resource, device, WINED3D_RTYPE_VOLUME, format,
721 WINED3D_MULTISAMPLE_NONE, 0, desc->usage, desc->pool, desc->width, desc->height, desc->depth,
722 size, NULL, &wined3d_null_parent_ops, &volume_resource_ops)))
724 WARN("Failed to initialize resource, returning %#x.\n", hr);
725 return hr;
728 volume->texture_level = level;
729 volume->locations = WINED3D_LOCATION_DISCARDED;
731 if (desc->pool == WINED3D_POOL_DEFAULT && desc->usage & WINED3DUSAGE_DYNAMIC
732 && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
733 && !format->convert)
735 wined3d_resource_free_sysmem(&volume->resource);
736 volume->resource.map_binding = WINED3D_LOCATION_BUFFER;
739 volume->container = container;
741 return WINED3D_OK;
744 HRESULT wined3d_volume_create(struct wined3d_texture *container, const struct wined3d_resource_desc *desc,
745 unsigned int level, struct wined3d_volume **volume)
747 struct wined3d_device_parent *device_parent = container->resource.device->device_parent;
748 const struct wined3d_parent_ops *parent_ops;
749 struct wined3d_volume *object;
750 void *parent;
751 HRESULT hr;
753 TRACE("container %p, width %u, height %u, depth %u, level %u, format %s, "
754 "usage %#x, pool %s, volume %p.\n",
755 container, desc->width, desc->height, desc->depth, level, debug_d3dformat(desc->format),
756 desc->usage, debug_d3dpool(desc->pool), volume);
758 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
759 return E_OUTOFMEMORY;
761 if (FAILED(hr = volume_init(object, container, desc, level)))
763 WARN("Failed to initialize volume, returning %#x.\n", hr);
764 HeapFree(GetProcessHeap(), 0, object);
765 return hr;
768 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
769 container, level, &parent, &parent_ops)))
771 WARN("Failed to create volume parent, hr %#x.\n", hr);
772 wined3d_volume_destroy(object);
773 return hr;
776 TRACE("Created volume %p, parent %p, parent_ops %p.\n", object, parent, parent_ops);
778 object->resource.parent = parent;
779 object->resource.parent_ops = parent_ops;
780 *volume = object;
782 return WINED3D_OK;