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_format.h"
40 #include "util/u_math.h"
41 #include "util/u_memory.h"
42 #include "util/u_transfer.h"
44 #include "lp_context.h"
46 #include "lp_screen.h"
47 #include "lp_tile_image.h"
48 #include "lp_texture.h"
50 #include "lp_tile_size.h"
52 #include "state_tracker/sw_winsys.h"
56 resource_is_texture(const struct pipe_resource
*resource
)
58 const unsigned tex_binds
= (PIPE_BIND_DISPLAY_TARGET
|
61 PIPE_BIND_DEPTH_STENCIL
|
62 PIPE_BIND_SAMPLER_VIEW
);
63 const struct llvmpipe_resource
*lpr
= llvmpipe_resource_const(resource
);
65 return (lpr
->base
.bind
& tex_binds
) ? TRUE
: FALSE
;
71 * Allocate storage for llvmpipe_texture::layout array.
72 * The number of elements is width_in_tiles * height_in_tiles.
74 static enum lp_texture_layout
*
75 alloc_layout_array(unsigned width
, unsigned height
)
77 const unsigned tx
= align(width
, TILE_SIZE
) / TILE_SIZE
;
78 const unsigned ty
= align(height
, TILE_SIZE
) / TILE_SIZE
;
81 assert(LP_TEX_LAYOUT_NONE
== 0); /* calloc'ing LP_TEX_LAYOUT_NONE here */
83 return (enum lp_texture_layout
*)
84 calloc(tx
* ty
, sizeof(enum lp_texture_layout
));
90 * Conventional allocation path for non-display textures:
91 * Just compute row strides here. Storage is allocated on demand later.
94 llvmpipe_texture_layout(struct llvmpipe_screen
*screen
,
95 struct llvmpipe_resource
*lpr
)
97 struct pipe_resource
*pt
= &lpr
->base
;
99 unsigned width
= pt
->width0
;
100 unsigned height
= pt
->height0
;
102 assert(LP_MAX_TEXTURE_2D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
103 assert(LP_MAX_TEXTURE_3D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
105 for (level
= 0; level
<= pt
->last_level
; level
++) {
106 const unsigned num_faces
= lpr
->base
.target
== PIPE_TEXTURE_CUBE
? 6 : 1;
107 unsigned nblocksx
, face
;
109 /* Allocate storage for whole quads. This is particularly important
110 * for depth surfaces, which are currently stored in a swizzled format.
112 nblocksx
= util_format_get_nblocksx(pt
->format
, align(width
, TILE_SIZE
));
115 align(nblocksx
* util_format_get_blocksize(pt
->format
), 16);
117 lpr
->tiles_per_row
[level
] = align(width
, TILE_SIZE
) / TILE_SIZE
;
119 for (face
= 0; face
< num_faces
; face
++) {
120 lpr
->layout
[face
][level
] = alloc_layout_array(width
, height
);
123 width
= u_minify(width
, 1);
124 height
= u_minify(height
, 1);
133 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
134 struct llvmpipe_resource
*lpr
)
136 struct sw_winsys
*winsys
= screen
->winsys
;
138 /* Round up the surface size to a multiple of the tile size to
139 * avoid tile clipping.
141 unsigned width
= align(lpr
->base
.width0
, TILE_SIZE
);
142 unsigned height
= align(lpr
->base
.height0
, TILE_SIZE
);
144 lpr
->tiles_per_row
[0] = align(width
, TILE_SIZE
) / TILE_SIZE
;
146 lpr
->layout
[0][0] = alloc_layout_array(width
, height
);
148 lpr
->dt
= winsys
->displaytarget_create(winsys
,
155 return lpr
->dt
!= NULL
;
159 static struct pipe_resource
*
160 llvmpipe_resource_create(struct pipe_screen
*_screen
,
161 const struct pipe_resource
*templat
)
163 static unsigned id_counter
= 0;
164 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
165 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
169 lpr
->base
= *templat
;
170 pipe_reference_init(&lpr
->base
.reference
, 1);
171 lpr
->base
.screen
= &screen
->base
;
173 assert(lpr
->base
.bind
);
175 if (lpr
->base
.bind
& (PIPE_BIND_DISPLAY_TARGET
|
178 /* displayable surface */
179 if (!llvmpipe_displaytarget_layout(screen
, lpr
))
181 assert(lpr
->layout
[0][0][0] == LP_TEX_LAYOUT_NONE
);
183 else if (lpr
->base
.bind
& (PIPE_BIND_SAMPLER_VIEW
|
184 PIPE_BIND_DEPTH_STENCIL
)) {
186 if (!llvmpipe_texture_layout(screen
, lpr
))
188 assert(lpr
->layout
[0][0][0] == LP_TEX_LAYOUT_NONE
);
191 /* other data (vertex buffer, const buffer, etc) */
192 const enum pipe_format format
= templat
->format
;
193 const uint w
= templat
->width0
/ util_format_get_blockheight(format
);
194 const uint h
= templat
->height0
/ util_format_get_blockwidth(format
);
195 const uint d
= templat
->depth0
;
196 const uint bpp
= util_format_get_blocksize(format
);
197 const uint bytes
= w
* h
* d
* bpp
;
198 lpr
->data
= align_malloc(bytes
, 16);
203 if (resource_is_texture(&lpr
->base
)) {
204 assert(lpr
->layout
[0][0]);
207 lpr
->id
= id_counter
++;
218 llvmpipe_resource_destroy(struct pipe_screen
*pscreen
,
219 struct pipe_resource
*pt
)
221 struct llvmpipe_screen
*screen
= llvmpipe_screen(pscreen
);
222 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
226 struct sw_winsys
*winsys
= screen
->winsys
;
227 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
229 else if (resource_is_texture(pt
)) {
230 /* regular texture */
231 const uint num_faces
= pt
->target
== PIPE_TEXTURE_CUBE
? 6 : 1;
234 /* free linear image data */
235 for (level
= 0; level
< Elements(lpr
->linear
); level
++) {
236 if (lpr
->linear
[level
].data
) {
237 align_free(lpr
->linear
[level
].data
);
238 lpr
->linear
[level
].data
= NULL
;
242 /* free tiled image data */
243 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
244 if (lpr
->tiled
[level
].data
) {
245 align_free(lpr
->tiled
[level
].data
);
246 lpr
->tiled
[level
].data
= NULL
;
250 /* free layout flag arrays */
251 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
252 for (face
= 0; face
< num_faces
; face
++) {
253 free(lpr
->layout
[face
][level
]);
254 lpr
->layout
[face
][level
] = NULL
;
258 else if (!lpr
->userBuffer
) {
260 align_free(lpr
->data
);
268 * Map a resource for read/write.
271 llvmpipe_resource_map(struct pipe_resource
*resource
,
275 enum lp_texture_usage tex_usage
,
276 enum lp_texture_layout layout
)
278 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
282 assert(level
< LP_MAX_TEXTURE_LEVELS
);
284 assert(tex_usage
== LP_TEX_USAGE_READ
||
285 tex_usage
== LP_TEX_USAGE_READ_WRITE
||
286 tex_usage
== LP_TEX_USAGE_WRITE_ALL
);
288 assert(layout
== LP_TEX_LAYOUT_NONE
||
289 layout
== LP_TEX_LAYOUT_TILED
||
290 layout
== LP_TEX_LAYOUT_LINEAR
);
294 struct llvmpipe_screen
*screen
= llvmpipe_screen(resource
->screen
);
295 struct sw_winsys
*winsys
= screen
->winsys
;
298 if (tex_usage
== LP_TEX_USAGE_READ
) {
299 dt_usage
= PIPE_TRANSFER_READ
;
302 dt_usage
= PIPE_TRANSFER_READ_WRITE
;
309 /* FIXME: keep map count? */
310 map
= winsys
->displaytarget_map(winsys
, lpr
->dt
, dt_usage
);
312 /* install this linear image in texture data structure */
313 lpr
->linear
[level
].data
= map
;
315 map
= llvmpipe_get_texture_image(lpr
, face
, level
, tex_usage
, layout
);
320 else if (resource_is_texture(resource
)) {
321 /* regular texture */
322 const unsigned tex_height
= u_minify(resource
->height0
, level
);
323 const unsigned nblocksy
=
324 util_format_get_nblocksy(resource
->format
, tex_height
);
325 const unsigned stride
= lpr
->stride
[level
];
328 if (resource
->target
== PIPE_TEXTURE_CUBE
) {
330 offset = face * nblocksy * stride;
333 else if (resource
->target
== PIPE_TEXTURE_3D
) {
334 offset
= zslice
* nblocksy
* stride
;
342 map
= llvmpipe_get_texture_image(lpr
, face
, level
, tex_usage
, layout
);
357 llvmpipe_resource_unmap(struct pipe_resource
*resource
,
362 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
366 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(resource
->screen
);
367 struct sw_winsys
*winsys
= lp_screen
->winsys
;
373 /* make sure linear image is up to date */
374 (void) llvmpipe_get_texture_image(lpr
, 0, 0,
376 LP_TEX_LAYOUT_LINEAR
);
378 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
384 llvmpipe_resource_data(struct pipe_resource
*resource
)
386 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
388 assert((lpr
->base
.bind
& (PIPE_BIND_DISPLAY_TARGET
|
391 PIPE_BIND_SAMPLER_VIEW
)) == 0);
397 static struct pipe_resource
*
398 llvmpipe_resource_from_handle(struct pipe_screen
*screen
,
399 const struct pipe_resource
*template,
400 struct winsys_handle
*whandle
)
402 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
403 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
407 lpr
->base
= *template;
408 pipe_reference_init(&lpr
->base
.reference
, 1);
409 lpr
->base
.screen
= screen
;
411 lpr
->dt
= winsys
->displaytarget_from_handle(winsys
,
427 llvmpipe_resource_get_handle(struct pipe_screen
*screen
,
428 struct pipe_resource
*pt
,
429 struct winsys_handle
*whandle
)
431 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
432 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
438 return winsys
->displaytarget_get_handle(winsys
, lpr
->dt
, whandle
);
442 static struct pipe_surface
*
443 llvmpipe_get_tex_surface(struct pipe_screen
*screen
,
444 struct pipe_resource
*pt
,
445 unsigned face
, unsigned level
, unsigned zslice
,
446 enum lp_texture_usage usage
)
448 struct pipe_surface
*ps
;
450 assert(level
<= pt
->last_level
);
452 ps
= CALLOC_STRUCT(pipe_surface
);
454 pipe_reference_init(&ps
->reference
, 1);
455 pipe_resource_reference(&ps
->texture
, pt
);
456 ps
->format
= pt
->format
;
457 ps
->width
= u_minify(pt
->width0
, level
);
458 ps
->height
= u_minify(pt
->height0
, level
);
470 llvmpipe_tex_surface_destroy(struct pipe_surface
*surf
)
472 /* Effectively do the texture_update work here - if texture images
473 * needed post-processing to put them into hardware layout, this is
474 * where it would happen. For llvmpipe, nothing to do.
476 assert(surf
->texture
);
477 pipe_resource_reference(&surf
->texture
, NULL
);
482 static struct pipe_transfer
*
483 llvmpipe_get_transfer(struct pipe_context
*pipe
,
484 struct pipe_resource
*resource
,
485 struct pipe_subresource sr
,
487 const struct pipe_box
*box
)
489 struct llvmpipe_resource
*lprex
= llvmpipe_resource(resource
);
490 struct llvmpipe_transfer
*lpr
;
493 assert(sr
.level
<= resource
->last_level
);
495 lpr
= CALLOC_STRUCT(llvmpipe_transfer
);
497 struct pipe_transfer
*pt
= &lpr
->base
;
498 pipe_resource_reference(&pt
->resource
, resource
);
501 pt
->stride
= lprex
->stride
[sr
.level
];
511 llvmpipe_transfer_destroy(struct pipe_context
*pipe
,
512 struct pipe_transfer
*transfer
)
514 /* Effectively do the texture_update work here - if texture images
515 * needed post-processing to put them into hardware layout, this is
516 * where it would happen. For llvmpipe, nothing to do.
518 assert (transfer
->resource
);
519 pipe_resource_reference(&transfer
->resource
, NULL
);
525 llvmpipe_transfer_map( struct pipe_context
*pipe
,
526 struct pipe_transfer
*transfer
)
528 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
530 struct llvmpipe_resource
*lpr
;
531 enum pipe_format format
;
532 enum lp_texture_usage tex_usage
;
535 assert(transfer
->sr
.face
< 6);
536 assert(transfer
->sr
.level
< LP_MAX_TEXTURE_LEVELS
);
539 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
540 transfer->x, transfer->y, transfer->width, transfer->height,
541 transfer->texture->width0,
542 transfer->texture->height0,
546 if (transfer
->usage
== PIPE_TRANSFER_READ
) {
547 tex_usage
= LP_TEX_USAGE_READ
;
551 tex_usage
= LP_TEX_USAGE_READ_WRITE
;
556 struct llvmpipe_resource
*lpr
= llvmpipe_resource(transfer
->resource
);
557 printf("transfer map tex %u mode %s\n", lpr
->id
, mode
);
561 assert(transfer
->resource
);
562 lpr
= llvmpipe_resource(transfer
->resource
);
563 format
= lpr
->base
.format
;
566 * Transfers, like other pipe operations, must happen in order, so flush the
567 * context if necessary.
569 llvmpipe_flush_texture(pipe
,
574 !(transfer
->usage
& PIPE_TRANSFER_WRITE
), /* read_only */
575 TRUE
, /* cpu_access */
576 FALSE
); /* do_not_flush */
578 map
= llvmpipe_resource_map(transfer
->resource
,
582 tex_usage
, LP_TEX_LAYOUT_LINEAR
);
585 /* May want to do different things here depending on read/write nature
588 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
589 /* Do something to notify sharing contexts of a texture change.
595 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
596 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
603 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
604 struct pipe_transfer
*transfer
)
606 assert(transfer
->resource
);
608 llvmpipe_resource_unmap(transfer
->resource
,
615 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
616 struct pipe_resource
*presource
,
617 unsigned face
, unsigned level
)
619 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
621 if (presource
->target
== PIPE_BUFFER
)
622 return PIPE_UNREFERENCED
;
624 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
630 * Create buffer which wraps user-space data.
632 static struct pipe_resource
*
633 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
638 struct llvmpipe_resource
*buffer
;
640 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
644 pipe_reference_init(&buffer
->base
.reference
, 1);
645 buffer
->base
.screen
= screen
;
646 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
647 buffer
->base
.bind
= bind_flags
;
648 buffer
->base
._usage
= PIPE_USAGE_IMMUTABLE
;
649 buffer
->base
.flags
= 0;
650 buffer
->base
.width0
= bytes
;
651 buffer
->base
.height0
= 1;
652 buffer
->base
.depth0
= 1;
653 buffer
->userBuffer
= TRUE
;
656 return &buffer
->base
;
661 * Compute size (in bytes) need to store a texture image / mipmap level,
662 * for just one cube face.
665 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
666 enum lp_texture_layout layout
)
668 /* for tiled layout, force a 32bpp format */
669 enum pipe_format format
= layout
== LP_TEX_LAYOUT_TILED
670 ? PIPE_FORMAT_B8G8R8A8_UNORM
: lpr
->base
.format
;
671 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
672 const unsigned depth
= u_minify(lpr
->base
.depth0
, level
);
673 const unsigned nblocksy
=
674 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
675 const unsigned buffer_size
=
676 nblocksy
* lpr
->stride
[level
] *
677 (lpr
->base
.target
== PIPE_TEXTURE_3D
? depth
: 1);
683 * Compute size (in bytes) need to store a texture image / mipmap level,
684 * including all cube faces.
687 tex_image_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
688 enum lp_texture_layout layout
)
690 const unsigned buf_size
= tex_image_face_size(lpr
, level
, layout
);
691 const unsigned num_faces
= lpr
->base
.target
== PIPE_TEXTURE_CUBE
? 6 : 1;
692 return buf_size
* num_faces
;
697 * This function encapsulates some complicated logic for determining
698 * how to convert a tile of image data from linear layout to tiled
699 * layout, or vice versa.
700 * \param cur_layout the current tile layout
701 * \param target_layout the desired tile layout
702 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
703 * \param new_layout_return returns the new layout mode
704 * \param convert_return returns TRUE if image conversion is needed
707 layout_logic(enum lp_texture_layout cur_layout
,
708 enum lp_texture_layout target_layout
,
709 enum lp_texture_usage usage
,
710 enum lp_texture_layout
*new_layout_return
,
713 enum lp_texture_layout other_layout
, new_layout
;
717 new_layout
= 99; /* debug check */
719 if (target_layout
== LP_TEX_LAYOUT_LINEAR
) {
720 other_layout
= LP_TEX_LAYOUT_TILED
;
723 assert(target_layout
== LP_TEX_LAYOUT_TILED
);
724 other_layout
= LP_TEX_LAYOUT_LINEAR
;
727 new_layout
= target_layout
; /* may get changed below */
729 if (cur_layout
== LP_TEX_LAYOUT_BOTH
) {
730 if (usage
== LP_TEX_USAGE_READ
) {
731 new_layout
= LP_TEX_LAYOUT_BOTH
;
734 else if (cur_layout
== other_layout
) {
735 if (usage
!= LP_TEX_USAGE_WRITE_ALL
) {
736 /* need to convert tiled data to linear or vice versa */
739 if (usage
== LP_TEX_USAGE_READ
)
740 new_layout
= LP_TEX_LAYOUT_BOTH
;
744 assert(cur_layout
== LP_TEX_LAYOUT_NONE
||
745 cur_layout
== target_layout
);
748 assert(new_layout
== LP_TEX_LAYOUT_BOTH
||
749 new_layout
== target_layout
);
751 *new_layout_return
= new_layout
;
756 * Return pointer to a texture image. No tiled/linear conversion is done.
759 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
760 unsigned face
, unsigned level
,
761 enum lp_texture_layout layout
)
763 struct llvmpipe_texture_image
*img
;
764 unsigned face_offset
;
766 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
767 img
= &lpr
->linear
[level
];
770 assert (layout
== LP_TEX_LAYOUT_TILED
);
771 img
= &lpr
->tiled
[level
];
775 face_offset
= face
* tex_image_face_size(lpr
, level
, layout
);
779 return (ubyte
*) img
->data
+ face_offset
;
785 * Return pointer to texture image data (either linear or tiled layout).
786 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
787 * \param layout either LP_TEX_LAYOUT_LINEAR or LP_TEX_LAYOUT_TILED
790 llvmpipe_get_texture_image(struct llvmpipe_resource
*lpr
,
791 unsigned face
, unsigned level
,
792 enum lp_texture_usage usage
,
793 enum lp_texture_layout layout
)
796 * 'target' refers to the image which we're retrieving (either in
797 * tiled or linear layout).
798 * 'other' refers to the same image but in the other layout. (it may
801 struct llvmpipe_texture_image
*target_img
;
802 struct llvmpipe_texture_image
*other_img
;
805 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
806 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
807 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
808 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
809 enum lp_texture_layout other_layout
;
811 assert(layout
== LP_TEX_LAYOUT_NONE
||
812 layout
== LP_TEX_LAYOUT_TILED
||
813 layout
== LP_TEX_LAYOUT_LINEAR
);
815 assert(usage
== LP_TEX_USAGE_READ
||
816 usage
== LP_TEX_USAGE_READ_WRITE
||
817 usage
== LP_TEX_USAGE_WRITE_ALL
);
820 assert(lpr
->linear
[level
].data
);
823 /* which is target? which is other? */
824 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
825 target_img
= &lpr
->linear
[level
];
826 other_img
= &lpr
->tiled
[level
];
827 other_layout
= LP_TEX_LAYOUT_TILED
;
830 target_img
= &lpr
->tiled
[level
];
831 other_img
= &lpr
->linear
[level
];
832 other_layout
= LP_TEX_LAYOUT_LINEAR
;
835 target_data
= target_img
->data
;
836 other_data
= other_img
->data
;
839 /* allocate memory for the target image now */
840 unsigned buffer_size
= tex_image_size(lpr
, level
, layout
);
841 target_img
->data
= align_malloc(buffer_size
, 16);
842 target_data
= target_img
->data
;
846 unsigned offset
= face
* tex_image_face_size(lpr
, level
, layout
);
848 target_data
= (uint8_t *) target_data
+ offset
;
851 other_data
= (uint8_t *) other_data
+ offset
;
855 if (layout
== LP_TEX_LAYOUT_NONE
) {
856 /* just allocating memory */
861 /* may need to convert other data to the requested layout */
862 enum lp_texture_layout new_layout
;
863 unsigned x
, y
, i
= 0;
865 /* loop over all image tiles, doing layout conversion where needed */
866 for (y
= 0; y
< height_t
; y
++) {
867 for (x
= 0; x
< width_t
; x
++) {
868 enum lp_texture_layout cur_layout
= lpr
->layout
[face
][level
][i
];
871 layout_logic(cur_layout
, layout
, usage
, &new_layout
, &convert
);
874 if (layout
== LP_TEX_LAYOUT_TILED
) {
875 lp_linear_to_tiled(other_data
, target_data
,
876 x
* TILE_SIZE
, y
* TILE_SIZE
,
877 TILE_SIZE
, TILE_SIZE
,
882 lp_tiled_to_linear(other_data
, target_data
,
883 x
* TILE_SIZE
, y
* TILE_SIZE
,
884 TILE_SIZE
, TILE_SIZE
,
890 lpr
->layout
[face
][level
][i
] = new_layout
;
898 for (i
= 0; i
< width_t
* height_t
; i
++) {
899 lpr
->layout
[face
][level
][i
] = layout
;
909 static INLINE
enum lp_texture_layout
910 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource
*lpr
,
911 unsigned face
, unsigned level
,
912 unsigned x
, unsigned y
)
915 assert(resource_is_texture(&lpr
->base
));
916 assert(x
< lpr
->tiles_per_row
[level
]);
917 i
= y
* lpr
->tiles_per_row
[level
] + x
;
918 return lpr
->layout
[face
][level
][i
];
923 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource
*lpr
,
924 unsigned face
, unsigned level
,
925 unsigned x
, unsigned y
,
926 enum lp_texture_layout layout
)
929 assert(resource_is_texture(&lpr
->base
));
930 assert(x
< lpr
->tiles_per_row
[level
]);
931 i
= y
* lpr
->tiles_per_row
[level
] + x
;
932 lpr
->layout
[face
][level
][i
] = layout
;
937 * Get pointer to a linear image where the tile at (x,y) is known to be
939 * Conversion from tiled to linear will be done if necessary.
940 * \return pointer to start of image/face (not the tile)
943 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource
*lpr
,
944 unsigned face
, unsigned level
,
945 enum lp_texture_usage usage
,
946 unsigned x
, unsigned y
)
948 struct llvmpipe_texture_image
*tiled_img
= &lpr
->tiled
[level
];
949 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
950 enum lp_texture_layout cur_layout
, new_layout
;
951 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
954 assert(resource_is_texture(&lpr
->base
));
955 assert(x
% TILE_SIZE
== 0);
956 assert(y
% TILE_SIZE
== 0);
958 if (!linear_img
->data
) {
959 /* allocate memory for the tiled image now */
960 unsigned buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
961 linear_img
->data
= align_malloc(buffer_size
, 16);
964 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face
, level
, tx
, ty
);
966 layout_logic(cur_layout
, LP_TEX_LAYOUT_LINEAR
, usage
,
967 &new_layout
, &convert
);
970 lp_tiled_to_linear(tiled_img
->data
, linear_img
->data
,
971 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
975 if (new_layout
!= cur_layout
)
976 llvmpipe_set_texture_tile_layout(lpr
, face
, level
, tx
, ty
, new_layout
);
980 = face
* tex_image_face_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
981 return (ubyte
*) linear_img
->data
+ offset
;
984 return linear_img
->data
;
990 * Get pointer to tiled data for rendering.
991 * \return pointer to the tiled data at the given tile position
994 llvmpipe_get_texture_tile(struct llvmpipe_resource
*lpr
,
995 unsigned face
, unsigned level
,
996 enum lp_texture_usage usage
,
997 unsigned x
, unsigned y
)
999 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
1000 struct llvmpipe_texture_image
*tiled_img
= &lpr
->tiled
[level
];
1001 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1002 enum lp_texture_layout cur_layout
, new_layout
;
1003 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1006 assert(x
% TILE_SIZE
== 0);
1007 assert(y
% TILE_SIZE
== 0);
1009 if (!tiled_img
->data
) {
1010 /* allocate memory for the tiled image now */
1011 unsigned buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_TILED
);
1012 tiled_img
->data
= align_malloc(buffer_size
, 16);
1015 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face
, level
, tx
, ty
);
1017 layout_logic(cur_layout
, LP_TEX_LAYOUT_TILED
, usage
, &new_layout
, &convert
);
1019 lp_linear_to_tiled(linear_img
->data
, tiled_img
->data
,
1020 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1021 lpr
->stride
[level
]);
1024 if (new_layout
!= cur_layout
)
1025 llvmpipe_set_texture_tile_layout(lpr
, face
, level
, tx
, ty
, new_layout
);
1027 /* compute, return address of the 64x64 tile */
1029 unsigned tiles_per_row
, tile_offset
, face_offset
;
1031 tiles_per_row
= align(width
, TILE_SIZE
) / TILE_SIZE
;
1033 assert(tiles_per_row
== lpr
->tiles_per_row
[level
]);
1035 tile_offset
= ty
* tiles_per_row
+ tx
;
1036 tile_offset
*= TILE_SIZE
* TILE_SIZE
* 4;
1038 assert(tiled_img
->data
);
1040 face_offset
= (face
> 0)
1041 ? (face
* tex_image_face_size(lpr
, level
, LP_TEX_LAYOUT_TILED
))
1044 return (ubyte
*) tiled_img
->data
+ face_offset
+ tile_offset
;
1050 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
1052 screen
->resource_create
= llvmpipe_resource_create
;
1053 screen
->resource_destroy
= llvmpipe_resource_destroy
;
1054 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
1055 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
1056 screen
->user_buffer_create
= llvmpipe_user_buffer_create
;
1058 screen
->get_tex_surface
= llvmpipe_get_tex_surface
;
1059 screen
->tex_surface_destroy
= llvmpipe_tex_surface_destroy
;
1064 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
1066 pipe
->get_transfer
= llvmpipe_get_transfer
;
1067 pipe
->transfer_destroy
= llvmpipe_transfer_destroy
;
1068 pipe
->transfer_map
= llvmpipe_transfer_map
;
1069 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
1070 pipe
->is_resource_referenced
= llvmpipe_is_resource_referenced
;
1072 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
1073 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;