llvmpipe: fix overflow bug in total texture size computation
[mesa.git] / src / gallium / drivers / llvmpipe / lp_texture.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 <keith@tungstengraphics.com>
30 * Michel Dänzer <michel@tungstengraphics.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/u_format.h"
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43 #include "util/u_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_tile_image.h"
50 #include "lp_texture.h"
51 #include "lp_setup.h"
52 #include "lp_state.h"
53
54 #include "state_tracker/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 static INLINE boolean
64 resource_is_texture(const struct pipe_resource *resource)
65 {
66 switch (resource->target) {
67 case PIPE_BUFFER:
68 return FALSE;
69 case PIPE_TEXTURE_1D:
70 case PIPE_TEXTURE_2D:
71 case PIPE_TEXTURE_RECT:
72 case PIPE_TEXTURE_3D:
73 case PIPE_TEXTURE_CUBE:
74 return TRUE;
75 default:
76 assert(0);
77 return FALSE;
78 }
79 }
80
81
82
83 /**
84 * Allocate storage for llvmpipe_texture::layout array.
85 * The number of elements is width_in_tiles * height_in_tiles.
86 */
87 static enum lp_texture_layout *
88 alloc_layout_array(unsigned num_slices, unsigned width, unsigned height)
89 {
90 const unsigned tx = align(width, TILE_SIZE) / TILE_SIZE;
91 const unsigned ty = align(height, TILE_SIZE) / TILE_SIZE;
92
93 assert(num_slices * tx * ty > 0);
94 assert(LP_TEX_LAYOUT_NONE == 0); /* calloc'ing LP_TEX_LAYOUT_NONE here */
95
96 return (enum lp_texture_layout *)
97 CALLOC(num_slices * tx * ty, sizeof(enum lp_texture_layout));
98 }
99
100
101
102 /**
103 * Conventional allocation path for non-display textures:
104 * Just compute row strides here. Storage is allocated on demand later.
105 */
106 static boolean
107 llvmpipe_texture_layout(struct llvmpipe_screen *screen,
108 struct llvmpipe_resource *lpr,
109 boolean allocate)
110 {
111 struct pipe_resource *pt = &lpr->base;
112 unsigned level;
113 unsigned width = pt->width0;
114 unsigned height = pt->height0;
115 unsigned depth = pt->depth0;
116 uint64_t total_size = 0;
117
118 assert(LP_MAX_TEXTURE_2D_LEVELS <= LP_MAX_TEXTURE_LEVELS);
119 assert(LP_MAX_TEXTURE_3D_LEVELS <= LP_MAX_TEXTURE_LEVELS);
120
121 for (level = 0; level <= pt->last_level; level++) {
122
123 /* Row stride and image stride (for linear layout) */
124 {
125 unsigned alignment, nblocksx, nblocksy, block_size;
126
127 /* For non-compressed formats we need to align the texture size
128 * to the tile size to facilitate render-to-texture.
129 */
130 if (util_format_is_compressed(pt->format))
131 alignment = 1;
132 else
133 alignment = TILE_SIZE;
134
135 nblocksx = util_format_get_nblocksx(pt->format,
136 align(width, alignment));
137 nblocksy = util_format_get_nblocksy(pt->format,
138 align(height, alignment));
139 block_size = util_format_get_blocksize(pt->format);
140
141 lpr->row_stride[level] = align(nblocksx * block_size, 16);
142
143 /* if row_stride * height > LP_MAX_TEXTURE_SIZE */
144 if (lpr->row_stride[level] > LP_MAX_TEXTURE_SIZE / nblocksy) {
145 /* image too large */
146 goto fail;
147 }
148
149 lpr->img_stride[level] = lpr->row_stride[level] * nblocksy;
150 }
151
152 /* Size of the image in tiles (for tiled layout) */
153 {
154 const unsigned width_t = align(width, TILE_SIZE) / TILE_SIZE;
155 const unsigned height_t = align(height, TILE_SIZE) / TILE_SIZE;
156 lpr->tiles_per_row[level] = width_t;
157 lpr->tiles_per_image[level] = width_t * height_t;
158 }
159
160 /* Number of 3D image slices or cube faces */
161 {
162 unsigned num_slices;
163
164 if (lpr->base.target == PIPE_TEXTURE_CUBE)
165 num_slices = 6;
166 else if (lpr->base.target == PIPE_TEXTURE_3D)
167 num_slices = depth;
168 else
169 num_slices = 1;
170
171 lpr->num_slices_faces[level] = num_slices;
172
173 if (allocate) {
174 lpr->layout[level] = alloc_layout_array(num_slices, width, height);
175 if (!lpr->layout[level]) {
176 goto fail;
177 }
178 }
179 }
180
181 /* if img_stride * num_slices_faces > LP_MAX_TEXTURE_SIZE */
182 if (lpr->img_stride[level] >
183 LP_MAX_TEXTURE_SIZE / lpr->num_slices_faces[level]) {
184 /* volume too large */
185 goto fail;
186 }
187
188 total_size += (uint64_t) lpr->num_slices_faces[level]
189 * (uint64_t) lpr->img_stride[level];
190 if (total_size > LP_MAX_TEXTURE_SIZE) {
191 goto fail;
192 }
193
194 /* Compute size of next mipmap level */
195 width = u_minify(width, 1);
196 height = u_minify(height, 1);
197 depth = u_minify(depth, 1);
198 }
199
200 return TRUE;
201
202 fail:
203 for (level = 0; level <= pt->last_level; level++) {
204 FREE(lpr->layout[level]);
205 }
206
207 return FALSE;
208 }
209
210
211 /**
212 * Check the size of the texture specified by 'res'.
213 * \return TRUE if OK, FALSE if too large.
214 */
215 static boolean
216 llvmpipe_can_create_resource(struct pipe_screen *screen,
217 const struct pipe_resource *res)
218 {
219 struct llvmpipe_resource lpr;
220 memset(&lpr, 0, sizeof(lpr));
221 lpr.base = *res;
222 return llvmpipe_texture_layout(llvmpipe_screen(screen), &lpr, FALSE);
223 }
224
225
226 static boolean
227 llvmpipe_displaytarget_layout(struct llvmpipe_screen *screen,
228 struct llvmpipe_resource *lpr)
229 {
230 struct sw_winsys *winsys = screen->winsys;
231
232 /* Round up the surface size to a multiple of the tile size to
233 * avoid tile clipping.
234 */
235 const unsigned width = align(lpr->base.width0, TILE_SIZE);
236 const unsigned height = align(lpr->base.height0, TILE_SIZE);
237 const unsigned width_t = width / TILE_SIZE;
238 const unsigned height_t = height / TILE_SIZE;
239
240 lpr->tiles_per_row[0] = width_t;
241 lpr->tiles_per_image[0] = width_t * height_t;
242 lpr->num_slices_faces[0] = 1;
243 lpr->img_stride[0] = 0;
244
245 lpr->layout[0] = alloc_layout_array(1, width, height);
246 if (!lpr->layout[0]) {
247 return FALSE;
248 }
249
250 lpr->dt = winsys->displaytarget_create(winsys,
251 lpr->base.bind,
252 lpr->base.format,
253 width, height,
254 16,
255 &lpr->row_stride[0] );
256
257 if (lpr->dt == NULL)
258 return FALSE;
259
260 {
261 void *map = winsys->displaytarget_map(winsys, lpr->dt,
262 PIPE_TRANSFER_WRITE);
263
264 if (map)
265 memset(map, 0, height * lpr->row_stride[0]);
266
267 winsys->displaytarget_unmap(winsys, lpr->dt);
268 }
269
270 return TRUE;
271 }
272
273
274 static struct pipe_resource *
275 llvmpipe_resource_create(struct pipe_screen *_screen,
276 const struct pipe_resource *templat)
277 {
278 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
279 struct llvmpipe_resource *lpr = CALLOC_STRUCT(llvmpipe_resource);
280 if (!lpr)
281 return NULL;
282
283 lpr->base = *templat;
284 pipe_reference_init(&lpr->base.reference, 1);
285 lpr->base.screen = &screen->base;
286
287 /* assert(lpr->base.bind); */
288
289 if (resource_is_texture(&lpr->base)) {
290 if (lpr->base.bind & PIPE_BIND_DISPLAY_TARGET) {
291 /* displayable surface */
292 if (!llvmpipe_displaytarget_layout(screen, lpr))
293 goto fail;
294 assert(lpr->layout[0][0] == LP_TEX_LAYOUT_NONE);
295 }
296 else {
297 /* texture map */
298 if (!llvmpipe_texture_layout(screen, lpr, TRUE))
299 goto fail;
300 assert(lpr->layout[0][0] == LP_TEX_LAYOUT_NONE);
301 }
302 assert(lpr->layout[0]);
303 }
304 else {
305 /* other data (vertex buffer, const buffer, etc) */
306 const enum pipe_format format = templat->format;
307 const uint w = templat->width0 / util_format_get_blockheight(format);
308 /* XXX buffers should only have one dimension, those values should be 1 */
309 const uint h = templat->height0 / util_format_get_blockwidth(format);
310 const uint d = templat->depth0;
311 const uint bpp = util_format_get_blocksize(format);
312 const uint bytes = w * h * d * bpp;
313 lpr->data = align_malloc(bytes, 16);
314 if (!lpr->data)
315 goto fail;
316 memset(lpr->data, 0, bytes);
317 }
318
319 lpr->id = id_counter++;
320
321 #ifdef DEBUG
322 insert_at_tail(&resource_list, lpr);
323 #endif
324
325 return &lpr->base;
326
327 fail:
328 FREE(lpr);
329 return NULL;
330 }
331
332
333 static void
334 llvmpipe_resource_destroy(struct pipe_screen *pscreen,
335 struct pipe_resource *pt)
336 {
337 struct llvmpipe_screen *screen = llvmpipe_screen(pscreen);
338 struct llvmpipe_resource *lpr = llvmpipe_resource(pt);
339
340 if (lpr->dt) {
341 /* display target */
342 struct sw_winsys *winsys = screen->winsys;
343 winsys->displaytarget_destroy(winsys, lpr->dt);
344
345 if (lpr->tiled[0].data) {
346 align_free(lpr->tiled[0].data);
347 lpr->tiled[0].data = NULL;
348 }
349
350 FREE(lpr->layout[0]);
351 }
352 else if (resource_is_texture(pt)) {
353 /* regular texture */
354 uint level;
355
356 /* free linear image data */
357 for (level = 0; level < Elements(lpr->linear); level++) {
358 if (lpr->linear[level].data) {
359 align_free(lpr->linear[level].data);
360 lpr->linear[level].data = NULL;
361 }
362 }
363
364 /* free tiled image data */
365 for (level = 0; level < Elements(lpr->tiled); level++) {
366 if (lpr->tiled[level].data) {
367 align_free(lpr->tiled[level].data);
368 lpr->tiled[level].data = NULL;
369 }
370 }
371
372 /* free layout flag arrays */
373 for (level = 0; level < Elements(lpr->tiled); level++) {
374 FREE(lpr->layout[level]);
375 lpr->layout[level] = NULL;
376 }
377 }
378 else if (!lpr->userBuffer) {
379 assert(lpr->data);
380 align_free(lpr->data);
381 }
382
383 #ifdef DEBUG
384 if (lpr->next)
385 remove_from_list(lpr);
386 #endif
387
388 FREE(lpr);
389 }
390
391
392 /**
393 * Map a resource for read/write.
394 */
395 void *
396 llvmpipe_resource_map(struct pipe_resource *resource,
397 unsigned level,
398 unsigned layer,
399 enum lp_texture_usage tex_usage,
400 enum lp_texture_layout layout)
401 {
402 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
403 uint8_t *map;
404
405 assert(level < LP_MAX_TEXTURE_LEVELS);
406 assert(layer < (u_minify(resource->depth0, level) + resource->array_size - 1));
407
408 assert(tex_usage == LP_TEX_USAGE_READ ||
409 tex_usage == LP_TEX_USAGE_READ_WRITE ||
410 tex_usage == LP_TEX_USAGE_WRITE_ALL);
411
412 assert(layout == LP_TEX_LAYOUT_NONE ||
413 layout == LP_TEX_LAYOUT_TILED ||
414 layout == LP_TEX_LAYOUT_LINEAR);
415
416 if (lpr->dt) {
417 /* display target */
418 struct llvmpipe_screen *screen = llvmpipe_screen(resource->screen);
419 struct sw_winsys *winsys = screen->winsys;
420 unsigned dt_usage;
421 uint8_t *map2;
422
423 if (tex_usage == LP_TEX_USAGE_READ) {
424 dt_usage = PIPE_TRANSFER_READ;
425 }
426 else {
427 dt_usage = PIPE_TRANSFER_READ_WRITE;
428 }
429
430 assert(level == 0);
431 assert(layer == 0);
432
433 /* FIXME: keep map count? */
434 map = winsys->displaytarget_map(winsys, lpr->dt, dt_usage);
435
436 /* install this linear image in texture data structure */
437 lpr->linear[level].data = map;
438
439 /* make sure tiled data gets converted to linear data */
440 map2 = llvmpipe_get_texture_image(lpr, 0, 0, tex_usage, layout);
441 if (layout == LP_TEX_LAYOUT_LINEAR)
442 assert(map == map2);
443
444 return map2;
445 }
446 else if (resource_is_texture(resource)) {
447
448 map = llvmpipe_get_texture_image(lpr, layer, level,
449 tex_usage, layout);
450 return map;
451 }
452 else {
453 return lpr->data;
454 }
455 }
456
457
458 /**
459 * Unmap a resource.
460 */
461 void
462 llvmpipe_resource_unmap(struct pipe_resource *resource,
463 unsigned level,
464 unsigned layer)
465 {
466 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
467
468 if (lpr->dt) {
469 /* display target */
470 struct llvmpipe_screen *lp_screen = llvmpipe_screen(resource->screen);
471 struct sw_winsys *winsys = lp_screen->winsys;
472
473 assert(level == 0);
474 assert(layer == 0);
475
476 /* make sure linear image is up to date */
477 (void) llvmpipe_get_texture_image(lpr, layer, level,
478 LP_TEX_USAGE_READ,
479 LP_TEX_LAYOUT_LINEAR);
480
481 winsys->displaytarget_unmap(winsys, lpr->dt);
482 }
483 }
484
485
486 void *
487 llvmpipe_resource_data(struct pipe_resource *resource)
488 {
489 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
490
491 assert(!resource_is_texture(resource));
492
493 return lpr->data;
494 }
495
496
497 static struct pipe_resource *
498 llvmpipe_resource_from_handle(struct pipe_screen *screen,
499 const struct pipe_resource *template,
500 struct winsys_handle *whandle)
501 {
502 struct sw_winsys *winsys = llvmpipe_screen(screen)->winsys;
503 struct llvmpipe_resource *lpr;
504 unsigned width, height, width_t, height_t;
505
506 /* XXX Seems like from_handled depth textures doesn't work that well */
507
508 lpr = CALLOC_STRUCT(llvmpipe_resource);
509 if (!lpr) {
510 goto no_lpr;
511 }
512
513 lpr->base = *template;
514 pipe_reference_init(&lpr->base.reference, 1);
515 lpr->base.screen = screen;
516
517 width = align(lpr->base.width0, TILE_SIZE);
518 height = align(lpr->base.height0, TILE_SIZE);
519 width_t = width / TILE_SIZE;
520 height_t = height / TILE_SIZE;
521
522 /*
523 * Looks like unaligned displaytargets work just fine,
524 * at least sampler/render ones.
525 */
526 #if 0
527 assert(lpr->base.width0 == width);
528 assert(lpr->base.height0 == height);
529 #endif
530
531 lpr->tiles_per_row[0] = width_t;
532 lpr->tiles_per_image[0] = width_t * height_t;
533 lpr->num_slices_faces[0] = 1;
534 lpr->img_stride[0] = 0;
535
536 lpr->dt = winsys->displaytarget_from_handle(winsys,
537 template,
538 whandle,
539 &lpr->row_stride[0]);
540 if (!lpr->dt) {
541 goto no_dt;
542 }
543
544 lpr->layout[0] = alloc_layout_array(1, lpr->base.width0, lpr->base.height0);
545 if (!lpr->layout[0]) {
546 goto no_layout_0;
547 }
548
549 assert(lpr->layout[0][0] == LP_TEX_LAYOUT_NONE);
550
551 lpr->id = id_counter++;
552
553 #ifdef DEBUG
554 insert_at_tail(&resource_list, lpr);
555 #endif
556
557 return &lpr->base;
558
559 no_layout_0:
560 winsys->displaytarget_destroy(winsys, lpr->dt);
561 no_dt:
562 FREE(lpr);
563 no_lpr:
564 return NULL;
565 }
566
567
568 static boolean
569 llvmpipe_resource_get_handle(struct pipe_screen *screen,
570 struct pipe_resource *pt,
571 struct winsys_handle *whandle)
572 {
573 struct sw_winsys *winsys = llvmpipe_screen(screen)->winsys;
574 struct llvmpipe_resource *lpr = llvmpipe_resource(pt);
575
576 assert(lpr->dt);
577 if (!lpr->dt)
578 return FALSE;
579
580 return winsys->displaytarget_get_handle(winsys, lpr->dt, whandle);
581 }
582
583
584 static struct pipe_surface *
585 llvmpipe_create_surface(struct pipe_context *pipe,
586 struct pipe_resource *pt,
587 const struct pipe_surface *surf_tmpl)
588 {
589 struct pipe_surface *ps;
590
591 assert(surf_tmpl->u.tex.level <= pt->last_level);
592
593 ps = CALLOC_STRUCT(pipe_surface);
594 if (ps) {
595 pipe_reference_init(&ps->reference, 1);
596 pipe_resource_reference(&ps->texture, pt);
597 ps->context = pipe;
598 ps->format = surf_tmpl->format;
599 ps->width = u_minify(pt->width0, surf_tmpl->u.tex.level);
600 ps->height = u_minify(pt->height0, surf_tmpl->u.tex.level);
601 ps->usage = surf_tmpl->usage;
602
603 ps->u.tex.level = surf_tmpl->u.tex.level;
604 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
605 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
606 }
607 return ps;
608 }
609
610
611 static void
612 llvmpipe_surface_destroy(struct pipe_context *pipe,
613 struct pipe_surface *surf)
614 {
615 /* Effectively do the texture_update work here - if texture images
616 * needed post-processing to put them into hardware layout, this is
617 * where it would happen. For llvmpipe, nothing to do.
618 */
619 assert(surf->texture);
620 pipe_resource_reference(&surf->texture, NULL);
621 FREE(surf);
622 }
623
624
625 static struct pipe_transfer *
626 llvmpipe_get_transfer(struct pipe_context *pipe,
627 struct pipe_resource *resource,
628 unsigned level,
629 unsigned usage,
630 const struct pipe_box *box)
631 {
632 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
633 struct llvmpipe_resource *lprex = llvmpipe_resource(resource);
634 struct llvmpipe_transfer *lpr;
635
636 assert(resource);
637 assert(level <= resource->last_level);
638
639 /*
640 * Transfers, like other pipe operations, must happen in order, so flush the
641 * context if necessary.
642 */
643 if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
644 boolean read_only = !(usage & PIPE_TRANSFER_WRITE);
645 boolean do_not_block = !!(usage & PIPE_TRANSFER_DONTBLOCK);
646 if (!llvmpipe_flush_resource(pipe, resource,
647 level,
648 box->depth > 1 ? -1 : box->z,
649 read_only,
650 TRUE, /* cpu_access */
651 do_not_block,
652 __FUNCTION__)) {
653 /*
654 * It would have blocked, but state tracker requested no to.
655 */
656 assert(do_not_block);
657 return NULL;
658 }
659 }
660
661 if (resource == llvmpipe->constants[PIPE_SHADER_FRAGMENT][0])
662 llvmpipe->dirty |= LP_NEW_CONSTANTS;
663
664 lpr = CALLOC_STRUCT(llvmpipe_transfer);
665 if (lpr) {
666 struct pipe_transfer *pt = &lpr->base;
667 pipe_resource_reference(&pt->resource, resource);
668 pt->box = *box;
669 pt->level = level;
670 pt->stride = lprex->row_stride[level];
671 pt->layer_stride = lprex->img_stride[level];
672 pt->usage = usage;
673
674 return pt;
675 }
676 return NULL;
677 }
678
679
680 static void
681 llvmpipe_transfer_destroy(struct pipe_context *pipe,
682 struct pipe_transfer *transfer)
683 {
684 /* Effectively do the texture_update work here - if texture images
685 * needed post-processing to put them into hardware layout, this is
686 * where it would happen. For llvmpipe, nothing to do.
687 */
688 assert (transfer->resource);
689 pipe_resource_reference(&transfer->resource, NULL);
690 FREE(transfer);
691 }
692
693
694 static void *
695 llvmpipe_transfer_map( struct pipe_context *pipe,
696 struct pipe_transfer *transfer )
697 {
698 struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);
699 ubyte *map;
700 struct llvmpipe_resource *lpr;
701 enum pipe_format format;
702 enum lp_texture_usage tex_usage;
703 const char *mode;
704
705 assert(transfer->level < LP_MAX_TEXTURE_LEVELS);
706
707 /*
708 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
709 transfer->x, transfer->y, transfer->width, transfer->height,
710 transfer->texture->width0,
711 transfer->texture->height0,
712 transfer->usage);
713 */
714
715 if (transfer->usage == PIPE_TRANSFER_READ) {
716 tex_usage = LP_TEX_USAGE_READ;
717 mode = "read";
718 }
719 else {
720 tex_usage = LP_TEX_USAGE_READ_WRITE;
721 mode = "read/write";
722 }
723
724 if (0) {
725 struct llvmpipe_resource *lpr = llvmpipe_resource(transfer->resource);
726 printf("transfer map tex %u mode %s\n", lpr->id, mode);
727 }
728
729
730 assert(transfer->resource);
731 lpr = llvmpipe_resource(transfer->resource);
732 format = lpr->base.format;
733
734 map = llvmpipe_resource_map(transfer->resource,
735 transfer->level,
736 transfer->box.z,
737 tex_usage, LP_TEX_LAYOUT_LINEAR);
738
739
740 /* May want to do different things here depending on read/write nature
741 * of the map:
742 */
743 if (transfer->usage & PIPE_TRANSFER_WRITE) {
744 /* Do something to notify sharing contexts of a texture change.
745 */
746 screen->timestamp++;
747 }
748
749 map +=
750 transfer->box.y / util_format_get_blockheight(format) * transfer->stride +
751 transfer->box.x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
752
753 return map;
754 }
755
756
757 static void
758 llvmpipe_transfer_unmap(struct pipe_context *pipe,
759 struct pipe_transfer *transfer)
760 {
761 assert(transfer->resource);
762
763 llvmpipe_resource_unmap(transfer->resource,
764 transfer->level,
765 transfer->box.z);
766 }
767
768 unsigned int
769 llvmpipe_is_resource_referenced( struct pipe_context *pipe,
770 struct pipe_resource *presource,
771 unsigned level, int layer)
772 {
773 struct llvmpipe_context *llvmpipe = llvmpipe_context( pipe );
774
775 if (presource->target == PIPE_BUFFER)
776 return LP_UNREFERENCED;
777
778 return lp_setup_is_resource_referenced(llvmpipe->setup, presource);
779 }
780
781
782
783 /**
784 * Create buffer which wraps user-space data.
785 */
786 struct pipe_resource *
787 llvmpipe_user_buffer_create(struct pipe_screen *screen,
788 void *ptr,
789 unsigned bytes,
790 unsigned bind_flags)
791 {
792 struct llvmpipe_resource *buffer;
793
794 buffer = CALLOC_STRUCT(llvmpipe_resource);
795 if(!buffer)
796 return NULL;
797
798 pipe_reference_init(&buffer->base.reference, 1);
799 buffer->base.screen = screen;
800 buffer->base.format = PIPE_FORMAT_R8_UNORM; /* ?? */
801 buffer->base.bind = bind_flags;
802 buffer->base.usage = PIPE_USAGE_IMMUTABLE;
803 buffer->base.flags = 0;
804 buffer->base.width0 = bytes;
805 buffer->base.height0 = 1;
806 buffer->base.depth0 = 1;
807 buffer->base.array_size = 1;
808 buffer->userBuffer = TRUE;
809 buffer->data = ptr;
810
811 return &buffer->base;
812 }
813
814
815 /**
816 * Compute size (in bytes) need to store a texture image / mipmap level,
817 * for just one cube face or one 3D texture slice
818 */
819 static unsigned
820 tex_image_face_size(const struct llvmpipe_resource *lpr, unsigned level,
821 enum lp_texture_layout layout)
822 {
823 const unsigned width = u_minify(lpr->base.width0, level);
824 const unsigned height = u_minify(lpr->base.height0, level);
825
826 assert(layout == LP_TEX_LAYOUT_TILED ||
827 layout == LP_TEX_LAYOUT_LINEAR);
828
829 if (layout == LP_TEX_LAYOUT_TILED) {
830 /* for tiled layout, force a 32bpp format */
831 const enum pipe_format format = PIPE_FORMAT_B8G8R8A8_UNORM;
832 const unsigned block_size = util_format_get_blocksize(format);
833 const unsigned nblocksy =
834 util_format_get_nblocksy(format, align(height, TILE_SIZE));
835 const unsigned nblocksx =
836 util_format_get_nblocksx(format, align(width, TILE_SIZE));
837 const unsigned buffer_size = block_size * nblocksy * nblocksx;
838 return buffer_size;
839 }
840 else {
841 /* we already computed this */
842 return lpr->img_stride[level];
843 }
844 }
845
846
847 /**
848 * Compute size (in bytes) need to store a texture image / mipmap level,
849 * including all cube faces or 3D image slices
850 */
851 static unsigned
852 tex_image_size(const struct llvmpipe_resource *lpr, unsigned level,
853 enum lp_texture_layout layout)
854 {
855 const unsigned buf_size = tex_image_face_size(lpr, level, layout);
856 return buf_size * lpr->num_slices_faces[level];
857 }
858
859
860 /**
861 * This function encapsulates some complicated logic for determining
862 * how to convert a tile of image data from linear layout to tiled
863 * layout, or vice versa.
864 * \param cur_layout the current tile layout
865 * \param target_layout the desired tile layout
866 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
867 * \param new_layout_return returns the new layout mode
868 * \param convert_return returns TRUE if image conversion is needed
869 */
870 static void
871 layout_logic(enum lp_texture_layout cur_layout,
872 enum lp_texture_layout target_layout,
873 enum lp_texture_usage usage,
874 enum lp_texture_layout *new_layout_return,
875 boolean *convert)
876 {
877 enum lp_texture_layout other_layout, new_layout;
878
879 *convert = FALSE;
880
881 new_layout = 99; /* debug check */
882
883 if (target_layout == LP_TEX_LAYOUT_LINEAR) {
884 other_layout = LP_TEX_LAYOUT_TILED;
885 }
886 else {
887 assert(target_layout == LP_TEX_LAYOUT_TILED);
888 other_layout = LP_TEX_LAYOUT_LINEAR;
889 }
890
891 new_layout = target_layout; /* may get changed below */
892
893 if (cur_layout == LP_TEX_LAYOUT_BOTH) {
894 if (usage == LP_TEX_USAGE_READ) {
895 new_layout = LP_TEX_LAYOUT_BOTH;
896 }
897 }
898 else if (cur_layout == other_layout) {
899 if (usage != LP_TEX_USAGE_WRITE_ALL) {
900 /* need to convert tiled data to linear or vice versa */
901 *convert = TRUE;
902
903 if (usage == LP_TEX_USAGE_READ)
904 new_layout = LP_TEX_LAYOUT_BOTH;
905 }
906 }
907 else {
908 assert(cur_layout == LP_TEX_LAYOUT_NONE ||
909 cur_layout == target_layout);
910 }
911
912 assert(new_layout == LP_TEX_LAYOUT_BOTH ||
913 new_layout == target_layout);
914
915 *new_layout_return = new_layout;
916 }
917
918
919 /**
920 * Return pointer to a 2D texture image/face/slice.
921 * No tiled/linear conversion is done.
922 */
923 ubyte *
924 llvmpipe_get_texture_image_address(struct llvmpipe_resource *lpr,
925 unsigned face_slice, unsigned level,
926 enum lp_texture_layout layout)
927 {
928 struct llvmpipe_texture_image *img;
929 unsigned offset;
930
931 if (layout == LP_TEX_LAYOUT_LINEAR) {
932 img = &lpr->linear[level];
933 }
934 else {
935 assert (layout == LP_TEX_LAYOUT_TILED);
936 img = &lpr->tiled[level];
937 }
938
939 if (face_slice > 0)
940 offset = face_slice * tex_image_face_size(lpr, level, layout);
941 else
942 offset = 0;
943
944 return (ubyte *) img->data + offset;
945 }
946
947
948 static INLINE enum lp_texture_layout
949 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource *lpr,
950 unsigned face_slice, unsigned level,
951 unsigned x, unsigned y)
952 {
953 uint i;
954 assert(resource_is_texture(&lpr->base));
955 assert(x < lpr->tiles_per_row[level]);
956 i = face_slice * lpr->tiles_per_image[level]
957 + y * lpr->tiles_per_row[level] + x;
958 return lpr->layout[level][i];
959 }
960
961
962 static INLINE void
963 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource *lpr,
964 unsigned face_slice, unsigned level,
965 unsigned x, unsigned y,
966 enum lp_texture_layout layout)
967 {
968 uint i;
969 assert(resource_is_texture(&lpr->base));
970 assert(x < lpr->tiles_per_row[level]);
971 i = face_slice * lpr->tiles_per_image[level]
972 + y * lpr->tiles_per_row[level] + x;
973 lpr->layout[level][i] = layout;
974 }
975
976
977 /**
978 * Set the layout mode for all tiles in a particular image.
979 */
980 static INLINE void
981 llvmpipe_set_texture_image_layout(struct llvmpipe_resource *lpr,
982 unsigned face_slice, unsigned level,
983 unsigned width_t, unsigned height_t,
984 enum lp_texture_layout layout)
985 {
986 const unsigned start = face_slice * lpr->tiles_per_image[level];
987 unsigned i;
988
989 for (i = 0; i < width_t * height_t; i++) {
990 lpr->layout[level][start + i] = layout;
991 }
992 }
993
994
995 /**
996 * Allocate storage for a linear or tile texture image (all cube
997 * faces and all 3D slices.
998 */
999 static void
1000 alloc_image_data(struct llvmpipe_resource *lpr, unsigned level,
1001 enum lp_texture_layout layout)
1002 {
1003 uint alignment = MAX2(16, util_cpu_caps.cacheline);
1004
1005 if (lpr->dt)
1006 assert(level == 0);
1007
1008 if (layout == LP_TEX_LAYOUT_TILED) {
1009 /* tiled data is stored in regular memory */
1010 uint buffer_size = tex_image_size(lpr, level, layout);
1011 lpr->tiled[level].data = align_malloc(buffer_size, alignment);
1012 if (lpr->tiled[level].data) {
1013 memset(lpr->tiled[level].data, 0, buffer_size);
1014 }
1015 }
1016 else {
1017 assert(layout == LP_TEX_LAYOUT_LINEAR);
1018 if (lpr->dt) {
1019 /* we get the linear memory from the winsys, and it has
1020 * already been zeroed
1021 */
1022 struct llvmpipe_screen *screen = llvmpipe_screen(lpr->base.screen);
1023 struct sw_winsys *winsys = screen->winsys;
1024
1025 lpr->linear[0].data =
1026 winsys->displaytarget_map(winsys, lpr->dt,
1027 PIPE_TRANSFER_READ_WRITE);
1028 }
1029 else {
1030 /* not a display target - allocate regular memory */
1031 uint buffer_size = tex_image_size(lpr, level, LP_TEX_LAYOUT_LINEAR);
1032 lpr->linear[level].data = align_malloc(buffer_size, alignment);
1033 if (lpr->linear[level].data) {
1034 memset(lpr->linear[level].data, 0, buffer_size);
1035 }
1036 }
1037 }
1038 }
1039
1040
1041
1042 /**
1043 * Return pointer to texture image data (either linear or tiled layout)
1044 * for a particular cube face or 3D texture slice.
1045 *
1046 * \param face_slice the cube face or 3D slice of interest
1047 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
1048 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
1049 */
1050 void *
1051 llvmpipe_get_texture_image(struct llvmpipe_resource *lpr,
1052 unsigned face_slice, unsigned level,
1053 enum lp_texture_usage usage,
1054 enum lp_texture_layout layout)
1055 {
1056 /*
1057 * 'target' refers to the image which we're retrieving (either in
1058 * tiled or linear layout).
1059 * 'other' refers to the same image but in the other layout. (it may
1060 * or may not exist.
1061 */
1062 struct llvmpipe_texture_image *target_img;
1063 struct llvmpipe_texture_image *other_img;
1064 void *target_data;
1065 void *other_data;
1066 const unsigned width = u_minify(lpr->base.width0, level);
1067 const unsigned height = u_minify(lpr->base.height0, level);
1068 const unsigned width_t = align(width, TILE_SIZE) / TILE_SIZE;
1069 const unsigned height_t = align(height, TILE_SIZE) / TILE_SIZE;
1070 enum lp_texture_layout other_layout;
1071 boolean only_allocate;
1072
1073 assert(layout == LP_TEX_LAYOUT_NONE ||
1074 layout == LP_TEX_LAYOUT_TILED ||
1075 layout == LP_TEX_LAYOUT_LINEAR);
1076
1077 assert(usage == LP_TEX_USAGE_READ ||
1078 usage == LP_TEX_USAGE_READ_WRITE ||
1079 usage == LP_TEX_USAGE_WRITE_ALL);
1080
1081 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
1082 if (layout == LP_TEX_LAYOUT_NONE) {
1083 only_allocate = TRUE;
1084 layout = LP_TEX_LAYOUT_TILED;
1085 }
1086 else {
1087 only_allocate = FALSE;
1088 }
1089
1090 if (lpr->dt) {
1091 assert(lpr->linear[level].data);
1092 }
1093
1094 /* which is target? which is other? */
1095 if (layout == LP_TEX_LAYOUT_LINEAR) {
1096 target_img = &lpr->linear[level];
1097 other_img = &lpr->tiled[level];
1098 other_layout = LP_TEX_LAYOUT_TILED;
1099 }
1100 else {
1101 target_img = &lpr->tiled[level];
1102 other_img = &lpr->linear[level];
1103 other_layout = LP_TEX_LAYOUT_LINEAR;
1104 }
1105
1106 target_data = target_img->data;
1107 other_data = other_img->data;
1108
1109 if (!target_data) {
1110 /* allocate memory for the target image now */
1111 alloc_image_data(lpr, level, layout);
1112 target_data = target_img->data;
1113 }
1114
1115 if (face_slice > 0) {
1116 unsigned target_offset, other_offset;
1117
1118 target_offset = face_slice * tex_image_face_size(lpr, level, layout);
1119 other_offset = face_slice * tex_image_face_size(lpr, level, other_layout);
1120 if (target_data) {
1121 target_data = (uint8_t *) target_data + target_offset;
1122 }
1123 if (other_data) {
1124 other_data = (uint8_t *) other_data + other_offset;
1125 }
1126 }
1127
1128 if (only_allocate) {
1129 /* Just allocating tiled memory. Don't initialize it from the
1130 * linear data if it exists.
1131 */
1132 return target_data;
1133 }
1134
1135 if (other_data) {
1136 /* may need to convert other data to the requested layout */
1137 enum lp_texture_layout new_layout;
1138 unsigned x, y;
1139
1140 /* loop over all image tiles, doing layout conversion where needed */
1141 for (y = 0; y < height_t; y++) {
1142 for (x = 0; x < width_t; x++) {
1143 enum lp_texture_layout cur_layout =
1144 llvmpipe_get_texture_tile_layout(lpr, face_slice, level, x, y);
1145 boolean convert;
1146
1147 layout_logic(cur_layout, layout, usage, &new_layout, &convert);
1148
1149 if (convert && other_data && target_data) {
1150 if (layout == LP_TEX_LAYOUT_TILED) {
1151 lp_linear_to_tiled(other_data, target_data,
1152 x * TILE_SIZE, y * TILE_SIZE,
1153 TILE_SIZE, TILE_SIZE,
1154 lpr->base.format,
1155 lpr->row_stride[level],
1156 lpr->tiles_per_row[level]);
1157 }
1158 else {
1159 assert(layout == LP_TEX_LAYOUT_LINEAR);
1160 lp_tiled_to_linear(other_data, target_data,
1161 x * TILE_SIZE, y * TILE_SIZE,
1162 TILE_SIZE, TILE_SIZE,
1163 lpr->base.format,
1164 lpr->row_stride[level],
1165 lpr->tiles_per_row[level]);
1166 }
1167 }
1168
1169 if (new_layout != cur_layout)
1170 llvmpipe_set_texture_tile_layout(lpr, face_slice, level, x, y,
1171 new_layout);
1172 }
1173 }
1174 }
1175 else {
1176 /* no other data */
1177 llvmpipe_set_texture_image_layout(lpr, face_slice, level,
1178 width_t, height_t, layout);
1179 }
1180
1181 return target_data;
1182 }
1183
1184
1185 /**
1186 * Return pointer to start of a texture image (1D, 2D, 3D, CUBE).
1187 * All cube faces and 3D slices will be converted to the requested
1188 * layout if needed.
1189 * This is typically used when we're about to sample from a texture.
1190 */
1191 void *
1192 llvmpipe_get_texture_image_all(struct llvmpipe_resource *lpr,
1193 unsigned level,
1194 enum lp_texture_usage usage,
1195 enum lp_texture_layout layout)
1196 {
1197 const int slices = lpr->num_slices_faces[level];
1198 int slice;
1199 void *map = NULL;
1200
1201 assert(slices > 0);
1202
1203 for (slice = slices - 1; slice >= 0; slice--) {
1204 map = llvmpipe_get_texture_image(lpr, slice, level, usage, layout);
1205 }
1206
1207 return map;
1208 }
1209
1210
1211 /**
1212 * Get pointer to a linear image (not the tile!) where the tile at (x,y)
1213 * is known to be in linear layout.
1214 * Conversion from tiled to linear will be done if necessary.
1215 * \return pointer to start of image/face (not the tile)
1216 */
1217 ubyte *
1218 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource *lpr,
1219 unsigned face_slice, unsigned level,
1220 enum lp_texture_usage usage,
1221 unsigned x, unsigned y)
1222 {
1223 struct llvmpipe_texture_image *linear_img = &lpr->linear[level];
1224 enum lp_texture_layout cur_layout, new_layout;
1225 const unsigned tx = x / TILE_SIZE, ty = y / TILE_SIZE;
1226 boolean convert;
1227 uint8_t *tiled_image, *linear_image;
1228
1229 assert(resource_is_texture(&lpr->base));
1230 assert(x % TILE_SIZE == 0);
1231 assert(y % TILE_SIZE == 0);
1232
1233 if (!linear_img->data) {
1234 /* allocate memory for the linear image now */
1235 alloc_image_data(lpr, level, LP_TEX_LAYOUT_LINEAR);
1236 }
1237
1238 /* compute address of the slice/face of the image that contains the tile */
1239 tiled_image = llvmpipe_get_texture_image_address(lpr, face_slice, level,
1240 LP_TEX_LAYOUT_TILED);
1241 linear_image = llvmpipe_get_texture_image_address(lpr, face_slice, level,
1242 LP_TEX_LAYOUT_LINEAR);
1243
1244 /* get current tile layout and determine if data conversion is needed */
1245 cur_layout = llvmpipe_get_texture_tile_layout(lpr, face_slice, level, tx, ty);
1246
1247 layout_logic(cur_layout, LP_TEX_LAYOUT_LINEAR, usage,
1248 &new_layout, &convert);
1249
1250 if (convert && tiled_image && linear_image) {
1251 lp_tiled_to_linear(tiled_image, linear_image,
1252 x, y, TILE_SIZE, TILE_SIZE, lpr->base.format,
1253 lpr->row_stride[level],
1254 lpr->tiles_per_row[level]);
1255 }
1256
1257 if (new_layout != cur_layout)
1258 llvmpipe_set_texture_tile_layout(lpr, face_slice, level, tx, ty, new_layout);
1259
1260 return linear_image;
1261 }
1262
1263
1264 /**
1265 * Get pointer to tiled data for rendering.
1266 * \return pointer to the tiled data at the given tile position
1267 */
1268 ubyte *
1269 llvmpipe_get_texture_tile(struct llvmpipe_resource *lpr,
1270 unsigned face_slice, unsigned level,
1271 enum lp_texture_usage usage,
1272 unsigned x, unsigned y)
1273 {
1274 struct llvmpipe_texture_image *tiled_img = &lpr->tiled[level];
1275 enum lp_texture_layout cur_layout, new_layout;
1276 const unsigned tx = x / TILE_SIZE, ty = y / TILE_SIZE;
1277 boolean convert;
1278 uint8_t *tiled_image, *linear_image;
1279 unsigned tile_offset;
1280
1281 assert(x % TILE_SIZE == 0);
1282 assert(y % TILE_SIZE == 0);
1283
1284 if (!tiled_img->data) {
1285 /* allocate memory for the tiled image now */
1286 alloc_image_data(lpr, level, LP_TEX_LAYOUT_TILED);
1287 }
1288
1289 /* compute address of the slice/face of the image that contains the tile */
1290 tiled_image = llvmpipe_get_texture_image_address(lpr, face_slice, level,
1291 LP_TEX_LAYOUT_TILED);
1292 linear_image = llvmpipe_get_texture_image_address(lpr, face_slice, level,
1293 LP_TEX_LAYOUT_LINEAR);
1294
1295 /* get current tile layout and see if we need to convert the data */
1296 cur_layout = llvmpipe_get_texture_tile_layout(lpr, face_slice, level, tx, ty);
1297
1298 layout_logic(cur_layout, LP_TEX_LAYOUT_TILED, usage, &new_layout, &convert);
1299 if (convert && linear_image && tiled_image) {
1300 lp_linear_to_tiled(linear_image, tiled_image,
1301 x, y, TILE_SIZE, TILE_SIZE, lpr->base.format,
1302 lpr->row_stride[level],
1303 lpr->tiles_per_row[level]);
1304 }
1305
1306 if (!tiled_image)
1307 return NULL;
1308
1309 if (new_layout != cur_layout)
1310 llvmpipe_set_texture_tile_layout(lpr, face_slice, level, tx, ty, new_layout);
1311
1312 /* compute, return address of the 64x64 tile */
1313 tile_offset = (ty * lpr->tiles_per_row[level] + tx)
1314 * TILE_SIZE * TILE_SIZE * 4;
1315
1316 return (ubyte *) tiled_image + tile_offset;
1317 }
1318
1319
1320 /**
1321 * Get pointer to tiled data for rendering.
1322 * \return pointer to the tiled data at the given tile position
1323 */
1324 void
1325 llvmpipe_unswizzle_cbuf_tile(struct llvmpipe_resource *lpr,
1326 unsigned face_slice, unsigned level,
1327 unsigned x, unsigned y,
1328 uint8_t *tile)
1329 {
1330 struct llvmpipe_texture_image *linear_img = &lpr->linear[level];
1331 const unsigned tx = x / TILE_SIZE, ty = y / TILE_SIZE;
1332 uint8_t *linear_image;
1333
1334 assert(x % TILE_SIZE == 0);
1335 assert(y % TILE_SIZE == 0);
1336
1337 if (!linear_img->data) {
1338 /* allocate memory for the linear image now */
1339 alloc_image_data(lpr, level, LP_TEX_LAYOUT_LINEAR);
1340 }
1341
1342 /* compute address of the slice/face of the image that contains the tile */
1343 linear_image = llvmpipe_get_texture_image_address(lpr, face_slice, level,
1344 LP_TEX_LAYOUT_LINEAR);
1345
1346 {
1347 uint ii = x, jj = y;
1348 uint tile_offset = jj / TILE_SIZE + ii / TILE_SIZE;
1349 uint byte_offset = tile_offset * TILE_SIZE * TILE_SIZE * 4;
1350
1351 /* Note that lp_tiled_to_linear expects the tile parameter to
1352 * point at the first tile in a whole-image sized array. In
1353 * this code, we have only a single tile and have to do some
1354 * pointer arithmetic to figure out where the "image" would have
1355 * started.
1356 */
1357 lp_tiled_to_linear(tile - byte_offset, linear_image,
1358 x, y, TILE_SIZE, TILE_SIZE,
1359 lpr->base.format,
1360 lpr->row_stride[level],
1361 1); /* tiles per row */
1362 }
1363
1364 llvmpipe_set_texture_tile_layout(lpr, face_slice, level, tx, ty,
1365 LP_TEX_LAYOUT_LINEAR);
1366 }
1367
1368
1369 /**
1370 * Get pointer to tiled data for rendering.
1371 * \return pointer to the tiled data at the given tile position
1372 */
1373 void
1374 llvmpipe_swizzle_cbuf_tile(struct llvmpipe_resource *lpr,
1375 unsigned face_slice, unsigned level,
1376 unsigned x, unsigned y,
1377 uint8_t *tile)
1378 {
1379 uint8_t *linear_image;
1380
1381 assert(x % TILE_SIZE == 0);
1382 assert(y % TILE_SIZE == 0);
1383
1384 /* compute address of the slice/face of the image that contains the tile */
1385 linear_image = llvmpipe_get_texture_image_address(lpr, face_slice, level,
1386 LP_TEX_LAYOUT_LINEAR);
1387
1388 if (linear_image) {
1389 uint ii = x, jj = y;
1390 uint tile_offset = jj / TILE_SIZE + ii / TILE_SIZE;
1391 uint byte_offset = tile_offset * TILE_SIZE * TILE_SIZE * 4;
1392
1393 /* Note that lp_linear_to_tiled expects the tile parameter to
1394 * point at the first tile in a whole-image sized array. In
1395 * this code, we have only a single tile and have to do some
1396 * pointer arithmetic to figure out where the "image" would have
1397 * started.
1398 */
1399 lp_linear_to_tiled(linear_image, tile - byte_offset,
1400 x, y, TILE_SIZE, TILE_SIZE,
1401 lpr->base.format,
1402 lpr->row_stride[level],
1403 1); /* tiles per row */
1404 }
1405 }
1406
1407
1408 /**
1409 * Return size of resource in bytes
1410 */
1411 unsigned
1412 llvmpipe_resource_size(const struct pipe_resource *resource)
1413 {
1414 const struct llvmpipe_resource *lpr = llvmpipe_resource_const(resource);
1415 unsigned lvl, size = 0;
1416
1417 for (lvl = 0; lvl <= lpr->base.last_level; lvl++) {
1418 if (lpr->linear[lvl].data)
1419 size += tex_image_size(lpr, lvl, LP_TEX_LAYOUT_LINEAR);
1420
1421 if (lpr->tiled[lvl].data)
1422 size += tex_image_size(lpr, lvl, LP_TEX_LAYOUT_TILED);
1423 }
1424
1425 return size;
1426 }
1427
1428
1429 #ifdef DEBUG
1430 void
1431 llvmpipe_print_resources(void)
1432 {
1433 struct llvmpipe_resource *lpr;
1434 unsigned n = 0, total = 0;
1435
1436 debug_printf("LLVMPIPE: current resources:\n");
1437 foreach(lpr, &resource_list) {
1438 unsigned size = llvmpipe_resource_size(&lpr->base);
1439 debug_printf("resource %u at %p, size %ux%ux%u: %u bytes, refcount %u\n",
1440 lpr->id, (void *) lpr,
1441 lpr->base.width0, lpr->base.height0, lpr->base.depth0,
1442 size, lpr->base.reference.count);
1443 total += size;
1444 n++;
1445 }
1446 debug_printf("LLVMPIPE: total size of %u resources: %u\n", n, total);
1447 }
1448 #endif
1449
1450
1451 void
1452 llvmpipe_init_screen_resource_funcs(struct pipe_screen *screen)
1453 {
1454 #ifdef DEBUG
1455 /* init linked list for tracking resources */
1456 {
1457 static boolean first_call = TRUE;
1458 if (first_call) {
1459 memset(&resource_list, 0, sizeof(resource_list));
1460 make_empty_list(&resource_list);
1461 first_call = FALSE;
1462 }
1463 }
1464 #endif
1465
1466 screen->resource_create = llvmpipe_resource_create;
1467 screen->resource_destroy = llvmpipe_resource_destroy;
1468 screen->resource_from_handle = llvmpipe_resource_from_handle;
1469 screen->resource_get_handle = llvmpipe_resource_get_handle;
1470 screen->can_create_resource = llvmpipe_can_create_resource;
1471 }
1472
1473
1474 void
1475 llvmpipe_init_context_resource_funcs(struct pipe_context *pipe)
1476 {
1477 pipe->get_transfer = llvmpipe_get_transfer;
1478 pipe->transfer_destroy = llvmpipe_transfer_destroy;
1479 pipe->transfer_map = llvmpipe_transfer_map;
1480 pipe->transfer_unmap = llvmpipe_transfer_unmap;
1481
1482 pipe->transfer_flush_region = u_default_transfer_flush_region;
1483 pipe->transfer_inline_write = u_default_transfer_inline_write;
1484
1485 pipe->create_surface = llvmpipe_create_surface;
1486 pipe->surface_destroy = llvmpipe_surface_destroy;
1487 }