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"
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"
48 cell_texture_layout(struct cell_texture
*ct
)
50 struct pipe_texture
*pt
= &ct
->base
;
52 unsigned width
= pt
->width0
;
53 unsigned height
= pt
->height0
;
54 unsigned depth
= pt
->depth0
;
58 for (level
= 0; level
<= pt
->last_level
; level
++) {
60 unsigned w_tile
, h_tile
;
62 assert(level
< CELL_MAX_TEXTURE_LEVELS
);
64 /* width, height, rounded up to tile size */
65 w_tile
= align(width
, TILE_SIZE
);
66 h_tile
= align(height
, TILE_SIZE
);
68 ct
->stride
[level
] = pf_get_stride(pt
->format
, w_tile
);
70 ct
->level_offset
[level
] = ct
->buffer_size
;
72 size
= ct
->stride
[level
] * pf_get_nblocksy(pt
->format
, h_tile
);
73 if (pt
->target
== PIPE_TEXTURE_CUBE
)
78 ct
->buffer_size
+= size
;
80 width
= u_minify(width
, 1);
81 height
= u_minify(height
, 1);
82 depth
= u_minify(depth
, 1);
87 static struct pipe_texture
*
88 cell_texture_create(struct pipe_screen
*screen
,
89 const struct pipe_texture
*templat
)
91 struct cell_texture
*ct
= CALLOC_STRUCT(cell_texture
);
96 pipe_reference_init(&ct
->base
.reference
, 1);
97 ct
->base
.screen
= screen
;
99 cell_texture_layout(ct
);
101 ct
->buffer
= screen
->buffer_create(screen
, 32, PIPE_BUFFER_USAGE_PIXEL
,
114 cell_texture_destroy(struct pipe_texture
*pt
)
116 struct cell_texture
*ct
= cell_texture(pt
);
119 pipe_buffer_unmap(ct
->buffer
->screen
, ct
->buffer
);
123 pipe_buffer_reference(&ct
->buffer
, NULL
);
131 * Convert image from linear layout to tiled layout. 4-byte pixels.
134 twiddle_image_uint(uint w
, uint h
, uint tile_size
, uint
*dst
,
135 uint src_stride
, const uint
*src
)
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
;
141 uint it
, jt
; /* tile counters */
142 uint i
, j
; /* intra-tile counters */
144 src_stride
/= 4; /* convert from bytes to pixels */
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
;
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
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
);
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
;
168 tdst
[i
* tile_size
+ j
] = src
[srci
* src_stride
+ srcj
];
177 * For Cell. Basically, rearrange the pixels/quads from this layout:
190 twiddle_tile(const uint
*tileIn
, uint
*tileOut
)
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];
207 * Convert image from tiled layout to linear layout. 4-byte pixels.
210 untwiddle_image_uint(uint w
, uint h
, uint tile_size
, uint
*dst
,
211 uint dst_stride
, const uint
*src
)
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
;
217 uint it
, jt
; /* tile counters */
218 uint i
, j
; /* intra-tile counters */
220 dst_stride
/= 4; /* convert from bytes to pixels */
222 tile_buf
= align_malloc(tile_size
* tile_size
* 4, 16);
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
;
230 twiddle_tile(tsrc
, tile_buf
);
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
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
);
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
;
249 dst
[dsti
* dst_stride
+ dstj
] = tsrc
[i
* tile_size
+ j
];
255 align_free(tile_buf
);
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
,
265 struct cell_texture
*ct
= cell_texture(pt
);
266 struct pipe_surface
*ps
;
268 ps
= CALLOC_STRUCT(pipe_surface
);
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) */
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
];
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
];
300 cell_tex_surface_destroy(struct pipe_surface
*surf
)
302 pipe_texture_reference(&surf
->texture
, NULL
);
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).
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
)
319 struct cell_texture
*ct
= cell_texture(texture
);
320 struct cell_transfer
*ctrans
;
323 assert(level
<= texture
->last_level
);
325 ctrans
= CALLOC_STRUCT(cell_transfer
);
327 struct pipe_transfer
*pt
= &ctrans
->base
;
328 pipe_texture_reference(&pt
->texture
, texture
);
333 pt
->stride
= ct
->stride
[level
];
339 ctrans
->offset
= ct
->level_offset
[level
];
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
;
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
;
360 cell_tex_transfer_destroy(struct pipe_transfer
*t
)
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.
367 assert (transfer
->base
.texture
);
368 pipe_texture_reference(&transfer
->base
.texture
, NULL
);
374 * Return pointer to texture image data in linear layout.
377 cell_transfer_map(struct pipe_screen
*screen
, struct pipe_transfer
*transfer
)
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
];
388 assert(transfer
->texture
);
392 ct
->mapped
= pipe_buffer_map(screen
, ct
->buffer
,
393 pipe_transfer_buffer_flags(transfer
));
397 * Create a buffer of ordinary memory for the linear texture.
398 * This is the memory that the user will read/write.
400 size
= pf_get_stride(pt
->format
, align(texWidth
, TILE_SIZE
)) *
401 pf_get_nblocksy(pt
->format
, align(texHeight
, TILE_SIZE
));
403 ctrans
->map
= align_malloc(size
, 16);
405 return NULL
; /* out of memory */
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
);
411 const uint
*src
= (uint
*) (ct
->mapped
+ ctrans
->offset
);
412 uint
*dst
= ctrans
->map
;
413 untwiddle_image_uint(texWidth
, texHeight
, TILE_SIZE
,
426 * Called when user is done reading/writing texture data.
427 * If new data was written, this is where we convert the linear data
431 cell_transfer_unmap(struct pipe_screen
*screen
,
432 struct pipe_transfer
*transfer
)
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
];
444 ct
->mapped
= pipe_buffer_map(screen
, ct
->buffer
,
445 PIPE_BUFFER_USAGE_CPU_READ
);
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.
452 const uint bpp
= pf_get_blocksize(ct
->base
.format
);
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
);
463 align_free(ctrans
->map
);
469 cell_init_screen_texture_funcs(struct pipe_screen
*screen
)
471 screen
->texture_create
= cell_texture_create
;
472 screen
->texture_destroy
= cell_texture_destroy
;
474 screen
->get_tex_surface
= cell_get_tex_surface
;
475 screen
->tex_surface_destroy
= cell_tex_surface_destroy
;
477 screen
->get_tex_transfer
= cell_get_tex_transfer
;
478 screen
->tex_transfer_destroy
= cell_tex_transfer_destroy
;
480 screen
->transfer_map
= cell_transfer_map
;
481 screen
->transfer_unmap
= cell_transfer_unmap
;