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 #if !defined(STAGING_CSMT)
42 void wined3d_volume_get_pitch(const struct wined3d_volume
*volume
, UINT
*row_pitch
, UINT
*slice_pitch
)
44 const struct wined3d_format
*format
= volume
->resource
.format
;
46 if (volume
->container
->resource
.format_flags
& WINED3DFMT_FLAG_BLOCKS
)
48 /* Since compressed formats are block based, pitch means the amount of
49 * bytes to the next row of block rather than the next row of pixels. */
50 UINT row_block_count
= (volume
->resource
.width
+ format
->block_width
- 1) / format
->block_width
;
51 UINT slice_block_count
= (volume
->resource
.height
+ format
->block_height
- 1) / format
->block_height
;
52 *row_pitch
= row_block_count
* format
->block_byte_count
;
53 *slice_pitch
= *row_pitch
* slice_block_count
;
57 unsigned char alignment
= volume
->resource
.device
->surface_alignment
;
58 *row_pitch
= format
->byte_count
* volume
->resource
.width
; /* Bytes / row */
59 *row_pitch
= (*row_pitch
+ alignment
- 1) & ~(alignment
- 1);
60 *slice_pitch
= *row_pitch
* volume
->resource
.height
;
63 TRACE("Returning row pitch %u, slice pitch %u.\n", *row_pitch
, *slice_pitch
);
66 #endif /* STAGING_CSMT */
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
->container
->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 #if defined(STAGING_CSMT)
99 wined3d_resource_get_pitch(&volume
->resource
, &src_row_pitch
, &src_slice_pitch
);
100 #else /* STAGING_CSMT */
101 wined3d_volume_get_pitch(volume
, &src_row_pitch
, &src_slice_pitch
);
102 #endif /* STAGING_CSMT */
104 converted_mem
= HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch
* depth
);
105 format
->convert(data
->addr
, converted_mem
, src_row_pitch
, src_slice_pitch
,
106 dst_row_pitch
, dst_slice_pitch
, width
, height
, depth
);
110 if (data
->buffer_object
)
112 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, data
->buffer_object
));
113 checkGLcall("glBindBuffer");
116 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D
, volume
->texture_level
, 0, 0, 0,
117 width
, height
, depth
,
118 format
->glFormat
, format
->glType
, mem
));
119 checkGLcall("glTexSubImage3D");
121 if (data
->buffer_object
)
123 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
124 checkGLcall("glBindBuffer");
127 HeapFree(GetProcessHeap(), 0, converted_mem
);
130 #if !defined(STAGING_CSMT)
131 void wined3d_volume_validate_location(struct wined3d_volume
*volume
, DWORD location
)
133 TRACE("Volume %p, setting %s.\n", volume
, wined3d_debug_location(location
));
134 volume
->locations
|= location
;
135 TRACE("new location flags are %s.\n", wined3d_debug_location(volume
->locations
));
138 void wined3d_volume_invalidate_location(struct wined3d_volume
*volume
, DWORD location
)
140 TRACE("Volume %p, clearing %s.\n", volume
, wined3d_debug_location(location
));
141 volume
->locations
&= ~location
;
142 TRACE("new location flags are %s.\n", wined3d_debug_location(volume
->locations
));
145 #endif /* STAGING_CSMT */
146 /* Context activation is done by the caller. */
147 static void wined3d_volume_download_data(struct wined3d_volume
*volume
,
148 const struct wined3d_context
*context
, const struct wined3d_bo_address
*data
)
150 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
151 const struct wined3d_format
*format
= volume
->resource
.format
;
155 FIXME("Attempting to download a converted volume, format %s.\n",
156 debug_d3dformat(format
->id
));
160 if (data
->buffer_object
)
162 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, data
->buffer_object
));
163 checkGLcall("glBindBuffer");
166 gl_info
->gl_ops
.gl
.p_glGetTexImage(GL_TEXTURE_3D
, volume
->texture_level
,
167 format
->glFormat
, format
->glType
, data
->addr
);
168 checkGLcall("glGetTexImage");
170 if (data
->buffer_object
)
172 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
173 checkGLcall("glBindBuffer");
178 static void wined3d_volume_evict_sysmem(struct wined3d_volume
*volume
)
180 wined3d_resource_free_sysmem(&volume
->resource
);
181 #if defined(STAGING_CSMT)
182 volume
->resource
.map_heap_memory
= NULL
;
183 wined3d_resource_invalidate_location(&volume
->resource
, WINED3D_LOCATION_SYSMEM
);
184 #else /* STAGING_CSMT */
185 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_SYSMEM
);
188 static DWORD
volume_access_from_location(DWORD location
)
192 case WINED3D_LOCATION_DISCARDED
:
195 case WINED3D_LOCATION_SYSMEM
:
196 return WINED3D_RESOURCE_ACCESS_CPU
;
198 case WINED3D_LOCATION_BUFFER
:
199 case WINED3D_LOCATION_TEXTURE_RGB
:
200 case WINED3D_LOCATION_TEXTURE_SRGB
:
201 return WINED3D_RESOURCE_ACCESS_GPU
;
204 FIXME("Unhandled location %#x.\n", location
);
207 #endif /* STAGING_CSMT */
210 /* Context activation is done by the caller. */
211 static void wined3d_volume_srgb_transfer(struct wined3d_volume
*volume
,
212 struct wined3d_context
*context
, BOOL dest_is_srgb
)
214 struct wined3d_bo_address data
;
215 /* Optimizations are possible, but the effort should be put into either
216 * implementing EXT_SRGB_DECODE in the driver or finding out why we
217 * picked the wrong copy for the original upload and fixing that.
219 * Also keep in mind that we want to avoid using resource.heap_memory
220 * for DEFAULT pool surfaces. */
222 WARN_(d3d_perf
)("Performing slow rgb/srgb volume transfer.\n");
223 data
.buffer_object
= 0;
224 data
.addr
= HeapAlloc(GetProcessHeap(), 0, volume
->resource
.size
);
228 wined3d_texture_bind_and_dirtify(volume
->container
, context
, !dest_is_srgb
);
229 wined3d_volume_download_data(volume
, context
, &data
);
230 wined3d_texture_bind_and_dirtify(volume
->container
, context
, dest_is_srgb
);
231 wined3d_volume_upload_data(volume
, context
, wined3d_const_bo_address(&data
));
233 HeapFree(GetProcessHeap(), 0, data
.addr
);
236 static BOOL
wined3d_volume_can_evict(const struct wined3d_volume
*volume
)
238 if (volume
->resource
.pool
!= WINED3D_POOL_MANAGED
)
240 if (volume
->download_count
>= 10)
242 if (volume
->resource
.format
->convert
)
244 if (volume
->flags
& WINED3D_VFLAG_CLIENT_STORAGE
)
249 #if defined(STAGING_CSMT)
251 /* Context activation is done by the caller. */
252 static void wined3d_volume_load_location(struct wined3d_resource
*resource
,
253 struct wined3d_context
*context
, DWORD location
)
255 struct wined3d_volume
*volume
= volume_from_resource(resource
);
256 DWORD required_access
= wined3d_resource_access_from_location(location
);
258 TRACE("Volume %p, loading %s, have %s.\n", volume
, wined3d_debug_location(location
),
259 wined3d_debug_location(volume
->resource
.locations
));
260 #else /* STAGING_CSMT */
261 /* Context activation is done by the caller. */
262 static void wined3d_volume_load_location(struct wined3d_volume
*volume
,
263 struct wined3d_context
*context
, DWORD location
)
265 DWORD required_access
= volume_access_from_location(location
);
267 TRACE("Volume %p, loading %s, have %s.\n", volume
, wined3d_debug_location(location
),
268 wined3d_debug_location(volume
->locations
));
270 if ((volume
->locations
& location
) == location
)
272 TRACE("Location(s) already up to date.\n");
275 #endif /* STAGING_CSMT */
277 if ((volume
->resource
.access_flags
& required_access
) != required_access
)
279 ERR("Operation requires %#x access, but volume only has %#x.\n",
280 required_access
, volume
->resource
.access_flags
);
286 case WINED3D_LOCATION_TEXTURE_RGB
:
287 case WINED3D_LOCATION_TEXTURE_SRGB
:
288 if ((location
== WINED3D_LOCATION_TEXTURE_RGB
289 && !(volume
->container
->flags
& WINED3D_TEXTURE_RGB_ALLOCATED
))
290 || (location
== WINED3D_LOCATION_TEXTURE_SRGB
291 && !(volume
->container
->flags
& WINED3D_TEXTURE_SRGB_ALLOCATED
)))
292 ERR("Trying to load (s)RGB texture without prior allocation.\n");
294 #if defined(STAGING_CSMT)
295 if (volume
->resource
.locations
& WINED3D_LOCATION_DISCARDED
)
297 TRACE("Volume previously discarded, nothing to do.\n");
298 wined3d_resource_invalidate_location(&volume
->resource
, WINED3D_LOCATION_DISCARDED
);
300 else if (volume
->resource
.locations
& WINED3D_LOCATION_SYSMEM
)
302 struct wined3d_const_bo_address data
= {0, volume
->resource
.heap_memory
};
303 wined3d_texture_bind_and_dirtify(volume
->container
, context
,
304 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
305 wined3d_volume_upload_data(volume
, context
, &data
);
307 else if (volume
->resource
.locations
& WINED3D_LOCATION_BUFFER
)
309 struct wined3d_const_bo_address data
= {volume
->resource
.buffer
->name
, NULL
};
310 wined3d_texture_bind_and_dirtify(volume
->container
, context
,
311 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
312 wined3d_volume_upload_data(volume
, context
, &data
);
314 else if (volume
->resource
.locations
& WINED3D_LOCATION_TEXTURE_RGB
)
316 wined3d_volume_srgb_transfer(volume
, context
, TRUE
);
318 else if (volume
->resource
.locations
& WINED3D_LOCATION_TEXTURE_SRGB
)
320 wined3d_volume_srgb_transfer(volume
, context
, FALSE
);
324 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume
->resource
.locations
));
327 wined3d_resource_validate_location(&volume
->resource
, location
);
328 #else /* STAGING_CSMT */
329 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
331 TRACE("Volume previously discarded, nothing to do.\n");
332 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
334 else if (volume
->locations
& WINED3D_LOCATION_SYSMEM
)
336 struct wined3d_const_bo_address data
= {0, volume
->resource
.heap_memory
};
337 wined3d_texture_bind_and_dirtify(volume
->container
, context
,
338 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
339 wined3d_volume_upload_data(volume
, context
, &data
);
341 else if (volume
->locations
& WINED3D_LOCATION_BUFFER
)
343 struct wined3d_const_bo_address data
= {volume
->pbo
, NULL
};
344 wined3d_texture_bind_and_dirtify(volume
->container
, context
,
345 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
346 wined3d_volume_upload_data(volume
, context
, &data
);
348 else if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
350 wined3d_volume_srgb_transfer(volume
, context
, TRUE
);
352 else if (volume
->locations
& WINED3D_LOCATION_TEXTURE_SRGB
)
354 wined3d_volume_srgb_transfer(volume
, context
, FALSE
);
358 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume
->locations
));
361 wined3d_volume_validate_location(volume
, location
);
362 #endif /* STAGING_CSMT */
364 if (wined3d_volume_can_evict(volume
))
365 wined3d_volume_evict_sysmem(volume
);
369 case WINED3D_LOCATION_SYSMEM
:
370 if (!volume
->resource
.heap_memory
)
371 ERR("Trying to load WINED3D_LOCATION_SYSMEM without setting it up first.\n");
373 #if defined(STAGING_CSMT)
374 if (volume
->resource
.locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
376 struct wined3d_bo_address data
= {0, volume
->resource
.heap_memory
};
378 if (volume
->resource
.locations
& WINED3D_LOCATION_TEXTURE_RGB
)
379 #else /* STAGING_CSMT */
380 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
382 TRACE("Volume previously discarded, nothing to do.\n");
383 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
385 else if (volume
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
387 struct wined3d_bo_address data
= {0, volume
->resource
.heap_memory
};
389 if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
390 #endif /* STAGING_CSMT */
391 wined3d_texture_bind_and_dirtify(volume
->container
, context
, FALSE
);
393 wined3d_texture_bind_and_dirtify(volume
->container
, context
, TRUE
);
395 volume
->download_count
++;
396 wined3d_volume_download_data(volume
, context
, &data
);
400 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
401 #if defined(STAGING_CSMT)
402 wined3d_debug_location(volume
->resource
.locations
));
405 wined3d_resource_validate_location(&volume
->resource
, WINED3D_LOCATION_SYSMEM
);
408 case WINED3D_LOCATION_BUFFER
:
409 if (!volume
->resource
.buffer
)
410 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
412 if (volume
->resource
.locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
414 struct wined3d_bo_address data
= {volume
->resource
.buffer
->name
, NULL
};
416 if (volume
->resource
.locations
& WINED3D_LOCATION_TEXTURE_RGB
)
417 #else /* STAGING_CSMT */
418 wined3d_debug_location(volume
->locations
));
421 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_SYSMEM
);
424 case WINED3D_LOCATION_BUFFER
:
426 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
428 if (volume
->locations
& WINED3D_LOCATION_DISCARDED
)
430 TRACE("Volume previously discarded, nothing to do.\n");
431 wined3d_volume_invalidate_location(volume
, WINED3D_LOCATION_DISCARDED
);
433 else if (volume
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
435 struct wined3d_bo_address data
= {volume
->pbo
, NULL
};
437 if (volume
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
438 #endif /* STAGING_CSMT */
439 wined3d_texture_bind_and_dirtify(volume
->container
, context
, FALSE
);
441 wined3d_texture_bind_and_dirtify(volume
->container
, context
, TRUE
);
443 wined3d_volume_download_data(volume
, context
, &data
);
447 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
448 #if defined(STAGING_CSMT)
449 wined3d_debug_location(volume
->resource
.locations
));
452 wined3d_resource_validate_location(&volume
->resource
, WINED3D_LOCATION_BUFFER
);
456 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location
),
457 wined3d_debug_location(volume
->resource
.locations
));
458 #else /* STAGING_CSMT */
459 wined3d_debug_location(volume
->locations
));
462 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_BUFFER
);
466 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location
),
467 wined3d_debug_location(volume
->locations
));
468 #endif /* STAGING_CSMT */
472 /* Context activation is done by the caller. */
473 void wined3d_volume_load(struct wined3d_volume
*volume
, struct wined3d_context
*context
, BOOL srgb_mode
)
475 wined3d_texture_prepare_texture(volume
->container
, context
, srgb_mode
);
476 #if defined(STAGING_CSMT)
477 wined3d_resource_load_location(&volume
->resource
, context
,
478 srgb_mode
? WINED3D_LOCATION_TEXTURE_SRGB
: WINED3D_LOCATION_TEXTURE_RGB
);
481 void wined3d_volume_destroy(struct wined3d_volume
*volume
)
483 struct wined3d_device
*device
= volume
->resource
.device
;
484 TRACE("volume %p.\n", volume
);
486 resource_cleanup(&volume
->resource
);
487 volume
->resource
.parent_ops
->wined3d_object_destroyed(volume
->resource
.parent
);
488 wined3d_cs_emit_volume_cleanup(device
->cs
, volume
);
489 #else /* STAGING_CSMT */
490 wined3d_volume_load_location(volume
, context
,
491 srgb_mode
? WINED3D_LOCATION_TEXTURE_SRGB
: WINED3D_LOCATION_TEXTURE_RGB
);
494 /* Context activation is done by the caller. */
495 static void wined3d_volume_prepare_pbo(struct wined3d_volume
*volume
, struct wined3d_context
*context
)
497 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
502 GL_EXTCALL(glGenBuffers(1, &volume
->pbo
));
503 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, volume
->pbo
));
504 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER
, volume
->resource
.size
, NULL
, GL_STREAM_DRAW
));
505 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
506 checkGLcall("Create PBO");
508 TRACE("Created PBO %u for volume %p.\n", volume
->pbo
, volume
);
511 static void wined3d_volume_free_pbo(struct wined3d_volume
*volume
)
513 struct wined3d_context
*context
= context_acquire(volume
->resource
.device
, NULL
);
514 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
516 TRACE("Deleting PBO %u belonging to volume %p.\n", volume
->pbo
, volume
);
517 GL_EXTCALL(glDeleteBuffers(1, &volume
->pbo
));
518 checkGLcall("glDeleteBuffers");
520 context_release(context
);
523 void wined3d_volume_destroy(struct wined3d_volume
*volume
)
525 TRACE("volume %p.\n", volume
);
528 wined3d_volume_free_pbo(volume
);
530 resource_cleanup(&volume
->resource
);
531 volume
->resource
.parent_ops
->wined3d_object_destroyed(volume
->resource
.parent
);
532 HeapFree(GetProcessHeap(), 0, volume
);
533 #endif /* STAGING_CSMT */
536 static void volume_unload(struct wined3d_resource
*resource
)
538 struct wined3d_volume
*volume
= volume_from_resource(resource
);
539 struct wined3d_device
*device
= volume
->resource
.device
;
540 struct wined3d_context
*context
;
542 if (volume
->resource
.pool
== WINED3D_POOL_DEFAULT
)
543 ERR("Unloading DEFAULT pool volume.\n");
545 TRACE("texture %p.\n", resource
);
547 #if defined(STAGING_CSMT)
548 if (wined3d_resource_prepare_system_memory(&volume
->resource
))
550 context
= context_acquire(device
, NULL
);
551 wined3d_resource_load_location(&volume
->resource
, context
, WINED3D_LOCATION_SYSMEM
);
552 context_release(context
);
553 wined3d_resource_invalidate_location(&volume
->resource
, ~WINED3D_LOCATION_SYSMEM
);
557 ERR("Out of memory when unloading volume %p.\n", volume
);
558 wined3d_resource_validate_location(&volume
->resource
, WINED3D_LOCATION_DISCARDED
);
559 wined3d_resource_invalidate_location(&volume
->resource
, ~WINED3D_LOCATION_DISCARDED
);
560 #else /* STAGING_CSMT */
561 if (volume_prepare_system_memory(volume
))
563 context
= context_acquire(device
, NULL
);
564 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_SYSMEM
);
565 context_release(context
);
566 wined3d_volume_invalidate_location(volume
, ~WINED3D_LOCATION_SYSMEM
);
570 ERR("Out of memory when unloading volume %p.\n", volume
);
571 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_DISCARDED
);
572 wined3d_volume_invalidate_location(volume
, ~WINED3D_LOCATION_DISCARDED
);
577 /* Should not happen because only dynamic default pool volumes
578 * have a buffer, and those are not evicted by device_evit_managed_resources
579 * and must be freed before a non-ex device reset. */
580 ERR("Unloading a volume with a buffer\n");
581 wined3d_volume_free_pbo(volume
);
582 #endif /* STAGING_CSMT */
585 /* The texture name is managed by the container. */
586 volume
->flags
&= ~WINED3D_VFLAG_CLIENT_STORAGE
;
588 resource_unload(resource
);
591 #if !defined(STAGING_CSMT)
592 static BOOL
volume_check_block_align(const struct wined3d_volume
*volume
,
593 const struct wined3d_box
*box
)
595 UINT width_mask
, height_mask
;
596 const struct wined3d_format
*format
= volume
->resource
.format
;
601 /* This assumes power of two block sizes, but NPOT block sizes would be
604 * This also assumes that the format's block depth is 1. */
605 width_mask
= format
->block_width
- 1;
606 height_mask
= format
->block_height
- 1;
608 if (box
->left
& width_mask
)
610 if (box
->top
& height_mask
)
612 if (box
->right
& width_mask
&& box
->right
!= volume
->resource
.width
)
614 if (box
->bottom
& height_mask
&& box
->bottom
!= volume
->resource
.height
)
620 #endif /* STAGING_CSMT */
621 static BOOL
wined3d_volume_check_box_dimensions(const struct wined3d_volume
*volume
,
622 const struct wined3d_box
*box
)
627 if (box
->left
>= box
->right
)
629 if (box
->top
>= box
->bottom
)
631 if (box
->front
>= box
->back
)
633 if (box
->right
> volume
->resource
.width
)
635 if (box
->bottom
> volume
->resource
.height
)
637 if (box
->back
> volume
->resource
.depth
)
643 HRESULT
wined3d_volume_map(struct wined3d_volume
*volume
,
644 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
646 #if defined(STAGING_CSMT)
648 const struct wined3d_format
*format
= volume
->resource
.format
;
649 const unsigned int fmt_flags
= volume
->container
->resource
.format_flags
;
651 map_desc
->data
= NULL
;
652 if (!(volume
->resource
.access_flags
& WINED3D_RESOURCE_ACCESS_CPU
))
654 WARN("Volume %p is not CPU accessible.\n", volume
);
655 return WINED3DERR_INVALIDCALL
;
657 if (!wined3d_volume_check_box_dimensions(volume
, box
))
659 WARN("Map box is invalid.\n");
660 return WINED3DERR_INVALIDCALL
;
662 if ((fmt_flags
& WINED3DFMT_FLAG_BLOCKS
) && !wined3d_resource_check_block_align(&volume
->resource
, box
))
664 WARN("Map box is misaligned for %ux%u blocks.\n",
665 format
->block_width
, format
->block_height
);
666 return WINED3DERR_INVALIDCALL
;
669 hr
= wined3d_resource_map(&volume
->resource
, map_desc
, box
, flags
);
676 HRESULT
wined3d_volume_unmap(struct wined3d_volume
*volume
)
680 if (volume
->resource
.unmap_dirtify
)
681 wined3d_texture_set_dirty(volume
->container
);
683 hr
= wined3d_resource_unmap(&volume
->resource
);
684 if (hr
== WINEDDERR_NOTLOCKED
)
685 return WINED3DERR_INVALIDCALL
;
687 #else /* STAGING_CSMT */
688 struct wined3d_device
*device
= volume
->resource
.device
;
689 struct wined3d_context
*context
;
690 const struct wined3d_gl_info
*gl_info
;
692 const struct wined3d_format
*format
= volume
->resource
.format
;
693 const unsigned int fmt_flags
= volume
->container
->resource
.format_flags
;
695 TRACE("volume %p, map_desc %p, box %p, flags %#x.\n",
696 volume
, map_desc
, box
, flags
);
698 map_desc
->data
= NULL
;
699 if (!(volume
->resource
.access_flags
& WINED3D_RESOURCE_ACCESS_CPU
))
701 WARN("Volume %p is not CPU accessible.\n", volume
);
702 return WINED3DERR_INVALIDCALL
;
704 if (volume
->resource
.map_count
)
706 WARN("Volume is already mapped.\n");
707 return WINED3DERR_INVALIDCALL
;
709 if (!wined3d_volume_check_box_dimensions(volume
, box
))
711 WARN("Map box is invalid.\n");
712 return WINED3DERR_INVALIDCALL
;
714 if ((fmt_flags
& WINED3DFMT_FLAG_BLOCKS
) && !volume_check_block_align(volume
, box
))
716 WARN("Map box is misaligned for %ux%u blocks.\n",
717 format
->block_width
, format
->block_height
);
718 return WINED3DERR_INVALIDCALL
;
721 flags
= wined3d_resource_sanitize_map_flags(&volume
->resource
, flags
);
723 if (volume
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
)
725 context
= context_acquire(device
, NULL
);
726 gl_info
= context
->gl_info
;
728 wined3d_volume_prepare_pbo(volume
, context
);
729 if (flags
& WINED3D_MAP_DISCARD
)
730 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_BUFFER
);
732 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_BUFFER
);
734 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, volume
->pbo
));
736 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
738 GLbitfield mapflags
= wined3d_resource_gl_map_flags(flags
);
739 mapflags
&= ~GL_MAP_FLUSH_EXPLICIT_BIT
;
740 base_memory
= GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER
,
741 0, volume
->resource
.size
, mapflags
));
745 GLenum access
= wined3d_resource_gl_legacy_map_flags(flags
);
746 base_memory
= GL_EXTCALL(glMapBuffer(GL_PIXEL_UNPACK_BUFFER
, access
));
749 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
750 checkGLcall("Map PBO");
752 context_release(context
);
756 if (!volume_prepare_system_memory(volume
))
758 WARN("Out of memory.\n");
759 map_desc
->data
= NULL
;
760 return E_OUTOFMEMORY
;
763 if (flags
& WINED3D_MAP_DISCARD
)
765 wined3d_volume_validate_location(volume
, WINED3D_LOCATION_SYSMEM
);
767 else if (!(volume
->locations
& WINED3D_LOCATION_SYSMEM
))
769 context
= context_acquire(device
, NULL
);
770 wined3d_volume_load_location(volume
, context
, WINED3D_LOCATION_SYSMEM
);
771 context_release(context
);
773 base_memory
= volume
->resource
.heap_memory
;
776 TRACE("Base memory pointer %p.\n", base_memory
);
778 if (fmt_flags
& WINED3DFMT_FLAG_BROKEN_PITCH
)
780 map_desc
->row_pitch
= volume
->resource
.width
* format
->byte_count
;
781 map_desc
->slice_pitch
= map_desc
->row_pitch
* volume
->resource
.height
;
785 wined3d_volume_get_pitch(volume
, &map_desc
->row_pitch
, &map_desc
->slice_pitch
);
790 TRACE("No box supplied - all is ok\n");
791 map_desc
->data
= base_memory
;
795 TRACE("Lock Box (%p) = l %u, t %u, r %u, b %u, fr %u, ba %u\n",
796 box
, box
->left
, box
->top
, box
->right
, box
->bottom
, box
->front
, box
->back
);
798 if ((fmt_flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BROKEN_PITCH
)) == WINED3DFMT_FLAG_BLOCKS
)
800 /* Compressed textures are block based, so calculate the offset of
801 * the block that contains the top-left pixel of the locked rectangle. */
802 map_desc
->data
= base_memory
803 + (box
->front
* map_desc
->slice_pitch
)
804 + ((box
->top
/ format
->block_height
) * map_desc
->row_pitch
)
805 + ((box
->left
/ format
->block_width
) * format
->block_byte_count
);
809 map_desc
->data
= base_memory
810 + (map_desc
->slice_pitch
* box
->front
)
811 + (map_desc
->row_pitch
* box
->top
)
812 + (box
->left
* volume
->resource
.format
->byte_count
);
816 if (!(flags
& (WINED3D_MAP_NO_DIRTY_UPDATE
| WINED3D_MAP_READONLY
)))
818 wined3d_texture_set_dirty(volume
->container
);
819 wined3d_volume_invalidate_location(volume
, ~volume
->resource
.map_binding
);
822 volume
->resource
.map_count
++;
824 TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
825 map_desc
->data
, map_desc
->row_pitch
, map_desc
->slice_pitch
);
830 HRESULT
wined3d_volume_unmap(struct wined3d_volume
*volume
)
832 TRACE("volume %p.\n", volume
);
834 if (!volume
->resource
.map_count
)
836 WARN("Trying to unlock an unlocked volume %p.\n", volume
);
837 return WINED3DERR_INVALIDCALL
;
840 if (volume
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
)
842 struct wined3d_device
*device
= volume
->resource
.device
;
843 struct wined3d_context
*context
= context_acquire(device
, NULL
);
844 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
846 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, volume
->pbo
));
847 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER
));
848 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
849 checkGLcall("Unmap PBO");
851 context_release(context
);
854 volume
->resource
.map_count
--;
857 #endif /* STAGING_CSMT */
860 static ULONG
volume_resource_incref(struct wined3d_resource
*resource
)
862 struct wined3d_volume
*volume
= volume_from_resource(resource
);
863 TRACE("Forwarding to container %p.\n", volume
->container
);
865 return wined3d_texture_incref(volume
->container
);
868 #if defined(STAGING_CSMT)
869 void wined3d_volume_cleanup_cs(struct wined3d_volume
*volume
)
871 HeapFree(GetProcessHeap(), 0, volume
);
874 #endif /* STAGING_CSMT */
875 static ULONG
volume_resource_decref(struct wined3d_resource
*resource
)
877 struct wined3d_volume
*volume
= volume_from_resource(resource
);
878 TRACE("Forwarding to container %p.\n", volume
->container
);
880 return wined3d_texture_decref(volume
->container
);
883 static HRESULT
volume_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
884 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
886 ERR("Not supported on sub-resources.\n");
887 return WINED3DERR_INVALIDCALL
;
890 static HRESULT
volume_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
892 ERR("Not supported on sub-resources.\n");
893 return WINED3DERR_INVALIDCALL
;
896 #if defined(STAGING_CSMT)
897 static void wined3d_volume_location_invalidated(struct wined3d_resource
*resource
, DWORD location
)
899 struct wined3d_volume
*volume
= volume_from_resource(resource
);
901 if (location
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
902 wined3d_texture_set_dirty(volume
->container
);
905 #endif /* STAGING_CSMT */
906 static const struct wined3d_resource_ops volume_resource_ops
=
908 volume_resource_incref
,
909 volume_resource_decref
,
911 volume_resource_sub_resource_map
,
912 volume_resource_sub_resource_unmap
,
913 #if defined(STAGING_CSMT)
914 wined3d_volume_location_invalidated
,
915 wined3d_volume_load_location
,
916 #endif /* STAGING_CSMT */
919 static HRESULT
volume_init(struct wined3d_volume
*volume
, struct wined3d_texture
*container
,
920 const struct wined3d_resource_desc
*desc
, UINT level
)
922 struct wined3d_device
*device
= container
->resource
.device
;
923 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
924 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, desc
->format
);
928 /* TODO: Write tests for other resources and move this check
929 * to resource_init, if applicable. */
930 if (desc
->usage
& WINED3DUSAGE_DYNAMIC
931 && (desc
->pool
== WINED3D_POOL_MANAGED
|| desc
->pool
== WINED3D_POOL_SCRATCH
))
933 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc
->pool
));
934 return WINED3DERR_INVALIDCALL
;
937 size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, desc
->width
, desc
->height
, desc
->depth
);
939 if (FAILED(hr
= resource_init(&volume
->resource
, device
, WINED3D_RTYPE_VOLUME
, format
,
940 WINED3D_MULTISAMPLE_NONE
, 0, desc
->usage
, desc
->pool
, desc
->width
, desc
->height
, desc
->depth
,
941 size
, NULL
, &wined3d_null_parent_ops
, &volume_resource_ops
)))
943 WARN("Failed to initialize resource, returning %#x.\n", hr
);
947 volume
->texture_level
= level
;
948 #if defined(STAGING_CSMT)
949 volume
->resource
.locations
= WINED3D_LOCATION_DISCARDED
;
950 #else /* STAGING_CSMT */
951 volume
->locations
= WINED3D_LOCATION_DISCARDED
;
952 #endif /* STAGING_CSMT */
954 if (desc
->pool
== WINED3D_POOL_DEFAULT
&& desc
->usage
& WINED3DUSAGE_DYNAMIC
955 && gl_info
->supported
[ARB_PIXEL_BUFFER_OBJECT
]
958 wined3d_resource_free_sysmem(&volume
->resource
);
959 volume
->resource
.map_binding
= WINED3D_LOCATION_BUFFER
;
960 #if defined(STAGING_CSMT)
961 volume
->resource
.map_heap_memory
= NULL
;
962 #endif /* STAGING_CSMT */
965 volume
->container
= container
;
970 HRESULT
wined3d_volume_create(struct wined3d_texture
*container
, const struct wined3d_resource_desc
*desc
,
971 unsigned int level
, struct wined3d_volume
**volume
)
973 struct wined3d_device_parent
*device_parent
= container
->resource
.device
->device_parent
;
974 const struct wined3d_parent_ops
*parent_ops
;
975 struct wined3d_volume
*object
;
979 TRACE("container %p, width %u, height %u, depth %u, level %u, format %s, "
980 "usage %#x, pool %s, volume %p.\n",
981 container
, desc
->width
, desc
->height
, desc
->depth
, level
, debug_d3dformat(desc
->format
),
982 desc
->usage
, debug_d3dpool(desc
->pool
), volume
);
984 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
985 return E_OUTOFMEMORY
;
987 if (FAILED(hr
= volume_init(object
, container
, desc
, level
)))
989 WARN("Failed to initialize volume, returning %#x.\n", hr
);
990 HeapFree(GetProcessHeap(), 0, object
);
994 if (FAILED(hr
= device_parent
->ops
->volume_created(device_parent
,
995 container
, level
, &parent
, &parent_ops
)))
997 WARN("Failed to create volume parent, hr %#x.\n", hr
);
998 wined3d_volume_destroy(object
);
1002 TRACE("Created volume %p, parent %p, parent_ops %p.\n", object
, parent
, parent_ops
);
1004 object
->resource
.parent
= parent
;
1005 object
->resource
.parent_ops
= parent_ops
;