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
++) {
111 /* Row stride and image stride (for linear layout) */
113 unsigned alignment
, nblocksx
, nblocksy
, block_size
;
115 /* For non-compressed formats we need to align the texture size
116 * to the tile size to facilitate render-to-texture.
118 if (util_format_is_compressed(pt
->format
))
121 alignment
= TILE_SIZE
;
123 nblocksx
= util_format_get_nblocksx(pt
->format
,
124 align(width
, alignment
));
125 nblocksy
= util_format_get_nblocksy(pt
->format
,
126 align(height
, alignment
));
127 block_size
= util_format_get_blocksize(pt
->format
);
129 lpr
->row_stride
[level
] = align(nblocksx
* block_size
, 16);
131 lpr
->img_stride
[level
] = lpr
->row_stride
[level
] * nblocksy
* block_size
;
134 /* Size of the image in tiles (for tiled layout) */
136 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
137 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
138 lpr
->tiles_per_row
[level
] = width_t
;
139 lpr
->tiles_per_image
[level
] = width_t
* height_t
;
142 /* Number of 3D image slices or cube faces */
146 if (lpr
->base
.target
== PIPE_TEXTURE_CUBE
)
148 else if (lpr
->base
.target
== PIPE_TEXTURE_3D
)
153 lpr
->num_slices_faces
[level
] = num_slices
;
155 lpr
->layout
[level
] = alloc_layout_array(num_slices
, width
, height
);
158 /* Compute size of next mipmap level */
159 width
= u_minify(width
, 1);
160 height
= u_minify(height
, 1);
161 depth
= u_minify(depth
, 1);
170 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
171 struct llvmpipe_resource
*lpr
)
173 struct sw_winsys
*winsys
= screen
->winsys
;
175 /* Round up the surface size to a multiple of the tile size to
176 * avoid tile clipping.
178 const unsigned width
= align(lpr
->base
.width0
, TILE_SIZE
);
179 const unsigned height
= align(lpr
->base
.height0
, TILE_SIZE
);
180 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
181 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
183 lpr
->tiles_per_row
[0] = width_t
;
184 lpr
->tiles_per_image
[0] = width_t
* height_t
;
185 lpr
->num_slices_faces
[0] = 1;
186 lpr
->img_stride
[0] = 0;
188 lpr
->layout
[0] = alloc_layout_array(1, width
, height
);
189 //lpr->layout[0][0] = LP_TEX_LAYOUT_LINEAR;
191 lpr
->dt
= winsys
->displaytarget_create(winsys
,
196 &lpr
->row_stride
[0] );
198 return lpr
->dt
!= NULL
;
202 static struct pipe_resource
*
203 llvmpipe_resource_create(struct pipe_screen
*_screen
,
204 const struct pipe_resource
*templat
)
206 static unsigned id_counter
= 0;
207 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
208 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
212 lpr
->base
= *templat
;
213 pipe_reference_init(&lpr
->base
.reference
, 1);
214 lpr
->base
.screen
= &screen
->base
;
216 assert(lpr
->base
.bind
);
218 if (resource_is_texture(&lpr
->base
)) {
219 if (lpr
->base
.bind
& PIPE_BIND_DISPLAY_TARGET
) {
220 /* displayable surface */
221 if (!llvmpipe_displaytarget_layout(screen
, lpr
))
223 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
227 if (!llvmpipe_texture_layout(screen
, lpr
))
229 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
231 assert(lpr
->layout
[0]);
234 /* other data (vertex buffer, const buffer, etc) */
235 const enum pipe_format format
= templat
->format
;
236 const uint w
= templat
->width0
/ util_format_get_blockheight(format
);
237 const uint h
= templat
->height0
/ util_format_get_blockwidth(format
);
238 const uint d
= templat
->depth0
;
239 const uint bpp
= util_format_get_blocksize(format
);
240 const uint bytes
= w
* h
* d
* bpp
;
241 lpr
->data
= align_malloc(bytes
, 16);
246 lpr
->id
= id_counter
++;
257 llvmpipe_resource_destroy(struct pipe_screen
*pscreen
,
258 struct pipe_resource
*pt
)
260 struct llvmpipe_screen
*screen
= llvmpipe_screen(pscreen
);
261 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
265 struct sw_winsys
*winsys
= screen
->winsys
;
266 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
268 if (lpr
->tiled
[0].data
) {
269 align_free(lpr
->tiled
[0].data
);
270 lpr
->tiled
[0].data
= NULL
;
273 FREE(lpr
->layout
[0]);
275 else if (resource_is_texture(pt
)) {
276 /* regular texture */
279 /* free linear image data */
280 for (level
= 0; level
< Elements(lpr
->linear
); level
++) {
281 if (lpr
->linear
[level
].data
) {
282 align_free(lpr
->linear
[level
].data
);
283 lpr
->linear
[level
].data
= NULL
;
287 /* free tiled image data */
288 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
289 if (lpr
->tiled
[level
].data
) {
290 align_free(lpr
->tiled
[level
].data
);
291 lpr
->tiled
[level
].data
= NULL
;
295 /* free layout flag arrays */
296 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
297 FREE(lpr
->layout
[level
]);
298 lpr
->layout
[level
] = NULL
;
301 else if (!lpr
->userBuffer
) {
303 align_free(lpr
->data
);
311 * Map a resource for read/write.
314 llvmpipe_resource_map(struct pipe_resource
*resource
,
318 enum lp_texture_usage tex_usage
,
319 enum lp_texture_layout layout
)
321 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
325 assert(level
< LP_MAX_TEXTURE_LEVELS
);
327 assert(tex_usage
== LP_TEX_USAGE_READ
||
328 tex_usage
== LP_TEX_USAGE_READ_WRITE
||
329 tex_usage
== LP_TEX_USAGE_WRITE_ALL
);
331 assert(layout
== LP_TEX_LAYOUT_NONE
||
332 layout
== LP_TEX_LAYOUT_TILED
||
333 layout
== LP_TEX_LAYOUT_LINEAR
);
337 struct llvmpipe_screen
*screen
= llvmpipe_screen(resource
->screen
);
338 struct sw_winsys
*winsys
= screen
->winsys
;
342 if (tex_usage
== LP_TEX_USAGE_READ
) {
343 dt_usage
= PIPE_TRANSFER_READ
;
346 dt_usage
= PIPE_TRANSFER_READ_WRITE
;
353 /* FIXME: keep map count? */
354 map
= winsys
->displaytarget_map(winsys
, lpr
->dt
, dt_usage
);
356 /* install this linear image in texture data structure */
357 lpr
->linear
[level
].data
= map
;
359 /* make sure tiled data gets converted to linear data */
360 map2
= llvmpipe_get_texture_image(lpr
, 0, 0, tex_usage
, layout
);
361 if (layout
== LP_TEX_LAYOUT_LINEAR
)
366 else if (resource_is_texture(resource
)) {
367 /* regular texture */
368 if (resource
->target
!= PIPE_TEXTURE_CUBE
) {
371 if (resource
->target
!= PIPE_TEXTURE_3D
) {
375 map
= llvmpipe_get_texture_image(lpr
, face
+ zslice
, level
,
390 llvmpipe_resource_unmap(struct pipe_resource
*resource
,
395 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
399 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(resource
->screen
);
400 struct sw_winsys
*winsys
= lp_screen
->winsys
;
406 /* make sure linear image is up to date */
407 (void) llvmpipe_get_texture_image(lpr
, face
+ zslice
, level
,
409 LP_TEX_LAYOUT_LINEAR
);
411 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
417 llvmpipe_resource_data(struct pipe_resource
*resource
)
419 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
421 assert(!resource_is_texture(resource
));
427 static struct pipe_resource
*
428 llvmpipe_resource_from_handle(struct pipe_screen
*screen
,
429 const struct pipe_resource
*template,
430 struct winsys_handle
*whandle
)
432 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
433 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
437 lpr
->base
= *template;
438 pipe_reference_init(&lpr
->base
.reference
, 1);
439 lpr
->base
.screen
= screen
;
441 lpr
->dt
= winsys
->displaytarget_from_handle(winsys
,
444 &lpr
->row_stride
[0]);
457 llvmpipe_resource_get_handle(struct pipe_screen
*screen
,
458 struct pipe_resource
*pt
,
459 struct winsys_handle
*whandle
)
461 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
462 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
468 return winsys
->displaytarget_get_handle(winsys
, lpr
->dt
, whandle
);
472 static struct pipe_surface
*
473 llvmpipe_get_tex_surface(struct pipe_screen
*screen
,
474 struct pipe_resource
*pt
,
475 unsigned face
, unsigned level
, unsigned zslice
,
478 struct pipe_surface
*ps
;
480 assert(level
<= pt
->last_level
);
482 ps
= CALLOC_STRUCT(pipe_surface
);
484 pipe_reference_init(&ps
->reference
, 1);
485 pipe_resource_reference(&ps
->texture
, pt
);
486 ps
->format
= pt
->format
;
487 ps
->width
= u_minify(pt
->width0
, level
);
488 ps
->height
= u_minify(pt
->height0
, level
);
500 llvmpipe_tex_surface_destroy(struct pipe_surface
*surf
)
502 /* Effectively do the texture_update work here - if texture images
503 * needed post-processing to put them into hardware layout, this is
504 * where it would happen. For llvmpipe, nothing to do.
506 assert(surf
->texture
);
507 pipe_resource_reference(&surf
->texture
, NULL
);
512 static struct pipe_transfer
*
513 llvmpipe_get_transfer(struct pipe_context
*pipe
,
514 struct pipe_resource
*resource
,
515 struct pipe_subresource sr
,
517 const struct pipe_box
*box
)
519 struct llvmpipe_resource
*lprex
= llvmpipe_resource(resource
);
520 struct llvmpipe_transfer
*lpr
;
523 assert(sr
.level
<= resource
->last_level
);
526 * Transfers, like other pipe operations, must happen in order, so flush the
527 * context if necessary.
529 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
530 boolean read_only
= !(usage
& PIPE_TRANSFER_WRITE
);
531 boolean do_not_block
= !!(usage
& PIPE_TRANSFER_DONTBLOCK
);
532 if (!llvmpipe_flush_resource(pipe
, resource
,
536 TRUE
, /* cpu_access */
539 * It would have blocked, but state tracker requested no to.
541 assert(do_not_block
);
546 lpr
= CALLOC_STRUCT(llvmpipe_transfer
);
548 struct pipe_transfer
*pt
= &lpr
->base
;
549 pipe_resource_reference(&pt
->resource
, resource
);
552 pt
->stride
= lprex
->row_stride
[sr
.level
];
562 llvmpipe_transfer_destroy(struct pipe_context
*pipe
,
563 struct pipe_transfer
*transfer
)
565 /* Effectively do the texture_update work here - if texture images
566 * needed post-processing to put them into hardware layout, this is
567 * where it would happen. For llvmpipe, nothing to do.
569 assert (transfer
->resource
);
570 pipe_resource_reference(&transfer
->resource
, NULL
);
576 llvmpipe_transfer_map( struct pipe_context
*pipe
,
577 struct pipe_transfer
*transfer
)
579 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
581 struct llvmpipe_resource
*lpr
;
582 enum pipe_format format
;
583 enum lp_texture_usage tex_usage
;
586 assert(transfer
->sr
.face
< 6);
587 assert(transfer
->sr
.level
< LP_MAX_TEXTURE_LEVELS
);
590 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
591 transfer->x, transfer->y, transfer->width, transfer->height,
592 transfer->texture->width0,
593 transfer->texture->height0,
597 if (transfer
->usage
== PIPE_TRANSFER_READ
) {
598 tex_usage
= LP_TEX_USAGE_READ
;
602 tex_usage
= LP_TEX_USAGE_READ_WRITE
;
607 struct llvmpipe_resource
*lpr
= llvmpipe_resource(transfer
->resource
);
608 printf("transfer map tex %u mode %s\n", lpr
->id
, mode
);
612 assert(transfer
->resource
);
613 lpr
= llvmpipe_resource(transfer
->resource
);
614 format
= lpr
->base
.format
;
616 map
= llvmpipe_resource_map(transfer
->resource
,
620 tex_usage
, LP_TEX_LAYOUT_LINEAR
);
623 /* May want to do different things here depending on read/write nature
626 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
627 /* Do something to notify sharing contexts of a texture change.
633 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
634 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
641 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
642 struct pipe_transfer
*transfer
)
644 assert(transfer
->resource
);
646 llvmpipe_resource_unmap(transfer
->resource
,
653 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
654 struct pipe_resource
*presource
,
655 unsigned face
, unsigned level
)
657 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
659 if (presource
->target
== PIPE_BUFFER
)
660 return PIPE_UNREFERENCED
;
662 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
668 * Create buffer which wraps user-space data.
670 static struct pipe_resource
*
671 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
676 struct llvmpipe_resource
*buffer
;
678 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
682 pipe_reference_init(&buffer
->base
.reference
, 1);
683 buffer
->base
.screen
= screen
;
684 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
685 buffer
->base
.bind
= bind_flags
;
686 buffer
->base
.usage
= PIPE_USAGE_IMMUTABLE
;
687 buffer
->base
.flags
= 0;
688 buffer
->base
.width0
= bytes
;
689 buffer
->base
.height0
= 1;
690 buffer
->base
.depth0
= 1;
691 buffer
->userBuffer
= TRUE
;
694 return &buffer
->base
;
699 * Compute size (in bytes) need to store a texture image / mipmap level,
700 * for just one cube face or one 3D texture slice
703 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
704 enum lp_texture_layout layout
)
706 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
707 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
709 assert(layout
== LP_TEX_LAYOUT_TILED
||
710 layout
== LP_TEX_LAYOUT_LINEAR
);
712 if (layout
== LP_TEX_LAYOUT_TILED
) {
713 /* for tiled layout, force a 32bpp format */
714 const enum pipe_format format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
715 const unsigned block_size
= util_format_get_blocksize(format
);
716 const unsigned nblocksy
=
717 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
718 const unsigned nblocksx
=
719 util_format_get_nblocksx(format
, align(width
, TILE_SIZE
));
720 const unsigned buffer_size
= block_size
* nblocksy
* nblocksx
;
724 /* we already computed this */
725 return lpr
->img_stride
[level
];
731 * Compute size (in bytes) need to store a texture image / mipmap level,
732 * including all cube faces or 3D image slices
735 tex_image_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
736 enum lp_texture_layout layout
)
738 const unsigned buf_size
= tex_image_face_size(lpr
, level
, layout
);
739 return buf_size
* lpr
->num_slices_faces
[level
];
744 * This function encapsulates some complicated logic for determining
745 * how to convert a tile of image data from linear layout to tiled
746 * layout, or vice versa.
747 * \param cur_layout the current tile layout
748 * \param target_layout the desired tile layout
749 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
750 * \param new_layout_return returns the new layout mode
751 * \param convert_return returns TRUE if image conversion is needed
754 layout_logic(enum lp_texture_layout cur_layout
,
755 enum lp_texture_layout target_layout
,
756 enum lp_texture_usage usage
,
757 enum lp_texture_layout
*new_layout_return
,
760 enum lp_texture_layout other_layout
, new_layout
;
764 new_layout
= 99; /* debug check */
766 if (target_layout
== LP_TEX_LAYOUT_LINEAR
) {
767 other_layout
= LP_TEX_LAYOUT_TILED
;
770 assert(target_layout
== LP_TEX_LAYOUT_TILED
);
771 other_layout
= LP_TEX_LAYOUT_LINEAR
;
774 new_layout
= target_layout
; /* may get changed below */
776 if (cur_layout
== LP_TEX_LAYOUT_BOTH
) {
777 if (usage
== LP_TEX_USAGE_READ
) {
778 new_layout
= LP_TEX_LAYOUT_BOTH
;
781 else if (cur_layout
== other_layout
) {
782 if (usage
!= LP_TEX_USAGE_WRITE_ALL
) {
783 /* need to convert tiled data to linear or vice versa */
786 if (usage
== LP_TEX_USAGE_READ
)
787 new_layout
= LP_TEX_LAYOUT_BOTH
;
791 assert(cur_layout
== LP_TEX_LAYOUT_NONE
||
792 cur_layout
== target_layout
);
795 assert(new_layout
== LP_TEX_LAYOUT_BOTH
||
796 new_layout
== target_layout
);
798 *new_layout_return
= new_layout
;
803 * Return pointer to a 2D texture image/face/slice.
804 * No tiled/linear conversion is done.
807 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
808 unsigned face_slice
, unsigned level
,
809 enum lp_texture_layout layout
)
811 struct llvmpipe_texture_image
*img
;
814 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
815 img
= &lpr
->linear
[level
];
818 assert (layout
== LP_TEX_LAYOUT_TILED
);
819 img
= &lpr
->tiled
[level
];
823 offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
827 return (ubyte
*) img
->data
+ offset
;
831 static INLINE
enum lp_texture_layout
832 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource
*lpr
,
833 unsigned face_slice
, unsigned level
,
834 unsigned x
, unsigned y
)
837 assert(resource_is_texture(&lpr
->base
));
838 assert(x
< lpr
->tiles_per_row
[level
]);
839 i
= face_slice
* lpr
->tiles_per_image
[level
]
840 + y
* lpr
->tiles_per_row
[level
] + x
;
841 return lpr
->layout
[level
][i
];
846 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource
*lpr
,
847 unsigned face_slice
, unsigned level
,
848 unsigned x
, unsigned y
,
849 enum lp_texture_layout layout
)
852 assert(resource_is_texture(&lpr
->base
));
853 assert(x
< lpr
->tiles_per_row
[level
]);
854 i
= face_slice
* lpr
->tiles_per_image
[level
]
855 + y
* lpr
->tiles_per_row
[level
] + x
;
856 lpr
->layout
[level
][i
] = layout
;
861 * Set the layout mode for all tiles in a particular image.
864 llvmpipe_set_texture_image_layout(struct llvmpipe_resource
*lpr
,
865 unsigned face_slice
, unsigned level
,
866 unsigned width_t
, unsigned height_t
,
867 enum lp_texture_layout layout
)
869 const unsigned start
= face_slice
* lpr
->tiles_per_image
[level
];
872 for (i
= 0; i
< width_t
* height_t
; i
++) {
873 lpr
->layout
[level
][start
+ i
] = layout
;
879 * Allocate storage for a linear or tile texture image (all cube
880 * faces and all 3D slices.
883 alloc_image_data(struct llvmpipe_resource
*lpr
, unsigned level
,
884 enum lp_texture_layout layout
)
889 if (layout
== LP_TEX_LAYOUT_TILED
) {
890 /* tiled data is stored in regular memory */
891 uint buffer_size
= tex_image_size(lpr
, level
, layout
);
892 lpr
->tiled
[level
].data
= align_malloc(buffer_size
, 16);
895 assert(layout
== LP_TEX_LAYOUT_LINEAR
);
897 /* we get the linear memory from the winsys */
898 struct llvmpipe_screen
*screen
= llvmpipe_screen(lpr
->base
.screen
);
899 struct sw_winsys
*winsys
= screen
->winsys
;
901 lpr
->linear
[0].data
=
902 winsys
->displaytarget_map(winsys
, lpr
->dt
,
903 PIPE_TRANSFER_READ_WRITE
);
906 /* not a display target - allocate regular memory */
907 uint buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
908 lpr
->linear
[level
].data
= align_malloc(buffer_size
, 16);
916 * Return pointer to texture image data (either linear or tiled layout)
917 * for a particular cube face or 3D texture slice.
919 * \param face_slice the cube face or 3D slice of interest
920 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
921 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
924 llvmpipe_get_texture_image(struct llvmpipe_resource
*lpr
,
925 unsigned face_slice
, unsigned level
,
926 enum lp_texture_usage usage
,
927 enum lp_texture_layout layout
)
930 * 'target' refers to the image which we're retrieving (either in
931 * tiled or linear layout).
932 * 'other' refers to the same image but in the other layout. (it may
935 struct llvmpipe_texture_image
*target_img
;
936 struct llvmpipe_texture_image
*other_img
;
939 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
940 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
941 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
942 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
943 enum lp_texture_layout other_layout
;
944 boolean only_allocate
;
946 assert(layout
== LP_TEX_LAYOUT_NONE
||
947 layout
== LP_TEX_LAYOUT_TILED
||
948 layout
== LP_TEX_LAYOUT_LINEAR
);
950 assert(usage
== LP_TEX_USAGE_READ
||
951 usage
== LP_TEX_USAGE_READ_WRITE
||
952 usage
== LP_TEX_USAGE_WRITE_ALL
);
954 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
955 if (layout
== LP_TEX_LAYOUT_NONE
) {
956 only_allocate
= TRUE
;
957 layout
= LP_TEX_LAYOUT_TILED
;
960 only_allocate
= FALSE
;
964 assert(lpr
->linear
[level
].data
);
967 /* which is target? which is other? */
968 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
969 target_img
= &lpr
->linear
[level
];
970 other_img
= &lpr
->tiled
[level
];
971 other_layout
= LP_TEX_LAYOUT_TILED
;
974 target_img
= &lpr
->tiled
[level
];
975 other_img
= &lpr
->linear
[level
];
976 other_layout
= LP_TEX_LAYOUT_LINEAR
;
979 target_data
= target_img
->data
;
980 other_data
= other_img
->data
;
983 /* allocate memory for the target image now */
984 alloc_image_data(lpr
, level
, layout
);
985 target_data
= target_img
->data
;
988 if (face_slice
> 0) {
989 unsigned target_offset
, other_offset
;
991 target_offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
992 other_offset
= face_slice
* tex_image_face_size(lpr
, level
, other_layout
);
994 target_data
= (uint8_t *) target_data
+ target_offset
;
997 other_data
= (uint8_t *) other_data
+ other_offset
;
1001 if (only_allocate
) {
1002 /* Just allocating tiled memory. Don't initialize it from the
1003 * linear data if it exists.
1009 /* may need to convert other data to the requested layout */
1010 enum lp_texture_layout new_layout
;
1013 /* loop over all image tiles, doing layout conversion where needed */
1014 for (y
= 0; y
< height_t
; y
++) {
1015 for (x
= 0; x
< width_t
; x
++) {
1016 enum lp_texture_layout cur_layout
=
1017 llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, x
, y
);
1020 layout_logic(cur_layout
, layout
, usage
, &new_layout
, &convert
);
1023 if (layout
== LP_TEX_LAYOUT_TILED
) {
1024 lp_linear_to_tiled(other_data
, target_data
,
1025 x
* TILE_SIZE
, y
* TILE_SIZE
,
1026 TILE_SIZE
, TILE_SIZE
,
1028 lpr
->row_stride
[level
],
1029 lpr
->tiles_per_row
[level
]);
1032 lp_tiled_to_linear(other_data
, target_data
,
1033 x
* TILE_SIZE
, y
* TILE_SIZE
,
1034 TILE_SIZE
, TILE_SIZE
,
1036 lpr
->row_stride
[level
],
1037 lpr
->tiles_per_row
[level
]);
1041 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, x
, y
,
1048 llvmpipe_set_texture_image_layout(lpr
, face_slice
, level
,
1049 width_t
, height_t
, layout
);
1052 assert(target_data
);
1059 * Return pointer to start of a texture image (1D, 2D, 3D, CUBE).
1060 * All cube faces and 3D slices will be converted to the requested
1062 * This is typically used when we're about to sample from a texture.
1065 llvmpipe_get_texture_image_all(struct llvmpipe_resource
*lpr
,
1067 enum lp_texture_usage usage
,
1068 enum lp_texture_layout layout
)
1070 const int slices
= lpr
->num_slices_faces
[level
];
1076 for (slice
= slices
- 1; slice
>= 0; slice
--) {
1077 map
= llvmpipe_get_texture_image(lpr
, slice
, level
, usage
, layout
);
1085 * Get pointer to a linear image (not the tile!) where the tile at (x,y)
1086 * is known to be in linear layout.
1087 * Conversion from tiled to linear will be done if necessary.
1088 * \return pointer to start of image/face (not the tile)
1091 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource
*lpr
,
1092 unsigned face_slice
, unsigned level
,
1093 enum lp_texture_usage usage
,
1094 unsigned x
, unsigned y
)
1096 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1097 enum lp_texture_layout cur_layout
, new_layout
;
1098 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1100 uint8_t *tiled_image
, *linear_image
;
1102 assert(resource_is_texture(&lpr
->base
));
1103 assert(x
% TILE_SIZE
== 0);
1104 assert(y
% TILE_SIZE
== 0);
1106 if (!linear_img
->data
) {
1107 /* allocate memory for the linear image now */
1108 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1111 /* compute address of the slice/face of the image that contains the tile */
1112 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1113 LP_TEX_LAYOUT_TILED
);
1114 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1115 LP_TEX_LAYOUT_LINEAR
);
1117 /* get current tile layout and determine if data conversion is needed */
1118 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1120 layout_logic(cur_layout
, LP_TEX_LAYOUT_LINEAR
, usage
,
1121 &new_layout
, &convert
);
1124 lp_tiled_to_linear(tiled_image
, linear_image
,
1125 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1126 lpr
->row_stride
[level
],
1127 lpr
->tiles_per_row
[level
]);
1130 if (new_layout
!= cur_layout
)
1131 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1133 return linear_image
;
1138 * Get pointer to tiled data for rendering.
1139 * \return pointer to the tiled data at the given tile position
1142 llvmpipe_get_texture_tile(struct llvmpipe_resource
*lpr
,
1143 unsigned face_slice
, unsigned level
,
1144 enum lp_texture_usage usage
,
1145 unsigned x
, unsigned y
)
1147 struct llvmpipe_texture_image
*tiled_img
= &lpr
->tiled
[level
];
1148 enum lp_texture_layout cur_layout
, new_layout
;
1149 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1151 uint8_t *tiled_image
, *linear_image
;
1152 unsigned tile_offset
;
1154 assert(x
% TILE_SIZE
== 0);
1155 assert(y
% TILE_SIZE
== 0);
1157 if (!tiled_img
->data
) {
1158 /* allocate memory for the tiled image now */
1159 alloc_image_data(lpr
, level
, LP_TEX_LAYOUT_TILED
);
1162 /* compute address of the slice/face of the image that contains the tile */
1163 tiled_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1164 LP_TEX_LAYOUT_TILED
);
1165 linear_image
= llvmpipe_get_texture_image_address(lpr
, face_slice
, level
,
1166 LP_TEX_LAYOUT_LINEAR
);
1168 /* get current tile layout and see if we need to convert the data */
1169 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1171 layout_logic(cur_layout
, LP_TEX_LAYOUT_TILED
, usage
, &new_layout
, &convert
);
1173 lp_linear_to_tiled(linear_image
, tiled_image
,
1174 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1175 lpr
->row_stride
[level
],
1176 lpr
->tiles_per_row
[level
]);
1179 if (new_layout
!= cur_layout
)
1180 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1182 /* compute, return address of the 64x64 tile */
1183 tile_offset
= (ty
* lpr
->tiles_per_row
[level
] + tx
)
1184 * TILE_SIZE
* TILE_SIZE
* 4;
1186 return (ubyte
*) tiled_image
+ tile_offset
;
1191 * Return size of resource in bytes
1194 llvmpipe_resource_size(const struct pipe_resource
*resource
)
1196 const struct llvmpipe_resource
*lpr
= llvmpipe_resource_const(resource
);
1197 unsigned lvl
, size
= 0;
1199 for (lvl
= 0; lvl
<= lpr
->base
.last_level
; lvl
++) {
1200 if (lpr
->linear
[lvl
].data
)
1201 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_LINEAR
);
1203 if (lpr
->tiled
[lvl
].data
)
1204 size
+= tex_image_size(lpr
, lvl
, LP_TEX_LAYOUT_TILED
);
1212 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
1214 screen
->resource_create
= llvmpipe_resource_create
;
1215 screen
->resource_destroy
= llvmpipe_resource_destroy
;
1216 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
1217 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
1218 screen
->user_buffer_create
= llvmpipe_user_buffer_create
;
1220 screen
->get_tex_surface
= llvmpipe_get_tex_surface
;
1221 screen
->tex_surface_destroy
= llvmpipe_tex_surface_destroy
;
1226 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
1228 pipe
->get_transfer
= llvmpipe_get_transfer
;
1229 pipe
->transfer_destroy
= llvmpipe_transfer_destroy
;
1230 pipe
->transfer_map
= llvmpipe_transfer_map
;
1231 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
1232 pipe
->is_resource_referenced
= llvmpipe_is_resource_referenced
;
1234 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
1235 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;