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 "wined3d_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface
);
26 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
28 void volume_set_container(struct wined3d_volume
*volume
, struct wined3d_texture
*container
)
30 TRACE("volume %p, container %p.\n", volume
, container
);
32 volume
->container
= container
;
35 static BOOL
volume_prepare_system_memory(struct wined3d_volume
*volume
)
37 if (volume
->resource
.heap_memory
)
40 if (!wined3d_resource_allocate_sysmem(&volume
->resource
))
42 ERR("Failed to allocate system memory.\n");
48 /* Context activation is done by the caller. */
49 static void wined3d_volume_allocate_texture(struct wined3d_volume
*volume
,
50 const struct wined3d_context
*context
, BOOL srgb
)
52 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
53 const struct wined3d_format
*format
= volume
->resource
.format
;
56 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
] && !format
->convert
57 && volume_prepare_system_memory(volume
))
59 TRACE("Enabling GL_UNPACK_CLIENT_STORAGE_APPLE for volume %p\n", volume
);
60 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
61 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
62 mem
= volume
->resource
.heap_memory
;
63 volume
->flags
|= WINED3D_VFLAG_CLIENT_STORAGE
;
66 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D
, volume
->texture_level
,
67 srgb
? format
->glGammaInternal
: format
->glInternal
,
68 volume
->resource
.width
, volume
->resource
.height
, volume
->resource
.depth
,
69 0, format
->glFormat
, format
->glType
, mem
));
70 checkGLcall("glTexImage3D");
74 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
75 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
79 static void wined3d_volume_get_pitch(const struct wined3d_volume
*volume
, UINT
*row_pitch
,
82 const struct wined3d_format
*format
= volume
->resource
.format
;
84 if (format
->flags
& WINED3DFMT_FLAG_BLOCKS
)
86 /* Since compressed formats are block based, pitch means the amount of
87 * bytes to the next row of block rather than the next row of pixels. */
88 UINT row_block_count
= (volume
->resource
.width
+ format
->block_width
- 1) / format
->block_width
;
89 UINT slice_block_count
= (volume
->resource
.height
+ format
->block_height
- 1) / format
->block_height
;
90 *row_pitch
= row_block_count
* format
->block_byte_count
;
91 *slice_pitch
= *row_pitch
* slice_block_count
;
95 unsigned char alignment
= volume
->resource
.device
->surface_alignment
;
96 *row_pitch
= format
->byte_count
* volume
->resource
.width
; /* Bytes / row */
97 *row_pitch
= (*row_pitch
+ alignment
- 1) & ~(alignment
- 1);
98 *slice_pitch
= *row_pitch
* volume
->resource
.height
;
101 TRACE("Returning row pitch %u, slice pitch %u.\n", *row_pitch
, *slice_pitch
);
104 /* Context activation is done by the caller. */
105 void wined3d_volume_upload_data(struct wined3d_volume
*volume
, const struct wined3d_context
*context
,
106 const struct wined3d_bo_address
*data
)
108 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
109 const struct wined3d_format
*format
= volume
->resource
.format
;
110 UINT width
= volume
->resource
.width
;
111 UINT height
= volume
->resource
.height
;
112 UINT depth
= volume
->resource
.depth
;
113 BYTE
*mem
= data
->addr
;
115 TRACE("volume %p, context %p, level %u, format %s (%#x).\n",
116 volume
, context
, volume
->texture_level
, debug_d3dformat(format
->id
),
121 UINT dst_row_pitch
, dst_slice_pitch
;
122 UINT src_row_pitch
, src_slice_pitch
;
123 UINT alignment
= volume
->resource
.device
->surface_alignment
;
125 if (data
->buffer_object
)
126 ERR("Loading a converted volume from a PBO.\n");
127 if (format
->flags
& WINED3DFMT_FLAG_BLOCKS
)
128 ERR("Converting a block-based format.\n");
130 dst_row_pitch
= width
* format
->conv_byte_count
;
131 dst_row_pitch
= (dst_row_pitch
+ alignment
- 1) & ~(alignment
- 1);
132 dst_slice_pitch
= dst_row_pitch
* height
;
134 wined3d_volume_get_pitch(volume
, &src_row_pitch
, &src_slice_pitch
);
136 mem
= HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch
* depth
);
137 format
->convert(data
->addr
, mem
, src_row_pitch
, src_slice_pitch
,
138 dst_row_pitch
, dst_slice_pitch
, width
, height
, depth
);
141 if (data
->buffer_object
)
143 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, data
->buffer_object
));
144 checkGLcall("glBindBufferARB");
147 GL_EXTCALL(glTexSubImage3DEXT(GL_TEXTURE_3D
, volume
->texture_level
, 0, 0, 0,
148 width
, height
, depth
,
149 format
->glFormat
, format
->glType
, mem
));
150 checkGLcall("glTexSubImage3D");
152 if (data
->buffer_object
)
154 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
155 checkGLcall("glBindBufferARB");
158 if (mem
!= data
->addr
)
159 HeapFree(GetProcessHeap(), 0, mem
);
162 static void wined3d_volume_validate_location(struct wined3d_volume
*volume
, DWORD location
)
164 TRACE("Volume %p, setting %s.\n", volume
, wined3d_debug_location(location
));
165 volume
->locations
|= location
;
166 TRACE("new location flags are %s.\n", wined3d_debug_location(volume
->locations
));
169 void wined3d_volume_invalidate_location(struct wined3d_volume
*volume
, DWORD location
)
171 TRACE("Volume %p, clearing %s.\n", volume
, wined3d_debug_location(location
));
172 volume
->locations
&= ~location
;
173 TRACE("new location flags are %s.\n", wined3d_debug_location(volume
->locations
));
176 /* Context activation is done by the caller. */
177 static void wined3d_volume_download_data(struct wined3d_volume
*volume
,
178 const struct wined3d_context
*context
, const struct wined3d_bo_address
*data
)
180 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
181 const struct wined3d_format
*format
= volume
->resource
.format
;
185 FIXME("Attempting to download a converted volume, format %s.\n",
186 debug_d3dformat(format
->id
));
190 if (data
->buffer_object
)
192 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, data
->buffer_object
));
193 checkGLcall("glBindBufferARB");
196 gl_info
->gl_ops
.gl
.p_glGetTexImage(GL_TEXTURE_3D
, volume
->texture_level
,
197 format
->glFormat
, format
->glType
, data
->addr
);
198 checkGLcall("glGetTexImage");
200 if (data
->buffer_object
)
202 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, 0));
203 checkGLcall("glBindBufferARB");
208 static void wined3d_volume_evict_sysmem(struct wined3d_volume
*volume
)
210 wined3d_resource_free_sysmem(&volume
->resource
);
211 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_SYSMEM
);
214 static DWORD
volume_access_from_location(DWORD location
)
218 case WINED3D_LOCATION_DISCARDED
:
221 case WINED3D_LOCATION_SYSMEM
:
222 return WINED3D_RESOURCE_ACCESS_CPU
;
224 case WINED3D_LOCATION_BUFFER
:
225 case WINED3D_LOCATION_TEXTURE_RGB
:
226 case WINED3D_LOCATION_TEXTURE_SRGB
:
227 return WINED3D_RESOURCE_ACCESS_GPU
;
230 FIXME("Unhandled location %#x.\n", location
);
235 /* Context activation is done by the caller. */
236 static void wined3d_volume_srgb_transfer(struct wined3d_volume
*volume
,
237 struct wined3d_context
*context
, BOOL dest_is_srgb
)
239 struct wined3d_bo_address data
;
240 /* Optimizations are possible, but the effort should be put into either
241 * implementing EXT_SRGB_DECODE in the driver or finding out why we
242 * picked the wrong copy for the original upload and fixing that.
244 * Also keep in mind that we want to avoid using resource.heap_memory
245 * for DEFAULT pool surfaces. */
247 WARN_(d3d_perf
)("Performing slow rgb/srgb volume transfer.\n");
248 data
.buffer_object
= 0;
249 data
.addr
= HeapAlloc(GetProcessHeap(), 0, volume
->resource
.size
);
253 wined3d_texture_bind_and_dirtify(volume
->container
, context
, !dest_is_srgb
);
254 wined3d_volume_download_data(volume
, context
, &data
);
255 wined3d_texture_bind_and_dirtify(volume
->container
, context
, dest_is_srgb
);
256 wined3d_volume_upload_data(volume
, context
, &data
);
258 HeapFree(GetProcessHeap(), 0, data
.addr
);
261 static BOOL
wined3d_volume_can_evict(const struct wined3d_volume
*volume
)
263 if (volume
->resource
.pool
!= WINED3D_POOL_MANAGED
)
265 if (volume
->download_count
>= 10)
267 if (volume
->resource
.format
->convert
)
269 if (volume
->flags
& WINED3D_VFLAG_CLIENT_STORAGE
)
274 /* Context activation is done by the caller. */
275 static void wined3d_volume_load_location(struct wined3d_volume
*volume
,
276 struct wined3d_context
*context
, DWORD location
)
278 DWORD required_access
= volume_access_from_location(location
);
280 TRACE("Volume %p, loading %s, have %s.\n", volume
, wined3d_debug_location(location
),
281 wined3d_debug_location(volume
->locations
));
283 if ((volume
->locations
& location
) == location
)
285 TRACE("Location(s) already up to date.\n");
289 if ((volume
->resource
.access_flags
& required_access
) != required_access
)
291 ERR("Operation requires %#x access, but volume only has %#x.\n",
292 required_access
, volume
->resource
.access_flags
);
298 case WINED3D_LOCATION_TEXTURE_RGB
:
299 case WINED3D_LOCATION_TEXTURE_SRGB
:
300 if ((location
== WINED3D_LOCATION_TEXTURE_RGB
301 && !(volume
->flags
& WINED3D_VFLAG_ALLOCATED
))
302 || (location
== WINED3D_LOCATION_TEXTURE_SRGB
303 && !(volume
->flags
& WINED3D_VFLAG_SRGB_ALLOCATED
)))
304 ERR("Trying to load (s)RGB texture without prior allocation.\n");
306 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
308 TRACE("Volume previously discarded, nothing to do.\n");
309 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
311 else if (volume
->locations
& WINED3D_LOCATION_SYSMEM
)
313 struct wined3d_bo_address data
= {0, volume
->resource
.heap_memory
};
314 wined3d_volume_upload_data(volume
, context
, &data
);
316 else if (volume
->locations
& WINED3D_LOCATION_BUFFER
)
318 struct wined3d_bo_address data
= {volume
->pbo
, NULL
};
319 wined3d_volume_upload_data(volume
, context
, &data
);
321 else if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
323 wined3d_volume_srgb_transfer(volume
, context
, TRUE
);
325 else if (volume
->locations
& WINED3D_LOCATION_TEXTURE_SRGB
)
327 wined3d_volume_srgb_transfer(volume
, context
, FALSE
);
331 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume
->locations
));
334 wined3d_volume_validate_location(volume
, location
);
336 if (wined3d_volume_can_evict(volume
))
337 wined3d_volume_evict_sysmem(volume
);
341 case WINED3D_LOCATION_SYSMEM
:
342 if (!volume
->resource
.heap_memory
)
343 ERR("Trying to load WINED3D_LOCATION_SYSMEM without setting it up first.\n");
345 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
347 TRACE("Volume previously discarded, nothing to do.\n");
348 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
350 else if (volume
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
352 struct wined3d_bo_address data
= {0, volume
->resource
.heap_memory
};
354 if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
355 wined3d_texture_bind_and_dirtify(volume
->container
, context
, FALSE
);
357 wined3d_texture_bind_and_dirtify(volume
->container
, context
, TRUE
);
359 volume
->download_count
++;
360 wined3d_volume_download_data(volume
, context
, &data
);
364 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
365 wined3d_debug_location(volume
->locations
));
368 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_SYSMEM
);
371 case WINED3D_LOCATION_BUFFER
:
372 if (!volume
->pbo
|| !(volume
->flags
& WINED3D_VFLAG_PBO
))
373 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
375 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
377 TRACE("Volume previously discarded, nothing to do.\n");
378 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
380 else if (volume
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
382 struct wined3d_bo_address data
= {volume
->pbo
, NULL
};
384 if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
385 wined3d_texture_bind_and_dirtify(volume
->container
, context
, FALSE
);
387 wined3d_texture_bind_and_dirtify(volume
->container
, context
, TRUE
);
389 wined3d_volume_download_data(volume
, context
, &data
);
393 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
394 wined3d_debug_location(volume
->locations
));
397 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_BUFFER
);
401 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location
),
402 wined3d_debug_location(volume
->locations
));
406 /* Context activation is done by the caller. */
407 void wined3d_volume_load(struct wined3d_volume
*volume
, struct wined3d_context
*context
, BOOL srgb_mode
)
409 wined3d_texture_bind_and_dirtify(volume
->container
, context
, srgb_mode
);
413 if (!(volume
->flags
& WINED3D_VFLAG_SRGB_ALLOCATED
))
415 wined3d_volume_allocate_texture(volume
, context
, TRUE
);
416 volume
->flags
|= WINED3D_VFLAG_SRGB_ALLOCATED
;
419 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_TEXTURE_SRGB
);
423 if (!(volume
->flags
& WINED3D_VFLAG_ALLOCATED
))
425 wined3d_volume_allocate_texture(volume
, context
, FALSE
);
426 volume
->flags
|= WINED3D_VFLAG_ALLOCATED
;
429 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_TEXTURE_RGB
);
433 /* Context activation is done by the caller. */
434 static void wined3d_volume_prepare_pbo(struct wined3d_volume
*volume
, struct wined3d_context
*context
)
436 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
441 GL_EXTCALL(glGenBuffersARB(1, &volume
->pbo
));
442 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, volume
->pbo
));
443 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB
, volume
->resource
.size
, NULL
, GL_STREAM_DRAW_ARB
));
444 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
445 checkGLcall("Create PBO");
447 TRACE("Created PBO %u for volume %p.\n", volume
->pbo
, volume
);
450 static void wined3d_volume_free_pbo(struct wined3d_volume
*volume
)
452 struct wined3d_context
*context
= context_acquire(volume
->resource
.device
, NULL
);
453 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
455 TRACE("Deleting PBO %u belonging to volume %p.\n", volume
->pbo
, volume
);
456 GL_EXTCALL(glDeleteBuffersARB(1, &volume
->pbo
));
457 checkGLcall("glDeleteBuffersARB");
459 context_release(context
);
462 static void volume_unload(struct wined3d_resource
*resource
)
464 struct wined3d_volume
*volume
= volume_from_resource(resource
);
465 struct wined3d_device
*device
= volume
->resource
.device
;
466 struct wined3d_context
*context
;
468 if (volume
->resource
.pool
== WINED3D_POOL_DEFAULT
)
469 ERR("Unloading DEFAULT pool volume.\n");
471 TRACE("texture %p.\n", resource
);
473 if (volume_prepare_system_memory(volume
))
475 context
= context_acquire(device
, NULL
);
476 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_SYSMEM
);
477 context_release(context
);
478 wined3d_volume_invalidate_location(volume
, ~WINED3D_LOCATION_SYSMEM
);
482 ERR("Out of memory when unloading volume %p.\n", volume
);
483 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_DISCARDED
);
484 wined3d_volume_invalidate_location(volume
, ~WINED3D_LOCATION_DISCARDED
);
489 /* Should not happen because only dynamic default pool volumes
490 * have a buffer, and those are not evicted by device_evit_managed_resources
491 * and must be freed before a non-ex device reset. */
492 ERR("Unloading a volume with a buffer\n");
493 wined3d_volume_free_pbo(volume
);
496 /* The texture name is managed by the container. */
497 volume
->flags
&= ~(WINED3D_VFLAG_ALLOCATED
| WINED3D_VFLAG_SRGB_ALLOCATED
498 | WINED3D_VFLAG_CLIENT_STORAGE
);
500 resource_unload(resource
);
503 ULONG CDECL
wined3d_volume_incref(struct wined3d_volume
*volume
)
507 if (volume
->container
)
509 TRACE("Forwarding to container %p.\n", volume
->container
);
510 return wined3d_texture_incref(volume
->container
);
513 refcount
= InterlockedIncrement(&volume
->resource
.ref
);
515 TRACE("%p increasing refcount to %u.\n", volume
, refcount
);
520 ULONG CDECL
wined3d_volume_decref(struct wined3d_volume
*volume
)
524 if (volume
->container
)
526 TRACE("Forwarding to container %p.\n", volume
->container
);
527 return wined3d_texture_decref(volume
->container
);
530 refcount
= InterlockedDecrement(&volume
->resource
.ref
);
532 TRACE("%p decreasing refcount to %u.\n", volume
, refcount
);
537 wined3d_volume_free_pbo(volume
);
539 resource_cleanup(&volume
->resource
);
540 volume
->resource
.parent_ops
->wined3d_object_destroyed(volume
->resource
.parent
);
541 HeapFree(GetProcessHeap(), 0, volume
);
547 void * CDECL
wined3d_volume_get_parent(const struct wined3d_volume
*volume
)
549 TRACE("volume %p.\n", volume
);
551 return volume
->resource
.parent
;
554 DWORD CDECL
wined3d_volume_set_priority(struct wined3d_volume
*volume
, DWORD priority
)
556 return resource_set_priority(&volume
->resource
, priority
);
559 DWORD CDECL
wined3d_volume_get_priority(const struct wined3d_volume
*volume
)
561 return resource_get_priority(&volume
->resource
);
564 void CDECL
wined3d_volume_preload(struct wined3d_volume
*volume
)
566 FIXME("volume %p stub!\n", volume
);
569 struct wined3d_resource
* CDECL
wined3d_volume_get_resource(struct wined3d_volume
*volume
)
571 TRACE("volume %p.\n", volume
);
573 return &volume
->resource
;
576 static BOOL
volume_check_block_align(const struct wined3d_volume
*volume
,
577 const struct wined3d_box
*box
)
579 UINT width_mask
, height_mask
;
580 const struct wined3d_format
*format
= volume
->resource
.format
;
585 /* This assumes power of two block sizes, but NPOT block sizes would be
588 * This also assumes that the format's block depth is 1. */
589 width_mask
= format
->block_width
- 1;
590 height_mask
= format
->block_height
- 1;
592 if (box
->left
& width_mask
)
594 if (box
->top
& height_mask
)
596 if (box
->right
& width_mask
&& box
->right
!= volume
->resource
.width
)
598 if (box
->bottom
& height_mask
&& box
->bottom
!= volume
->resource
.height
)
604 static BOOL
wined3d_volume_check_box_dimensions(const struct wined3d_volume
*volume
,
605 const struct wined3d_box
*box
)
610 if (box
->left
>= box
->right
)
612 if (box
->top
>= box
->bottom
)
614 if (box
->front
>= box
->back
)
616 if (box
->right
> volume
->resource
.width
)
618 if (box
->bottom
> volume
->resource
.height
)
620 if (box
->back
> volume
->resource
.depth
)
626 HRESULT CDECL
wined3d_volume_map(struct wined3d_volume
*volume
,
627 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
629 struct wined3d_device
*device
= volume
->resource
.device
;
630 struct wined3d_context
*context
;
631 const struct wined3d_gl_info
*gl_info
;
633 const struct wined3d_format
*format
= volume
->resource
.format
;
635 TRACE("volume %p, map_desc %p, box %p, flags %#x.\n",
636 volume
, map_desc
, box
, flags
);
638 map_desc
->data
= NULL
;
639 if (!(volume
->resource
.access_flags
& WINED3D_RESOURCE_ACCESS_CPU
))
641 WARN("Volume %p is not CPU accessible.\n", volume
);
642 return WINED3DERR_INVALIDCALL
;
644 if (volume
->resource
.map_count
)
646 WARN("Volume is already mapped.\n");
647 return WINED3DERR_INVALIDCALL
;
649 if (!wined3d_volume_check_box_dimensions(volume
, box
))
651 WARN("Map box is invalid.\n");
652 return WINED3DERR_INVALIDCALL
;
654 if ((format
->flags
& WINED3DFMT_FLAG_BLOCKS
) && !volume_check_block_align(volume
, box
))
656 WARN("Map box is misaligned for %ux%u blocks.\n",
657 format
->block_width
, format
->block_height
);
658 return WINED3DERR_INVALIDCALL
;
661 flags
= wined3d_resource_sanitize_map_flags(&volume
->resource
, flags
);
663 if (volume
->flags
& WINED3D_VFLAG_PBO
)
665 context
= context_acquire(device
, NULL
);
666 gl_info
= context
->gl_info
;
668 wined3d_volume_prepare_pbo(volume
, context
);
669 if (flags
& WINED3D_MAP_DISCARD
)
670 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_BUFFER
);
672 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_BUFFER
);
674 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, volume
->pbo
));
676 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
678 GLbitfield mapflags
= wined3d_resource_gl_map_flags(flags
);
679 mapflags
&= ~GL_MAP_FLUSH_EXPLICIT_BIT
;
680 base_memory
= GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER_ARB
,
681 0, volume
->resource
.size
, mapflags
));
685 GLenum access
= wined3d_resource_gl_legacy_map_flags(flags
);
686 base_memory
= GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, access
));
689 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
690 checkGLcall("Map PBO");
692 context_release(context
);
696 if (!volume_prepare_system_memory(volume
))
698 WARN("Out of memory.\n");
699 map_desc
->data
= NULL
;
700 return E_OUTOFMEMORY
;
703 if (flags
& WINED3D_MAP_DISCARD
)
705 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_SYSMEM
);
707 else if (!(volume
->locations
& WINED3D_LOCATION_SYSMEM
))
709 context
= context_acquire(device
, NULL
);
710 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_SYSMEM
);
711 context_release(context
);
713 base_memory
= volume
->resource
.heap_memory
;
716 TRACE("Base memory pointer %p.\n", base_memory
);
718 if (format
->flags
& WINED3DFMT_FLAG_BROKEN_PITCH
)
720 map_desc
->row_pitch
= volume
->resource
.width
* format
->byte_count
;
721 map_desc
->slice_pitch
= map_desc
->row_pitch
* volume
->resource
.height
;
725 wined3d_volume_get_pitch(volume
, &map_desc
->row_pitch
, &map_desc
->slice_pitch
);
730 TRACE("No box supplied - all is ok\n");
731 map_desc
->data
= base_memory
;
735 TRACE("Lock Box (%p) = l %u, t %u, r %u, b %u, fr %u, ba %u\n",
736 box
, box
->left
, box
->top
, box
->right
, box
->bottom
, box
->front
, box
->back
);
738 if ((format
->flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BROKEN_PITCH
)) == WINED3DFMT_FLAG_BLOCKS
)
740 /* Compressed textures are block based, so calculate the offset of
741 * the block that contains the top-left pixel of the locked rectangle. */
742 map_desc
->data
= base_memory
743 + (box
->front
* map_desc
->slice_pitch
)
744 + ((box
->top
/ format
->block_height
) * map_desc
->row_pitch
)
745 + ((box
->left
/ format
->block_width
) * format
->block_byte_count
);
749 map_desc
->data
= base_memory
750 + (map_desc
->slice_pitch
* box
->front
)
751 + (map_desc
->row_pitch
* box
->top
)
752 + (box
->left
* volume
->resource
.format
->byte_count
);
756 if (!(flags
& (WINED3D_MAP_NO_DIRTY_UPDATE
| WINED3D_MAP_READONLY
)))
758 wined3d_texture_set_dirty(volume
->container
);
760 if (volume
->flags
& WINED3D_VFLAG_PBO
)
761 wined3d_volume_invalidate_location(volume
, ~WINED3D_LOCATION_BUFFER
);
763 wined3d_volume_invalidate_location(volume
, ~WINED3D_LOCATION_SYSMEM
);
766 volume
->resource
.map_count
++;
768 TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
769 map_desc
->data
, map_desc
->row_pitch
, map_desc
->slice_pitch
);
774 struct wined3d_volume
* CDECL
wined3d_volume_from_resource(struct wined3d_resource
*resource
)
776 return volume_from_resource(resource
);
779 HRESULT CDECL
wined3d_volume_unmap(struct wined3d_volume
*volume
)
781 TRACE("volume %p.\n", volume
);
783 if (!volume
->resource
.map_count
)
785 WARN("Trying to unlock an unlocked volume %p.\n", volume
);
786 return WINED3DERR_INVALIDCALL
;
789 if (volume
->flags
& WINED3D_VFLAG_PBO
)
791 struct wined3d_device
*device
= volume
->resource
.device
;
792 struct wined3d_context
*context
= context_acquire(device
, NULL
);
793 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
795 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, volume
->pbo
));
796 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
));
797 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
798 checkGLcall("Unmap PBO");
800 context_release(context
);
803 volume
->resource
.map_count
--;
808 static const struct wined3d_resource_ops volume_resource_ops
=
813 static HRESULT
volume_init(struct wined3d_volume
*volume
, struct wined3d_texture
*container
,
814 const struct wined3d_resource_desc
*desc
, UINT level
)
816 struct wined3d_device
*device
= container
->resource
.device
;
817 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
818 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, desc
->format
);
822 if (!gl_info
->supported
[EXT_TEXTURE3D
])
824 WARN("Volume cannot be created - no volume texture support.\n");
825 return WINED3DERR_INVALIDCALL
;
827 /* TODO: Write tests for other resources and move this check
828 * to resource_init, if applicable. */
829 if (desc
->usage
& WINED3DUSAGE_DYNAMIC
830 && (desc
->pool
== WINED3D_POOL_MANAGED
|| desc
->pool
== WINED3D_POOL_SCRATCH
))
832 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc
->pool
));
833 return WINED3DERR_INVALIDCALL
;
836 size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, desc
->width
, desc
->height
, desc
->depth
);
838 if (FAILED(hr
= resource_init(&volume
->resource
, device
, WINED3D_RTYPE_VOLUME
, format
,
839 WINED3D_MULTISAMPLE_NONE
, 0, desc
->usage
, desc
->pool
, desc
->width
, desc
->height
, desc
->depth
,
840 size
, NULL
, &wined3d_null_parent_ops
, &volume_resource_ops
)))
842 WARN("Failed to initialize resource, returning %#x.\n", hr
);
846 volume
->texture_level
= level
;
847 volume
->locations
= WINED3D_LOCATION_DISCARDED
;
849 if (desc
->pool
== WINED3D_POOL_DEFAULT
&& desc
->usage
& WINED3DUSAGE_DYNAMIC
850 && gl_info
->supported
[ARB_PIXEL_BUFFER_OBJECT
]
853 wined3d_resource_free_sysmem(&volume
->resource
);
854 volume
->flags
|= WINED3D_VFLAG_PBO
;
857 volume_set_container(volume
, container
);
862 HRESULT
wined3d_volume_create(struct wined3d_texture
*container
, const struct wined3d_resource_desc
*desc
,
863 unsigned int level
, struct wined3d_volume
**volume
)
865 struct wined3d_device_parent
*device_parent
= container
->resource
.device
->device_parent
;
866 const struct wined3d_parent_ops
*parent_ops
;
867 struct wined3d_volume
*object
;
871 TRACE("container %p, width %u, height %u, depth %u, level %u, format %s, "
872 "usage %#x, pool %s, volume %p.\n",
873 container
, desc
->width
, desc
->height
, desc
->depth
, level
, debug_d3dformat(desc
->format
),
874 desc
->usage
, debug_d3dpool(desc
->pool
), volume
);
876 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
877 return E_OUTOFMEMORY
;
879 if (FAILED(hr
= volume_init(object
, container
, desc
, level
)))
881 WARN("Failed to initialize volume, returning %#x.\n", hr
);
882 HeapFree(GetProcessHeap(), 0, object
);
886 if (FAILED(hr
= device_parent
->ops
->volume_created(device_parent
,
887 wined3d_texture_get_parent(container
), object
, &parent
, &parent_ops
)))
889 WARN("Failed to create volume parent, hr %#x.\n", hr
);
890 volume_set_container(object
, NULL
);
891 wined3d_volume_decref(object
);
895 TRACE("Created volume %p, parent %p, parent_ops %p.\n", object
, parent
, parent_ops
);
897 object
->resource
.parent
= parent
;
898 object
->resource
.parent_ops
= parent_ops
;