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