1596cd0ae7622db0802d27ad73761fa0b7daf6ee
[mesa.git] / src / gallium / drivers / softpipe / sp_tile_cache.h
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #ifndef SP_TILE_CACHE_H
29 #define SP_TILE_CACHE_H
30
31 #define TILE_CLEAR_OPTIMIZATION 1
32
33
34 #include "pipe/p_compiler.h"
35
36
37 struct softpipe_tile_cache;
38
39
40 /**
41 * Cache tile size (width and height). This needs to be a power of two.
42 */
43 #define TILE_SIZE 64
44
45
46 /* If we need to support > 4096, just expand this to be a 64 bit
47 * union, or consider tiling in Z as well.
48 */
49 union tile_address {
50 struct {
51 unsigned x:6; /* 4096 / TILE_SIZE */
52 unsigned y:6; /* 4096 / TILE_SIZE */
53 unsigned z:12; /* 4096 -- z not tiled */
54 unsigned face:3;
55 unsigned level:4;
56 unsigned invalid:1;
57 } bits;
58 unsigned value;
59 };
60
61
62 struct softpipe_cached_tile
63 {
64 union tile_address addr;
65 union {
66 float color[TILE_SIZE][TILE_SIZE][4];
67 uint color32[TILE_SIZE][TILE_SIZE];
68 uint depth32[TILE_SIZE][TILE_SIZE];
69 ushort depth16[TILE_SIZE][TILE_SIZE];
70 ubyte stencil8[TILE_SIZE][TILE_SIZE];
71 ubyte any[1];
72 } data;
73 };
74
75 #define NUM_ENTRIES 50
76
77
78 /** XXX move these */
79 #define MAX_WIDTH 2048
80 #define MAX_HEIGHT 2048
81
82
83 struct softpipe_tile_cache
84 {
85 struct pipe_screen *screen;
86 struct pipe_surface *surface; /**< the surface we're caching */
87 struct pipe_transfer *transfer;
88 void *transfer_map;
89
90 struct pipe_texture *texture; /**< if caching a texture */
91 unsigned timestamp;
92
93 struct softpipe_cached_tile entries[NUM_ENTRIES];
94 uint clear_flags[(MAX_WIDTH / TILE_SIZE) * (MAX_HEIGHT / TILE_SIZE) / 32];
95 float clear_color[4]; /**< for color bufs */
96 uint clear_val; /**< for z+stencil, or packed color clear value */
97 boolean depth_stencil; /**< Is the surface a depth/stencil format? */
98
99 struct pipe_transfer *tex_trans;
100 void *tex_trans_map;
101 int tex_face, tex_level, tex_z;
102
103 struct softpipe_cached_tile tile; /**< scratch tile for clears */
104
105 struct softpipe_cached_tile *last_tile; /**< most recently retrieved tile */
106 };
107
108
109 extern struct softpipe_tile_cache *
110 sp_create_tile_cache( struct pipe_screen *screen );
111
112 extern void
113 sp_destroy_tile_cache(struct softpipe_tile_cache *tc);
114
115 extern void
116 sp_tile_cache_set_surface(struct softpipe_tile_cache *tc,
117 struct pipe_surface *sps);
118
119 extern struct pipe_surface *
120 sp_tile_cache_get_surface(struct softpipe_tile_cache *tc);
121
122 extern void
123 sp_tile_cache_map_transfers(struct softpipe_tile_cache *tc);
124
125 extern void
126 sp_tile_cache_unmap_transfers(struct softpipe_tile_cache *tc);
127
128 extern void
129 sp_tile_cache_set_texture(struct softpipe_tile_cache *tc,
130 struct pipe_texture *texture);
131
132 void
133 sp_tile_cache_validate_texture(struct softpipe_tile_cache *tc);
134
135 extern void
136 sp_flush_tile_cache(struct softpipe_tile_cache *tc);
137
138 extern void
139 sp_tile_cache_clear(struct softpipe_tile_cache *tc, const float *rgba,
140 uint clearValue);
141
142 extern struct softpipe_cached_tile *
143 sp_find_cached_tile(struct softpipe_tile_cache *tc,
144 union tile_address addr );
145
146 extern const struct softpipe_cached_tile *
147 sp_find_cached_tile_tex(struct softpipe_tile_cache *tc,
148 union tile_address addr );
149
150 static INLINE const union tile_address
151 tile_address( unsigned x,
152 unsigned y,
153 unsigned z,
154 unsigned face,
155 unsigned level )
156 {
157 union tile_address addr;
158
159 addr.value = 0;
160 addr.bits.x = x / TILE_SIZE;
161 addr.bits.y = y / TILE_SIZE;
162 addr.bits.z = z;
163 addr.bits.face = face;
164 addr.bits.level = level;
165
166 return addr;
167 }
168
169 /* Quickly retrieve tile if it matches last lookup.
170 */
171 static INLINE const struct softpipe_cached_tile *
172 sp_get_cached_tile_tex(struct softpipe_tile_cache *tc,
173 union tile_address addr )
174 {
175 if (tc->last_tile->addr.value == addr.value)
176 return tc->last_tile;
177
178 return sp_find_cached_tile_tex( tc, addr );
179 }
180
181
182 static INLINE struct softpipe_cached_tile *
183 sp_get_cached_tile(struct softpipe_tile_cache *tc,
184 int x, int y )
185 {
186 union tile_address addr = tile_address( x, y, 0, 0, 0 );
187
188 if (tc->last_tile->addr.value == addr.value)
189 return tc->last_tile;
190
191 return sp_find_cached_tile( tc, addr );
192 }
193
194
195
196
197 #endif /* SP_TILE_CACHE_H */
198