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