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 * Brian Paul
32 */
33
34 #include "pipe/p_context.h"
35 #include "pipe/p_defines.h"
36 #include "pipe/p_inlines.h"
37 #include "pipe/p_winsys.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
40
41 #include "cell_context.h"
42 #include "cell_state.h"
43 #include "cell_texture.h"
44
45
46
47 static unsigned
48 minify(unsigned d)
49 {
50 return MAX2(1, d>>1);
51 }
52
53
54 static void
55 cell_texture_layout(struct cell_texture *ct)
56 {
57 struct pipe_texture *pt = &ct->base;
58 unsigned level;
59 unsigned width = pt->width[0];
60 unsigned height = pt->height[0];
61 unsigned depth = pt->depth[0];
62
63 ct->buffer_size = 0;
64
65 for ( level = 0 ; level <= pt->last_level ; level++ ) {
66 unsigned size;
67 unsigned w_tile, h_tile;
68
69 assert(level < CELL_MAX_TEXTURE_LEVELS);
70
71 /* width, height, rounded up to tile size */
72 w_tile = align(width, TILE_SIZE);
73 h_tile = align(height, TILE_SIZE);
74
75 pt->width[level] = width;
76 pt->height[level] = height;
77 pt->depth[level] = depth;
78 pt->nblocksx[level] = pf_get_nblocksx(&pt->block, w_tile);
79 pt->nblocksy[level] = pf_get_nblocksy(&pt->block, h_tile);
80
81 ct->stride[level] = pt->nblocksx[level] * pt->block.size;
82
83 ct->level_offset[level] = ct->buffer_size;
84
85 size = pt->nblocksx[level] * pt->nblocksy[level] * pt->block.size;
86 if (pt->target == PIPE_TEXTURE_CUBE)
87 size *= 6;
88 else
89 size *= depth;
90
91 ct->buffer_size += size;
92
93 width = minify(width);
94 height = minify(height);
95 depth = minify(depth);
96 }
97 }
98
99
100 static struct pipe_texture *
101 cell_texture_create(struct pipe_screen *screen,
102 const struct pipe_texture *templat)
103 {
104 struct pipe_winsys *ws = screen->winsys;
105 struct cell_texture *ct = CALLOC_STRUCT(cell_texture);
106 if (!ct)
107 return NULL;
108
109 ct->base = *templat;
110 ct->base.refcount = 1;
111 ct->base.screen = screen;
112
113 cell_texture_layout(ct);
114
115 ct->buffer = ws->buffer_create(ws, 32, PIPE_BUFFER_USAGE_PIXEL,
116 ct->buffer_size);
117
118 if (!ct->buffer) {
119 FREE(ct);
120 return NULL;
121 }
122
123 return &ct->base;
124 }
125
126
127 static void
128 cell_texture_release(struct pipe_screen *screen,
129 struct pipe_texture **pt)
130 {
131 if (!*pt)
132 return;
133
134 /*
135 DBG("%s %p refcount will be %d\n",
136 __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
137 */
138 if (--(*pt)->refcount <= 0) {
139 /* Delete this texture now.
140 * But note that the underlying pipe_buffer may linger...
141 */
142 struct cell_texture *ct = cell_texture(*pt);
143 uint i;
144
145 /*
146 DBG("%s deleting %p\n", __FUNCTION__, (void *) ct);
147 */
148
149 pipe_buffer_reference(screen, &ct->buffer, NULL);
150
151 for (i = 0; i < CELL_MAX_TEXTURE_LEVELS; i++) {
152 /* Unreference the tiled image buffer.
153 * It may not actually be deleted until a fence is hit.
154 */
155 if (ct->tiled_buffer[i]) {
156 ct->tiled_mapped[i] = NULL;
157 winsys_buffer_reference(screen->winsys, &ct->tiled_buffer[i], NULL);
158 }
159 }
160
161 FREE(ct);
162 }
163 *pt = NULL;
164 }
165
166
167
168 /**
169 * Convert image from linear layout to tiled layout. 4-byte pixels.
170 */
171 static void
172 twiddle_image_uint(uint w, uint h, uint tile_size, uint *dst,
173 uint src_stride, const uint *src)
174 {
175 const uint tile_size2 = tile_size * tile_size;
176 const uint h_t = (h + tile_size - 1) / tile_size;
177 const uint w_t = (w + tile_size - 1) / tile_size;
178
179 uint it, jt; /* tile counters */
180 uint i, j; /* intra-tile counters */
181
182 src_stride /= 4; /* convert from bytes to pixels */
183
184 /* loop over dest tiles */
185 for (it = 0; it < h_t; it++) {
186 for (jt = 0; jt < w_t; jt++) {
187 /* start of dest tile: */
188 uint *tdst = dst + (it * w_t + jt) * tile_size2;
189
190 /* compute size of this tile (may be smaller than tile_size) */
191 /* XXX note: a compiler bug was found here. That's why the code
192 * looks as it does.
193 */
194 uint tile_width = w - jt * tile_size;
195 tile_width = MIN2(tile_width, tile_size);
196 uint tile_height = h - it * tile_size;
197 tile_height = MIN2(tile_height, tile_size);
198
199 /* loop over texels in the tile */
200 for (i = 0; i < tile_height; i++) {
201 for (j = 0; j < tile_width; j++) {
202 const uint srci = it * tile_size + i;
203 const uint srcj = jt * tile_size + j;
204 ASSERT(srci < h);
205 ASSERT(srcj < w);
206 tdst[i * tile_size + j] = src[srci * src_stride + srcj];
207 }
208 }
209 }
210 }
211 }
212
213
214 /**
215 * For Cell. Basically, rearrange the pixels/quads from this layout:
216 * +--+--+--+--+
217 * |p0|p1|p2|p3|....
218 * +--+--+--+--+
219 *
220 * to this layout:
221 * +--+--+
222 * |p0|p1|....
223 * +--+--+
224 * |p2|p3|
225 * +--+--+
226 */
227 static void
228 twiddle_tile(const uint *tileIn, uint *tileOut)
229 {
230 int y, x;
231
232 for (y = 0; y < TILE_SIZE; y+=2) {
233 for (x = 0; x < TILE_SIZE; x+=2) {
234 int k = 4 * (y/2 * TILE_SIZE/2 + x/2);
235 tileOut[y * TILE_SIZE + (x + 0)] = tileIn[k];
236 tileOut[y * TILE_SIZE + (x + 1)] = tileIn[k+1];
237 tileOut[(y + 1) * TILE_SIZE + (x + 0)] = tileIn[k+2];
238 tileOut[(y + 1) * TILE_SIZE + (x + 1)] = tileIn[k+3];
239 }
240 }
241 }
242
243
244 /**
245 * Convert image from tiled layout to linear layout. 4-byte pixels.
246 */
247 static void
248 untwiddle_image_uint(uint w, uint h, uint tile_size, uint *dst,
249 uint dst_stride, const uint *src)
250 {
251 const uint tile_size2 = tile_size * tile_size;
252 const uint h_t = (h + tile_size - 1) / tile_size;
253 const uint w_t = (w + tile_size - 1) / tile_size;
254 uint *tile_buf;
255 uint it, jt; /* tile counters */
256 uint i, j; /* intra-tile counters */
257
258 dst_stride /= 4; /* convert from bytes to pixels */
259
260 tile_buf = align_malloc(tile_size * tile_size * 4, 16);
261
262 /* loop over src tiles */
263 for (it = 0; it < h_t; it++) {
264 for (jt = 0; jt < w_t; jt++) {
265 /* start of src tile: */
266 const uint *tsrc = src + (it * w_t + jt) * tile_size2;
267
268 twiddle_tile(tsrc, tile_buf);
269 tsrc = tile_buf;
270
271 /* compute size of this tile (may be smaller than tile_size) */
272 /* XXX note: a compiler bug was found here. That's why the code
273 * looks as it does.
274 */
275 uint tile_width = w - jt * tile_size;
276 tile_width = MIN2(tile_width, tile_size);
277 uint tile_height = h - it * tile_size;
278 tile_height = MIN2(tile_height, tile_size);
279
280 /* loop over texels in the tile */
281 for (i = 0; i < tile_height; i++) {
282 for (j = 0; j < tile_width; j++) {
283 uint dsti = it * tile_size + i;
284 uint dstj = jt * tile_size + j;
285 ASSERT(dsti < h);
286 ASSERT(dstj < w);
287 dst[dsti * dst_stride + dstj] = tsrc[i * tile_size + j];
288 }
289 }
290 }
291 }
292
293 align_free(tile_buf);
294 }
295
296
297 /**
298 * Convert linear texture image data to tiled format for SPU usage.
299 */
300 static void
301 cell_twiddle_texture(struct pipe_screen *screen,
302 struct pipe_surface *surface)
303 {
304 struct cell_texture *ct = cell_texture(surface->texture);
305 const uint level = surface->level;
306 const uint texWidth = ct->base.width[level];
307 const uint texHeight = ct->base.height[level];
308 const uint bufWidth = align(texWidth, TILE_SIZE);
309 const uint bufHeight = align(texHeight, TILE_SIZE);
310 const void *map = pipe_buffer_map(screen, surface->buffer,
311 PIPE_BUFFER_USAGE_CPU_READ);
312 const uint *src = (const uint *) ((const ubyte *) map + surface->offset);
313
314 switch (ct->base.format) {
315 case PIPE_FORMAT_A8R8G8B8_UNORM:
316 case PIPE_FORMAT_B8G8R8A8_UNORM:
317 case PIPE_FORMAT_S8Z24_UNORM:
318 {
319 int numFaces = ct->base.target == PIPE_TEXTURE_CUBE ? 6 : 1;
320 int offset = bufWidth * bufHeight * 4 * surface->face;
321 uint *dst;
322
323 if (!ct->tiled_buffer[level]) {
324 /* allocate buffer for tiled data now */
325 struct pipe_winsys *ws = screen->winsys;
326 uint bytes = bufWidth * bufHeight * 4 * numFaces;
327 ct->tiled_buffer[level] = ws->buffer_create(ws, 16,
328 PIPE_BUFFER_USAGE_PIXEL,
329 bytes);
330 /* and map it */
331 ct->tiled_mapped[level] = ws->buffer_map(ws, ct->tiled_buffer[level],
332 PIPE_BUFFER_USAGE_GPU_READ);
333 }
334 dst = (uint *) ((ubyte *) ct->tiled_mapped[level] + offset);
335
336 twiddle_image_uint(texWidth, texHeight, TILE_SIZE, dst,
337 surface->stride, src);
338 }
339 break;
340 default:
341 printf("Cell: twiddle unsupported texture format %s\n", pf_name(ct->base.format));
342 ;
343 }
344
345 pipe_buffer_unmap(screen, surface->buffer);
346 }
347
348
349 /**
350 * Convert SPU tiled texture image data to linear format for app usage.
351 */
352 static void
353 cell_untwiddle_texture(struct pipe_screen *screen,
354 struct pipe_surface *surface)
355 {
356 struct cell_texture *ct = cell_texture(surface->texture);
357 const uint level = surface->level;
358 const uint texWidth = ct->base.width[level];
359 const uint texHeight = ct->base.height[level];
360 const void *map = pipe_buffer_map(screen, surface->buffer,
361 PIPE_BUFFER_USAGE_CPU_READ);
362 const uint *src = (const uint *) ((const ubyte *) map + surface->offset);
363
364 switch (ct->base.format) {
365 case PIPE_FORMAT_A8R8G8B8_UNORM:
366 case PIPE_FORMAT_B8G8R8A8_UNORM:
367 case PIPE_FORMAT_S8Z24_UNORM:
368 {
369 int numFaces = ct->base.target == PIPE_TEXTURE_CUBE ? 6 : 1;
370 int offset = surface->stride * texHeight * 4 * surface->face;
371 uint *dst;
372
373 if (!ct->untiled_data[level]) {
374 ct->untiled_data[level] =
375 align_malloc(surface->stride * texHeight * 4 * numFaces, 16);
376 }
377
378 dst = (uint *) ((ubyte *) ct->untiled_data[level] + offset);
379
380 untwiddle_image_uint(texWidth, texHeight, TILE_SIZE, dst,
381 surface->stride, src);
382 }
383 break;
384 default:
385 {
386 ct->untiled_data[level] = NULL;
387 printf("Cell: untwiddle unsupported texture format %s\n", pf_name(ct->base.format));
388 }
389 }
390
391 pipe_buffer_unmap(screen, surface->buffer);
392 }
393
394
395 static struct pipe_surface *
396 cell_get_tex_surface(struct pipe_screen *screen,
397 struct pipe_texture *pt,
398 unsigned face, unsigned level, unsigned zslice,
399 unsigned usage)
400 {
401 struct pipe_winsys *ws = screen->winsys;
402 struct cell_texture *ct = cell_texture(pt);
403 struct pipe_surface *ps;
404
405 ps = ws->surface_alloc(ws);
406 if (ps) {
407 assert(ps->refcount);
408 assert(ps->winsys);
409 winsys_buffer_reference(ws, &ps->buffer, ct->buffer);
410 ps->format = pt->format;
411 ps->block = pt->block;
412 ps->width = pt->width[level];
413 ps->height = pt->height[level];
414 ps->nblocksx = pt->nblocksx[level];
415 ps->nblocksy = pt->nblocksy[level];
416 ps->stride = ct->stride[level];
417 ps->offset = ct->level_offset[level];
418 ps->usage = usage;
419
420 /* XXX may need to override usage flags (see sp_texture.c) */
421
422 pipe_texture_reference(&ps->texture, pt);
423 ps->face = face;
424 ps->level = level;
425 ps->zslice = zslice;
426
427 if (pt->target == PIPE_TEXTURE_CUBE || pt->target == PIPE_TEXTURE_3D) {
428 ps->offset += ((pt->target == PIPE_TEXTURE_CUBE) ? face : zslice) *
429 ps->nblocksy *
430 ps->stride;
431 }
432 else {
433 assert(face == 0);
434 assert(zslice == 0);
435 }
436
437 if (ps->usage & PIPE_BUFFER_USAGE_CPU_READ) {
438 /* convert from tiled to linear layout */
439 cell_untwiddle_texture(screen, ps);
440 }
441 }
442 return ps;
443 }
444
445
446 static void
447 cell_tex_surface_release(struct pipe_screen *screen,
448 struct pipe_surface **s)
449 {
450 struct cell_texture *ct = cell_texture((*s)->texture);
451 const uint level = (*s)->level;
452
453 if (((*s)->usage & PIPE_BUFFER_USAGE_CPU_READ) && (ct->untiled_data[level]))
454 {
455 align_free(ct->untiled_data[level]);
456 ct->untiled_data[level] = NULL;
457 }
458
459 /* XXX if done rendering to teximage, re-tile */
460
461 pipe_texture_reference(&(*s)->texture, NULL);
462
463 screen->winsys->surface_release(screen->winsys, s);
464 }
465
466
467 static void *
468 cell_surface_map(struct pipe_screen *screen,
469 struct pipe_surface *surface,
470 unsigned flags)
471 {
472 ubyte *map;
473 struct cell_texture *ct = cell_texture(surface->texture);
474 const uint level = surface->level;
475
476 assert(ct);
477
478 if (flags & ~surface->usage) {
479 assert(0);
480 return NULL;
481 }
482
483 map = pipe_buffer_map( screen, surface->buffer, flags );
484 if (map == NULL)
485 return NULL;
486 else
487 {
488 if ((surface->usage & PIPE_BUFFER_USAGE_CPU_READ) && (ct->untiled_data[level])) {
489 return (void *) ((ubyte *) ct->untiled_data[level] + surface->offset);
490 }
491 else {
492 return (void *) (map + surface->offset);
493 }
494 }
495 }
496
497
498 static void
499 cell_surface_unmap(struct pipe_screen *screen,
500 struct pipe_surface *surface)
501 {
502 struct cell_texture *ct = cell_texture(surface->texture);
503
504 assert(ct);
505
506 if ((ct->base.tex_usage & PIPE_TEXTURE_USAGE_SAMPLER) &&
507 (surface->usage & PIPE_BUFFER_USAGE_CPU_WRITE)) {
508 /* convert from linear to tiled layout */
509 cell_twiddle_texture(screen, surface);
510 }
511
512 pipe_buffer_unmap( screen, surface->buffer );
513 }
514
515
516
517 void
518 cell_init_screen_texture_funcs(struct pipe_screen *screen)
519 {
520 screen->texture_create = cell_texture_create;
521 screen->texture_release = cell_texture_release;
522
523 screen->get_tex_surface = cell_get_tex_surface;
524 screen->tex_surface_release = cell_tex_surface_release;
525
526 screen->surface_map = cell_surface_map;
527 screen->surface_unmap = cell_surface_unmap;
528 }