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 */
589 * It would have blocked, but state tracker requested no to.
591 assert(do_not_block
);
596 lpr
= CALLOC_STRUCT(llvmpipe_transfer
);
598 struct pipe_transfer
*pt
= &lpr
->base
;
599 pipe_resource_reference(&pt
->resource
, resource
);
602 pt
->stride
= lprex
->row_stride
[sr
.level
];
603 pt
->slice_stride
= lprex
->img_stride
[sr
.level
];
613 llvmpipe_transfer_destroy(struct pipe_context
*pipe
,
614 struct pipe_transfer
*transfer
)
616 /* Effectively do the texture_update work here - if texture images
617 * needed post-processing to put them into hardware layout, this is
618 * where it would happen. For llvmpipe, nothing to do.
620 assert (transfer
->resource
);
621 pipe_resource_reference(&transfer
->resource
, NULL
);
627 llvmpipe_transfer_map( struct pipe_context
*pipe
,
628 struct pipe_transfer
*transfer
)
630 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
632 struct llvmpipe_resource
*lpr
;
633 enum pipe_format format
;
634 enum lp_texture_usage tex_usage
;
637 assert(transfer
->sr
.face
< 6);
638 assert(transfer
->sr
.level
< LP_MAX_TEXTURE_LEVELS
);
641 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
642 transfer->x, transfer->y, transfer->width, transfer->height,
643 transfer->texture->width0,
644 transfer->texture->height0,
648 if (transfer
->usage
== PIPE_TRANSFER_READ
) {
649 tex_usage
= LP_TEX_USAGE_READ
;
653 tex_usage
= LP_TEX_USAGE_READ_WRITE
;
658 struct llvmpipe_resource
*lpr
= llvmpipe_resource(transfer
->resource
);
659 printf("transfer map tex %u mode %s\n", lpr
->id
, mode
);
663 assert(transfer
->resource
);
664 lpr
= llvmpipe_resource(transfer
->resource
);
665 format
= lpr
->base
.format
;
667 map
= llvmpipe_resource_map(transfer
->resource
,
671 tex_usage
, LP_TEX_LAYOUT_LINEAR
);
674 /* May want to do different things here depending on read/write nature
677 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
678 /* Do something to notify sharing contexts of a texture change.
684 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
685 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
692 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
693 struct pipe_transfer
*transfer
)
695 assert(transfer
->resource
);
697 llvmpipe_resource_unmap(transfer
->resource
,
704 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
705 struct pipe_resource
*presource
,
706 unsigned face
, unsigned level
)
708 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
710 if (presource
->target
== PIPE_BUFFER
)
711 return PIPE_UNREFERENCED
;
713 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
719 * Create buffer which wraps user-space data.
721 static struct pipe_resource
*
722 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
727 struct llvmpipe_resource
*buffer
;
729 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
733 pipe_reference_init(&buffer
->base
.reference
, 1);
734 buffer
->base
.screen
= screen
;
735 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
736 buffer
->base
.bind
= bind_flags
;
737 buffer
->base
.usage
= PIPE_USAGE_IMMUTABLE
;
738 buffer
->base
.flags
= 0;
739 buffer
->base
.width0
= bytes
;
740 buffer
->base
.height0
= 1;
741 buffer
->base
.depth0
= 1;
742 buffer
->userBuffer
= TRUE
;
745 return &buffer
->base
;
750 * Compute size (in bytes) need to store a texture image / mipmap level,
751 * for just one cube face or one 3D texture slice
754 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
755 enum lp_texture_layout layout
)
757 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
758 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
760 assert(layout
== LP_TEX_LAYOUT_TILED
||
761 layout
== LP_TEX_LAYOUT_LINEAR
);
763 if (layout
== LP_TEX_LAYOUT_TILED
) {
764 /* for tiled layout, force a 32bpp format */
765 const enum pipe_format format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
766 const unsigned block_size
= util_format_get_blocksize(format
);
767 const unsigned nblocksy
=
768 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
769 const unsigned nblocksx
=
770 util_format_get_nblocksx(format
, align(width
, TILE_SIZE
));
771 const unsigned buffer_size
= block_size
* nblocksy
* nblocksx
;
775 /* we already computed this */
776 return lpr
->img_stride
[level
];
782 * Compute size (in bytes) need to store a texture image / mipmap level,
783 * including all cube faces or 3D image slices
786 tex_image_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
787 enum lp_texture_layout layout
)
789 const unsigned buf_size
= tex_image_face_size(lpr
, level
, layout
);
790 return buf_size
* lpr
->num_slices_faces
[level
];
795 * This function encapsulates some complicated logic for determining
796 * how to convert a tile of image data from linear layout to tiled
797 * layout, or vice versa.
798 * \param cur_layout the current tile layout
799 * \param target_layout the desired tile layout
800 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
801 * \param new_layout_return returns the new layout mode
802 * \param convert_return returns TRUE if image conversion is needed
805 layout_logic(enum lp_texture_layout cur_layout
,
806 enum lp_texture_layout target_layout
,
807 enum lp_texture_usage usage
,
808 enum lp_texture_layout
*new_layout_return
,
811 enum lp_texture_layout other_layout
, new_layout
;
815 new_layout
= 99; /* debug check */
817 if (target_layout
== LP_TEX_LAYOUT_LINEAR
) {
818 other_layout
= LP_TEX_LAYOUT_TILED
;
821 assert(target_layout
== LP_TEX_LAYOUT_TILED
);
822 other_layout
= LP_TEX_LAYOUT_LINEAR
;
825 new_layout
= target_layout
; /* may get changed below */
827 if (cur_layout
== LP_TEX_LAYOUT_BOTH
) {
828 if (usage
== LP_TEX_USAGE_READ
) {
829 new_layout
= LP_TEX_LAYOUT_BOTH
;
832 else if (cur_layout
== other_layout
) {
833 if (usage
!= LP_TEX_USAGE_WRITE_ALL
) {
834 /* need to convert tiled data to linear or vice versa */
837 if (usage
== LP_TEX_USAGE_READ
)
838 new_layout
= LP_TEX_LAYOUT_BOTH
;
842 assert(cur_layout
== LP_TEX_LAYOUT_NONE
||
843 cur_layout
== target_layout
);
846 assert(new_layout
== LP_TEX_LAYOUT_BOTH
||
847 new_layout
== target_layout
);
849 *new_layout_return
= new_layout
;
854 * Return pointer to a 2D texture image/face/slice.
855 * No tiled/linear conversion is done.
858 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
859 unsigned face_slice
, unsigned level
,
860 enum lp_texture_layout layout
)
862 struct llvmpipe_texture_image
*img
;
865 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
866 img
= &lpr
->linear
[level
];
869 assert (layout
== LP_TEX_LAYOUT_TILED
);
870 img
= &lpr
->tiled
[level
];
874 offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
878 return (ubyte
*) img
->data
+ offset
;
882 static INLINE
enum lp_texture_layout
883 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource
*lpr
,
884 unsigned face_slice
, unsigned level
,
885 unsigned x
, unsigned y
)
888 assert(resource_is_texture(&lpr
->base
));
889 assert(x
< lpr
->tiles_per_row
[level
]);
890 i
= face_slice
* lpr
->tiles_per_image
[level
]
891 + y
* lpr
->tiles_per_row
[level
] + x
;
892 return lpr
->layout
[level
][i
];
897 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource
*lpr
,
898 unsigned face_slice
, unsigned level
,
899 unsigned x
, unsigned y
,
900 enum lp_texture_layout layout
)
903 assert(resource_is_texture(&lpr
->base
));
904 assert(x
< lpr
->tiles_per_row
[level
]);
905 i
= face_slice
* lpr
->tiles_per_image
[level
]
906 + y
* lpr
->tiles_per_row
[level
] + x
;
907 lpr
->layout
[level
][i
] = layout
;
912 * Set the layout mode for all tiles in a particular image.
915 llvmpipe_set_texture_image_layout(struct llvmpipe_resource
*lpr
,
916 unsigned face_slice
, unsigned level
,
917 unsigned width_t
, unsigned height_t
,
918 enum lp_texture_layout layout
)
920 const unsigned start
= face_slice
* lpr
->tiles_per_image
[level
];
923 for (i
= 0; i
< width_t
* height_t
; i
++) {
924 lpr
->layout
[level
][start
+ i
] = layout
;
930 * Allocate storage for a linear or tile texture image (all cube
931 * faces and all 3D slices.
934 alloc_image_data(struct llvmpipe_resource
*lpr
, unsigned level
,
935 enum lp_texture_layout layout
)
937 uint alignment
= MAX2(16, util_cpu_caps
.cacheline
);
942 if (layout
== LP_TEX_LAYOUT_TILED
) {
943 /* tiled data is stored in regular memory */
944 uint buffer_size
= tex_image_size(lpr
, level
, layout
);
945 lpr
->tiled
[level
].data
= align_malloc(buffer_size
, alignment
);
948 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
950 /* we get the linear memory from the winsys */
951 struct llvmpipe_screen
*screen
= llvmpipe_screen(lpr
->base
.screen
);
952 struct sw_winsys
*winsys
= screen
->winsys
;
954 lpr
->linear
[0].data
=
955 winsys
->displaytarget_map(winsys
, lpr
->dt
,
956 PIPE_TRANSFER_READ_WRITE
);
959 /* not a display target - allocate regular memory */
960 uint buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
961 lpr
->linear
[level
].data
= align_malloc(buffer_size
, alignment
);
969 * Return pointer to texture image data (either linear or tiled layout)
970 * for a particular cube face or 3D texture slice.
972 * \param face_slice the cube face or 3D slice of interest
973 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
974 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
977 llvmpipe_get_texture_image(struct llvmpipe_resource
*lpr
,
978 unsigned face_slice
, unsigned level
,
979 enum lp_texture_usage usage
,
980 enum lp_texture_layout layout
)
983 * 'target' refers to the image which we're retrieving (either in
984 * tiled or linear layout).
985 * 'other' refers to the same image but in the other layout. (it may
988 struct llvmpipe_texture_image
*target_img
;
989 struct llvmpipe_texture_image
*other_img
;
992 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
993 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
994 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
995 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
996 enum lp_texture_layout other_layout
;
997 boolean only_allocate
;
999 assert(layout
== LP_TEX_LAYOUT_NONE
||
1000 layout
== LP_TEX_LAYOUT_TILED
||
1001 layout
== LP_TEX_LAYOUT_LINEAR
);
1003 assert(usage
== LP_TEX_USAGE_READ
||
1004 usage
== LP_TEX_USAGE_READ_WRITE
||
1005 usage
== LP_TEX_USAGE_WRITE_ALL
);
1007 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
1008 if (layout
== LP_TEX_LAYOUT_NONE
) {
1009 only_allocate
= TRUE
;
1010 layout
= LP_TEX_LAYOUT_TILED
;
1013 only_allocate
= FALSE
;
1017 assert(lpr
->linear
[level
].data
);
1020 /* which is target? which is other? */
1021 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
1022 target_img
= &lpr
->linear
[level
];
1023 other_img
= &lpr
->tiled
[level
];
1024 other_layout
= LP_TEX_LAYOUT_TILED
;
1027 target_img
= &lpr
->tiled
[level
];
1028 other_img
= &lpr
->linear
[level
];
1029 other_layout
= LP_TEX_LAYOUT_LINEAR
;
1032 target_data
= target_img
->data
;
1033 other_data
= other_img
->data
;
1036 /* allocate memory for the target image now */
1037 alloc_image_data(lpr
, level
, layout
);
1038 target_data
= target_img
->data
;
1041 if (face_slice
> 0) {
1042 unsigned target_offset
, other_offset
;
1044 target_offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
1045 other_offset
= face_slice
* tex_image_face_size(lpr
, level
, other_layout
);
1047 target_data
= (uint8_t *) target_data
+ target_offset
;
1050 other_data
= (uint8_t *) other_data
+ other_offset
;
1054 if (only_allocate
) {
1055 /* Just allocating tiled memory. Don't initialize it from the
1056 * linear data if it exists.
1062 /* may need to convert other data to the requested layout */
1063 enum lp_texture_layout new_layout
;
1066 /* loop over all image tiles, doing layout conversion where needed */
1067 for (y
= 0; y
< height_t
; y
++) {
1068 for (x
= 0; x
< width_t
; x
++) {
1069 enum lp_texture_layout cur_layout
=
1070 llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, x
, y
);
1073 layout_logic(cur_layout
, layout
, usage
, &new_layout
, &convert
);
1075 if (convert
&& other_data
&& target_data
) {
1076 if (layout
== LP_TEX_LAYOUT_TILED
) {
1077 lp_linear_to_tiled(other_data
, target_data
,
1078 x
* TILE_SIZE
, y
* TILE_SIZE
,
1079 TILE_SIZE
, TILE_SIZE
,
1081 lpr
->row_stride
[level
],
1082 lpr
->tiles_per_row
[level
]);
1085 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
1086 lp_tiled_to_linear(other_data
, target_data
,
1087 x
* TILE_SIZE
, y
* TILE_SIZE
,
1088 TILE_SIZE
, TILE_SIZE
,
1090 lpr
->row_stride
[level
],
1091 lpr
->tiles_per_row
[level
]);
1095 if (new_layout
!= cur_layout
)
1096 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, x
, y
,
1103 llvmpipe_set_texture_image_layout(lpr
, face_slice
, level
,
1104 width_t
, height_t
, layout
);
1112 * Return pointer to start of a texture image (1D, 2D, 3D, CUBE).
1113 * All cube faces and 3D slices will be converted to the requested
1115 * This is typically used when we're about to sample from a texture.
1118 llvmpipe_get_texture_image_all(struct llvmpipe_resource
*lpr
,
1120 enum lp_texture_usage usage
,
1121 enum lp_texture_layout layout
)
1123 const int slices
= lpr
->num_slices_faces
[level
];
1129 for (slice
= slices
- 1; slice
>= 0; slice
--) {
1130 map
= llvmpipe_get_texture_image(lpr
, slice
, level
, usage
, layout
);
1138 * Get pointer to a linear image (not the tile!) where the tile at (x,y)
1139 * is known to be in linear layout.
1140 * Conversion from tiled to linear will be done if necessary.
1141 * \return pointer to start of image/face (not the tile)
1144 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource
*lpr
,
1145 unsigned face_slice
, unsigned level
,
1146 enum lp_texture_usage usage
,
1147 unsigned x
, unsigned y
)
1149 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1150 enum lp_texture_layout cur_layout
, new_layout
;
1151 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1153 uint8_t *tiled_image
, *linear_image
;
1155 assert(resource_is_texture(&lpr
->base
));
1156 assert(x
% TILE_SIZE
== 0);
1157 assert(y
% TILE_SIZE
== 0);
1159 if (!linear_img
->data
) {
1160 /* allocate memory for the linear image now */
1161 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1164 /* compute address of the slice/face of the image that contains the tile */
1165 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1166 LP_TEX_LAYOUT_TILED
);
1167 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1168 LP_TEX_LAYOUT_LINEAR
);
1170 /* get current tile layout and determine if data conversion is needed */
1171 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1173 layout_logic(cur_layout
, LP_TEX_LAYOUT_LINEAR
, usage
,
1174 &new_layout
, &convert
);
1176 if (convert
&& tiled_image
&& linear_image
) {
1177 lp_tiled_to_linear(tiled_image
, linear_image
,
1178 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1179 lpr
->row_stride
[level
],
1180 lpr
->tiles_per_row
[level
]);
1183 if (new_layout
!= cur_layout
)
1184 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1186 return linear_image
;
1191 * Get pointer to tiled data for rendering.
1192 * \return pointer to the tiled data at the given tile position
1195 llvmpipe_get_texture_tile(struct llvmpipe_resource
*lpr
,
1196 unsigned face_slice
, unsigned level
,
1197 enum lp_texture_usage usage
,
1198 unsigned x
, unsigned y
)
1200 struct llvmpipe_texture_image
*tiled_img
= &lpr
->tiled
[level
];
1201 enum lp_texture_layout cur_layout
, new_layout
;
1202 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1204 uint8_t *tiled_image
, *linear_image
;
1205 unsigned tile_offset
;
1207 assert(x
% TILE_SIZE
== 0);
1208 assert(y
% TILE_SIZE
== 0);
1210 if (!tiled_img
->data
) {
1211 /* allocate memory for the tiled image now */
1212 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_TILED
);
1215 /* compute address of the slice/face of the image that contains the tile */
1216 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1217 LP_TEX_LAYOUT_TILED
);
1218 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1219 LP_TEX_LAYOUT_LINEAR
);
1221 /* get current tile layout and see if we need to convert the data */
1222 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1224 layout_logic(cur_layout
, LP_TEX_LAYOUT_TILED
, usage
, &new_layout
, &convert
);
1225 if (convert
&& linear_image
&& tiled_image
) {
1226 lp_linear_to_tiled(linear_image
, tiled_image
,
1227 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1228 lpr
->row_stride
[level
],
1229 lpr
->tiles_per_row
[level
]);
1235 if (new_layout
!= cur_layout
)
1236 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1238 /* compute, return address of the 64x64 tile */
1239 tile_offset
= (ty
* lpr
->tiles_per_row
[level
] + tx
)
1240 * TILE_SIZE
* TILE_SIZE
* 4;
1242 return (ubyte
*) tiled_image
+ tile_offset
;
1247 * Get pointer to tiled data for rendering.
1248 * \return pointer to the tiled data at the given tile position
1251 llvmpipe_unswizzle_cbuf_tile(struct llvmpipe_resource
*lpr
,
1252 unsigned face_slice
, unsigned level
,
1253 unsigned x
, unsigned y
,
1256 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1257 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1258 uint8_t *linear_image
;
1260 assert(x
% TILE_SIZE
== 0);
1261 assert(y
% TILE_SIZE
== 0);
1263 if (!linear_img
->data
) {
1264 /* allocate memory for the linear image now */
1265 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1268 /* compute address of the slice/face of the image that contains the tile */
1269 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1270 LP_TEX_LAYOUT_LINEAR
);
1273 uint ii
= x
, jj
= y
;
1274 uint tile_offset
= jj
/ TILE_SIZE
+ ii
/ TILE_SIZE
;
1275 uint byte_offset
= tile_offset
* TILE_SIZE
* TILE_SIZE
* 4;
1277 /* Note that lp_tiled_to_linear expects the tile parameter to
1278 * point at the first tile in a whole-image sized array. In
1279 * this code, we have only a single tile and have to do some
1280 * pointer arithmetic to figure out where the "image" would have
1283 lp_tiled_to_linear(tile
- byte_offset
, linear_image
,
1284 x
, y
, TILE_SIZE
, TILE_SIZE
,
1286 lpr
->row_stride
[level
],
1287 1); /* tiles per row */
1290 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
,
1291 LP_TEX_LAYOUT_LINEAR
);
1296 * Get pointer to tiled data for rendering.
1297 * \return pointer to the tiled data at the given tile position
1300 llvmpipe_swizzle_cbuf_tile(struct llvmpipe_resource
*lpr
,
1301 unsigned face_slice
, unsigned level
,
1302 unsigned x
, unsigned y
,
1305 uint8_t *linear_image
;
1307 assert(x
% TILE_SIZE
== 0);
1308 assert(y
% TILE_SIZE
== 0);
1310 /* compute address of the slice/face of the image that contains the tile */
1311 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1312 LP_TEX_LAYOUT_LINEAR
);
1315 uint ii
= x
, jj
= y
;
1316 uint tile_offset
= jj
/ TILE_SIZE
+ ii
/ TILE_SIZE
;
1317 uint byte_offset
= tile_offset
* TILE_SIZE
* TILE_SIZE
* 4;
1319 /* Note that lp_linear_to_tiled expects the tile parameter to
1320 * point at the first tile in a whole-image sized array. In
1321 * this code, we have only a single tile and have to do some
1322 * pointer arithmetic to figure out where the "image" would have
1325 lp_linear_to_tiled(linear_image
, tile
- byte_offset
,
1326 x
, y
, TILE_SIZE
, TILE_SIZE
,
1328 lpr
->row_stride
[level
],
1329 1); /* tiles per row */
1335 * Return size of resource in bytes
1338 llvmpipe_resource_size(const struct pipe_resource
*resource
)
1340 const struct llvmpipe_resource
*lpr
= llvmpipe_resource_const(resource
);
1341 unsigned lvl
, size
= 0;
1343 for (lvl
= 0; lvl
<= lpr
->base
.last_level
; lvl
++) {
1344 if (lpr
->linear
[lvl
].data
)
1345 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_LINEAR
);
1347 if (lpr
->tiled
[lvl
].data
)
1348 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_TILED
);
1357 llvmpipe_print_resources(void)
1359 struct llvmpipe_resource
*lpr
;
1360 unsigned n
= 0, total
= 0;
1362 debug_printf("LLVMPIPE: current resources:\n");
1363 foreach(lpr
, &resource_list
) {
1364 unsigned size
= llvmpipe_resource_size(&lpr
->base
);
1365 debug_printf("resource %u at %p, size %ux%ux%u: %u bytes, refcount %u\n",
1366 lpr
->id
, (void *) lpr
,
1367 lpr
->base
.width0
, lpr
->base
.height0
, lpr
->base
.depth0
,
1368 size
, lpr
->base
.reference
.count
);
1372 debug_printf("LLVMPIPE: total size of %u resources: %u\n", n
, total
);
1378 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
1381 /* init linked list for tracking resources */
1383 static boolean first_call
= TRUE
;
1385 memset(&resource_list
, 0, sizeof(resource_list
));
1386 make_empty_list(&resource_list
);
1392 screen
->resource_create
= llvmpipe_resource_create
;
1393 screen
->resource_destroy
= llvmpipe_resource_destroy
;
1394 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
1395 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
1396 screen
->user_buffer_create
= llvmpipe_user_buffer_create
;
1398 screen
->get_tex_surface
= llvmpipe_get_tex_surface
;
1399 screen
->tex_surface_destroy
= llvmpipe_tex_surface_destroy
;
1404 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
1406 pipe
->get_transfer
= llvmpipe_get_transfer
;
1407 pipe
->transfer_destroy
= llvmpipe_transfer_destroy
;
1408 pipe
->transfer_map
= llvmpipe_transfer_map
;
1409 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
1410 pipe
->is_resource_referenced
= llvmpipe_is_resource_referenced
;
1412 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
1413 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;