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
) {
70 case PIPE_TEXTURE_1D_ARRAY
:
72 case PIPE_TEXTURE_2D_ARRAY
:
73 case PIPE_TEXTURE_RECT
:
75 case PIPE_TEXTURE_CUBE
:
86 * Allocate storage for llvmpipe_texture::layout array.
87 * The number of elements is width_in_tiles * height_in_tiles.
89 static enum lp_texture_layout
*
90 alloc_layout_array(unsigned num_slices
, unsigned width
, unsigned height
)
92 const unsigned tx
= align(width
, TILE_SIZE
) / TILE_SIZE
;
93 const unsigned ty
= align(height
, TILE_SIZE
) / TILE_SIZE
;
95 assert(num_slices
* tx
* ty
> 0);
96 assert(LP_TEX_LAYOUT_NONE
== 0); /* calloc'ing LP_TEX_LAYOUT_NONE here */
98 return (enum lp_texture_layout
*)
99 CALLOC(num_slices
* tx
* ty
, sizeof(enum lp_texture_layout
));
105 * Conventional allocation path for non-display textures:
106 * Just compute row strides here. Storage is allocated on demand later.
109 llvmpipe_texture_layout(struct llvmpipe_screen
*screen
,
110 struct llvmpipe_resource
*lpr
,
113 struct pipe_resource
*pt
= &lpr
->base
;
115 unsigned width
= pt
->width0
;
116 unsigned height
= pt
->height0
;
117 unsigned depth
= pt
->depth0
;
118 uint64_t total_size
= 0;
119 unsigned layers
= pt
->array_size
;
121 assert(LP_MAX_TEXTURE_2D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
122 assert(LP_MAX_TEXTURE_3D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
124 for (level
= 0; level
<= pt
->last_level
; level
++) {
126 /* Row stride and image stride (for linear layout) */
128 unsigned alignment
, nblocksx
, nblocksy
, block_size
;
130 /* For non-compressed formats we need to align the texture size
131 * to the tile size to facilitate render-to-texture.
132 * XXX this blows up 1d/1d array textures by unreasonable
133 * amount (factor 64), probably should do something about it.
135 if (util_format_is_compressed(pt
->format
))
138 alignment
= TILE_SIZE
;
140 nblocksx
= util_format_get_nblocksx(pt
->format
,
141 align(width
, alignment
));
142 nblocksy
= util_format_get_nblocksy(pt
->format
,
143 align(height
, alignment
));
144 block_size
= util_format_get_blocksize(pt
->format
);
146 lpr
->row_stride
[level
] = align(nblocksx
* block_size
, 16);
148 /* if row_stride * height > LP_MAX_TEXTURE_SIZE */
149 if (lpr
->row_stride
[level
] > LP_MAX_TEXTURE_SIZE
/ nblocksy
) {
150 /* image too large */
154 lpr
->img_stride
[level
] = lpr
->row_stride
[level
] * nblocksy
;
157 /* Size of the image in tiles (for tiled layout) */
159 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
160 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
161 lpr
->tiles_per_row
[level
] = width_t
;
162 lpr
->tiles_per_image
[level
] = width_t
* height_t
;
165 /* Number of 3D image slices, cube faces or texture array layers */
169 if (lpr
->base
.target
== PIPE_TEXTURE_CUBE
)
171 else if (lpr
->base
.target
== PIPE_TEXTURE_3D
)
173 else if (lpr
->base
.target
== PIPE_TEXTURE_1D_ARRAY
||
174 lpr
->base
.target
== PIPE_TEXTURE_2D_ARRAY
)
179 lpr
->num_slices_faces
[level
] = num_slices
;
182 lpr
->layout
[level
] = alloc_layout_array(num_slices
, width
, height
);
183 if (!lpr
->layout
[level
]) {
189 /* if img_stride * num_slices_faces > LP_MAX_TEXTURE_SIZE */
190 if (lpr
->img_stride
[level
] >
191 LP_MAX_TEXTURE_SIZE
/ lpr
->num_slices_faces
[level
]) {
192 /* volume too large */
196 total_size
+= (uint64_t) lpr
->num_slices_faces
[level
]
197 * (uint64_t) lpr
->img_stride
[level
];
198 if (total_size
> LP_MAX_TEXTURE_SIZE
) {
202 /* Compute size of next mipmap level */
203 width
= u_minify(width
, 1);
204 height
= u_minify(height
, 1);
205 depth
= u_minify(depth
, 1);
211 for (level
= 0; level
<= pt
->last_level
; level
++) {
212 FREE(lpr
->layout
[level
]);
220 * Check the size of the texture specified by 'res'.
221 * \return TRUE if OK, FALSE if too large.
224 llvmpipe_can_create_resource(struct pipe_screen
*screen
,
225 const struct pipe_resource
*res
)
227 struct llvmpipe_resource lpr
;
228 memset(&lpr
, 0, sizeof(lpr
));
230 return llvmpipe_texture_layout(llvmpipe_screen(screen
), &lpr
, FALSE
);
235 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
236 struct llvmpipe_resource
*lpr
)
238 struct sw_winsys
*winsys
= screen
->winsys
;
240 /* Round up the surface size to a multiple of the tile size to
241 * avoid tile clipping.
243 const unsigned width
= MAX2(1, align(lpr
->base
.width0
, TILE_SIZE
));
244 const unsigned height
= MAX2(1, align(lpr
->base
.height0
, TILE_SIZE
));
245 const unsigned width_t
= width
/ TILE_SIZE
;
246 const unsigned height_t
= height
/ TILE_SIZE
;
248 lpr
->tiles_per_row
[0] = width_t
;
249 lpr
->tiles_per_image
[0] = width_t
* height_t
;
250 lpr
->num_slices_faces
[0] = 1;
251 lpr
->img_stride
[0] = 0;
253 lpr
->layout
[0] = alloc_layout_array(1, width
, height
);
254 if (!lpr
->layout
[0]) {
258 lpr
->dt
= winsys
->displaytarget_create(winsys
,
263 &lpr
->row_stride
[0] );
269 void *map
= winsys
->displaytarget_map(winsys
, lpr
->dt
,
270 PIPE_TRANSFER_WRITE
);
273 memset(map
, 0, height
* lpr
->row_stride
[0]);
275 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
282 static struct pipe_resource
*
283 llvmpipe_resource_create(struct pipe_screen
*_screen
,
284 const struct pipe_resource
*templat
)
286 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
287 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
291 lpr
->base
= *templat
;
292 pipe_reference_init(&lpr
->base
.reference
, 1);
293 lpr
->base
.screen
= &screen
->base
;
295 /* assert(lpr->base.bind); */
297 if (resource_is_texture(&lpr
->base
)) {
298 if (lpr
->base
.bind
& PIPE_BIND_DISPLAY_TARGET
) {
299 /* displayable surface */
300 if (!llvmpipe_displaytarget_layout(screen
, lpr
))
302 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
306 if (!llvmpipe_texture_layout(screen
, lpr
, TRUE
))
308 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
310 assert(lpr
->layout
[0]);
313 /* other data (vertex buffer, const buffer, etc) */
314 const enum pipe_format format
= templat
->format
;
315 const uint w
= templat
->width0
/ util_format_get_blockheight(format
);
316 /* XXX buffers should only have one dimension, those values should be 1 */
317 const uint h
= templat
->height0
/ util_format_get_blockwidth(format
);
318 const uint d
= templat
->depth0
;
319 const uint bpp
= util_format_get_blocksize(format
);
320 const uint bytes
= w
* h
* d
* bpp
;
321 lpr
->data
= align_malloc(bytes
, 16);
324 memset(lpr
->data
, 0, bytes
);
327 lpr
->id
= id_counter
++;
330 insert_at_tail(&resource_list
, lpr
);
342 llvmpipe_resource_destroy(struct pipe_screen
*pscreen
,
343 struct pipe_resource
*pt
)
345 struct llvmpipe_screen
*screen
= llvmpipe_screen(pscreen
);
346 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
350 struct sw_winsys
*winsys
= screen
->winsys
;
351 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
353 if (lpr
->tiled_img
.data
) {
354 align_free(lpr
->tiled_img
.data
);
355 lpr
->tiled_img
.data
= NULL
;
358 FREE(lpr
->layout
[0]);
360 else if (resource_is_texture(pt
)) {
361 /* regular texture */
364 /* free linear image data */
365 if (lpr
->linear_img
.data
) {
366 align_free(lpr
->linear_img
.data
);
367 lpr
->linear_img
.data
= NULL
;
370 /* free tiled image data */
371 if (lpr
->tiled_img
.data
) {
372 align_free(lpr
->tiled_img
.data
);
373 lpr
->tiled_img
.data
= NULL
;
376 /* free layout flag arrays */
377 for (level
= 0; level
< Elements(lpr
->layout
); level
++) {
378 FREE(lpr
->layout
[level
]);
379 lpr
->layout
[level
] = NULL
;
382 else if (!lpr
->userBuffer
) {
384 align_free(lpr
->data
);
389 remove_from_list(lpr
);
397 * Map a resource for read/write.
400 llvmpipe_resource_map(struct pipe_resource
*resource
,
403 enum lp_texture_usage tex_usage
,
404 enum lp_texture_layout layout
)
406 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
409 assert(level
< LP_MAX_TEXTURE_LEVELS
);
410 assert(layer
< (u_minify(resource
->depth0
, level
) + resource
->array_size
- 1));
412 assert(tex_usage
== LP_TEX_USAGE_READ
||
413 tex_usage
== LP_TEX_USAGE_READ_WRITE
||
414 tex_usage
== LP_TEX_USAGE_WRITE_ALL
);
416 assert(layout
== LP_TEX_LAYOUT_NONE
||
417 layout
== LP_TEX_LAYOUT_TILED
||
418 layout
== LP_TEX_LAYOUT_LINEAR
);
422 struct llvmpipe_screen
*screen
= llvmpipe_screen(resource
->screen
);
423 struct sw_winsys
*winsys
= screen
->winsys
;
427 if (tex_usage
== LP_TEX_USAGE_READ
) {
428 dt_usage
= PIPE_TRANSFER_READ
;
431 dt_usage
= PIPE_TRANSFER_READ_WRITE
;
437 /* FIXME: keep map count? */
438 map
= winsys
->displaytarget_map(winsys
, lpr
->dt
, dt_usage
);
440 /* install this linear image in texture data structure */
441 lpr
->linear_img
.data
= map
;
443 /* make sure tiled data gets converted to linear data */
444 map2
= llvmpipe_get_texture_image(lpr
, 0, 0, tex_usage
, layout
);
445 if (layout
== LP_TEX_LAYOUT_LINEAR
)
450 else if (resource_is_texture(resource
)) {
452 map
= llvmpipe_get_texture_image(lpr
, layer
, level
,
466 llvmpipe_resource_unmap(struct pipe_resource
*resource
,
470 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
474 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(resource
->screen
);
475 struct sw_winsys
*winsys
= lp_screen
->winsys
;
480 /* make sure linear image is up to date */
481 (void) llvmpipe_get_texture_image(lpr
, layer
, level
,
483 LP_TEX_LAYOUT_LINEAR
);
485 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
491 llvmpipe_resource_data(struct pipe_resource
*resource
)
493 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
495 assert(!resource_is_texture(resource
));
501 static struct pipe_resource
*
502 llvmpipe_resource_from_handle(struct pipe_screen
*screen
,
503 const struct pipe_resource
*template,
504 struct winsys_handle
*whandle
)
506 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
507 struct llvmpipe_resource
*lpr
;
508 unsigned width
, height
, width_t
, height_t
;
510 /* XXX Seems like from_handled depth textures doesn't work that well */
512 lpr
= CALLOC_STRUCT(llvmpipe_resource
);
517 lpr
->base
= *template;
518 pipe_reference_init(&lpr
->base
.reference
, 1);
519 lpr
->base
.screen
= screen
;
521 width
= align(lpr
->base
.width0
, TILE_SIZE
);
522 height
= align(lpr
->base
.height0
, TILE_SIZE
);
523 width_t
= width
/ TILE_SIZE
;
524 height_t
= height
/ TILE_SIZE
;
527 * Looks like unaligned displaytargets work just fine,
528 * at least sampler/render ones.
531 assert(lpr
->base
.width0
== width
);
532 assert(lpr
->base
.height0
== height
);
535 lpr
->tiles_per_row
[0] = width_t
;
536 lpr
->tiles_per_image
[0] = width_t
* height_t
;
537 lpr
->num_slices_faces
[0] = 1;
538 lpr
->img_stride
[0] = 0;
540 lpr
->dt
= winsys
->displaytarget_from_handle(winsys
,
543 &lpr
->row_stride
[0]);
548 lpr
->layout
[0] = alloc_layout_array(1, lpr
->base
.width0
, lpr
->base
.height0
);
549 if (!lpr
->layout
[0]) {
553 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
555 lpr
->id
= id_counter
++;
558 insert_at_tail(&resource_list
, lpr
);
564 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
573 llvmpipe_resource_get_handle(struct pipe_screen
*screen
,
574 struct pipe_resource
*pt
,
575 struct winsys_handle
*whandle
)
577 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
578 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
584 return winsys
->displaytarget_get_handle(winsys
, lpr
->dt
, whandle
);
588 static struct pipe_surface
*
589 llvmpipe_create_surface(struct pipe_context
*pipe
,
590 struct pipe_resource
*pt
,
591 const struct pipe_surface
*surf_tmpl
)
593 struct pipe_surface
*ps
;
595 assert(surf_tmpl
->u
.tex
.level
<= pt
->last_level
);
597 ps
= CALLOC_STRUCT(pipe_surface
);
599 pipe_reference_init(&ps
->reference
, 1);
600 pipe_resource_reference(&ps
->texture
, pt
);
602 ps
->format
= surf_tmpl
->format
;
603 ps
->width
= u_minify(pt
->width0
, surf_tmpl
->u
.tex
.level
);
604 ps
->height
= u_minify(pt
->height0
, surf_tmpl
->u
.tex
.level
);
606 ps
->u
.tex
.level
= surf_tmpl
->u
.tex
.level
;
607 ps
->u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
608 ps
->u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
615 llvmpipe_surface_destroy(struct pipe_context
*pipe
,
616 struct pipe_surface
*surf
)
618 /* Effectively do the texture_update work here - if texture images
619 * needed post-processing to put them into hardware layout, this is
620 * where it would happen. For llvmpipe, nothing to do.
622 assert(surf
->texture
);
623 pipe_resource_reference(&surf
->texture
, NULL
);
629 llvmpipe_transfer_map( struct pipe_context
*pipe
,
630 struct pipe_resource
*resource
,
633 const struct pipe_box
*box
,
634 struct pipe_transfer
**transfer
)
636 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
637 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
638 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
639 struct llvmpipe_transfer
*lpt
;
640 struct pipe_transfer
*pt
;
642 enum pipe_format format
;
643 enum lp_texture_usage tex_usage
;
647 assert(level
<= resource
->last_level
);
650 * Transfers, like other pipe operations, must happen in order, so flush the
651 * context if necessary.
653 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
654 boolean read_only
= !(usage
& PIPE_TRANSFER_WRITE
);
655 boolean do_not_block
= !!(usage
& PIPE_TRANSFER_DONTBLOCK
);
656 if (!llvmpipe_flush_resource(pipe
, resource
,
659 TRUE
, /* cpu_access */
663 * It would have blocked, but state tracker requested no to.
665 assert(do_not_block
);
670 /* Check if we're mapping the current constant buffer */
671 if ((usage
& PIPE_TRANSFER_WRITE
) &&
672 resource
== llvmpipe
->constants
[PIPE_SHADER_FRAGMENT
][0].buffer
) {
673 /* constants may have changed */
674 llvmpipe
->dirty
|= LP_NEW_CONSTANTS
;
677 lpt
= CALLOC_STRUCT(llvmpipe_transfer
);
681 pipe_resource_reference(&pt
->resource
, resource
);
684 pt
->stride
= lpr
->row_stride
[level
];
685 pt
->layer_stride
= lpr
->img_stride
[level
];
689 assert(level
< LP_MAX_TEXTURE_LEVELS
);
692 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
693 transfer->x, transfer->y, transfer->width, transfer->height,
694 transfer->texture->width0,
695 transfer->texture->height0,
699 if (usage
== PIPE_TRANSFER_READ
) {
700 tex_usage
= LP_TEX_USAGE_READ
;
704 tex_usage
= LP_TEX_USAGE_READ_WRITE
;
709 printf("transfer map tex %u mode %s\n", lpr
->id
, mode
);
712 format
= lpr
->base
.format
;
714 map
= llvmpipe_resource_map(resource
,
717 tex_usage
, LP_TEX_LAYOUT_LINEAR
);
720 /* May want to do different things here depending on read/write nature
723 if (usage
& PIPE_TRANSFER_WRITE
) {
724 /* Do something to notify sharing contexts of a texture change.
730 box
->y
/ util_format_get_blockheight(format
) * pt
->stride
+
731 box
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
738 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
739 struct pipe_transfer
*transfer
)
741 assert(transfer
->resource
);
743 llvmpipe_resource_unmap(transfer
->resource
,
747 /* Effectively do the texture_update work here - if texture images
748 * needed post-processing to put them into hardware layout, this is
749 * where it would happen. For llvmpipe, nothing to do.
751 assert (transfer
->resource
);
752 pipe_resource_reference(&transfer
->resource
, NULL
);
757 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
758 struct pipe_resource
*presource
,
761 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
763 if (presource
->target
== PIPE_BUFFER
)
764 return LP_UNREFERENCED
;
766 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
771 * Returns the largest possible alignment for a format in llvmpipe
774 llvmpipe_get_format_alignment( enum pipe_format format
)
776 const struct util_format_description
*desc
= util_format_description(format
);
781 for (i
= 0; i
< desc
->nr_channels
; ++i
) {
782 size
+= desc
->channel
[i
].size
;
787 if (!util_is_power_of_two(bytes
)) {
788 bytes
/= desc
->nr_channels
;
791 if (bytes
% 2 || bytes
< 1) {
800 * Create buffer which wraps user-space data.
802 struct pipe_resource
*
803 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
808 struct llvmpipe_resource
*buffer
;
810 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
814 pipe_reference_init(&buffer
->base
.reference
, 1);
815 buffer
->base
.screen
= screen
;
816 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
817 buffer
->base
.bind
= bind_flags
;
818 buffer
->base
.usage
= PIPE_USAGE_IMMUTABLE
;
819 buffer
->base
.flags
= 0;
820 buffer
->base
.width0
= bytes
;
821 buffer
->base
.height0
= 1;
822 buffer
->base
.depth0
= 1;
823 buffer
->base
.array_size
= 1;
824 buffer
->userBuffer
= TRUE
;
827 return &buffer
->base
;
832 * Compute size (in bytes) need to store a texture image / mipmap level,
833 * for just one cube face, one array layer or one 3D texture slice
836 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
837 enum lp_texture_layout layout
)
839 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
840 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
842 assert(layout
== LP_TEX_LAYOUT_TILED
||
843 layout
== LP_TEX_LAYOUT_LINEAR
);
845 if (layout
== LP_TEX_LAYOUT_TILED
) {
846 /* for tiled layout, force a 32bpp format */
847 const enum pipe_format format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
848 const unsigned block_size
= util_format_get_blocksize(format
);
849 const unsigned nblocksy
=
850 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
851 const unsigned nblocksx
=
852 util_format_get_nblocksx(format
, align(width
, TILE_SIZE
));
853 const unsigned buffer_size
= block_size
* nblocksy
* nblocksx
;
857 /* we already computed this */
858 return lpr
->img_stride
[level
];
864 * Compute size (in bytes) need to store a texture image / mipmap level,
865 * including all cube faces or 3D image slices
868 tex_image_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
869 enum lp_texture_layout layout
)
871 const unsigned buf_size
= tex_image_face_size(lpr
, level
, layout
);
872 return buf_size
* lpr
->num_slices_faces
[level
];
877 * This function encapsulates some complicated logic for determining
878 * how to convert a tile of image data from linear layout to tiled
879 * layout, or vice versa.
880 * \param cur_layout the current tile layout
881 * \param target_layout the desired tile layout
882 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
883 * \param new_layout_return returns the new layout mode
884 * \param convert_return returns TRUE if image conversion is needed
887 layout_logic(enum lp_texture_layout cur_layout
,
888 enum lp_texture_layout target_layout
,
889 enum lp_texture_usage usage
,
890 enum lp_texture_layout
*new_layout_return
,
893 enum lp_texture_layout other_layout
, new_layout
;
897 new_layout
= 99; /* debug check */
899 if (target_layout
== LP_TEX_LAYOUT_LINEAR
) {
900 other_layout
= LP_TEX_LAYOUT_TILED
;
903 assert(target_layout
== LP_TEX_LAYOUT_TILED
);
904 other_layout
= LP_TEX_LAYOUT_LINEAR
;
907 new_layout
= target_layout
; /* may get changed below */
909 if (cur_layout
== LP_TEX_LAYOUT_BOTH
) {
910 if (usage
== LP_TEX_USAGE_READ
) {
911 new_layout
= LP_TEX_LAYOUT_BOTH
;
914 else if (cur_layout
== other_layout
) {
915 if (usage
!= LP_TEX_USAGE_WRITE_ALL
) {
916 /* need to convert tiled data to linear or vice versa */
919 if (usage
== LP_TEX_USAGE_READ
)
920 new_layout
= LP_TEX_LAYOUT_BOTH
;
924 assert(cur_layout
== LP_TEX_LAYOUT_NONE
||
925 cur_layout
== target_layout
);
928 assert(new_layout
== LP_TEX_LAYOUT_BOTH
||
929 new_layout
== target_layout
);
931 *new_layout_return
= new_layout
;
936 * Return pointer to a 2D texture image/face/slice.
937 * No tiled/linear conversion is done.
940 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
941 unsigned face_slice
, unsigned level
,
942 enum lp_texture_layout layout
)
944 struct llvmpipe_texture_image
*img
;
947 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
948 img
= &lpr
->linear_img
;
949 offset
= lpr
->linear_mip_offsets
[level
];
952 assert (layout
== LP_TEX_LAYOUT_TILED
);
953 img
= &lpr
->tiled_img
;
954 offset
= lpr
->tiled_mip_offsets
[level
];
958 offset
+= face_slice
* tex_image_face_size(lpr
, level
, layout
);
960 return (ubyte
*) img
->data
+ offset
;
964 static INLINE
enum lp_texture_layout
965 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource
*lpr
,
966 unsigned face_slice
, unsigned level
,
967 unsigned x
, unsigned y
)
970 assert(resource_is_texture(&lpr
->base
));
971 assert(x
< lpr
->tiles_per_row
[level
]);
972 i
= face_slice
* lpr
->tiles_per_image
[level
]
973 + y
* lpr
->tiles_per_row
[level
] + x
;
974 return lpr
->layout
[level
][i
];
979 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource
*lpr
,
980 unsigned face_slice
, unsigned level
,
981 unsigned x
, unsigned y
,
982 enum lp_texture_layout layout
)
985 assert(resource_is_texture(&lpr
->base
));
986 assert(x
< lpr
->tiles_per_row
[level
]);
987 i
= face_slice
* lpr
->tiles_per_image
[level
]
988 + y
* lpr
->tiles_per_row
[level
] + x
;
989 lpr
->layout
[level
][i
] = layout
;
994 * Set the layout mode for all tiles in a particular image.
997 llvmpipe_set_texture_image_layout(struct llvmpipe_resource
*lpr
,
998 unsigned face_slice
, unsigned level
,
999 unsigned width_t
, unsigned height_t
,
1000 enum lp_texture_layout layout
)
1002 const unsigned start
= face_slice
* lpr
->tiles_per_image
[level
];
1005 for (i
= 0; i
< width_t
* height_t
; i
++) {
1006 lpr
->layout
[level
][start
+ i
] = layout
;
1012 * Allocate storage for a linear or tile texture image (all cube
1013 * faces and all 3D slices, all levels).
1016 alloc_image_data(struct llvmpipe_resource
*lpr
,
1017 enum lp_texture_layout layout
)
1019 uint alignment
= MAX2(16, util_cpu_caps
.cacheline
);
1024 assert(lpr
->base
.last_level
== 0);
1026 if (layout
== LP_TEX_LAYOUT_TILED
) {
1027 /* tiled data is stored in regular memory */
1028 for (level
= 0; level
<= lpr
->base
.last_level
; level
++) {
1029 uint buffer_size
= tex_image_size(lpr
, level
, layout
);
1030 lpr
->tiled_mip_offsets
[level
] = offset
;
1031 offset
+= align(buffer_size
, alignment
);
1033 lpr
->tiled_img
.data
= align_malloc(offset
, alignment
);
1034 if (lpr
->tiled_img
.data
) {
1035 memset(lpr
->tiled_img
.data
, 0, offset
);
1039 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
1041 /* we get the linear memory from the winsys, and it has
1042 * already been zeroed
1044 struct llvmpipe_screen
*screen
= llvmpipe_screen(lpr
->base
.screen
);
1045 struct sw_winsys
*winsys
= screen
->winsys
;
1047 lpr
->linear_img
.data
=
1048 winsys
->displaytarget_map(winsys
, lpr
->dt
,
1049 PIPE_TRANSFER_READ_WRITE
);
1052 /* not a display target - allocate regular memory */
1054 * Offset calculation for start of a specific mip/layer is always
1055 * offset = lpr->linear_mip_offsets[level] + lpr->img_stride[level] * layer
1057 for (level
= 0; level
<= lpr
->base
.last_level
; level
++) {
1058 uint buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1059 lpr
->linear_mip_offsets
[level
] = offset
;
1060 offset
+= align(buffer_size
, alignment
);
1062 lpr
->linear_img
.data
= align_malloc(offset
, alignment
);
1063 if (lpr
->linear_img
.data
) {
1064 memset(lpr
->linear_img
.data
, 0, offset
);
1073 * Return pointer to texture image data (either linear or tiled layout)
1074 * for a particular cube face or 3D texture slice.
1076 * \param face_slice the cube face or 3D slice of interest
1077 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
1078 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
1081 llvmpipe_get_texture_image(struct llvmpipe_resource
*lpr
,
1082 unsigned face_slice
, unsigned level
,
1083 enum lp_texture_usage usage
,
1084 enum lp_texture_layout layout
)
1087 * 'target' refers to the image which we're retrieving (either in
1088 * tiled or linear layout).
1089 * 'other' refers to the same image but in the other layout. (it may
1092 struct llvmpipe_texture_image
*target_img
;
1093 struct llvmpipe_texture_image
*other_img
;
1096 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
1097 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
1098 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
1099 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
1100 unsigned target_offset
, other_offset
;
1101 unsigned *target_off_ptr
, *other_off_ptr
;
1102 enum lp_texture_layout other_layout
;
1103 boolean only_allocate
;
1105 assert(layout
== LP_TEX_LAYOUT_NONE
||
1106 layout
== LP_TEX_LAYOUT_TILED
||
1107 layout
== LP_TEX_LAYOUT_LINEAR
);
1109 assert(usage
== LP_TEX_USAGE_READ
||
1110 usage
== LP_TEX_USAGE_READ_WRITE
||
1111 usage
== LP_TEX_USAGE_WRITE_ALL
);
1113 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
1114 if (layout
== LP_TEX_LAYOUT_NONE
) {
1115 only_allocate
= TRUE
;
1116 layout
= LP_TEX_LAYOUT_TILED
;
1119 only_allocate
= FALSE
;
1123 assert(lpr
->linear_img
.data
);
1126 /* which is target? which is other? */
1127 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
1128 target_img
= &lpr
->linear_img
;
1129 target_off_ptr
= lpr
->linear_mip_offsets
;
1130 other_img
= &lpr
->tiled_img
;
1131 other_off_ptr
= lpr
->tiled_mip_offsets
;
1132 other_layout
= LP_TEX_LAYOUT_TILED
;
1135 target_img
= &lpr
->tiled_img
;
1136 target_off_ptr
= lpr
->tiled_mip_offsets
;
1137 other_img
= &lpr
->linear_img
;
1138 other_off_ptr
= lpr
->linear_mip_offsets
;
1139 other_layout
= LP_TEX_LAYOUT_LINEAR
;
1142 target_data
= target_img
->data
;
1143 other_data
= other_img
->data
;
1146 /* allocate memory for the target image now */
1147 alloc_image_data(lpr
, layout
);
1148 target_data
= target_img
->data
;
1151 target_offset
= target_off_ptr
[level
];
1152 other_offset
= other_off_ptr
[level
];
1154 if (face_slice
> 0) {
1155 target_offset
+= face_slice
* tex_image_face_size(lpr
, level
, layout
);
1156 other_offset
+= face_slice
* tex_image_face_size(lpr
, level
, other_layout
);
1160 target_data
= (uint8_t *) target_data
+ target_offset
;
1163 other_data
= (uint8_t *) other_data
+ other_offset
;
1166 if (only_allocate
) {
1167 /* Just allocating tiled memory. Don't initialize it from the
1168 * linear data if it exists.
1174 /* may need to convert other data to the requested layout */
1175 enum lp_texture_layout new_layout
;
1178 /* loop over all image tiles, doing layout conversion where needed */
1179 for (y
= 0; y
< height_t
; y
++) {
1180 for (x
= 0; x
< width_t
; x
++) {
1181 enum lp_texture_layout cur_layout
=
1182 llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, x
, y
);
1185 layout_logic(cur_layout
, layout
, usage
, &new_layout
, &convert
);
1187 if (convert
&& other_data
&& target_data
) {
1188 if (layout
== LP_TEX_LAYOUT_TILED
) {
1189 lp_linear_to_tiled(other_data
, target_data
,
1190 x
* TILE_SIZE
, y
* TILE_SIZE
,
1191 TILE_SIZE
, TILE_SIZE
,
1193 lpr
->row_stride
[level
],
1194 lpr
->tiles_per_row
[level
]);
1197 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
1198 lp_tiled_to_linear(other_data
, target_data
,
1199 x
* TILE_SIZE
, y
* TILE_SIZE
,
1200 TILE_SIZE
, TILE_SIZE
,
1202 lpr
->row_stride
[level
],
1203 lpr
->tiles_per_row
[level
]);
1207 if (new_layout
!= cur_layout
)
1208 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, x
, y
,
1215 llvmpipe_set_texture_image_layout(lpr
, face_slice
, level
,
1216 width_t
, height_t
, layout
);
1224 * Return pointer to start of a texture image (1D, 2D, 3D, CUBE).
1225 * All cube faces and 3D slices will be converted to the requested
1227 * This is typically used when we're about to sample from a texture.
1230 llvmpipe_get_texture_image_all(struct llvmpipe_resource
*lpr
,
1232 enum lp_texture_usage usage
,
1233 enum lp_texture_layout layout
)
1235 const int slices
= lpr
->num_slices_faces
[level
];
1241 for (slice
= slices
- 1; slice
>= 0; slice
--) {
1242 map
= llvmpipe_get_texture_image(lpr
, slice
, level
, usage
, layout
);
1250 * Get pointer to a linear image (not the tile!) where the tile at (x,y)
1251 * is known to be in linear layout.
1252 * Conversion from tiled to linear will be done if necessary.
1253 * \return pointer to start of image/face (not the tile)
1256 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource
*lpr
,
1257 unsigned face_slice
, unsigned level
,
1258 enum lp_texture_usage usage
,
1259 unsigned x
, unsigned y
)
1261 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear_img
;
1262 enum lp_texture_layout cur_layout
, new_layout
;
1263 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1265 uint8_t *tiled_image
, *linear_image
;
1267 assert(resource_is_texture(&lpr
->base
));
1268 assert(x
% TILE_SIZE
== 0);
1269 assert(y
% TILE_SIZE
== 0);
1271 if (!linear_img
->data
) {
1272 /* allocate memory for the linear image now */
1273 alloc_image_data(lpr
, LP_TEX_LAYOUT_LINEAR
);
1276 /* compute address of the slice/face of the image that contains the tile */
1277 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1278 LP_TEX_LAYOUT_TILED
);
1279 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1280 LP_TEX_LAYOUT_LINEAR
);
1282 /* get current tile layout and determine if data conversion is needed */
1283 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1285 layout_logic(cur_layout
, LP_TEX_LAYOUT_LINEAR
, usage
,
1286 &new_layout
, &convert
);
1288 if (convert
&& tiled_image
&& linear_image
) {
1289 lp_tiled_to_linear(tiled_image
, linear_image
,
1290 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1291 lpr
->row_stride
[level
],
1292 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 return linear_image
;
1303 * Get pointer to tiled data for rendering.
1304 * \return pointer to the tiled data at the given tile position
1307 llvmpipe_get_texture_tile(struct llvmpipe_resource
*lpr
,
1308 unsigned face_slice
, unsigned level
,
1309 enum lp_texture_usage usage
,
1310 unsigned x
, unsigned y
)
1312 struct llvmpipe_texture_image
*tiled_img
= &lpr
->tiled_img
;
1313 enum lp_texture_layout cur_layout
, new_layout
;
1314 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1316 uint8_t *tiled_image
, *linear_image
;
1317 unsigned tile_offset
;
1319 assert(x
% TILE_SIZE
== 0);
1320 assert(y
% TILE_SIZE
== 0);
1322 if (!tiled_img
->data
) {
1323 /* allocate memory for the tiled image now */
1324 alloc_image_data(lpr
, LP_TEX_LAYOUT_TILED
);
1327 /* compute address of the slice/face of the image that contains the tile */
1328 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1329 LP_TEX_LAYOUT_TILED
);
1330 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1331 LP_TEX_LAYOUT_LINEAR
);
1333 /* get current tile layout and see if we need to convert the data */
1334 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1336 layout_logic(cur_layout
, LP_TEX_LAYOUT_TILED
, usage
, &new_layout
, &convert
);
1337 if (convert
&& linear_image
&& tiled_image
) {
1338 lp_linear_to_tiled(linear_image
, tiled_image
,
1339 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1340 lpr
->row_stride
[level
],
1341 lpr
->tiles_per_row
[level
]);
1347 if (new_layout
!= cur_layout
)
1348 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1350 /* compute, return address of the 64x64 tile */
1351 tile_offset
= (ty
* lpr
->tiles_per_row
[level
] + tx
)
1352 * TILE_SIZE
* TILE_SIZE
* 4;
1354 return (ubyte
*) tiled_image
+ tile_offset
;
1359 * Return size of resource in bytes
1362 llvmpipe_resource_size(const struct pipe_resource
*resource
)
1364 const struct llvmpipe_resource
*lpr
= llvmpipe_resource_const(resource
);
1365 unsigned lvl
, size
= 0;
1367 for (lvl
= 0; lvl
<= lpr
->base
.last_level
; lvl
++) {
1368 if (lpr
->linear_img
.data
)
1369 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_LINEAR
);
1371 if (lpr
->tiled_img
.data
)
1372 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_TILED
);
1381 llvmpipe_print_resources(void)
1383 struct llvmpipe_resource
*lpr
;
1384 unsigned n
= 0, total
= 0;
1386 debug_printf("LLVMPIPE: current resources:\n");
1387 foreach(lpr
, &resource_list
) {
1388 unsigned size
= llvmpipe_resource_size(&lpr
->base
);
1389 debug_printf("resource %u at %p, size %ux%ux%u: %u bytes, refcount %u\n",
1390 lpr
->id
, (void *) lpr
,
1391 lpr
->base
.width0
, lpr
->base
.height0
, lpr
->base
.depth0
,
1392 size
, lpr
->base
.reference
.count
);
1396 debug_printf("LLVMPIPE: total size of %u resources: %u\n", n
, total
);
1402 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
1405 /* init linked list for tracking resources */
1407 static boolean first_call
= TRUE
;
1409 memset(&resource_list
, 0, sizeof(resource_list
));
1410 make_empty_list(&resource_list
);
1416 screen
->resource_create
= llvmpipe_resource_create
;
1417 screen
->resource_destroy
= llvmpipe_resource_destroy
;
1418 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
1419 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
1420 screen
->can_create_resource
= llvmpipe_can_create_resource
;
1425 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
1427 pipe
->transfer_map
= llvmpipe_transfer_map
;
1428 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
1430 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
1431 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;
1433 pipe
->create_surface
= llvmpipe_create_surface
;
1434 pipe
->surface_destroy
= llvmpipe_surface_destroy
;