Merge commit 'origin/gallium-master-merge'
[mesa.git] / src / gallium / drivers / cell / ppu / cell_vbuf.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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 /**
29 * Vertex buffer code. The draw module transforms vertices to window
30 * coords, etc. and emits the vertices into buffer supplied by this module.
31 * When a vertex buffer is full, or we flush, we'll send the vertex data
32 * to the SPUs.
33 *
34 * Authors
35 * Brian Paul
36 */
37
38
39 #include "cell_batch.h"
40 #include "cell_context.h"
41 #include "cell_fence.h"
42 #include "cell_flush.h"
43 #include "cell_spu.h"
44 #include "cell_vbuf.h"
45 #include "draw/draw_vbuf.h"
46 #include "util/u_memory.h"
47
48
49 /** Allow vertex data to be inlined after RENDER command */
50 #define ALLOW_INLINE_VERTS 1
51
52
53 /**
54 * Subclass of vbuf_render because we need a cell_context pointer in
55 * a few places.
56 */
57 struct cell_vbuf_render
58 {
59 struct vbuf_render base;
60 struct cell_context *cell;
61 uint prim; /**< PIPE_PRIM_x */
62 uint vertex_size; /**< in bytes */
63 void *vertex_buffer; /**< just for debug, really */
64 uint vertex_buf; /**< in [0, CELL_NUM_BUFFERS-1] */
65 };
66
67
68 /** cast wrapper */
69 static struct cell_vbuf_render *
70 cell_vbuf_render(struct vbuf_render *vbr)
71 {
72 return (struct cell_vbuf_render *) vbr;
73 }
74
75
76
77 static const struct vertex_info *
78 cell_vbuf_get_vertex_info(struct vbuf_render *vbr)
79 {
80 struct cell_vbuf_render *cvbr = cell_vbuf_render(vbr);
81 return &cvbr->cell->vertex_info;
82 }
83
84
85 static void *
86 cell_vbuf_allocate_vertices(struct vbuf_render *vbr,
87 ushort vertex_size, ushort nr_vertices)
88 {
89 struct cell_vbuf_render *cvbr = cell_vbuf_render(vbr);
90 /*printf("Alloc verts %u * %u\n", vertex_size, nr_vertices);*/
91
92 assert(cvbr->vertex_buf == ~0);
93 cvbr->vertex_buf = cell_get_empty_buffer(cvbr->cell);
94 cvbr->vertex_buffer = cvbr->cell->buffer[cvbr->vertex_buf];
95 cvbr->vertex_size = vertex_size;
96 return cvbr->vertex_buffer;
97 }
98
99
100 static void
101 cell_vbuf_release_vertices(struct vbuf_render *vbr, void *vertices,
102 unsigned vertex_size, unsigned vertices_used)
103 {
104 struct cell_vbuf_render *cvbr = cell_vbuf_render(vbr);
105 struct cell_context *cell = cvbr->cell;
106
107 /*
108 printf("%s vertex_buf = %u count = %u\n",
109 __FUNCTION__, cvbr->vertex_buf, vertices_used);
110 */
111
112 /* Make sure texture buffers aren't released until we're done rendering
113 * with them.
114 */
115 cell_add_fenced_textures(cell);
116
117 /* Tell SPUs they can release the vert buf */
118 if (cvbr->vertex_buf != ~0U) {
119 STATIC_ASSERT(sizeof(struct cell_command_release_verts) % 16 == 0);
120 struct cell_command_release_verts *release
121 = (struct cell_command_release_verts *)
122 cell_batch_alloc16(cell, sizeof(struct cell_command_release_verts));
123 release->opcode[0] = CELL_CMD_RELEASE_VERTS;
124 release->vertex_buf = cvbr->vertex_buf;
125 }
126
127 cvbr->vertex_buf = ~0;
128 cell_flush_int(cell, 0x0);
129
130 assert(vertices == cvbr->vertex_buffer);
131 cvbr->vertex_buffer = NULL;
132 }
133
134
135
136 static boolean
137 cell_vbuf_set_primitive(struct vbuf_render *vbr, unsigned prim)
138 {
139 struct cell_vbuf_render *cvbr = cell_vbuf_render(vbr);
140 cvbr->prim = prim;
141 /*printf("cell_set_prim %u\n", prim);*/
142 return TRUE;
143 }
144
145
146 static void
147 cell_vbuf_draw(struct vbuf_render *vbr,
148 const ushort *indices,
149 uint nr_indices)
150 {
151 struct cell_vbuf_render *cvbr = cell_vbuf_render(vbr);
152 struct cell_context *cell = cvbr->cell;
153 float xmin, ymin, xmax, ymax;
154 uint i;
155 uint nr_vertices = 0, min_index = ~0;
156 const void *vertices = cvbr->vertex_buffer;
157 const uint vertex_size = cvbr->vertex_size;
158
159 for (i = 0; i < nr_indices; i++) {
160 if (indices[i] > nr_vertices)
161 nr_vertices = indices[i];
162 if (indices[i] < min_index)
163 min_index = indices[i];
164 }
165 nr_vertices++;
166
167 #if 0
168 /*if (min_index > 0)*/
169 printf("%s min_index = %u\n", __FUNCTION__, min_index);
170 #endif
171
172 #if 0
173 printf("cell_vbuf_draw() nr_indices = %u nr_verts = %u\n",
174 nr_indices, nr_vertices);
175 printf(" ");
176 for (i = 0; i < nr_indices; i += 3) {
177 printf("%u %u %u, ", indices[i+0], indices[i+1], indices[i+2]);
178 }
179 printf("\n");
180 #elif 0
181 printf("cell_vbuf_draw() nr_indices = %u nr_verts = %u indexes = [%u %u %u ...]\n",
182 nr_indices, nr_vertices,
183 indices[0], indices[1], indices[2]);
184 printf("ind space = %u, vert space = %u, space = %u\n",
185 nr_indices * 2,
186 nr_vertices * 4 * cell->vertex_info.size,
187 cell_batch_free_space(cell));
188 #endif
189
190 /* compute x/y bounding box */
191 xmin = ymin = 1e50;
192 xmax = ymax = -1e50;
193 for (i = min_index; i < nr_vertices; i++) {
194 const float *v = (float *) ((ubyte *) vertices + i * vertex_size);
195 if (v[0] < xmin)
196 xmin = v[0];
197 if (v[0] > xmax)
198 xmax = v[0];
199 if (v[1] < ymin)
200 ymin = v[1];
201 if (v[1] > ymax)
202 ymax = v[1];
203 }
204 #if 0
205 printf("PPU Bounds %g, %g .. %g, %g\n", xmin, ymin, xmax, ymax);
206 fflush(stdout);
207 #endif
208
209 if (cvbr->prim != PIPE_PRIM_TRIANGLES)
210 return; /* only render tris for now */
211
212 /* build/insert batch RENDER command */
213 {
214 const uint index_bytes = ROUNDUP16(nr_indices * 2);
215 const uint vertex_bytes = ROUNDUP16(nr_vertices * 4 * cell->vertex_info.size);
216 STATIC_ASSERT(sizeof(struct cell_command_render) % 16 == 0);
217 const uint batch_size = sizeof(struct cell_command_render) + index_bytes;
218
219 struct cell_command_render *render
220 = (struct cell_command_render *)
221 cell_batch_alloc16(cell, batch_size);
222
223 render->opcode[0] = CELL_CMD_RENDER;
224 render->prim_type = cvbr->prim;
225
226 render->num_indexes = nr_indices;
227 render->min_index = min_index;
228
229 /* append indices after render command */
230 memcpy(render + 1, indices, nr_indices * 2);
231
232 /* if there's room, append vertices after the indices, else leave
233 * vertices in the original/separate buffer.
234 */
235 render->vertex_size = 4 * cell->vertex_info.size;
236 render->num_verts = nr_vertices;
237 if (ALLOW_INLINE_VERTS &&
238 min_index == 0 &&
239 vertex_bytes + 16 <= cell_batch_free_space(cell)) {
240 /* vertex data inlined, after indices, at 16-byte boundary */
241 void *dst = cell_batch_alloc16(cell, vertex_bytes);
242 memcpy(dst, vertices, vertex_bytes);
243 render->inline_verts = TRUE;
244 render->vertex_buf = ~0;
245 }
246 else {
247 /* vertex data in separate buffer */
248 render->inline_verts = FALSE;
249 ASSERT(cvbr->vertex_buf >= 0);
250 render->vertex_buf = cvbr->vertex_buf;
251 }
252
253 render->xmin = xmin;
254 render->ymin = ymin;
255 render->xmax = xmax;
256 render->ymax = ymax;
257 }
258
259 #if 0
260 /* helpful for debug */
261 cell_flush_int(cell, CELL_FLUSH_WAIT);
262 #endif
263 }
264
265
266 static void
267 cell_vbuf_destroy(struct vbuf_render *vbr)
268 {
269 struct cell_vbuf_render *cvbr = cell_vbuf_render(vbr);
270 cvbr->cell->vbuf_render = NULL;
271 FREE(cvbr);
272 }
273
274
275 /**
276 * Initialize the post-transform vertex buffer information for the given
277 * context.
278 */
279 void
280 cell_init_vbuf(struct cell_context *cell)
281 {
282 assert(cell->draw);
283
284 cell->vbuf_render = CALLOC_STRUCT(cell_vbuf_render);
285
286 /* The max number of indexes is what can fix into a batch buffer,
287 * minus the render and release-verts commands.
288 */
289 cell->vbuf_render->base.max_indices
290 = (CELL_BUFFER_SIZE
291 - sizeof(struct cell_command_render)
292 - sizeof(struct cell_command_release_verts))
293 / sizeof(ushort);
294 cell->vbuf_render->base.max_vertex_buffer_bytes = CELL_BUFFER_SIZE;
295
296 cell->vbuf_render->base.get_vertex_info = cell_vbuf_get_vertex_info;
297 cell->vbuf_render->base.allocate_vertices = cell_vbuf_allocate_vertices;
298 cell->vbuf_render->base.set_primitive = cell_vbuf_set_primitive;
299 cell->vbuf_render->base.draw = cell_vbuf_draw;
300 cell->vbuf_render->base.release_vertices = cell_vbuf_release_vertices;
301 cell->vbuf_render->base.destroy = cell_vbuf_destroy;
302
303 cell->vbuf_render->cell = cell;
304 #if 1
305 cell->vbuf_render->vertex_buf = ~0;
306 #endif
307
308 cell->vbuf = draw_vbuf_stage(cell->draw, &cell->vbuf_render->base);
309 }