1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Michel Dänzer <michel@tungstengraphics.com>
34 #include "pipe/p_context.h"
35 #include "pipe/p_defines.h"
36 #include "pipe/p_inlines.h"
37 #include "pipe/p_winsys.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
41 #include "cell_context.h"
42 #include "cell_state.h"
43 #include "cell_texture.h"
55 cell_texture_layout(struct cell_texture
*ct
)
57 struct pipe_texture
*pt
= &ct
->base
;
59 unsigned width
= pt
->width
[0];
60 unsigned height
= pt
->height
[0];
61 unsigned depth
= pt
->depth
[0];
65 for ( level
= 0 ; level
<= pt
->last_level
; level
++ ) {
67 unsigned w_tile
, h_tile
;
69 assert(level
< CELL_MAX_TEXTURE_LEVELS
);
71 /* width, height, rounded up to tile size */
72 w_tile
= align(width
, TILE_SIZE
);
73 h_tile
= align(height
, TILE_SIZE
);
75 pt
->width
[level
] = width
;
76 pt
->height
[level
] = height
;
77 pt
->depth
[level
] = depth
;
78 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, w_tile
);
79 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, h_tile
);
81 ct
->stride
[level
] = pt
->nblocksx
[level
] * pt
->block
.size
;
83 ct
->level_offset
[level
] = ct
->buffer_size
;
85 size
= pt
->nblocksx
[level
] * pt
->nblocksy
[level
] * pt
->block
.size
;
86 if (pt
->target
== PIPE_TEXTURE_CUBE
)
91 ct
->buffer_size
+= size
;
93 width
= minify(width
);
94 height
= minify(height
);
95 depth
= minify(depth
);
100 static struct pipe_texture
*
101 cell_texture_create(struct pipe_screen
*screen
,
102 const struct pipe_texture
*templat
)
104 struct pipe_winsys
*ws
= screen
->winsys
;
105 struct cell_texture
*ct
= CALLOC_STRUCT(cell_texture
);
110 ct
->base
.refcount
= 1;
111 ct
->base
.screen
= screen
;
113 cell_texture_layout(ct
);
115 ct
->buffer
= ws
->buffer_create(ws
, 32, PIPE_BUFFER_USAGE_PIXEL
,
128 cell_texture_release(struct pipe_screen
*screen
,
129 struct pipe_texture
**pt
)
135 DBG("%s %p refcount will be %d\n",
136 __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
138 if (--(*pt
)->refcount
<= 0) {
139 /* Delete this texture now.
140 * But note that the underlying pipe_buffer may linger...
142 struct cell_texture
*ct
= cell_texture(*pt
);
146 DBG("%s deleting %p\n", __FUNCTION__, (void *) ct);
149 pipe_buffer_reference(screen
, &ct
->buffer
, NULL
);
151 for (i
= 0; i
< CELL_MAX_TEXTURE_LEVELS
; i
++) {
152 /* Unreference the tiled image buffer.
153 * It may not actually be deleted until a fence is hit.
155 if (ct
->tiled_buffer
[i
]) {
156 ct
->tiled_mapped
[i
] = NULL
;
157 winsys_buffer_reference(screen
->winsys
, &ct
->tiled_buffer
[i
], NULL
);
169 * Convert image from linear layout to tiled layout. 4-byte pixels.
172 twiddle_image_uint(uint w
, uint h
, uint tile_size
, uint
*dst
,
173 uint src_stride
, const uint
*src
)
175 const uint tile_size2
= tile_size
* tile_size
;
176 const uint h_t
= (h
+ tile_size
- 1) / tile_size
;
177 const uint w_t
= (w
+ tile_size
- 1) / tile_size
;
179 uint it
, jt
; /* tile counters */
180 uint i
, j
; /* intra-tile counters */
182 src_stride
/= 4; /* convert from bytes to pixels */
184 /* loop over dest tiles */
185 for (it
= 0; it
< h_t
; it
++) {
186 for (jt
= 0; jt
< w_t
; jt
++) {
187 /* start of dest tile: */
188 uint
*tdst
= dst
+ (it
* w_t
+ jt
) * tile_size2
;
190 /* compute size of this tile (may be smaller than tile_size) */
191 /* XXX note: a compiler bug was found here. That's why the code
194 uint tile_width
= w
- jt
* tile_size
;
195 tile_width
= MIN2(tile_width
, tile_size
);
196 uint tile_height
= h
- it
* tile_size
;
197 tile_height
= MIN2(tile_height
, tile_size
);
199 /* loop over texels in the tile */
200 for (i
= 0; i
< tile_height
; i
++) {
201 for (j
= 0; j
< tile_width
; j
++) {
202 const uint srci
= it
* tile_size
+ i
;
203 const uint srcj
= jt
* tile_size
+ j
;
206 tdst
[i
* tile_size
+ j
] = src
[srci
* src_stride
+ srcj
];
215 * For Cell. Basically, rearrange the pixels/quads from this layout:
228 twiddle_tile(const uint
*tileIn
, uint
*tileOut
)
232 for (y
= 0; y
< TILE_SIZE
; y
+=2) {
233 for (x
= 0; x
< TILE_SIZE
; x
+=2) {
234 int k
= 4 * (y
/2 * TILE_SIZE
/2 + x
/2);
235 tileOut
[y
* TILE_SIZE
+ (x
+ 0)] = tileIn
[k
];
236 tileOut
[y
* TILE_SIZE
+ (x
+ 1)] = tileIn
[k
+1];
237 tileOut
[(y
+ 1) * TILE_SIZE
+ (x
+ 0)] = tileIn
[k
+2];
238 tileOut
[(y
+ 1) * TILE_SIZE
+ (x
+ 1)] = tileIn
[k
+3];
245 * Convert image from tiled layout to linear layout. 4-byte pixels.
248 untwiddle_image_uint(uint w
, uint h
, uint tile_size
, uint
*dst
,
249 uint dst_stride
, const uint
*src
)
251 const uint tile_size2
= tile_size
* tile_size
;
252 const uint h_t
= (h
+ tile_size
- 1) / tile_size
;
253 const uint w_t
= (w
+ tile_size
- 1) / tile_size
;
255 uint it
, jt
; /* tile counters */
256 uint i
, j
; /* intra-tile counters */
258 dst_stride
/= 4; /* convert from bytes to pixels */
260 tile_buf
= align_malloc(tile_size
* tile_size
* 4, 16);
262 /* loop over src tiles */
263 for (it
= 0; it
< h_t
; it
++) {
264 for (jt
= 0; jt
< w_t
; jt
++) {
265 /* start of src tile: */
266 const uint
*tsrc
= src
+ (it
* w_t
+ jt
) * tile_size2
;
268 twiddle_tile(tsrc
, tile_buf
);
271 /* compute size of this tile (may be smaller than tile_size) */
272 /* XXX note: a compiler bug was found here. That's why the code
275 uint tile_width
= w
- jt
* tile_size
;
276 tile_width
= MIN2(tile_width
, tile_size
);
277 uint tile_height
= h
- it
* tile_size
;
278 tile_height
= MIN2(tile_height
, tile_size
);
280 /* loop over texels in the tile */
281 for (i
= 0; i
< tile_height
; i
++) {
282 for (j
= 0; j
< tile_width
; j
++) {
283 uint dsti
= it
* tile_size
+ i
;
284 uint dstj
= jt
* tile_size
+ j
;
287 dst
[dsti
* dst_stride
+ dstj
] = tsrc
[i
* tile_size
+ j
];
293 align_free(tile_buf
);
298 * Convert linear texture image data to tiled format for SPU usage.
301 cell_twiddle_texture(struct pipe_screen
*screen
,
302 struct pipe_surface
*surface
)
304 struct cell_texture
*ct
= cell_texture(surface
->texture
);
305 const uint level
= surface
->level
;
306 const uint texWidth
= ct
->base
.width
[level
];
307 const uint texHeight
= ct
->base
.height
[level
];
308 const uint bufWidth
= align(texWidth
, TILE_SIZE
);
309 const uint bufHeight
= align(texHeight
, TILE_SIZE
);
310 const void *map
= pipe_buffer_map(screen
, surface
->buffer
,
311 PIPE_BUFFER_USAGE_CPU_READ
);
312 const uint
*src
= (const uint
*) ((const ubyte
*) map
+ surface
->offset
);
314 switch (ct
->base
.format
) {
315 case PIPE_FORMAT_A8R8G8B8_UNORM
:
316 case PIPE_FORMAT_B8G8R8A8_UNORM
:
317 case PIPE_FORMAT_S8Z24_UNORM
:
319 int numFaces
= ct
->base
.target
== PIPE_TEXTURE_CUBE
? 6 : 1;
320 int offset
= bufWidth
* bufHeight
* 4 * surface
->face
;
323 if (!ct
->tiled_buffer
[level
]) {
324 /* allocate buffer for tiled data now */
325 struct pipe_winsys
*ws
= screen
->winsys
;
326 uint bytes
= bufWidth
* bufHeight
* 4 * numFaces
;
327 ct
->tiled_buffer
[level
] = ws
->buffer_create(ws
, 16,
328 PIPE_BUFFER_USAGE_PIXEL
,
331 ct
->tiled_mapped
[level
] = ws
->buffer_map(ws
, ct
->tiled_buffer
[level
],
332 PIPE_BUFFER_USAGE_GPU_READ
);
334 dst
= (uint
*) ((ubyte
*) ct
->tiled_mapped
[level
] + offset
);
336 twiddle_image_uint(texWidth
, texHeight
, TILE_SIZE
, dst
,
337 surface
->stride
, src
);
341 printf("Cell: twiddle unsupported texture format 0x%x\n", ct
->base
.format
);
345 pipe_buffer_unmap(screen
, surface
->buffer
);
350 * Convert SPU tiled texture image data to linear format for app usage.
353 cell_untwiddle_texture(struct pipe_screen
*screen
,
354 struct pipe_surface
*surface
)
356 struct cell_texture
*ct
= cell_texture(surface
->texture
);
357 const uint level
= surface
->level
;
358 const uint texWidth
= ct
->base
.width
[level
];
359 const uint texHeight
= ct
->base
.height
[level
];
360 const void *map
= pipe_buffer_map(screen
, surface
->buffer
,
361 PIPE_BUFFER_USAGE_CPU_READ
);
362 const uint
*src
= (const uint
*) ((const ubyte
*) map
+ surface
->offset
);
364 switch (ct
->base
.format
) {
365 case PIPE_FORMAT_A8R8G8B8_UNORM
:
366 case PIPE_FORMAT_B8G8R8A8_UNORM
:
367 case PIPE_FORMAT_S8Z24_UNORM
:
369 int numFaces
= ct
->base
.target
== PIPE_TEXTURE_CUBE
? 6 : 1;
370 int offset
= surface
->stride
* texHeight
* 4 * surface
->face
;
373 if (!ct
->untiled_data
[level
]) {
374 ct
->untiled_data
[level
] =
375 align_malloc(surface
->stride
* texHeight
* 4 * numFaces
, 16);
378 dst
= (uint
*) ((ubyte
*) ct
->untiled_data
[level
] + offset
);
380 untwiddle_image_uint(texWidth
, texHeight
, TILE_SIZE
, dst
,
381 surface
->stride
, src
);
386 ct
->untiled_data
[level
] = NULL
;
387 printf("Cell: untwiddle unsupported texture format 0x%x\n", ct
->base
.format
);
391 pipe_buffer_unmap(screen
, surface
->buffer
);
395 static struct pipe_surface
*
396 cell_get_tex_surface(struct pipe_screen
*screen
,
397 struct pipe_texture
*pt
,
398 unsigned face
, unsigned level
, unsigned zslice
,
401 struct pipe_winsys
*ws
= screen
->winsys
;
402 struct cell_texture
*ct
= cell_texture(pt
);
403 struct pipe_surface
*ps
;
405 ps
= ws
->surface_alloc(ws
);
407 assert(ps
->refcount
);
409 winsys_buffer_reference(ws
, &ps
->buffer
, ct
->buffer
);
410 ps
->format
= pt
->format
;
411 ps
->block
= pt
->block
;
412 ps
->width
= pt
->width
[level
];
413 ps
->height
= pt
->height
[level
];
414 ps
->nblocksx
= pt
->nblocksx
[level
];
415 ps
->nblocksy
= pt
->nblocksy
[level
];
416 ps
->stride
= ct
->stride
[level
];
417 ps
->offset
= ct
->level_offset
[level
];
420 /* XXX may need to override usage flags (see sp_texture.c) */
422 pipe_texture_reference(&ps
->texture
, pt
);
427 if (pt
->target
== PIPE_TEXTURE_CUBE
|| pt
->target
== PIPE_TEXTURE_3D
) {
428 ps
->offset
+= ((pt
->target
== PIPE_TEXTURE_CUBE
) ? face
: zslice
) *
437 if (ps
->usage
& PIPE_BUFFER_USAGE_CPU_READ
) {
438 /* convert from tiled to linear layout */
439 cell_untwiddle_texture(screen
, ps
);
447 cell_tex_surface_release(struct pipe_screen
*screen
,
448 struct pipe_surface
**s
)
450 struct cell_texture
*ct
= cell_texture((*s
)->texture
);
451 const uint level
= (*s
)->level
;
453 if (((*s
)->usage
& PIPE_BUFFER_USAGE_CPU_READ
) && (ct
->untiled_data
[level
]))
455 align_free(ct
->untiled_data
[level
]);
456 ct
->untiled_data
[level
] = NULL
;
459 /* XXX if done rendering to teximage, re-tile */
461 pipe_texture_reference(&(*s
)->texture
, NULL
);
463 screen
->winsys
->surface_release(screen
->winsys
, s
);
468 cell_surface_map(struct pipe_screen
*screen
,
469 struct pipe_surface
*surface
,
473 struct cell_texture
*ct
= cell_texture(surface
->texture
);
474 const uint level
= surface
->level
;
478 if (flags
& ~surface
->usage
) {
483 map
= pipe_buffer_map( screen
, surface
->buffer
, flags
);
488 if ((surface
->usage
& PIPE_BUFFER_USAGE_CPU_READ
) && (ct
->untiled_data
[level
])) {
489 return (void *) ((ubyte
*) ct
->untiled_data
[level
] + surface
->offset
);
492 return (void *) (map
+ surface
->offset
);
499 cell_surface_unmap(struct pipe_screen
*screen
,
500 struct pipe_surface
*surface
)
502 struct cell_texture
*ct
= cell_texture(surface
->texture
);
506 if ((ct
->base
.tex_usage
& PIPE_TEXTURE_USAGE_SAMPLER
) &&
507 (surface
->usage
& PIPE_BUFFER_USAGE_CPU_WRITE
)) {
508 /* convert from linear to tiled layout */
509 cell_twiddle_texture(screen
, surface
);
512 pipe_buffer_unmap( screen
, surface
->buffer
);
518 cell_init_screen_texture_funcs(struct pipe_screen
*screen
)
520 screen
->texture_create
= cell_texture_create
;
521 screen
->texture_release
= cell_texture_release
;
523 screen
->get_tex_surface
= cell_get_tex_surface
;
524 screen
->tex_surface_release
= cell_tex_surface_release
;
526 screen
->surface_map
= cell_surface_map
;
527 screen
->surface_unmap
= cell_surface_unmap
;