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 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, w_tile
);
69 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, h_tile
);
71 ct
->stride
[level
] = pt
->nblocksx
[level
] * pt
->block
.size
;
73 ct
->level_offset
[level
] = ct
->buffer_size
;
75 size
= pt
->nblocksx
[level
] * pt
->nblocksy
[level
] * pt
->block
.size
;
76 if (pt
->target
== PIPE_TEXTURE_CUBE
)
81 ct
->buffer_size
+= size
;
83 width
= u_minify(width
, 1);
84 height
= u_minify(height
, 1);
85 depth
= u_minify(depth
, 1);
90 static struct pipe_texture
*
91 cell_texture_create(struct pipe_screen
*screen
,
92 const struct pipe_texture
*templat
)
94 struct cell_texture
*ct
= CALLOC_STRUCT(cell_texture
);
99 pipe_reference_init(&ct
->base
.reference
, 1);
100 ct
->base
.screen
= screen
;
102 cell_texture_layout(ct
);
104 ct
->buffer
= screen
->buffer_create(screen
, 32, PIPE_BUFFER_USAGE_PIXEL
,
117 cell_texture_destroy(struct pipe_texture
*pt
)
119 struct cell_texture
*ct
= cell_texture(pt
);
122 pipe_buffer_unmap(ct
->buffer
->screen
, ct
->buffer
);
126 pipe_buffer_reference(&ct
->buffer
, NULL
);
134 * Convert image from linear layout to tiled layout. 4-byte pixels.
137 twiddle_image_uint(uint w
, uint h
, uint tile_size
, uint
*dst
,
138 uint src_stride
, const uint
*src
)
140 const uint tile_size2
= tile_size
* tile_size
;
141 const uint h_t
= (h
+ tile_size
- 1) / tile_size
;
142 const uint w_t
= (w
+ tile_size
- 1) / tile_size
;
144 uint it
, jt
; /* tile counters */
145 uint i
, j
; /* intra-tile counters */
147 src_stride
/= 4; /* convert from bytes to pixels */
149 /* loop over dest tiles */
150 for (it
= 0; it
< h_t
; it
++) {
151 for (jt
= 0; jt
< w_t
; jt
++) {
152 /* start of dest tile: */
153 uint
*tdst
= dst
+ (it
* w_t
+ jt
) * tile_size2
;
155 /* compute size of this tile (may be smaller than tile_size) */
156 /* XXX note: a compiler bug was found here. That's why the code
159 uint tile_width
= w
- jt
* tile_size
;
160 tile_width
= MIN2(tile_width
, tile_size
);
161 uint tile_height
= h
- it
* tile_size
;
162 tile_height
= MIN2(tile_height
, tile_size
);
164 /* loop over texels in the tile */
165 for (i
= 0; i
< tile_height
; i
++) {
166 for (j
= 0; j
< tile_width
; j
++) {
167 const uint srci
= it
* tile_size
+ i
;
168 const uint srcj
= jt
* tile_size
+ j
;
171 tdst
[i
* tile_size
+ j
] = src
[srci
* src_stride
+ srcj
];
180 * For Cell. Basically, rearrange the pixels/quads from this layout:
193 twiddle_tile(const uint
*tileIn
, uint
*tileOut
)
197 for (y
= 0; y
< TILE_SIZE
; y
+=2) {
198 for (x
= 0; x
< TILE_SIZE
; x
+=2) {
199 int k
= 4 * (y
/2 * TILE_SIZE
/2 + x
/2);
200 tileOut
[y
* TILE_SIZE
+ (x
+ 0)] = tileIn
[k
];
201 tileOut
[y
* TILE_SIZE
+ (x
+ 1)] = tileIn
[k
+1];
202 tileOut
[(y
+ 1) * TILE_SIZE
+ (x
+ 0)] = tileIn
[k
+2];
203 tileOut
[(y
+ 1) * TILE_SIZE
+ (x
+ 1)] = tileIn
[k
+3];
210 * Convert image from tiled layout to linear layout. 4-byte pixels.
213 untwiddle_image_uint(uint w
, uint h
, uint tile_size
, uint
*dst
,
214 uint dst_stride
, const uint
*src
)
216 const uint tile_size2
= tile_size
* tile_size
;
217 const uint h_t
= (h
+ tile_size
- 1) / tile_size
;
218 const uint w_t
= (w
+ tile_size
- 1) / tile_size
;
220 uint it
, jt
; /* tile counters */
221 uint i
, j
; /* intra-tile counters */
223 dst_stride
/= 4; /* convert from bytes to pixels */
225 tile_buf
= align_malloc(tile_size
* tile_size
* 4, 16);
227 /* loop over src tiles */
228 for (it
= 0; it
< h_t
; it
++) {
229 for (jt
= 0; jt
< w_t
; jt
++) {
230 /* start of src tile: */
231 const uint
*tsrc
= src
+ (it
* w_t
+ jt
) * tile_size2
;
233 twiddle_tile(tsrc
, tile_buf
);
236 /* compute size of this tile (may be smaller than tile_size) */
237 /* XXX note: a compiler bug was found here. That's why the code
240 uint tile_width
= w
- jt
* tile_size
;
241 tile_width
= MIN2(tile_width
, tile_size
);
242 uint tile_height
= h
- it
* tile_size
;
243 tile_height
= MIN2(tile_height
, tile_size
);
245 /* loop over texels in the tile */
246 for (i
= 0; i
< tile_height
; i
++) {
247 for (j
= 0; j
< tile_width
; j
++) {
248 uint dsti
= it
* tile_size
+ i
;
249 uint dstj
= jt
* tile_size
+ j
;
252 dst
[dsti
* dst_stride
+ dstj
] = tsrc
[i
* tile_size
+ j
];
258 align_free(tile_buf
);
262 static struct pipe_surface
*
263 cell_get_tex_surface(struct pipe_screen
*screen
,
264 struct pipe_texture
*pt
,
265 unsigned face
, unsigned level
, unsigned zslice
,
268 struct cell_texture
*ct
= cell_texture(pt
);
269 struct pipe_surface
*ps
;
271 ps
= CALLOC_STRUCT(pipe_surface
);
273 pipe_reference_init(&ps
->reference
, 1);
274 pipe_texture_reference(&ps
->texture
, pt
);
275 ps
->format
= pt
->format
;
276 ps
->width
= u_minify(pt
->width0
, level
);
277 ps
->height
= u_minify(pt
->height0
, level
);
278 ps
->offset
= ct
->level_offset
[level
];
279 /* XXX may need to override usage flags (see sp_texture.c) */
285 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
286 ps
->offset
+= face
* pt
->nblocksy
[level
] * ct
->stride
[level
];
288 else if (pt
->target
== PIPE_TEXTURE_3D
) {
289 ps
->offset
+= zslice
* pt
->nblocksy
[level
] * 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
);
330 pt
->format
= texture
->format
;
331 pt
->block
= texture
->block
;
336 pt
->nblocksx
= texture
->nblocksx
[level
];
337 pt
->nblocksy
= texture
->nblocksy
[level
];
338 pt
->stride
= ct
->stride
[level
];
344 ctrans
->offset
= ct
->level_offset
[level
];
346 if (texture
->target
== PIPE_TEXTURE_CUBE
) {
347 ctrans
->offset
+= face
* pt
->nblocksy
* pt
->stride
;
349 else if (texture
->target
== PIPE_TEXTURE_3D
) {
350 ctrans
->offset
+= zslice
* pt
->nblocksy
* pt
->stride
;
363 cell_tex_transfer_destroy(struct pipe_transfer
*t
)
365 struct cell_transfer
*transfer
= cell_transfer(t
);
366 /* Effectively do the texture_update work here - if texture images
367 * needed post-processing to put them into hardware layout, this is
368 * where it would happen. For cell, nothing to do.
370 assert (transfer
->base
.texture
);
371 pipe_texture_reference(&transfer
->base
.texture
, NULL
);
377 * Return pointer to texture image data in linear layout.
380 cell_transfer_map(struct pipe_screen
*screen
, struct pipe_transfer
*transfer
)
382 struct cell_transfer
*ctrans
= cell_transfer(transfer
);
383 struct pipe_texture
*pt
= transfer
->texture
;
384 struct cell_texture
*ct
= cell_texture(pt
);
385 const uint level
= ctrans
->base
.level
;
386 const uint texWidth
= u_minify(pt
->width0
, level
);
387 const uint texHeight
= u_minify(pt
->height0
, level
);
388 const uint stride
= ct
->stride
[level
];
391 assert(transfer
->texture
);
395 ct
->mapped
= pipe_buffer_map(screen
, ct
->buffer
,
396 pipe_transfer_buffer_flags(transfer
));
400 * Create a buffer of ordinary memory for the linear texture.
401 * This is the memory that the user will read/write.
403 size
= pt
->nblocksx
[level
] * pt
->nblocksy
[level
] * pt
->block
.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
= pf_get_size(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
= pf_get_size(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
;