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