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