cbeaf3fd0baa5f5882708a98a56716e04cbac0f6
[mesa.git] / src / gallium / drivers / llvmpipe / lp_texture.c
1 /**************************************************************************
2 *
3 * Copyright 2006 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keithw@vmware.com>
30 * Michel Dänzer <daenzer@vmware.com>
31 */
32
33 #include <stdio.h>
34
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
37
38 #include "util/u_inlines.h"
39 #include "util/u_cpu_detect.h"
40 #include "util/format/u_format.h"
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43 #include "util/simple_list.h"
44 #include "util/u_transfer.h"
45
46 #include "lp_context.h"
47 #include "lp_flush.h"
48 #include "lp_screen.h"
49 #include "lp_texture.h"
50 #include "lp_setup.h"
51 #include "lp_state.h"
52 #include "lp_rast.h"
53
54 #include "frontend/sw_winsys.h"
55
56
57 #ifdef DEBUG
58 static struct llvmpipe_resource resource_list;
59 #endif
60 static unsigned id_counter = 0;
61
62
63 /**
64 * Conventional allocation path for non-display textures:
65 * Compute strides and allocate data (unless asked not to).
66 */
67 static boolean
68 llvmpipe_texture_layout(struct llvmpipe_screen *screen,
69 struct llvmpipe_resource *lpr,
70 boolean allocate)
71 {
72 struct pipe_resource *pt = &lpr->base;
73 unsigned level;
74 unsigned width = pt->width0;
75 unsigned height = pt->height0;
76 unsigned depth = pt->depth0;
77 uint64_t total_size = 0;
78 unsigned layers = pt->array_size;
79 unsigned num_samples = util_res_sample_count(pt);
80
81 /* XXX:
82 * This alignment here (same for displaytarget) was added for the purpose of
83 * ARB_map_buffer_alignment. I am not convinced it's needed for non-buffer
84 * resources. Otherwise we'd want the max of cacheline size and 16 (max size
85 * of a block for all formats) though this should not be strictly necessary
86 * neither. In any case it can only affect compressed or 1d textures.
87 */
88 unsigned mip_align = MAX2(64, util_cpu_caps.cacheline);
89
90 assert(LP_MAX_TEXTURE_2D_LEVELS <= LP_MAX_TEXTURE_LEVELS);
91 assert(LP_MAX_TEXTURE_3D_LEVELS <= LP_MAX_TEXTURE_LEVELS);
92
93 for (level = 0; level <= pt->last_level; level++) {
94 uint64_t mipsize;
95 unsigned align_x, align_y, nblocksx, nblocksy, block_size, num_slices;
96
97 /* Row stride and image stride */
98
99 /* For non-compressed formats we need 4x4 pixel alignment
100 * so we can read/write LP_RASTER_BLOCK_SIZE when rendering to them.
101 * We also want cache line size in x direction,
102 * otherwise same cache line could end up in multiple threads.
103 * For explicit 1d resources however we reduce this to 4x1 and
104 * handle specially in render output code (as we need to do special
105 * handling there for buffers in any case).
106 */
107 if (util_format_is_compressed(pt->format))
108 align_x = align_y = 1;
109 else {
110 align_x = LP_RASTER_BLOCK_SIZE;
111 if (llvmpipe_resource_is_1d(&lpr->base))
112 align_y = 1;
113 else
114 align_y = LP_RASTER_BLOCK_SIZE;
115 }
116
117 nblocksx = util_format_get_nblocksx(pt->format,
118 align(width, align_x));
119 nblocksy = util_format_get_nblocksy(pt->format,
120 align(height, align_y));
121 block_size = util_format_get_blocksize(pt->format);
122
123 if (util_format_is_compressed(pt->format))
124 lpr->row_stride[level] = nblocksx * block_size;
125 else
126 lpr->row_stride[level] = align(nblocksx * block_size, util_cpu_caps.cacheline);
127
128 /* if row_stride * height > LP_MAX_TEXTURE_SIZE */
129 if ((uint64_t)lpr->row_stride[level] * nblocksy > LP_MAX_TEXTURE_SIZE) {
130 /* image too large */
131 goto fail;
132 }
133
134 lpr->img_stride[level] = lpr->row_stride[level] * nblocksy;
135
136 /* Number of 3D image slices, cube faces or texture array layers */
137 if (lpr->base.target == PIPE_TEXTURE_CUBE) {
138 assert(layers == 6);
139 }
140
141 if (lpr->base.target == PIPE_TEXTURE_3D)
142 num_slices = depth;
143 else if (lpr->base.target == PIPE_TEXTURE_1D_ARRAY ||
144 lpr->base.target == PIPE_TEXTURE_2D_ARRAY ||
145 lpr->base.target == PIPE_TEXTURE_CUBE ||
146 lpr->base.target == PIPE_TEXTURE_CUBE_ARRAY)
147 num_slices = layers;
148 else
149 num_slices = 1;
150
151 /* if img_stride * num_slices_faces > LP_MAX_TEXTURE_SIZE */
152 mipsize = (uint64_t)lpr->img_stride[level] * num_slices;
153 if (mipsize > LP_MAX_TEXTURE_SIZE) {
154 /* volume too large */
155 goto fail;
156 }
157
158 lpr->mip_offsets[level] = total_size;
159
160 total_size += align((unsigned)mipsize, mip_align);
161 if (total_size > LP_MAX_TEXTURE_SIZE) {
162 goto fail;
163 }
164
165 /* Compute size of next mipmap level */
166 width = u_minify(width, 1);
167 height = u_minify(height, 1);
168 depth = u_minify(depth, 1);
169 }
170
171 lpr->sample_stride = total_size;
172 total_size *= num_samples;
173
174 if (allocate) {
175 lpr->tex_data = align_malloc(total_size, mip_align);
176 if (!lpr->tex_data) {
177 return FALSE;
178 }
179 else {
180 memset(lpr->tex_data, 0, total_size);
181 }
182 }
183
184 return TRUE;
185
186 fail:
187 return FALSE;
188 }
189
190
191 /**
192 * Check the size of the texture specified by 'res'.
193 * \return TRUE if OK, FALSE if too large.
194 */
195 static bool
196 llvmpipe_can_create_resource(struct pipe_screen *screen,
197 const struct pipe_resource *res)
198 {
199 struct llvmpipe_resource lpr;
200 memset(&lpr, 0, sizeof(lpr));
201 lpr.base = *res;
202 return llvmpipe_texture_layout(llvmpipe_screen(screen), &lpr, false);
203 }
204
205
206 static boolean
207 llvmpipe_displaytarget_layout(struct llvmpipe_screen *screen,
208 struct llvmpipe_resource *lpr,
209 const void *map_front_private)
210 {
211 struct sw_winsys *winsys = screen->winsys;
212
213 /* Round up the surface size to a multiple of the tile size to
214 * avoid tile clipping.
215 */
216 const unsigned width = MAX2(1, align(lpr->base.width0, TILE_SIZE));
217 const unsigned height = MAX2(1, align(lpr->base.height0, TILE_SIZE));
218
219 lpr->dt = winsys->displaytarget_create(winsys,
220 lpr->base.bind,
221 lpr->base.format,
222 width, height,
223 64,
224 map_front_private,
225 &lpr->row_stride[0] );
226
227 if (lpr->dt == NULL)
228 return FALSE;
229
230 if (!map_front_private) {
231 void *map = winsys->displaytarget_map(winsys, lpr->dt,
232 PIPE_TRANSFER_WRITE);
233
234 if (map)
235 memset(map, 0, height * lpr->row_stride[0]);
236
237 winsys->displaytarget_unmap(winsys, lpr->dt);
238 }
239
240 return TRUE;
241 }
242
243
244 static struct pipe_resource *
245 llvmpipe_resource_create_front(struct pipe_screen *_screen,
246 const struct pipe_resource *templat,
247 const void *map_front_private)
248 {
249 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
250 struct llvmpipe_resource *lpr = CALLOC_STRUCT(llvmpipe_resource);
251 if (!lpr)
252 return NULL;
253
254 lpr->base = *templat;
255 pipe_reference_init(&lpr->base.reference, 1);
256 lpr->base.screen = &screen->base;
257
258 /* assert(lpr->base.bind); */
259
260 if (llvmpipe_resource_is_texture(&lpr->base)) {
261 if (lpr->base.bind & (PIPE_BIND_DISPLAY_TARGET |
262 PIPE_BIND_SCANOUT |
263 PIPE_BIND_SHARED)) {
264 /* displayable surface */
265 if (!llvmpipe_displaytarget_layout(screen, lpr, map_front_private))
266 goto fail;
267 }
268 else {
269 /* texture map */
270 if (!llvmpipe_texture_layout(screen, lpr, true))
271 goto fail;
272 }
273 }
274 else {
275 /* other data (vertex buffer, const buffer, etc) */
276 const uint bytes = templat->width0;
277 assert(util_format_get_blocksize(templat->format) == 1);
278 assert(templat->height0 == 1);
279 assert(templat->depth0 == 1);
280 assert(templat->last_level == 0);
281 /*
282 * Reserve some extra storage since if we'd render to a buffer we
283 * read/write always LP_RASTER_BLOCK_SIZE pixels, but the element
284 * offset doesn't need to be aligned to LP_RASTER_BLOCK_SIZE.
285 */
286 lpr->data = align_malloc(bytes + (LP_RASTER_BLOCK_SIZE - 1) * 4 * sizeof(float), 64);
287
288 /*
289 * buffers don't really have stride but it's probably safer
290 * (for code doing same calculations for buffers and textures)
291 * to put something sane in there.
292 */
293 lpr->row_stride[0] = bytes;
294 if (!lpr->data)
295 goto fail;
296 memset(lpr->data, 0, bytes);
297 }
298
299 lpr->id = id_counter++;
300
301 #ifdef DEBUG
302 insert_at_tail(&resource_list, lpr);
303 #endif
304
305 return &lpr->base;
306
307 fail:
308 FREE(lpr);
309 return NULL;
310 }
311
312
313 static struct pipe_resource *
314 llvmpipe_resource_create(struct pipe_screen *_screen,
315 const struct pipe_resource *templat)
316 {
317 return llvmpipe_resource_create_front(_screen, templat, NULL);
318 }
319
320
321 static void
322 llvmpipe_resource_destroy(struct pipe_screen *pscreen,
323 struct pipe_resource *pt)
324 {
325 struct llvmpipe_screen *screen = llvmpipe_screen(pscreen);
326 struct llvmpipe_resource *lpr = llvmpipe_resource(pt);
327
328 if (lpr->dt) {
329 /* display target */
330 struct sw_winsys *winsys = screen->winsys;
331 winsys->displaytarget_destroy(winsys, lpr->dt);
332 }
333 else if (llvmpipe_resource_is_texture(pt)) {
334 /* free linear image data */
335 if (lpr->tex_data) {
336 align_free(lpr->tex_data);
337 lpr->tex_data = NULL;
338 }
339 }
340 else if (!lpr->userBuffer) {
341 assert(lpr->data);
342 align_free(lpr->data);
343 }
344
345 #ifdef DEBUG
346 if (lpr->next)
347 remove_from_list(lpr);
348 #endif
349
350 FREE(lpr);
351 }
352
353
354 /**
355 * Map a resource for read/write.
356 */
357 void *
358 llvmpipe_resource_map(struct pipe_resource *resource,
359 unsigned level,
360 unsigned layer,
361 enum lp_texture_usage tex_usage)
362 {
363 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
364 uint8_t *map;
365
366 assert(level < LP_MAX_TEXTURE_LEVELS);
367 assert(layer < (u_minify(resource->depth0, level) + resource->array_size - 1));
368
369 assert(tex_usage == LP_TEX_USAGE_READ ||
370 tex_usage == LP_TEX_USAGE_READ_WRITE ||
371 tex_usage == LP_TEX_USAGE_WRITE_ALL);
372
373 if (lpr->dt) {
374 /* display target */
375 struct llvmpipe_screen *screen = llvmpipe_screen(resource->screen);
376 struct sw_winsys *winsys = screen->winsys;
377 unsigned dt_usage;
378
379 if (tex_usage == LP_TEX_USAGE_READ) {
380 dt_usage = PIPE_TRANSFER_READ;
381 }
382 else {
383 dt_usage = PIPE_TRANSFER_READ_WRITE;
384 }
385
386 assert(level == 0);
387 assert(layer == 0);
388
389 /* FIXME: keep map count? */
390 map = winsys->displaytarget_map(winsys, lpr->dt, dt_usage);
391
392 /* install this linear image in texture data structure */
393 lpr->tex_data = map;
394
395 return map;
396 }
397 else if (llvmpipe_resource_is_texture(resource)) {
398
399 map = llvmpipe_get_texture_image_address(lpr, layer, level);
400 return map;
401 }
402 else {
403 return lpr->data;
404 }
405 }
406
407
408 /**
409 * Unmap a resource.
410 */
411 void
412 llvmpipe_resource_unmap(struct pipe_resource *resource,
413 unsigned level,
414 unsigned layer)
415 {
416 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
417
418 if (lpr->dt) {
419 /* display target */
420 struct llvmpipe_screen *lp_screen = llvmpipe_screen(resource->screen);
421 struct sw_winsys *winsys = lp_screen->winsys;
422
423 assert(level == 0);
424 assert(layer == 0);
425
426 winsys->displaytarget_unmap(winsys, lpr->dt);
427 }
428 }
429
430
431 void *
432 llvmpipe_resource_data(struct pipe_resource *resource)
433 {
434 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
435
436 assert(!llvmpipe_resource_is_texture(resource));
437
438 return lpr->data;
439 }
440
441
442 static struct pipe_resource *
443 llvmpipe_resource_from_handle(struct pipe_screen *screen,
444 const struct pipe_resource *template,
445 struct winsys_handle *whandle,
446 unsigned usage)
447 {
448 struct sw_winsys *winsys = llvmpipe_screen(screen)->winsys;
449 struct llvmpipe_resource *lpr;
450
451 /* XXX Seems like from_handled depth textures doesn't work that well */
452
453 lpr = CALLOC_STRUCT(llvmpipe_resource);
454 if (!lpr) {
455 goto no_lpr;
456 }
457
458 lpr->base = *template;
459 pipe_reference_init(&lpr->base.reference, 1);
460 lpr->base.screen = screen;
461
462 /*
463 * Looks like unaligned displaytargets work just fine,
464 * at least sampler/render ones.
465 */
466 #if 0
467 assert(lpr->base.width0 == width);
468 assert(lpr->base.height0 == height);
469 #endif
470
471 lpr->dt = winsys->displaytarget_from_handle(winsys,
472 template,
473 whandle,
474 &lpr->row_stride[0]);
475 if (!lpr->dt) {
476 goto no_dt;
477 }
478
479 lpr->id = id_counter++;
480
481 #ifdef DEBUG
482 insert_at_tail(&resource_list, lpr);
483 #endif
484
485 return &lpr->base;
486
487 no_dt:
488 FREE(lpr);
489 no_lpr:
490 return NULL;
491 }
492
493
494 static bool
495 llvmpipe_resource_get_handle(struct pipe_screen *screen,
496 struct pipe_context *ctx,
497 struct pipe_resource *pt,
498 struct winsys_handle *whandle,
499 unsigned usage)
500 {
501 struct sw_winsys *winsys = llvmpipe_screen(screen)->winsys;
502 struct llvmpipe_resource *lpr = llvmpipe_resource(pt);
503
504 assert(lpr->dt);
505 if (!lpr->dt)
506 return false;
507
508 return winsys->displaytarget_get_handle(winsys, lpr->dt, whandle);
509 }
510
511
512 void *
513 llvmpipe_transfer_map_ms( struct pipe_context *pipe,
514 struct pipe_resource *resource,
515 unsigned level,
516 unsigned usage,
517 unsigned sample,
518 const struct pipe_box *box,
519 struct pipe_transfer **transfer )
520 {
521 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
522 struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);
523 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
524 struct llvmpipe_transfer *lpt;
525 struct pipe_transfer *pt;
526 ubyte *map;
527 enum pipe_format format;
528 enum lp_texture_usage tex_usage;
529 const char *mode;
530
531 assert(resource);
532 assert(level <= resource->last_level);
533
534 /*
535 * Transfers, like other pipe operations, must happen in order, so flush the
536 * context if necessary.
537 */
538 if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
539 boolean read_only = !(usage & PIPE_TRANSFER_WRITE);
540 boolean do_not_block = !!(usage & PIPE_TRANSFER_DONTBLOCK);
541 if (!llvmpipe_flush_resource(pipe, resource,
542 level,
543 read_only,
544 TRUE, /* cpu_access */
545 do_not_block,
546 __FUNCTION__)) {
547 /*
548 * It would have blocked, but state tracker requested no to.
549 */
550 assert(do_not_block);
551 return NULL;
552 }
553 }
554
555 /* Check if we're mapping a current constant buffer */
556 if ((usage & PIPE_TRANSFER_WRITE) &&
557 (resource->bind & PIPE_BIND_CONSTANT_BUFFER)) {
558 unsigned i;
559 for (i = 0; i < ARRAY_SIZE(llvmpipe->constants[PIPE_SHADER_FRAGMENT]); ++i) {
560 if (resource == llvmpipe->constants[PIPE_SHADER_FRAGMENT][i].buffer) {
561 /* constants may have changed */
562 llvmpipe->dirty |= LP_NEW_FS_CONSTANTS;
563 break;
564 }
565 }
566 }
567
568 lpt = CALLOC_STRUCT(llvmpipe_transfer);
569 if (!lpt)
570 return NULL;
571 pt = &lpt->base;
572 pipe_resource_reference(&pt->resource, resource);
573 pt->box = *box;
574 pt->level = level;
575 pt->stride = lpr->row_stride[level];
576 pt->layer_stride = lpr->img_stride[level];
577 pt->usage = usage;
578 *transfer = pt;
579
580 assert(level < LP_MAX_TEXTURE_LEVELS);
581
582 /*
583 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
584 transfer->x, transfer->y, transfer->width, transfer->height,
585 transfer->texture->width0,
586 transfer->texture->height0,
587 transfer->usage);
588 */
589
590 if (usage == PIPE_TRANSFER_READ) {
591 tex_usage = LP_TEX_USAGE_READ;
592 mode = "read";
593 }
594 else {
595 tex_usage = LP_TEX_USAGE_READ_WRITE;
596 mode = "read/write";
597 }
598
599 if (0) {
600 printf("transfer map tex %u mode %s\n", lpr->id, mode);
601 }
602
603 format = lpr->base.format;
604
605 map = llvmpipe_resource_map(resource,
606 level,
607 box->z,
608 tex_usage);
609
610
611 /* May want to do different things here depending on read/write nature
612 * of the map:
613 */
614 if (usage & PIPE_TRANSFER_WRITE) {
615 /* Do something to notify sharing contexts of a texture change.
616 */
617 screen->timestamp++;
618 }
619
620 map +=
621 box->y / util_format_get_blockheight(format) * pt->stride +
622 box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
623
624 map += sample * lpr->sample_stride;
625 return map;
626 }
627
628 static void *
629 llvmpipe_transfer_map( struct pipe_context *pipe,
630 struct pipe_resource *resource,
631 unsigned level,
632 unsigned usage,
633 const struct pipe_box *box,
634 struct pipe_transfer **transfer )
635 {
636 return llvmpipe_transfer_map_ms(pipe, resource, level, usage, 0, box, transfer);
637 }
638
639 static void
640 llvmpipe_transfer_unmap(struct pipe_context *pipe,
641 struct pipe_transfer *transfer)
642 {
643 assert(transfer->resource);
644
645 llvmpipe_resource_unmap(transfer->resource,
646 transfer->level,
647 transfer->box.z);
648
649 /* Effectively do the texture_update work here - if texture images
650 * needed post-processing to put them into hardware layout, this is
651 * where it would happen. For llvmpipe, nothing to do.
652 */
653 assert (transfer->resource);
654 pipe_resource_reference(&transfer->resource, NULL);
655 FREE(transfer);
656 }
657
658 unsigned int
659 llvmpipe_is_resource_referenced( struct pipe_context *pipe,
660 struct pipe_resource *presource,
661 unsigned level)
662 {
663 struct llvmpipe_context *llvmpipe = llvmpipe_context( pipe );
664 if (!(presource->bind & (PIPE_BIND_DEPTH_STENCIL |
665 PIPE_BIND_RENDER_TARGET |
666 PIPE_BIND_SAMPLER_VIEW |
667 PIPE_BIND_SHADER_BUFFER |
668 PIPE_BIND_SHADER_IMAGE)))
669 return LP_UNREFERENCED;
670
671 return lp_setup_is_resource_referenced(llvmpipe->setup, presource);
672 }
673
674
675 /**
676 * Returns the largest possible alignment for a format in llvmpipe
677 */
678 unsigned
679 llvmpipe_get_format_alignment( enum pipe_format format )
680 {
681 const struct util_format_description *desc = util_format_description(format);
682 unsigned size = 0;
683 unsigned bytes;
684 unsigned i;
685
686 for (i = 0; i < desc->nr_channels; ++i) {
687 size += desc->channel[i].size;
688 }
689
690 bytes = size / 8;
691
692 if (!util_is_power_of_two_or_zero(bytes)) {
693 bytes /= desc->nr_channels;
694 }
695
696 if (bytes % 2 || bytes < 1) {
697 return 1;
698 } else {
699 return bytes;
700 }
701 }
702
703
704 /**
705 * Create buffer which wraps user-space data.
706 * XXX unreachable.
707 */
708 struct pipe_resource *
709 llvmpipe_user_buffer_create(struct pipe_screen *screen,
710 void *ptr,
711 unsigned bytes,
712 unsigned bind_flags)
713 {
714 struct llvmpipe_resource *buffer;
715
716 buffer = CALLOC_STRUCT(llvmpipe_resource);
717 if (!buffer)
718 return NULL;
719
720 pipe_reference_init(&buffer->base.reference, 1);
721 buffer->base.screen = screen;
722 buffer->base.format = PIPE_FORMAT_R8_UNORM; /* ?? */
723 buffer->base.bind = bind_flags;
724 buffer->base.usage = PIPE_USAGE_IMMUTABLE;
725 buffer->base.flags = 0;
726 buffer->base.width0 = bytes;
727 buffer->base.height0 = 1;
728 buffer->base.depth0 = 1;
729 buffer->base.array_size = 1;
730 buffer->userBuffer = TRUE;
731 buffer->data = ptr;
732
733 return &buffer->base;
734 }
735
736
737 /**
738 * Compute size (in bytes) need to store a texture image / mipmap level,
739 * for just one cube face, one array layer or one 3D texture slice
740 */
741 static unsigned
742 tex_image_face_size(const struct llvmpipe_resource *lpr, unsigned level)
743 {
744 return lpr->img_stride[level];
745 }
746
747
748 /**
749 * Return pointer to a 2D texture image/face/slice.
750 * No tiled/linear conversion is done.
751 */
752 ubyte *
753 llvmpipe_get_texture_image_address(struct llvmpipe_resource *lpr,
754 unsigned face_slice, unsigned level)
755 {
756 unsigned offset;
757
758 assert(llvmpipe_resource_is_texture(&lpr->base));
759
760 offset = lpr->mip_offsets[level];
761
762 if (face_slice > 0)
763 offset += face_slice * tex_image_face_size(lpr, level);
764
765 return (ubyte *) lpr->tex_data + offset;
766 }
767
768
769 /**
770 * Return size of resource in bytes
771 */
772 unsigned
773 llvmpipe_resource_size(const struct pipe_resource *resource)
774 {
775 const struct llvmpipe_resource *lpr = llvmpipe_resource_const(resource);
776 unsigned size = 0;
777
778 if (llvmpipe_resource_is_texture(resource)) {
779 /* Note this will always return 0 for displaytarget resources */
780 size = lpr->total_alloc_size;
781 }
782 else {
783 size = resource->width0;
784 }
785 return size;
786 }
787
788 static void
789 llvmpipe_memory_barrier(struct pipe_context *pipe,
790 unsigned flags)
791 {
792 /* this may be an overly large hammer for this nut. */
793 llvmpipe_finish(pipe, "barrier");
794 }
795
796 #ifdef DEBUG
797 void
798 llvmpipe_print_resources(void)
799 {
800 struct llvmpipe_resource *lpr;
801 unsigned n = 0, total = 0;
802
803 debug_printf("LLVMPIPE: current resources:\n");
804 foreach(lpr, &resource_list) {
805 unsigned size = llvmpipe_resource_size(&lpr->base);
806 debug_printf("resource %u at %p, size %ux%ux%u: %u bytes, refcount %u\n",
807 lpr->id, (void *) lpr,
808 lpr->base.width0, lpr->base.height0, lpr->base.depth0,
809 size, lpr->base.reference.count);
810 total += size;
811 n++;
812 }
813 debug_printf("LLVMPIPE: total size of %u resources: %u\n", n, total);
814 }
815 #endif
816
817
818 void
819 llvmpipe_init_screen_resource_funcs(struct pipe_screen *screen)
820 {
821 #ifdef DEBUG
822 /* init linked list for tracking resources */
823 {
824 static boolean first_call = TRUE;
825 if (first_call) {
826 memset(&resource_list, 0, sizeof(resource_list));
827 make_empty_list(&resource_list);
828 first_call = FALSE;
829 }
830 }
831 #endif
832
833 screen->resource_create = llvmpipe_resource_create;
834 /* screen->resource_create_front = llvmpipe_resource_create_front; */
835 screen->resource_destroy = llvmpipe_resource_destroy;
836 screen->resource_from_handle = llvmpipe_resource_from_handle;
837 screen->resource_get_handle = llvmpipe_resource_get_handle;
838 screen->can_create_resource = llvmpipe_can_create_resource;
839 }
840
841
842 void
843 llvmpipe_init_context_resource_funcs(struct pipe_context *pipe)
844 {
845 pipe->transfer_map = llvmpipe_transfer_map;
846 pipe->transfer_unmap = llvmpipe_transfer_unmap;
847
848 pipe->transfer_flush_region = u_default_transfer_flush_region;
849 pipe->buffer_subdata = u_default_buffer_subdata;
850 pipe->texture_subdata = u_default_texture_subdata;
851
852 pipe->memory_barrier = llvmpipe_memory_barrier;
853 }