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
);
168 /* Compute size of next mipmap level */
169 width
= u_minify(width
, 1);
170 height
= u_minify(height
, 1);
171 depth
= u_minify(depth
, 1);
180 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
181 struct llvmpipe_resource
*lpr
)
183 struct sw_winsys
*winsys
= screen
->winsys
;
185 /* Round up the surface size to a multiple of the tile size to
186 * avoid tile clipping.
188 const unsigned width
= align(lpr
->base
.width0
, TILE_SIZE
);
189 const unsigned height
= align(lpr
->base
.height0
, TILE_SIZE
);
190 const unsigned width_t
= width
/ TILE_SIZE
;
191 const unsigned height_t
= height
/ TILE_SIZE
;
193 lpr
->tiles_per_row
[0] = width_t
;
194 lpr
->tiles_per_image
[0] = width_t
* height_t
;
195 lpr
->num_slices_faces
[0] = 1;
196 lpr
->img_stride
[0] = 0;
198 lpr
->layout
[0] = alloc_layout_array(1, width
, height
);
199 //lpr->layout[0][0] = LP_TEX_LAYOUT_LINEAR;
201 lpr
->dt
= winsys
->displaytarget_create(winsys
,
206 &lpr
->row_stride
[0] );
208 return lpr
->dt
!= NULL
;
212 static struct pipe_resource
*
213 llvmpipe_resource_create(struct pipe_screen
*_screen
,
214 const struct pipe_resource
*templat
)
216 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
217 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
221 lpr
->base
= *templat
;
222 pipe_reference_init(&lpr
->base
.reference
, 1);
223 lpr
->base
.screen
= &screen
->base
;
225 /* assert(lpr->base.bind); */
227 if (resource_is_texture(&lpr
->base
)) {
228 if (lpr
->base
.bind
& PIPE_BIND_DISPLAY_TARGET
) {
229 /* displayable surface */
230 if (!llvmpipe_displaytarget_layout(screen
, lpr
))
232 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
236 if (!llvmpipe_texture_layout(screen
, lpr
))
238 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
240 assert(lpr
->layout
[0]);
243 /* other data (vertex buffer, const buffer, etc) */
244 const enum pipe_format format
= templat
->format
;
245 const uint w
= templat
->width0
/ util_format_get_blockheight(format
);
246 /* XXX buffers should only have one dimension, those values should be 1 */
247 const uint h
= templat
->height0
/ util_format_get_blockwidth(format
);
248 const uint d
= templat
->depth0
;
249 const uint bpp
= util_format_get_blocksize(format
);
250 const uint bytes
= w
* h
* d
* bpp
;
251 lpr
->data
= align_malloc(bytes
, 16);
256 lpr
->id
= id_counter
++;
259 insert_at_tail(&resource_list
, lpr
);
271 llvmpipe_resource_destroy(struct pipe_screen
*pscreen
,
272 struct pipe_resource
*pt
)
274 struct llvmpipe_screen
*screen
= llvmpipe_screen(pscreen
);
275 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
279 struct sw_winsys
*winsys
= screen
->winsys
;
280 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
282 if (lpr
->tiled
[0].data
) {
283 align_free(lpr
->tiled
[0].data
);
284 lpr
->tiled
[0].data
= NULL
;
287 FREE(lpr
->layout
[0]);
289 else if (resource_is_texture(pt
)) {
290 /* regular texture */
293 /* free linear image data */
294 for (level
= 0; level
< Elements(lpr
->linear
); level
++) {
295 if (lpr
->linear
[level
].data
) {
296 align_free(lpr
->linear
[level
].data
);
297 lpr
->linear
[level
].data
= NULL
;
301 /* free tiled image data */
302 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
303 if (lpr
->tiled
[level
].data
) {
304 align_free(lpr
->tiled
[level
].data
);
305 lpr
->tiled
[level
].data
= NULL
;
309 /* free layout flag arrays */
310 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
311 FREE(lpr
->layout
[level
]);
312 lpr
->layout
[level
] = NULL
;
315 else if (!lpr
->userBuffer
) {
317 align_free(lpr
->data
);
322 remove_from_list(lpr
);
330 * Map a resource for read/write.
333 llvmpipe_resource_map(struct pipe_resource
*resource
,
336 enum lp_texture_usage tex_usage
,
337 enum lp_texture_layout layout
)
339 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
342 assert(level
< LP_MAX_TEXTURE_LEVELS
);
343 assert(layer
< (u_minify(resource
->depth0
, level
) + resource
->array_size
- 1));
345 assert(tex_usage
== LP_TEX_USAGE_READ
||
346 tex_usage
== LP_TEX_USAGE_READ_WRITE
||
347 tex_usage
== LP_TEX_USAGE_WRITE_ALL
);
349 assert(layout
== LP_TEX_LAYOUT_NONE
||
350 layout
== LP_TEX_LAYOUT_TILED
||
351 layout
== LP_TEX_LAYOUT_LINEAR
);
355 struct llvmpipe_screen
*screen
= llvmpipe_screen(resource
->screen
);
356 struct sw_winsys
*winsys
= screen
->winsys
;
360 if (tex_usage
== LP_TEX_USAGE_READ
) {
361 dt_usage
= PIPE_TRANSFER_READ
;
364 dt_usage
= PIPE_TRANSFER_READ_WRITE
;
370 /* FIXME: keep map count? */
371 map
= winsys
->displaytarget_map(winsys
, lpr
->dt
, dt_usage
);
373 /* install this linear image in texture data structure */
374 lpr
->linear
[level
].data
= map
;
376 /* make sure tiled data gets converted to linear data */
377 map2
= llvmpipe_get_texture_image(lpr
, 0, 0, tex_usage
, layout
);
378 if (layout
== LP_TEX_LAYOUT_LINEAR
)
383 else if (resource_is_texture(resource
)) {
385 map
= llvmpipe_get_texture_image(lpr
, layer
, level
,
399 llvmpipe_resource_unmap(struct pipe_resource
*resource
,
403 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
407 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(resource
->screen
);
408 struct sw_winsys
*winsys
= lp_screen
->winsys
;
413 /* make sure linear image is up to date */
414 (void) llvmpipe_get_texture_image(lpr
, layer
, level
,
416 LP_TEX_LAYOUT_LINEAR
);
418 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
424 llvmpipe_resource_data(struct pipe_resource
*resource
)
426 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
428 assert(!resource_is_texture(resource
));
434 static struct pipe_resource
*
435 llvmpipe_resource_from_handle(struct pipe_screen
*screen
,
436 const struct pipe_resource
*template,
437 struct winsys_handle
*whandle
)
439 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
440 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
441 unsigned width
, height
, width_t
, height_t
;
443 /* XXX Seems like from_handled depth textures doesn't work that well */
448 lpr
->base
= *template;
449 pipe_reference_init(&lpr
->base
.reference
, 1);
450 lpr
->base
.screen
= screen
;
452 width
= align(lpr
->base
.width0
, TILE_SIZE
);
453 height
= align(lpr
->base
.height0
, TILE_SIZE
);
454 width_t
= width
/ TILE_SIZE
;
455 height_t
= height
/ TILE_SIZE
;
458 * Looks like unaligned displaytargets work just fine,
459 * at least sampler/render ones.
462 assert(lpr
->base
.width0
== width
);
463 assert(lpr
->base
.height0
== height
);
466 lpr
->tiles_per_row
[0] = width_t
;
467 lpr
->tiles_per_image
[0] = width_t
* height_t
;
468 lpr
->num_slices_faces
[0] = 1;
469 lpr
->img_stride
[0] = 0;
471 lpr
->dt
= winsys
->displaytarget_from_handle(winsys
,
474 &lpr
->row_stride
[0]);
478 lpr
->layout
[0] = alloc_layout_array(1, lpr
->base
.width0
, lpr
->base
.height0
);
480 assert(lpr
->layout
[0]);
481 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
483 lpr
->id
= id_counter
++;
486 insert_at_tail(&resource_list
, lpr
);
498 llvmpipe_resource_get_handle(struct pipe_screen
*screen
,
499 struct pipe_resource
*pt
,
500 struct winsys_handle
*whandle
)
502 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
503 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
509 return winsys
->displaytarget_get_handle(winsys
, lpr
->dt
, whandle
);
513 static struct pipe_surface
*
514 llvmpipe_create_surface(struct pipe_context
*pipe
,
515 struct pipe_resource
*pt
,
516 const struct pipe_surface
*surf_tmpl
)
518 struct pipe_surface
*ps
;
520 assert(surf_tmpl
->u
.tex
.level
<= pt
->last_level
);
522 ps
= CALLOC_STRUCT(pipe_surface
);
524 pipe_reference_init(&ps
->reference
, 1);
525 pipe_resource_reference(&ps
->texture
, pt
);
527 ps
->format
= surf_tmpl
->format
;
528 ps
->width
= u_minify(pt
->width0
, surf_tmpl
->u
.tex
.level
);
529 ps
->height
= u_minify(pt
->height0
, surf_tmpl
->u
.tex
.level
);
530 ps
->usage
= surf_tmpl
->usage
;
532 ps
->u
.tex
.level
= surf_tmpl
->u
.tex
.level
;
533 ps
->u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
534 ps
->u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
541 llvmpipe_surface_destroy(struct pipe_context
*pipe
,
542 struct pipe_surface
*surf
)
544 /* Effectively do the texture_update work here - if texture images
545 * needed post-processing to put them into hardware layout, this is
546 * where it would happen. For llvmpipe, nothing to do.
548 assert(surf
->texture
);
549 pipe_resource_reference(&surf
->texture
, NULL
);
554 static struct pipe_transfer
*
555 llvmpipe_get_transfer(struct pipe_context
*pipe
,
556 struct pipe_resource
*resource
,
559 const struct pipe_box
*box
)
561 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
562 struct llvmpipe_resource
*lprex
= llvmpipe_resource(resource
);
563 struct llvmpipe_transfer
*lpr
;
566 assert(level
<= resource
->last_level
);
569 * Transfers, like other pipe operations, must happen in order, so flush the
570 * context if necessary.
572 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
573 boolean read_only
= !(usage
& PIPE_TRANSFER_WRITE
);
574 boolean do_not_block
= !!(usage
& PIPE_TRANSFER_DONTBLOCK
);
575 if (!llvmpipe_flush_resource(pipe
, resource
,
577 box
->depth
> 1 ? -1 : box
->z
,
579 TRUE
, /* cpu_access */
583 * It would have blocked, but state tracker requested no to.
585 assert(do_not_block
);
590 if (resource
== llvmpipe
->constants
[PIPE_SHADER_FRAGMENT
][0])
591 llvmpipe
->dirty
|= LP_NEW_CONSTANTS
;
593 lpr
= CALLOC_STRUCT(llvmpipe_transfer
);
595 struct pipe_transfer
*pt
= &lpr
->base
;
596 pipe_resource_reference(&pt
->resource
, resource
);
599 pt
->stride
= lprex
->row_stride
[level
];
600 pt
->layer_stride
= lprex
->img_stride
[level
];
610 llvmpipe_transfer_destroy(struct pipe_context
*pipe
,
611 struct pipe_transfer
*transfer
)
613 /* Effectively do the texture_update work here - if texture images
614 * needed post-processing to put them into hardware layout, this is
615 * where it would happen. For llvmpipe, nothing to do.
617 assert (transfer
->resource
);
618 pipe_resource_reference(&transfer
->resource
, NULL
);
624 llvmpipe_transfer_map( struct pipe_context
*pipe
,
625 struct pipe_transfer
*transfer
)
627 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
629 struct llvmpipe_resource
*lpr
;
630 enum pipe_format format
;
631 enum lp_texture_usage tex_usage
;
634 assert(transfer
->level
< LP_MAX_TEXTURE_LEVELS
);
637 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
638 transfer->x, transfer->y, transfer->width, transfer->height,
639 transfer->texture->width0,
640 transfer->texture->height0,
644 if (transfer
->usage
== PIPE_TRANSFER_READ
) {
645 tex_usage
= LP_TEX_USAGE_READ
;
649 tex_usage
= LP_TEX_USAGE_READ_WRITE
;
654 struct llvmpipe_resource
*lpr
= llvmpipe_resource(transfer
->resource
);
655 printf("transfer map tex %u mode %s\n", lpr
->id
, mode
);
659 assert(transfer
->resource
);
660 lpr
= llvmpipe_resource(transfer
->resource
);
661 format
= lpr
->base
.format
;
663 map
= llvmpipe_resource_map(transfer
->resource
,
666 tex_usage
, LP_TEX_LAYOUT_LINEAR
);
669 /* May want to do different things here depending on read/write nature
672 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
673 /* Do something to notify sharing contexts of a texture change.
679 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
680 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
687 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
688 struct pipe_transfer
*transfer
)
690 assert(transfer
->resource
);
692 llvmpipe_resource_unmap(transfer
->resource
,
698 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
699 struct pipe_resource
*presource
,
700 unsigned level
, int layer
)
702 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
704 if (presource
->target
== PIPE_BUFFER
)
705 return LP_UNREFERENCED
;
707 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
713 * Create buffer which wraps user-space data.
715 static struct pipe_resource
*
716 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
721 struct llvmpipe_resource
*buffer
;
723 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
727 pipe_reference_init(&buffer
->base
.reference
, 1);
728 buffer
->base
.screen
= screen
;
729 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
730 buffer
->base
.bind
= bind_flags
;
731 buffer
->base
.usage
= PIPE_USAGE_IMMUTABLE
;
732 buffer
->base
.flags
= 0;
733 buffer
->base
.width0
= bytes
;
734 buffer
->base
.height0
= 1;
735 buffer
->base
.depth0
= 1;
736 buffer
->base
.array_size
= 1;
737 buffer
->userBuffer
= TRUE
;
740 return &buffer
->base
;
745 * Compute size (in bytes) need to store a texture image / mipmap level,
746 * for just one cube face or one 3D texture slice
749 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
750 enum lp_texture_layout layout
)
752 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
753 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
755 assert(layout
== LP_TEX_LAYOUT_TILED
||
756 layout
== LP_TEX_LAYOUT_LINEAR
);
758 if (layout
== LP_TEX_LAYOUT_TILED
) {
759 /* for tiled layout, force a 32bpp format */
760 const enum pipe_format format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
761 const unsigned block_size
= util_format_get_blocksize(format
);
762 const unsigned nblocksy
=
763 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
764 const unsigned nblocksx
=
765 util_format_get_nblocksx(format
, align(width
, TILE_SIZE
));
766 const unsigned buffer_size
= block_size
* nblocksy
* nblocksx
;
770 /* we already computed this */
771 return lpr
->img_stride
[level
];
777 * Compute size (in bytes) need to store a texture image / mipmap level,
778 * including all cube faces or 3D image slices
781 tex_image_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
782 enum lp_texture_layout layout
)
784 const unsigned buf_size
= tex_image_face_size(lpr
, level
, layout
);
785 return buf_size
* lpr
->num_slices_faces
[level
];
790 * This function encapsulates some complicated logic for determining
791 * how to convert a tile of image data from linear layout to tiled
792 * layout, or vice versa.
793 * \param cur_layout the current tile layout
794 * \param target_layout the desired tile layout
795 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
796 * \param new_layout_return returns the new layout mode
797 * \param convert_return returns TRUE if image conversion is needed
800 layout_logic(enum lp_texture_layout cur_layout
,
801 enum lp_texture_layout target_layout
,
802 enum lp_texture_usage usage
,
803 enum lp_texture_layout
*new_layout_return
,
806 enum lp_texture_layout other_layout
, new_layout
;
810 new_layout
= 99; /* debug check */
812 if (target_layout
== LP_TEX_LAYOUT_LINEAR
) {
813 other_layout
= LP_TEX_LAYOUT_TILED
;
816 assert(target_layout
== LP_TEX_LAYOUT_TILED
);
817 other_layout
= LP_TEX_LAYOUT_LINEAR
;
820 new_layout
= target_layout
; /* may get changed below */
822 if (cur_layout
== LP_TEX_LAYOUT_BOTH
) {
823 if (usage
== LP_TEX_USAGE_READ
) {
824 new_layout
= LP_TEX_LAYOUT_BOTH
;
827 else if (cur_layout
== other_layout
) {
828 if (usage
!= LP_TEX_USAGE_WRITE_ALL
) {
829 /* need to convert tiled data to linear or vice versa */
832 if (usage
== LP_TEX_USAGE_READ
)
833 new_layout
= LP_TEX_LAYOUT_BOTH
;
837 assert(cur_layout
== LP_TEX_LAYOUT_NONE
||
838 cur_layout
== target_layout
);
841 assert(new_layout
== LP_TEX_LAYOUT_BOTH
||
842 new_layout
== target_layout
);
844 *new_layout_return
= new_layout
;
849 * Return pointer to a 2D texture image/face/slice.
850 * No tiled/linear conversion is done.
853 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
854 unsigned face_slice
, unsigned level
,
855 enum lp_texture_layout layout
)
857 struct llvmpipe_texture_image
*img
;
860 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
861 img
= &lpr
->linear
[level
];
864 assert (layout
== LP_TEX_LAYOUT_TILED
);
865 img
= &lpr
->tiled
[level
];
869 offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
873 return (ubyte
*) img
->data
+ offset
;
877 static INLINE
enum lp_texture_layout
878 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource
*lpr
,
879 unsigned face_slice
, unsigned level
,
880 unsigned x
, unsigned y
)
883 assert(resource_is_texture(&lpr
->base
));
884 assert(x
< lpr
->tiles_per_row
[level
]);
885 i
= face_slice
* lpr
->tiles_per_image
[level
]
886 + y
* lpr
->tiles_per_row
[level
] + x
;
887 return lpr
->layout
[level
][i
];
892 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource
*lpr
,
893 unsigned face_slice
, unsigned level
,
894 unsigned x
, unsigned y
,
895 enum lp_texture_layout layout
)
898 assert(resource_is_texture(&lpr
->base
));
899 assert(x
< lpr
->tiles_per_row
[level
]);
900 i
= face_slice
* lpr
->tiles_per_image
[level
]
901 + y
* lpr
->tiles_per_row
[level
] + x
;
902 lpr
->layout
[level
][i
] = layout
;
907 * Set the layout mode for all tiles in a particular image.
910 llvmpipe_set_texture_image_layout(struct llvmpipe_resource
*lpr
,
911 unsigned face_slice
, unsigned level
,
912 unsigned width_t
, unsigned height_t
,
913 enum lp_texture_layout layout
)
915 const unsigned start
= face_slice
* lpr
->tiles_per_image
[level
];
918 for (i
= 0; i
< width_t
* height_t
; i
++) {
919 lpr
->layout
[level
][start
+ i
] = layout
;
925 * Allocate storage for a linear or tile texture image (all cube
926 * faces and all 3D slices.
929 alloc_image_data(struct llvmpipe_resource
*lpr
, unsigned level
,
930 enum lp_texture_layout layout
)
932 uint alignment
= MAX2(16, util_cpu_caps
.cacheline
);
937 if (layout
== LP_TEX_LAYOUT_TILED
) {
938 /* tiled data is stored in regular memory */
939 uint buffer_size
= tex_image_size(lpr
, level
, layout
);
940 lpr
->tiled
[level
].data
= align_malloc(buffer_size
, alignment
);
943 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
945 /* we get the linear memory from the winsys */
946 struct llvmpipe_screen
*screen
= llvmpipe_screen(lpr
->base
.screen
);
947 struct sw_winsys
*winsys
= screen
->winsys
;
949 lpr
->linear
[0].data
=
950 winsys
->displaytarget_map(winsys
, lpr
->dt
,
951 PIPE_TRANSFER_READ_WRITE
);
954 /* not a display target - allocate regular memory */
955 uint buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
956 lpr
->linear
[level
].data
= align_malloc(buffer_size
, alignment
);
964 * Return pointer to texture image data (either linear or tiled layout)
965 * for a particular cube face or 3D texture slice.
967 * \param face_slice the cube face or 3D slice of interest
968 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
969 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
972 llvmpipe_get_texture_image(struct llvmpipe_resource
*lpr
,
973 unsigned face_slice
, unsigned level
,
974 enum lp_texture_usage usage
,
975 enum lp_texture_layout layout
)
978 * 'target' refers to the image which we're retrieving (either in
979 * tiled or linear layout).
980 * 'other' refers to the same image but in the other layout. (it may
983 struct llvmpipe_texture_image
*target_img
;
984 struct llvmpipe_texture_image
*other_img
;
987 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
988 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
989 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
990 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
991 enum lp_texture_layout other_layout
;
992 boolean only_allocate
;
994 assert(layout
== LP_TEX_LAYOUT_NONE
||
995 layout
== LP_TEX_LAYOUT_TILED
||
996 layout
== LP_TEX_LAYOUT_LINEAR
);
998 assert(usage
== LP_TEX_USAGE_READ
||
999 usage
== LP_TEX_USAGE_READ_WRITE
||
1000 usage
== LP_TEX_USAGE_WRITE_ALL
);
1002 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
1003 if (layout
== LP_TEX_LAYOUT_NONE
) {
1004 only_allocate
= TRUE
;
1005 layout
= LP_TEX_LAYOUT_TILED
;
1008 only_allocate
= FALSE
;
1012 assert(lpr
->linear
[level
].data
);
1015 /* which is target? which is other? */
1016 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
1017 target_img
= &lpr
->linear
[level
];
1018 other_img
= &lpr
->tiled
[level
];
1019 other_layout
= LP_TEX_LAYOUT_TILED
;
1022 target_img
= &lpr
->tiled
[level
];
1023 other_img
= &lpr
->linear
[level
];
1024 other_layout
= LP_TEX_LAYOUT_LINEAR
;
1027 target_data
= target_img
->data
;
1028 other_data
= other_img
->data
;
1031 /* allocate memory for the target image now */
1032 alloc_image_data(lpr
, level
, layout
);
1033 target_data
= target_img
->data
;
1036 if (face_slice
> 0) {
1037 unsigned target_offset
, other_offset
;
1039 target_offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
1040 other_offset
= face_slice
* tex_image_face_size(lpr
, level
, other_layout
);
1042 target_data
= (uint8_t *) target_data
+ target_offset
;
1045 other_data
= (uint8_t *) other_data
+ other_offset
;
1049 if (only_allocate
) {
1050 /* Just allocating tiled memory. Don't initialize it from the
1051 * linear data if it exists.
1057 /* may need to convert other data to the requested layout */
1058 enum lp_texture_layout new_layout
;
1061 /* loop over all image tiles, doing layout conversion where needed */
1062 for (y
= 0; y
< height_t
; y
++) {
1063 for (x
= 0; x
< width_t
; x
++) {
1064 enum lp_texture_layout cur_layout
=
1065 llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, x
, y
);
1068 layout_logic(cur_layout
, layout
, usage
, &new_layout
, &convert
);
1070 if (convert
&& other_data
&& target_data
) {
1071 if (layout
== LP_TEX_LAYOUT_TILED
) {
1072 lp_linear_to_tiled(other_data
, target_data
,
1073 x
* TILE_SIZE
, y
* TILE_SIZE
,
1074 TILE_SIZE
, TILE_SIZE
,
1076 lpr
->row_stride
[level
],
1077 lpr
->tiles_per_row
[level
]);
1080 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
1081 lp_tiled_to_linear(other_data
, target_data
,
1082 x
* TILE_SIZE
, y
* TILE_SIZE
,
1083 TILE_SIZE
, TILE_SIZE
,
1085 lpr
->row_stride
[level
],
1086 lpr
->tiles_per_row
[level
]);
1090 if (new_layout
!= cur_layout
)
1091 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, x
, y
,
1098 llvmpipe_set_texture_image_layout(lpr
, face_slice
, level
,
1099 width_t
, height_t
, layout
);
1107 * Return pointer to start of a texture image (1D, 2D, 3D, CUBE).
1108 * All cube faces and 3D slices will be converted to the requested
1110 * This is typically used when we're about to sample from a texture.
1113 llvmpipe_get_texture_image_all(struct llvmpipe_resource
*lpr
,
1115 enum lp_texture_usage usage
,
1116 enum lp_texture_layout layout
)
1118 const int slices
= lpr
->num_slices_faces
[level
];
1124 for (slice
= slices
- 1; slice
>= 0; slice
--) {
1125 map
= llvmpipe_get_texture_image(lpr
, slice
, level
, usage
, layout
);
1133 * Get pointer to a linear image (not the tile!) where the tile at (x,y)
1134 * is known to be in linear layout.
1135 * Conversion from tiled to linear will be done if necessary.
1136 * \return pointer to start of image/face (not the tile)
1139 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource
*lpr
,
1140 unsigned face_slice
, unsigned level
,
1141 enum lp_texture_usage usage
,
1142 unsigned x
, unsigned y
)
1144 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1145 enum lp_texture_layout cur_layout
, new_layout
;
1146 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1148 uint8_t *tiled_image
, *linear_image
;
1150 assert(resource_is_texture(&lpr
->base
));
1151 assert(x
% TILE_SIZE
== 0);
1152 assert(y
% TILE_SIZE
== 0);
1154 if (!linear_img
->data
) {
1155 /* allocate memory for the linear image now */
1156 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1159 /* compute address of the slice/face of the image that contains the tile */
1160 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1161 LP_TEX_LAYOUT_TILED
);
1162 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1163 LP_TEX_LAYOUT_LINEAR
);
1165 /* get current tile layout and determine if data conversion is needed */
1166 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1168 layout_logic(cur_layout
, LP_TEX_LAYOUT_LINEAR
, usage
,
1169 &new_layout
, &convert
);
1171 if (convert
&& tiled_image
&& linear_image
) {
1172 lp_tiled_to_linear(tiled_image
, linear_image
,
1173 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1174 lpr
->row_stride
[level
],
1175 lpr
->tiles_per_row
[level
]);
1178 if (new_layout
!= cur_layout
)
1179 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1181 return linear_image
;
1186 * Get pointer to tiled data for rendering.
1187 * \return pointer to the tiled data at the given tile position
1190 llvmpipe_get_texture_tile(struct llvmpipe_resource
*lpr
,
1191 unsigned face_slice
, unsigned level
,
1192 enum lp_texture_usage usage
,
1193 unsigned x
, unsigned y
)
1195 struct llvmpipe_texture_image
*tiled_img
= &lpr
->tiled
[level
];
1196 enum lp_texture_layout cur_layout
, new_layout
;
1197 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1199 uint8_t *tiled_image
, *linear_image
;
1200 unsigned tile_offset
;
1202 assert(x
% TILE_SIZE
== 0);
1203 assert(y
% TILE_SIZE
== 0);
1205 if (!tiled_img
->data
) {
1206 /* allocate memory for the tiled image now */
1207 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_TILED
);
1210 /* compute address of the slice/face of the image that contains the tile */
1211 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1212 LP_TEX_LAYOUT_TILED
);
1213 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1214 LP_TEX_LAYOUT_LINEAR
);
1216 /* get current tile layout and see if we need to convert the data */
1217 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1219 layout_logic(cur_layout
, LP_TEX_LAYOUT_TILED
, usage
, &new_layout
, &convert
);
1220 if (convert
&& linear_image
&& tiled_image
) {
1221 lp_linear_to_tiled(linear_image
, tiled_image
,
1222 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1223 lpr
->row_stride
[level
],
1224 lpr
->tiles_per_row
[level
]);
1230 if (new_layout
!= cur_layout
)
1231 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1233 /* compute, return address of the 64x64 tile */
1234 tile_offset
= (ty
* lpr
->tiles_per_row
[level
] + tx
)
1235 * TILE_SIZE
* TILE_SIZE
* 4;
1237 return (ubyte
*) tiled_image
+ tile_offset
;
1242 * Get pointer to tiled data for rendering.
1243 * \return pointer to the tiled data at the given tile position
1246 llvmpipe_unswizzle_cbuf_tile(struct llvmpipe_resource
*lpr
,
1247 unsigned face_slice
, unsigned level
,
1248 unsigned x
, unsigned y
,
1251 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1252 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1253 uint8_t *linear_image
;
1255 assert(x
% TILE_SIZE
== 0);
1256 assert(y
% TILE_SIZE
== 0);
1258 if (!linear_img
->data
) {
1259 /* allocate memory for the linear image now */
1260 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1263 /* compute address of the slice/face of the image that contains the tile */
1264 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1265 LP_TEX_LAYOUT_LINEAR
);
1268 uint ii
= x
, jj
= y
;
1269 uint tile_offset
= jj
/ TILE_SIZE
+ ii
/ TILE_SIZE
;
1270 uint byte_offset
= tile_offset
* TILE_SIZE
* TILE_SIZE
* 4;
1272 /* Note that lp_tiled_to_linear expects the tile parameter to
1273 * point at the first tile in a whole-image sized array. In
1274 * this code, we have only a single tile and have to do some
1275 * pointer arithmetic to figure out where the "image" would have
1278 lp_tiled_to_linear(tile
- byte_offset
, linear_image
,
1279 x
, y
, TILE_SIZE
, TILE_SIZE
,
1281 lpr
->row_stride
[level
],
1282 1); /* tiles per row */
1285 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
,
1286 LP_TEX_LAYOUT_LINEAR
);
1291 * Get pointer to tiled data for rendering.
1292 * \return pointer to the tiled data at the given tile position
1295 llvmpipe_swizzle_cbuf_tile(struct llvmpipe_resource
*lpr
,
1296 unsigned face_slice
, unsigned level
,
1297 unsigned x
, unsigned y
,
1300 uint8_t *linear_image
;
1302 assert(x
% TILE_SIZE
== 0);
1303 assert(y
% TILE_SIZE
== 0);
1305 /* compute address of the slice/face of the image that contains the tile */
1306 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1307 LP_TEX_LAYOUT_LINEAR
);
1310 uint ii
= x
, jj
= y
;
1311 uint tile_offset
= jj
/ TILE_SIZE
+ ii
/ TILE_SIZE
;
1312 uint byte_offset
= tile_offset
* TILE_SIZE
* TILE_SIZE
* 4;
1314 /* Note that lp_linear_to_tiled expects the tile parameter to
1315 * point at the first tile in a whole-image sized array. In
1316 * this code, we have only a single tile and have to do some
1317 * pointer arithmetic to figure out where the "image" would have
1320 lp_linear_to_tiled(linear_image
, tile
- byte_offset
,
1321 x
, y
, TILE_SIZE
, TILE_SIZE
,
1323 lpr
->row_stride
[level
],
1324 1); /* tiles per row */
1330 * Return size of resource in bytes
1333 llvmpipe_resource_size(const struct pipe_resource
*resource
)
1335 const struct llvmpipe_resource
*lpr
= llvmpipe_resource_const(resource
);
1336 unsigned lvl
, size
= 0;
1338 for (lvl
= 0; lvl
<= lpr
->base
.last_level
; lvl
++) {
1339 if (lpr
->linear
[lvl
].data
)
1340 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_LINEAR
);
1342 if (lpr
->tiled
[lvl
].data
)
1343 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_TILED
);
1352 llvmpipe_print_resources(void)
1354 struct llvmpipe_resource
*lpr
;
1355 unsigned n
= 0, total
= 0;
1357 debug_printf("LLVMPIPE: current resources:\n");
1358 foreach(lpr
, &resource_list
) {
1359 unsigned size
= llvmpipe_resource_size(&lpr
->base
);
1360 debug_printf("resource %u at %p, size %ux%ux%u: %u bytes, refcount %u\n",
1361 lpr
->id
, (void *) lpr
,
1362 lpr
->base
.width0
, lpr
->base
.height0
, lpr
->base
.depth0
,
1363 size
, lpr
->base
.reference
.count
);
1367 debug_printf("LLVMPIPE: total size of %u resources: %u\n", n
, total
);
1373 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
1376 /* init linked list for tracking resources */
1378 static boolean first_call
= TRUE
;
1380 memset(&resource_list
, 0, sizeof(resource_list
));
1381 make_empty_list(&resource_list
);
1387 screen
->resource_create
= llvmpipe_resource_create
;
1388 screen
->resource_destroy
= llvmpipe_resource_destroy
;
1389 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
1390 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
1391 screen
->user_buffer_create
= llvmpipe_user_buffer_create
;
1397 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
1399 pipe
->get_transfer
= llvmpipe_get_transfer
;
1400 pipe
->transfer_destroy
= llvmpipe_transfer_destroy
;
1401 pipe
->transfer_map
= llvmpipe_transfer_map
;
1402 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
1404 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
1405 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;
1407 pipe
->create_surface
= llvmpipe_create_surface
;
1408 pipe
->surface_destroy
= llvmpipe_surface_destroy
;