llvmpipe: always allocate whole miptrees not individual levels
[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_img.data) {
346 align_free(lpr->tiled_img.data);
347 lpr->tiled_img.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 if (lpr->linear_img.data) {
358 align_free(lpr->linear_img.data);
359 lpr->linear_img.data = NULL;
360 }
361
362 /* free tiled image data */
363 if (lpr->tiled_img.data) {
364 align_free(lpr->tiled_img.data);
365 lpr->tiled_img.data = NULL;
366 }
367
368 /* free layout flag arrays */
369 for (level = 0; level < Elements(lpr->layout); level++) {
370 FREE(lpr->layout[level]);
371 lpr->layout[level] = NULL;
372 }
373 }
374 else if (!lpr->userBuffer) {
375 assert(lpr->data);
376 align_free(lpr->data);
377 }
378
379 #ifdef DEBUG
380 if (lpr->next)
381 remove_from_list(lpr);
382 #endif
383
384 FREE(lpr);
385 }
386
387
388 /**
389 * Map a resource for read/write.
390 */
391 void *
392 llvmpipe_resource_map(struct pipe_resource *resource,
393 unsigned level,
394 unsigned layer,
395 enum lp_texture_usage tex_usage,
396 enum lp_texture_layout layout)
397 {
398 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
399 uint8_t *map;
400
401 assert(level < LP_MAX_TEXTURE_LEVELS);
402 assert(layer < (u_minify(resource->depth0, level) + resource->array_size - 1));
403
404 assert(tex_usage == LP_TEX_USAGE_READ ||
405 tex_usage == LP_TEX_USAGE_READ_WRITE ||
406 tex_usage == LP_TEX_USAGE_WRITE_ALL);
407
408 assert(layout == LP_TEX_LAYOUT_NONE ||
409 layout == LP_TEX_LAYOUT_TILED ||
410 layout == LP_TEX_LAYOUT_LINEAR);
411
412 if (lpr->dt) {
413 /* display target */
414 struct llvmpipe_screen *screen = llvmpipe_screen(resource->screen);
415 struct sw_winsys *winsys = screen->winsys;
416 unsigned dt_usage;
417 uint8_t *map2;
418
419 if (tex_usage == LP_TEX_USAGE_READ) {
420 dt_usage = PIPE_TRANSFER_READ;
421 }
422 else {
423 dt_usage = PIPE_TRANSFER_READ_WRITE;
424 }
425
426 assert(level == 0);
427 assert(layer == 0);
428
429 /* FIXME: keep map count? */
430 map = winsys->displaytarget_map(winsys, lpr->dt, dt_usage);
431
432 /* install this linear image in texture data structure */
433 lpr->linear_img.data = map;
434
435 /* make sure tiled data gets converted to linear data */
436 map2 = llvmpipe_get_texture_image(lpr, 0, 0, tex_usage, layout);
437 if (layout == LP_TEX_LAYOUT_LINEAR)
438 assert(map == map2);
439
440 return map2;
441 }
442 else if (resource_is_texture(resource)) {
443
444 map = llvmpipe_get_texture_image(lpr, layer, level,
445 tex_usage, layout);
446 return map;
447 }
448 else {
449 return lpr->data;
450 }
451 }
452
453
454 /**
455 * Unmap a resource.
456 */
457 void
458 llvmpipe_resource_unmap(struct pipe_resource *resource,
459 unsigned level,
460 unsigned layer)
461 {
462 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
463
464 if (lpr->dt) {
465 /* display target */
466 struct llvmpipe_screen *lp_screen = llvmpipe_screen(resource->screen);
467 struct sw_winsys *winsys = lp_screen->winsys;
468
469 assert(level == 0);
470 assert(layer == 0);
471
472 /* make sure linear image is up to date */
473 (void) llvmpipe_get_texture_image(lpr, layer, level,
474 LP_TEX_USAGE_READ,
475 LP_TEX_LAYOUT_LINEAR);
476
477 winsys->displaytarget_unmap(winsys, lpr->dt);
478 }
479 }
480
481
482 void *
483 llvmpipe_resource_data(struct pipe_resource *resource)
484 {
485 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
486
487 assert(!resource_is_texture(resource));
488
489 return lpr->data;
490 }
491
492
493 static struct pipe_resource *
494 llvmpipe_resource_from_handle(struct pipe_screen *screen,
495 const struct pipe_resource *template,
496 struct winsys_handle *whandle)
497 {
498 struct sw_winsys *winsys = llvmpipe_screen(screen)->winsys;
499 struct llvmpipe_resource *lpr;
500 unsigned width, height, width_t, height_t;
501
502 /* XXX Seems like from_handled depth textures doesn't work that well */
503
504 lpr = CALLOC_STRUCT(llvmpipe_resource);
505 if (!lpr) {
506 goto no_lpr;
507 }
508
509 lpr->base = *template;
510 pipe_reference_init(&lpr->base.reference, 1);
511 lpr->base.screen = screen;
512
513 width = align(lpr->base.width0, TILE_SIZE);
514 height = align(lpr->base.height0, TILE_SIZE);
515 width_t = width / TILE_SIZE;
516 height_t = height / TILE_SIZE;
517
518 /*
519 * Looks like unaligned displaytargets work just fine,
520 * at least sampler/render ones.
521 */
522 #if 0
523 assert(lpr->base.width0 == width);
524 assert(lpr->base.height0 == height);
525 #endif
526
527 lpr->tiles_per_row[0] = width_t;
528 lpr->tiles_per_image[0] = width_t * height_t;
529 lpr->num_slices_faces[0] = 1;
530 lpr->img_stride[0] = 0;
531
532 lpr->dt = winsys->displaytarget_from_handle(winsys,
533 template,
534 whandle,
535 &lpr->row_stride[0]);
536 if (!lpr->dt) {
537 goto no_dt;
538 }
539
540 lpr->layout[0] = alloc_layout_array(1, lpr->base.width0, lpr->base.height0);
541 if (!lpr->layout[0]) {
542 goto no_layout_0;
543 }
544
545 assert(lpr->layout[0][0] == LP_TEX_LAYOUT_NONE);
546
547 lpr->id = id_counter++;
548
549 #ifdef DEBUG
550 insert_at_tail(&resource_list, lpr);
551 #endif
552
553 return &lpr->base;
554
555 no_layout_0:
556 winsys->displaytarget_destroy(winsys, lpr->dt);
557 no_dt:
558 FREE(lpr);
559 no_lpr:
560 return NULL;
561 }
562
563
564 static boolean
565 llvmpipe_resource_get_handle(struct pipe_screen *screen,
566 struct pipe_resource *pt,
567 struct winsys_handle *whandle)
568 {
569 struct sw_winsys *winsys = llvmpipe_screen(screen)->winsys;
570 struct llvmpipe_resource *lpr = llvmpipe_resource(pt);
571
572 assert(lpr->dt);
573 if (!lpr->dt)
574 return FALSE;
575
576 return winsys->displaytarget_get_handle(winsys, lpr->dt, whandle);
577 }
578
579
580 static struct pipe_surface *
581 llvmpipe_create_surface(struct pipe_context *pipe,
582 struct pipe_resource *pt,
583 const struct pipe_surface *surf_tmpl)
584 {
585 struct pipe_surface *ps;
586
587 assert(surf_tmpl->u.tex.level <= pt->last_level);
588
589 ps = CALLOC_STRUCT(pipe_surface);
590 if (ps) {
591 pipe_reference_init(&ps->reference, 1);
592 pipe_resource_reference(&ps->texture, pt);
593 ps->context = pipe;
594 ps->format = surf_tmpl->format;
595 ps->width = u_minify(pt->width0, surf_tmpl->u.tex.level);
596 ps->height = u_minify(pt->height0, surf_tmpl->u.tex.level);
597 ps->usage = surf_tmpl->usage;
598
599 ps->u.tex.level = surf_tmpl->u.tex.level;
600 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
601 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
602 }
603 return ps;
604 }
605
606
607 static void
608 llvmpipe_surface_destroy(struct pipe_context *pipe,
609 struct pipe_surface *surf)
610 {
611 /* Effectively do the texture_update work here - if texture images
612 * needed post-processing to put them into hardware layout, this is
613 * where it would happen. For llvmpipe, nothing to do.
614 */
615 assert(surf->texture);
616 pipe_resource_reference(&surf->texture, NULL);
617 FREE(surf);
618 }
619
620
621 static void *
622 llvmpipe_transfer_map( struct pipe_context *pipe,
623 struct pipe_resource *resource,
624 unsigned level,
625 unsigned usage,
626 const struct pipe_box *box,
627 struct pipe_transfer **transfer )
628 {
629 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
630 struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);
631 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
632 struct llvmpipe_transfer *lpt;
633 struct pipe_transfer *pt;
634 ubyte *map;
635 enum pipe_format format;
636 enum lp_texture_usage tex_usage;
637 const char *mode;
638
639 assert(resource);
640 assert(level <= resource->last_level);
641
642 /*
643 * Transfers, like other pipe operations, must happen in order, so flush the
644 * context if necessary.
645 */
646 if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
647 boolean read_only = !(usage & PIPE_TRANSFER_WRITE);
648 boolean do_not_block = !!(usage & PIPE_TRANSFER_DONTBLOCK);
649 if (!llvmpipe_flush_resource(pipe, resource,
650 level,
651 box->depth > 1 ? -1 : box->z,
652 read_only,
653 TRUE, /* cpu_access */
654 do_not_block,
655 __FUNCTION__)) {
656 /*
657 * It would have blocked, but state tracker requested no to.
658 */
659 assert(do_not_block);
660 return NULL;
661 }
662 }
663
664 if (resource == llvmpipe->constants[PIPE_SHADER_FRAGMENT][0])
665 llvmpipe->dirty |= LP_NEW_CONSTANTS;
666
667 lpt = CALLOC_STRUCT(llvmpipe_transfer);
668 if (!lpt)
669 return NULL;
670 pt = &lpt->base;
671 pipe_resource_reference(&pt->resource, resource);
672 pt->box = *box;
673 pt->level = level;
674 pt->stride = lpr->row_stride[level];
675 pt->layer_stride = lpr->img_stride[level];
676 pt->usage = usage;
677 *transfer = pt;
678
679 assert(level < LP_MAX_TEXTURE_LEVELS);
680
681 /*
682 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
683 transfer->x, transfer->y, transfer->width, transfer->height,
684 transfer->texture->width0,
685 transfer->texture->height0,
686 transfer->usage);
687 */
688
689 if (usage == PIPE_TRANSFER_READ) {
690 tex_usage = LP_TEX_USAGE_READ;
691 mode = "read";
692 }
693 else {
694 tex_usage = LP_TEX_USAGE_READ_WRITE;
695 mode = "read/write";
696 }
697
698 if (0) {
699 printf("transfer map tex %u mode %s\n", lpr->id, mode);
700 }
701
702 format = lpr->base.format;
703
704 map = llvmpipe_resource_map(resource,
705 level,
706 box->z,
707 tex_usage, LP_TEX_LAYOUT_LINEAR);
708
709
710 /* May want to do different things here depending on read/write nature
711 * of the map:
712 */
713 if (usage & PIPE_TRANSFER_WRITE) {
714 /* Do something to notify sharing contexts of a texture change.
715 */
716 screen->timestamp++;
717 }
718
719 map +=
720 box->y / util_format_get_blockheight(format) * pt->stride +
721 box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
722
723 return map;
724 }
725
726
727 static void
728 llvmpipe_transfer_unmap(struct pipe_context *pipe,
729 struct pipe_transfer *transfer)
730 {
731 assert(transfer->resource);
732
733 llvmpipe_resource_unmap(transfer->resource,
734 transfer->level,
735 transfer->box.z);
736
737 /* Effectively do the texture_update work here - if texture images
738 * needed post-processing to put them into hardware layout, this is
739 * where it would happen. For llvmpipe, nothing to do.
740 */
741 assert (transfer->resource);
742 pipe_resource_reference(&transfer->resource, NULL);
743 FREE(transfer);
744 }
745
746 unsigned int
747 llvmpipe_is_resource_referenced( struct pipe_context *pipe,
748 struct pipe_resource *presource,
749 unsigned level, int layer)
750 {
751 struct llvmpipe_context *llvmpipe = llvmpipe_context( pipe );
752
753 if (presource->target == PIPE_BUFFER)
754 return LP_UNREFERENCED;
755
756 return lp_setup_is_resource_referenced(llvmpipe->setup, presource);
757 }
758
759
760
761 /**
762 * Create buffer which wraps user-space data.
763 */
764 struct pipe_resource *
765 llvmpipe_user_buffer_create(struct pipe_screen *screen,
766 void *ptr,
767 unsigned bytes,
768 unsigned bind_flags)
769 {
770 struct llvmpipe_resource *buffer;
771
772 buffer = CALLOC_STRUCT(llvmpipe_resource);
773 if(!buffer)
774 return NULL;
775
776 pipe_reference_init(&buffer->base.reference, 1);
777 buffer->base.screen = screen;
778 buffer->base.format = PIPE_FORMAT_R8_UNORM; /* ?? */
779 buffer->base.bind = bind_flags;
780 buffer->base.usage = PIPE_USAGE_IMMUTABLE;
781 buffer->base.flags = 0;
782 buffer->base.width0 = bytes;
783 buffer->base.height0 = 1;
784 buffer->base.depth0 = 1;
785 buffer->base.array_size = 1;
786 buffer->userBuffer = TRUE;
787 buffer->data = ptr;
788
789 return &buffer->base;
790 }
791
792
793 /**
794 * Compute size (in bytes) need to store a texture image / mipmap level,
795 * for just one cube face or one 3D texture slice
796 */
797 static unsigned
798 tex_image_face_size(const struct llvmpipe_resource *lpr, unsigned level,
799 enum lp_texture_layout layout)
800 {
801 const unsigned width = u_minify(lpr->base.width0, level);
802 const unsigned height = u_minify(lpr->base.height0, level);
803
804 assert(layout == LP_TEX_LAYOUT_TILED ||
805 layout == LP_TEX_LAYOUT_LINEAR);
806
807 if (layout == LP_TEX_LAYOUT_TILED) {
808 /* for tiled layout, force a 32bpp format */
809 const enum pipe_format format = PIPE_FORMAT_B8G8R8A8_UNORM;
810 const unsigned block_size = util_format_get_blocksize(format);
811 const unsigned nblocksy =
812 util_format_get_nblocksy(format, align(height, TILE_SIZE));
813 const unsigned nblocksx =
814 util_format_get_nblocksx(format, align(width, TILE_SIZE));
815 const unsigned buffer_size = block_size * nblocksy * nblocksx;
816 return buffer_size;
817 }
818 else {
819 /* we already computed this */
820 return lpr->img_stride[level];
821 }
822 }
823
824
825 /**
826 * Compute size (in bytes) need to store a texture image / mipmap level,
827 * including all cube faces or 3D image slices
828 */
829 static unsigned
830 tex_image_size(const struct llvmpipe_resource *lpr, unsigned level,
831 enum lp_texture_layout layout)
832 {
833 const unsigned buf_size = tex_image_face_size(lpr, level, layout);
834 return buf_size * lpr->num_slices_faces[level];
835 }
836
837
838 /**
839 * This function encapsulates some complicated logic for determining
840 * how to convert a tile of image data from linear layout to tiled
841 * layout, or vice versa.
842 * \param cur_layout the current tile layout
843 * \param target_layout the desired tile layout
844 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
845 * \param new_layout_return returns the new layout mode
846 * \param convert_return returns TRUE if image conversion is needed
847 */
848 static void
849 layout_logic(enum lp_texture_layout cur_layout,
850 enum lp_texture_layout target_layout,
851 enum lp_texture_usage usage,
852 enum lp_texture_layout *new_layout_return,
853 boolean *convert)
854 {
855 enum lp_texture_layout other_layout, new_layout;
856
857 *convert = FALSE;
858
859 new_layout = 99; /* debug check */
860
861 if (target_layout == LP_TEX_LAYOUT_LINEAR) {
862 other_layout = LP_TEX_LAYOUT_TILED;
863 }
864 else {
865 assert(target_layout == LP_TEX_LAYOUT_TILED);
866 other_layout = LP_TEX_LAYOUT_LINEAR;
867 }
868
869 new_layout = target_layout; /* may get changed below */
870
871 if (cur_layout == LP_TEX_LAYOUT_BOTH) {
872 if (usage == LP_TEX_USAGE_READ) {
873 new_layout = LP_TEX_LAYOUT_BOTH;
874 }
875 }
876 else if (cur_layout == other_layout) {
877 if (usage != LP_TEX_USAGE_WRITE_ALL) {
878 /* need to convert tiled data to linear or vice versa */
879 *convert = TRUE;
880
881 if (usage == LP_TEX_USAGE_READ)
882 new_layout = LP_TEX_LAYOUT_BOTH;
883 }
884 }
885 else {
886 assert(cur_layout == LP_TEX_LAYOUT_NONE ||
887 cur_layout == target_layout);
888 }
889
890 assert(new_layout == LP_TEX_LAYOUT_BOTH ||
891 new_layout == target_layout);
892
893 *new_layout_return = new_layout;
894 }
895
896
897 /**
898 * Return pointer to a 2D texture image/face/slice.
899 * No tiled/linear conversion is done.
900 */
901 ubyte *
902 llvmpipe_get_texture_image_address(struct llvmpipe_resource *lpr,
903 unsigned face_slice, unsigned level,
904 enum lp_texture_layout layout)
905 {
906 struct llvmpipe_texture_image *img;
907 unsigned offset;
908
909 if (layout == LP_TEX_LAYOUT_LINEAR) {
910 img = &lpr->linear_img;
911 offset = lpr->linear_mip_offsets[level];
912 }
913 else {
914 assert (layout == LP_TEX_LAYOUT_TILED);
915 img = &lpr->tiled_img;
916 offset = lpr->tiled_mip_offsets[level];
917 }
918
919 if (face_slice > 0)
920 offset += face_slice * tex_image_face_size(lpr, level, layout);
921
922 return (ubyte *) img->data + offset;
923 }
924
925
926 static INLINE enum lp_texture_layout
927 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource *lpr,
928 unsigned face_slice, unsigned level,
929 unsigned x, unsigned y)
930 {
931 uint i;
932 assert(resource_is_texture(&lpr->base));
933 assert(x < lpr->tiles_per_row[level]);
934 i = face_slice * lpr->tiles_per_image[level]
935 + y * lpr->tiles_per_row[level] + x;
936 return lpr->layout[level][i];
937 }
938
939
940 static INLINE void
941 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource *lpr,
942 unsigned face_slice, unsigned level,
943 unsigned x, unsigned y,
944 enum lp_texture_layout layout)
945 {
946 uint i;
947 assert(resource_is_texture(&lpr->base));
948 assert(x < lpr->tiles_per_row[level]);
949 i = face_slice * lpr->tiles_per_image[level]
950 + y * lpr->tiles_per_row[level] + x;
951 lpr->layout[level][i] = layout;
952 }
953
954
955 /**
956 * Set the layout mode for all tiles in a particular image.
957 */
958 static INLINE void
959 llvmpipe_set_texture_image_layout(struct llvmpipe_resource *lpr,
960 unsigned face_slice, unsigned level,
961 unsigned width_t, unsigned height_t,
962 enum lp_texture_layout layout)
963 {
964 const unsigned start = face_slice * lpr->tiles_per_image[level];
965 unsigned i;
966
967 for (i = 0; i < width_t * height_t; i++) {
968 lpr->layout[level][start + i] = layout;
969 }
970 }
971
972
973 /**
974 * Allocate storage for a linear or tile texture image (all cube
975 * faces and all 3D slices, all levels).
976 */
977 static void
978 alloc_image_data(struct llvmpipe_resource *lpr,
979 enum lp_texture_layout layout)
980 {
981 uint alignment = MAX2(16, util_cpu_caps.cacheline);
982 uint level;
983 uint offset = 0;
984
985 if (lpr->dt)
986 assert(lpr->base.last_level == 0);
987
988 if (layout == LP_TEX_LAYOUT_TILED) {
989 /* tiled data is stored in regular memory */
990 for (level = 0; level <= lpr->base.last_level; level++) {
991 uint buffer_size = tex_image_size(lpr, level, layout);
992 lpr->tiled_mip_offsets[level] = offset;
993 offset += align(buffer_size, alignment);
994 }
995 lpr->tiled_img.data = align_malloc(offset, alignment);
996 if (lpr->tiled_img.data) {
997 memset(lpr->tiled_img.data, 0, offset);
998 }
999 }
1000 else {
1001 assert(layout == LP_TEX_LAYOUT_LINEAR);
1002 if (lpr->dt) {
1003 /* we get the linear memory from the winsys, and it has
1004 * already been zeroed
1005 */
1006 struct llvmpipe_screen *screen = llvmpipe_screen(lpr->base.screen);
1007 struct sw_winsys *winsys = screen->winsys;
1008
1009 lpr->linear_img.data =
1010 winsys->displaytarget_map(winsys, lpr->dt,
1011 PIPE_TRANSFER_READ_WRITE);
1012 }
1013 else {
1014 /* not a display target - allocate regular memory */
1015 /*
1016 * Offset calculation for start of a specific mip/layer is always
1017 * offset = lpr->linear_mip_offsets[level] + lpr->img_stride[level] * layer
1018 */
1019 for (level = 0; level <= lpr->base.last_level; level++) {
1020 uint buffer_size = tex_image_size(lpr, level, LP_TEX_LAYOUT_LINEAR);
1021 lpr->linear_mip_offsets[level] = offset;
1022 offset += align(buffer_size, alignment);
1023 }
1024 lpr->linear_img.data = align_malloc(offset, alignment);
1025 if (lpr->linear_img.data) {
1026 memset(lpr->linear_img.data, 0, offset);
1027 }
1028 }
1029 }
1030 }
1031
1032
1033
1034 /**
1035 * Return pointer to texture image data (either linear or tiled layout)
1036 * for a particular cube face or 3D texture slice.
1037 *
1038 * \param face_slice the cube face or 3D slice of interest
1039 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
1040 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
1041 */
1042 void *
1043 llvmpipe_get_texture_image(struct llvmpipe_resource *lpr,
1044 unsigned face_slice, unsigned level,
1045 enum lp_texture_usage usage,
1046 enum lp_texture_layout layout)
1047 {
1048 /*
1049 * 'target' refers to the image which we're retrieving (either in
1050 * tiled or linear layout).
1051 * 'other' refers to the same image but in the other layout. (it may
1052 * or may not exist.
1053 */
1054 struct llvmpipe_texture_image *target_img;
1055 struct llvmpipe_texture_image *other_img;
1056 void *target_data;
1057 void *other_data;
1058 const unsigned width = u_minify(lpr->base.width0, level);
1059 const unsigned height = u_minify(lpr->base.height0, level);
1060 const unsigned width_t = align(width, TILE_SIZE) / TILE_SIZE;
1061 const unsigned height_t = align(height, TILE_SIZE) / TILE_SIZE;
1062 unsigned target_offset, other_offset;
1063 unsigned *target_off_ptr, *other_off_ptr;
1064 enum lp_texture_layout other_layout;
1065 boolean only_allocate;
1066
1067 assert(layout == LP_TEX_LAYOUT_NONE ||
1068 layout == LP_TEX_LAYOUT_TILED ||
1069 layout == LP_TEX_LAYOUT_LINEAR);
1070
1071 assert(usage == LP_TEX_USAGE_READ ||
1072 usage == LP_TEX_USAGE_READ_WRITE ||
1073 usage == LP_TEX_USAGE_WRITE_ALL);
1074
1075 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
1076 if (layout == LP_TEX_LAYOUT_NONE) {
1077 only_allocate = TRUE;
1078 layout = LP_TEX_LAYOUT_TILED;
1079 }
1080 else {
1081 only_allocate = FALSE;
1082 }
1083
1084 if (lpr->dt) {
1085 assert(lpr->linear_img.data);
1086 }
1087
1088 /* which is target? which is other? */
1089 if (layout == LP_TEX_LAYOUT_LINEAR) {
1090 target_img = &lpr->linear_img;
1091 target_off_ptr = lpr->linear_mip_offsets;
1092 other_img = &lpr->tiled_img;
1093 other_off_ptr = lpr->tiled_mip_offsets;
1094 other_layout = LP_TEX_LAYOUT_TILED;
1095 }
1096 else {
1097 target_img = &lpr->tiled_img;
1098 target_off_ptr = lpr->tiled_mip_offsets;
1099 other_img = &lpr->linear_img;
1100 other_off_ptr = lpr->linear_mip_offsets;
1101 other_layout = LP_TEX_LAYOUT_LINEAR;
1102 }
1103
1104 target_data = target_img->data;
1105 other_data = other_img->data;
1106
1107 if (!target_data) {
1108 /* allocate memory for the target image now */
1109 alloc_image_data(lpr, layout);
1110 target_data = target_img->data;
1111 }
1112
1113 target_offset = target_off_ptr[level];
1114 other_offset = other_off_ptr[level];
1115
1116 if (face_slice > 0) {
1117 target_offset += face_slice * tex_image_face_size(lpr, level, layout);
1118 other_offset += face_slice * tex_image_face_size(lpr, level, other_layout);
1119 }
1120
1121 if (target_data) {
1122 target_data = (uint8_t *) target_data + target_offset;
1123 }
1124 if (other_data) {
1125 other_data = (uint8_t *) other_data + other_offset;
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_img;
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, 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_img;
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, 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_img;
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, 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_img.data)
1419 size += tex_image_size(lpr, lvl, LP_TEX_LAYOUT_LINEAR);
1420
1421 if (lpr->tiled_img.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->transfer_map = llvmpipe_transfer_map;
1478 pipe->transfer_unmap = llvmpipe_transfer_unmap;
1479
1480 pipe->transfer_flush_region = u_default_transfer_flush_region;
1481 pipe->transfer_inline_write = u_default_transfer_inline_write;
1482
1483 pipe->create_surface = llvmpipe_create_surface;
1484 pipe->surface_destroy = llvmpipe_surface_destroy;
1485 }