Sync with trunk rev.61910 to get latest improvements and bugfixes.
[reactos.git] / 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 void volume_set_container(struct wined3d_volume *volume, struct wined3d_texture *container)
29 {
30 TRACE("volume %p, container %p.\n", volume, container);
31
32 volume->container = container;
33 }
34
35 static BOOL volume_prepare_system_memory(struct wined3d_volume *volume)
36 {
37 if (volume->resource.heap_memory)
38 return TRUE;
39
40 if (!wined3d_resource_allocate_sysmem(&volume->resource))
41 {
42 ERR("Failed to allocate system memory.\n");
43 return FALSE;
44 }
45 return TRUE;
46 }
47
48 /* Context activation is done by the caller. */
49 static void wined3d_volume_allocate_texture(struct wined3d_volume *volume,
50 const struct wined3d_context *context, BOOL srgb)
51 {
52 const struct wined3d_gl_info *gl_info = context->gl_info;
53 const struct wined3d_format *format = volume->resource.format;
54 void *mem = NULL;
55
56 if (gl_info->supported[APPLE_CLIENT_STORAGE] && !format->convert
57 && volume_prepare_system_memory(volume))
58 {
59 TRACE("Enabling GL_UNPACK_CLIENT_STORAGE_APPLE for volume %p\n", volume);
60 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
61 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
62 mem = volume->resource.heap_memory;
63 volume->flags |= WINED3D_VFLAG_CLIENT_STORAGE;
64 }
65
66 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, volume->texture_level,
67 srgb ? format->glGammaInternal : format->glInternal,
68 volume->resource.width, volume->resource.height, volume->resource.depth,
69 0, format->glFormat, format->glType, mem));
70 checkGLcall("glTexImage3D");
71
72 if (mem)
73 {
74 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
75 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
76 }
77 }
78
79 static void wined3d_volume_get_pitch(const struct wined3d_volume *volume, UINT *row_pitch,
80 UINT *slice_pitch)
81 {
82 const struct wined3d_format *format = volume->resource.format;
83
84 if (format->flags & WINED3DFMT_FLAG_BLOCKS)
85 {
86 /* Since compressed formats are block based, pitch means the amount of
87 * bytes to the next row of block rather than the next row of pixels. */
88 UINT row_block_count = (volume->resource.width + format->block_width - 1) / format->block_width;
89 UINT slice_block_count = (volume->resource.height + format->block_height - 1) / format->block_height;
90 *row_pitch = row_block_count * format->block_byte_count;
91 *slice_pitch = *row_pitch * slice_block_count;
92 }
93 else
94 {
95 unsigned char alignment = volume->resource.device->surface_alignment;
96 *row_pitch = format->byte_count * volume->resource.width; /* Bytes / row */
97 *row_pitch = (*row_pitch + alignment - 1) & ~(alignment - 1);
98 *slice_pitch = *row_pitch * volume->resource.height;
99 }
100
101 TRACE("Returning row pitch %u, slice pitch %u.\n", *row_pitch, *slice_pitch);
102 }
103
104 /* Context activation is done by the caller. */
105 void wined3d_volume_upload_data(struct wined3d_volume *volume, const struct wined3d_context *context,
106 const struct wined3d_bo_address *data)
107 {
108 const struct wined3d_gl_info *gl_info = context->gl_info;
109 const struct wined3d_format *format = volume->resource.format;
110 UINT width = volume->resource.width;
111 UINT height = volume->resource.height;
112 UINT depth = volume->resource.depth;
113 BYTE *mem = data->addr;
114
115 TRACE("volume %p, context %p, level %u, format %s (%#x).\n",
116 volume, context, volume->texture_level, debug_d3dformat(format->id),
117 format->id);
118
119 if (format->convert)
120 {
121 UINT dst_row_pitch, dst_slice_pitch;
122 UINT src_row_pitch, src_slice_pitch;
123 UINT alignment = volume->resource.device->surface_alignment;
124
125 if (data->buffer_object)
126 ERR("Loading a converted volume from a PBO.\n");
127 if (format->flags & WINED3DFMT_FLAG_BLOCKS)
128 ERR("Converting a block-based format.\n");
129
130 dst_row_pitch = width * format->conv_byte_count;
131 dst_row_pitch = (dst_row_pitch + alignment - 1) & ~(alignment - 1);
132 dst_slice_pitch = dst_row_pitch * height;
133
134 wined3d_volume_get_pitch(volume, &src_row_pitch, &src_slice_pitch);
135
136 mem = HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch * depth);
137 format->convert(data->addr, mem, src_row_pitch, src_slice_pitch,
138 dst_row_pitch, dst_slice_pitch, width, height, depth);
139 }
140
141 if (data->buffer_object)
142 {
143 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, data->buffer_object));
144 checkGLcall("glBindBufferARB");
145 }
146
147 GL_EXTCALL(glTexSubImage3DEXT(GL_TEXTURE_3D, volume->texture_level, 0, 0, 0,
148 width, height, depth,
149 format->glFormat, format->glType, mem));
150 checkGLcall("glTexSubImage3D");
151
152 if (data->buffer_object)
153 {
154 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
155 checkGLcall("glBindBufferARB");
156 }
157
158 if (mem != data->addr)
159 HeapFree(GetProcessHeap(), 0, mem);
160 }
161
162 static void wined3d_volume_validate_location(struct wined3d_volume *volume, DWORD location)
163 {
164 TRACE("Volume %p, setting %s.\n", volume, wined3d_debug_location(location));
165 volume->locations |= location;
166 TRACE("new location flags are %s.\n", wined3d_debug_location(volume->locations));
167 }
168
169 void wined3d_volume_invalidate_location(struct wined3d_volume *volume, DWORD location)
170 {
171 TRACE("Volume %p, clearing %s.\n", volume, wined3d_debug_location(location));
172 volume->locations &= ~location;
173 TRACE("new location flags are %s.\n", wined3d_debug_location(volume->locations));
174 }
175
176 /* Context activation is done by the caller. */
177 static void wined3d_volume_download_data(struct wined3d_volume *volume,
178 const struct wined3d_context *context, const struct wined3d_bo_address *data)
179 {
180 const struct wined3d_gl_info *gl_info = context->gl_info;
181 const struct wined3d_format *format = volume->resource.format;
182
183 if (format->convert)
184 {
185 FIXME("Attempting to download a converted volume, format %s.\n",
186 debug_d3dformat(format->id));
187 return;
188 }
189
190 if (data->buffer_object)
191 {
192 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, data->buffer_object));
193 checkGLcall("glBindBufferARB");
194 }
195
196 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, volume->texture_level,
197 format->glFormat, format->glType, data->addr);
198 checkGLcall("glGetTexImage");
199
200 if (data->buffer_object)
201 {
202 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
203 checkGLcall("glBindBufferARB");
204 }
205
206 }
207
208 static void wined3d_volume_evict_sysmem(struct wined3d_volume *volume)
209 {
210 wined3d_resource_free_sysmem(&volume->resource);
211 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_SYSMEM);
212 }
213
214 static DWORD volume_access_from_location(DWORD location)
215 {
216 switch (location)
217 {
218 case WINED3D_LOCATION_DISCARDED:
219 return 0;
220
221 case WINED3D_LOCATION_SYSMEM:
222 return WINED3D_RESOURCE_ACCESS_CPU;
223
224 case WINED3D_LOCATION_BUFFER:
225 case WINED3D_LOCATION_TEXTURE_RGB:
226 case WINED3D_LOCATION_TEXTURE_SRGB:
227 return WINED3D_RESOURCE_ACCESS_GPU;
228
229 default:
230 FIXME("Unhandled location %#x.\n", location);
231 return 0;
232 }
233 }
234
235 /* Context activation is done by the caller. */
236 static void wined3d_volume_srgb_transfer(struct wined3d_volume *volume,
237 struct wined3d_context *context, BOOL dest_is_srgb)
238 {
239 struct wined3d_bo_address data;
240 /* Optimizations are possible, but the effort should be put into either
241 * implementing EXT_SRGB_DECODE in the driver or finding out why we
242 * picked the wrong copy for the original upload and fixing that.
243 *
244 * Also keep in mind that we want to avoid using resource.heap_memory
245 * for DEFAULT pool surfaces. */
246
247 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
248 data.buffer_object = 0;
249 data.addr = HeapAlloc(GetProcessHeap(), 0, volume->resource.size);
250 if (!data.addr)
251 return;
252
253 wined3d_texture_bind_and_dirtify(volume->container, context, !dest_is_srgb);
254 wined3d_volume_download_data(volume, context, &data);
255 wined3d_texture_bind_and_dirtify(volume->container, context, dest_is_srgb);
256 wined3d_volume_upload_data(volume, context, &data);
257
258 HeapFree(GetProcessHeap(), 0, data.addr);
259 }
260
261 static BOOL wined3d_volume_can_evict(const struct wined3d_volume *volume)
262 {
263 if (volume->resource.pool != WINED3D_POOL_MANAGED)
264 return FALSE;
265 if (volume->download_count >= 10)
266 return FALSE;
267 if (volume->resource.format->convert)
268 return FALSE;
269 if (volume->flags & WINED3D_VFLAG_CLIENT_STORAGE)
270 return FALSE;
271
272 return TRUE;
273 }
274 /* Context activation is done by the caller. */
275 static void wined3d_volume_load_location(struct wined3d_volume *volume,
276 struct wined3d_context *context, DWORD location)
277 {
278 DWORD required_access = volume_access_from_location(location);
279
280 TRACE("Volume %p, loading %s, have %s.\n", volume, wined3d_debug_location(location),
281 wined3d_debug_location(volume->locations));
282
283 if ((volume->locations & location) == location)
284 {
285 TRACE("Location(s) already up to date.\n");
286 return;
287 }
288
289 if ((volume->resource.access_flags & required_access) != required_access)
290 {
291 ERR("Operation requires %#x access, but volume only has %#x.\n",
292 required_access, volume->resource.access_flags);
293 return;
294 }
295
296 switch (location)
297 {
298 case WINED3D_LOCATION_TEXTURE_RGB:
299 case WINED3D_LOCATION_TEXTURE_SRGB:
300 if ((location == WINED3D_LOCATION_TEXTURE_RGB
301 && !(volume->flags & WINED3D_VFLAG_ALLOCATED))
302 || (location == WINED3D_LOCATION_TEXTURE_SRGB
303 && !(volume->flags & WINED3D_VFLAG_SRGB_ALLOCATED)))
304 ERR("Trying to load (s)RGB texture without prior allocation.\n");
305
306 if (volume->locations & WINED3D_LOCATION_DISCARDED)
307 {
308 TRACE("Volume previously discarded, nothing to do.\n");
309 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
310 }
311 else if (volume->locations & WINED3D_LOCATION_SYSMEM)
312 {
313 struct wined3d_bo_address data = {0, volume->resource.heap_memory};
314 wined3d_volume_upload_data(volume, context, &data);
315 }
316 else if (volume->locations & WINED3D_LOCATION_BUFFER)
317 {
318 struct wined3d_bo_address data = {volume->pbo, NULL};
319 wined3d_volume_upload_data(volume, context, &data);
320 }
321 else if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
322 {
323 wined3d_volume_srgb_transfer(volume, context, TRUE);
324 }
325 else if (volume->locations & WINED3D_LOCATION_TEXTURE_SRGB)
326 {
327 wined3d_volume_srgb_transfer(volume, context, FALSE);
328 }
329 else
330 {
331 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(volume->locations));
332 return;
333 }
334 wined3d_volume_validate_location(volume, location);
335
336 if (wined3d_volume_can_evict(volume))
337 wined3d_volume_evict_sysmem(volume);
338
339 break;
340
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");
344
345 if (volume->locations & WINED3D_LOCATION_DISCARDED)
346 {
347 TRACE("Volume previously discarded, nothing to do.\n");
348 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
349 }
350 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
351 {
352 struct wined3d_bo_address data = {0, volume->resource.heap_memory};
353
354 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
355 wined3d_texture_bind_and_dirtify(volume->container, context, FALSE);
356 else
357 wined3d_texture_bind_and_dirtify(volume->container, context, TRUE);
358
359 volume->download_count++;
360 wined3d_volume_download_data(volume, context, &data);
361 }
362 else
363 {
364 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
365 wined3d_debug_location(volume->locations));
366 return;
367 }
368 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
369 break;
370
371 case WINED3D_LOCATION_BUFFER:
372 if (!volume->pbo || !(volume->flags & WINED3D_VFLAG_PBO))
373 ERR("Trying to load WINED3D_LOCATION_BUFFER without setting it up first.\n");
374
375 if (volume->locations & WINED3D_LOCATION_DISCARDED)
376 {
377 TRACE("Volume previously discarded, nothing to do.\n");
378 wined3d_volume_invalidate_location(volume, WINED3D_LOCATION_DISCARDED);
379 }
380 else if (volume->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
381 {
382 struct wined3d_bo_address data = {volume->pbo, NULL};
383
384 if (volume->locations & WINED3D_LOCATION_TEXTURE_RGB)
385 wined3d_texture_bind_and_dirtify(volume->container, context, FALSE);
386 else
387 wined3d_texture_bind_and_dirtify(volume->container, context, TRUE);
388
389 wined3d_volume_download_data(volume, context, &data);
390 }
391 else
392 {
393 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
394 wined3d_debug_location(volume->locations));
395 return;
396 }
397 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
398 break;
399
400 default:
401 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
402 wined3d_debug_location(volume->locations));
403 }
404 }
405
406 /* Context activation is done by the caller. */
407 void wined3d_volume_load(struct wined3d_volume *volume, struct wined3d_context *context, BOOL srgb_mode)
408 {
409 wined3d_texture_bind_and_dirtify(volume->container, context, srgb_mode);
410
411 if (srgb_mode)
412 {
413 if (!(volume->flags & WINED3D_VFLAG_SRGB_ALLOCATED))
414 {
415 wined3d_volume_allocate_texture(volume, context, TRUE);
416 volume->flags |= WINED3D_VFLAG_SRGB_ALLOCATED;
417 }
418
419 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_TEXTURE_SRGB);
420 }
421 else
422 {
423 if (!(volume->flags & WINED3D_VFLAG_ALLOCATED))
424 {
425 wined3d_volume_allocate_texture(volume, context, FALSE);
426 volume->flags |= WINED3D_VFLAG_ALLOCATED;
427 }
428
429 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_TEXTURE_RGB);
430 }
431 }
432
433 /* Context activation is done by the caller. */
434 static void wined3d_volume_prepare_pbo(struct wined3d_volume *volume, struct wined3d_context *context)
435 {
436 const struct wined3d_gl_info *gl_info = context->gl_info;
437
438 if (volume->pbo)
439 return;
440
441 GL_EXTCALL(glGenBuffersARB(1, &volume->pbo));
442 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, volume->pbo));
443 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, volume->resource.size, NULL, GL_STREAM_DRAW_ARB));
444 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
445 checkGLcall("Create PBO");
446
447 TRACE("Created PBO %u for volume %p.\n", volume->pbo, volume);
448 }
449
450 static void wined3d_volume_free_pbo(struct wined3d_volume *volume)
451 {
452 struct wined3d_context *context = context_acquire(volume->resource.device, NULL);
453 const struct wined3d_gl_info *gl_info = context->gl_info;
454
455 TRACE("Deleting PBO %u belonging to volume %p.\n", volume->pbo, volume);
456 GL_EXTCALL(glDeleteBuffersARB(1, &volume->pbo));
457 checkGLcall("glDeleteBuffersARB");
458 volume->pbo = 0;
459 context_release(context);
460 }
461
462 static void volume_unload(struct wined3d_resource *resource)
463 {
464 struct wined3d_volume *volume = volume_from_resource(resource);
465 struct wined3d_device *device = volume->resource.device;
466 struct wined3d_context *context;
467
468 if (volume->resource.pool == WINED3D_POOL_DEFAULT)
469 ERR("Unloading DEFAULT pool volume.\n");
470
471 TRACE("texture %p.\n", resource);
472
473 if (volume_prepare_system_memory(volume))
474 {
475 context = context_acquire(device, NULL);
476 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
477 context_release(context);
478 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_SYSMEM);
479 }
480 else
481 {
482 ERR("Out of memory when unloading volume %p.\n", volume);
483 wined3d_volume_validate_location(volume, WINED3D_LOCATION_DISCARDED);
484 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_DISCARDED);
485 }
486
487 if (volume->pbo)
488 {
489 /* Should not happen because only dynamic default pool volumes
490 * have a buffer, and those are not evicted by device_evit_managed_resources
491 * and must be freed before a non-ex device reset. */
492 ERR("Unloading a volume with a buffer\n");
493 wined3d_volume_free_pbo(volume);
494 }
495
496 /* The texture name is managed by the container. */
497 volume->flags &= ~(WINED3D_VFLAG_ALLOCATED | WINED3D_VFLAG_SRGB_ALLOCATED
498 | WINED3D_VFLAG_CLIENT_STORAGE);
499
500 resource_unload(resource);
501 }
502
503 ULONG CDECL wined3d_volume_incref(struct wined3d_volume *volume)
504 {
505 ULONG refcount;
506
507 if (volume->container)
508 {
509 TRACE("Forwarding to container %p.\n", volume->container);
510 return wined3d_texture_incref(volume->container);
511 }
512
513 refcount = InterlockedIncrement(&volume->resource.ref);
514
515 TRACE("%p increasing refcount to %u.\n", volume, refcount);
516
517 return refcount;
518 }
519
520 ULONG CDECL wined3d_volume_decref(struct wined3d_volume *volume)
521 {
522 ULONG refcount;
523
524 if (volume->container)
525 {
526 TRACE("Forwarding to container %p.\n", volume->container);
527 return wined3d_texture_decref(volume->container);
528 }
529
530 refcount = InterlockedDecrement(&volume->resource.ref);
531
532 TRACE("%p decreasing refcount to %u.\n", volume, refcount);
533
534 if (!refcount)
535 {
536 if (volume->pbo)
537 wined3d_volume_free_pbo(volume);
538
539 resource_cleanup(&volume->resource);
540 volume->resource.parent_ops->wined3d_object_destroyed(volume->resource.parent);
541 HeapFree(GetProcessHeap(), 0, volume);
542 }
543
544 return refcount;
545 }
546
547 void * CDECL wined3d_volume_get_parent(const struct wined3d_volume *volume)
548 {
549 TRACE("volume %p.\n", volume);
550
551 return volume->resource.parent;
552 }
553
554 DWORD CDECL wined3d_volume_set_priority(struct wined3d_volume *volume, DWORD priority)
555 {
556 return resource_set_priority(&volume->resource, priority);
557 }
558
559 DWORD CDECL wined3d_volume_get_priority(const struct wined3d_volume *volume)
560 {
561 return resource_get_priority(&volume->resource);
562 }
563
564 void CDECL wined3d_volume_preload(struct wined3d_volume *volume)
565 {
566 FIXME("volume %p stub!\n", volume);
567 }
568
569 struct wined3d_resource * CDECL wined3d_volume_get_resource(struct wined3d_volume *volume)
570 {
571 TRACE("volume %p.\n", volume);
572
573 return &volume->resource;
574 }
575
576 static BOOL volume_check_block_align(const struct wined3d_volume *volume,
577 const struct wined3d_box *box)
578 {
579 UINT width_mask, height_mask;
580 const struct wined3d_format *format = volume->resource.format;
581
582 if (!box)
583 return TRUE;
584
585 /* This assumes power of two block sizes, but NPOT block sizes would be
586 * silly anyway.
587 *
588 * This also assumes that the format's block depth is 1. */
589 width_mask = format->block_width - 1;
590 height_mask = format->block_height - 1;
591
592 if (box->left & width_mask)
593 return FALSE;
594 if (box->top & height_mask)
595 return FALSE;
596 if (box->right & width_mask && box->right != volume->resource.width)
597 return FALSE;
598 if (box->bottom & height_mask && box->bottom != volume->resource.height)
599 return FALSE;
600
601 return TRUE;
602 }
603
604 static BOOL wined3d_volume_check_box_dimensions(const struct wined3d_volume *volume,
605 const struct wined3d_box *box)
606 {
607 if (!box)
608 return TRUE;
609
610 if (box->left >= box->right)
611 return FALSE;
612 if (box->top >= box->bottom)
613 return FALSE;
614 if (box->front >= box->back)
615 return FALSE;
616 if (box->right > volume->resource.width)
617 return FALSE;
618 if (box->bottom > volume->resource.height)
619 return FALSE;
620 if (box->back > volume->resource.depth)
621 return FALSE;
622
623 return TRUE;
624 }
625
626 HRESULT CDECL wined3d_volume_map(struct wined3d_volume *volume,
627 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
628 {
629 struct wined3d_device *device = volume->resource.device;
630 struct wined3d_context *context;
631 const struct wined3d_gl_info *gl_info;
632 BYTE *base_memory;
633 const struct wined3d_format *format = volume->resource.format;
634
635 TRACE("volume %p, map_desc %p, box %p, flags %#x.\n",
636 volume, map_desc, box, flags);
637
638 map_desc->data = NULL;
639 if (!(volume->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU))
640 {
641 WARN("Volume %p is not CPU accessible.\n", volume);
642 return WINED3DERR_INVALIDCALL;
643 }
644 if (volume->resource.map_count)
645 {
646 WARN("Volume is already mapped.\n");
647 return WINED3DERR_INVALIDCALL;
648 }
649 if (!wined3d_volume_check_box_dimensions(volume, box))
650 {
651 WARN("Map box is invalid.\n");
652 return WINED3DERR_INVALIDCALL;
653 }
654 if ((format->flags & WINED3DFMT_FLAG_BLOCKS) && !volume_check_block_align(volume, box))
655 {
656 WARN("Map box is misaligned for %ux%u blocks.\n",
657 format->block_width, format->block_height);
658 return WINED3DERR_INVALIDCALL;
659 }
660
661 flags = wined3d_resource_sanitize_map_flags(&volume->resource, flags);
662
663 if (volume->flags & WINED3D_VFLAG_PBO)
664 {
665 context = context_acquire(device, NULL);
666 gl_info = context->gl_info;
667
668 wined3d_volume_prepare_pbo(volume, context);
669 if (flags & WINED3D_MAP_DISCARD)
670 wined3d_volume_validate_location(volume, WINED3D_LOCATION_BUFFER);
671 else
672 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_BUFFER);
673
674 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, volume->pbo));
675
676 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
677 {
678 GLbitfield mapflags = wined3d_resource_gl_map_flags(flags);
679 mapflags &= ~GL_MAP_FLUSH_EXPLICIT_BIT;
680 base_memory = GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER_ARB,
681 0, volume->resource.size, mapflags));
682 }
683 else
684 {
685 GLenum access = wined3d_resource_gl_legacy_map_flags(flags);
686 base_memory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, access));
687 }
688
689 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
690 checkGLcall("Map PBO");
691
692 context_release(context);
693 }
694 else
695 {
696 if (!volume_prepare_system_memory(volume))
697 {
698 WARN("Out of memory.\n");
699 map_desc->data = NULL;
700 return E_OUTOFMEMORY;
701 }
702
703 if (flags & WINED3D_MAP_DISCARD)
704 {
705 wined3d_volume_validate_location(volume, WINED3D_LOCATION_SYSMEM);
706 }
707 else if (!(volume->locations & WINED3D_LOCATION_SYSMEM))
708 {
709 context = context_acquire(device, NULL);
710 wined3d_volume_load_location(volume, context, WINED3D_LOCATION_SYSMEM);
711 context_release(context);
712 }
713 base_memory = volume->resource.heap_memory;
714 }
715
716 TRACE("Base memory pointer %p.\n", base_memory);
717
718 if (format->flags & WINED3DFMT_FLAG_BROKEN_PITCH)
719 {
720 map_desc->row_pitch = volume->resource.width * format->byte_count;
721 map_desc->slice_pitch = map_desc->row_pitch * volume->resource.height;
722 }
723 else
724 {
725 wined3d_volume_get_pitch(volume, &map_desc->row_pitch, &map_desc->slice_pitch);
726 }
727
728 if (!box)
729 {
730 TRACE("No box supplied - all is ok\n");
731 map_desc->data = base_memory;
732 }
733 else
734 {
735 TRACE("Lock Box (%p) = l %u, t %u, r %u, b %u, fr %u, ba %u\n",
736 box, box->left, box->top, box->right, box->bottom, box->front, box->back);
737
738 if ((format->flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
739 {
740 /* Compressed textures are block based, so calculate the offset of
741 * the block that contains the top-left pixel of the locked rectangle. */
742 map_desc->data = base_memory
743 + (box->front * map_desc->slice_pitch)
744 + ((box->top / format->block_height) * map_desc->row_pitch)
745 + ((box->left / format->block_width) * format->block_byte_count);
746 }
747 else
748 {
749 map_desc->data = base_memory
750 + (map_desc->slice_pitch * box->front)
751 + (map_desc->row_pitch * box->top)
752 + (box->left * volume->resource.format->byte_count);
753 }
754 }
755
756 if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
757 {
758 wined3d_texture_set_dirty(volume->container);
759
760 if (volume->flags & WINED3D_VFLAG_PBO)
761 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_BUFFER);
762 else
763 wined3d_volume_invalidate_location(volume, ~WINED3D_LOCATION_SYSMEM);
764 }
765
766 volume->resource.map_count++;
767
768 TRACE("Returning memory %p, row pitch %d, slice pitch %d.\n",
769 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
770
771 return WINED3D_OK;
772 }
773
774 struct wined3d_volume * CDECL wined3d_volume_from_resource(struct wined3d_resource *resource)
775 {
776 return volume_from_resource(resource);
777 }
778
779 HRESULT CDECL wined3d_volume_unmap(struct wined3d_volume *volume)
780 {
781 TRACE("volume %p.\n", volume);
782
783 if (!volume->resource.map_count)
784 {
785 WARN("Trying to unlock an unlocked volume %p.\n", volume);
786 return WINED3DERR_INVALIDCALL;
787 }
788
789 if (volume->flags & WINED3D_VFLAG_PBO)
790 {
791 struct wined3d_device *device = volume->resource.device;
792 struct wined3d_context *context = context_acquire(device, NULL);
793 const struct wined3d_gl_info *gl_info = context->gl_info;
794
795 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, volume->pbo));
796 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
797 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
798 checkGLcall("Unmap PBO");
799
800 context_release(context);
801 }
802
803 volume->resource.map_count--;
804
805 return WINED3D_OK;
806 }
807
808 static const struct wined3d_resource_ops volume_resource_ops =
809 {
810 volume_unload,
811 };
812
813 static HRESULT volume_init(struct wined3d_volume *volume, struct wined3d_texture *container,
814 const struct wined3d_resource_desc *desc, UINT level)
815 {
816 struct wined3d_device *device = container->resource.device;
817 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
818 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format);
819 HRESULT hr;
820 UINT size;
821
822 if (!gl_info->supported[EXT_TEXTURE3D])
823 {
824 WARN("Volume cannot be created - no volume texture support.\n");
825 return WINED3DERR_INVALIDCALL;
826 }
827 /* TODO: Write tests for other resources and move this check
828 * to resource_init, if applicable. */
829 if (desc->usage & WINED3DUSAGE_DYNAMIC
830 && (desc->pool == WINED3D_POOL_MANAGED || desc->pool == WINED3D_POOL_SCRATCH))
831 {
832 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
833 return WINED3DERR_INVALIDCALL;
834 }
835
836 size = wined3d_format_calculate_size(format, device->surface_alignment, desc->width, desc->height, desc->depth);
837
838 if (FAILED(hr = resource_init(&volume->resource, device, WINED3D_RTYPE_VOLUME, format,
839 WINED3D_MULTISAMPLE_NONE, 0, desc->usage, desc->pool, desc->width, desc->height, desc->depth,
840 size, NULL, &wined3d_null_parent_ops, &volume_resource_ops)))
841 {
842 WARN("Failed to initialize resource, returning %#x.\n", hr);
843 return hr;
844 }
845
846 volume->texture_level = level;
847 volume->locations = WINED3D_LOCATION_DISCARDED;
848
849 if (desc->pool == WINED3D_POOL_DEFAULT && desc->usage & WINED3DUSAGE_DYNAMIC
850 && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
851 && !format->convert)
852 {
853 wined3d_resource_free_sysmem(&volume->resource);
854 volume->flags |= WINED3D_VFLAG_PBO;
855 }
856
857 volume_set_container(volume, container);
858
859 return WINED3D_OK;
860 }
861
862 HRESULT wined3d_volume_create(struct wined3d_texture *container, const struct wined3d_resource_desc *desc,
863 unsigned int level, struct wined3d_volume **volume)
864 {
865 struct wined3d_device_parent *device_parent = container->resource.device->device_parent;
866 const struct wined3d_parent_ops *parent_ops;
867 struct wined3d_volume *object;
868 void *parent;
869 HRESULT hr;
870
871 TRACE("container %p, width %u, height %u, depth %u, level %u, format %s, "
872 "usage %#x, pool %s, volume %p.\n",
873 container, desc->width, desc->height, desc->depth, level, debug_d3dformat(desc->format),
874 desc->usage, debug_d3dpool(desc->pool), volume);
875
876 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
877 return E_OUTOFMEMORY;
878
879 if (FAILED(hr = volume_init(object, container, desc, level)))
880 {
881 WARN("Failed to initialize volume, returning %#x.\n", hr);
882 HeapFree(GetProcessHeap(), 0, object);
883 return hr;
884 }
885
886 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
887 wined3d_texture_get_parent(container), object, &parent, &parent_ops)))
888 {
889 WARN("Failed to create volume parent, hr %#x.\n", hr);
890 volume_set_container(object, NULL);
891 wined3d_volume_decref(object);
892 return hr;
893 }
894
895 TRACE("Created volume %p, parent %p, parent_ops %p.\n", object, parent, parent_ops);
896
897 object->resource.parent = parent;
898 object->resource.parent_ops = parent_ops;
899 *volume = object;
900
901 return WINED3D_OK;
902 }