1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 * Texture tile caching.
35 #include "util/u_inlines.h"
36 #include "util/u_memory.h"
37 #include "util/u_tile.h"
38 #include "util/u_math.h"
39 #include "sp_context.h"
40 #include "sp_texture.h"
41 #include "sp_tex_tile_cache.h"
45 struct softpipe_tex_tile_cache *
46 sp_create_tex_tile_cache( struct pipe_context *pipe )
48 struct softpipe_tex_tile_cache *tc;
51 tc = CALLOC_STRUCT( softpipe_tex_tile_cache );
54 for (pos = 0; pos < NUM_ENTRIES; pos++) {
55 tc->entries[pos].addr.bits.invalid = 1;
57 tc->last_tile = &tc->entries[0]; /* any tile */
64 sp_destroy_tex_tile_cache(struct softpipe_tex_tile_cache *tc)
68 for (pos = 0; pos < NUM_ENTRIES; pos++) {
69 /*assert(tc->entries[pos].x < 0);*/
72 tc->pipe->tex_transfer_destroy(tc->pipe, tc->transfer);
75 tc->pipe->tex_transfer_destroy(tc->pipe, tc->tex_trans);
85 sp_tex_tile_cache_map_transfers(struct softpipe_tex_tile_cache *tc)
87 if (tc->tex_trans && !tc->tex_trans_map)
88 tc->tex_trans_map = tc->pipe->transfer_map(tc->pipe, tc->tex_trans);
93 sp_tex_tile_cache_unmap_transfers(struct softpipe_tex_tile_cache *tc)
95 if (tc->tex_trans_map) {
96 tc->pipe->transfer_unmap(tc->pipe, tc->tex_trans);
97 tc->tex_trans_map = NULL;
102 * Invalidate all cached tiles for the cached texture.
103 * Should be called when the texture is modified.
106 sp_tex_tile_cache_validate_texture(struct softpipe_tex_tile_cache *tc)
113 for (i = 0; i < NUM_ENTRIES; i++) {
114 tc->entries[i].addr.bits.invalid = 1;
119 * Specify the sampler view to cache.
122 sp_tex_tile_cache_set_sampler_view(struct softpipe_tex_tile_cache *tc,
123 struct pipe_sampler_view *view)
125 struct pipe_texture *texture = view ? view->texture : NULL;
128 assert(!tc->transfer);
130 if (tc->texture != texture) {
131 pipe_texture_reference(&tc->texture, texture);
134 if (tc->tex_trans_map) {
135 tc->pipe->transfer_unmap(tc->pipe, tc->tex_trans);
136 tc->tex_trans_map = NULL;
139 tc->pipe->tex_transfer_destroy(tc->pipe, tc->tex_trans);
140 tc->tex_trans = NULL;
144 tc->swizzle_r = view->swizzle_r;
145 tc->swizzle_g = view->swizzle_g;
146 tc->swizzle_b = view->swizzle_b;
147 tc->swizzle_a = view->swizzle_a;
148 tc->format = view->format;
151 /* mark as entries as invalid/empty */
152 /* XXX we should try to avoid this when the teximage hasn't changed */
153 for (i = 0; i < NUM_ENTRIES; i++) {
154 tc->entries[i].addr.bits.invalid = 1;
157 tc->tex_face = -1; /* any invalid value here */
165 * Flush the tile cache: write all dirty tiles back to the transfer.
166 * any tiles "flagged" as cleared will be "really" cleared.
169 sp_flush_tex_tile_cache(struct softpipe_tex_tile_cache *tc)
174 /* caching a texture, mark all entries as empty */
175 for (pos = 0; pos < NUM_ENTRIES; pos++) {
176 tc->entries[pos].addr.bits.invalid = 1;
185 * Given the texture face, level, zslice, x and y values, compute
186 * the cache entry position/index where we'd hope to find the
187 * cached texture tile.
188 * This is basically a direct-map cache.
189 * XXX There's probably lots of ways in which we can improve this.
192 tex_cache_pos( union tex_tile_address addr )
194 uint entry = (addr.bits.x +
198 addr.bits.level * 7);
200 return entry % NUM_ENTRIES;
204 * Similar to sp_get_cached_tile() but for textures.
205 * Tiles are read-only and indexed with more params.
207 const struct softpipe_tex_cached_tile *
208 sp_find_cached_tile_tex(struct softpipe_tex_tile_cache *tc,
209 union tex_tile_address addr )
211 struct softpipe_tex_cached_tile *tile;
213 tile = tc->entries + tex_cache_pos( addr );
215 if (addr.value != tile->addr.value) {
217 /* cache miss. Most misses are because we've invaldiated the
218 * texture cache previously -- most commonly on binding a new
219 * texture. Currently we effectively flush the cache on texture
223 _debug_printf("miss at %u: x=%d y=%d z=%d face=%d level=%d\n"
224 " tile %u: x=%d y=%d z=%d face=%d level=%d\n",
225 pos, x/TILE_SIZE, y/TILE_SIZE, z, face, level,
226 pos, tile->addr.bits.x, tile->addr.bits.y, tile->z, tile->face, tile->level);
229 /* check if we need to get a new transfer */
230 if (!tc->tex_trans ||
231 tc->tex_face != addr.bits.face ||
232 tc->tex_level != addr.bits.level ||
233 tc->tex_z != addr.bits.z) {
234 /* get new transfer (view into texture) */
237 if (tc->tex_trans_map) {
238 tc->pipe->transfer_unmap(tc->pipe, tc->tex_trans);
239 tc->tex_trans_map = NULL;
242 tc->pipe->tex_transfer_destroy(tc->pipe, tc->tex_trans);
243 tc->tex_trans = NULL;
247 tc->pipe->get_tex_transfer(tc->pipe, tc->texture,
251 PIPE_TRANSFER_READ, 0, 0,
252 u_minify(tc->texture->width0, addr.bits.level),
253 u_minify(tc->texture->height0, addr.bits.level));
255 tc->tex_trans_map = tc->pipe->transfer_map(tc->pipe, tc->tex_trans);
257 tc->tex_face = addr.bits.face;
258 tc->tex_level = addr.bits.level;
259 tc->tex_z = addr.bits.z;
262 /* get tile from the transfer (view into texture) */
263 pipe_get_tile_swizzle(tc->pipe,
265 addr.bits.x * TILE_SIZE,
266 addr.bits.y * TILE_SIZE,
274 (float *) tile->data.color);
278 tc->last_tile = tile;