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