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 num_slices
, unsigned width
, unsigned height
)
77 const unsigned tx
= align(width
, TILE_SIZE
) / TILE_SIZE
;
78 const unsigned ty
= align(height
, TILE_SIZE
) / TILE_SIZE
;
80 assert(num_slices
* tx
* ty
> 0);
81 assert(LP_TEX_LAYOUT_NONE
== 0); /* calloc'ing LP_TEX_LAYOUT_NONE here */
83 return (enum lp_texture_layout
*)
84 calloc(num_slices
* 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
;
101 unsigned depth
= pt
->depth0
;
103 assert(LP_MAX_TEXTURE_2D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
104 assert(LP_MAX_TEXTURE_3D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
106 for (level
= 0; level
<= pt
->last_level
; level
++) {
107 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
108 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
109 unsigned nblocksx
, num_slices
;
111 if (lpr
->base
.target
== PIPE_TEXTURE_CUBE
)
113 else if (lpr
->base
.target
== PIPE_TEXTURE_3D
)
118 /* Allocate storage for whole quads. This is particularly important
119 * for depth surfaces, which are currently stored in a swizzled format.
121 nblocksx
= util_format_get_nblocksx(pt
->format
, align(width
, TILE_SIZE
));
123 lpr
->row_stride
[level
] =
124 align(nblocksx
* util_format_get_blocksize(pt
->format
), 16);
126 lpr
->img_stride
[level
] = lpr
->row_stride
[level
] * align(height
, TILE_SIZE
);
128 lpr
->tiles_per_row
[level
] = width_t
;
129 lpr
->tiles_per_image
[level
] = width_t
* height_t
;
130 lpr
->num_slices_faces
[level
] = num_slices
;
131 lpr
->layout
[level
] = alloc_layout_array(num_slices
, width
, height
);
133 width
= u_minify(width
, 1);
134 height
= u_minify(height
, 1);
135 depth
= u_minify(depth
, 1);
144 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
145 struct llvmpipe_resource
*lpr
)
147 struct sw_winsys
*winsys
= screen
->winsys
;
149 /* Round up the surface size to a multiple of the tile size to
150 * avoid tile clipping.
152 const unsigned width
= align(lpr
->base
.width0
, TILE_SIZE
);
153 const unsigned height
= align(lpr
->base
.height0
, TILE_SIZE
);
154 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
155 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
157 lpr
->tiles_per_row
[0] = width_t
;
158 lpr
->tiles_per_image
[0] = width_t
* height_t
;
159 lpr
->num_slices_faces
[0] = 1;
160 lpr
->img_stride
[0] = 0;
162 lpr
->layout
[0] = alloc_layout_array(1, width
, height
);
164 lpr
->dt
= winsys
->displaytarget_create(winsys
,
169 &lpr
->row_stride
[0] );
171 return lpr
->dt
!= NULL
;
175 static struct pipe_resource
*
176 llvmpipe_resource_create(struct pipe_screen
*_screen
,
177 const struct pipe_resource
*templat
)
179 static unsigned id_counter
= 0;
180 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
181 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
185 lpr
->base
= *templat
;
186 pipe_reference_init(&lpr
->base
.reference
, 1);
187 lpr
->base
.screen
= &screen
->base
;
189 assert(lpr
->base
.bind
);
191 if (lpr
->base
.bind
& (PIPE_BIND_DISPLAY_TARGET
|
194 /* displayable surface */
195 if (!llvmpipe_displaytarget_layout(screen
, lpr
))
197 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
199 else if (lpr
->base
.bind
& (PIPE_BIND_SAMPLER_VIEW
|
200 PIPE_BIND_DEPTH_STENCIL
)) {
202 if (!llvmpipe_texture_layout(screen
, lpr
))
204 assert(lpr
->layout
[0][0] == LP_TEX_LAYOUT_NONE
);
207 /* other data (vertex buffer, const buffer, etc) */
208 const enum pipe_format format
= templat
->format
;
209 const uint w
= templat
->width0
/ util_format_get_blockheight(format
);
210 const uint h
= templat
->height0
/ util_format_get_blockwidth(format
);
211 const uint d
= templat
->depth0
;
212 const uint bpp
= util_format_get_blocksize(format
);
213 const uint bytes
= w
* h
* d
* bpp
;
214 lpr
->data
= align_malloc(bytes
, 16);
219 if (resource_is_texture(&lpr
->base
)) {
220 assert(lpr
->layout
[0]);
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 else if (resource_is_texture(pt
)) {
246 /* regular texture */
249 /* free linear image data */
250 for (level
= 0; level
< Elements(lpr
->linear
); level
++) {
251 if (lpr
->linear
[level
].data
) {
252 align_free(lpr
->linear
[level
].data
);
253 lpr
->linear
[level
].data
= NULL
;
257 /* free tiled image data */
258 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
259 if (lpr
->tiled
[level
].data
) {
260 align_free(lpr
->tiled
[level
].data
);
261 lpr
->tiled
[level
].data
= NULL
;
265 /* free layout flag arrays */
266 for (level
= 0; level
< Elements(lpr
->tiled
); level
++) {
267 free(lpr
->layout
[level
]);
268 lpr
->layout
[level
] = NULL
;
271 else if (!lpr
->userBuffer
) {
273 align_free(lpr
->data
);
281 * Map a resource for read/write.
284 llvmpipe_resource_map(struct pipe_resource
*resource
,
288 enum lp_texture_usage tex_usage
,
289 enum lp_texture_layout layout
)
291 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
295 assert(level
< LP_MAX_TEXTURE_LEVELS
);
297 assert(tex_usage
== LP_TEX_USAGE_READ
||
298 tex_usage
== LP_TEX_USAGE_READ_WRITE
||
299 tex_usage
== LP_TEX_USAGE_WRITE_ALL
);
301 assert(layout
== LP_TEX_LAYOUT_NONE
||
302 layout
== LP_TEX_LAYOUT_TILED
||
303 layout
== LP_TEX_LAYOUT_LINEAR
);
307 struct llvmpipe_screen
*screen
= llvmpipe_screen(resource
->screen
);
308 struct sw_winsys
*winsys
= screen
->winsys
;
311 if (tex_usage
== LP_TEX_USAGE_READ
) {
312 dt_usage
= PIPE_TRANSFER_READ
;
315 dt_usage
= PIPE_TRANSFER_READ_WRITE
;
322 /* FIXME: keep map count? */
323 map
= winsys
->displaytarget_map(winsys
, lpr
->dt
, dt_usage
);
325 /* install this linear image in texture data structure */
326 lpr
->linear
[level
].data
= map
;
328 map
= llvmpipe_get_texture_image(lpr
, face
+ zslice
, level
,
334 else if (resource_is_texture(resource
)) {
335 /* regular texture */
336 if (resource
->target
!= PIPE_TEXTURE_CUBE
) {
339 if (resource
->target
!= PIPE_TEXTURE_3D
) {
343 map
= llvmpipe_get_texture_image(lpr
, face
+ zslice
, level
,
358 llvmpipe_resource_unmap(struct pipe_resource
*resource
,
363 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
367 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(resource
->screen
);
368 struct sw_winsys
*winsys
= lp_screen
->winsys
;
374 /* make sure linear image is up to date */
375 (void) llvmpipe_get_texture_image(lpr
, face
+ zslice
, level
,
377 LP_TEX_LAYOUT_LINEAR
);
379 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
385 llvmpipe_resource_data(struct pipe_resource
*resource
)
387 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
389 assert((lpr
->base
.bind
& (PIPE_BIND_DISPLAY_TARGET
|
392 PIPE_BIND_SAMPLER_VIEW
)) == 0);
398 static struct pipe_resource
*
399 llvmpipe_resource_from_handle(struct pipe_screen
*screen
,
400 const struct pipe_resource
*template,
401 struct winsys_handle
*whandle
)
403 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
404 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
408 lpr
->base
= *template;
409 pipe_reference_init(&lpr
->base
.reference
, 1);
410 lpr
->base
.screen
= screen
;
412 lpr
->dt
= winsys
->displaytarget_from_handle(winsys
,
415 &lpr
->row_stride
[0]);
428 llvmpipe_resource_get_handle(struct pipe_screen
*screen
,
429 struct pipe_resource
*pt
,
430 struct winsys_handle
*whandle
)
432 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
433 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
439 return winsys
->displaytarget_get_handle(winsys
, lpr
->dt
, whandle
);
443 static struct pipe_surface
*
444 llvmpipe_get_tex_surface(struct pipe_screen
*screen
,
445 struct pipe_resource
*pt
,
446 unsigned face
, unsigned level
, unsigned zslice
,
447 enum lp_texture_usage usage
)
449 struct pipe_surface
*ps
;
451 assert(level
<= pt
->last_level
);
453 ps
= CALLOC_STRUCT(pipe_surface
);
455 pipe_reference_init(&ps
->reference
, 1);
456 pipe_resource_reference(&ps
->texture
, pt
);
457 ps
->format
= pt
->format
;
458 ps
->width
= u_minify(pt
->width0
, level
);
459 ps
->height
= u_minify(pt
->height0
, level
);
471 llvmpipe_tex_surface_destroy(struct pipe_surface
*surf
)
473 /* Effectively do the texture_update work here - if texture images
474 * needed post-processing to put them into hardware layout, this is
475 * where it would happen. For llvmpipe, nothing to do.
477 assert(surf
->texture
);
478 pipe_resource_reference(&surf
->texture
, NULL
);
483 static struct pipe_transfer
*
484 llvmpipe_get_transfer(struct pipe_context
*pipe
,
485 struct pipe_resource
*resource
,
486 struct pipe_subresource sr
,
488 const struct pipe_box
*box
)
490 struct llvmpipe_resource
*lprex
= llvmpipe_resource(resource
);
491 struct llvmpipe_transfer
*lpr
;
494 assert(sr
.level
<= resource
->last_level
);
496 lpr
= CALLOC_STRUCT(llvmpipe_transfer
);
498 struct pipe_transfer
*pt
= &lpr
->base
;
499 pipe_resource_reference(&pt
->resource
, resource
);
502 pt
->stride
= lprex
->row_stride
[sr
.level
];
512 llvmpipe_transfer_destroy(struct pipe_context
*pipe
,
513 struct pipe_transfer
*transfer
)
515 /* Effectively do the texture_update work here - if texture images
516 * needed post-processing to put them into hardware layout, this is
517 * where it would happen. For llvmpipe, nothing to do.
519 assert (transfer
->resource
);
520 pipe_resource_reference(&transfer
->resource
, NULL
);
526 llvmpipe_transfer_map( struct pipe_context
*pipe
,
527 struct pipe_transfer
*transfer
)
529 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
531 struct llvmpipe_resource
*lpr
;
532 enum pipe_format format
;
533 enum lp_texture_usage tex_usage
;
536 assert(transfer
->sr
.face
< 6);
537 assert(transfer
->sr
.level
< LP_MAX_TEXTURE_LEVELS
);
540 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
541 transfer->x, transfer->y, transfer->width, transfer->height,
542 transfer->texture->width0,
543 transfer->texture->height0,
547 if (transfer
->usage
== PIPE_TRANSFER_READ
) {
548 tex_usage
= LP_TEX_USAGE_READ
;
552 tex_usage
= LP_TEX_USAGE_READ_WRITE
;
557 struct llvmpipe_resource
*lpr
= llvmpipe_resource(transfer
->resource
);
558 printf("transfer map tex %u mode %s\n", lpr
->id
, mode
);
562 assert(transfer
->resource
);
563 lpr
= llvmpipe_resource(transfer
->resource
);
564 format
= lpr
->base
.format
;
567 * Transfers, like other pipe operations, must happen in order, so flush the
568 * context if necessary.
570 llvmpipe_flush_texture(pipe
,
575 !(transfer
->usage
& PIPE_TRANSFER_WRITE
), /* read_only */
576 TRUE
, /* cpu_access */
577 FALSE
); /* do_not_flush */
579 map
= llvmpipe_resource_map(transfer
->resource
,
583 tex_usage
, LP_TEX_LAYOUT_LINEAR
);
586 /* May want to do different things here depending on read/write nature
589 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
590 /* Do something to notify sharing contexts of a texture change.
596 transfer
->box
.y
/ util_format_get_blockheight(format
) * transfer
->stride
+
597 transfer
->box
.x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
604 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
605 struct pipe_transfer
*transfer
)
607 assert(transfer
->resource
);
609 llvmpipe_resource_unmap(transfer
->resource
,
616 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
617 struct pipe_resource
*presource
,
618 unsigned face
, unsigned level
)
620 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
622 if (presource
->target
== PIPE_BUFFER
)
623 return PIPE_UNREFERENCED
;
625 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
631 * Create buffer which wraps user-space data.
633 static struct pipe_resource
*
634 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
639 struct llvmpipe_resource
*buffer
;
641 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
645 pipe_reference_init(&buffer
->base
.reference
, 1);
646 buffer
->base
.screen
= screen
;
647 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
648 buffer
->base
.bind
= bind_flags
;
649 buffer
->base
._usage
= PIPE_USAGE_IMMUTABLE
;
650 buffer
->base
.flags
= 0;
651 buffer
->base
.width0
= bytes
;
652 buffer
->base
.height0
= 1;
653 buffer
->base
.depth0
= 1;
654 buffer
->userBuffer
= TRUE
;
657 return &buffer
->base
;
662 * Compute size (in bytes) need to store a texture image / mipmap level,
663 * for just one cube face or one 3D texture slice
666 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
667 enum lp_texture_layout layout
)
669 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
670 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
672 assert(layout
== LP_TEX_LAYOUT_TILED
||
673 layout
== LP_TEX_LAYOUT_LINEAR
);
675 if (layout
== LP_TEX_LAYOUT_TILED
) {
676 /* for tiled layout, force a 32bpp format */
677 const enum pipe_format format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
678 const unsigned block_size
= util_format_get_blocksize(format
);
679 const unsigned nblocksy
=
680 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
681 const unsigned nblocksx
=
682 util_format_get_nblocksx(format
, align(width
, TILE_SIZE
));
683 const unsigned buffer_size
= block_size
* nblocksy
* nblocksx
;
687 const enum pipe_format format
= lpr
->base
.format
;
688 const unsigned nblocksy
=
689 util_format_get_nblocksy(format
, align(height
, TILE_SIZE
));
690 const unsigned buffer_size
= nblocksy
* lpr
->row_stride
[level
];
697 * Compute size (in bytes) need to store a texture image / mipmap level,
698 * including all cube faces or 3D image slices
701 tex_image_size(const struct llvmpipe_resource
*lpr
, unsigned level
,
702 enum lp_texture_layout layout
)
704 const unsigned buf_size
= tex_image_face_size(lpr
, level
, layout
);
705 return buf_size
* lpr
->num_slices_faces
[level
];
710 * This function encapsulates some complicated logic for determining
711 * how to convert a tile of image data from linear layout to tiled
712 * layout, or vice versa.
713 * \param cur_layout the current tile layout
714 * \param target_layout the desired tile layout
715 * \param usage how the tile will be accessed (R/W vs. read-only, etc)
716 * \param new_layout_return returns the new layout mode
717 * \param convert_return returns TRUE if image conversion is needed
720 layout_logic(enum lp_texture_layout cur_layout
,
721 enum lp_texture_layout target_layout
,
722 enum lp_texture_usage usage
,
723 enum lp_texture_layout
*new_layout_return
,
726 enum lp_texture_layout other_layout
, new_layout
;
730 new_layout
= 99; /* debug check */
732 if (target_layout
== LP_TEX_LAYOUT_LINEAR
) {
733 other_layout
= LP_TEX_LAYOUT_TILED
;
736 assert(target_layout
== LP_TEX_LAYOUT_TILED
);
737 other_layout
= LP_TEX_LAYOUT_LINEAR
;
740 new_layout
= target_layout
; /* may get changed below */
742 if (cur_layout
== LP_TEX_LAYOUT_BOTH
) {
743 if (usage
== LP_TEX_USAGE_READ
) {
744 new_layout
= LP_TEX_LAYOUT_BOTH
;
747 else if (cur_layout
== other_layout
) {
748 if (usage
!= LP_TEX_USAGE_WRITE_ALL
) {
749 /* need to convert tiled data to linear or vice versa */
752 if (usage
== LP_TEX_USAGE_READ
)
753 new_layout
= LP_TEX_LAYOUT_BOTH
;
757 assert(cur_layout
== LP_TEX_LAYOUT_NONE
||
758 cur_layout
== target_layout
);
761 assert(new_layout
== LP_TEX_LAYOUT_BOTH
||
762 new_layout
== target_layout
);
764 *new_layout_return
= new_layout
;
769 * Return pointer to a texture image. No tiled/linear conversion is done.
772 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
773 unsigned face
, unsigned level
,
774 enum lp_texture_layout layout
)
776 struct llvmpipe_texture_image
*img
;
777 unsigned face_offset
;
779 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
780 img
= &lpr
->linear
[level
];
783 assert (layout
== LP_TEX_LAYOUT_TILED
);
784 img
= &lpr
->tiled
[level
];
788 face_offset
= face
* tex_image_face_size(lpr
, level
, layout
);
792 return (ubyte
*) img
->data
+ face_offset
;
796 static INLINE
enum lp_texture_layout
797 llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource
*lpr
,
798 unsigned face_slice
, unsigned level
,
799 unsigned x
, unsigned y
)
802 assert(resource_is_texture(&lpr
->base
));
803 assert(x
< lpr
->tiles_per_row
[level
]);
804 i
= face_slice
* lpr
->tiles_per_image
[level
]
805 + y
* lpr
->tiles_per_row
[level
] + x
;
806 return lpr
->layout
[level
][i
];
811 llvmpipe_set_texture_tile_layout(struct llvmpipe_resource
*lpr
,
812 unsigned face_slice
, unsigned level
,
813 unsigned x
, unsigned y
,
814 enum lp_texture_layout layout
)
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 lpr
->layout
[level
][i
] = layout
;
826 * Set the layout mode for all tiles in a particular image.
829 llvmpipe_set_texture_image_layout(struct llvmpipe_resource
*lpr
,
830 unsigned face_slice
, unsigned level
,
831 unsigned width_t
, unsigned height_t
,
832 enum lp_texture_layout layout
)
834 const unsigned start
= face_slice
* lpr
->tiles_per_image
[level
];
837 for (i
= 0; i
< width_t
* height_t
; i
++) {
838 lpr
->layout
[level
][start
+ i
] = layout
;
844 * Return pointer to texture image data (either linear or tiled layout)
845 * for a particular cube face or 3D texture slice.
847 * \param face_slice the cube face or 3D slice of interest
848 * \param usage one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
849 * \param layout either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
852 llvmpipe_get_texture_image(struct llvmpipe_resource
*lpr
,
853 unsigned face_slice
, unsigned level
,
854 enum lp_texture_usage usage
,
855 enum lp_texture_layout layout
)
858 * 'target' refers to the image which we're retrieving (either in
859 * tiled or linear layout).
860 * 'other' refers to the same image but in the other layout. (it may
863 struct llvmpipe_texture_image
*target_img
;
864 struct llvmpipe_texture_image
*other_img
;
867 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
868 const unsigned height
= u_minify(lpr
->base
.height0
, level
);
869 const unsigned width_t
= align(width
, TILE_SIZE
) / TILE_SIZE
;
870 const unsigned height_t
= align(height
, TILE_SIZE
) / TILE_SIZE
;
871 enum lp_texture_layout other_layout
;
872 boolean only_allocate
;
874 assert(layout
== LP_TEX_LAYOUT_NONE
||
875 layout
== LP_TEX_LAYOUT_TILED
||
876 layout
== LP_TEX_LAYOUT_LINEAR
);
878 assert(usage
== LP_TEX_USAGE_READ
||
879 usage
== LP_TEX_USAGE_READ_WRITE
||
880 usage
== LP_TEX_USAGE_WRITE_ALL
);
882 /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
883 if (layout
== LP_TEX_LAYOUT_NONE
) {
884 only_allocate
= TRUE
;
885 layout
= LP_TEX_LAYOUT_TILED
;
888 only_allocate
= FALSE
;
892 assert(lpr
->linear
[level
].data
);
895 /* which is target? which is other? */
896 if (layout
== LP_TEX_LAYOUT_LINEAR
) {
897 target_img
= &lpr
->linear
[level
];
898 other_img
= &lpr
->tiled
[level
];
899 other_layout
= LP_TEX_LAYOUT_TILED
;
902 target_img
= &lpr
->tiled
[level
];
903 other_img
= &lpr
->linear
[level
];
904 other_layout
= LP_TEX_LAYOUT_LINEAR
;
907 target_data
= target_img
->data
;
908 other_data
= other_img
->data
;
911 /* allocate memory for the target image now */
912 unsigned buffer_size
= tex_image_size(lpr
, level
, layout
);
913 target_img
->data
= align_malloc(buffer_size
, 16);
914 target_data
= target_img
->data
;
917 if (face_slice
> 0) {
918 unsigned target_offset
, other_offset
;
920 target_offset
= face_slice
* tex_image_face_size(lpr
, level
, layout
);
921 other_offset
= face_slice
* tex_image_face_size(lpr
, level
, other_layout
);
923 target_data
= (uint8_t *) target_data
+ target_offset
;
926 other_data
= (uint8_t *) other_data
+ other_offset
;
931 /* Just allocating tiled memory. Don't initialize it from the
932 * linear data if it exists.
934 llvmpipe_set_texture_image_layout(lpr
, face_slice
, level
,
935 width_t
, height_t
, layout
);
941 /* may need to convert other data to the requested layout */
942 enum lp_texture_layout new_layout
;
945 /* loop over all image tiles, doing layout conversion where needed */
946 for (y
= 0; y
< height_t
; y
++) {
947 for (x
= 0; x
< width_t
; x
++) {
948 enum lp_texture_layout cur_layout
=
949 llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, x
, y
);
952 layout_logic(cur_layout
, layout
, usage
, &new_layout
, &convert
);
955 if (layout
== LP_TEX_LAYOUT_TILED
) {
956 lp_linear_to_tiled(other_data
, target_data
,
957 x
* TILE_SIZE
, y
* TILE_SIZE
,
958 TILE_SIZE
, TILE_SIZE
,
960 lpr
->row_stride
[level
]);
963 lp_tiled_to_linear(other_data
, target_data
,
964 x
* TILE_SIZE
, y
* TILE_SIZE
,
965 TILE_SIZE
, TILE_SIZE
,
967 lpr
->row_stride
[level
]);
971 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, x
, y
,
978 llvmpipe_set_texture_image_layout(lpr
, face_slice
, level
,
979 width_t
, height_t
, layout
);
989 * Return pointer to start of a texture image (1D, 2D, 3D, CUBE).
990 * All cube faces and 3D slices will be converted to the requested
992 * This is typically used when we're about to sample from a texture.
995 llvmpipe_get_texture_image_all(struct llvmpipe_resource
*lpr
,
997 enum lp_texture_usage usage
,
998 enum lp_texture_layout layout
)
1000 const int slices
= lpr
->num_slices_faces
[level
];
1006 for (slice
= slices
- 1; slice
>= 0; slice
--) {
1007 map
= llvmpipe_get_texture_image(lpr
, slice
, level
, usage
, layout
);
1016 * Get pointer to a linear image where the tile at (x,y) is known to be
1018 * Conversion from tiled to linear will be done if necessary.
1019 * \return pointer to start of image/face (not the tile)
1022 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource
*lpr
,
1023 unsigned face_slice
, unsigned level
,
1024 enum lp_texture_usage usage
,
1025 unsigned x
, unsigned y
)
1027 struct llvmpipe_texture_image
*tiled_img
= &lpr
->tiled
[level
];
1028 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1029 enum lp_texture_layout cur_layout
, new_layout
;
1030 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1033 assert(resource_is_texture(&lpr
->base
));
1034 assert(x
% TILE_SIZE
== 0);
1035 assert(y
% TILE_SIZE
== 0);
1037 if (!linear_img
->data
) {
1038 /* allocate memory for the tiled image now */
1039 unsigned buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1040 linear_img
->data
= align_malloc(buffer_size
, 16);
1043 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1045 layout_logic(cur_layout
, LP_TEX_LAYOUT_LINEAR
, usage
,
1046 &new_layout
, &convert
);
1049 lp_tiled_to_linear(tiled_img
->data
, linear_img
->data
,
1050 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1051 lpr
->row_stride
[level
]);
1054 if (new_layout
!= cur_layout
)
1055 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1057 if (face_slice
> 0) {
1059 = face_slice
* tex_image_face_size(lpr
, level
, LP_TEX_LAYOUT_LINEAR
);
1060 return (ubyte
*) linear_img
->data
+ offset
;
1063 return linear_img
->data
;
1069 * Get pointer to tiled data for rendering.
1070 * \return pointer to the tiled data at the given tile position
1073 llvmpipe_get_texture_tile(struct llvmpipe_resource
*lpr
,
1074 unsigned face_slice
, unsigned level
,
1075 enum lp_texture_usage usage
,
1076 unsigned x
, unsigned y
)
1078 const unsigned width
= u_minify(lpr
->base
.width0
, level
);
1079 struct llvmpipe_texture_image
*tiled_img
= &lpr
->tiled
[level
];
1080 struct llvmpipe_texture_image
*linear_img
= &lpr
->linear
[level
];
1081 enum lp_texture_layout cur_layout
, new_layout
;
1082 const unsigned tx
= x
/ TILE_SIZE
, ty
= y
/ TILE_SIZE
;
1085 assert(x
% TILE_SIZE
== 0);
1086 assert(y
% TILE_SIZE
== 0);
1088 if (!tiled_img
->data
) {
1089 /* allocate memory for the tiled image now */
1090 unsigned buffer_size
= tex_image_size(lpr
, level
, LP_TEX_LAYOUT_TILED
);
1091 tiled_img
->data
= align_malloc(buffer_size
, 16);
1094 cur_layout
= llvmpipe_get_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
);
1096 layout_logic(cur_layout
, LP_TEX_LAYOUT_TILED
, usage
, &new_layout
, &convert
);
1098 lp_linear_to_tiled(linear_img
->data
, tiled_img
->data
,
1099 x
, y
, TILE_SIZE
, TILE_SIZE
, lpr
->base
.format
,
1100 lpr
->row_stride
[level
]);
1103 if (new_layout
!= cur_layout
)
1104 llvmpipe_set_texture_tile_layout(lpr
, face_slice
, level
, tx
, ty
, new_layout
);
1106 /* compute, return address of the 64x64 tile */
1108 unsigned tiles_per_row
, tile_offset
, face_slice_offset
;
1110 tiles_per_row
= align(width
, TILE_SIZE
) / TILE_SIZE
;
1112 assert(tiles_per_row
== lpr
->tiles_per_row
[level
]);
1114 tile_offset
= ty
* tiles_per_row
+ tx
;
1115 tile_offset
*= TILE_SIZE
* TILE_SIZE
* 4;
1117 assert(tiled_img
->data
);
1119 face_slice_offset
= (face_slice
> 0)
1120 ? (face_slice
* tex_image_face_size(lpr
, level
, LP_TEX_LAYOUT_TILED
))
1123 return (ubyte
*) tiled_img
->data
+ face_slice_offset
+ tile_offset
;
1129 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
1131 screen
->resource_create
= llvmpipe_resource_create
;
1132 screen
->resource_destroy
= llvmpipe_resource_destroy
;
1133 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
1134 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
1135 screen
->user_buffer_create
= llvmpipe_user_buffer_create
;
1137 screen
->get_tex_surface
= llvmpipe_get_tex_surface
;
1138 screen
->tex_surface_destroy
= llvmpipe_tex_surface_destroy
;
1143 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
1145 pipe
->get_transfer
= llvmpipe_get_transfer
;
1146 pipe
->transfer_destroy
= llvmpipe_transfer_destroy
;
1147 pipe
->transfer_map
= llvmpipe_transfer_map
;
1148 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
1149 pipe
->is_resource_referenced
= llvmpipe_is_resource_referenced
;
1151 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
1152 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;