wined3d: Pass a resource to wined3d_resource_free_sysmem().
[wine.git] / dlls / wined3d / volume.c
blob4865c594d8786ada280b84c5854658881061ac63
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 /* Context activation is done by the caller. */
31 static void volume_bind_and_dirtify(const struct wined3d_volume *volume,
32 struct wined3d_context *context, BOOL srgb)
34 struct wined3d_texture *container = volume->container;
35 DWORD active_sampler;
37 /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
38 * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
39 * gl states. The current texture unit should always be a valid one.
41 * To be more specific, this is tricky because we can implicitly be called
42 * from sampler() in state.c. This means we can't touch anything other than
43 * whatever happens to be the currently active texture, or we would risk
44 * marking already applied sampler states dirty again. */
45 active_sampler = volume->resource.device->rev_tex_unit_map[context->active_texture];
47 if (active_sampler != WINED3D_UNMAPPED_STAGE)
48 device_invalidate_state(volume->resource.device, STATE_SAMPLER(active_sampler));
50 container->texture_ops->texture_bind(container, context, srgb);
53 void volume_set_container(struct wined3d_volume *volume, struct wined3d_texture *container)
55 TRACE("volume %p, container %p.\n", volume, container);
57 volume->container = container;
60 /* Context activation is done by the caller. */
61 static void wined3d_volume_allocate_texture(const struct wined3d_volume *volume,
62 const struct wined3d_context *context, BOOL srgb)
64 const struct wined3d_gl_info *gl_info = context->gl_info;
65 const struct wined3d_format *format = volume->resource.format;
67 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, volume->texture_level,
68 srgb ? format->glGammaInternal : format->glInternal,
69 volume->resource.width, volume->resource.height, volume->resource.depth,
70 0, format->glFormat, format->glType, NULL));
71 checkGLcall("glTexImage3D");
74 /* Context activation is done by the caller. */
75 void wined3d_volume_upload_data(struct wined3d_volume *volume, const struct wined3d_context *context,
76 const struct wined3d_bo_address *data)
78 const struct wined3d_gl_info *gl_info = context->gl_info;
79 const struct wined3d_format *format = volume->resource.format;
81 TRACE("volume %p, context %p, level %u, format %s (%#x).\n",
82 volume, context, volume->texture_level, debug_d3dformat(format->id),
83 format->id);
86 if (data->buffer_object)
88 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, data->buffer_object));
89 checkGLcall("glBindBufferARB");
92 GL_EXTCALL(glTexSubImage3DEXT(GL_TEXTURE_3D, volume->texture_level, 0, 0, 0,
93 volume->resource.width, volume->resource.height, volume->resource.depth,
94 format->glFormat, format->glType, data->addr));
95 checkGLcall("glTexSubImage3D");
97 if (data->buffer_object)
99 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
100 checkGLcall("glBindBufferARB");
104 static void wined3d_volume_validate_location(struct wined3d_volume *volume, DWORD location)
106 TRACE("Volume %p, setting %s.\n", volume, wined3d_debug_location(location));
107 volume->locations |= location;
108 TRACE("new location flags are %s.\n", wined3d_debug_location(volume->locations));
111 void wined3d_volume_invalidate_location(struct wined3d_volume *volume, DWORD location)
113 TRACE("Volume %p, clearing %s.\n", volume, wined3d_debug_location(location));
114 volume->locations &= ~location;
115 TRACE("new location flags are %s.\n", wined3d_debug_location(volume->locations));
118 /* Context activation is done by the caller. */
119 static void wined3d_volume_download_data(struct wined3d_volume *volume,
120 const struct wined3d_context *context, const struct wined3d_bo_address *data)
122 const struct wined3d_gl_info *gl_info = context->gl_info;
123 const struct wined3d_format *format = volume->resource.format;
125 if (data->buffer_object)
127 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, data->buffer_object));
128 checkGLcall("glBindBufferARB");
131 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, volume->texture_level,
132 format->glFormat, format->glType, data->addr);
133 checkGLcall("glGetTexImage");
135 if (data->buffer_object)
137 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
138 checkGLcall("glBindBufferARB");
143 static void wined3d_volume_evict_sysmem(struct wined3d_volume *volume)
145 wined3d_resource_free_sysmem(&volume->resource);
146 volume->resource.allocatedMemory = NULL;
147 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_SYSMEM);
150 static DWORD volume_access_from_location(DWORD location)
152 switch (location)
154 case WINED3D_LOCATION_DISCARDED:
155 return 0;
157 case WINED3D_LOCATION_SYSMEM:
158 return WINED3D_RESOURCE_ACCESS_CPU;
160 case WINED3D_LOCATION_BUFFER:
161 case WINED3D_LOCATION_TEXTURE_RGB:
162 case WINED3D_LOCATION_TEXTURE_SRGB:
163 return WINED3D_RESOURCE_ACCESS_GPU;
165 default:
166 FIXME("Unhandled location %#x.\n", location);
167 return 0;
171 /* Context activation is done by the caller. */
172 static void wined3d_volume_srgb_transfer(struct wined3d_volume *volume,
173 struct wined3d_context *context, BOOL dest_is_srgb)
175 struct wined3d_bo_address data;
176 /* Optimizations are possible, but the effort should be put into either
177 * implementing EXT_SRGB_DECODE in the driver or finding out why we
178 * picked the wrong copy for the original upload and fixing that.
180 * Also keep in mind that we want to avoid using resource.allocatedMemory
181 * for DEFAULT pool surfaces. */
183 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
184 data.buffer_object = 0;
185 data.addr = HeapAlloc(GetProcessHeap(), 0, volume->resource.size);
186 if (!data.addr)
187 return;
189 volume_bind_and_dirtify(volume, context, !dest_is_srgb);
190 wined3d_volume_download_data(volume, context, &data);
191 volume_bind_and_dirtify(volume, context, dest_is_srgb);
192 wined3d_volume_upload_data(volume, context, &data);
194 HeapFree(GetProcessHeap(), 0, data.addr);
197 /* Context activation is done by the caller. */
198 static void wined3d_volume_load_location(struct wined3d_volume *volume,
199 struct wined3d_context *context, DWORD location)
201 DWORD required_access = volume_access_from_location(location);
203 TRACE("Volume %p, loading %s, have %s.\n", volume, wined3d_debug_location(location),
204 wined3d_debug_location(volume->locations));
206 if ((volume->locations & location) == location)
208 TRACE("Location(s) already up to date.\n");
209 return;
212 if ((volume->resource.access_flags & required_access) != required_access)
214 ERR("Operation requires %#x access, but volume only has %#x.\n",
215 required_access, volume->resource.access_flags);
216 return;
219 switch (location)
221 case WINED3D_LOCATION_TEXTURE_RGB:
222 case WINED3D_LOCATION_TEXTURE_SRGB:
223 if ((location == WINED3D_LOCATION_TEXTURE_RGB
224 && !(volume->flags & WINED3D_VFLAG_ALLOCATED))
225 || (location == WINED3D_LOCATION_TEXTURE_SRGB
226 && !(volume->flags & WINED3D_VFLAG_SRGB_ALLOCATED)))
227 ERR("Trying to load (s)RGB texture without prior allocation.\n");
229 if (volume->locations & WINED3D_LOCATION_DISCARDED)
231 TRACE("Volume previously discarded, nothing to do.\n");
232 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
234 else if (volume->locations & WINED3D_LOCATION_SYSMEM)
236 struct wined3d_bo_address data = {0, volume->resource.allocatedMemory};
237 wined3d_volume_upload_data(volume, context, &data);
239 else if (volume->locations & WINED3D_LOCATION_BUFFER)
241 struct wined3d_bo_address data = {volume->pbo, NULL};
242 wined3d_volume_upload_data(volume, context, &data);
244 else if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
246 wined3d_volume_srgb_transfer(volume, context, TRUE);
248 else if (volume->locations & WINED3D_LOCATION_TEXTURE_SRGB)
250 wined3d_volume_srgb_transfer(volume, context, FALSE);
252 else
254 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume->locations));
255 return;
257 wined3d_volume_validate_location(volume, location);
259 if (volume->resource.pool == WINED3D_POOL_MANAGED && volume->download_count < 10)
260 wined3d_volume_evict_sysmem(volume);
262 break;
264 case WINED3D_LOCATION_SYSMEM:
265 if (!volume->resource.allocatedMemory || !volume->resource.heap_memory)
267 ERR("Trying to load WINED3D_LOCATION_SYSMEM without setting it up first.\n");
268 return;
271 if (volume->locations & WINED3D_LOCATION_DISCARDED)
273 TRACE("Volume previously discarded, nothing to do.\n");
274 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
276 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
278 struct wined3d_bo_address data = {0, volume->resource.allocatedMemory};
280 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
281 volume_bind_and_dirtify(volume, context, FALSE);
282 else
283 volume_bind_and_dirtify(volume, context, TRUE);
285 volume->download_count++;
286 wined3d_volume_download_data(volume, context, &data);
288 else
290 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
291 wined3d_debug_location(volume->locations));
292 return;
294 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
295 break;
297 case WINED3D_LOCATION_BUFFER:
298 if (!volume->pbo || !(volume->flags & WINED3D_VFLAG_PBO))
299 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
301 if (volume->locations & WINED3D_LOCATION_DISCARDED)
303 TRACE("Volume previously discarded, nothing to do.\n");
304 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
306 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
308 struct wined3d_bo_address data = {volume->pbo, NULL};
310 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
311 volume_bind_and_dirtify(volume, context, FALSE);
312 else
313 volume_bind_and_dirtify(volume, context, TRUE);
315 wined3d_volume_download_data(volume, context, &data);
317 else
319 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
320 wined3d_debug_location(volume->locations));
321 return;
323 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
324 break;
326 default:
327 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
328 wined3d_debug_location(volume->locations));
332 /* Context activation is done by the caller. */
333 void wined3d_volume_load(struct wined3d_volume *volume, struct wined3d_context *context, BOOL srgb_mode)
335 volume_bind_and_dirtify(volume, context, srgb_mode);
337 if (srgb_mode)
339 if (!(volume->flags & WINED3D_VFLAG_SRGB_ALLOCATED))
341 wined3d_volume_allocate_texture(volume, context, TRUE);
342 volume->flags |= WINED3D_VFLAG_SRGB_ALLOCATED;
345 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_TEXTURE_SRGB);
347 else
349 if (!(volume->flags & WINED3D_VFLAG_ALLOCATED))
351 wined3d_volume_allocate_texture(volume, context, FALSE);
352 volume->flags |= WINED3D_VFLAG_ALLOCATED;
355 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_TEXTURE_RGB);
359 /* Context activation is done by the caller. */
360 static void wined3d_volume_prepare_pbo(struct wined3d_volume *volume, struct wined3d_context *context)
362 const struct wined3d_gl_info *gl_info = context->gl_info;
364 if (volume->pbo)
365 return;
367 GL_EXTCALL(glGenBuffersARB(1, &volume->pbo));
368 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, volume->pbo));
369 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, volume->resource.size, NULL, GL_STREAM_DRAW_ARB));
370 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
371 checkGLcall("Create PBO");
373 TRACE("Created PBO %u for volume %p.\n", volume->pbo, volume);
376 static void wined3d_volume_free_pbo(struct wined3d_volume *volume)
378 struct wined3d_context *context = context_acquire(volume->resource.device, NULL);
379 const struct wined3d_gl_info *gl_info = context->gl_info;
381 TRACE("Deleting PBO %u belonging to volume %p.\n", volume->pbo, volume);
382 GL_EXTCALL(glDeleteBuffersARB(1, &volume->pbo));
383 checkGLcall("glDeleteBuffersARB");
384 volume->pbo = 0;
385 context_release(context);
388 static BOOL volume_prepare_system_memory(struct wined3d_volume *volume)
390 if (volume->resource.allocatedMemory)
391 return TRUE;
393 if (!wined3d_resource_allocate_sysmem(&volume->resource))
395 ERR("Failed to allocate system memory.\n");
396 return FALSE;
398 volume->resource.allocatedMemory = volume->resource.heap_memory;
399 return TRUE;
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. */
438 volume->flags &= ~(WINED3D_VFLAG_ALLOCATED | WINED3D_VFLAG_SRGB_ALLOCATED);
440 resource_unload(resource);
443 ULONG CDECL wined3d_volume_incref(struct wined3d_volume *volume)
445 ULONG refcount;
447 if (volume->container)
449 TRACE("Forwarding to container %p.\n", volume->container);
450 return wined3d_texture_incref(volume->container);
453 refcount = InterlockedIncrement(&volume->resource.ref);
455 TRACE("%p increasing refcount to %u.\n", volume, refcount);
457 return refcount;
460 ULONG CDECL wined3d_volume_decref(struct wined3d_volume *volume)
462 ULONG refcount;
464 if (volume->container)
466 TRACE("Forwarding to container %p.\n", volume->container);
467 return wined3d_texture_decref(volume->container);
470 refcount = InterlockedDecrement(&volume->resource.ref);
472 TRACE("%p decreasing refcount to %u.\n", volume, refcount);
474 if (!refcount)
476 if (volume->pbo)
477 wined3d_volume_free_pbo(volume);
479 resource_cleanup(&volume->resource);
480 volume->resource.parent_ops->wined3d_object_destroyed(volume->resource.parent);
481 HeapFree(GetProcessHeap(), 0, volume);
484 return refcount;
487 void * CDECL wined3d_volume_get_parent(const struct wined3d_volume *volume)
489 TRACE("volume %p.\n", volume);
491 return volume->resource.parent;
494 DWORD CDECL wined3d_volume_set_priority(struct wined3d_volume *volume, DWORD priority)
496 return resource_set_priority(&volume->resource, priority);
499 DWORD CDECL wined3d_volume_get_priority(const struct wined3d_volume *volume)
501 return resource_get_priority(&volume->resource);
504 void CDECL wined3d_volume_preload(struct wined3d_volume *volume)
506 FIXME("volume %p stub!\n", volume);
509 struct wined3d_resource * CDECL wined3d_volume_get_resource(struct wined3d_volume *volume)
511 TRACE("volume %p.\n", volume);
513 return &volume->resource;
516 HRESULT CDECL wined3d_volume_map(struct wined3d_volume *volume,
517 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
519 struct wined3d_device *device = volume->resource.device;
520 struct wined3d_context *context;
521 const struct wined3d_gl_info *gl_info;
522 BYTE *base_memory;
524 TRACE("volume %p, map_desc %p, box %p, flags %#x.\n",
525 volume, map_desc, box, flags);
527 if (!(volume->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU))
529 WARN("Volume %p is not CPU accessible.\n", volume);
530 map_desc->data = NULL;
531 return WINED3DERR_INVALIDCALL;
533 flags = wined3d_resource_sanitize_map_flags(&volume->resource, flags);
535 if (volume->flags & WINED3D_VFLAG_PBO)
537 context = context_acquire(device, NULL);
538 gl_info = context->gl_info;
540 wined3d_volume_prepare_pbo(volume, context);
541 if (flags & WINED3D_MAP_DISCARD)
542 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
543 else
544 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_BUFFER);
546 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, volume->pbo));
548 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
550 GLbitfield mapflags = wined3d_resource_gl_map_flags(flags);
551 mapflags &= ~GL_MAP_FLUSH_EXPLICIT_BIT;
552 base_memory = GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER_ARB,
553 0, volume->resource.size, mapflags));
555 else
557 base_memory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
560 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
561 checkGLcall("Map PBO");
563 context_release(context);
565 else
567 if (!volume_prepare_system_memory(volume))
569 WARN("Out of memory.\n");
570 map_desc->data = NULL;
571 return E_OUTOFMEMORY;
574 if (flags & WINED3D_MAP_DISCARD)
576 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
578 else if (!(volume->locations & WINED3D_LOCATION_SYSMEM))
580 context = context_acquire(device, NULL);
581 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
582 context_release(context);
584 base_memory = volume->resource.allocatedMemory;
587 TRACE("Base memory pointer %p.\n", base_memory);
589 map_desc->row_pitch = volume->resource.format->byte_count * volume->resource.width; /* Bytes / row */
590 map_desc->slice_pitch = volume->resource.format->byte_count
591 * volume->resource.width * volume->resource.height; /* Bytes / slice */
592 if (!box)
594 TRACE("No box supplied - all is ok\n");
595 map_desc->data = base_memory;
597 else
599 TRACE("Lock Box (%p) = l %u, t %u, r %u, b %u, fr %u, ba %u\n",
600 box, box->left, box->top, box->right, box->bottom, box->front, box->back);
601 map_desc->data = base_memory
602 + (map_desc->slice_pitch * box->front) /* FIXME: is front < back or vica versa? */
603 + (map_desc->row_pitch * box->top)
604 + (box->left * volume->resource.format->byte_count);
607 if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
609 wined3d_texture_set_dirty(volume->container);
611 if (volume->flags & WINED3D_VFLAG_PBO)
612 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_BUFFER);
613 else
614 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_SYSMEM);
617 volume->flags |= WINED3D_VFLAG_LOCKED;
619 TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
620 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
622 return WINED3D_OK;
625 struct wined3d_volume * CDECL wined3d_volume_from_resource(struct wined3d_resource *resource)
627 return volume_from_resource(resource);
630 HRESULT CDECL wined3d_volume_unmap(struct wined3d_volume *volume)
632 TRACE("volume %p.\n", volume);
634 if (!(volume->flags & WINED3D_VFLAG_LOCKED))
636 WARN("Trying to unlock unlocked volume %p.\n", volume);
637 return WINED3DERR_INVALIDCALL;
640 if (volume->flags & WINED3D_VFLAG_PBO)
642 struct wined3d_device *device = volume->resource.device;
643 struct wined3d_context *context = context_acquire(device, NULL);
644 const struct wined3d_gl_info *gl_info = context->gl_info;
646 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, volume->pbo));
647 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
648 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
649 checkGLcall("Unmap PBO");
651 context_release(context);
654 volume->flags &= ~WINED3D_VFLAG_LOCKED;
656 return WINED3D_OK;
659 static const struct wined3d_resource_ops volume_resource_ops =
661 volume_unload,
664 static HRESULT volume_init(struct wined3d_volume *volume, struct wined3d_device *device, UINT width,
665 UINT height, UINT depth, UINT level, DWORD usage, enum wined3d_format_id format_id,
666 enum wined3d_pool pool, void *parent, const struct wined3d_parent_ops *parent_ops)
668 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
669 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
670 HRESULT hr;
671 UINT size;
673 if (!gl_info->supported[EXT_TEXTURE3D])
675 WARN("Volume cannot be created - no volume texture support.\n");
676 return WINED3DERR_INVALIDCALL;
678 /* TODO: Write tests for other resources and move this check
679 * to resource_init, if applicable. */
680 if (usage & WINED3DUSAGE_DYNAMIC
681 && (pool == WINED3D_POOL_MANAGED || pool == WINED3D_POOL_SCRATCH))
683 WARN("Attempted to create a DYNAMIC texture in pool %u.\n", pool);
684 return WINED3DERR_INVALIDCALL;
687 size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, depth);
689 hr = resource_init(&volume->resource, device, WINED3D_RTYPE_VOLUME, format,
690 WINED3D_MULTISAMPLE_NONE, 0, usage, pool, width, height, depth,
691 size, parent, parent_ops, &volume_resource_ops);
692 if (FAILED(hr))
694 WARN("Failed to initialize resource, returning %#x.\n", hr);
695 return hr;
698 volume->texture_level = level;
699 volume->locations = WINED3D_LOCATION_DISCARDED;
701 if (pool == WINED3D_POOL_DEFAULT && usage & WINED3DUSAGE_DYNAMIC
702 && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT])
704 wined3d_resource_free_sysmem(&volume->resource);
705 volume->resource.allocatedMemory = NULL;
706 volume->flags |= WINED3D_VFLAG_PBO;
709 return WINED3D_OK;
712 HRESULT CDECL wined3d_volume_create(struct wined3d_device *device, UINT width, UINT height,
713 UINT depth, UINT level, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool,
714 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_volume **volume)
716 struct wined3d_volume *object;
717 HRESULT hr;
719 TRACE("device %p, width %u, height %u, depth %u, usage %#x, format %s, pool %s\n",
720 device, width, height, depth, usage, debug_d3dformat(format_id), debug_d3dpool(pool));
721 TRACE("parent %p, parent_ops %p, volume %p.\n", parent, parent_ops, volume);
723 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
724 if (!object)
726 *volume = NULL;
727 return WINED3DERR_OUTOFVIDEOMEMORY;
730 hr = volume_init(object, device, width, height, depth, level,
731 usage, format_id, pool, parent, parent_ops);
732 if (FAILED(hr))
734 WARN("Failed to initialize volume, returning %#x.\n", hr);
735 HeapFree(GetProcessHeap(), 0, object);
736 return hr;
739 TRACE("Created volume %p.\n", object);
740 *volume = object;
742 return WINED3D_OK;