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