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 size_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 lpr
->img_stride
[level
] = lpr
->row_stride
[level
] * nblocksy
;
146 /* Size of the image in tiles (for tiled layout) */
148 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
149 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
150 lpr
->tiles_per_row
[level
] = width_t
;
151 lpr
->tiles_per_image
[level
] = width_t
* height_t
;
154 /* Number of 3D image slices or cube faces */
158 if (lpr
->base
.target
== PIPE_TEXTURE_CUBE
)
160 else if (lpr
->base
.target
== PIPE_TEXTURE_3D
)
165 lpr
->num_slices_faces
[level
] = num_slices
;
168 lpr
->layout
[level
] = alloc_layout_array(num_slices
, width
, height
);
169 if (!lpr
->layout
[level
]) {
175 total_size
+= lpr
->num_slices_faces
[level
] * lpr
->img_stride
[level
];
176 if (total_size
> LP_MAX_TEXTURE_SIZE
) {
180 /* Compute size of next mipmap level */
181 width
= u_minify(width
, 1);
182 height
= u_minify(height
, 1);
183 depth
= u_minify(depth
, 1);
189 for (level
= 0; level
<= pt
->last_level
; level
++) {
190 FREE(lpr
->layout
[level
]);
198 * Check the size of the texture specified by 'res'.
199 * \return TRUE if OK, FALSE if too large.
202 llvmpipe_can_create_resource(struct pipe_screen
*screen
,
203 const struct pipe_resource
*res
)
205 struct llvmpipe_resource lpr
;
206 memset(&lpr
, 0, sizeof(lpr
));
208 return llvmpipe_texture_layout(llvmpipe_screen(screen
), &lpr
, FALSE
);
213 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
214 struct llvmpipe_resource
*lpr
)
216 struct sw_winsys
*winsys
= screen
->winsys
;
218 /* Round up the surface size to a multiple of the tile size to
219 * avoid tile clipping.
221 const unsigned width
= align(lpr
->base
.width0
, TILE_SIZE
);
222 const unsigned height
= align(lpr
->base
.height0
, TILE_SIZE
);
223 const unsigned width_t
= width
/ TILE_SIZE
;
224 const unsigned height_t
= height
/ TILE_SIZE
;
226 lpr
->tiles_per_row
[0] = width_t
;
227 lpr
->tiles_per_image
[0] = width_t
* height_t
;
228 lpr
->num_slices_faces
[0] = 1;
229 lpr
->img_stride
[0] = 0;
231 lpr
->layout
[0] = alloc_layout_array(1, width
, height
);
232 if (!lpr
->layout
[0]) {
236 lpr
->dt
= winsys
->displaytarget_create(winsys
,
241 &lpr
->row_stride
[0] );
247 void *map
= winsys
->displaytarget_map(winsys
, lpr
->dt
,
248 PIPE_TRANSFER_WRITE
);
251 memset(map
, 0, height
* lpr
->row_stride
[0]);
253 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
260 static struct pipe_resource
*
261 llvmpipe_resource_create(struct pipe_screen
*_screen
,
262 const struct pipe_resource
*templat
)
264 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
265 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
269 lpr
->base
= *templat
;
270 pipe_reference_init(&lpr
->base
.reference
, 1);
271 lpr
->base
.screen
= &screen
->base
;
273 /* assert(lpr->base.bind); */
275 if (resource_is_texture(&lpr
->base
)) {
276 if (lpr
->base
.bind
& PIPE_BIND_DISPLAY_TARGET
) {
277 /* displayable surface */
278 if (!llvmpipe_displaytarget_layout(screen
, lpr
))
280 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
284 if (!llvmpipe_texture_layout(screen
, lpr
, TRUE
))
286 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
288 assert(lpr
->layout
[0]);
291 /* other data (vertex buffer, const buffer, etc) */
292 const enum pipe_format format
= templat
->format
;
293 const uint w
= templat
->width0
/ util_format_get_blockheight(format
);
294 /* XXX buffers should only have one dimension, those values should be 1 */
295 const uint h
= templat
->height0
/ util_format_get_blockwidth(format
);
296 const uint d
= templat
->depth0
;
297 const uint bpp
= util_format_get_blocksize(format
);
298 const uint bytes
= w
* h
* d
* bpp
;
299 lpr
->data
= align_malloc(bytes
, 16);
302 memset(lpr
->data
, 0, bytes
);
305 lpr
->id
= id_counter
++;
308 insert_at_tail(&resource_list
, lpr
);
320 llvmpipe_resource_destroy(struct pipe_screen
*pscreen
,
321 struct pipe_resource
*pt
)
323 struct llvmpipe_screen
*screen
= llvmpipe_screen(pscreen
);
324 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
328 struct sw_winsys
*winsys
= screen
->winsys
;
329 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
331 if (lpr
->tiled
[0].data
) {
332 align_free(lpr
->tiled
[0].data
);
333 lpr
->tiled
[0].data
= NULL
;
336 FREE(lpr
->layout
[0]);
338 else if (resource_is_texture(pt
)) {
339 /* regular texture */
342 /* free linear image data */
343 for (level
= 0; level
< Elements(lpr
->linear
); level
++) {
344 if (lpr
->linear
[level
].data
) {
345 align_free(lpr
->linear
[level
].data
);
346 lpr
->linear
[level
].data
= NULL
;
350 /* free tiled image data */
351 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
352 if (lpr
->tiled
[level
].data
) {
353 align_free(lpr
->tiled
[level
].data
);
354 lpr
->tiled
[level
].data
= NULL
;
358 /* free layout flag arrays */
359 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
360 FREE(lpr
->layout
[level
]);
361 lpr
->layout
[level
] = NULL
;
364 else if (!lpr
->userBuffer
) {
366 align_free(lpr
->data
);
371 remove_from_list(lpr
);
379 * Map a resource for read/write.
382 llvmpipe_resource_map(struct pipe_resource
*resource
,
385 enum lp_texture_usage tex_usage
,
386 enum lp_texture_layout layout
)
388 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
391 assert(level
< LP_MAX_TEXTURE_LEVELS
);
392 assert(layer
< (u_minify(resource
->depth0
, level
) + resource
->array_size
- 1));
394 assert(tex_usage
== LP_TEX_USAGE_READ
||
395 tex_usage
== LP_TEX_USAGE_READ_WRITE
||
396 tex_usage
== LP_TEX_USAGE_WRITE_ALL
);
398 assert(layout
== LP_TEX_LAYOUT_NONE
||
399 layout
== LP_TEX_LAYOUT_TILED
||
400 layout
== LP_TEX_LAYOUT_LINEAR
);
404 struct llvmpipe_screen
*screen
= llvmpipe_screen(resource
->screen
);
405 struct sw_winsys
*winsys
= screen
->winsys
;
409 if (tex_usage
== LP_TEX_USAGE_READ
) {
410 dt_usage
= PIPE_TRANSFER_READ
;
413 dt_usage
= PIPE_TRANSFER_READ_WRITE
;
419 /* FIXME: keep map count? */
420 map
= winsys
->displaytarget_map(winsys
, lpr
->dt
, dt_usage
);
422 /* install this linear image in texture data structure */
423 lpr
->linear
[level
].data
= map
;
425 /* make sure tiled data gets converted to linear data */
426 map2
= llvmpipe_get_texture_image(lpr
, 0, 0, tex_usage
, layout
);
427 if (layout
== LP_TEX_LAYOUT_LINEAR
)
432 else if (resource_is_texture(resource
)) {
434 map
= llvmpipe_get_texture_image(lpr
, layer
, level
,
448 llvmpipe_resource_unmap(struct pipe_resource
*resource
,
452 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
456 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(resource
->screen
);
457 struct sw_winsys
*winsys
= lp_screen
->winsys
;
462 /* make sure linear image is up to date */
463 (void) llvmpipe_get_texture_image(lpr
, layer
, level
,
465 LP_TEX_LAYOUT_LINEAR
);
467 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
473 llvmpipe_resource_data(struct pipe_resource
*resource
)
475 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
477 assert(!resource_is_texture(resource
));
483 static struct pipe_resource
*
484 llvmpipe_resource_from_handle(struct pipe_screen
*screen
,
485 const struct pipe_resource
*template,
486 struct winsys_handle
*whandle
)
488 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
489 struct llvmpipe_resource
*lpr
;
490 unsigned width
, height
, width_t
, height_t
;
492 /* XXX Seems like from_handled depth textures doesn't work that well */
494 lpr
= CALLOC_STRUCT(llvmpipe_resource
);
499 lpr
->base
= *template;
500 pipe_reference_init(&lpr
->base
.reference
, 1);
501 lpr
->base
.screen
= screen
;
503 width
= align(lpr
->base
.width0
, TILE_SIZE
);
504 height
= align(lpr
->base
.height0
, TILE_SIZE
);
505 width_t
= width
/ TILE_SIZE
;
506 height_t
= height
/ TILE_SIZE
;
509 * Looks like unaligned displaytargets work just fine,
510 * at least sampler/render ones.
513 assert(lpr
->base
.width0
== width
);
514 assert(lpr
->base
.height0
== height
);
517 lpr
->tiles_per_row
[0] = width_t
;
518 lpr
->tiles_per_image
[0] = width_t
* height_t
;
519 lpr
->num_slices_faces
[0] = 1;
520 lpr
->img_stride
[0] = 0;
522 lpr
->dt
= winsys
->displaytarget_from_handle(winsys
,
525 &lpr
->row_stride
[0]);
530 lpr
->layout
[0] = alloc_layout_array(1, lpr
->base
.width0
, lpr
->base
.height0
);
531 if (!lpr
->layout
[0]) {
535 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
537 lpr
->id
= id_counter
++;
540 insert_at_tail(&resource_list
, lpr
);
546 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
555 llvmpipe_resource_get_handle(struct pipe_screen
*screen
,
556 struct pipe_resource
*pt
,
557 struct winsys_handle
*whandle
)
559 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
560 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
566 return winsys
->displaytarget_get_handle(winsys
, lpr
->dt
, whandle
);
570 static struct pipe_surface
*
571 llvmpipe_create_surface(struct pipe_context
*pipe
,
572 struct pipe_resource
*pt
,
573 const struct pipe_surface
*surf_tmpl
)
575 struct pipe_surface
*ps
;
577 assert(surf_tmpl
->u
.tex
.level
<= pt
->last_level
);
579 ps
= CALLOC_STRUCT(pipe_surface
);
581 pipe_reference_init(&ps
->reference
, 1);
582 pipe_resource_reference(&ps
->texture
, pt
);
584 ps
->format
= surf_tmpl
->format
;
585 ps
->width
= u_minify(pt
->width0
, surf_tmpl
->u
.tex
.level
);
586 ps
->height
= u_minify(pt
->height0
, surf_tmpl
->u
.tex
.level
);
587 ps
->usage
= surf_tmpl
->usage
;
589 ps
->u
.tex
.level
= surf_tmpl
->u
.tex
.level
;
590 ps
->u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
591 ps
->u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
598 llvmpipe_surface_destroy(struct pipe_context
*pipe
,
599 struct pipe_surface
*surf
)
601 /* Effectively do the texture_update work here - if texture images
602 * needed post-processing to put them into hardware layout, this is
603 * where it would happen. For llvmpipe, nothing to do.
605 assert(surf
->texture
);
606 pipe_resource_reference(&surf
->texture
, NULL
);
611 static struct pipe_transfer
*
612 llvmpipe_get_transfer(struct pipe_context
*pipe
,
613 struct pipe_resource
*resource
,
616 const struct pipe_box
*box
)
618 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
619 struct llvmpipe_resource
*lprex
= llvmpipe_resource(resource
);
620 struct llvmpipe_transfer
*lpr
;
623 assert(level
<= resource
->last_level
);
626 * Transfers, like other pipe operations, must happen in order, so flush the
627 * context if necessary.
629 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
630 boolean read_only
= !(usage
& PIPE_TRANSFER_WRITE
);
631 boolean do_not_block
= !!(usage
& PIPE_TRANSFER_DONTBLOCK
);
632 if (!llvmpipe_flush_resource(pipe
, resource
,
634 box
->depth
> 1 ? -1 : box
->z
,
636 TRUE
, /* cpu_access */
640 * It would have blocked, but state tracker requested no to.
642 assert(do_not_block
);
647 if (resource
== llvmpipe
->constants
[PIPE_SHADER_FRAGMENT
][0])
648 llvmpipe
->dirty
|= LP_NEW_CONSTANTS
;
650 lpr
= CALLOC_STRUCT(llvmpipe_transfer
);
652 struct pipe_transfer
*pt
= &lpr
->base
;
653 pipe_resource_reference(&pt
->resource
, resource
);
656 pt
->stride
= lprex
->row_stride
[level
];
657 pt
->layer_stride
= lprex
->img_stride
[level
];
667 llvmpipe_transfer_destroy(struct pipe_context
*pipe
,
668 struct pipe_transfer
*transfer
)
670 /* Effectively do the texture_update work here - if texture images
671 * needed post-processing to put them into hardware layout, this is
672 * where it would happen. For llvmpipe, nothing to do.
674 assert (transfer
->resource
);
675 pipe_resource_reference(&transfer
->resource
, NULL
);
681 llvmpipe_transfer_map( struct pipe_context
*pipe
,
682 struct pipe_transfer
*transfer
)
684 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
686 struct llvmpipe_resource
*lpr
;
687 enum pipe_format format
;
688 enum lp_texture_usage tex_usage
;
691 assert(transfer
->level
< LP_MAX_TEXTURE_LEVELS
);
694 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
695 transfer->x, transfer->y, transfer->width, transfer->height,
696 transfer->texture->width0,
697 transfer->texture->height0,
701 if (transfer
->usage
== PIPE_TRANSFER_READ
) {
702 tex_usage
= LP_TEX_USAGE_READ
;
706 tex_usage
= LP_TEX_USAGE_READ_WRITE
;
711 struct llvmpipe_resource
*lpr
= llvmpipe_resource(transfer
->resource
);
712 printf("transfer map tex %u mode %s\n", lpr
->id
, mode
);
716 assert(transfer
->resource
);
717 lpr
= llvmpipe_resource(transfer
->resource
);
718 format
= lpr
->base
.format
;
720 map
= llvmpipe_resource_map(transfer
->resource
,
723 tex_usage
, LP_TEX_LAYOUT_LINEAR
);
726 /* May want to do different things here depending on read/write nature
729 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
730 /* Do something to notify sharing contexts of a texture change.
736 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
737 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
744 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
745 struct pipe_transfer
*transfer
)
747 assert(transfer
->resource
);
749 llvmpipe_resource_unmap(transfer
->resource
,
755 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
756 struct pipe_resource
*presource
,
757 unsigned level
, int layer
)
759 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
761 if (presource
->target
== PIPE_BUFFER
)
762 return LP_UNREFERENCED
;
764 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
770 * Create buffer which wraps user-space data.
772 struct pipe_resource
*
773 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
778 struct llvmpipe_resource
*buffer
;
780 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
784 pipe_reference_init(&buffer
->base
.reference
, 1);
785 buffer
->base
.screen
= screen
;
786 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
787 buffer
->base
.bind
= bind_flags
;
788 buffer
->base
.usage
= PIPE_USAGE_IMMUTABLE
;
789 buffer
->base
.flags
= 0;
790 buffer
->base
.width0
= bytes
;
791 buffer
->base
.height0
= 1;
792 buffer
->base
.depth0
= 1;
793 buffer
->base
.array_size
= 1;
794 buffer
->userBuffer
= TRUE
;
797 return &buffer
->base
;
802 * Compute size (in bytes) need to store a texture image / mipmap level,
803 * for just one cube face or one 3D texture slice
806 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
807 enum lp_texture_layout layout
)
809 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
810 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
812 assert(layout
== LP_TEX_LAYOUT_TILED
||
813 layout
== LP_TEX_LAYOUT_LINEAR
);
815 if (layout
== LP_TEX_LAYOUT_TILED
) {
816 /* for tiled layout, force a 32bpp format */
817 const enum pipe_format format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
818 const unsigned block_size
= util_format_get_blocksize(format
);
819 const unsigned nblocksy
=
820 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
821 const unsigned nblocksx
=
822 util_format_get_nblocksx(format
, align(width
, TILE_SIZE
));
823 const unsigned buffer_size
= block_size
* nblocksy
* nblocksx
;
827 /* we already computed this */
828 return lpr
->img_stride
[level
];
834 * Compute size (in bytes) need to store a texture image / mipmap level,
835 * including all cube faces or 3D image slices
838 tex_image_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
839 enum lp_texture_layout layout
)
841 const unsigned buf_size
= tex_image_face_size(lpr
, level
, layout
);
842 return buf_size
* lpr
->num_slices_faces
[level
];
847 * This function encapsulates some complicated logic for determining
848 * how to convert a tile of image data from linear layout to tiled
849 * layout, or vice versa.
850 * \param cur_layout the current tile layout
851 * \param target_layout the desired tile layout
852 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
853 * \param new_layout_return returns the new layout mode
854 * \param convert_return returns TRUE if image conversion is needed
857 layout_logic(enum lp_texture_layout cur_layout
,
858 enum lp_texture_layout target_layout
,
859 enum lp_texture_usage usage
,
860 enum lp_texture_layout
*new_layout_return
,
863 enum lp_texture_layout other_layout
, new_layout
;
867 new_layout
= 99; /* debug check */
869 if (target_layout
== LP_TEX_LAYOUT_LINEAR
) {
870 other_layout
= LP_TEX_LAYOUT_TILED
;
873 assert(target_layout
== LP_TEX_LAYOUT_TILED
);
874 other_layout
= LP_TEX_LAYOUT_LINEAR
;
877 new_layout
= target_layout
; /* may get changed below */
879 if (cur_layout
== LP_TEX_LAYOUT_BOTH
) {
880 if (usage
== LP_TEX_USAGE_READ
) {
881 new_layout
= LP_TEX_LAYOUT_BOTH
;
884 else if (cur_layout
== other_layout
) {
885 if (usage
!= LP_TEX_USAGE_WRITE_ALL
) {
886 /* need to convert tiled data to linear or vice versa */
889 if (usage
== LP_TEX_USAGE_READ
)
890 new_layout
= LP_TEX_LAYOUT_BOTH
;
894 assert(cur_layout
== LP_TEX_LAYOUT_NONE
||
895 cur_layout
== target_layout
);
898 assert(new_layout
== LP_TEX_LAYOUT_BOTH
||
899 new_layout
== target_layout
);
901 *new_layout_return
= new_layout
;
906 * Return pointer to a 2D texture image/face/slice.
907 * No tiled/linear conversion is done.
910 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
911 unsigned face_slice
, unsigned level
,
912 enum lp_texture_layout layout
)
914 struct llvmpipe_texture_image
*img
;
917 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
918 img
= &lpr
->linear
[level
];
921 assert (layout
== LP_TEX_LAYOUT_TILED
);
922 img
= &lpr
->tiled
[level
];
926 offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
930 return (ubyte
*) img
->data
+ offset
;
934 static INLINE
enum lp_texture_layout
935 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource
*lpr
,
936 unsigned face_slice
, unsigned level
,
937 unsigned x
, unsigned y
)
940 assert(resource_is_texture(&lpr
->base
));
941 assert(x
< lpr
->tiles_per_row
[level
]);
942 i
= face_slice
* lpr
->tiles_per_image
[level
]
943 + y
* lpr
->tiles_per_row
[level
] + x
;
944 return lpr
->layout
[level
][i
];
949 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource
*lpr
,
950 unsigned face_slice
, unsigned level
,
951 unsigned x
, unsigned y
,
952 enum lp_texture_layout layout
)
955 assert(resource_is_texture(&lpr
->base
));
956 assert(x
< lpr
->tiles_per_row
[level
]);
957 i
= face_slice
* lpr
->tiles_per_image
[level
]
958 + y
* lpr
->tiles_per_row
[level
] + x
;
959 lpr
->layout
[level
][i
] = layout
;
964 * Set the layout mode for all tiles in a particular image.
967 llvmpipe_set_texture_image_layout(struct llvmpipe_resource
*lpr
,
968 unsigned face_slice
, unsigned level
,
969 unsigned width_t
, unsigned height_t
,
970 enum lp_texture_layout layout
)
972 const unsigned start
= face_slice
* lpr
->tiles_per_image
[level
];
975 for (i
= 0; i
< width_t
* height_t
; i
++) {
976 lpr
->layout
[level
][start
+ i
] = layout
;
982 * Allocate storage for a linear or tile texture image (all cube
983 * faces and all 3D slices.
986 alloc_image_data(struct llvmpipe_resource
*lpr
, unsigned level
,
987 enum lp_texture_layout layout
)
989 uint alignment
= MAX2(16, util_cpu_caps
.cacheline
);
994 if (layout
== LP_TEX_LAYOUT_TILED
) {
995 /* tiled data is stored in regular memory */
996 uint buffer_size
= tex_image_size(lpr
, level
, layout
);
997 lpr
->tiled
[level
].data
= align_malloc(buffer_size
, alignment
);
998 if (lpr
->tiled
[level
].data
) {
999 memset(lpr
->tiled
[level
].data
, 0, buffer_size
);
1003 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
1005 /* we get the linear memory from the winsys, and it has
1006 * already been zeroed
1008 struct llvmpipe_screen
*screen
= llvmpipe_screen(lpr
->base
.screen
);
1009 struct sw_winsys
*winsys
= screen
->winsys
;
1011 lpr
->linear
[0].data
=
1012 winsys
->displaytarget_map(winsys
, lpr
->dt
,
1013 PIPE_TRANSFER_READ_WRITE
);
1016 /* not a display target - allocate regular memory */
1017 uint buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1018 lpr
->linear
[level
].data
= align_malloc(buffer_size
, alignment
);
1019 if (lpr
->linear
[level
].data
) {
1020 memset(lpr
->linear
[level
].data
, 0, buffer_size
);
1029 * Return pointer to texture image data (either linear or tiled layout)
1030 * for a particular cube face or 3D texture slice.
1032 * \param face_slice the cube face or 3D slice of interest
1033 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
1034 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
1037 llvmpipe_get_texture_image(struct llvmpipe_resource
*lpr
,
1038 unsigned face_slice
, unsigned level
,
1039 enum lp_texture_usage usage
,
1040 enum lp_texture_layout layout
)
1043 * 'target' refers to the image which we're retrieving (either in
1044 * tiled or linear layout).
1045 * 'other' refers to the same image but in the other layout. (it may
1048 struct llvmpipe_texture_image
*target_img
;
1049 struct llvmpipe_texture_image
*other_img
;
1052 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
1053 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
1054 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
1055 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
1056 enum lp_texture_layout other_layout
;
1057 boolean only_allocate
;
1059 assert(layout
== LP_TEX_LAYOUT_NONE
||
1060 layout
== LP_TEX_LAYOUT_TILED
||
1061 layout
== LP_TEX_LAYOUT_LINEAR
);
1063 assert(usage
== LP_TEX_USAGE_READ
||
1064 usage
== LP_TEX_USAGE_READ_WRITE
||
1065 usage
== LP_TEX_USAGE_WRITE_ALL
);
1067 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
1068 if (layout
== LP_TEX_LAYOUT_NONE
) {
1069 only_allocate
= TRUE
;
1070 layout
= LP_TEX_LAYOUT_TILED
;
1073 only_allocate
= FALSE
;
1077 assert(lpr
->linear
[level
].data
);
1080 /* which is target? which is other? */
1081 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
1082 target_img
= &lpr
->linear
[level
];
1083 other_img
= &lpr
->tiled
[level
];
1084 other_layout
= LP_TEX_LAYOUT_TILED
;
1087 target_img
= &lpr
->tiled
[level
];
1088 other_img
= &lpr
->linear
[level
];
1089 other_layout
= LP_TEX_LAYOUT_LINEAR
;
1092 target_data
= target_img
->data
;
1093 other_data
= other_img
->data
;
1096 /* allocate memory for the target image now */
1097 alloc_image_data(lpr
, level
, layout
);
1098 target_data
= target_img
->data
;
1101 if (face_slice
> 0) {
1102 unsigned target_offset
, other_offset
;
1104 target_offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
1105 other_offset
= face_slice
* tex_image_face_size(lpr
, level
, other_layout
);
1107 target_data
= (uint8_t *) target_data
+ target_offset
;
1110 other_data
= (uint8_t *) other_data
+ other_offset
;
1114 if (only_allocate
) {
1115 /* Just allocating tiled memory. Don't initialize it from the
1116 * linear data if it exists.
1122 /* may need to convert other data to the requested layout */
1123 enum lp_texture_layout new_layout
;
1126 /* loop over all image tiles, doing layout conversion where needed */
1127 for (y
= 0; y
< height_t
; y
++) {
1128 for (x
= 0; x
< width_t
; x
++) {
1129 enum lp_texture_layout cur_layout
=
1130 llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, x
, y
);
1133 layout_logic(cur_layout
, layout
, usage
, &new_layout
, &convert
);
1135 if (convert
&& other_data
&& target_data
) {
1136 if (layout
== LP_TEX_LAYOUT_TILED
) {
1137 lp_linear_to_tiled(other_data
, target_data
,
1138 x
* TILE_SIZE
, y
* TILE_SIZE
,
1139 TILE_SIZE
, TILE_SIZE
,
1141 lpr
->row_stride
[level
],
1142 lpr
->tiles_per_row
[level
]);
1145 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
1146 lp_tiled_to_linear(other_data
, target_data
,
1147 x
* TILE_SIZE
, y
* TILE_SIZE
,
1148 TILE_SIZE
, TILE_SIZE
,
1150 lpr
->row_stride
[level
],
1151 lpr
->tiles_per_row
[level
]);
1155 if (new_layout
!= cur_layout
)
1156 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, x
, y
,
1163 llvmpipe_set_texture_image_layout(lpr
, face_slice
, level
,
1164 width_t
, height_t
, layout
);
1172 * Return pointer to start of a texture image (1D, 2D, 3D, CUBE).
1173 * All cube faces and 3D slices will be converted to the requested
1175 * This is typically used when we're about to sample from a texture.
1178 llvmpipe_get_texture_image_all(struct llvmpipe_resource
*lpr
,
1180 enum lp_texture_usage usage
,
1181 enum lp_texture_layout layout
)
1183 const int slices
= lpr
->num_slices_faces
[level
];
1189 for (slice
= slices
- 1; slice
>= 0; slice
--) {
1190 map
= llvmpipe_get_texture_image(lpr
, slice
, level
, usage
, layout
);
1198 * Get pointer to a linear image (not the tile!) where the tile at (x,y)
1199 * is known to be in linear layout.
1200 * Conversion from tiled to linear will be done if necessary.
1201 * \return pointer to start of image/face (not the tile)
1204 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource
*lpr
,
1205 unsigned face_slice
, unsigned level
,
1206 enum lp_texture_usage usage
,
1207 unsigned x
, unsigned y
)
1209 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1210 enum lp_texture_layout cur_layout
, new_layout
;
1211 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1213 uint8_t *tiled_image
, *linear_image
;
1215 assert(resource_is_texture(&lpr
->base
));
1216 assert(x
% TILE_SIZE
== 0);
1217 assert(y
% TILE_SIZE
== 0);
1219 if (!linear_img
->data
) {
1220 /* allocate memory for the linear image now */
1221 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1224 /* compute address of the slice/face of the image that contains the tile */
1225 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1226 LP_TEX_LAYOUT_TILED
);
1227 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1228 LP_TEX_LAYOUT_LINEAR
);
1230 /* get current tile layout and determine if data conversion is needed */
1231 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1233 layout_logic(cur_layout
, LP_TEX_LAYOUT_LINEAR
, usage
,
1234 &new_layout
, &convert
);
1236 if (convert
&& tiled_image
&& linear_image
) {
1237 lp_tiled_to_linear(tiled_image
, linear_image
,
1238 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1239 lpr
->row_stride
[level
],
1240 lpr
->tiles_per_row
[level
]);
1243 if (new_layout
!= cur_layout
)
1244 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1246 return linear_image
;
1251 * Get pointer to tiled data for rendering.
1252 * \return pointer to the tiled data at the given tile position
1255 llvmpipe_get_texture_tile(struct llvmpipe_resource
*lpr
,
1256 unsigned face_slice
, unsigned level
,
1257 enum lp_texture_usage usage
,
1258 unsigned x
, unsigned y
)
1260 struct llvmpipe_texture_image
*tiled_img
= &lpr
->tiled
[level
];
1261 enum lp_texture_layout cur_layout
, new_layout
;
1262 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1264 uint8_t *tiled_image
, *linear_image
;
1265 unsigned tile_offset
;
1267 assert(x
% TILE_SIZE
== 0);
1268 assert(y
% TILE_SIZE
== 0);
1270 if (!tiled_img
->data
) {
1271 /* allocate memory for the tiled image now */
1272 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_TILED
);
1275 /* compute address of the slice/face of the image that contains the tile */
1276 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1277 LP_TEX_LAYOUT_TILED
);
1278 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1279 LP_TEX_LAYOUT_LINEAR
);
1281 /* get current tile layout and see if we need to convert the data */
1282 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1284 layout_logic(cur_layout
, LP_TEX_LAYOUT_TILED
, usage
, &new_layout
, &convert
);
1285 if (convert
&& linear_image
&& tiled_image
) {
1286 lp_linear_to_tiled(linear_image
, tiled_image
,
1287 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1288 lpr
->row_stride
[level
],
1289 lpr
->tiles_per_row
[level
]);
1295 if (new_layout
!= cur_layout
)
1296 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1298 /* compute, return address of the 64x64 tile */
1299 tile_offset
= (ty
* lpr
->tiles_per_row
[level
] + tx
)
1300 * TILE_SIZE
* TILE_SIZE
* 4;
1302 return (ubyte
*) tiled_image
+ tile_offset
;
1307 * Get pointer to tiled data for rendering.
1308 * \return pointer to the tiled data at the given tile position
1311 llvmpipe_unswizzle_cbuf_tile(struct llvmpipe_resource
*lpr
,
1312 unsigned face_slice
, unsigned level
,
1313 unsigned x
, unsigned y
,
1316 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1317 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1318 uint8_t *linear_image
;
1320 assert(x
% TILE_SIZE
== 0);
1321 assert(y
% TILE_SIZE
== 0);
1323 if (!linear_img
->data
) {
1324 /* allocate memory for the linear image now */
1325 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1328 /* compute address of the slice/face of the image that contains the tile */
1329 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1330 LP_TEX_LAYOUT_LINEAR
);
1333 uint ii
= x
, jj
= y
;
1334 uint tile_offset
= jj
/ TILE_SIZE
+ ii
/ TILE_SIZE
;
1335 uint byte_offset
= tile_offset
* TILE_SIZE
* TILE_SIZE
* 4;
1337 /* Note that lp_tiled_to_linear expects the tile parameter to
1338 * point at the first tile in a whole-image sized array. In
1339 * this code, we have only a single tile and have to do some
1340 * pointer arithmetic to figure out where the "image" would have
1343 lp_tiled_to_linear(tile
- byte_offset
, linear_image
,
1344 x
, y
, TILE_SIZE
, TILE_SIZE
,
1346 lpr
->row_stride
[level
],
1347 1); /* tiles per row */
1350 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
,
1351 LP_TEX_LAYOUT_LINEAR
);
1356 * Get pointer to tiled data for rendering.
1357 * \return pointer to the tiled data at the given tile position
1360 llvmpipe_swizzle_cbuf_tile(struct llvmpipe_resource
*lpr
,
1361 unsigned face_slice
, unsigned level
,
1362 unsigned x
, unsigned y
,
1365 uint8_t *linear_image
;
1367 assert(x
% TILE_SIZE
== 0);
1368 assert(y
% TILE_SIZE
== 0);
1370 /* compute address of the slice/face of the image that contains the tile */
1371 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1372 LP_TEX_LAYOUT_LINEAR
);
1375 uint ii
= x
, jj
= y
;
1376 uint tile_offset
= jj
/ TILE_SIZE
+ ii
/ TILE_SIZE
;
1377 uint byte_offset
= tile_offset
* TILE_SIZE
* TILE_SIZE
* 4;
1379 /* Note that lp_linear_to_tiled expects the tile parameter to
1380 * point at the first tile in a whole-image sized array. In
1381 * this code, we have only a single tile and have to do some
1382 * pointer arithmetic to figure out where the "image" would have
1385 lp_linear_to_tiled(linear_image
, tile
- byte_offset
,
1386 x
, y
, TILE_SIZE
, TILE_SIZE
,
1388 lpr
->row_stride
[level
],
1389 1); /* tiles per row */
1395 * Return size of resource in bytes
1398 llvmpipe_resource_size(const struct pipe_resource
*resource
)
1400 const struct llvmpipe_resource
*lpr
= llvmpipe_resource_const(resource
);
1401 unsigned lvl
, size
= 0;
1403 for (lvl
= 0; lvl
<= lpr
->base
.last_level
; lvl
++) {
1404 if (lpr
->linear
[lvl
].data
)
1405 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_LINEAR
);
1407 if (lpr
->tiled
[lvl
].data
)
1408 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_TILED
);
1417 llvmpipe_print_resources(void)
1419 struct llvmpipe_resource
*lpr
;
1420 unsigned n
= 0, total
= 0;
1422 debug_printf("LLVMPIPE: current resources:\n");
1423 foreach(lpr
, &resource_list
) {
1424 unsigned size
= llvmpipe_resource_size(&lpr
->base
);
1425 debug_printf("resource %u at %p, size %ux%ux%u: %u bytes, refcount %u\n",
1426 lpr
->id
, (void *) lpr
,
1427 lpr
->base
.width0
, lpr
->base
.height0
, lpr
->base
.depth0
,
1428 size
, lpr
->base
.reference
.count
);
1432 debug_printf("LLVMPIPE: total size of %u resources: %u\n", n
, total
);
1438 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
1441 /* init linked list for tracking resources */
1443 static boolean first_call
= TRUE
;
1445 memset(&resource_list
, 0, sizeof(resource_list
));
1446 make_empty_list(&resource_list
);
1452 screen
->resource_create
= llvmpipe_resource_create
;
1453 screen
->resource_destroy
= llvmpipe_resource_destroy
;
1454 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
1455 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
1456 screen
->can_create_resource
= llvmpipe_can_create_resource
;
1461 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
1463 pipe
->get_transfer
= llvmpipe_get_transfer
;
1464 pipe
->transfer_destroy
= llvmpipe_transfer_destroy
;
1465 pipe
->transfer_map
= llvmpipe_transfer_map
;
1466 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
1468 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
1469 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;
1471 pipe
->create_surface
= llvmpipe_create_surface
;
1472 pipe
->surface_destroy
= llvmpipe_surface_destroy
;