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_util.h"
37 #include "pipe/p_winsys.h"
39 #include "cell_context.h"
40 #include "cell_state.h"
41 #include "cell_texture.h"
44 /* Simple, maximally packed layout.
47 static unsigned minify( unsigned d
)
54 cell_texture_layout(struct cell_texture
* spt
)
56 struct pipe_texture
*pt
= &spt
->base
;
58 unsigned width
= pt
->width
[0];
59 unsigned height
= pt
->height
[0];
60 unsigned depth
= pt
->depth
[0];
64 for ( level
= 0 ; level
<= pt
->last_level
; level
++ ) {
65 pt
->width
[level
] = width
;
66 pt
->height
[level
] = height
;
67 pt
->depth
[level
] = depth
;
68 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, width
);
69 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, height
);
71 spt
->level_offset
[level
] = spt
->buffer_size
;
73 spt
->buffer_size
+= (pt
->nblocksy
[level
] *
74 ((pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : depth
) *
75 pt
->nblocksx
[level
] * pt
->block
.size
;
77 width
= minify(width
);
78 height
= minify(height
);
79 depth
= minify(depth
);
84 static struct pipe_texture
*
85 cell_texture_create_screen(struct pipe_screen
*screen
,
86 const struct pipe_texture
*templat
)
88 struct pipe_winsys
*ws
= screen
->winsys
;
89 struct cell_texture
*spt
= CALLOC_STRUCT(cell_texture
);
94 spt
->base
.refcount
= 1;
95 spt
->base
.screen
= screen
;
97 cell_texture_layout(spt
);
99 spt
->buffer
= ws
->buffer_create(ws
, 32,
100 PIPE_BUFFER_USAGE_PIXEL
,
113 cell_texture_release_screen(struct pipe_screen
*screen
,
114 struct pipe_texture
**pt
)
120 DBG("%s %p refcount will be %d\n",
121 __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
123 if (--(*pt
)->refcount
<= 0) {
124 struct cell_texture
*spt
= cell_texture(*pt
);
127 DBG("%s deleting %p\n", __FUNCTION__, (void *) spt);
130 pipe_buffer_reference(screen
->winsys
, &spt
->buffer
, NULL
);
139 cell_texture_update(struct pipe_context
*pipe
, struct pipe_texture
*texture
,
140 uint face
, uint levelsMask
)
142 /* XXX TO DO: re-tile the texture data ... */
147 static struct pipe_surface
*
148 cell_get_tex_surface_screen(struct pipe_screen
*screen
,
149 struct pipe_texture
*pt
,
150 unsigned face
, unsigned level
, unsigned zslice
)
152 struct pipe_winsys
*ws
= screen
->winsys
;
153 struct cell_texture
*spt
= cell_texture(pt
);
154 struct pipe_surface
*ps
;
156 ps
= ws
->surface_alloc(ws
);
158 assert(ps
->refcount
);
160 pipe_buffer_reference(ws
, &ps
->buffer
, spt
->buffer
);
161 ps
->format
= pt
->format
;
162 ps
->block
= pt
->block
;
163 ps
->width
= pt
->width
[level
];
164 ps
->height
= pt
->height
[level
];
165 ps
->nblocksx
= pt
->nblocksx
[level
];
166 ps
->nblocksy
= pt
->nblocksy
[level
];
167 ps
->stride
= spt
->stride
[level
];
168 ps
->offset
= spt
->level_offset
[level
];
170 if (pt
->target
== PIPE_TEXTURE_CUBE
|| pt
->target
== PIPE_TEXTURE_3D
) {
171 ps
->offset
+= ((pt
->target
== PIPE_TEXTURE_CUBE
) ? face
: zslice
) *
185 tile_copy_data(uint w
, uint h
, uint tile_size
, uint
*dst
, const uint
*src
)
187 const uint tile_size2
= tile_size
* tile_size
;
188 const uint h_t
= h
/ tile_size
, w_t
= w
/ tile_size
;
190 uint it
, jt
; /* tile counters */
191 uint i
, j
; /* intra-tile counters */
193 /* loop over dest tiles */
194 for (it
= 0; it
< h_t
; it
++) {
195 for (jt
= 0; jt
< w_t
; jt
++) {
196 /* start of dest tile: */
197 uint
*tdst
= dst
+ (it
* w_t
+ jt
) * tile_size2
;
198 /* loop over texels in the tile */
199 for (i
= 0; i
< tile_size
; i
++) {
200 for (j
= 0; j
< tile_size
; j
++) {
201 const uint srci
= it
* tile_size
+ i
;
202 const uint srcj
= jt
* tile_size
+ j
;
203 *tdst
++ = src
[srci
* w
+ srcj
];
213 * Convert linear texture image data to tiled format for SPU usage.
216 cell_tile_texture(struct cell_context
*cell
,
217 struct cell_texture
*texture
)
219 struct pipe_screen
*screen
= cell
->pipe
.screen
;
220 uint face
= 0, level
= 0, zslice
= 0;
221 struct pipe_surface
*surf
;
222 const uint w
= texture
->base
.width
[0], h
= texture
->base
.height
[0];
225 /* temporary restrictions: */
226 assert(w
>= TILE_SIZE
);
227 assert(h
>= TILE_SIZE
);
228 assert(w
% TILE_SIZE
== 0);
229 assert(h
% TILE_SIZE
== 0);
231 surf
= screen
->get_tex_surface(screen
, &texture
->base
, face
, level
, zslice
);
234 src
= (const uint
*) pipe_surface_map(surf
);
236 if (texture
->tiled_data
) {
237 align_free(texture
->tiled_data
);
239 texture
->tiled_data
= align_malloc(w
* h
* 4, 16);
241 tile_copy_data(w
, h
, TILE_SIZE
, texture
->tiled_data
, src
);
243 pipe_surface_unmap(surf
);
245 pipe_surface_reference(&surf
, NULL
);
251 cell_update_texture_mapping(struct cell_context
*cell
)
253 uint face
= 0, level
= 0, zslice
= 0;
256 for (i
= 0; i
< CELL_MAX_SAMPLERS
; i
++) {
257 if (cell
->texture
[i
])
258 cell_tile_texture(cell
, cell
->texture
[i
]);
262 if (cell
->tex_surf
&& cell
->tex_map
) {
263 pipe_surface_unmap(cell
->tex_surf
);
264 cell
->tex_map
= NULL
;
267 /* XXX free old surface */
269 cell
->tex_surf
= cell_get_tex_surface(&cell
->pipe
,
270 &cell
->texture
[0]->base
,
271 face
, level
, zslice
);
273 cell
->tex_map
= pipe_surface_map(cell
->tex_surf
);
279 cell_init_texture_functions(struct cell_context
*cell
)
281 cell
->pipe
.texture_update
= cell_texture_update
;
285 cell_init_screen_texture_funcs(struct pipe_screen
*screen
)
287 screen
->texture_create
= cell_texture_create_screen
;
288 screen
->texture_release
= cell_texture_release_screen
;
289 screen
->get_tex_surface
= cell_get_tex_surface_screen
;