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 4096
48 #define MAX_HEIGHT 4096
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
;
119 int maxLevels
, maxTexSize
;
121 /* sanity checking: max sure MAX_WIDTH/HEIGHT >= largest texture image */
122 maxLevels
= screen
->get_param(screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
);
123 maxTexSize
= 1 << (maxLevels
- 1);
124 assert(MAX_WIDTH
>= maxTexSize
);
126 tc
= CALLOC_STRUCT( softpipe_tile_cache
);
129 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
131 tc
->entries
[pos
].y
= -1;
139 sp_destroy_tile_cache(struct softpipe_tile_cache
*tc
)
141 struct pipe_screen
*screen
;
144 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
145 /*assert(tc->entries[pos].x < 0);*/
148 screen
= tc
->transfer
->texture
->screen
;
149 screen
->tex_transfer_destroy(tc
->transfer
);
152 screen
= tc
->tex_trans
->texture
->screen
;
153 screen
->tex_transfer_destroy(tc
->tex_trans
);
161 * Specify the surface to cache.
164 sp_tile_cache_set_surface(struct softpipe_tile_cache
*tc
,
165 struct pipe_surface
*ps
)
167 assert(!tc
->texture
);
170 struct pipe_screen
*screen
= tc
->transfer
->texture
->screen
;
172 if (ps
== tc
->surface
)
175 if (tc
->transfer_map
) {
176 screen
->transfer_unmap(screen
, tc
->transfer
);
177 tc
->transfer_map
= NULL
;
180 screen
->tex_transfer_destroy(tc
->transfer
);
187 struct pipe_screen
*screen
= ps
->texture
->screen
;
189 tc
->transfer
= screen
->get_tex_transfer(screen
, ps
->texture
, ps
->face
,
190 ps
->level
, ps
->zslice
,
191 PIPE_TRANSFER_READ_WRITE
,
192 0, 0, ps
->width
, ps
->height
);
194 tc
->depth_stencil
= (ps
->format
== PIPE_FORMAT_S8Z24_UNORM
||
195 ps
->format
== PIPE_FORMAT_X8Z24_UNORM
||
196 ps
->format
== PIPE_FORMAT_Z24S8_UNORM
||
197 ps
->format
== PIPE_FORMAT_Z24X8_UNORM
||
198 ps
->format
== PIPE_FORMAT_Z16_UNORM
||
199 ps
->format
== PIPE_FORMAT_Z32_UNORM
||
200 ps
->format
== PIPE_FORMAT_S8_UNORM
);
206 * Return the transfer being cached.
208 struct pipe_surface
*
209 sp_tile_cache_get_surface(struct softpipe_tile_cache
*tc
)
216 sp_tile_cache_map_transfers(struct softpipe_tile_cache
*tc
)
218 if (tc
->transfer
&& !tc
->transfer_map
)
219 tc
->transfer_map
= tc
->screen
->transfer_map(tc
->screen
, tc
->transfer
);
221 if (tc
->tex_trans
&& !tc
->tex_trans_map
)
222 tc
->tex_trans_map
= tc
->screen
->transfer_map(tc
->screen
, tc
->tex_trans
);
227 sp_tile_cache_unmap_transfers(struct softpipe_tile_cache
*tc
)
229 if (tc
->transfer_map
) {
230 tc
->screen
->transfer_unmap(tc
->screen
, tc
->transfer
);
231 tc
->transfer_map
= NULL
;
234 if (tc
->tex_trans_map
) {
235 tc
->screen
->transfer_unmap(tc
->screen
, tc
->tex_trans
);
236 tc
->tex_trans_map
= NULL
;
242 * Specify the texture to cache.
245 sp_tile_cache_set_texture(struct pipe_context
*pipe
,
246 struct softpipe_tile_cache
*tc
,
247 struct pipe_texture
*texture
)
251 assert(!tc
->transfer
);
253 pipe_texture_reference(&tc
->texture
, texture
);
256 struct pipe_screen
*screen
= tc
->tex_trans
->texture
->screen
;
258 if (tc
->tex_trans_map
) {
259 screen
->transfer_unmap(screen
, tc
->tex_trans
);
260 tc
->tex_trans_map
= NULL
;
263 screen
->tex_transfer_destroy(tc
->tex_trans
);
264 tc
->tex_trans
= NULL
;
267 /* mark as entries as invalid/empty */
268 /* XXX we should try to avoid this when the teximage hasn't changed */
269 for (i
= 0; i
< NUM_ENTRIES
; i
++) {
270 tc
->entries
[i
].x
= -1;
273 tc
->tex_face
= -1; /* any invalid value here */
278 * Set pixels in a tile to the given clear color/value, float.
281 clear_tile_rgba(struct softpipe_cached_tile
*tile
,
282 enum pipe_format format
,
283 const float clear_value
[4])
285 if (clear_value
[0] == 0.0 &&
286 clear_value
[1] == 0.0 &&
287 clear_value
[2] == 0.0 &&
288 clear_value
[3] == 0.0) {
289 memset(tile
->data
.color
, 0, sizeof(tile
->data
.color
));
293 for (i
= 0; i
< TILE_SIZE
; i
++) {
294 for (j
= 0; j
< TILE_SIZE
; j
++) {
295 tile
->data
.color
[i
][j
][0] = clear_value
[0];
296 tile
->data
.color
[i
][j
][1] = clear_value
[1];
297 tile
->data
.color
[i
][j
][2] = clear_value
[2];
298 tile
->data
.color
[i
][j
][3] = clear_value
[3];
306 * Set a tile to a solid value/color.
309 clear_tile(struct softpipe_cached_tile
*tile
,
310 enum pipe_format format
,
315 switch (pf_get_size(format
)) {
317 memset(tile
->data
.any
, 0, TILE_SIZE
* TILE_SIZE
);
320 if (clear_value
== 0) {
321 memset(tile
->data
.any
, 0, 2 * TILE_SIZE
* TILE_SIZE
);
324 for (i
= 0; i
< TILE_SIZE
; i
++) {
325 for (j
= 0; j
< TILE_SIZE
; j
++) {
326 tile
->data
.depth16
[i
][j
] = (ushort
) clear_value
;
332 if (clear_value
== 0) {
333 memset(tile
->data
.any
, 0, 4 * TILE_SIZE
* TILE_SIZE
);
336 for (i
= 0; i
< TILE_SIZE
; i
++) {
337 for (j
= 0; j
< TILE_SIZE
; j
++) {
338 tile
->data
.color32
[i
][j
] = clear_value
;
350 * Actually clear the tiles which were flagged as being in a clear state.
353 sp_tile_cache_flush_clear(struct pipe_context
*pipe
,
354 struct softpipe_tile_cache
*tc
)
356 struct pipe_transfer
*pt
= tc
->transfer
;
357 const uint w
= tc
->transfer
->width
;
358 const uint h
= tc
->transfer
->height
;
362 /* clear the scratch tile to the clear value */
363 clear_tile(&tc
->tile
, pt
->format
, tc
->clear_val
);
365 /* push the tile to all positions marked as clear */
366 for (y
= 0; y
< h
; y
+= TILE_SIZE
) {
367 for (x
= 0; x
< w
; x
+= TILE_SIZE
) {
368 if (is_clear_flag_set(tc
->clear_flags
, x
, y
)) {
369 pipe_put_tile_raw(pt
,
370 x
, y
, TILE_SIZE
, TILE_SIZE
,
371 tc
->tile
.data
.color32
, 0/*STRIDE*/);
374 clear_clear_flag(tc
->clear_flags
, x
, y
);
381 debug_printf("num cleared: %u\n", numCleared
);
387 * Flush the tile cache: write all dirty tiles back to the transfer.
388 * any tiles "flagged" as cleared will be "really" cleared.
391 sp_flush_tile_cache(struct softpipe_context
*softpipe
,
392 struct softpipe_tile_cache
*tc
)
394 struct pipe_transfer
*pt
= tc
->transfer
;
398 /* caching a drawing transfer */
399 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
400 struct softpipe_cached_tile
*tile
= tc
->entries
+ pos
;
402 if (tc
->depth_stencil
) {
403 pipe_put_tile_raw(pt
,
404 tile
->x
, tile
->y
, TILE_SIZE
, TILE_SIZE
,
405 tile
->data
.depth32
, 0/*STRIDE*/);
408 pipe_put_tile_rgba(pt
,
409 tile
->x
, tile
->y
, TILE_SIZE
, TILE_SIZE
,
410 (float *) tile
->data
.color
);
412 tile
->x
= tile
->y
= -1; /* mark as empty */
417 #if TILE_CLEAR_OPTIMIZATION
418 sp_tile_cache_flush_clear(&softpipe
->pipe
, tc
);
421 else if (tc
->texture
) {
422 /* caching a texture, mark all entries as empty */
423 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
424 tc
->entries
[pos
].x
= -1;
430 debug_printf("flushed tiles in use: %d\n", inuse
);
436 * Get a tile from the cache.
437 * \param x, y position of tile, in pixels
439 struct softpipe_cached_tile
*
440 sp_get_cached_tile(struct softpipe_context
*softpipe
,
441 struct softpipe_tile_cache
*tc
, int x
, int y
)
443 struct pipe_transfer
*pt
= tc
->transfer
;
445 /* tile pos in framebuffer: */
446 const int tile_x
= x
& ~(TILE_SIZE
- 1);
447 const int tile_y
= y
& ~(TILE_SIZE
- 1);
449 /* cache pos/entry: */
450 const int pos
= CACHE_POS(x
, y
);
451 struct softpipe_cached_tile
*tile
= tc
->entries
+ pos
;
453 if (tile_x
!= tile
->x
||
457 /* put dirty tile back in framebuffer */
458 if (tc
->depth_stencil
) {
459 pipe_put_tile_raw(pt
,
460 tile
->x
, tile
->y
, TILE_SIZE
, TILE_SIZE
,
461 tile
->data
.depth32
, 0/*STRIDE*/);
464 pipe_put_tile_rgba(pt
,
465 tile
->x
, tile
->y
, TILE_SIZE
, TILE_SIZE
,
466 (float *) tile
->data
.color
);
473 if (is_clear_flag_set(tc
->clear_flags
, x
, y
)) {
474 /* don't get tile from framebuffer, just clear it */
475 if (tc
->depth_stencil
) {
476 clear_tile(tile
, pt
->format
, tc
->clear_val
);
479 clear_tile_rgba(tile
, pt
->format
, tc
->clear_color
);
481 clear_clear_flag(tc
->clear_flags
, x
, y
);
484 /* get new tile data from transfer */
485 if (tc
->depth_stencil
) {
486 pipe_get_tile_raw(pt
,
487 tile
->x
, tile
->y
, TILE_SIZE
, TILE_SIZE
,
488 tile
->data
.depth32
, 0/*STRIDE*/);
491 pipe_get_tile_rgba(pt
,
492 tile
->x
, tile
->y
, TILE_SIZE
, TILE_SIZE
,
493 (float *) tile
->data
.color
);
503 * Given the texture face, level, zslice, x and y values, compute
504 * the cache entry position/index where we'd hope to find the
505 * cached texture tile.
506 * This is basically a direct-map cache.
507 * XXX There's probably lots of ways in which we can improve this.
510 tex_cache_pos(int x
, int y
, int z
, int face
, int level
)
512 uint entry
= x
+ y
* 9 + z
* 3 + face
+ level
* 7;
513 return entry
% NUM_ENTRIES
;
518 * Similar to sp_get_cached_tile() but for textures.
519 * Tiles are read-only and indexed with more params.
521 const struct softpipe_cached_tile
*
522 sp_get_cached_tile_tex(struct softpipe_context
*sp
,
523 struct softpipe_tile_cache
*tc
, int x
, int y
, int z
,
526 struct pipe_screen
*screen
= sp
->pipe
.screen
;
527 /* tile pos in framebuffer: */
528 const int tile_x
= x
& ~(TILE_SIZE
- 1);
529 const int tile_y
= y
& ~(TILE_SIZE
- 1);
530 /* cache pos/entry: */
531 const uint pos
= tex_cache_pos(x
/ TILE_SIZE
, y
/ TILE_SIZE
, z
,
533 struct softpipe_cached_tile
*tile
= tc
->entries
+ pos
;
536 struct softpipe_texture
*spt
= softpipe_texture(tc
->texture
);
538 /* texture was modified, invalidate all cached tiles */
540 for (p
= 0; p
< NUM_ENTRIES
; p
++) {
541 tile
= tc
->entries
+ p
;
544 spt
->modified
= FALSE
;
548 if (tile_x
!= tile
->x
||
551 face
!= tile
->face
||
552 level
!= tile
->level
) {
556 printf("miss at %u x=%d y=%d z=%d face=%d level=%d\n", pos
,
557 x
/TILE_SIZE
, y
/TILE_SIZE
, z
, face
, level
);
559 /* check if we need to get a new transfer */
560 if (!tc
->tex_trans
||
561 tc
->tex_face
!= face
||
562 tc
->tex_level
!= level
||
564 /* get new transfer (view into texture) */
567 if (tc
->tex_trans_map
) {
568 tc
->screen
->transfer_unmap(tc
->screen
, tc
->tex_trans
);
569 tc
->tex_trans_map
= NULL
;
572 screen
->tex_transfer_destroy(tc
->tex_trans
);
573 tc
->tex_trans
= NULL
;
576 tc
->tex_trans
= screen
->get_tex_transfer(screen
, tc
->texture
, face
, level
, z
,
577 PIPE_TRANSFER_READ
, 0, 0,
578 tc
->texture
->width
[level
],
579 tc
->texture
->height
[level
]);
580 tc
->tex_trans_map
= screen
->transfer_map(screen
, tc
->tex_trans
);
583 tc
->tex_level
= level
;
587 /* get tile from the transfer (view into texture) */
588 pipe_get_tile_rgba(tc
->tex_trans
,
589 tile_x
, tile_y
, TILE_SIZE
, TILE_SIZE
,
590 (float *) tile
->data
.color
);
603 * When a whole surface is being cleared to a value we can avoid
604 * fetching tiles above.
605 * Save the color and set a 'clearflag' for each tile of the screen.
608 sp_tile_cache_clear(struct softpipe_tile_cache
*tc
, const float *rgba
,
613 tc
->clear_color
[0] = rgba
[0];
614 tc
->clear_color
[1] = rgba
[1];
615 tc
->clear_color
[2] = rgba
[2];
616 tc
->clear_color
[3] = rgba
[3];
618 tc
->clear_val
= clearValue
;
620 #if TILE_CLEAR_OPTIMIZATION
621 /* set flags to indicate all the tiles are cleared */
622 memset(tc
->clear_flags
, 255, sizeof(tc
->clear_flags
));
624 /* disable the optimization */
625 memset(tc
->clear_flags
, 0, sizeof(tc
->clear_flags
));
628 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
629 struct softpipe_cached_tile
*tile
= tc
->entries
+ pos
;
630 tile
->x
= tile
->y
= -1;