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_format.h"
40 #include "util/u_math.h"
41 #include "util/u_memory.h"
42 #include "util/u_simple_list.h"
43 #include "util/u_transfer.h"
45 #include "lp_context.h"
47 #include "lp_screen.h"
48 #include "lp_tile_image.h"
49 #include "lp_texture.h"
52 #include "state_tracker/sw_winsys.h"
56 static struct llvmpipe_resource resource_list
;
61 resource_is_texture(const struct pipe_resource
*resource
)
63 switch (resource
->target
) {
69 case PIPE_TEXTURE_CUBE
:
80 * Allocate storage for llvmpipe_texture::layout array.
81 * The number of elements is width_in_tiles * height_in_tiles.
83 static enum lp_texture_layout
*
84 alloc_layout_array(unsigned num_slices
, unsigned width
, unsigned height
)
86 const unsigned tx
= align(width
, TILE_SIZE
) / TILE_SIZE
;
87 const unsigned ty
= align(height
, TILE_SIZE
) / TILE_SIZE
;
89 assert(num_slices
* tx
* ty
> 0);
90 assert(LP_TEX_LAYOUT_NONE
== 0); /* calloc'ing LP_TEX_LAYOUT_NONE here */
92 return (enum lp_texture_layout
*)
93 CALLOC(num_slices
* tx
* ty
, sizeof(enum lp_texture_layout
));
99 * Conventional allocation path for non-display textures:
100 * Just compute row strides here. Storage is allocated on demand later.
103 llvmpipe_texture_layout(struct llvmpipe_screen
*screen
,
104 struct llvmpipe_resource
*lpr
)
106 struct pipe_resource
*pt
= &lpr
->base
;
108 unsigned width
= pt
->width0
;
109 unsigned height
= pt
->height0
;
110 unsigned depth
= pt
->depth0
;
112 assert(LP_MAX_TEXTURE_2D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
113 assert(LP_MAX_TEXTURE_3D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
115 for (level
= 0; level
<= pt
->last_level
; level
++) {
117 /* Row stride and image stride (for linear layout) */
119 unsigned alignment
, nblocksx
, nblocksy
, block_size
;
121 /* For non-compressed formats we need to align the texture size
122 * to the tile size to facilitate render-to-texture.
124 if (util_format_is_compressed(pt
->format
))
127 alignment
= TILE_SIZE
;
129 nblocksx
= util_format_get_nblocksx(pt
->format
,
130 align(width
, alignment
));
131 nblocksy
= util_format_get_nblocksy(pt
->format
,
132 align(height
, alignment
));
133 block_size
= util_format_get_blocksize(pt
->format
);
135 lpr
->row_stride
[level
] = align(nblocksx
* block_size
, 16);
137 lpr
->img_stride
[level
] = lpr
->row_stride
[level
] * nblocksy
;
140 /* Size of the image in tiles (for tiled layout) */
142 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
143 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
144 lpr
->tiles_per_row
[level
] = width_t
;
145 lpr
->tiles_per_image
[level
] = width_t
* height_t
;
148 /* Number of 3D image slices or cube faces */
152 if (lpr
->base
.target
== PIPE_TEXTURE_CUBE
)
154 else if (lpr
->base
.target
== PIPE_TEXTURE_3D
)
159 lpr
->num_slices_faces
[level
] = num_slices
;
161 lpr
->layout
[level
] = alloc_layout_array(num_slices
, width
, height
);
164 /* Compute size of next mipmap level */
165 width
= u_minify(width
, 1);
166 height
= u_minify(height
, 1);
167 depth
= u_minify(depth
, 1);
176 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
177 struct llvmpipe_resource
*lpr
)
179 struct sw_winsys
*winsys
= screen
->winsys
;
181 /* Round up the surface size to a multiple of the tile size to
182 * avoid tile clipping.
184 const unsigned width
= align(lpr
->base
.width0
, TILE_SIZE
);
185 const unsigned height
= align(lpr
->base
.height0
, TILE_SIZE
);
186 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
187 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
189 lpr
->tiles_per_row
[0] = width_t
;
190 lpr
->tiles_per_image
[0] = width_t
* height_t
;
191 lpr
->num_slices_faces
[0] = 1;
192 lpr
->img_stride
[0] = 0;
194 lpr
->layout
[0] = alloc_layout_array(1, width
, height
);
195 //lpr->layout[0][0] = LP_TEX_LAYOUT_LINEAR;
197 lpr
->dt
= winsys
->displaytarget_create(winsys
,
202 &lpr
->row_stride
[0] );
204 return lpr
->dt
!= NULL
;
208 static struct pipe_resource
*
209 llvmpipe_resource_create(struct pipe_screen
*_screen
,
210 const struct pipe_resource
*templat
)
212 static unsigned id_counter
= 0;
213 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
214 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
218 lpr
->base
= *templat
;
219 pipe_reference_init(&lpr
->base
.reference
, 1);
220 lpr
->base
.screen
= &screen
->base
;
222 assert(lpr
->base
.bind
);
224 if (resource_is_texture(&lpr
->base
)) {
225 if (lpr
->base
.bind
& PIPE_BIND_DISPLAY_TARGET
) {
226 /* displayable surface */
227 if (!llvmpipe_displaytarget_layout(screen
, lpr
))
229 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
233 if (!llvmpipe_texture_layout(screen
, lpr
))
235 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
237 assert(lpr
->layout
[0]);
240 /* other data (vertex buffer, const buffer, etc) */
241 const enum pipe_format format
= templat
->format
;
242 const uint w
= templat
->width0
/ util_format_get_blockheight(format
);
243 const uint h
= templat
->height0
/ util_format_get_blockwidth(format
);
244 const uint d
= templat
->depth0
;
245 const uint bpp
= util_format_get_blocksize(format
);
246 const uint bytes
= w
* h
* d
* bpp
;
247 lpr
->data
= align_malloc(bytes
, 16);
252 lpr
->id
= id_counter
++;
255 insert_at_tail(&resource_list
, lpr
);
267 llvmpipe_resource_destroy(struct pipe_screen
*pscreen
,
268 struct pipe_resource
*pt
)
270 struct llvmpipe_screen
*screen
= llvmpipe_screen(pscreen
);
271 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
275 struct sw_winsys
*winsys
= screen
->winsys
;
276 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
278 if (lpr
->tiled
[0].data
) {
279 align_free(lpr
->tiled
[0].data
);
280 lpr
->tiled
[0].data
= NULL
;
283 FREE(lpr
->layout
[0]);
285 else if (resource_is_texture(pt
)) {
286 /* regular texture */
289 /* free linear image data */
290 for (level
= 0; level
< Elements(lpr
->linear
); level
++) {
291 if (lpr
->linear
[level
].data
) {
292 align_free(lpr
->linear
[level
].data
);
293 lpr
->linear
[level
].data
= NULL
;
297 /* free tiled image data */
298 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
299 if (lpr
->tiled
[level
].data
) {
300 align_free(lpr
->tiled
[level
].data
);
301 lpr
->tiled
[level
].data
= NULL
;
305 /* free layout flag arrays */
306 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
307 FREE(lpr
->layout
[level
]);
308 lpr
->layout
[level
] = NULL
;
311 else if (!lpr
->userBuffer
) {
313 align_free(lpr
->data
);
318 remove_from_list(lpr
);
326 * Map a resource for read/write.
329 llvmpipe_resource_map(struct pipe_resource
*resource
,
333 enum lp_texture_usage tex_usage
,
334 enum lp_texture_layout layout
)
336 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
340 assert(level
< LP_MAX_TEXTURE_LEVELS
);
342 assert(tex_usage
== LP_TEX_USAGE_READ
||
343 tex_usage
== LP_TEX_USAGE_READ_WRITE
||
344 tex_usage
== LP_TEX_USAGE_WRITE_ALL
);
346 assert(layout
== LP_TEX_LAYOUT_NONE
||
347 layout
== LP_TEX_LAYOUT_TILED
||
348 layout
== LP_TEX_LAYOUT_LINEAR
);
352 struct llvmpipe_screen
*screen
= llvmpipe_screen(resource
->screen
);
353 struct sw_winsys
*winsys
= screen
->winsys
;
357 if (tex_usage
== LP_TEX_USAGE_READ
) {
358 dt_usage
= PIPE_TRANSFER_READ
;
361 dt_usage
= PIPE_TRANSFER_READ_WRITE
;
368 /* FIXME: keep map count? */
369 map
= winsys
->displaytarget_map(winsys
, lpr
->dt
, dt_usage
);
371 /* install this linear image in texture data structure */
372 lpr
->linear
[level
].data
= map
;
374 /* make sure tiled data gets converted to linear data */
375 map2
= llvmpipe_get_texture_image(lpr
, 0, 0, tex_usage
, layout
);
376 if (layout
== LP_TEX_LAYOUT_LINEAR
)
381 else if (resource_is_texture(resource
)) {
382 /* regular texture */
383 if (resource
->target
!= PIPE_TEXTURE_CUBE
) {
386 if (resource
->target
!= PIPE_TEXTURE_3D
) {
390 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
);
452 lpr
->base
= *template;
453 pipe_reference_init(&lpr
->base
.reference
, 1);
454 lpr
->base
.screen
= screen
;
456 lpr
->dt
= winsys
->displaytarget_from_handle(winsys
,
459 &lpr
->row_stride
[0]);
472 llvmpipe_resource_get_handle(struct pipe_screen
*screen
,
473 struct pipe_resource
*pt
,
474 struct winsys_handle
*whandle
)
476 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
477 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
483 return winsys
->displaytarget_get_handle(winsys
, lpr
->dt
, whandle
);
487 static struct pipe_surface
*
488 llvmpipe_get_tex_surface(struct pipe_screen
*screen
,
489 struct pipe_resource
*pt
,
490 unsigned face
, unsigned level
, unsigned zslice
,
493 struct pipe_surface
*ps
;
495 assert(level
<= pt
->last_level
);
497 ps
= CALLOC_STRUCT(pipe_surface
);
499 pipe_reference_init(&ps
->reference
, 1);
500 pipe_resource_reference(&ps
->texture
, pt
);
501 ps
->format
= pt
->format
;
502 ps
->width
= u_minify(pt
->width0
, level
);
503 ps
->height
= u_minify(pt
->height0
, level
);
515 llvmpipe_tex_surface_destroy(struct pipe_surface
*surf
)
517 /* Effectively do the texture_update work here - if texture images
518 * needed post-processing to put them into hardware layout, this is
519 * where it would happen. For llvmpipe, nothing to do.
521 assert(surf
->texture
);
522 pipe_resource_reference(&surf
->texture
, NULL
);
527 static struct pipe_transfer
*
528 llvmpipe_get_transfer(struct pipe_context
*pipe
,
529 struct pipe_resource
*resource
,
530 struct pipe_subresource sr
,
532 const struct pipe_box
*box
)
534 struct llvmpipe_resource
*lprex
= llvmpipe_resource(resource
);
535 struct llvmpipe_transfer
*lpr
;
538 assert(sr
.level
<= resource
->last_level
);
541 * Transfers, like other pipe operations, must happen in order, so flush the
542 * context if necessary.
544 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
545 boolean read_only
= !(usage
& PIPE_TRANSFER_WRITE
);
546 boolean do_not_block
= !!(usage
& PIPE_TRANSFER_DONTBLOCK
);
547 if (!llvmpipe_flush_resource(pipe
, resource
,
551 TRUE
, /* cpu_access */
554 * It would have blocked, but state tracker requested no to.
556 assert(do_not_block
);
561 lpr
= CALLOC_STRUCT(llvmpipe_transfer
);
563 struct pipe_transfer
*pt
= &lpr
->base
;
564 pipe_resource_reference(&pt
->resource
, resource
);
567 pt
->stride
= lprex
->row_stride
[sr
.level
];
568 pt
->slice_stride
= lprex
->img_stride
[sr
.level
];
578 llvmpipe_transfer_destroy(struct pipe_context
*pipe
,
579 struct pipe_transfer
*transfer
)
581 /* Effectively do the texture_update work here - if texture images
582 * needed post-processing to put them into hardware layout, this is
583 * where it would happen. For llvmpipe, nothing to do.
585 assert (transfer
->resource
);
586 pipe_resource_reference(&transfer
->resource
, NULL
);
592 llvmpipe_transfer_map( struct pipe_context
*pipe
,
593 struct pipe_transfer
*transfer
)
595 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
597 struct llvmpipe_resource
*lpr
;
598 enum pipe_format format
;
599 enum lp_texture_usage tex_usage
;
602 assert(transfer
->sr
.face
< 6);
603 assert(transfer
->sr
.level
< LP_MAX_TEXTURE_LEVELS
);
606 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
607 transfer->x, transfer->y, transfer->width, transfer->height,
608 transfer->texture->width0,
609 transfer->texture->height0,
613 if (transfer
->usage
== PIPE_TRANSFER_READ
) {
614 tex_usage
= LP_TEX_USAGE_READ
;
618 tex_usage
= LP_TEX_USAGE_READ_WRITE
;
623 struct llvmpipe_resource
*lpr
= llvmpipe_resource(transfer
->resource
);
624 printf("transfer map tex %u mode %s\n", lpr
->id
, mode
);
628 assert(transfer
->resource
);
629 lpr
= llvmpipe_resource(transfer
->resource
);
630 format
= lpr
->base
.format
;
632 map
= llvmpipe_resource_map(transfer
->resource
,
636 tex_usage
, LP_TEX_LAYOUT_LINEAR
);
639 /* May want to do different things here depending on read/write nature
642 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
643 /* Do something to notify sharing contexts of a texture change.
649 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
650 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
657 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
658 struct pipe_transfer
*transfer
)
660 assert(transfer
->resource
);
662 llvmpipe_resource_unmap(transfer
->resource
,
669 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
670 struct pipe_resource
*presource
,
671 unsigned face
, unsigned level
)
673 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
675 if (presource
->target
== PIPE_BUFFER
)
676 return PIPE_UNREFERENCED
;
678 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
684 * Create buffer which wraps user-space data.
686 static struct pipe_resource
*
687 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
692 struct llvmpipe_resource
*buffer
;
694 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
698 pipe_reference_init(&buffer
->base
.reference
, 1);
699 buffer
->base
.screen
= screen
;
700 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
701 buffer
->base
.bind
= bind_flags
;
702 buffer
->base
.usage
= PIPE_USAGE_IMMUTABLE
;
703 buffer
->base
.flags
= 0;
704 buffer
->base
.width0
= bytes
;
705 buffer
->base
.height0
= 1;
706 buffer
->base
.depth0
= 1;
707 buffer
->userBuffer
= TRUE
;
710 return &buffer
->base
;
715 * Compute size (in bytes) need to store a texture image / mipmap level,
716 * for just one cube face or one 3D texture slice
719 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
720 enum lp_texture_layout layout
)
722 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
723 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
725 assert(layout
== LP_TEX_LAYOUT_TILED
||
726 layout
== LP_TEX_LAYOUT_LINEAR
);
728 if (layout
== LP_TEX_LAYOUT_TILED
) {
729 /* for tiled layout, force a 32bpp format */
730 const enum pipe_format format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
731 const unsigned block_size
= util_format_get_blocksize(format
);
732 const unsigned nblocksy
=
733 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
734 const unsigned nblocksx
=
735 util_format_get_nblocksx(format
, align(width
, TILE_SIZE
));
736 const unsigned buffer_size
= block_size
* nblocksy
* nblocksx
;
740 /* we already computed this */
741 return lpr
->img_stride
[level
];
747 * Compute size (in bytes) need to store a texture image / mipmap level,
748 * including all cube faces or 3D image slices
751 tex_image_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
752 enum lp_texture_layout layout
)
754 const unsigned buf_size
= tex_image_face_size(lpr
, level
, layout
);
755 return buf_size
* lpr
->num_slices_faces
[level
];
760 * This function encapsulates some complicated logic for determining
761 * how to convert a tile of image data from linear layout to tiled
762 * layout, or vice versa.
763 * \param cur_layout the current tile layout
764 * \param target_layout the desired tile layout
765 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
766 * \param new_layout_return returns the new layout mode
767 * \param convert_return returns TRUE if image conversion is needed
770 layout_logic(enum lp_texture_layout cur_layout
,
771 enum lp_texture_layout target_layout
,
772 enum lp_texture_usage usage
,
773 enum lp_texture_layout
*new_layout_return
,
776 enum lp_texture_layout other_layout
, new_layout
;
780 new_layout
= 99; /* debug check */
782 if (target_layout
== LP_TEX_LAYOUT_LINEAR
) {
783 other_layout
= LP_TEX_LAYOUT_TILED
;
786 assert(target_layout
== LP_TEX_LAYOUT_TILED
);
787 other_layout
= LP_TEX_LAYOUT_LINEAR
;
790 new_layout
= target_layout
; /* may get changed below */
792 if (cur_layout
== LP_TEX_LAYOUT_BOTH
) {
793 if (usage
== LP_TEX_USAGE_READ
) {
794 new_layout
= LP_TEX_LAYOUT_BOTH
;
797 else if (cur_layout
== other_layout
) {
798 if (usage
!= LP_TEX_USAGE_WRITE_ALL
) {
799 /* need to convert tiled data to linear or vice versa */
802 if (usage
== LP_TEX_USAGE_READ
)
803 new_layout
= LP_TEX_LAYOUT_BOTH
;
807 assert(cur_layout
== LP_TEX_LAYOUT_NONE
||
808 cur_layout
== target_layout
);
811 assert(new_layout
== LP_TEX_LAYOUT_BOTH
||
812 new_layout
== target_layout
);
814 *new_layout_return
= new_layout
;
819 * Return pointer to a 2D texture image/face/slice.
820 * No tiled/linear conversion is done.
823 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
824 unsigned face_slice
, unsigned level
,
825 enum lp_texture_layout layout
)
827 struct llvmpipe_texture_image
*img
;
830 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
831 img
= &lpr
->linear
[level
];
834 assert (layout
== LP_TEX_LAYOUT_TILED
);
835 img
= &lpr
->tiled
[level
];
839 offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
843 return (ubyte
*) img
->data
+ offset
;
847 static INLINE
enum lp_texture_layout
848 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource
*lpr
,
849 unsigned face_slice
, unsigned level
,
850 unsigned x
, unsigned y
)
853 assert(resource_is_texture(&lpr
->base
));
854 assert(x
< lpr
->tiles_per_row
[level
]);
855 i
= face_slice
* lpr
->tiles_per_image
[level
]
856 + y
* lpr
->tiles_per_row
[level
] + x
;
857 return lpr
->layout
[level
][i
];
862 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource
*lpr
,
863 unsigned face_slice
, unsigned level
,
864 unsigned x
, unsigned y
,
865 enum lp_texture_layout layout
)
868 assert(resource_is_texture(&lpr
->base
));
869 assert(x
< lpr
->tiles_per_row
[level
]);
870 i
= face_slice
* lpr
->tiles_per_image
[level
]
871 + y
* lpr
->tiles_per_row
[level
] + x
;
872 lpr
->layout
[level
][i
] = layout
;
877 * Set the layout mode for all tiles in a particular image.
880 llvmpipe_set_texture_image_layout(struct llvmpipe_resource
*lpr
,
881 unsigned face_slice
, unsigned level
,
882 unsigned width_t
, unsigned height_t
,
883 enum lp_texture_layout layout
)
885 const unsigned start
= face_slice
* lpr
->tiles_per_image
[level
];
888 for (i
= 0; i
< width_t
* height_t
; i
++) {
889 lpr
->layout
[level
][start
+ i
] = layout
;
895 * Allocate storage for a linear or tile texture image (all cube
896 * faces and all 3D slices.
899 alloc_image_data(struct llvmpipe_resource
*lpr
, unsigned level
,
900 enum lp_texture_layout layout
)
905 if (layout
== LP_TEX_LAYOUT_TILED
) {
906 /* tiled data is stored in regular memory */
907 uint buffer_size
= tex_image_size(lpr
, level
, layout
);
908 lpr
->tiled
[level
].data
= align_malloc(buffer_size
, 16);
911 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
913 /* we get the linear memory from the winsys */
914 struct llvmpipe_screen
*screen
= llvmpipe_screen(lpr
->base
.screen
);
915 struct sw_winsys
*winsys
= screen
->winsys
;
917 lpr
->linear
[0].data
=
918 winsys
->displaytarget_map(winsys
, lpr
->dt
,
919 PIPE_TRANSFER_READ_WRITE
);
922 /* not a display target - allocate regular memory */
923 uint buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
924 lpr
->linear
[level
].data
= align_malloc(buffer_size
, 16);
932 * Return pointer to texture image data (either linear or tiled layout)
933 * for a particular cube face or 3D texture slice.
935 * \param face_slice the cube face or 3D slice of interest
936 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
937 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
940 llvmpipe_get_texture_image(struct llvmpipe_resource
*lpr
,
941 unsigned face_slice
, unsigned level
,
942 enum lp_texture_usage usage
,
943 enum lp_texture_layout layout
)
946 * 'target' refers to the image which we're retrieving (either in
947 * tiled or linear layout).
948 * 'other' refers to the same image but in the other layout. (it may
951 struct llvmpipe_texture_image
*target_img
;
952 struct llvmpipe_texture_image
*other_img
;
955 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
956 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
957 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
958 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
959 enum lp_texture_layout other_layout
;
960 boolean only_allocate
;
962 assert(layout
== LP_TEX_LAYOUT_NONE
||
963 layout
== LP_TEX_LAYOUT_TILED
||
964 layout
== LP_TEX_LAYOUT_LINEAR
);
966 assert(usage
== LP_TEX_USAGE_READ
||
967 usage
== LP_TEX_USAGE_READ_WRITE
||
968 usage
== LP_TEX_USAGE_WRITE_ALL
);
970 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
971 if (layout
== LP_TEX_LAYOUT_NONE
) {
972 only_allocate
= TRUE
;
973 layout
= LP_TEX_LAYOUT_TILED
;
976 only_allocate
= FALSE
;
980 assert(lpr
->linear
[level
].data
);
983 /* which is target? which is other? */
984 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
985 target_img
= &lpr
->linear
[level
];
986 other_img
= &lpr
->tiled
[level
];
987 other_layout
= LP_TEX_LAYOUT_TILED
;
990 target_img
= &lpr
->tiled
[level
];
991 other_img
= &lpr
->linear
[level
];
992 other_layout
= LP_TEX_LAYOUT_LINEAR
;
995 target_data
= target_img
->data
;
996 other_data
= other_img
->data
;
999 /* allocate memory for the target image now */
1000 alloc_image_data(lpr
, level
, layout
);
1001 target_data
= target_img
->data
;
1004 if (face_slice
> 0) {
1005 unsigned target_offset
, other_offset
;
1007 target_offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
1008 other_offset
= face_slice
* tex_image_face_size(lpr
, level
, other_layout
);
1010 target_data
= (uint8_t *) target_data
+ target_offset
;
1013 other_data
= (uint8_t *) other_data
+ other_offset
;
1017 if (only_allocate
) {
1018 /* Just allocating tiled memory. Don't initialize it from the
1019 * linear data if it exists.
1025 /* may need to convert other data to the requested layout */
1026 enum lp_texture_layout new_layout
;
1029 /* loop over all image tiles, doing layout conversion where needed */
1030 for (y
= 0; y
< height_t
; y
++) {
1031 for (x
= 0; x
< width_t
; x
++) {
1032 enum lp_texture_layout cur_layout
=
1033 llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, x
, y
);
1036 layout_logic(cur_layout
, layout
, usage
, &new_layout
, &convert
);
1039 if (layout
== LP_TEX_LAYOUT_TILED
) {
1040 lp_linear_to_tiled(other_data
, target_data
,
1041 x
* TILE_SIZE
, y
* TILE_SIZE
,
1042 TILE_SIZE
, TILE_SIZE
,
1044 lpr
->row_stride
[level
],
1045 lpr
->tiles_per_row
[level
]);
1048 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
1049 lp_tiled_to_linear(other_data
, target_data
,
1050 x
* TILE_SIZE
, y
* TILE_SIZE
,
1051 TILE_SIZE
, TILE_SIZE
,
1053 lpr
->row_stride
[level
],
1054 lpr
->tiles_per_row
[level
]);
1058 if (new_layout
!= cur_layout
)
1059 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, x
, y
,
1066 llvmpipe_set_texture_image_layout(lpr
, face_slice
, level
,
1067 width_t
, height_t
, layout
);
1070 assert(target_data
);
1077 * Return pointer to start of a texture image (1D, 2D, 3D, CUBE).
1078 * All cube faces and 3D slices will be converted to the requested
1080 * This is typically used when we're about to sample from a texture.
1083 llvmpipe_get_texture_image_all(struct llvmpipe_resource
*lpr
,
1085 enum lp_texture_usage usage
,
1086 enum lp_texture_layout layout
)
1088 const int slices
= lpr
->num_slices_faces
[level
];
1094 for (slice
= slices
- 1; slice
>= 0; slice
--) {
1095 map
= llvmpipe_get_texture_image(lpr
, slice
, level
, usage
, layout
);
1103 * Get pointer to a linear image (not the tile!) where the tile at (x,y)
1104 * is known to be in linear layout.
1105 * Conversion from tiled to linear will be done if necessary.
1106 * \return pointer to start of image/face (not the tile)
1109 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource
*lpr
,
1110 unsigned face_slice
, unsigned level
,
1111 enum lp_texture_usage usage
,
1112 unsigned x
, unsigned y
)
1114 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1115 enum lp_texture_layout cur_layout
, new_layout
;
1116 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1118 uint8_t *tiled_image
, *linear_image
;
1120 assert(resource_is_texture(&lpr
->base
));
1121 assert(x
% TILE_SIZE
== 0);
1122 assert(y
% TILE_SIZE
== 0);
1124 if (!linear_img
->data
) {
1125 /* allocate memory for the linear image now */
1126 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1129 /* compute address of the slice/face of the image that contains the tile */
1130 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1131 LP_TEX_LAYOUT_TILED
);
1132 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1133 LP_TEX_LAYOUT_LINEAR
);
1135 /* get current tile layout and determine if data conversion is needed */
1136 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1138 layout_logic(cur_layout
, LP_TEX_LAYOUT_LINEAR
, usage
,
1139 &new_layout
, &convert
);
1142 lp_tiled_to_linear(tiled_image
, linear_image
,
1143 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1144 lpr
->row_stride
[level
],
1145 lpr
->tiles_per_row
[level
]);
1148 if (new_layout
!= cur_layout
)
1149 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1151 return linear_image
;
1156 * Get pointer to tiled data for rendering.
1157 * \return pointer to the tiled data at the given tile position
1160 llvmpipe_get_texture_tile(struct llvmpipe_resource
*lpr
,
1161 unsigned face_slice
, unsigned level
,
1162 enum lp_texture_usage usage
,
1163 unsigned x
, unsigned y
)
1165 struct llvmpipe_texture_image
*tiled_img
= &lpr
->tiled
[level
];
1166 enum lp_texture_layout cur_layout
, new_layout
;
1167 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1169 uint8_t *tiled_image
, *linear_image
;
1170 unsigned tile_offset
;
1172 assert(x
% TILE_SIZE
== 0);
1173 assert(y
% TILE_SIZE
== 0);
1175 if (!tiled_img
->data
) {
1176 /* allocate memory for the tiled image now */
1177 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_TILED
);
1180 /* compute address of the slice/face of the image that contains the tile */
1181 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1182 LP_TEX_LAYOUT_TILED
);
1183 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1184 LP_TEX_LAYOUT_LINEAR
);
1186 /* get current tile layout and see if we need to convert the data */
1187 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1189 layout_logic(cur_layout
, LP_TEX_LAYOUT_TILED
, usage
, &new_layout
, &convert
);
1191 lp_linear_to_tiled(linear_image
, tiled_image
,
1192 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1193 lpr
->row_stride
[level
],
1194 lpr
->tiles_per_row
[level
]);
1197 if (new_layout
!= cur_layout
)
1198 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1200 /* compute, return address of the 64x64 tile */
1201 tile_offset
= (ty
* lpr
->tiles_per_row
[level
] + tx
)
1202 * TILE_SIZE
* TILE_SIZE
* 4;
1204 return (ubyte
*) tiled_image
+ tile_offset
;
1209 * Return size of resource in bytes
1212 llvmpipe_resource_size(const struct pipe_resource
*resource
)
1214 const struct llvmpipe_resource
*lpr
= llvmpipe_resource_const(resource
);
1215 unsigned lvl
, size
= 0;
1217 for (lvl
= 0; lvl
<= lpr
->base
.last_level
; lvl
++) {
1218 if (lpr
->linear
[lvl
].data
)
1219 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_LINEAR
);
1221 if (lpr
->tiled
[lvl
].data
)
1222 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_TILED
);
1231 llvmpipe_print_resources(void)
1233 struct llvmpipe_resource
*lpr
;
1234 unsigned n
= 0, total
= 0;
1236 debug_printf("LLVMPIPE: current resources:\n");
1237 foreach(lpr
, &resource_list
) {
1238 unsigned size
= llvmpipe_resource_size(&lpr
->base
);
1239 debug_printf("resource %u at %p, size %ux%ux%u: %u bytes, refcount %u\n",
1240 lpr
->id
, (void *) lpr
,
1241 lpr
->base
.width0
, lpr
->base
.height0
, lpr
->base
.depth0
,
1242 size
, lpr
->base
.reference
.count
);
1246 debug_printf("LLVMPIPE: total size of %u resources: %u\n", n
, total
);
1252 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
1255 /* init linked list for tracking resources */
1257 static boolean first_call
= TRUE
;
1259 memset(&resource_list
, 0, sizeof(resource_list
));
1260 make_empty_list(&resource_list
);
1266 screen
->resource_create
= llvmpipe_resource_create
;
1267 screen
->resource_destroy
= llvmpipe_resource_destroy
;
1268 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
1269 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
1270 screen
->user_buffer_create
= llvmpipe_user_buffer_create
;
1272 screen
->get_tex_surface
= llvmpipe_get_tex_surface
;
1273 screen
->tex_surface_destroy
= llvmpipe_tex_surface_destroy
;
1278 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
1280 pipe
->get_transfer
= llvmpipe_get_transfer
;
1281 pipe
->transfer_destroy
= llvmpipe_transfer_destroy
;
1282 pipe
->transfer_map
= llvmpipe_transfer_map
;
1283 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
1284 pipe
->is_resource_referenced
= llvmpipe_is_resource_referenced
;
1286 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
1287 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;