llvmpipe: don't store number of layers per level
[mesa.git] / src / gallium / drivers / llvmpipe / lp_texture.c
1 /**************************************************************************
2 *
3 * Copyright 2006 VMware, Inc.
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 VMWARE 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 <keithw@vmware.com>
30 * Michel Dänzer <daenzer@vmware.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_texture.h"
50 #include "lp_setup.h"
51 #include "lp_state.h"
52 #include "lp_rast.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 * Conventional allocation path for non-display textures:
65 * Compute strides and allocate data (unless asked not to).
66 */
67 static boolean
68 llvmpipe_texture_layout(struct llvmpipe_screen *screen,
69 struct llvmpipe_resource *lpr,
70 boolean allocate)
71 {
72 struct pipe_resource *pt = &lpr->base;
73 unsigned level;
74 unsigned width = pt->width0;
75 unsigned height = pt->height0;
76 unsigned depth = pt->depth0;
77 uint64_t total_size = 0;
78 unsigned layers = pt->array_size;
79 /* XXX:
80 * This alignment here (same for displaytarget) was added for the purpose of
81 * ARB_map_buffer_alignment. I am not convinced it's needed for non-buffer
82 * resources. Otherwise we'd want the max of cacheline size and 16 (max size
83 * of a block for all formats) though this should not be strictly necessary
84 * neither. In any case it can only affect compressed or 1d textures.
85 */
86 unsigned mip_align = MAX2(64, util_cpu_caps.cacheline);
87
88 assert(LP_MAX_TEXTURE_2D_LEVELS <= LP_MAX_TEXTURE_LEVELS);
89 assert(LP_MAX_TEXTURE_3D_LEVELS <= LP_MAX_TEXTURE_LEVELS);
90
91 for (level = 0; level <= pt->last_level; level++) {
92 uint64_t mipsize;
93 unsigned align_x, align_y, nblocksx, nblocksy, block_size, num_slices;
94
95 /* Row stride and image stride */
96
97 /* For non-compressed formats we need 4x4 pixel alignment
98 * so we can read/write LP_RASTER_BLOCK_SIZE when rendering to them.
99 * We also want cache line size in x direction,
100 * otherwise same cache line could end up in multiple threads.
101 * For explicit 1d resources however we reduce this to 4x1 and
102 * handle specially in render output code (as we need to do special
103 * handling there for buffers in any case).
104 */
105 if (util_format_is_compressed(pt->format))
106 align_x = align_y = 1;
107 else {
108 align_x = LP_RASTER_BLOCK_SIZE;
109 if (llvmpipe_resource_is_1d(&lpr->base))
110 align_y = 1;
111 else
112 align_y = LP_RASTER_BLOCK_SIZE;
113 }
114
115 nblocksx = util_format_get_nblocksx(pt->format,
116 align(width, align_x));
117 nblocksy = util_format_get_nblocksy(pt->format,
118 align(height, align_y));
119 block_size = util_format_get_blocksize(pt->format);
120
121 if (util_format_is_compressed(pt->format))
122 lpr->row_stride[level] = nblocksx * block_size;
123 else
124 lpr->row_stride[level] = align(nblocksx * block_size, util_cpu_caps.cacheline);
125
126 /* if row_stride * height > LP_MAX_TEXTURE_SIZE */
127 if ((uint64_t)lpr->row_stride[level] * nblocksy > LP_MAX_TEXTURE_SIZE) {
128 /* image too large */
129 goto fail;
130 }
131
132 lpr->img_stride[level] = lpr->row_stride[level] * nblocksy;
133
134 /* Number of 3D image slices, cube faces or texture array layers */
135 if (lpr->base.target == PIPE_TEXTURE_CUBE)
136 num_slices = 6;
137 else if (lpr->base.target == PIPE_TEXTURE_3D)
138 num_slices = depth;
139 else if (lpr->base.target == PIPE_TEXTURE_1D_ARRAY ||
140 lpr->base.target == PIPE_TEXTURE_2D_ARRAY)
141 num_slices = layers;
142 else
143 num_slices = 1;
144
145 /* if img_stride * num_slices_faces > LP_MAX_TEXTURE_SIZE */
146 mipsize = (uint64_t)lpr->img_stride[level] * num_slices;
147 if (mipsize > LP_MAX_TEXTURE_SIZE) {
148 /* volume too large */
149 goto fail;
150 }
151
152 lpr->mip_offsets[level] = total_size;
153
154 total_size += align((unsigned)mipsize, mip_align);
155 if (total_size > LP_MAX_TEXTURE_SIZE) {
156 goto fail;
157 }
158
159 /* Compute size of next mipmap level */
160 width = u_minify(width, 1);
161 height = u_minify(height, 1);
162 depth = u_minify(depth, 1);
163 }
164
165 if (allocate) {
166 lpr->tex_data = align_malloc(total_size, mip_align);
167 if (!lpr->tex_data) {
168 return FALSE;
169 }
170 else {
171 memset(lpr->tex_data, 0, total_size);
172 }
173 }
174
175 return TRUE;
176
177 fail:
178 return FALSE;
179 }
180
181
182 /**
183 * Check the size of the texture specified by 'res'.
184 * \return TRUE if OK, FALSE if too large.
185 */
186 static boolean
187 llvmpipe_can_create_resource(struct pipe_screen *screen,
188 const struct pipe_resource *res)
189 {
190 struct llvmpipe_resource lpr;
191 memset(&lpr, 0, sizeof(lpr));
192 lpr.base = *res;
193 return llvmpipe_texture_layout(llvmpipe_screen(screen), &lpr, false);
194 }
195
196
197 static boolean
198 llvmpipe_displaytarget_layout(struct llvmpipe_screen *screen,
199 struct llvmpipe_resource *lpr)
200 {
201 struct sw_winsys *winsys = screen->winsys;
202
203 /* Round up the surface size to a multiple of the tile size to
204 * avoid tile clipping.
205 */
206 const unsigned width = MAX2(1, align(lpr->base.width0, TILE_SIZE));
207 const unsigned height = MAX2(1, align(lpr->base.height0, TILE_SIZE));
208
209 lpr->dt = winsys->displaytarget_create(winsys,
210 lpr->base.bind,
211 lpr->base.format,
212 width, height,
213 64,
214 &lpr->row_stride[0] );
215
216 if (lpr->dt == NULL)
217 return FALSE;
218
219 {
220 void *map = winsys->displaytarget_map(winsys, lpr->dt,
221 PIPE_TRANSFER_WRITE);
222
223 if (map)
224 memset(map, 0, height * lpr->row_stride[0]);
225
226 winsys->displaytarget_unmap(winsys, lpr->dt);
227 }
228
229 return TRUE;
230 }
231
232
233 static struct pipe_resource *
234 llvmpipe_resource_create(struct pipe_screen *_screen,
235 const struct pipe_resource *templat)
236 {
237 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
238 struct llvmpipe_resource *lpr = CALLOC_STRUCT(llvmpipe_resource);
239 if (!lpr)
240 return NULL;
241
242 lpr->base = *templat;
243 pipe_reference_init(&lpr->base.reference, 1);
244 lpr->base.screen = &screen->base;
245
246 /* assert(lpr->base.bind); */
247
248 if (llvmpipe_resource_is_texture(&lpr->base)) {
249 if (lpr->base.bind & (PIPE_BIND_DISPLAY_TARGET |
250 PIPE_BIND_SCANOUT |
251 PIPE_BIND_SHARED)) {
252 /* displayable surface */
253 if (!llvmpipe_displaytarget_layout(screen, lpr))
254 goto fail;
255 }
256 else {
257 /* texture map */
258 if (!llvmpipe_texture_layout(screen, lpr, true))
259 goto fail;
260 }
261 }
262 else {
263 /* other data (vertex buffer, const buffer, etc) */
264 const uint bytes = templat->width0;
265 assert(util_format_get_blocksize(templat->format) == 1);
266 assert(templat->height0 == 1);
267 assert(templat->depth0 == 1);
268 assert(templat->last_level == 0);
269 /*
270 * Reserve some extra storage since if we'd render to a buffer we
271 * read/write always LP_RASTER_BLOCK_SIZE pixels, but the element
272 * offset doesn't need to be aligned to LP_RASTER_BLOCK_SIZE.
273 */
274 lpr->data = align_malloc(bytes + (LP_RASTER_BLOCK_SIZE - 1) * 4 * sizeof(float), 64);
275
276 /*
277 * buffers don't really have stride but it's probably safer
278 * (for code doing same calculations for buffers and textures)
279 * to put something sane in there.
280 */
281 lpr->row_stride[0] = bytes;
282 if (!lpr->data)
283 goto fail;
284 memset(lpr->data, 0, bytes);
285 }
286
287 lpr->id = id_counter++;
288
289 #ifdef DEBUG
290 insert_at_tail(&resource_list, lpr);
291 #endif
292
293 return &lpr->base;
294
295 fail:
296 FREE(lpr);
297 return NULL;
298 }
299
300
301 static void
302 llvmpipe_resource_destroy(struct pipe_screen *pscreen,
303 struct pipe_resource *pt)
304 {
305 struct llvmpipe_screen *screen = llvmpipe_screen(pscreen);
306 struct llvmpipe_resource *lpr = llvmpipe_resource(pt);
307
308 if (lpr->dt) {
309 /* display target */
310 struct sw_winsys *winsys = screen->winsys;
311 winsys->displaytarget_destroy(winsys, lpr->dt);
312 }
313 else if (llvmpipe_resource_is_texture(pt)) {
314 /* free linear image data */
315 if (lpr->tex_data) {
316 align_free(lpr->tex_data);
317 lpr->tex_data = NULL;
318 }
319 }
320 else if (!lpr->userBuffer) {
321 assert(lpr->data);
322 align_free(lpr->data);
323 }
324
325 #ifdef DEBUG
326 if (lpr->next)
327 remove_from_list(lpr);
328 #endif
329
330 FREE(lpr);
331 }
332
333
334 /**
335 * Map a resource for read/write.
336 */
337 void *
338 llvmpipe_resource_map(struct pipe_resource *resource,
339 unsigned level,
340 unsigned layer,
341 enum lp_texture_usage tex_usage)
342 {
343 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
344 uint8_t *map;
345
346 assert(level < LP_MAX_TEXTURE_LEVELS);
347 assert(layer < (u_minify(resource->depth0, level) + resource->array_size - 1));
348
349 assert(tex_usage == LP_TEX_USAGE_READ ||
350 tex_usage == LP_TEX_USAGE_READ_WRITE ||
351 tex_usage == LP_TEX_USAGE_WRITE_ALL);
352
353 if (lpr->dt) {
354 /* display target */
355 struct llvmpipe_screen *screen = llvmpipe_screen(resource->screen);
356 struct sw_winsys *winsys = screen->winsys;
357 unsigned dt_usage;
358
359 if (tex_usage == LP_TEX_USAGE_READ) {
360 dt_usage = PIPE_TRANSFER_READ;
361 }
362 else {
363 dt_usage = PIPE_TRANSFER_READ_WRITE;
364 }
365
366 assert(level == 0);
367 assert(layer == 0);
368
369 /* FIXME: keep map count? */
370 map = winsys->displaytarget_map(winsys, lpr->dt, dt_usage);
371
372 /* install this linear image in texture data structure */
373 lpr->tex_data = map;
374
375 return map;
376 }
377 else if (llvmpipe_resource_is_texture(resource)) {
378
379 map = llvmpipe_get_texture_image_address(lpr, layer, level);
380 return map;
381 }
382 else {
383 return lpr->data;
384 }
385 }
386
387
388 /**
389 * Unmap a resource.
390 */
391 void
392 llvmpipe_resource_unmap(struct pipe_resource *resource,
393 unsigned level,
394 unsigned layer)
395 {
396 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
397
398 if (lpr->dt) {
399 /* display target */
400 struct llvmpipe_screen *lp_screen = llvmpipe_screen(resource->screen);
401 struct sw_winsys *winsys = lp_screen->winsys;
402
403 assert(level == 0);
404 assert(layer == 0);
405
406 winsys->displaytarget_unmap(winsys, lpr->dt);
407 }
408 }
409
410
411 void *
412 llvmpipe_resource_data(struct pipe_resource *resource)
413 {
414 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
415
416 assert(!llvmpipe_resource_is_texture(resource));
417
418 return lpr->data;
419 }
420
421
422 static struct pipe_resource *
423 llvmpipe_resource_from_handle(struct pipe_screen *screen,
424 const struct pipe_resource *template,
425 struct winsys_handle *whandle)
426 {
427 struct sw_winsys *winsys = llvmpipe_screen(screen)->winsys;
428 struct llvmpipe_resource *lpr;
429
430 /* XXX Seems like from_handled depth textures doesn't work that well */
431
432 lpr = CALLOC_STRUCT(llvmpipe_resource);
433 if (!lpr) {
434 goto no_lpr;
435 }
436
437 lpr->base = *template;
438 pipe_reference_init(&lpr->base.reference, 1);
439 lpr->base.screen = screen;
440
441 /*
442 * Looks like unaligned displaytargets work just fine,
443 * at least sampler/render ones.
444 */
445 #if 0
446 assert(lpr->base.width0 == width);
447 assert(lpr->base.height0 == height);
448 #endif
449
450 lpr->dt = winsys->displaytarget_from_handle(winsys,
451 template,
452 whandle,
453 &lpr->row_stride[0]);
454 if (!lpr->dt) {
455 goto no_dt;
456 }
457
458 lpr->id = id_counter++;
459
460 #ifdef DEBUG
461 insert_at_tail(&resource_list, lpr);
462 #endif
463
464 return &lpr->base;
465
466 no_dt:
467 FREE(lpr);
468 no_lpr:
469 return NULL;
470 }
471
472
473 static boolean
474 llvmpipe_resource_get_handle(struct pipe_screen *screen,
475 struct pipe_resource *pt,
476 struct winsys_handle *whandle)
477 {
478 struct sw_winsys *winsys = llvmpipe_screen(screen)->winsys;
479 struct llvmpipe_resource *lpr = llvmpipe_resource(pt);
480
481 assert(lpr->dt);
482 if (!lpr->dt)
483 return FALSE;
484
485 return winsys->displaytarget_get_handle(winsys, lpr->dt, whandle);
486 }
487
488
489 static void *
490 llvmpipe_transfer_map( struct pipe_context *pipe,
491 struct pipe_resource *resource,
492 unsigned level,
493 unsigned usage,
494 const struct pipe_box *box,
495 struct pipe_transfer **transfer )
496 {
497 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
498 struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);
499 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
500 struct llvmpipe_transfer *lpt;
501 struct pipe_transfer *pt;
502 ubyte *map;
503 enum pipe_format format;
504 enum lp_texture_usage tex_usage;
505 const char *mode;
506
507 assert(resource);
508 assert(level <= resource->last_level);
509
510 /*
511 * Transfers, like other pipe operations, must happen in order, so flush the
512 * context if necessary.
513 */
514 if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
515 boolean read_only = !(usage & PIPE_TRANSFER_WRITE);
516 boolean do_not_block = !!(usage & PIPE_TRANSFER_DONTBLOCK);
517 if (!llvmpipe_flush_resource(pipe, resource,
518 level,
519 read_only,
520 TRUE, /* cpu_access */
521 do_not_block,
522 __FUNCTION__)) {
523 /*
524 * It would have blocked, but state tracker requested no to.
525 */
526 assert(do_not_block);
527 return NULL;
528 }
529 }
530
531 /* Check if we're mapping the current constant buffer */
532 if ((usage & PIPE_TRANSFER_WRITE) &&
533 (resource->bind & PIPE_BIND_CONSTANT_BUFFER)) {
534 unsigned i;
535 for (i = 0; i < Elements(llvmpipe->constants[PIPE_SHADER_FRAGMENT]); ++i) {
536 if (resource == llvmpipe->constants[PIPE_SHADER_FRAGMENT][i].buffer) {
537 /* constants may have changed */
538 llvmpipe->dirty |= LP_NEW_CONSTANTS;
539 break;
540 }
541 }
542 }
543
544 lpt = CALLOC_STRUCT(llvmpipe_transfer);
545 if (!lpt)
546 return NULL;
547 pt = &lpt->base;
548 pipe_resource_reference(&pt->resource, resource);
549 pt->box = *box;
550 pt->level = level;
551 pt->stride = lpr->row_stride[level];
552 pt->layer_stride = lpr->img_stride[level];
553 pt->usage = usage;
554 *transfer = pt;
555
556 assert(level < LP_MAX_TEXTURE_LEVELS);
557
558 /*
559 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
560 transfer->x, transfer->y, transfer->width, transfer->height,
561 transfer->texture->width0,
562 transfer->texture->height0,
563 transfer->usage);
564 */
565
566 if (usage == PIPE_TRANSFER_READ) {
567 tex_usage = LP_TEX_USAGE_READ;
568 mode = "read";
569 }
570 else {
571 tex_usage = LP_TEX_USAGE_READ_WRITE;
572 mode = "read/write";
573 }
574
575 if (0) {
576 printf("transfer map tex %u mode %s\n", lpr->id, mode);
577 }
578
579 format = lpr->base.format;
580
581 map = llvmpipe_resource_map(resource,
582 level,
583 box->z,
584 tex_usage);
585
586
587 /* May want to do different things here depending on read/write nature
588 * of the map:
589 */
590 if (usage & PIPE_TRANSFER_WRITE) {
591 /* Do something to notify sharing contexts of a texture change.
592 */
593 screen->timestamp++;
594 }
595
596 map +=
597 box->y / util_format_get_blockheight(format) * pt->stride +
598 box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
599
600 return map;
601 }
602
603
604 static void
605 llvmpipe_transfer_unmap(struct pipe_context *pipe,
606 struct pipe_transfer *transfer)
607 {
608 assert(transfer->resource);
609
610 llvmpipe_resource_unmap(transfer->resource,
611 transfer->level,
612 transfer->box.z);
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 (transfer->resource);
619 pipe_resource_reference(&transfer->resource, NULL);
620 FREE(transfer);
621 }
622
623 unsigned int
624 llvmpipe_is_resource_referenced( struct pipe_context *pipe,
625 struct pipe_resource *presource,
626 unsigned level)
627 {
628 struct llvmpipe_context *llvmpipe = llvmpipe_context( pipe );
629
630 /*
631 * XXX checking only resources with the right bind flags
632 * is unsafe since with opengl state tracker we can end up
633 * with resources bound to places they weren't supposed to be
634 * (buffers bound as sampler views is one possibility here).
635 */
636 if (!(presource->bind & (PIPE_BIND_DEPTH_STENCIL |
637 PIPE_BIND_RENDER_TARGET |
638 PIPE_BIND_SAMPLER_VIEW)))
639 return LP_UNREFERENCED;
640
641 return lp_setup_is_resource_referenced(llvmpipe->setup, presource);
642 }
643
644
645 /**
646 * Returns the largest possible alignment for a format in llvmpipe
647 */
648 unsigned
649 llvmpipe_get_format_alignment( enum pipe_format format )
650 {
651 const struct util_format_description *desc = util_format_description(format);
652 unsigned size = 0;
653 unsigned bytes;
654 unsigned i;
655
656 for (i = 0; i < desc->nr_channels; ++i) {
657 size += desc->channel[i].size;
658 }
659
660 bytes = size / 8;
661
662 if (!util_is_power_of_two(bytes)) {
663 bytes /= desc->nr_channels;
664 }
665
666 if (bytes % 2 || bytes < 1) {
667 return 1;
668 } else {
669 return bytes;
670 }
671 }
672
673
674 /**
675 * Create buffer which wraps user-space data.
676 */
677 struct pipe_resource *
678 llvmpipe_user_buffer_create(struct pipe_screen *screen,
679 void *ptr,
680 unsigned bytes,
681 unsigned bind_flags)
682 {
683 struct llvmpipe_resource *buffer;
684
685 buffer = CALLOC_STRUCT(llvmpipe_resource);
686 if(!buffer)
687 return NULL;
688
689 pipe_reference_init(&buffer->base.reference, 1);
690 buffer->base.screen = screen;
691 buffer->base.format = PIPE_FORMAT_R8_UNORM; /* ?? */
692 buffer->base.bind = bind_flags;
693 buffer->base.usage = PIPE_USAGE_IMMUTABLE;
694 buffer->base.flags = 0;
695 buffer->base.width0 = bytes;
696 buffer->base.height0 = 1;
697 buffer->base.depth0 = 1;
698 buffer->base.array_size = 1;
699 buffer->userBuffer = TRUE;
700 buffer->data = ptr;
701
702 return &buffer->base;
703 }
704
705
706 /**
707 * Compute size (in bytes) need to store a texture image / mipmap level,
708 * for just one cube face, one array layer or one 3D texture slice
709 */
710 static unsigned
711 tex_image_face_size(const struct llvmpipe_resource *lpr, unsigned level)
712 {
713 return lpr->img_stride[level];
714 }
715
716
717 /**
718 * Return pointer to a 2D texture image/face/slice.
719 * No tiled/linear conversion is done.
720 */
721 ubyte *
722 llvmpipe_get_texture_image_address(struct llvmpipe_resource *lpr,
723 unsigned face_slice, unsigned level)
724 {
725 unsigned offset;
726
727 assert(llvmpipe_resource_is_texture(&lpr->base));
728
729 offset = lpr->mip_offsets[level];
730
731 if (face_slice > 0)
732 offset += face_slice * tex_image_face_size(lpr, level);
733
734 return (ubyte *) lpr->tex_data + offset;
735 }
736
737
738 /**
739 * Return size of resource in bytes
740 */
741 unsigned
742 llvmpipe_resource_size(const struct pipe_resource *resource)
743 {
744 const struct llvmpipe_resource *lpr = llvmpipe_resource_const(resource);
745 unsigned size = 0;
746
747 if (llvmpipe_resource_is_texture(resource)) {
748 /* Note this will always return 0 for displaytarget resources */
749 size = lpr->total_alloc_size;
750 }
751 else {
752 size = resource->width0;
753 }
754 return size;
755 }
756
757
758 #ifdef DEBUG
759 void
760 llvmpipe_print_resources(void)
761 {
762 struct llvmpipe_resource *lpr;
763 unsigned n = 0, total = 0;
764
765 debug_printf("LLVMPIPE: current resources:\n");
766 foreach(lpr, &resource_list) {
767 unsigned size = llvmpipe_resource_size(&lpr->base);
768 debug_printf("resource %u at %p, size %ux%ux%u: %u bytes, refcount %u\n",
769 lpr->id, (void *) lpr,
770 lpr->base.width0, lpr->base.height0, lpr->base.depth0,
771 size, lpr->base.reference.count);
772 total += size;
773 n++;
774 }
775 debug_printf("LLVMPIPE: total size of %u resources: %u\n", n, total);
776 }
777 #endif
778
779
780 void
781 llvmpipe_init_screen_resource_funcs(struct pipe_screen *screen)
782 {
783 #ifdef DEBUG
784 /* init linked list for tracking resources */
785 {
786 static boolean first_call = TRUE;
787 if (first_call) {
788 memset(&resource_list, 0, sizeof(resource_list));
789 make_empty_list(&resource_list);
790 first_call = FALSE;
791 }
792 }
793 #endif
794
795 screen->resource_create = llvmpipe_resource_create;
796 screen->resource_destroy = llvmpipe_resource_destroy;
797 screen->resource_from_handle = llvmpipe_resource_from_handle;
798 screen->resource_get_handle = llvmpipe_resource_get_handle;
799 screen->can_create_resource = llvmpipe_can_create_resource;
800 }
801
802
803 void
804 llvmpipe_init_context_resource_funcs(struct pipe_context *pipe)
805 {
806 pipe->transfer_map = llvmpipe_transfer_map;
807 pipe->transfer_unmap = llvmpipe_transfer_unmap;
808
809 pipe->transfer_flush_region = u_default_transfer_flush_region;
810 pipe->transfer_inline_write = u_default_transfer_inline_write;
811 }