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 "util/u_inlines.h"
38 #include "util/u_format.h"
39 #include "util/u_math.h"
40 #include "util/u_memory.h"
42 #include "cell_context.h"
43 #include "cell_state.h"
44 #include "cell_texture.h"
49 cell_texture_layout(struct cell_texture
*ct
)
51 struct pipe_texture
*pt
= &ct
->base
;
53 unsigned width
= pt
->width0
;
54 unsigned height
= pt
->height0
;
55 unsigned depth
= pt
->depth0
;
59 for (level
= 0; level
<= pt
->last_level
; level
++) {
61 unsigned w_tile
, h_tile
;
63 assert(level
< CELL_MAX_TEXTURE_LEVELS
);
65 /* width, height, rounded up to tile size */
66 w_tile
= align(width
, TILE_SIZE
);
67 h_tile
= align(height
, TILE_SIZE
);
69 ct
->stride
[level
] = util_format_get_stride(pt
->format
, w_tile
);
71 ct
->level_offset
[level
] = ct
->buffer_size
;
73 size
= ct
->stride
[level
] * util_format_get_nblocksy(pt
->format
, h_tile
);
74 if (pt
->target
== PIPE_TEXTURE_CUBE
)
79 ct
->buffer_size
+= size
;
81 width
= u_minify(width
, 1);
82 height
= u_minify(height
, 1);
83 depth
= u_minify(depth
, 1);
88 static struct pipe_texture
*
89 cell_texture_create(struct pipe_screen
*screen
,
90 const struct pipe_texture
*templat
)
92 struct cell_texture
*ct
= CALLOC_STRUCT(cell_texture
);
97 pipe_reference_init(&ct
->base
.reference
, 1);
98 ct
->base
.screen
= screen
;
100 cell_texture_layout(ct
);
102 ct
->buffer
= screen
->buffer_create(screen
, 32, PIPE_BUFFER_USAGE_PIXEL
,
115 cell_texture_destroy(struct pipe_texture
*pt
)
117 struct cell_texture
*ct
= cell_texture(pt
);
120 pipe_buffer_unmap(ct
->buffer
->screen
, ct
->buffer
);
124 pipe_buffer_reference(&ct
->buffer
, NULL
);
132 * Convert image from linear layout to tiled layout. 4-byte pixels.
135 twiddle_image_uint(uint w
, uint h
, uint tile_size
, uint
*dst
,
136 uint src_stride
, const uint
*src
)
138 const uint tile_size2
= tile_size
* tile_size
;
139 const uint h_t
= (h
+ tile_size
- 1) / tile_size
;
140 const uint w_t
= (w
+ tile_size
- 1) / tile_size
;
142 uint it
, jt
; /* tile counters */
143 uint i
, j
; /* intra-tile counters */
145 src_stride
/= 4; /* convert from bytes to pixels */
147 /* loop over dest tiles */
148 for (it
= 0; it
< h_t
; it
++) {
149 for (jt
= 0; jt
< w_t
; jt
++) {
150 /* start of dest tile: */
151 uint
*tdst
= dst
+ (it
* w_t
+ jt
) * tile_size2
;
153 /* compute size of this tile (may be smaller than tile_size) */
154 /* XXX note: a compiler bug was found here. That's why the code
157 uint tile_width
= w
- jt
* tile_size
;
158 tile_width
= MIN2(tile_width
, tile_size
);
159 uint tile_height
= h
- it
* tile_size
;
160 tile_height
= MIN2(tile_height
, tile_size
);
162 /* loop over texels in the tile */
163 for (i
= 0; i
< tile_height
; i
++) {
164 for (j
= 0; j
< tile_width
; j
++) {
165 const uint srci
= it
* tile_size
+ i
;
166 const uint srcj
= jt
* tile_size
+ j
;
169 tdst
[i
* tile_size
+ j
] = src
[srci
* src_stride
+ srcj
];
178 * For Cell. Basically, rearrange the pixels/quads from this layout:
191 twiddle_tile(const uint
*tileIn
, uint
*tileOut
)
195 for (y
= 0; y
< TILE_SIZE
; y
+=2) {
196 for (x
= 0; x
< TILE_SIZE
; x
+=2) {
197 int k
= 4 * (y
/2 * TILE_SIZE
/2 + x
/2);
198 tileOut
[y
* TILE_SIZE
+ (x
+ 0)] = tileIn
[k
];
199 tileOut
[y
* TILE_SIZE
+ (x
+ 1)] = tileIn
[k
+1];
200 tileOut
[(y
+ 1) * TILE_SIZE
+ (x
+ 0)] = tileIn
[k
+2];
201 tileOut
[(y
+ 1) * TILE_SIZE
+ (x
+ 1)] = tileIn
[k
+3];
208 * Convert image from tiled layout to linear layout. 4-byte pixels.
211 untwiddle_image_uint(uint w
, uint h
, uint tile_size
, uint
*dst
,
212 uint dst_stride
, const uint
*src
)
214 const uint tile_size2
= tile_size
* tile_size
;
215 const uint h_t
= (h
+ tile_size
- 1) / tile_size
;
216 const uint w_t
= (w
+ tile_size
- 1) / tile_size
;
218 uint it
, jt
; /* tile counters */
219 uint i
, j
; /* intra-tile counters */
221 dst_stride
/= 4; /* convert from bytes to pixels */
223 tile_buf
= align_malloc(tile_size
* tile_size
* 4, 16);
225 /* loop over src tiles */
226 for (it
= 0; it
< h_t
; it
++) {
227 for (jt
= 0; jt
< w_t
; jt
++) {
228 /* start of src tile: */
229 const uint
*tsrc
= src
+ (it
* w_t
+ jt
) * tile_size2
;
231 twiddle_tile(tsrc
, tile_buf
);
234 /* compute size of this tile (may be smaller than tile_size) */
235 /* XXX note: a compiler bug was found here. That's why the code
238 uint tile_width
= w
- jt
* tile_size
;
239 tile_width
= MIN2(tile_width
, tile_size
);
240 uint tile_height
= h
- it
* tile_size
;
241 tile_height
= MIN2(tile_height
, tile_size
);
243 /* loop over texels in the tile */
244 for (i
= 0; i
< tile_height
; i
++) {
245 for (j
= 0; j
< tile_width
; j
++) {
246 uint dsti
= it
* tile_size
+ i
;
247 uint dstj
= jt
* tile_size
+ j
;
250 dst
[dsti
* dst_stride
+ dstj
] = tsrc
[i
* tile_size
+ j
];
256 align_free(tile_buf
);
260 static struct pipe_surface
*
261 cell_get_tex_surface(struct pipe_screen
*screen
,
262 struct pipe_texture
*pt
,
263 unsigned face
, unsigned level
, unsigned zslice
,
266 struct cell_texture
*ct
= cell_texture(pt
);
267 struct pipe_surface
*ps
;
269 ps
= CALLOC_STRUCT(pipe_surface
);
271 pipe_reference_init(&ps
->reference
, 1);
272 pipe_texture_reference(&ps
->texture
, pt
);
273 ps
->format
= pt
->format
;
274 ps
->width
= u_minify(pt
->width0
, level
);
275 ps
->height
= u_minify(pt
->height0
, level
);
276 ps
->offset
= ct
->level_offset
[level
];
277 /* XXX may need to override usage flags (see sp_texture.c) */
283 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
284 unsigned h_tile
= align(ps
->height
, TILE_SIZE
);
285 ps
->offset
+= face
* util_format_get_nblocksy(ps
->format
, h_tile
) * ct
->stride
[level
];
287 else if (pt
->target
== PIPE_TEXTURE_3D
) {
288 unsigned h_tile
= align(ps
->height
, TILE_SIZE
);
289 ps
->offset
+= zslice
* util_format_get_nblocksy(ps
->format
, h_tile
) * ct
->stride
[level
];
301 cell_tex_surface_destroy(struct pipe_surface
*surf
)
303 pipe_texture_reference(&surf
->texture
, NULL
);
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).
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
)
320 struct cell_texture
*ct
= cell_texture(texture
);
321 struct cell_transfer
*ctrans
;
324 assert(level
<= texture
->last_level
);
326 ctrans
= CALLOC_STRUCT(cell_transfer
);
328 struct pipe_transfer
*pt
= &ctrans
->base
;
329 pipe_texture_reference(&pt
->texture
, texture
);
334 pt
->stride
= ct
->stride
[level
];
340 ctrans
->offset
= ct
->level_offset
[level
];
342 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
343 unsigned h_tile
= align(u_minify(texture
->height0
, level
), TILE_SIZE
);
344 ctrans
->offset
+= face
* util_format_get_nblocksy(texture
->format
, h_tile
) * pt
->stride
;
346 else if (texture
->target
== PIPE_TEXTURE_3D
) {
347 unsigned h_tile
= align(u_minify(texture
->height0
, level
), TILE_SIZE
);
348 ctrans
->offset
+= zslice
* util_format_get_nblocksy(texture
->format
, h_tile
) * pt
->stride
;
361 cell_tex_transfer_destroy(struct pipe_transfer
*t
)
363 struct cell_transfer
*transfer
= cell_transfer(t
);
364 /* Effectively do the texture_update work here - if texture images
365 * needed post-processing to put them into hardware layout, this is
366 * where it would happen. For cell, nothing to do.
368 assert (transfer
->base
.texture
);
369 pipe_texture_reference(&transfer
->base
.texture
, NULL
);
375 * Return pointer to texture image data in linear layout.
378 cell_transfer_map(struct pipe_screen
*screen
, struct pipe_transfer
*transfer
)
380 struct cell_transfer
*ctrans
= cell_transfer(transfer
);
381 struct pipe_texture
*pt
= transfer
->texture
;
382 struct cell_texture
*ct
= cell_texture(pt
);
383 const uint level
= ctrans
->base
.level
;
384 const uint texWidth
= u_minify(pt
->width0
, level
);
385 const uint texHeight
= u_minify(pt
->height0
, level
);
386 const uint stride
= ct
->stride
[level
];
389 assert(transfer
->texture
);
393 ct
->mapped
= pipe_buffer_map(screen
, ct
->buffer
,
394 pipe_transfer_buffer_flags(transfer
));
398 * Create a buffer of ordinary memory for the linear texture.
399 * This is the memory that the user will read/write.
401 size
= util_format_get_stride(pt
->format
, align(texWidth
, TILE_SIZE
)) *
402 util_format_get_nblocksy(pt
->format
, align(texHeight
, TILE_SIZE
));
404 ctrans
->map
= align_malloc(size
, 16);
406 return NULL
; /* out of memory */
408 if (transfer
->usage
& PIPE_TRANSFER_READ
) {
409 /* need to untwiddle the texture to make a linear version */
410 const uint bpp
= util_format_get_blocksize(ct
->base
.format
);
412 const uint
*src
= (uint
*) (ct
->mapped
+ ctrans
->offset
);
413 uint
*dst
= ctrans
->map
;
414 untwiddle_image_uint(texWidth
, texHeight
, TILE_SIZE
,
427 * Called when user is done reading/writing texture data.
428 * If new data was written, this is where we convert the linear data
432 cell_transfer_unmap(struct pipe_screen
*screen
,
433 struct pipe_transfer
*transfer
)
435 struct cell_transfer
*ctrans
= cell_transfer(transfer
);
436 struct pipe_texture
*pt
= transfer
->texture
;
437 struct cell_texture
*ct
= cell_texture(pt
);
438 const uint level
= ctrans
->base
.level
;
439 const uint texWidth
= u_minify(pt
->width0
, level
);
440 const uint texHeight
= u_minify(pt
->height0
, level
);
441 const uint stride
= ct
->stride
[level
];
445 ct
->mapped
= pipe_buffer_map(screen
, ct
->buffer
,
446 PIPE_BUFFER_USAGE_CPU_READ
);
449 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
450 /* The user wrote new texture data into the mapped buffer.
451 * We need to convert the new linear data into the twiddled/tiled format.
453 const uint bpp
= util_format_get_blocksize(ct
->base
.format
);
455 const uint
*src
= ctrans
->map
;
456 uint
*dst
= (uint
*) (ct
->mapped
+ ctrans
->offset
);
457 twiddle_image_uint(texWidth
, texHeight
, TILE_SIZE
, dst
, stride
, src
);
464 align_free(ctrans
->map
);
470 cell_init_screen_texture_funcs(struct pipe_screen
*screen
)
472 screen
->texture_create
= cell_texture_create
;
473 screen
->texture_destroy
= cell_texture_destroy
;
475 screen
->get_tex_surface
= cell_get_tex_surface
;
476 screen
->tex_surface_destroy
= cell_tex_surface_destroy
;
478 screen
->get_tex_transfer
= cell_get_tex_transfer
;
479 screen
->tex_transfer_destroy
= cell_tex_transfer_destroy
;
481 screen
->transfer_map
= cell_transfer_map
;
482 screen
->transfer_unmap
= cell_transfer_unmap
;