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 * Texture tile caching.
35 #include "pipe/p_inlines.h"
36 #include "util/u_memory.h"
37 #include "util/u_tile.h"
38 #include "sp_context.h"
39 #include "sp_surface.h"
40 #include "sp_texture.h"
41 #include "sp_tile_cache.h"
43 #define NUM_ENTRIES 50
47 #define MAX_WIDTH 2048
48 #define MAX_HEIGHT 2048
51 struct softpipe_tile_cache
53 struct pipe_screen
*screen
;
54 struct pipe_surface
*surface
; /**< the surface we're caching */
55 struct pipe_transfer
*transfer
;
57 struct pipe_texture
*texture
; /**< if caching a texture */
58 struct softpipe_cached_tile entries
[NUM_ENTRIES
];
59 uint clear_flags
[(MAX_WIDTH
/ TILE_SIZE
) * (MAX_HEIGHT
/ TILE_SIZE
) / 32];
60 float clear_color
[4]; /**< for color bufs */
61 uint clear_val
; /**< for z+stencil, or packed color clear value */
62 boolean depth_stencil
; /**< Is the surface a depth/stencil format? */
64 struct pipe_transfer
*tex_trans
;
66 int tex_face
, tex_level
, tex_z
;
68 struct softpipe_cached_tile tile
; /**< scratch tile for clears */
73 * Return the position in the cache for the tile that contains win pos (x,y).
74 * We currently use a direct mapped cache so this is like a hack key.
75 * At some point we should investige something more sophisticated, like
76 * a LRU replacement policy.
78 #define CACHE_POS(x, y) \
79 (((x) / TILE_SIZE + ((y) / TILE_SIZE) * 5) % NUM_ENTRIES)
84 * Is the tile at (x,y) in cleared state?
87 is_clear_flag_set(const uint
*bitvec
, int x
, int y
)
92 pos
= y
* (MAX_WIDTH
/ TILE_SIZE
) + x
;
93 assert(pos
/ 32 < (MAX_WIDTH
/ TILE_SIZE
) * (MAX_HEIGHT
/ TILE_SIZE
) / 32);
94 bit
= bitvec
[pos
/ 32] & (1 << (pos
& 31));
100 * Mark the tile at (x,y) as not cleared.
103 clear_clear_flag(uint
*bitvec
, int x
, int y
)
108 pos
= y
* (MAX_WIDTH
/ TILE_SIZE
) + x
;
109 assert(pos
/ 32 < (MAX_WIDTH
/ TILE_SIZE
) * (MAX_HEIGHT
/ TILE_SIZE
) / 32);
110 bitvec
[pos
/ 32] &= ~(1 << (pos
& 31));
114 struct softpipe_tile_cache
*
115 sp_create_tile_cache( struct pipe_screen
*screen
)
117 struct softpipe_tile_cache
*tc
;
120 tc
= CALLOC_STRUCT( softpipe_tile_cache
);
123 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
125 tc
->entries
[pos
].y
= -1;
133 sp_destroy_tile_cache(struct softpipe_tile_cache
*tc
)
135 struct pipe_screen
*screen
;
138 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
139 /*assert(tc->entries[pos].x < 0);*/
142 screen
= tc
->transfer
->texture
->screen
;
143 screen
->tex_transfer_destroy(tc
->transfer
);
146 screen
= tc
->tex_trans
->texture
->screen
;
147 screen
->tex_transfer_destroy(tc
->tex_trans
);
155 * Specify the surface to cache.
158 sp_tile_cache_set_surface(struct softpipe_tile_cache
*tc
,
159 struct pipe_surface
*ps
)
161 assert(!tc
->texture
);
164 struct pipe_screen
*screen
= tc
->transfer
->texture
->screen
;
166 if (ps
== tc
->surface
)
169 if (tc
->transfer_map
) {
170 screen
->transfer_unmap(screen
, tc
->transfer
);
171 tc
->transfer_map
= NULL
;
174 screen
->tex_transfer_destroy(tc
->transfer
);
181 struct pipe_screen
*screen
= ps
->texture
->screen
;
183 tc
->transfer
= screen
->get_tex_transfer(screen
, ps
->texture
, ps
->face
,
184 ps
->level
, ps
->zslice
,
185 PIPE_TRANSFER_READ_WRITE
,
186 0, 0, ps
->width
, ps
->height
);
188 tc
->depth_stencil
= (ps
->format
== PIPE_FORMAT_S8Z24_UNORM
||
189 ps
->format
== PIPE_FORMAT_X8Z24_UNORM
||
190 ps
->format
== PIPE_FORMAT_Z24S8_UNORM
||
191 ps
->format
== PIPE_FORMAT_Z24X8_UNORM
||
192 ps
->format
== PIPE_FORMAT_Z16_UNORM
||
193 ps
->format
== PIPE_FORMAT_Z32_UNORM
||
194 ps
->format
== PIPE_FORMAT_S8_UNORM
);
200 * Return the transfer being cached.
202 struct pipe_surface
*
203 sp_tile_cache_get_surface(struct softpipe_tile_cache
*tc
)
210 sp_tile_cache_map_transfers(struct softpipe_tile_cache
*tc
)
212 if (tc
->transfer
&& !tc
->transfer_map
)
213 tc
->transfer_map
= tc
->screen
->transfer_map(tc
->screen
, tc
->transfer
);
215 if (tc
->tex_trans
&& !tc
->tex_trans_map
)
216 tc
->tex_trans_map
= tc
->screen
->transfer_map(tc
->screen
, tc
->tex_trans
);
221 sp_tile_cache_unmap_transfers(struct softpipe_tile_cache
*tc
)
223 if (tc
->transfer_map
) {
224 tc
->screen
->transfer_unmap(tc
->screen
, tc
->transfer
);
225 tc
->transfer_map
= NULL
;
228 if (tc
->tex_trans_map
) {
229 tc
->screen
->transfer_unmap(tc
->screen
, tc
->tex_trans
);
230 tc
->tex_trans_map
= NULL
;
236 * Specify the texture to cache.
239 sp_tile_cache_set_texture(struct softpipe_tile_cache
*tc
,
240 struct pipe_texture
*texture
)
244 assert(!tc
->transfer
);
246 pipe_texture_reference(&tc
->texture
, texture
);
249 struct pipe_screen
*screen
= tc
->tex_trans
->texture
->screen
;
251 if (tc
->tex_trans_map
) {
252 screen
->transfer_unmap(screen
, tc
->tex_trans
);
253 tc
->tex_trans_map
= NULL
;
256 screen
->tex_transfer_destroy(tc
->tex_trans
);
257 tc
->tex_trans
= NULL
;
260 /* mark as entries as invalid/empty */
261 /* XXX we should try to avoid this when the teximage hasn't changed */
262 for (i
= 0; i
< NUM_ENTRIES
; i
++) {
263 tc
->entries
[i
].x
= -1;
266 tc
->tex_face
= -1; /* any invalid value here */
271 * Set pixels in a tile to the given clear color/value, float.
274 clear_tile_rgba(struct softpipe_cached_tile
*tile
,
275 enum pipe_format format
,
276 const float clear_value
[4])
278 if (clear_value
[0] == 0.0 &&
279 clear_value
[1] == 0.0 &&
280 clear_value
[2] == 0.0 &&
281 clear_value
[3] == 0.0) {
282 memset(tile
->data
.color
, 0, sizeof(tile
->data
.color
));
286 for (i
= 0; i
< TILE_SIZE
; i
++) {
287 for (j
= 0; j
< TILE_SIZE
; j
++) {
288 tile
->data
.color
[i
][j
][0] = clear_value
[0];
289 tile
->data
.color
[i
][j
][1] = clear_value
[1];
290 tile
->data
.color
[i
][j
][2] = clear_value
[2];
291 tile
->data
.color
[i
][j
][3] = clear_value
[3];
299 * Set a tile to a solid value/color.
302 clear_tile(struct softpipe_cached_tile
*tile
,
303 enum pipe_format format
,
308 switch (pf_get_size(format
)) {
310 memset(tile
->data
.any
, 0, TILE_SIZE
* TILE_SIZE
);
313 if (clear_value
== 0) {
314 memset(tile
->data
.any
, 0, 2 * TILE_SIZE
* TILE_SIZE
);
317 for (i
= 0; i
< TILE_SIZE
; i
++) {
318 for (j
= 0; j
< TILE_SIZE
; j
++) {
319 tile
->data
.depth16
[i
][j
] = (ushort
) clear_value
;
325 if (clear_value
== 0) {
326 memset(tile
->data
.any
, 0, 4 * TILE_SIZE
* TILE_SIZE
);
329 for (i
= 0; i
< TILE_SIZE
; i
++) {
330 for (j
= 0; j
< TILE_SIZE
; j
++) {
331 tile
->data
.color32
[i
][j
] = clear_value
;
343 * Actually clear the tiles which were flagged as being in a clear state.
346 sp_tile_cache_flush_clear(struct softpipe_tile_cache
*tc
)
348 struct pipe_transfer
*pt
= tc
->transfer
;
349 const uint w
= tc
->transfer
->width
;
350 const uint h
= tc
->transfer
->height
;
354 /* clear the scratch tile to the clear value */
355 clear_tile(&tc
->tile
, pt
->format
, tc
->clear_val
);
357 /* push the tile to all positions marked as clear */
358 for (y
= 0; y
< h
; y
+= TILE_SIZE
) {
359 for (x
= 0; x
< w
; x
+= TILE_SIZE
) {
360 if (is_clear_flag_set(tc
->clear_flags
, x
, y
)) {
361 pipe_put_tile_raw(pt
,
362 x
, y
, TILE_SIZE
, TILE_SIZE
,
363 tc
->tile
.data
.color32
, 0/*STRIDE*/);
366 clear_clear_flag(tc
->clear_flags
, x
, y
);
373 debug_printf("num cleared: %u\n", numCleared
);
379 * Flush the tile cache: write all dirty tiles back to the transfer.
380 * any tiles "flagged" as cleared will be "really" cleared.
383 sp_flush_tile_cache(struct softpipe_tile_cache
*tc
)
385 struct pipe_transfer
*pt
= tc
->transfer
;
389 /* caching a drawing transfer */
390 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
391 struct softpipe_cached_tile
*tile
= tc
->entries
+ pos
;
393 if (tc
->depth_stencil
) {
394 pipe_put_tile_raw(pt
,
395 tile
->x
, tile
->y
, TILE_SIZE
, TILE_SIZE
,
396 tile
->data
.depth32
, 0/*STRIDE*/);
399 pipe_put_tile_rgba(pt
,
400 tile
->x
, tile
->y
, TILE_SIZE
, TILE_SIZE
,
401 (float *) tile
->data
.color
);
403 tile
->x
= tile
->y
= -1; /* mark as empty */
408 #if TILE_CLEAR_OPTIMIZATION
409 sp_tile_cache_flush_clear(tc
);
412 else if (tc
->texture
) {
413 /* caching a texture, mark all entries as empty */
414 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
415 tc
->entries
[pos
].x
= -1;
421 debug_printf("flushed tiles in use: %d\n", inuse
);
427 * Get a tile from the cache.
428 * \param x, y position of tile, in pixels
430 struct softpipe_cached_tile
*
431 sp_get_cached_tile(struct softpipe_tile_cache
*tc
, int x
, int y
)
433 struct pipe_transfer
*pt
= tc
->transfer
;
435 /* tile pos in framebuffer: */
436 const int tile_x
= x
& ~(TILE_SIZE
- 1);
437 const int tile_y
= y
& ~(TILE_SIZE
- 1);
439 /* cache pos/entry: */
440 const int pos
= CACHE_POS(x
, y
);
441 struct softpipe_cached_tile
*tile
= tc
->entries
+ pos
;
443 if (tile_x
!= tile
->x
||
447 /* put dirty tile back in framebuffer */
448 if (tc
->depth_stencil
) {
449 pipe_put_tile_raw(pt
,
450 tile
->x
, tile
->y
, TILE_SIZE
, TILE_SIZE
,
451 tile
->data
.depth32
, 0/*STRIDE*/);
454 pipe_put_tile_rgba(pt
,
455 tile
->x
, tile
->y
, TILE_SIZE
, TILE_SIZE
,
456 (float *) tile
->data
.color
);
463 if (is_clear_flag_set(tc
->clear_flags
, x
, y
)) {
464 /* don't get tile from framebuffer, just clear it */
465 if (tc
->depth_stencil
) {
466 clear_tile(tile
, pt
->format
, tc
->clear_val
);
469 clear_tile_rgba(tile
, pt
->format
, tc
->clear_color
);
471 clear_clear_flag(tc
->clear_flags
, x
, y
);
474 /* get new tile data from transfer */
475 if (tc
->depth_stencil
) {
476 pipe_get_tile_raw(pt
,
477 tile
->x
, tile
->y
, TILE_SIZE
, TILE_SIZE
,
478 tile
->data
.depth32
, 0/*STRIDE*/);
481 pipe_get_tile_rgba(pt
,
482 tile
->x
, tile
->y
, TILE_SIZE
, TILE_SIZE
,
483 (float *) tile
->data
.color
);
493 * Given the texture face, level, zslice, x and y values, compute
494 * the cache entry position/index where we'd hope to find the
495 * cached texture tile.
496 * This is basically a direct-map cache.
497 * XXX There's probably lots of ways in which we can improve this.
500 tex_cache_pos(int x
, int y
, int z
, int face
, int level
)
502 uint entry
= x
+ y
* 9 + z
* 3 + face
+ level
* 7;
503 return entry
% NUM_ENTRIES
;
508 * Similar to sp_get_cached_tile() but for textures.
509 * Tiles are read-only and indexed with more params.
511 const struct softpipe_cached_tile
*
512 sp_get_cached_tile_tex(struct softpipe_tile_cache
*tc
,
516 struct pipe_screen
*screen
= tc
->screen
;
517 /* tile pos in framebuffer: */
518 const int tile_x
= x
& ~(TILE_SIZE
- 1);
519 const int tile_y
= y
& ~(TILE_SIZE
- 1);
520 /* cache pos/entry: */
521 const uint pos
= tex_cache_pos(x
/ TILE_SIZE
, y
/ TILE_SIZE
, z
,
523 struct softpipe_cached_tile
*tile
= tc
->entries
+ pos
;
526 struct softpipe_texture
*spt
= softpipe_texture(tc
->texture
);
528 /* texture was modified, invalidate all cached tiles */
530 for (p
= 0; p
< NUM_ENTRIES
; p
++) {
531 tile
= tc
->entries
+ p
;
534 spt
->modified
= FALSE
;
538 if (tile_x
!= tile
->x
||
541 face
!= tile
->face
||
542 level
!= tile
->level
) {
546 printf("miss at %u x=%d y=%d z=%d face=%d level=%d\n", pos
,
547 x
/TILE_SIZE
, y
/TILE_SIZE
, z
, face
, level
);
549 /* check if we need to get a new transfer */
550 if (!tc
->tex_trans
||
551 tc
->tex_face
!= face
||
552 tc
->tex_level
!= level
||
554 /* get new transfer (view into texture) */
557 if (tc
->tex_trans_map
) {
558 tc
->screen
->transfer_unmap(tc
->screen
, tc
->tex_trans
);
559 tc
->tex_trans_map
= NULL
;
562 screen
->tex_transfer_destroy(tc
->tex_trans
);
563 tc
->tex_trans
= NULL
;
566 tc
->tex_trans
= screen
->get_tex_transfer(screen
, tc
->texture
, face
, level
, z
,
567 PIPE_TRANSFER_READ
, 0, 0,
568 tc
->texture
->width
[level
],
569 tc
->texture
->height
[level
]);
570 tc
->tex_trans_map
= screen
->transfer_map(screen
, tc
->tex_trans
);
573 tc
->tex_level
= level
;
577 /* get tile from the transfer (view into texture) */
578 pipe_get_tile_rgba(tc
->tex_trans
,
579 tile_x
, tile_y
, TILE_SIZE
, TILE_SIZE
,
580 (float *) tile
->data
.color
);
593 * When a whole surface is being cleared to a value we can avoid
594 * fetching tiles above.
595 * Save the color and set a 'clearflag' for each tile of the screen.
598 sp_tile_cache_clear(struct softpipe_tile_cache
*tc
, const float *rgba
,
603 tc
->clear_color
[0] = rgba
[0];
604 tc
->clear_color
[1] = rgba
[1];
605 tc
->clear_color
[2] = rgba
[2];
606 tc
->clear_color
[3] = rgba
[3];
608 tc
->clear_val
= clearValue
;
610 #if TILE_CLEAR_OPTIMIZATION
611 /* set flags to indicate all the tiles are cleared */
612 memset(tc
->clear_flags
, 255, sizeof(tc
->clear_flags
));
614 /* disable the optimization */
615 memset(tc
->clear_flags
, 0, sizeof(tc
->clear_flags
));
618 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
619 struct softpipe_cached_tile
*tile
= tc
->entries
+ pos
;
620 tile
->x
= tile
->y
= -1;