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
[0].data
) {
346 align_free(lpr
->tiled
[0].data
);
347 lpr
->tiled
[0].data
= NULL
;
350 FREE(lpr
->layout
[0]);
352 else if (resource_is_texture(pt
)) {
353 /* regular texture */
356 /* free linear image data */
357 for (level
= 0; level
< Elements(lpr
->linear
); level
++) {
358 if (lpr
->linear
[level
].data
) {
359 align_free(lpr
->linear
[level
].data
);
360 lpr
->linear
[level
].data
= NULL
;
364 /* free tiled image data */
365 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
366 if (lpr
->tiled
[level
].data
) {
367 align_free(lpr
->tiled
[level
].data
);
368 lpr
->tiled
[level
].data
= NULL
;
372 /* free layout flag arrays */
373 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
374 FREE(lpr
->layout
[level
]);
375 lpr
->layout
[level
] = NULL
;
378 else if (!lpr
->userBuffer
) {
380 align_free(lpr
->data
);
385 remove_from_list(lpr
);
393 * Map a resource for read/write.
396 llvmpipe_resource_map(struct pipe_resource
*resource
,
399 enum lp_texture_usage tex_usage
,
400 enum lp_texture_layout layout
)
402 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
405 assert(level
< LP_MAX_TEXTURE_LEVELS
);
406 assert(layer
< (u_minify(resource
->depth0
, level
) + resource
->array_size
- 1));
408 assert(tex_usage
== LP_TEX_USAGE_READ
||
409 tex_usage
== LP_TEX_USAGE_READ_WRITE
||
410 tex_usage
== LP_TEX_USAGE_WRITE_ALL
);
412 assert(layout
== LP_TEX_LAYOUT_NONE
||
413 layout
== LP_TEX_LAYOUT_TILED
||
414 layout
== LP_TEX_LAYOUT_LINEAR
);
418 struct llvmpipe_screen
*screen
= llvmpipe_screen(resource
->screen
);
419 struct sw_winsys
*winsys
= screen
->winsys
;
423 if (tex_usage
== LP_TEX_USAGE_READ
) {
424 dt_usage
= PIPE_TRANSFER_READ
;
427 dt_usage
= PIPE_TRANSFER_READ_WRITE
;
433 /* FIXME: keep map count? */
434 map
= winsys
->displaytarget_map(winsys
, lpr
->dt
, dt_usage
);
436 /* install this linear image in texture data structure */
437 lpr
->linear
[level
].data
= map
;
439 /* make sure tiled data gets converted to linear data */
440 map2
= llvmpipe_get_texture_image(lpr
, 0, 0, tex_usage
, layout
);
441 if (layout
== LP_TEX_LAYOUT_LINEAR
)
446 else if (resource_is_texture(resource
)) {
448 map
= llvmpipe_get_texture_image(lpr
, layer
, level
,
462 llvmpipe_resource_unmap(struct pipe_resource
*resource
,
466 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
470 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(resource
->screen
);
471 struct sw_winsys
*winsys
= lp_screen
->winsys
;
476 /* make sure linear image is up to date */
477 (void) llvmpipe_get_texture_image(lpr
, layer
, level
,
479 LP_TEX_LAYOUT_LINEAR
);
481 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
487 llvmpipe_resource_data(struct pipe_resource
*resource
)
489 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
491 assert(!resource_is_texture(resource
));
497 static struct pipe_resource
*
498 llvmpipe_resource_from_handle(struct pipe_screen
*screen
,
499 const struct pipe_resource
*template,
500 struct winsys_handle
*whandle
)
502 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
503 struct llvmpipe_resource
*lpr
;
504 unsigned width
, height
, width_t
, height_t
;
506 /* XXX Seems like from_handled depth textures doesn't work that well */
508 lpr
= CALLOC_STRUCT(llvmpipe_resource
);
513 lpr
->base
= *template;
514 pipe_reference_init(&lpr
->base
.reference
, 1);
515 lpr
->base
.screen
= screen
;
517 width
= align(lpr
->base
.width0
, TILE_SIZE
);
518 height
= align(lpr
->base
.height0
, TILE_SIZE
);
519 width_t
= width
/ TILE_SIZE
;
520 height_t
= height
/ TILE_SIZE
;
523 * Looks like unaligned displaytargets work just fine,
524 * at least sampler/render ones.
527 assert(lpr
->base
.width0
== width
);
528 assert(lpr
->base
.height0
== height
);
531 lpr
->tiles_per_row
[0] = width_t
;
532 lpr
->tiles_per_image
[0] = width_t
* height_t
;
533 lpr
->num_slices_faces
[0] = 1;
534 lpr
->img_stride
[0] = 0;
536 lpr
->dt
= winsys
->displaytarget_from_handle(winsys
,
539 &lpr
->row_stride
[0]);
544 lpr
->layout
[0] = alloc_layout_array(1, lpr
->base
.width0
, lpr
->base
.height0
);
545 if (!lpr
->layout
[0]) {
549 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
551 lpr
->id
= id_counter
++;
554 insert_at_tail(&resource_list
, lpr
);
560 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
569 llvmpipe_resource_get_handle(struct pipe_screen
*screen
,
570 struct pipe_resource
*pt
,
571 struct winsys_handle
*whandle
)
573 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
574 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
580 return winsys
->displaytarget_get_handle(winsys
, lpr
->dt
, whandle
);
584 static struct pipe_surface
*
585 llvmpipe_create_surface(struct pipe_context
*pipe
,
586 struct pipe_resource
*pt
,
587 const struct pipe_surface
*surf_tmpl
)
589 struct pipe_surface
*ps
;
591 assert(surf_tmpl
->u
.tex
.level
<= pt
->last_level
);
593 ps
= CALLOC_STRUCT(pipe_surface
);
595 pipe_reference_init(&ps
->reference
, 1);
596 pipe_resource_reference(&ps
->texture
, pt
);
598 ps
->format
= surf_tmpl
->format
;
599 ps
->width
= u_minify(pt
->width0
, surf_tmpl
->u
.tex
.level
);
600 ps
->height
= u_minify(pt
->height0
, surf_tmpl
->u
.tex
.level
);
601 ps
->usage
= surf_tmpl
->usage
;
603 ps
->u
.tex
.level
= surf_tmpl
->u
.tex
.level
;
604 ps
->u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
605 ps
->u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
612 llvmpipe_surface_destroy(struct pipe_context
*pipe
,
613 struct pipe_surface
*surf
)
615 /* Effectively do the texture_update work here - if texture images
616 * needed post-processing to put them into hardware layout, this is
617 * where it would happen. For llvmpipe, nothing to do.
619 assert(surf
->texture
);
620 pipe_resource_reference(&surf
->texture
, NULL
);
625 static struct pipe_transfer
*
626 llvmpipe_get_transfer(struct pipe_context
*pipe
,
627 struct pipe_resource
*resource
,
630 const struct pipe_box
*box
)
632 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
633 struct llvmpipe_resource
*lprex
= llvmpipe_resource(resource
);
634 struct llvmpipe_transfer
*lpr
;
637 assert(level
<= resource
->last_level
);
640 * Transfers, like other pipe operations, must happen in order, so flush the
641 * context if necessary.
643 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
644 boolean read_only
= !(usage
& PIPE_TRANSFER_WRITE
);
645 boolean do_not_block
= !!(usage
& PIPE_TRANSFER_DONTBLOCK
);
646 if (!llvmpipe_flush_resource(pipe
, resource
,
648 box
->depth
> 1 ? -1 : box
->z
,
650 TRUE
, /* cpu_access */
654 * It would have blocked, but state tracker requested no to.
656 assert(do_not_block
);
661 if (resource
== llvmpipe
->constants
[PIPE_SHADER_FRAGMENT
][0])
662 llvmpipe
->dirty
|= LP_NEW_CONSTANTS
;
664 lpr
= CALLOC_STRUCT(llvmpipe_transfer
);
666 struct pipe_transfer
*pt
= &lpr
->base
;
667 pipe_resource_reference(&pt
->resource
, resource
);
670 pt
->stride
= lprex
->row_stride
[level
];
671 pt
->layer_stride
= lprex
->img_stride
[level
];
681 llvmpipe_transfer_destroy(struct pipe_context
*pipe
,
682 struct pipe_transfer
*transfer
)
684 /* Effectively do the texture_update work here - if texture images
685 * needed post-processing to put them into hardware layout, this is
686 * where it would happen. For llvmpipe, nothing to do.
688 assert (transfer
->resource
);
689 pipe_resource_reference(&transfer
->resource
, NULL
);
695 llvmpipe_transfer_map( struct pipe_context
*pipe
,
696 struct pipe_transfer
*transfer
)
698 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
700 struct llvmpipe_resource
*lpr
;
701 enum pipe_format format
;
702 enum lp_texture_usage tex_usage
;
705 assert(transfer
->level
< LP_MAX_TEXTURE_LEVELS
);
708 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
709 transfer->x, transfer->y, transfer->width, transfer->height,
710 transfer->texture->width0,
711 transfer->texture->height0,
715 if (transfer
->usage
== PIPE_TRANSFER_READ
) {
716 tex_usage
= LP_TEX_USAGE_READ
;
720 tex_usage
= LP_TEX_USAGE_READ_WRITE
;
725 struct llvmpipe_resource
*lpr
= llvmpipe_resource(transfer
->resource
);
726 printf("transfer map tex %u mode %s\n", lpr
->id
, mode
);
730 assert(transfer
->resource
);
731 lpr
= llvmpipe_resource(transfer
->resource
);
732 format
= lpr
->base
.format
;
734 map
= llvmpipe_resource_map(transfer
->resource
,
737 tex_usage
, LP_TEX_LAYOUT_LINEAR
);
740 /* May want to do different things here depending on read/write nature
743 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
744 /* Do something to notify sharing contexts of a texture change.
750 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
751 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
758 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
759 struct pipe_transfer
*transfer
)
761 assert(transfer
->resource
);
763 llvmpipe_resource_unmap(transfer
->resource
,
769 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
770 struct pipe_resource
*presource
,
771 unsigned level
, int layer
)
773 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
775 if (presource
->target
== PIPE_BUFFER
)
776 return LP_UNREFERENCED
;
778 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
784 * Create buffer which wraps user-space data.
786 struct pipe_resource
*
787 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
792 struct llvmpipe_resource
*buffer
;
794 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
798 pipe_reference_init(&buffer
->base
.reference
, 1);
799 buffer
->base
.screen
= screen
;
800 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
801 buffer
->base
.bind
= bind_flags
;
802 buffer
->base
.usage
= PIPE_USAGE_IMMUTABLE
;
803 buffer
->base
.flags
= 0;
804 buffer
->base
.width0
= bytes
;
805 buffer
->base
.height0
= 1;
806 buffer
->base
.depth0
= 1;
807 buffer
->base
.array_size
= 1;
808 buffer
->userBuffer
= TRUE
;
811 return &buffer
->base
;
816 * Compute size (in bytes) need to store a texture image / mipmap level,
817 * for just one cube face or one 3D texture slice
820 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
821 enum lp_texture_layout layout
)
823 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
824 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
826 assert(layout
== LP_TEX_LAYOUT_TILED
||
827 layout
== LP_TEX_LAYOUT_LINEAR
);
829 if (layout
== LP_TEX_LAYOUT_TILED
) {
830 /* for tiled layout, force a 32bpp format */
831 const enum pipe_format format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
832 const unsigned block_size
= util_format_get_blocksize(format
);
833 const unsigned nblocksy
=
834 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
835 const unsigned nblocksx
=
836 util_format_get_nblocksx(format
, align(width
, TILE_SIZE
));
837 const unsigned buffer_size
= block_size
* nblocksy
* nblocksx
;
841 /* we already computed this */
842 return lpr
->img_stride
[level
];
848 * Compute size (in bytes) need to store a texture image / mipmap level,
849 * including all cube faces or 3D image slices
852 tex_image_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
853 enum lp_texture_layout layout
)
855 const unsigned buf_size
= tex_image_face_size(lpr
, level
, layout
);
856 return buf_size
* lpr
->num_slices_faces
[level
];
861 * This function encapsulates some complicated logic for determining
862 * how to convert a tile of image data from linear layout to tiled
863 * layout, or vice versa.
864 * \param cur_layout the current tile layout
865 * \param target_layout the desired tile layout
866 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
867 * \param new_layout_return returns the new layout mode
868 * \param convert_return returns TRUE if image conversion is needed
871 layout_logic(enum lp_texture_layout cur_layout
,
872 enum lp_texture_layout target_layout
,
873 enum lp_texture_usage usage
,
874 enum lp_texture_layout
*new_layout_return
,
877 enum lp_texture_layout other_layout
, new_layout
;
881 new_layout
= 99; /* debug check */
883 if (target_layout
== LP_TEX_LAYOUT_LINEAR
) {
884 other_layout
= LP_TEX_LAYOUT_TILED
;
887 assert(target_layout
== LP_TEX_LAYOUT_TILED
);
888 other_layout
= LP_TEX_LAYOUT_LINEAR
;
891 new_layout
= target_layout
; /* may get changed below */
893 if (cur_layout
== LP_TEX_LAYOUT_BOTH
) {
894 if (usage
== LP_TEX_USAGE_READ
) {
895 new_layout
= LP_TEX_LAYOUT_BOTH
;
898 else if (cur_layout
== other_layout
) {
899 if (usage
!= LP_TEX_USAGE_WRITE_ALL
) {
900 /* need to convert tiled data to linear or vice versa */
903 if (usage
== LP_TEX_USAGE_READ
)
904 new_layout
= LP_TEX_LAYOUT_BOTH
;
908 assert(cur_layout
== LP_TEX_LAYOUT_NONE
||
909 cur_layout
== target_layout
);
912 assert(new_layout
== LP_TEX_LAYOUT_BOTH
||
913 new_layout
== target_layout
);
915 *new_layout_return
= new_layout
;
920 * Return pointer to a 2D texture image/face/slice.
921 * No tiled/linear conversion is done.
924 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
925 unsigned face_slice
, unsigned level
,
926 enum lp_texture_layout layout
)
928 struct llvmpipe_texture_image
*img
;
931 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
932 img
= &lpr
->linear
[level
];
935 assert (layout
== LP_TEX_LAYOUT_TILED
);
936 img
= &lpr
->tiled
[level
];
940 offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
944 return (ubyte
*) img
->data
+ offset
;
948 static INLINE
enum lp_texture_layout
949 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource
*lpr
,
950 unsigned face_slice
, unsigned level
,
951 unsigned x
, unsigned y
)
954 assert(resource_is_texture(&lpr
->base
));
955 assert(x
< lpr
->tiles_per_row
[level
]);
956 i
= face_slice
* lpr
->tiles_per_image
[level
]
957 + y
* lpr
->tiles_per_row
[level
] + x
;
958 return lpr
->layout
[level
][i
];
963 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource
*lpr
,
964 unsigned face_slice
, unsigned level
,
965 unsigned x
, unsigned y
,
966 enum lp_texture_layout layout
)
969 assert(resource_is_texture(&lpr
->base
));
970 assert(x
< lpr
->tiles_per_row
[level
]);
971 i
= face_slice
* lpr
->tiles_per_image
[level
]
972 + y
* lpr
->tiles_per_row
[level
] + x
;
973 lpr
->layout
[level
][i
] = layout
;
978 * Set the layout mode for all tiles in a particular image.
981 llvmpipe_set_texture_image_layout(struct llvmpipe_resource
*lpr
,
982 unsigned face_slice
, unsigned level
,
983 unsigned width_t
, unsigned height_t
,
984 enum lp_texture_layout layout
)
986 const unsigned start
= face_slice
* lpr
->tiles_per_image
[level
];
989 for (i
= 0; i
< width_t
* height_t
; i
++) {
990 lpr
->layout
[level
][start
+ i
] = layout
;
996 * Allocate storage for a linear or tile texture image (all cube
997 * faces and all 3D slices.
1000 alloc_image_data(struct llvmpipe_resource
*lpr
, unsigned level
,
1001 enum lp_texture_layout layout
)
1003 uint alignment
= MAX2(16, util_cpu_caps
.cacheline
);
1008 if (layout
== LP_TEX_LAYOUT_TILED
) {
1009 /* tiled data is stored in regular memory */
1010 uint buffer_size
= tex_image_size(lpr
, level
, layout
);
1011 lpr
->tiled
[level
].data
= align_malloc(buffer_size
, alignment
);
1012 if (lpr
->tiled
[level
].data
) {
1013 memset(lpr
->tiled
[level
].data
, 0, buffer_size
);
1017 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
1019 /* we get the linear memory from the winsys, and it has
1020 * already been zeroed
1022 struct llvmpipe_screen
*screen
= llvmpipe_screen(lpr
->base
.screen
);
1023 struct sw_winsys
*winsys
= screen
->winsys
;
1025 lpr
->linear
[0].data
=
1026 winsys
->displaytarget_map(winsys
, lpr
->dt
,
1027 PIPE_TRANSFER_READ_WRITE
);
1030 /* not a display target - allocate regular memory */
1031 uint buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1032 lpr
->linear
[level
].data
= align_malloc(buffer_size
, alignment
);
1033 if (lpr
->linear
[level
].data
) {
1034 memset(lpr
->linear
[level
].data
, 0, buffer_size
);
1043 * Return pointer to texture image data (either linear or tiled layout)
1044 * for a particular cube face or 3D texture slice.
1046 * \param face_slice the cube face or 3D slice of interest
1047 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
1048 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
1051 llvmpipe_get_texture_image(struct llvmpipe_resource
*lpr
,
1052 unsigned face_slice
, unsigned level
,
1053 enum lp_texture_usage usage
,
1054 enum lp_texture_layout layout
)
1057 * 'target' refers to the image which we're retrieving (either in
1058 * tiled or linear layout).
1059 * 'other' refers to the same image but in the other layout. (it may
1062 struct llvmpipe_texture_image
*target_img
;
1063 struct llvmpipe_texture_image
*other_img
;
1066 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
1067 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
1068 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
1069 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
1070 enum lp_texture_layout other_layout
;
1071 boolean only_allocate
;
1073 assert(layout
== LP_TEX_LAYOUT_NONE
||
1074 layout
== LP_TEX_LAYOUT_TILED
||
1075 layout
== LP_TEX_LAYOUT_LINEAR
);
1077 assert(usage
== LP_TEX_USAGE_READ
||
1078 usage
== LP_TEX_USAGE_READ_WRITE
||
1079 usage
== LP_TEX_USAGE_WRITE_ALL
);
1081 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
1082 if (layout
== LP_TEX_LAYOUT_NONE
) {
1083 only_allocate
= TRUE
;
1084 layout
= LP_TEX_LAYOUT_TILED
;
1087 only_allocate
= FALSE
;
1091 assert(lpr
->linear
[level
].data
);
1094 /* which is target? which is other? */
1095 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
1096 target_img
= &lpr
->linear
[level
];
1097 other_img
= &lpr
->tiled
[level
];
1098 other_layout
= LP_TEX_LAYOUT_TILED
;
1101 target_img
= &lpr
->tiled
[level
];
1102 other_img
= &lpr
->linear
[level
];
1103 other_layout
= LP_TEX_LAYOUT_LINEAR
;
1106 target_data
= target_img
->data
;
1107 other_data
= other_img
->data
;
1110 /* allocate memory for the target image now */
1111 alloc_image_data(lpr
, level
, layout
);
1112 target_data
= target_img
->data
;
1115 if (face_slice
> 0) {
1116 unsigned target_offset
, other_offset
;
1118 target_offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
1119 other_offset
= face_slice
* tex_image_face_size(lpr
, level
, other_layout
);
1121 target_data
= (uint8_t *) target_data
+ target_offset
;
1124 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
[level
];
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
, level
, 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
[level
];
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
, level
, 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
[level
];
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
, level
, 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
[lvl
].data
)
1419 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_LINEAR
);
1421 if (lpr
->tiled
[lvl
].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
->get_transfer
= llvmpipe_get_transfer
;
1478 pipe
->transfer_destroy
= llvmpipe_transfer_destroy
;
1479 pipe
->transfer_map
= llvmpipe_transfer_map
;
1480 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
1482 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
1483 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;
1485 pipe
->create_surface
= llvmpipe_create_surface
;
1486 pipe
->surface_destroy
= llvmpipe_surface_destroy
;