1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/**************************************************************************
*
* Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
* IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**************************************************************************/
/**
* Texture tile caching.
*
* Author:
* Brian Paul
*/
#include "pipe/p_inlines.h"
#include "util/u_memory.h"
#include "util/u_math.h"
#include "util/u_tile.h"
#include "util/u_rect.h"
#include "lp_context.h"
#include "lp_surface.h"
#include "lp_texture.h"
#include "lp_tile_soa.h"
#include "lp_tile_cache.h"
#define MAX_WIDTH 4096
#define MAX_HEIGHT 4096
enum llvmpipe_tile_status
{
LP_TILE_STATUS_UNDEFINED = 0,
LP_TILE_STATUS_CLEAR = 1,
LP_TILE_STATUS_DEFINED = 2
};
struct llvmpipe_cached_tile
{
enum llvmpipe_tile_status status;
/** color in SOA format */
uint8_t *color;
};
struct llvmpipe_tile_cache
{
struct pipe_screen *screen;
struct pipe_surface *surface; /**< the surface we're caching */
struct pipe_transfer *transfer;
void *transfer_map;
struct llvmpipe_cached_tile entries[MAX_WIDTH/TILE_SIZE][MAX_HEIGHT/TILE_SIZE];
uint8_t clear_color[4]; /**< for color bufs */
uint clear_val; /**< for z+stencil, or packed color clear value */
struct llvmpipe_cached_tile *last_tile; /**< most recently retrieved tile */
};
struct llvmpipe_tile_cache *
lp_create_tile_cache( struct pipe_screen *screen )
{
struct llvmpipe_tile_cache *tc;
int maxLevels, maxTexSize;
/* sanity checking: max sure MAX_WIDTH/HEIGHT >= largest texture image */
maxLevels = screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS);
maxTexSize = 1 << (maxLevels - 1);
assert(MAX_WIDTH >= maxTexSize);
tc = CALLOC_STRUCT( llvmpipe_tile_cache );
if(!tc)
return NULL;
tc->screen = screen;
return tc;
}
void
lp_destroy_tile_cache(struct llvmpipe_tile_cache *tc)
{
struct pipe_screen *screen;
unsigned x, y;
for (y = 0; y < MAX_HEIGHT; y += TILE_SIZE) {
for (x = 0; x < MAX_WIDTH; x += TILE_SIZE) {
struct llvmpipe_cached_tile *tile = &tc->entries[y/TILE_SIZE][x/TILE_SIZE];
if(tile->color)
align_free(tile->color);
}
}
if (tc->transfer) {
screen = tc->transfer->texture->screen;
screen->tex_transfer_destroy(tc->transfer);
}
FREE( tc );
}
/**
* Specify the surface to cache.
*/
void
lp_tile_cache_set_surface(struct llvmpipe_tile_cache *tc,
struct pipe_surface *ps)
{
if (tc->transfer) {
struct pipe_screen *screen = tc->transfer->texture->screen;
if (ps == tc->surface)
return;
if (tc->transfer_map) {
screen->transfer_unmap(screen, tc->transfer);
tc->transfer_map = NULL;
}
screen->tex_transfer_destroy(tc->transfer);
tc->transfer = NULL;
}
tc->surface = ps;
if (ps) {
struct pipe_screen *screen = ps->texture->screen;
unsigned x, y;
tc->transfer = screen->get_tex_transfer(screen, ps->texture, ps->face,
ps->level, ps->zslice,
PIPE_TRANSFER_READ_WRITE,
0, 0, ps->width, ps->height);
for (y = 0; y < ps->height; y += TILE_SIZE) {
for (x = 0; x < ps->width; x += TILE_SIZE) {
struct llvmpipe_cached_tile *tile = &tc->entries[y/TILE_SIZE][x/TILE_SIZE];
tile->status = LP_TILE_STATUS_UNDEFINED;
if(!tile->color)
tile->color = align_malloc( TILE_SIZE*TILE_SIZE*NUM_CHANNELS, 16 );
}
}
}
}
/**
* Return the transfer being cached.
*/
struct pipe_surface *
lp_tile_cache_get_surface(struct llvmpipe_tile_cache *tc)
{
return tc->surface;
}
void
lp_tile_cache_map_transfers(struct llvmpipe_tile_cache *tc)
{
if (tc->transfer && !tc->transfer_map)
tc->transfer_map = tc->screen->transfer_map(tc->screen, tc->transfer);
}
void
lp_tile_cache_unmap_transfers(struct llvmpipe_tile_cache *tc)
{
if (tc->transfer_map) {
tc->screen->transfer_unmap(tc->screen, tc->transfer);
tc->transfer_map = NULL;
}
}
/**
* Set a tile to a solid color.
*/
static void
clear_tile(struct llvmpipe_cached_tile *tile,
uint8_t clear_color[4])
{
if (clear_color[0] == clear_color[1] &&
clear_color[1] == clear_color[2] &&
clear_color[2] == clear_color[3]) {
memset(tile->color, clear_color[0], TILE_SIZE * TILE_SIZE * 4);
}
else {
uint x, y, chan;
for (y = 0; y < TILE_SIZE; y++)
for (x = 0; x < TILE_SIZE; x++)
for (chan = 0; chan < 4; ++chan)
TILE_PIXEL(tile->color, x, y, chan) = clear_color[chan];
}
}
/**
* Flush the tile cache: write all dirty tiles back to the transfer.
* any tiles "flagged" as cleared will be "really" cleared.
*/
void
lp_flush_tile_cache(struct llvmpipe_tile_cache *tc)
{
struct pipe_transfer *pt = tc->transfer;
unsigned x, y;
if(!pt)
return;
/* push the tile to all positions marked as clear */
for (y = 0; y < pt->height; y += TILE_SIZE) {
for (x = 0; x < pt->width; x += TILE_SIZE) {
struct llvmpipe_cached_tile *tile = &tc->entries[y/TILE_SIZE][x/TILE_SIZE];
if(tile->status != LP_TILE_STATUS_UNDEFINED) {
unsigned w = TILE_SIZE;
unsigned h = TILE_SIZE;
if (!pipe_clip_tile(x, y, &w, &h, pt)) {
switch(tile->status) {
case LP_TILE_STATUS_CLEAR:
/* Actually clear the tiles which were flagged as being in a
* clear state. */
util_fill_rect(tc->transfer_map, &pt->block, pt->stride,
x, y, w, h,
tc->clear_val);
break;
case LP_TILE_STATUS_DEFINED:
lp_tile_write_4ub(pt->format,
tile->color,
tc->transfer_map, pt->stride,
x, y, w, h);
break;
default:
assert(0);
break;
}
}
tile->status = LP_TILE_STATUS_UNDEFINED;
}
}
}
}
/**
* Get a tile from the cache.
* \param x, y position of tile, in pixels
*/
void *
lp_get_cached_tile(struct llvmpipe_tile_cache *tc,
unsigned x, unsigned y )
{
struct llvmpipe_cached_tile *tile = &tc->entries[y/TILE_SIZE][x/TILE_SIZE];
struct pipe_transfer *pt = tc->transfer;
assert(tc->surface);
assert(tc->transfer);
switch(tile->status) {
case LP_TILE_STATUS_CLEAR:
/* don't get tile from framebuffer, just clear it */
clear_tile(tile, tc->clear_color);
tile->status = LP_TILE_STATUS_DEFINED;
break;
case LP_TILE_STATUS_UNDEFINED: {
unsigned w = TILE_SIZE;
unsigned h = TILE_SIZE;
x &= ~(TILE_SIZE - 1);
y &= ~(TILE_SIZE - 1);
if (!pipe_clip_tile(x, y, &w, &h, tc->transfer))
lp_tile_read_4ub(pt->format,
tile->color,
tc->transfer_map, tc->transfer->stride,
x, y, w, h);
tile->status = LP_TILE_STATUS_DEFINED;
break;
}
case LP_TILE_STATUS_DEFINED:
/* nothing to do */
break;
}
return tile->color;
}
/**
* When a whole surface is being cleared to a value we can avoid
* fetching tiles above.
* Save the color and set a 'clearflag' for each tile of the screen.
*/
void
lp_tile_cache_clear(struct llvmpipe_tile_cache *tc, const float *rgba,
uint clearValue)
{
struct pipe_transfer *pt = tc->transfer;
const unsigned w = pt->width;
const unsigned h = pt->height;
unsigned x, y, chan;
for(chan = 0; chan < 4; ++chan)
tc->clear_color[chan] = float_to_ubyte(rgba[chan]);
tc->clear_val = clearValue;
/* push the tile to all positions marked as clear */
for (y = 0; y < h; y += TILE_SIZE) {
for (x = 0; x < w; x += TILE_SIZE) {
struct llvmpipe_cached_tile *tile = &tc->entries[y/TILE_SIZE][x/TILE_SIZE];
tile->status = LP_TILE_STATUS_CLEAR;
}
}
}
|