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 "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_tile_cache.h"
45 * Return the position in the cache for the tile that contains win pos (x,y).
46 * We currently use a direct mapped cache so this is like a hack key.
47 * At some point we should investige something more sophisticated, like
48 * a LRU replacement policy.
50 #define CACHE_POS(x, y) \
51 (((x) + (y) * 5) % NUM_ENTRIES)
56 * Is the tile at (x,y) in cleared state?
59 is_clear_flag_set(const uint
*bitvec
, union tile_address addr
)
62 pos
= addr
.bits
.y
* (MAX_WIDTH
/ TILE_SIZE
) + addr
.bits
.x
;
63 assert(pos
/ 32 < (MAX_WIDTH
/ TILE_SIZE
) * (MAX_HEIGHT
/ TILE_SIZE
) / 32);
64 bit
= bitvec
[pos
/ 32] & (1 << (pos
& 31));
70 * Mark the tile at (x,y) as not cleared.
73 clear_clear_flag(uint
*bitvec
, union tile_address addr
)
76 pos
= addr
.bits
.y
* (MAX_WIDTH
/ TILE_SIZE
) + addr
.bits
.x
;
77 assert(pos
/ 32 < (MAX_WIDTH
/ TILE_SIZE
) * (MAX_HEIGHT
/ TILE_SIZE
) / 32);
78 bitvec
[pos
/ 32] &= ~(1 << (pos
& 31));
82 struct softpipe_tile_cache
*
83 sp_create_tile_cache( struct pipe_screen
*screen
)
85 struct softpipe_tile_cache
*tc
;
88 tc
= CALLOC_STRUCT( softpipe_tile_cache
);
91 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
92 tc
->entries
[pos
].addr
.bits
.invalid
= 1;
94 tc
->last_tile
= &tc
->entries
[0]; /* any tile */
101 sp_destroy_tile_cache(struct softpipe_tile_cache
*tc
)
103 struct pipe_screen
*screen
;
106 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
107 /*assert(tc->entries[pos].x < 0);*/
110 screen
= tc
->transfer
->texture
->screen
;
111 screen
->tex_transfer_destroy(tc
->transfer
);
119 * Specify the surface to cache.
122 sp_tile_cache_set_surface(struct softpipe_tile_cache
*tc
,
123 struct pipe_surface
*ps
)
126 struct pipe_screen
*screen
= tc
->transfer
->texture
->screen
;
128 if (ps
== tc
->surface
)
131 if (tc
->transfer_map
) {
132 screen
->transfer_unmap(screen
, tc
->transfer
);
133 tc
->transfer_map
= NULL
;
136 screen
->tex_transfer_destroy(tc
->transfer
);
143 struct pipe_screen
*screen
= ps
->texture
->screen
;
145 tc
->transfer
= screen
->get_tex_transfer(screen
, ps
->texture
, ps
->face
,
146 ps
->level
, ps
->zslice
,
147 PIPE_TRANSFER_READ_WRITE
,
148 0, 0, ps
->width
, ps
->height
);
150 tc
->depth_stencil
= (ps
->format
== PIPE_FORMAT_S8Z24_UNORM
||
151 ps
->format
== PIPE_FORMAT_X8Z24_UNORM
||
152 ps
->format
== PIPE_FORMAT_Z24S8_UNORM
||
153 ps
->format
== PIPE_FORMAT_Z24X8_UNORM
||
154 ps
->format
== PIPE_FORMAT_Z16_UNORM
||
155 ps
->format
== PIPE_FORMAT_Z32_UNORM
||
156 ps
->format
== PIPE_FORMAT_S8_UNORM
);
162 * Return the transfer being cached.
164 struct pipe_surface
*
165 sp_tile_cache_get_surface(struct softpipe_tile_cache
*tc
)
172 sp_tile_cache_map_transfers(struct softpipe_tile_cache
*tc
)
174 if (tc
->transfer
&& !tc
->transfer_map
)
175 tc
->transfer_map
= tc
->screen
->transfer_map(tc
->screen
, tc
->transfer
);
180 sp_tile_cache_unmap_transfers(struct softpipe_tile_cache
*tc
)
182 if (tc
->transfer_map
) {
183 tc
->screen
->transfer_unmap(tc
->screen
, tc
->transfer
);
184 tc
->transfer_map
= NULL
;
189 * Set pixels in a tile to the given clear color/value, float.
192 clear_tile_rgba(struct softpipe_cached_tile
*tile
,
193 enum pipe_format format
,
194 const float clear_value
[4])
196 if (clear_value
[0] == 0.0 &&
197 clear_value
[1] == 0.0 &&
198 clear_value
[2] == 0.0 &&
199 clear_value
[3] == 0.0) {
200 memset(tile
->data
.color
, 0, sizeof(tile
->data
.color
));
204 for (i
= 0; i
< TILE_SIZE
; i
++) {
205 for (j
= 0; j
< TILE_SIZE
; j
++) {
206 tile
->data
.color
[i
][j
][0] = clear_value
[0];
207 tile
->data
.color
[i
][j
][1] = clear_value
[1];
208 tile
->data
.color
[i
][j
][2] = clear_value
[2];
209 tile
->data
.color
[i
][j
][3] = clear_value
[3];
217 * Set a tile to a solid value/color.
220 clear_tile(struct softpipe_cached_tile
*tile
,
221 enum pipe_format format
,
226 switch (pf_get_size(format
)) {
228 memset(tile
->data
.any
, 0, TILE_SIZE
* TILE_SIZE
);
231 if (clear_value
== 0) {
232 memset(tile
->data
.any
, 0, 2 * TILE_SIZE
* TILE_SIZE
);
235 for (i
= 0; i
< TILE_SIZE
; i
++) {
236 for (j
= 0; j
< TILE_SIZE
; j
++) {
237 tile
->data
.depth16
[i
][j
] = (ushort
) clear_value
;
243 if (clear_value
== 0) {
244 memset(tile
->data
.any
, 0, 4 * TILE_SIZE
* TILE_SIZE
);
247 for (i
= 0; i
< TILE_SIZE
; i
++) {
248 for (j
= 0; j
< TILE_SIZE
; j
++) {
249 tile
->data
.color32
[i
][j
] = clear_value
;
261 * Actually clear the tiles which were flagged as being in a clear state.
264 sp_tile_cache_flush_clear(struct softpipe_tile_cache
*tc
)
266 struct pipe_transfer
*pt
= tc
->transfer
;
267 const uint w
= tc
->transfer
->width
;
268 const uint h
= tc
->transfer
->height
;
272 /* clear the scratch tile to the clear value */
273 clear_tile(&tc
->tile
, pt
->format
, tc
->clear_val
);
275 /* push the tile to all positions marked as clear */
276 for (y
= 0; y
< h
; y
+= TILE_SIZE
) {
277 for (x
= 0; x
< w
; x
+= TILE_SIZE
) {
278 union tile_address addr
= tile_address(x
, y
);
280 if (is_clear_flag_set(tc
->clear_flags
, addr
)) {
281 pipe_put_tile_raw(pt
,
282 x
, y
, TILE_SIZE
, TILE_SIZE
,
283 tc
->tile
.data
.color32
, 0/*STRIDE*/);
286 clear_clear_flag(tc
->clear_flags
, addr
);
293 debug_printf("num cleared: %u\n", numCleared
);
299 * Flush the tile cache: write all dirty tiles back to the transfer.
300 * any tiles "flagged" as cleared will be "really" cleared.
303 sp_flush_tile_cache(struct softpipe_tile_cache
*tc
)
305 struct pipe_transfer
*pt
= tc
->transfer
;
309 /* caching a drawing transfer */
310 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
311 struct softpipe_cached_tile
*tile
= tc
->entries
+ pos
;
312 if (!tile
->addr
.bits
.invalid
) {
313 if (tc
->depth_stencil
) {
314 pipe_put_tile_raw(pt
,
315 tile
->addr
.bits
.x
* TILE_SIZE
,
316 tile
->addr
.bits
.y
* TILE_SIZE
,
317 TILE_SIZE
, TILE_SIZE
,
318 tile
->data
.depth32
, 0/*STRIDE*/);
321 pipe_put_tile_rgba(pt
,
322 tile
->addr
.bits
.x
* TILE_SIZE
,
323 tile
->addr
.bits
.y
* TILE_SIZE
,
324 TILE_SIZE
, TILE_SIZE
,
325 (float *) tile
->data
.color
);
327 tile
->addr
.bits
.invalid
= 1; /* mark as empty */
332 #if TILE_CLEAR_OPTIMIZATION
333 sp_tile_cache_flush_clear(tc
);
338 debug_printf("flushed tiles in use: %d\n", inuse
);
344 * Get a tile from the cache.
345 * \param x, y position of tile, in pixels
347 struct softpipe_cached_tile
*
348 sp_find_cached_tile(struct softpipe_tile_cache
*tc
,
349 union tile_address addr
)
351 struct pipe_transfer
*pt
= tc
->transfer
;
353 /* cache pos/entry: */
354 const int pos
= CACHE_POS(addr
.bits
.x
,
356 struct softpipe_cached_tile
*tile
= tc
->entries
+ pos
;
358 if (addr
.value
!= tile
->addr
.value
) {
360 if (tile
->addr
.bits
.invalid
== 0) {
361 /* put dirty tile back in framebuffer */
362 if (tc
->depth_stencil
) {
363 pipe_put_tile_raw(pt
,
364 tile
->addr
.bits
.x
* TILE_SIZE
,
365 tile
->addr
.bits
.y
* TILE_SIZE
,
366 TILE_SIZE
, TILE_SIZE
,
367 tile
->data
.depth32
, 0/*STRIDE*/);
370 pipe_put_tile_rgba(pt
,
371 tile
->addr
.bits
.x
* TILE_SIZE
,
372 tile
->addr
.bits
.y
* TILE_SIZE
,
373 TILE_SIZE
, TILE_SIZE
,
374 (float *) tile
->data
.color
);
380 if (is_clear_flag_set(tc
->clear_flags
, addr
)) {
381 /* don't get tile from framebuffer, just clear it */
382 if (tc
->depth_stencil
) {
383 clear_tile(tile
, pt
->format
, tc
->clear_val
);
386 clear_tile_rgba(tile
, pt
->format
, tc
->clear_color
);
388 clear_clear_flag(tc
->clear_flags
, addr
);
391 /* get new tile data from transfer */
392 if (tc
->depth_stencil
) {
393 pipe_get_tile_raw(pt
,
394 tile
->addr
.bits
.x
* TILE_SIZE
,
395 tile
->addr
.bits
.y
* TILE_SIZE
,
396 TILE_SIZE
, TILE_SIZE
,
397 tile
->data
.depth32
, 0/*STRIDE*/);
400 pipe_get_tile_rgba(pt
,
401 tile
->addr
.bits
.x
* TILE_SIZE
,
402 tile
->addr
.bits
.y
* TILE_SIZE
,
403 TILE_SIZE
, TILE_SIZE
,
404 (float *) tile
->data
.color
);
409 tc
->last_tile
= tile
;
418 * When a whole surface is being cleared to a value we can avoid
419 * fetching tiles above.
420 * Save the color and set a 'clearflag' for each tile of the screen.
423 sp_tile_cache_clear(struct softpipe_tile_cache
*tc
, const float *rgba
,
428 tc
->clear_color
[0] = rgba
[0];
429 tc
->clear_color
[1] = rgba
[1];
430 tc
->clear_color
[2] = rgba
[2];
431 tc
->clear_color
[3] = rgba
[3];
433 tc
->clear_val
= clearValue
;
435 #if TILE_CLEAR_OPTIMIZATION
436 /* set flags to indicate all the tiles are cleared */
437 memset(tc
->clear_flags
, 255, sizeof(tc
->clear_flags
));
439 /* disable the optimization */
440 memset(tc
->clear_flags
, 0, sizeof(tc
->clear_flags
));
443 for (pos
= 0; pos
< NUM_ENTRIES
; pos
++) {
444 struct softpipe_cached_tile
*tile
= tc
->entries
+ pos
;
445 tile
->addr
.bits
.invalid
= 1;