gallium: rename pipe_buffer_handle to pipe_buffer, rework pipebuffer/ code
[mesa.git] / src / mesa / pipe / draw / draw_prim.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 * Keith Whitwell <keith@tungstengraphics.com>
31 */
32
33 #include "draw_private.h"
34 #include "draw_context.h"
35
36
37
38 #define RP_NONE 0
39 #define RP_POINT 1
40 #define RP_LINE 2
41 #define RP_TRI 3
42
43
44 static unsigned reduced_prim[PIPE_PRIM_POLYGON + 1] = {
45 RP_POINT,
46 RP_LINE,
47 RP_LINE,
48 RP_LINE,
49 RP_TRI,
50 RP_TRI,
51 RP_TRI,
52 RP_TRI,
53 RP_TRI,
54 RP_TRI
55 };
56
57
58 static void draw_prim_queue_flush( struct draw_context *draw )
59 {
60 struct draw_stage *first = draw->pipeline.first;
61 unsigned i;
62
63 if (0)
64 fprintf(stdout,"Flushing with %d prims, %d verts\n",
65 draw->pq.queue_nr, draw->vs.queue_nr);
66
67 /* Make sure all vertices are available/shaded:
68 */
69 if (draw->vs.queue_nr)
70 draw_vertex_shader_queue_flush(draw);
71
72 switch (draw->reduced_prim) {
73 case RP_TRI:
74 for (i = 0; i < draw->pq.queue_nr; i++) {
75 if (draw->pq.queue[i].reset_line_stipple)
76 first->reset_stipple_counter( first );
77
78 first->tri( first, &draw->pq.queue[i] );
79 }
80 break;
81 case RP_LINE:
82 for (i = 0; i < draw->pq.queue_nr; i++) {
83 if (draw->pq.queue[i].reset_line_stipple)
84 first->reset_stipple_counter( first );
85
86 first->line( first, &draw->pq.queue[i] );
87 }
88 break;
89 case RP_POINT:
90 first->reset_stipple_counter( first );
91 for (i = 0; i < draw->pq.queue_nr; i++)
92 first->point( first, &draw->pq.queue[i] );
93 break;
94 }
95
96 draw->pq.queue_nr = 0;
97 draw_vertex_cache_unreference( draw );
98 }
99
100
101 void draw_do_flush( struct draw_context *draw,
102 unsigned flush )
103 {
104 if ((flush & (DRAW_FLUSH_PRIM_QUEUE |
105 DRAW_FLUSH_VERTEX_CACHE_INVALIDATE |
106 DRAW_FLUSH_DRAW)) &&
107 draw->pq.queue_nr)
108 {
109 draw_prim_queue_flush(draw);
110 }
111
112 if ((flush & (DRAW_FLUSH_VERTEX_CACHE_INVALIDATE |
113 DRAW_FLUSH_DRAW)) &&
114 draw->drawing)
115 {
116 draw_vertex_cache_invalidate(draw);
117 }
118
119 if ((flush & DRAW_FLUSH_DRAW) &&
120 draw->drawing)
121 {
122 draw->pipeline.first->end( draw->pipeline.first );
123 draw->drawing = FALSE;
124 draw->prim = ~0;
125 draw->pipeline.first = draw->pipeline.validate;
126 }
127
128 }
129
130
131
132 /* Return a pointer to a freshly queued primitive header. Ensure that
133 * there is room in the vertex cache for a maximum of "nr_verts" new
134 * vertices. Flush primitive and/or vertex queues if necessary to
135 * make space.
136 */
137 static struct prim_header *get_queued_prim( struct draw_context *draw,
138 unsigned nr_verts )
139 {
140 if (!draw_vertex_cache_check_space( draw, nr_verts )) {
141 // fprintf(stderr, "v");
142 draw_do_flush( draw, DRAW_FLUSH_VERTEX_CACHE_INVALIDATE );
143 }
144 else if (draw->pq.queue_nr == PRIM_QUEUE_LENGTH) {
145 // fprintf(stderr, "p");
146 draw_do_flush( draw, DRAW_FLUSH_PRIM_QUEUE );
147 }
148
149 assert(draw->pq.queue_nr < PRIM_QUEUE_LENGTH);
150
151 return &draw->pq.queue[draw->pq.queue_nr++];
152 }
153
154
155
156 /**
157 * Add a point to the primitive queue.
158 * \param i0 index into user's vertex arrays
159 */
160 static void do_point( struct draw_context *draw,
161 unsigned i0 )
162 {
163 struct prim_header *prim = get_queued_prim( draw, 1 );
164
165 prim->reset_line_stipple = 0;
166 prim->edgeflags = 1;
167 prim->pad = 0;
168 prim->v[0] = draw->vcache.get_vertex( draw, i0 );
169 }
170
171
172 /**
173 * Add a line to the primitive queue.
174 * \param i0 index into user's vertex arrays
175 * \param i1 index into user's vertex arrays
176 */
177 static void do_line( struct draw_context *draw,
178 boolean reset_stipple,
179 unsigned i0,
180 unsigned i1 )
181 {
182 struct prim_header *prim = get_queued_prim( draw, 2 );
183
184 prim->reset_line_stipple = reset_stipple;
185 prim->edgeflags = 1;
186 prim->pad = 0;
187 prim->v[0] = draw->vcache.get_vertex( draw, i0 );
188 prim->v[1] = draw->vcache.get_vertex( draw, i1 );
189 }
190
191 /**
192 * Add a triangle to the primitive queue.
193 */
194 static void do_triangle( struct draw_context *draw,
195 unsigned i0,
196 unsigned i1,
197 unsigned i2 )
198 {
199 struct prim_header *prim = get_queued_prim( draw, 3 );
200
201 prim->reset_line_stipple = 1;
202 prim->edgeflags = ~0;
203 prim->pad = 0;
204 prim->v[0] = draw->vcache.get_vertex( draw, i0 );
205 prim->v[1] = draw->vcache.get_vertex( draw, i1 );
206 prim->v[2] = draw->vcache.get_vertex( draw, i2 );
207 }
208
209 static void do_ef_triangle( struct draw_context *draw,
210 boolean reset_stipple,
211 unsigned ef_mask,
212 unsigned i0,
213 unsigned i1,
214 unsigned i2 )
215 {
216 struct prim_header *prim = get_queued_prim( draw, 3 );
217 struct vertex_header *v0 = draw->vcache.get_vertex( draw, i0 );
218 struct vertex_header *v1 = draw->vcache.get_vertex( draw, i1 );
219 struct vertex_header *v2 = draw->vcache.get_vertex( draw, i2 );
220
221 prim->reset_line_stipple = reset_stipple;
222
223 prim->edgeflags = ef_mask & ((v0->edgeflag << 0) |
224 (v1->edgeflag << 1) |
225 (v2->edgeflag << 2));
226 prim->pad = 0;
227 prim->v[0] = v0;
228 prim->v[1] = v1;
229 prim->v[2] = v2;
230 }
231
232
233 static void do_quad( struct draw_context *draw,
234 unsigned v0,
235 unsigned v1,
236 unsigned v2,
237 unsigned v3 )
238 {
239 const unsigned omitEdge2 = ~(1 << 1);
240 const unsigned omitEdge3 = ~(1 << 2);
241 do_ef_triangle( draw, 1, omitEdge2, v0, v1, v3 );
242 do_ef_triangle( draw, 0, omitEdge3, v1, v2, v3 );
243 }
244
245
246 /**
247 * Main entrypoint to draw some number of points/lines/triangles
248 */
249 static void
250 draw_prim( struct draw_context *draw, unsigned start, unsigned count )
251 {
252 unsigned i;
253
254 // _mesa_printf("%s (%d) %d/%d\n", __FUNCTION__, draw->prim, start, count );
255
256 switch (draw->prim) {
257 case PIPE_PRIM_POINTS:
258 for (i = 0; i < count; i ++) {
259 do_point( draw,
260 start + i );
261 }
262 break;
263
264 case PIPE_PRIM_LINES:
265 for (i = 0; i+1 < count; i += 2) {
266 do_line( draw,
267 TRUE,
268 start + i + 0,
269 start + i + 1);
270 }
271 break;
272
273 case PIPE_PRIM_LINE_LOOP:
274 if (count >= 2) {
275 for (i = 1; i < count; i++) {
276 do_line( draw,
277 i == 1, /* XXX: only if vb not split */
278 start + i - 1,
279 start + i );
280 }
281
282 do_line( draw,
283 0,
284 start + count - 1,
285 start + 0 );
286 }
287 break;
288
289 case PIPE_PRIM_LINE_STRIP:
290 if (count >= 2) {
291 for (i = 1; i < count; i++) {
292 do_line( draw,
293 i == 1,
294 start + i - 1,
295 start + i );
296 }
297 }
298 break;
299
300 case PIPE_PRIM_TRIANGLES:
301 for (i = 0; i+2 < count; i += 3) {
302 do_ef_triangle( draw,
303 1,
304 ~0,
305 start + i + 0,
306 start + i + 1,
307 start + i + 2 );
308 }
309 break;
310
311 case PIPE_PRIM_TRIANGLE_STRIP:
312 for (i = 0; i+2 < count; i++) {
313 if (i & 1) {
314 do_triangle( draw,
315 start + i + 1,
316 start + i + 0,
317 start + i + 2 );
318 }
319 else {
320 do_triangle( draw,
321 start + i + 0,
322 start + i + 1,
323 start + i + 2 );
324 }
325 }
326 break;
327
328 case PIPE_PRIM_TRIANGLE_FAN:
329 if (count >= 3) {
330 for (i = 0; i+2 < count; i++) {
331 do_triangle( draw,
332 start + 0,
333 start + i + 1,
334 start + i + 2 );
335 }
336 }
337 break;
338
339
340 case PIPE_PRIM_QUADS:
341 for (i = 0; i+3 < count; i += 4) {
342 do_quad( draw,
343 start + i + 0,
344 start + i + 1,
345 start + i + 2,
346 start + i + 3);
347 }
348 break;
349
350 case PIPE_PRIM_QUAD_STRIP:
351 for (i = 0; i+3 < count; i += 2) {
352 do_quad( draw,
353 start + i + 2,
354 start + i + 0,
355 start + i + 1,
356 start + i + 3);
357 }
358 break;
359
360 case PIPE_PRIM_POLYGON:
361 if (count >= 3) {
362 unsigned ef_mask = (1<<2) | (1<<0);
363
364 for (i = 0; i+2 < count; i++) {
365
366 if (i + 3 >= count)
367 ef_mask |= (1<<1);
368
369 do_ef_triangle( draw,
370 i == 0,
371 ef_mask,
372 start + i + 1,
373 start + i + 2,
374 start + 0);
375
376 ef_mask &= ~(1<<2);
377 }
378 }
379 break;
380
381 default:
382 assert(0);
383 break;
384 }
385 }
386
387
388 static void
389 draw_set_prim( struct draw_context *draw, unsigned prim )
390 {
391 assert(prim >= PIPE_PRIM_POINTS);
392 assert(prim <= PIPE_PRIM_POLYGON);
393
394 if (reduced_prim[prim] != draw->reduced_prim) {
395 draw_do_flush( draw, DRAW_FLUSH_PRIM_QUEUE );
396 draw->reduced_prim = reduced_prim[prim];
397 }
398
399 draw->prim = prim;
400 }
401
402
403
404
405 /**
406 * Draw vertex arrays
407 * This is the main entrypoint into the drawing module.
408 * \param prim one of PIPE_PRIM_x
409 * \param start index of first vertex to draw
410 * \param count number of vertices to draw
411 */
412 void
413 draw_arrays(struct draw_context *draw, unsigned prim,
414 unsigned start, unsigned count)
415 {
416 if (!draw->drawing) {
417 draw->drawing = TRUE;
418
419 /* tell drawing pipeline we're beginning drawing */
420 draw->pipeline.first->begin( draw->pipeline.first );
421 }
422
423 if (draw->prim != prim) {
424 draw_set_prim( draw, prim );
425 }
426
427 /* drawing done here: */
428 draw_prim(draw, start, count);
429 }
430
431