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/u_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;
63 alloc_image_data(struct llvmpipe_resource
*lpr
);
66 * Conventional allocation path for non-display textures:
67 * Just compute row strides here. Storage is allocated on demand later.
70 llvmpipe_texture_layout(struct llvmpipe_screen
*screen
,
71 struct llvmpipe_resource
*lpr
)
73 struct pipe_resource
*pt
= &lpr
->base
;
75 unsigned width
= pt
->width0
;
76 unsigned height
= pt
->height0
;
77 unsigned depth
= pt
->depth0
;
78 uint64_t total_size
= 0;
79 unsigned layers
= pt
->array_size
;
81 assert(LP_MAX_TEXTURE_2D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
82 assert(LP_MAX_TEXTURE_3D_LEVELS
<= LP_MAX_TEXTURE_LEVELS
);
84 for (level
= 0; level
<= pt
->last_level
; level
++) {
86 /* Row stride and image stride */
88 unsigned align_x
, align_y
, nblocksx
, nblocksy
, block_size
;
90 /* For non-compressed formats we need 4x4 pixel alignment
91 * so we can read/write LP_RASTER_BLOCK_SIZE when rendering to them.
92 * We also want cache line size in x direction,
93 * otherwise same cache line could end up in multiple threads.
94 * For explicit 1d resources however we reduce this to 4x1 and
95 * handle specially in render output code (as we need to do special
96 * handling there for buffers in any case).
98 if (util_format_is_compressed(pt
->format
))
99 align_x
= align_y
= 1;
101 align_x
= LP_RASTER_BLOCK_SIZE
;
102 if (llvmpipe_resource_is_1d(&lpr
->base
))
105 align_y
= LP_RASTER_BLOCK_SIZE
;
108 nblocksx
= util_format_get_nblocksx(pt
->format
,
109 align(width
, align_x
));
110 nblocksy
= util_format_get_nblocksy(pt
->format
,
111 align(height
, align_y
));
112 block_size
= util_format_get_blocksize(pt
->format
);
114 if (util_format_is_compressed(pt
->format
))
115 lpr
->row_stride
[level
] = nblocksx
* block_size
;
117 lpr
->row_stride
[level
] = align(nblocksx
* block_size
, util_cpu_caps
.cacheline
);
119 /* if row_stride * height > LP_MAX_TEXTURE_SIZE */
120 if ((uint64_t)lpr
->row_stride
[level
] * nblocksy
> LP_MAX_TEXTURE_SIZE
) {
121 /* image too large */
125 lpr
->img_stride
[level
] = lpr
->row_stride
[level
] * nblocksy
;
128 /* Number of 3D image slices, cube faces or texture array layers */
132 if (lpr
->base
.target
== PIPE_TEXTURE_CUBE
)
134 else if (lpr
->base
.target
== PIPE_TEXTURE_3D
)
136 else if (lpr
->base
.target
== PIPE_TEXTURE_1D_ARRAY
||
137 lpr
->base
.target
== PIPE_TEXTURE_2D_ARRAY
)
142 lpr
->num_slices_faces
[level
] = num_slices
;
145 /* if img_stride * num_slices_faces > LP_MAX_TEXTURE_SIZE */
146 if (lpr
->img_stride
[level
] >
147 LP_MAX_TEXTURE_SIZE
/ lpr
->num_slices_faces
[level
]) {
148 /* volume too large */
152 total_size
+= (uint64_t) lpr
->num_slices_faces
[level
]
153 * (uint64_t) lpr
->img_stride
[level
];
154 if (total_size
> LP_MAX_TEXTURE_SIZE
) {
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);
172 * Check the size of the texture specified by 'res'.
173 * \return TRUE if OK, FALSE if too large.
176 llvmpipe_can_create_resource(struct pipe_screen
*screen
,
177 const struct pipe_resource
*res
)
179 struct llvmpipe_resource lpr
;
180 memset(&lpr
, 0, sizeof(lpr
));
182 return llvmpipe_texture_layout(llvmpipe_screen(screen
), &lpr
);
187 llvmpipe_displaytarget_layout(struct llvmpipe_screen
*screen
,
188 struct llvmpipe_resource
*lpr
)
190 struct sw_winsys
*winsys
= screen
->winsys
;
192 /* Round up the surface size to a multiple of the tile size to
193 * avoid tile clipping.
195 const unsigned width
= MAX2(1, align(lpr
->base
.width0
, TILE_SIZE
));
196 const unsigned height
= MAX2(1, align(lpr
->base
.height0
, TILE_SIZE
));
198 lpr
->num_slices_faces
[0] = 1;
199 lpr
->img_stride
[0] = 0;
201 lpr
->dt
= winsys
->displaytarget_create(winsys
,
206 &lpr
->row_stride
[0] );
212 void *map
= winsys
->displaytarget_map(winsys
, lpr
->dt
,
213 PIPE_TRANSFER_WRITE
);
216 memset(map
, 0, height
* lpr
->row_stride
[0]);
218 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
225 static struct pipe_resource
*
226 llvmpipe_resource_create(struct pipe_screen
*_screen
,
227 const struct pipe_resource
*templat
)
229 struct llvmpipe_screen
*screen
= llvmpipe_screen(_screen
);
230 struct llvmpipe_resource
*lpr
= CALLOC_STRUCT(llvmpipe_resource
);
234 lpr
->base
= *templat
;
235 pipe_reference_init(&lpr
->base
.reference
, 1);
236 lpr
->base
.screen
= &screen
->base
;
238 /* assert(lpr->base.bind); */
240 if (llvmpipe_resource_is_texture(&lpr
->base
)) {
241 if (lpr
->base
.bind
& (PIPE_BIND_DISPLAY_TARGET
|
244 /* displayable surface */
245 if (!llvmpipe_displaytarget_layout(screen
, lpr
))
250 if (!llvmpipe_texture_layout(screen
, lpr
))
253 alloc_image_data(lpr
);
254 if (!lpr
->tex_data
) {
260 /* other data (vertex buffer, const buffer, etc) */
261 const uint bytes
= templat
->width0
;
262 assert(util_format_get_blocksize(templat
->format
) == 1);
263 assert(templat
->height0
== 1);
264 assert(templat
->depth0
== 1);
265 assert(templat
->last_level
== 0);
267 * Reserve some extra storage since if we'd render to a buffer we
268 * read/write always LP_RASTER_BLOCK_SIZE pixels, but the element
269 * offset doesn't need to be aligned to LP_RASTER_BLOCK_SIZE.
271 lpr
->data
= align_malloc(bytes
+ (LP_RASTER_BLOCK_SIZE
- 1) * 4 * sizeof(float), 64);
273 * buffers don't really have stride but it's probably safer
274 * (for code doing same calculations for buffers and textures)
275 * to put something sane in there.
277 lpr
->row_stride
[0] = bytes
;
280 memset(lpr
->data
, 0, bytes
);
283 lpr
->id
= id_counter
++;
286 insert_at_tail(&resource_list
, lpr
);
298 llvmpipe_resource_destroy(struct pipe_screen
*pscreen
,
299 struct pipe_resource
*pt
)
301 struct llvmpipe_screen
*screen
= llvmpipe_screen(pscreen
);
302 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
306 struct sw_winsys
*winsys
= screen
->winsys
;
307 winsys
->displaytarget_destroy(winsys
, lpr
->dt
);
309 else if (llvmpipe_resource_is_texture(pt
)) {
310 /* free linear image data */
312 align_free(lpr
->tex_data
);
313 lpr
->tex_data
= NULL
;
316 else if (!lpr
->userBuffer
) {
318 align_free(lpr
->data
);
323 remove_from_list(lpr
);
331 * Map a resource for read/write.
334 llvmpipe_resource_map(struct pipe_resource
*resource
,
337 enum lp_texture_usage tex_usage
)
339 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
342 assert(level
< LP_MAX_TEXTURE_LEVELS
);
343 assert(layer
< (u_minify(resource
->depth0
, level
) + resource
->array_size
- 1));
345 assert(tex_usage
== LP_TEX_USAGE_READ
||
346 tex_usage
== LP_TEX_USAGE_READ_WRITE
||
347 tex_usage
== LP_TEX_USAGE_WRITE_ALL
);
351 struct llvmpipe_screen
*screen
= llvmpipe_screen(resource
->screen
);
352 struct sw_winsys
*winsys
= screen
->winsys
;
355 if (tex_usage
== LP_TEX_USAGE_READ
) {
356 dt_usage
= PIPE_TRANSFER_READ
;
359 dt_usage
= PIPE_TRANSFER_READ_WRITE
;
365 /* FIXME: keep map count? */
366 map
= winsys
->displaytarget_map(winsys
, lpr
->dt
, dt_usage
);
368 /* install this linear image in texture data structure */
373 else if (llvmpipe_resource_is_texture(resource
)) {
375 map
= llvmpipe_get_texture_image_address(lpr
, layer
, level
);
388 llvmpipe_resource_unmap(struct pipe_resource
*resource
,
392 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
396 struct llvmpipe_screen
*lp_screen
= llvmpipe_screen(resource
->screen
);
397 struct sw_winsys
*winsys
= lp_screen
->winsys
;
402 winsys
->displaytarget_unmap(winsys
, lpr
->dt
);
408 llvmpipe_resource_data(struct pipe_resource
*resource
)
410 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
412 assert(!llvmpipe_resource_is_texture(resource
));
418 static struct pipe_resource
*
419 llvmpipe_resource_from_handle(struct pipe_screen
*screen
,
420 const struct pipe_resource
*template,
421 struct winsys_handle
*whandle
)
423 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
424 struct llvmpipe_resource
*lpr
;
426 /* XXX Seems like from_handled depth textures doesn't work that well */
428 lpr
= CALLOC_STRUCT(llvmpipe_resource
);
433 lpr
->base
= *template;
434 pipe_reference_init(&lpr
->base
.reference
, 1);
435 lpr
->base
.screen
= screen
;
438 * Looks like unaligned displaytargets work just fine,
439 * at least sampler/render ones.
442 assert(lpr
->base
.width0
== width
);
443 assert(lpr
->base
.height0
== height
);
446 lpr
->num_slices_faces
[0] = 1;
447 lpr
->img_stride
[0] = 0;
449 lpr
->dt
= winsys
->displaytarget_from_handle(winsys
,
452 &lpr
->row_stride
[0]);
457 lpr
->id
= id_counter
++;
460 insert_at_tail(&resource_list
, lpr
);
473 llvmpipe_resource_get_handle(struct pipe_screen
*screen
,
474 struct pipe_resource
*pt
,
475 struct winsys_handle
*whandle
)
477 struct sw_winsys
*winsys
= llvmpipe_screen(screen
)->winsys
;
478 struct llvmpipe_resource
*lpr
= llvmpipe_resource(pt
);
484 return winsys
->displaytarget_get_handle(winsys
, lpr
->dt
, whandle
);
489 llvmpipe_transfer_map( struct pipe_context
*pipe
,
490 struct pipe_resource
*resource
,
493 const struct pipe_box
*box
,
494 struct pipe_transfer
**transfer
)
496 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
497 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
498 struct llvmpipe_resource
*lpr
= llvmpipe_resource(resource
);
499 struct llvmpipe_transfer
*lpt
;
500 struct pipe_transfer
*pt
;
502 enum pipe_format format
;
503 enum lp_texture_usage tex_usage
;
507 assert(level
<= resource
->last_level
);
510 * Transfers, like other pipe operations, must happen in order, so flush the
511 * context if necessary.
513 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
514 boolean read_only
= !(usage
& PIPE_TRANSFER_WRITE
);
515 boolean do_not_block
= !!(usage
& PIPE_TRANSFER_DONTBLOCK
);
516 if (!llvmpipe_flush_resource(pipe
, resource
,
519 TRUE
, /* cpu_access */
523 * It would have blocked, but state tracker requested no to.
525 assert(do_not_block
);
530 /* Check if we're mapping the current constant buffer */
531 if ((usage
& PIPE_TRANSFER_WRITE
) &&
532 (resource
->bind
& PIPE_BIND_CONSTANT_BUFFER
)) {
534 for (i
= 0; i
< Elements(llvmpipe
->constants
[PIPE_SHADER_FRAGMENT
]); ++i
) {
535 if (resource
== llvmpipe
->constants
[PIPE_SHADER_FRAGMENT
][i
].buffer
) {
536 /* constants may have changed */
537 llvmpipe
->dirty
|= LP_NEW_CONSTANTS
;
543 lpt
= CALLOC_STRUCT(llvmpipe_transfer
);
547 pipe_resource_reference(&pt
->resource
, resource
);
550 pt
->stride
= lpr
->row_stride
[level
];
551 pt
->layer_stride
= lpr
->img_stride
[level
];
555 assert(level
< LP_MAX_TEXTURE_LEVELS
);
558 printf("tex_transfer_map(%d, %d %d x %d of %d x %d, usage %d )\n",
559 transfer->x, transfer->y, transfer->width, transfer->height,
560 transfer->texture->width0,
561 transfer->texture->height0,
565 if (usage
== PIPE_TRANSFER_READ
) {
566 tex_usage
= LP_TEX_USAGE_READ
;
570 tex_usage
= LP_TEX_USAGE_READ_WRITE
;
575 printf("transfer map tex %u mode %s\n", lpr
->id
, mode
);
578 format
= lpr
->base
.format
;
580 map
= llvmpipe_resource_map(resource
,
586 /* May want to do different things here depending on read/write nature
589 if (usage
& PIPE_TRANSFER_WRITE
) {
590 /* Do something to notify sharing contexts of a texture change.
596 box
->y
/ util_format_get_blockheight(format
) * pt
->stride
+
597 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
,
613 /* Effectively do the texture_update work here - if texture images
614 * needed post-processing to put them into hardware layout, this is
615 * where it would happen. For llvmpipe, nothing to do.
617 assert (transfer
->resource
);
618 pipe_resource_reference(&transfer
->resource
, NULL
);
623 llvmpipe_is_resource_referenced( struct pipe_context
*pipe
,
624 struct pipe_resource
*presource
,
627 struct llvmpipe_context
*llvmpipe
= llvmpipe_context( pipe
);
630 * XXX checking only resources with the right bind flags
631 * is unsafe since with opengl state tracker we can end up
632 * with resources bound to places they weren't supposed to be
633 * (buffers bound as sampler views is one possibility here).
635 if (!(presource
->bind
& (PIPE_BIND_DEPTH_STENCIL
|
636 PIPE_BIND_RENDER_TARGET
|
637 PIPE_BIND_SAMPLER_VIEW
)))
638 return LP_UNREFERENCED
;
640 return lp_setup_is_resource_referenced(llvmpipe
->setup
, presource
);
645 * Returns the largest possible alignment for a format in llvmpipe
648 llvmpipe_get_format_alignment( enum pipe_format format
)
650 const struct util_format_description
*desc
= util_format_description(format
);
655 for (i
= 0; i
< desc
->nr_channels
; ++i
) {
656 size
+= desc
->channel
[i
].size
;
661 if (!util_is_power_of_two(bytes
)) {
662 bytes
/= desc
->nr_channels
;
665 if (bytes
% 2 || bytes
< 1) {
674 * Create buffer which wraps user-space data.
676 struct pipe_resource
*
677 llvmpipe_user_buffer_create(struct pipe_screen
*screen
,
682 struct llvmpipe_resource
*buffer
;
684 buffer
= CALLOC_STRUCT(llvmpipe_resource
);
688 pipe_reference_init(&buffer
->base
.reference
, 1);
689 buffer
->base
.screen
= screen
;
690 buffer
->base
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
691 buffer
->base
.bind
= bind_flags
;
692 buffer
->base
.usage
= PIPE_USAGE_IMMUTABLE
;
693 buffer
->base
.flags
= 0;
694 buffer
->base
.width0
= bytes
;
695 buffer
->base
.height0
= 1;
696 buffer
->base
.depth0
= 1;
697 buffer
->base
.array_size
= 1;
698 buffer
->userBuffer
= TRUE
;
701 return &buffer
->base
;
706 * Compute size (in bytes) need to store a texture image / mipmap level,
707 * for just one cube face, one array layer or one 3D texture slice
710 tex_image_face_size(const struct llvmpipe_resource
*lpr
, unsigned level
)
712 return lpr
->img_stride
[level
];
717 * Compute size (in bytes) need to store a texture image / mipmap level,
718 * including all cube faces or 3D image slices
721 tex_image_size(const struct llvmpipe_resource
*lpr
, unsigned level
)
723 const unsigned buf_size
= tex_image_face_size(lpr
, level
);
724 return buf_size
* lpr
->num_slices_faces
[level
];
729 * Return pointer to a 2D texture image/face/slice.
730 * No tiled/linear conversion is done.
733 llvmpipe_get_texture_image_address(struct llvmpipe_resource
*lpr
,
734 unsigned face_slice
, unsigned level
)
738 assert(llvmpipe_resource_is_texture(&lpr
->base
));
740 offset
= lpr
->mip_offsets
[level
];
743 offset
+= face_slice
* tex_image_face_size(lpr
, level
);
745 return (ubyte
*) lpr
->tex_data
+ offset
;
750 * Allocate storage for a linear image
751 * (all cube faces and all 3D slices, all levels).
754 alloc_image_data(struct llvmpipe_resource
*lpr
)
756 uint alignment
= MAX2(64, util_cpu_caps
.cacheline
);
762 /* not a display target - allocate regular memory */
764 * Offset calculation for start of a specific mip/layer is always
765 * offset = lpr->linear_mip_offsets[level] + lpr->img_stride[level] * layer
767 for (level
= 0; level
<= lpr
->base
.last_level
; level
++) {
768 uint buffer_size
= tex_image_size(lpr
, level
);
769 lpr
->mip_offsets
[level
] = offset
;
770 offset
+= align(buffer_size
, alignment
);
772 lpr
->tex_data
= align_malloc(offset
, alignment
);
774 memset(lpr
->tex_data
, 0, offset
);
780 * Return size of resource in bytes
783 llvmpipe_resource_size(const struct pipe_resource
*resource
)
785 const struct llvmpipe_resource
*lpr
= llvmpipe_resource_const(resource
);
786 unsigned lvl
, size
= 0;
788 if (llvmpipe_resource_is_texture(resource
)) {
789 for (lvl
= 0; lvl
<= lpr
->base
.last_level
; lvl
++) {
791 size
+= tex_image_size(lpr
, lvl
);
795 size
= resource
->width0
;
804 llvmpipe_print_resources(void)
806 struct llvmpipe_resource
*lpr
;
807 unsigned n
= 0, total
= 0;
809 debug_printf("LLVMPIPE: current resources:\n");
810 foreach(lpr
, &resource_list
) {
811 unsigned size
= llvmpipe_resource_size(&lpr
->base
);
812 debug_printf("resource %u at %p, size %ux%ux%u: %u bytes, refcount %u\n",
813 lpr
->id
, (void *) lpr
,
814 lpr
->base
.width0
, lpr
->base
.height0
, lpr
->base
.depth0
,
815 size
, lpr
->base
.reference
.count
);
819 debug_printf("LLVMPIPE: total size of %u resources: %u\n", n
, total
);
825 llvmpipe_init_screen_resource_funcs(struct pipe_screen
*screen
)
828 /* init linked list for tracking resources */
830 static boolean first_call
= TRUE
;
832 memset(&resource_list
, 0, sizeof(resource_list
));
833 make_empty_list(&resource_list
);
839 screen
->resource_create
= llvmpipe_resource_create
;
840 screen
->resource_destroy
= llvmpipe_resource_destroy
;
841 screen
->resource_from_handle
= llvmpipe_resource_from_handle
;
842 screen
->resource_get_handle
= llvmpipe_resource_get_handle
;
843 screen
->can_create_resource
= llvmpipe_can_create_resource
;
848 llvmpipe_init_context_resource_funcs(struct pipe_context
*pipe
)
850 pipe
->transfer_map
= llvmpipe_transfer_map
;
851 pipe
->transfer_unmap
= llvmpipe_transfer_unmap
;
853 pipe
->transfer_flush_region
= u_default_transfer_flush_region
;
854 pipe
->transfer_inline_write
= u_default_transfer_inline_write
;