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"
51 #include "state_tracker/sw_winsys.h"
55 resource_is_texture(const struct pipe_resource
*resource
)
57 switch (resource
->target
) {
63 case PIPE_TEXTURE_CUBE
:
74 * Allocate storage for llvmpipe_texture::layout array.
75 * The number of elements is width_in_tiles * height_in_tiles.
77 static enum lp_texture_layout
*
78 alloc_layout_array(unsigned num_slices
, unsigned width
, unsigned height
)
80 const unsigned tx
= align(width
, TILE_SIZE
) / TILE_SIZE
;
81 const unsigned ty
= align(height
, TILE_SIZE
) / TILE_SIZE
;
83 assert(num_slices
* tx
* ty
> 0);
84 assert(LP_TEX_LAYOUT_NONE
== 0); /* calloc'ing LP_TEX_LAYOUT_NONE here */
86 return (enum lp_texture_layout
*)
87 CALLOC(num_slices
* tx
* ty
, sizeof(enum lp_texture_layout
));
93 * Conventional allocation path for non-display textures:
94 * Just compute row strides here. Storage is allocated on demand later.
97 llvmpipe_texture_layout(struct llvmpipe_screen
*screen
,
98 struct llvmpipe_resource
*lpr
)
100 struct pipe_resource
*pt
= &lpr
->base
;
102 unsigned width
= pt
->width0
;
103 unsigned height
= pt
->height0
;
104 unsigned depth
= pt
->depth0
;
106 assert(LP_MAX_TEXTURE_2D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
107 assert(LP_MAX_TEXTURE_3D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
109 for (level
= 0; level
<= pt
->last_level
; level
++) {
110 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
111 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
112 unsigned nblocksx
, num_slices
;
114 if (lpr
->base
.target
== PIPE_TEXTURE_CUBE
)
116 else if (lpr
->base
.target
== PIPE_TEXTURE_3D
)
121 /* Allocate storage for whole quads. This is particularly important
122 * for depth surfaces, which are currently stored in a swizzled format.
124 nblocksx
= util_format_get_nblocksx(pt
->format
, align(width
, TILE_SIZE
));
126 lpr
->row_stride
[level
] =
127 align(nblocksx
* util_format_get_blocksize(pt
->format
), 16);
129 lpr
->img_stride
[level
] = lpr
->row_stride
[level
] * align(height
, TILE_SIZE
);
131 lpr
->tiles_per_row
[level
] = width_t
;
132 lpr
->tiles_per_image
[level
] = width_t
* height_t
;
133 lpr
->num_slices_faces
[level
] = num_slices
;
134 lpr
->layout
[level
] = alloc_layout_array(num_slices
, width
, height
);
136 width
= u_minify(width
, 1);
137 height
= u_minify(height
, 1);
138 depth
= u_minify(depth
, 1);
147 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
148 struct llvmpipe_resource
*lpr
)
150 struct sw_winsys
*winsys
= screen
->winsys
;
152 /* Round up the surface size to a multiple of the tile size to
153 * avoid tile clipping.
155 const unsigned width
= align(lpr
->base
.width0
, TILE_SIZE
);
156 const unsigned height
= align(lpr
->base
.height0
, TILE_SIZE
);
157 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
158 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
160 lpr
->tiles_per_row
[0] = width_t
;
161 lpr
->tiles_per_image
[0] = width_t
* height_t
;
162 lpr
->num_slices_faces
[0] = 1;
163 lpr
->img_stride
[0] = 0;
165 lpr
->layout
[0] = alloc_layout_array(1, width
, height
);
166 //lpr->layout[0][0] = LP_TEX_LAYOUT_LINEAR;
168 lpr
->dt
= winsys
->displaytarget_create(winsys
,
173 &lpr
->row_stride
[0] );
175 return lpr
->dt
!= NULL
;
179 static struct pipe_resource
*
180 llvmpipe_resource_create(struct pipe_screen
*_screen
,
181 const struct pipe_resource
*templat
)
183 static unsigned id_counter
= 0;
184 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
185 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
189 lpr
->base
= *templat
;
190 pipe_reference_init(&lpr
->base
.reference
, 1);
191 lpr
->base
.screen
= &screen
->base
;
193 assert(lpr
->base
.bind
);
195 if (resource_is_texture(&lpr
->base
)) {
196 if (lpr
->base
.bind
& PIPE_BIND_DISPLAY_TARGET
) {
197 /* displayable surface */
198 if (!llvmpipe_displaytarget_layout(screen
, lpr
))
200 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
204 if (!llvmpipe_texture_layout(screen
, lpr
))
206 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
208 assert(lpr
->layout
[0]);
211 /* other data (vertex buffer, const buffer, etc) */
212 const enum pipe_format format
= templat
->format
;
213 const uint w
= templat
->width0
/ util_format_get_blockheight(format
);
214 const uint h
= templat
->height0
/ util_format_get_blockwidth(format
);
215 const uint d
= templat
->depth0
;
216 const uint bpp
= util_format_get_blocksize(format
);
217 const uint bytes
= w
* h
* d
* bpp
;
218 lpr
->data
= align_malloc(bytes
, 16);
223 lpr
->id
= id_counter
++;
234 llvmpipe_resource_destroy(struct pipe_screen
*pscreen
,
235 struct pipe_resource
*pt
)
237 struct llvmpipe_screen
*screen
= llvmpipe_screen(pscreen
);
238 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
242 struct sw_winsys
*winsys
= screen
->winsys
;
243 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
245 if (lpr
->tiled
[0].data
) {
246 align_free(lpr
->tiled
[0].data
);
247 lpr
->tiled
[0].data
= NULL
;
250 FREE(lpr
->layout
[0]);
252 else if (resource_is_texture(pt
)) {
253 /* regular texture */
256 /* free linear image data */
257 for (level
= 0; level
< Elements(lpr
->linear
); level
++) {
258 if (lpr
->linear
[level
].data
) {
259 align_free(lpr
->linear
[level
].data
);
260 lpr
->linear
[level
].data
= NULL
;
264 /* free tiled image data */
265 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
266 if (lpr
->tiled
[level
].data
) {
267 align_free(lpr
->tiled
[level
].data
);
268 lpr
->tiled
[level
].data
= NULL
;
272 /* free layout flag arrays */
273 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
274 FREE(lpr
->layout
[level
]);
275 lpr
->layout
[level
] = NULL
;
278 else if (!lpr
->userBuffer
) {
280 align_free(lpr
->data
);
288 * Map a resource for read/write.
291 llvmpipe_resource_map(struct pipe_resource
*resource
,
295 enum lp_texture_usage tex_usage
,
296 enum lp_texture_layout layout
)
298 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
302 assert(level
< LP_MAX_TEXTURE_LEVELS
);
304 assert(tex_usage
== LP_TEX_USAGE_READ
||
305 tex_usage
== LP_TEX_USAGE_READ_WRITE
||
306 tex_usage
== LP_TEX_USAGE_WRITE_ALL
);
308 assert(layout
== LP_TEX_LAYOUT_NONE
||
309 layout
== LP_TEX_LAYOUT_TILED
||
310 layout
== LP_TEX_LAYOUT_LINEAR
);
314 struct llvmpipe_screen
*screen
= llvmpipe_screen(resource
->screen
);
315 struct sw_winsys
*winsys
= screen
->winsys
;
319 if (tex_usage
== LP_TEX_USAGE_READ
) {
320 dt_usage
= PIPE_TRANSFER_READ
;
323 dt_usage
= PIPE_TRANSFER_READ_WRITE
;
330 /* FIXME: keep map count? */
331 map
= winsys
->displaytarget_map(winsys
, lpr
->dt
, dt_usage
);
333 /* install this linear image in texture data structure */
334 lpr
->linear
[level
].data
= map
;
336 /* make sure tiled data gets converted to linear data */
337 map2
= llvmpipe_get_texture_image(lpr
, 0, 0, tex_usage
, layout
);
338 if (layout
== LP_TEX_LAYOUT_LINEAR
)
343 else if (resource_is_texture(resource
)) {
344 /* regular texture */
345 if (resource
->target
!= PIPE_TEXTURE_CUBE
) {
348 if (resource
->target
!= PIPE_TEXTURE_3D
) {
352 map
= llvmpipe_get_texture_image(lpr
, face
+ zslice
, level
,
367 llvmpipe_resource_unmap(struct pipe_resource
*resource
,
372 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
376 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(resource
->screen
);
377 struct sw_winsys
*winsys
= lp_screen
->winsys
;
383 /* make sure linear image is up to date */
384 (void) llvmpipe_get_texture_image(lpr
, face
+ zslice
, level
,
386 LP_TEX_LAYOUT_LINEAR
);
388 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
394 llvmpipe_resource_data(struct pipe_resource
*resource
)
396 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
398 assert(!resource_is_texture(resource
));
404 static struct pipe_resource
*
405 llvmpipe_resource_from_handle(struct pipe_screen
*screen
,
406 const struct pipe_resource
*template,
407 struct winsys_handle
*whandle
)
409 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
410 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
414 lpr
->base
= *template;
415 pipe_reference_init(&lpr
->base
.reference
, 1);
416 lpr
->base
.screen
= screen
;
418 lpr
->dt
= winsys
->displaytarget_from_handle(winsys
,
421 &lpr
->row_stride
[0]);
434 llvmpipe_resource_get_handle(struct pipe_screen
*screen
,
435 struct pipe_resource
*pt
,
436 struct winsys_handle
*whandle
)
438 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
439 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
445 return winsys
->displaytarget_get_handle(winsys
, lpr
->dt
, whandle
);
449 static struct pipe_surface
*
450 llvmpipe_get_tex_surface(struct pipe_screen
*screen
,
451 struct pipe_resource
*pt
,
452 unsigned face
, unsigned level
, unsigned zslice
,
453 enum lp_texture_usage usage
)
455 struct pipe_surface
*ps
;
457 assert(level
<= pt
->last_level
);
459 ps
= CALLOC_STRUCT(pipe_surface
);
461 pipe_reference_init(&ps
->reference
, 1);
462 pipe_resource_reference(&ps
->texture
, pt
);
463 ps
->format
= pt
->format
;
464 ps
->width
= u_minify(pt
->width0
, level
);
465 ps
->height
= u_minify(pt
->height0
, level
);
477 llvmpipe_tex_surface_destroy(struct pipe_surface
*surf
)
479 /* Effectively do the texture_update work here - if texture images
480 * needed post-processing to put them into hardware layout, this is
481 * where it would happen. For llvmpipe, nothing to do.
483 assert(surf
->texture
);
484 pipe_resource_reference(&surf
->texture
, NULL
);
489 static struct pipe_transfer
*
490 llvmpipe_get_transfer(struct pipe_context
*pipe
,
491 struct pipe_resource
*resource
,
492 struct pipe_subresource sr
,
494 const struct pipe_box
*box
)
496 struct llvmpipe_resource
*lprex
= llvmpipe_resource(resource
);
497 struct llvmpipe_transfer
*lpr
;
500 assert(sr
.level
<= resource
->last_level
);
503 * Transfers, like other pipe operations, must happen in order, so flush the
504 * context if necessary.
506 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
507 boolean read_only
= !(usage
& PIPE_TRANSFER_WRITE
);
508 boolean do_not_block
= !!(usage
& PIPE_TRANSFER_DONTBLOCK
);
509 if (!llvmpipe_flush_resource(pipe
, resource
,
513 TRUE
, /* cpu_access */
516 * It would have blocked, but state tracker requested no to.
518 assert(do_not_block
);
523 lpr
= CALLOC_STRUCT(llvmpipe_transfer
);
525 struct pipe_transfer
*pt
= &lpr
->base
;
526 pipe_resource_reference(&pt
->resource
, resource
);
529 pt
->stride
= lprex
->row_stride
[sr
.level
];
539 llvmpipe_transfer_destroy(struct pipe_context
*pipe
,
540 struct pipe_transfer
*transfer
)
542 /* Effectively do the texture_update work here - if texture images
543 * needed post-processing to put them into hardware layout, this is
544 * where it would happen. For llvmpipe, nothing to do.
546 assert (transfer
->resource
);
547 pipe_resource_reference(&transfer
->resource
, NULL
);
553 llvmpipe_transfer_map( struct pipe_context
*pipe
,
554 struct pipe_transfer
*transfer
)
556 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
558 struct llvmpipe_resource
*lpr
;
559 enum pipe_format format
;
560 enum lp_texture_usage tex_usage
;
563 assert(transfer
->sr
.face
< 6);
564 assert(transfer
->sr
.level
< LP_MAX_TEXTURE_LEVELS
);
567 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
568 transfer->x, transfer->y, transfer->width, transfer->height,
569 transfer->texture->width0,
570 transfer->texture->height0,
574 if (transfer
->usage
== PIPE_TRANSFER_READ
) {
575 tex_usage
= LP_TEX_USAGE_READ
;
579 tex_usage
= LP_TEX_USAGE_READ_WRITE
;
584 struct llvmpipe_resource
*lpr
= llvmpipe_resource(transfer
->resource
);
585 printf("transfer map tex %u mode %s\n", lpr
->id
, mode
);
589 assert(transfer
->resource
);
590 lpr
= llvmpipe_resource(transfer
->resource
);
591 format
= lpr
->base
.format
;
593 map
= llvmpipe_resource_map(transfer
->resource
,
597 tex_usage
, LP_TEX_LAYOUT_LINEAR
);
600 /* May want to do different things here depending on read/write nature
603 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
604 /* Do something to notify sharing contexts of a texture change.
610 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
611 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
618 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
619 struct pipe_transfer
*transfer
)
621 assert(transfer
->resource
);
623 llvmpipe_resource_unmap(transfer
->resource
,
630 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
631 struct pipe_resource
*presource
,
632 unsigned face
, unsigned level
)
634 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
636 if (presource
->target
== PIPE_BUFFER
)
637 return PIPE_UNREFERENCED
;
639 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
645 * Create buffer which wraps user-space data.
647 static struct pipe_resource
*
648 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
653 struct llvmpipe_resource
*buffer
;
655 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
659 pipe_reference_init(&buffer
->base
.reference
, 1);
660 buffer
->base
.screen
= screen
;
661 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
662 buffer
->base
.bind
= bind_flags
;
663 buffer
->base
.usage
= PIPE_USAGE_IMMUTABLE
;
664 buffer
->base
.flags
= 0;
665 buffer
->base
.width0
= bytes
;
666 buffer
->base
.height0
= 1;
667 buffer
->base
.depth0
= 1;
668 buffer
->userBuffer
= TRUE
;
671 return &buffer
->base
;
676 * Compute size (in bytes) need to store a texture image / mipmap level,
677 * for just one cube face or one 3D texture slice
680 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
681 enum lp_texture_layout layout
)
683 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
684 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
686 assert(layout
== LP_TEX_LAYOUT_TILED
||
687 layout
== LP_TEX_LAYOUT_LINEAR
);
689 if (layout
== LP_TEX_LAYOUT_TILED
) {
690 /* for tiled layout, force a 32bpp format */
691 const enum pipe_format format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
692 const unsigned block_size
= util_format_get_blocksize(format
);
693 const unsigned nblocksy
=
694 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
695 const unsigned nblocksx
=
696 util_format_get_nblocksx(format
, align(width
, TILE_SIZE
));
697 const unsigned buffer_size
= block_size
* nblocksy
* nblocksx
;
701 const enum pipe_format format
= lpr
->base
.format
;
702 const unsigned nblocksy
=
703 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
704 const unsigned buffer_size
= nblocksy
* lpr
->row_stride
[level
];
711 * Compute size (in bytes) need to store a texture image / mipmap level,
712 * including all cube faces or 3D image slices
715 tex_image_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
716 enum lp_texture_layout layout
)
718 const unsigned buf_size
= tex_image_face_size(lpr
, level
, layout
);
719 return buf_size
* lpr
->num_slices_faces
[level
];
724 * This function encapsulates some complicated logic for determining
725 * how to convert a tile of image data from linear layout to tiled
726 * layout, or vice versa.
727 * \param cur_layout the current tile layout
728 * \param target_layout the desired tile layout
729 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
730 * \param new_layout_return returns the new layout mode
731 * \param convert_return returns TRUE if image conversion is needed
734 layout_logic(enum lp_texture_layout cur_layout
,
735 enum lp_texture_layout target_layout
,
736 enum lp_texture_usage usage
,
737 enum lp_texture_layout
*new_layout_return
,
740 enum lp_texture_layout other_layout
, new_layout
;
744 new_layout
= 99; /* debug check */
746 if (target_layout
== LP_TEX_LAYOUT_LINEAR
) {
747 other_layout
= LP_TEX_LAYOUT_TILED
;
750 assert(target_layout
== LP_TEX_LAYOUT_TILED
);
751 other_layout
= LP_TEX_LAYOUT_LINEAR
;
754 new_layout
= target_layout
; /* may get changed below */
756 if (cur_layout
== LP_TEX_LAYOUT_BOTH
) {
757 if (usage
== LP_TEX_USAGE_READ
) {
758 new_layout
= LP_TEX_LAYOUT_BOTH
;
761 else if (cur_layout
== other_layout
) {
762 if (usage
!= LP_TEX_USAGE_WRITE_ALL
) {
763 /* need to convert tiled data to linear or vice versa */
766 if (usage
== LP_TEX_USAGE_READ
)
767 new_layout
= LP_TEX_LAYOUT_BOTH
;
771 assert(cur_layout
== LP_TEX_LAYOUT_NONE
||
772 cur_layout
== target_layout
);
775 assert(new_layout
== LP_TEX_LAYOUT_BOTH
||
776 new_layout
== target_layout
);
778 *new_layout_return
= new_layout
;
783 * Return pointer to a 2D texture image/face/slice.
784 * No tiled/linear conversion is done.
787 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
788 unsigned face_slice
, unsigned level
,
789 enum lp_texture_layout layout
)
791 struct llvmpipe_texture_image
*img
;
794 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
795 img
= &lpr
->linear
[level
];
798 assert (layout
== LP_TEX_LAYOUT_TILED
);
799 img
= &lpr
->tiled
[level
];
803 offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
807 return (ubyte
*) img
->data
+ offset
;
811 static INLINE
enum lp_texture_layout
812 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource
*lpr
,
813 unsigned face_slice
, unsigned level
,
814 unsigned x
, unsigned y
)
817 assert(resource_is_texture(&lpr
->base
));
818 assert(x
< lpr
->tiles_per_row
[level
]);
819 i
= face_slice
* lpr
->tiles_per_image
[level
]
820 + y
* lpr
->tiles_per_row
[level
] + x
;
821 return lpr
->layout
[level
][i
];
826 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource
*lpr
,
827 unsigned face_slice
, unsigned level
,
828 unsigned x
, unsigned y
,
829 enum lp_texture_layout layout
)
832 assert(resource_is_texture(&lpr
->base
));
833 assert(x
< lpr
->tiles_per_row
[level
]);
834 i
= face_slice
* lpr
->tiles_per_image
[level
]
835 + y
* lpr
->tiles_per_row
[level
] + x
;
836 lpr
->layout
[level
][i
] = layout
;
841 * Set the layout mode for all tiles in a particular image.
844 llvmpipe_set_texture_image_layout(struct llvmpipe_resource
*lpr
,
845 unsigned face_slice
, unsigned level
,
846 unsigned width_t
, unsigned height_t
,
847 enum lp_texture_layout layout
)
849 const unsigned start
= face_slice
* lpr
->tiles_per_image
[level
];
852 for (i
= 0; i
< width_t
* height_t
; i
++) {
853 lpr
->layout
[level
][start
+ i
] = layout
;
859 * Allocate storage for a linear or tile texture image (all cube
860 * faces and all 3D slices.
863 alloc_image_data(struct llvmpipe_resource
*lpr
, unsigned level
,
864 enum lp_texture_layout layout
)
869 if (layout
== LP_TEX_LAYOUT_TILED
) {
870 /* tiled data is stored in regular memory */
871 uint buffer_size
= tex_image_size(lpr
, level
, layout
);
872 lpr
->tiled
[level
].data
= align_malloc(buffer_size
, 16);
875 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
877 /* we get the linear memory from the winsys */
878 struct llvmpipe_screen
*screen
= llvmpipe_screen(lpr
->base
.screen
);
879 struct sw_winsys
*winsys
= screen
->winsys
;
881 lpr
->linear
[0].data
=
882 winsys
->displaytarget_map(winsys
, lpr
->dt
,
883 PIPE_TRANSFER_READ_WRITE
);
886 /* not a display target - allocate regular memory */
887 uint buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
888 lpr
->linear
[level
].data
= align_malloc(buffer_size
, 16);
896 * Return pointer to texture image data (either linear or tiled layout)
897 * for a particular cube face or 3D texture slice.
899 * \param face_slice the cube face or 3D slice of interest
900 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
901 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
904 llvmpipe_get_texture_image(struct llvmpipe_resource
*lpr
,
905 unsigned face_slice
, unsigned level
,
906 enum lp_texture_usage usage
,
907 enum lp_texture_layout layout
)
910 * 'target' refers to the image which we're retrieving (either in
911 * tiled or linear layout).
912 * 'other' refers to the same image but in the other layout. (it may
915 struct llvmpipe_texture_image
*target_img
;
916 struct llvmpipe_texture_image
*other_img
;
919 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
920 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
921 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
922 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
923 enum lp_texture_layout other_layout
;
924 boolean only_allocate
;
926 assert(layout
== LP_TEX_LAYOUT_NONE
||
927 layout
== LP_TEX_LAYOUT_TILED
||
928 layout
== LP_TEX_LAYOUT_LINEAR
);
930 assert(usage
== LP_TEX_USAGE_READ
||
931 usage
== LP_TEX_USAGE_READ_WRITE
||
932 usage
== LP_TEX_USAGE_WRITE_ALL
);
934 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
935 if (layout
== LP_TEX_LAYOUT_NONE
) {
936 only_allocate
= TRUE
;
937 layout
= LP_TEX_LAYOUT_TILED
;
940 only_allocate
= FALSE
;
944 assert(lpr
->linear
[level
].data
);
947 /* which is target? which is other? */
948 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
949 target_img
= &lpr
->linear
[level
];
950 other_img
= &lpr
->tiled
[level
];
951 other_layout
= LP_TEX_LAYOUT_TILED
;
954 target_img
= &lpr
->tiled
[level
];
955 other_img
= &lpr
->linear
[level
];
956 other_layout
= LP_TEX_LAYOUT_LINEAR
;
959 target_data
= target_img
->data
;
960 other_data
= other_img
->data
;
963 /* allocate memory for the target image now */
964 alloc_image_data(lpr
, level
, layout
);
965 target_data
= target_img
->data
;
968 if (face_slice
> 0) {
969 unsigned target_offset
, other_offset
;
971 target_offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
972 other_offset
= face_slice
* tex_image_face_size(lpr
, level
, other_layout
);
974 target_data
= (uint8_t *) target_data
+ target_offset
;
977 other_data
= (uint8_t *) other_data
+ other_offset
;
982 /* Just allocating tiled memory. Don't initialize it from the
983 * linear data if it exists.
989 /* may need to convert other data to the requested layout */
990 enum lp_texture_layout new_layout
;
993 /* loop over all image tiles, doing layout conversion where needed */
994 for (y
= 0; y
< height_t
; y
++) {
995 for (x
= 0; x
< width_t
; x
++) {
996 enum lp_texture_layout cur_layout
=
997 llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, x
, y
);
1000 layout_logic(cur_layout
, layout
, usage
, &new_layout
, &convert
);
1003 if (layout
== LP_TEX_LAYOUT_TILED
) {
1004 lp_linear_to_tiled(other_data
, target_data
,
1005 x
* TILE_SIZE
, y
* TILE_SIZE
,
1006 TILE_SIZE
, TILE_SIZE
,
1008 lpr
->row_stride
[level
],
1009 lpr
->tiles_per_row
[level
]);
1012 lp_tiled_to_linear(other_data
, target_data
,
1013 x
* TILE_SIZE
, y
* TILE_SIZE
,
1014 TILE_SIZE
, TILE_SIZE
,
1016 lpr
->row_stride
[level
],
1017 lpr
->tiles_per_row
[level
]);
1021 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, x
, y
,
1028 llvmpipe_set_texture_image_layout(lpr
, face_slice
, level
,
1029 width_t
, height_t
, layout
);
1032 assert(target_data
);
1039 * Return pointer to start of a texture image (1D, 2D, 3D, CUBE).
1040 * All cube faces and 3D slices will be converted to the requested
1042 * This is typically used when we're about to sample from a texture.
1045 llvmpipe_get_texture_image_all(struct llvmpipe_resource
*lpr
,
1047 enum lp_texture_usage usage
,
1048 enum lp_texture_layout layout
)
1050 const int slices
= lpr
->num_slices_faces
[level
];
1056 for (slice
= slices
- 1; slice
>= 0; slice
--) {
1057 map
= llvmpipe_get_texture_image(lpr
, slice
, level
, usage
, layout
);
1065 * Get pointer to a linear image (not the tile!) where the tile at (x,y)
1066 * is known to be in linear layout.
1067 * Conversion from tiled to linear will be done if necessary.
1068 * \return pointer to start of image/face (not the tile)
1071 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource
*lpr
,
1072 unsigned face_slice
, unsigned level
,
1073 enum lp_texture_usage usage
,
1074 unsigned x
, unsigned y
)
1076 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1077 enum lp_texture_layout cur_layout
, new_layout
;
1078 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1080 uint8_t *tiled_image
, *linear_image
;
1082 assert(resource_is_texture(&lpr
->base
));
1083 assert(x
% TILE_SIZE
== 0);
1084 assert(y
% TILE_SIZE
== 0);
1086 if (!linear_img
->data
) {
1087 /* allocate memory for the linear image now */
1088 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1091 /* compute address of the slice/face of the image that contains the tile */
1092 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1093 LP_TEX_LAYOUT_TILED
);
1094 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1095 LP_TEX_LAYOUT_LINEAR
);
1097 /* get current tile layout and determine if data conversion is needed */
1098 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1100 layout_logic(cur_layout
, LP_TEX_LAYOUT_LINEAR
, usage
,
1101 &new_layout
, &convert
);
1104 lp_tiled_to_linear(tiled_image
, linear_image
,
1105 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1106 lpr
->row_stride
[level
],
1107 lpr
->tiles_per_row
[level
]);
1110 if (new_layout
!= cur_layout
)
1111 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1113 return linear_image
;
1118 * Get pointer to tiled data for rendering.
1119 * \return pointer to the tiled data at the given tile position
1122 llvmpipe_get_texture_tile(struct llvmpipe_resource
*lpr
,
1123 unsigned face_slice
, unsigned level
,
1124 enum lp_texture_usage usage
,
1125 unsigned x
, unsigned y
)
1127 struct llvmpipe_texture_image
*tiled_img
= &lpr
->tiled
[level
];
1128 enum lp_texture_layout cur_layout
, new_layout
;
1129 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1131 uint8_t *tiled_image
, *linear_image
;
1132 unsigned tile_offset
;
1134 assert(x
% TILE_SIZE
== 0);
1135 assert(y
% TILE_SIZE
== 0);
1137 if (!tiled_img
->data
) {
1138 /* allocate memory for the tiled image now */
1139 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_TILED
);
1142 /* compute address of the slice/face of the image that contains the tile */
1143 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1144 LP_TEX_LAYOUT_TILED
);
1145 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1146 LP_TEX_LAYOUT_LINEAR
);
1148 /* get current tile layout and see if we need to convert the data */
1149 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1151 layout_logic(cur_layout
, LP_TEX_LAYOUT_TILED
, usage
, &new_layout
, &convert
);
1153 lp_linear_to_tiled(linear_image
, tiled_image
,
1154 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1155 lpr
->row_stride
[level
],
1156 lpr
->tiles_per_row
[level
]);
1159 if (new_layout
!= cur_layout
)
1160 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1162 /* compute, return address of the 64x64 tile */
1163 tile_offset
= (ty
* lpr
->tiles_per_row
[level
] + tx
)
1164 * TILE_SIZE
* TILE_SIZE
* 4;
1166 return (ubyte
*) tiled_image
+ tile_offset
;
1171 * Return size of resource in bytes
1174 llvmpipe_resource_size(const struct pipe_resource
*resource
)
1176 const struct llvmpipe_resource
*lpr
= llvmpipe_resource_const(resource
);
1177 unsigned lvl
, size
= 0;
1179 for (lvl
= 0; lvl
<= lpr
->base
.last_level
; lvl
++) {
1180 if (lpr
->linear
[lvl
].data
)
1181 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_LINEAR
);
1183 if (lpr
->tiled
[lvl
].data
)
1184 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_TILED
);
1192 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
1194 screen
->resource_create
= llvmpipe_resource_create
;
1195 screen
->resource_destroy
= llvmpipe_resource_destroy
;
1196 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
1197 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
1198 screen
->user_buffer_create
= llvmpipe_user_buffer_create
;
1200 screen
->get_tex_surface
= llvmpipe_get_tex_surface
;
1201 screen
->tex_surface_destroy
= llvmpipe_tex_surface_destroy
;
1206 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
1208 pipe
->get_transfer
= llvmpipe_get_transfer
;
1209 pipe
->transfer_destroy
= llvmpipe_transfer_destroy
;
1210 pipe
->transfer_map
= llvmpipe_transfer_map
;
1211 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
1212 pipe
->is_resource_referenced
= llvmpipe_is_resource_referenced
;
1214 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
1215 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;