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 #define WINED3D_VFLAG_ALLOCATED 0x00000001
31 #define WINED3D_VFLAG_SRGB_ALLOCATED 0x00000002
32 #define WINED3D_VFLAG_CLIENT_STORAGE 0x00000004
34 static BOOL
volume_prepare_system_memory(struct wined3d_volume
*volume
)
36 if (volume
->resource
.heap_memory
)
39 if (!wined3d_resource_allocate_sysmem(&volume
->resource
))
41 ERR("Failed to allocate system memory.\n");
47 /* Context activation is done by the caller. */
48 static void wined3d_volume_allocate_texture(struct wined3d_volume
*volume
,
49 const struct wined3d_context
*context
, BOOL srgb
)
51 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
52 const struct wined3d_format
*format
= volume
->resource
.format
;
55 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
] && !format
->convert
56 && volume_prepare_system_memory(volume
))
58 TRACE("Enabling GL_UNPACK_CLIENT_STORAGE_APPLE for volume %p\n", volume
);
59 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
60 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
61 mem
= volume
->resource
.heap_memory
;
62 volume
->flags
|= WINED3D_VFLAG_CLIENT_STORAGE
;
65 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D
, volume
->texture_level
,
66 srgb
? format
->glGammaInternal
: format
->glInternal
,
67 volume
->resource
.width
, volume
->resource
.height
, volume
->resource
.depth
,
68 0, format
->glFormat
, format
->glType
, mem
));
69 checkGLcall("glTexImage3D");
73 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_FALSE
);
74 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
78 static void wined3d_volume_get_pitch(const struct wined3d_volume
*volume
, UINT
*row_pitch
,
81 const struct wined3d_format
*format
= volume
->resource
.format
;
83 if (format
->flags
& WINED3DFMT_FLAG_BLOCKS
)
85 /* Since compressed formats are block based, pitch means the amount of
86 * bytes to the next row of block rather than the next row of pixels. */
87 UINT row_block_count
= (volume
->resource
.width
+ format
->block_width
- 1) / format
->block_width
;
88 UINT slice_block_count
= (volume
->resource
.height
+ format
->block_height
- 1) / format
->block_height
;
89 *row_pitch
= row_block_count
* format
->block_byte_count
;
90 *slice_pitch
= *row_pitch
* slice_block_count
;
94 unsigned char alignment
= volume
->resource
.device
->surface_alignment
;
95 *row_pitch
= format
->byte_count
* volume
->resource
.width
; /* Bytes / row */
96 *row_pitch
= (*row_pitch
+ alignment
- 1) & ~(alignment
- 1);
97 *slice_pitch
= *row_pitch
* volume
->resource
.height
;
100 TRACE("Returning row pitch %u, slice pitch %u.\n", *row_pitch
, *slice_pitch
);
103 /* Context activation is done by the caller. */
104 void wined3d_volume_upload_data(struct wined3d_volume
*volume
, const struct wined3d_context
*context
,
105 const struct wined3d_bo_address
*data
)
107 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
108 const struct wined3d_format
*format
= volume
->resource
.format
;
109 UINT width
= volume
->resource
.width
;
110 UINT height
= volume
->resource
.height
;
111 UINT depth
= volume
->resource
.depth
;
112 BYTE
*mem
= data
->addr
;
114 TRACE("volume %p, context %p, level %u, format %s (%#x).\n",
115 volume
, context
, volume
->texture_level
, debug_d3dformat(format
->id
),
120 UINT dst_row_pitch
, dst_slice_pitch
;
121 UINT src_row_pitch
, src_slice_pitch
;
122 UINT alignment
= volume
->resource
.device
->surface_alignment
;
124 if (data
->buffer_object
)
125 ERR("Loading a converted volume from a PBO.\n");
126 if (format
->flags
& WINED3DFMT_FLAG_BLOCKS
)
127 ERR("Converting a block-based format.\n");
129 dst_row_pitch
= width
* format
->conv_byte_count
;
130 dst_row_pitch
= (dst_row_pitch
+ alignment
- 1) & ~(alignment
- 1);
131 dst_slice_pitch
= dst_row_pitch
* height
;
133 wined3d_volume_get_pitch(volume
, &src_row_pitch
, &src_slice_pitch
);
135 mem
= HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch
* depth
);
136 format
->convert(data
->addr
, mem
, src_row_pitch
, src_slice_pitch
,
137 dst_row_pitch
, dst_slice_pitch
, width
, height
, depth
);
140 if (data
->buffer_object
)
142 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, data
->buffer_object
));
143 checkGLcall("glBindBufferARB");
146 GL_EXTCALL(glTexSubImage3DEXT(GL_TEXTURE_3D
, volume
->texture_level
, 0, 0, 0,
147 width
, height
, depth
,
148 format
->glFormat
, format
->glType
, mem
));
149 checkGLcall("glTexSubImage3D");
151 if (data
->buffer_object
)
153 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
154 checkGLcall("glBindBufferARB");
157 if (mem
!= data
->addr
)
158 HeapFree(GetProcessHeap(), 0, mem
);
161 static void wined3d_volume_validate_location(struct wined3d_volume
*volume
, DWORD location
)
163 TRACE("Volume %p, setting %s.\n", volume
, wined3d_debug_location(location
));
164 volume
->locations
|= location
;
165 TRACE("new location flags are %s.\n", wined3d_debug_location(volume
->locations
));
168 void wined3d_volume_invalidate_location(struct wined3d_volume
*volume
, DWORD location
)
170 TRACE("Volume %p, clearing %s.\n", volume
, wined3d_debug_location(location
));
171 volume
->locations
&= ~location
;
172 TRACE("new location flags are %s.\n", wined3d_debug_location(volume
->locations
));
175 /* Context activation is done by the caller. */
176 static void wined3d_volume_download_data(struct wined3d_volume
*volume
,
177 const struct wined3d_context
*context
, const struct wined3d_bo_address
*data
)
179 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
180 const struct wined3d_format
*format
= volume
->resource
.format
;
184 FIXME("Attempting to download a converted volume, format %s.\n",
185 debug_d3dformat(format
->id
));
189 if (data
->buffer_object
)
191 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, data
->buffer_object
));
192 checkGLcall("glBindBufferARB");
195 gl_info
->gl_ops
.gl
.p_glGetTexImage(GL_TEXTURE_3D
, volume
->texture_level
,
196 format
->glFormat
, format
->glType
, data
->addr
);
197 checkGLcall("glGetTexImage");
199 if (data
->buffer_object
)
201 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, 0));
202 checkGLcall("glBindBufferARB");
207 static void wined3d_volume_evict_sysmem(struct wined3d_volume
*volume
)
209 wined3d_resource_free_sysmem(&volume
->resource
);
210 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_SYSMEM
);
213 static DWORD
volume_access_from_location(DWORD location
)
217 case WINED3D_LOCATION_DISCARDED
:
220 case WINED3D_LOCATION_SYSMEM
:
221 return WINED3D_RESOURCE_ACCESS_CPU
;
223 case WINED3D_LOCATION_BUFFER
:
224 case WINED3D_LOCATION_TEXTURE_RGB
:
225 case WINED3D_LOCATION_TEXTURE_SRGB
:
226 return WINED3D_RESOURCE_ACCESS_GPU
;
229 FIXME("Unhandled location %#x.\n", location
);
234 /* Context activation is done by the caller. */
235 static void wined3d_volume_srgb_transfer(struct wined3d_volume
*volume
,
236 struct wined3d_context
*context
, BOOL dest_is_srgb
)
238 struct wined3d_bo_address data
;
239 /* Optimizations are possible, but the effort should be put into either
240 * implementing EXT_SRGB_DECODE in the driver or finding out why we
241 * picked the wrong copy for the original upload and fixing that.
243 * Also keep in mind that we want to avoid using resource.heap_memory
244 * for DEFAULT pool surfaces. */
246 WARN_(d3d_perf
)("Performing slow rgb/srgb volume transfer.\n");
247 data
.buffer_object
= 0;
248 data
.addr
= HeapAlloc(GetProcessHeap(), 0, volume
->resource
.size
);
252 wined3d_texture_bind_and_dirtify(volume
->container
, context
, !dest_is_srgb
);
253 wined3d_volume_download_data(volume
, context
, &data
);
254 wined3d_texture_bind_and_dirtify(volume
->container
, context
, dest_is_srgb
);
255 wined3d_volume_upload_data(volume
, context
, &data
);
257 HeapFree(GetProcessHeap(), 0, data
.addr
);
260 static BOOL
wined3d_volume_can_evict(const struct wined3d_volume
*volume
)
262 if (volume
->resource
.pool
!= WINED3D_POOL_MANAGED
)
264 if (volume
->download_count
>= 10)
266 if (volume
->resource
.format
->convert
)
268 if (volume
->flags
& WINED3D_VFLAG_CLIENT_STORAGE
)
273 /* Context activation is done by the caller. */
274 static void wined3d_volume_load_location(struct wined3d_volume
*volume
,
275 struct wined3d_context
*context
, DWORD location
)
277 DWORD required_access
= volume_access_from_location(location
);
279 TRACE("Volume %p, loading %s, have %s.\n", volume
, wined3d_debug_location(location
),
280 wined3d_debug_location(volume
->locations
));
282 if ((volume
->locations
& location
) == location
)
284 TRACE("Location(s) already up to date.\n");
288 if ((volume
->resource
.access_flags
& required_access
) != required_access
)
290 ERR("Operation requires %#x access, but volume only has %#x.\n",
291 required_access
, volume
->resource
.access_flags
);
297 case WINED3D_LOCATION_TEXTURE_RGB
:
298 case WINED3D_LOCATION_TEXTURE_SRGB
:
299 if ((location
== WINED3D_LOCATION_TEXTURE_RGB
300 && !(volume
->flags
& WINED3D_VFLAG_ALLOCATED
))
301 || (location
== WINED3D_LOCATION_TEXTURE_SRGB
302 && !(volume
->flags
& WINED3D_VFLAG_SRGB_ALLOCATED
)))
303 ERR("Trying to load (s)RGB texture without prior allocation.\n");
305 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
307 TRACE("Volume previously discarded, nothing to do.\n");
308 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
310 else if (volume
->locations
& WINED3D_LOCATION_SYSMEM
)
312 struct wined3d_bo_address data
= {0, volume
->resource
.heap_memory
};
313 wined3d_volume_upload_data(volume
, context
, &data
);
315 else if (volume
->locations
& WINED3D_LOCATION_BUFFER
)
317 struct wined3d_bo_address data
= {volume
->pbo
, NULL
};
318 wined3d_volume_upload_data(volume
, context
, &data
);
320 else if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
322 wined3d_volume_srgb_transfer(volume
, context
, TRUE
);
324 else if (volume
->locations
& WINED3D_LOCATION_TEXTURE_SRGB
)
326 wined3d_volume_srgb_transfer(volume
, context
, FALSE
);
330 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume
->locations
));
333 wined3d_volume_validate_location(volume
, location
);
335 if (wined3d_volume_can_evict(volume
))
336 wined3d_volume_evict_sysmem(volume
);
340 case WINED3D_LOCATION_SYSMEM
:
341 if (!volume
->resource
.heap_memory
)
342 ERR("Trying to load WINED3D_LOCATION_SYSMEM without setting it up first.\n");
344 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
346 TRACE("Volume previously discarded, nothing to do.\n");
347 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
349 else if (volume
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
351 struct wined3d_bo_address data
= {0, volume
->resource
.heap_memory
};
353 if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
354 wined3d_texture_bind_and_dirtify(volume
->container
, context
, FALSE
);
356 wined3d_texture_bind_and_dirtify(volume
->container
, context
, TRUE
);
358 volume
->download_count
++;
359 wined3d_volume_download_data(volume
, context
, &data
);
363 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
364 wined3d_debug_location(volume
->locations
));
367 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_SYSMEM
);
370 case WINED3D_LOCATION_BUFFER
:
372 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
374 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
376 TRACE("Volume previously discarded, nothing to do.\n");
377 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
379 else if (volume
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
381 struct wined3d_bo_address data
= {volume
->pbo
, NULL
};
383 if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
384 wined3d_texture_bind_and_dirtify(volume
->container
, context
, FALSE
);
386 wined3d_texture_bind_and_dirtify(volume
->container
, context
, TRUE
);
388 wined3d_volume_download_data(volume
, context
, &data
);
392 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
393 wined3d_debug_location(volume
->locations
));
396 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_BUFFER
);
400 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location
),
401 wined3d_debug_location(volume
->locations
));
405 /* Context activation is done by the caller. */
406 void wined3d_volume_load(struct wined3d_volume
*volume
, struct wined3d_context
*context
, BOOL srgb_mode
)
408 wined3d_texture_bind_and_dirtify(volume
->container
, context
, srgb_mode
);
412 if (!(volume
->flags
& WINED3D_VFLAG_SRGB_ALLOCATED
))
414 wined3d_volume_allocate_texture(volume
, context
, TRUE
);
415 volume
->flags
|= WINED3D_VFLAG_SRGB_ALLOCATED
;
418 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_TEXTURE_SRGB
);
422 if (!(volume
->flags
& WINED3D_VFLAG_ALLOCATED
))
424 wined3d_volume_allocate_texture(volume
, context
, FALSE
);
425 volume
->flags
|= WINED3D_VFLAG_ALLOCATED
;
428 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_TEXTURE_RGB
);
432 /* Context activation is done by the caller. */
433 static void wined3d_volume_prepare_pbo(struct wined3d_volume
*volume
, struct wined3d_context
*context
)
435 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
440 GL_EXTCALL(glGenBuffersARB(1, &volume
->pbo
));
441 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, volume
->pbo
));
442 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB
, volume
->resource
.size
, NULL
, GL_STREAM_DRAW_ARB
));
443 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
444 checkGLcall("Create PBO");
446 TRACE("Created PBO %u for volume %p.\n", volume
->pbo
, volume
);
449 static void wined3d_volume_free_pbo(struct wined3d_volume
*volume
)
451 struct wined3d_context
*context
= context_acquire(volume
->resource
.device
, NULL
);
452 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
454 TRACE("Deleting PBO %u belonging to volume %p.\n", volume
->pbo
, volume
);
455 GL_EXTCALL(glDeleteBuffersARB(1, &volume
->pbo
));
456 checkGLcall("glDeleteBuffersARB");
458 context_release(context
);
461 void wined3d_volume_destroy(struct wined3d_volume
*volume
)
463 TRACE("volume %p.\n", volume
);
466 wined3d_volume_free_pbo(volume
);
468 resource_cleanup(&volume
->resource
);
469 volume
->resource
.parent_ops
->wined3d_object_destroyed(volume
->resource
.parent
);
470 HeapFree(GetProcessHeap(), 0, volume
);
473 static void volume_unload(struct wined3d_resource
*resource
)
475 struct wined3d_volume
*volume
= volume_from_resource(resource
);
476 struct wined3d_device
*device
= volume
->resource
.device
;
477 struct wined3d_context
*context
;
479 if (volume
->resource
.pool
== WINED3D_POOL_DEFAULT
)
480 ERR("Unloading DEFAULT pool volume.\n");
482 TRACE("texture %p.\n", resource
);
484 if (volume_prepare_system_memory(volume
))
486 context
= context_acquire(device
, NULL
);
487 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_SYSMEM
);
488 context_release(context
);
489 wined3d_volume_invalidate_location(volume
, ~WINED3D_LOCATION_SYSMEM
);
493 ERR("Out of memory when unloading volume %p.\n", volume
);
494 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_DISCARDED
);
495 wined3d_volume_invalidate_location(volume
, ~WINED3D_LOCATION_DISCARDED
);
500 /* Should not happen because only dynamic default pool volumes
501 * have a buffer, and those are not evicted by device_evit_managed_resources
502 * and must be freed before a non-ex device reset. */
503 ERR("Unloading a volume with a buffer\n");
504 wined3d_volume_free_pbo(volume
);
507 /* The texture name is managed by the container. */
508 volume
->flags
&= ~(WINED3D_VFLAG_ALLOCATED
| WINED3D_VFLAG_SRGB_ALLOCATED
509 | WINED3D_VFLAG_CLIENT_STORAGE
);
511 resource_unload(resource
);
514 ULONG CDECL
wined3d_volume_incref(struct wined3d_volume
*volume
)
516 TRACE("Forwarding to container %p.\n", volume
->container
);
518 return wined3d_texture_incref(volume
->container
);
521 ULONG CDECL
wined3d_volume_decref(struct wined3d_volume
*volume
)
523 TRACE("Forwarding to container %p.\n", volume
->container
);
525 return wined3d_texture_decref(volume
->container
);
528 void * CDECL
wined3d_volume_get_parent(const struct wined3d_volume
*volume
)
530 TRACE("volume %p.\n", volume
);
532 return volume
->resource
.parent
;
535 void CDECL
wined3d_volume_preload(struct wined3d_volume
*volume
)
537 FIXME("volume %p stub!\n", volume
);
540 struct wined3d_resource
* CDECL
wined3d_volume_get_resource(struct wined3d_volume
*volume
)
542 TRACE("volume %p.\n", volume
);
544 return &volume
->resource
;
547 static BOOL
volume_check_block_align(const struct wined3d_volume
*volume
,
548 const struct wined3d_box
*box
)
550 UINT width_mask
, height_mask
;
551 const struct wined3d_format
*format
= volume
->resource
.format
;
556 /* This assumes power of two block sizes, but NPOT block sizes would be
559 * This also assumes that the format's block depth is 1. */
560 width_mask
= format
->block_width
- 1;
561 height_mask
= format
->block_height
- 1;
563 if (box
->left
& width_mask
)
565 if (box
->top
& height_mask
)
567 if (box
->right
& width_mask
&& box
->right
!= volume
->resource
.width
)
569 if (box
->bottom
& height_mask
&& box
->bottom
!= volume
->resource
.height
)
575 static BOOL
wined3d_volume_check_box_dimensions(const struct wined3d_volume
*volume
,
576 const struct wined3d_box
*box
)
581 if (box
->left
>= box
->right
)
583 if (box
->top
>= box
->bottom
)
585 if (box
->front
>= box
->back
)
587 if (box
->right
> volume
->resource
.width
)
589 if (box
->bottom
> volume
->resource
.height
)
591 if (box
->back
> volume
->resource
.depth
)
597 HRESULT CDECL
wined3d_volume_map(struct wined3d_volume
*volume
,
598 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
600 struct wined3d_device
*device
= volume
->resource
.device
;
601 struct wined3d_context
*context
;
602 const struct wined3d_gl_info
*gl_info
;
604 const struct wined3d_format
*format
= volume
->resource
.format
;
606 TRACE("volume %p, map_desc %p, box %p, flags %#x.\n",
607 volume
, map_desc
, box
, flags
);
609 map_desc
->data
= NULL
;
610 if (!(volume
->resource
.access_flags
& WINED3D_RESOURCE_ACCESS_CPU
))
612 WARN("Volume %p is not CPU accessible.\n", volume
);
613 return WINED3DERR_INVALIDCALL
;
615 if (volume
->resource
.map_count
)
617 WARN("Volume is already mapped.\n");
618 return WINED3DERR_INVALIDCALL
;
620 if (!wined3d_volume_check_box_dimensions(volume
, box
))
622 WARN("Map box is invalid.\n");
623 return WINED3DERR_INVALIDCALL
;
625 if ((format
->flags
& WINED3DFMT_FLAG_BLOCKS
) && !volume_check_block_align(volume
, box
))
627 WARN("Map box is misaligned for %ux%u blocks.\n",
628 format
->block_width
, format
->block_height
);
629 return WINED3DERR_INVALIDCALL
;
632 flags
= wined3d_resource_sanitize_map_flags(&volume
->resource
, flags
);
634 if (volume
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
)
636 context
= context_acquire(device
, NULL
);
637 gl_info
= context
->gl_info
;
639 wined3d_volume_prepare_pbo(volume
, context
);
640 if (flags
& WINED3D_MAP_DISCARD
)
641 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_BUFFER
);
643 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_BUFFER
);
645 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, volume
->pbo
));
647 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
649 GLbitfield mapflags
= wined3d_resource_gl_map_flags(flags
);
650 mapflags
&= ~GL_MAP_FLUSH_EXPLICIT_BIT
;
651 base_memory
= GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER_ARB
,
652 0, volume
->resource
.size
, mapflags
));
656 GLenum access
= wined3d_resource_gl_legacy_map_flags(flags
);
657 base_memory
= GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, access
));
660 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
661 checkGLcall("Map PBO");
663 context_release(context
);
667 if (!volume_prepare_system_memory(volume
))
669 WARN("Out of memory.\n");
670 map_desc
->data
= NULL
;
671 return E_OUTOFMEMORY
;
674 if (flags
& WINED3D_MAP_DISCARD
)
676 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_SYSMEM
);
678 else if (!(volume
->locations
& WINED3D_LOCATION_SYSMEM
))
680 context
= context_acquire(device
, NULL
);
681 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_SYSMEM
);
682 context_release(context
);
684 base_memory
= volume
->resource
.heap_memory
;
687 TRACE("Base memory pointer %p.\n", base_memory
);
689 if (format
->flags
& WINED3DFMT_FLAG_BROKEN_PITCH
)
691 map_desc
->row_pitch
= volume
->resource
.width
* format
->byte_count
;
692 map_desc
->slice_pitch
= map_desc
->row_pitch
* volume
->resource
.height
;
696 wined3d_volume_get_pitch(volume
, &map_desc
->row_pitch
, &map_desc
->slice_pitch
);
701 TRACE("No box supplied - all is ok\n");
702 map_desc
->data
= base_memory
;
706 TRACE("Lock Box (%p) = l %u, t %u, r %u, b %u, fr %u, ba %u\n",
707 box
, box
->left
, box
->top
, box
->right
, box
->bottom
, box
->front
, box
->back
);
709 if ((format
->flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BROKEN_PITCH
)) == WINED3DFMT_FLAG_BLOCKS
)
711 /* Compressed textures are block based, so calculate the offset of
712 * the block that contains the top-left pixel of the locked rectangle. */
713 map_desc
->data
= base_memory
714 + (box
->front
* map_desc
->slice_pitch
)
715 + ((box
->top
/ format
->block_height
) * map_desc
->row_pitch
)
716 + ((box
->left
/ format
->block_width
) * format
->block_byte_count
);
720 map_desc
->data
= base_memory
721 + (map_desc
->slice_pitch
* box
->front
)
722 + (map_desc
->row_pitch
* box
->top
)
723 + (box
->left
* volume
->resource
.format
->byte_count
);
727 if (!(flags
& (WINED3D_MAP_NO_DIRTY_UPDATE
| WINED3D_MAP_READONLY
)))
729 wined3d_texture_set_dirty(volume
->container
);
730 wined3d_volume_invalidate_location(volume
, ~volume
->resource
.map_binding
);
733 volume
->resource
.map_count
++;
735 TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
736 map_desc
->data
, map_desc
->row_pitch
, map_desc
->slice_pitch
);
741 struct wined3d_volume
* CDECL
wined3d_volume_from_resource(struct wined3d_resource
*resource
)
743 return volume_from_resource(resource
);
746 HRESULT CDECL
wined3d_volume_unmap(struct wined3d_volume
*volume
)
748 TRACE("volume %p.\n", volume
);
750 if (!volume
->resource
.map_count
)
752 WARN("Trying to unlock an unlocked volume %p.\n", volume
);
753 return WINED3DERR_INVALIDCALL
;
756 if (volume
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
)
758 struct wined3d_device
*device
= volume
->resource
.device
;
759 struct wined3d_context
*context
= context_acquire(device
, NULL
);
760 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
762 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, volume
->pbo
));
763 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
));
764 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
765 checkGLcall("Unmap PBO");
767 context_release(context
);
770 volume
->resource
.map_count
--;
775 static ULONG
volume_resource_incref(struct wined3d_resource
*resource
)
777 return wined3d_volume_incref(volume_from_resource(resource
));
780 static ULONG
volume_resource_decref(struct wined3d_resource
*resource
)
782 return wined3d_volume_decref(volume_from_resource(resource
));
785 static const struct wined3d_resource_ops volume_resource_ops
=
787 volume_resource_incref
,
788 volume_resource_decref
,
792 static HRESULT
volume_init(struct wined3d_volume
*volume
, struct wined3d_texture
*container
,
793 const struct wined3d_resource_desc
*desc
, UINT level
)
795 struct wined3d_device
*device
= container
->resource
.device
;
796 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
797 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, desc
->format
);
801 if (!gl_info
->supported
[EXT_TEXTURE3D
])
803 WARN("Volume cannot be created - no volume texture support.\n");
804 return WINED3DERR_INVALIDCALL
;
806 /* TODO: Write tests for other resources and move this check
807 * to resource_init, if applicable. */
808 if (desc
->usage
& WINED3DUSAGE_DYNAMIC
809 && (desc
->pool
== WINED3D_POOL_MANAGED
|| desc
->pool
== WINED3D_POOL_SCRATCH
))
811 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc
->pool
));
812 return WINED3DERR_INVALIDCALL
;
815 size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, desc
->width
, desc
->height
, desc
->depth
);
817 if (FAILED(hr
= resource_init(&volume
->resource
, device
, WINED3D_RTYPE_VOLUME
, format
,
818 WINED3D_MULTISAMPLE_NONE
, 0, desc
->usage
, desc
->pool
, desc
->width
, desc
->height
, desc
->depth
,
819 size
, NULL
, &wined3d_null_parent_ops
, &volume_resource_ops
)))
821 WARN("Failed to initialize resource, returning %#x.\n", hr
);
825 volume
->texture_level
= level
;
826 volume
->locations
= WINED3D_LOCATION_DISCARDED
;
828 if (desc
->pool
== WINED3D_POOL_DEFAULT
&& desc
->usage
& WINED3DUSAGE_DYNAMIC
829 && gl_info
->supported
[ARB_PIXEL_BUFFER_OBJECT
]
832 wined3d_resource_free_sysmem(&volume
->resource
);
833 volume
->resource
.map_binding
= WINED3D_LOCATION_BUFFER
;
836 volume
->container
= container
;
841 HRESULT
wined3d_volume_create(struct wined3d_texture
*container
, const struct wined3d_resource_desc
*desc
,
842 unsigned int level
, struct wined3d_volume
**volume
)
844 struct wined3d_device_parent
*device_parent
= container
->resource
.device
->device_parent
;
845 const struct wined3d_parent_ops
*parent_ops
;
846 struct wined3d_volume
*object
;
850 TRACE("container %p, width %u, height %u, depth %u, level %u, format %s, "
851 "usage %#x, pool %s, volume %p.\n",
852 container
, desc
->width
, desc
->height
, desc
->depth
, level
, debug_d3dformat(desc
->format
),
853 desc
->usage
, debug_d3dpool(desc
->pool
), volume
);
855 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
856 return E_OUTOFMEMORY
;
858 if (FAILED(hr
= volume_init(object
, container
, desc
, level
)))
860 WARN("Failed to initialize volume, returning %#x.\n", hr
);
861 HeapFree(GetProcessHeap(), 0, object
);
865 if (FAILED(hr
= device_parent
->ops
->volume_created(device_parent
,
866 wined3d_texture_get_parent(container
), object
, &parent
, &parent_ops
)))
868 WARN("Failed to create volume parent, hr %#x.\n", hr
);
869 wined3d_volume_destroy(object
);
873 TRACE("Created volume %p, parent %p, parent_ops %p.\n", object
, parent
, parent_ops
);
875 object
->resource
.parent
= parent
;
876 object
->resource
.parent_ops
= parent_ops
;