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 BOOL
volume_prepare_system_memory(struct wined3d_volume
*volume
)
30 if (volume
->resource
.heap_memory
)
33 if (!wined3d_resource_allocate_sysmem(&volume
->resource
))
35 ERR("Failed to allocate system memory.\n");
41 /* This call just uploads data, the caller is responsible for binding the
43 /* Context activation is done by the caller. */
44 void wined3d_volume_upload_data(struct wined3d_volume
*volume
, const struct wined3d_context
*context
,
45 const struct wined3d_const_bo_address
*data
)
47 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
48 const struct wined3d_format
*format
= volume
->resource
.format
;
49 UINT width
= volume
->resource
.width
;
50 UINT height
= volume
->resource
.height
;
51 UINT depth
= volume
->resource
.depth
;
52 const void *mem
= data
->addr
;
53 void *converted_mem
= NULL
;
55 TRACE("volume %p, context %p, level %u, format %s (%#x).\n",
56 volume
, context
, volume
->texture_level
, debug_d3dformat(format
->id
),
61 UINT dst_row_pitch
, dst_slice_pitch
;
62 UINT src_row_pitch
, src_slice_pitch
;
64 if (data
->buffer_object
)
65 ERR("Loading a converted volume from a PBO.\n");
66 if (volume
->container
->resource
.format_flags
& WINED3DFMT_FLAG_BLOCKS
)
67 ERR("Converting a block-based format.\n");
69 dst_row_pitch
= width
* format
->conv_byte_count
;
70 dst_slice_pitch
= dst_row_pitch
* height
;
72 #if defined(STAGING_CSMT)
73 wined3d_resource_get_pitch(&volume
->resource
, &src_row_pitch
, &src_slice_pitch
);
74 #else /* STAGING_CSMT */
75 wined3d_texture_get_pitch(volume
->container
, volume
->texture_level
, &src_row_pitch
, &src_slice_pitch
);
76 #endif /* STAGING_CSMT */
78 converted_mem
= HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch
* depth
);
79 format
->convert(data
->addr
, converted_mem
, src_row_pitch
, src_slice_pitch
,
80 dst_row_pitch
, dst_slice_pitch
, width
, height
, depth
);
84 if (data
->buffer_object
)
86 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, data
->buffer_object
));
87 checkGLcall("glBindBuffer");
90 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D
, volume
->texture_level
, 0, 0, 0,
92 format
->glFormat
, format
->glType
, mem
));
93 checkGLcall("glTexSubImage3D");
95 if (data
->buffer_object
)
97 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
98 checkGLcall("glBindBuffer");
101 HeapFree(GetProcessHeap(), 0, converted_mem
);
104 #if !defined(STAGING_CSMT)
105 void wined3d_volume_validate_location(struct wined3d_volume
*volume
, DWORD location
)
107 TRACE("Volume %p, setting %s.\n", volume
, wined3d_debug_location(location
));
108 volume
->locations
|= location
;
109 TRACE("new location flags are %s.\n", wined3d_debug_location(volume
->locations
));
112 void wined3d_volume_invalidate_location(struct wined3d_volume
*volume
, DWORD location
)
114 TRACE("Volume %p, clearing %s.\n", volume
, wined3d_debug_location(location
));
115 volume
->locations
&= ~location
;
116 TRACE("new location flags are %s.\n", wined3d_debug_location(volume
->locations
));
119 #endif /* STAGING_CSMT */
120 /* Context activation is done by the caller. */
121 static void wined3d_volume_download_data(struct wined3d_volume
*volume
,
122 const struct wined3d_context
*context
, const struct wined3d_bo_address
*data
)
124 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
125 const struct wined3d_format
*format
= volume
->resource
.format
;
129 FIXME("Attempting to download a converted volume, format %s.\n",
130 debug_d3dformat(format
->id
));
134 if (data
->buffer_object
)
136 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, data
->buffer_object
));
137 checkGLcall("glBindBuffer");
140 gl_info
->gl_ops
.gl
.p_glGetTexImage(GL_TEXTURE_3D
, volume
->texture_level
,
141 format
->glFormat
, format
->glType
, data
->addr
);
142 checkGLcall("glGetTexImage");
144 if (data
->buffer_object
)
146 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
147 checkGLcall("glBindBuffer");
152 static void wined3d_volume_evict_sysmem(struct wined3d_volume
*volume
)
154 wined3d_resource_free_sysmem(&volume
->resource
);
155 #if defined(STAGING_CSMT)
156 volume
->resource
.map_heap_memory
= NULL
;
157 wined3d_resource_invalidate_location(&volume
->resource
, WINED3D_LOCATION_SYSMEM
);
158 #else /* STAGING_CSMT */
159 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_SYSMEM
);
162 static DWORD
volume_access_from_location(DWORD location
)
166 case WINED3D_LOCATION_DISCARDED
:
169 case WINED3D_LOCATION_SYSMEM
:
170 return WINED3D_RESOURCE_ACCESS_CPU
;
172 case WINED3D_LOCATION_BUFFER
:
173 case WINED3D_LOCATION_TEXTURE_RGB
:
174 case WINED3D_LOCATION_TEXTURE_SRGB
:
175 return WINED3D_RESOURCE_ACCESS_GPU
;
178 FIXME("Unhandled location %#x.\n", location
);
181 #endif /* STAGING_CSMT */
184 /* Context activation is done by the caller. */
185 static void wined3d_volume_srgb_transfer(struct wined3d_volume
*volume
,
186 struct wined3d_context
*context
, BOOL dest_is_srgb
)
188 struct wined3d_bo_address data
;
189 /* Optimizations are possible, but the effort should be put into either
190 * implementing EXT_SRGB_DECODE in the driver or finding out why we
191 * picked the wrong copy for the original upload and fixing that.
193 * Also keep in mind that we want to avoid using resource.heap_memory
194 * for DEFAULT pool surfaces. */
196 WARN_(d3d_perf
)("Performing slow rgb/srgb volume transfer.\n");
197 data
.buffer_object
= 0;
198 data
.addr
= HeapAlloc(GetProcessHeap(), 0, volume
->resource
.size
);
202 wined3d_texture_bind_and_dirtify(volume
->container
, context
, !dest_is_srgb
);
203 wined3d_volume_download_data(volume
, context
, &data
);
204 wined3d_texture_bind_and_dirtify(volume
->container
, context
, dest_is_srgb
);
205 wined3d_volume_upload_data(volume
, context
, wined3d_const_bo_address(&data
));
207 HeapFree(GetProcessHeap(), 0, data
.addr
);
210 static BOOL
wined3d_volume_can_evict(const struct wined3d_volume
*volume
)
212 if (volume
->resource
.pool
!= WINED3D_POOL_MANAGED
)
214 if (volume
->download_count
>= 10)
216 if (volume
->resource
.format
->convert
)
221 #if defined(STAGING_CSMT)
223 /* Context activation is done by the caller. */
224 static void wined3d_volume_load_location(struct wined3d_resource
*resource
,
225 struct wined3d_context
*context
, DWORD location
)
227 struct wined3d_volume
*volume
= volume_from_resource(resource
);
228 DWORD required_access
= wined3d_resource_access_from_location(location
);
230 TRACE("Volume %p, loading %s, have %s.\n", volume
, wined3d_debug_location(location
),
231 wined3d_debug_location(volume
->resource
.locations
));
232 #else /* STAGING_CSMT */
233 /* Context activation is done by the caller. */
234 static void wined3d_volume_load_location(struct wined3d_volume
*volume
,
235 struct wined3d_context
*context
, DWORD location
)
237 DWORD required_access
= volume_access_from_location(location
);
239 TRACE("Volume %p, loading %s, have %s.\n", volume
, wined3d_debug_location(location
),
240 wined3d_debug_location(volume
->locations
));
242 if ((volume
->locations
& location
) == location
)
244 TRACE("Location(s) already up to date.\n");
247 #endif /* STAGING_CSMT */
249 if ((volume
->resource
.access_flags
& required_access
) != required_access
)
251 ERR("Operation requires %#x access, but volume only has %#x.\n",
252 required_access
, volume
->resource
.access_flags
);
258 case WINED3D_LOCATION_TEXTURE_RGB
:
259 case WINED3D_LOCATION_TEXTURE_SRGB
:
260 if ((location
== WINED3D_LOCATION_TEXTURE_RGB
261 && !(volume
->container
->flags
& WINED3D_TEXTURE_RGB_ALLOCATED
))
262 || (location
== WINED3D_LOCATION_TEXTURE_SRGB
263 && !(volume
->container
->flags
& WINED3D_TEXTURE_SRGB_ALLOCATED
)))
264 ERR("Trying to load (s)RGB texture without prior allocation.\n");
266 #if defined(STAGING_CSMT)
267 if (volume
->resource
.locations
& WINED3D_LOCATION_DISCARDED
)
269 TRACE("Volume previously discarded, nothing to do.\n");
270 wined3d_resource_invalidate_location(&volume
->resource
, WINED3D_LOCATION_DISCARDED
);
272 else if (volume
->resource
.locations
& WINED3D_LOCATION_SYSMEM
)
274 struct wined3d_const_bo_address data
= {0, volume
->resource
.heap_memory
};
275 wined3d_texture_bind_and_dirtify(volume
->container
, context
,
276 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
277 wined3d_volume_upload_data(volume
, context
, &data
);
279 else if (volume
->resource
.locations
& WINED3D_LOCATION_BUFFER
)
281 struct wined3d_const_bo_address data
= {volume
->resource
.buffer
->name
, NULL
};
282 wined3d_texture_bind_and_dirtify(volume
->container
, context
,
283 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
284 wined3d_volume_upload_data(volume
, context
, &data
);
286 else if (volume
->resource
.locations
& WINED3D_LOCATION_TEXTURE_RGB
)
288 wined3d_volume_srgb_transfer(volume
, context
, TRUE
);
290 else if (volume
->resource
.locations
& WINED3D_LOCATION_TEXTURE_SRGB
)
292 wined3d_volume_srgb_transfer(volume
, context
, FALSE
);
296 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume
->resource
.locations
));
299 wined3d_resource_validate_location(&volume
->resource
, location
);
300 #else /* STAGING_CSMT */
301 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
303 TRACE("Volume previously discarded, nothing to do.\n");
304 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
306 else if (volume
->locations
& WINED3D_LOCATION_SYSMEM
)
308 struct wined3d_const_bo_address data
= {0, volume
->resource
.heap_memory
};
309 wined3d_texture_bind_and_dirtify(volume
->container
, context
,
310 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
311 wined3d_volume_upload_data(volume
, context
, &data
);
313 else if (volume
->locations
& WINED3D_LOCATION_BUFFER
)
315 struct wined3d_const_bo_address data
= {volume
->pbo
, NULL
};
316 wined3d_texture_bind_and_dirtify(volume
->container
, context
,
317 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
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
);
334 #endif /* STAGING_CSMT */
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 defined(STAGING_CSMT)
346 if (volume
->resource
.locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
348 struct wined3d_bo_address data
= {0, volume
->resource
.heap_memory
};
350 if (volume
->resource
.locations
& WINED3D_LOCATION_TEXTURE_RGB
)
351 #else /* STAGING_CSMT */
352 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
354 TRACE("Volume previously discarded, nothing to do.\n");
355 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
357 else if (volume
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
359 struct wined3d_bo_address data
= {0, volume
->resource
.heap_memory
};
361 if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
362 #endif /* STAGING_CSMT */
363 wined3d_texture_bind_and_dirtify(volume
->container
, context
, FALSE
);
365 wined3d_texture_bind_and_dirtify(volume
->container
, context
, TRUE
);
367 volume
->download_count
++;
368 wined3d_volume_download_data(volume
, context
, &data
);
372 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
373 #if defined(STAGING_CSMT)
374 wined3d_debug_location(volume
->resource
.locations
));
377 wined3d_resource_validate_location(&volume
->resource
, WINED3D_LOCATION_SYSMEM
);
380 case WINED3D_LOCATION_BUFFER
:
381 if (!volume
->resource
.buffer
)
382 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
384 if (volume
->resource
.locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
386 struct wined3d_bo_address data
= {volume
->resource
.buffer
->name
, NULL
};
388 if (volume
->resource
.locations
& WINED3D_LOCATION_TEXTURE_RGB
)
389 #else /* STAGING_CSMT */
390 wined3d_debug_location(volume
->locations
));
393 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_SYSMEM
);
396 case WINED3D_LOCATION_BUFFER
:
398 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
400 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
402 TRACE("Volume previously discarded, nothing to do.\n");
403 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
405 else if (volume
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
407 struct wined3d_bo_address data
= {volume
->pbo
, NULL
};
409 if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
410 #endif /* STAGING_CSMT */
411 wined3d_texture_bind_and_dirtify(volume
->container
, context
, FALSE
);
413 wined3d_texture_bind_and_dirtify(volume
->container
, context
, TRUE
);
415 wined3d_volume_download_data(volume
, context
, &data
);
419 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
420 #if defined(STAGING_CSMT)
421 wined3d_debug_location(volume
->resource
.locations
));
424 wined3d_resource_validate_location(&volume
->resource
, WINED3D_LOCATION_BUFFER
);
428 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location
),
429 wined3d_debug_location(volume
->resource
.locations
));
430 #else /* STAGING_CSMT */
431 wined3d_debug_location(volume
->locations
));
434 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_BUFFER
);
438 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location
),
439 wined3d_debug_location(volume
->locations
));
440 #endif /* STAGING_CSMT */
444 /* Context activation is done by the caller. */
445 void wined3d_volume_load(struct wined3d_volume
*volume
, struct wined3d_context
*context
, BOOL srgb_mode
)
447 wined3d_texture_prepare_texture(volume
->container
, context
, srgb_mode
);
448 #if defined(STAGING_CSMT)
449 wined3d_resource_load_location(&volume
->resource
, context
,
450 srgb_mode
? WINED3D_LOCATION_TEXTURE_SRGB
: WINED3D_LOCATION_TEXTURE_RGB
);
453 void wined3d_volume_destroy(struct wined3d_volume
*volume
)
455 struct wined3d_device
*device
= volume
->resource
.device
;
456 TRACE("volume %p.\n", volume
);
458 resource_cleanup(&volume
->resource
);
459 volume
->resource
.parent_ops
->wined3d_object_destroyed(volume
->resource
.parent
);
460 wined3d_cs_emit_volume_cleanup(device
->cs
, volume
);
461 #else /* STAGING_CSMT */
462 wined3d_volume_load_location(volume
, context
,
463 srgb_mode
? WINED3D_LOCATION_TEXTURE_SRGB
: WINED3D_LOCATION_TEXTURE_RGB
);
466 /* Context activation is done by the caller. */
467 static void wined3d_volume_prepare_pbo(struct wined3d_volume
*volume
, struct wined3d_context
*context
)
469 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
474 GL_EXTCALL(glGenBuffers(1, &volume
->pbo
));
475 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, volume
->pbo
));
476 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER
, volume
->resource
.size
, NULL
, GL_STREAM_DRAW
));
477 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
478 checkGLcall("Create PBO");
480 TRACE("Created PBO %u for volume %p.\n", volume
->pbo
, volume
);
483 static void wined3d_volume_free_pbo(struct wined3d_volume
*volume
)
485 struct wined3d_context
*context
= context_acquire(volume
->resource
.device
, NULL
);
486 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
488 TRACE("Deleting PBO %u belonging to volume %p.\n", volume
->pbo
, volume
);
489 GL_EXTCALL(glDeleteBuffers(1, &volume
->pbo
));
490 checkGLcall("glDeleteBuffers");
492 context_release(context
);
495 void wined3d_volume_destroy(struct wined3d_volume
*volume
)
497 TRACE("volume %p.\n", volume
);
500 wined3d_volume_free_pbo(volume
);
502 resource_cleanup(&volume
->resource
);
503 volume
->resource
.parent_ops
->wined3d_object_destroyed(volume
->resource
.parent
);
504 HeapFree(GetProcessHeap(), 0, volume
);
505 #endif /* STAGING_CSMT */
508 static void volume_unload(struct wined3d_resource
*resource
)
510 struct wined3d_volume
*volume
= volume_from_resource(resource
);
511 struct wined3d_device
*device
= volume
->resource
.device
;
512 struct wined3d_context
*context
;
514 if (volume
->resource
.pool
== WINED3D_POOL_DEFAULT
)
515 ERR("Unloading DEFAULT pool volume.\n");
517 TRACE("texture %p.\n", resource
);
519 #if defined(STAGING_CSMT)
520 if (wined3d_resource_prepare_system_memory(&volume
->resource
))
522 context
= context_acquire(device
, NULL
);
523 wined3d_resource_load_location(&volume
->resource
, context
, WINED3D_LOCATION_SYSMEM
);
524 context_release(context
);
525 wined3d_resource_invalidate_location(&volume
->resource
, ~WINED3D_LOCATION_SYSMEM
);
529 ERR("Out of memory when unloading volume %p.\n", volume
);
530 wined3d_resource_validate_location(&volume
->resource
, WINED3D_LOCATION_DISCARDED
);
531 wined3d_resource_invalidate_location(&volume
->resource
, ~WINED3D_LOCATION_DISCARDED
);
532 #else /* STAGING_CSMT */
533 if (volume_prepare_system_memory(volume
))
535 context
= context_acquire(device
, NULL
);
536 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_SYSMEM
);
537 context_release(context
);
538 wined3d_volume_invalidate_location(volume
, ~WINED3D_LOCATION_SYSMEM
);
542 ERR("Out of memory when unloading volume %p.\n", volume
);
543 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_DISCARDED
);
544 wined3d_volume_invalidate_location(volume
, ~WINED3D_LOCATION_DISCARDED
);
549 /* Should not happen because only dynamic default pool volumes
550 * have a buffer, and those are not evicted by device_evit_managed_resources
551 * and must be freed before a non-ex device reset. */
552 ERR("Unloading a volume with a buffer\n");
553 wined3d_volume_free_pbo(volume
);
554 #endif /* STAGING_CSMT */
557 /* The texture name is managed by the container. */
559 resource_unload(resource
);
562 static BOOL
wined3d_volume_check_box_dimensions(const struct wined3d_volume
*volume
,
563 const struct wined3d_box
*box
)
568 if (box
->left
>= box
->right
)
570 if (box
->top
>= box
->bottom
)
572 if (box
->front
>= box
->back
)
574 if (box
->right
> volume
->resource
.width
)
576 if (box
->bottom
> volume
->resource
.height
)
578 if (box
->back
> volume
->resource
.depth
)
584 HRESULT
wined3d_volume_map(struct wined3d_volume
*volume
,
585 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
587 #if defined(STAGING_CSMT)
589 const struct wined3d_format
*format
= volume
->resource
.format
;
590 const unsigned int fmt_flags
= volume
->container
->resource
.format_flags
;
592 map_desc
->data
= NULL
;
593 if (!(volume
->resource
.access_flags
& WINED3D_RESOURCE_ACCESS_CPU
))
595 WARN("Volume %p is not CPU accessible.\n", volume
);
596 return WINED3DERR_INVALIDCALL
;
598 if (!wined3d_volume_check_box_dimensions(volume
, box
))
600 WARN("Map box is invalid.\n");
601 return WINED3DERR_INVALIDCALL
;
603 if ((fmt_flags
& WINED3DFMT_FLAG_BLOCKS
) && !wined3d_resource_check_block_align(&volume
->resource
, box
))
605 WARN("Map box %s is misaligned for %ux%u blocks.\n",
606 debug_box(box
), format
->block_width
, format
->block_height
);
607 return WINED3DERR_INVALIDCALL
;
610 hr
= wined3d_resource_map(&volume
->resource
, map_desc
, box
, flags
);
617 HRESULT
wined3d_volume_unmap(struct wined3d_volume
*volume
)
621 if (volume
->resource
.unmap_dirtify
)
622 wined3d_texture_set_dirty(volume
->container
);
624 hr
= wined3d_resource_unmap(&volume
->resource
);
625 if (hr
== WINEDDERR_NOTLOCKED
)
626 return WINED3DERR_INVALIDCALL
;
628 #else /* STAGING_CSMT */
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
;
634 const unsigned int fmt_flags
= volume
->container
->resource
.format_flags
;
636 TRACE("volume %p, map_desc %p, box %s, flags %#x.\n",
637 volume
, map_desc
, debug_box(box
), flags
);
639 map_desc
->data
= NULL
;
640 if (!(volume
->resource
.access_flags
& WINED3D_RESOURCE_ACCESS_CPU
))
642 WARN("Volume %p is not CPU accessible.\n", volume
);
643 return WINED3DERR_INVALIDCALL
;
645 if (volume
->resource
.map_count
)
647 WARN("Volume is already mapped.\n");
648 return WINED3DERR_INVALIDCALL
;
650 if (!wined3d_volume_check_box_dimensions(volume
, box
))
652 WARN("Map box is invalid.\n");
653 return WINED3DERR_INVALIDCALL
;
655 if ((fmt_flags
& WINED3DFMT_FLAG_BLOCKS
) && box
656 && !wined3d_texture_check_block_align(volume
->container
, volume
->texture_level
, box
))
658 WARN("Map box %s is misaligned for %ux%u blocks.\n",
659 debug_box(box
), format
->block_width
, format
->block_height
);
660 return WINED3DERR_INVALIDCALL
;
663 flags
= wined3d_resource_sanitize_map_flags(&volume
->resource
, flags
);
665 if (volume
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
)
667 context
= context_acquire(device
, NULL
);
668 gl_info
= context
->gl_info
;
670 wined3d_volume_prepare_pbo(volume
, context
);
671 if (flags
& WINED3D_MAP_DISCARD
)
672 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_BUFFER
);
674 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_BUFFER
);
676 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, volume
->pbo
));
678 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
680 GLbitfield mapflags
= wined3d_resource_gl_map_flags(flags
);
681 mapflags
&= ~GL_MAP_FLUSH_EXPLICIT_BIT
;
682 base_memory
= GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER
,
683 0, volume
->resource
.size
, mapflags
));
687 GLenum access
= wined3d_resource_gl_legacy_map_flags(flags
);
688 base_memory
= GL_EXTCALL(glMapBuffer(GL_PIXEL_UNPACK_BUFFER
, access
));
691 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
692 checkGLcall("Map PBO");
694 context_release(context
);
698 if (!volume_prepare_system_memory(volume
))
700 WARN("Out of memory.\n");
701 map_desc
->data
= NULL
;
702 return E_OUTOFMEMORY
;
705 if (flags
& WINED3D_MAP_DISCARD
)
707 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_SYSMEM
);
709 else if (!(volume
->locations
& WINED3D_LOCATION_SYSMEM
))
711 context
= context_acquire(device
, NULL
);
712 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_SYSMEM
);
713 context_release(context
);
715 base_memory
= volume
->resource
.heap_memory
;
718 TRACE("Base memory pointer %p.\n", base_memory
);
720 if (fmt_flags
& WINED3DFMT_FLAG_BROKEN_PITCH
)
722 map_desc
->row_pitch
= volume
->resource
.width
* format
->byte_count
;
723 map_desc
->slice_pitch
= map_desc
->row_pitch
* volume
->resource
.height
;
727 wined3d_texture_get_pitch(volume
->container
, volume
->texture_level
,
728 &map_desc
->row_pitch
, &map_desc
->slice_pitch
);
733 map_desc
->data
= base_memory
;
737 if ((fmt_flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BROKEN_PITCH
)) == WINED3DFMT_FLAG_BLOCKS
)
739 /* Compressed textures are block based, so calculate the offset of
740 * the block that contains the top-left pixel of the locked rectangle. */
741 map_desc
->data
= base_memory
742 + (box
->front
* map_desc
->slice_pitch
)
743 + ((box
->top
/ format
->block_height
) * map_desc
->row_pitch
)
744 + ((box
->left
/ format
->block_width
) * format
->block_byte_count
);
748 map_desc
->data
= base_memory
749 + (map_desc
->slice_pitch
* box
->front
)
750 + (map_desc
->row_pitch
* box
->top
)
751 + (box
->left
* volume
->resource
.format
->byte_count
);
755 if (!(flags
& (WINED3D_MAP_NO_DIRTY_UPDATE
| WINED3D_MAP_READONLY
)))
757 wined3d_texture_set_dirty(volume
->container
);
758 wined3d_volume_invalidate_location(volume
, ~volume
->resource
.map_binding
);
761 volume
->resource
.map_count
++;
763 TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
764 map_desc
->data
, map_desc
->row_pitch
, map_desc
->slice_pitch
);
769 HRESULT
wined3d_volume_unmap(struct wined3d_volume
*volume
)
771 TRACE("volume %p.\n", volume
);
773 if (!volume
->resource
.map_count
)
775 WARN("Trying to unlock an unlocked volume %p.\n", volume
);
776 return WINED3DERR_INVALIDCALL
;
779 if (volume
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
)
781 struct wined3d_device
*device
= volume
->resource
.device
;
782 struct wined3d_context
*context
= context_acquire(device
, NULL
);
783 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
785 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, volume
->pbo
));
786 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER
));
787 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
788 checkGLcall("Unmap PBO");
790 context_release(context
);
793 volume
->resource
.map_count
--;
796 #endif /* STAGING_CSMT */
799 static ULONG
volume_resource_incref(struct wined3d_resource
*resource
)
801 struct wined3d_volume
*volume
= volume_from_resource(resource
);
802 TRACE("Forwarding to container %p.\n", volume
->container
);
804 return wined3d_texture_incref(volume
->container
);
807 #if defined(STAGING_CSMT)
808 void wined3d_volume_cleanup_cs(struct wined3d_volume
*volume
)
810 HeapFree(GetProcessHeap(), 0, volume
);
813 #endif /* STAGING_CSMT */
814 static ULONG
volume_resource_decref(struct wined3d_resource
*resource
)
816 struct wined3d_volume
*volume
= volume_from_resource(resource
);
817 TRACE("Forwarding to container %p.\n", volume
->container
);
819 return wined3d_texture_decref(volume
->container
);
822 static HRESULT
volume_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
823 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
825 ERR("Not supported on sub-resources.\n");
826 return WINED3DERR_INVALIDCALL
;
829 static HRESULT
volume_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
831 ERR("Not supported on sub-resources.\n");
832 return WINED3DERR_INVALIDCALL
;
835 #if defined(STAGING_CSMT)
836 static void wined3d_volume_location_invalidated(struct wined3d_resource
*resource
, DWORD location
)
838 struct wined3d_volume
*volume
= volume_from_resource(resource
);
840 if (location
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
841 wined3d_texture_set_dirty(volume
->container
);
844 #endif /* STAGING_CSMT */
845 static const struct wined3d_resource_ops volume_resource_ops
=
847 volume_resource_incref
,
848 volume_resource_decref
,
850 volume_resource_sub_resource_map
,
851 volume_resource_sub_resource_unmap
,
852 #if defined(STAGING_CSMT)
853 wined3d_volume_location_invalidated
,
854 wined3d_volume_load_location
,
855 #endif /* STAGING_CSMT */
858 static HRESULT
volume_init(struct wined3d_volume
*volume
, struct wined3d_texture
*container
,
859 const struct wined3d_resource_desc
*desc
, UINT level
)
861 struct wined3d_device
*device
= container
->resource
.device
;
862 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
863 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, desc
->format
);
867 /* TODO: Write tests for other resources and move this check
868 * to resource_init, if applicable. */
869 if (desc
->usage
& WINED3DUSAGE_DYNAMIC
870 && (desc
->pool
== WINED3D_POOL_MANAGED
|| desc
->pool
== WINED3D_POOL_SCRATCH
))
872 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc
->pool
));
873 return WINED3DERR_INVALIDCALL
;
876 size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, desc
->width
, desc
->height
, desc
->depth
);
878 if (FAILED(hr
= resource_init(&volume
->resource
, device
, WINED3D_RTYPE_VOLUME
, format
,
879 WINED3D_MULTISAMPLE_NONE
, 0, desc
->usage
, desc
->pool
, desc
->width
, desc
->height
, desc
->depth
,
880 size
, NULL
, &wined3d_null_parent_ops
, &volume_resource_ops
)))
882 WARN("Failed to initialize resource, returning %#x.\n", hr
);
886 volume
->texture_level
= level
;
887 #if defined(STAGING_CSMT)
888 volume
->resource
.locations
= WINED3D_LOCATION_DISCARDED
;
889 #else /* STAGING_CSMT */
890 volume
->locations
= WINED3D_LOCATION_DISCARDED
;
891 #endif /* STAGING_CSMT */
893 if (desc
->pool
== WINED3D_POOL_DEFAULT
&& desc
->usage
& WINED3DUSAGE_DYNAMIC
894 && gl_info
->supported
[ARB_PIXEL_BUFFER_OBJECT
]
897 wined3d_resource_free_sysmem(&volume
->resource
);
898 volume
->resource
.map_binding
= WINED3D_LOCATION_BUFFER
;
899 #if defined(STAGING_CSMT)
900 volume
->resource
.map_heap_memory
= NULL
;
901 #endif /* STAGING_CSMT */
904 volume
->container
= container
;
909 HRESULT
wined3d_volume_create(struct wined3d_texture
*container
, const struct wined3d_resource_desc
*desc
,
910 unsigned int level
, struct wined3d_volume
**volume
)
912 struct wined3d_device_parent
*device_parent
= container
->resource
.device
->device_parent
;
913 const struct wined3d_parent_ops
*parent_ops
;
914 struct wined3d_volume
*object
;
918 TRACE("container %p, width %u, height %u, depth %u, level %u, format %s, "
919 "usage %#x, pool %s, volume %p.\n",
920 container
, desc
->width
, desc
->height
, desc
->depth
, level
, debug_d3dformat(desc
->format
),
921 desc
->usage
, debug_d3dpool(desc
->pool
), volume
);
923 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
924 return E_OUTOFMEMORY
;
926 if (FAILED(hr
= volume_init(object
, container
, desc
, level
)))
928 WARN("Failed to initialize volume, returning %#x.\n", hr
);
929 HeapFree(GetProcessHeap(), 0, object
);
933 if (FAILED(hr
= device_parent
->ops
->volume_created(device_parent
,
934 container
, level
, &parent
, &parent_ops
)))
936 WARN("Failed to create volume parent, hr %#x.\n", hr
);
937 wined3d_volume_destroy(object
);
941 TRACE("Created volume %p, parent %p, parent_ops %p.\n", object
, parent
, parent_ops
);
943 object
->resource
.parent
= parent
;
944 object
->resource
.parent_ops
= parent_ops
;