cell: attempt conversion to sw_winsys
[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 "util/u_inlines.h"
37
38 #include "util/u_format.h"
39 #include "util/u_math.h"
40 #include "util/u_memory.h"
41
42 #include "cell_context.h"
43 #include "cell_state.h"
44 #include "cell_texture.h"
45
46 #include "state_tracker/sw_winsys.h"
47
48
49
50 static boolean
51 cell_texture_layout(struct pipe_screen *screen,
52 struct cell_texture *ct)
53 {
54 struct pipe_texture *pt = &ct->base;
55 unsigned level;
56 unsigned width = pt->width0;
57 unsigned height = pt->height0;
58 unsigned depth = pt->depth0;
59
60 ct->buffer_size = 0;
61
62 for (level = 0; level <= pt->last_level; level++) {
63 unsigned size;
64 unsigned w_tile, h_tile;
65
66 assert(level < CELL_MAX_TEXTURE_LEVELS);
67
68 /* width, height, rounded up to tile size */
69 w_tile = align(width, TILE_SIZE);
70 h_tile = align(height, TILE_SIZE);
71
72 ct->stride[level] = util_format_get_stride(pt->format, w_tile);
73
74 ct->level_offset[level] = ct->buffer_size;
75
76 size = ct->stride[level] * util_format_get_nblocksy(pt->format, h_tile);
77 if (pt->target == PIPE_TEXTURE_CUBE)
78 size *= 6;
79 else
80 size *= depth;
81
82 ct->buffer_size += size;
83
84 width = u_minify(width, 1);
85 height = u_minify(height, 1);
86 depth = u_minify(depth, 1);
87 }
88
89 ct->data = align_malloc(ct->buffer_size, 16);
90
91 return ct->data != NULL;
92 }
93
94
95 /**
96 * Texture layout for simple color buffers.
97 */
98 static boolean
99 cell_displaytarget_layout(struct pipe_screen *screen,
100 struct cell_texture * ct)
101 {
102 struct sw_winsys *winsys = cell_screen(screen)->winsys;
103
104 /* Round up the surface size to a multiple of the tile size?
105 */
106 ct->dt = winsys->displaytarget_create(winsys,
107 ct->base.format,
108 ct->base.width0,
109 ct->base.height0,
110 16,
111 &ct->stride[0] );
112
113 return ct->dt != NULL;
114 }
115
116 static struct pipe_texture *
117 cell_texture_create(struct pipe_screen *screen,
118 const struct pipe_texture *templat)
119 {
120 struct cell_texture *ct = CALLOC_STRUCT(cell_texture);
121 if (!ct)
122 return NULL;
123
124 ct->base = *templat;
125 pipe_reference_init(&ct->base.reference, 1);
126 ct->base.screen = screen;
127
128 if (ct->base.tex_usage & (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
129 PIPE_TEXTURE_USAGE_SCANOUT |
130 PIPE_TEXTURE_USAGE_SHARED)) {
131 if (!cell_displaytarget_layout(screen, ct))
132 goto fail;
133 }
134 else {
135 if (!cell_texture_layout(screen, ct))
136 goto fail;
137 }
138
139 return &ct->base;
140
141 fail:
142 FREE(ct);
143 return NULL;
144 }
145
146
147 static void
148 cell_texture_destroy(struct pipe_texture *pt)
149 {
150 struct cell_screen *screen = cell_screen(pt->screen);
151 struct sw_winsys *winsys = screen->winsys;
152 struct cell_texture *ct = cell_texture(pt);
153
154 if (ct->mapped) {
155 if (ct->dt)
156 winsys->displaytarget_unmap(winsys, ct->dt);
157 ct->mapped = NULL;
158 }
159
160 if (ct->dt) {
161 /* display target */
162 winsys->displaytarget_destroy(winsys, ct->dt);
163 }
164 else {
165 /* regular texture */
166 align_free(ct->data);
167 }
168
169 FREE(ct);
170 }
171
172
173
174 /**
175 * Convert image from linear layout to tiled layout. 4-byte pixels.
176 */
177 static void
178 twiddle_image_uint(uint w, uint h, uint tile_size, uint *dst,
179 uint src_stride, const uint *src)
180 {
181 const uint tile_size2 = tile_size * tile_size;
182 const uint h_t = (h + tile_size - 1) / tile_size;
183 const uint w_t = (w + tile_size - 1) / tile_size;
184
185 uint it, jt; /* tile counters */
186 uint i, j; /* intra-tile counters */
187
188 src_stride /= 4; /* convert from bytes to pixels */
189
190 /* loop over dest tiles */
191 for (it = 0; it < h_t; it++) {
192 for (jt = 0; jt < w_t; jt++) {
193 /* start of dest tile: */
194 uint *tdst = dst + (it * w_t + jt) * tile_size2;
195
196 /* compute size of this tile (may be smaller than tile_size) */
197 /* XXX note: a compiler bug was found here. That's why the code
198 * looks as it does.
199 */
200 uint tile_width = w - jt * tile_size;
201 tile_width = MIN2(tile_width, tile_size);
202 uint tile_height = h - it * tile_size;
203 tile_height = MIN2(tile_height, tile_size);
204
205 /* loop over texels in the tile */
206 for (i = 0; i < tile_height; i++) {
207 for (j = 0; j < tile_width; j++) {
208 const uint srci = it * tile_size + i;
209 const uint srcj = jt * tile_size + j;
210 ASSERT(srci < h);
211 ASSERT(srcj < w);
212 tdst[i * tile_size + j] = src[srci * src_stride + srcj];
213 }
214 }
215 }
216 }
217 }
218
219
220 /**
221 * For Cell. Basically, rearrange the pixels/quads from this layout:
222 * +--+--+--+--+
223 * |p0|p1|p2|p3|....
224 * +--+--+--+--+
225 *
226 * to this layout:
227 * +--+--+
228 * |p0|p1|....
229 * +--+--+
230 * |p2|p3|
231 * +--+--+
232 */
233 static void
234 twiddle_tile(const uint *tileIn, uint *tileOut)
235 {
236 int y, x;
237
238 for (y = 0; y < TILE_SIZE; y+=2) {
239 for (x = 0; x < TILE_SIZE; x+=2) {
240 int k = 4 * (y/2 * TILE_SIZE/2 + x/2);
241 tileOut[y * TILE_SIZE + (x + 0)] = tileIn[k];
242 tileOut[y * TILE_SIZE + (x + 1)] = tileIn[k+1];
243 tileOut[(y + 1) * TILE_SIZE + (x + 0)] = tileIn[k+2];
244 tileOut[(y + 1) * TILE_SIZE + (x + 1)] = tileIn[k+3];
245 }
246 }
247 }
248
249
250 /**
251 * Convert image from tiled layout to linear layout. 4-byte pixels.
252 */
253 static void
254 untwiddle_image_uint(uint w, uint h, uint tile_size, uint *dst,
255 uint dst_stride, const uint *src)
256 {
257 const uint tile_size2 = tile_size * tile_size;
258 const uint h_t = (h + tile_size - 1) / tile_size;
259 const uint w_t = (w + tile_size - 1) / tile_size;
260 uint *tile_buf;
261 uint it, jt; /* tile counters */
262 uint i, j; /* intra-tile counters */
263
264 dst_stride /= 4; /* convert from bytes to pixels */
265
266 tile_buf = align_malloc(tile_size * tile_size * 4, 16);
267
268 /* loop over src tiles */
269 for (it = 0; it < h_t; it++) {
270 for (jt = 0; jt < w_t; jt++) {
271 /* start of src tile: */
272 const uint *tsrc = src + (it * w_t + jt) * tile_size2;
273
274 twiddle_tile(tsrc, tile_buf);
275 tsrc = tile_buf;
276
277 /* compute size of this tile (may be smaller than tile_size) */
278 /* XXX note: a compiler bug was found here. That's why the code
279 * looks as it does.
280 */
281 uint tile_width = w - jt * tile_size;
282 tile_width = MIN2(tile_width, tile_size);
283 uint tile_height = h - it * tile_size;
284 tile_height = MIN2(tile_height, tile_size);
285
286 /* loop over texels in the tile */
287 for (i = 0; i < tile_height; i++) {
288 for (j = 0; j < tile_width; j++) {
289 uint dsti = it * tile_size + i;
290 uint dstj = jt * tile_size + j;
291 ASSERT(dsti < h);
292 ASSERT(dstj < w);
293 dst[dsti * dst_stride + dstj] = tsrc[i * tile_size + j];
294 }
295 }
296 }
297 }
298
299 align_free(tile_buf);
300 }
301
302
303 static struct pipe_surface *
304 cell_get_tex_surface(struct pipe_screen *screen,
305 struct pipe_texture *pt,
306 unsigned face, unsigned level, unsigned zslice,
307 unsigned usage)
308 {
309 struct cell_texture *ct = cell_texture(pt);
310 struct pipe_surface *ps;
311
312 ps = CALLOC_STRUCT(pipe_surface);
313 if (ps) {
314 pipe_reference_init(&ps->reference, 1);
315 pipe_texture_reference(&ps->texture, pt);
316 ps->format = pt->format;
317 ps->width = u_minify(pt->width0, level);
318 ps->height = u_minify(pt->height0, level);
319 ps->offset = ct->level_offset[level];
320 /* XXX may need to override usage flags (see sp_texture.c) */
321 ps->usage = usage;
322 ps->face = face;
323 ps->level = level;
324 ps->zslice = zslice;
325
326 if (pt->target == PIPE_TEXTURE_CUBE) {
327 unsigned h_tile = align(ps->height, TILE_SIZE);
328 ps->offset += face * util_format_get_nblocksy(ps->format, h_tile) * ct->stride[level];
329 }
330 else if (pt->target == PIPE_TEXTURE_3D) {
331 unsigned h_tile = align(ps->height, TILE_SIZE);
332 ps->offset += zslice * util_format_get_nblocksy(ps->format, h_tile) * ct->stride[level];
333 }
334 else {
335 assert(face == 0);
336 assert(zslice == 0);
337 }
338 }
339 return ps;
340 }
341
342
343 static void
344 cell_tex_surface_destroy(struct pipe_surface *surf)
345 {
346 pipe_texture_reference(&surf->texture, NULL);
347 FREE(surf);
348 }
349
350
351 /**
352 * Create new pipe_transfer object.
353 * This is used by the user to put tex data into a texture (and get it
354 * back out for glGetTexImage).
355 */
356 static struct pipe_transfer *
357 cell_get_tex_transfer(struct pipe_screen *screen,
358 struct pipe_texture *texture,
359 unsigned face, unsigned level, unsigned zslice,
360 enum pipe_transfer_usage usage,
361 unsigned x, unsigned y, unsigned w, unsigned h)
362 {
363 struct cell_texture *ct = cell_texture(texture);
364 struct cell_transfer *ctrans;
365
366 assert(texture);
367 assert(level <= texture->last_level);
368
369 ctrans = CALLOC_STRUCT(cell_transfer);
370 if (ctrans) {
371 struct pipe_transfer *pt = &ctrans->base;
372 pipe_texture_reference(&pt->texture, texture);
373 pt->x = x;
374 pt->y = y;
375 pt->width = w;
376 pt->height = h;
377 pt->stride = ct->stride[level];
378 pt->usage = usage;
379 pt->face = face;
380 pt->level = level;
381 pt->zslice = zslice;
382
383 ctrans->offset = ct->level_offset[level];
384
385 if (texture->target == PIPE_TEXTURE_CUBE) {
386 unsigned h_tile = align(u_minify(texture->height0, level), TILE_SIZE);
387 ctrans->offset += face * util_format_get_nblocksy(texture->format, h_tile) * pt->stride;
388 }
389 else if (texture->target == PIPE_TEXTURE_3D) {
390 unsigned h_tile = align(u_minify(texture->height0, level), TILE_SIZE);
391 ctrans->offset += zslice * util_format_get_nblocksy(texture->format, h_tile) * pt->stride;
392 }
393 else {
394 assert(face == 0);
395 assert(zslice == 0);
396 }
397 return pt;
398 }
399 return NULL;
400 }
401
402
403 static void
404 cell_tex_transfer_destroy(struct pipe_transfer *t)
405 {
406 struct cell_transfer *transfer = cell_transfer(t);
407 /* Effectively do the texture_update work here - if texture images
408 * needed post-processing to put them into hardware layout, this is
409 * where it would happen. For cell, nothing to do.
410 */
411 assert (transfer->base.texture);
412 pipe_texture_reference(&transfer->base.texture, NULL);
413 FREE(transfer);
414 }
415
416
417 /**
418 * Return pointer to texture image data in linear layout.
419 */
420 static void *
421 cell_transfer_map(struct pipe_screen *screen, struct pipe_transfer *transfer)
422 {
423 struct cell_transfer *ctrans = cell_transfer(transfer);
424 struct pipe_texture *pt = transfer->texture;
425 struct cell_texture *ct = cell_texture(pt);
426 const uint level = ctrans->base.level;
427 const uint texWidth = u_minify(pt->width0, level);
428 const uint texHeight = u_minify(pt->height0, level);
429 const uint stride = ct->stride[level];
430 unsigned size;
431
432 assert(transfer->texture);
433
434 if (ct->mapped == NULL) {
435 if (ct->dt) {
436 struct sw_winsys *winsys = cell_screen(screen)->winsys;
437 ct->mapped = winsys->displaytarget_map(screen, ct->dt,
438 pipe_transfer_buffer_flags(transfer));
439 }
440 else {
441 ct->mapped = ct->data;
442 }
443 }
444
445 if (ct->mapped == NULL)
446 return NULL;
447
448 /*
449 * Create a buffer of ordinary memory for the linear texture.
450 * This is the memory that the user will read/write.
451 */
452 size = util_format_get_stride(pt->format, align(texWidth, TILE_SIZE)) *
453 util_format_get_nblocksy(pt->format, align(texHeight, TILE_SIZE));
454
455 ctrans->map = align_malloc(size, 16);
456 if (!ctrans->map)
457 return NULL; /* out of memory */
458
459 if (transfer->usage & PIPE_TRANSFER_READ) {
460 /* need to untwiddle the texture to make a linear version */
461 const uint bpp = util_format_get_blocksize(ct->base.format);
462 if (bpp == 4) {
463 const uint *src = (uint *) (ct->mapped + ctrans->offset);
464 uint *dst = ctrans->map;
465 untwiddle_image_uint(texWidth, texHeight, TILE_SIZE,
466 dst, stride, src);
467 }
468 else {
469 // xxx fix
470 }
471 }
472
473 return ctrans->map;
474 }
475
476
477 /**
478 * Called when user is done reading/writing texture data.
479 * If new data was written, this is where we convert the linear data
480 * to tiled data.
481 */
482 static void
483 cell_transfer_unmap(struct pipe_screen *screen,
484 struct pipe_transfer *transfer)
485 {
486 struct cell_transfer *ctrans = cell_transfer(transfer);
487 struct pipe_texture *pt = transfer->texture;
488 struct cell_texture *ct = cell_texture(pt);
489 const uint level = ctrans->base.level;
490 const uint texWidth = u_minify(pt->width0, level);
491 const uint texHeight = u_minify(pt->height0, level);
492 const uint stride = ct->stride[level];
493
494 if (!ct->mapped) {
495 assert(0);
496 return;
497 }
498
499 if (transfer->usage & PIPE_TRANSFER_WRITE) {
500 /* The user wrote new texture data into the mapped buffer.
501 * We need to convert the new linear data into the twiddled/tiled format.
502 */
503 const uint bpp = util_format_get_blocksize(ct->base.format);
504 if (bpp == 4) {
505 const uint *src = ctrans->map;
506 uint *dst = (uint *) (ct->mapped + ctrans->offset);
507 twiddle_image_uint(texWidth, texHeight, TILE_SIZE, dst, stride, src);
508 }
509 else {
510 // xxx fix
511 }
512 }
513
514 if (ct->dt) {
515 /* display target */
516 struct sw_winsys *winsys = cell_screen(screen)->winsys;
517 winsys->displaytarget_unmap(winsys, ct->dt);
518 }
519
520 align_free(ctrans->map);
521 ctrans->map = NULL;
522 }
523
524
525
526
527
528 void
529 cell_init_screen_texture_funcs(struct pipe_screen *screen)
530 {
531 screen->texture_create = cell_texture_create;
532 screen->texture_destroy = cell_texture_destroy;
533
534 screen->get_tex_surface = cell_get_tex_surface;
535 screen->tex_surface_destroy = cell_tex_surface_destroy;
536
537 screen->get_tex_transfer = cell_get_tex_transfer;
538 screen->tex_transfer_destroy = cell_tex_transfer_destroy;
539
540 screen->transfer_map = cell_transfer_map;
541 screen->transfer_unmap = cell_transfer_unmap;
542 }