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