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