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 ct
->stride
[level
] = util_format_get_stride(pt
->format
, w_tile
);
72 ct
->level_offset
[level
] = ct
->buffer_size
;
74 size
= ct
->stride
[level
] * util_format_get_nblocksy(pt
->format
, h_tile
);
75 if (pt
->target
== PIPE_TEXTURE_CUBE
)
80 ct
->buffer_size
+= size
;
82 width
= u_minify(width
, 1);
83 height
= u_minify(height
, 1);
84 depth
= u_minify(depth
, 1);
89 static struct pipe_texture
*
90 cell_texture_create(struct pipe_screen
*screen
,
91 const struct pipe_texture
*templat
)
93 struct cell_texture
*ct
= CALLOC_STRUCT(cell_texture
);
98 pipe_reference_init(&ct
->base
.reference
, 1);
99 ct
->base
.screen
= screen
;
101 cell_texture_layout(ct
);
103 ct
->buffer
= screen
->buffer_create(screen
, 32, PIPE_BUFFER_USAGE_PIXEL
,
116 cell_texture_destroy(struct pipe_texture
*pt
)
118 struct cell_texture
*ct
= cell_texture(pt
);
121 pipe_buffer_unmap(ct
->buffer
->screen
, ct
->buffer
);
125 pipe_buffer_reference(&ct
->buffer
, NULL
);
133 * Convert image from linear layout to tiled layout. 4-byte pixels.
136 twiddle_image_uint(uint w
, uint h
, uint tile_size
, uint
*dst
,
137 uint src_stride
, const uint
*src
)
139 const uint tile_size2
= tile_size
* tile_size
;
140 const uint h_t
= (h
+ tile_size
- 1) / tile_size
;
141 const uint w_t
= (w
+ tile_size
- 1) / tile_size
;
143 uint it
, jt
; /* tile counters */
144 uint i
, j
; /* intra-tile counters */
146 src_stride
/= 4; /* convert from bytes to pixels */
148 /* loop over dest tiles */
149 for (it
= 0; it
< h_t
; it
++) {
150 for (jt
= 0; jt
< w_t
; jt
++) {
151 /* start of dest tile: */
152 uint
*tdst
= dst
+ (it
* w_t
+ jt
) * tile_size2
;
154 /* compute size of this tile (may be smaller than tile_size) */
155 /* XXX note: a compiler bug was found here. That's why the code
158 uint tile_width
= w
- jt
* tile_size
;
159 tile_width
= MIN2(tile_width
, tile_size
);
160 uint tile_height
= h
- it
* tile_size
;
161 tile_height
= MIN2(tile_height
, tile_size
);
163 /* loop over texels in the tile */
164 for (i
= 0; i
< tile_height
; i
++) {
165 for (j
= 0; j
< tile_width
; j
++) {
166 const uint srci
= it
* tile_size
+ i
;
167 const uint srcj
= jt
* tile_size
+ j
;
170 tdst
[i
* tile_size
+ j
] = src
[srci
* src_stride
+ srcj
];
179 * For Cell. Basically, rearrange the pixels/quads from this layout:
192 twiddle_tile(const uint
*tileIn
, uint
*tileOut
)
196 for (y
= 0; y
< TILE_SIZE
; y
+=2) {
197 for (x
= 0; x
< TILE_SIZE
; x
+=2) {
198 int k
= 4 * (y
/2 * TILE_SIZE
/2 + x
/2);
199 tileOut
[y
* TILE_SIZE
+ (x
+ 0)] = tileIn
[k
];
200 tileOut
[y
* TILE_SIZE
+ (x
+ 1)] = tileIn
[k
+1];
201 tileOut
[(y
+ 1) * TILE_SIZE
+ (x
+ 0)] = tileIn
[k
+2];
202 tileOut
[(y
+ 1) * TILE_SIZE
+ (x
+ 1)] = tileIn
[k
+3];
209 * Convert image from tiled layout to linear layout. 4-byte pixels.
212 untwiddle_image_uint(uint w
, uint h
, uint tile_size
, uint
*dst
,
213 uint dst_stride
, const uint
*src
)
215 const uint tile_size2
= tile_size
* tile_size
;
216 const uint h_t
= (h
+ tile_size
- 1) / tile_size
;
217 const uint w_t
= (w
+ tile_size
- 1) / tile_size
;
219 uint it
, jt
; /* tile counters */
220 uint i
, j
; /* intra-tile counters */
222 dst_stride
/= 4; /* convert from bytes to pixels */
224 tile_buf
= align_malloc(tile_size
* tile_size
* 4, 16);
226 /* loop over src tiles */
227 for (it
= 0; it
< h_t
; it
++) {
228 for (jt
= 0; jt
< w_t
; jt
++) {
229 /* start of src tile: */
230 const uint
*tsrc
= src
+ (it
* w_t
+ jt
) * tile_size2
;
232 twiddle_tile(tsrc
, tile_buf
);
235 /* compute size of this tile (may be smaller than tile_size) */
236 /* XXX note: a compiler bug was found here. That's why the code
239 uint tile_width
= w
- jt
* tile_size
;
240 tile_width
= MIN2(tile_width
, tile_size
);
241 uint tile_height
= h
- it
* tile_size
;
242 tile_height
= MIN2(tile_height
, tile_size
);
244 /* loop over texels in the tile */
245 for (i
= 0; i
< tile_height
; i
++) {
246 for (j
= 0; j
< tile_width
; j
++) {
247 uint dsti
= it
* tile_size
+ i
;
248 uint dstj
= jt
* tile_size
+ j
;
251 dst
[dsti
* dst_stride
+ dstj
] = tsrc
[i
* tile_size
+ j
];
257 align_free(tile_buf
);
261 static struct pipe_surface
*
262 cell_get_tex_surface(struct pipe_screen
*screen
,
263 struct pipe_texture
*pt
,
264 unsigned face
, unsigned level
, unsigned zslice
,
267 struct cell_texture
*ct
= cell_texture(pt
);
268 struct pipe_surface
*ps
;
270 ps
= CALLOC_STRUCT(pipe_surface
);
272 pipe_reference_init(&ps
->reference
, 1);
273 pipe_texture_reference(&ps
->texture
, pt
);
274 ps
->format
= pt
->format
;
275 ps
->width
= u_minify(pt
->width0
, level
);
276 ps
->height
= u_minify(pt
->height0
, level
);
277 ps
->offset
= ct
->level_offset
[level
];
278 /* XXX may need to override usage flags (see sp_texture.c) */
284 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
285 unsigned h_tile
= align(ps
->height
, TILE_SIZE
);
286 ps
->offset
+= face
* util_format_get_nblocksy(ps
->format
, h_tile
) * ct
->stride
[level
];
288 else if (pt
->target
== PIPE_TEXTURE_3D
) {
289 unsigned h_tile
= align(ps
->height
, TILE_SIZE
);
290 ps
->offset
+= zslice
* util_format_get_nblocksy(ps
->format
, h_tile
) * ct
->stride
[level
];
302 cell_tex_surface_destroy(struct pipe_surface
*surf
)
304 pipe_texture_reference(&surf
->texture
, NULL
);
310 * Create new pipe_transfer object.
311 * This is used by the user to put tex data into a texture (and get it
312 * back out for glGetTexImage).
314 static struct pipe_transfer
*
315 cell_get_tex_transfer(struct pipe_screen
*screen
,
316 struct pipe_texture
*texture
,
317 unsigned face
, unsigned level
, unsigned zslice
,
318 enum pipe_transfer_usage usage
,
319 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
321 struct cell_texture
*ct
= cell_texture(texture
);
322 struct cell_transfer
*ctrans
;
325 assert(level
<= texture
->last_level
);
327 ctrans
= CALLOC_STRUCT(cell_transfer
);
329 struct pipe_transfer
*pt
= &ctrans
->base
;
330 pipe_texture_reference(&pt
->texture
, texture
);
335 pt
->stride
= ct
->stride
[level
];
341 ctrans
->offset
= ct
->level_offset
[level
];
343 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
344 unsigned h_tile
= align(u_minify(texture
->height0
, level
), TILE_SIZE
);
345 ctrans
->offset
+= face
* util_format_get_nblocksy(texture
->format
, h_tile
) * pt
->stride
;
347 else if (texture
->target
== PIPE_TEXTURE_3D
) {
348 unsigned h_tile
= align(u_minify(texture
->height0
, level
), TILE_SIZE
);
349 ctrans
->offset
+= zslice
* util_format_get_nblocksy(texture
->format
, h_tile
) * pt
->stride
;
362 cell_tex_transfer_destroy(struct pipe_transfer
*t
)
364 struct cell_transfer
*transfer
= cell_transfer(t
);
365 /* Effectively do the texture_update work here - if texture images
366 * needed post-processing to put them into hardware layout, this is
367 * where it would happen. For cell, nothing to do.
369 assert (transfer
->base
.texture
);
370 pipe_texture_reference(&transfer
->base
.texture
, NULL
);
376 * Return pointer to texture image data in linear layout.
379 cell_transfer_map(struct pipe_screen
*screen
, struct pipe_transfer
*transfer
)
381 struct cell_transfer
*ctrans
= cell_transfer(transfer
);
382 struct pipe_texture
*pt
= transfer
->texture
;
383 struct cell_texture
*ct
= cell_texture(pt
);
384 const uint level
= ctrans
->base
.level
;
385 const uint texWidth
= u_minify(pt
->width0
, level
);
386 const uint texHeight
= u_minify(pt
->height0
, level
);
387 const uint stride
= ct
->stride
[level
];
390 assert(transfer
->texture
);
394 ct
->mapped
= pipe_buffer_map(screen
, ct
->buffer
,
395 pipe_transfer_buffer_flags(transfer
));
399 * Create a buffer of ordinary memory for the linear texture.
400 * This is the memory that the user will read/write.
402 size
= util_format_get_stride(pt
->format
, align(texWidth
, TILE_SIZE
)) *
403 util_format_get_nblocksy(pt
->format
, align(texHeight
, TILE_SIZE
));
405 ctrans
->map
= align_malloc(size
, 16);
407 return NULL
; /* out of memory */
409 if (transfer
->usage
& PIPE_TRANSFER_READ
) {
410 /* need to untwiddle the texture to make a linear version */
411 const uint bpp
= util_format_get_blocksize(ct
->base
.format
);
413 const uint
*src
= (uint
*) (ct
->mapped
+ ctrans
->offset
);
414 uint
*dst
= ctrans
->map
;
415 untwiddle_image_uint(texWidth
, texHeight
, TILE_SIZE
,
428 * Called when user is done reading/writing texture data.
429 * If new data was written, this is where we convert the linear data
433 cell_transfer_unmap(struct pipe_screen
*screen
,
434 struct pipe_transfer
*transfer
)
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
];
446 ct
->mapped
= pipe_buffer_map(screen
, ct
->buffer
,
447 PIPE_BUFFER_USAGE_CPU_READ
);
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.
454 const uint bpp
= util_format_get_blocksize(ct
->base
.format
);
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
);
465 align_free(ctrans
->map
);
471 cell_init_screen_texture_funcs(struct pipe_screen
*screen
)
473 screen
->texture_create
= cell_texture_create
;
474 screen
->texture_destroy
= cell_texture_destroy
;
476 screen
->get_tex_surface
= cell_get_tex_surface
;
477 screen
->tex_surface_destroy
= cell_tex_surface_destroy
;
479 screen
->get_tex_transfer
= cell_get_tex_transfer
;
480 screen
->tex_transfer_destroy
= cell_tex_transfer_destroy
;
482 screen
->transfer_map
= cell_transfer_map
;
483 screen
->transfer_unmap
= cell_transfer_unmap
;