cell: Update for cpp removal.
[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_util.h"
37 #include "pipe/p_winsys.h"
38
39 #include "cell_context.h"
40 #include "cell_state.h"
41 #include "cell_texture.h"
42
43
44 /* Simple, maximally packed layout.
45 */
46
47 static unsigned minify( unsigned d )
48 {
49 return MAX2(1, d>>1);
50 }
51
52
53 static void
54 cell_texture_layout(struct cell_texture * spt)
55 {
56 struct pipe_texture *pt = &spt->base;
57 unsigned level;
58 unsigned width = pt->width[0];
59 unsigned height = pt->height[0];
60 unsigned depth = pt->depth[0];
61
62 spt->buffer_size = 0;
63
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);
70
71 spt->level_offset[level] = spt->buffer_size;
72
73 spt->buffer_size += (pt->nblocksy[level] *
74 ((pt->target == PIPE_TEXTURE_CUBE) ? 6 : depth) *
75 pt->nblocksx[level] * pt->block.size;
76
77 width = minify(width);
78 height = minify(height);
79 depth = minify(depth);
80 }
81 }
82
83
84 static struct pipe_texture *
85 cell_texture_create_screen(struct pipe_screen *screen,
86 const struct pipe_texture *templat)
87 {
88 struct pipe_winsys *ws = screen->winsys;
89 struct cell_texture *spt = CALLOC_STRUCT(cell_texture);
90 if (!spt)
91 return NULL;
92
93 spt->base = *templat;
94 spt->base.refcount = 1;
95 spt->base.screen = screen;
96
97 cell_texture_layout(spt);
98
99 spt->buffer = ws->buffer_create(ws, 32,
100 PIPE_BUFFER_USAGE_PIXEL,
101 spt->buffer_size);
102
103 if (!spt->buffer) {
104 FREE(spt);
105 return NULL;
106 }
107
108 return &spt->base;
109 }
110
111
112 static void
113 cell_texture_release_screen(struct pipe_screen *screen,
114 struct pipe_texture **pt)
115 {
116 if (!*pt)
117 return;
118
119 /*
120 DBG("%s %p refcount will be %d\n",
121 __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
122 */
123 if (--(*pt)->refcount <= 0) {
124 struct cell_texture *spt = cell_texture(*pt);
125
126 /*
127 DBG("%s deleting %p\n", __FUNCTION__, (void *) spt);
128 */
129
130 pipe_buffer_reference(screen->winsys, &spt->buffer, NULL);
131
132 FREE(spt);
133 }
134 *pt = NULL;
135 }
136
137
138 static void
139 cell_texture_update(struct pipe_context *pipe, struct pipe_texture *texture,
140 uint face, uint levelsMask)
141 {
142 /* XXX TO DO: re-tile the texture data ... */
143
144 }
145
146
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)
151 {
152 struct pipe_winsys *ws = screen->winsys;
153 struct cell_texture *spt = cell_texture(pt);
154 struct pipe_surface *ps;
155
156 ps = ws->surface_alloc(ws);
157 if (ps) {
158 assert(ps->refcount);
159 assert(ps->winsys);
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];
169
170 if (pt->target == PIPE_TEXTURE_CUBE || pt->target == PIPE_TEXTURE_3D) {
171 ps->offset += ((pt->target == PIPE_TEXTURE_CUBE) ? face : zslice) *
172 ps->nblocksy *
173 ps->stride;
174 } else {
175 assert(face == 0);
176 assert(zslice == 0);
177 }
178 }
179 return ps;
180 }
181
182
183
184 static void
185 tile_copy_data(uint w, uint h, uint tile_size, uint *dst, const uint *src)
186 {
187 const uint tile_size2 = tile_size * tile_size;
188 const uint h_t = h / tile_size, w_t = w / tile_size;
189
190 uint it, jt; /* tile counters */
191 uint i, j; /* intra-tile counters */
192
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];
204 }
205 }
206 }
207 }
208 }
209
210
211
212 /**
213 * Convert linear texture image data to tiled format for SPU usage.
214 */
215 static void
216 cell_tile_texture(struct cell_context *cell,
217 struct cell_texture *texture)
218 {
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];
223 const uint *src;
224
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);
230
231 surf = screen->get_tex_surface(screen, &texture->base, face, level, zslice);
232 ASSERT(surf);
233
234 src = (const uint *) pipe_surface_map(surf);
235
236 if (texture->tiled_data) {
237 align_free(texture->tiled_data);
238 }
239 texture->tiled_data = align_malloc(w * h * 4, 16);
240
241 tile_copy_data(w, h, TILE_SIZE, texture->tiled_data, src);
242
243 pipe_surface_unmap(surf);
244
245 pipe_surface_reference(&surf, NULL);
246 }
247
248
249
250 void
251 cell_update_texture_mapping(struct cell_context *cell)
252 {
253 uint face = 0, level = 0, zslice = 0;
254 uint i;
255
256 for (i = 0; i < CELL_MAX_SAMPLERS; i++) {
257 if (cell->texture[i])
258 cell_tile_texture(cell, cell->texture[i]);
259 }
260
261 #if 0
262 if (cell->tex_surf && cell->tex_map) {
263 pipe_surface_unmap(cell->tex_surf);
264 cell->tex_map = NULL;
265 }
266
267 /* XXX free old surface */
268
269 cell->tex_surf = cell_get_tex_surface(&cell->pipe,
270 &cell->texture[0]->base,
271 face, level, zslice);
272
273 cell->tex_map = pipe_surface_map(cell->tex_surf);
274 #endif
275 }
276
277
278 void
279 cell_init_texture_functions(struct cell_context *cell)
280 {
281 cell->pipe.texture_update = cell_texture_update;
282 }
283
284 void
285 cell_init_screen_texture_funcs(struct pipe_screen *screen)
286 {
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;
290 }