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