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