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