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