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 void volume_set_container(struct wined3d_volume
*volume
, struct wined3d_texture
*container
)
32 TRACE("volume %p, container %p.\n", volume
, container
);
34 volume
->container
= container
;
37 static BOOL
volume_prepare_system_memory(struct wined3d_volume
*volume
)
39 if (volume
->resource
.heap_memory
)
42 if (!wined3d_resource_allocate_sysmem(&volume
->resource
))
44 ERR("Failed to allocate system memory.\n");
50 /* Context activation is done by the caller. */
51 static void wined3d_volume_allocate_texture(struct wined3d_volume
*volume
,
52 const struct wined3d_context
*context
, BOOL srgb
)
54 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
55 const struct wined3d_format
*format
= volume
->resource
.format
;
58 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
] && !format
->convert
59 && volume_prepare_system_memory(volume
))
61 TRACE("Enabling GL_UNPACK_CLIENT_STORAGE_APPLE for volume %p\n", volume
);
62 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
63 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
64 mem
= volume
->resource
.heap_memory
;
65 volume
->flags
|= WINED3D_VFLAG_CLIENT_STORAGE
;
68 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D
, volume
->texture_level
,
69 srgb
? format
->glGammaInternal
: format
->glInternal
,
70 volume
->resource
.width
, volume
->resource
.height
, volume
->resource
.depth
,
71 0, format
->glFormat
, format
->glType
, mem
));
72 checkGLcall("glTexImage3D");
76 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
77 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
81 static void wined3d_volume_get_pitch(const struct wined3d_volume
*volume
, UINT
*row_pitch
,
84 const struct wined3d_format
*format
= volume
->resource
.format
;
86 if (format
->flags
& WINED3DFMT_FLAG_BLOCKS
)
88 /* Since compressed formats are block based, pitch means the amount of
89 * bytes to the next row of block rather than the next row of pixels. */
90 UINT row_block_count
= (volume
->resource
.width
+ format
->block_width
- 1) / format
->block_width
;
91 UINT slice_block_count
= (volume
->resource
.height
+ format
->block_height
- 1) / format
->block_height
;
92 *row_pitch
= row_block_count
* format
->block_byte_count
;
93 *slice_pitch
= *row_pitch
* slice_block_count
;
97 unsigned char alignment
= volume
->resource
.device
->surface_alignment
;
98 *row_pitch
= format
->byte_count
* volume
->resource
.width
; /* Bytes / row */
99 *row_pitch
= (*row_pitch
+ alignment
- 1) & ~(alignment
- 1);
100 *slice_pitch
= *row_pitch
* volume
->resource
.height
;
103 TRACE("Returning row pitch %u, slice pitch %u.\n", *row_pitch
, *slice_pitch
);
106 /* Context activation is done by the caller. */
107 void wined3d_volume_upload_data(struct wined3d_volume
*volume
, const struct wined3d_context
*context
,
108 const struct wined3d_bo_address
*data
)
110 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
111 const struct wined3d_format
*format
= volume
->resource
.format
;
112 UINT width
= volume
->resource
.width
;
113 UINT height
= volume
->resource
.height
;
114 UINT depth
= volume
->resource
.depth
;
115 BYTE
*mem
= data
->addr
;
117 TRACE("volume %p, context %p, level %u, format %s (%#x).\n",
118 volume
, context
, volume
->texture_level
, debug_d3dformat(format
->id
),
123 UINT dst_row_pitch
, dst_slice_pitch
;
124 UINT src_row_pitch
, src_slice_pitch
;
125 UINT alignment
= volume
->resource
.device
->surface_alignment
;
127 if (data
->buffer_object
)
128 ERR("Loading a converted volume from a PBO.\n");
129 if (format
->flags
& WINED3DFMT_FLAG_BLOCKS
)
130 ERR("Converting a block-based format.\n");
132 dst_row_pitch
= width
* format
->conv_byte_count
;
133 dst_row_pitch
= (dst_row_pitch
+ alignment
- 1) & ~(alignment
- 1);
134 dst_slice_pitch
= dst_row_pitch
* height
;
136 wined3d_volume_get_pitch(volume
, &src_row_pitch
, &src_slice_pitch
);
138 mem
= HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch
* depth
);
139 format
->convert(data
->addr
, mem
, src_row_pitch
, src_slice_pitch
,
140 dst_row_pitch
, dst_slice_pitch
, width
, height
, depth
);
143 if (data
->buffer_object
)
145 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, data
->buffer_object
));
146 checkGLcall("glBindBufferARB");
149 GL_EXTCALL(glTexSubImage3DEXT(GL_TEXTURE_3D
, volume
->texture_level
, 0, 0, 0,
150 width
, height
, depth
,
151 format
->glFormat
, format
->glType
, mem
));
152 checkGLcall("glTexSubImage3D");
154 if (data
->buffer_object
)
156 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
157 checkGLcall("glBindBufferARB");
160 if (mem
!= data
->addr
)
161 HeapFree(GetProcessHeap(), 0, mem
);
164 static void wined3d_volume_validate_location(struct wined3d_volume
*volume
, DWORD location
)
166 TRACE("Volume %p, setting %s.\n", volume
, wined3d_debug_location(location
));
167 volume
->locations
|= location
;
168 TRACE("new location flags are %s.\n", wined3d_debug_location(volume
->locations
));
171 void wined3d_volume_invalidate_location(struct wined3d_volume
*volume
, DWORD location
)
173 TRACE("Volume %p, clearing %s.\n", volume
, wined3d_debug_location(location
));
174 volume
->locations
&= ~location
;
175 TRACE("new location flags are %s.\n", wined3d_debug_location(volume
->locations
));
178 /* Context activation is done by the caller. */
179 static void wined3d_volume_download_data(struct wined3d_volume
*volume
,
180 const struct wined3d_context
*context
, const struct wined3d_bo_address
*data
)
182 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
183 const struct wined3d_format
*format
= volume
->resource
.format
;
187 FIXME("Attempting to download a converted volume, format %s.\n",
188 debug_d3dformat(format
->id
));
192 if (data
->buffer_object
)
194 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, data
->buffer_object
));
195 checkGLcall("glBindBufferARB");
198 gl_info
->gl_ops
.gl
.p_glGetTexImage(GL_TEXTURE_3D
, volume
->texture_level
,
199 format
->glFormat
, format
->glType
, data
->addr
);
200 checkGLcall("glGetTexImage");
202 if (data
->buffer_object
)
204 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, 0));
205 checkGLcall("glBindBufferARB");
210 static void wined3d_volume_evict_sysmem(struct wined3d_volume
*volume
)
212 wined3d_resource_free_sysmem(&volume
->resource
);
213 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_SYSMEM
);
216 static DWORD
volume_access_from_location(DWORD location
)
220 case WINED3D_LOCATION_DISCARDED
:
223 case WINED3D_LOCATION_SYSMEM
:
224 return WINED3D_RESOURCE_ACCESS_CPU
;
226 case WINED3D_LOCATION_BUFFER
:
227 case WINED3D_LOCATION_TEXTURE_RGB
:
228 case WINED3D_LOCATION_TEXTURE_SRGB
:
229 return WINED3D_RESOURCE_ACCESS_GPU
;
232 FIXME("Unhandled location %#x.\n", location
);
237 /* Context activation is done by the caller. */
238 static void wined3d_volume_srgb_transfer(struct wined3d_volume
*volume
,
239 struct wined3d_context
*context
, BOOL dest_is_srgb
)
241 struct wined3d_bo_address data
;
242 /* Optimizations are possible, but the effort should be put into either
243 * implementing EXT_SRGB_DECODE in the driver or finding out why we
244 * picked the wrong copy for the original upload and fixing that.
246 * Also keep in mind that we want to avoid using resource.heap_memory
247 * for DEFAULT pool surfaces. */
249 WARN_(d3d_perf
)("Performing slow rgb/srgb volume transfer.\n");
250 data
.buffer_object
= 0;
251 data
.addr
= HeapAlloc(GetProcessHeap(), 0, volume
->resource
.size
);
255 wined3d_texture_bind_and_dirtify(volume
->container
, context
, !dest_is_srgb
);
256 wined3d_volume_download_data(volume
, context
, &data
);
257 wined3d_texture_bind_and_dirtify(volume
->container
, context
, dest_is_srgb
);
258 wined3d_volume_upload_data(volume
, context
, &data
);
260 HeapFree(GetProcessHeap(), 0, data
.addr
);
263 static BOOL
wined3d_volume_can_evict(const struct wined3d_volume
*volume
)
265 if (volume
->resource
.pool
!= WINED3D_POOL_MANAGED
)
267 if (volume
->download_count
>= 10)
269 if (volume
->resource
.format
->convert
)
271 if (volume
->flags
& WINED3D_VFLAG_CLIENT_STORAGE
)
276 /* Context activation is done by the caller. */
277 static void wined3d_volume_load_location(struct wined3d_volume
*volume
,
278 struct wined3d_context
*context
, DWORD location
)
280 DWORD required_access
= volume_access_from_location(location
);
282 TRACE("Volume %p, loading %s, have %s.\n", volume
, wined3d_debug_location(location
),
283 wined3d_debug_location(volume
->locations
));
285 if ((volume
->locations
& location
) == location
)
287 TRACE("Location(s) already up to date.\n");
291 if ((volume
->resource
.access_flags
& required_access
) != required_access
)
293 ERR("Operation requires %#x access, but volume only has %#x.\n",
294 required_access
, volume
->resource
.access_flags
);
300 case WINED3D_LOCATION_TEXTURE_RGB
:
301 case WINED3D_LOCATION_TEXTURE_SRGB
:
302 if ((location
== WINED3D_LOCATION_TEXTURE_RGB
303 && !(volume
->flags
& WINED3D_VFLAG_ALLOCATED
))
304 || (location
== WINED3D_LOCATION_TEXTURE_SRGB
305 && !(volume
->flags
& WINED3D_VFLAG_SRGB_ALLOCATED
)))
306 ERR("Trying to load (s)RGB texture without prior allocation.\n");
308 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
310 TRACE("Volume previously discarded, nothing to do.\n");
311 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
313 else if (volume
->locations
& WINED3D_LOCATION_SYSMEM
)
315 struct wined3d_bo_address data
= {0, volume
->resource
.heap_memory
};
316 wined3d_volume_upload_data(volume
, context
, &data
);
318 else if (volume
->locations
& WINED3D_LOCATION_BUFFER
)
320 struct wined3d_bo_address data
= {volume
->pbo
, NULL
};
321 wined3d_volume_upload_data(volume
, context
, &data
);
323 else if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
325 wined3d_volume_srgb_transfer(volume
, context
, TRUE
);
327 else if (volume
->locations
& WINED3D_LOCATION_TEXTURE_SRGB
)
329 wined3d_volume_srgb_transfer(volume
, context
, FALSE
);
333 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume
->locations
));
336 wined3d_volume_validate_location(volume
, location
);
338 if (wined3d_volume_can_evict(volume
))
339 wined3d_volume_evict_sysmem(volume
);
343 case WINED3D_LOCATION_SYSMEM
:
344 if (!volume
->resource
.heap_memory
)
345 ERR("Trying to load WINED3D_LOCATION_SYSMEM without setting it up first.\n");
347 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
349 TRACE("Volume previously discarded, nothing to do.\n");
350 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
352 else if (volume
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
354 struct wined3d_bo_address data
= {0, volume
->resource
.heap_memory
};
356 if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
357 wined3d_texture_bind_and_dirtify(volume
->container
, context
, FALSE
);
359 wined3d_texture_bind_and_dirtify(volume
->container
, context
, TRUE
);
361 volume
->download_count
++;
362 wined3d_volume_download_data(volume
, context
, &data
);
366 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
367 wined3d_debug_location(volume
->locations
));
370 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_SYSMEM
);
373 case WINED3D_LOCATION_BUFFER
:
374 if (!volume
->pbo
|| !(volume
->flags
& WINED3D_VFLAG_PBO
))
375 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
377 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
379 TRACE("Volume previously discarded, nothing to do.\n");
380 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
382 else if (volume
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
384 struct wined3d_bo_address data
= {volume
->pbo
, NULL
};
386 if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
387 wined3d_texture_bind_and_dirtify(volume
->container
, context
, FALSE
);
389 wined3d_texture_bind_and_dirtify(volume
->container
, context
, TRUE
);
391 wined3d_volume_download_data(volume
, context
, &data
);
395 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
396 wined3d_debug_location(volume
->locations
));
399 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_BUFFER
);
403 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location
),
404 wined3d_debug_location(volume
->locations
));
408 /* Context activation is done by the caller. */
409 void wined3d_volume_load(struct wined3d_volume
*volume
, struct wined3d_context
*context
, BOOL srgb_mode
)
411 wined3d_texture_bind_and_dirtify(volume
->container
, context
, srgb_mode
);
415 if (!(volume
->flags
& WINED3D_VFLAG_SRGB_ALLOCATED
))
417 wined3d_volume_allocate_texture(volume
, context
, TRUE
);
418 volume
->flags
|= WINED3D_VFLAG_SRGB_ALLOCATED
;
421 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_TEXTURE_SRGB
);
425 if (!(volume
->flags
& WINED3D_VFLAG_ALLOCATED
))
427 wined3d_volume_allocate_texture(volume
, context
, FALSE
);
428 volume
->flags
|= WINED3D_VFLAG_ALLOCATED
;
431 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_TEXTURE_RGB
);
435 /* Context activation is done by the caller. */
436 static void wined3d_volume_prepare_pbo(struct wined3d_volume
*volume
, struct wined3d_context
*context
)
438 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
443 GL_EXTCALL(glGenBuffersARB(1, &volume
->pbo
));
444 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, volume
->pbo
));
445 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB
, volume
->resource
.size
, NULL
, GL_STREAM_DRAW_ARB
));
446 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
447 checkGLcall("Create PBO");
449 TRACE("Created PBO %u for volume %p.\n", volume
->pbo
, volume
);
452 static void wined3d_volume_free_pbo(struct wined3d_volume
*volume
)
454 struct wined3d_context
*context
= context_acquire(volume
->resource
.device
, NULL
);
455 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
457 TRACE("Deleting PBO %u belonging to volume %p.\n", volume
->pbo
, volume
);
458 GL_EXTCALL(glDeleteBuffersARB(1, &volume
->pbo
));
459 checkGLcall("glDeleteBuffersARB");
461 context_release(context
);
464 static void volume_unload(struct wined3d_resource
*resource
)
466 struct wined3d_volume
*volume
= volume_from_resource(resource
);
467 struct wined3d_device
*device
= volume
->resource
.device
;
468 struct wined3d_context
*context
;
470 if (volume
->resource
.pool
== WINED3D_POOL_DEFAULT
)
471 ERR("Unloading DEFAULT pool volume.\n");
473 TRACE("texture %p.\n", resource
);
475 if (volume_prepare_system_memory(volume
))
477 context
= context_acquire(device
, NULL
);
478 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_SYSMEM
);
479 context_release(context
);
480 wined3d_volume_invalidate_location(volume
, ~WINED3D_LOCATION_SYSMEM
);
484 ERR("Out of memory when unloading volume %p.\n", volume
);
485 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_DISCARDED
);
486 wined3d_volume_invalidate_location(volume
, ~WINED3D_LOCATION_DISCARDED
);
491 /* Should not happen because only dynamic default pool volumes
492 * have a buffer, and those are not evicted by device_evit_managed_resources
493 * and must be freed before a non-ex device reset. */
494 ERR("Unloading a volume with a buffer\n");
495 wined3d_volume_free_pbo(volume
);
498 /* The texture name is managed by the container. */
499 volume
->flags
&= ~(WINED3D_VFLAG_ALLOCATED
| WINED3D_VFLAG_SRGB_ALLOCATED
500 | WINED3D_VFLAG_CLIENT_STORAGE
);
502 resource_unload(resource
);
505 ULONG CDECL
wined3d_volume_incref(struct wined3d_volume
*volume
)
509 if (volume
->container
)
511 TRACE("Forwarding to container %p.\n", volume
->container
);
512 return wined3d_texture_incref(volume
->container
);
515 refcount
= InterlockedIncrement(&volume
->resource
.ref
);
517 TRACE("%p increasing refcount to %u.\n", volume
, refcount
);
522 ULONG CDECL
wined3d_volume_decref(struct wined3d_volume
*volume
)
526 if (volume
->container
)
528 TRACE("Forwarding to container %p.\n", volume
->container
);
529 return wined3d_texture_decref(volume
->container
);
532 refcount
= InterlockedDecrement(&volume
->resource
.ref
);
534 TRACE("%p decreasing refcount to %u.\n", volume
, refcount
);
539 wined3d_volume_free_pbo(volume
);
541 resource_cleanup(&volume
->resource
);
542 volume
->resource
.parent_ops
->wined3d_object_destroyed(volume
->resource
.parent
);
543 HeapFree(GetProcessHeap(), 0, volume
);
549 void * CDECL
wined3d_volume_get_parent(const struct wined3d_volume
*volume
)
551 TRACE("volume %p.\n", volume
);
553 return volume
->resource
.parent
;
556 DWORD CDECL
wined3d_volume_set_priority(struct wined3d_volume
*volume
, DWORD priority
)
558 return resource_set_priority(&volume
->resource
, priority
);
561 DWORD CDECL
wined3d_volume_get_priority(const struct wined3d_volume
*volume
)
563 return resource_get_priority(&volume
->resource
);
566 void CDECL
wined3d_volume_preload(struct wined3d_volume
*volume
)
568 FIXME("volume %p stub!\n", volume
);
571 struct wined3d_resource
* CDECL
wined3d_volume_get_resource(struct wined3d_volume
*volume
)
573 TRACE("volume %p.\n", volume
);
575 return &volume
->resource
;
578 static BOOL
volume_check_block_align(const struct wined3d_volume
*volume
,
579 const struct wined3d_box
*box
)
581 UINT width_mask
, height_mask
;
582 const struct wined3d_format
*format
= volume
->resource
.format
;
587 /* This assumes power of two block sizes, but NPOT block sizes would be
590 * This also assumes that the format's block depth is 1. */
591 width_mask
= format
->block_width
- 1;
592 height_mask
= format
->block_height
- 1;
594 if (box
->left
& width_mask
)
596 if (box
->top
& height_mask
)
598 if (box
->right
& width_mask
&& box
->right
!= volume
->resource
.width
)
600 if (box
->bottom
& height_mask
&& box
->bottom
!= volume
->resource
.height
)
606 static BOOL
wined3d_volume_check_box_dimensions(const struct wined3d_volume
*volume
,
607 const struct wined3d_box
*box
)
612 if (box
->left
>= box
->right
)
614 if (box
->top
>= box
->bottom
)
616 if (box
->front
>= box
->back
)
618 if (box
->right
> volume
->resource
.width
)
620 if (box
->bottom
> volume
->resource
.height
)
622 if (box
->back
> volume
->resource
.depth
)
628 HRESULT CDECL
wined3d_volume_map(struct wined3d_volume
*volume
,
629 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
631 struct wined3d_device
*device
= volume
->resource
.device
;
632 struct wined3d_context
*context
;
633 const struct wined3d_gl_info
*gl_info
;
635 const struct wined3d_format
*format
= volume
->resource
.format
;
637 TRACE("volume %p, map_desc %p, box %p, flags %#x.\n",
638 volume
, map_desc
, box
, flags
);
640 map_desc
->data
= NULL
;
641 if (!(volume
->resource
.access_flags
& WINED3D_RESOURCE_ACCESS_CPU
))
643 WARN("Volume %p is not CPU accessible.\n", volume
);
644 return WINED3DERR_INVALIDCALL
;
646 if (volume
->resource
.map_count
)
648 WARN("Volume is already mapped.\n");
649 return WINED3DERR_INVALIDCALL
;
651 if (!wined3d_volume_check_box_dimensions(volume
, box
))
653 WARN("Map box is invalid.\n");
654 return WINED3DERR_INVALIDCALL
;
656 if ((format
->flags
& WINED3DFMT_FLAG_BLOCKS
) && !volume_check_block_align(volume
, box
))
658 WARN("Map box is misaligned for %ux%u blocks.\n",
659 format
->block_width
, format
->block_height
);
660 return WINED3DERR_INVALIDCALL
;
663 flags
= wined3d_resource_sanitize_map_flags(&volume
->resource
, flags
);
665 if (volume
->flags
& WINED3D_VFLAG_PBO
)
667 context
= context_acquire(device
, NULL
);
668 gl_info
= context
->gl_info
;
670 wined3d_volume_prepare_pbo(volume
, context
);
671 if (flags
& WINED3D_MAP_DISCARD
)
672 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_BUFFER
);
674 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_BUFFER
);
676 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, volume
->pbo
));
678 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
680 GLbitfield mapflags
= wined3d_resource_gl_map_flags(flags
);
681 mapflags
&= ~GL_MAP_FLUSH_EXPLICIT_BIT
;
682 base_memory
= GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER_ARB
,
683 0, volume
->resource
.size
, mapflags
));
687 GLenum access
= wined3d_resource_gl_legacy_map_flags(flags
);
688 base_memory
= GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, access
));
691 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
692 checkGLcall("Map PBO");
694 context_release(context
);
698 if (!volume_prepare_system_memory(volume
))
700 WARN("Out of memory.\n");
701 map_desc
->data
= NULL
;
702 return E_OUTOFMEMORY
;
705 if (flags
& WINED3D_MAP_DISCARD
)
707 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_SYSMEM
);
709 else if (!(volume
->locations
& WINED3D_LOCATION_SYSMEM
))
711 context
= context_acquire(device
, NULL
);
712 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_SYSMEM
);
713 context_release(context
);
715 base_memory
= volume
->resource
.heap_memory
;
718 TRACE("Base memory pointer %p.\n", base_memory
);
720 if (format
->flags
& WINED3DFMT_FLAG_BROKEN_PITCH
)
722 map_desc
->row_pitch
= volume
->resource
.width
* format
->byte_count
;
723 map_desc
->slice_pitch
= map_desc
->row_pitch
* volume
->resource
.height
;
727 wined3d_volume_get_pitch(volume
, &map_desc
->row_pitch
, &map_desc
->slice_pitch
);
732 TRACE("No box supplied - all is ok\n");
733 map_desc
->data
= base_memory
;
737 TRACE("Lock Box (%p) = l %u, t %u, r %u, b %u, fr %u, ba %u\n",
738 box
, box
->left
, box
->top
, box
->right
, box
->bottom
, box
->front
, box
->back
);
740 if ((format
->flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BROKEN_PITCH
)) == WINED3DFMT_FLAG_BLOCKS
)
742 /* Compressed textures are block based, so calculate the offset of
743 * the block that contains the top-left pixel of the locked rectangle. */
744 map_desc
->data
= base_memory
745 + (box
->front
* map_desc
->slice_pitch
)
746 + ((box
->top
/ format
->block_height
) * map_desc
->row_pitch
)
747 + ((box
->left
/ format
->block_width
) * format
->block_byte_count
);
751 map_desc
->data
= base_memory
752 + (map_desc
->slice_pitch
* box
->front
)
753 + (map_desc
->row_pitch
* box
->top
)
754 + (box
->left
* volume
->resource
.format
->byte_count
);
758 if (!(flags
& (WINED3D_MAP_NO_DIRTY_UPDATE
| WINED3D_MAP_READONLY
)))
760 wined3d_texture_set_dirty(volume
->container
);
762 if (volume
->flags
& WINED3D_VFLAG_PBO
)
763 wined3d_volume_invalidate_location(volume
, ~WINED3D_LOCATION_BUFFER
);
765 wined3d_volume_invalidate_location(volume
, ~WINED3D_LOCATION_SYSMEM
);
768 volume
->resource
.map_count
++;
770 TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
771 map_desc
->data
, map_desc
->row_pitch
, map_desc
->slice_pitch
);
776 struct wined3d_volume
* CDECL
wined3d_volume_from_resource(struct wined3d_resource
*resource
)
778 return volume_from_resource(resource
);
781 HRESULT CDECL
wined3d_volume_unmap(struct wined3d_volume
*volume
)
783 TRACE("volume %p.\n", volume
);
785 if (!volume
->resource
.map_count
)
787 WARN("Trying to unlock an unlocked volume %p.\n", volume
);
788 return WINED3DERR_INVALIDCALL
;
791 if (volume
->flags
& WINED3D_VFLAG_PBO
)
793 struct wined3d_device
*device
= volume
->resource
.device
;
794 struct wined3d_context
*context
= context_acquire(device
, NULL
);
795 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
797 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, volume
->pbo
));
798 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
));
799 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
800 checkGLcall("Unmap PBO");
802 context_release(context
);
805 volume
->resource
.map_count
--;
810 static const struct wined3d_resource_ops volume_resource_ops
=
815 static HRESULT
volume_init(struct wined3d_volume
*volume
, struct wined3d_texture
*container
,
816 const struct wined3d_resource_desc
*desc
, UINT level
)
818 struct wined3d_device
*device
= container
->resource
.device
;
819 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
820 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, desc
->format
);
824 if (!gl_info
->supported
[EXT_TEXTURE3D
])
826 WARN("Volume cannot be created - no volume texture support.\n");
827 return WINED3DERR_INVALIDCALL
;
829 /* TODO: Write tests for other resources and move this check
830 * to resource_init, if applicable. */
831 if (desc
->usage
& WINED3DUSAGE_DYNAMIC
832 && (desc
->pool
== WINED3D_POOL_MANAGED
|| desc
->pool
== WINED3D_POOL_SCRATCH
))
834 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc
->pool
));
835 return WINED3DERR_INVALIDCALL
;
838 size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, desc
->width
, desc
->height
, desc
->depth
);
840 if (FAILED(hr
= resource_init(&volume
->resource
, device
, WINED3D_RTYPE_VOLUME
, format
,
841 WINED3D_MULTISAMPLE_NONE
, 0, desc
->usage
, desc
->pool
, desc
->width
, desc
->height
, desc
->depth
,
842 size
, NULL
, &wined3d_null_parent_ops
, &volume_resource_ops
)))
844 WARN("Failed to initialize resource, returning %#x.\n", hr
);
848 volume
->texture_level
= level
;
849 volume
->locations
= WINED3D_LOCATION_DISCARDED
;
851 if (desc
->pool
== WINED3D_POOL_DEFAULT
&& desc
->usage
& WINED3DUSAGE_DYNAMIC
852 && gl_info
->supported
[ARB_PIXEL_BUFFER_OBJECT
]
855 wined3d_resource_free_sysmem(&volume
->resource
);
856 volume
->flags
|= WINED3D_VFLAG_PBO
;
859 volume_set_container(volume
, container
);
864 HRESULT
wined3d_volume_create(struct wined3d_texture
*container
, const struct wined3d_resource_desc
*desc
,
865 unsigned int level
, struct wined3d_volume
**volume
)
867 struct wined3d_device_parent
*device_parent
= container
->resource
.device
->device_parent
;
868 const struct wined3d_parent_ops
*parent_ops
;
869 struct wined3d_volume
*object
;
873 TRACE("container %p, width %u, height %u, depth %u, level %u, format %s, "
874 "usage %#x, pool %s, volume %p.\n",
875 container
, desc
->width
, desc
->height
, desc
->depth
, level
, debug_d3dformat(desc
->format
),
876 desc
->usage
, debug_d3dpool(desc
->pool
), volume
);
878 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
879 return E_OUTOFMEMORY
;
881 if (FAILED(hr
= volume_init(object
, container
, desc
, level
)))
883 WARN("Failed to initialize volume, returning %#x.\n", hr
);
884 HeapFree(GetProcessHeap(), 0, object
);
888 if (FAILED(hr
= device_parent
->ops
->volume_created(device_parent
,
889 wined3d_texture_get_parent(container
), object
, &parent
, &parent_ops
)))
891 WARN("Failed to create volume parent, hr %#x.\n", hr
);
892 volume_set_container(object
, NULL
);
893 wined3d_volume_decref(object
);
897 TRACE("Created volume %p, parent %p, parent_ops %p.\n", object
, parent
, parent_ops
);
899 object
->resource
.parent
= parent
;
900 object
->resource
.parent_ops
= parent_ops
;