1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Michel Dänzer <michel@tungstengraphics.com>
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
38 #include "util/u_inlines.h"
39 #include "util/u_cpu_detect.h"
40 #include "util/u_format.h"
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43 #include "util/u_simple_list.h"
44 #include "util/u_transfer.h"
46 #include "lp_context.h"
48 #include "lp_screen.h"
49 #include "lp_tile_image.h"
50 #include "lp_texture.h"
54 #include "state_tracker/sw_winsys.h"
58 static struct llvmpipe_resource resource_list
;
60 static unsigned id_counter
= 0;
64 resource_is_texture(const struct pipe_resource
*resource
)
66 switch (resource
->target
) {
71 case PIPE_TEXTURE_RECT
:
73 case PIPE_TEXTURE_CUBE
:
84 * Allocate storage for llvmpipe_texture::layout array.
85 * The number of elements is width_in_tiles * height_in_tiles.
87 static enum lp_texture_layout
*
88 alloc_layout_array(unsigned num_slices
, unsigned width
, unsigned height
)
90 const unsigned tx
= align(width
, TILE_SIZE
) / TILE_SIZE
;
91 const unsigned ty
= align(height
, TILE_SIZE
) / TILE_SIZE
;
93 assert(num_slices
* tx
* ty
> 0);
94 assert(LP_TEX_LAYOUT_NONE
== 0); /* calloc'ing LP_TEX_LAYOUT_NONE here */
96 return (enum lp_texture_layout
*)
97 CALLOC(num_slices
* tx
* ty
, sizeof(enum lp_texture_layout
));
103 * Conventional allocation path for non-display textures:
104 * Just compute row strides here. Storage is allocated on demand later.
107 llvmpipe_texture_layout(struct llvmpipe_screen
*screen
,
108 struct llvmpipe_resource
*lpr
)
110 struct pipe_resource
*pt
= &lpr
->base
;
112 unsigned width
= pt
->width0
;
113 unsigned height
= pt
->height0
;
114 unsigned depth
= pt
->depth0
;
116 assert(LP_MAX_TEXTURE_2D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
117 assert(LP_MAX_TEXTURE_3D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
119 for (level
= 0; level
<= pt
->last_level
; level
++) {
121 /* Row stride and image stride (for linear layout) */
123 unsigned alignment
, nblocksx
, nblocksy
, block_size
;
125 /* For non-compressed formats we need to align the texture size
126 * to the tile size to facilitate render-to-texture.
128 if (util_format_is_compressed(pt
->format
))
131 alignment
= TILE_SIZE
;
133 nblocksx
= util_format_get_nblocksx(pt
->format
,
134 align(width
, alignment
));
135 nblocksy
= util_format_get_nblocksy(pt
->format
,
136 align(height
, alignment
));
137 block_size
= util_format_get_blocksize(pt
->format
);
139 lpr
->row_stride
[level
] = align(nblocksx
* block_size
, 16);
141 lpr
->img_stride
[level
] = lpr
->row_stride
[level
] * nblocksy
;
144 /* Size of the image in tiles (for tiled layout) */
146 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
147 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
148 lpr
->tiles_per_row
[level
] = width_t
;
149 lpr
->tiles_per_image
[level
] = width_t
* height_t
;
152 /* Number of 3D image slices or cube faces */
156 if (lpr
->base
.target
== PIPE_TEXTURE_CUBE
)
158 else if (lpr
->base
.target
== PIPE_TEXTURE_3D
)
163 lpr
->num_slices_faces
[level
] = num_slices
;
165 lpr
->layout
[level
] = alloc_layout_array(num_slices
, width
, height
);
166 if (!lpr
->layout
[level
]) {
171 /* Compute size of next mipmap level */
172 width
= u_minify(width
, 1);
173 height
= u_minify(height
, 1);
174 depth
= u_minify(depth
, 1);
180 for (level
= 0; level
<= pt
->last_level
; level
++) {
181 if (lpr
->layout
[level
]) {
182 FREE(lpr
->layout
[level
]);
192 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
193 struct llvmpipe_resource
*lpr
)
195 struct sw_winsys
*winsys
= screen
->winsys
;
197 /* Round up the surface size to a multiple of the tile size to
198 * avoid tile clipping.
200 const unsigned width
= align(lpr
->base
.width0
, TILE_SIZE
);
201 const unsigned height
= align(lpr
->base
.height0
, TILE_SIZE
);
202 const unsigned width_t
= width
/ TILE_SIZE
;
203 const unsigned height_t
= height
/ TILE_SIZE
;
205 lpr
->tiles_per_row
[0] = width_t
;
206 lpr
->tiles_per_image
[0] = width_t
* height_t
;
207 lpr
->num_slices_faces
[0] = 1;
208 lpr
->img_stride
[0] = 0;
210 lpr
->layout
[0] = alloc_layout_array(1, width
, height
);
211 if (!lpr
->layout
[0]) {
215 lpr
->dt
= winsys
->displaytarget_create(winsys
,
220 &lpr
->row_stride
[0] );
226 void *map
= winsys
->displaytarget_map(winsys
, lpr
->dt
,
227 PIPE_TRANSFER_WRITE
);
230 memset(map
, 0, height
* lpr
->row_stride
[0]);
232 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
239 static struct pipe_resource
*
240 llvmpipe_resource_create(struct pipe_screen
*_screen
,
241 const struct pipe_resource
*templat
)
243 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
244 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
248 lpr
->base
= *templat
;
249 pipe_reference_init(&lpr
->base
.reference
, 1);
250 lpr
->base
.screen
= &screen
->base
;
252 /* assert(lpr->base.bind); */
254 if (resource_is_texture(&lpr
->base
)) {
255 if (lpr
->base
.bind
& PIPE_BIND_DISPLAY_TARGET
) {
256 /* displayable surface */
257 if (!llvmpipe_displaytarget_layout(screen
, lpr
))
259 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
263 if (!llvmpipe_texture_layout(screen
, lpr
))
265 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
267 assert(lpr
->layout
[0]);
270 /* other data (vertex buffer, const buffer, etc) */
271 const enum pipe_format format
= templat
->format
;
272 const uint w
= templat
->width0
/ util_format_get_blockheight(format
);
273 /* XXX buffers should only have one dimension, those values should be 1 */
274 const uint h
= templat
->height0
/ util_format_get_blockwidth(format
);
275 const uint d
= templat
->depth0
;
276 const uint bpp
= util_format_get_blocksize(format
);
277 const uint bytes
= w
* h
* d
* bpp
;
278 lpr
->data
= align_malloc(bytes
, 16);
281 memset(lpr
->data
, 0, bytes
);
284 lpr
->id
= id_counter
++;
287 insert_at_tail(&resource_list
, lpr
);
299 llvmpipe_resource_destroy(struct pipe_screen
*pscreen
,
300 struct pipe_resource
*pt
)
302 struct llvmpipe_screen
*screen
= llvmpipe_screen(pscreen
);
303 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
307 struct sw_winsys
*winsys
= screen
->winsys
;
308 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
310 if (lpr
->tiled
[0].data
) {
311 align_free(lpr
->tiled
[0].data
);
312 lpr
->tiled
[0].data
= NULL
;
315 FREE(lpr
->layout
[0]);
317 else if (resource_is_texture(pt
)) {
318 /* regular texture */
321 /* free linear image data */
322 for (level
= 0; level
< Elements(lpr
->linear
); level
++) {
323 if (lpr
->linear
[level
].data
) {
324 align_free(lpr
->linear
[level
].data
);
325 lpr
->linear
[level
].data
= NULL
;
329 /* free tiled image data */
330 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
331 if (lpr
->tiled
[level
].data
) {
332 align_free(lpr
->tiled
[level
].data
);
333 lpr
->tiled
[level
].data
= NULL
;
337 /* free layout flag arrays */
338 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
339 FREE(lpr
->layout
[level
]);
340 lpr
->layout
[level
] = NULL
;
343 else if (!lpr
->userBuffer
) {
345 align_free(lpr
->data
);
350 remove_from_list(lpr
);
358 * Map a resource for read/write.
361 llvmpipe_resource_map(struct pipe_resource
*resource
,
364 enum lp_texture_usage tex_usage
,
365 enum lp_texture_layout layout
)
367 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
370 assert(level
< LP_MAX_TEXTURE_LEVELS
);
371 assert(layer
< (u_minify(resource
->depth0
, level
) + resource
->array_size
- 1));
373 assert(tex_usage
== LP_TEX_USAGE_READ
||
374 tex_usage
== LP_TEX_USAGE_READ_WRITE
||
375 tex_usage
== LP_TEX_USAGE_WRITE_ALL
);
377 assert(layout
== LP_TEX_LAYOUT_NONE
||
378 layout
== LP_TEX_LAYOUT_TILED
||
379 layout
== LP_TEX_LAYOUT_LINEAR
);
383 struct llvmpipe_screen
*screen
= llvmpipe_screen(resource
->screen
);
384 struct sw_winsys
*winsys
= screen
->winsys
;
388 if (tex_usage
== LP_TEX_USAGE_READ
) {
389 dt_usage
= PIPE_TRANSFER_READ
;
392 dt_usage
= PIPE_TRANSFER_READ_WRITE
;
398 /* FIXME: keep map count? */
399 map
= winsys
->displaytarget_map(winsys
, lpr
->dt
, dt_usage
);
401 /* install this linear image in texture data structure */
402 lpr
->linear
[level
].data
= map
;
404 /* make sure tiled data gets converted to linear data */
405 map2
= llvmpipe_get_texture_image(lpr
, 0, 0, tex_usage
, layout
);
406 if (layout
== LP_TEX_LAYOUT_LINEAR
)
411 else if (resource_is_texture(resource
)) {
413 map
= llvmpipe_get_texture_image(lpr
, layer
, level
,
427 llvmpipe_resource_unmap(struct pipe_resource
*resource
,
431 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
435 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(resource
->screen
);
436 struct sw_winsys
*winsys
= lp_screen
->winsys
;
441 /* make sure linear image is up to date */
442 (void) llvmpipe_get_texture_image(lpr
, layer
, level
,
444 LP_TEX_LAYOUT_LINEAR
);
446 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
452 llvmpipe_resource_data(struct pipe_resource
*resource
)
454 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
456 assert(!resource_is_texture(resource
));
462 static struct pipe_resource
*
463 llvmpipe_resource_from_handle(struct pipe_screen
*screen
,
464 const struct pipe_resource
*template,
465 struct winsys_handle
*whandle
)
467 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
468 struct llvmpipe_resource
*lpr
;
469 unsigned width
, height
, width_t
, height_t
;
471 /* XXX Seems like from_handled depth textures doesn't work that well */
473 lpr
= CALLOC_STRUCT(llvmpipe_resource
);
478 lpr
->base
= *template;
479 pipe_reference_init(&lpr
->base
.reference
, 1);
480 lpr
->base
.screen
= screen
;
482 width
= align(lpr
->base
.width0
, TILE_SIZE
);
483 height
= align(lpr
->base
.height0
, TILE_SIZE
);
484 width_t
= width
/ TILE_SIZE
;
485 height_t
= height
/ TILE_SIZE
;
488 * Looks like unaligned displaytargets work just fine,
489 * at least sampler/render ones.
492 assert(lpr
->base
.width0
== width
);
493 assert(lpr
->base
.height0
== height
);
496 lpr
->tiles_per_row
[0] = width_t
;
497 lpr
->tiles_per_image
[0] = width_t
* height_t
;
498 lpr
->num_slices_faces
[0] = 1;
499 lpr
->img_stride
[0] = 0;
501 lpr
->dt
= winsys
->displaytarget_from_handle(winsys
,
504 &lpr
->row_stride
[0]);
509 lpr
->layout
[0] = alloc_layout_array(1, lpr
->base
.width0
, lpr
->base
.height0
);
510 if (!lpr
->layout
[0]) {
514 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
516 lpr
->id
= id_counter
++;
519 insert_at_tail(&resource_list
, lpr
);
525 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
534 llvmpipe_resource_get_handle(struct pipe_screen
*screen
,
535 struct pipe_resource
*pt
,
536 struct winsys_handle
*whandle
)
538 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
539 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
545 return winsys
->displaytarget_get_handle(winsys
, lpr
->dt
, whandle
);
549 static struct pipe_surface
*
550 llvmpipe_create_surface(struct pipe_context
*pipe
,
551 struct pipe_resource
*pt
,
552 const struct pipe_surface
*surf_tmpl
)
554 struct pipe_surface
*ps
;
556 assert(surf_tmpl
->u
.tex
.level
<= pt
->last_level
);
558 ps
= CALLOC_STRUCT(pipe_surface
);
560 pipe_reference_init(&ps
->reference
, 1);
561 pipe_resource_reference(&ps
->texture
, pt
);
563 ps
->format
= surf_tmpl
->format
;
564 ps
->width
= u_minify(pt
->width0
, surf_tmpl
->u
.tex
.level
);
565 ps
->height
= u_minify(pt
->height0
, surf_tmpl
->u
.tex
.level
);
566 ps
->usage
= surf_tmpl
->usage
;
568 ps
->u
.tex
.level
= surf_tmpl
->u
.tex
.level
;
569 ps
->u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
570 ps
->u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
577 llvmpipe_surface_destroy(struct pipe_context
*pipe
,
578 struct pipe_surface
*surf
)
580 /* Effectively do the texture_update work here - if texture images
581 * needed post-processing to put them into hardware layout, this is
582 * where it would happen. For llvmpipe, nothing to do.
584 assert(surf
->texture
);
585 pipe_resource_reference(&surf
->texture
, NULL
);
590 static struct pipe_transfer
*
591 llvmpipe_get_transfer(struct pipe_context
*pipe
,
592 struct pipe_resource
*resource
,
595 const struct pipe_box
*box
)
597 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
598 struct llvmpipe_resource
*lprex
= llvmpipe_resource(resource
);
599 struct llvmpipe_transfer
*lpr
;
602 assert(level
<= resource
->last_level
);
605 * Transfers, like other pipe operations, must happen in order, so flush the
606 * context if necessary.
608 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
609 boolean read_only
= !(usage
& PIPE_TRANSFER_WRITE
);
610 boolean do_not_block
= !!(usage
& PIPE_TRANSFER_DONTBLOCK
);
611 if (!llvmpipe_flush_resource(pipe
, resource
,
613 box
->depth
> 1 ? -1 : box
->z
,
615 TRUE
, /* cpu_access */
619 * It would have blocked, but state tracker requested no to.
621 assert(do_not_block
);
626 if (resource
== llvmpipe
->constants
[PIPE_SHADER_FRAGMENT
][0])
627 llvmpipe
->dirty
|= LP_NEW_CONSTANTS
;
629 lpr
= CALLOC_STRUCT(llvmpipe_transfer
);
631 struct pipe_transfer
*pt
= &lpr
->base
;
632 pipe_resource_reference(&pt
->resource
, resource
);
635 pt
->stride
= lprex
->row_stride
[level
];
636 pt
->layer_stride
= lprex
->img_stride
[level
];
646 llvmpipe_transfer_destroy(struct pipe_context
*pipe
,
647 struct pipe_transfer
*transfer
)
649 /* Effectively do the texture_update work here - if texture images
650 * needed post-processing to put them into hardware layout, this is
651 * where it would happen. For llvmpipe, nothing to do.
653 assert (transfer
->resource
);
654 pipe_resource_reference(&transfer
->resource
, NULL
);
660 llvmpipe_transfer_map( struct pipe_context
*pipe
,
661 struct pipe_transfer
*transfer
)
663 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
665 struct llvmpipe_resource
*lpr
;
666 enum pipe_format format
;
667 enum lp_texture_usage tex_usage
;
670 assert(transfer
->level
< LP_MAX_TEXTURE_LEVELS
);
673 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
674 transfer->x, transfer->y, transfer->width, transfer->height,
675 transfer->texture->width0,
676 transfer->texture->height0,
680 if (transfer
->usage
== PIPE_TRANSFER_READ
) {
681 tex_usage
= LP_TEX_USAGE_READ
;
685 tex_usage
= LP_TEX_USAGE_READ_WRITE
;
690 struct llvmpipe_resource
*lpr
= llvmpipe_resource(transfer
->resource
);
691 printf("transfer map tex %u mode %s\n", lpr
->id
, mode
);
695 assert(transfer
->resource
);
696 lpr
= llvmpipe_resource(transfer
->resource
);
697 format
= lpr
->base
.format
;
699 map
= llvmpipe_resource_map(transfer
->resource
,
702 tex_usage
, LP_TEX_LAYOUT_LINEAR
);
705 /* May want to do different things here depending on read/write nature
708 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
709 /* Do something to notify sharing contexts of a texture change.
715 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
716 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
723 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
724 struct pipe_transfer
*transfer
)
726 assert(transfer
->resource
);
728 llvmpipe_resource_unmap(transfer
->resource
,
734 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
735 struct pipe_resource
*presource
,
736 unsigned level
, int layer
)
738 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
740 if (presource
->target
== PIPE_BUFFER
)
741 return LP_UNREFERENCED
;
743 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
749 * Create buffer which wraps user-space data.
751 static struct pipe_resource
*
752 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
757 struct llvmpipe_resource
*buffer
;
759 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
763 pipe_reference_init(&buffer
->base
.reference
, 1);
764 buffer
->base
.screen
= screen
;
765 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
766 buffer
->base
.bind
= bind_flags
;
767 buffer
->base
.usage
= PIPE_USAGE_IMMUTABLE
;
768 buffer
->base
.flags
= 0;
769 buffer
->base
.width0
= bytes
;
770 buffer
->base
.height0
= 1;
771 buffer
->base
.depth0
= 1;
772 buffer
->base
.array_size
= 1;
773 buffer
->base
.user_ptr
= ptr
;
774 buffer
->userBuffer
= TRUE
;
777 return &buffer
->base
;
782 * Compute size (in bytes) need to store a texture image / mipmap level,
783 * for just one cube face or one 3D texture slice
786 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
787 enum lp_texture_layout layout
)
789 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
790 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
792 assert(layout
== LP_TEX_LAYOUT_TILED
||
793 layout
== LP_TEX_LAYOUT_LINEAR
);
795 if (layout
== LP_TEX_LAYOUT_TILED
) {
796 /* for tiled layout, force a 32bpp format */
797 const enum pipe_format format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
798 const unsigned block_size
= util_format_get_blocksize(format
);
799 const unsigned nblocksy
=
800 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
801 const unsigned nblocksx
=
802 util_format_get_nblocksx(format
, align(width
, TILE_SIZE
));
803 const unsigned buffer_size
= block_size
* nblocksy
* nblocksx
;
807 /* we already computed this */
808 return lpr
->img_stride
[level
];
814 * Compute size (in bytes) need to store a texture image / mipmap level,
815 * including all cube faces or 3D image slices
818 tex_image_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
819 enum lp_texture_layout layout
)
821 const unsigned buf_size
= tex_image_face_size(lpr
, level
, layout
);
822 return buf_size
* lpr
->num_slices_faces
[level
];
827 * This function encapsulates some complicated logic for determining
828 * how to convert a tile of image data from linear layout to tiled
829 * layout, or vice versa.
830 * \param cur_layout the current tile layout
831 * \param target_layout the desired tile layout
832 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
833 * \param new_layout_return returns the new layout mode
834 * \param convert_return returns TRUE if image conversion is needed
837 layout_logic(enum lp_texture_layout cur_layout
,
838 enum lp_texture_layout target_layout
,
839 enum lp_texture_usage usage
,
840 enum lp_texture_layout
*new_layout_return
,
843 enum lp_texture_layout other_layout
, new_layout
;
847 new_layout
= 99; /* debug check */
849 if (target_layout
== LP_TEX_LAYOUT_LINEAR
) {
850 other_layout
= LP_TEX_LAYOUT_TILED
;
853 assert(target_layout
== LP_TEX_LAYOUT_TILED
);
854 other_layout
= LP_TEX_LAYOUT_LINEAR
;
857 new_layout
= target_layout
; /* may get changed below */
859 if (cur_layout
== LP_TEX_LAYOUT_BOTH
) {
860 if (usage
== LP_TEX_USAGE_READ
) {
861 new_layout
= LP_TEX_LAYOUT_BOTH
;
864 else if (cur_layout
== other_layout
) {
865 if (usage
!= LP_TEX_USAGE_WRITE_ALL
) {
866 /* need to convert tiled data to linear or vice versa */
869 if (usage
== LP_TEX_USAGE_READ
)
870 new_layout
= LP_TEX_LAYOUT_BOTH
;
874 assert(cur_layout
== LP_TEX_LAYOUT_NONE
||
875 cur_layout
== target_layout
);
878 assert(new_layout
== LP_TEX_LAYOUT_BOTH
||
879 new_layout
== target_layout
);
881 *new_layout_return
= new_layout
;
886 * Return pointer to a 2D texture image/face/slice.
887 * No tiled/linear conversion is done.
890 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
891 unsigned face_slice
, unsigned level
,
892 enum lp_texture_layout layout
)
894 struct llvmpipe_texture_image
*img
;
897 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
898 img
= &lpr
->linear
[level
];
901 assert (layout
== LP_TEX_LAYOUT_TILED
);
902 img
= &lpr
->tiled
[level
];
906 offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
910 return (ubyte
*) img
->data
+ offset
;
914 static INLINE
enum lp_texture_layout
915 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource
*lpr
,
916 unsigned face_slice
, unsigned level
,
917 unsigned x
, unsigned y
)
920 assert(resource_is_texture(&lpr
->base
));
921 assert(x
< lpr
->tiles_per_row
[level
]);
922 i
= face_slice
* lpr
->tiles_per_image
[level
]
923 + y
* lpr
->tiles_per_row
[level
] + x
;
924 return lpr
->layout
[level
][i
];
929 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource
*lpr
,
930 unsigned face_slice
, unsigned level
,
931 unsigned x
, unsigned y
,
932 enum lp_texture_layout layout
)
935 assert(resource_is_texture(&lpr
->base
));
936 assert(x
< lpr
->tiles_per_row
[level
]);
937 i
= face_slice
* lpr
->tiles_per_image
[level
]
938 + y
* lpr
->tiles_per_row
[level
] + x
;
939 lpr
->layout
[level
][i
] = layout
;
944 * Set the layout mode for all tiles in a particular image.
947 llvmpipe_set_texture_image_layout(struct llvmpipe_resource
*lpr
,
948 unsigned face_slice
, unsigned level
,
949 unsigned width_t
, unsigned height_t
,
950 enum lp_texture_layout layout
)
952 const unsigned start
= face_slice
* lpr
->tiles_per_image
[level
];
955 for (i
= 0; i
< width_t
* height_t
; i
++) {
956 lpr
->layout
[level
][start
+ i
] = layout
;
962 * Allocate storage for a linear or tile texture image (all cube
963 * faces and all 3D slices.
966 alloc_image_data(struct llvmpipe_resource
*lpr
, unsigned level
,
967 enum lp_texture_layout layout
)
969 uint alignment
= MAX2(16, util_cpu_caps
.cacheline
);
974 if (layout
== LP_TEX_LAYOUT_TILED
) {
975 /* tiled data is stored in regular memory */
976 uint buffer_size
= tex_image_size(lpr
, level
, layout
);
977 lpr
->tiled
[level
].data
= align_malloc(buffer_size
, alignment
);
978 if (lpr
->tiled
[level
].data
) {
979 memset(lpr
->tiled
[level
].data
, 0, buffer_size
);
983 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
985 /* we get the linear memory from the winsys, and it has
986 * already been zeroed
988 struct llvmpipe_screen
*screen
= llvmpipe_screen(lpr
->base
.screen
);
989 struct sw_winsys
*winsys
= screen
->winsys
;
991 lpr
->linear
[0].data
=
992 winsys
->displaytarget_map(winsys
, lpr
->dt
,
993 PIPE_TRANSFER_READ_WRITE
);
996 /* not a display target - allocate regular memory */
997 uint buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
998 lpr
->linear
[level
].data
= align_malloc(buffer_size
, alignment
);
999 if (lpr
->linear
[level
].data
) {
1000 memset(lpr
->linear
[level
].data
, 0, buffer_size
);
1009 * Return pointer to texture image data (either linear or tiled layout)
1010 * for a particular cube face or 3D texture slice.
1012 * \param face_slice the cube face or 3D slice of interest
1013 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
1014 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
1017 llvmpipe_get_texture_image(struct llvmpipe_resource
*lpr
,
1018 unsigned face_slice
, unsigned level
,
1019 enum lp_texture_usage usage
,
1020 enum lp_texture_layout layout
)
1023 * 'target' refers to the image which we're retrieving (either in
1024 * tiled or linear layout).
1025 * 'other' refers to the same image but in the other layout. (it may
1028 struct llvmpipe_texture_image
*target_img
;
1029 struct llvmpipe_texture_image
*other_img
;
1032 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
1033 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
1034 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
1035 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
1036 enum lp_texture_layout other_layout
;
1037 boolean only_allocate
;
1039 assert(layout
== LP_TEX_LAYOUT_NONE
||
1040 layout
== LP_TEX_LAYOUT_TILED
||
1041 layout
== LP_TEX_LAYOUT_LINEAR
);
1043 assert(usage
== LP_TEX_USAGE_READ
||
1044 usage
== LP_TEX_USAGE_READ_WRITE
||
1045 usage
== LP_TEX_USAGE_WRITE_ALL
);
1047 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
1048 if (layout
== LP_TEX_LAYOUT_NONE
) {
1049 only_allocate
= TRUE
;
1050 layout
= LP_TEX_LAYOUT_TILED
;
1053 only_allocate
= FALSE
;
1057 assert(lpr
->linear
[level
].data
);
1060 /* which is target? which is other? */
1061 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
1062 target_img
= &lpr
->linear
[level
];
1063 other_img
= &lpr
->tiled
[level
];
1064 other_layout
= LP_TEX_LAYOUT_TILED
;
1067 target_img
= &lpr
->tiled
[level
];
1068 other_img
= &lpr
->linear
[level
];
1069 other_layout
= LP_TEX_LAYOUT_LINEAR
;
1072 target_data
= target_img
->data
;
1073 other_data
= other_img
->data
;
1076 /* allocate memory for the target image now */
1077 alloc_image_data(lpr
, level
, layout
);
1078 target_data
= target_img
->data
;
1081 if (face_slice
> 0) {
1082 unsigned target_offset
, other_offset
;
1084 target_offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
1085 other_offset
= face_slice
* tex_image_face_size(lpr
, level
, other_layout
);
1087 target_data
= (uint8_t *) target_data
+ target_offset
;
1090 other_data
= (uint8_t *) other_data
+ other_offset
;
1094 if (only_allocate
) {
1095 /* Just allocating tiled memory. Don't initialize it from the
1096 * linear data if it exists.
1102 /* may need to convert other data to the requested layout */
1103 enum lp_texture_layout new_layout
;
1106 /* loop over all image tiles, doing layout conversion where needed */
1107 for (y
= 0; y
< height_t
; y
++) {
1108 for (x
= 0; x
< width_t
; x
++) {
1109 enum lp_texture_layout cur_layout
=
1110 llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, x
, y
);
1113 layout_logic(cur_layout
, layout
, usage
, &new_layout
, &convert
);
1115 if (convert
&& other_data
&& target_data
) {
1116 if (layout
== LP_TEX_LAYOUT_TILED
) {
1117 lp_linear_to_tiled(other_data
, target_data
,
1118 x
* TILE_SIZE
, y
* TILE_SIZE
,
1119 TILE_SIZE
, TILE_SIZE
,
1121 lpr
->row_stride
[level
],
1122 lpr
->tiles_per_row
[level
]);
1125 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
1126 lp_tiled_to_linear(other_data
, target_data
,
1127 x
* TILE_SIZE
, y
* TILE_SIZE
,
1128 TILE_SIZE
, TILE_SIZE
,
1130 lpr
->row_stride
[level
],
1131 lpr
->tiles_per_row
[level
]);
1135 if (new_layout
!= cur_layout
)
1136 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, x
, y
,
1143 llvmpipe_set_texture_image_layout(lpr
, face_slice
, level
,
1144 width_t
, height_t
, layout
);
1152 * Return pointer to start of a texture image (1D, 2D, 3D, CUBE).
1153 * All cube faces and 3D slices will be converted to the requested
1155 * This is typically used when we're about to sample from a texture.
1158 llvmpipe_get_texture_image_all(struct llvmpipe_resource
*lpr
,
1160 enum lp_texture_usage usage
,
1161 enum lp_texture_layout layout
)
1163 const int slices
= lpr
->num_slices_faces
[level
];
1169 for (slice
= slices
- 1; slice
>= 0; slice
--) {
1170 map
= llvmpipe_get_texture_image(lpr
, slice
, level
, usage
, layout
);
1178 * Get pointer to a linear image (not the tile!) where the tile at (x,y)
1179 * is known to be in linear layout.
1180 * Conversion from tiled to linear will be done if necessary.
1181 * \return pointer to start of image/face (not the tile)
1184 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource
*lpr
,
1185 unsigned face_slice
, unsigned level
,
1186 enum lp_texture_usage usage
,
1187 unsigned x
, unsigned y
)
1189 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1190 enum lp_texture_layout cur_layout
, new_layout
;
1191 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1193 uint8_t *tiled_image
, *linear_image
;
1195 assert(resource_is_texture(&lpr
->base
));
1196 assert(x
% TILE_SIZE
== 0);
1197 assert(y
% TILE_SIZE
== 0);
1199 if (!linear_img
->data
) {
1200 /* allocate memory for the linear image now */
1201 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1204 /* compute address of the slice/face of the image that contains the tile */
1205 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1206 LP_TEX_LAYOUT_TILED
);
1207 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1208 LP_TEX_LAYOUT_LINEAR
);
1210 /* get current tile layout and determine if data conversion is needed */
1211 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1213 layout_logic(cur_layout
, LP_TEX_LAYOUT_LINEAR
, usage
,
1214 &new_layout
, &convert
);
1216 if (convert
&& tiled_image
&& linear_image
) {
1217 lp_tiled_to_linear(tiled_image
, linear_image
,
1218 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1219 lpr
->row_stride
[level
],
1220 lpr
->tiles_per_row
[level
]);
1223 if (new_layout
!= cur_layout
)
1224 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1226 return linear_image
;
1231 * Get pointer to tiled data for rendering.
1232 * \return pointer to the tiled data at the given tile position
1235 llvmpipe_get_texture_tile(struct llvmpipe_resource
*lpr
,
1236 unsigned face_slice
, unsigned level
,
1237 enum lp_texture_usage usage
,
1238 unsigned x
, unsigned y
)
1240 struct llvmpipe_texture_image
*tiled_img
= &lpr
->tiled
[level
];
1241 enum lp_texture_layout cur_layout
, new_layout
;
1242 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1244 uint8_t *tiled_image
, *linear_image
;
1245 unsigned tile_offset
;
1247 assert(x
% TILE_SIZE
== 0);
1248 assert(y
% TILE_SIZE
== 0);
1250 if (!tiled_img
->data
) {
1251 /* allocate memory for the tiled image now */
1252 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_TILED
);
1255 /* compute address of the slice/face of the image that contains the tile */
1256 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1257 LP_TEX_LAYOUT_TILED
);
1258 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1259 LP_TEX_LAYOUT_LINEAR
);
1261 /* get current tile layout and see if we need to convert the data */
1262 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1264 layout_logic(cur_layout
, LP_TEX_LAYOUT_TILED
, usage
, &new_layout
, &convert
);
1265 if (convert
&& linear_image
&& tiled_image
) {
1266 lp_linear_to_tiled(linear_image
, tiled_image
,
1267 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1268 lpr
->row_stride
[level
],
1269 lpr
->tiles_per_row
[level
]);
1275 if (new_layout
!= cur_layout
)
1276 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1278 /* compute, return address of the 64x64 tile */
1279 tile_offset
= (ty
* lpr
->tiles_per_row
[level
] + tx
)
1280 * TILE_SIZE
* TILE_SIZE
* 4;
1282 return (ubyte
*) tiled_image
+ tile_offset
;
1287 * Get pointer to tiled data for rendering.
1288 * \return pointer to the tiled data at the given tile position
1291 llvmpipe_unswizzle_cbuf_tile(struct llvmpipe_resource
*lpr
,
1292 unsigned face_slice
, unsigned level
,
1293 unsigned x
, unsigned y
,
1296 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1297 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1298 uint8_t *linear_image
;
1300 assert(x
% TILE_SIZE
== 0);
1301 assert(y
% TILE_SIZE
== 0);
1303 if (!linear_img
->data
) {
1304 /* allocate memory for the linear image now */
1305 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1308 /* compute address of the slice/face of the image that contains the tile */
1309 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1310 LP_TEX_LAYOUT_LINEAR
);
1313 uint ii
= x
, jj
= y
;
1314 uint tile_offset
= jj
/ TILE_SIZE
+ ii
/ TILE_SIZE
;
1315 uint byte_offset
= tile_offset
* TILE_SIZE
* TILE_SIZE
* 4;
1317 /* Note that lp_tiled_to_linear expects the tile parameter to
1318 * point at the first tile in a whole-image sized array. In
1319 * this code, we have only a single tile and have to do some
1320 * pointer arithmetic to figure out where the "image" would have
1323 lp_tiled_to_linear(tile
- byte_offset
, linear_image
,
1324 x
, y
, TILE_SIZE
, TILE_SIZE
,
1326 lpr
->row_stride
[level
],
1327 1); /* tiles per row */
1330 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
,
1331 LP_TEX_LAYOUT_LINEAR
);
1336 * Get pointer to tiled data for rendering.
1337 * \return pointer to the tiled data at the given tile position
1340 llvmpipe_swizzle_cbuf_tile(struct llvmpipe_resource
*lpr
,
1341 unsigned face_slice
, unsigned level
,
1342 unsigned x
, unsigned y
,
1345 uint8_t *linear_image
;
1347 assert(x
% TILE_SIZE
== 0);
1348 assert(y
% TILE_SIZE
== 0);
1350 /* compute address of the slice/face of the image that contains the tile */
1351 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1352 LP_TEX_LAYOUT_LINEAR
);
1355 uint ii
= x
, jj
= y
;
1356 uint tile_offset
= jj
/ TILE_SIZE
+ ii
/ TILE_SIZE
;
1357 uint byte_offset
= tile_offset
* TILE_SIZE
* TILE_SIZE
* 4;
1359 /* Note that lp_linear_to_tiled expects the tile parameter to
1360 * point at the first tile in a whole-image sized array. In
1361 * this code, we have only a single tile and have to do some
1362 * pointer arithmetic to figure out where the "image" would have
1365 lp_linear_to_tiled(linear_image
, tile
- byte_offset
,
1366 x
, y
, TILE_SIZE
, TILE_SIZE
,
1368 lpr
->row_stride
[level
],
1369 1); /* tiles per row */
1375 * Return size of resource in bytes
1378 llvmpipe_resource_size(const struct pipe_resource
*resource
)
1380 const struct llvmpipe_resource
*lpr
= llvmpipe_resource_const(resource
);
1381 unsigned lvl
, size
= 0;
1383 for (lvl
= 0; lvl
<= lpr
->base
.last_level
; lvl
++) {
1384 if (lpr
->linear
[lvl
].data
)
1385 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_LINEAR
);
1387 if (lpr
->tiled
[lvl
].data
)
1388 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_TILED
);
1397 llvmpipe_print_resources(void)
1399 struct llvmpipe_resource
*lpr
;
1400 unsigned n
= 0, total
= 0;
1402 debug_printf("LLVMPIPE: current resources:\n");
1403 foreach(lpr
, &resource_list
) {
1404 unsigned size
= llvmpipe_resource_size(&lpr
->base
);
1405 debug_printf("resource %u at %p, size %ux%ux%u: %u bytes, refcount %u\n",
1406 lpr
->id
, (void *) lpr
,
1407 lpr
->base
.width0
, lpr
->base
.height0
, lpr
->base
.depth0
,
1408 size
, lpr
->base
.reference
.count
);
1412 debug_printf("LLVMPIPE: total size of %u resources: %u\n", n
, total
);
1418 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
1421 /* init linked list for tracking resources */
1423 static boolean first_call
= TRUE
;
1425 memset(&resource_list
, 0, sizeof(resource_list
));
1426 make_empty_list(&resource_list
);
1432 screen
->resource_create
= llvmpipe_resource_create
;
1433 screen
->resource_destroy
= llvmpipe_resource_destroy
;
1434 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
1435 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
1436 screen
->user_buffer_create
= llvmpipe_user_buffer_create
;
1442 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
1444 pipe
->get_transfer
= llvmpipe_get_transfer
;
1445 pipe
->transfer_destroy
= llvmpipe_transfer_destroy
;
1446 pipe
->transfer_map
= llvmpipe_transfer_map
;
1447 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
1449 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
1450 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;
1452 pipe
->create_surface
= llvmpipe_create_surface
;
1453 pipe
->surface_destroy
= llvmpipe_surface_destroy
;