gallium: add user_ptr in pipe_resource
[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->base.user_ptr = ptr;
774 buffer->userBuffer = TRUE;
775 buffer->data = ptr;
776
777 return &buffer->base;
778 }
779
780
781 /**
782 * Compute size (in bytes) need to store a texture image / mipmap level,
783 * for just one cube face or one 3D texture slice
784 */
785 static unsigned
786 tex_image_face_size(const struct llvmpipe_resource *lpr, unsigned level,
787 enum lp_texture_layout layout)
788 {
789 const unsigned width = u_minify(lpr->base.width0, level);
790 const unsigned height = u_minify(lpr->base.height0, level);
791
792 assert(layout == LP_TEX_LAYOUT_TILED ||
793 layout == LP_TEX_LAYOUT_LINEAR);
794
795 if (layout == LP_TEX_LAYOUT_TILED) {
796 /* for tiled layout, force a 32bpp format */
797 const enum pipe_format format = PIPE_FORMAT_B8G8R8A8_UNORM;
798 const unsigned block_size = util_format_get_blocksize(format);
799 const unsigned nblocksy =
800 util_format_get_nblocksy(format, align(height, TILE_SIZE));
801 const unsigned nblocksx =
802 util_format_get_nblocksx(format, align(width, TILE_SIZE));
803 const unsigned buffer_size = block_size * nblocksy * nblocksx;
804 return buffer_size;
805 }
806 else {
807 /* we already computed this */
808 return lpr->img_stride[level];
809 }
810 }
811
812
813 /**
814 * Compute size (in bytes) need to store a texture image / mipmap level,
815 * including all cube faces or 3D image slices
816 */
817 static unsigned
818 tex_image_size(const struct llvmpipe_resource *lpr, unsigned level,
819 enum lp_texture_layout layout)
820 {
821 const unsigned buf_size = tex_image_face_size(lpr, level, layout);
822 return buf_size * lpr->num_slices_faces[level];
823 }
824
825
826 /**
827 * This function encapsulates some complicated logic for determining
828 * how to convert a tile of image data from linear layout to tiled
829 * layout, or vice versa.
830 * \param cur_layout the current tile layout
831 * \param target_layout the desired tile layout
832 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
833 * \param new_layout_return returns the new layout mode
834 * \param convert_return returns TRUE if image conversion is needed
835 */
836 static void
837 layout_logic(enum lp_texture_layout cur_layout,
838 enum lp_texture_layout target_layout,
839 enum lp_texture_usage usage,
840 enum lp_texture_layout *new_layout_return,
841 boolean *convert)
842 {
843 enum lp_texture_layout other_layout, new_layout;
844
845 *convert = FALSE;
846
847 new_layout = 99; /* debug check */
848
849 if (target_layout == LP_TEX_LAYOUT_LINEAR) {
850 other_layout = LP_TEX_LAYOUT_TILED;
851 }
852 else {
853 assert(target_layout == LP_TEX_LAYOUT_TILED);
854 other_layout = LP_TEX_LAYOUT_LINEAR;
855 }
856
857 new_layout = target_layout; /* may get changed below */
858
859 if (cur_layout == LP_TEX_LAYOUT_BOTH) {
860 if (usage == LP_TEX_USAGE_READ) {
861 new_layout = LP_TEX_LAYOUT_BOTH;
862 }
863 }
864 else if (cur_layout == other_layout) {
865 if (usage != LP_TEX_USAGE_WRITE_ALL) {
866 /* need to convert tiled data to linear or vice versa */
867 *convert = TRUE;
868
869 if (usage == LP_TEX_USAGE_READ)
870 new_layout = LP_TEX_LAYOUT_BOTH;
871 }
872 }
873 else {
874 assert(cur_layout == LP_TEX_LAYOUT_NONE ||
875 cur_layout == target_layout);
876 }
877
878 assert(new_layout == LP_TEX_LAYOUT_BOTH ||
879 new_layout == target_layout);
880
881 *new_layout_return = new_layout;
882 }
883
884
885 /**
886 * Return pointer to a 2D texture image/face/slice.
887 * No tiled/linear conversion is done.
888 */
889 ubyte *
890 llvmpipe_get_texture_image_address(struct llvmpipe_resource *lpr,
891 unsigned face_slice, unsigned level,
892 enum lp_texture_layout layout)
893 {
894 struct llvmpipe_texture_image *img;
895 unsigned offset;
896
897 if (layout == LP_TEX_LAYOUT_LINEAR) {
898 img = &lpr->linear[level];
899 }
900 else {
901 assert (layout == LP_TEX_LAYOUT_TILED);
902 img = &lpr->tiled[level];
903 }
904
905 if (face_slice > 0)
906 offset = face_slice * tex_image_face_size(lpr, level, layout);
907 else
908 offset = 0;
909
910 return (ubyte *) img->data + offset;
911 }
912
913
914 static INLINE enum lp_texture_layout
915 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource *lpr,
916 unsigned face_slice, unsigned level,
917 unsigned x, unsigned y)
918 {
919 uint i;
920 assert(resource_is_texture(&lpr->base));
921 assert(x < lpr->tiles_per_row[level]);
922 i = face_slice * lpr->tiles_per_image[level]
923 + y * lpr->tiles_per_row[level] + x;
924 return lpr->layout[level][i];
925 }
926
927
928 static INLINE void
929 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource *lpr,
930 unsigned face_slice, unsigned level,
931 unsigned x, unsigned y,
932 enum lp_texture_layout layout)
933 {
934 uint i;
935 assert(resource_is_texture(&lpr->base));
936 assert(x < lpr->tiles_per_row[level]);
937 i = face_slice * lpr->tiles_per_image[level]
938 + y * lpr->tiles_per_row[level] + x;
939 lpr->layout[level][i] = layout;
940 }
941
942
943 /**
944 * Set the layout mode for all tiles in a particular image.
945 */
946 static INLINE void
947 llvmpipe_set_texture_image_layout(struct llvmpipe_resource *lpr,
948 unsigned face_slice, unsigned level,
949 unsigned width_t, unsigned height_t,
950 enum lp_texture_layout layout)
951 {
952 const unsigned start = face_slice * lpr->tiles_per_image[level];
953 unsigned i;
954
955 for (i = 0; i < width_t * height_t; i++) {
956 lpr->layout[level][start + i] = layout;
957 }
958 }
959
960
961 /**
962 * Allocate storage for a linear or tile texture image (all cube
963 * faces and all 3D slices.
964 */
965 static void
966 alloc_image_data(struct llvmpipe_resource *lpr, unsigned level,
967 enum lp_texture_layout layout)
968 {
969 uint alignment = MAX2(16, util_cpu_caps.cacheline);
970
971 if (lpr->dt)
972 assert(level == 0);
973
974 if (layout == LP_TEX_LAYOUT_TILED) {
975 /* tiled data is stored in regular memory */
976 uint buffer_size = tex_image_size(lpr, level, layout);
977 lpr->tiled[level].data = align_malloc(buffer_size, alignment);
978 if (lpr->tiled[level].data) {
979 memset(lpr->tiled[level].data, 0, buffer_size);
980 }
981 }
982 else {
983 assert(layout == LP_TEX_LAYOUT_LINEAR);
984 if (lpr->dt) {
985 /* we get the linear memory from the winsys, and it has
986 * already been zeroed
987 */
988 struct llvmpipe_screen *screen = llvmpipe_screen(lpr->base.screen);
989 struct sw_winsys *winsys = screen->winsys;
990
991 lpr->linear[0].data =
992 winsys->displaytarget_map(winsys, lpr->dt,
993 PIPE_TRANSFER_READ_WRITE);
994 }
995 else {
996 /* not a display target - allocate regular memory */
997 uint buffer_size = tex_image_size(lpr, level, LP_TEX_LAYOUT_LINEAR);
998 lpr->linear[level].data = align_malloc(buffer_size, alignment);
999 if (lpr->linear[level].data) {
1000 memset(lpr->linear[level].data, 0, buffer_size);
1001 }
1002 }
1003 }
1004 }
1005
1006
1007
1008 /**
1009 * Return pointer to texture image data (either linear or tiled layout)
1010 * for a particular cube face or 3D texture slice.
1011 *
1012 * \param face_slice the cube face or 3D slice of interest
1013 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
1014 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
1015 */
1016 void *
1017 llvmpipe_get_texture_image(struct llvmpipe_resource *lpr,
1018 unsigned face_slice, unsigned level,
1019 enum lp_texture_usage usage,
1020 enum lp_texture_layout layout)
1021 {
1022 /*
1023 * 'target' refers to the image which we're retrieving (either in
1024 * tiled or linear layout).
1025 * 'other' refers to the same image but in the other layout. (it may
1026 * or may not exist.
1027 */
1028 struct llvmpipe_texture_image *target_img;
1029 struct llvmpipe_texture_image *other_img;
1030 void *target_data;
1031 void *other_data;
1032 const unsigned width = u_minify(lpr->base.width0, level);
1033 const unsigned height = u_minify(lpr->base.height0, level);
1034 const unsigned width_t = align(width, TILE_SIZE) / TILE_SIZE;
1035 const unsigned height_t = align(height, TILE_SIZE) / TILE_SIZE;
1036 enum lp_texture_layout other_layout;
1037 boolean only_allocate;
1038
1039 assert(layout == LP_TEX_LAYOUT_NONE ||
1040 layout == LP_TEX_LAYOUT_TILED ||
1041 layout == LP_TEX_LAYOUT_LINEAR);
1042
1043 assert(usage == LP_TEX_USAGE_READ ||
1044 usage == LP_TEX_USAGE_READ_WRITE ||
1045 usage == LP_TEX_USAGE_WRITE_ALL);
1046
1047 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
1048 if (layout == LP_TEX_LAYOUT_NONE) {
1049 only_allocate = TRUE;
1050 layout = LP_TEX_LAYOUT_TILED;
1051 }
1052 else {
1053 only_allocate = FALSE;
1054 }
1055
1056 if (lpr->dt) {
1057 assert(lpr->linear[level].data);
1058 }
1059
1060 /* which is target? which is other? */
1061 if (layout == LP_TEX_LAYOUT_LINEAR) {
1062 target_img = &lpr->linear[level];
1063 other_img = &lpr->tiled[level];
1064 other_layout = LP_TEX_LAYOUT_TILED;
1065 }
1066 else {
1067 target_img = &lpr->tiled[level];
1068 other_img = &lpr->linear[level];
1069 other_layout = LP_TEX_LAYOUT_LINEAR;
1070 }
1071
1072 target_data = target_img->data;
1073 other_data = other_img->data;
1074
1075 if (!target_data) {
1076 /* allocate memory for the target image now */
1077 alloc_image_data(lpr, level, layout);
1078 target_data = target_img->data;
1079 }
1080
1081 if (face_slice > 0) {
1082 unsigned target_offset, other_offset;
1083
1084 target_offset = face_slice * tex_image_face_size(lpr, level, layout);
1085 other_offset = face_slice * tex_image_face_size(lpr, level, other_layout);
1086 if (target_data) {
1087 target_data = (uint8_t *) target_data + target_offset;
1088 }
1089 if (other_data) {
1090 other_data = (uint8_t *) other_data + other_offset;
1091 }
1092 }
1093
1094 if (only_allocate) {
1095 /* Just allocating tiled memory. Don't initialize it from the
1096 * linear data if it exists.
1097 */
1098 return target_data;
1099 }
1100
1101 if (other_data) {
1102 /* may need to convert other data to the requested layout */
1103 enum lp_texture_layout new_layout;
1104 unsigned x, y;
1105
1106 /* loop over all image tiles, doing layout conversion where needed */
1107 for (y = 0; y < height_t; y++) {
1108 for (x = 0; x < width_t; x++) {
1109 enum lp_texture_layout cur_layout =
1110 llvmpipe_get_texture_tile_layout(lpr, face_slice, level, x, y);
1111 boolean convert;
1112
1113 layout_logic(cur_layout, layout, usage, &new_layout, &convert);
1114
1115 if (convert && other_data && target_data) {
1116 if (layout == LP_TEX_LAYOUT_TILED) {
1117 lp_linear_to_tiled(other_data, target_data,
1118 x * TILE_SIZE, y * TILE_SIZE,
1119 TILE_SIZE, TILE_SIZE,
1120 lpr->base.format,
1121 lpr->row_stride[level],
1122 lpr->tiles_per_row[level]);
1123 }
1124 else {
1125 assert(layout == LP_TEX_LAYOUT_LINEAR);
1126 lp_tiled_to_linear(other_data, target_data,
1127 x * TILE_SIZE, y * TILE_SIZE,
1128 TILE_SIZE, TILE_SIZE,
1129 lpr->base.format,
1130 lpr->row_stride[level],
1131 lpr->tiles_per_row[level]);
1132 }
1133 }
1134
1135 if (new_layout != cur_layout)
1136 llvmpipe_set_texture_tile_layout(lpr, face_slice, level, x, y,
1137 new_layout);
1138 }
1139 }
1140 }
1141 else {
1142 /* no other data */
1143 llvmpipe_set_texture_image_layout(lpr, face_slice, level,
1144 width_t, height_t, layout);
1145 }
1146
1147 return target_data;
1148 }
1149
1150
1151 /**
1152 * Return pointer to start of a texture image (1D, 2D, 3D, CUBE).
1153 * All cube faces and 3D slices will be converted to the requested
1154 * layout if needed.
1155 * This is typically used when we're about to sample from a texture.
1156 */
1157 void *
1158 llvmpipe_get_texture_image_all(struct llvmpipe_resource *lpr,
1159 unsigned level,
1160 enum lp_texture_usage usage,
1161 enum lp_texture_layout layout)
1162 {
1163 const int slices = lpr->num_slices_faces[level];
1164 int slice;
1165 void *map = NULL;
1166
1167 assert(slices > 0);
1168
1169 for (slice = slices - 1; slice >= 0; slice--) {
1170 map = llvmpipe_get_texture_image(lpr, slice, level, usage, layout);
1171 }
1172
1173 return map;
1174 }
1175
1176
1177 /**
1178 * Get pointer to a linear image (not the tile!) where the tile at (x,y)
1179 * is known to be in linear layout.
1180 * Conversion from tiled to linear will be done if necessary.
1181 * \return pointer to start of image/face (not the tile)
1182 */
1183 ubyte *
1184 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource *lpr,
1185 unsigned face_slice, unsigned level,
1186 enum lp_texture_usage usage,
1187 unsigned x, unsigned y)
1188 {
1189 struct llvmpipe_texture_image *linear_img = &lpr->linear[level];
1190 enum lp_texture_layout cur_layout, new_layout;
1191 const unsigned tx = x / TILE_SIZE, ty = y / TILE_SIZE;
1192 boolean convert;
1193 uint8_t *tiled_image, *linear_image;
1194
1195 assert(resource_is_texture(&lpr->base));
1196 assert(x % TILE_SIZE == 0);
1197 assert(y % TILE_SIZE == 0);
1198
1199 if (!linear_img->data) {
1200 /* allocate memory for the linear image now */
1201 alloc_image_data(lpr, level, LP_TEX_LAYOUT_LINEAR);
1202 }
1203
1204 /* compute address of the slice/face of the image that contains the tile */
1205 tiled_image = llvmpipe_get_texture_image_address(lpr, face_slice, level,
1206 LP_TEX_LAYOUT_TILED);
1207 linear_image = llvmpipe_get_texture_image_address(lpr, face_slice, level,
1208 LP_TEX_LAYOUT_LINEAR);
1209
1210 /* get current tile layout and determine if data conversion is needed */
1211 cur_layout = llvmpipe_get_texture_tile_layout(lpr, face_slice, level, tx, ty);
1212
1213 layout_logic(cur_layout, LP_TEX_LAYOUT_LINEAR, usage,
1214 &new_layout, &convert);
1215
1216 if (convert && tiled_image && linear_image) {
1217 lp_tiled_to_linear(tiled_image, linear_image,
1218 x, y, TILE_SIZE, TILE_SIZE, lpr->base.format,
1219 lpr->row_stride[level],
1220 lpr->tiles_per_row[level]);
1221 }
1222
1223 if (new_layout != cur_layout)
1224 llvmpipe_set_texture_tile_layout(lpr, face_slice, level, tx, ty, new_layout);
1225
1226 return linear_image;
1227 }
1228
1229
1230 /**
1231 * Get pointer to tiled data for rendering.
1232 * \return pointer to the tiled data at the given tile position
1233 */
1234 ubyte *
1235 llvmpipe_get_texture_tile(struct llvmpipe_resource *lpr,
1236 unsigned face_slice, unsigned level,
1237 enum lp_texture_usage usage,
1238 unsigned x, unsigned y)
1239 {
1240 struct llvmpipe_texture_image *tiled_img = &lpr->tiled[level];
1241 enum lp_texture_layout cur_layout, new_layout;
1242 const unsigned tx = x / TILE_SIZE, ty = y / TILE_SIZE;
1243 boolean convert;
1244 uint8_t *tiled_image, *linear_image;
1245 unsigned tile_offset;
1246
1247 assert(x % TILE_SIZE == 0);
1248 assert(y % TILE_SIZE == 0);
1249
1250 if (!tiled_img->data) {
1251 /* allocate memory for the tiled image now */
1252 alloc_image_data(lpr, level, LP_TEX_LAYOUT_TILED);
1253 }
1254
1255 /* compute address of the slice/face of the image that contains the tile */
1256 tiled_image = llvmpipe_get_texture_image_address(lpr, face_slice, level,
1257 LP_TEX_LAYOUT_TILED);
1258 linear_image = llvmpipe_get_texture_image_address(lpr, face_slice, level,
1259 LP_TEX_LAYOUT_LINEAR);
1260
1261 /* get current tile layout and see if we need to convert the data */
1262 cur_layout = llvmpipe_get_texture_tile_layout(lpr, face_slice, level, tx, ty);
1263
1264 layout_logic(cur_layout, LP_TEX_LAYOUT_TILED, usage, &new_layout, &convert);
1265 if (convert && linear_image && tiled_image) {
1266 lp_linear_to_tiled(linear_image, tiled_image,
1267 x, y, TILE_SIZE, TILE_SIZE, lpr->base.format,
1268 lpr->row_stride[level],
1269 lpr->tiles_per_row[level]);
1270 }
1271
1272 if (!tiled_image)
1273 return NULL;
1274
1275 if (new_layout != cur_layout)
1276 llvmpipe_set_texture_tile_layout(lpr, face_slice, level, tx, ty, new_layout);
1277
1278 /* compute, return address of the 64x64 tile */
1279 tile_offset = (ty * lpr->tiles_per_row[level] + tx)
1280 * TILE_SIZE * TILE_SIZE * 4;
1281
1282 return (ubyte *) tiled_image + tile_offset;
1283 }
1284
1285
1286 /**
1287 * Get pointer to tiled data for rendering.
1288 * \return pointer to the tiled data at the given tile position
1289 */
1290 void
1291 llvmpipe_unswizzle_cbuf_tile(struct llvmpipe_resource *lpr,
1292 unsigned face_slice, unsigned level,
1293 unsigned x, unsigned y,
1294 uint8_t *tile)
1295 {
1296 struct llvmpipe_texture_image *linear_img = &lpr->linear[level];
1297 const unsigned tx = x / TILE_SIZE, ty = y / TILE_SIZE;
1298 uint8_t *linear_image;
1299
1300 assert(x % TILE_SIZE == 0);
1301 assert(y % TILE_SIZE == 0);
1302
1303 if (!linear_img->data) {
1304 /* allocate memory for the linear image now */
1305 alloc_image_data(lpr, level, LP_TEX_LAYOUT_LINEAR);
1306 }
1307
1308 /* compute address of the slice/face of the image that contains the tile */
1309 linear_image = llvmpipe_get_texture_image_address(lpr, face_slice, level,
1310 LP_TEX_LAYOUT_LINEAR);
1311
1312 {
1313 uint ii = x, jj = y;
1314 uint tile_offset = jj / TILE_SIZE + ii / TILE_SIZE;
1315 uint byte_offset = tile_offset * TILE_SIZE * TILE_SIZE * 4;
1316
1317 /* Note that lp_tiled_to_linear expects the tile parameter to
1318 * point at the first tile in a whole-image sized array. In
1319 * this code, we have only a single tile and have to do some
1320 * pointer arithmetic to figure out where the "image" would have
1321 * started.
1322 */
1323 lp_tiled_to_linear(tile - byte_offset, linear_image,
1324 x, y, TILE_SIZE, TILE_SIZE,
1325 lpr->base.format,
1326 lpr->row_stride[level],
1327 1); /* tiles per row */
1328 }
1329
1330 llvmpipe_set_texture_tile_layout(lpr, face_slice, level, tx, ty,
1331 LP_TEX_LAYOUT_LINEAR);
1332 }
1333
1334
1335 /**
1336 * Get pointer to tiled data for rendering.
1337 * \return pointer to the tiled data at the given tile position
1338 */
1339 void
1340 llvmpipe_swizzle_cbuf_tile(struct llvmpipe_resource *lpr,
1341 unsigned face_slice, unsigned level,
1342 unsigned x, unsigned y,
1343 uint8_t *tile)
1344 {
1345 uint8_t *linear_image;
1346
1347 assert(x % TILE_SIZE == 0);
1348 assert(y % TILE_SIZE == 0);
1349
1350 /* compute address of the slice/face of the image that contains the tile */
1351 linear_image = llvmpipe_get_texture_image_address(lpr, face_slice, level,
1352 LP_TEX_LAYOUT_LINEAR);
1353
1354 if (linear_image) {
1355 uint ii = x, jj = y;
1356 uint tile_offset = jj / TILE_SIZE + ii / TILE_SIZE;
1357 uint byte_offset = tile_offset * TILE_SIZE * TILE_SIZE * 4;
1358
1359 /* Note that lp_linear_to_tiled expects the tile parameter to
1360 * point at the first tile in a whole-image sized array. In
1361 * this code, we have only a single tile and have to do some
1362 * pointer arithmetic to figure out where the "image" would have
1363 * started.
1364 */
1365 lp_linear_to_tiled(linear_image, tile - byte_offset,
1366 x, y, TILE_SIZE, TILE_SIZE,
1367 lpr->base.format,
1368 lpr->row_stride[level],
1369 1); /* tiles per row */
1370 }
1371 }
1372
1373
1374 /**
1375 * Return size of resource in bytes
1376 */
1377 unsigned
1378 llvmpipe_resource_size(const struct pipe_resource *resource)
1379 {
1380 const struct llvmpipe_resource *lpr = llvmpipe_resource_const(resource);
1381 unsigned lvl, size = 0;
1382
1383 for (lvl = 0; lvl <= lpr->base.last_level; lvl++) {
1384 if (lpr->linear[lvl].data)
1385 size += tex_image_size(lpr, lvl, LP_TEX_LAYOUT_LINEAR);
1386
1387 if (lpr->tiled[lvl].data)
1388 size += tex_image_size(lpr, lvl, LP_TEX_LAYOUT_TILED);
1389 }
1390
1391 return size;
1392 }
1393
1394
1395 #ifdef DEBUG
1396 void
1397 llvmpipe_print_resources(void)
1398 {
1399 struct llvmpipe_resource *lpr;
1400 unsigned n = 0, total = 0;
1401
1402 debug_printf("LLVMPIPE: current resources:\n");
1403 foreach(lpr, &resource_list) {
1404 unsigned size = llvmpipe_resource_size(&lpr->base);
1405 debug_printf("resource %u at %p, size %ux%ux%u: %u bytes, refcount %u\n",
1406 lpr->id, (void *) lpr,
1407 lpr->base.width0, lpr->base.height0, lpr->base.depth0,
1408 size, lpr->base.reference.count);
1409 total += size;
1410 n++;
1411 }
1412 debug_printf("LLVMPIPE: total size of %u resources: %u\n", n, total);
1413 }
1414 #endif
1415
1416
1417 void
1418 llvmpipe_init_screen_resource_funcs(struct pipe_screen *screen)
1419 {
1420 #ifdef DEBUG
1421 /* init linked list for tracking resources */
1422 {
1423 static boolean first_call = TRUE;
1424 if (first_call) {
1425 memset(&resource_list, 0, sizeof(resource_list));
1426 make_empty_list(&resource_list);
1427 first_call = FALSE;
1428 }
1429 }
1430 #endif
1431
1432 screen->resource_create = llvmpipe_resource_create;
1433 screen->resource_destroy = llvmpipe_resource_destroy;
1434 screen->resource_from_handle = llvmpipe_resource_from_handle;
1435 screen->resource_get_handle = llvmpipe_resource_get_handle;
1436 screen->user_buffer_create = llvmpipe_user_buffer_create;
1437
1438 }
1439
1440
1441 void
1442 llvmpipe_init_context_resource_funcs(struct pipe_context *pipe)
1443 {
1444 pipe->get_transfer = llvmpipe_get_transfer;
1445 pipe->transfer_destroy = llvmpipe_transfer_destroy;
1446 pipe->transfer_map = llvmpipe_transfer_map;
1447 pipe->transfer_unmap = llvmpipe_transfer_unmap;
1448
1449 pipe->transfer_flush_region = u_default_transfer_flush_region;
1450 pipe->transfer_inline_write = u_default_transfer_inline_write;
1451
1452 pipe->create_surface = llvmpipe_create_surface;
1453 pipe->surface_destroy = llvmpipe_surface_destroy;
1454 }