[D3D8][D3D9][DDRAW][WINED3D] Sync with Wine Staging 1.7.55. CORE-10536
[reactos.git] / reactos / dll / directx / wine / wined3d / volume.c
1 /*
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
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include "wined3d_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
26 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
27
28 BOOL volume_prepare_system_memory(struct wined3d_volume *volume)
29 {
30 if (volume->resource.heap_memory)
31 return TRUE;
32
33 if (!wined3d_resource_allocate_sysmem(&volume->resource))
34 {
35 ERR("Failed to allocate system memory.\n");
36 return FALSE;
37 }
38 return TRUE;
39 }
40
41 #if !defined(STAGING_CSMT)
42 void wined3d_volume_get_pitch(const struct wined3d_volume *volume, UINT *row_pitch, UINT *slice_pitch)
43 {
44 const struct wined3d_format *format = volume->resource.format;
45
46 if (volume->container->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
47 {
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;
54 }
55 else
56 {
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;
61 }
62
63 TRACE("Returning row pitch %u, slice pitch %u.\n", *row_pitch, *slice_pitch);
64 }
65
66 #endif /* STAGING_CSMT */
67 /* This call just uploads data, the caller is responsible for binding the
68 * correct texture. */
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)
72 {
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;
80
81 TRACE("volume %p, context %p, level %u, format %s (%#x).\n",
82 volume, context, volume->texture_level, debug_d3dformat(format->id),
83 format->id);
84
85 if (format->convert)
86 {
87 UINT dst_row_pitch, dst_slice_pitch;
88 UINT src_row_pitch, src_slice_pitch;
89
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");
94
95 dst_row_pitch = width * format->conv_byte_count;
96 dst_slice_pitch = dst_row_pitch * height;
97
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 */
103
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);
107 mem = converted_mem;
108 }
109
110 if (data->buffer_object)
111 {
112 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
113 checkGLcall("glBindBuffer");
114 }
115
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");
120
121 if (data->buffer_object)
122 {
123 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
124 checkGLcall("glBindBuffer");
125 }
126
127 HeapFree(GetProcessHeap(), 0, converted_mem);
128 }
129
130 #if !defined(STAGING_CSMT)
131 void wined3d_volume_validate_location(struct wined3d_volume *volume, DWORD location)
132 {
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));
136 }
137
138 void wined3d_volume_invalidate_location(struct wined3d_volume *volume, DWORD location)
139 {
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));
143 }
144
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)
149 {
150 const struct wined3d_gl_info *gl_info = context->gl_info;
151 const struct wined3d_format *format = volume->resource.format;
152
153 if (format->convert)
154 {
155 FIXME("Attempting to download a converted volume, format %s.\n",
156 debug_d3dformat(format->id));
157 return;
158 }
159
160 if (data->buffer_object)
161 {
162 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
163 checkGLcall("glBindBuffer");
164 }
165
166 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, volume->texture_level,
167 format->glFormat, format->glType, data->addr);
168 checkGLcall("glGetTexImage");
169
170 if (data->buffer_object)
171 {
172 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
173 checkGLcall("glBindBuffer");
174 }
175
176 }
177
178 static void wined3d_volume_evict_sysmem(struct wined3d_volume *volume)
179 {
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);
186 }
187
188 static DWORD volume_access_from_location(DWORD location)
189 {
190 switch (location)
191 {
192 case WINED3D_LOCATION_DISCARDED:
193 return 0;
194
195 case WINED3D_LOCATION_SYSMEM:
196 return WINED3D_RESOURCE_ACCESS_CPU;
197
198 case WINED3D_LOCATION_BUFFER:
199 case WINED3D_LOCATION_TEXTURE_RGB:
200 case WINED3D_LOCATION_TEXTURE_SRGB:
201 return WINED3D_RESOURCE_ACCESS_GPU;
202
203 default:
204 FIXME("Unhandled location %#x.\n", location);
205 return 0;
206 }
207 #endif /* STAGING_CSMT */
208 }
209
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)
213 {
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.
218 *
219 * Also keep in mind that we want to avoid using resource.heap_memory
220 * for DEFAULT pool surfaces. */
221
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);
225 if (!data.addr)
226 return;
227
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));
232
233 HeapFree(GetProcessHeap(), 0, data.addr);
234 }
235
236 static BOOL wined3d_volume_can_evict(const struct wined3d_volume *volume)
237 {
238 if (volume->resource.pool != WINED3D_POOL_MANAGED)
239 return FALSE;
240 if (volume->download_count >= 10)
241 return FALSE;
242 if (volume->resource.format->convert)
243 return FALSE;
244 if (volume->flags & WINED3D_VFLAG_CLIENT_STORAGE)
245 return FALSE;
246
247 return TRUE;
248 }
249 #if defined(STAGING_CSMT)
250
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)
254 {
255 struct wined3d_volume *volume = volume_from_resource(resource);
256 DWORD required_access = wined3d_resource_access_from_location(location);
257
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)
264 {
265 DWORD required_access = volume_access_from_location(location);
266
267 TRACE("Volume %p, loading %s, have %s.\n", volume, wined3d_debug_location(location),
268 wined3d_debug_location(volume->locations));
269
270 if ((volume->locations & location) == location)
271 {
272 TRACE("Location(s) already up to date.\n");
273 return;
274 }
275 #endif /* STAGING_CSMT */
276
277 if ((volume->resource.access_flags & required_access) != required_access)
278 {
279 ERR("Operation requires %#x access, but volume only has %#x.\n",
280 required_access, volume->resource.access_flags);
281 return;
282 }
283
284 switch (location)
285 {
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");
293
294 #if defined(STAGING_CSMT)
295 if (volume->resource.locations & WINED3D_LOCATION_DISCARDED)
296 {
297 TRACE("Volume previously discarded, nothing to do.\n");
298 wined3d_resource_invalidate_location(&volume->resource, WINED3D_LOCATION_DISCARDED);
299 }
300 else if (volume->resource.locations & WINED3D_LOCATION_SYSMEM)
301 {
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);
306 }
307 else if (volume->resource.locations & WINED3D_LOCATION_BUFFER)
308 {
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);
313 }
314 else if (volume->resource.locations & WINED3D_LOCATION_TEXTURE_RGB)
315 {
316 wined3d_volume_srgb_transfer(volume, context, TRUE);
317 }
318 else if (volume->resource.locations & WINED3D_LOCATION_TEXTURE_SRGB)
319 {
320 wined3d_volume_srgb_transfer(volume, context, FALSE);
321 }
322 else
323 {
324 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume->resource.locations));
325 return;
326 }
327 wined3d_resource_validate_location(&volume->resource, location);
328 #else /* STAGING_CSMT */
329 if (volume->locations & WINED3D_LOCATION_DISCARDED)
330 {
331 TRACE("Volume previously discarded, nothing to do.\n");
332 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
333 }
334 else if (volume->locations & WINED3D_LOCATION_SYSMEM)
335 {
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);
340 }
341 else if (volume->locations & WINED3D_LOCATION_BUFFER)
342 {
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);
347 }
348 else if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
349 {
350 wined3d_volume_srgb_transfer(volume, context, TRUE);
351 }
352 else if (volume->locations & WINED3D_LOCATION_TEXTURE_SRGB)
353 {
354 wined3d_volume_srgb_transfer(volume, context, FALSE);
355 }
356 else
357 {
358 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume->locations));
359 return;
360 }
361 wined3d_volume_validate_location(volume, location);
362 #endif /* STAGING_CSMT */
363
364 if (wined3d_volume_can_evict(volume))
365 wined3d_volume_evict_sysmem(volume);
366
367 break;
368
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");
372
373 #if defined(STAGING_CSMT)
374 if (volume->resource.locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
375 {
376 struct wined3d_bo_address data = {0, volume->resource.heap_memory};
377
378 if (volume->resource.locations & WINED3D_LOCATION_TEXTURE_RGB)
379 #else /* STAGING_CSMT */
380 if (volume->locations & WINED3D_LOCATION_DISCARDED)
381 {
382 TRACE("Volume previously discarded, nothing to do.\n");
383 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
384 }
385 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
386 {
387 struct wined3d_bo_address data = {0, volume->resource.heap_memory};
388
389 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
390 #endif /* STAGING_CSMT */
391 wined3d_texture_bind_and_dirtify(volume->container, context, FALSE);
392 else
393 wined3d_texture_bind_and_dirtify(volume->container, context, TRUE);
394
395 volume->download_count++;
396 wined3d_volume_download_data(volume, context, &data);
397 }
398 else
399 {
400 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
401 #if defined(STAGING_CSMT)
402 wined3d_debug_location(volume->resource.locations));
403 return;
404 }
405 wined3d_resource_validate_location(&volume->resource, WINED3D_LOCATION_SYSMEM);
406 break;
407
408 case WINED3D_LOCATION_BUFFER:
409 if (!volume->resource.buffer)
410 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
411
412 if (volume->resource.locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
413 {
414 struct wined3d_bo_address data = {volume->resource.buffer->name, NULL};
415
416 if (volume->resource.locations & WINED3D_LOCATION_TEXTURE_RGB)
417 #else /* STAGING_CSMT */
418 wined3d_debug_location(volume->locations));
419 return;
420 }
421 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
422 break;
423
424 case WINED3D_LOCATION_BUFFER:
425 if (!volume->pbo)
426 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
427
428 if (volume->locations & WINED3D_LOCATION_DISCARDED)
429 {
430 TRACE("Volume previously discarded, nothing to do.\n");
431 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
432 }
433 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
434 {
435 struct wined3d_bo_address data = {volume->pbo, NULL};
436
437 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
438 #endif /* STAGING_CSMT */
439 wined3d_texture_bind_and_dirtify(volume->container, context, FALSE);
440 else
441 wined3d_texture_bind_and_dirtify(volume->container, context, TRUE);
442
443 wined3d_volume_download_data(volume, context, &data);
444 }
445 else
446 {
447 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
448 #if defined(STAGING_CSMT)
449 wined3d_debug_location(volume->resource.locations));
450 return;
451 }
452 wined3d_resource_validate_location(&volume->resource, WINED3D_LOCATION_BUFFER);
453 break;
454
455 default:
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));
460 return;
461 }
462 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
463 break;
464
465 default:
466 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
467 wined3d_debug_location(volume->locations));
468 #endif /* STAGING_CSMT */
469 }
470 }
471
472 /* Context activation is done by the caller. */
473 void wined3d_volume_load(struct wined3d_volume *volume, struct wined3d_context *context, BOOL srgb_mode)
474 {
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);
479 }
480
481 void wined3d_volume_destroy(struct wined3d_volume *volume)
482 {
483 struct wined3d_device *device = volume->resource.device;
484 TRACE("volume %p.\n", volume);
485
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);
492 }
493
494 /* Context activation is done by the caller. */
495 static void wined3d_volume_prepare_pbo(struct wined3d_volume *volume, struct wined3d_context *context)
496 {
497 const struct wined3d_gl_info *gl_info = context->gl_info;
498
499 if (volume->pbo)
500 return;
501
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");
507
508 TRACE("Created PBO %u for volume %p.\n", volume->pbo, volume);
509 }
510
511 static void wined3d_volume_free_pbo(struct wined3d_volume *volume)
512 {
513 struct wined3d_context *context = context_acquire(volume->resource.device, NULL);
514 const struct wined3d_gl_info *gl_info = context->gl_info;
515
516 TRACE("Deleting PBO %u belonging to volume %p.\n", volume->pbo, volume);
517 GL_EXTCALL(glDeleteBuffers(1, &volume->pbo));
518 checkGLcall("glDeleteBuffers");
519 volume->pbo = 0;
520 context_release(context);
521 }
522
523 void wined3d_volume_destroy(struct wined3d_volume *volume)
524 {
525 TRACE("volume %p.\n", volume);
526
527 if (volume->pbo)
528 wined3d_volume_free_pbo(volume);
529
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 */
534 }
535
536 static void volume_unload(struct wined3d_resource *resource)
537 {
538 struct wined3d_volume *volume = volume_from_resource(resource);
539 struct wined3d_device *device = volume->resource.device;
540 struct wined3d_context *context;
541
542 if (volume->resource.pool == WINED3D_POOL_DEFAULT)
543 ERR("Unloading DEFAULT pool volume.\n");
544
545 TRACE("texture %p.\n", resource);
546
547 #if defined(STAGING_CSMT)
548 if (wined3d_resource_prepare_system_memory(&volume->resource))
549 {
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);
554 }
555 else
556 {
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))
562 {
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);
567 }
568 else
569 {
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);
573 }
574
575 if (volume->pbo)
576 {
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 */
583 }
584
585 /* The texture name is managed by the container. */
586 volume->flags &= ~WINED3D_VFLAG_CLIENT_STORAGE;
587
588 resource_unload(resource);
589 }
590
591 #if !defined(STAGING_CSMT)
592 static BOOL volume_check_block_align(const struct wined3d_volume *volume,
593 const struct wined3d_box *box)
594 {
595 UINT width_mask, height_mask;
596 const struct wined3d_format *format = volume->resource.format;
597
598 if (!box)
599 return TRUE;
600
601 /* This assumes power of two block sizes, but NPOT block sizes would be
602 * silly anyway.
603 *
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;
607
608 if (box->left & width_mask)
609 return FALSE;
610 if (box->top & height_mask)
611 return FALSE;
612 if (box->right & width_mask && box->right != volume->resource.width)
613 return FALSE;
614 if (box->bottom & height_mask && box->bottom != volume->resource.height)
615 return FALSE;
616
617 return TRUE;
618 }
619
620 #endif /* STAGING_CSMT */
621 static BOOL wined3d_volume_check_box_dimensions(const struct wined3d_volume *volume,
622 const struct wined3d_box *box)
623 {
624 if (!box)
625 return TRUE;
626
627 if (box->left >= box->right)
628 return FALSE;
629 if (box->top >= box->bottom)
630 return FALSE;
631 if (box->front >= box->back)
632 return FALSE;
633 if (box->right > volume->resource.width)
634 return FALSE;
635 if (box->bottom > volume->resource.height)
636 return FALSE;
637 if (box->back > volume->resource.depth)
638 return FALSE;
639
640 return TRUE;
641 }
642
643 HRESULT wined3d_volume_map(struct wined3d_volume *volume,
644 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
645 {
646 #if defined(STAGING_CSMT)
647 HRESULT hr;
648 const struct wined3d_format *format = volume->resource.format;
649 const unsigned int fmt_flags = volume->container->resource.format_flags;
650
651 map_desc->data = NULL;
652 if (!(volume->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU))
653 {
654 WARN("Volume %p is not CPU accessible.\n", volume);
655 return WINED3DERR_INVALIDCALL;
656 }
657 if (!wined3d_volume_check_box_dimensions(volume, box))
658 {
659 WARN("Map box is invalid.\n");
660 return WINED3DERR_INVALIDCALL;
661 }
662 if ((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && !wined3d_resource_check_block_align(&volume->resource, box))
663 {
664 WARN("Map box is misaligned for %ux%u blocks.\n",
665 format->block_width, format->block_height);
666 return WINED3DERR_INVALIDCALL;
667 }
668
669 hr = wined3d_resource_map(&volume->resource, map_desc, box, flags);
670 if (FAILED(hr))
671 return hr;
672
673 return hr;
674 }
675
676 HRESULT wined3d_volume_unmap(struct wined3d_volume *volume)
677 {
678 HRESULT hr;
679
680 if (volume->resource.unmap_dirtify)
681 wined3d_texture_set_dirty(volume->container);
682
683 hr = wined3d_resource_unmap(&volume->resource);
684 if (hr == WINEDDERR_NOTLOCKED)
685 return WINED3DERR_INVALIDCALL;
686 return hr;
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;
691 BYTE *base_memory;
692 const struct wined3d_format *format = volume->resource.format;
693 const unsigned int fmt_flags = volume->container->resource.format_flags;
694
695 TRACE("volume %p, map_desc %p, box %p, flags %#x.\n",
696 volume, map_desc, box, flags);
697
698 map_desc->data = NULL;
699 if (!(volume->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU))
700 {
701 WARN("Volume %p is not CPU accessible.\n", volume);
702 return WINED3DERR_INVALIDCALL;
703 }
704 if (volume->resource.map_count)
705 {
706 WARN("Volume is already mapped.\n");
707 return WINED3DERR_INVALIDCALL;
708 }
709 if (!wined3d_volume_check_box_dimensions(volume, box))
710 {
711 WARN("Map box is invalid.\n");
712 return WINED3DERR_INVALIDCALL;
713 }
714 if ((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && !volume_check_block_align(volume, box))
715 {
716 WARN("Map box is misaligned for %ux%u blocks.\n",
717 format->block_width, format->block_height);
718 return WINED3DERR_INVALIDCALL;
719 }
720
721 flags = wined3d_resource_sanitize_map_flags(&volume->resource, flags);
722
723 if (volume->resource.map_binding == WINED3D_LOCATION_BUFFER)
724 {
725 context = context_acquire(device, NULL);
726 gl_info = context->gl_info;
727
728 wined3d_volume_prepare_pbo(volume, context);
729 if (flags & WINED3D_MAP_DISCARD)
730 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
731 else
732 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_BUFFER);
733
734 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, volume->pbo));
735
736 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
737 {
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));
742 }
743 else
744 {
745 GLenum access = wined3d_resource_gl_legacy_map_flags(flags);
746 base_memory = GL_EXTCALL(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, access));
747 }
748
749 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
750 checkGLcall("Map PBO");
751
752 context_release(context);
753 }
754 else
755 {
756 if (!volume_prepare_system_memory(volume))
757 {
758 WARN("Out of memory.\n");
759 map_desc->data = NULL;
760 return E_OUTOFMEMORY;
761 }
762
763 if (flags & WINED3D_MAP_DISCARD)
764 {
765 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
766 }
767 else if (!(volume->locations & WINED3D_LOCATION_SYSMEM))
768 {
769 context = context_acquire(device, NULL);
770 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
771 context_release(context);
772 }
773 base_memory = volume->resource.heap_memory;
774 }
775
776 TRACE("Base memory pointer %p.\n", base_memory);
777
778 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
779 {
780 map_desc->row_pitch = volume->resource.width * format->byte_count;
781 map_desc->slice_pitch = map_desc->row_pitch * volume->resource.height;
782 }
783 else
784 {
785 wined3d_volume_get_pitch(volume, &map_desc->row_pitch, &map_desc->slice_pitch);
786 }
787
788 if (!box)
789 {
790 TRACE("No box supplied - all is ok\n");
791 map_desc->data = base_memory;
792 }
793 else
794 {
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);
797
798 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
799 {
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);
806 }
807 else
808 {
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);
813 }
814 }
815
816 if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
817 {
818 wined3d_texture_set_dirty(volume->container);
819 wined3d_volume_invalidate_location(volume, ~volume->resource.map_binding);
820 }
821
822 volume->resource.map_count++;
823
824 TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
825 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
826
827 return WINED3D_OK;
828 }
829
830 HRESULT wined3d_volume_unmap(struct wined3d_volume *volume)
831 {
832 TRACE("volume %p.\n", volume);
833
834 if (!volume->resource.map_count)
835 {
836 WARN("Trying to unlock an unlocked volume %p.\n", volume);
837 return WINED3DERR_INVALIDCALL;
838 }
839
840 if (volume->resource.map_binding == WINED3D_LOCATION_BUFFER)
841 {
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;
845
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");
850
851 context_release(context);
852 }
853
854 volume->resource.map_count--;
855
856 return WINED3D_OK;
857 #endif /* STAGING_CSMT */
858 }
859
860 static ULONG volume_resource_incref(struct wined3d_resource *resource)
861 {
862 struct wined3d_volume *volume = volume_from_resource(resource);
863 TRACE("Forwarding to container %p.\n", volume->container);
864
865 return wined3d_texture_incref(volume->container);
866 }
867
868 #if defined(STAGING_CSMT)
869 void wined3d_volume_cleanup_cs(struct wined3d_volume *volume)
870 {
871 HeapFree(GetProcessHeap(), 0, volume);
872 }
873
874 #endif /* STAGING_CSMT */
875 static ULONG volume_resource_decref(struct wined3d_resource *resource)
876 {
877 struct wined3d_volume *volume = volume_from_resource(resource);
878 TRACE("Forwarding to container %p.\n", volume->container);
879
880 return wined3d_texture_decref(volume->container);
881 }
882
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)
885 {
886 ERR("Not supported on sub-resources.\n");
887 return WINED3DERR_INVALIDCALL;
888 }
889
890 static HRESULT volume_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
891 {
892 ERR("Not supported on sub-resources.\n");
893 return WINED3DERR_INVALIDCALL;
894 }
895
896 #if defined(STAGING_CSMT)
897 static void wined3d_volume_location_invalidated(struct wined3d_resource *resource, DWORD location)
898 {
899 struct wined3d_volume *volume = volume_from_resource(resource);
900
901 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
902 wined3d_texture_set_dirty(volume->container);
903 }
904
905 #endif /* STAGING_CSMT */
906 static const struct wined3d_resource_ops volume_resource_ops =
907 {
908 volume_resource_incref,
909 volume_resource_decref,
910 volume_unload,
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 */
917 };
918
919 static HRESULT volume_init(struct wined3d_volume *volume, struct wined3d_texture *container,
920 const struct wined3d_resource_desc *desc, UINT level)
921 {
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);
925 HRESULT hr;
926 UINT size;
927
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))
932 {
933 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
934 return WINED3DERR_INVALIDCALL;
935 }
936
937 size = wined3d_format_calculate_size(format, device->surface_alignment, desc->width, desc->height, desc->depth);
938
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)))
942 {
943 WARN("Failed to initialize resource, returning %#x.\n", hr);
944 return hr;
945 }
946
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 */
953
954 if (desc->pool == WINED3D_POOL_DEFAULT && desc->usage & WINED3DUSAGE_DYNAMIC
955 && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
956 && !format->convert)
957 {
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 */
963 }
964
965 volume->container = container;
966
967 return WINED3D_OK;
968 }
969
970 HRESULT wined3d_volume_create(struct wined3d_texture *container, const struct wined3d_resource_desc *desc,
971 unsigned int level, struct wined3d_volume **volume)
972 {
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;
976 void *parent;
977 HRESULT hr;
978
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);
983
984 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
985 return E_OUTOFMEMORY;
986
987 if (FAILED(hr = volume_init(object, container, desc, level)))
988 {
989 WARN("Failed to initialize volume, returning %#x.\n", hr);
990 HeapFree(GetProcessHeap(), 0, object);
991 return hr;
992 }
993
994 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
995 container, level, &parent, &parent_ops)))
996 {
997 WARN("Failed to create volume parent, hr %#x.\n", hr);
998 wined3d_volume_destroy(object);
999 return hr;
1000 }
1001
1002 TRACE("Created volume %p, parent %p, parent_ops %p.\n", object, parent, parent_ops);
1003
1004 object->resource.parent = parent;
1005 object->resource.parent_ops = parent_ops;
1006 *volume = object;
1007
1008 return WINED3D_OK;
1009 }