winedevice: Get rid of global driver_hkey variable and fix some leaks.
[wine.git] / dlls / wined3d / volume.c
blob6f5de73f524ebccd655553875cfa760e81009dce
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 wined3d_volume_check_box_dimensions(const struct wined3d_volume *volume,
437 const struct wined3d_box *box)
439 if (!box)
440 return TRUE;
442 if (box->left >= box->right)
443 return FALSE;
444 if (box->top >= box->bottom)
445 return FALSE;
446 if (box->front >= box->back)
447 return FALSE;
448 if (box->right > volume->resource.width)
449 return FALSE;
450 if (box->bottom > volume->resource.height)
451 return FALSE;
452 if (box->back > volume->resource.depth)
453 return FALSE;
455 return TRUE;
458 HRESULT wined3d_volume_map(struct wined3d_volume *volume,
459 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
461 struct wined3d_device *device = volume->resource.device;
462 struct wined3d_context *context;
463 const struct wined3d_gl_info *gl_info;
464 BYTE *base_memory;
465 const struct wined3d_format *format = volume->resource.format;
466 const unsigned int fmt_flags = volume->container->resource.format_flags;
468 TRACE("volume %p, map_desc %p, box %s, flags %#x.\n",
469 volume, map_desc, debug_box(box), flags);
471 map_desc->data = NULL;
472 if (!(volume->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU))
474 WARN("Volume %p is not CPU accessible.\n", volume);
475 return WINED3DERR_INVALIDCALL;
477 if (volume->resource.map_count)
479 WARN("Volume is already mapped.\n");
480 return WINED3DERR_INVALIDCALL;
482 if (!wined3d_volume_check_box_dimensions(volume, box))
484 WARN("Map box is invalid.\n");
485 return WINED3DERR_INVALIDCALL;
487 if ((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && box
488 && !wined3d_texture_check_block_align(volume->container, volume->texture_level, box))
490 WARN("Map box %s is misaligned for %ux%u blocks.\n",
491 debug_box(box), format->block_width, format->block_height);
492 return WINED3DERR_INVALIDCALL;
495 flags = wined3d_resource_sanitize_map_flags(&volume->resource, flags);
497 if (volume->resource.map_binding == WINED3D_LOCATION_BUFFER)
499 context = context_acquire(device, NULL);
500 gl_info = context->gl_info;
502 wined3d_volume_prepare_pbo(volume, context);
503 if (flags & WINED3D_MAP_DISCARD)
504 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
505 else
506 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_BUFFER);
508 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, volume->pbo));
510 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
512 GLbitfield mapflags = wined3d_resource_gl_map_flags(flags);
513 mapflags &= ~GL_MAP_FLUSH_EXPLICIT_BIT;
514 base_memory = GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER,
515 0, volume->resource.size, mapflags));
517 else
519 GLenum access = wined3d_resource_gl_legacy_map_flags(flags);
520 base_memory = GL_EXTCALL(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, access));
523 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
524 checkGLcall("Map PBO");
526 context_release(context);
528 else
530 if (!volume_prepare_system_memory(volume))
532 WARN("Out of memory.\n");
533 map_desc->data = NULL;
534 return E_OUTOFMEMORY;
537 if (flags & WINED3D_MAP_DISCARD)
539 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
541 else if (!(volume->locations & WINED3D_LOCATION_SYSMEM))
543 context = context_acquire(device, NULL);
544 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
545 context_release(context);
547 base_memory = volume->resource.heap_memory;
550 TRACE("Base memory pointer %p.\n", base_memory);
552 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
554 map_desc->row_pitch = volume->resource.width * format->byte_count;
555 map_desc->slice_pitch = map_desc->row_pitch * volume->resource.height;
557 else
559 wined3d_texture_get_pitch(volume->container, volume->texture_level,
560 &map_desc->row_pitch, &map_desc->slice_pitch);
563 if (!box)
565 map_desc->data = base_memory;
567 else
569 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
571 /* Compressed textures are block based, so calculate the offset of
572 * the block that contains the top-left pixel of the locked rectangle. */
573 map_desc->data = base_memory
574 + (box->front * map_desc->slice_pitch)
575 + ((box->top / format->block_height) * map_desc->row_pitch)
576 + ((box->left / format->block_width) * format->block_byte_count);
578 else
580 map_desc->data = base_memory
581 + (map_desc->slice_pitch * box->front)
582 + (map_desc->row_pitch * box->top)
583 + (box->left * volume->resource.format->byte_count);
587 if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
589 wined3d_texture_set_dirty(volume->container);
590 wined3d_volume_invalidate_location(volume, ~volume->resource.map_binding);
593 volume->resource.map_count++;
595 TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
596 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
598 return WINED3D_OK;
601 HRESULT wined3d_volume_unmap(struct wined3d_volume *volume)
603 TRACE("volume %p.\n", volume);
605 if (!volume->resource.map_count)
607 WARN("Trying to unlock an unlocked volume %p.\n", volume);
608 return WINED3DERR_INVALIDCALL;
611 if (volume->resource.map_binding == WINED3D_LOCATION_BUFFER)
613 struct wined3d_device *device = volume->resource.device;
614 struct wined3d_context *context = context_acquire(device, NULL);
615 const struct wined3d_gl_info *gl_info = context->gl_info;
617 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, volume->pbo));
618 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER));
619 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
620 checkGLcall("Unmap PBO");
622 context_release(context);
625 volume->resource.map_count--;
627 return WINED3D_OK;
630 static ULONG volume_resource_incref(struct wined3d_resource *resource)
632 struct wined3d_volume *volume = volume_from_resource(resource);
633 TRACE("Forwarding to container %p.\n", volume->container);
635 return wined3d_texture_incref(volume->container);
638 static ULONG volume_resource_decref(struct wined3d_resource *resource)
640 struct wined3d_volume *volume = volume_from_resource(resource);
641 TRACE("Forwarding to container %p.\n", volume->container);
643 return wined3d_texture_decref(volume->container);
646 static HRESULT volume_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
647 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
649 ERR("Not supported on sub-resources.\n");
650 return WINED3DERR_INVALIDCALL;
653 static HRESULT volume_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
655 ERR("Not supported on sub-resources.\n");
656 return WINED3DERR_INVALIDCALL;
659 static const struct wined3d_resource_ops volume_resource_ops =
661 volume_resource_incref,
662 volume_resource_decref,
663 volume_unload,
664 volume_resource_sub_resource_map,
665 volume_resource_sub_resource_unmap,
668 static HRESULT volume_init(struct wined3d_volume *volume, struct wined3d_texture *container,
669 const struct wined3d_resource_desc *desc, UINT level)
671 struct wined3d_device *device = container->resource.device;
672 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
673 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format);
674 HRESULT hr;
675 UINT size;
677 /* TODO: Write tests for other resources and move this check
678 * to resource_init, if applicable. */
679 if (desc->usage & WINED3DUSAGE_DYNAMIC
680 && (desc->pool == WINED3D_POOL_MANAGED || desc->pool == WINED3D_POOL_SCRATCH))
682 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
683 return WINED3DERR_INVALIDCALL;
686 size = wined3d_format_calculate_size(format, device->surface_alignment, desc->width, desc->height, desc->depth);
688 if (FAILED(hr = resource_init(&volume->resource, device, WINED3D_RTYPE_VOLUME, format,
689 WINED3D_MULTISAMPLE_NONE, 0, desc->usage, desc->pool, desc->width, desc->height, desc->depth,
690 size, NULL, &wined3d_null_parent_ops, &volume_resource_ops)))
692 WARN("Failed to initialize resource, returning %#x.\n", hr);
693 return hr;
696 volume->texture_level = level;
697 volume->locations = WINED3D_LOCATION_DISCARDED;
699 if (desc->pool == WINED3D_POOL_DEFAULT && desc->usage & WINED3DUSAGE_DYNAMIC
700 && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
701 && !format->convert)
703 wined3d_resource_free_sysmem(&volume->resource);
704 volume->resource.map_binding = WINED3D_LOCATION_BUFFER;
707 volume->container = container;
709 return WINED3D_OK;
712 HRESULT wined3d_volume_create(struct wined3d_texture *container, const struct wined3d_resource_desc *desc,
713 unsigned int level, struct wined3d_volume **volume)
715 struct wined3d_device_parent *device_parent = container->resource.device->device_parent;
716 const struct wined3d_parent_ops *parent_ops;
717 struct wined3d_volume *object;
718 void *parent;
719 HRESULT hr;
721 TRACE("container %p, width %u, height %u, depth %u, level %u, format %s, "
722 "usage %#x, pool %s, volume %p.\n",
723 container, desc->width, desc->height, desc->depth, level, debug_d3dformat(desc->format),
724 desc->usage, debug_d3dpool(desc->pool), volume);
726 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
727 return E_OUTOFMEMORY;
729 if (FAILED(hr = volume_init(object, container, desc, level)))
731 WARN("Failed to initialize volume, returning %#x.\n", hr);
732 HeapFree(GetProcessHeap(), 0, object);
733 return hr;
736 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
737 container, level, &parent, &parent_ops)))
739 WARN("Failed to create volume parent, hr %#x.\n", hr);
740 wined3d_volume_destroy(object);
741 return hr;
744 TRACE("Created volume %p, parent %p, parent_ops %p.\n", object, parent, parent_ops);
746 object->resource.parent = parent;
747 object->resource.parent_ops = parent_ops;
748 *volume = object;
750 return WINED3D_OK;