2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
6 * Copyright 2013 Stefan Dösinger for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface
);
28 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
30 BOOL
volume_prepare_system_memory(struct wined3d_volume
*volume
)
32 if (volume
->resource
.heap_memory
)
35 if (!wined3d_resource_allocate_sysmem(&volume
->resource
))
37 ERR("Failed to allocate system memory.\n");
43 void wined3d_volume_get_pitch(const struct wined3d_volume
*volume
, UINT
*row_pitch
, UINT
*slice_pitch
)
45 const struct wined3d_format
*format
= volume
->resource
.format
;
47 if (volume
->resource
.format_flags
& WINED3DFMT_FLAG_BLOCKS
)
49 /* Since compressed formats are block based, pitch means the amount of
50 * bytes to the next row of block rather than the next row of pixels. */
51 UINT row_block_count
= (volume
->resource
.width
+ format
->block_width
- 1) / format
->block_width
;
52 UINT slice_block_count
= (volume
->resource
.height
+ format
->block_height
- 1) / format
->block_height
;
53 *row_pitch
= row_block_count
* format
->block_byte_count
;
54 *slice_pitch
= *row_pitch
* slice_block_count
;
58 unsigned char alignment
= volume
->resource
.device
->surface_alignment
;
59 *row_pitch
= format
->byte_count
* volume
->resource
.width
; /* Bytes / row */
60 *row_pitch
= (*row_pitch
+ alignment
- 1) & ~(alignment
- 1);
61 *slice_pitch
= *row_pitch
* volume
->resource
.height
;
64 TRACE("Returning row pitch %u, slice pitch %u.\n", *row_pitch
, *slice_pitch
);
67 /* This call just uploads data, the caller is responsible for binding the
69 /* Context activation is done by the caller. */
70 void wined3d_volume_upload_data(struct wined3d_volume
*volume
, const struct wined3d_context
*context
,
71 const struct wined3d_const_bo_address
*data
)
73 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
74 const struct wined3d_format
*format
= volume
->resource
.format
;
75 UINT width
= volume
->resource
.width
;
76 UINT height
= volume
->resource
.height
;
77 UINT depth
= volume
->resource
.depth
;
78 const void *mem
= data
->addr
;
79 void *converted_mem
= NULL
;
81 TRACE("volume %p, context %p, level %u, format %s (%#x).\n",
82 volume
, context
, volume
->texture_level
, debug_d3dformat(format
->id
),
87 UINT dst_row_pitch
, dst_slice_pitch
;
88 UINT src_row_pitch
, src_slice_pitch
;
90 if (data
->buffer_object
)
91 ERR("Loading a converted volume from a PBO.\n");
92 if (volume
->resource
.format_flags
& WINED3DFMT_FLAG_BLOCKS
)
93 ERR("Converting a block-based format.\n");
95 dst_row_pitch
= width
* format
->conv_byte_count
;
96 dst_slice_pitch
= dst_row_pitch
* height
;
98 wined3d_volume_get_pitch(volume
, &src_row_pitch
, &src_slice_pitch
);
100 converted_mem
= HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch
* depth
);
101 format
->convert(data
->addr
, converted_mem
, src_row_pitch
, src_slice_pitch
,
102 dst_row_pitch
, dst_slice_pitch
, width
, height
, depth
);
106 if (data
->buffer_object
)
108 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, data
->buffer_object
));
109 checkGLcall("glBindBuffer");
112 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D
, volume
->texture_level
, 0, 0, 0,
113 width
, height
, depth
,
114 format
->glFormat
, format
->glType
, mem
));
115 checkGLcall("glTexSubImage3D");
117 if (data
->buffer_object
)
119 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
120 checkGLcall("glBindBuffer");
123 HeapFree(GetProcessHeap(), 0, converted_mem
);
126 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(glBindBuffer(GL_PIXEL_PACK_BUFFER
, data
->buffer_object
));
157 checkGLcall("glBindBuffer");
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(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
167 checkGLcall("glBindBuffer");
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
, wined3d_const_bo_address(&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_const_bo_address data
= {0, volume
->resource
.heap_memory
};
278 wined3d_texture_bind_and_dirtify(volume
->container
, context
,
279 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
280 wined3d_volume_upload_data(volume
, context
, &data
);
282 else if (volume
->locations
& WINED3D_LOCATION_BUFFER
)
284 struct wined3d_const_bo_address data
= {volume
->pbo
, NULL
};
285 wined3d_texture_bind_and_dirtify(volume
->container
, context
,
286 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
287 wined3d_volume_upload_data(volume
, context
, &data
);
289 else if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
291 wined3d_volume_srgb_transfer(volume
, context
, TRUE
);
293 else if (volume
->locations
& WINED3D_LOCATION_TEXTURE_SRGB
)
295 wined3d_volume_srgb_transfer(volume
, context
, FALSE
);
299 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume
->locations
));
302 wined3d_volume_validate_location(volume
, location
);
304 if (wined3d_volume_can_evict(volume
))
305 wined3d_volume_evict_sysmem(volume
);
309 case WINED3D_LOCATION_SYSMEM
:
310 if (!volume
->resource
.heap_memory
)
311 ERR("Trying to load WINED3D_LOCATION_SYSMEM without setting it up first.\n");
313 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
315 TRACE("Volume previously discarded, nothing to do.\n");
316 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
318 else if (volume
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
320 struct wined3d_bo_address data
= {0, volume
->resource
.heap_memory
};
322 if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
323 wined3d_texture_bind_and_dirtify(volume
->container
, context
, FALSE
);
325 wined3d_texture_bind_and_dirtify(volume
->container
, context
, TRUE
);
327 volume
->download_count
++;
328 wined3d_volume_download_data(volume
, context
, &data
);
332 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
333 wined3d_debug_location(volume
->locations
));
336 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_SYSMEM
);
339 case WINED3D_LOCATION_BUFFER
:
341 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
343 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
345 TRACE("Volume previously discarded, nothing to do.\n");
346 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
348 else if (volume
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
350 struct wined3d_bo_address data
= {volume
->pbo
, NULL
};
352 if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
353 wined3d_texture_bind_and_dirtify(volume
->container
, context
, FALSE
);
355 wined3d_texture_bind_and_dirtify(volume
->container
, context
, TRUE
);
357 wined3d_volume_download_data(volume
, context
, &data
);
361 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
362 wined3d_debug_location(volume
->locations
));
365 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_BUFFER
);
369 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location
),
370 wined3d_debug_location(volume
->locations
));
374 /* Context activation is done by the caller. */
375 void wined3d_volume_load(struct wined3d_volume
*volume
, struct wined3d_context
*context
, BOOL srgb_mode
)
377 wined3d_texture_prepare_texture(volume
->container
, context
, srgb_mode
);
378 wined3d_volume_load_location(volume
, context
,
379 srgb_mode
? WINED3D_LOCATION_TEXTURE_SRGB
: WINED3D_LOCATION_TEXTURE_RGB
);
382 /* Context activation is done by the caller. */
383 static void wined3d_volume_prepare_pbo(struct wined3d_volume
*volume
, struct wined3d_context
*context
)
385 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
390 GL_EXTCALL(glGenBuffers(1, &volume
->pbo
));
391 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, volume
->pbo
));
392 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER
, volume
->resource
.size
, NULL
, GL_STREAM_DRAW
));
393 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
394 checkGLcall("Create PBO");
396 TRACE("Created PBO %u for volume %p.\n", volume
->pbo
, volume
);
399 static void wined3d_volume_free_pbo(struct wined3d_volume
*volume
)
401 struct wined3d_context
*context
= context_acquire(volume
->resource
.device
, NULL
);
402 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
404 TRACE("Deleting PBO %u belonging to volume %p.\n", volume
->pbo
, volume
);
405 GL_EXTCALL(glDeleteBuffers(1, &volume
->pbo
));
406 checkGLcall("glDeleteBuffers");
408 context_release(context
);
411 void wined3d_volume_destroy(struct wined3d_volume
*volume
)
413 TRACE("volume %p.\n", volume
);
416 wined3d_volume_free_pbo(volume
);
418 resource_cleanup(&volume
->resource
);
419 volume
->resource
.parent_ops
->wined3d_object_destroyed(volume
->resource
.parent
);
420 HeapFree(GetProcessHeap(), 0, volume
);
423 static void volume_unload(struct wined3d_resource
*resource
)
425 struct wined3d_volume
*volume
= volume_from_resource(resource
);
426 struct wined3d_device
*device
= volume
->resource
.device
;
427 struct wined3d_context
*context
;
429 if (volume
->resource
.pool
== WINED3D_POOL_DEFAULT
)
430 ERR("Unloading DEFAULT pool volume.\n");
432 TRACE("texture %p.\n", resource
);
434 if (volume_prepare_system_memory(volume
))
436 context
= context_acquire(device
, NULL
);
437 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_SYSMEM
);
438 context_release(context
);
439 wined3d_volume_invalidate_location(volume
, ~WINED3D_LOCATION_SYSMEM
);
443 ERR("Out of memory when unloading volume %p.\n", volume
);
444 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_DISCARDED
);
445 wined3d_volume_invalidate_location(volume
, ~WINED3D_LOCATION_DISCARDED
);
450 /* Should not happen because only dynamic default pool volumes
451 * have a buffer, and those are not evicted by device_evit_managed_resources
452 * and must be freed before a non-ex device reset. */
453 ERR("Unloading a volume with a buffer\n");
454 wined3d_volume_free_pbo(volume
);
457 /* The texture name is managed by the container. */
458 volume
->flags
&= ~WINED3D_VFLAG_CLIENT_STORAGE
;
460 resource_unload(resource
);
463 ULONG CDECL
wined3d_volume_incref(struct wined3d_volume
*volume
)
465 TRACE("Forwarding to container %p.\n", volume
->container
);
467 return wined3d_texture_incref(volume
->container
);
470 ULONG CDECL
wined3d_volume_decref(struct wined3d_volume
*volume
)
472 TRACE("Forwarding to container %p.\n", volume
->container
);
474 return wined3d_texture_decref(volume
->container
);
477 void * CDECL
wined3d_volume_get_parent(const struct wined3d_volume
*volume
)
479 TRACE("volume %p.\n", volume
);
481 return volume
->resource
.parent
;
484 void CDECL
wined3d_volume_preload(struct wined3d_volume
*volume
)
486 FIXME("volume %p stub!\n", volume
);
489 struct wined3d_resource
* CDECL
wined3d_volume_get_resource(struct wined3d_volume
*volume
)
491 TRACE("volume %p.\n", volume
);
493 return &volume
->resource
;
496 static BOOL
volume_check_block_align(const struct wined3d_volume
*volume
,
497 const struct wined3d_box
*box
)
499 UINT width_mask
, height_mask
;
500 const struct wined3d_format
*format
= volume
->resource
.format
;
505 /* This assumes power of two block sizes, but NPOT block sizes would be
508 * This also assumes that the format's block depth is 1. */
509 width_mask
= format
->block_width
- 1;
510 height_mask
= format
->block_height
- 1;
512 if (box
->left
& width_mask
)
514 if (box
->top
& height_mask
)
516 if (box
->right
& width_mask
&& box
->right
!= volume
->resource
.width
)
518 if (box
->bottom
& height_mask
&& box
->bottom
!= volume
->resource
.height
)
524 static BOOL
wined3d_volume_check_box_dimensions(const struct wined3d_volume
*volume
,
525 const struct wined3d_box
*box
)
530 if (box
->left
>= box
->right
)
532 if (box
->top
>= box
->bottom
)
534 if (box
->front
>= box
->back
)
536 if (box
->right
> volume
->resource
.width
)
538 if (box
->bottom
> volume
->resource
.height
)
540 if (box
->back
> volume
->resource
.depth
)
546 HRESULT CDECL
wined3d_volume_map(struct wined3d_volume
*volume
,
547 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
549 struct wined3d_device
*device
= volume
->resource
.device
;
550 struct wined3d_context
*context
;
551 const struct wined3d_gl_info
*gl_info
;
553 const struct wined3d_format
*format
= volume
->resource
.format
;
554 const unsigned int fmt_flags
= volume
->resource
.format_flags
;
556 TRACE("volume %p, map_desc %p, box %p, flags %#x.\n",
557 volume
, map_desc
, box
, flags
);
559 map_desc
->data
= NULL
;
560 if (!(volume
->resource
.access_flags
& WINED3D_RESOURCE_ACCESS_CPU
))
562 WARN("Volume %p is not CPU accessible.\n", volume
);
563 return WINED3DERR_INVALIDCALL
;
565 if (volume
->resource
.map_count
)
567 WARN("Volume is already mapped.\n");
568 return WINED3DERR_INVALIDCALL
;
570 if (!wined3d_volume_check_box_dimensions(volume
, box
))
572 WARN("Map box is invalid.\n");
573 return WINED3DERR_INVALIDCALL
;
575 if ((fmt_flags
& WINED3DFMT_FLAG_BLOCKS
) && !volume_check_block_align(volume
, box
))
577 WARN("Map box is misaligned for %ux%u blocks.\n",
578 format
->block_width
, format
->block_height
);
579 return WINED3DERR_INVALIDCALL
;
582 flags
= wined3d_resource_sanitize_map_flags(&volume
->resource
, flags
);
584 if (volume
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
)
586 context
= context_acquire(device
, NULL
);
587 gl_info
= context
->gl_info
;
589 wined3d_volume_prepare_pbo(volume
, context
);
590 if (flags
& WINED3D_MAP_DISCARD
)
591 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_BUFFER
);
593 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_BUFFER
);
595 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, volume
->pbo
));
597 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
599 GLbitfield mapflags
= wined3d_resource_gl_map_flags(flags
);
600 mapflags
&= ~GL_MAP_FLUSH_EXPLICIT_BIT
;
601 base_memory
= GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER
,
602 0, volume
->resource
.size
, mapflags
));
606 GLenum access
= wined3d_resource_gl_legacy_map_flags(flags
);
607 base_memory
= GL_EXTCALL(glMapBuffer(GL_PIXEL_UNPACK_BUFFER
, access
));
610 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
611 checkGLcall("Map PBO");
613 context_release(context
);
617 if (!volume_prepare_system_memory(volume
))
619 WARN("Out of memory.\n");
620 map_desc
->data
= NULL
;
621 return E_OUTOFMEMORY
;
624 if (flags
& WINED3D_MAP_DISCARD
)
626 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_SYSMEM
);
628 else if (!(volume
->locations
& WINED3D_LOCATION_SYSMEM
))
630 context
= context_acquire(device
, NULL
);
631 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_SYSMEM
);
632 context_release(context
);
634 base_memory
= volume
->resource
.heap_memory
;
637 TRACE("Base memory pointer %p.\n", base_memory
);
639 if (fmt_flags
& WINED3DFMT_FLAG_BROKEN_PITCH
)
641 map_desc
->row_pitch
= volume
->resource
.width
* format
->byte_count
;
642 map_desc
->slice_pitch
= map_desc
->row_pitch
* volume
->resource
.height
;
646 wined3d_volume_get_pitch(volume
, &map_desc
->row_pitch
, &map_desc
->slice_pitch
);
651 TRACE("No box supplied - all is ok\n");
652 map_desc
->data
= base_memory
;
656 TRACE("Lock Box (%p) = l %u, t %u, r %u, b %u, fr %u, ba %u\n",
657 box
, box
->left
, box
->top
, box
->right
, box
->bottom
, box
->front
, box
->back
);
659 if ((fmt_flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BROKEN_PITCH
)) == WINED3DFMT_FLAG_BLOCKS
)
661 /* Compressed textures are block based, so calculate the offset of
662 * the block that contains the top-left pixel of the locked rectangle. */
663 map_desc
->data
= base_memory
664 + (box
->front
* map_desc
->slice_pitch
)
665 + ((box
->top
/ format
->block_height
) * map_desc
->row_pitch
)
666 + ((box
->left
/ format
->block_width
) * format
->block_byte_count
);
670 map_desc
->data
= base_memory
671 + (map_desc
->slice_pitch
* box
->front
)
672 + (map_desc
->row_pitch
* box
->top
)
673 + (box
->left
* volume
->resource
.format
->byte_count
);
677 if (!(flags
& (WINED3D_MAP_NO_DIRTY_UPDATE
| WINED3D_MAP_READONLY
)))
679 wined3d_texture_set_dirty(volume
->container
);
680 wined3d_volume_invalidate_location(volume
, ~volume
->resource
.map_binding
);
683 volume
->resource
.map_count
++;
685 TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
686 map_desc
->data
, map_desc
->row_pitch
, map_desc
->slice_pitch
);
691 struct wined3d_volume
* CDECL
wined3d_volume_from_resource(struct wined3d_resource
*resource
)
693 return volume_from_resource(resource
);
696 HRESULT CDECL
wined3d_volume_unmap(struct wined3d_volume
*volume
)
698 TRACE("volume %p.\n", volume
);
700 if (!volume
->resource
.map_count
)
702 WARN("Trying to unlock an unlocked volume %p.\n", volume
);
703 return WINED3DERR_INVALIDCALL
;
706 if (volume
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
)
708 struct wined3d_device
*device
= volume
->resource
.device
;
709 struct wined3d_context
*context
= context_acquire(device
, NULL
);
710 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
712 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, volume
->pbo
));
713 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER
));
714 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
715 checkGLcall("Unmap PBO");
717 context_release(context
);
720 volume
->resource
.map_count
--;
725 static ULONG
volume_resource_incref(struct wined3d_resource
*resource
)
727 return wined3d_volume_incref(volume_from_resource(resource
));
730 static ULONG
volume_resource_decref(struct wined3d_resource
*resource
)
732 return wined3d_volume_decref(volume_from_resource(resource
));
735 static const struct wined3d_resource_ops volume_resource_ops
=
737 volume_resource_incref
,
738 volume_resource_decref
,
742 static HRESULT
volume_init(struct wined3d_volume
*volume
, struct wined3d_texture
*container
,
743 const struct wined3d_resource_desc
*desc
, UINT level
)
745 struct wined3d_device
*device
= container
->resource
.device
;
746 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
747 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, desc
->format
);
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
, container
->resource
.gl_type
,
763 format
, 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
;