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
) {
71 case PIPE_TEXTURE_CUBE
:
82 * Allocate storage for llvmpipe_texture::layout array.
83 * The number of elements is width_in_tiles * height_in_tiles.
85 static enum lp_texture_layout
*
86 alloc_layout_array(unsigned num_slices
, unsigned width
, unsigned height
)
88 const unsigned tx
= align(width
, TILE_SIZE
) / TILE_SIZE
;
89 const unsigned ty
= align(height
, TILE_SIZE
) / TILE_SIZE
;
91 assert(num_slices
* tx
* ty
> 0);
92 assert(LP_TEX_LAYOUT_NONE
== 0); /* calloc'ing LP_TEX_LAYOUT_NONE here */
94 return (enum lp_texture_layout
*)
95 CALLOC(num_slices
* tx
* ty
, sizeof(enum lp_texture_layout
));
101 * Conventional allocation path for non-display textures:
102 * Just compute row strides here. Storage is allocated on demand later.
105 llvmpipe_texture_layout(struct llvmpipe_screen
*screen
,
106 struct llvmpipe_resource
*lpr
)
108 struct pipe_resource
*pt
= &lpr
->base
;
110 unsigned width
= pt
->width0
;
111 unsigned height
= pt
->height0
;
112 unsigned depth
= pt
->depth0
;
114 assert(LP_MAX_TEXTURE_2D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
115 assert(LP_MAX_TEXTURE_3D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
117 for (level
= 0; level
<= pt
->last_level
; level
++) {
119 /* Row stride and image stride (for linear layout) */
121 unsigned alignment
, nblocksx
, nblocksy
, block_size
;
123 /* For non-compressed formats we need to align the texture size
124 * to the tile size to facilitate render-to-texture.
126 if (util_format_is_compressed(pt
->format
))
129 alignment
= TILE_SIZE
;
131 nblocksx
= util_format_get_nblocksx(pt
->format
,
132 align(width
, alignment
));
133 nblocksy
= util_format_get_nblocksy(pt
->format
,
134 align(height
, alignment
));
135 block_size
= util_format_get_blocksize(pt
->format
);
137 lpr
->row_stride
[level
] = align(nblocksx
* block_size
, 16);
139 lpr
->img_stride
[level
] = lpr
->row_stride
[level
] * nblocksy
;
142 /* Size of the image in tiles (for tiled layout) */
144 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
145 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
146 lpr
->tiles_per_row
[level
] = width_t
;
147 lpr
->tiles_per_image
[level
] = width_t
* height_t
;
150 /* Number of 3D image slices or cube faces */
154 if (lpr
->base
.target
== PIPE_TEXTURE_CUBE
)
156 else if (lpr
->base
.target
== PIPE_TEXTURE_3D
)
161 lpr
->num_slices_faces
[level
] = num_slices
;
163 lpr
->layout
[level
] = alloc_layout_array(num_slices
, width
, height
);
166 /* Compute size of next mipmap level */
167 width
= u_minify(width
, 1);
168 height
= u_minify(height
, 1);
169 depth
= u_minify(depth
, 1);
178 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
179 struct llvmpipe_resource
*lpr
)
181 struct sw_winsys
*winsys
= screen
->winsys
;
183 /* Round up the surface size to a multiple of the tile size to
184 * avoid tile clipping.
186 const unsigned width
= align(lpr
->base
.width0
, TILE_SIZE
);
187 const unsigned height
= align(lpr
->base
.height0
, TILE_SIZE
);
188 const unsigned width_t
= width
/ TILE_SIZE
;
189 const unsigned height_t
= height
/ TILE_SIZE
;
191 lpr
->tiles_per_row
[0] = width_t
;
192 lpr
->tiles_per_image
[0] = width_t
* height_t
;
193 lpr
->num_slices_faces
[0] = 1;
194 lpr
->img_stride
[0] = 0;
196 lpr
->layout
[0] = alloc_layout_array(1, width
, height
);
197 //lpr->layout[0][0] = LP_TEX_LAYOUT_LINEAR;
199 lpr
->dt
= winsys
->displaytarget_create(winsys
,
204 &lpr
->row_stride
[0] );
206 return lpr
->dt
!= NULL
;
210 static struct pipe_resource
*
211 llvmpipe_resource_create(struct pipe_screen
*_screen
,
212 const struct pipe_resource
*templat
)
214 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
215 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
219 lpr
->base
= *templat
;
220 pipe_reference_init(&lpr
->base
.reference
, 1);
221 lpr
->base
.screen
= &screen
->base
;
223 /* assert(lpr->base.bind); */
225 if (resource_is_texture(&lpr
->base
)) {
226 if (lpr
->base
.bind
& PIPE_BIND_DISPLAY_TARGET
) {
227 /* displayable surface */
228 if (!llvmpipe_displaytarget_layout(screen
, lpr
))
230 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
234 if (!llvmpipe_texture_layout(screen
, lpr
))
236 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
238 assert(lpr
->layout
[0]);
241 /* other data (vertex buffer, const buffer, etc) */
242 const enum pipe_format format
= templat
->format
;
243 const uint w
= templat
->width0
/ util_format_get_blockheight(format
);
244 const uint h
= templat
->height0
/ util_format_get_blockwidth(format
);
245 const uint d
= templat
->depth0
;
246 const uint bpp
= util_format_get_blocksize(format
);
247 const uint bytes
= w
* h
* d
* bpp
;
248 lpr
->data
= align_malloc(bytes
, 16);
253 lpr
->id
= id_counter
++;
256 insert_at_tail(&resource_list
, lpr
);
268 llvmpipe_resource_destroy(struct pipe_screen
*pscreen
,
269 struct pipe_resource
*pt
)
271 struct llvmpipe_screen
*screen
= llvmpipe_screen(pscreen
);
272 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
276 struct sw_winsys
*winsys
= screen
->winsys
;
277 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
279 if (lpr
->tiled
[0].data
) {
280 align_free(lpr
->tiled
[0].data
);
281 lpr
->tiled
[0].data
= NULL
;
284 FREE(lpr
->layout
[0]);
286 else if (resource_is_texture(pt
)) {
287 /* regular texture */
290 /* free linear image data */
291 for (level
= 0; level
< Elements(lpr
->linear
); level
++) {
292 if (lpr
->linear
[level
].data
) {
293 align_free(lpr
->linear
[level
].data
);
294 lpr
->linear
[level
].data
= NULL
;
298 /* free tiled image data */
299 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
300 if (lpr
->tiled
[level
].data
) {
301 align_free(lpr
->tiled
[level
].data
);
302 lpr
->tiled
[level
].data
= NULL
;
306 /* free layout flag arrays */
307 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
308 FREE(lpr
->layout
[level
]);
309 lpr
->layout
[level
] = NULL
;
312 else if (!lpr
->userBuffer
) {
314 align_free(lpr
->data
);
319 remove_from_list(lpr
);
327 * Map a resource for read/write.
330 llvmpipe_resource_map(struct pipe_resource
*resource
,
334 enum lp_texture_usage tex_usage
,
335 enum lp_texture_layout layout
)
337 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
341 assert(level
< LP_MAX_TEXTURE_LEVELS
);
343 assert(tex_usage
== LP_TEX_USAGE_READ
||
344 tex_usage
== LP_TEX_USAGE_READ_WRITE
||
345 tex_usage
== LP_TEX_USAGE_WRITE_ALL
);
347 assert(layout
== LP_TEX_LAYOUT_NONE
||
348 layout
== LP_TEX_LAYOUT_TILED
||
349 layout
== LP_TEX_LAYOUT_LINEAR
);
353 struct llvmpipe_screen
*screen
= llvmpipe_screen(resource
->screen
);
354 struct sw_winsys
*winsys
= screen
->winsys
;
358 if (tex_usage
== LP_TEX_USAGE_READ
) {
359 dt_usage
= PIPE_TRANSFER_READ
;
362 dt_usage
= PIPE_TRANSFER_READ_WRITE
;
369 /* FIXME: keep map count? */
370 map
= winsys
->displaytarget_map(winsys
, lpr
->dt
, dt_usage
);
372 /* install this linear image in texture data structure */
373 lpr
->linear
[level
].data
= map
;
375 /* make sure tiled data gets converted to linear data */
376 map2
= llvmpipe_get_texture_image(lpr
, 0, 0, tex_usage
, layout
);
377 if (layout
== LP_TEX_LAYOUT_LINEAR
)
382 else if (resource_is_texture(resource
)) {
383 /* regular texture */
384 if (resource
->target
!= PIPE_TEXTURE_CUBE
) {
387 if (resource
->target
!= PIPE_TEXTURE_3D
) {
391 map
= llvmpipe_get_texture_image(lpr
, face
+ zslice
, level
,
405 llvmpipe_resource_unmap(struct pipe_resource
*resource
,
410 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
414 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(resource
->screen
);
415 struct sw_winsys
*winsys
= lp_screen
->winsys
;
421 /* make sure linear image is up to date */
422 (void) llvmpipe_get_texture_image(lpr
, face
+ zslice
, level
,
424 LP_TEX_LAYOUT_LINEAR
);
426 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
432 llvmpipe_resource_data(struct pipe_resource
*resource
)
434 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
436 assert(!resource_is_texture(resource
));
442 static struct pipe_resource
*
443 llvmpipe_resource_from_handle(struct pipe_screen
*screen
,
444 const struct pipe_resource
*template,
445 struct winsys_handle
*whandle
)
447 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
448 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
449 unsigned width
, height
, width_t
, height_t
;
451 /* XXX Seems like from_handled depth textures doesn't work that well */
456 lpr
->base
= *template;
457 pipe_reference_init(&lpr
->base
.reference
, 1);
458 lpr
->base
.screen
= screen
;
460 width
= align(lpr
->base
.width0
, TILE_SIZE
);
461 height
= align(lpr
->base
.height0
, TILE_SIZE
);
462 width_t
= width
/ TILE_SIZE
;
463 height_t
= height
/ TILE_SIZE
;
466 * Looks like unaligned displaytargets work just fine,
467 * at least sampler/render ones.
470 assert(lpr
->base
.width0
== width
);
471 assert(lpr
->base
.height0
== height
);
474 lpr
->tiles_per_row
[0] = width_t
;
475 lpr
->tiles_per_image
[0] = width_t
* height_t
;
476 lpr
->num_slices_faces
[0] = 1;
477 lpr
->img_stride
[0] = 0;
479 lpr
->dt
= winsys
->displaytarget_from_handle(winsys
,
482 &lpr
->row_stride
[0]);
486 lpr
->layout
[0] = alloc_layout_array(1, lpr
->base
.width0
, lpr
->base
.height0
);
488 assert(lpr
->layout
[0]);
489 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
491 lpr
->id
= id_counter
++;
494 insert_at_tail(&resource_list
, lpr
);
506 llvmpipe_resource_get_handle(struct pipe_screen
*screen
,
507 struct pipe_resource
*pt
,
508 struct winsys_handle
*whandle
)
510 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
511 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
517 return winsys
->displaytarget_get_handle(winsys
, lpr
->dt
, whandle
);
521 static struct pipe_surface
*
522 llvmpipe_get_tex_surface(struct pipe_screen
*screen
,
523 struct pipe_resource
*pt
,
524 unsigned face
, unsigned level
, unsigned zslice
,
527 struct pipe_surface
*ps
;
529 assert(level
<= pt
->last_level
);
531 ps
= CALLOC_STRUCT(pipe_surface
);
533 pipe_reference_init(&ps
->reference
, 1);
534 pipe_resource_reference(&ps
->texture
, pt
);
535 ps
->format
= pt
->format
;
536 ps
->width
= u_minify(pt
->width0
, level
);
537 ps
->height
= u_minify(pt
->height0
, level
);
549 llvmpipe_tex_surface_destroy(struct pipe_surface
*surf
)
551 /* Effectively do the texture_update work here - if texture images
552 * needed post-processing to put them into hardware layout, this is
553 * where it would happen. For llvmpipe, nothing to do.
555 assert(surf
->texture
);
556 pipe_resource_reference(&surf
->texture
, NULL
);
561 static struct pipe_transfer
*
562 llvmpipe_get_transfer(struct pipe_context
*pipe
,
563 struct pipe_resource
*resource
,
564 struct pipe_subresource sr
,
566 const struct pipe_box
*box
)
568 struct llvmpipe_resource
*lprex
= llvmpipe_resource(resource
);
569 struct llvmpipe_transfer
*lpr
;
572 assert(sr
.level
<= resource
->last_level
);
575 * Transfers, like other pipe operations, must happen in order, so flush the
576 * context if necessary.
578 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
579 boolean read_only
= !(usage
& PIPE_TRANSFER_WRITE
);
580 boolean do_not_block
= !!(usage
& PIPE_TRANSFER_DONTBLOCK
);
581 if (!llvmpipe_flush_resource(pipe
, resource
,
585 TRUE
, /* cpu_access */
588 * It would have blocked, but state tracker requested no to.
590 assert(do_not_block
);
595 lpr
= CALLOC_STRUCT(llvmpipe_transfer
);
597 struct pipe_transfer
*pt
= &lpr
->base
;
598 pipe_resource_reference(&pt
->resource
, resource
);
601 pt
->stride
= lprex
->row_stride
[sr
.level
];
602 pt
->slice_stride
= lprex
->img_stride
[sr
.level
];
612 llvmpipe_transfer_destroy(struct pipe_context
*pipe
,
613 struct pipe_transfer
*transfer
)
615 /* Effectively do the texture_update work here - if texture images
616 * needed post-processing to put them into hardware layout, this is
617 * where it would happen. For llvmpipe, nothing to do.
619 assert (transfer
->resource
);
620 pipe_resource_reference(&transfer
->resource
, NULL
);
626 llvmpipe_transfer_map( struct pipe_context
*pipe
,
627 struct pipe_transfer
*transfer
)
629 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
631 struct llvmpipe_resource
*lpr
;
632 enum pipe_format format
;
633 enum lp_texture_usage tex_usage
;
636 assert(transfer
->sr
.face
< 6);
637 assert(transfer
->sr
.level
< LP_MAX_TEXTURE_LEVELS
);
640 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
641 transfer->x, transfer->y, transfer->width, transfer->height,
642 transfer->texture->width0,
643 transfer->texture->height0,
647 if (transfer
->usage
== PIPE_TRANSFER_READ
) {
648 tex_usage
= LP_TEX_USAGE_READ
;
652 tex_usage
= LP_TEX_USAGE_READ_WRITE
;
657 struct llvmpipe_resource
*lpr
= llvmpipe_resource(transfer
->resource
);
658 printf("transfer map tex %u mode %s\n", lpr
->id
, mode
);
662 assert(transfer
->resource
);
663 lpr
= llvmpipe_resource(transfer
->resource
);
664 format
= lpr
->base
.format
;
666 map
= llvmpipe_resource_map(transfer
->resource
,
670 tex_usage
, LP_TEX_LAYOUT_LINEAR
);
673 /* May want to do different things here depending on read/write nature
676 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
677 /* Do something to notify sharing contexts of a texture change.
683 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
684 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
691 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
692 struct pipe_transfer
*transfer
)
694 assert(transfer
->resource
);
696 llvmpipe_resource_unmap(transfer
->resource
,
703 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
704 struct pipe_resource
*presource
,
705 unsigned face
, unsigned level
)
707 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
709 if (presource
->target
== PIPE_BUFFER
)
710 return PIPE_UNREFERENCED
;
712 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
718 * Create buffer which wraps user-space data.
720 static struct pipe_resource
*
721 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
726 struct llvmpipe_resource
*buffer
;
728 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
732 pipe_reference_init(&buffer
->base
.reference
, 1);
733 buffer
->base
.screen
= screen
;
734 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
735 buffer
->base
.bind
= bind_flags
;
736 buffer
->base
.usage
= PIPE_USAGE_IMMUTABLE
;
737 buffer
->base
.flags
= 0;
738 buffer
->base
.width0
= bytes
;
739 buffer
->base
.height0
= 1;
740 buffer
->base
.depth0
= 1;
741 buffer
->userBuffer
= TRUE
;
744 return &buffer
->base
;
749 * Compute size (in bytes) need to store a texture image / mipmap level,
750 * for just one cube face or one 3D texture slice
753 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
754 enum lp_texture_layout layout
)
756 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
757 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
759 assert(layout
== LP_TEX_LAYOUT_TILED
||
760 layout
== LP_TEX_LAYOUT_LINEAR
);
762 if (layout
== LP_TEX_LAYOUT_TILED
) {
763 /* for tiled layout, force a 32bpp format */
764 const enum pipe_format format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
765 const unsigned block_size
= util_format_get_blocksize(format
);
766 const unsigned nblocksy
=
767 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
768 const unsigned nblocksx
=
769 util_format_get_nblocksx(format
, align(width
, TILE_SIZE
));
770 const unsigned buffer_size
= block_size
* nblocksy
* nblocksx
;
774 /* we already computed this */
775 return lpr
->img_stride
[level
];
781 * Compute size (in bytes) need to store a texture image / mipmap level,
782 * including all cube faces or 3D image slices
785 tex_image_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
786 enum lp_texture_layout layout
)
788 const unsigned buf_size
= tex_image_face_size(lpr
, level
, layout
);
789 return buf_size
* lpr
->num_slices_faces
[level
];
794 * This function encapsulates some complicated logic for determining
795 * how to convert a tile of image data from linear layout to tiled
796 * layout, or vice versa.
797 * \param cur_layout the current tile layout
798 * \param target_layout the desired tile layout
799 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
800 * \param new_layout_return returns the new layout mode
801 * \param convert_return returns TRUE if image conversion is needed
804 layout_logic(enum lp_texture_layout cur_layout
,
805 enum lp_texture_layout target_layout
,
806 enum lp_texture_usage usage
,
807 enum lp_texture_layout
*new_layout_return
,
810 enum lp_texture_layout other_layout
, new_layout
;
814 new_layout
= 99; /* debug check */
816 if (target_layout
== LP_TEX_LAYOUT_LINEAR
) {
817 other_layout
= LP_TEX_LAYOUT_TILED
;
820 assert(target_layout
== LP_TEX_LAYOUT_TILED
);
821 other_layout
= LP_TEX_LAYOUT_LINEAR
;
824 new_layout
= target_layout
; /* may get changed below */
826 if (cur_layout
== LP_TEX_LAYOUT_BOTH
) {
827 if (usage
== LP_TEX_USAGE_READ
) {
828 new_layout
= LP_TEX_LAYOUT_BOTH
;
831 else if (cur_layout
== other_layout
) {
832 if (usage
!= LP_TEX_USAGE_WRITE_ALL
) {
833 /* need to convert tiled data to linear or vice versa */
836 if (usage
== LP_TEX_USAGE_READ
)
837 new_layout
= LP_TEX_LAYOUT_BOTH
;
841 assert(cur_layout
== LP_TEX_LAYOUT_NONE
||
842 cur_layout
== target_layout
);
845 assert(new_layout
== LP_TEX_LAYOUT_BOTH
||
846 new_layout
== target_layout
);
848 *new_layout_return
= new_layout
;
853 * Return pointer to a 2D texture image/face/slice.
854 * No tiled/linear conversion is done.
857 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
858 unsigned face_slice
, unsigned level
,
859 enum lp_texture_layout layout
)
861 struct llvmpipe_texture_image
*img
;
864 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
865 img
= &lpr
->linear
[level
];
868 assert (layout
== LP_TEX_LAYOUT_TILED
);
869 img
= &lpr
->tiled
[level
];
873 offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
877 return (ubyte
*) img
->data
+ offset
;
881 static INLINE
enum lp_texture_layout
882 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource
*lpr
,
883 unsigned face_slice
, unsigned level
,
884 unsigned x
, unsigned y
)
887 assert(resource_is_texture(&lpr
->base
));
888 assert(x
< lpr
->tiles_per_row
[level
]);
889 i
= face_slice
* lpr
->tiles_per_image
[level
]
890 + y
* lpr
->tiles_per_row
[level
] + x
;
891 return lpr
->layout
[level
][i
];
896 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource
*lpr
,
897 unsigned face_slice
, unsigned level
,
898 unsigned x
, unsigned y
,
899 enum lp_texture_layout layout
)
902 assert(resource_is_texture(&lpr
->base
));
903 assert(x
< lpr
->tiles_per_row
[level
]);
904 i
= face_slice
* lpr
->tiles_per_image
[level
]
905 + y
* lpr
->tiles_per_row
[level
] + x
;
906 lpr
->layout
[level
][i
] = layout
;
911 * Set the layout mode for all tiles in a particular image.
914 llvmpipe_set_texture_image_layout(struct llvmpipe_resource
*lpr
,
915 unsigned face_slice
, unsigned level
,
916 unsigned width_t
, unsigned height_t
,
917 enum lp_texture_layout layout
)
919 const unsigned start
= face_slice
* lpr
->tiles_per_image
[level
];
922 for (i
= 0; i
< width_t
* height_t
; i
++) {
923 lpr
->layout
[level
][start
+ i
] = layout
;
929 * Allocate storage for a linear or tile texture image (all cube
930 * faces and all 3D slices.
933 alloc_image_data(struct llvmpipe_resource
*lpr
, unsigned level
,
934 enum lp_texture_layout layout
)
936 uint alignment
= MAX2(16, util_cpu_caps
.cacheline
);
941 if (layout
== LP_TEX_LAYOUT_TILED
) {
942 /* tiled data is stored in regular memory */
943 uint buffer_size
= tex_image_size(lpr
, level
, layout
);
944 lpr
->tiled
[level
].data
= align_malloc(buffer_size
, alignment
);
947 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
949 /* we get the linear memory from the winsys */
950 struct llvmpipe_screen
*screen
= llvmpipe_screen(lpr
->base
.screen
);
951 struct sw_winsys
*winsys
= screen
->winsys
;
953 lpr
->linear
[0].data
=
954 winsys
->displaytarget_map(winsys
, lpr
->dt
,
955 PIPE_TRANSFER_READ_WRITE
);
958 /* not a display target - allocate regular memory */
959 uint buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
960 lpr
->linear
[level
].data
= align_malloc(buffer_size
, alignment
);
968 * Return pointer to texture image data (either linear or tiled layout)
969 * for a particular cube face or 3D texture slice.
971 * \param face_slice the cube face or 3D slice of interest
972 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
973 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
976 llvmpipe_get_texture_image(struct llvmpipe_resource
*lpr
,
977 unsigned face_slice
, unsigned level
,
978 enum lp_texture_usage usage
,
979 enum lp_texture_layout layout
)
982 * 'target' refers to the image which we're retrieving (either in
983 * tiled or linear layout).
984 * 'other' refers to the same image but in the other layout. (it may
987 struct llvmpipe_texture_image
*target_img
;
988 struct llvmpipe_texture_image
*other_img
;
991 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
992 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
993 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
994 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
995 enum lp_texture_layout other_layout
;
996 boolean only_allocate
;
998 assert(layout
== LP_TEX_LAYOUT_NONE
||
999 layout
== LP_TEX_LAYOUT_TILED
||
1000 layout
== LP_TEX_LAYOUT_LINEAR
);
1002 assert(usage
== LP_TEX_USAGE_READ
||
1003 usage
== LP_TEX_USAGE_READ_WRITE
||
1004 usage
== LP_TEX_USAGE_WRITE_ALL
);
1006 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
1007 if (layout
== LP_TEX_LAYOUT_NONE
) {
1008 only_allocate
= TRUE
;
1009 layout
= LP_TEX_LAYOUT_TILED
;
1012 only_allocate
= FALSE
;
1016 assert(lpr
->linear
[level
].data
);
1019 /* which is target? which is other? */
1020 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
1021 target_img
= &lpr
->linear
[level
];
1022 other_img
= &lpr
->tiled
[level
];
1023 other_layout
= LP_TEX_LAYOUT_TILED
;
1026 target_img
= &lpr
->tiled
[level
];
1027 other_img
= &lpr
->linear
[level
];
1028 other_layout
= LP_TEX_LAYOUT_LINEAR
;
1031 target_data
= target_img
->data
;
1032 other_data
= other_img
->data
;
1035 /* allocate memory for the target image now */
1036 alloc_image_data(lpr
, level
, layout
);
1037 target_data
= target_img
->data
;
1040 if (face_slice
> 0) {
1041 unsigned target_offset
, other_offset
;
1043 target_offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
1044 other_offset
= face_slice
* tex_image_face_size(lpr
, level
, other_layout
);
1046 target_data
= (uint8_t *) target_data
+ target_offset
;
1049 other_data
= (uint8_t *) other_data
+ other_offset
;
1053 if (only_allocate
) {
1054 /* Just allocating tiled memory. Don't initialize it from the
1055 * linear data if it exists.
1061 /* may need to convert other data to the requested layout */
1062 enum lp_texture_layout new_layout
;
1065 /* loop over all image tiles, doing layout conversion where needed */
1066 for (y
= 0; y
< height_t
; y
++) {
1067 for (x
= 0; x
< width_t
; x
++) {
1068 enum lp_texture_layout cur_layout
=
1069 llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, x
, y
);
1072 layout_logic(cur_layout
, layout
, usage
, &new_layout
, &convert
);
1074 if (convert
&& other_data
&& target_data
) {
1075 if (layout
== LP_TEX_LAYOUT_TILED
) {
1076 lp_linear_to_tiled(other_data
, target_data
,
1077 x
* TILE_SIZE
, y
* TILE_SIZE
,
1078 TILE_SIZE
, TILE_SIZE
,
1080 lpr
->row_stride
[level
],
1081 lpr
->tiles_per_row
[level
]);
1084 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
1085 lp_tiled_to_linear(other_data
, target_data
,
1086 x
* TILE_SIZE
, y
* TILE_SIZE
,
1087 TILE_SIZE
, TILE_SIZE
,
1089 lpr
->row_stride
[level
],
1090 lpr
->tiles_per_row
[level
]);
1094 if (new_layout
!= cur_layout
)
1095 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, x
, y
,
1102 llvmpipe_set_texture_image_layout(lpr
, face_slice
, level
,
1103 width_t
, height_t
, layout
);
1111 * Return pointer to start of a texture image (1D, 2D, 3D, CUBE).
1112 * All cube faces and 3D slices will be converted to the requested
1114 * This is typically used when we're about to sample from a texture.
1117 llvmpipe_get_texture_image_all(struct llvmpipe_resource
*lpr
,
1119 enum lp_texture_usage usage
,
1120 enum lp_texture_layout layout
)
1122 const int slices
= lpr
->num_slices_faces
[level
];
1128 for (slice
= slices
- 1; slice
>= 0; slice
--) {
1129 map
= llvmpipe_get_texture_image(lpr
, slice
, level
, usage
, layout
);
1137 * Get pointer to a linear image (not the tile!) where the tile at (x,y)
1138 * is known to be in linear layout.
1139 * Conversion from tiled to linear will be done if necessary.
1140 * \return pointer to start of image/face (not the tile)
1143 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource
*lpr
,
1144 unsigned face_slice
, unsigned level
,
1145 enum lp_texture_usage usage
,
1146 unsigned x
, unsigned y
)
1148 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1149 enum lp_texture_layout cur_layout
, new_layout
;
1150 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1152 uint8_t *tiled_image
, *linear_image
;
1154 assert(resource_is_texture(&lpr
->base
));
1155 assert(x
% TILE_SIZE
== 0);
1156 assert(y
% TILE_SIZE
== 0);
1158 if (!linear_img
->data
) {
1159 /* allocate memory for the linear image now */
1160 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1163 /* compute address of the slice/face of the image that contains the tile */
1164 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1165 LP_TEX_LAYOUT_TILED
);
1166 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1167 LP_TEX_LAYOUT_LINEAR
);
1169 /* get current tile layout and determine if data conversion is needed */
1170 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1172 layout_logic(cur_layout
, LP_TEX_LAYOUT_LINEAR
, usage
,
1173 &new_layout
, &convert
);
1175 if (convert
&& tiled_image
&& linear_image
) {
1176 lp_tiled_to_linear(tiled_image
, linear_image
,
1177 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1178 lpr
->row_stride
[level
],
1179 lpr
->tiles_per_row
[level
]);
1182 if (new_layout
!= cur_layout
)
1183 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1185 return linear_image
;
1190 * Get pointer to tiled data for rendering.
1191 * \return pointer to the tiled data at the given tile position
1194 llvmpipe_get_texture_tile(struct llvmpipe_resource
*lpr
,
1195 unsigned face_slice
, unsigned level
,
1196 enum lp_texture_usage usage
,
1197 unsigned x
, unsigned y
)
1199 struct llvmpipe_texture_image
*tiled_img
= &lpr
->tiled
[level
];
1200 enum lp_texture_layout cur_layout
, new_layout
;
1201 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1203 uint8_t *tiled_image
, *linear_image
;
1204 unsigned tile_offset
;
1206 assert(x
% TILE_SIZE
== 0);
1207 assert(y
% TILE_SIZE
== 0);
1209 if (!tiled_img
->data
) {
1210 /* allocate memory for the tiled image now */
1211 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_TILED
);
1214 /* compute address of the slice/face of the image that contains the tile */
1215 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1216 LP_TEX_LAYOUT_TILED
);
1217 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1218 LP_TEX_LAYOUT_LINEAR
);
1220 /* get current tile layout and see if we need to convert the data */
1221 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1223 layout_logic(cur_layout
, LP_TEX_LAYOUT_TILED
, usage
, &new_layout
, &convert
);
1224 if (convert
&& linear_image
&& tiled_image
) {
1225 lp_linear_to_tiled(linear_image
, tiled_image
,
1226 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1227 lpr
->row_stride
[level
],
1228 lpr
->tiles_per_row
[level
]);
1234 if (new_layout
!= cur_layout
)
1235 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1237 /* compute, return address of the 64x64 tile */
1238 tile_offset
= (ty
* lpr
->tiles_per_row
[level
] + tx
)
1239 * TILE_SIZE
* TILE_SIZE
* 4;
1241 return (ubyte
*) tiled_image
+ tile_offset
;
1246 * Get pointer to tiled data for rendering.
1247 * \return pointer to the tiled data at the given tile position
1250 llvmpipe_unswizzle_cbuf_tile(struct llvmpipe_resource
*lpr
,
1251 unsigned face_slice
, unsigned level
,
1252 unsigned x
, unsigned y
,
1255 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1256 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1257 uint8_t *linear_image
;
1259 assert(x
% TILE_SIZE
== 0);
1260 assert(y
% TILE_SIZE
== 0);
1262 if (!linear_img
->data
) {
1263 /* allocate memory for the linear image now */
1264 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1267 /* compute address of the slice/face of the image that contains the tile */
1268 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1269 LP_TEX_LAYOUT_LINEAR
);
1272 uint ii
= x
, jj
= y
;
1273 uint tile_offset
= jj
/ TILE_SIZE
+ ii
/ TILE_SIZE
;
1274 uint byte_offset
= tile_offset
* TILE_SIZE
* TILE_SIZE
* 4;
1276 /* Note that lp_tiled_to_linear expects the tile parameter to
1277 * point at the first tile in a whole-image sized array. In
1278 * this code, we have only a single tile and have to do some
1279 * pointer arithmetic to figure out where the "image" would have
1282 lp_tiled_to_linear(tile
- byte_offset
, linear_image
,
1283 x
, y
, TILE_SIZE
, TILE_SIZE
,
1285 lpr
->row_stride
[level
],
1286 1); /* tiles per row */
1289 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
,
1290 LP_TEX_LAYOUT_LINEAR
);
1295 * Get pointer to tiled data for rendering.
1296 * \return pointer to the tiled data at the given tile position
1299 llvmpipe_swizzle_cbuf_tile(struct llvmpipe_resource
*lpr
,
1300 unsigned face_slice
, unsigned level
,
1301 unsigned x
, unsigned y
,
1304 uint8_t *linear_image
;
1306 assert(x
% TILE_SIZE
== 0);
1307 assert(y
% TILE_SIZE
== 0);
1309 /* compute address of the slice/face of the image that contains the tile */
1310 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1311 LP_TEX_LAYOUT_LINEAR
);
1314 uint ii
= x
, jj
= y
;
1315 uint tile_offset
= jj
/ TILE_SIZE
+ ii
/ TILE_SIZE
;
1316 uint byte_offset
= tile_offset
* TILE_SIZE
* TILE_SIZE
* 4;
1318 /* Note that lp_linear_to_tiled expects the tile parameter to
1319 * point at the first tile in a whole-image sized array. In
1320 * this code, we have only a single tile and have to do some
1321 * pointer arithmetic to figure out where the "image" would have
1324 lp_linear_to_tiled(linear_image
, tile
- byte_offset
,
1325 x
, y
, TILE_SIZE
, TILE_SIZE
,
1327 lpr
->row_stride
[level
],
1328 1); /* tiles per row */
1334 * Return size of resource in bytes
1337 llvmpipe_resource_size(const struct pipe_resource
*resource
)
1339 const struct llvmpipe_resource
*lpr
= llvmpipe_resource_const(resource
);
1340 unsigned lvl
, size
= 0;
1342 for (lvl
= 0; lvl
<= lpr
->base
.last_level
; lvl
++) {
1343 if (lpr
->linear
[lvl
].data
)
1344 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_LINEAR
);
1346 if (lpr
->tiled
[lvl
].data
)
1347 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_TILED
);
1356 llvmpipe_print_resources(void)
1358 struct llvmpipe_resource
*lpr
;
1359 unsigned n
= 0, total
= 0;
1361 debug_printf("LLVMPIPE: current resources:\n");
1362 foreach(lpr
, &resource_list
) {
1363 unsigned size
= llvmpipe_resource_size(&lpr
->base
);
1364 debug_printf("resource %u at %p, size %ux%ux%u: %u bytes, refcount %u\n",
1365 lpr
->id
, (void *) lpr
,
1366 lpr
->base
.width0
, lpr
->base
.height0
, lpr
->base
.depth0
,
1367 size
, lpr
->base
.reference
.count
);
1371 debug_printf("LLVMPIPE: total size of %u resources: %u\n", n
, total
);
1377 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
1380 /* init linked list for tracking resources */
1382 static boolean first_call
= TRUE
;
1384 memset(&resource_list
, 0, sizeof(resource_list
));
1385 make_empty_list(&resource_list
);
1391 screen
->resource_create
= llvmpipe_resource_create
;
1392 screen
->resource_destroy
= llvmpipe_resource_destroy
;
1393 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
1394 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
1395 screen
->user_buffer_create
= llvmpipe_user_buffer_create
;
1397 screen
->get_tex_surface
= llvmpipe_get_tex_surface
;
1398 screen
->tex_surface_destroy
= llvmpipe_tex_surface_destroy
;
1403 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
1405 pipe
->get_transfer
= llvmpipe_get_transfer
;
1406 pipe
->transfer_destroy
= llvmpipe_transfer_destroy
;
1407 pipe
->transfer_map
= llvmpipe_transfer_map
;
1408 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
1409 pipe
->is_resource_referenced
= llvmpipe_is_resource_referenced
;
1411 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
1412 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;