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