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 * Framebuffer/surface tile caching.
35 #include "pipe/p_util.h"
36 #include "pipe/p_inlines.h"
37 #include "util/p_tile.h"
38 #include "sp_context.h"
39 #include "sp_surface.h"
40 #include "sp_tile_cache.h"
42 #define NUM_ENTRIES 32
46 #define MAX_WIDTH 2048
47 #define MAX_HEIGHT 2048
50 struct softpipe_tile_cache
52 struct pipe_screen
*screen
;
53 struct pipe_surface
*surface
; /**< the surface we're caching */
55 struct pipe_texture
*texture
; /**< if caching a texture */
56 struct softpipe_cached_tile entries
[NUM_ENTRIES
];
57 uint clear_flags
[(MAX_WIDTH
/ TILE_SIZE
) * (MAX_HEIGHT
/ TILE_SIZE
) / 32];
60 boolean depth_stencil
; /** Is the surface a depth/stencil format? */
62 struct pipe_surface
*tex_surf
;
64 int tex_face
, tex_level
, tex_z
;
66 struct softpipe_cached_tile tile
; /**< scratch tile for clears */
71 * Return the position in the cache for the tile that contains win pos (x,y).
72 * We currently use a direct mapped cache so this is like a hack key.
73 * At some point we should investige something more sophisticated, like
74 * a LRU replacement policy.
76 #define CACHE_POS(x, y) \
77 (((x) / TILE_SIZE + ((y) / TILE_SIZE) * 5) % NUM_ENTRIES)
82 * Is the tile at (x,y) in cleared state?
85 is_clear_flag_set(const uint
*bitvec
, int x
, int y
)
90 pos
= y
* (MAX_WIDTH
/ TILE_SIZE
) + x
;
91 assert(pos
/ 32 < (MAX_WIDTH
/ TILE_SIZE
) * (MAX_HEIGHT
/ TILE_SIZE
) / 32);
92 bit
= bitvec
[pos
/ 32] & (1 << (pos
& 31));
98 * Mark the tile at (x,y) as not cleared.
101 clear_clear_flag(uint
*bitvec
, int x
, int y
)
106 pos
= y
* (MAX_WIDTH
/ TILE_SIZE
) + x
;
107 assert(pos
/ 32 < (MAX_WIDTH
/ TILE_SIZE
) * (MAX_HEIGHT
/ TILE_SIZE
) / 32);
108 bitvec
[pos
/ 32] &= ~(1 << (pos
& 31));
112 struct softpipe_tile_cache
*
113 sp_create_tile_cache( struct pipe_screen
*screen
)
115 struct softpipe_tile_cache
*tc
;
118 tc
= CALLOC_STRUCT( softpipe_tile_cache
);
121 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
123 tc
->entries
[pos
].y
= -1;
131 sp_destroy_tile_cache(struct softpipe_tile_cache
*tc
)
135 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
136 /*assert(tc->entries[pos].x < 0);*/
139 pipe_surface_reference(&tc
->surface
, NULL
);
142 pipe_surface_reference(&tc
->tex_surf
, NULL
);
150 * Specify the surface to cache.
153 sp_tile_cache_set_surface(struct softpipe_tile_cache
*tc
,
154 struct pipe_surface
*ps
)
156 assert(!tc
->texture
);
158 if (tc
->surface_map
) {
159 tc
->screen
->surface_unmap(tc
->screen
, tc
->surface
);
160 tc
->surface_map
= NULL
;
163 pipe_surface_reference(&tc
->surface
, ps
);
166 if (tc
->surface_map
) /* XXX: this is always NULL!? */
167 tc
->surface_map
= tc
->screen
->surface_map(tc
->screen
, tc
->surface
,
168 PIPE_BUFFER_USAGE_CPU_READ
|
169 PIPE_BUFFER_USAGE_CPU_WRITE
);
171 tc
->depth_stencil
= (ps
->format
== PIPE_FORMAT_S8Z24_UNORM
||
172 ps
->format
== PIPE_FORMAT_Z16_UNORM
||
173 ps
->format
== PIPE_FORMAT_Z32_UNORM
||
174 ps
->format
== PIPE_FORMAT_S8_UNORM
);
180 * Return the surface being cached.
182 struct pipe_surface
*
183 sp_tile_cache_get_surface(struct softpipe_tile_cache
*tc
)
190 sp_tile_cache_map_surfaces(struct softpipe_tile_cache
*tc
)
192 if (tc
->surface
&& !tc
->surface_map
)
193 tc
->surface_map
= tc
->screen
->surface_map(tc
->screen
, tc
->surface
,
194 PIPE_BUFFER_USAGE_CPU_WRITE
|
195 PIPE_BUFFER_USAGE_CPU_READ
);
197 if (tc
->tex_surf
&& !tc
->tex_surf_map
)
198 tc
->tex_surf_map
= tc
->screen
->surface_map(tc
->screen
, tc
->tex_surf
,
199 PIPE_BUFFER_USAGE_CPU_READ
);
204 sp_tile_cache_unmap_surfaces(struct softpipe_tile_cache
*tc
)
206 if (tc
->surface_map
) {
207 tc
->screen
->surface_unmap(tc
->screen
, tc
->surface
);
208 tc
->surface_map
= NULL
;
211 if (tc
->tex_surf_map
) {
212 tc
->screen
->surface_unmap(tc
->screen
, tc
->tex_surf
);
213 tc
->tex_surf_map
= NULL
;
219 * Specify the texture to cache.
222 sp_tile_cache_set_texture(struct pipe_context
*pipe
,
223 struct softpipe_tile_cache
*tc
,
224 struct pipe_texture
*texture
)
228 assert(!tc
->surface
);
230 pipe_texture_reference(&tc
->texture
, texture
);
232 if (tc
->tex_surf_map
) {
233 tc
->screen
->surface_unmap(tc
->screen
, tc
->tex_surf
);
234 tc
->tex_surf_map
= NULL
;
236 pipe_surface_reference(&tc
->tex_surf
, NULL
);
238 /* mark as entries as invalid/empty */
239 /* XXX we should try to avoid this when the teximage hasn't changed */
240 for (i
= 0; i
< NUM_ENTRIES
; i
++) {
241 tc
->entries
[i
].x
= -1;
244 tc
->tex_face
= -1; /* any invalid value here */
249 * Set pixels in a tile to the given clear color/value, float.
252 clear_tile_rgba(struct softpipe_cached_tile
*tile
,
253 enum pipe_format format
,
254 const float clear_value
[4])
256 if (clear_value
[0] == 0.0 &&
257 clear_value
[1] == 0.0 &&
258 clear_value
[2] == 0.0 &&
259 clear_value
[3] == 0.0) {
260 memset(tile
->data
.color
, 0, sizeof(tile
->data
.color
));
264 for (i
= 0; i
< TILE_SIZE
; i
++) {
265 for (j
= 0; j
< TILE_SIZE
; j
++) {
266 tile
->data
.color
[i
][j
][0] = clear_value
[0];
267 tile
->data
.color
[i
][j
][1] = clear_value
[1];
268 tile
->data
.color
[i
][j
][2] = clear_value
[2];
269 tile
->data
.color
[i
][j
][3] = clear_value
[3];
277 * Set a tile to a solid value/color.
280 clear_tile(struct softpipe_cached_tile
*tile
,
281 enum pipe_format format
,
286 switch (pf_get_size(format
)) {
288 memset(tile
->data
.any
, 0, TILE_SIZE
* TILE_SIZE
);
291 if (clear_value
== 0) {
292 memset(tile
->data
.any
, 0, 2 * TILE_SIZE
* TILE_SIZE
);
295 for (i
= 0; i
< TILE_SIZE
; i
++) {
296 for (j
= 0; j
< TILE_SIZE
; j
++) {
297 tile
->data
.depth16
[i
][j
] = (ushort
) clear_value
;
303 if (clear_value
== 0) {
304 memset(tile
->data
.any
, 0, 4 * TILE_SIZE
* TILE_SIZE
);
307 for (i
= 0; i
< TILE_SIZE
; i
++) {
308 for (j
= 0; j
< TILE_SIZE
; j
++) {
309 tile
->data
.color32
[i
][j
] = clear_value
;
321 * Actually clear the tiles which were flagged as being in a clear state.
324 sp_tile_cache_flush_clear(struct pipe_context
*pipe
,
325 struct softpipe_tile_cache
*tc
)
327 struct pipe_surface
*ps
= tc
->surface
;
328 const uint w
= tc
->surface
->width
;
329 const uint h
= tc
->surface
->height
;
333 /* clear the scratch tile to the clear value */
334 clear_tile(&tc
->tile
, ps
->format
, tc
->clear_val
);
336 /* push the tile to all positions marked as clear */
337 for (y
= 0; y
< h
; y
+= TILE_SIZE
) {
338 for (x
= 0; x
< w
; x
+= TILE_SIZE
) {
339 if (is_clear_flag_set(tc
->clear_flags
, x
, y
)) {
340 pipe_put_tile_raw(pipe
, ps
,
341 x
, y
, TILE_SIZE
, TILE_SIZE
,
342 tc
->tile
.data
.color32
, 0/*STRIDE*/);
345 clear_clear_flag(tc
->clear_flags
, x
, y
);
352 debug_printf("num cleared: %u\n", numCleared
);
358 * Flush the tile cache: write all dirty tiles back to the surface.
359 * any tiles "flagged" as cleared will be "really" cleared.
362 sp_flush_tile_cache(struct softpipe_context
*softpipe
,
363 struct softpipe_tile_cache
*tc
)
365 struct pipe_context
*pipe
= &softpipe
->pipe
;
366 struct pipe_surface
*ps
= tc
->surface
;
369 if (ps
&& ps
->buffer
) {
370 /* caching a drawing surface */
371 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
372 struct softpipe_cached_tile
*tile
= tc
->entries
+ pos
;
374 if (tc
->depth_stencil
) {
375 pipe_put_tile_raw(pipe
, ps
,
376 tile
->x
, tile
->y
, TILE_SIZE
, TILE_SIZE
,
377 tile
->data
.depth32
, 0/*STRIDE*/);
380 pipe_put_tile_rgba(pipe
, ps
,
381 tile
->x
, tile
->y
, TILE_SIZE
, TILE_SIZE
,
382 (float *) tile
->data
.color
);
384 tile
->x
= tile
->y
= -1; /* mark as empty */
389 #if TILE_CLEAR_OPTIMIZATION
390 sp_tile_cache_flush_clear(&softpipe
->pipe
, tc
);
393 else if (tc
->texture
) {
394 /* caching a texture, mark all entries as empty */
395 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
396 tc
->entries
[pos
].x
= -1;
402 debug_printf("flushed tiles in use: %d\n", inuse
);
408 * Get a tile from the cache.
409 * \param x, y position of tile, in pixels
411 struct softpipe_cached_tile
*
412 sp_get_cached_tile(struct softpipe_context
*softpipe
,
413 struct softpipe_tile_cache
*tc
, int x
, int y
)
415 struct pipe_context
*pipe
= &softpipe
->pipe
;
416 struct pipe_surface
*ps
= tc
->surface
;
418 /* tile pos in framebuffer: */
419 const int tile_x
= x
& ~(TILE_SIZE
- 1);
420 const int tile_y
= y
& ~(TILE_SIZE
- 1);
422 /* cache pos/entry: */
423 const int pos
= CACHE_POS(x
, y
);
424 struct softpipe_cached_tile
*tile
= tc
->entries
+ pos
;
426 if (tile_x
!= tile
->x
||
430 /* put dirty tile back in framebuffer */
431 if (tc
->depth_stencil
) {
432 pipe_put_tile_raw(pipe
, ps
,
433 tile
->x
, tile
->y
, TILE_SIZE
, TILE_SIZE
,
434 tile
->data
.depth32
, 0/*STRIDE*/);
437 pipe_put_tile_rgba(pipe
, ps
,
438 tile
->x
, tile
->y
, TILE_SIZE
, TILE_SIZE
,
439 (float *) tile
->data
.color
);
446 if (is_clear_flag_set(tc
->clear_flags
, x
, y
)) {
447 /* don't get tile from framebuffer, just clear it */
448 if (tc
->depth_stencil
) {
449 clear_tile(tile
, ps
->format
, tc
->clear_val
);
452 clear_tile_rgba(tile
, ps
->format
, tc
->clear_color
);
454 clear_clear_flag(tc
->clear_flags
, x
, y
);
457 /* get new tile data from surface */
458 if (tc
->depth_stencil
) {
459 pipe_get_tile_raw(pipe
, ps
,
460 tile
->x
, tile
->y
, TILE_SIZE
, TILE_SIZE
,
461 tile
->data
.depth32
, 0/*STRIDE*/);
464 pipe_get_tile_rgba(pipe
, ps
,
465 tile
->x
, tile
->y
, TILE_SIZE
, TILE_SIZE
,
466 (float *) tile
->data
.color
);
476 * Given the texture face, level, zslice, x and y values, compute
477 * the cache entry position/index where we'd hope to find the
478 * cached texture tile.
479 * This is basically a direct-map cache.
480 * XXX There's probably lots of ways in which we can improve this.
483 tex_cache_pos(int x
, int y
, int z
, int face
, int level
)
485 uint entry
= x
+ y
* 5 + z
* 4 + face
+ level
;
486 return entry
% NUM_ENTRIES
;
491 * Similar to sp_get_cached_tile() but for textures.
492 * Tiles are read-only and indexed with more params.
494 const struct softpipe_cached_tile
*
495 sp_get_cached_tile_tex(struct pipe_context
*pipe
,
496 struct softpipe_tile_cache
*tc
, int x
, int y
, int z
,
499 struct pipe_screen
*screen
= pipe
->screen
;
500 /* tile pos in framebuffer: */
501 const int tile_x
= x
& ~(TILE_SIZE
- 1);
502 const int tile_y
= y
& ~(TILE_SIZE
- 1);
503 /* cache pos/entry: */
504 const uint pos
= tex_cache_pos(x
/ TILE_SIZE
, y
/ TILE_SIZE
, z
,
506 struct softpipe_cached_tile
*tile
= tc
->entries
+ pos
;
508 if (tile_x
!= tile
->x
||
511 face
!= tile
->face
||
512 level
!= tile
->level
) {
515 /* check if we need to get a new surface */
517 tc
->tex_face
!= face
||
518 tc
->tex_level
!= level
||
520 /* get new surface (view into texture) */
522 if (tc
->tex_surf_map
)
523 tc
->screen
->surface_unmap(tc
->screen
, tc
->tex_surf
);
525 tc
->tex_surf
= screen
->get_tex_surface(screen
, tc
->texture
, face
, level
, z
,
526 PIPE_BUFFER_USAGE_CPU_READ
);
527 tc
->tex_surf_map
= screen
->surface_map(screen
, tc
->tex_surf
,
528 PIPE_BUFFER_USAGE_CPU_READ
);
531 tc
->tex_level
= level
;
535 /* get tile from the surface (view into texture) */
536 pipe_get_tile_rgba(pipe
, tc
->tex_surf
,
537 tile_x
, tile_y
, TILE_SIZE
, TILE_SIZE
,
538 (float *) tile
->data
.color
);
551 * When a whole surface is being cleared to a value we can avoid
552 * fetching tiles above.
553 * Save the color and set a 'clearflag' for each tile of the screen.
556 sp_tile_cache_clear(struct softpipe_tile_cache
*tc
, uint clearValue
)
561 tc
->clear_val
= clearValue
;
563 switch (tc
->surface
->format
) {
564 case PIPE_FORMAT_R8G8B8A8_UNORM
:
565 r
= (clearValue
>> 24) & 0xff;
566 g
= (clearValue
>> 16) & 0xff;
567 b
= (clearValue
>> 8) & 0xff;
568 a
= (clearValue
) & 0xff;
570 case PIPE_FORMAT_A8R8G8B8_UNORM
:
571 r
= (clearValue
>> 16) & 0xff;
572 g
= (clearValue
>> 8) & 0xff;
573 b
= (clearValue
) & 0xff;
574 a
= (clearValue
>> 24) & 0xff;
576 case PIPE_FORMAT_B8G8R8A8_UNORM
:
577 r
= (clearValue
>> 8) & 0xff;
578 g
= (clearValue
>> 16) & 0xff;
579 b
= (clearValue
>> 24) & 0xff;
580 a
= (clearValue
) & 0xff;
586 tc
->clear_color
[0] = r
/ 255.0f
;
587 tc
->clear_color
[1] = g
/ 255.0f
;
588 tc
->clear_color
[2] = b
/ 255.0f
;
589 tc
->clear_color
[3] = a
/ 255.0f
;
591 #if TILE_CLEAR_OPTIMIZATION
592 /* set flags to indicate all the tiles are cleared */
593 memset(tc
->clear_flags
, 255, sizeof(tc
->clear_flags
));
595 /* disable the optimization */
596 memset(tc
->clear_flags
, 0, sizeof(tc
->clear_flags
));
599 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
600 struct softpipe_cached_tile
*tile
= tc
->entries
+ pos
;
601 tile
->x
= tile
->y
= -1;