1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Michel Dänzer <michel@tungstengraphics.com>
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
38 #include "util/u_inlines.h"
39 #include "util/u_cpu_detect.h"
40 #include "util/u_format.h"
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43 #include "util/u_simple_list.h"
44 #include "util/u_transfer.h"
46 #include "lp_context.h"
48 #include "lp_screen.h"
49 #include "lp_tile_image.h"
50 #include "lp_texture.h"
54 #include "state_tracker/sw_winsys.h"
58 static struct llvmpipe_resource resource_list
;
60 static unsigned id_counter
= 0;
64 resource_is_texture(const struct pipe_resource
*resource
)
66 switch (resource
->target
) {
71 case PIPE_TEXTURE_RECT
:
73 case PIPE_TEXTURE_CUBE
:
84 * Allocate storage for llvmpipe_texture::layout array.
85 * The number of elements is width_in_tiles * height_in_tiles.
87 static enum lp_texture_layout
*
88 alloc_layout_array(unsigned num_slices
, unsigned width
, unsigned height
)
90 const unsigned tx
= align(width
, TILE_SIZE
) / TILE_SIZE
;
91 const unsigned ty
= align(height
, TILE_SIZE
) / TILE_SIZE
;
93 assert(num_slices
* tx
* ty
> 0);
94 assert(LP_TEX_LAYOUT_NONE
== 0); /* calloc'ing LP_TEX_LAYOUT_NONE here */
96 return (enum lp_texture_layout
*)
97 CALLOC(num_slices
* tx
* ty
, sizeof(enum lp_texture_layout
));
103 * Conventional allocation path for non-display textures:
104 * Just compute row strides here. Storage is allocated on demand later.
107 llvmpipe_texture_layout(struct llvmpipe_screen
*screen
,
108 struct llvmpipe_resource
*lpr
,
111 struct pipe_resource
*pt
= &lpr
->base
;
113 unsigned width
= pt
->width0
;
114 unsigned height
= pt
->height0
;
115 unsigned depth
= pt
->depth0
;
116 uint64_t total_size
= 0;
118 assert(LP_MAX_TEXTURE_2D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
119 assert(LP_MAX_TEXTURE_3D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
121 for (level
= 0; level
<= pt
->last_level
; level
++) {
123 /* Row stride and image stride (for linear layout) */
125 unsigned alignment
, nblocksx
, nblocksy
, block_size
;
127 /* For non-compressed formats we need to align the texture size
128 * to the tile size to facilitate render-to-texture.
130 if (util_format_is_compressed(pt
->format
))
133 alignment
= TILE_SIZE
;
135 nblocksx
= util_format_get_nblocksx(pt
->format
,
136 align(width
, alignment
));
137 nblocksy
= util_format_get_nblocksy(pt
->format
,
138 align(height
, alignment
));
139 block_size
= util_format_get_blocksize(pt
->format
);
141 lpr
->row_stride
[level
] = align(nblocksx
* block_size
, 16);
143 /* if row_stride * height > LP_MAX_TEXTURE_SIZE */
144 if (lpr
->row_stride
[level
] > LP_MAX_TEXTURE_SIZE
/ nblocksy
) {
145 /* image too large */
149 lpr
->img_stride
[level
] = lpr
->row_stride
[level
] * nblocksy
;
152 /* Size of the image in tiles (for tiled layout) */
154 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
155 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
156 lpr
->tiles_per_row
[level
] = width_t
;
157 lpr
->tiles_per_image
[level
] = width_t
* height_t
;
160 /* Number of 3D image slices or cube faces */
164 if (lpr
->base
.target
== PIPE_TEXTURE_CUBE
)
166 else if (lpr
->base
.target
== PIPE_TEXTURE_3D
)
171 lpr
->num_slices_faces
[level
] = num_slices
;
174 lpr
->layout
[level
] = alloc_layout_array(num_slices
, width
, height
);
175 if (!lpr
->layout
[level
]) {
181 /* if img_stride * num_slices_faces > LP_MAX_TEXTURE_SIZE */
182 if (lpr
->img_stride
[level
] >
183 LP_MAX_TEXTURE_SIZE
/ lpr
->num_slices_faces
[level
]) {
184 /* volume too large */
188 total_size
+= (uint64_t) lpr
->num_slices_faces
[level
]
189 * (uint64_t) lpr
->img_stride
[level
];
190 if (total_size
> LP_MAX_TEXTURE_SIZE
) {
194 /* Compute size of next mipmap level */
195 width
= u_minify(width
, 1);
196 height
= u_minify(height
, 1);
197 depth
= u_minify(depth
, 1);
203 for (level
= 0; level
<= pt
->last_level
; level
++) {
204 FREE(lpr
->layout
[level
]);
212 * Check the size of the texture specified by 'res'.
213 * \return TRUE if OK, FALSE if too large.
216 llvmpipe_can_create_resource(struct pipe_screen
*screen
,
217 const struct pipe_resource
*res
)
219 struct llvmpipe_resource lpr
;
220 memset(&lpr
, 0, sizeof(lpr
));
222 return llvmpipe_texture_layout(llvmpipe_screen(screen
), &lpr
, FALSE
);
227 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
228 struct llvmpipe_resource
*lpr
)
230 struct sw_winsys
*winsys
= screen
->winsys
;
232 /* Round up the surface size to a multiple of the tile size to
233 * avoid tile clipping.
235 const unsigned width
= align(lpr
->base
.width0
, TILE_SIZE
);
236 const unsigned height
= align(lpr
->base
.height0
, TILE_SIZE
);
237 const unsigned width_t
= width
/ TILE_SIZE
;
238 const unsigned height_t
= height
/ TILE_SIZE
;
240 lpr
->tiles_per_row
[0] = width_t
;
241 lpr
->tiles_per_image
[0] = width_t
* height_t
;
242 lpr
->num_slices_faces
[0] = 1;
243 lpr
->img_stride
[0] = 0;
245 lpr
->layout
[0] = alloc_layout_array(1, width
, height
);
246 if (!lpr
->layout
[0]) {
250 lpr
->dt
= winsys
->displaytarget_create(winsys
,
255 &lpr
->row_stride
[0] );
261 void *map
= winsys
->displaytarget_map(winsys
, lpr
->dt
,
262 PIPE_TRANSFER_WRITE
);
265 memset(map
, 0, height
* lpr
->row_stride
[0]);
267 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
274 static struct pipe_resource
*
275 llvmpipe_resource_create(struct pipe_screen
*_screen
,
276 const struct pipe_resource
*templat
)
278 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
279 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
283 lpr
->base
= *templat
;
284 pipe_reference_init(&lpr
->base
.reference
, 1);
285 lpr
->base
.screen
= &screen
->base
;
287 /* assert(lpr->base.bind); */
289 if (resource_is_texture(&lpr
->base
)) {
290 if (lpr
->base
.bind
& PIPE_BIND_DISPLAY_TARGET
) {
291 /* displayable surface */
292 if (!llvmpipe_displaytarget_layout(screen
, lpr
))
294 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
298 if (!llvmpipe_texture_layout(screen
, lpr
, TRUE
))
300 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
302 assert(lpr
->layout
[0]);
305 /* other data (vertex buffer, const buffer, etc) */
306 const enum pipe_format format
= templat
->format
;
307 const uint w
= templat
->width0
/ util_format_get_blockheight(format
);
308 /* XXX buffers should only have one dimension, those values should be 1 */
309 const uint h
= templat
->height0
/ util_format_get_blockwidth(format
);
310 const uint d
= templat
->depth0
;
311 const uint bpp
= util_format_get_blocksize(format
);
312 const uint bytes
= w
* h
* d
* bpp
;
313 lpr
->data
= align_malloc(bytes
, 16);
316 memset(lpr
->data
, 0, bytes
);
319 lpr
->id
= id_counter
++;
322 insert_at_tail(&resource_list
, lpr
);
334 llvmpipe_resource_destroy(struct pipe_screen
*pscreen
,
335 struct pipe_resource
*pt
)
337 struct llvmpipe_screen
*screen
= llvmpipe_screen(pscreen
);
338 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
342 struct sw_winsys
*winsys
= screen
->winsys
;
343 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
345 if (lpr
->tiled_img
.data
) {
346 align_free(lpr
->tiled_img
.data
);
347 lpr
->tiled_img
.data
= NULL
;
350 FREE(lpr
->layout
[0]);
352 else if (resource_is_texture(pt
)) {
353 /* regular texture */
356 /* free linear image data */
357 if (lpr
->linear_img
.data
) {
358 align_free(lpr
->linear_img
.data
);
359 lpr
->linear_img
.data
= NULL
;
362 /* free tiled image data */
363 if (lpr
->tiled_img
.data
) {
364 align_free(lpr
->tiled_img
.data
);
365 lpr
->tiled_img
.data
= NULL
;
368 /* free layout flag arrays */
369 for (level
= 0; level
< Elements(lpr
->layout
); level
++) {
370 FREE(lpr
->layout
[level
]);
371 lpr
->layout
[level
] = NULL
;
374 else if (!lpr
->userBuffer
) {
376 align_free(lpr
->data
);
381 remove_from_list(lpr
);
389 * Map a resource for read/write.
392 llvmpipe_resource_map(struct pipe_resource
*resource
,
395 enum lp_texture_usage tex_usage
,
396 enum lp_texture_layout layout
)
398 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
401 assert(level
< LP_MAX_TEXTURE_LEVELS
);
402 assert(layer
< (u_minify(resource
->depth0
, level
) + resource
->array_size
- 1));
404 assert(tex_usage
== LP_TEX_USAGE_READ
||
405 tex_usage
== LP_TEX_USAGE_READ_WRITE
||
406 tex_usage
== LP_TEX_USAGE_WRITE_ALL
);
408 assert(layout
== LP_TEX_LAYOUT_NONE
||
409 layout
== LP_TEX_LAYOUT_TILED
||
410 layout
== LP_TEX_LAYOUT_LINEAR
);
414 struct llvmpipe_screen
*screen
= llvmpipe_screen(resource
->screen
);
415 struct sw_winsys
*winsys
= screen
->winsys
;
419 if (tex_usage
== LP_TEX_USAGE_READ
) {
420 dt_usage
= PIPE_TRANSFER_READ
;
423 dt_usage
= PIPE_TRANSFER_READ_WRITE
;
429 /* FIXME: keep map count? */
430 map
= winsys
->displaytarget_map(winsys
, lpr
->dt
, dt_usage
);
432 /* install this linear image in texture data structure */
433 lpr
->linear_img
.data
= map
;
435 /* make sure tiled data gets converted to linear data */
436 map2
= llvmpipe_get_texture_image(lpr
, 0, 0, tex_usage
, layout
);
437 if (layout
== LP_TEX_LAYOUT_LINEAR
)
442 else if (resource_is_texture(resource
)) {
444 map
= llvmpipe_get_texture_image(lpr
, layer
, level
,
458 llvmpipe_resource_unmap(struct pipe_resource
*resource
,
462 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
466 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(resource
->screen
);
467 struct sw_winsys
*winsys
= lp_screen
->winsys
;
472 /* make sure linear image is up to date */
473 (void) llvmpipe_get_texture_image(lpr
, layer
, level
,
475 LP_TEX_LAYOUT_LINEAR
);
477 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
483 llvmpipe_resource_data(struct pipe_resource
*resource
)
485 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
487 assert(!resource_is_texture(resource
));
493 static struct pipe_resource
*
494 llvmpipe_resource_from_handle(struct pipe_screen
*screen
,
495 const struct pipe_resource
*template,
496 struct winsys_handle
*whandle
)
498 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
499 struct llvmpipe_resource
*lpr
;
500 unsigned width
, height
, width_t
, height_t
;
502 /* XXX Seems like from_handled depth textures doesn't work that well */
504 lpr
= CALLOC_STRUCT(llvmpipe_resource
);
509 lpr
->base
= *template;
510 pipe_reference_init(&lpr
->base
.reference
, 1);
511 lpr
->base
.screen
= screen
;
513 width
= align(lpr
->base
.width0
, TILE_SIZE
);
514 height
= align(lpr
->base
.height0
, TILE_SIZE
);
515 width_t
= width
/ TILE_SIZE
;
516 height_t
= height
/ TILE_SIZE
;
519 * Looks like unaligned displaytargets work just fine,
520 * at least sampler/render ones.
523 assert(lpr
->base
.width0
== width
);
524 assert(lpr
->base
.height0
== height
);
527 lpr
->tiles_per_row
[0] = width_t
;
528 lpr
->tiles_per_image
[0] = width_t
* height_t
;
529 lpr
->num_slices_faces
[0] = 1;
530 lpr
->img_stride
[0] = 0;
532 lpr
->dt
= winsys
->displaytarget_from_handle(winsys
,
535 &lpr
->row_stride
[0]);
540 lpr
->layout
[0] = alloc_layout_array(1, lpr
->base
.width0
, lpr
->base
.height0
);
541 if (!lpr
->layout
[0]) {
545 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
547 lpr
->id
= id_counter
++;
550 insert_at_tail(&resource_list
, lpr
);
556 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
565 llvmpipe_resource_get_handle(struct pipe_screen
*screen
,
566 struct pipe_resource
*pt
,
567 struct winsys_handle
*whandle
)
569 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
570 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
576 return winsys
->displaytarget_get_handle(winsys
, lpr
->dt
, whandle
);
580 static struct pipe_surface
*
581 llvmpipe_create_surface(struct pipe_context
*pipe
,
582 struct pipe_resource
*pt
,
583 const struct pipe_surface
*surf_tmpl
)
585 struct pipe_surface
*ps
;
587 assert(surf_tmpl
->u
.tex
.level
<= pt
->last_level
);
589 ps
= CALLOC_STRUCT(pipe_surface
);
591 pipe_reference_init(&ps
->reference
, 1);
592 pipe_resource_reference(&ps
->texture
, pt
);
594 ps
->format
= surf_tmpl
->format
;
595 ps
->width
= u_minify(pt
->width0
, surf_tmpl
->u
.tex
.level
);
596 ps
->height
= u_minify(pt
->height0
, surf_tmpl
->u
.tex
.level
);
597 ps
->usage
= surf_tmpl
->usage
;
599 ps
->u
.tex
.level
= surf_tmpl
->u
.tex
.level
;
600 ps
->u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
601 ps
->u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
608 llvmpipe_surface_destroy(struct pipe_context
*pipe
,
609 struct pipe_surface
*surf
)
611 /* Effectively do the texture_update work here - if texture images
612 * needed post-processing to put them into hardware layout, this is
613 * where it would happen. For llvmpipe, nothing to do.
615 assert(surf
->texture
);
616 pipe_resource_reference(&surf
->texture
, NULL
);
622 llvmpipe_transfer_map( struct pipe_context
*pipe
,
623 struct pipe_resource
*resource
,
626 const struct pipe_box
*box
,
627 struct pipe_transfer
**transfer
)
629 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
630 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
631 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
632 struct llvmpipe_transfer
*lpt
;
633 struct pipe_transfer
*pt
;
635 enum pipe_format format
;
636 enum lp_texture_usage tex_usage
;
640 assert(level
<= resource
->last_level
);
643 * Transfers, like other pipe operations, must happen in order, so flush the
644 * context if necessary.
646 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
647 boolean read_only
= !(usage
& PIPE_TRANSFER_WRITE
);
648 boolean do_not_block
= !!(usage
& PIPE_TRANSFER_DONTBLOCK
);
649 if (!llvmpipe_flush_resource(pipe
, resource
,
651 box
->depth
> 1 ? -1 : box
->z
,
653 TRUE
, /* cpu_access */
657 * It would have blocked, but state tracker requested no to.
659 assert(do_not_block
);
664 if (resource
== llvmpipe
->constants
[PIPE_SHADER_FRAGMENT
][0])
665 llvmpipe
->dirty
|= LP_NEW_CONSTANTS
;
667 lpt
= CALLOC_STRUCT(llvmpipe_transfer
);
671 pipe_resource_reference(&pt
->resource
, resource
);
674 pt
->stride
= lpr
->row_stride
[level
];
675 pt
->layer_stride
= lpr
->img_stride
[level
];
679 assert(level
< LP_MAX_TEXTURE_LEVELS
);
682 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
683 transfer->x, transfer->y, transfer->width, transfer->height,
684 transfer->texture->width0,
685 transfer->texture->height0,
689 if (usage
== PIPE_TRANSFER_READ
) {
690 tex_usage
= LP_TEX_USAGE_READ
;
694 tex_usage
= LP_TEX_USAGE_READ_WRITE
;
699 printf("transfer map tex %u mode %s\n", lpr
->id
, mode
);
702 format
= lpr
->base
.format
;
704 map
= llvmpipe_resource_map(resource
,
707 tex_usage
, LP_TEX_LAYOUT_LINEAR
);
710 /* May want to do different things here depending on read/write nature
713 if (usage
& PIPE_TRANSFER_WRITE
) {
714 /* Do something to notify sharing contexts of a texture change.
720 box
->y
/ util_format_get_blockheight(format
) * pt
->stride
+
721 box
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
728 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
729 struct pipe_transfer
*transfer
)
731 assert(transfer
->resource
);
733 llvmpipe_resource_unmap(transfer
->resource
,
737 /* Effectively do the texture_update work here - if texture images
738 * needed post-processing to put them into hardware layout, this is
739 * where it would happen. For llvmpipe, nothing to do.
741 assert (transfer
->resource
);
742 pipe_resource_reference(&transfer
->resource
, NULL
);
747 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
748 struct pipe_resource
*presource
,
749 unsigned level
, int layer
)
751 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
753 if (presource
->target
== PIPE_BUFFER
)
754 return LP_UNREFERENCED
;
756 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
761 * Returns the largest possible alignment for a format in llvmpipe
764 llvmpipe_get_format_alignment( enum pipe_format format
)
766 const struct util_format_description
*desc
= util_format_description(format
);
771 for (i
= 0; i
< desc
->nr_channels
; ++i
) {
772 size
+= desc
->channel
[i
].size
;
777 if (!util_is_power_of_two(bytes
)) {
778 bytes
/= desc
->nr_channels
;
781 if (bytes
% 2 || bytes
< 1) {
790 * Create buffer which wraps user-space data.
792 struct pipe_resource
*
793 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
798 struct llvmpipe_resource
*buffer
;
800 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
804 pipe_reference_init(&buffer
->base
.reference
, 1);
805 buffer
->base
.screen
= screen
;
806 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
807 buffer
->base
.bind
= bind_flags
;
808 buffer
->base
.usage
= PIPE_USAGE_IMMUTABLE
;
809 buffer
->base
.flags
= 0;
810 buffer
->base
.width0
= bytes
;
811 buffer
->base
.height0
= 1;
812 buffer
->base
.depth0
= 1;
813 buffer
->base
.array_size
= 1;
814 buffer
->userBuffer
= TRUE
;
817 return &buffer
->base
;
822 * Compute size (in bytes) need to store a texture image / mipmap level,
823 * for just one cube face or one 3D texture slice
826 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
827 enum lp_texture_layout layout
)
829 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
830 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
832 assert(layout
== LP_TEX_LAYOUT_TILED
||
833 layout
== LP_TEX_LAYOUT_LINEAR
);
835 if (layout
== LP_TEX_LAYOUT_TILED
) {
836 /* for tiled layout, force a 32bpp format */
837 const enum pipe_format format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
838 const unsigned block_size
= util_format_get_blocksize(format
);
839 const unsigned nblocksy
=
840 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
841 const unsigned nblocksx
=
842 util_format_get_nblocksx(format
, align(width
, TILE_SIZE
));
843 const unsigned buffer_size
= block_size
* nblocksy
* nblocksx
;
847 /* we already computed this */
848 return lpr
->img_stride
[level
];
854 * Compute size (in bytes) need to store a texture image / mipmap level,
855 * including all cube faces or 3D image slices
858 tex_image_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
859 enum lp_texture_layout layout
)
861 const unsigned buf_size
= tex_image_face_size(lpr
, level
, layout
);
862 return buf_size
* lpr
->num_slices_faces
[level
];
867 * This function encapsulates some complicated logic for determining
868 * how to convert a tile of image data from linear layout to tiled
869 * layout, or vice versa.
870 * \param cur_layout the current tile layout
871 * \param target_layout the desired tile layout
872 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
873 * \param new_layout_return returns the new layout mode
874 * \param convert_return returns TRUE if image conversion is needed
877 layout_logic(enum lp_texture_layout cur_layout
,
878 enum lp_texture_layout target_layout
,
879 enum lp_texture_usage usage
,
880 enum lp_texture_layout
*new_layout_return
,
883 enum lp_texture_layout other_layout
, new_layout
;
887 new_layout
= 99; /* debug check */
889 if (target_layout
== LP_TEX_LAYOUT_LINEAR
) {
890 other_layout
= LP_TEX_LAYOUT_TILED
;
893 assert(target_layout
== LP_TEX_LAYOUT_TILED
);
894 other_layout
= LP_TEX_LAYOUT_LINEAR
;
897 new_layout
= target_layout
; /* may get changed below */
899 if (cur_layout
== LP_TEX_LAYOUT_BOTH
) {
900 if (usage
== LP_TEX_USAGE_READ
) {
901 new_layout
= LP_TEX_LAYOUT_BOTH
;
904 else if (cur_layout
== other_layout
) {
905 if (usage
!= LP_TEX_USAGE_WRITE_ALL
) {
906 /* need to convert tiled data to linear or vice versa */
909 if (usage
== LP_TEX_USAGE_READ
)
910 new_layout
= LP_TEX_LAYOUT_BOTH
;
914 assert(cur_layout
== LP_TEX_LAYOUT_NONE
||
915 cur_layout
== target_layout
);
918 assert(new_layout
== LP_TEX_LAYOUT_BOTH
||
919 new_layout
== target_layout
);
921 *new_layout_return
= new_layout
;
926 * Return pointer to a 2D texture image/face/slice.
927 * No tiled/linear conversion is done.
930 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
931 unsigned face_slice
, unsigned level
,
932 enum lp_texture_layout layout
)
934 struct llvmpipe_texture_image
*img
;
937 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
938 img
= &lpr
->linear_img
;
939 offset
= lpr
->linear_mip_offsets
[level
];
942 assert (layout
== LP_TEX_LAYOUT_TILED
);
943 img
= &lpr
->tiled_img
;
944 offset
= lpr
->tiled_mip_offsets
[level
];
948 offset
+= face_slice
* tex_image_face_size(lpr
, level
, layout
);
950 return (ubyte
*) img
->data
+ offset
;
954 static INLINE
enum lp_texture_layout
955 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource
*lpr
,
956 unsigned face_slice
, unsigned level
,
957 unsigned x
, unsigned y
)
960 assert(resource_is_texture(&lpr
->base
));
961 assert(x
< lpr
->tiles_per_row
[level
]);
962 i
= face_slice
* lpr
->tiles_per_image
[level
]
963 + y
* lpr
->tiles_per_row
[level
] + x
;
964 return lpr
->layout
[level
][i
];
969 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource
*lpr
,
970 unsigned face_slice
, unsigned level
,
971 unsigned x
, unsigned y
,
972 enum lp_texture_layout layout
)
975 assert(resource_is_texture(&lpr
->base
));
976 assert(x
< lpr
->tiles_per_row
[level
]);
977 i
= face_slice
* lpr
->tiles_per_image
[level
]
978 + y
* lpr
->tiles_per_row
[level
] + x
;
979 lpr
->layout
[level
][i
] = layout
;
984 * Set the layout mode for all tiles in a particular image.
987 llvmpipe_set_texture_image_layout(struct llvmpipe_resource
*lpr
,
988 unsigned face_slice
, unsigned level
,
989 unsigned width_t
, unsigned height_t
,
990 enum lp_texture_layout layout
)
992 const unsigned start
= face_slice
* lpr
->tiles_per_image
[level
];
995 for (i
= 0; i
< width_t
* height_t
; i
++) {
996 lpr
->layout
[level
][start
+ i
] = layout
;
1002 * Allocate storage for a linear or tile texture image (all cube
1003 * faces and all 3D slices, all levels).
1006 alloc_image_data(struct llvmpipe_resource
*lpr
,
1007 enum lp_texture_layout layout
)
1009 uint alignment
= MAX2(16, util_cpu_caps
.cacheline
);
1014 assert(lpr
->base
.last_level
== 0);
1016 if (layout
== LP_TEX_LAYOUT_TILED
) {
1017 /* tiled data is stored in regular memory */
1018 for (level
= 0; level
<= lpr
->base
.last_level
; level
++) {
1019 uint buffer_size
= tex_image_size(lpr
, level
, layout
);
1020 lpr
->tiled_mip_offsets
[level
] = offset
;
1021 offset
+= align(buffer_size
, alignment
);
1023 lpr
->tiled_img
.data
= align_malloc(offset
, alignment
);
1024 if (lpr
->tiled_img
.data
) {
1025 memset(lpr
->tiled_img
.data
, 0, offset
);
1029 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
1031 /* we get the linear memory from the winsys, and it has
1032 * already been zeroed
1034 struct llvmpipe_screen
*screen
= llvmpipe_screen(lpr
->base
.screen
);
1035 struct sw_winsys
*winsys
= screen
->winsys
;
1037 lpr
->linear_img
.data
=
1038 winsys
->displaytarget_map(winsys
, lpr
->dt
,
1039 PIPE_TRANSFER_READ_WRITE
);
1042 /* not a display target - allocate regular memory */
1044 * Offset calculation for start of a specific mip/layer is always
1045 * offset = lpr->linear_mip_offsets[level] + lpr->img_stride[level] * layer
1047 for (level
= 0; level
<= lpr
->base
.last_level
; level
++) {
1048 uint buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1049 lpr
->linear_mip_offsets
[level
] = offset
;
1050 offset
+= align(buffer_size
, alignment
);
1052 lpr
->linear_img
.data
= align_malloc(offset
, alignment
);
1053 if (lpr
->linear_img
.data
) {
1054 memset(lpr
->linear_img
.data
, 0, offset
);
1063 * Return pointer to texture image data (either linear or tiled layout)
1064 * for a particular cube face or 3D texture slice.
1066 * \param face_slice the cube face or 3D slice of interest
1067 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
1068 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
1071 llvmpipe_get_texture_image(struct llvmpipe_resource
*lpr
,
1072 unsigned face_slice
, unsigned level
,
1073 enum lp_texture_usage usage
,
1074 enum lp_texture_layout layout
)
1077 * 'target' refers to the image which we're retrieving (either in
1078 * tiled or linear layout).
1079 * 'other' refers to the same image but in the other layout. (it may
1082 struct llvmpipe_texture_image
*target_img
;
1083 struct llvmpipe_texture_image
*other_img
;
1086 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
1087 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
1088 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
1089 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
1090 unsigned target_offset
, other_offset
;
1091 unsigned *target_off_ptr
, *other_off_ptr
;
1092 enum lp_texture_layout other_layout
;
1093 boolean only_allocate
;
1095 assert(layout
== LP_TEX_LAYOUT_NONE
||
1096 layout
== LP_TEX_LAYOUT_TILED
||
1097 layout
== LP_TEX_LAYOUT_LINEAR
);
1099 assert(usage
== LP_TEX_USAGE_READ
||
1100 usage
== LP_TEX_USAGE_READ_WRITE
||
1101 usage
== LP_TEX_USAGE_WRITE_ALL
);
1103 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
1104 if (layout
== LP_TEX_LAYOUT_NONE
) {
1105 only_allocate
= TRUE
;
1106 layout
= LP_TEX_LAYOUT_TILED
;
1109 only_allocate
= FALSE
;
1113 assert(lpr
->linear_img
.data
);
1116 /* which is target? which is other? */
1117 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
1118 target_img
= &lpr
->linear_img
;
1119 target_off_ptr
= lpr
->linear_mip_offsets
;
1120 other_img
= &lpr
->tiled_img
;
1121 other_off_ptr
= lpr
->tiled_mip_offsets
;
1122 other_layout
= LP_TEX_LAYOUT_TILED
;
1125 target_img
= &lpr
->tiled_img
;
1126 target_off_ptr
= lpr
->tiled_mip_offsets
;
1127 other_img
= &lpr
->linear_img
;
1128 other_off_ptr
= lpr
->linear_mip_offsets
;
1129 other_layout
= LP_TEX_LAYOUT_LINEAR
;
1132 target_data
= target_img
->data
;
1133 other_data
= other_img
->data
;
1136 /* allocate memory for the target image now */
1137 alloc_image_data(lpr
, layout
);
1138 target_data
= target_img
->data
;
1141 target_offset
= target_off_ptr
[level
];
1142 other_offset
= other_off_ptr
[level
];
1144 if (face_slice
> 0) {
1145 target_offset
+= face_slice
* tex_image_face_size(lpr
, level
, layout
);
1146 other_offset
+= face_slice
* tex_image_face_size(lpr
, level
, other_layout
);
1150 target_data
= (uint8_t *) target_data
+ target_offset
;
1153 other_data
= (uint8_t *) other_data
+ other_offset
;
1156 if (only_allocate
) {
1157 /* Just allocating tiled memory. Don't initialize it from the
1158 * linear data if it exists.
1164 /* may need to convert other data to the requested layout */
1165 enum lp_texture_layout new_layout
;
1168 /* loop over all image tiles, doing layout conversion where needed */
1169 for (y
= 0; y
< height_t
; y
++) {
1170 for (x
= 0; x
< width_t
; x
++) {
1171 enum lp_texture_layout cur_layout
=
1172 llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, x
, y
);
1175 layout_logic(cur_layout
, layout
, usage
, &new_layout
, &convert
);
1177 if (convert
&& other_data
&& target_data
) {
1178 if (layout
== LP_TEX_LAYOUT_TILED
) {
1179 lp_linear_to_tiled(other_data
, target_data
,
1180 x
* TILE_SIZE
, y
* TILE_SIZE
,
1181 TILE_SIZE
, TILE_SIZE
,
1183 lpr
->row_stride
[level
],
1184 lpr
->tiles_per_row
[level
]);
1187 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
1188 lp_tiled_to_linear(other_data
, target_data
,
1189 x
* TILE_SIZE
, y
* TILE_SIZE
,
1190 TILE_SIZE
, TILE_SIZE
,
1192 lpr
->row_stride
[level
],
1193 lpr
->tiles_per_row
[level
]);
1197 if (new_layout
!= cur_layout
)
1198 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, x
, y
,
1205 llvmpipe_set_texture_image_layout(lpr
, face_slice
, level
,
1206 width_t
, height_t
, layout
);
1214 * Return pointer to start of a texture image (1D, 2D, 3D, CUBE).
1215 * All cube faces and 3D slices will be converted to the requested
1217 * This is typically used when we're about to sample from a texture.
1220 llvmpipe_get_texture_image_all(struct llvmpipe_resource
*lpr
,
1222 enum lp_texture_usage usage
,
1223 enum lp_texture_layout layout
)
1225 const int slices
= lpr
->num_slices_faces
[level
];
1231 for (slice
= slices
- 1; slice
>= 0; slice
--) {
1232 map
= llvmpipe_get_texture_image(lpr
, slice
, level
, usage
, layout
);
1240 * Get pointer to a linear image (not the tile!) where the tile at (x,y)
1241 * is known to be in linear layout.
1242 * Conversion from tiled to linear will be done if necessary.
1243 * \return pointer to start of image/face (not the tile)
1246 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource
*lpr
,
1247 unsigned face_slice
, unsigned level
,
1248 enum lp_texture_usage usage
,
1249 unsigned x
, unsigned y
)
1251 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear_img
;
1252 enum lp_texture_layout cur_layout
, new_layout
;
1253 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1255 uint8_t *tiled_image
, *linear_image
;
1257 assert(resource_is_texture(&lpr
->base
));
1258 assert(x
% TILE_SIZE
== 0);
1259 assert(y
% TILE_SIZE
== 0);
1261 if (!linear_img
->data
) {
1262 /* allocate memory for the linear image now */
1263 alloc_image_data(lpr
, LP_TEX_LAYOUT_LINEAR
);
1266 /* compute address of the slice/face of the image that contains the tile */
1267 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1268 LP_TEX_LAYOUT_TILED
);
1269 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1270 LP_TEX_LAYOUT_LINEAR
);
1272 /* get current tile layout and determine if data conversion is needed */
1273 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1275 layout_logic(cur_layout
, LP_TEX_LAYOUT_LINEAR
, usage
,
1276 &new_layout
, &convert
);
1278 if (convert
&& tiled_image
&& linear_image
) {
1279 lp_tiled_to_linear(tiled_image
, linear_image
,
1280 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1281 lpr
->row_stride
[level
],
1282 lpr
->tiles_per_row
[level
]);
1285 if (new_layout
!= cur_layout
)
1286 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1288 return linear_image
;
1293 * Get pointer to tiled data for rendering.
1294 * \return pointer to the tiled data at the given tile position
1297 llvmpipe_get_texture_tile(struct llvmpipe_resource
*lpr
,
1298 unsigned face_slice
, unsigned level
,
1299 enum lp_texture_usage usage
,
1300 unsigned x
, unsigned y
)
1302 struct llvmpipe_texture_image
*tiled_img
= &lpr
->tiled_img
;
1303 enum lp_texture_layout cur_layout
, new_layout
;
1304 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1306 uint8_t *tiled_image
, *linear_image
;
1307 unsigned tile_offset
;
1309 assert(x
% TILE_SIZE
== 0);
1310 assert(y
% TILE_SIZE
== 0);
1312 if (!tiled_img
->data
) {
1313 /* allocate memory for the tiled image now */
1314 alloc_image_data(lpr
, LP_TEX_LAYOUT_TILED
);
1317 /* compute address of the slice/face of the image that contains the tile */
1318 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1319 LP_TEX_LAYOUT_TILED
);
1320 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1321 LP_TEX_LAYOUT_LINEAR
);
1323 /* get current tile layout and see if we need to convert the data */
1324 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1326 layout_logic(cur_layout
, LP_TEX_LAYOUT_TILED
, usage
, &new_layout
, &convert
);
1327 if (convert
&& linear_image
&& tiled_image
) {
1328 lp_linear_to_tiled(linear_image
, tiled_image
,
1329 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1330 lpr
->row_stride
[level
],
1331 lpr
->tiles_per_row
[level
]);
1337 if (new_layout
!= cur_layout
)
1338 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1340 /* compute, return address of the 64x64 tile */
1341 tile_offset
= (ty
* lpr
->tiles_per_row
[level
] + tx
)
1342 * TILE_SIZE
* TILE_SIZE
* 4;
1344 return (ubyte
*) tiled_image
+ tile_offset
;
1349 * Return size of resource in bytes
1352 llvmpipe_resource_size(const struct pipe_resource
*resource
)
1354 const struct llvmpipe_resource
*lpr
= llvmpipe_resource_const(resource
);
1355 unsigned lvl
, size
= 0;
1357 for (lvl
= 0; lvl
<= lpr
->base
.last_level
; lvl
++) {
1358 if (lpr
->linear_img
.data
)
1359 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_LINEAR
);
1361 if (lpr
->tiled_img
.data
)
1362 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_TILED
);
1371 llvmpipe_print_resources(void)
1373 struct llvmpipe_resource
*lpr
;
1374 unsigned n
= 0, total
= 0;
1376 debug_printf("LLVMPIPE: current resources:\n");
1377 foreach(lpr
, &resource_list
) {
1378 unsigned size
= llvmpipe_resource_size(&lpr
->base
);
1379 debug_printf("resource %u at %p, size %ux%ux%u: %u bytes, refcount %u\n",
1380 lpr
->id
, (void *) lpr
,
1381 lpr
->base
.width0
, lpr
->base
.height0
, lpr
->base
.depth0
,
1382 size
, lpr
->base
.reference
.count
);
1386 debug_printf("LLVMPIPE: total size of %u resources: %u\n", n
, total
);
1392 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
1395 /* init linked list for tracking resources */
1397 static boolean first_call
= TRUE
;
1399 memset(&resource_list
, 0, sizeof(resource_list
));
1400 make_empty_list(&resource_list
);
1406 screen
->resource_create
= llvmpipe_resource_create
;
1407 screen
->resource_destroy
= llvmpipe_resource_destroy
;
1408 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
1409 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
1410 screen
->can_create_resource
= llvmpipe_can_create_resource
;
1415 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
1417 pipe
->transfer_map
= llvmpipe_transfer_map
;
1418 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
1420 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
1421 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;
1423 pipe
->create_surface
= llvmpipe_create_surface
;
1424 pipe
->surface_destroy
= llvmpipe_surface_destroy
;