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/internal/p_winsys_screen.h"
39 #include "util/u_format.h"
40 #include "util/u_math.h"
41 #include "util/u_memory.h"
43 #include "cell_context.h"
44 #include "cell_state.h"
45 #include "cell_texture.h"
50 cell_texture_layout(struct cell_texture
*ct
)
52 struct pipe_texture
*pt
= &ct
->base
;
54 unsigned width
= pt
->width0
;
55 unsigned height
= pt
->height0
;
56 unsigned depth
= pt
->depth0
;
60 for (level
= 0; level
<= pt
->last_level
; level
++) {
62 unsigned w_tile
, h_tile
;
64 assert(level
< CELL_MAX_TEXTURE_LEVELS
);
66 /* width, height, rounded up to tile size */
67 w_tile
= align(width
, TILE_SIZE
);
68 h_tile
= align(height
, TILE_SIZE
);
70 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, w_tile
);
71 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, h_tile
);
73 ct
->stride
[level
] = pt
->nblocksx
[level
] * pt
->block
.size
;
75 ct
->level_offset
[level
] = ct
->buffer_size
;
77 size
= pt
->nblocksx
[level
] * pt
->nblocksy
[level
] * pt
->block
.size
;
78 if (pt
->target
== PIPE_TEXTURE_CUBE
)
83 ct
->buffer_size
+= size
;
85 width
= u_minify(width
, 1);
86 height
= u_minify(height
, 1);
87 depth
= u_minify(depth
, 1);
92 static struct pipe_texture
*
93 cell_texture_create(struct pipe_screen
*screen
,
94 const struct pipe_texture
*templat
)
96 struct cell_texture
*ct
= CALLOC_STRUCT(cell_texture
);
101 pipe_reference_init(&ct
->base
.reference
, 1);
102 ct
->base
.screen
= screen
;
104 cell_texture_layout(ct
);
106 ct
->buffer
= screen
->buffer_create(screen
, 32, PIPE_BUFFER_USAGE_PIXEL
,
119 cell_texture_destroy(struct pipe_texture
*pt
)
121 struct cell_texture
*ct
= cell_texture(pt
);
124 pipe_buffer_unmap(ct
->buffer
->screen
, ct
->buffer
);
128 pipe_buffer_reference(&ct
->buffer
, NULL
);
136 * Convert image from linear layout to tiled layout. 4-byte pixels.
139 twiddle_image_uint(uint w
, uint h
, uint tile_size
, uint
*dst
,
140 uint src_stride
, const uint
*src
)
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
;
146 uint it
, jt
; /* tile counters */
147 uint i
, j
; /* intra-tile counters */
149 src_stride
/= 4; /* convert from bytes to pixels */
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
;
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
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
);
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
;
173 tdst
[i
* tile_size
+ j
] = src
[srci
* src_stride
+ srcj
];
182 * For Cell. Basically, rearrange the pixels/quads from this layout:
195 twiddle_tile(const uint
*tileIn
, uint
*tileOut
)
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];
212 * Convert image from tiled layout to linear layout. 4-byte pixels.
215 untwiddle_image_uint(uint w
, uint h
, uint tile_size
, uint
*dst
,
216 uint dst_stride
, const uint
*src
)
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
;
222 uint it
, jt
; /* tile counters */
223 uint i
, j
; /* intra-tile counters */
225 dst_stride
/= 4; /* convert from bytes to pixels */
227 tile_buf
= align_malloc(tile_size
* tile_size
* 4, 16);
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
;
235 twiddle_tile(tsrc
, tile_buf
);
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
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
);
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
;
254 dst
[dsti
* dst_stride
+ dstj
] = tsrc
[i
* tile_size
+ j
];
260 align_free(tile_buf
);
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
,
270 struct cell_texture
*ct
= cell_texture(pt
);
271 struct pipe_surface
*ps
;
273 ps
= CALLOC_STRUCT(pipe_surface
);
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) */
287 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
288 ps
->offset
+= face
* pt
->nblocksy
[level
] * ct
->stride
[level
];
290 else if (pt
->target
== PIPE_TEXTURE_3D
) {
291 ps
->offset
+= zslice
* pt
->nblocksy
[level
] * ct
->stride
[level
];
303 cell_tex_surface_destroy(struct pipe_surface
*surf
)
305 pipe_texture_reference(&surf
->texture
, NULL
);
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).
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
)
322 struct cell_texture
*ct
= cell_texture(texture
);
323 struct cell_transfer
*ctrans
;
326 assert(level
<= texture
->last_level
);
328 ctrans
= CALLOC_STRUCT(cell_transfer
);
330 struct pipe_transfer
*pt
= &ctrans
->base
;
331 pipe_texture_reference(&pt
->texture
, texture
);
332 pt
->format
= texture
->format
;
333 pt
->block
= texture
->block
;
338 pt
->nblocksx
= texture
->nblocksx
[level
];
339 pt
->nblocksy
= texture
->nblocksy
[level
];
340 pt
->stride
= ct
->stride
[level
];
346 ctrans
->offset
= ct
->level_offset
[level
];
348 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
349 ctrans
->offset
+= face
* pt
->nblocksy
* pt
->stride
;
351 else if (texture
->target
== PIPE_TEXTURE_3D
) {
352 ctrans
->offset
+= zslice
* pt
->nblocksy
* pt
->stride
;
365 cell_tex_transfer_destroy(struct pipe_transfer
*t
)
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.
372 assert (transfer
->base
.texture
);
373 pipe_texture_reference(&transfer
->base
.texture
, NULL
);
379 * Return pointer to texture image data in linear layout.
382 cell_transfer_map(struct pipe_screen
*screen
, struct pipe_transfer
*transfer
)
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
];
393 assert(transfer
->texture
);
397 ct
->mapped
= pipe_buffer_map(screen
, ct
->buffer
,
398 pipe_transfer_buffer_flags(transfer
));
402 * Create a buffer of ordinary memory for the linear texture.
403 * This is the memory that the user will read/write.
405 size
= pt
->nblocksx
[level
] * pt
->nblocksy
[level
] * pt
->block
.size
;
407 ctrans
->map
= align_malloc(size
, 16);
409 return NULL
; /* out of memory */
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
);
415 const uint
*src
= (uint
*) (ct
->mapped
+ ctrans
->offset
);
416 uint
*dst
= ctrans
->map
;
417 untwiddle_image_uint(texWidth
, texHeight
, TILE_SIZE
,
430 * Called when user is done reading/writing texture data.
431 * If new data was written, this is where we convert the linear data
435 cell_transfer_unmap(struct pipe_screen
*screen
,
436 struct pipe_transfer
*transfer
)
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
];
448 ct
->mapped
= pipe_buffer_map(screen
, ct
->buffer
,
449 PIPE_BUFFER_USAGE_CPU_READ
);
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.
456 const uint bpp
= util_format_get_size(ct
->base
.format
);
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
);
467 align_free(ctrans
->map
);
473 cell_init_screen_texture_funcs(struct pipe_screen
*screen
)
475 screen
->texture_create
= cell_texture_create
;
476 screen
->texture_destroy
= cell_texture_destroy
;
478 screen
->get_tex_surface
= cell_get_tex_surface
;
479 screen
->tex_surface_destroy
= cell_tex_surface_destroy
;
481 screen
->get_tex_transfer
= cell_get_tex_transfer
;
482 screen
->tex_transfer_destroy
= cell_tex_transfer_destroy
;
484 screen
->transfer_map
= cell_transfer_map
;
485 screen
->transfer_unmap
= cell_transfer_unmap
;