gallium: Have pipe_buffer_* receive a pipe_screen instead of a pipe_context.
[mesa.git] / src / gallium / drivers / cell / ppu / cell_texture.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Michel Dänzer <michel@tungstengraphics.com>
31 */
32
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"
39
40 #include "cell_context.h"
41 #include "cell_state.h"
42 #include "cell_texture.h"
43
44
45 /* Simple, maximally packed layout.
46 */
47
48 static unsigned minify( unsigned d )
49 {
50 return MAX2(1, d>>1);
51 }
52
53
54 static void
55 cell_texture_layout(struct cell_texture * spt)
56 {
57 struct pipe_texture *pt = &spt->base;
58 unsigned level;
59 unsigned width = pt->width[0];
60 unsigned height = pt->height[0];
61 unsigned depth = pt->depth[0];
62
63 spt->buffer_size = 0;
64
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);
71
72 spt->stride[level] = pt->nblocksx[level] * pt->block.size;
73
74 spt->level_offset[level] = spt->buffer_size;
75
76 spt->buffer_size += (pt->nblocksy[level] *
77 ((pt->target == PIPE_TEXTURE_CUBE) ? 6 : depth) *
78 pt->nblocksx[level] * pt->block.size);
79
80 width = minify(width);
81 height = minify(height);
82 depth = minify(depth);
83 }
84 }
85
86
87 static struct pipe_texture *
88 cell_texture_create_screen(struct pipe_screen *screen,
89 const struct pipe_texture *templat)
90 {
91 struct pipe_winsys *ws = screen->winsys;
92 struct cell_texture *spt = CALLOC_STRUCT(cell_texture);
93 if (!spt)
94 return NULL;
95
96 spt->base = *templat;
97 spt->base.refcount = 1;
98 spt->base.screen = screen;
99
100 cell_texture_layout(spt);
101
102 spt->buffer = ws->buffer_create(ws, 32,
103 PIPE_BUFFER_USAGE_PIXEL,
104 spt->buffer_size);
105
106 if (!spt->buffer) {
107 FREE(spt);
108 return NULL;
109 }
110
111 return &spt->base;
112 }
113
114
115 static void
116 cell_texture_release_screen(struct pipe_screen *screen,
117 struct pipe_texture **pt)
118 {
119 if (!*pt)
120 return;
121
122 /*
123 DBG("%s %p refcount will be %d\n",
124 __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
125 */
126 if (--(*pt)->refcount <= 0) {
127 struct cell_texture *spt = cell_texture(*pt);
128
129 /*
130 DBG("%s deleting %p\n", __FUNCTION__, (void *) spt);
131 */
132
133 pipe_buffer_reference(screen, &spt->buffer, NULL);
134
135 FREE(spt);
136 }
137 *pt = NULL;
138 }
139
140
141 static void
142 cell_texture_update(struct pipe_context *pipe, struct pipe_texture *texture,
143 uint face, uint levelsMask)
144 {
145 /* XXX TO DO: re-tile the texture data ... */
146
147 }
148
149
150 static struct pipe_surface *
151 cell_get_tex_surface_screen(struct pipe_screen *screen,
152 struct pipe_texture *pt,
153 unsigned face, unsigned level, unsigned zslice,
154 unsigned usage)
155 {
156 struct pipe_winsys *ws = screen->winsys;
157 struct cell_texture *spt = cell_texture(pt);
158 struct pipe_surface *ps;
159
160 ps = ws->surface_alloc(ws);
161 if (ps) {
162 assert(ps->refcount);
163 assert(ps->winsys);
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];
173 ps->usage = usage;
174
175 /* XXX may need to override usage flags (see sp_texture.c) */
176
177
178 if (pt->target == PIPE_TEXTURE_CUBE || pt->target == PIPE_TEXTURE_3D) {
179 ps->offset += ((pt->target == PIPE_TEXTURE_CUBE) ? face : zslice) *
180 ps->nblocksy *
181 ps->stride;
182 } else {
183 assert(face == 0);
184 assert(zslice == 0);
185 }
186 }
187 return ps;
188 }
189
190
191
192 static void
193 tile_copy_data(uint w, uint h, uint tile_size, uint *dst, const uint *src)
194 {
195 const uint tile_size2 = tile_size * tile_size;
196 const uint h_t = h / tile_size, w_t = w / tile_size;
197
198 uint it, jt; /* tile counters */
199 uint i, j; /* intra-tile counters */
200
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];
212 }
213 }
214 }
215 }
216 }
217
218
219
220 /**
221 * Convert linear texture image data to tiled format for SPU usage.
222 */
223 static void
224 cell_tile_texture(struct cell_context *cell,
225 struct cell_texture *texture)
226 {
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];
231 const uint *src;
232
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);
238
239 surf = screen->get_tex_surface(screen, &texture->base, face, level, zslice,
240 PIPE_BUFFER_USAGE_CPU_WRITE);
241 ASSERT(surf);
242
243 src = (const uint *) pipe_surface_map(surf, PIPE_BUFFER_USAGE_CPU_WRITE);
244
245 if (texture->tiled_data) {
246 align_free(texture->tiled_data);
247 }
248 texture->tiled_data = align_malloc(w * h * 4, 16);
249
250 tile_copy_data(w, h, TILE_SIZE, texture->tiled_data, src);
251
252 pipe_surface_unmap(surf);
253
254 pipe_surface_reference(&surf, NULL);
255 }
256
257
258 void
259 cell_update_texture_mapping(struct cell_context *cell)
260 {
261 #if 0
262 uint face = 0, level = 0, zslice = 0;
263 #endif
264 uint i;
265
266 for (i = 0; i < CELL_MAX_SAMPLERS; i++) {
267 if (cell->texture[i])
268 cell_tile_texture(cell, cell->texture[i]);
269 }
270
271 #if 0
272 if (cell->tex_surf && cell->tex_map) {
273 pipe_surface_unmap(cell->tex_surf);
274 cell->tex_map = NULL;
275 }
276
277 /* XXX free old surface */
278
279 cell->tex_surf = cell_get_tex_surface(&cell->pipe,
280 &cell->texture[0]->base,
281 face, level, zslice);
282
283 cell->tex_map = pipe_surface_map(cell->tex_surf);
284 #endif
285 }
286
287
288 static void *
289 cell_surface_map( struct pipe_screen *screen,
290 struct pipe_surface *surface,
291 unsigned flags )
292 {
293 ubyte *map;
294
295 if (flags & ~surface->usage) {
296 assert(0);
297 return NULL;
298 }
299
300 map = screen->winsys->buffer_map( screen->winsys, surface->buffer, flags );
301 if (map == NULL)
302 return NULL;
303
304 /* May want to different things here depending on read/write nature
305 * of the map:
306 */
307 if (surface->texture &&
308 (flags & PIPE_BUFFER_USAGE_CPU_WRITE))
309 {
310 /* Do something to notify sharing contexts of a texture change.
311 * In softpipe, that would mean flushing the texture cache.
312 */
313 #if 0
314 cell_screen(screen)->timestamp++;
315 #endif
316 }
317
318 return map + surface->offset;
319 }
320
321
322 static void
323 cell_surface_unmap(struct pipe_screen *screen,
324 struct pipe_surface *surface)
325 {
326 screen->winsys->buffer_unmap( screen->winsys, surface->buffer );
327 }
328
329
330 void
331 cell_init_texture_functions(struct cell_context *cell)
332 {
333 /*cell->pipe.texture_update = cell_texture_update;*/
334 }
335
336 void
337 cell_init_screen_texture_funcs(struct pipe_screen *screen)
338 {
339 screen->texture_create = cell_texture_create_screen;
340 screen->texture_release = cell_texture_release_screen;
341 screen->get_tex_surface = cell_get_tex_surface_screen;
342
343 screen->surface_map = cell_surface_map;
344 screen->surface_unmap = cell_surface_unmap;
345 }