Merge branch 'mesa_7_6_branch'
[mesa.git] / src / gallium / drivers / llvmpipe / lp_tile_cache.c
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 /**
29 * Texture tile caching.
30 *
31 * Author:
32 * Brian Paul
33 */
34
35 #include "pipe/p_inlines.h"
36 #include "util/u_memory.h"
37 #include "util/u_math.h"
38 #include "util/u_tile.h"
39 #include "util/u_rect.h"
40 #include "lp_context.h"
41 #include "lp_surface.h"
42 #include "lp_texture.h"
43 #include "lp_tile_soa.h"
44 #include "lp_tile_cache.h"
45
46
47 #define MAX_WIDTH 4096
48 #define MAX_HEIGHT 4096
49
50
51 enum llvmpipe_tile_status
52 {
53 LP_TILE_STATUS_UNDEFINED = 0,
54 LP_TILE_STATUS_CLEAR = 1,
55 LP_TILE_STATUS_DEFINED = 2
56 };
57
58
59 struct llvmpipe_cached_tile
60 {
61 enum llvmpipe_tile_status status;
62
63 /** color in SOA format */
64 uint8_t *color;
65 };
66
67
68 struct llvmpipe_tile_cache
69 {
70 struct pipe_screen *screen;
71 struct pipe_surface *surface; /**< the surface we're caching */
72 struct pipe_transfer *transfer;
73 void *transfer_map;
74
75 struct llvmpipe_cached_tile entries[MAX_WIDTH/TILE_SIZE][MAX_HEIGHT/TILE_SIZE];
76
77 uint8_t clear_color[4]; /**< for color bufs */
78 uint clear_val; /**< for z+stencil, or packed color clear value */
79
80 struct llvmpipe_cached_tile *last_tile; /**< most recently retrieved tile */
81 };
82
83
84 struct llvmpipe_tile_cache *
85 lp_create_tile_cache( struct pipe_screen *screen )
86 {
87 struct llvmpipe_tile_cache *tc;
88 int maxLevels, maxTexSize;
89
90 /* sanity checking: max sure MAX_WIDTH/HEIGHT >= largest texture image */
91 maxLevels = screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS);
92 maxTexSize = 1 << (maxLevels - 1);
93 assert(MAX_WIDTH >= maxTexSize);
94
95 tc = CALLOC_STRUCT( llvmpipe_tile_cache );
96 if(!tc)
97 return NULL;
98
99 tc->screen = screen;
100
101 return tc;
102 }
103
104
105 void
106 lp_destroy_tile_cache(struct llvmpipe_tile_cache *tc)
107 {
108 struct pipe_screen *screen;
109 unsigned x, y;
110
111 for (y = 0; y < MAX_HEIGHT; y += TILE_SIZE) {
112 for (x = 0; x < MAX_WIDTH; x += TILE_SIZE) {
113 struct llvmpipe_cached_tile *tile = &tc->entries[y/TILE_SIZE][x/TILE_SIZE];
114
115 if(tile->color)
116 align_free(tile->color);
117 }
118 }
119
120 if (tc->transfer) {
121 screen = tc->transfer->texture->screen;
122 screen->tex_transfer_destroy(tc->transfer);
123 }
124
125 FREE( tc );
126 }
127
128
129 /**
130 * Specify the surface to cache.
131 */
132 void
133 lp_tile_cache_set_surface(struct llvmpipe_tile_cache *tc,
134 struct pipe_surface *ps)
135 {
136 if (tc->transfer) {
137 struct pipe_screen *screen = tc->transfer->texture->screen;
138
139 if (ps == tc->surface)
140 return;
141
142 if (tc->transfer_map) {
143 screen->transfer_unmap(screen, tc->transfer);
144 tc->transfer_map = NULL;
145 }
146
147 screen->tex_transfer_destroy(tc->transfer);
148 tc->transfer = NULL;
149 }
150
151 tc->surface = ps;
152
153 if (ps) {
154 struct pipe_screen *screen = ps->texture->screen;
155 unsigned x, y;
156
157 tc->transfer = screen->get_tex_transfer(screen, ps->texture, ps->face,
158 ps->level, ps->zslice,
159 PIPE_TRANSFER_READ_WRITE,
160 0, 0, ps->width, ps->height);
161
162 for (y = 0; y < ps->height; y += TILE_SIZE) {
163 for (x = 0; x < ps->width; x += TILE_SIZE) {
164 struct llvmpipe_cached_tile *tile = &tc->entries[y/TILE_SIZE][x/TILE_SIZE];
165
166 tile->status = LP_TILE_STATUS_UNDEFINED;
167
168 if(!tile->color)
169 tile->color = align_malloc( TILE_SIZE*TILE_SIZE*NUM_CHANNELS, 16 );
170 }
171 }
172 }
173 }
174
175
176 /**
177 * Return the transfer being cached.
178 */
179 struct pipe_surface *
180 lp_tile_cache_get_surface(struct llvmpipe_tile_cache *tc)
181 {
182 return tc->surface;
183 }
184
185
186 void
187 lp_tile_cache_map_transfers(struct llvmpipe_tile_cache *tc)
188 {
189 if (tc->transfer && !tc->transfer_map)
190 tc->transfer_map = tc->screen->transfer_map(tc->screen, tc->transfer);
191 }
192
193
194 void
195 lp_tile_cache_unmap_transfers(struct llvmpipe_tile_cache *tc)
196 {
197 if (tc->transfer_map) {
198 tc->screen->transfer_unmap(tc->screen, tc->transfer);
199 tc->transfer_map = NULL;
200 }
201 }
202
203
204 /**
205 * Set a tile to a solid color.
206 */
207 static void
208 clear_tile(struct llvmpipe_cached_tile *tile,
209 uint8_t clear_color[4])
210 {
211 if (clear_color[0] == clear_color[1] &&
212 clear_color[1] == clear_color[2] &&
213 clear_color[2] == clear_color[3]) {
214 memset(tile->color, clear_color[0], TILE_SIZE * TILE_SIZE * 4);
215 }
216 else {
217 uint x, y, chan;
218 for (y = 0; y < TILE_SIZE; y++)
219 for (x = 0; x < TILE_SIZE; x++)
220 for (chan = 0; chan < 4; ++chan)
221 TILE_PIXEL(tile->color, x, y, chan) = clear_color[chan];
222 }
223 }
224
225
226 /**
227 * Flush the tile cache: write all dirty tiles back to the transfer.
228 * any tiles "flagged" as cleared will be "really" cleared.
229 */
230 void
231 lp_flush_tile_cache(struct llvmpipe_tile_cache *tc)
232 {
233 struct pipe_transfer *pt = tc->transfer;
234 unsigned x, y;
235
236 if(!pt)
237 return;
238
239 /* push the tile to all positions marked as clear */
240 for (y = 0; y < pt->height; y += TILE_SIZE) {
241 for (x = 0; x < pt->width; x += TILE_SIZE) {
242 struct llvmpipe_cached_tile *tile = &tc->entries[y/TILE_SIZE][x/TILE_SIZE];
243
244 switch(tile->status) {
245 case LP_TILE_STATUS_UNDEFINED:
246 break;
247
248 case LP_TILE_STATUS_CLEAR: {
249 /**
250 * Actually clear the tiles which were flagged as being in a clear state.
251 */
252
253 struct pipe_screen *screen = pt->texture->screen;
254 unsigned tw = TILE_SIZE;
255 unsigned th = TILE_SIZE;
256 void *dst;
257
258 if (pipe_clip_tile(x, y, &tw, &th, pt))
259 continue;
260
261 dst = screen->transfer_map(screen, pt);
262 assert(dst);
263 if(!dst)
264 continue;
265
266 util_fill_rect(dst, &pt->block, pt->stride,
267 x, y, tw, th,
268 tc->clear_val);
269
270 screen->transfer_unmap(screen, pt);
271
272 tile->status = LP_TILE_STATUS_UNDEFINED;
273 break;
274 }
275
276 case LP_TILE_STATUS_DEFINED:
277 lp_put_tile_rgba_soa(pt, x, y, tile->color);
278 tile->status = LP_TILE_STATUS_UNDEFINED;
279 break;
280 }
281 }
282 }
283 }
284
285
286 /**
287 * Get a tile from the cache.
288 * \param x, y position of tile, in pixels
289 */
290 void *
291 lp_get_cached_tile(struct llvmpipe_tile_cache *tc,
292 unsigned x, unsigned y )
293 {
294 struct llvmpipe_cached_tile *tile = &tc->entries[y/TILE_SIZE][x/TILE_SIZE];
295 struct pipe_transfer *pt = tc->transfer;
296
297 switch(tile->status) {
298 case LP_TILE_STATUS_CLEAR:
299 /* don't get tile from framebuffer, just clear it */
300 clear_tile(tile, tc->clear_color);
301 tile->status = LP_TILE_STATUS_DEFINED;
302 break;
303
304 case LP_TILE_STATUS_UNDEFINED:
305 /* get new tile data from transfer */
306 lp_get_tile_rgba_soa(pt, x & ~(TILE_SIZE - 1), y & ~(TILE_SIZE - 1), tile->color);
307 tile->status = LP_TILE_STATUS_DEFINED;
308 break;
309
310 case LP_TILE_STATUS_DEFINED:
311 /* nothing to do */
312 break;
313 }
314
315 return tile->color;
316 }
317
318
319 /**
320 * When a whole surface is being cleared to a value we can avoid
321 * fetching tiles above.
322 * Save the color and set a 'clearflag' for each tile of the screen.
323 */
324 void
325 lp_tile_cache_clear(struct llvmpipe_tile_cache *tc, const float *rgba,
326 uint clearValue)
327 {
328 struct pipe_transfer *pt = tc->transfer;
329 const unsigned w = pt->width;
330 const unsigned h = pt->height;
331 unsigned x, y, chan;
332
333 for(chan = 0; chan < 4; ++chan)
334 tc->clear_color[chan] = float_to_ubyte(rgba[chan]);
335
336 tc->clear_val = clearValue;
337
338 /* push the tile to all positions marked as clear */
339 for (y = 0; y < h; y += TILE_SIZE) {
340 for (x = 0; x < w; x += TILE_SIZE) {
341 struct llvmpipe_cached_tile *tile = &tc->entries[y/TILE_SIZE][x/TILE_SIZE];
342 tile->status = LP_TILE_STATUS_CLEAR;
343 }
344 }
345 }