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 {
318 int numFaces = ct->base.target == PIPE_TEXTURE_CUBE ? 6 : 1;
319 int offset = bufWidth * bufHeight * 4 * surface->face;
320 uint *dst;
321
322 if (!ct->tiled_buffer[level]) {
323 /* allocate buffer for tiled data now */
324 struct pipe_winsys *ws = screen->winsys;
325 uint bytes = bufWidth * bufHeight * 4 * numFaces;
326 ct->tiled_buffer[level] = ws->buffer_create(ws, 16,
327 PIPE_BUFFER_USAGE_PIXEL,
328 bytes);
329 /* and map it */
330 ct->tiled_mapped[level] = ws->buffer_map(ws, ct->tiled_buffer[level],
331 PIPE_BUFFER_USAGE_GPU_READ);
332 }
333 dst = (uint *) ((ubyte *) ct->tiled_mapped[level] + offset);
334
335 twiddle_image_uint(texWidth, texHeight, TILE_SIZE, dst,
336 surface->stride, src);
337 }
338 break;
339 default:
340 printf("Cell: twiddle unsupported texture format\n");
341 ;
342 }
343
344 pipe_buffer_unmap(screen, surface->buffer);
345 }
346
347
348 /**
349 * Convert SPU tiled texture image data to linear format for app usage.
350 */
351 static void
352 cell_untwiddle_texture(struct pipe_screen *screen,
353 struct pipe_surface *surface)
354 {
355 struct cell_texture *ct = cell_texture(surface->texture);
356 const uint level = surface->level;
357 const uint texWidth = ct->base.width[level];
358 const uint texHeight = ct->base.height[level];
359 const void *map = pipe_buffer_map(screen, surface->buffer,
360 PIPE_BUFFER_USAGE_CPU_READ);
361 const uint *src = (const uint *) ((const ubyte *) map + surface->offset);
362
363 switch (ct->base.format) {
364 case PIPE_FORMAT_A8R8G8B8_UNORM:
365 case PIPE_FORMAT_B8G8R8A8_UNORM:
366 {
367 int numFaces = ct->base.target == PIPE_TEXTURE_CUBE ? 6 : 1;
368 int offset = surface->stride * texHeight * 4 * surface->face;
369 uint *dst;
370
371 if (!ct->untiled_data[level]) {
372 ct->untiled_data[level] =
373 align_malloc(surface->stride * texHeight * 4 * numFaces, 16);
374 }
375
376 dst = (uint *) ((ubyte *) ct->untiled_data[level] + offset);
377
378 untwiddle_image_uint(texWidth, texHeight, TILE_SIZE, dst,
379 surface->stride, src);
380 }
381 break;
382 default:
383 {
384 ct->untiled_data[level] = NULL;
385 printf("Cell: untwiddle unsupported texture format\n");
386 }
387 }
388
389 pipe_buffer_unmap(screen, surface->buffer);
390 }
391
392
393 static struct pipe_surface *
394 cell_get_tex_surface(struct pipe_screen *screen,
395 struct pipe_texture *pt,
396 unsigned face, unsigned level, unsigned zslice,
397 unsigned usage)
398 {
399 struct pipe_winsys *ws = screen->winsys;
400 struct cell_texture *ct = cell_texture(pt);
401 struct pipe_surface *ps;
402
403 ps = ws->surface_alloc(ws);
404 if (ps) {
405 assert(ps->refcount);
406 assert(ps->winsys);
407 winsys_buffer_reference(ws, &ps->buffer, ct->buffer);
408 ps->format = pt->format;
409 ps->block = pt->block;
410 ps->width = pt->width[level];
411 ps->height = pt->height[level];
412 ps->nblocksx = pt->nblocksx[level];
413 ps->nblocksy = pt->nblocksy[level];
414 ps->stride = ct->stride[level];
415 ps->offset = ct->level_offset[level];
416 ps->usage = usage;
417
418 /* XXX may need to override usage flags (see sp_texture.c) */
419
420 pipe_texture_reference(&ps->texture, pt);
421 ps->face = face;
422 ps->level = level;
423 ps->zslice = zslice;
424
425 if (pt->target == PIPE_TEXTURE_CUBE || pt->target == PIPE_TEXTURE_3D) {
426 ps->offset += ((pt->target == PIPE_TEXTURE_CUBE) ? face : zslice) *
427 ps->nblocksy *
428 ps->stride;
429 }
430 else {
431 assert(face == 0);
432 assert(zslice == 0);
433 }
434
435 if (ps->usage & PIPE_BUFFER_USAGE_CPU_READ) {
436 /* convert from tiled to linear layout */
437 cell_untwiddle_texture(screen, ps);
438 }
439 }
440 return ps;
441 }
442
443
444 static void
445 cell_tex_surface_release(struct pipe_screen *screen,
446 struct pipe_surface **s)
447 {
448 struct cell_texture *ct = cell_texture((*s)->texture);
449 const uint level = (*s)->level;
450
451 if (((*s)->usage & PIPE_BUFFER_USAGE_CPU_READ) && (ct->untiled_data[level]))
452 {
453 align_free(ct->untiled_data[level]);
454 ct->untiled_data[level] = NULL;
455 }
456
457 /* XXX if done rendering to teximage, re-tile */
458
459 pipe_texture_reference(&(*s)->texture, NULL);
460
461 screen->winsys->surface_release(screen->winsys, s);
462 }
463
464
465 static void *
466 cell_surface_map(struct pipe_screen *screen,
467 struct pipe_surface *surface,
468 unsigned flags)
469 {
470 ubyte *map;
471 struct cell_texture *ct = cell_texture(surface->texture);
472 const uint level = surface->level;
473
474 assert(ct);
475
476 if (flags & ~surface->usage) {
477 assert(0);
478 return NULL;
479 }
480
481 map = pipe_buffer_map( screen, surface->buffer, flags );
482 if (map == NULL)
483 return NULL;
484 else
485 {
486 if ((surface->usage & PIPE_BUFFER_USAGE_CPU_READ) && (ct->untiled_data[level])) {
487 return (void *) ((ubyte *) ct->untiled_data[level] + surface->offset);
488 }
489 else {
490 return (void *) (map + surface->offset);
491 }
492 }
493 }
494
495
496 static void
497 cell_surface_unmap(struct pipe_screen *screen,
498 struct pipe_surface *surface)
499 {
500 struct cell_texture *ct = cell_texture(surface->texture);
501
502 assert(ct);
503
504 if ((ct->base.tex_usage & PIPE_TEXTURE_USAGE_SAMPLER) &&
505 (surface->usage & PIPE_BUFFER_USAGE_CPU_WRITE)) {
506 /* convert from linear to tiled layout */
507 cell_twiddle_texture(screen, surface);
508 }
509
510 pipe_buffer_unmap( screen, surface->buffer );
511 }
512
513
514
515 void
516 cell_init_screen_texture_funcs(struct pipe_screen *screen)
517 {
518 screen->texture_create = cell_texture_create;
519 screen->texture_release = cell_texture_release;
520
521 screen->get_tex_surface = cell_get_tex_surface;
522 screen->tex_surface_release = cell_tex_surface_release;
523
524 screen->surface_map = cell_surface_map;
525 screen->surface_unmap = cell_surface_unmap;
526 }