softpipe: remove unused functions
[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 * Invalidate all cached tiles for the cached texture.
90 * Should be called when the texture is modified.
91 */
92 void
93 sp_tex_tile_cache_validate_texture(struct softpipe_tex_tile_cache *tc)
94 {
95 unsigned i;
96
97 assert(tc);
98 assert(tc->texture);
99
100 for (i = 0; i < NUM_ENTRIES; i++) {
101 tc->entries[i].addr.bits.invalid = 1;
102 }
103 }
104
105 static boolean
106 sp_tex_tile_is_compat_view(struct softpipe_tex_tile_cache *tc,
107 struct pipe_sampler_view *view)
108 {
109 if (!view)
110 return FALSE;
111 return (tc->texture == view->texture &&
112 tc->format == view->format &&
113 tc->swizzle_r == view->swizzle_r &&
114 tc->swizzle_g == view->swizzle_g &&
115 tc->swizzle_b == view->swizzle_b &&
116 tc->swizzle_a == view->swizzle_a);
117 }
118
119 /**
120 * Specify the sampler view to cache.
121 */
122 void
123 sp_tex_tile_cache_set_sampler_view(struct softpipe_tex_tile_cache *tc,
124 struct pipe_sampler_view *view)
125 {
126 struct pipe_resource *texture = view ? view->texture : NULL;
127 uint i;
128
129 assert(!tc->transfer);
130
131 if (!sp_tex_tile_is_compat_view(tc, view)) {
132 pipe_resource_reference(&tc->texture, texture);
133
134 if (tc->tex_trans) {
135 if (tc->tex_trans_map) {
136 tc->pipe->transfer_unmap(tc->pipe, tc->tex_trans);
137 tc->tex_trans_map = NULL;
138 }
139
140 tc->pipe->transfer_destroy(tc->pipe, tc->tex_trans);
141 tc->tex_trans = NULL;
142 }
143
144 if (view) {
145 tc->swizzle_r = view->swizzle_r;
146 tc->swizzle_g = view->swizzle_g;
147 tc->swizzle_b = view->swizzle_b;
148 tc->swizzle_a = view->swizzle_a;
149 tc->format = view->format;
150 }
151
152 /* mark as entries as invalid/empty */
153 /* XXX we should try to avoid this when the teximage hasn't changed */
154 for (i = 0; i < NUM_ENTRIES; i++) {
155 tc->entries[i].addr.bits.invalid = 1;
156 }
157
158 tc->tex_face = -1; /* any invalid value here */
159 }
160 }
161
162
163
164
165 /**
166 * Flush the tile cache: write all dirty tiles back to the transfer.
167 * any tiles "flagged" as cleared will be "really" cleared.
168 */
169 void
170 sp_flush_tex_tile_cache(struct softpipe_tex_tile_cache *tc)
171 {
172 int pos;
173
174 if (tc->texture) {
175 /* caching a texture, mark all entries as empty */
176 for (pos = 0; pos < NUM_ENTRIES; pos++) {
177 tc->entries[pos].addr.bits.invalid = 1;
178 }
179 tc->tex_face = -1;
180 }
181
182 }
183
184
185 /**
186 * Given the texture face, level, zslice, x and y values, compute
187 * the cache entry position/index where we'd hope to find the
188 * cached texture tile.
189 * This is basically a direct-map cache.
190 * XXX There's probably lots of ways in which we can improve this.
191 */
192 static INLINE uint
193 tex_cache_pos( union tex_tile_address addr )
194 {
195 uint entry = (addr.bits.x +
196 addr.bits.y * 9 +
197 addr.bits.z * 3 +
198 addr.bits.face +
199 addr.bits.level * 7);
200
201 return entry % NUM_ENTRIES;
202 }
203
204 /**
205 * Similar to sp_get_cached_tile() but for textures.
206 * Tiles are read-only and indexed with more params.
207 */
208 const struct softpipe_tex_cached_tile *
209 sp_find_cached_tile_tex(struct softpipe_tex_tile_cache *tc,
210 union tex_tile_address addr )
211 {
212 struct softpipe_tex_cached_tile *tile;
213 boolean zs = util_format_is_depth_or_stencil(tc->format);
214
215 tile = tc->entries + tex_cache_pos( addr );
216
217 if (addr.value != tile->addr.value) {
218
219 /* cache miss. Most misses are because we've invaldiated the
220 * texture cache previously -- most commonly on binding a new
221 * texture. Currently we effectively flush the cache on texture
222 * bind.
223 */
224 #if 0
225 _debug_printf("miss at %u: x=%d y=%d z=%d face=%d level=%d\n"
226 " tile %u: x=%d y=%d z=%d face=%d level=%d\n",
227 pos, x/TILE_SIZE, y/TILE_SIZE, z, face, level,
228 pos, tile->addr.bits.x, tile->addr.bits.y, tile->z, tile->face, tile->level);
229 #endif
230
231 /* check if we need to get a new transfer */
232 if (!tc->tex_trans ||
233 tc->tex_face != addr.bits.face ||
234 tc->tex_level != addr.bits.level ||
235 tc->tex_z != addr.bits.z) {
236 /* get new transfer (view into texture) */
237 unsigned width, height, layer;
238
239 if (tc->tex_trans) {
240 if (tc->tex_trans_map) {
241 tc->pipe->transfer_unmap(tc->pipe, tc->tex_trans);
242 tc->tex_trans_map = NULL;
243 }
244
245 tc->pipe->transfer_destroy(tc->pipe, tc->tex_trans);
246 tc->tex_trans = NULL;
247 }
248
249 width = u_minify(tc->texture->width0, addr.bits.level);
250 if (tc->texture->target == PIPE_TEXTURE_1D_ARRAY) {
251 height = tc->texture->array_size;
252 layer = 0;
253 }
254 else {
255 height = u_minify(tc->texture->height0, addr.bits.level);
256 layer = addr.bits.face + addr.bits.z;
257 }
258
259 tc->tex_trans =
260 pipe_get_transfer(tc->pipe, tc->texture,
261 addr.bits.level,
262 layer,
263 PIPE_TRANSFER_READ | PIPE_TRANSFER_UNSYNCHRONIZED,
264 0, 0, width, height);
265
266 tc->tex_trans_map = tc->pipe->transfer_map(tc->pipe, tc->tex_trans);
267
268 tc->tex_face = addr.bits.face;
269 tc->tex_level = addr.bits.level;
270 tc->tex_z = addr.bits.z;
271 }
272
273 /* Get tile from the transfer (view into texture), explicitly passing
274 * the image format.
275 */
276 if (!zs && util_format_is_pure_uint(tc->format)) {
277 pipe_get_tile_ui_format(tc->pipe,
278 tc->tex_trans,
279 addr.bits.x * TILE_SIZE,
280 addr.bits.y * TILE_SIZE,
281 TILE_SIZE,
282 TILE_SIZE,
283 tc->format,
284 (unsigned *) tile->data.colorui);
285 } else if (!zs && util_format_is_pure_sint(tc->format)) {
286 pipe_get_tile_i_format(tc->pipe,
287 tc->tex_trans,
288 addr.bits.x * TILE_SIZE,
289 addr.bits.y * TILE_SIZE,
290 TILE_SIZE,
291 TILE_SIZE,
292 tc->format,
293 (int *) tile->data.colori);
294 } else {
295 pipe_get_tile_rgba_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 (float *) tile->data.color);
303 }
304 tile->addr = addr;
305 }
306
307 tc->last_tile = tile;
308 return tile;
309 }