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