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