wined3d: Get rid of some leftover comments referring to the GL lock.
[wine/wine-gecko.git] / dlls / wined3d / volume.c
bloba1c787630b32f654f75583b2a0a7ecea1566edb2
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.heap_memory);
146 volume->resource.heap_memory = NULL;
147 volume->resource.allocatedMemory = NULL;
148 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_SYSMEM);
151 static DWORD volume_access_from_location(DWORD location)
153 switch (location)
155 case WINED3D_LOCATION_DISCARDED:
156 return 0;
158 case WINED3D_LOCATION_SYSMEM:
159 return WINED3D_RESOURCE_ACCESS_CPU;
161 case WINED3D_LOCATION_BUFFER:
162 case WINED3D_LOCATION_TEXTURE_RGB:
163 case WINED3D_LOCATION_TEXTURE_SRGB:
164 return WINED3D_RESOURCE_ACCESS_GPU;
166 default:
167 FIXME("Unhandled location %#x.\n", location);
168 return 0;
172 /* Context activation is done by the caller. */
173 static void wined3d_volume_srgb_transfer(struct wined3d_volume *volume,
174 struct wined3d_context *context, BOOL dest_is_srgb)
176 struct wined3d_bo_address data;
177 /* Optimizations are possible, but the effort should be put into either
178 * implementing EXT_SRGB_DECODE in the driver or finding out why we
179 * picked the wrong copy for the original upload and fixing that.
181 * Also keep in mind that we want to avoid using resource.allocatedMemory
182 * for DEFAULT pool surfaces. */
184 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
185 data.buffer_object = 0;
186 data.addr = HeapAlloc(GetProcessHeap(), 0, volume->resource.size);
187 if (!data.addr)
188 return;
190 volume_bind_and_dirtify(volume, context, !dest_is_srgb);
191 wined3d_volume_download_data(volume, context, &data);
192 volume_bind_and_dirtify(volume, context, dest_is_srgb);
193 wined3d_volume_upload_data(volume, context, &data);
195 HeapFree(GetProcessHeap(), 0, data.addr);
198 /* Context activation is done by the caller. */
199 static void wined3d_volume_load_location(struct wined3d_volume *volume,
200 struct wined3d_context *context, DWORD location)
202 DWORD required_access = volume_access_from_location(location);
204 TRACE("Volume %p, loading %s, have %s.\n", volume, wined3d_debug_location(location),
205 wined3d_debug_location(volume->locations));
207 if ((volume->locations & location) == location)
209 TRACE("Location(s) already up to date.\n");
210 return;
213 if ((volume->resource.access_flags & required_access) != required_access)
215 ERR("Operation requires %#x access, but volume only has %#x.\n",
216 required_access, volume->resource.access_flags);
217 return;
220 switch (location)
222 case WINED3D_LOCATION_TEXTURE_RGB:
223 case WINED3D_LOCATION_TEXTURE_SRGB:
224 if ((location == WINED3D_LOCATION_TEXTURE_RGB
225 && !(volume->flags & WINED3D_VFLAG_ALLOCATED))
226 || (location == WINED3D_LOCATION_TEXTURE_SRGB
227 && !(volume->flags & WINED3D_VFLAG_SRGB_ALLOCATED)))
228 ERR("Trying to load (s)RGB texture without prior allocation.\n");
230 if (volume->locations & WINED3D_LOCATION_DISCARDED)
232 TRACE("Volume previously discarded, nothing to do.\n");
233 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
235 else if (volume->locations & WINED3D_LOCATION_SYSMEM)
237 struct wined3d_bo_address data = {0, volume->resource.allocatedMemory};
238 wined3d_volume_upload_data(volume, context, &data);
240 else if (volume->locations & WINED3D_LOCATION_BUFFER)
242 struct wined3d_bo_address data = {volume->pbo, NULL};
243 wined3d_volume_upload_data(volume, context, &data);
245 else if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
247 wined3d_volume_srgb_transfer(volume, context, TRUE);
249 else if (volume->locations & WINED3D_LOCATION_TEXTURE_SRGB)
251 wined3d_volume_srgb_transfer(volume, context, FALSE);
253 else
255 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume->locations));
256 return;
258 wined3d_volume_validate_location(volume, location);
260 if (volume->resource.pool == WINED3D_POOL_MANAGED && volume->download_count < 10)
261 wined3d_volume_evict_sysmem(volume);
263 break;
265 case WINED3D_LOCATION_SYSMEM:
266 if (!volume->resource.allocatedMemory || !volume->resource.heap_memory)
268 ERR("Trying to load WINED3D_LOCATION_SYSMEM without setting it up first.\n");
269 return;
272 if (volume->locations & WINED3D_LOCATION_DISCARDED)
274 TRACE("Volume previously discarded, nothing to do.\n");
275 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
277 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
279 struct wined3d_bo_address data = {0, volume->resource.allocatedMemory};
281 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
282 volume_bind_and_dirtify(volume, context, FALSE);
283 else
284 volume_bind_and_dirtify(volume, context, TRUE);
286 volume->download_count++;
287 wined3d_volume_download_data(volume, context, &data);
289 else
291 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
292 wined3d_debug_location(volume->locations));
293 return;
295 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
296 break;
298 case WINED3D_LOCATION_BUFFER:
299 if (!volume->pbo || !(volume->flags & WINED3D_VFLAG_PBO))
300 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
302 if (volume->locations & WINED3D_LOCATION_DISCARDED)
304 TRACE("Volume previously discarded, nothing to do.\n");
305 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
307 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
309 struct wined3d_bo_address data = {volume->pbo, NULL};
311 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
312 volume_bind_and_dirtify(volume, context, FALSE);
313 else
314 volume_bind_and_dirtify(volume, context, TRUE);
316 wined3d_volume_download_data(volume, context, &data);
318 else
320 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
321 wined3d_debug_location(volume->locations));
322 return;
324 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
325 break;
327 default:
328 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
329 wined3d_debug_location(volume->locations));
333 /* Context activation is done by the caller. */
334 void wined3d_volume_load(struct wined3d_volume *volume, struct wined3d_context *context, BOOL srgb_mode)
336 volume_bind_and_dirtify(volume, context, srgb_mode);
338 if (srgb_mode)
340 if (!(volume->flags & WINED3D_VFLAG_SRGB_ALLOCATED))
342 wined3d_volume_allocate_texture(volume, context, TRUE);
343 volume->flags |= WINED3D_VFLAG_SRGB_ALLOCATED;
346 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_TEXTURE_SRGB);
348 else
350 if (!(volume->flags & WINED3D_VFLAG_ALLOCATED))
352 wined3d_volume_allocate_texture(volume, context, FALSE);
353 volume->flags |= WINED3D_VFLAG_ALLOCATED;
356 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_TEXTURE_RGB);
360 /* Context activation is done by the caller. */
361 static void wined3d_volume_prepare_pbo(struct wined3d_volume *volume, struct wined3d_context *context)
363 const struct wined3d_gl_info *gl_info = context->gl_info;
365 if (volume->pbo)
366 return;
368 GL_EXTCALL(glGenBuffersARB(1, &volume->pbo));
369 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, volume->pbo));
370 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, volume->resource.size, NULL, GL_STREAM_DRAW_ARB));
371 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
372 checkGLcall("Create PBO");
374 TRACE("Created PBO %u for volume %p.\n", volume->pbo, volume);
377 static void wined3d_volume_free_pbo(struct wined3d_volume *volume)
379 struct wined3d_context *context = context_acquire(volume->resource.device, NULL);
380 const struct wined3d_gl_info *gl_info = context->gl_info;
382 TRACE("Deleting PBO %u belonging to volume %p.\n", volume->pbo, volume);
383 GL_EXTCALL(glDeleteBuffersARB(1, &volume->pbo));
384 checkGLcall("glDeleteBuffersARB");
385 volume->pbo = 0;
386 context_release(context);
389 static BOOL volume_prepare_system_memory(struct wined3d_volume *volume)
391 if (volume->resource.allocatedMemory)
392 return TRUE;
394 volume->resource.heap_memory = wined3d_resource_allocate_sysmem(volume->resource.size);
395 if (!volume->resource.heap_memory)
396 return FALSE;
397 volume->resource.allocatedMemory = volume->resource.heap_memory;
398 return TRUE;
402 static void volume_unload(struct wined3d_resource *resource)
404 struct wined3d_volume *volume = volume_from_resource(resource);
405 struct wined3d_device *device = volume->resource.device;
406 struct wined3d_context *context;
408 if (volume->resource.pool == WINED3D_POOL_DEFAULT)
409 ERR("Unloading DEFAULT pool volume.\n");
411 TRACE("texture %p.\n", resource);
413 if (volume_prepare_system_memory(volume))
415 context = context_acquire(device, NULL);
416 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
417 context_release(context);
418 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_SYSMEM);
420 else
422 ERR("Out of memory when unloading volume %p.\n", volume);
423 wined3d_volume_validate_location(volume, WINED3D_LOCATION_DISCARDED);
424 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_DISCARDED);
427 if (volume->pbo)
429 /* Should not happen because only dynamic default pool volumes
430 * have a buffer, and those are not evicted by device_evit_managed_resources
431 * and must be freed before a non-ex device reset. */
432 ERR("Unloading a volume with a buffer\n");
433 wined3d_volume_free_pbo(volume);
436 /* The texture name is managed by the container. */
437 volume->flags &= ~(WINED3D_VFLAG_ALLOCATED | WINED3D_VFLAG_SRGB_ALLOCATED);
439 resource_unload(resource);
442 ULONG CDECL wined3d_volume_incref(struct wined3d_volume *volume)
444 ULONG refcount;
446 if (volume->container)
448 TRACE("Forwarding to container %p.\n", volume->container);
449 return wined3d_texture_incref(volume->container);
452 refcount = InterlockedIncrement(&volume->resource.ref);
454 TRACE("%p increasing refcount to %u.\n", volume, refcount);
456 return refcount;
459 ULONG CDECL wined3d_volume_decref(struct wined3d_volume *volume)
461 ULONG refcount;
463 if (volume->container)
465 TRACE("Forwarding to container %p.\n", volume->container);
466 return wined3d_texture_decref(volume->container);
469 refcount = InterlockedDecrement(&volume->resource.ref);
471 TRACE("%p decreasing refcount to %u.\n", volume, refcount);
473 if (!refcount)
475 if (volume->pbo)
476 wined3d_volume_free_pbo(volume);
478 resource_cleanup(&volume->resource);
479 volume->resource.parent_ops->wined3d_object_destroyed(volume->resource.parent);
480 HeapFree(GetProcessHeap(), 0, volume);
483 return refcount;
486 void * CDECL wined3d_volume_get_parent(const struct wined3d_volume *volume)
488 TRACE("volume %p.\n", volume);
490 return volume->resource.parent;
493 DWORD CDECL wined3d_volume_set_priority(struct wined3d_volume *volume, DWORD priority)
495 return resource_set_priority(&volume->resource, priority);
498 DWORD CDECL wined3d_volume_get_priority(const struct wined3d_volume *volume)
500 return resource_get_priority(&volume->resource);
503 void CDECL wined3d_volume_preload(struct wined3d_volume *volume)
505 FIXME("volume %p stub!\n", volume);
508 struct wined3d_resource * CDECL wined3d_volume_get_resource(struct wined3d_volume *volume)
510 TRACE("volume %p.\n", volume);
512 return &volume->resource;
515 HRESULT CDECL wined3d_volume_map(struct wined3d_volume *volume,
516 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
518 struct wined3d_device *device = volume->resource.device;
519 struct wined3d_context *context;
520 const struct wined3d_gl_info *gl_info;
521 BYTE *base_memory;
523 TRACE("volume %p, map_desc %p, box %p, flags %#x.\n",
524 volume, map_desc, box, flags);
526 if (!(volume->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU))
528 WARN("Volume %p is not CPU accessible.\n", volume);
529 map_desc->data = NULL;
530 return WINED3DERR_INVALIDCALL;
532 flags = wined3d_resource_sanitize_map_flags(&volume->resource, flags);
534 if (volume->flags & WINED3D_VFLAG_PBO)
536 context = context_acquire(device, NULL);
537 gl_info = context->gl_info;
539 wined3d_volume_prepare_pbo(volume, context);
540 if (flags & WINED3D_MAP_DISCARD)
541 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
542 else
543 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_BUFFER);
545 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, volume->pbo));
547 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
549 GLbitfield mapflags = wined3d_resource_gl_map_flags(flags);
550 mapflags &= ~GL_MAP_FLUSH_EXPLICIT_BIT;
551 base_memory = GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER_ARB,
552 0, volume->resource.size, mapflags));
554 else
556 base_memory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
559 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
560 checkGLcall("Map PBO");
562 context_release(context);
564 else
566 if (!volume_prepare_system_memory(volume))
568 WARN("Out of memory.\n");
569 map_desc->data = NULL;
570 return E_OUTOFMEMORY;
573 if (flags & WINED3D_MAP_DISCARD)
575 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
577 else if (!(volume->locations & WINED3D_LOCATION_SYSMEM))
579 context = context_acquire(device, NULL);
580 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
581 context_release(context);
583 base_memory = volume->resource.allocatedMemory;
586 TRACE("Base memory pointer %p.\n", base_memory);
588 map_desc->row_pitch = volume->resource.format->byte_count * volume->resource.width; /* Bytes / row */
589 map_desc->slice_pitch = volume->resource.format->byte_count
590 * volume->resource.width * volume->resource.height; /* Bytes / slice */
591 if (!box)
593 TRACE("No box supplied - all is ok\n");
594 map_desc->data = base_memory;
596 else
598 TRACE("Lock Box (%p) = l %u, t %u, r %u, b %u, fr %u, ba %u\n",
599 box, box->left, box->top, box->right, box->bottom, box->front, box->back);
600 map_desc->data = base_memory
601 + (map_desc->slice_pitch * box->front) /* FIXME: is front < back or vica versa? */
602 + (map_desc->row_pitch * box->top)
603 + (box->left * volume->resource.format->byte_count);
606 if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
608 wined3d_texture_set_dirty(volume->container, TRUE);
610 if (volume->flags & WINED3D_VFLAG_PBO)
611 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_BUFFER);
612 else
613 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_SYSMEM);
616 volume->flags |= WINED3D_VFLAG_LOCKED;
618 TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
619 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
621 return WINED3D_OK;
624 struct wined3d_volume * CDECL wined3d_volume_from_resource(struct wined3d_resource *resource)
626 return volume_from_resource(resource);
629 HRESULT CDECL wined3d_volume_unmap(struct wined3d_volume *volume)
631 TRACE("volume %p.\n", volume);
633 if (!(volume->flags & WINED3D_VFLAG_LOCKED))
635 WARN("Trying to unlock unlocked volume %p.\n", volume);
636 return WINED3DERR_INVALIDCALL;
639 if (volume->flags & WINED3D_VFLAG_PBO)
641 struct wined3d_device *device = volume->resource.device;
642 struct wined3d_context *context = context_acquire(device, NULL);
643 const struct wined3d_gl_info *gl_info = context->gl_info;
645 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, volume->pbo));
646 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
647 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
648 checkGLcall("Unmap PBO");
650 context_release(context);
653 volume->flags &= ~WINED3D_VFLAG_LOCKED;
655 return WINED3D_OK;
658 static const struct wined3d_resource_ops volume_resource_ops =
660 volume_unload,
663 static HRESULT volume_init(struct wined3d_volume *volume, struct wined3d_device *device, UINT width,
664 UINT height, UINT depth, UINT level, DWORD usage, enum wined3d_format_id format_id,
665 enum wined3d_pool pool, void *parent, const struct wined3d_parent_ops *parent_ops)
667 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
668 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
669 HRESULT hr;
670 UINT size;
672 if (!gl_info->supported[EXT_TEXTURE3D])
674 WARN("Volume cannot be created - no volume texture support.\n");
675 return WINED3DERR_INVALIDCALL;
677 /* TODO: Write tests for other resources and move this check
678 * to resource_init, if applicable. */
679 if (usage & WINED3DUSAGE_DYNAMIC
680 && (pool == WINED3D_POOL_MANAGED || pool == WINED3D_POOL_SCRATCH))
682 WARN("Attempted to create a DYNAMIC texture in pool %u.\n", pool);
683 return WINED3DERR_INVALIDCALL;
686 size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, depth);
688 hr = resource_init(&volume->resource, device, WINED3D_RTYPE_VOLUME, format,
689 WINED3D_MULTISAMPLE_NONE, 0, usage, pool, width, height, depth,
690 size, parent, parent_ops, &volume_resource_ops);
691 if (FAILED(hr))
693 WARN("Failed to initialize resource, returning %#x.\n", hr);
694 return hr;
697 volume->texture_level = level;
698 volume->locations = WINED3D_LOCATION_DISCARDED;
700 if (pool == WINED3D_POOL_DEFAULT && usage & WINED3DUSAGE_DYNAMIC
701 && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT])
703 wined3d_resource_free_sysmem(volume->resource.heap_memory);
704 volume->resource.heap_memory = NULL;
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;