softpipe: add integer support
[mesa.git] / src / gallium / drivers / softpipe / sp_tex_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 "util/u_inlines.h"
36 #include "util/u_memory.h"
37 #include "util/u_tile.h"
38 #include "util/u_format.h"
39 #include "util/u_math.h"
40 #include "sp_context.h"
41 #include "sp_texture.h"
42 #include "sp_tex_tile_cache.h"
43
44
45
46 struct softpipe_tex_tile_cache *
47 sp_create_tex_tile_cache( struct pipe_context *pipe )
48 {
49 struct softpipe_tex_tile_cache *tc;
50 uint pos;
51
52 /* make sure max texture size works */
53 assert((TILE_SIZE << TEX_ADDR_BITS) >= (1 << (SP_MAX_TEXTURE_2D_LEVELS-1)));
54
55 tc = CALLOC_STRUCT( softpipe_tex_tile_cache );
56 if (tc) {
57 tc->pipe = pipe;
58 for (pos = 0; pos < NUM_ENTRIES; pos++) {
59 tc->entries[pos].addr.bits.invalid = 1;
60 }
61 tc->last_tile = &tc->entries[0]; /* any tile */
62 }
63 return tc;
64 }
65
66
67 void
68 sp_destroy_tex_tile_cache(struct softpipe_tex_tile_cache *tc)
69 {
70 if (tc) {
71 uint pos;
72
73 for (pos = 0; pos < NUM_ENTRIES; pos++) {
74 /*assert(tc->entries[pos].x < 0);*/
75 }
76 if (tc->transfer) {
77 tc->pipe->transfer_destroy(tc->pipe, tc->transfer);
78 }
79 if (tc->tex_trans) {
80 tc->pipe->transfer_destroy(tc->pipe, tc->tex_trans);
81 }
82
83 FREE( tc );
84 }
85 }
86
87
88
89
90 void
91 sp_tex_tile_cache_map_transfers(struct softpipe_tex_tile_cache *tc)
92 {
93 if (tc->tex_trans && !tc->tex_trans_map)
94 tc->tex_trans_map = tc->pipe->transfer_map(tc->pipe, tc->tex_trans);
95 }
96
97
98 void
99 sp_tex_tile_cache_unmap_transfers(struct softpipe_tex_tile_cache *tc)
100 {
101 if (tc->tex_trans_map) {
102 tc->pipe->transfer_unmap(tc->pipe, tc->tex_trans);
103 tc->tex_trans_map = NULL;
104 }
105 }
106
107 /**
108 * Invalidate all cached tiles for the cached texture.
109 * Should be called when the texture is modified.
110 */
111 void
112 sp_tex_tile_cache_validate_texture(struct softpipe_tex_tile_cache *tc)
113 {
114 unsigned i;
115
116 assert(tc);
117 assert(tc->texture);
118
119 for (i = 0; i < NUM_ENTRIES; i++) {
120 tc->entries[i].addr.bits.invalid = 1;
121 }
122 }
123
124 static boolean
125 sp_tex_tile_is_compat_view(struct softpipe_tex_tile_cache *tc,
126 struct pipe_sampler_view *view)
127 {
128 if (!view)
129 return FALSE;
130 return (tc->texture == view->texture &&
131 tc->format == view->format &&
132 tc->swizzle_r == view->swizzle_r &&
133 tc->swizzle_g == view->swizzle_g &&
134 tc->swizzle_b == view->swizzle_b &&
135 tc->swizzle_a == view->swizzle_a);
136 }
137
138 /**
139 * Specify the sampler view to cache.
140 */
141 void
142 sp_tex_tile_cache_set_sampler_view(struct softpipe_tex_tile_cache *tc,
143 struct pipe_sampler_view *view)
144 {
145 struct pipe_resource *texture = view ? view->texture : NULL;
146 uint i;
147
148 assert(!tc->transfer);
149
150 if (!sp_tex_tile_is_compat_view(tc, view)) {
151 pipe_resource_reference(&tc->texture, texture);
152
153 if (tc->tex_trans) {
154 if (tc->tex_trans_map) {
155 tc->pipe->transfer_unmap(tc->pipe, tc->tex_trans);
156 tc->tex_trans_map = NULL;
157 }
158
159 tc->pipe->transfer_destroy(tc->pipe, tc->tex_trans);
160 tc->tex_trans = NULL;
161 }
162
163 if (view) {
164 tc->swizzle_r = view->swizzle_r;
165 tc->swizzle_g = view->swizzle_g;
166 tc->swizzle_b = view->swizzle_b;
167 tc->swizzle_a = view->swizzle_a;
168 tc->format = view->format;
169 }
170
171 /* mark as entries as invalid/empty */
172 /* XXX we should try to avoid this when the teximage hasn't changed */
173 for (i = 0; i < NUM_ENTRIES; i++) {
174 tc->entries[i].addr.bits.invalid = 1;
175 }
176
177 tc->tex_face = -1; /* any invalid value here */
178 }
179 }
180
181
182
183
184 /**
185 * Flush the tile cache: write all dirty tiles back to the transfer.
186 * any tiles "flagged" as cleared will be "really" cleared.
187 */
188 void
189 sp_flush_tex_tile_cache(struct softpipe_tex_tile_cache *tc)
190 {
191 int pos;
192
193 if (tc->texture) {
194 /* caching a texture, mark all entries as empty */
195 for (pos = 0; pos < NUM_ENTRIES; pos++) {
196 tc->entries[pos].addr.bits.invalid = 1;
197 }
198 tc->tex_face = -1;
199 }
200
201 }
202
203
204 /**
205 * Given the texture face, level, zslice, x and y values, compute
206 * the cache entry position/index where we'd hope to find the
207 * cached texture tile.
208 * This is basically a direct-map cache.
209 * XXX There's probably lots of ways in which we can improve this.
210 */
211 static INLINE uint
212 tex_cache_pos( union tex_tile_address addr )
213 {
214 uint entry = (addr.bits.x +
215 addr.bits.y * 9 +
216 addr.bits.z * 3 +
217 addr.bits.face +
218 addr.bits.level * 7);
219
220 return entry % NUM_ENTRIES;
221 }
222
223 /**
224 * Similar to sp_get_cached_tile() but for textures.
225 * Tiles are read-only and indexed with more params.
226 */
227 const struct softpipe_tex_cached_tile *
228 sp_find_cached_tile_tex(struct softpipe_tex_tile_cache *tc,
229 union tex_tile_address addr )
230 {
231 struct softpipe_tex_cached_tile *tile;
232
233 tile = tc->entries + tex_cache_pos( addr );
234
235 if (addr.value != tile->addr.value) {
236
237 /* cache miss. Most misses are because we've invaldiated the
238 * texture cache previously -- most commonly on binding a new
239 * texture. Currently we effectively flush the cache on texture
240 * bind.
241 */
242 #if 0
243 _debug_printf("miss at %u: x=%d y=%d z=%d face=%d level=%d\n"
244 " tile %u: x=%d y=%d z=%d face=%d level=%d\n",
245 pos, x/TILE_SIZE, y/TILE_SIZE, z, face, level,
246 pos, tile->addr.bits.x, tile->addr.bits.y, tile->z, tile->face, tile->level);
247 #endif
248
249 /* check if we need to get a new transfer */
250 if (!tc->tex_trans ||
251 tc->tex_face != addr.bits.face ||
252 tc->tex_level != addr.bits.level ||
253 tc->tex_z != addr.bits.z) {
254 /* get new transfer (view into texture) */
255 unsigned width, height, layer;
256
257 if (tc->tex_trans) {
258 if (tc->tex_trans_map) {
259 tc->pipe->transfer_unmap(tc->pipe, tc->tex_trans);
260 tc->tex_trans_map = NULL;
261 }
262
263 tc->pipe->transfer_destroy(tc->pipe, tc->tex_trans);
264 tc->tex_trans = NULL;
265 }
266
267 width = u_minify(tc->texture->width0, addr.bits.level);
268 if (tc->texture->target == PIPE_TEXTURE_1D_ARRAY) {
269 height = tc->texture->array_size;
270 layer = 0;
271 }
272 else {
273 height = u_minify(tc->texture->height0, addr.bits.level);
274 layer = addr.bits.face + addr.bits.z;
275 }
276
277 tc->tex_trans =
278 pipe_get_transfer(tc->pipe, tc->texture,
279 addr.bits.level,
280 layer,
281 PIPE_TRANSFER_READ | PIPE_TRANSFER_UNSYNCHRONIZED,
282 0, 0, width, height);
283
284 tc->tex_trans_map = tc->pipe->transfer_map(tc->pipe, tc->tex_trans);
285
286 tc->tex_face = addr.bits.face;
287 tc->tex_level = addr.bits.level;
288 tc->tex_z = addr.bits.z;
289 }
290
291 /* Get tile from the transfer (view into texture), explicitly passing
292 * the image format.
293 */
294 if (util_format_is_pure_uint(tc->format)) {
295 pipe_get_tile_ui_format(tc->pipe,
296 tc->tex_trans,
297 addr.bits.x * TILE_SIZE,
298 addr.bits.y * TILE_SIZE,
299 TILE_SIZE,
300 TILE_SIZE,
301 tc->format,
302 (unsigned *) tile->data.colorui);
303 } else if (util_format_is_pure_sint(tc->format)) {
304 pipe_get_tile_i_format(tc->pipe,
305 tc->tex_trans,
306 addr.bits.x * TILE_SIZE,
307 addr.bits.y * TILE_SIZE,
308 TILE_SIZE,
309 TILE_SIZE,
310 tc->format,
311 (int *) tile->data.colori);
312 } else {
313 pipe_get_tile_rgba_format(tc->pipe,
314 tc->tex_trans,
315 addr.bits.x * TILE_SIZE,
316 addr.bits.y * TILE_SIZE,
317 TILE_SIZE,
318 TILE_SIZE,
319 tc->format,
320 (float *) tile->data.color);
321 }
322 tile->addr = addr;
323 }
324
325 tc->last_tile = tile;
326 return tile;
327 }