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"
53 #include "state_tracker/sw_winsys.h"
57 static struct llvmpipe_resource resource_list
;
59 static unsigned id_counter
= 0;
63 resource_is_texture(const struct pipe_resource
*resource
)
65 switch (resource
->target
) {
70 case PIPE_TEXTURE_RECT
:
72 case PIPE_TEXTURE_CUBE
:
83 * Allocate storage for llvmpipe_texture::layout array.
84 * The number of elements is width_in_tiles * height_in_tiles.
86 static enum lp_texture_layout
*
87 alloc_layout_array(unsigned num_slices
, unsigned width
, unsigned height
)
89 const unsigned tx
= align(width
, TILE_SIZE
) / TILE_SIZE
;
90 const unsigned ty
= align(height
, TILE_SIZE
) / TILE_SIZE
;
92 assert(num_slices
* tx
* ty
> 0);
93 assert(LP_TEX_LAYOUT_NONE
== 0); /* calloc'ing LP_TEX_LAYOUT_NONE here */
95 return (enum lp_texture_layout
*)
96 CALLOC(num_slices
* tx
* ty
, sizeof(enum lp_texture_layout
));
102 * Conventional allocation path for non-display textures:
103 * Just compute row strides here. Storage is allocated on demand later.
106 llvmpipe_texture_layout(struct llvmpipe_screen
*screen
,
107 struct llvmpipe_resource
*lpr
)
109 struct pipe_resource
*pt
= &lpr
->base
;
111 unsigned width
= pt
->width0
;
112 unsigned height
= pt
->height0
;
113 unsigned depth
= pt
->depth0
;
115 assert(LP_MAX_TEXTURE_2D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
116 assert(LP_MAX_TEXTURE_3D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
118 for (level
= 0; level
<= pt
->last_level
; level
++) {
120 /* Row stride and image stride (for linear layout) */
122 unsigned alignment
, nblocksx
, nblocksy
, block_size
;
124 /* For non-compressed formats we need to align the texture size
125 * to the tile size to facilitate render-to-texture.
127 if (util_format_is_compressed(pt
->format
))
130 alignment
= TILE_SIZE
;
132 nblocksx
= util_format_get_nblocksx(pt
->format
,
133 align(width
, alignment
));
134 nblocksy
= util_format_get_nblocksy(pt
->format
,
135 align(height
, alignment
));
136 block_size
= util_format_get_blocksize(pt
->format
);
138 lpr
->row_stride
[level
] = align(nblocksx
* block_size
, 16);
140 lpr
->img_stride
[level
] = lpr
->row_stride
[level
] * nblocksy
;
143 /* Size of the image in tiles (for tiled layout) */
145 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
146 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
147 lpr
->tiles_per_row
[level
] = width_t
;
148 lpr
->tiles_per_image
[level
] = width_t
* height_t
;
151 /* Number of 3D image slices or cube faces */
155 if (lpr
->base
.target
== PIPE_TEXTURE_CUBE
)
157 else if (lpr
->base
.target
== PIPE_TEXTURE_3D
)
162 lpr
->num_slices_faces
[level
] = num_slices
;
164 lpr
->layout
[level
] = alloc_layout_array(num_slices
, width
, height
);
167 /* Compute size of next mipmap level */
168 width
= u_minify(width
, 1);
169 height
= u_minify(height
, 1);
170 depth
= u_minify(depth
, 1);
179 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
180 struct llvmpipe_resource
*lpr
)
182 struct sw_winsys
*winsys
= screen
->winsys
;
184 /* Round up the surface size to a multiple of the tile size to
185 * avoid tile clipping.
187 const unsigned width
= align(lpr
->base
.width0
, TILE_SIZE
);
188 const unsigned height
= align(lpr
->base
.height0
, TILE_SIZE
);
189 const unsigned width_t
= width
/ TILE_SIZE
;
190 const unsigned height_t
= height
/ TILE_SIZE
;
192 lpr
->tiles_per_row
[0] = width_t
;
193 lpr
->tiles_per_image
[0] = width_t
* height_t
;
194 lpr
->num_slices_faces
[0] = 1;
195 lpr
->img_stride
[0] = 0;
197 lpr
->layout
[0] = alloc_layout_array(1, width
, height
);
198 //lpr->layout[0][0] = LP_TEX_LAYOUT_LINEAR;
200 lpr
->dt
= winsys
->displaytarget_create(winsys
,
205 &lpr
->row_stride
[0] );
207 return lpr
->dt
!= NULL
;
211 static struct pipe_resource
*
212 llvmpipe_resource_create(struct pipe_screen
*_screen
,
213 const struct pipe_resource
*templat
)
215 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
216 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
220 lpr
->base
= *templat
;
221 pipe_reference_init(&lpr
->base
.reference
, 1);
222 lpr
->base
.screen
= &screen
->base
;
224 /* assert(lpr->base.bind); */
226 if (resource_is_texture(&lpr
->base
)) {
227 if (lpr
->base
.bind
& PIPE_BIND_DISPLAY_TARGET
) {
228 /* displayable surface */
229 if (!llvmpipe_displaytarget_layout(screen
, lpr
))
231 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
235 if (!llvmpipe_texture_layout(screen
, lpr
))
237 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
239 assert(lpr
->layout
[0]);
242 /* other data (vertex buffer, const buffer, etc) */
243 const enum pipe_format format
= templat
->format
;
244 const uint w
= templat
->width0
/ util_format_get_blockheight(format
);
245 const uint h
= templat
->height0
/ util_format_get_blockwidth(format
);
246 const uint d
= templat
->depth0
;
247 const uint bpp
= util_format_get_blocksize(format
);
248 const uint bytes
= w
* h
* d
* bpp
;
249 lpr
->data
= align_malloc(bytes
, 16);
254 lpr
->id
= id_counter
++;
257 insert_at_tail(&resource_list
, lpr
);
269 llvmpipe_resource_destroy(struct pipe_screen
*pscreen
,
270 struct pipe_resource
*pt
)
272 struct llvmpipe_screen
*screen
= llvmpipe_screen(pscreen
);
273 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
277 struct sw_winsys
*winsys
= screen
->winsys
;
278 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
280 if (lpr
->tiled
[0].data
) {
281 align_free(lpr
->tiled
[0].data
);
282 lpr
->tiled
[0].data
= NULL
;
285 FREE(lpr
->layout
[0]);
287 else if (resource_is_texture(pt
)) {
288 /* regular texture */
291 /* free linear image data */
292 for (level
= 0; level
< Elements(lpr
->linear
); level
++) {
293 if (lpr
->linear
[level
].data
) {
294 align_free(lpr
->linear
[level
].data
);
295 lpr
->linear
[level
].data
= NULL
;
299 /* free tiled image data */
300 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
301 if (lpr
->tiled
[level
].data
) {
302 align_free(lpr
->tiled
[level
].data
);
303 lpr
->tiled
[level
].data
= NULL
;
307 /* free layout flag arrays */
308 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
309 FREE(lpr
->layout
[level
]);
310 lpr
->layout
[level
] = NULL
;
313 else if (!lpr
->userBuffer
) {
315 align_free(lpr
->data
);
320 remove_from_list(lpr
);
328 * Map a resource for read/write.
331 llvmpipe_resource_map(struct pipe_resource
*resource
,
335 enum lp_texture_usage tex_usage
,
336 enum lp_texture_layout layout
)
338 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
342 assert(level
< LP_MAX_TEXTURE_LEVELS
);
344 assert(tex_usage
== LP_TEX_USAGE_READ
||
345 tex_usage
== LP_TEX_USAGE_READ_WRITE
||
346 tex_usage
== LP_TEX_USAGE_WRITE_ALL
);
348 assert(layout
== LP_TEX_LAYOUT_NONE
||
349 layout
== LP_TEX_LAYOUT_TILED
||
350 layout
== LP_TEX_LAYOUT_LINEAR
);
354 struct llvmpipe_screen
*screen
= llvmpipe_screen(resource
->screen
);
355 struct sw_winsys
*winsys
= screen
->winsys
;
359 if (tex_usage
== LP_TEX_USAGE_READ
) {
360 dt_usage
= PIPE_TRANSFER_READ
;
363 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
)) {
384 /* regular texture */
385 if (resource
->target
!= PIPE_TEXTURE_CUBE
) {
388 if (resource
->target
!= PIPE_TEXTURE_3D
) {
392 map
= llvmpipe_get_texture_image(lpr
, face
+ zslice
, level
,
406 llvmpipe_resource_unmap(struct pipe_resource
*resource
,
411 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
415 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(resource
->screen
);
416 struct sw_winsys
*winsys
= lp_screen
->winsys
;
422 /* make sure linear image is up to date */
423 (void) llvmpipe_get_texture_image(lpr
, face
+ zslice
, level
,
425 LP_TEX_LAYOUT_LINEAR
);
427 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
433 llvmpipe_resource_data(struct pipe_resource
*resource
)
435 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
437 assert(!resource_is_texture(resource
));
443 static struct pipe_resource
*
444 llvmpipe_resource_from_handle(struct pipe_screen
*screen
,
445 const struct pipe_resource
*template,
446 struct winsys_handle
*whandle
)
448 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
449 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
450 unsigned width
, height
, width_t
, height_t
;
452 /* XXX Seems like from_handled depth textures doesn't work that well */
457 lpr
->base
= *template;
458 pipe_reference_init(&lpr
->base
.reference
, 1);
459 lpr
->base
.screen
= screen
;
461 width
= align(lpr
->base
.width0
, TILE_SIZE
);
462 height
= align(lpr
->base
.height0
, TILE_SIZE
);
463 width_t
= width
/ TILE_SIZE
;
464 height_t
= height
/ TILE_SIZE
;
467 * Looks like unaligned displaytargets work just fine,
468 * at least sampler/render ones.
471 assert(lpr
->base
.width0
== width
);
472 assert(lpr
->base
.height0
== height
);
475 lpr
->tiles_per_row
[0] = width_t
;
476 lpr
->tiles_per_image
[0] = width_t
* height_t
;
477 lpr
->num_slices_faces
[0] = 1;
478 lpr
->img_stride
[0] = 0;
480 lpr
->dt
= winsys
->displaytarget_from_handle(winsys
,
483 &lpr
->row_stride
[0]);
487 lpr
->layout
[0] = alloc_layout_array(1, lpr
->base
.width0
, lpr
->base
.height0
);
489 assert(lpr
->layout
[0]);
490 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
492 lpr
->id
= id_counter
++;
495 insert_at_tail(&resource_list
, lpr
);
507 llvmpipe_resource_get_handle(struct pipe_screen
*screen
,
508 struct pipe_resource
*pt
,
509 struct winsys_handle
*whandle
)
511 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
512 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
518 return winsys
->displaytarget_get_handle(winsys
, lpr
->dt
, whandle
);
522 static struct pipe_surface
*
523 llvmpipe_get_tex_surface(struct pipe_screen
*screen
,
524 struct pipe_resource
*pt
,
525 unsigned face
, unsigned level
, unsigned zslice
,
528 struct pipe_surface
*ps
;
530 assert(level
<= pt
->last_level
);
532 ps
= CALLOC_STRUCT(pipe_surface
);
534 pipe_reference_init(&ps
->reference
, 1);
535 pipe_resource_reference(&ps
->texture
, pt
);
536 ps
->format
= pt
->format
;
537 ps
->width
= u_minify(pt
->width0
, level
);
538 ps
->height
= u_minify(pt
->height0
, level
);
550 llvmpipe_tex_surface_destroy(struct pipe_surface
*surf
)
552 /* Effectively do the texture_update work here - if texture images
553 * needed post-processing to put them into hardware layout, this is
554 * where it would happen. For llvmpipe, nothing to do.
556 assert(surf
->texture
);
557 pipe_resource_reference(&surf
->texture
, NULL
);
562 static struct pipe_transfer
*
563 llvmpipe_get_transfer(struct pipe_context
*pipe
,
564 struct pipe_resource
*resource
,
565 struct pipe_subresource sr
,
567 const struct pipe_box
*box
)
569 struct llvmpipe_resource
*lprex
= llvmpipe_resource(resource
);
570 struct llvmpipe_transfer
*lpr
;
573 assert(sr
.level
<= resource
->last_level
);
576 * Transfers, like other pipe operations, must happen in order, so flush the
577 * context if necessary.
579 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
580 boolean read_only
= !(usage
& PIPE_TRANSFER_WRITE
);
581 boolean do_not_block
= !!(usage
& PIPE_TRANSFER_DONTBLOCK
);
582 if (!llvmpipe_flush_resource(pipe
, resource
,
586 TRUE
, /* cpu_access */
590 * It would have blocked, but state tracker requested no to.
592 assert(do_not_block
);
597 lpr
= CALLOC_STRUCT(llvmpipe_transfer
);
599 struct pipe_transfer
*pt
= &lpr
->base
;
600 pipe_resource_reference(&pt
->resource
, resource
);
603 pt
->stride
= lprex
->row_stride
[sr
.level
];
604 pt
->slice_stride
= lprex
->img_stride
[sr
.level
];
614 llvmpipe_transfer_destroy(struct pipe_context
*pipe
,
615 struct pipe_transfer
*transfer
)
617 /* Effectively do the texture_update work here - if texture images
618 * needed post-processing to put them into hardware layout, this is
619 * where it would happen. For llvmpipe, nothing to do.
621 assert (transfer
->resource
);
622 pipe_resource_reference(&transfer
->resource
, NULL
);
628 llvmpipe_transfer_map( struct pipe_context
*pipe
,
629 struct pipe_transfer
*transfer
)
631 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
633 struct llvmpipe_resource
*lpr
;
634 enum pipe_format format
;
635 enum lp_texture_usage tex_usage
;
638 assert(transfer
->sr
.face
< 6);
639 assert(transfer
->sr
.level
< LP_MAX_TEXTURE_LEVELS
);
642 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
643 transfer->x, transfer->y, transfer->width, transfer->height,
644 transfer->texture->width0,
645 transfer->texture->height0,
649 if (transfer
->usage
== PIPE_TRANSFER_READ
) {
650 tex_usage
= LP_TEX_USAGE_READ
;
654 tex_usage
= LP_TEX_USAGE_READ_WRITE
;
659 struct llvmpipe_resource
*lpr
= llvmpipe_resource(transfer
->resource
);
660 printf("transfer map tex %u mode %s\n", lpr
->id
, mode
);
664 assert(transfer
->resource
);
665 lpr
= llvmpipe_resource(transfer
->resource
);
666 format
= lpr
->base
.format
;
668 map
= llvmpipe_resource_map(transfer
->resource
,
672 tex_usage
, LP_TEX_LAYOUT_LINEAR
);
675 /* May want to do different things here depending on read/write nature
678 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
679 /* Do something to notify sharing contexts of a texture change.
685 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
686 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
693 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
694 struct pipe_transfer
*transfer
)
696 assert(transfer
->resource
);
698 llvmpipe_resource_unmap(transfer
->resource
,
705 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
706 struct pipe_resource
*presource
,
707 unsigned face
, unsigned level
)
709 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
711 if (presource
->target
== PIPE_BUFFER
)
712 return PIPE_UNREFERENCED
;
714 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
720 * Create buffer which wraps user-space data.
722 static struct pipe_resource
*
723 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
728 struct llvmpipe_resource
*buffer
;
730 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
734 pipe_reference_init(&buffer
->base
.reference
, 1);
735 buffer
->base
.screen
= screen
;
736 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
737 buffer
->base
.bind
= bind_flags
;
738 buffer
->base
.usage
= PIPE_USAGE_IMMUTABLE
;
739 buffer
->base
.flags
= 0;
740 buffer
->base
.width0
= bytes
;
741 buffer
->base
.height0
= 1;
742 buffer
->base
.depth0
= 1;
743 buffer
->userBuffer
= TRUE
;
746 return &buffer
->base
;
751 * Compute size (in bytes) need to store a texture image / mipmap level,
752 * for just one cube face or one 3D texture slice
755 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
756 enum lp_texture_layout layout
)
758 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
759 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
761 assert(layout
== LP_TEX_LAYOUT_TILED
||
762 layout
== LP_TEX_LAYOUT_LINEAR
);
764 if (layout
== LP_TEX_LAYOUT_TILED
) {
765 /* for tiled layout, force a 32bpp format */
766 const enum pipe_format format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
767 const unsigned block_size
= util_format_get_blocksize(format
);
768 const unsigned nblocksy
=
769 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
770 const unsigned nblocksx
=
771 util_format_get_nblocksx(format
, align(width
, TILE_SIZE
));
772 const unsigned buffer_size
= block_size
* nblocksy
* nblocksx
;
776 /* we already computed this */
777 return lpr
->img_stride
[level
];
783 * Compute size (in bytes) need to store a texture image / mipmap level,
784 * including all cube faces or 3D image slices
787 tex_image_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
788 enum lp_texture_layout layout
)
790 const unsigned buf_size
= tex_image_face_size(lpr
, level
, layout
);
791 return buf_size
* lpr
->num_slices_faces
[level
];
796 * This function encapsulates some complicated logic for determining
797 * how to convert a tile of image data from linear layout to tiled
798 * layout, or vice versa.
799 * \param cur_layout the current tile layout
800 * \param target_layout the desired tile layout
801 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
802 * \param new_layout_return returns the new layout mode
803 * \param convert_return returns TRUE if image conversion is needed
806 layout_logic(enum lp_texture_layout cur_layout
,
807 enum lp_texture_layout target_layout
,
808 enum lp_texture_usage usage
,
809 enum lp_texture_layout
*new_layout_return
,
812 enum lp_texture_layout other_layout
, new_layout
;
816 new_layout
= 99; /* debug check */
818 if (target_layout
== LP_TEX_LAYOUT_LINEAR
) {
819 other_layout
= LP_TEX_LAYOUT_TILED
;
822 assert(target_layout
== LP_TEX_LAYOUT_TILED
);
823 other_layout
= LP_TEX_LAYOUT_LINEAR
;
826 new_layout
= target_layout
; /* may get changed below */
828 if (cur_layout
== LP_TEX_LAYOUT_BOTH
) {
829 if (usage
== LP_TEX_USAGE_READ
) {
830 new_layout
= LP_TEX_LAYOUT_BOTH
;
833 else if (cur_layout
== other_layout
) {
834 if (usage
!= LP_TEX_USAGE_WRITE_ALL
) {
835 /* need to convert tiled data to linear or vice versa */
838 if (usage
== LP_TEX_USAGE_READ
)
839 new_layout
= LP_TEX_LAYOUT_BOTH
;
843 assert(cur_layout
== LP_TEX_LAYOUT_NONE
||
844 cur_layout
== target_layout
);
847 assert(new_layout
== LP_TEX_LAYOUT_BOTH
||
848 new_layout
== target_layout
);
850 *new_layout_return
= new_layout
;
855 * Return pointer to a 2D texture image/face/slice.
856 * No tiled/linear conversion is done.
859 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
860 unsigned face_slice
, unsigned level
,
861 enum lp_texture_layout layout
)
863 struct llvmpipe_texture_image
*img
;
866 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
867 img
= &lpr
->linear
[level
];
870 assert (layout
== LP_TEX_LAYOUT_TILED
);
871 img
= &lpr
->tiled
[level
];
875 offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
879 return (ubyte
*) img
->data
+ offset
;
883 static INLINE
enum lp_texture_layout
884 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource
*lpr
,
885 unsigned face_slice
, unsigned level
,
886 unsigned x
, unsigned y
)
889 assert(resource_is_texture(&lpr
->base
));
890 assert(x
< lpr
->tiles_per_row
[level
]);
891 i
= face_slice
* lpr
->tiles_per_image
[level
]
892 + y
* lpr
->tiles_per_row
[level
] + x
;
893 return lpr
->layout
[level
][i
];
898 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource
*lpr
,
899 unsigned face_slice
, unsigned level
,
900 unsigned x
, unsigned y
,
901 enum lp_texture_layout layout
)
904 assert(resource_is_texture(&lpr
->base
));
905 assert(x
< lpr
->tiles_per_row
[level
]);
906 i
= face_slice
* lpr
->tiles_per_image
[level
]
907 + y
* lpr
->tiles_per_row
[level
] + x
;
908 lpr
->layout
[level
][i
] = layout
;
913 * Set the layout mode for all tiles in a particular image.
916 llvmpipe_set_texture_image_layout(struct llvmpipe_resource
*lpr
,
917 unsigned face_slice
, unsigned level
,
918 unsigned width_t
, unsigned height_t
,
919 enum lp_texture_layout layout
)
921 const unsigned start
= face_slice
* lpr
->tiles_per_image
[level
];
924 for (i
= 0; i
< width_t
* height_t
; i
++) {
925 lpr
->layout
[level
][start
+ i
] = layout
;
931 * Allocate storage for a linear or tile texture image (all cube
932 * faces and all 3D slices.
935 alloc_image_data(struct llvmpipe_resource
*lpr
, unsigned level
,
936 enum lp_texture_layout layout
)
938 uint alignment
= MAX2(16, util_cpu_caps
.cacheline
);
943 if (layout
== LP_TEX_LAYOUT_TILED
) {
944 /* tiled data is stored in regular memory */
945 uint buffer_size
= tex_image_size(lpr
, level
, layout
);
946 lpr
->tiled
[level
].data
= align_malloc(buffer_size
, alignment
);
949 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
951 /* we get the linear memory from the winsys */
952 struct llvmpipe_screen
*screen
= llvmpipe_screen(lpr
->base
.screen
);
953 struct sw_winsys
*winsys
= screen
->winsys
;
955 lpr
->linear
[0].data
=
956 winsys
->displaytarget_map(winsys
, lpr
->dt
,
957 PIPE_TRANSFER_READ_WRITE
);
960 /* not a display target - allocate regular memory */
961 uint buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
962 lpr
->linear
[level
].data
= align_malloc(buffer_size
, alignment
);
970 * Return pointer to texture image data (either linear or tiled layout)
971 * for a particular cube face or 3D texture slice.
973 * \param face_slice the cube face or 3D slice of interest
974 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
975 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
978 llvmpipe_get_texture_image(struct llvmpipe_resource
*lpr
,
979 unsigned face_slice
, unsigned level
,
980 enum lp_texture_usage usage
,
981 enum lp_texture_layout layout
)
984 * 'target' refers to the image which we're retrieving (either in
985 * tiled or linear layout).
986 * 'other' refers to the same image but in the other layout. (it may
989 struct llvmpipe_texture_image
*target_img
;
990 struct llvmpipe_texture_image
*other_img
;
993 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
994 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
995 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
996 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
997 enum lp_texture_layout other_layout
;
998 boolean only_allocate
;
1000 assert(layout
== LP_TEX_LAYOUT_NONE
||
1001 layout
== LP_TEX_LAYOUT_TILED
||
1002 layout
== LP_TEX_LAYOUT_LINEAR
);
1004 assert(usage
== LP_TEX_USAGE_READ
||
1005 usage
== LP_TEX_USAGE_READ_WRITE
||
1006 usage
== LP_TEX_USAGE_WRITE_ALL
);
1008 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
1009 if (layout
== LP_TEX_LAYOUT_NONE
) {
1010 only_allocate
= TRUE
;
1011 layout
= LP_TEX_LAYOUT_TILED
;
1014 only_allocate
= FALSE
;
1018 assert(lpr
->linear
[level
].data
);
1021 /* which is target? which is other? */
1022 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
1023 target_img
= &lpr
->linear
[level
];
1024 other_img
= &lpr
->tiled
[level
];
1025 other_layout
= LP_TEX_LAYOUT_TILED
;
1028 target_img
= &lpr
->tiled
[level
];
1029 other_img
= &lpr
->linear
[level
];
1030 other_layout
= LP_TEX_LAYOUT_LINEAR
;
1033 target_data
= target_img
->data
;
1034 other_data
= other_img
->data
;
1037 /* allocate memory for the target image now */
1038 alloc_image_data(lpr
, level
, layout
);
1039 target_data
= target_img
->data
;
1042 if (face_slice
> 0) {
1043 unsigned target_offset
, other_offset
;
1045 target_offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
1046 other_offset
= face_slice
* tex_image_face_size(lpr
, level
, other_layout
);
1048 target_data
= (uint8_t *) target_data
+ target_offset
;
1051 other_data
= (uint8_t *) other_data
+ other_offset
;
1055 if (only_allocate
) {
1056 /* Just allocating tiled memory. Don't initialize it from the
1057 * linear data if it exists.
1063 /* may need to convert other data to the requested layout */
1064 enum lp_texture_layout new_layout
;
1067 /* loop over all image tiles, doing layout conversion where needed */
1068 for (y
= 0; y
< height_t
; y
++) {
1069 for (x
= 0; x
< width_t
; x
++) {
1070 enum lp_texture_layout cur_layout
=
1071 llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, x
, y
);
1074 layout_logic(cur_layout
, layout
, usage
, &new_layout
, &convert
);
1076 if (convert
&& other_data
&& target_data
) {
1077 if (layout
== LP_TEX_LAYOUT_TILED
) {
1078 lp_linear_to_tiled(other_data
, target_data
,
1079 x
* TILE_SIZE
, y
* TILE_SIZE
,
1080 TILE_SIZE
, TILE_SIZE
,
1082 lpr
->row_stride
[level
],
1083 lpr
->tiles_per_row
[level
]);
1086 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
1087 lp_tiled_to_linear(other_data
, target_data
,
1088 x
* TILE_SIZE
, y
* TILE_SIZE
,
1089 TILE_SIZE
, TILE_SIZE
,
1091 lpr
->row_stride
[level
],
1092 lpr
->tiles_per_row
[level
]);
1096 if (new_layout
!= cur_layout
)
1097 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, x
, y
,
1104 llvmpipe_set_texture_image_layout(lpr
, face_slice
, level
,
1105 width_t
, height_t
, layout
);
1113 * Return pointer to start of a texture image (1D, 2D, 3D, CUBE).
1114 * All cube faces and 3D slices will be converted to the requested
1116 * This is typically used when we're about to sample from a texture.
1119 llvmpipe_get_texture_image_all(struct llvmpipe_resource
*lpr
,
1121 enum lp_texture_usage usage
,
1122 enum lp_texture_layout layout
)
1124 const int slices
= lpr
->num_slices_faces
[level
];
1130 for (slice
= slices
- 1; slice
>= 0; slice
--) {
1131 map
= llvmpipe_get_texture_image(lpr
, slice
, level
, usage
, layout
);
1139 * Get pointer to a linear image (not the tile!) where the tile at (x,y)
1140 * is known to be in linear layout.
1141 * Conversion from tiled to linear will be done if necessary.
1142 * \return pointer to start of image/face (not the tile)
1145 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource
*lpr
,
1146 unsigned face_slice
, unsigned level
,
1147 enum lp_texture_usage usage
,
1148 unsigned x
, unsigned y
)
1150 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1151 enum lp_texture_layout cur_layout
, new_layout
;
1152 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1154 uint8_t *tiled_image
, *linear_image
;
1156 assert(resource_is_texture(&lpr
->base
));
1157 assert(x
% TILE_SIZE
== 0);
1158 assert(y
% TILE_SIZE
== 0);
1160 if (!linear_img
->data
) {
1161 /* allocate memory for the linear image now */
1162 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1165 /* compute address of the slice/face of the image that contains the tile */
1166 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1167 LP_TEX_LAYOUT_TILED
);
1168 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1169 LP_TEX_LAYOUT_LINEAR
);
1171 /* get current tile layout and determine if data conversion is needed */
1172 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1174 layout_logic(cur_layout
, LP_TEX_LAYOUT_LINEAR
, usage
,
1175 &new_layout
, &convert
);
1177 if (convert
&& tiled_image
&& linear_image
) {
1178 lp_tiled_to_linear(tiled_image
, linear_image
,
1179 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1180 lpr
->row_stride
[level
],
1181 lpr
->tiles_per_row
[level
]);
1184 if (new_layout
!= cur_layout
)
1185 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1187 return linear_image
;
1192 * Get pointer to tiled data for rendering.
1193 * \return pointer to the tiled data at the given tile position
1196 llvmpipe_get_texture_tile(struct llvmpipe_resource
*lpr
,
1197 unsigned face_slice
, unsigned level
,
1198 enum lp_texture_usage usage
,
1199 unsigned x
, unsigned y
)
1201 struct llvmpipe_texture_image
*tiled_img
= &lpr
->tiled
[level
];
1202 enum lp_texture_layout cur_layout
, new_layout
;
1203 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1205 uint8_t *tiled_image
, *linear_image
;
1206 unsigned tile_offset
;
1208 assert(x
% TILE_SIZE
== 0);
1209 assert(y
% TILE_SIZE
== 0);
1211 if (!tiled_img
->data
) {
1212 /* allocate memory for the tiled image now */
1213 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_TILED
);
1216 /* compute address of the slice/face of the image that contains the tile */
1217 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1218 LP_TEX_LAYOUT_TILED
);
1219 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1220 LP_TEX_LAYOUT_LINEAR
);
1222 /* get current tile layout and see if we need to convert the data */
1223 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1225 layout_logic(cur_layout
, LP_TEX_LAYOUT_TILED
, usage
, &new_layout
, &convert
);
1226 if (convert
&& linear_image
&& tiled_image
) {
1227 lp_linear_to_tiled(linear_image
, tiled_image
,
1228 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1229 lpr
->row_stride
[level
],
1230 lpr
->tiles_per_row
[level
]);
1236 if (new_layout
!= cur_layout
)
1237 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1239 /* compute, return address of the 64x64 tile */
1240 tile_offset
= (ty
* lpr
->tiles_per_row
[level
] + tx
)
1241 * TILE_SIZE
* TILE_SIZE
* 4;
1243 return (ubyte
*) tiled_image
+ tile_offset
;
1248 * Get pointer to tiled data for rendering.
1249 * \return pointer to the tiled data at the given tile position
1252 llvmpipe_unswizzle_cbuf_tile(struct llvmpipe_resource
*lpr
,
1253 unsigned face_slice
, unsigned level
,
1254 unsigned x
, unsigned y
,
1257 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1258 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1259 uint8_t *linear_image
;
1261 assert(x
% TILE_SIZE
== 0);
1262 assert(y
% TILE_SIZE
== 0);
1264 if (!linear_img
->data
) {
1265 /* allocate memory for the linear image now */
1266 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1269 /* compute address of the slice/face of the image that contains the tile */
1270 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1271 LP_TEX_LAYOUT_LINEAR
);
1274 uint ii
= x
, jj
= y
;
1275 uint tile_offset
= jj
/ TILE_SIZE
+ ii
/ TILE_SIZE
;
1276 uint byte_offset
= tile_offset
* TILE_SIZE
* TILE_SIZE
* 4;
1278 /* Note that lp_tiled_to_linear expects the tile parameter to
1279 * point at the first tile in a whole-image sized array. In
1280 * this code, we have only a single tile and have to do some
1281 * pointer arithmetic to figure out where the "image" would have
1284 lp_tiled_to_linear(tile
- byte_offset
, linear_image
,
1285 x
, y
, TILE_SIZE
, TILE_SIZE
,
1287 lpr
->row_stride
[level
],
1288 1); /* tiles per row */
1291 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
,
1292 LP_TEX_LAYOUT_LINEAR
);
1297 * Get pointer to tiled data for rendering.
1298 * \return pointer to the tiled data at the given tile position
1301 llvmpipe_swizzle_cbuf_tile(struct llvmpipe_resource
*lpr
,
1302 unsigned face_slice
, unsigned level
,
1303 unsigned x
, unsigned y
,
1306 uint8_t *linear_image
;
1308 assert(x
% TILE_SIZE
== 0);
1309 assert(y
% TILE_SIZE
== 0);
1311 /* compute address of the slice/face of the image that contains the tile */
1312 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1313 LP_TEX_LAYOUT_LINEAR
);
1316 uint ii
= x
, jj
= y
;
1317 uint tile_offset
= jj
/ TILE_SIZE
+ ii
/ TILE_SIZE
;
1318 uint byte_offset
= tile_offset
* TILE_SIZE
* TILE_SIZE
* 4;
1320 /* Note that lp_linear_to_tiled expects the tile parameter to
1321 * point at the first tile in a whole-image sized array. In
1322 * this code, we have only a single tile and have to do some
1323 * pointer arithmetic to figure out where the "image" would have
1326 lp_linear_to_tiled(linear_image
, tile
- byte_offset
,
1327 x
, y
, TILE_SIZE
, TILE_SIZE
,
1329 lpr
->row_stride
[level
],
1330 1); /* tiles per row */
1336 * Return size of resource in bytes
1339 llvmpipe_resource_size(const struct pipe_resource
*resource
)
1341 const struct llvmpipe_resource
*lpr
= llvmpipe_resource_const(resource
);
1342 unsigned lvl
, size
= 0;
1344 for (lvl
= 0; lvl
<= lpr
->base
.last_level
; lvl
++) {
1345 if (lpr
->linear
[lvl
].data
)
1346 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_LINEAR
);
1348 if (lpr
->tiled
[lvl
].data
)
1349 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_TILED
);
1358 llvmpipe_print_resources(void)
1360 struct llvmpipe_resource
*lpr
;
1361 unsigned n
= 0, total
= 0;
1363 debug_printf("LLVMPIPE: current resources:\n");
1364 foreach(lpr
, &resource_list
) {
1365 unsigned size
= llvmpipe_resource_size(&lpr
->base
);
1366 debug_printf("resource %u at %p, size %ux%ux%u: %u bytes, refcount %u\n",
1367 lpr
->id
, (void *) lpr
,
1368 lpr
->base
.width0
, lpr
->base
.height0
, lpr
->base
.depth0
,
1369 size
, lpr
->base
.reference
.count
);
1373 debug_printf("LLVMPIPE: total size of %u resources: %u\n", n
, total
);
1379 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
1382 /* init linked list for tracking resources */
1384 static boolean first_call
= TRUE
;
1386 memset(&resource_list
, 0, sizeof(resource_list
));
1387 make_empty_list(&resource_list
);
1393 screen
->resource_create
= llvmpipe_resource_create
;
1394 screen
->resource_destroy
= llvmpipe_resource_destroy
;
1395 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
1396 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
1397 screen
->user_buffer_create
= llvmpipe_user_buffer_create
;
1399 screen
->get_tex_surface
= llvmpipe_get_tex_surface
;
1400 screen
->tex_surface_destroy
= llvmpipe_tex_surface_destroy
;
1405 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
1407 pipe
->get_transfer
= llvmpipe_get_transfer
;
1408 pipe
->transfer_destroy
= llvmpipe_transfer_destroy
;
1409 pipe
->transfer_map
= llvmpipe_transfer_map
;
1410 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
1411 pipe
->is_resource_referenced
= llvmpipe_is_resource_referenced
;
1413 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
1414 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;