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
);
762 * Create buffer which wraps user-space data.
764 struct pipe_resource
*
765 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
770 struct llvmpipe_resource
*buffer
;
772 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
776 pipe_reference_init(&buffer
->base
.reference
, 1);
777 buffer
->base
.screen
= screen
;
778 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
779 buffer
->base
.bind
= bind_flags
;
780 buffer
->base
.usage
= PIPE_USAGE_IMMUTABLE
;
781 buffer
->base
.flags
= 0;
782 buffer
->base
.width0
= bytes
;
783 buffer
->base
.height0
= 1;
784 buffer
->base
.depth0
= 1;
785 buffer
->base
.array_size
= 1;
786 buffer
->userBuffer
= TRUE
;
789 return &buffer
->base
;
794 * Compute size (in bytes) need to store a texture image / mipmap level,
795 * for just one cube face or one 3D texture slice
798 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
799 enum lp_texture_layout layout
)
801 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
802 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
804 assert(layout
== LP_TEX_LAYOUT_TILED
||
805 layout
== LP_TEX_LAYOUT_LINEAR
);
807 if (layout
== LP_TEX_LAYOUT_TILED
) {
808 /* for tiled layout, force a 32bpp format */
809 const enum pipe_format format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
810 const unsigned block_size
= util_format_get_blocksize(format
);
811 const unsigned nblocksy
=
812 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
813 const unsigned nblocksx
=
814 util_format_get_nblocksx(format
, align(width
, TILE_SIZE
));
815 const unsigned buffer_size
= block_size
* nblocksy
* nblocksx
;
819 /* we already computed this */
820 return lpr
->img_stride
[level
];
826 * Compute size (in bytes) need to store a texture image / mipmap level,
827 * including all cube faces or 3D image slices
830 tex_image_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
831 enum lp_texture_layout layout
)
833 const unsigned buf_size
= tex_image_face_size(lpr
, level
, layout
);
834 return buf_size
* lpr
->num_slices_faces
[level
];
839 * This function encapsulates some complicated logic for determining
840 * how to convert a tile of image data from linear layout to tiled
841 * layout, or vice versa.
842 * \param cur_layout the current tile layout
843 * \param target_layout the desired tile layout
844 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
845 * \param new_layout_return returns the new layout mode
846 * \param convert_return returns TRUE if image conversion is needed
849 layout_logic(enum lp_texture_layout cur_layout
,
850 enum lp_texture_layout target_layout
,
851 enum lp_texture_usage usage
,
852 enum lp_texture_layout
*new_layout_return
,
855 enum lp_texture_layout other_layout
, new_layout
;
859 new_layout
= 99; /* debug check */
861 if (target_layout
== LP_TEX_LAYOUT_LINEAR
) {
862 other_layout
= LP_TEX_LAYOUT_TILED
;
865 assert(target_layout
== LP_TEX_LAYOUT_TILED
);
866 other_layout
= LP_TEX_LAYOUT_LINEAR
;
869 new_layout
= target_layout
; /* may get changed below */
871 if (cur_layout
== LP_TEX_LAYOUT_BOTH
) {
872 if (usage
== LP_TEX_USAGE_READ
) {
873 new_layout
= LP_TEX_LAYOUT_BOTH
;
876 else if (cur_layout
== other_layout
) {
877 if (usage
!= LP_TEX_USAGE_WRITE_ALL
) {
878 /* need to convert tiled data to linear or vice versa */
881 if (usage
== LP_TEX_USAGE_READ
)
882 new_layout
= LP_TEX_LAYOUT_BOTH
;
886 assert(cur_layout
== LP_TEX_LAYOUT_NONE
||
887 cur_layout
== target_layout
);
890 assert(new_layout
== LP_TEX_LAYOUT_BOTH
||
891 new_layout
== target_layout
);
893 *new_layout_return
= new_layout
;
898 * Return pointer to a 2D texture image/face/slice.
899 * No tiled/linear conversion is done.
902 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
903 unsigned face_slice
, unsigned level
,
904 enum lp_texture_layout layout
)
906 struct llvmpipe_texture_image
*img
;
909 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
910 img
= &lpr
->linear_img
;
911 offset
= lpr
->linear_mip_offsets
[level
];
914 assert (layout
== LP_TEX_LAYOUT_TILED
);
915 img
= &lpr
->tiled_img
;
916 offset
= lpr
->tiled_mip_offsets
[level
];
920 offset
+= face_slice
* tex_image_face_size(lpr
, level
, layout
);
922 return (ubyte
*) img
->data
+ offset
;
926 static INLINE
enum lp_texture_layout
927 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource
*lpr
,
928 unsigned face_slice
, unsigned level
,
929 unsigned x
, unsigned y
)
932 assert(resource_is_texture(&lpr
->base
));
933 assert(x
< lpr
->tiles_per_row
[level
]);
934 i
= face_slice
* lpr
->tiles_per_image
[level
]
935 + y
* lpr
->tiles_per_row
[level
] + x
;
936 return lpr
->layout
[level
][i
];
941 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource
*lpr
,
942 unsigned face_slice
, unsigned level
,
943 unsigned x
, unsigned y
,
944 enum lp_texture_layout layout
)
947 assert(resource_is_texture(&lpr
->base
));
948 assert(x
< lpr
->tiles_per_row
[level
]);
949 i
= face_slice
* lpr
->tiles_per_image
[level
]
950 + y
* lpr
->tiles_per_row
[level
] + x
;
951 lpr
->layout
[level
][i
] = layout
;
956 * Set the layout mode for all tiles in a particular image.
959 llvmpipe_set_texture_image_layout(struct llvmpipe_resource
*lpr
,
960 unsigned face_slice
, unsigned level
,
961 unsigned width_t
, unsigned height_t
,
962 enum lp_texture_layout layout
)
964 const unsigned start
= face_slice
* lpr
->tiles_per_image
[level
];
967 for (i
= 0; i
< width_t
* height_t
; i
++) {
968 lpr
->layout
[level
][start
+ i
] = layout
;
974 * Allocate storage for a linear or tile texture image (all cube
975 * faces and all 3D slices, all levels).
978 alloc_image_data(struct llvmpipe_resource
*lpr
,
979 enum lp_texture_layout layout
)
981 uint alignment
= MAX2(16, util_cpu_caps
.cacheline
);
986 assert(lpr
->base
.last_level
== 0);
988 if (layout
== LP_TEX_LAYOUT_TILED
) {
989 /* tiled data is stored in regular memory */
990 for (level
= 0; level
<= lpr
->base
.last_level
; level
++) {
991 uint buffer_size
= tex_image_size(lpr
, level
, layout
);
992 lpr
->tiled_mip_offsets
[level
] = offset
;
993 offset
+= align(buffer_size
, alignment
);
995 lpr
->tiled_img
.data
= align_malloc(offset
, alignment
);
996 if (lpr
->tiled_img
.data
) {
997 memset(lpr
->tiled_img
.data
, 0, offset
);
1001 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
1003 /* we get the linear memory from the winsys, and it has
1004 * already been zeroed
1006 struct llvmpipe_screen
*screen
= llvmpipe_screen(lpr
->base
.screen
);
1007 struct sw_winsys
*winsys
= screen
->winsys
;
1009 lpr
->linear_img
.data
=
1010 winsys
->displaytarget_map(winsys
, lpr
->dt
,
1011 PIPE_TRANSFER_READ_WRITE
);
1014 /* not a display target - allocate regular memory */
1016 * Offset calculation for start of a specific mip/layer is always
1017 * offset = lpr->linear_mip_offsets[level] + lpr->img_stride[level] * layer
1019 for (level
= 0; level
<= lpr
->base
.last_level
; level
++) {
1020 uint buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1021 lpr
->linear_mip_offsets
[level
] = offset
;
1022 offset
+= align(buffer_size
, alignment
);
1024 lpr
->linear_img
.data
= align_malloc(offset
, alignment
);
1025 if (lpr
->linear_img
.data
) {
1026 memset(lpr
->linear_img
.data
, 0, offset
);
1035 * Return pointer to texture image data (either linear or tiled layout)
1036 * for a particular cube face or 3D texture slice.
1038 * \param face_slice the cube face or 3D slice of interest
1039 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
1040 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
1043 llvmpipe_get_texture_image(struct llvmpipe_resource
*lpr
,
1044 unsigned face_slice
, unsigned level
,
1045 enum lp_texture_usage usage
,
1046 enum lp_texture_layout layout
)
1049 * 'target' refers to the image which we're retrieving (either in
1050 * tiled or linear layout).
1051 * 'other' refers to the same image but in the other layout. (it may
1054 struct llvmpipe_texture_image
*target_img
;
1055 struct llvmpipe_texture_image
*other_img
;
1058 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
1059 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
1060 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
1061 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
1062 unsigned target_offset
, other_offset
;
1063 unsigned *target_off_ptr
, *other_off_ptr
;
1064 enum lp_texture_layout other_layout
;
1065 boolean only_allocate
;
1067 assert(layout
== LP_TEX_LAYOUT_NONE
||
1068 layout
== LP_TEX_LAYOUT_TILED
||
1069 layout
== LP_TEX_LAYOUT_LINEAR
);
1071 assert(usage
== LP_TEX_USAGE_READ
||
1072 usage
== LP_TEX_USAGE_READ_WRITE
||
1073 usage
== LP_TEX_USAGE_WRITE_ALL
);
1075 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
1076 if (layout
== LP_TEX_LAYOUT_NONE
) {
1077 only_allocate
= TRUE
;
1078 layout
= LP_TEX_LAYOUT_TILED
;
1081 only_allocate
= FALSE
;
1085 assert(lpr
->linear_img
.data
);
1088 /* which is target? which is other? */
1089 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
1090 target_img
= &lpr
->linear_img
;
1091 target_off_ptr
= lpr
->linear_mip_offsets
;
1092 other_img
= &lpr
->tiled_img
;
1093 other_off_ptr
= lpr
->tiled_mip_offsets
;
1094 other_layout
= LP_TEX_LAYOUT_TILED
;
1097 target_img
= &lpr
->tiled_img
;
1098 target_off_ptr
= lpr
->tiled_mip_offsets
;
1099 other_img
= &lpr
->linear_img
;
1100 other_off_ptr
= lpr
->linear_mip_offsets
;
1101 other_layout
= LP_TEX_LAYOUT_LINEAR
;
1104 target_data
= target_img
->data
;
1105 other_data
= other_img
->data
;
1108 /* allocate memory for the target image now */
1109 alloc_image_data(lpr
, layout
);
1110 target_data
= target_img
->data
;
1113 target_offset
= target_off_ptr
[level
];
1114 other_offset
= other_off_ptr
[level
];
1116 if (face_slice
> 0) {
1117 target_offset
+= face_slice
* tex_image_face_size(lpr
, level
, layout
);
1118 other_offset
+= face_slice
* tex_image_face_size(lpr
, level
, other_layout
);
1122 target_data
= (uint8_t *) target_data
+ target_offset
;
1125 other_data
= (uint8_t *) other_data
+ other_offset
;
1128 if (only_allocate
) {
1129 /* Just allocating tiled memory. Don't initialize it from the
1130 * linear data if it exists.
1136 /* may need to convert other data to the requested layout */
1137 enum lp_texture_layout new_layout
;
1140 /* loop over all image tiles, doing layout conversion where needed */
1141 for (y
= 0; y
< height_t
; y
++) {
1142 for (x
= 0; x
< width_t
; x
++) {
1143 enum lp_texture_layout cur_layout
=
1144 llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, x
, y
);
1147 layout_logic(cur_layout
, layout
, usage
, &new_layout
, &convert
);
1149 if (convert
&& other_data
&& target_data
) {
1150 if (layout
== LP_TEX_LAYOUT_TILED
) {
1151 lp_linear_to_tiled(other_data
, target_data
,
1152 x
* TILE_SIZE
, y
* TILE_SIZE
,
1153 TILE_SIZE
, TILE_SIZE
,
1155 lpr
->row_stride
[level
],
1156 lpr
->tiles_per_row
[level
]);
1159 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
1160 lp_tiled_to_linear(other_data
, target_data
,
1161 x
* TILE_SIZE
, y
* TILE_SIZE
,
1162 TILE_SIZE
, TILE_SIZE
,
1164 lpr
->row_stride
[level
],
1165 lpr
->tiles_per_row
[level
]);
1169 if (new_layout
!= cur_layout
)
1170 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, x
, y
,
1177 llvmpipe_set_texture_image_layout(lpr
, face_slice
, level
,
1178 width_t
, height_t
, layout
);
1186 * Return pointer to start of a texture image (1D, 2D, 3D, CUBE).
1187 * All cube faces and 3D slices will be converted to the requested
1189 * This is typically used when we're about to sample from a texture.
1192 llvmpipe_get_texture_image_all(struct llvmpipe_resource
*lpr
,
1194 enum lp_texture_usage usage
,
1195 enum lp_texture_layout layout
)
1197 const int slices
= lpr
->num_slices_faces
[level
];
1203 for (slice
= slices
- 1; slice
>= 0; slice
--) {
1204 map
= llvmpipe_get_texture_image(lpr
, slice
, level
, usage
, layout
);
1212 * Get pointer to a linear image (not the tile!) where the tile at (x,y)
1213 * is known to be in linear layout.
1214 * Conversion from tiled to linear will be done if necessary.
1215 * \return pointer to start of image/face (not the tile)
1218 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource
*lpr
,
1219 unsigned face_slice
, unsigned level
,
1220 enum lp_texture_usage usage
,
1221 unsigned x
, unsigned y
)
1223 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear_img
;
1224 enum lp_texture_layout cur_layout
, new_layout
;
1225 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1227 uint8_t *tiled_image
, *linear_image
;
1229 assert(resource_is_texture(&lpr
->base
));
1230 assert(x
% TILE_SIZE
== 0);
1231 assert(y
% TILE_SIZE
== 0);
1233 if (!linear_img
->data
) {
1234 /* allocate memory for the linear image now */
1235 alloc_image_data(lpr
, LP_TEX_LAYOUT_LINEAR
);
1238 /* compute address of the slice/face of the image that contains the tile */
1239 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1240 LP_TEX_LAYOUT_TILED
);
1241 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1242 LP_TEX_LAYOUT_LINEAR
);
1244 /* get current tile layout and determine if data conversion is needed */
1245 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1247 layout_logic(cur_layout
, LP_TEX_LAYOUT_LINEAR
, usage
,
1248 &new_layout
, &convert
);
1250 if (convert
&& tiled_image
&& linear_image
) {
1251 lp_tiled_to_linear(tiled_image
, linear_image
,
1252 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1253 lpr
->row_stride
[level
],
1254 lpr
->tiles_per_row
[level
]);
1257 if (new_layout
!= cur_layout
)
1258 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1260 return linear_image
;
1265 * Get pointer to tiled data for rendering.
1266 * \return pointer to the tiled data at the given tile position
1269 llvmpipe_get_texture_tile(struct llvmpipe_resource
*lpr
,
1270 unsigned face_slice
, unsigned level
,
1271 enum lp_texture_usage usage
,
1272 unsigned x
, unsigned y
)
1274 struct llvmpipe_texture_image
*tiled_img
= &lpr
->tiled_img
;
1275 enum lp_texture_layout cur_layout
, new_layout
;
1276 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1278 uint8_t *tiled_image
, *linear_image
;
1279 unsigned tile_offset
;
1281 assert(x
% TILE_SIZE
== 0);
1282 assert(y
% TILE_SIZE
== 0);
1284 if (!tiled_img
->data
) {
1285 /* allocate memory for the tiled image now */
1286 alloc_image_data(lpr
, LP_TEX_LAYOUT_TILED
);
1289 /* compute address of the slice/face of the image that contains the tile */
1290 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1291 LP_TEX_LAYOUT_TILED
);
1292 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1293 LP_TEX_LAYOUT_LINEAR
);
1295 /* get current tile layout and see if we need to convert the data */
1296 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1298 layout_logic(cur_layout
, LP_TEX_LAYOUT_TILED
, usage
, &new_layout
, &convert
);
1299 if (convert
&& linear_image
&& tiled_image
) {
1300 lp_linear_to_tiled(linear_image
, tiled_image
,
1301 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1302 lpr
->row_stride
[level
],
1303 lpr
->tiles_per_row
[level
]);
1309 if (new_layout
!= cur_layout
)
1310 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1312 /* compute, return address of the 64x64 tile */
1313 tile_offset
= (ty
* lpr
->tiles_per_row
[level
] + tx
)
1314 * TILE_SIZE
* TILE_SIZE
* 4;
1316 return (ubyte
*) tiled_image
+ tile_offset
;
1321 * Get pointer to tiled data for rendering.
1322 * \return pointer to the tiled data at the given tile position
1325 llvmpipe_unswizzle_cbuf_tile(struct llvmpipe_resource
*lpr
,
1326 unsigned face_slice
, unsigned level
,
1327 unsigned x
, unsigned y
,
1330 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear_img
;
1331 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1332 uint8_t *linear_image
;
1334 assert(x
% TILE_SIZE
== 0);
1335 assert(y
% TILE_SIZE
== 0);
1337 if (!linear_img
->data
) {
1338 /* allocate memory for the linear image now */
1339 alloc_image_data(lpr
, LP_TEX_LAYOUT_LINEAR
);
1342 /* compute address of the slice/face of the image that contains the tile */
1343 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1344 LP_TEX_LAYOUT_LINEAR
);
1347 uint ii
= x
, jj
= y
;
1348 uint tile_offset
= jj
/ TILE_SIZE
+ ii
/ TILE_SIZE
;
1349 uint byte_offset
= tile_offset
* TILE_SIZE
* TILE_SIZE
* 4;
1351 /* Note that lp_tiled_to_linear expects the tile parameter to
1352 * point at the first tile in a whole-image sized array. In
1353 * this code, we have only a single tile and have to do some
1354 * pointer arithmetic to figure out where the "image" would have
1357 lp_tiled_to_linear(tile
- byte_offset
, linear_image
,
1358 x
, y
, TILE_SIZE
, TILE_SIZE
,
1360 lpr
->row_stride
[level
],
1361 1); /* tiles per row */
1364 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
,
1365 LP_TEX_LAYOUT_LINEAR
);
1370 * Get pointer to tiled data for rendering.
1371 * \return pointer to the tiled data at the given tile position
1374 llvmpipe_swizzle_cbuf_tile(struct llvmpipe_resource
*lpr
,
1375 unsigned face_slice
, unsigned level
,
1376 unsigned x
, unsigned y
,
1379 uint8_t *linear_image
;
1381 assert(x
% TILE_SIZE
== 0);
1382 assert(y
% TILE_SIZE
== 0);
1384 /* compute address of the slice/face of the image that contains the tile */
1385 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1386 LP_TEX_LAYOUT_LINEAR
);
1389 uint ii
= x
, jj
= y
;
1390 uint tile_offset
= jj
/ TILE_SIZE
+ ii
/ TILE_SIZE
;
1391 uint byte_offset
= tile_offset
* TILE_SIZE
* TILE_SIZE
* 4;
1393 /* Note that lp_linear_to_tiled expects the tile parameter to
1394 * point at the first tile in a whole-image sized array. In
1395 * this code, we have only a single tile and have to do some
1396 * pointer arithmetic to figure out where the "image" would have
1399 lp_linear_to_tiled(linear_image
, tile
- byte_offset
,
1400 x
, y
, TILE_SIZE
, TILE_SIZE
,
1402 lpr
->row_stride
[level
],
1403 1); /* tiles per row */
1409 * Return size of resource in bytes
1412 llvmpipe_resource_size(const struct pipe_resource
*resource
)
1414 const struct llvmpipe_resource
*lpr
= llvmpipe_resource_const(resource
);
1415 unsigned lvl
, size
= 0;
1417 for (lvl
= 0; lvl
<= lpr
->base
.last_level
; lvl
++) {
1418 if (lpr
->linear_img
.data
)
1419 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_LINEAR
);
1421 if (lpr
->tiled_img
.data
)
1422 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_TILED
);
1431 llvmpipe_print_resources(void)
1433 struct llvmpipe_resource
*lpr
;
1434 unsigned n
= 0, total
= 0;
1436 debug_printf("LLVMPIPE: current resources:\n");
1437 foreach(lpr
, &resource_list
) {
1438 unsigned size
= llvmpipe_resource_size(&lpr
->base
);
1439 debug_printf("resource %u at %p, size %ux%ux%u: %u bytes, refcount %u\n",
1440 lpr
->id
, (void *) lpr
,
1441 lpr
->base
.width0
, lpr
->base
.height0
, lpr
->base
.depth0
,
1442 size
, lpr
->base
.reference
.count
);
1446 debug_printf("LLVMPIPE: total size of %u resources: %u\n", n
, total
);
1452 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
1455 /* init linked list for tracking resources */
1457 static boolean first_call
= TRUE
;
1459 memset(&resource_list
, 0, sizeof(resource_list
));
1460 make_empty_list(&resource_list
);
1466 screen
->resource_create
= llvmpipe_resource_create
;
1467 screen
->resource_destroy
= llvmpipe_resource_destroy
;
1468 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
1469 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
1470 screen
->can_create_resource
= llvmpipe_can_create_resource
;
1475 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
1477 pipe
->transfer_map
= llvmpipe_transfer_map
;
1478 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
1480 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
1481 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;
1483 pipe
->create_surface
= llvmpipe_create_surface
;
1484 pipe
->surface_destroy
= llvmpipe_surface_destroy
;