Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / drivers / cell / ppu / cell_texture.c
1 /**************************************************************************
2 *
3 * Copyright 2006 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 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Michel Dänzer <michel@tungstengraphics.com>
31 */
32
33 #include "pipe/p_context.h"
34 #include "pipe/p_defines.h"
35 #include "pipe/p_inlines.h"
36 #include "pipe/p_winsys.h"
37 #include "util/u_math.h"
38 #include "util/u_memory.h"
39
40 #include "cell_context.h"
41 #include "cell_state.h"
42 #include "cell_texture.h"
43
44
45 /* Simple, maximally packed layout.
46 */
47
48 static unsigned minify( unsigned d )
49 {
50 return MAX2(1, d>>1);
51 }
52
53
54 static void
55 cell_texture_layout(struct cell_texture * spt)
56 {
57 struct pipe_texture *pt = &spt->base;
58 unsigned level;
59 unsigned width = pt->width[0];
60 unsigned height = pt->height[0];
61 unsigned depth = pt->depth[0];
62
63 spt->buffer_size = 0;
64
65 for ( level = 0 ; level <= pt->last_level ; level++ ) {
66 unsigned size;
67 unsigned w_tile, h_tile;
68
69 /* width, height, rounded up to tile size */
70 w_tile = align(width, TILE_SIZE);
71 h_tile = align(height, TILE_SIZE);
72
73 pt->width[level] = width;
74 pt->height[level] = height;
75 pt->depth[level] = depth;
76 pt->nblocksx[level] = pf_get_nblocksx(&pt->block, w_tile);
77 pt->nblocksy[level] = pf_get_nblocksy(&pt->block, h_tile);
78
79 spt->stride[level] = pt->nblocksx[level] * pt->block.size;
80
81 spt->level_offset[level] = spt->buffer_size;
82
83 size = pt->nblocksx[level] * pt->nblocksy[level] * pt->block.size;
84 if (pt->target == PIPE_TEXTURE_CUBE)
85 size *= 6;
86 else
87 size *= depth;
88
89 spt->buffer_size += size;
90
91 width = minify(width);
92 height = minify(height);
93 depth = minify(depth);
94 }
95 }
96
97
98 static struct pipe_texture *
99 cell_texture_create(struct pipe_screen *screen,
100 const struct pipe_texture *templat)
101 {
102 struct pipe_winsys *ws = screen->winsys;
103 struct cell_texture *spt = CALLOC_STRUCT(cell_texture);
104 if (!spt)
105 return NULL;
106
107 spt->base = *templat;
108 spt->base.refcount = 1;
109 spt->base.screen = screen;
110
111 cell_texture_layout(spt);
112
113 spt->buffer = ws->buffer_create(ws, 32,
114 PIPE_BUFFER_USAGE_PIXEL,
115 spt->buffer_size);
116
117 if (!spt->buffer) {
118 FREE(spt);
119 return NULL;
120 }
121
122 return &spt->base;
123 }
124
125
126 static void
127 cell_texture_release(struct pipe_screen *screen,
128 struct pipe_texture **pt)
129 {
130 if (!*pt)
131 return;
132
133 /*
134 DBG("%s %p refcount will be %d\n",
135 __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
136 */
137 if (--(*pt)->refcount <= 0) {
138 struct cell_texture *spt = cell_texture(*pt);
139
140 /*
141 DBG("%s deleting %p\n", __FUNCTION__, (void *) spt);
142 */
143
144 pipe_buffer_reference(screen, &spt->buffer, NULL);
145
146 FREE(spt);
147 }
148 *pt = NULL;
149 }
150
151
152 #if 0
153 static void
154 cell_texture_update(struct pipe_context *pipe, struct pipe_texture *texture,
155 uint face, uint levelsMask)
156 {
157 /* XXX TO DO: re-tile the texture data ... */
158
159 }
160 #endif
161
162
163 static struct pipe_surface *
164 cell_get_tex_surface(struct pipe_screen *screen,
165 struct pipe_texture *pt,
166 unsigned face, unsigned level, unsigned zslice,
167 unsigned usage)
168 {
169 struct pipe_winsys *ws = screen->winsys;
170 struct cell_texture *spt = cell_texture(pt);
171 struct pipe_surface *ps;
172
173 ps = ws->surface_alloc(ws);
174 if (ps) {
175 assert(ps->refcount);
176 assert(ps->winsys);
177 winsys_buffer_reference(ws, &ps->buffer, spt->buffer);
178 ps->format = pt->format;
179 ps->block = pt->block;
180 ps->width = pt->width[level];
181 ps->height = pt->height[level];
182 ps->nblocksx = pt->nblocksx[level];
183 ps->nblocksy = pt->nblocksy[level];
184 ps->stride = spt->stride[level];
185 ps->offset = spt->level_offset[level];
186 ps->usage = usage;
187
188 /* XXX may need to override usage flags (see sp_texture.c) */
189
190 pipe_texture_reference(&ps->texture, pt);
191 ps->face = face;
192 ps->level = level;
193 ps->zslice = zslice;
194
195 if (pt->target == PIPE_TEXTURE_CUBE || pt->target == PIPE_TEXTURE_3D) {
196 ps->offset += ((pt->target == PIPE_TEXTURE_CUBE) ? face : zslice) *
197 ps->nblocksy *
198 ps->stride;
199 }
200 else {
201 assert(face == 0);
202 assert(zslice == 0);
203 }
204 }
205 return ps;
206 }
207
208
209
210 /**
211 * Copy tile data from linear layout to tiled layout.
212 * XXX this should be rolled into the future surface-creation code.
213 * XXX also need "untile" code...
214 */
215 static void
216 tile_copy_data(uint w, uint h, uint tile_size, uint *dst, const uint *src)
217 {
218 const uint tile_size2 = tile_size * tile_size;
219 const uint h_t = h / tile_size, w_t = w / tile_size;
220
221 uint it, jt; /* tile counters */
222 uint i, j; /* intra-tile counters */
223
224 /* loop over dest tiles */
225 for (it = 0; it < h_t; it++) {
226 for (jt = 0; jt < w_t; jt++) {
227 /* start of dest tile: */
228 uint *tdst = dst + (it * w_t + jt) * tile_size2;
229 /* loop over texels in the tile */
230 for (i = 0; i < tile_size; i++) {
231 for (j = 0; j < tile_size; j++) {
232 const uint srci = it * tile_size + i;
233 const uint srcj = jt * tile_size + j;
234 *tdst++ = src[srci * w + srcj];
235 }
236 }
237 }
238 }
239 }
240
241
242
243 /**
244 * Convert linear texture image data to tiled format for SPU usage.
245 * XXX recast this in terms of pipe_surfaces (aka texture views).
246 */
247 static void
248 cell_tile_texture(struct cell_context *cell,
249 struct cell_texture *texture)
250 {
251 struct pipe_screen *screen = cell->pipe.screen;
252 uint face = 0, level = 0, zslice = 0;
253 struct pipe_surface *surf;
254 const uint w = texture->base.width[0], h = texture->base.height[0];
255 const uint *src;
256
257 /* temporary restrictions: */
258 assert(w >= TILE_SIZE);
259 assert(h >= TILE_SIZE);
260 assert(w % TILE_SIZE == 0);
261 assert(h % TILE_SIZE == 0);
262
263 surf = screen->get_tex_surface(screen, &texture->base, face, level, zslice,
264 PIPE_BUFFER_USAGE_CPU_WRITE);
265 ASSERT(surf);
266
267 src = (const uint *) pipe_surface_map(surf, PIPE_BUFFER_USAGE_CPU_WRITE);
268
269 if (texture->tiled_data) {
270 align_free(texture->tiled_data);
271 }
272 texture->tiled_data = align_malloc(w * h * 4, 16);
273
274 tile_copy_data(w, h, TILE_SIZE, texture->tiled_data, src);
275
276 pipe_surface_unmap(surf);
277
278 pipe_surface_reference(&surf, NULL);
279 }
280
281
282 void
283 cell_update_texture_mapping(struct cell_context *cell)
284 {
285 #if 0
286 uint face = 0, level = 0, zslice = 0;
287 #endif
288 uint i;
289
290 for (i = 0; i < CELL_MAX_SAMPLERS; i++) {
291 if (cell->texture[i])
292 cell_tile_texture(cell, cell->texture[i]);
293 }
294
295 #if 0
296 if (cell->tex_surf && cell->tex_map) {
297 pipe_surface_unmap(cell->tex_surf);
298 cell->tex_map = NULL;
299 }
300
301 /* XXX free old surface */
302
303 cell->tex_surf = cell_get_tex_surface(&cell->pipe,
304 &cell->texture[0]->base,
305 face, level, zslice);
306
307 cell->tex_map = pipe_surface_map(cell->tex_surf);
308 #endif
309 }
310
311
312 static void
313 cell_tex_surface_release(struct pipe_screen *screen,
314 struct pipe_surface **s)
315 {
316 /* Effectively do the texture_update work here - if texture images
317 * needed post-processing to put them into hardware layout, this is
318 * where it would happen. For softpipe, nothing to do.
319 */
320 assert ((*s)->texture);
321 pipe_texture_reference(&(*s)->texture, NULL);
322
323 screen->winsys->surface_release(screen->winsys, s);
324 }
325
326
327 static void *
328 cell_surface_map( struct pipe_screen *screen,
329 struct pipe_surface *surface,
330 unsigned flags )
331 {
332 ubyte *map;
333
334 if (flags & ~surface->usage) {
335 assert(0);
336 return NULL;
337 }
338
339 map = pipe_buffer_map( screen, surface->buffer, flags );
340 if (map == NULL)
341 return NULL;
342
343 /* May want to different things here depending on read/write nature
344 * of the map:
345 */
346 if (surface->texture &&
347 (flags & PIPE_BUFFER_USAGE_CPU_WRITE))
348 {
349 /* Do something to notify sharing contexts of a texture change.
350 * In softpipe, that would mean flushing the texture cache.
351 */
352 #if 0
353 cell_screen(screen)->timestamp++;
354 #endif
355 }
356
357 return map + surface->offset;
358 }
359
360
361 static void
362 cell_surface_unmap(struct pipe_screen *screen,
363 struct pipe_surface *surface)
364 {
365 pipe_buffer_unmap( screen, surface->buffer );
366 }
367
368
369 void
370 cell_init_texture_functions(struct cell_context *cell)
371 {
372 /*cell->pipe.texture_update = cell_texture_update;*/
373 }
374
375
376 void
377 cell_init_screen_texture_funcs(struct pipe_screen *screen)
378 {
379 screen->texture_create = cell_texture_create;
380 screen->texture_release = cell_texture_release;
381
382 screen->get_tex_surface = cell_get_tex_surface;
383 screen->tex_surface_release = cell_tex_surface_release;
384
385 screen->surface_map = cell_surface_map;
386 screen->surface_unmap = cell_surface_unmap;
387 }