1 /**************************************************************************
3 * Copyright 2006 VMware, Inc.
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 VMWARE 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 <keithw@vmware.com>
30 * Michel Dänzer <daenzer@vmware.com>
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
38 #include "util/u_inlines.h"
39 #include "util/u_cpu_detect.h"
40 #include "util/u_format.h"
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43 #include "util/simple_list.h"
44 #include "util/u_transfer.h"
46 #include "lp_context.h"
48 #include "lp_screen.h"
49 #include "lp_texture.h"
54 #include "state_tracker/sw_winsys.h"
58 static struct llvmpipe_resource resource_list
;
60 static unsigned id_counter
= 0;
64 * Conventional allocation path for non-display textures:
65 * Compute strides and allocate data (unless asked not to).
68 llvmpipe_texture_layout(struct llvmpipe_screen
*screen
,
69 struct llvmpipe_resource
*lpr
,
72 struct pipe_resource
*pt
= &lpr
->base
;
74 unsigned width
= pt
->width0
;
75 unsigned height
= pt
->height0
;
76 unsigned depth
= pt
->depth0
;
77 uint64_t total_size
= 0;
78 unsigned layers
= pt
->array_size
;
80 * This alignment here (same for displaytarget) was added for the purpose of
81 * ARB_map_buffer_alignment. I am not convinced it's needed for non-buffer
82 * resources. Otherwise we'd want the max of cacheline size and 16 (max size
83 * of a block for all formats) though this should not be strictly necessary
84 * neither. In any case it can only affect compressed or 1d textures.
86 unsigned mip_align
= MAX2(64, util_cpu_caps
.cacheline
);
88 assert(LP_MAX_TEXTURE_2D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
89 assert(LP_MAX_TEXTURE_3D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
91 for (level
= 0; level
<= pt
->last_level
; level
++) {
93 unsigned align_x
, align_y
, nblocksx
, nblocksy
, block_size
, num_slices
;
95 /* Row stride and image stride */
97 /* For non-compressed formats we need 4x4 pixel alignment
98 * so we can read/write LP_RASTER_BLOCK_SIZE when rendering to them.
99 * We also want cache line size in x direction,
100 * otherwise same cache line could end up in multiple threads.
101 * For explicit 1d resources however we reduce this to 4x1 and
102 * handle specially in render output code (as we need to do special
103 * handling there for buffers in any case).
105 if (util_format_is_compressed(pt
->format
))
106 align_x
= align_y
= 1;
108 align_x
= LP_RASTER_BLOCK_SIZE
;
109 if (llvmpipe_resource_is_1d(&lpr
->base
))
112 align_y
= LP_RASTER_BLOCK_SIZE
;
115 nblocksx
= util_format_get_nblocksx(pt
->format
,
116 align(width
, align_x
));
117 nblocksy
= util_format_get_nblocksy(pt
->format
,
118 align(height
, align_y
));
119 block_size
= util_format_get_blocksize(pt
->format
);
121 if (util_format_is_compressed(pt
->format
))
122 lpr
->row_stride
[level
] = nblocksx
* block_size
;
124 lpr
->row_stride
[level
] = align(nblocksx
* block_size
, util_cpu_caps
.cacheline
);
126 /* if row_stride * height > LP_MAX_TEXTURE_SIZE */
127 if ((uint64_t)lpr
->row_stride
[level
] * nblocksy
> LP_MAX_TEXTURE_SIZE
) {
128 /* image too large */
132 lpr
->img_stride
[level
] = lpr
->row_stride
[level
] * nblocksy
;
134 /* Number of 3D image slices, cube faces or texture array layers */
135 if (lpr
->base
.target
== PIPE_TEXTURE_CUBE
) {
139 if (lpr
->base
.target
== PIPE_TEXTURE_3D
)
141 else if (lpr
->base
.target
== PIPE_TEXTURE_1D_ARRAY
||
142 lpr
->base
.target
== PIPE_TEXTURE_2D_ARRAY
||
143 lpr
->base
.target
== PIPE_TEXTURE_CUBE
||
144 lpr
->base
.target
== PIPE_TEXTURE_CUBE_ARRAY
)
149 /* if img_stride * num_slices_faces > LP_MAX_TEXTURE_SIZE */
150 mipsize
= (uint64_t)lpr
->img_stride
[level
] * num_slices
;
151 if (mipsize
> LP_MAX_TEXTURE_SIZE
) {
152 /* volume too large */
156 lpr
->mip_offsets
[level
] = total_size
;
158 total_size
+= align((unsigned)mipsize
, mip_align
);
159 if (total_size
> LP_MAX_TEXTURE_SIZE
) {
163 /* Compute size of next mipmap level */
164 width
= u_minify(width
, 1);
165 height
= u_minify(height
, 1);
166 depth
= u_minify(depth
, 1);
170 lpr
->tex_data
= align_malloc(total_size
, mip_align
);
171 if (!lpr
->tex_data
) {
175 memset(lpr
->tex_data
, 0, total_size
);
187 * Check the size of the texture specified by 'res'.
188 * \return TRUE if OK, FALSE if too large.
191 llvmpipe_can_create_resource(struct pipe_screen
*screen
,
192 const struct pipe_resource
*res
)
194 struct llvmpipe_resource lpr
;
195 memset(&lpr
, 0, sizeof(lpr
));
197 return llvmpipe_texture_layout(llvmpipe_screen(screen
), &lpr
, false);
202 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
203 struct llvmpipe_resource
*lpr
,
204 const void *map_front_private
)
206 struct sw_winsys
*winsys
= screen
->winsys
;
208 /* Round up the surface size to a multiple of the tile size to
209 * avoid tile clipping.
211 const unsigned width
= MAX2(1, align(lpr
->base
.width0
, TILE_SIZE
));
212 const unsigned height
= MAX2(1, align(lpr
->base
.height0
, TILE_SIZE
));
214 lpr
->dt
= winsys
->displaytarget_create(winsys
,
220 &lpr
->row_stride
[0] );
225 if (!map_front_private
) {
226 void *map
= winsys
->displaytarget_map(winsys
, lpr
->dt
,
227 PIPE_TRANSFER_WRITE
);
230 memset(map
, 0, height
* lpr
->row_stride
[0]);
232 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
239 static struct pipe_resource
*
240 llvmpipe_resource_create_front(struct pipe_screen
*_screen
,
241 const struct pipe_resource
*templat
,
242 const void *map_front_private
)
244 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
245 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
249 lpr
->base
= *templat
;
250 pipe_reference_init(&lpr
->base
.reference
, 1);
251 lpr
->base
.screen
= &screen
->base
;
253 /* assert(lpr->base.bind); */
255 if (llvmpipe_resource_is_texture(&lpr
->base
)) {
256 if (lpr
->base
.bind
& (PIPE_BIND_DISPLAY_TARGET
|
259 /* displayable surface */
260 if (!llvmpipe_displaytarget_layout(screen
, lpr
, map_front_private
))
265 if (!llvmpipe_texture_layout(screen
, lpr
, true))
270 /* other data (vertex buffer, const buffer, etc) */
271 const uint bytes
= templat
->width0
;
272 assert(util_format_get_blocksize(templat
->format
) == 1);
273 assert(templat
->height0
== 1);
274 assert(templat
->depth0
== 1);
275 assert(templat
->last_level
== 0);
277 * Reserve some extra storage since if we'd render to a buffer we
278 * read/write always LP_RASTER_BLOCK_SIZE pixels, but the element
279 * offset doesn't need to be aligned to LP_RASTER_BLOCK_SIZE.
281 lpr
->data
= align_malloc(bytes
+ (LP_RASTER_BLOCK_SIZE
- 1) * 4 * sizeof(float), 64);
284 * buffers don't really have stride but it's probably safer
285 * (for code doing same calculations for buffers and textures)
286 * to put something sane in there.
288 lpr
->row_stride
[0] = bytes
;
291 memset(lpr
->data
, 0, bytes
);
294 lpr
->id
= id_counter
++;
297 insert_at_tail(&resource_list
, lpr
);
306 static struct pipe_resource
*
307 llvmpipe_resource_create(struct pipe_screen
*_screen
,
308 const struct pipe_resource
*templat
)
310 return llvmpipe_resource_create_front(_screen
, templat
, NULL
);
314 llvmpipe_resource_destroy(struct pipe_screen
*pscreen
,
315 struct pipe_resource
*pt
)
317 struct llvmpipe_screen
*screen
= llvmpipe_screen(pscreen
);
318 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
322 struct sw_winsys
*winsys
= screen
->winsys
;
323 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
325 else if (llvmpipe_resource_is_texture(pt
)) {
326 /* free linear image data */
328 align_free(lpr
->tex_data
);
329 lpr
->tex_data
= NULL
;
332 else if (!lpr
->userBuffer
) {
334 align_free(lpr
->data
);
339 remove_from_list(lpr
);
347 * Map a resource for read/write.
350 llvmpipe_resource_map(struct pipe_resource
*resource
,
353 enum lp_texture_usage tex_usage
)
355 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
358 assert(level
< LP_MAX_TEXTURE_LEVELS
);
359 assert(layer
< (u_minify(resource
->depth0
, level
) + resource
->array_size
- 1));
361 assert(tex_usage
== LP_TEX_USAGE_READ
||
362 tex_usage
== LP_TEX_USAGE_READ_WRITE
||
363 tex_usage
== LP_TEX_USAGE_WRITE_ALL
);
367 struct llvmpipe_screen
*screen
= llvmpipe_screen(resource
->screen
);
368 struct sw_winsys
*winsys
= screen
->winsys
;
371 if (tex_usage
== LP_TEX_USAGE_READ
) {
372 dt_usage
= PIPE_TRANSFER_READ
;
375 dt_usage
= PIPE_TRANSFER_READ_WRITE
;
381 /* FIXME: keep map count? */
382 map
= winsys
->displaytarget_map(winsys
, lpr
->dt
, dt_usage
);
384 /* install this linear image in texture data structure */
389 else if (llvmpipe_resource_is_texture(resource
)) {
391 map
= llvmpipe_get_texture_image_address(lpr
, layer
, level
);
404 llvmpipe_resource_unmap(struct pipe_resource
*resource
,
408 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
412 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(resource
->screen
);
413 struct sw_winsys
*winsys
= lp_screen
->winsys
;
418 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
424 llvmpipe_resource_data(struct pipe_resource
*resource
)
426 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
428 assert(!llvmpipe_resource_is_texture(resource
));
434 static struct pipe_resource
*
435 llvmpipe_resource_from_handle(struct pipe_screen
*screen
,
436 const struct pipe_resource
*template,
437 struct winsys_handle
*whandle
,
440 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
441 struct llvmpipe_resource
*lpr
;
443 /* XXX Seems like from_handled depth textures doesn't work that well */
445 lpr
= CALLOC_STRUCT(llvmpipe_resource
);
450 lpr
->base
= *template;
451 pipe_reference_init(&lpr
->base
.reference
, 1);
452 lpr
->base
.screen
= screen
;
455 * Looks like unaligned displaytargets work just fine,
456 * at least sampler/render ones.
459 assert(lpr
->base
.width0
== width
);
460 assert(lpr
->base
.height0
== height
);
463 lpr
->dt
= winsys
->displaytarget_from_handle(winsys
,
466 &lpr
->row_stride
[0]);
471 lpr
->id
= id_counter
++;
474 insert_at_tail(&resource_list
, lpr
);
487 llvmpipe_resource_get_handle(struct pipe_screen
*screen
,
488 struct pipe_context
*ctx
,
489 struct pipe_resource
*pt
,
490 struct winsys_handle
*whandle
,
493 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
494 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
500 return winsys
->displaytarget_get_handle(winsys
, lpr
->dt
, whandle
);
505 llvmpipe_transfer_map( struct pipe_context
*pipe
,
506 struct pipe_resource
*resource
,
509 const struct pipe_box
*box
,
510 struct pipe_transfer
**transfer
)
512 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
513 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
514 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
515 struct llvmpipe_transfer
*lpt
;
516 struct pipe_transfer
*pt
;
518 enum pipe_format format
;
519 enum lp_texture_usage tex_usage
;
523 assert(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
,
535 TRUE
, /* cpu_access */
539 * It would have blocked, but state tracker requested no to.
541 assert(do_not_block
);
546 /* Check if we're mapping a current constant buffer */
547 if ((usage
& PIPE_TRANSFER_WRITE
) &&
548 (resource
->bind
& PIPE_BIND_CONSTANT_BUFFER
)) {
550 for (i
= 0; i
< ARRAY_SIZE(llvmpipe
->constants
[PIPE_SHADER_FRAGMENT
]); ++i
) {
551 if (resource
== llvmpipe
->constants
[PIPE_SHADER_FRAGMENT
][i
].buffer
) {
552 /* constants may have changed */
553 llvmpipe
->dirty
|= LP_NEW_FS_CONSTANTS
;
559 lpt
= CALLOC_STRUCT(llvmpipe_transfer
);
563 pipe_resource_reference(&pt
->resource
, resource
);
566 pt
->stride
= lpr
->row_stride
[level
];
567 pt
->layer_stride
= lpr
->img_stride
[level
];
571 assert(level
< LP_MAX_TEXTURE_LEVELS
);
574 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
575 transfer->x, transfer->y, transfer->width, transfer->height,
576 transfer->texture->width0,
577 transfer->texture->height0,
581 if (usage
== PIPE_TRANSFER_READ
) {
582 tex_usage
= LP_TEX_USAGE_READ
;
586 tex_usage
= LP_TEX_USAGE_READ_WRITE
;
591 printf("transfer map tex %u mode %s\n", lpr
->id
, mode
);
594 format
= lpr
->base
.format
;
596 map
= llvmpipe_resource_map(resource
,
602 /* May want to do different things here depending on read/write nature
605 if (usage
& PIPE_TRANSFER_WRITE
) {
606 /* Do something to notify sharing contexts of a texture change.
612 box
->y
/ util_format_get_blockheight(format
) * pt
->stride
+
613 box
->x
/ util_format_get_blockwidth(format
) * util_format_get_blocksize(format
);
620 llvmpipe_transfer_unmap(struct pipe_context
*pipe
,
621 struct pipe_transfer
*transfer
)
623 assert(transfer
->resource
);
625 llvmpipe_resource_unmap(transfer
->resource
,
629 /* Effectively do the texture_update work here - if texture images
630 * needed post-processing to put them into hardware layout, this is
631 * where it would happen. For llvmpipe, nothing to do.
633 assert (transfer
->resource
);
634 pipe_resource_reference(&transfer
->resource
, NULL
);
639 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
640 struct pipe_resource
*presource
,
643 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
644 if (!(presource
->bind
& (PIPE_BIND_DEPTH_STENCIL
|
645 PIPE_BIND_RENDER_TARGET
|
646 PIPE_BIND_SAMPLER_VIEW
)))
647 return LP_UNREFERENCED
;
649 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
654 * Returns the largest possible alignment for a format in llvmpipe
657 llvmpipe_get_format_alignment( enum pipe_format format
)
659 const struct util_format_description
*desc
= util_format_description(format
);
664 for (i
= 0; i
< desc
->nr_channels
; ++i
) {
665 size
+= desc
->channel
[i
].size
;
670 if (!util_is_power_of_two(bytes
)) {
671 bytes
/= desc
->nr_channels
;
674 if (bytes
% 2 || bytes
< 1) {
683 * Create buffer which wraps user-space data.
686 struct pipe_resource
*
687 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
692 struct llvmpipe_resource
*buffer
;
694 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
698 pipe_reference_init(&buffer
->base
.reference
, 1);
699 buffer
->base
.screen
= screen
;
700 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
701 buffer
->base
.bind
= bind_flags
;
702 buffer
->base
.usage
= PIPE_USAGE_IMMUTABLE
;
703 buffer
->base
.flags
= 0;
704 buffer
->base
.width0
= bytes
;
705 buffer
->base
.height0
= 1;
706 buffer
->base
.depth0
= 1;
707 buffer
->base
.array_size
= 1;
708 buffer
->userBuffer
= TRUE
;
711 return &buffer
->base
;
716 * Compute size (in bytes) need to store a texture image / mipmap level,
717 * for just one cube face, one array layer or one 3D texture slice
720 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
)
722 return lpr
->img_stride
[level
];
727 * Return pointer to a 2D texture image/face/slice.
728 * No tiled/linear conversion is done.
731 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
732 unsigned face_slice
, unsigned level
)
736 assert(llvmpipe_resource_is_texture(&lpr
->base
));
738 offset
= lpr
->mip_offsets
[level
];
741 offset
+= face_slice
* tex_image_face_size(lpr
, level
);
743 return (ubyte
*) lpr
->tex_data
+ offset
;
748 * Return size of resource in bytes
751 llvmpipe_resource_size(const struct pipe_resource
*resource
)
753 const struct llvmpipe_resource
*lpr
= llvmpipe_resource_const(resource
);
756 if (llvmpipe_resource_is_texture(resource
)) {
757 /* Note this will always return 0 for displaytarget resources */
758 size
= lpr
->total_alloc_size
;
761 size
= resource
->width0
;
769 llvmpipe_print_resources(void)
771 struct llvmpipe_resource
*lpr
;
772 unsigned n
= 0, total
= 0;
774 debug_printf("LLVMPIPE: current resources:\n");
775 foreach(lpr
, &resource_list
) {
776 unsigned size
= llvmpipe_resource_size(&lpr
->base
);
777 debug_printf("resource %u at %p, size %ux%ux%u: %u bytes, refcount %u\n",
778 lpr
->id
, (void *) lpr
,
779 lpr
->base
.width0
, lpr
->base
.height0
, lpr
->base
.depth0
,
780 size
, lpr
->base
.reference
.count
);
784 debug_printf("LLVMPIPE: total size of %u resources: %u\n", n
, total
);
790 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
793 /* init linked list for tracking resources */
795 static boolean first_call
= TRUE
;
797 memset(&resource_list
, 0, sizeof(resource_list
));
798 make_empty_list(&resource_list
);
804 screen
->resource_create
= llvmpipe_resource_create
;
805 /* screen->resource_create_front = llvmpipe_resource_create_front; */
806 screen
->resource_destroy
= llvmpipe_resource_destroy
;
807 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
808 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
809 screen
->can_create_resource
= llvmpipe_can_create_resource
;
814 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
816 pipe
->transfer_map
= llvmpipe_transfer_map
;
817 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
819 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
820 pipe
->buffer_subdata
= u_default_buffer_subdata
;
821 pipe
->texture_subdata
= u_default_texture_subdata
;