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 * Allocate storage for llvmpipe_texture::layout array.
65 * The number of elements is width_in_tiles * height_in_tiles.
67 static enum lp_texture_layout
*
68 alloc_layout_array(unsigned num_slices
, unsigned width
, unsigned height
)
70 const unsigned tx
= align(width
, TILE_SIZE
) / TILE_SIZE
;
71 const unsigned ty
= align(height
, TILE_SIZE
) / TILE_SIZE
;
73 assert(num_slices
* tx
* ty
> 0);
74 assert(LP_TEX_LAYOUT_NONE
== 0); /* calloc'ing LP_TEX_LAYOUT_NONE here */
76 return (enum lp_texture_layout
*)
77 CALLOC(num_slices
* tx
* ty
, sizeof(enum lp_texture_layout
));
83 * Conventional allocation path for non-display textures:
84 * Just compute row strides here. Storage is allocated on demand later.
87 llvmpipe_texture_layout(struct llvmpipe_screen
*screen
,
88 struct llvmpipe_resource
*lpr
,
91 struct pipe_resource
*pt
= &lpr
->base
;
93 unsigned width
= pt
->width0
;
94 unsigned height
= pt
->height0
;
95 unsigned depth
= pt
->depth0
;
96 uint64_t total_size
= 0;
97 unsigned layers
= pt
->array_size
;
99 assert(LP_MAX_TEXTURE_2D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
100 assert(LP_MAX_TEXTURE_3D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
102 for (level
= 0; level
<= pt
->last_level
; level
++) {
104 /* Row stride and image stride (for linear layout) */
106 unsigned alignment
, nblocksx
, nblocksy
, block_size
;
108 /* For non-compressed formats we need to align the texture size
109 * to the tile size to facilitate render-to-texture.
110 * XXX this blows up 1d/1d array textures by unreasonable
111 * amount (factor 64), probably should do something about it.
113 if (util_format_is_compressed(pt
->format
))
116 alignment
= TILE_SIZE
;
118 nblocksx
= util_format_get_nblocksx(pt
->format
,
119 align(width
, alignment
));
120 nblocksy
= util_format_get_nblocksy(pt
->format
,
121 align(height
, alignment
));
122 block_size
= util_format_get_blocksize(pt
->format
);
124 lpr
->row_stride
[level
] = align(nblocksx
* block_size
, 16);
126 /* if row_stride * height > LP_MAX_TEXTURE_SIZE */
127 if (lpr
->row_stride
[level
] > LP_MAX_TEXTURE_SIZE
/ nblocksy
) {
128 /* image too large */
132 lpr
->img_stride
[level
] = lpr
->row_stride
[level
] * nblocksy
;
135 /* Size of the image in tiles (for tiled layout) */
137 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
138 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
139 lpr
->tiles_per_row
[level
] = width_t
;
140 lpr
->tiles_per_image
[level
] = width_t
* height_t
;
143 /* Number of 3D image slices, cube faces or texture array layers */
147 if (lpr
->base
.target
== PIPE_TEXTURE_CUBE
)
149 else if (lpr
->base
.target
== PIPE_TEXTURE_3D
)
151 else if (lpr
->base
.target
== PIPE_TEXTURE_1D_ARRAY
||
152 lpr
->base
.target
== PIPE_TEXTURE_2D_ARRAY
)
157 lpr
->num_slices_faces
[level
] = num_slices
;
160 lpr
->layout
[level
] = alloc_layout_array(num_slices
, width
, height
);
161 if (!lpr
->layout
[level
]) {
167 /* if img_stride * num_slices_faces > LP_MAX_TEXTURE_SIZE */
168 if (lpr
->img_stride
[level
] >
169 LP_MAX_TEXTURE_SIZE
/ lpr
->num_slices_faces
[level
]) {
170 /* volume too large */
174 total_size
+= (uint64_t) lpr
->num_slices_faces
[level
]
175 * (uint64_t) lpr
->img_stride
[level
];
176 if (total_size
> LP_MAX_TEXTURE_SIZE
) {
180 /* Compute size of next mipmap level */
181 width
= u_minify(width
, 1);
182 height
= u_minify(height
, 1);
183 depth
= u_minify(depth
, 1);
189 for (level
= 0; level
<= pt
->last_level
; level
++) {
190 FREE(lpr
->layout
[level
]);
198 * Check the size of the texture specified by 'res'.
199 * \return TRUE if OK, FALSE if too large.
202 llvmpipe_can_create_resource(struct pipe_screen
*screen
,
203 const struct pipe_resource
*res
)
205 struct llvmpipe_resource lpr
;
206 memset(&lpr
, 0, sizeof(lpr
));
208 return llvmpipe_texture_layout(llvmpipe_screen(screen
), &lpr
, FALSE
);
213 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
214 struct llvmpipe_resource
*lpr
)
216 struct sw_winsys
*winsys
= screen
->winsys
;
218 /* Round up the surface size to a multiple of the tile size to
219 * avoid tile clipping.
221 const unsigned width
= MAX2(1, align(lpr
->base
.width0
, TILE_SIZE
));
222 const unsigned height
= MAX2(1, align(lpr
->base
.height0
, TILE_SIZE
));
223 const unsigned width_t
= width
/ TILE_SIZE
;
224 const unsigned height_t
= height
/ TILE_SIZE
;
226 lpr
->tiles_per_row
[0] = width_t
;
227 lpr
->tiles_per_image
[0] = width_t
* height_t
;
228 lpr
->num_slices_faces
[0] = 1;
229 lpr
->img_stride
[0] = 0;
231 lpr
->layout
[0] = alloc_layout_array(1, width
, height
);
232 if (!lpr
->layout
[0]) {
236 lpr
->dt
= winsys
->displaytarget_create(winsys
,
241 &lpr
->row_stride
[0] );
247 void *map
= winsys
->displaytarget_map(winsys
, lpr
->dt
,
248 PIPE_TRANSFER_WRITE
);
251 memset(map
, 0, height
* lpr
->row_stride
[0]);
253 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
260 static struct pipe_resource
*
261 llvmpipe_resource_create(struct pipe_screen
*_screen
,
262 const struct pipe_resource
*templat
)
264 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
265 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
269 lpr
->base
= *templat
;
270 pipe_reference_init(&lpr
->base
.reference
, 1);
271 lpr
->base
.screen
= &screen
->base
;
273 /* assert(lpr->base.bind); */
275 if (llvmpipe_resource_is_texture(&lpr
->base
)) {
276 if (lpr
->base
.bind
& (PIPE_BIND_DISPLAY_TARGET
|
279 /* displayable surface */
280 if (!llvmpipe_displaytarget_layout(screen
, lpr
))
282 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
286 if (!llvmpipe_texture_layout(screen
, lpr
, TRUE
))
288 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
290 assert(lpr
->layout
[0]);
293 /* other data (vertex buffer, const buffer, etc) */
294 const uint bytes
= templat
->width0
;
295 assert(util_format_get_blocksize(templat
->format
) == 1);
296 assert(templat
->height0
== 1);
297 assert(templat
->depth0
== 1);
298 assert(templat
->last_level
== 0);
299 lpr
->data
= align_malloc(bytes
, 16);
301 * buffers don't really have stride but it's probably safer
302 * (for code doing same calculations for buffers and textures)
303 * to put something sane in there.
305 lpr
->row_stride
[0] = bytes
;
308 memset(lpr
->data
, 0, bytes
);
311 lpr
->id
= id_counter
++;
314 insert_at_tail(&resource_list
, lpr
);
326 llvmpipe_resource_destroy(struct pipe_screen
*pscreen
,
327 struct pipe_resource
*pt
)
329 struct llvmpipe_screen
*screen
= llvmpipe_screen(pscreen
);
330 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
334 struct sw_winsys
*winsys
= screen
->winsys
;
335 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
337 if (lpr
->tiled_img
.data
) {
338 align_free(lpr
->tiled_img
.data
);
339 lpr
->tiled_img
.data
= NULL
;
342 FREE(lpr
->layout
[0]);
344 else if (llvmpipe_resource_is_texture(pt
)) {
345 /* regular texture */
348 /* free linear image data */
349 if (lpr
->linear_img
.data
) {
350 align_free(lpr
->linear_img
.data
);
351 lpr
->linear_img
.data
= NULL
;
354 /* free tiled image data */
355 if (lpr
->tiled_img
.data
) {
356 align_free(lpr
->tiled_img
.data
);
357 lpr
->tiled_img
.data
= NULL
;
360 /* free layout flag arrays */
361 for (level
= 0; level
< Elements(lpr
->layout
); level
++) {
362 FREE(lpr
->layout
[level
]);
363 lpr
->layout
[level
] = NULL
;
366 else if (!lpr
->userBuffer
) {
368 align_free(lpr
->data
);
373 remove_from_list(lpr
);
381 * Map a resource for read/write.
384 llvmpipe_resource_map(struct pipe_resource
*resource
,
387 enum lp_texture_usage tex_usage
,
388 enum lp_texture_layout layout
)
390 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
393 assert(level
< LP_MAX_TEXTURE_LEVELS
);
394 assert(layer
< (u_minify(resource
->depth0
, level
) + resource
->array_size
- 1));
396 assert(tex_usage
== LP_TEX_USAGE_READ
||
397 tex_usage
== LP_TEX_USAGE_READ_WRITE
||
398 tex_usage
== LP_TEX_USAGE_WRITE_ALL
);
400 assert(layout
== LP_TEX_LAYOUT_NONE
||
401 layout
== LP_TEX_LAYOUT_TILED
||
402 layout
== LP_TEX_LAYOUT_LINEAR
);
406 struct llvmpipe_screen
*screen
= llvmpipe_screen(resource
->screen
);
407 struct sw_winsys
*winsys
= screen
->winsys
;
411 if (tex_usage
== LP_TEX_USAGE_READ
) {
412 dt_usage
= PIPE_TRANSFER_READ
;
415 dt_usage
= PIPE_TRANSFER_READ_WRITE
;
421 /* FIXME: keep map count? */
422 map
= winsys
->displaytarget_map(winsys
, lpr
->dt
, dt_usage
);
424 /* install this linear image in texture data structure */
425 lpr
->linear_img
.data
= map
;
427 /* make sure tiled data gets converted to linear data */
428 map2
= llvmpipe_get_texture_image(lpr
, 0, 0, tex_usage
, layout
);
429 if (layout
== LP_TEX_LAYOUT_LINEAR
)
434 else if (llvmpipe_resource_is_texture(resource
)) {
436 map
= llvmpipe_get_texture_image(lpr
, layer
, level
,
450 llvmpipe_resource_unmap(struct pipe_resource
*resource
,
454 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
458 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(resource
->screen
);
459 struct sw_winsys
*winsys
= lp_screen
->winsys
;
464 /* make sure linear image is up to date */
465 (void) llvmpipe_get_texture_image(lpr
, layer
, level
,
467 LP_TEX_LAYOUT_LINEAR
);
469 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
475 llvmpipe_resource_data(struct pipe_resource
*resource
)
477 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
479 assert(!llvmpipe_resource_is_texture(resource
));
485 static struct pipe_resource
*
486 llvmpipe_resource_from_handle(struct pipe_screen
*screen
,
487 const struct pipe_resource
*template,
488 struct winsys_handle
*whandle
)
490 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
491 struct llvmpipe_resource
*lpr
;
492 unsigned width
, height
, width_t
, height_t
;
494 /* XXX Seems like from_handled depth textures doesn't work that well */
496 lpr
= CALLOC_STRUCT(llvmpipe_resource
);
501 lpr
->base
= *template;
502 pipe_reference_init(&lpr
->base
.reference
, 1);
503 lpr
->base
.screen
= screen
;
505 width
= align(lpr
->base
.width0
, TILE_SIZE
);
506 height
= align(lpr
->base
.height0
, TILE_SIZE
);
507 width_t
= width
/ TILE_SIZE
;
508 height_t
= height
/ TILE_SIZE
;
511 * Looks like unaligned displaytargets work just fine,
512 * at least sampler/render ones.
515 assert(lpr
->base
.width0
== width
);
516 assert(lpr
->base
.height0
== height
);
519 lpr
->tiles_per_row
[0] = width_t
;
520 lpr
->tiles_per_image
[0] = width_t
* height_t
;
521 lpr
->num_slices_faces
[0] = 1;
522 lpr
->img_stride
[0] = 0;
524 lpr
->dt
= winsys
->displaytarget_from_handle(winsys
,
527 &lpr
->row_stride
[0]);
532 lpr
->layout
[0] = alloc_layout_array(1, lpr
->base
.width0
, lpr
->base
.height0
);
533 if (!lpr
->layout
[0]) {
537 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
539 lpr
->id
= id_counter
++;
542 insert_at_tail(&resource_list
, lpr
);
548 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
557 llvmpipe_resource_get_handle(struct pipe_screen
*screen
,
558 struct pipe_resource
*pt
,
559 struct winsys_handle
*whandle
)
561 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
562 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
568 return winsys
->displaytarget_get_handle(winsys
, lpr
->dt
, whandle
);
572 static struct pipe_surface
*
573 llvmpipe_create_surface(struct pipe_context
*pipe
,
574 struct pipe_resource
*pt
,
575 const struct pipe_surface
*surf_tmpl
)
577 struct pipe_surface
*ps
;
579 assert(surf_tmpl
->u
.tex
.level
<= pt
->last_level
);
580 if (!(pt
->bind
& (PIPE_BIND_DEPTH_STENCIL
| PIPE_BIND_RENDER_TARGET
)))
581 debug_printf("Illegal surface creation without bind flag\n");
583 ps
= CALLOC_STRUCT(pipe_surface
);
585 pipe_reference_init(&ps
->reference
, 1);
586 pipe_resource_reference(&ps
->texture
, pt
);
588 ps
->format
= surf_tmpl
->format
;
589 if (llvmpipe_resource_is_texture(pt
)) {
590 assert(surf_tmpl
->u
.tex
.level
<= pt
->last_level
);
591 ps
->width
= u_minify(pt
->width0
, surf_tmpl
->u
.tex
.level
);
592 ps
->height
= u_minify(pt
->height0
, surf_tmpl
->u
.tex
.level
);
593 ps
->u
.tex
.level
= surf_tmpl
->u
.tex
.level
;
594 ps
->u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
595 ps
->u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
598 /* setting width as number of elements should get us correct renderbuffer width */
599 ps
->width
= surf_tmpl
->u
.buf
.last_element
- surf_tmpl
->u
.buf
.first_element
+ 1;
600 ps
->height
= pt
->height0
;
601 ps
->u
.buf
.first_element
= surf_tmpl
->u
.buf
.first_element
;
602 ps
->u
.buf
.last_element
= surf_tmpl
->u
.buf
.last_element
;
603 assert(ps
->u
.buf
.first_element
<= ps
->u
.buf
.last_element
);
604 assert(ps
->u
.buf
.last_element
< ps
->width
);
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
);
626 llvmpipe_transfer_map( struct pipe_context
*pipe
,
627 struct pipe_resource
*resource
,
630 const struct pipe_box
*box
,
631 struct pipe_transfer
**transfer
)
633 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
634 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
635 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
636 struct llvmpipe_transfer
*lpt
;
637 struct pipe_transfer
*pt
;
639 enum pipe_format format
;
640 enum lp_texture_usage tex_usage
;
644 assert(level
<= resource
->last_level
);
647 * Transfers, like other pipe operations, must happen in order, so flush the
648 * context if necessary.
650 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
651 boolean read_only
= !(usage
& PIPE_TRANSFER_WRITE
);
652 boolean do_not_block
= !!(usage
& PIPE_TRANSFER_DONTBLOCK
);
653 if (!llvmpipe_flush_resource(pipe
, resource
,
656 TRUE
, /* cpu_access */
660 * It would have blocked, but state tracker requested no to.
662 assert(do_not_block
);
667 /* Check if we're mapping the current constant buffer */
668 if ((usage
& PIPE_TRANSFER_WRITE
) &&
669 resource
== llvmpipe
->constants
[PIPE_SHADER_FRAGMENT
][0].buffer
) {
670 /* constants may have changed */
671 llvmpipe
->dirty
|= LP_NEW_CONSTANTS
;
674 lpt
= CALLOC_STRUCT(llvmpipe_transfer
);
678 pipe_resource_reference(&pt
->resource
, resource
);
681 pt
->stride
= lpr
->row_stride
[level
];
682 pt
->layer_stride
= lpr
->img_stride
[level
];
686 assert(level
< LP_MAX_TEXTURE_LEVELS
);
689 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
690 transfer->x, transfer->y, transfer->width, transfer->height,
691 transfer->texture->width0,
692 transfer->texture->height0,
696 if (usage
== PIPE_TRANSFER_READ
) {
697 tex_usage
= LP_TEX_USAGE_READ
;
701 tex_usage
= LP_TEX_USAGE_READ_WRITE
;
706 printf("transfer map tex %u mode %s\n", lpr
->id
, mode
);
709 format
= lpr
->base
.format
;
711 map
= llvmpipe_resource_map(resource
,
714 tex_usage
, LP_TEX_LAYOUT_LINEAR
);
717 /* May want to do different things here depending on read/write nature
720 if (usage
& PIPE_TRANSFER_WRITE
) {
721 /* Do something to notify sharing contexts of a texture change.
727 box
->y
/ util_format_get_blockheight(format
) * pt
->stride
+
728 box
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
735 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
736 struct pipe_transfer
*transfer
)
738 assert(transfer
->resource
);
740 llvmpipe_resource_unmap(transfer
->resource
,
744 /* Effectively do the texture_update work here - if texture images
745 * needed post-processing to put them into hardware layout, this is
746 * where it would happen. For llvmpipe, nothing to do.
748 assert (transfer
->resource
);
749 pipe_resource_reference(&transfer
->resource
, NULL
);
754 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
755 struct pipe_resource
*presource
,
758 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
761 * XXX checking only resources with the right bind flags
762 * is unsafe since with opengl state tracker we can end up
763 * with resources bound to places they weren't supposed to be
764 * (buffers bound as sampler views is one possibility here).
766 if (!(presource
->bind
& (PIPE_BIND_DEPTH_STENCIL
|
767 PIPE_BIND_RENDER_TARGET
|
768 PIPE_BIND_SAMPLER_VIEW
)))
769 return LP_UNREFERENCED
;
771 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
776 * Returns the largest possible alignment for a format in llvmpipe
779 llvmpipe_get_format_alignment( enum pipe_format format
)
781 const struct util_format_description
*desc
= util_format_description(format
);
786 for (i
= 0; i
< desc
->nr_channels
; ++i
) {
787 size
+= desc
->channel
[i
].size
;
792 if (!util_is_power_of_two(bytes
)) {
793 bytes
/= desc
->nr_channels
;
796 if (bytes
% 2 || bytes
< 1) {
805 * Create buffer which wraps user-space data.
807 struct pipe_resource
*
808 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
813 struct llvmpipe_resource
*buffer
;
815 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
819 pipe_reference_init(&buffer
->base
.reference
, 1);
820 buffer
->base
.screen
= screen
;
821 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
822 buffer
->base
.bind
= bind_flags
;
823 buffer
->base
.usage
= PIPE_USAGE_IMMUTABLE
;
824 buffer
->base
.flags
= 0;
825 buffer
->base
.width0
= bytes
;
826 buffer
->base
.height0
= 1;
827 buffer
->base
.depth0
= 1;
828 buffer
->base
.array_size
= 1;
829 buffer
->userBuffer
= TRUE
;
832 return &buffer
->base
;
837 * Compute size (in bytes) need to store a texture image / mipmap level,
838 * for just one cube face, one array layer or one 3D texture slice
841 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
842 enum lp_texture_layout layout
)
844 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
845 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
847 assert(layout
== LP_TEX_LAYOUT_TILED
||
848 layout
== LP_TEX_LAYOUT_LINEAR
);
850 if (layout
== LP_TEX_LAYOUT_TILED
) {
851 /* for tiled layout, force a 32bpp format */
852 const enum pipe_format format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
853 const unsigned block_size
= util_format_get_blocksize(format
);
854 const unsigned nblocksy
=
855 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
856 const unsigned nblocksx
=
857 util_format_get_nblocksx(format
, align(width
, TILE_SIZE
));
858 const unsigned buffer_size
= block_size
* nblocksy
* nblocksx
;
862 /* we already computed this */
863 return lpr
->img_stride
[level
];
869 * Compute size (in bytes) need to store a texture image / mipmap level,
870 * including all cube faces or 3D image slices
873 tex_image_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
874 enum lp_texture_layout layout
)
876 const unsigned buf_size
= tex_image_face_size(lpr
, level
, layout
);
877 return buf_size
* lpr
->num_slices_faces
[level
];
882 * This function encapsulates some complicated logic for determining
883 * how to convert a tile of image data from linear layout to tiled
884 * layout, or vice versa.
885 * \param cur_layout the current tile layout
886 * \param target_layout the desired tile layout
887 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
888 * \param new_layout_return returns the new layout mode
889 * \param convert_return returns TRUE if image conversion is needed
892 layout_logic(enum lp_texture_layout cur_layout
,
893 enum lp_texture_layout target_layout
,
894 enum lp_texture_usage usage
,
895 enum lp_texture_layout
*new_layout_return
,
898 enum lp_texture_layout other_layout
, new_layout
;
902 new_layout
= 99; /* debug check */
904 if (target_layout
== LP_TEX_LAYOUT_LINEAR
) {
905 other_layout
= LP_TEX_LAYOUT_TILED
;
908 assert(target_layout
== LP_TEX_LAYOUT_TILED
);
909 other_layout
= LP_TEX_LAYOUT_LINEAR
;
912 new_layout
= target_layout
; /* may get changed below */
914 if (cur_layout
== LP_TEX_LAYOUT_BOTH
) {
915 if (usage
== LP_TEX_USAGE_READ
) {
916 new_layout
= LP_TEX_LAYOUT_BOTH
;
919 else if (cur_layout
== other_layout
) {
920 if (usage
!= LP_TEX_USAGE_WRITE_ALL
) {
921 /* need to convert tiled data to linear or vice versa */
924 if (usage
== LP_TEX_USAGE_READ
)
925 new_layout
= LP_TEX_LAYOUT_BOTH
;
929 assert(cur_layout
== LP_TEX_LAYOUT_NONE
||
930 cur_layout
== target_layout
);
933 assert(new_layout
== LP_TEX_LAYOUT_BOTH
||
934 new_layout
== target_layout
);
936 *new_layout_return
= new_layout
;
941 * Return pointer to a 2D texture image/face/slice.
942 * No tiled/linear conversion is done.
945 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
946 unsigned face_slice
, unsigned level
,
947 enum lp_texture_layout layout
)
949 struct llvmpipe_texture_image
*img
;
952 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
953 img
= &lpr
->linear_img
;
954 offset
= lpr
->linear_mip_offsets
[level
];
957 assert (layout
== LP_TEX_LAYOUT_TILED
);
958 img
= &lpr
->tiled_img
;
959 offset
= lpr
->tiled_mip_offsets
[level
];
963 offset
+= face_slice
* tex_image_face_size(lpr
, level
, layout
);
965 return (ubyte
*) img
->data
+ offset
;
969 static INLINE
enum lp_texture_layout
970 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource
*lpr
,
971 unsigned face_slice
, unsigned level
,
972 unsigned x
, unsigned y
)
975 assert(llvmpipe_resource_is_texture(&lpr
->base
));
976 assert(x
< lpr
->tiles_per_row
[level
]);
977 i
= face_slice
* lpr
->tiles_per_image
[level
]
978 + y
* lpr
->tiles_per_row
[level
] + x
;
979 return lpr
->layout
[level
][i
];
984 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource
*lpr
,
985 unsigned face_slice
, unsigned level
,
986 unsigned x
, unsigned y
,
987 enum lp_texture_layout layout
)
990 assert(llvmpipe_resource_is_texture(&lpr
->base
));
991 assert(x
< lpr
->tiles_per_row
[level
]);
992 i
= face_slice
* lpr
->tiles_per_image
[level
]
993 + y
* lpr
->tiles_per_row
[level
] + x
;
994 lpr
->layout
[level
][i
] = layout
;
999 * Set the layout mode for all tiles in a particular image.
1002 llvmpipe_set_texture_image_layout(struct llvmpipe_resource
*lpr
,
1003 unsigned face_slice
, unsigned level
,
1004 unsigned width_t
, unsigned height_t
,
1005 enum lp_texture_layout layout
)
1007 const unsigned start
= face_slice
* lpr
->tiles_per_image
[level
];
1010 for (i
= 0; i
< width_t
* height_t
; i
++) {
1011 lpr
->layout
[level
][start
+ i
] = layout
;
1017 * Allocate storage for a linear or tile texture image (all cube
1018 * faces and all 3D slices, all levels).
1021 alloc_image_data(struct llvmpipe_resource
*lpr
,
1022 enum lp_texture_layout layout
)
1024 uint alignment
= MAX2(16, util_cpu_caps
.cacheline
);
1029 assert(lpr
->base
.last_level
== 0);
1031 if (layout
== LP_TEX_LAYOUT_TILED
) {
1032 /* tiled data is stored in regular memory */
1033 for (level
= 0; level
<= lpr
->base
.last_level
; level
++) {
1034 uint buffer_size
= tex_image_size(lpr
, level
, layout
);
1035 lpr
->tiled_mip_offsets
[level
] = offset
;
1036 offset
+= align(buffer_size
, alignment
);
1038 lpr
->tiled_img
.data
= align_malloc(offset
, alignment
);
1039 if (lpr
->tiled_img
.data
) {
1040 memset(lpr
->tiled_img
.data
, 0, offset
);
1044 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
1046 /* we get the linear memory from the winsys, and it has
1047 * already been zeroed
1049 struct llvmpipe_screen
*screen
= llvmpipe_screen(lpr
->base
.screen
);
1050 struct sw_winsys
*winsys
= screen
->winsys
;
1052 lpr
->linear_img
.data
=
1053 winsys
->displaytarget_map(winsys
, lpr
->dt
,
1054 PIPE_TRANSFER_READ_WRITE
);
1057 /* not a display target - allocate regular memory */
1059 * Offset calculation for start of a specific mip/layer is always
1060 * offset = lpr->linear_mip_offsets[level] + lpr->img_stride[level] * layer
1062 for (level
= 0; level
<= lpr
->base
.last_level
; level
++) {
1063 uint buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1064 lpr
->linear_mip_offsets
[level
] = offset
;
1065 offset
+= align(buffer_size
, alignment
);
1067 lpr
->linear_img
.data
= align_malloc(offset
, alignment
);
1068 if (lpr
->linear_img
.data
) {
1069 memset(lpr
->linear_img
.data
, 0, offset
);
1078 * Return pointer to texture image data (either linear or tiled layout)
1079 * for a particular cube face or 3D texture slice.
1081 * \param face_slice the cube face or 3D slice of interest
1082 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
1083 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
1086 llvmpipe_get_texture_image(struct llvmpipe_resource
*lpr
,
1087 unsigned face_slice
, unsigned level
,
1088 enum lp_texture_usage usage
,
1089 enum lp_texture_layout layout
)
1092 * 'target' refers to the image which we're retrieving (either in
1093 * tiled or linear layout).
1094 * 'other' refers to the same image but in the other layout. (it may
1097 struct llvmpipe_texture_image
*target_img
;
1098 struct llvmpipe_texture_image
*other_img
;
1101 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
1102 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
1103 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
1104 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
1105 unsigned target_offset
, other_offset
;
1106 unsigned *target_off_ptr
, *other_off_ptr
;
1107 enum lp_texture_layout other_layout
;
1108 boolean only_allocate
;
1110 assert(layout
== LP_TEX_LAYOUT_NONE
||
1111 layout
== LP_TEX_LAYOUT_TILED
||
1112 layout
== LP_TEX_LAYOUT_LINEAR
);
1114 assert(usage
== LP_TEX_USAGE_READ
||
1115 usage
== LP_TEX_USAGE_READ_WRITE
||
1116 usage
== LP_TEX_USAGE_WRITE_ALL
);
1118 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
1119 if (layout
== LP_TEX_LAYOUT_NONE
) {
1120 only_allocate
= TRUE
;
1121 layout
= LP_TEX_LAYOUT_TILED
;
1124 only_allocate
= FALSE
;
1128 assert(lpr
->linear_img
.data
);
1131 /* which is target? which is other? */
1132 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
1133 target_img
= &lpr
->linear_img
;
1134 target_off_ptr
= lpr
->linear_mip_offsets
;
1135 other_img
= &lpr
->tiled_img
;
1136 other_off_ptr
= lpr
->tiled_mip_offsets
;
1137 other_layout
= LP_TEX_LAYOUT_TILED
;
1140 target_img
= &lpr
->tiled_img
;
1141 target_off_ptr
= lpr
->tiled_mip_offsets
;
1142 other_img
= &lpr
->linear_img
;
1143 other_off_ptr
= lpr
->linear_mip_offsets
;
1144 other_layout
= LP_TEX_LAYOUT_LINEAR
;
1147 target_data
= target_img
->data
;
1148 other_data
= other_img
->data
;
1151 /* allocate memory for the target image now */
1152 alloc_image_data(lpr
, layout
);
1153 target_data
= target_img
->data
;
1156 target_offset
= target_off_ptr
[level
];
1157 other_offset
= other_off_ptr
[level
];
1159 if (face_slice
> 0) {
1160 target_offset
+= face_slice
* tex_image_face_size(lpr
, level
, layout
);
1161 other_offset
+= face_slice
* tex_image_face_size(lpr
, level
, other_layout
);
1165 target_data
= (uint8_t *) target_data
+ target_offset
;
1168 other_data
= (uint8_t *) other_data
+ other_offset
;
1171 if (only_allocate
) {
1172 /* Just allocating tiled memory. Don't initialize it from the
1173 * linear data if it exists.
1179 /* may need to convert other data to the requested layout */
1180 enum lp_texture_layout new_layout
;
1183 /* loop over all image tiles, doing layout conversion where needed */
1184 for (y
= 0; y
< height_t
; y
++) {
1185 for (x
= 0; x
< width_t
; x
++) {
1186 enum lp_texture_layout cur_layout
=
1187 llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, x
, y
);
1190 layout_logic(cur_layout
, layout
, usage
, &new_layout
, &convert
);
1192 if (convert
&& other_data
&& target_data
) {
1193 if (layout
== LP_TEX_LAYOUT_TILED
) {
1194 lp_linear_to_tiled(other_data
, target_data
,
1195 x
* TILE_SIZE
, y
* TILE_SIZE
,
1196 TILE_SIZE
, TILE_SIZE
,
1198 lpr
->row_stride
[level
],
1199 lpr
->tiles_per_row
[level
]);
1202 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
1203 lp_tiled_to_linear(other_data
, target_data
,
1204 x
* TILE_SIZE
, y
* TILE_SIZE
,
1205 TILE_SIZE
, TILE_SIZE
,
1207 lpr
->row_stride
[level
],
1208 lpr
->tiles_per_row
[level
]);
1212 if (new_layout
!= cur_layout
)
1213 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, x
, y
,
1220 llvmpipe_set_texture_image_layout(lpr
, face_slice
, level
,
1221 width_t
, height_t
, layout
);
1229 * Return pointer to start of a texture image (1D, 2D, 3D, CUBE).
1230 * All cube faces and 3D slices will be converted to the requested
1232 * This is typically used when we're about to sample from a texture.
1235 llvmpipe_get_texture_image_all(struct llvmpipe_resource
*lpr
,
1237 enum lp_texture_usage usage
,
1238 enum lp_texture_layout layout
)
1240 const int slices
= lpr
->num_slices_faces
[level
];
1246 for (slice
= slices
- 1; slice
>= 0; slice
--) {
1247 map
= llvmpipe_get_texture_image(lpr
, slice
, level
, usage
, layout
);
1255 * Get pointer to a linear image (not the tile!) where the tile at (x,y)
1256 * is known to be in linear layout.
1257 * Conversion from tiled to linear will be done if necessary.
1258 * \return pointer to start of image/face (not the tile)
1261 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource
*lpr
,
1262 unsigned face_slice
, unsigned level
,
1263 enum lp_texture_usage usage
,
1264 unsigned x
, unsigned y
)
1266 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear_img
;
1267 enum lp_texture_layout cur_layout
, new_layout
;
1268 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1270 uint8_t *tiled_image
, *linear_image
;
1272 assert(llvmpipe_resource_is_texture(&lpr
->base
));
1273 assert(x
% TILE_SIZE
== 0);
1274 assert(y
% TILE_SIZE
== 0);
1276 if (!linear_img
->data
) {
1277 /* allocate memory for the linear image now */
1278 alloc_image_data(lpr
, LP_TEX_LAYOUT_LINEAR
);
1281 /* compute address of the slice/face of the image that contains the tile */
1282 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1283 LP_TEX_LAYOUT_TILED
);
1284 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1285 LP_TEX_LAYOUT_LINEAR
);
1287 /* get current tile layout and determine if data conversion is needed */
1288 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1290 layout_logic(cur_layout
, LP_TEX_LAYOUT_LINEAR
, usage
,
1291 &new_layout
, &convert
);
1293 if (convert
&& tiled_image
&& linear_image
) {
1294 lp_tiled_to_linear(tiled_image
, linear_image
,
1295 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1296 lpr
->row_stride
[level
],
1297 lpr
->tiles_per_row
[level
]);
1300 if (new_layout
!= cur_layout
)
1301 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1303 return linear_image
;
1308 * Get pointer to tiled data for rendering.
1309 * \return pointer to the tiled data at the given tile position
1312 llvmpipe_get_texture_tile(struct llvmpipe_resource
*lpr
,
1313 unsigned face_slice
, unsigned level
,
1314 enum lp_texture_usage usage
,
1315 unsigned x
, unsigned y
)
1317 struct llvmpipe_texture_image
*tiled_img
= &lpr
->tiled_img
;
1318 enum lp_texture_layout cur_layout
, new_layout
;
1319 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1321 uint8_t *tiled_image
, *linear_image
;
1322 unsigned tile_offset
;
1324 assert(x
% TILE_SIZE
== 0);
1325 assert(y
% TILE_SIZE
== 0);
1327 if (!tiled_img
->data
) {
1328 /* allocate memory for the tiled image now */
1329 alloc_image_data(lpr
, LP_TEX_LAYOUT_TILED
);
1332 /* compute address of the slice/face of the image that contains the tile */
1333 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1334 LP_TEX_LAYOUT_TILED
);
1335 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1336 LP_TEX_LAYOUT_LINEAR
);
1338 /* get current tile layout and see if we need to convert the data */
1339 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1341 layout_logic(cur_layout
, LP_TEX_LAYOUT_TILED
, usage
, &new_layout
, &convert
);
1342 if (convert
&& linear_image
&& tiled_image
) {
1343 lp_linear_to_tiled(linear_image
, tiled_image
,
1344 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1345 lpr
->row_stride
[level
],
1346 lpr
->tiles_per_row
[level
]);
1352 if (new_layout
!= cur_layout
)
1353 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1355 /* compute, return address of the 64x64 tile */
1356 tile_offset
= (ty
* lpr
->tiles_per_row
[level
] + tx
)
1357 * TILE_SIZE
* TILE_SIZE
* 4;
1359 return (ubyte
*) tiled_image
+ tile_offset
;
1364 * Return size of resource in bytes
1367 llvmpipe_resource_size(const struct pipe_resource
*resource
)
1369 const struct llvmpipe_resource
*lpr
= llvmpipe_resource_const(resource
);
1370 unsigned lvl
, size
= 0;
1372 if (llvmpipe_resource_is_texture(resource
)) {
1373 for (lvl
= 0; lvl
<= lpr
->base
.last_level
; lvl
++) {
1374 if (lpr
->linear_img
.data
)
1375 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_LINEAR
);
1377 if (lpr
->tiled_img
.data
)
1378 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_TILED
);
1382 size
= resource
->width0
;
1391 llvmpipe_print_resources(void)
1393 struct llvmpipe_resource
*lpr
;
1394 unsigned n
= 0, total
= 0;
1396 debug_printf("LLVMPIPE: current resources:\n");
1397 foreach(lpr
, &resource_list
) {
1398 unsigned size
= llvmpipe_resource_size(&lpr
->base
);
1399 debug_printf("resource %u at %p, size %ux%ux%u: %u bytes, refcount %u\n",
1400 lpr
->id
, (void *) lpr
,
1401 lpr
->base
.width0
, lpr
->base
.height0
, lpr
->base
.depth0
,
1402 size
, lpr
->base
.reference
.count
);
1406 debug_printf("LLVMPIPE: total size of %u resources: %u\n", n
, total
);
1412 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
1415 /* init linked list for tracking resources */
1417 static boolean first_call
= TRUE
;
1419 memset(&resource_list
, 0, sizeof(resource_list
));
1420 make_empty_list(&resource_list
);
1426 screen
->resource_create
= llvmpipe_resource_create
;
1427 screen
->resource_destroy
= llvmpipe_resource_destroy
;
1428 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
1429 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
1430 screen
->can_create_resource
= llvmpipe_can_create_resource
;
1435 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
1437 pipe
->transfer_map
= llvmpipe_transfer_map
;
1438 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
1440 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
1441 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;
1443 pipe
->create_surface
= llvmpipe_create_surface
;
1444 pipe
->surface_destroy
= llvmpipe_surface_destroy
;