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] );
222 return lpr
->dt
!= NULL
;
226 static struct pipe_resource
*
227 llvmpipe_resource_create(struct pipe_screen
*_screen
,
228 const struct pipe_resource
*templat
)
230 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
231 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
235 lpr
->base
= *templat
;
236 pipe_reference_init(&lpr
->base
.reference
, 1);
237 lpr
->base
.screen
= &screen
->base
;
239 /* assert(lpr->base.bind); */
241 if (resource_is_texture(&lpr
->base
)) {
242 if (lpr
->base
.bind
& PIPE_BIND_DISPLAY_TARGET
) {
243 /* displayable surface */
244 if (!llvmpipe_displaytarget_layout(screen
, lpr
))
246 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
250 if (!llvmpipe_texture_layout(screen
, lpr
))
252 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
254 assert(lpr
->layout
[0]);
257 /* other data (vertex buffer, const buffer, etc) */
258 const enum pipe_format format
= templat
->format
;
259 const uint w
= templat
->width0
/ util_format_get_blockheight(format
);
260 /* XXX buffers should only have one dimension, those values should be 1 */
261 const uint h
= templat
->height0
/ util_format_get_blockwidth(format
);
262 const uint d
= templat
->depth0
;
263 const uint bpp
= util_format_get_blocksize(format
);
264 const uint bytes
= w
* h
* d
* bpp
;
265 lpr
->data
= align_malloc(bytes
, 16);
270 lpr
->id
= id_counter
++;
273 insert_at_tail(&resource_list
, lpr
);
285 llvmpipe_resource_destroy(struct pipe_screen
*pscreen
,
286 struct pipe_resource
*pt
)
288 struct llvmpipe_screen
*screen
= llvmpipe_screen(pscreen
);
289 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
293 struct sw_winsys
*winsys
= screen
->winsys
;
294 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
296 if (lpr
->tiled
[0].data
) {
297 align_free(lpr
->tiled
[0].data
);
298 lpr
->tiled
[0].data
= NULL
;
301 FREE(lpr
->layout
[0]);
303 else if (resource_is_texture(pt
)) {
304 /* regular texture */
307 /* free linear image data */
308 for (level
= 0; level
< Elements(lpr
->linear
); level
++) {
309 if (lpr
->linear
[level
].data
) {
310 align_free(lpr
->linear
[level
].data
);
311 lpr
->linear
[level
].data
= NULL
;
315 /* free tiled image data */
316 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
317 if (lpr
->tiled
[level
].data
) {
318 align_free(lpr
->tiled
[level
].data
);
319 lpr
->tiled
[level
].data
= NULL
;
323 /* free layout flag arrays */
324 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
325 FREE(lpr
->layout
[level
]);
326 lpr
->layout
[level
] = NULL
;
329 else if (!lpr
->userBuffer
) {
331 align_free(lpr
->data
);
336 remove_from_list(lpr
);
344 * Map a resource for read/write.
347 llvmpipe_resource_map(struct pipe_resource
*resource
,
350 enum lp_texture_usage tex_usage
,
351 enum lp_texture_layout layout
)
353 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
356 assert(level
< LP_MAX_TEXTURE_LEVELS
);
357 assert(layer
< (u_minify(resource
->depth0
, level
) + resource
->array_size
- 1));
359 assert(tex_usage
== LP_TEX_USAGE_READ
||
360 tex_usage
== LP_TEX_USAGE_READ_WRITE
||
361 tex_usage
== LP_TEX_USAGE_WRITE_ALL
);
363 assert(layout
== LP_TEX_LAYOUT_NONE
||
364 layout
== LP_TEX_LAYOUT_TILED
||
365 layout
== LP_TEX_LAYOUT_LINEAR
);
369 struct llvmpipe_screen
*screen
= llvmpipe_screen(resource
->screen
);
370 struct sw_winsys
*winsys
= screen
->winsys
;
374 if (tex_usage
== LP_TEX_USAGE_READ
) {
375 dt_usage
= PIPE_TRANSFER_READ
;
378 dt_usage
= PIPE_TRANSFER_READ_WRITE
;
384 /* FIXME: keep map count? */
385 map
= winsys
->displaytarget_map(winsys
, lpr
->dt
, dt_usage
);
387 /* install this linear image in texture data structure */
388 lpr
->linear
[level
].data
= map
;
390 /* make sure tiled data gets converted to linear data */
391 map2
= llvmpipe_get_texture_image(lpr
, 0, 0, tex_usage
, layout
);
392 if (layout
== LP_TEX_LAYOUT_LINEAR
)
397 else if (resource_is_texture(resource
)) {
399 map
= llvmpipe_get_texture_image(lpr
, layer
, level
,
413 llvmpipe_resource_unmap(struct pipe_resource
*resource
,
417 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
421 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(resource
->screen
);
422 struct sw_winsys
*winsys
= lp_screen
->winsys
;
427 /* make sure linear image is up to date */
428 (void) llvmpipe_get_texture_image(lpr
, layer
, level
,
430 LP_TEX_LAYOUT_LINEAR
);
432 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
438 llvmpipe_resource_data(struct pipe_resource
*resource
)
440 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
442 assert(!resource_is_texture(resource
));
448 static struct pipe_resource
*
449 llvmpipe_resource_from_handle(struct pipe_screen
*screen
,
450 const struct pipe_resource
*template,
451 struct winsys_handle
*whandle
)
453 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
454 struct llvmpipe_resource
*lpr
;
455 unsigned width
, height
, width_t
, height_t
;
457 /* XXX Seems like from_handled depth textures doesn't work that well */
459 lpr
= CALLOC_STRUCT(llvmpipe_resource
);
464 lpr
->base
= *template;
465 pipe_reference_init(&lpr
->base
.reference
, 1);
466 lpr
->base
.screen
= screen
;
468 width
= align(lpr
->base
.width0
, TILE_SIZE
);
469 height
= align(lpr
->base
.height0
, TILE_SIZE
);
470 width_t
= width
/ TILE_SIZE
;
471 height_t
= height
/ TILE_SIZE
;
474 * Looks like unaligned displaytargets work just fine,
475 * at least sampler/render ones.
478 assert(lpr
->base
.width0
== width
);
479 assert(lpr
->base
.height0
== height
);
482 lpr
->tiles_per_row
[0] = width_t
;
483 lpr
->tiles_per_image
[0] = width_t
* height_t
;
484 lpr
->num_slices_faces
[0] = 1;
485 lpr
->img_stride
[0] = 0;
487 lpr
->dt
= winsys
->displaytarget_from_handle(winsys
,
490 &lpr
->row_stride
[0]);
495 lpr
->layout
[0] = alloc_layout_array(1, lpr
->base
.width0
, lpr
->base
.height0
);
496 if (!lpr
->layout
[0]) {
500 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
502 lpr
->id
= id_counter
++;
505 insert_at_tail(&resource_list
, lpr
);
511 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
520 llvmpipe_resource_get_handle(struct pipe_screen
*screen
,
521 struct pipe_resource
*pt
,
522 struct winsys_handle
*whandle
)
524 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
525 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
531 return winsys
->displaytarget_get_handle(winsys
, lpr
->dt
, whandle
);
535 static struct pipe_surface
*
536 llvmpipe_create_surface(struct pipe_context
*pipe
,
537 struct pipe_resource
*pt
,
538 const struct pipe_surface
*surf_tmpl
)
540 struct pipe_surface
*ps
;
542 assert(surf_tmpl
->u
.tex
.level
<= pt
->last_level
);
544 ps
= CALLOC_STRUCT(pipe_surface
);
546 pipe_reference_init(&ps
->reference
, 1);
547 pipe_resource_reference(&ps
->texture
, pt
);
549 ps
->format
= surf_tmpl
->format
;
550 ps
->width
= u_minify(pt
->width0
, surf_tmpl
->u
.tex
.level
);
551 ps
->height
= u_minify(pt
->height0
, surf_tmpl
->u
.tex
.level
);
552 ps
->usage
= surf_tmpl
->usage
;
554 ps
->u
.tex
.level
= surf_tmpl
->u
.tex
.level
;
555 ps
->u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
556 ps
->u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
563 llvmpipe_surface_destroy(struct pipe_context
*pipe
,
564 struct pipe_surface
*surf
)
566 /* Effectively do the texture_update work here - if texture images
567 * needed post-processing to put them into hardware layout, this is
568 * where it would happen. For llvmpipe, nothing to do.
570 assert(surf
->texture
);
571 pipe_resource_reference(&surf
->texture
, NULL
);
576 static struct pipe_transfer
*
577 llvmpipe_get_transfer(struct pipe_context
*pipe
,
578 struct pipe_resource
*resource
,
581 const struct pipe_box
*box
)
583 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
584 struct llvmpipe_resource
*lprex
= llvmpipe_resource(resource
);
585 struct llvmpipe_transfer
*lpr
;
588 assert(level
<= resource
->last_level
);
590 if (usage
& PIPE_TRANSFER_MAP_PERMANENTLY
) {
595 * Transfers, like other pipe operations, must happen in order, so flush the
596 * context if necessary.
598 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
599 boolean read_only
= !(usage
& PIPE_TRANSFER_WRITE
);
600 boolean do_not_block
= !!(usage
& PIPE_TRANSFER_DONTBLOCK
);
601 if (!llvmpipe_flush_resource(pipe
, resource
,
603 box
->depth
> 1 ? -1 : box
->z
,
605 TRUE
, /* cpu_access */
609 * It would have blocked, but state tracker requested no to.
611 assert(do_not_block
);
616 if (resource
== llvmpipe
->constants
[PIPE_SHADER_FRAGMENT
][0])
617 llvmpipe
->dirty
|= LP_NEW_CONSTANTS
;
619 lpr
= CALLOC_STRUCT(llvmpipe_transfer
);
621 struct pipe_transfer
*pt
= &lpr
->base
;
622 pipe_resource_reference(&pt
->resource
, resource
);
625 pt
->stride
= lprex
->row_stride
[level
];
626 pt
->layer_stride
= lprex
->img_stride
[level
];
636 llvmpipe_transfer_destroy(struct pipe_context
*pipe
,
637 struct pipe_transfer
*transfer
)
639 /* Effectively do the texture_update work here - if texture images
640 * needed post-processing to put them into hardware layout, this is
641 * where it would happen. For llvmpipe, nothing to do.
643 assert (transfer
->resource
);
644 pipe_resource_reference(&transfer
->resource
, NULL
);
650 llvmpipe_transfer_map( struct pipe_context
*pipe
,
651 struct pipe_transfer
*transfer
)
653 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
655 struct llvmpipe_resource
*lpr
;
656 enum pipe_format format
;
657 enum lp_texture_usage tex_usage
;
660 assert(transfer
->level
< LP_MAX_TEXTURE_LEVELS
);
663 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
664 transfer->x, transfer->y, transfer->width, transfer->height,
665 transfer->texture->width0,
666 transfer->texture->height0,
670 if (transfer
->usage
== PIPE_TRANSFER_READ
) {
671 tex_usage
= LP_TEX_USAGE_READ
;
675 tex_usage
= LP_TEX_USAGE_READ_WRITE
;
680 struct llvmpipe_resource
*lpr
= llvmpipe_resource(transfer
->resource
);
681 printf("transfer map tex %u mode %s\n", lpr
->id
, mode
);
685 assert(transfer
->resource
);
686 lpr
= llvmpipe_resource(transfer
->resource
);
687 format
= lpr
->base
.format
;
689 map
= llvmpipe_resource_map(transfer
->resource
,
692 tex_usage
, LP_TEX_LAYOUT_LINEAR
);
695 /* May want to do different things here depending on read/write nature
698 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
699 /* Do something to notify sharing contexts of a texture change.
705 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
706 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
713 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
714 struct pipe_transfer
*transfer
)
716 assert(transfer
->resource
);
718 llvmpipe_resource_unmap(transfer
->resource
,
724 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
725 struct pipe_resource
*presource
,
726 unsigned level
, int layer
)
728 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
730 if (presource
->target
== PIPE_BUFFER
)
731 return LP_UNREFERENCED
;
733 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
739 * Create buffer which wraps user-space data.
741 static struct pipe_resource
*
742 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
747 struct llvmpipe_resource
*buffer
;
749 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
753 pipe_reference_init(&buffer
->base
.reference
, 1);
754 buffer
->base
.screen
= screen
;
755 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
756 buffer
->base
.bind
= bind_flags
;
757 buffer
->base
.usage
= PIPE_USAGE_IMMUTABLE
;
758 buffer
->base
.flags
= 0;
759 buffer
->base
.width0
= bytes
;
760 buffer
->base
.height0
= 1;
761 buffer
->base
.depth0
= 1;
762 buffer
->base
.array_size
= 1;
763 buffer
->userBuffer
= TRUE
;
766 return &buffer
->base
;
771 * Compute size (in bytes) need to store a texture image / mipmap level,
772 * for just one cube face or one 3D texture slice
775 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
776 enum lp_texture_layout layout
)
778 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
779 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
781 assert(layout
== LP_TEX_LAYOUT_TILED
||
782 layout
== LP_TEX_LAYOUT_LINEAR
);
784 if (layout
== LP_TEX_LAYOUT_TILED
) {
785 /* for tiled layout, force a 32bpp format */
786 const enum pipe_format format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
787 const unsigned block_size
= util_format_get_blocksize(format
);
788 const unsigned nblocksy
=
789 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
790 const unsigned nblocksx
=
791 util_format_get_nblocksx(format
, align(width
, TILE_SIZE
));
792 const unsigned buffer_size
= block_size
* nblocksy
* nblocksx
;
796 /* we already computed this */
797 return lpr
->img_stride
[level
];
803 * Compute size (in bytes) need to store a texture image / mipmap level,
804 * including all cube faces or 3D image slices
807 tex_image_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
808 enum lp_texture_layout layout
)
810 const unsigned buf_size
= tex_image_face_size(lpr
, level
, layout
);
811 return buf_size
* lpr
->num_slices_faces
[level
];
816 * This function encapsulates some complicated logic for determining
817 * how to convert a tile of image data from linear layout to tiled
818 * layout, or vice versa.
819 * \param cur_layout the current tile layout
820 * \param target_layout the desired tile layout
821 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
822 * \param new_layout_return returns the new layout mode
823 * \param convert_return returns TRUE if image conversion is needed
826 layout_logic(enum lp_texture_layout cur_layout
,
827 enum lp_texture_layout target_layout
,
828 enum lp_texture_usage usage
,
829 enum lp_texture_layout
*new_layout_return
,
832 enum lp_texture_layout other_layout
, new_layout
;
836 new_layout
= 99; /* debug check */
838 if (target_layout
== LP_TEX_LAYOUT_LINEAR
) {
839 other_layout
= LP_TEX_LAYOUT_TILED
;
842 assert(target_layout
== LP_TEX_LAYOUT_TILED
);
843 other_layout
= LP_TEX_LAYOUT_LINEAR
;
846 new_layout
= target_layout
; /* may get changed below */
848 if (cur_layout
== LP_TEX_LAYOUT_BOTH
) {
849 if (usage
== LP_TEX_USAGE_READ
) {
850 new_layout
= LP_TEX_LAYOUT_BOTH
;
853 else if (cur_layout
== other_layout
) {
854 if (usage
!= LP_TEX_USAGE_WRITE_ALL
) {
855 /* need to convert tiled data to linear or vice versa */
858 if (usage
== LP_TEX_USAGE_READ
)
859 new_layout
= LP_TEX_LAYOUT_BOTH
;
863 assert(cur_layout
== LP_TEX_LAYOUT_NONE
||
864 cur_layout
== target_layout
);
867 assert(new_layout
== LP_TEX_LAYOUT_BOTH
||
868 new_layout
== target_layout
);
870 *new_layout_return
= new_layout
;
875 * Return pointer to a 2D texture image/face/slice.
876 * No tiled/linear conversion is done.
879 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
880 unsigned face_slice
, unsigned level
,
881 enum lp_texture_layout layout
)
883 struct llvmpipe_texture_image
*img
;
886 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
887 img
= &lpr
->linear
[level
];
890 assert (layout
== LP_TEX_LAYOUT_TILED
);
891 img
= &lpr
->tiled
[level
];
895 offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
899 return (ubyte
*) img
->data
+ offset
;
903 static INLINE
enum lp_texture_layout
904 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource
*lpr
,
905 unsigned face_slice
, unsigned level
,
906 unsigned x
, unsigned y
)
909 assert(resource_is_texture(&lpr
->base
));
910 assert(x
< lpr
->tiles_per_row
[level
]);
911 i
= face_slice
* lpr
->tiles_per_image
[level
]
912 + y
* lpr
->tiles_per_row
[level
] + x
;
913 return lpr
->layout
[level
][i
];
918 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource
*lpr
,
919 unsigned face_slice
, unsigned level
,
920 unsigned x
, unsigned y
,
921 enum lp_texture_layout layout
)
924 assert(resource_is_texture(&lpr
->base
));
925 assert(x
< lpr
->tiles_per_row
[level
]);
926 i
= face_slice
* lpr
->tiles_per_image
[level
]
927 + y
* lpr
->tiles_per_row
[level
] + x
;
928 lpr
->layout
[level
][i
] = layout
;
933 * Set the layout mode for all tiles in a particular image.
936 llvmpipe_set_texture_image_layout(struct llvmpipe_resource
*lpr
,
937 unsigned face_slice
, unsigned level
,
938 unsigned width_t
, unsigned height_t
,
939 enum lp_texture_layout layout
)
941 const unsigned start
= face_slice
* lpr
->tiles_per_image
[level
];
944 for (i
= 0; i
< width_t
* height_t
; i
++) {
945 lpr
->layout
[level
][start
+ i
] = layout
;
951 * Allocate storage for a linear or tile texture image (all cube
952 * faces and all 3D slices.
955 alloc_image_data(struct llvmpipe_resource
*lpr
, unsigned level
,
956 enum lp_texture_layout layout
)
958 uint alignment
= MAX2(16, util_cpu_caps
.cacheline
);
963 if (layout
== LP_TEX_LAYOUT_TILED
) {
964 /* tiled data is stored in regular memory */
965 uint buffer_size
= tex_image_size(lpr
, level
, layout
);
966 lpr
->tiled
[level
].data
= align_malloc(buffer_size
, alignment
);
969 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
971 /* we get the linear memory from the winsys */
972 struct llvmpipe_screen
*screen
= llvmpipe_screen(lpr
->base
.screen
);
973 struct sw_winsys
*winsys
= screen
->winsys
;
975 lpr
->linear
[0].data
=
976 winsys
->displaytarget_map(winsys
, lpr
->dt
,
977 PIPE_TRANSFER_READ_WRITE
);
980 /* not a display target - allocate regular memory */
981 uint buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
982 lpr
->linear
[level
].data
= align_malloc(buffer_size
, alignment
);
990 * Return pointer to texture image data (either linear or tiled layout)
991 * for a particular cube face or 3D texture slice.
993 * \param face_slice the cube face or 3D slice of interest
994 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
995 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
998 llvmpipe_get_texture_image(struct llvmpipe_resource
*lpr
,
999 unsigned face_slice
, unsigned level
,
1000 enum lp_texture_usage usage
,
1001 enum lp_texture_layout layout
)
1004 * 'target' refers to the image which we're retrieving (either in
1005 * tiled or linear layout).
1006 * 'other' refers to the same image but in the other layout. (it may
1009 struct llvmpipe_texture_image
*target_img
;
1010 struct llvmpipe_texture_image
*other_img
;
1013 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
1014 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
1015 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
1016 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
1017 enum lp_texture_layout other_layout
;
1018 boolean only_allocate
;
1020 assert(layout
== LP_TEX_LAYOUT_NONE
||
1021 layout
== LP_TEX_LAYOUT_TILED
||
1022 layout
== LP_TEX_LAYOUT_LINEAR
);
1024 assert(usage
== LP_TEX_USAGE_READ
||
1025 usage
== LP_TEX_USAGE_READ_WRITE
||
1026 usage
== LP_TEX_USAGE_WRITE_ALL
);
1028 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
1029 if (layout
== LP_TEX_LAYOUT_NONE
) {
1030 only_allocate
= TRUE
;
1031 layout
= LP_TEX_LAYOUT_TILED
;
1034 only_allocate
= FALSE
;
1038 assert(lpr
->linear
[level
].data
);
1041 /* which is target? which is other? */
1042 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
1043 target_img
= &lpr
->linear
[level
];
1044 other_img
= &lpr
->tiled
[level
];
1045 other_layout
= LP_TEX_LAYOUT_TILED
;
1048 target_img
= &lpr
->tiled
[level
];
1049 other_img
= &lpr
->linear
[level
];
1050 other_layout
= LP_TEX_LAYOUT_LINEAR
;
1053 target_data
= target_img
->data
;
1054 other_data
= other_img
->data
;
1057 /* allocate memory for the target image now */
1058 alloc_image_data(lpr
, level
, layout
);
1059 target_data
= target_img
->data
;
1062 if (face_slice
> 0) {
1063 unsigned target_offset
, other_offset
;
1065 target_offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
1066 other_offset
= face_slice
* tex_image_face_size(lpr
, level
, other_layout
);
1068 target_data
= (uint8_t *) target_data
+ target_offset
;
1071 other_data
= (uint8_t *) other_data
+ other_offset
;
1075 if (only_allocate
) {
1076 /* Just allocating tiled memory. Don't initialize it from the
1077 * linear data if it exists.
1083 /* may need to convert other data to the requested layout */
1084 enum lp_texture_layout new_layout
;
1087 /* loop over all image tiles, doing layout conversion where needed */
1088 for (y
= 0; y
< height_t
; y
++) {
1089 for (x
= 0; x
< width_t
; x
++) {
1090 enum lp_texture_layout cur_layout
=
1091 llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, x
, y
);
1094 layout_logic(cur_layout
, layout
, usage
, &new_layout
, &convert
);
1096 if (convert
&& other_data
&& target_data
) {
1097 if (layout
== LP_TEX_LAYOUT_TILED
) {
1098 lp_linear_to_tiled(other_data
, target_data
,
1099 x
* TILE_SIZE
, y
* TILE_SIZE
,
1100 TILE_SIZE
, TILE_SIZE
,
1102 lpr
->row_stride
[level
],
1103 lpr
->tiles_per_row
[level
]);
1106 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
1107 lp_tiled_to_linear(other_data
, target_data
,
1108 x
* TILE_SIZE
, y
* TILE_SIZE
,
1109 TILE_SIZE
, TILE_SIZE
,
1111 lpr
->row_stride
[level
],
1112 lpr
->tiles_per_row
[level
]);
1116 if (new_layout
!= cur_layout
)
1117 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, x
, y
,
1124 llvmpipe_set_texture_image_layout(lpr
, face_slice
, level
,
1125 width_t
, height_t
, layout
);
1133 * Return pointer to start of a texture image (1D, 2D, 3D, CUBE).
1134 * All cube faces and 3D slices will be converted to the requested
1136 * This is typically used when we're about to sample from a texture.
1139 llvmpipe_get_texture_image_all(struct llvmpipe_resource
*lpr
,
1141 enum lp_texture_usage usage
,
1142 enum lp_texture_layout layout
)
1144 const int slices
= lpr
->num_slices_faces
[level
];
1150 for (slice
= slices
- 1; slice
>= 0; slice
--) {
1151 map
= llvmpipe_get_texture_image(lpr
, slice
, level
, usage
, layout
);
1159 * Get pointer to a linear image (not the tile!) where the tile at (x,y)
1160 * is known to be in linear layout.
1161 * Conversion from tiled to linear will be done if necessary.
1162 * \return pointer to start of image/face (not the tile)
1165 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource
*lpr
,
1166 unsigned face_slice
, unsigned level
,
1167 enum lp_texture_usage usage
,
1168 unsigned x
, unsigned y
)
1170 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1171 enum lp_texture_layout cur_layout
, new_layout
;
1172 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1174 uint8_t *tiled_image
, *linear_image
;
1176 assert(resource_is_texture(&lpr
->base
));
1177 assert(x
% TILE_SIZE
== 0);
1178 assert(y
% TILE_SIZE
== 0);
1180 if (!linear_img
->data
) {
1181 /* allocate memory for the linear image now */
1182 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1185 /* compute address of the slice/face of the image that contains the tile */
1186 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1187 LP_TEX_LAYOUT_TILED
);
1188 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1189 LP_TEX_LAYOUT_LINEAR
);
1191 /* get current tile layout and determine if data conversion is needed */
1192 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1194 layout_logic(cur_layout
, LP_TEX_LAYOUT_LINEAR
, usage
,
1195 &new_layout
, &convert
);
1197 if (convert
&& tiled_image
&& linear_image
) {
1198 lp_tiled_to_linear(tiled_image
, linear_image
,
1199 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1200 lpr
->row_stride
[level
],
1201 lpr
->tiles_per_row
[level
]);
1204 if (new_layout
!= cur_layout
)
1205 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1207 return linear_image
;
1212 * Get pointer to tiled data for rendering.
1213 * \return pointer to the tiled data at the given tile position
1216 llvmpipe_get_texture_tile(struct llvmpipe_resource
*lpr
,
1217 unsigned face_slice
, unsigned level
,
1218 enum lp_texture_usage usage
,
1219 unsigned x
, unsigned y
)
1221 struct llvmpipe_texture_image
*tiled_img
= &lpr
->tiled
[level
];
1222 enum lp_texture_layout cur_layout
, new_layout
;
1223 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1225 uint8_t *tiled_image
, *linear_image
;
1226 unsigned tile_offset
;
1228 assert(x
% TILE_SIZE
== 0);
1229 assert(y
% TILE_SIZE
== 0);
1231 if (!tiled_img
->data
) {
1232 /* allocate memory for the tiled image now */
1233 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_TILED
);
1236 /* compute address of the slice/face of the image that contains the tile */
1237 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1238 LP_TEX_LAYOUT_TILED
);
1239 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1240 LP_TEX_LAYOUT_LINEAR
);
1242 /* get current tile layout and see if we need to convert the data */
1243 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1245 layout_logic(cur_layout
, LP_TEX_LAYOUT_TILED
, usage
, &new_layout
, &convert
);
1246 if (convert
&& linear_image
&& tiled_image
) {
1247 lp_linear_to_tiled(linear_image
, tiled_image
,
1248 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1249 lpr
->row_stride
[level
],
1250 lpr
->tiles_per_row
[level
]);
1256 if (new_layout
!= cur_layout
)
1257 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1259 /* compute, return address of the 64x64 tile */
1260 tile_offset
= (ty
* lpr
->tiles_per_row
[level
] + tx
)
1261 * TILE_SIZE
* TILE_SIZE
* 4;
1263 return (ubyte
*) tiled_image
+ tile_offset
;
1268 * Get pointer to tiled data for rendering.
1269 * \return pointer to the tiled data at the given tile position
1272 llvmpipe_unswizzle_cbuf_tile(struct llvmpipe_resource
*lpr
,
1273 unsigned face_slice
, unsigned level
,
1274 unsigned x
, unsigned y
,
1277 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1278 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1279 uint8_t *linear_image
;
1281 assert(x
% TILE_SIZE
== 0);
1282 assert(y
% TILE_SIZE
== 0);
1284 if (!linear_img
->data
) {
1285 /* allocate memory for the linear image now */
1286 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1289 /* compute address of the slice/face of the image that contains the tile */
1290 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1291 LP_TEX_LAYOUT_LINEAR
);
1294 uint ii
= x
, jj
= y
;
1295 uint tile_offset
= jj
/ TILE_SIZE
+ ii
/ TILE_SIZE
;
1296 uint byte_offset
= tile_offset
* TILE_SIZE
* TILE_SIZE
* 4;
1298 /* Note that lp_tiled_to_linear expects the tile parameter to
1299 * point at the first tile in a whole-image sized array. In
1300 * this code, we have only a single tile and have to do some
1301 * pointer arithmetic to figure out where the "image" would have
1304 lp_tiled_to_linear(tile
- byte_offset
, linear_image
,
1305 x
, y
, TILE_SIZE
, TILE_SIZE
,
1307 lpr
->row_stride
[level
],
1308 1); /* tiles per row */
1311 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
,
1312 LP_TEX_LAYOUT_LINEAR
);
1317 * Get pointer to tiled data for rendering.
1318 * \return pointer to the tiled data at the given tile position
1321 llvmpipe_swizzle_cbuf_tile(struct llvmpipe_resource
*lpr
,
1322 unsigned face_slice
, unsigned level
,
1323 unsigned x
, unsigned y
,
1326 uint8_t *linear_image
;
1328 assert(x
% TILE_SIZE
== 0);
1329 assert(y
% TILE_SIZE
== 0);
1331 /* compute address of the slice/face of the image that contains the tile */
1332 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1333 LP_TEX_LAYOUT_LINEAR
);
1336 uint ii
= x
, jj
= y
;
1337 uint tile_offset
= jj
/ TILE_SIZE
+ ii
/ TILE_SIZE
;
1338 uint byte_offset
= tile_offset
* TILE_SIZE
* TILE_SIZE
* 4;
1340 /* Note that lp_linear_to_tiled expects the tile parameter to
1341 * point at the first tile in a whole-image sized array. In
1342 * this code, we have only a single tile and have to do some
1343 * pointer arithmetic to figure out where the "image" would have
1346 lp_linear_to_tiled(linear_image
, tile
- byte_offset
,
1347 x
, y
, TILE_SIZE
, TILE_SIZE
,
1349 lpr
->row_stride
[level
],
1350 1); /* tiles per row */
1356 * Return size of resource in bytes
1359 llvmpipe_resource_size(const struct pipe_resource
*resource
)
1361 const struct llvmpipe_resource
*lpr
= llvmpipe_resource_const(resource
);
1362 unsigned lvl
, size
= 0;
1364 for (lvl
= 0; lvl
<= lpr
->base
.last_level
; lvl
++) {
1365 if (lpr
->linear
[lvl
].data
)
1366 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_LINEAR
);
1368 if (lpr
->tiled
[lvl
].data
)
1369 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_TILED
);
1378 llvmpipe_print_resources(void)
1380 struct llvmpipe_resource
*lpr
;
1381 unsigned n
= 0, total
= 0;
1383 debug_printf("LLVMPIPE: current resources:\n");
1384 foreach(lpr
, &resource_list
) {
1385 unsigned size
= llvmpipe_resource_size(&lpr
->base
);
1386 debug_printf("resource %u at %p, size %ux%ux%u: %u bytes, refcount %u\n",
1387 lpr
->id
, (void *) lpr
,
1388 lpr
->base
.width0
, lpr
->base
.height0
, lpr
->base
.depth0
,
1389 size
, lpr
->base
.reference
.count
);
1393 debug_printf("LLVMPIPE: total size of %u resources: %u\n", n
, total
);
1399 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
1402 /* init linked list for tracking resources */
1404 static boolean first_call
= TRUE
;
1406 memset(&resource_list
, 0, sizeof(resource_list
));
1407 make_empty_list(&resource_list
);
1413 screen
->resource_create
= llvmpipe_resource_create
;
1414 screen
->resource_destroy
= llvmpipe_resource_destroy
;
1415 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
1416 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
1417 screen
->user_buffer_create
= llvmpipe_user_buffer_create
;
1423 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
1425 pipe
->get_transfer
= llvmpipe_get_transfer
;
1426 pipe
->transfer_destroy
= llvmpipe_transfer_destroy
;
1427 pipe
->transfer_map
= llvmpipe_transfer_map
;
1428 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
1430 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
1431 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;
1433 pipe
->create_surface
= llvmpipe_create_surface
;
1434 pipe
->surface_destroy
= llvmpipe_surface_destroy
;