wined3d: Use wined3d_texture_get_pitch() in wined3d_volume_map().
[wine.git] / dlls / wined3d / volume.c
blob75585d83814456b577abdd665cef7d794d5e73a5
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 /* This call just uploads data, the caller is responsible for binding the
44 * correct texture. */
45 /* Context activation is done by the caller. */
46 void wined3d_volume_upload_data(struct wined3d_volume *volume, const struct wined3d_context *context,
47 const struct wined3d_const_bo_address *data)
49 const struct wined3d_gl_info *gl_info = context->gl_info;
50 const struct wined3d_format *format = volume->resource.format;
51 UINT width = volume->resource.width;
52 UINT height = volume->resource.height;
53 UINT depth = volume->resource.depth;
54 const void *mem = data->addr;
55 void *converted_mem = NULL;
57 TRACE("volume %p, context %p, level %u, format %s (%#x).\n",
58 volume, context, volume->texture_level, debug_d3dformat(format->id),
59 format->id);
61 if (format->convert)
63 UINT dst_row_pitch, dst_slice_pitch;
64 UINT src_row_pitch, src_slice_pitch;
66 if (data->buffer_object)
67 ERR("Loading a converted volume from a PBO.\n");
68 if (volume->container->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
69 ERR("Converting a block-based format.\n");
71 dst_row_pitch = width * format->conv_byte_count;
72 dst_slice_pitch = dst_row_pitch * height;
74 wined3d_texture_get_pitch(volume->container, volume->texture_level, &src_row_pitch, &src_slice_pitch);
76 converted_mem = HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch * depth);
77 format->convert(data->addr, converted_mem, src_row_pitch, src_slice_pitch,
78 dst_row_pitch, dst_slice_pitch, width, height, depth);
79 mem = converted_mem;
82 if (data->buffer_object)
84 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
85 checkGLcall("glBindBuffer");
88 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D, volume->texture_level, 0, 0, 0,
89 width, height, depth,
90 format->glFormat, format->glType, mem));
91 checkGLcall("glTexSubImage3D");
93 if (data->buffer_object)
95 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
96 checkGLcall("glBindBuffer");
99 HeapFree(GetProcessHeap(), 0, converted_mem);
102 void wined3d_volume_validate_location(struct wined3d_volume *volume, DWORD location)
104 TRACE("Volume %p, setting %s.\n", volume, wined3d_debug_location(location));
105 volume->locations |= location;
106 TRACE("new location flags are %s.\n", wined3d_debug_location(volume->locations));
109 void wined3d_volume_invalidate_location(struct wined3d_volume *volume, DWORD location)
111 TRACE("Volume %p, clearing %s.\n", volume, wined3d_debug_location(location));
112 volume->locations &= ~location;
113 TRACE("new location flags are %s.\n", wined3d_debug_location(volume->locations));
116 /* Context activation is done by the caller. */
117 static void wined3d_volume_download_data(struct wined3d_volume *volume,
118 const struct wined3d_context *context, const struct wined3d_bo_address *data)
120 const struct wined3d_gl_info *gl_info = context->gl_info;
121 const struct wined3d_format *format = volume->resource.format;
123 if (format->convert)
125 FIXME("Attempting to download a converted volume, format %s.\n",
126 debug_d3dformat(format->id));
127 return;
130 if (data->buffer_object)
132 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
133 checkGLcall("glBindBuffer");
136 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, volume->texture_level,
137 format->glFormat, format->glType, data->addr);
138 checkGLcall("glGetTexImage");
140 if (data->buffer_object)
142 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
143 checkGLcall("glBindBuffer");
148 static void wined3d_volume_evict_sysmem(struct wined3d_volume *volume)
150 wined3d_resource_free_sysmem(&volume->resource);
151 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_SYSMEM);
154 static DWORD volume_access_from_location(DWORD location)
156 switch (location)
158 case WINED3D_LOCATION_DISCARDED:
159 return 0;
161 case WINED3D_LOCATION_SYSMEM:
162 return WINED3D_RESOURCE_ACCESS_CPU;
164 case WINED3D_LOCATION_BUFFER:
165 case WINED3D_LOCATION_TEXTURE_RGB:
166 case WINED3D_LOCATION_TEXTURE_SRGB:
167 return WINED3D_RESOURCE_ACCESS_GPU;
169 default:
170 FIXME("Unhandled location %#x.\n", location);
171 return 0;
175 /* Context activation is done by the caller. */
176 static void wined3d_volume_srgb_transfer(struct wined3d_volume *volume,
177 struct wined3d_context *context, BOOL dest_is_srgb)
179 struct wined3d_bo_address data;
180 /* Optimizations are possible, but the effort should be put into either
181 * implementing EXT_SRGB_DECODE in the driver or finding out why we
182 * picked the wrong copy for the original upload and fixing that.
184 * Also keep in mind that we want to avoid using resource.heap_memory
185 * for DEFAULT pool surfaces. */
187 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
188 data.buffer_object = 0;
189 data.addr = HeapAlloc(GetProcessHeap(), 0, volume->resource.size);
190 if (!data.addr)
191 return;
193 wined3d_texture_bind_and_dirtify(volume->container, context, !dest_is_srgb);
194 wined3d_volume_download_data(volume, context, &data);
195 wined3d_texture_bind_and_dirtify(volume->container, context, dest_is_srgb);
196 wined3d_volume_upload_data(volume, context, wined3d_const_bo_address(&data));
198 HeapFree(GetProcessHeap(), 0, data.addr);
201 static BOOL wined3d_volume_can_evict(const struct wined3d_volume *volume)
203 if (volume->resource.pool != WINED3D_POOL_MANAGED)
204 return FALSE;
205 if (volume->download_count >= 10)
206 return FALSE;
207 if (volume->resource.format->convert)
208 return FALSE;
210 return TRUE;
212 /* Context activation is done by the caller. */
213 static void wined3d_volume_load_location(struct wined3d_volume *volume,
214 struct wined3d_context *context, DWORD location)
216 DWORD required_access = volume_access_from_location(location);
218 TRACE("Volume %p, loading %s, have %s.\n", volume, wined3d_debug_location(location),
219 wined3d_debug_location(volume->locations));
221 if ((volume->locations & location) == location)
223 TRACE("Location(s) already up to date.\n");
224 return;
227 if ((volume->resource.access_flags & required_access) != required_access)
229 ERR("Operation requires %#x access, but volume only has %#x.\n",
230 required_access, volume->resource.access_flags);
231 return;
234 switch (location)
236 case WINED3D_LOCATION_TEXTURE_RGB:
237 case WINED3D_LOCATION_TEXTURE_SRGB:
238 if ((location == WINED3D_LOCATION_TEXTURE_RGB
239 && !(volume->container->flags & WINED3D_TEXTURE_RGB_ALLOCATED))
240 || (location == WINED3D_LOCATION_TEXTURE_SRGB
241 && !(volume->container->flags & WINED3D_TEXTURE_SRGB_ALLOCATED)))
242 ERR("Trying to load (s)RGB texture without prior allocation.\n");
244 if (volume->locations & WINED3D_LOCATION_DISCARDED)
246 TRACE("Volume previously discarded, nothing to do.\n");
247 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
249 else if (volume->locations & WINED3D_LOCATION_SYSMEM)
251 struct wined3d_const_bo_address data = {0, volume->resource.heap_memory};
252 wined3d_texture_bind_and_dirtify(volume->container, context,
253 location == WINED3D_LOCATION_TEXTURE_SRGB);
254 wined3d_volume_upload_data(volume, context, &data);
256 else if (volume->locations & WINED3D_LOCATION_BUFFER)
258 struct wined3d_const_bo_address data = {volume->pbo, NULL};
259 wined3d_texture_bind_and_dirtify(volume->container, context,
260 location == WINED3D_LOCATION_TEXTURE_SRGB);
261 wined3d_volume_upload_data(volume, context, &data);
263 else if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
265 wined3d_volume_srgb_transfer(volume, context, TRUE);
267 else if (volume->locations & WINED3D_LOCATION_TEXTURE_SRGB)
269 wined3d_volume_srgb_transfer(volume, context, FALSE);
271 else
273 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume->locations));
274 return;
276 wined3d_volume_validate_location(volume, location);
278 if (wined3d_volume_can_evict(volume))
279 wined3d_volume_evict_sysmem(volume);
281 break;
283 case WINED3D_LOCATION_SYSMEM:
284 if (!volume->resource.heap_memory)
285 ERR("Trying to load WINED3D_LOCATION_SYSMEM without setting it up first.\n");
287 if (volume->locations & WINED3D_LOCATION_DISCARDED)
289 TRACE("Volume previously discarded, nothing to do.\n");
290 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
292 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
294 struct wined3d_bo_address data = {0, volume->resource.heap_memory};
296 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
297 wined3d_texture_bind_and_dirtify(volume->container, context, FALSE);
298 else
299 wined3d_texture_bind_and_dirtify(volume->container, context, TRUE);
301 volume->download_count++;
302 wined3d_volume_download_data(volume, context, &data);
304 else
306 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
307 wined3d_debug_location(volume->locations));
308 return;
310 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
311 break;
313 case WINED3D_LOCATION_BUFFER:
314 if (!volume->pbo)
315 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
317 if (volume->locations & WINED3D_LOCATION_DISCARDED)
319 TRACE("Volume previously discarded, nothing to do.\n");
320 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
322 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
324 struct wined3d_bo_address data = {volume->pbo, NULL};
326 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
327 wined3d_texture_bind_and_dirtify(volume->container, context, FALSE);
328 else
329 wined3d_texture_bind_and_dirtify(volume->container, context, TRUE);
331 wined3d_volume_download_data(volume, context, &data);
333 else
335 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
336 wined3d_debug_location(volume->locations));
337 return;
339 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
340 break;
342 default:
343 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
344 wined3d_debug_location(volume->locations));
348 /* Context activation is done by the caller. */
349 void wined3d_volume_load(struct wined3d_volume *volume, struct wined3d_context *context, BOOL srgb_mode)
351 wined3d_texture_prepare_texture(volume->container, context, srgb_mode);
352 wined3d_volume_load_location(volume, context,
353 srgb_mode ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB);
356 /* Context activation is done by the caller. */
357 static void wined3d_volume_prepare_pbo(struct wined3d_volume *volume, struct wined3d_context *context)
359 const struct wined3d_gl_info *gl_info = context->gl_info;
361 if (volume->pbo)
362 return;
364 GL_EXTCALL(glGenBuffers(1, &volume->pbo));
365 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, volume->pbo));
366 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER, volume->resource.size, NULL, GL_STREAM_DRAW));
367 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
368 checkGLcall("Create PBO");
370 TRACE("Created PBO %u for volume %p.\n", volume->pbo, volume);
373 static void wined3d_volume_free_pbo(struct wined3d_volume *volume)
375 struct wined3d_context *context = context_acquire(volume->resource.device, NULL);
376 const struct wined3d_gl_info *gl_info = context->gl_info;
378 TRACE("Deleting PBO %u belonging to volume %p.\n", volume->pbo, volume);
379 GL_EXTCALL(glDeleteBuffers(1, &volume->pbo));
380 checkGLcall("glDeleteBuffers");
381 volume->pbo = 0;
382 context_release(context);
385 void wined3d_volume_destroy(struct wined3d_volume *volume)
387 TRACE("volume %p.\n", volume);
389 if (volume->pbo)
390 wined3d_volume_free_pbo(volume);
392 resource_cleanup(&volume->resource);
393 volume->resource.parent_ops->wined3d_object_destroyed(volume->resource.parent);
394 HeapFree(GetProcessHeap(), 0, volume);
397 static void volume_unload(struct wined3d_resource *resource)
399 struct wined3d_volume *volume = volume_from_resource(resource);
400 struct wined3d_device *device = volume->resource.device;
401 struct wined3d_context *context;
403 if (volume->resource.pool == WINED3D_POOL_DEFAULT)
404 ERR("Unloading DEFAULT pool volume.\n");
406 TRACE("texture %p.\n", resource);
408 if (volume_prepare_system_memory(volume))
410 context = context_acquire(device, NULL);
411 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
412 context_release(context);
413 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_SYSMEM);
415 else
417 ERR("Out of memory when unloading volume %p.\n", volume);
418 wined3d_volume_validate_location(volume, WINED3D_LOCATION_DISCARDED);
419 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_DISCARDED);
422 if (volume->pbo)
424 /* Should not happen because only dynamic default pool volumes
425 * have a buffer, and those are not evicted by device_evit_managed_resources
426 * and must be freed before a non-ex device reset. */
427 ERR("Unloading a volume with a buffer\n");
428 wined3d_volume_free_pbo(volume);
431 /* The texture name is managed by the container. */
433 resource_unload(resource);
436 static BOOL volume_check_block_align(const struct wined3d_volume *volume,
437 const struct wined3d_box *box)
439 UINT width_mask, height_mask;
440 const struct wined3d_format *format = volume->resource.format;
442 if (!box)
443 return TRUE;
445 /* This assumes power of two block sizes, but NPOT block sizes would be
446 * silly anyway.
448 * This also assumes that the format's block depth is 1. */
449 width_mask = format->block_width - 1;
450 height_mask = format->block_height - 1;
452 if (box->left & width_mask)
453 return FALSE;
454 if (box->top & height_mask)
455 return FALSE;
456 if (box->right & width_mask && box->right != volume->resource.width)
457 return FALSE;
458 if (box->bottom & height_mask && box->bottom != volume->resource.height)
459 return FALSE;
461 return TRUE;
464 static BOOL wined3d_volume_check_box_dimensions(const struct wined3d_volume *volume,
465 const struct wined3d_box *box)
467 if (!box)
468 return TRUE;
470 if (box->left >= box->right)
471 return FALSE;
472 if (box->top >= box->bottom)
473 return FALSE;
474 if (box->front >= box->back)
475 return FALSE;
476 if (box->right > volume->resource.width)
477 return FALSE;
478 if (box->bottom > volume->resource.height)
479 return FALSE;
480 if (box->back > volume->resource.depth)
481 return FALSE;
483 return TRUE;
486 HRESULT wined3d_volume_map(struct wined3d_volume *volume,
487 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
489 struct wined3d_device *device = volume->resource.device;
490 struct wined3d_context *context;
491 const struct wined3d_gl_info *gl_info;
492 BYTE *base_memory;
493 const struct wined3d_format *format = volume->resource.format;
494 const unsigned int fmt_flags = volume->container->resource.format_flags;
496 TRACE("volume %p, map_desc %p, box %s, flags %#x.\n",
497 volume, map_desc, debug_box(box), flags);
499 map_desc->data = NULL;
500 if (!(volume->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU))
502 WARN("Volume %p is not CPU accessible.\n", volume);
503 return WINED3DERR_INVALIDCALL;
505 if (volume->resource.map_count)
507 WARN("Volume is already mapped.\n");
508 return WINED3DERR_INVALIDCALL;
510 if (!wined3d_volume_check_box_dimensions(volume, box))
512 WARN("Map box is invalid.\n");
513 return WINED3DERR_INVALIDCALL;
515 if ((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && !volume_check_block_align(volume, box))
517 WARN("Map box %s is misaligned for %ux%u blocks.\n",
518 debug_box(box), format->block_width, format->block_height);
519 return WINED3DERR_INVALIDCALL;
522 flags = wined3d_resource_sanitize_map_flags(&volume->resource, flags);
524 if (volume->resource.map_binding == WINED3D_LOCATION_BUFFER)
526 context = context_acquire(device, NULL);
527 gl_info = context->gl_info;
529 wined3d_volume_prepare_pbo(volume, context);
530 if (flags & WINED3D_MAP_DISCARD)
531 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
532 else
533 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_BUFFER);
535 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, volume->pbo));
537 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
539 GLbitfield mapflags = wined3d_resource_gl_map_flags(flags);
540 mapflags &= ~GL_MAP_FLUSH_EXPLICIT_BIT;
541 base_memory = GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER,
542 0, volume->resource.size, mapflags));
544 else
546 GLenum access = wined3d_resource_gl_legacy_map_flags(flags);
547 base_memory = GL_EXTCALL(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, access));
550 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
551 checkGLcall("Map PBO");
553 context_release(context);
555 else
557 if (!volume_prepare_system_memory(volume))
559 WARN("Out of memory.\n");
560 map_desc->data = NULL;
561 return E_OUTOFMEMORY;
564 if (flags & WINED3D_MAP_DISCARD)
566 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
568 else if (!(volume->locations & WINED3D_LOCATION_SYSMEM))
570 context = context_acquire(device, NULL);
571 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
572 context_release(context);
574 base_memory = volume->resource.heap_memory;
577 TRACE("Base memory pointer %p.\n", base_memory);
579 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
581 map_desc->row_pitch = volume->resource.width * format->byte_count;
582 map_desc->slice_pitch = map_desc->row_pitch * volume->resource.height;
584 else
586 wined3d_texture_get_pitch(volume->container, volume->texture_level,
587 &map_desc->row_pitch, &map_desc->slice_pitch);
590 if (!box)
592 map_desc->data = base_memory;
594 else
596 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
598 /* Compressed textures are block based, so calculate the offset of
599 * the block that contains the top-left pixel of the locked rectangle. */
600 map_desc->data = base_memory
601 + (box->front * map_desc->slice_pitch)
602 + ((box->top / format->block_height) * map_desc->row_pitch)
603 + ((box->left / format->block_width) * format->block_byte_count);
605 else
607 map_desc->data = base_memory
608 + (map_desc->slice_pitch * box->front)
609 + (map_desc->row_pitch * box->top)
610 + (box->left * volume->resource.format->byte_count);
614 if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
616 wined3d_texture_set_dirty(volume->container);
617 wined3d_volume_invalidate_location(volume, ~volume->resource.map_binding);
620 volume->resource.map_count++;
622 TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
623 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
625 return WINED3D_OK;
628 HRESULT wined3d_volume_unmap(struct wined3d_volume *volume)
630 TRACE("volume %p.\n", volume);
632 if (!volume->resource.map_count)
634 WARN("Trying to unlock an unlocked volume %p.\n", volume);
635 return WINED3DERR_INVALIDCALL;
638 if (volume->resource.map_binding == WINED3D_LOCATION_BUFFER)
640 struct wined3d_device *device = volume->resource.device;
641 struct wined3d_context *context = context_acquire(device, NULL);
642 const struct wined3d_gl_info *gl_info = context->gl_info;
644 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, volume->pbo));
645 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER));
646 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
647 checkGLcall("Unmap PBO");
649 context_release(context);
652 volume->resource.map_count--;
654 return WINED3D_OK;
657 static ULONG volume_resource_incref(struct wined3d_resource *resource)
659 struct wined3d_volume *volume = volume_from_resource(resource);
660 TRACE("Forwarding to container %p.\n", volume->container);
662 return wined3d_texture_incref(volume->container);
665 static ULONG volume_resource_decref(struct wined3d_resource *resource)
667 struct wined3d_volume *volume = volume_from_resource(resource);
668 TRACE("Forwarding to container %p.\n", volume->container);
670 return wined3d_texture_decref(volume->container);
673 static HRESULT volume_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
674 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
676 ERR("Not supported on sub-resources.\n");
677 return WINED3DERR_INVALIDCALL;
680 static HRESULT volume_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
682 ERR("Not supported on sub-resources.\n");
683 return WINED3DERR_INVALIDCALL;
686 static const struct wined3d_resource_ops volume_resource_ops =
688 volume_resource_incref,
689 volume_resource_decref,
690 volume_unload,
691 volume_resource_sub_resource_map,
692 volume_resource_sub_resource_unmap,
695 static HRESULT volume_init(struct wined3d_volume *volume, struct wined3d_texture *container,
696 const struct wined3d_resource_desc *desc, UINT level)
698 struct wined3d_device *device = container->resource.device;
699 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
700 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format);
701 HRESULT hr;
702 UINT size;
704 /* TODO: Write tests for other resources and move this check
705 * to resource_init, if applicable. */
706 if (desc->usage & WINED3DUSAGE_DYNAMIC
707 && (desc->pool == WINED3D_POOL_MANAGED || desc->pool == WINED3D_POOL_SCRATCH))
709 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
710 return WINED3DERR_INVALIDCALL;
713 size = wined3d_format_calculate_size(format, device->surface_alignment, desc->width, desc->height, desc->depth);
715 if (FAILED(hr = resource_init(&volume->resource, device, WINED3D_RTYPE_VOLUME, format,
716 WINED3D_MULTISAMPLE_NONE, 0, desc->usage, desc->pool, desc->width, desc->height, desc->depth,
717 size, NULL, &wined3d_null_parent_ops, &volume_resource_ops)))
719 WARN("Failed to initialize resource, returning %#x.\n", hr);
720 return hr;
723 volume->texture_level = level;
724 volume->locations = WINED3D_LOCATION_DISCARDED;
726 if (desc->pool == WINED3D_POOL_DEFAULT && desc->usage & WINED3DUSAGE_DYNAMIC
727 && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
728 && !format->convert)
730 wined3d_resource_free_sysmem(&volume->resource);
731 volume->resource.map_binding = WINED3D_LOCATION_BUFFER;
734 volume->container = container;
736 return WINED3D_OK;
739 HRESULT wined3d_volume_create(struct wined3d_texture *container, const struct wined3d_resource_desc *desc,
740 unsigned int level, struct wined3d_volume **volume)
742 struct wined3d_device_parent *device_parent = container->resource.device->device_parent;
743 const struct wined3d_parent_ops *parent_ops;
744 struct wined3d_volume *object;
745 void *parent;
746 HRESULT hr;
748 TRACE("container %p, width %u, height %u, depth %u, level %u, format %s, "
749 "usage %#x, pool %s, volume %p.\n",
750 container, desc->width, desc->height, desc->depth, level, debug_d3dformat(desc->format),
751 desc->usage, debug_d3dpool(desc->pool), volume);
753 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
754 return E_OUTOFMEMORY;
756 if (FAILED(hr = volume_init(object, container, desc, level)))
758 WARN("Failed to initialize volume, returning %#x.\n", hr);
759 HeapFree(GetProcessHeap(), 0, object);
760 return hr;
763 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
764 container, level, &parent, &parent_ops)))
766 WARN("Failed to create volume parent, hr %#x.\n", hr);
767 wined3d_volume_destroy(object);
768 return hr;
771 TRACE("Created volume %p, parent %p, parent_ops %p.\n", object, parent, parent_ops);
773 object->resource.parent = parent;
774 object->resource.parent_ops = parent_ops;
775 *volume = object;
777 return WINED3D_OK;