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
->width
[0];
53 unsigned height
= pt
->height
[0];
54 unsigned depth
= pt
->depth
[0];
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 pt
->width
[level
] = width
;
69 pt
->height
[level
] = height
;
70 pt
->depth
[level
] = depth
;
71 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, w_tile
);
72 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, h_tile
);
74 ct
->stride
[level
] = pt
->nblocksx
[level
] * pt
->block
.size
;
76 ct
->level_offset
[level
] = ct
->buffer_size
;
78 size
= pt
->nblocksx
[level
] * pt
->nblocksy
[level
] * pt
->block
.size
;
79 if (pt
->target
== PIPE_TEXTURE_CUBE
)
84 ct
->buffer_size
+= size
;
86 width
= minify(width
);
87 height
= minify(height
);
88 depth
= minify(depth
);
93 static struct pipe_texture
*
94 cell_texture_create(struct pipe_screen
*screen
,
95 const struct pipe_texture
*templat
)
97 struct cell_texture
*ct
= CALLOC_STRUCT(cell_texture
);
102 pipe_reference_init(&ct
->base
.reference
, 1);
103 ct
->base
.screen
= screen
;
105 cell_texture_layout(ct
);
107 ct
->buffer
= screen
->buffer_create(screen
, 32, PIPE_BUFFER_USAGE_PIXEL
,
120 cell_texture_destroy(struct pipe_texture
*pt
)
122 struct cell_texture
*ct
= cell_texture(pt
);
125 pipe_buffer_unmap(ct
->buffer
->screen
, ct
->buffer
);
129 pipe_buffer_reference(&ct
->buffer
, NULL
);
137 * Convert image from linear layout to tiled layout. 4-byte pixels.
140 twiddle_image_uint(uint w
, uint h
, uint tile_size
, uint
*dst
,
141 uint src_stride
, const uint
*src
)
143 const uint tile_size2
= tile_size
* tile_size
;
144 const uint h_t
= (h
+ tile_size
- 1) / tile_size
;
145 const uint w_t
= (w
+ tile_size
- 1) / tile_size
;
147 uint it
, jt
; /* tile counters */
148 uint i
, j
; /* intra-tile counters */
150 src_stride
/= 4; /* convert from bytes to pixels */
152 /* loop over dest tiles */
153 for (it
= 0; it
< h_t
; it
++) {
154 for (jt
= 0; jt
< w_t
; jt
++) {
155 /* start of dest tile: */
156 uint
*tdst
= dst
+ (it
* w_t
+ jt
) * tile_size2
;
158 /* compute size of this tile (may be smaller than tile_size) */
159 /* XXX note: a compiler bug was found here. That's why the code
162 uint tile_width
= w
- jt
* tile_size
;
163 tile_width
= MIN2(tile_width
, tile_size
);
164 uint tile_height
= h
- it
* tile_size
;
165 tile_height
= MIN2(tile_height
, tile_size
);
167 /* loop over texels in the tile */
168 for (i
= 0; i
< tile_height
; i
++) {
169 for (j
= 0; j
< tile_width
; j
++) {
170 const uint srci
= it
* tile_size
+ i
;
171 const uint srcj
= jt
* tile_size
+ j
;
174 tdst
[i
* tile_size
+ j
] = src
[srci
* src_stride
+ srcj
];
183 * For Cell. Basically, rearrange the pixels/quads from this layout:
196 twiddle_tile(const uint
*tileIn
, uint
*tileOut
)
200 for (y
= 0; y
< TILE_SIZE
; y
+=2) {
201 for (x
= 0; x
< TILE_SIZE
; x
+=2) {
202 int k
= 4 * (y
/2 * TILE_SIZE
/2 + x
/2);
203 tileOut
[y
* TILE_SIZE
+ (x
+ 0)] = tileIn
[k
];
204 tileOut
[y
* TILE_SIZE
+ (x
+ 1)] = tileIn
[k
+1];
205 tileOut
[(y
+ 1) * TILE_SIZE
+ (x
+ 0)] = tileIn
[k
+2];
206 tileOut
[(y
+ 1) * TILE_SIZE
+ (x
+ 1)] = tileIn
[k
+3];
213 * Convert image from tiled layout to linear layout. 4-byte pixels.
216 untwiddle_image_uint(uint w
, uint h
, uint tile_size
, uint
*dst
,
217 uint dst_stride
, const uint
*src
)
219 const uint tile_size2
= tile_size
* tile_size
;
220 const uint h_t
= (h
+ tile_size
- 1) / tile_size
;
221 const uint w_t
= (w
+ tile_size
- 1) / tile_size
;
223 uint it
, jt
; /* tile counters */
224 uint i
, j
; /* intra-tile counters */
226 dst_stride
/= 4; /* convert from bytes to pixels */
228 tile_buf
= align_malloc(tile_size
* tile_size
* 4, 16);
230 /* loop over src tiles */
231 for (it
= 0; it
< h_t
; it
++) {
232 for (jt
= 0; jt
< w_t
; jt
++) {
233 /* start of src tile: */
234 const uint
*tsrc
= src
+ (it
* w_t
+ jt
) * tile_size2
;
236 twiddle_tile(tsrc
, tile_buf
);
239 /* compute size of this tile (may be smaller than tile_size) */
240 /* XXX note: a compiler bug was found here. That's why the code
243 uint tile_width
= w
- jt
* tile_size
;
244 tile_width
= MIN2(tile_width
, tile_size
);
245 uint tile_height
= h
- it
* tile_size
;
246 tile_height
= MIN2(tile_height
, tile_size
);
248 /* loop over texels in the tile */
249 for (i
= 0; i
< tile_height
; i
++) {
250 for (j
= 0; j
< tile_width
; j
++) {
251 uint dsti
= it
* tile_size
+ i
;
252 uint dstj
= jt
* tile_size
+ j
;
255 dst
[dsti
* dst_stride
+ dstj
] = tsrc
[i
* tile_size
+ j
];
261 align_free(tile_buf
);
265 static struct pipe_surface
*
266 cell_get_tex_surface(struct pipe_screen
*screen
,
267 struct pipe_texture
*pt
,
268 unsigned face
, unsigned level
, unsigned zslice
,
271 struct cell_texture
*ct
= cell_texture(pt
);
272 struct pipe_surface
*ps
;
274 ps
= CALLOC_STRUCT(pipe_surface
);
276 pipe_reference_init(&ps
->reference
, 1);
277 pipe_texture_reference(&ps
->texture
, pt
);
278 ps
->format
= pt
->format
;
279 ps
->width
= pt
->width
[level
];
280 ps
->height
= pt
->height
[level
];
281 ps
->offset
= ct
->level_offset
[level
];
282 /* XXX may need to override usage flags (see sp_texture.c) */
288 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
289 ps
->offset
+= face
* pt
->nblocksy
[level
] * ct
->stride
[level
];
291 else if (pt
->target
== PIPE_TEXTURE_3D
) {
292 ps
->offset
+= zslice
* pt
->nblocksy
[level
] * ct
->stride
[level
];
304 cell_tex_surface_destroy(struct pipe_surface
*surf
)
306 pipe_texture_reference(&surf
->texture
, NULL
);
312 * Create new pipe_transfer object.
313 * This is used by the user to put tex data into a texture (and get it
314 * back out for glGetTexImage).
316 static struct pipe_transfer
*
317 cell_get_tex_transfer(struct pipe_screen
*screen
,
318 struct pipe_texture
*texture
,
319 unsigned face
, unsigned level
, unsigned zslice
,
320 enum pipe_transfer_usage usage
,
321 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
323 struct cell_texture
*ct
= cell_texture(texture
);
324 struct cell_transfer
*ctrans
;
327 assert(level
<= texture
->last_level
);
329 ctrans
= CALLOC_STRUCT(cell_transfer
);
331 struct pipe_transfer
*pt
= &ctrans
->base
;
332 pipe_texture_reference(&pt
->texture
, texture
);
333 pt
->format
= texture
->format
;
334 pt
->block
= texture
->block
;
339 pt
->nblocksx
= texture
->nblocksx
[level
];
340 pt
->nblocksy
= texture
->nblocksy
[level
];
341 pt
->stride
= ct
->stride
[level
];
347 ctrans
->offset
= ct
->level_offset
[level
];
349 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
350 ctrans
->offset
+= face
* pt
->nblocksy
* pt
->stride
;
352 else if (texture
->target
== PIPE_TEXTURE_3D
) {
353 ctrans
->offset
+= zslice
* pt
->nblocksy
* pt
->stride
;
366 cell_tex_transfer_destroy(struct pipe_transfer
*t
)
368 struct cell_transfer
*transfer
= cell_transfer(t
);
369 /* Effectively do the texture_update work here - if texture images
370 * needed post-processing to put them into hardware layout, this is
371 * where it would happen. For cell, nothing to do.
373 assert (transfer
->base
.texture
);
374 pipe_texture_reference(&transfer
->base
.texture
, NULL
);
380 * Return pointer to texture image data in linear layout.
383 cell_transfer_map(struct pipe_screen
*screen
, struct pipe_transfer
*transfer
)
385 struct cell_transfer
*ctrans
= cell_transfer(transfer
);
386 struct pipe_texture
*pt
= transfer
->texture
;
387 struct cell_texture
*ct
= cell_texture(pt
);
388 const uint level
= ctrans
->base
.level
;
389 const uint texWidth
= pt
->width
[level
];
390 const uint texHeight
= pt
->height
[level
];
391 const uint stride
= ct
->stride
[level
];
392 unsigned flags
= 0x0;
395 assert(transfer
->texture
);
397 if (transfer
->usage
!= PIPE_TRANSFER_READ
) {
398 flags
|= PIPE_BUFFER_USAGE_CPU_WRITE
;
401 if (transfer
->usage
!= PIPE_TRANSFER_WRITE
) {
402 flags
|= PIPE_BUFFER_USAGE_CPU_READ
;
407 ct
->mapped
= pipe_buffer_map(screen
, ct
->buffer
, flags
);
411 * Create a buffer of ordinary memory for the linear texture.
412 * This is the memory that the user will read/write.
414 size
= pt
->nblocksx
[level
] * pt
->nblocksy
[level
] * pt
->block
.size
;
416 ctrans
->map
= align_malloc(size
, 16);
418 return NULL
; /* out of memory */
420 if (transfer
->usage
== PIPE_TRANSFER_READ
||
421 transfer
->usage
== PIPE_TRANSFER_READ_WRITE
) {
422 /* need to untwiddle the texture to make a linear version */
423 const uint bpp
= pf_get_size(ct
->base
.format
);
425 const uint
*src
= (uint
*) (ct
->mapped
+ ctrans
->offset
);
426 uint
*dst
= ctrans
->map
;
427 untwiddle_image_uint(texWidth
, texHeight
, TILE_SIZE
,
440 * Called when user is done reading/writing texture data.
441 * If new data was written, this is where we convert the linear data
445 cell_transfer_unmap(struct pipe_screen
*screen
,
446 struct pipe_transfer
*transfer
)
448 struct cell_transfer
*ctrans
= cell_transfer(transfer
);
449 struct pipe_texture
*pt
= transfer
->texture
;
450 struct cell_texture
*ct
= cell_texture(pt
);
451 const uint level
= ctrans
->base
.level
;
452 const uint texWidth
= pt
->width
[level
];
453 const uint texHeight
= pt
->height
[level
];
454 const uint stride
= ct
->stride
[level
];
458 ct
->mapped
= pipe_buffer_map(screen
, ct
->buffer
,
459 PIPE_BUFFER_USAGE_CPU_READ
);
462 if (transfer
->usage
== PIPE_TRANSFER_WRITE
||
463 transfer
->usage
== PIPE_TRANSFER_READ_WRITE
) {
464 /* The user wrote new texture data into the mapped buffer.
465 * We need to convert the new linear data into the twiddled/tiled format.
467 const uint bpp
= pf_get_size(ct
->base
.format
);
469 const uint
*src
= ctrans
->map
;
470 uint
*dst
= (uint
*) (ct
->mapped
+ ctrans
->offset
);
471 twiddle_image_uint(texWidth
, texHeight
, TILE_SIZE
, dst
, stride
, src
);
478 align_free(ctrans
->map
);
484 cell_init_screen_texture_funcs(struct pipe_screen
*screen
)
486 screen
->texture_create
= cell_texture_create
;
487 screen
->texture_destroy
= cell_texture_destroy
;
489 screen
->get_tex_surface
= cell_get_tex_surface
;
490 screen
->tex_surface_destroy
= cell_tex_surface_destroy
;
492 screen
->get_tex_transfer
= cell_get_tex_transfer
;
493 screen
->tex_transfer_destroy
= cell_tex_transfer_destroy
;
495 screen
->transfer_map
= cell_transfer_map
;
496 screen
->transfer_unmap
= cell_transfer_unmap
;