1 /**************************************************************************
3 * Copyright 2007 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 * Render target tile caching.
35 #include "util/u_inlines.h"
36 #include "util/u_format.h"
37 #include "util/u_memory.h"
38 #include "util/u_tile.h"
39 #include "sp_tile_cache.h"
41 static struct softpipe_cached_tile
*
42 sp_alloc_tile(struct softpipe_tile_cache
*tc
);
46 * Return the position in the cache for the tile that contains win pos (x,y).
47 * We currently use a direct mapped cache so this is like a hack key.
48 * At some point we should investige something more sophisticated, like
49 * a LRU replacement policy.
51 #define CACHE_POS(x, y) \
52 (((x) + (y) * 5) % NUM_ENTRIES)
57 * Is the tile at (x,y) in cleared state?
60 is_clear_flag_set(const uint
*bitvec
, union tile_address addr
)
63 pos
= addr
.bits
.y
* (MAX_WIDTH
/ TILE_SIZE
) + addr
.bits
.x
;
64 assert(pos
/ 32 < (MAX_WIDTH
/ TILE_SIZE
) * (MAX_HEIGHT
/ TILE_SIZE
) / 32);
65 bit
= bitvec
[pos
/ 32] & (1 << (pos
& 31));
71 * Mark the tile at (x,y) as not cleared.
74 clear_clear_flag(uint
*bitvec
, union tile_address addr
)
77 pos
= addr
.bits
.y
* (MAX_WIDTH
/ TILE_SIZE
) + addr
.bits
.x
;
78 assert(pos
/ 32 < (MAX_WIDTH
/ TILE_SIZE
) * (MAX_HEIGHT
/ TILE_SIZE
) / 32);
79 bitvec
[pos
/ 32] &= ~(1 << (pos
& 31));
83 struct softpipe_tile_cache
*
84 sp_create_tile_cache( struct pipe_context
*pipe
)
86 struct softpipe_tile_cache
*tc
;
88 int maxLevels
, maxTexSize
;
90 /* sanity checking: max sure MAX_WIDTH/HEIGHT >= largest texture image */
91 maxLevels
= pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
);
92 maxTexSize
= 1 << (maxLevels
- 1);
93 assert(MAX_WIDTH
>= maxTexSize
);
95 assert(sizeof(union tile_address
) == 4);
97 assert((TILE_SIZE
<< TILE_ADDR_BITS
) >= MAX_WIDTH
);
99 tc
= CALLOC_STRUCT( softpipe_tile_cache
);
102 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
103 tc
->tile_addrs
[pos
].bits
.invalid
= 1;
105 tc
->last_tile_addr
.bits
.invalid
= 1;
107 /* this allocation allows us to guarantee that allocation
108 * failures are never fatal later
110 tc
->tile
= MALLOC_STRUCT( softpipe_cached_tile
);
117 /* XXX this code prevents valgrind warnings about use of uninitialized
118 * memory in programs that don't clear the surface before rendering.
119 * However, it breaks clearing in other situations (such as in
120 * progs/tests/drawbuffers, see bug 24402).
123 /* set flags to indicate all the tiles are cleared */
124 memset(tc
->clear_flags
, 255, sizeof(tc
->clear_flags
));
132 sp_destroy_tile_cache(struct softpipe_tile_cache
*tc
)
137 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
138 /*assert(tc->entries[pos].x < 0);*/
139 FREE( tc
->entries
[pos
] );
144 tc
->pipe
->transfer_destroy(tc
->pipe
, tc
->transfer
);
153 * Specify the surface to cache.
156 sp_tile_cache_set_surface(struct softpipe_tile_cache
*tc
,
157 struct pipe_surface
*ps
)
159 struct pipe_context
*pipe
= tc
->pipe
;
162 if (ps
== tc
->surface
)
165 if (tc
->transfer_map
) {
166 pipe
->transfer_unmap(pipe
, tc
->transfer
);
167 tc
->transfer_map
= NULL
;
170 pipe
->transfer_destroy(pipe
, tc
->transfer
);
177 tc
->transfer
= pipe_get_transfer(pipe
, ps
->texture
,
178 ps
->u
.tex
.level
, ps
->u
.tex
.first_layer
,
179 PIPE_TRANSFER_READ_WRITE
|
180 PIPE_TRANSFER_UNSYNCHRONIZED
,
181 0, 0, ps
->width
, ps
->height
);
183 tc
->depth_stencil
= util_format_is_depth_or_stencil(ps
->format
);
189 * Return the transfer being cached.
191 struct pipe_surface
*
192 sp_tile_cache_get_surface(struct softpipe_tile_cache
*tc
)
199 sp_tile_cache_map_transfers(struct softpipe_tile_cache
*tc
)
201 if (tc
->transfer
&& !tc
->transfer_map
)
202 tc
->transfer_map
= tc
->pipe
->transfer_map(tc
->pipe
, tc
->transfer
);
207 sp_tile_cache_unmap_transfers(struct softpipe_tile_cache
*tc
)
209 if (tc
->transfer_map
) {
210 tc
->pipe
->transfer_unmap(tc
->pipe
, tc
->transfer
);
211 tc
->transfer_map
= NULL
;
217 * Set pixels in a tile to the given clear color/value, float.
220 clear_tile_rgba(struct softpipe_cached_tile
*tile
,
221 enum pipe_format format
,
222 const union pipe_color_union
*clear_value
)
224 if (clear_value
->f
[0] == 0.0 &&
225 clear_value
->f
[1] == 0.0 &&
226 clear_value
->f
[2] == 0.0 &&
227 clear_value
->f
[3] == 0.0) {
228 memset(tile
->data
.color
, 0, sizeof(tile
->data
.color
));
233 if (util_format_is_pure_uint(format
)) {
234 for (i
= 0; i
< TILE_SIZE
; i
++) {
235 for (j
= 0; j
< TILE_SIZE
; j
++) {
236 tile
->data
.colorui128
[i
][j
][0] = clear_value
->ui
[0];
237 tile
->data
.colorui128
[i
][j
][1] = clear_value
->ui
[1];
238 tile
->data
.colorui128
[i
][j
][2] = clear_value
->ui
[2];
239 tile
->data
.colorui128
[i
][j
][3] = clear_value
->ui
[3];
242 } else if (util_format_is_pure_sint(format
)) {
243 for (i
= 0; i
< TILE_SIZE
; i
++) {
244 for (j
= 0; j
< TILE_SIZE
; j
++) {
245 tile
->data
.colori128
[i
][j
][0] = clear_value
->i
[0];
246 tile
->data
.colori128
[i
][j
][1] = clear_value
->i
[1];
247 tile
->data
.colori128
[i
][j
][2] = clear_value
->i
[2];
248 tile
->data
.colori128
[i
][j
][3] = clear_value
->i
[3];
252 for (i
= 0; i
< TILE_SIZE
; i
++) {
253 for (j
= 0; j
< TILE_SIZE
; j
++) {
254 tile
->data
.color
[i
][j
][0] = clear_value
->f
[0];
255 tile
->data
.color
[i
][j
][1] = clear_value
->f
[1];
256 tile
->data
.color
[i
][j
][2] = clear_value
->f
[2];
257 tile
->data
.color
[i
][j
][3] = clear_value
->f
[3];
266 * Set a tile to a solid value/color.
269 clear_tile(struct softpipe_cached_tile
*tile
,
270 enum pipe_format format
,
275 switch (util_format_get_blocksize(format
)) {
277 memset(tile
->data
.any
, clear_value
, TILE_SIZE
* TILE_SIZE
);
280 if (clear_value
== 0) {
281 memset(tile
->data
.any
, 0, 2 * TILE_SIZE
* TILE_SIZE
);
284 for (i
= 0; i
< TILE_SIZE
; i
++) {
285 for (j
= 0; j
< TILE_SIZE
; j
++) {
286 tile
->data
.depth16
[i
][j
] = (ushort
) clear_value
;
292 if (clear_value
== 0) {
293 memset(tile
->data
.any
, 0, 4 * TILE_SIZE
* TILE_SIZE
);
296 for (i
= 0; i
< TILE_SIZE
; i
++) {
297 for (j
= 0; j
< TILE_SIZE
; j
++) {
298 tile
->data
.color32
[i
][j
] = clear_value
;
310 * Actually clear the tiles which were flagged as being in a clear state.
313 sp_tile_cache_flush_clear(struct softpipe_tile_cache
*tc
)
315 struct pipe_transfer
*pt
= tc
->transfer
;
316 const uint w
= tc
->transfer
->box
.width
;
317 const uint h
= tc
->transfer
->box
.height
;
321 assert(pt
->resource
);
323 tc
->tile
= sp_alloc_tile(tc
);
325 /* clear the scratch tile to the clear value */
326 if (tc
->depth_stencil
) {
327 clear_tile(tc
->tile
, pt
->resource
->format
, tc
->clear_val
);
329 clear_tile_rgba(tc
->tile
, pt
->resource
->format
, &tc
->clear_color
);
332 /* push the tile to all positions marked as clear */
333 for (y
= 0; y
< h
; y
+= TILE_SIZE
) {
334 for (x
= 0; x
< w
; x
+= TILE_SIZE
) {
335 union tile_address addr
= tile_address(x
, y
);
337 if (is_clear_flag_set(tc
->clear_flags
, addr
)) {
338 /* write the scratch tile to the surface */
339 if (tc
->depth_stencil
) {
340 pipe_put_tile_raw(tc
->pipe
,
342 x
, y
, TILE_SIZE
, TILE_SIZE
,
343 tc
->tile
->data
.any
, 0/*STRIDE*/);
346 if (util_format_is_pure_uint(tc
->surface
->format
)) {
347 pipe_put_tile_ui_format(tc
->pipe
, pt
,
348 x
, y
, TILE_SIZE
, TILE_SIZE
,
349 pt
->resource
->format
,
350 (unsigned *) tc
->tile
->data
.colorui128
);
351 } else if (util_format_is_pure_sint(tc
->surface
->format
)) {
352 pipe_put_tile_i_format(tc
->pipe
, pt
,
353 x
, y
, TILE_SIZE
, TILE_SIZE
,
354 pt
->resource
->format
,
355 (int *) tc
->tile
->data
.colori128
);
357 pipe_put_tile_rgba(tc
->pipe
, pt
,
358 x
, y
, TILE_SIZE
, TILE_SIZE
,
359 (float *) tc
->tile
->data
.color
);
367 /* reset all clear flags to zero */
368 memset(tc
->clear_flags
, 0, sizeof(tc
->clear_flags
));
371 debug_printf("num cleared: %u\n", numCleared
);
376 sp_flush_tile(struct softpipe_tile_cache
* tc
, unsigned pos
)
378 if (!tc
->tile_addrs
[pos
].bits
.invalid
) {
379 if (tc
->depth_stencil
) {
380 pipe_put_tile_raw(tc
->pipe
, tc
->transfer
,
381 tc
->tile_addrs
[pos
].bits
.x
* TILE_SIZE
,
382 tc
->tile_addrs
[pos
].bits
.y
* TILE_SIZE
,
383 TILE_SIZE
, TILE_SIZE
,
384 tc
->entries
[pos
]->data
.depth32
, 0/*STRIDE*/);
387 if (util_format_is_pure_uint(tc
->surface
->format
)) {
388 pipe_put_tile_ui_format(tc
->pipe
, tc
->transfer
,
389 tc
->tile_addrs
[pos
].bits
.x
* TILE_SIZE
,
390 tc
->tile_addrs
[pos
].bits
.y
* TILE_SIZE
,
391 TILE_SIZE
, TILE_SIZE
,
393 (unsigned *) tc
->entries
[pos
]->data
.colorui128
);
394 } else if (util_format_is_pure_sint(tc
->surface
->format
)) {
395 pipe_put_tile_i_format(tc
->pipe
, tc
->transfer
,
396 tc
->tile_addrs
[pos
].bits
.x
* TILE_SIZE
,
397 tc
->tile_addrs
[pos
].bits
.y
* TILE_SIZE
,
398 TILE_SIZE
, TILE_SIZE
,
400 (int *) tc
->entries
[pos
]->data
.colori128
);
402 pipe_put_tile_rgba_format(tc
->pipe
, tc
->transfer
,
403 tc
->tile_addrs
[pos
].bits
.x
* TILE_SIZE
,
404 tc
->tile_addrs
[pos
].bits
.y
* TILE_SIZE
,
405 TILE_SIZE
, TILE_SIZE
,
407 (float *) tc
->entries
[pos
]->data
.color
);
410 tc
->tile_addrs
[pos
].bits
.invalid
= 1; /* mark as empty */
415 * Flush the tile cache: write all dirty tiles back to the transfer.
416 * any tiles "flagged" as cleared will be "really" cleared.
419 sp_flush_tile_cache(struct softpipe_tile_cache
*tc
)
421 struct pipe_transfer
*pt
= tc
->transfer
;
425 /* caching a drawing transfer */
426 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
427 struct softpipe_cached_tile
*tile
= tc
->entries
[pos
];
430 assert(tc
->tile_addrs
[pos
].bits
.invalid
);
434 sp_flush_tile(tc
, pos
);
438 sp_tile_cache_flush_clear(tc
);
441 tc
->last_tile_addr
.bits
.invalid
= 1;
445 debug_printf("flushed tiles in use: %d\n", inuse
);
449 static struct softpipe_cached_tile
*
450 sp_alloc_tile(struct softpipe_tile_cache
*tc
)
452 struct softpipe_cached_tile
* tile
= MALLOC_STRUCT(softpipe_cached_tile
);
455 /* in this case, steal an existing tile */
459 for (pos
= 0; pos
< NUM_ENTRIES
; ++pos
) {
460 if (!tc
->entries
[pos
])
463 sp_flush_tile(tc
, pos
);
464 tc
->tile
= tc
->entries
[pos
];
465 tc
->entries
[pos
] = NULL
;
469 /* this should never happen */
477 tc
->last_tile_addr
.bits
.invalid
= 1;
483 * Get a tile from the cache.
484 * \param x, y position of tile, in pixels
486 struct softpipe_cached_tile
*
487 sp_find_cached_tile(struct softpipe_tile_cache
*tc
,
488 union tile_address addr
)
490 struct pipe_transfer
*pt
= tc
->transfer
;
491 /* cache pos/entry: */
492 const int pos
= CACHE_POS(addr
.bits
.x
,
494 struct softpipe_cached_tile
*tile
= tc
->entries
[pos
];
497 tile
= sp_alloc_tile(tc
);
498 tc
->entries
[pos
] = tile
;
501 if (addr
.value
!= tc
->tile_addrs
[pos
].value
) {
503 assert(pt
->resource
);
504 if (tc
->tile_addrs
[pos
].bits
.invalid
== 0) {
505 /* put dirty tile back in framebuffer */
506 if (tc
->depth_stencil
) {
507 pipe_put_tile_raw(tc
->pipe
, pt
,
508 tc
->tile_addrs
[pos
].bits
.x
* TILE_SIZE
,
509 tc
->tile_addrs
[pos
].bits
.y
* TILE_SIZE
,
510 TILE_SIZE
, TILE_SIZE
,
511 tile
->data
.depth32
, 0/*STRIDE*/);
514 if (util_format_is_pure_uint(tc
->surface
->format
)) {
515 pipe_put_tile_ui_format(tc
->pipe
, pt
,
516 tc
->tile_addrs
[pos
].bits
.x
* TILE_SIZE
,
517 tc
->tile_addrs
[pos
].bits
.y
* TILE_SIZE
,
518 TILE_SIZE
, TILE_SIZE
,
520 (unsigned *) tile
->data
.colorui128
);
521 } else if (util_format_is_pure_sint(tc
->surface
->format
)) {
522 pipe_put_tile_i_format(tc
->pipe
, pt
,
523 tc
->tile_addrs
[pos
].bits
.x
* TILE_SIZE
,
524 tc
->tile_addrs
[pos
].bits
.y
* TILE_SIZE
,
525 TILE_SIZE
, TILE_SIZE
,
527 (int *) tile
->data
.colori128
);
529 pipe_put_tile_rgba_format(tc
->pipe
, pt
,
530 tc
->tile_addrs
[pos
].bits
.x
* TILE_SIZE
,
531 tc
->tile_addrs
[pos
].bits
.y
* TILE_SIZE
,
532 TILE_SIZE
, TILE_SIZE
,
534 (float *) tile
->data
.color
);
539 tc
->tile_addrs
[pos
] = addr
;
541 if (is_clear_flag_set(tc
->clear_flags
, addr
)) {
542 /* don't get tile from framebuffer, just clear it */
543 if (tc
->depth_stencil
) {
544 clear_tile(tile
, pt
->resource
->format
, tc
->clear_val
);
547 clear_tile_rgba(tile
, pt
->resource
->format
, &tc
->clear_color
);
549 clear_clear_flag(tc
->clear_flags
, addr
);
552 /* get new tile data from transfer */
553 if (tc
->depth_stencil
) {
554 pipe_get_tile_raw(tc
->pipe
, pt
,
555 tc
->tile_addrs
[pos
].bits
.x
* TILE_SIZE
,
556 tc
->tile_addrs
[pos
].bits
.y
* TILE_SIZE
,
557 TILE_SIZE
, TILE_SIZE
,
558 tile
->data
.depth32
, 0/*STRIDE*/);
561 if (util_format_is_pure_uint(tc
->surface
->format
)) {
562 pipe_get_tile_ui_format(tc
->pipe
, pt
,
563 tc
->tile_addrs
[pos
].bits
.x
* TILE_SIZE
,
564 tc
->tile_addrs
[pos
].bits
.y
* TILE_SIZE
,
565 TILE_SIZE
, TILE_SIZE
,
567 (unsigned *) tile
->data
.colorui128
);
568 } else if (util_format_is_pure_sint(tc
->surface
->format
)) {
569 pipe_get_tile_i_format(tc
->pipe
, pt
,
570 tc
->tile_addrs
[pos
].bits
.x
* TILE_SIZE
,
571 tc
->tile_addrs
[pos
].bits
.y
* TILE_SIZE
,
572 TILE_SIZE
, TILE_SIZE
,
574 (int *) tile
->data
.colori128
);
576 pipe_get_tile_rgba_format(tc
->pipe
, pt
,
577 tc
->tile_addrs
[pos
].bits
.x
* TILE_SIZE
,
578 tc
->tile_addrs
[pos
].bits
.y
* TILE_SIZE
,
579 TILE_SIZE
, TILE_SIZE
,
581 (float *) tile
->data
.color
);
587 tc
->last_tile
= tile
;
588 tc
->last_tile_addr
= addr
;
597 * When a whole surface is being cleared to a value we can avoid
598 * fetching tiles above.
599 * Save the color and set a 'clearflag' for each tile of the screen.
602 sp_tile_cache_clear(struct softpipe_tile_cache
*tc
,
603 const union pipe_color_union
*color
,
608 tc
->clear_color
= *color
;
610 tc
->clear_val
= clearValue
;
612 /* set flags to indicate all the tiles are cleared */
613 memset(tc
->clear_flags
, 255, sizeof(tc
->clear_flags
));
615 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
616 tc
->tile_addrs
[pos
].bits
.invalid
= 1;
618 tc
->last_tile_addr
.bits
.invalid
= 1;