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>
33 #include "pipe/p_context.h"
34 #include "pipe/p_defines.h"
35 #include "pipe/p_inlines.h"
36 #include "pipe/p_winsys.h"
37 #include "util/u_math.h"
38 #include "util/u_memory.h"
40 #include "cell_context.h"
41 #include "cell_state.h"
42 #include "cell_texture.h"
45 /* Simple, maximally packed layout.
48 static unsigned minify( unsigned d
)
55 cell_texture_layout(struct cell_texture
* spt
)
57 struct pipe_texture
*pt
= &spt
->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
++ ) {
66 pt
->width
[level
] = width
;
67 pt
->height
[level
] = height
;
68 pt
->depth
[level
] = depth
;
69 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, width
);
70 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, height
);
72 spt
->stride
[level
] = pt
->nblocksx
[level
] * pt
->block
.size
;
74 spt
->level_offset
[level
] = spt
->buffer_size
;
76 spt
->buffer_size
+= (pt
->nblocksy
[level
] *
77 ((pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : depth
) *
78 pt
->nblocksx
[level
] * pt
->block
.size
);
80 width
= minify(width
);
81 height
= minify(height
);
82 depth
= minify(depth
);
87 static struct pipe_texture
*
88 cell_texture_create(struct pipe_screen
*screen
,
89 const struct pipe_texture
*templat
)
91 struct pipe_winsys
*ws
= screen
->winsys
;
92 struct cell_texture
*spt
= CALLOC_STRUCT(cell_texture
);
97 spt
->base
.refcount
= 1;
98 spt
->base
.screen
= screen
;
100 cell_texture_layout(spt
);
102 spt
->buffer
= ws
->buffer_create(ws
, 32,
103 PIPE_BUFFER_USAGE_PIXEL
,
116 cell_texture_release(struct pipe_screen
*screen
,
117 struct pipe_texture
**pt
)
123 DBG("%s %p refcount will be %d\n",
124 __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
126 if (--(*pt
)->refcount
<= 0) {
127 struct cell_texture
*spt
= cell_texture(*pt
);
130 DBG("%s deleting %p\n", __FUNCTION__, (void *) spt);
133 pipe_buffer_reference(screen
, &spt
->buffer
, NULL
);
142 cell_texture_update(struct pipe_context
*pipe
, struct pipe_texture
*texture
,
143 uint face
, uint levelsMask
)
145 /* XXX TO DO: re-tile the texture data ... */
150 static struct pipe_surface
*
151 cell_get_tex_surface(struct pipe_screen
*screen
,
152 struct pipe_texture
*pt
,
153 unsigned face
, unsigned level
, unsigned zslice
,
156 struct pipe_winsys
*ws
= screen
->winsys
;
157 struct cell_texture
*spt
= cell_texture(pt
);
158 struct pipe_surface
*ps
;
160 ps
= ws
->surface_alloc(ws
);
162 assert(ps
->refcount
);
164 winsys_buffer_reference(ws
, &ps
->buffer
, spt
->buffer
);
165 ps
->format
= pt
->format
;
166 ps
->block
= pt
->block
;
167 ps
->width
= pt
->width
[level
];
168 ps
->height
= pt
->height
[level
];
169 ps
->nblocksx
= pt
->nblocksx
[level
];
170 ps
->nblocksy
= pt
->nblocksy
[level
];
171 ps
->stride
= spt
->stride
[level
];
172 ps
->offset
= spt
->level_offset
[level
];
175 /* XXX may need to override usage flags (see sp_texture.c) */
178 if (pt
->target
== PIPE_TEXTURE_CUBE
|| pt
->target
== PIPE_TEXTURE_3D
) {
179 ps
->offset
+= ((pt
->target
== PIPE_TEXTURE_CUBE
) ? face
: zslice
) *
193 tile_copy_data(uint w
, uint h
, uint tile_size
, uint
*dst
, const uint
*src
)
195 const uint tile_size2
= tile_size
* tile_size
;
196 const uint h_t
= h
/ tile_size
, w_t
= w
/ tile_size
;
198 uint it
, jt
; /* tile counters */
199 uint i
, j
; /* intra-tile counters */
201 /* loop over dest tiles */
202 for (it
= 0; it
< h_t
; it
++) {
203 for (jt
= 0; jt
< w_t
; jt
++) {
204 /* start of dest tile: */
205 uint
*tdst
= dst
+ (it
* w_t
+ jt
) * tile_size2
;
206 /* loop over texels in the tile */
207 for (i
= 0; i
< tile_size
; i
++) {
208 for (j
= 0; j
< tile_size
; j
++) {
209 const uint srci
= it
* tile_size
+ i
;
210 const uint srcj
= jt
* tile_size
+ j
;
211 *tdst
++ = src
[srci
* w
+ srcj
];
221 * Convert linear texture image data to tiled format for SPU usage.
224 cell_tile_texture(struct cell_context
*cell
,
225 struct cell_texture
*texture
)
227 struct pipe_screen
*screen
= cell
->pipe
.screen
;
228 uint face
= 0, level
= 0, zslice
= 0;
229 struct pipe_surface
*surf
;
230 const uint w
= texture
->base
.width
[0], h
= texture
->base
.height
[0];
233 /* temporary restrictions: */
234 assert(w
>= TILE_SIZE
);
235 assert(h
>= TILE_SIZE
);
236 assert(w
% TILE_SIZE
== 0);
237 assert(h
% TILE_SIZE
== 0);
239 surf
= screen
->get_tex_surface(screen
, &texture
->base
, face
, level
, zslice
,
240 PIPE_BUFFER_USAGE_CPU_WRITE
);
243 src
= (const uint
*) pipe_surface_map(surf
, PIPE_BUFFER_USAGE_CPU_WRITE
);
245 if (texture
->tiled_data
) {
246 align_free(texture
->tiled_data
);
248 texture
->tiled_data
= align_malloc(w
* h
* 4, 16);
250 tile_copy_data(w
, h
, TILE_SIZE
, texture
->tiled_data
, src
);
252 pipe_surface_unmap(surf
);
254 pipe_surface_reference(&surf
, NULL
);
259 cell_update_texture_mapping(struct cell_context
*cell
)
262 uint face
= 0, level
= 0, zslice
= 0;
266 for (i
= 0; i
< CELL_MAX_SAMPLERS
; i
++) {
267 if (cell
->texture
[i
])
268 cell_tile_texture(cell
, cell
->texture
[i
]);
272 if (cell
->tex_surf
&& cell
->tex_map
) {
273 pipe_surface_unmap(cell
->tex_surf
);
274 cell
->tex_map
= NULL
;
277 /* XXX free old surface */
279 cell
->tex_surf
= cell_get_tex_surface(&cell
->pipe
,
280 &cell
->texture
[0]->base
,
281 face
, level
, zslice
);
283 cell
->tex_map
= pipe_surface_map(cell
->tex_surf
);
289 cell_tex_surface_release(struct pipe_screen
*screen
,
290 struct pipe_surface
**s
)
292 /* Effectively do the texture_update work here - if texture images
293 * needed post-processing to put them into hardware layout, this is
294 * where it would happen. For softpipe, nothing to do.
296 assert ((*s
)->texture
);
297 pipe_texture_reference(&(*s
)->texture
, NULL
);
299 screen
->winsys
->surface_release(screen
->winsys
, s
);
304 cell_surface_map( struct pipe_screen
*screen
,
305 struct pipe_surface
*surface
,
310 if (flags
& ~surface
->usage
) {
315 map
= screen
->winsys
->buffer_map( screen
->winsys
, surface
->buffer
, flags
);
319 /* May want to different things here depending on read/write nature
322 if (surface
->texture
&&
323 (flags
& PIPE_BUFFER_USAGE_CPU_WRITE
))
325 /* Do something to notify sharing contexts of a texture change.
326 * In softpipe, that would mean flushing the texture cache.
329 cell_screen(screen
)->timestamp
++;
333 return map
+ surface
->offset
;
338 cell_surface_unmap(struct pipe_screen
*screen
,
339 struct pipe_surface
*surface
)
341 screen
->winsys
->buffer_unmap( screen
->winsys
, surface
->buffer
);
346 cell_init_texture_functions(struct cell_context
*cell
)
348 /*cell->pipe.texture_update = cell_texture_update;*/
352 cell_init_screen_texture_funcs(struct pipe_screen
*screen
)
354 screen
->texture_create
= cell_texture_create
;
355 screen
->texture_release
= cell_texture_release
;
357 screen
->get_tex_surface
= cell_get_tex_surface
;
358 screen
->tex_surface_release
= cell_tex_surface_release
;
360 screen
->surface_map
= cell_surface_map
;
361 screen
->surface_unmap
= cell_surface_unmap
;