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 static void wined3d_volume_get_pitch(const struct wined3d_volume
*volume
, UINT
*row_pitch
,
46 const struct wined3d_format
*format
= volume
->resource
.format
;
48 if (format
->flags
& WINED3DFMT_FLAG_BLOCKS
)
50 /* Since compressed formats are block based, pitch means the amount of
51 * bytes to the next row of block rather than the next row of pixels. */
52 UINT row_block_count
= (volume
->resource
.width
+ format
->block_width
- 1) / format
->block_width
;
53 UINT slice_block_count
= (volume
->resource
.height
+ format
->block_height
- 1) / format
->block_height
;
54 *row_pitch
= row_block_count
* format
->block_byte_count
;
55 *slice_pitch
= *row_pitch
* slice_block_count
;
59 unsigned char alignment
= volume
->resource
.device
->surface_alignment
;
60 *row_pitch
= format
->byte_count
* volume
->resource
.width
; /* Bytes / row */
61 *row_pitch
= (*row_pitch
+ alignment
- 1) & ~(alignment
- 1);
62 *slice_pitch
= *row_pitch
* volume
->resource
.height
;
65 TRACE("Returning row pitch %u, slice pitch %u.\n", *row_pitch
, *slice_pitch
);
68 /* Context activation is done by the caller. */
69 void wined3d_volume_upload_data(struct wined3d_volume
*volume
, const struct wined3d_context
*context
,
70 const struct wined3d_bo_address
*data
)
72 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
73 const struct wined3d_format
*format
= volume
->resource
.format
;
74 UINT width
= volume
->resource
.width
;
75 UINT height
= volume
->resource
.height
;
76 UINT depth
= volume
->resource
.depth
;
77 BYTE
*mem
= data
->addr
;
79 TRACE("volume %p, context %p, level %u, format %s (%#x).\n",
80 volume
, context
, volume
->texture_level
, debug_d3dformat(format
->id
),
85 UINT dst_row_pitch
, dst_slice_pitch
;
86 UINT src_row_pitch
, src_slice_pitch
;
87 UINT alignment
= volume
->resource
.device
->surface_alignment
;
89 if (data
->buffer_object
)
90 ERR("Loading a converted volume from a PBO.\n");
91 if (format
->flags
& WINED3DFMT_FLAG_BLOCKS
)
92 ERR("Converting a block-based format.\n");
94 dst_row_pitch
= width
* format
->conv_byte_count
;
95 dst_row_pitch
= (dst_row_pitch
+ alignment
- 1) & ~(alignment
- 1);
96 dst_slice_pitch
= dst_row_pitch
* height
;
98 wined3d_volume_get_pitch(volume
, &src_row_pitch
, &src_slice_pitch
);
100 mem
= HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch
* depth
);
101 format
->convert(data
->addr
, mem
, src_row_pitch
, src_slice_pitch
,
102 dst_row_pitch
, dst_slice_pitch
, width
, height
, depth
);
105 if (data
->buffer_object
)
107 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, data
->buffer_object
));
108 checkGLcall("glBindBufferARB");
111 GL_EXTCALL(glTexSubImage3DEXT(GL_TEXTURE_3D
, volume
->texture_level
, 0, 0, 0,
112 width
, height
, depth
,
113 format
->glFormat
, format
->glType
, mem
));
114 checkGLcall("glTexSubImage3D");
116 if (data
->buffer_object
)
118 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
119 checkGLcall("glBindBufferARB");
122 if (mem
!= data
->addr
)
123 HeapFree(GetProcessHeap(), 0, mem
);
126 static void wined3d_volume_validate_location(struct wined3d_volume
*volume
, DWORD location
)
128 TRACE("Volume %p, setting %s.\n", volume
, wined3d_debug_location(location
));
129 volume
->locations
|= location
;
130 TRACE("new location flags are %s.\n", wined3d_debug_location(volume
->locations
));
133 void wined3d_volume_invalidate_location(struct wined3d_volume
*volume
, DWORD location
)
135 TRACE("Volume %p, clearing %s.\n", volume
, wined3d_debug_location(location
));
136 volume
->locations
&= ~location
;
137 TRACE("new location flags are %s.\n", wined3d_debug_location(volume
->locations
));
140 /* Context activation is done by the caller. */
141 static void wined3d_volume_download_data(struct wined3d_volume
*volume
,
142 const struct wined3d_context
*context
, const struct wined3d_bo_address
*data
)
144 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
145 const struct wined3d_format
*format
= volume
->resource
.format
;
149 FIXME("Attempting to download a converted volume, format %s.\n",
150 debug_d3dformat(format
->id
));
154 if (data
->buffer_object
)
156 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, data
->buffer_object
));
157 checkGLcall("glBindBufferARB");
160 gl_info
->gl_ops
.gl
.p_glGetTexImage(GL_TEXTURE_3D
, volume
->texture_level
,
161 format
->glFormat
, format
->glType
, data
->addr
);
162 checkGLcall("glGetTexImage");
164 if (data
->buffer_object
)
166 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB
, 0));
167 checkGLcall("glBindBufferARB");
172 static void wined3d_volume_evict_sysmem(struct wined3d_volume
*volume
)
174 wined3d_resource_free_sysmem(&volume
->resource
);
175 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_SYSMEM
);
178 static DWORD
volume_access_from_location(DWORD location
)
182 case WINED3D_LOCATION_DISCARDED
:
185 case WINED3D_LOCATION_SYSMEM
:
186 return WINED3D_RESOURCE_ACCESS_CPU
;
188 case WINED3D_LOCATION_BUFFER
:
189 case WINED3D_LOCATION_TEXTURE_RGB
:
190 case WINED3D_LOCATION_TEXTURE_SRGB
:
191 return WINED3D_RESOURCE_ACCESS_GPU
;
194 FIXME("Unhandled location %#x.\n", location
);
199 /* Context activation is done by the caller. */
200 static void wined3d_volume_srgb_transfer(struct wined3d_volume
*volume
,
201 struct wined3d_context
*context
, BOOL dest_is_srgb
)
203 struct wined3d_bo_address data
;
204 /* Optimizations are possible, but the effort should be put into either
205 * implementing EXT_SRGB_DECODE in the driver or finding out why we
206 * picked the wrong copy for the original upload and fixing that.
208 * Also keep in mind that we want to avoid using resource.heap_memory
209 * for DEFAULT pool surfaces. */
211 WARN_(d3d_perf
)("Performing slow rgb/srgb volume transfer.\n");
212 data
.buffer_object
= 0;
213 data
.addr
= HeapAlloc(GetProcessHeap(), 0, volume
->resource
.size
);
217 wined3d_texture_bind_and_dirtify(volume
->container
, context
, !dest_is_srgb
);
218 wined3d_volume_download_data(volume
, context
, &data
);
219 wined3d_texture_bind_and_dirtify(volume
->container
, context
, dest_is_srgb
);
220 wined3d_volume_upload_data(volume
, context
, &data
);
222 HeapFree(GetProcessHeap(), 0, data
.addr
);
225 static BOOL
wined3d_volume_can_evict(const struct wined3d_volume
*volume
)
227 if (volume
->resource
.pool
!= WINED3D_POOL_MANAGED
)
229 if (volume
->download_count
>= 10)
231 if (volume
->resource
.format
->convert
)
233 if (volume
->flags
& WINED3D_VFLAG_CLIENT_STORAGE
)
238 /* Context activation is done by the caller. */
239 static void wined3d_volume_load_location(struct wined3d_volume
*volume
,
240 struct wined3d_context
*context
, DWORD location
)
242 DWORD required_access
= volume_access_from_location(location
);
244 TRACE("Volume %p, loading %s, have %s.\n", volume
, wined3d_debug_location(location
),
245 wined3d_debug_location(volume
->locations
));
247 if ((volume
->locations
& location
) == location
)
249 TRACE("Location(s) already up to date.\n");
253 if ((volume
->resource
.access_flags
& required_access
) != required_access
)
255 ERR("Operation requires %#x access, but volume only has %#x.\n",
256 required_access
, volume
->resource
.access_flags
);
262 case WINED3D_LOCATION_TEXTURE_RGB
:
263 case WINED3D_LOCATION_TEXTURE_SRGB
:
264 if ((location
== WINED3D_LOCATION_TEXTURE_RGB
265 && !(volume
->container
->flags
& WINED3D_TEXTURE_RGB_ALLOCATED
))
266 || (location
== WINED3D_LOCATION_TEXTURE_SRGB
267 && !(volume
->container
->flags
& WINED3D_TEXTURE_SRGB_ALLOCATED
)))
268 ERR("Trying to load (s)RGB texture without prior allocation.\n");
270 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
272 TRACE("Volume previously discarded, nothing to do.\n");
273 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
275 else if (volume
->locations
& WINED3D_LOCATION_SYSMEM
)
277 struct wined3d_bo_address data
= {0, volume
->resource
.heap_memory
};
278 wined3d_volume_upload_data(volume
, context
, &data
);
280 else if (volume
->locations
& WINED3D_LOCATION_BUFFER
)
282 struct wined3d_bo_address data
= {volume
->pbo
, NULL
};
283 wined3d_volume_upload_data(volume
, context
, &data
);
285 else if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
287 wined3d_volume_srgb_transfer(volume
, context
, TRUE
);
289 else if (volume
->locations
& WINED3D_LOCATION_TEXTURE_SRGB
)
291 wined3d_volume_srgb_transfer(volume
, context
, FALSE
);
295 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume
->locations
));
298 wined3d_volume_validate_location(volume
, location
);
300 if (wined3d_volume_can_evict(volume
))
301 wined3d_volume_evict_sysmem(volume
);
305 case WINED3D_LOCATION_SYSMEM
:
306 if (!volume
->resource
.heap_memory
)
307 ERR("Trying to load WINED3D_LOCATION_SYSMEM without setting it up first.\n");
309 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
311 TRACE("Volume previously discarded, nothing to do.\n");
312 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
314 else if (volume
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
316 struct wined3d_bo_address data
= {0, volume
->resource
.heap_memory
};
318 if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
319 wined3d_texture_bind_and_dirtify(volume
->container
, context
, FALSE
);
321 wined3d_texture_bind_and_dirtify(volume
->container
, context
, TRUE
);
323 volume
->download_count
++;
324 wined3d_volume_download_data(volume
, context
, &data
);
328 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
329 wined3d_debug_location(volume
->locations
));
332 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_SYSMEM
);
335 case WINED3D_LOCATION_BUFFER
:
337 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
339 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
341 TRACE("Volume previously discarded, nothing to do.\n");
342 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
344 else if (volume
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
346 struct wined3d_bo_address data
= {volume
->pbo
, NULL
};
348 if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
349 wined3d_texture_bind_and_dirtify(volume
->container
, context
, FALSE
);
351 wined3d_texture_bind_and_dirtify(volume
->container
, context
, TRUE
);
353 wined3d_volume_download_data(volume
, context
, &data
);
357 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
358 wined3d_debug_location(volume
->locations
));
361 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_BUFFER
);
365 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location
),
366 wined3d_debug_location(volume
->locations
));
370 /* Context activation is done by the caller. */
371 void wined3d_volume_load(struct wined3d_volume
*volume
, struct wined3d_context
*context
, BOOL srgb_mode
)
373 wined3d_texture_prepare_texture(volume
->container
, context
, srgb_mode
);
374 wined3d_volume_load_location(volume
, context
,
375 srgb_mode
? WINED3D_LOCATION_TEXTURE_SRGB
: WINED3D_LOCATION_TEXTURE_RGB
);
378 /* Context activation is done by the caller. */
379 static void wined3d_volume_prepare_pbo(struct wined3d_volume
*volume
, struct wined3d_context
*context
)
381 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
386 GL_EXTCALL(glGenBuffersARB(1, &volume
->pbo
));
387 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, volume
->pbo
));
388 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB
, volume
->resource
.size
, NULL
, GL_STREAM_DRAW_ARB
));
389 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
390 checkGLcall("Create PBO");
392 TRACE("Created PBO %u for volume %p.\n", volume
->pbo
, volume
);
395 static void wined3d_volume_free_pbo(struct wined3d_volume
*volume
)
397 struct wined3d_context
*context
= context_acquire(volume
->resource
.device
, NULL
);
398 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
400 TRACE("Deleting PBO %u belonging to volume %p.\n", volume
->pbo
, volume
);
401 GL_EXTCALL(glDeleteBuffersARB(1, &volume
->pbo
));
402 checkGLcall("glDeleteBuffersARB");
404 context_release(context
);
407 void wined3d_volume_destroy(struct wined3d_volume
*volume
)
409 TRACE("volume %p.\n", volume
);
412 wined3d_volume_free_pbo(volume
);
414 resource_cleanup(&volume
->resource
);
415 volume
->resource
.parent_ops
->wined3d_object_destroyed(volume
->resource
.parent
);
416 HeapFree(GetProcessHeap(), 0, volume
);
419 static void volume_unload(struct wined3d_resource
*resource
)
421 struct wined3d_volume
*volume
= volume_from_resource(resource
);
422 struct wined3d_device
*device
= volume
->resource
.device
;
423 struct wined3d_context
*context
;
425 if (volume
->resource
.pool
== WINED3D_POOL_DEFAULT
)
426 ERR("Unloading DEFAULT pool volume.\n");
428 TRACE("texture %p.\n", resource
);
430 if (volume_prepare_system_memory(volume
))
432 context
= context_acquire(device
, NULL
);
433 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_SYSMEM
);
434 context_release(context
);
435 wined3d_volume_invalidate_location(volume
, ~WINED3D_LOCATION_SYSMEM
);
439 ERR("Out of memory when unloading volume %p.\n", volume
);
440 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_DISCARDED
);
441 wined3d_volume_invalidate_location(volume
, ~WINED3D_LOCATION_DISCARDED
);
446 /* Should not happen because only dynamic default pool volumes
447 * have a buffer, and those are not evicted by device_evit_managed_resources
448 * and must be freed before a non-ex device reset. */
449 ERR("Unloading a volume with a buffer\n");
450 wined3d_volume_free_pbo(volume
);
453 /* The texture name is managed by the container. */
454 volume
->flags
&= ~WINED3D_VFLAG_CLIENT_STORAGE
;
456 resource_unload(resource
);
459 ULONG CDECL
wined3d_volume_incref(struct wined3d_volume
*volume
)
461 TRACE("Forwarding to container %p.\n", volume
->container
);
463 return wined3d_texture_incref(volume
->container
);
466 ULONG CDECL
wined3d_volume_decref(struct wined3d_volume
*volume
)
468 TRACE("Forwarding to container %p.\n", volume
->container
);
470 return wined3d_texture_decref(volume
->container
);
473 void * CDECL
wined3d_volume_get_parent(const struct wined3d_volume
*volume
)
475 TRACE("volume %p.\n", volume
);
477 return volume
->resource
.parent
;
480 void CDECL
wined3d_volume_preload(struct wined3d_volume
*volume
)
482 FIXME("volume %p stub!\n", volume
);
485 struct wined3d_resource
* CDECL
wined3d_volume_get_resource(struct wined3d_volume
*volume
)
487 TRACE("volume %p.\n", volume
);
489 return &volume
->resource
;
492 static BOOL
volume_check_block_align(const struct wined3d_volume
*volume
,
493 const struct wined3d_box
*box
)
495 UINT width_mask
, height_mask
;
496 const struct wined3d_format
*format
= volume
->resource
.format
;
501 /* This assumes power of two block sizes, but NPOT block sizes would be
504 * This also assumes that the format's block depth is 1. */
505 width_mask
= format
->block_width
- 1;
506 height_mask
= format
->block_height
- 1;
508 if (box
->left
& width_mask
)
510 if (box
->top
& height_mask
)
512 if (box
->right
& width_mask
&& box
->right
!= volume
->resource
.width
)
514 if (box
->bottom
& height_mask
&& box
->bottom
!= volume
->resource
.height
)
520 static BOOL
wined3d_volume_check_box_dimensions(const struct wined3d_volume
*volume
,
521 const struct wined3d_box
*box
)
526 if (box
->left
>= box
->right
)
528 if (box
->top
>= box
->bottom
)
530 if (box
->front
>= box
->back
)
532 if (box
->right
> volume
->resource
.width
)
534 if (box
->bottom
> volume
->resource
.height
)
536 if (box
->back
> volume
->resource
.depth
)
542 HRESULT CDECL
wined3d_volume_map(struct wined3d_volume
*volume
,
543 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
545 struct wined3d_device
*device
= volume
->resource
.device
;
546 struct wined3d_context
*context
;
547 const struct wined3d_gl_info
*gl_info
;
549 const struct wined3d_format
*format
= volume
->resource
.format
;
551 TRACE("volume %p, map_desc %p, box %p, flags %#x.\n",
552 volume
, map_desc
, box
, flags
);
554 map_desc
->data
= NULL
;
555 if (!(volume
->resource
.access_flags
& WINED3D_RESOURCE_ACCESS_CPU
))
557 WARN("Volume %p is not CPU accessible.\n", volume
);
558 return WINED3DERR_INVALIDCALL
;
560 if (volume
->resource
.map_count
)
562 WARN("Volume is already mapped.\n");
563 return WINED3DERR_INVALIDCALL
;
565 if (!wined3d_volume_check_box_dimensions(volume
, box
))
567 WARN("Map box is invalid.\n");
568 return WINED3DERR_INVALIDCALL
;
570 if ((format
->flags
& WINED3DFMT_FLAG_BLOCKS
) && !volume_check_block_align(volume
, box
))
572 WARN("Map box is misaligned for %ux%u blocks.\n",
573 format
->block_width
, format
->block_height
);
574 return WINED3DERR_INVALIDCALL
;
577 flags
= wined3d_resource_sanitize_map_flags(&volume
->resource
, flags
);
579 if (volume
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
)
581 context
= context_acquire(device
, NULL
);
582 gl_info
= context
->gl_info
;
584 wined3d_volume_prepare_pbo(volume
, context
);
585 if (flags
& WINED3D_MAP_DISCARD
)
586 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_BUFFER
);
588 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_BUFFER
);
590 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, volume
->pbo
));
592 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
594 GLbitfield mapflags
= wined3d_resource_gl_map_flags(flags
);
595 mapflags
&= ~GL_MAP_FLUSH_EXPLICIT_BIT
;
596 base_memory
= GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER_ARB
,
597 0, volume
->resource
.size
, mapflags
));
601 GLenum access
= wined3d_resource_gl_legacy_map_flags(flags
);
602 base_memory
= GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, access
));
605 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
606 checkGLcall("Map PBO");
608 context_release(context
);
612 if (!volume_prepare_system_memory(volume
))
614 WARN("Out of memory.\n");
615 map_desc
->data
= NULL
;
616 return E_OUTOFMEMORY
;
619 if (flags
& WINED3D_MAP_DISCARD
)
621 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_SYSMEM
);
623 else if (!(volume
->locations
& WINED3D_LOCATION_SYSMEM
))
625 context
= context_acquire(device
, NULL
);
626 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_SYSMEM
);
627 context_release(context
);
629 base_memory
= volume
->resource
.heap_memory
;
632 TRACE("Base memory pointer %p.\n", base_memory
);
634 if (format
->flags
& WINED3DFMT_FLAG_BROKEN_PITCH
)
636 map_desc
->row_pitch
= volume
->resource
.width
* format
->byte_count
;
637 map_desc
->slice_pitch
= map_desc
->row_pitch
* volume
->resource
.height
;
641 wined3d_volume_get_pitch(volume
, &map_desc
->row_pitch
, &map_desc
->slice_pitch
);
646 TRACE("No box supplied - all is ok\n");
647 map_desc
->data
= base_memory
;
651 TRACE("Lock Box (%p) = l %u, t %u, r %u, b %u, fr %u, ba %u\n",
652 box
, box
->left
, box
->top
, box
->right
, box
->bottom
, box
->front
, box
->back
);
654 if ((format
->flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BROKEN_PITCH
)) == WINED3DFMT_FLAG_BLOCKS
)
656 /* Compressed textures are block based, so calculate the offset of
657 * the block that contains the top-left pixel of the locked rectangle. */
658 map_desc
->data
= base_memory
659 + (box
->front
* map_desc
->slice_pitch
)
660 + ((box
->top
/ format
->block_height
) * map_desc
->row_pitch
)
661 + ((box
->left
/ format
->block_width
) * format
->block_byte_count
);
665 map_desc
->data
= base_memory
666 + (map_desc
->slice_pitch
* box
->front
)
667 + (map_desc
->row_pitch
* box
->top
)
668 + (box
->left
* volume
->resource
.format
->byte_count
);
672 if (!(flags
& (WINED3D_MAP_NO_DIRTY_UPDATE
| WINED3D_MAP_READONLY
)))
674 wined3d_texture_set_dirty(volume
->container
);
675 wined3d_volume_invalidate_location(volume
, ~volume
->resource
.map_binding
);
678 volume
->resource
.map_count
++;
680 TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
681 map_desc
->data
, map_desc
->row_pitch
, map_desc
->slice_pitch
);
686 struct wined3d_volume
* CDECL
wined3d_volume_from_resource(struct wined3d_resource
*resource
)
688 return volume_from_resource(resource
);
691 HRESULT CDECL
wined3d_volume_unmap(struct wined3d_volume
*volume
)
693 TRACE("volume %p.\n", volume
);
695 if (!volume
->resource
.map_count
)
697 WARN("Trying to unlock an unlocked volume %p.\n", volume
);
698 return WINED3DERR_INVALIDCALL
;
701 if (volume
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
)
703 struct wined3d_device
*device
= volume
->resource
.device
;
704 struct wined3d_context
*context
= context_acquire(device
, NULL
);
705 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
707 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, volume
->pbo
));
708 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
));
709 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB
, 0));
710 checkGLcall("Unmap PBO");
712 context_release(context
);
715 volume
->resource
.map_count
--;
720 static ULONG
volume_resource_incref(struct wined3d_resource
*resource
)
722 return wined3d_volume_incref(volume_from_resource(resource
));
725 static ULONG
volume_resource_decref(struct wined3d_resource
*resource
)
727 return wined3d_volume_decref(volume_from_resource(resource
));
730 static const struct wined3d_resource_ops volume_resource_ops
=
732 volume_resource_incref
,
733 volume_resource_decref
,
737 static HRESULT
volume_init(struct wined3d_volume
*volume
, struct wined3d_texture
*container
,
738 const struct wined3d_resource_desc
*desc
, UINT level
)
740 struct wined3d_device
*device
= container
->resource
.device
;
741 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
742 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, desc
->format
);
746 if (!gl_info
->supported
[EXT_TEXTURE3D
])
748 WARN("Volume cannot be created - no volume texture support.\n");
749 return WINED3DERR_INVALIDCALL
;
751 /* TODO: Write tests for other resources and move this check
752 * to resource_init, if applicable. */
753 if (desc
->usage
& WINED3DUSAGE_DYNAMIC
754 && (desc
->pool
== WINED3D_POOL_MANAGED
|| desc
->pool
== WINED3D_POOL_SCRATCH
))
756 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc
->pool
));
757 return WINED3DERR_INVALIDCALL
;
760 size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, desc
->width
, desc
->height
, desc
->depth
);
762 if (FAILED(hr
= resource_init(&volume
->resource
, device
, WINED3D_RTYPE_VOLUME
, format
,
763 WINED3D_MULTISAMPLE_NONE
, 0, desc
->usage
, desc
->pool
, desc
->width
, desc
->height
, desc
->depth
,
764 size
, NULL
, &wined3d_null_parent_ops
, &volume_resource_ops
)))
766 WARN("Failed to initialize resource, returning %#x.\n", hr
);
770 volume
->texture_level
= level
;
771 volume
->locations
= WINED3D_LOCATION_DISCARDED
;
773 if (desc
->pool
== WINED3D_POOL_DEFAULT
&& desc
->usage
& WINED3DUSAGE_DYNAMIC
774 && gl_info
->supported
[ARB_PIXEL_BUFFER_OBJECT
]
777 wined3d_resource_free_sysmem(&volume
->resource
);
778 volume
->resource
.map_binding
= WINED3D_LOCATION_BUFFER
;
781 volume
->container
= container
;
786 HRESULT
wined3d_volume_create(struct wined3d_texture
*container
, const struct wined3d_resource_desc
*desc
,
787 unsigned int level
, struct wined3d_volume
**volume
)
789 struct wined3d_device_parent
*device_parent
= container
->resource
.device
->device_parent
;
790 const struct wined3d_parent_ops
*parent_ops
;
791 struct wined3d_volume
*object
;
795 TRACE("container %p, width %u, height %u, depth %u, level %u, format %s, "
796 "usage %#x, pool %s, volume %p.\n",
797 container
, desc
->width
, desc
->height
, desc
->depth
, level
, debug_d3dformat(desc
->format
),
798 desc
->usage
, debug_d3dpool(desc
->pool
), volume
);
800 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
801 return E_OUTOFMEMORY
;
803 if (FAILED(hr
= volume_init(object
, container
, desc
, level
)))
805 WARN("Failed to initialize volume, returning %#x.\n", hr
);
806 HeapFree(GetProcessHeap(), 0, object
);
810 if (FAILED(hr
= device_parent
->ops
->volume_created(device_parent
,
811 wined3d_texture_get_parent(container
), object
, &parent
, &parent_ops
)))
813 WARN("Failed to create volume parent, hr %#x.\n", hr
);
814 wined3d_volume_destroy(object
);
818 TRACE("Created volume %p, parent %p, parent_ops %p.\n", object
, parent
, parent_ops
);
820 object
->resource
.parent
= parent
;
821 object
->resource
.parent_ops
= parent_ops
;