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
++ ) {
67 unsigned w_tile
, h_tile
;
69 /* width, height, rounded up to tile size */
70 w_tile
= align(width
, TILE_SIZE
);
71 h_tile
= align(height
, TILE_SIZE
);
73 pt
->width
[level
] = width
;
74 pt
->height
[level
] = height
;
75 pt
->depth
[level
] = depth
;
76 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, w_tile
);
77 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, h_tile
);
79 spt
->stride
[level
] = pt
->nblocksx
[level
] * pt
->block
.size
;
81 spt
->level_offset
[level
] = spt
->buffer_size
;
83 size
= pt
->nblocksx
[level
] * pt
->nblocksy
[level
] * pt
->block
.size
;
84 if (pt
->target
== PIPE_TEXTURE_CUBE
)
89 spt
->buffer_size
+= size
;
91 width
= minify(width
);
92 height
= minify(height
);
93 depth
= minify(depth
);
98 static struct pipe_texture
*
99 cell_texture_create(struct pipe_screen
*screen
,
100 const struct pipe_texture
*templat
)
102 struct pipe_winsys
*ws
= screen
->winsys
;
103 struct cell_texture
*spt
= CALLOC_STRUCT(cell_texture
);
107 spt
->base
= *templat
;
108 spt
->base
.refcount
= 1;
109 spt
->base
.screen
= screen
;
111 cell_texture_layout(spt
);
113 spt
->buffer
= ws
->buffer_create(ws
, 32,
114 PIPE_BUFFER_USAGE_PIXEL
,
127 cell_texture_release(struct pipe_screen
*screen
,
128 struct pipe_texture
**pt
)
134 DBG("%s %p refcount will be %d\n",
135 __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
137 if (--(*pt
)->refcount
<= 0) {
138 struct cell_texture
*spt
= cell_texture(*pt
);
141 DBG("%s deleting %p\n", __FUNCTION__, (void *) spt);
144 pipe_buffer_reference(screen
, &spt
->buffer
, NULL
);
154 cell_texture_update(struct pipe_context
*pipe
, struct pipe_texture
*texture
,
155 uint face
, uint levelsMask
)
157 /* XXX TO DO: re-tile the texture data ... */
163 static struct pipe_surface
*
164 cell_get_tex_surface(struct pipe_screen
*screen
,
165 struct pipe_texture
*pt
,
166 unsigned face
, unsigned level
, unsigned zslice
,
169 struct pipe_winsys
*ws
= screen
->winsys
;
170 struct cell_texture
*spt
= cell_texture(pt
);
171 struct pipe_surface
*ps
;
173 ps
= ws
->surface_alloc(ws
);
175 assert(ps
->refcount
);
177 winsys_buffer_reference(ws
, &ps
->buffer
, spt
->buffer
);
178 ps
->format
= pt
->format
;
179 ps
->block
= pt
->block
;
180 ps
->width
= pt
->width
[level
];
181 ps
->height
= pt
->height
[level
];
182 ps
->nblocksx
= pt
->nblocksx
[level
];
183 ps
->nblocksy
= pt
->nblocksy
[level
];
184 ps
->stride
= spt
->stride
[level
];
185 ps
->offset
= spt
->level_offset
[level
];
188 /* XXX may need to override usage flags (see sp_texture.c) */
190 pipe_texture_reference(&ps
->texture
, pt
);
195 if (pt
->target
== PIPE_TEXTURE_CUBE
|| pt
->target
== PIPE_TEXTURE_3D
) {
196 ps
->offset
+= ((pt
->target
== PIPE_TEXTURE_CUBE
) ? face
: zslice
) *
211 * Copy tile data from linear layout to tiled layout.
212 * XXX this should be rolled into the future surface-creation code.
213 * XXX also need "untile" code...
216 tile_copy_data(uint w
, uint h
, uint tile_size
, uint
*dst
, const uint
*src
)
218 const uint tile_size2
= tile_size
* tile_size
;
219 const uint h_t
= h
/ tile_size
, w_t
= w
/ tile_size
;
221 uint it
, jt
; /* tile counters */
222 uint i
, j
; /* intra-tile counters */
224 /* loop over dest tiles */
225 for (it
= 0; it
< h_t
; it
++) {
226 for (jt
= 0; jt
< w_t
; jt
++) {
227 /* start of dest tile: */
228 uint
*tdst
= dst
+ (it
* w_t
+ jt
) * tile_size2
;
229 /* loop over texels in the tile */
230 for (i
= 0; i
< tile_size
; i
++) {
231 for (j
= 0; j
< tile_size
; j
++) {
232 const uint srci
= it
* tile_size
+ i
;
233 const uint srcj
= jt
* tile_size
+ j
;
234 *tdst
++ = src
[srci
* w
+ srcj
];
244 * Convert linear texture image data to tiled format for SPU usage.
245 * XXX recast this in terms of pipe_surfaces (aka texture views).
248 cell_tile_texture(struct cell_context
*cell
,
249 struct cell_texture
*texture
)
251 struct pipe_screen
*screen
= cell
->pipe
.screen
;
252 uint face
= 0, level
= 0, zslice
= 0;
253 struct pipe_surface
*surf
;
254 const uint w
= texture
->base
.width
[0], h
= texture
->base
.height
[0];
257 /* temporary restrictions: */
258 assert(w
>= TILE_SIZE
);
259 assert(h
>= TILE_SIZE
);
260 assert(w
% TILE_SIZE
== 0);
261 assert(h
% TILE_SIZE
== 0);
263 surf
= screen
->get_tex_surface(screen
, &texture
->base
, face
, level
, zslice
,
264 PIPE_BUFFER_USAGE_CPU_WRITE
);
267 src
= (const uint
*) pipe_surface_map(surf
, PIPE_BUFFER_USAGE_CPU_WRITE
);
269 if (texture
->tiled_data
) {
270 align_free(texture
->tiled_data
);
272 texture
->tiled_data
= align_malloc(w
* h
* 4, 16);
274 tile_copy_data(w
, h
, TILE_SIZE
, texture
->tiled_data
, src
);
276 pipe_surface_unmap(surf
);
278 pipe_surface_reference(&surf
, NULL
);
283 cell_update_texture_mapping(struct cell_context
*cell
)
286 uint face
= 0, level
= 0, zslice
= 0;
290 for (i
= 0; i
< CELL_MAX_SAMPLERS
; i
++) {
291 if (cell
->texture
[i
])
292 cell_tile_texture(cell
, cell
->texture
[i
]);
296 if (cell
->tex_surf
&& cell
->tex_map
) {
297 pipe_surface_unmap(cell
->tex_surf
);
298 cell
->tex_map
= NULL
;
301 /* XXX free old surface */
303 cell
->tex_surf
= cell_get_tex_surface(&cell
->pipe
,
304 &cell
->texture
[0]->base
,
305 face
, level
, zslice
);
307 cell
->tex_map
= pipe_surface_map(cell
->tex_surf
);
313 cell_tex_surface_release(struct pipe_screen
*screen
,
314 struct pipe_surface
**s
)
316 /* Effectively do the texture_update work here - if texture images
317 * needed post-processing to put them into hardware layout, this is
318 * where it would happen. For softpipe, nothing to do.
320 assert ((*s
)->texture
);
321 pipe_texture_reference(&(*s
)->texture
, NULL
);
323 screen
->winsys
->surface_release(screen
->winsys
, s
);
328 cell_surface_map( struct pipe_screen
*screen
,
329 struct pipe_surface
*surface
,
334 if (flags
& ~surface
->usage
) {
339 map
= pipe_buffer_map( screen
, surface
->buffer
, flags
);
343 /* May want to different things here depending on read/write nature
346 if (surface
->texture
&&
347 (flags
& PIPE_BUFFER_USAGE_CPU_WRITE
))
349 /* Do something to notify sharing contexts of a texture change.
350 * In softpipe, that would mean flushing the texture cache.
353 cell_screen(screen
)->timestamp
++;
357 return map
+ surface
->offset
;
362 cell_surface_unmap(struct pipe_screen
*screen
,
363 struct pipe_surface
*surface
)
365 pipe_buffer_unmap( screen
, surface
->buffer
);
370 cell_init_texture_functions(struct cell_context
*cell
)
372 /*cell->pipe.texture_update = cell_texture_update;*/
377 cell_init_screen_texture_funcs(struct pipe_screen
*screen
)
379 screen
->texture_create
= cell_texture_create
;
380 screen
->texture_release
= cell_texture_release
;
382 screen
->get_tex_surface
= cell_get_tex_surface
;
383 screen
->tex_surface_release
= cell_tex_surface_release
;
385 screen
->surface_map
= cell_surface_map
;
386 screen
->surface_unmap
= cell_surface_unmap
;