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"
55 cell_texture_layout(struct cell_texture
*ct
)
57 struct pipe_texture
*pt
= &ct
->base
;
59 unsigned width
= pt
->width
[0];
60 unsigned height
= pt
->height
[0];
61 unsigned depth
= pt
->depth
[0];
65 for (level
= 0; level
<= pt
->last_level
; level
++) {
67 unsigned w_tile
, h_tile
;
69 assert(level
< CELL_MAX_TEXTURE_LEVELS
);
71 /* width, height, rounded up to tile size */
72 w_tile
= align(width
, TILE_SIZE
);
73 h_tile
= align(height
, TILE_SIZE
);
75 pt
->width
[level
] = width
;
76 pt
->height
[level
] = height
;
77 pt
->depth
[level
] = depth
;
78 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, w_tile
);
79 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, h_tile
);
81 ct
->stride
[level
] = pt
->nblocksx
[level
] * pt
->block
.size
;
83 ct
->level_offset
[level
] = ct
->buffer_size
;
85 size
= pt
->nblocksx
[level
] * pt
->nblocksy
[level
] * pt
->block
.size
;
86 if (pt
->target
== PIPE_TEXTURE_CUBE
)
91 ct
->buffer_size
+= size
;
93 width
= minify(width
);
94 height
= minify(height
);
95 depth
= minify(depth
);
100 static struct pipe_texture
*
101 cell_texture_create(struct pipe_screen
*screen
,
102 const struct pipe_texture
*templat
)
104 struct cell_texture
*ct
= CALLOC_STRUCT(cell_texture
);
109 pipe_reference_init(&ct
->base
.reference
, 1);
110 ct
->base
.screen
= screen
;
112 cell_texture_layout(ct
);
114 ct
->buffer
= screen
->buffer_create(screen
, 32, PIPE_BUFFER_USAGE_PIXEL
,
127 cell_texture_destroy(struct pipe_texture
*pt
)
129 struct cell_texture
*ct
= cell_texture(pt
);
132 pipe_buffer_unmap(ct
->buffer
->screen
, ct
->buffer
);
136 pipe_buffer_reference(&ct
->buffer
, NULL
);
144 * Convert image from linear layout to tiled layout. 4-byte pixels.
147 twiddle_image_uint(uint w
, uint h
, uint tile_size
, uint
*dst
,
148 uint src_stride
, const uint
*src
)
150 const uint tile_size2
= tile_size
* tile_size
;
151 const uint h_t
= (h
+ tile_size
- 1) / tile_size
;
152 const uint w_t
= (w
+ tile_size
- 1) / tile_size
;
154 uint it
, jt
; /* tile counters */
155 uint i
, j
; /* intra-tile counters */
157 src_stride
/= 4; /* convert from bytes to pixels */
159 /* loop over dest tiles */
160 for (it
= 0; it
< h_t
; it
++) {
161 for (jt
= 0; jt
< w_t
; jt
++) {
162 /* start of dest tile: */
163 uint
*tdst
= dst
+ (it
* w_t
+ jt
) * tile_size2
;
165 /* compute size of this tile (may be smaller than tile_size) */
166 /* XXX note: a compiler bug was found here. That's why the code
169 uint tile_width
= w
- jt
* tile_size
;
170 tile_width
= MIN2(tile_width
, tile_size
);
171 uint tile_height
= h
- it
* tile_size
;
172 tile_height
= MIN2(tile_height
, tile_size
);
174 /* loop over texels in the tile */
175 for (i
= 0; i
< tile_height
; i
++) {
176 for (j
= 0; j
< tile_width
; j
++) {
177 const uint srci
= it
* tile_size
+ i
;
178 const uint srcj
= jt
* tile_size
+ j
;
181 tdst
[i
* tile_size
+ j
] = src
[srci
* src_stride
+ srcj
];
190 * For Cell. Basically, rearrange the pixels/quads from this layout:
203 twiddle_tile(const uint
*tileIn
, uint
*tileOut
)
207 for (y
= 0; y
< TILE_SIZE
; y
+=2) {
208 for (x
= 0; x
< TILE_SIZE
; x
+=2) {
209 int k
= 4 * (y
/2 * TILE_SIZE
/2 + x
/2);
210 tileOut
[y
* TILE_SIZE
+ (x
+ 0)] = tileIn
[k
];
211 tileOut
[y
* TILE_SIZE
+ (x
+ 1)] = tileIn
[k
+1];
212 tileOut
[(y
+ 1) * TILE_SIZE
+ (x
+ 0)] = tileIn
[k
+2];
213 tileOut
[(y
+ 1) * TILE_SIZE
+ (x
+ 1)] = tileIn
[k
+3];
220 * Convert image from tiled layout to linear layout. 4-byte pixels.
223 untwiddle_image_uint(uint w
, uint h
, uint tile_size
, uint
*dst
,
224 uint dst_stride
, const uint
*src
)
226 const uint tile_size2
= tile_size
* tile_size
;
227 const uint h_t
= (h
+ tile_size
- 1) / tile_size
;
228 const uint w_t
= (w
+ tile_size
- 1) / tile_size
;
230 uint it
, jt
; /* tile counters */
231 uint i
, j
; /* intra-tile counters */
233 dst_stride
/= 4; /* convert from bytes to pixels */
235 tile_buf
= align_malloc(tile_size
* tile_size
* 4, 16);
237 /* loop over src tiles */
238 for (it
= 0; it
< h_t
; it
++) {
239 for (jt
= 0; jt
< w_t
; jt
++) {
240 /* start of src tile: */
241 const uint
*tsrc
= src
+ (it
* w_t
+ jt
) * tile_size2
;
243 twiddle_tile(tsrc
, tile_buf
);
246 /* compute size of this tile (may be smaller than tile_size) */
247 /* XXX note: a compiler bug was found here. That's why the code
250 uint tile_width
= w
- jt
* tile_size
;
251 tile_width
= MIN2(tile_width
, tile_size
);
252 uint tile_height
= h
- it
* tile_size
;
253 tile_height
= MIN2(tile_height
, tile_size
);
255 /* loop over texels in the tile */
256 for (i
= 0; i
< tile_height
; i
++) {
257 for (j
= 0; j
< tile_width
; j
++) {
258 uint dsti
= it
* tile_size
+ i
;
259 uint dstj
= jt
* tile_size
+ j
;
262 dst
[dsti
* dst_stride
+ dstj
] = tsrc
[i
* tile_size
+ j
];
268 align_free(tile_buf
);
272 static struct pipe_surface
*
273 cell_get_tex_surface(struct pipe_screen
*screen
,
274 struct pipe_texture
*pt
,
275 unsigned face
, unsigned level
, unsigned zslice
,
278 struct cell_texture
*ct
= cell_texture(pt
);
279 struct pipe_surface
*ps
;
281 ps
= CALLOC_STRUCT(pipe_surface
);
283 pipe_reference_init(&ps
->reference
, 1);
284 pipe_texture_reference(&ps
->texture
, pt
);
285 ps
->format
= pt
->format
;
286 ps
->width
= pt
->width
[level
];
287 ps
->height
= pt
->height
[level
];
288 ps
->offset
= ct
->level_offset
[level
];
289 /* XXX may need to override usage flags (see sp_texture.c) */
295 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
296 ps
->offset
+= face
* pt
->nblocksy
[level
] * ct
->stride
[level
];
298 else if (pt
->target
== PIPE_TEXTURE_3D
) {
299 ps
->offset
+= zslice
* pt
->nblocksy
[level
] * ct
->stride
[level
];
311 cell_tex_surface_destroy(struct pipe_surface
*surf
)
313 pipe_texture_reference(&surf
->texture
, NULL
);
319 * Create new pipe_transfer object.
320 * This is used by the user to put tex data into a texture (and get it
321 * back out for glGetTexImage).
323 static struct pipe_transfer
*
324 cell_get_tex_transfer(struct pipe_screen
*screen
,
325 struct pipe_texture
*texture
,
326 unsigned face
, unsigned level
, unsigned zslice
,
327 enum pipe_transfer_usage usage
,
328 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
330 struct cell_texture
*ct
= cell_texture(texture
);
331 struct cell_transfer
*ctrans
;
334 assert(level
<= texture
->last_level
);
336 ctrans
= CALLOC_STRUCT(cell_transfer
);
338 struct pipe_transfer
*pt
= &ctrans
->base
;
339 pipe_texture_reference(&pt
->texture
, texture
);
340 pt
->format
= texture
->format
;
341 pt
->block
= texture
->block
;
346 pt
->nblocksx
= texture
->nblocksx
[level
];
347 pt
->nblocksy
= texture
->nblocksy
[level
];
348 pt
->stride
= ct
->stride
[level
];
354 ctrans
->offset
= ct
->level_offset
[level
];
356 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
357 ctrans
->offset
+= face
* pt
->nblocksy
* pt
->stride
;
359 else if (texture
->target
== PIPE_TEXTURE_3D
) {
360 ctrans
->offset
+= zslice
* pt
->nblocksy
* pt
->stride
;
373 cell_tex_transfer_destroy(struct pipe_transfer
*t
)
375 struct cell_transfer
*transfer
= cell_transfer(t
);
376 /* Effectively do the texture_update work here - if texture images
377 * needed post-processing to put them into hardware layout, this is
378 * where it would happen. For cell, nothing to do.
380 assert (transfer
->base
.texture
);
381 pipe_texture_reference(&transfer
->base
.texture
, NULL
);
387 * Return pointer to texture image data in linear layout.
390 cell_transfer_map(struct pipe_screen
*screen
, struct pipe_transfer
*transfer
)
392 struct cell_transfer
*ctrans
= cell_transfer(transfer
);
393 struct pipe_texture
*pt
= transfer
->texture
;
394 struct cell_texture
*ct
= cell_texture(pt
);
395 const uint level
= ctrans
->base
.level
;
396 const uint texWidth
= pt
->width
[level
];
397 const uint texHeight
= pt
->height
[level
];
398 const uint stride
= ct
->stride
[level
];
399 unsigned flags
= 0x0;
402 assert(transfer
->texture
);
404 if (transfer
->usage
!= PIPE_TRANSFER_READ
) {
405 flags
|= PIPE_BUFFER_USAGE_CPU_WRITE
;
408 if (transfer
->usage
!= PIPE_TRANSFER_WRITE
) {
409 flags
|= PIPE_BUFFER_USAGE_CPU_READ
;
414 ct
->mapped
= pipe_buffer_map(screen
, ct
->buffer
, flags
);
418 * Create a buffer of ordinary memory for the linear texture.
419 * This is the memory that the user will read/write.
421 size
= pt
->nblocksx
[level
] * pt
->nblocksy
[level
] * pt
->block
.size
;
423 ctrans
->map
= align_malloc(size
, 16);
425 return NULL
; /* out of memory */
427 if (transfer
->usage
& PIPE_TRANSFER_READ
) {
428 /* need to untwiddle the texture to make a linear version */
429 const uint bpp
= pf_get_size(ct
->base
.format
);
431 const uint
*src
= (uint
*) (ct
->mapped
+ ctrans
->offset
);
432 uint
*dst
= ctrans
->map
;
433 untwiddle_image_uint(texWidth
, texHeight
, TILE_SIZE
,
446 * Called when user is done reading/writing texture data.
447 * If new data was written, this is where we convert the linear data
451 cell_transfer_unmap(struct pipe_screen
*screen
,
452 struct pipe_transfer
*transfer
)
454 struct cell_transfer
*ctrans
= cell_transfer(transfer
);
455 struct pipe_texture
*pt
= transfer
->texture
;
456 struct cell_texture
*ct
= cell_texture(pt
);
457 const uint level
= ctrans
->base
.level
;
458 const uint texWidth
= pt
->width
[level
];
459 const uint texHeight
= pt
->height
[level
];
460 const uint stride
= ct
->stride
[level
];
464 ct
->mapped
= pipe_buffer_map(screen
, ct
->buffer
,
465 PIPE_BUFFER_USAGE_CPU_READ
);
468 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
469 /* The user wrote new texture data into the mapped buffer.
470 * We need to convert the new linear data into the twiddled/tiled format.
472 const uint bpp
= pf_get_size(ct
->base
.format
);
474 const uint
*src
= ctrans
->map
;
475 uint
*dst
= (uint
*) (ct
->mapped
+ ctrans
->offset
);
476 twiddle_image_uint(texWidth
, texHeight
, TILE_SIZE
, dst
, stride
, src
);
483 align_free(ctrans
->map
);
489 cell_init_screen_texture_funcs(struct pipe_screen
*screen
)
491 screen
->texture_create
= cell_texture_create
;
492 screen
->texture_destroy
= cell_texture_destroy
;
494 screen
->get_tex_surface
= cell_get_tex_surface
;
495 screen
->tex_surface_destroy
= cell_tex_surface_destroy
;
497 screen
->get_tex_transfer
= cell_get_tex_transfer
;
498 screen
->tex_transfer_destroy
= cell_tex_transfer_destroy
;
500 screen
->transfer_map
= cell_transfer_map
;
501 screen
->transfer_unmap
= cell_transfer_unmap
;