summaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/draw/draw_prim.c
blob: cb0277fb6c92b6f6074fb008d8a7d9b28e0eea08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/**************************************************************************
 * 
 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 **************************************************************************/

 /*
  * Authors:
  *   Keith Whitwell <keith@tungstengraphics.com>
  */

#include "pipe/p_debug.h"
#include "pipe/p_util.h"

#include "draw_private.h"
#include "draw_context.h"



#define RP_NONE  0
#define RP_POINT 1
#define RP_LINE  2
#define RP_TRI   3


static unsigned reduced_prim[PIPE_PRIM_POLYGON + 1] = {
   RP_POINT,
   RP_LINE,
   RP_LINE,
   RP_LINE,
   RP_TRI,
   RP_TRI,
   RP_TRI,
   RP_TRI,
   RP_TRI,
   RP_TRI
};


static void draw_prim_queue_flush( struct draw_context *draw )
{
   unsigned i;

   if (0)
      debug_printf("Flushing with %d prims, %d verts\n",
                   draw->pq.queue_nr, draw->vs.queue_nr);

   assert (draw->pq.queue_nr != 0);

   /* NOTE: we cannot save draw->pipeline->first in a local var because
    * draw->pipeline->first is often changed by the first call to tri(),
    * line(), etc.
    */
   if (draw->rasterizer->line_stipple_enable) {
      switch (draw->reduced_prim) {
      case RP_TRI:
	 for (i = 0; i < draw->pq.queue_nr; i++) {
	    if (draw->pq.queue[i].reset_line_stipple)
	       draw->pipeline.first->reset_stipple_counter( draw->pipeline.first );
	    
	    draw->pipeline.first->tri( draw->pipeline.first, &draw->pq.queue[i] );
	 }
	 break;
      case RP_LINE:
	 for (i = 0; i < draw->pq.queue_nr; i++) {
	    if (draw->pq.queue[i].reset_line_stipple)
	       draw->pipeline.first->reset_stipple_counter( draw->pipeline.first );
	    
	    draw->pipeline.first->line( draw->pipeline.first, &draw->pq.queue[i] );
	 }
	 break;
      case RP_POINT:
	 draw->pipeline.first->reset_stipple_counter( draw->pipeline.first );
	 for (i = 0; i < draw->pq.queue_nr; i++)
	    draw->pipeline.first->point( draw->pipeline.first, &draw->pq.queue[i] );
	 break;
      }
   }
   else {
      switch (draw->reduced_prim) {
      case RP_TRI:
	 for (i = 0; i < draw->pq.queue_nr; i++) 
	    draw->pipeline.first->tri( draw->pipeline.first, &draw->pq.queue[i] );
	 break;
      case RP_LINE:
	 for (i = 0; i < draw->pq.queue_nr; i++) 
	    draw->pipeline.first->line( draw->pipeline.first, &draw->pq.queue[i] );
	 break;
      case RP_POINT:
	 for (i = 0; i < draw->pq.queue_nr; i++)
	    draw->pipeline.first->point( draw->pipeline.first, &draw->pq.queue[i] );
	 break;
      }
   }

   draw->pq.queue_nr = 0;   
   draw->vs.post_nr = 0;   
   draw_vertex_cache_unreference( draw );
}

static INLINE void fetch_and_store(struct draw_context *draw)
{
   /* run vertex shader on vertex cache entries, four per invokation */
#if 0
   {
      const struct vertex_info *vinfo = draw->render->get_vertex_info(draw->render);
      memcpy(draw->vs.queue[0].vertex, draw->vs.queue[i + j].elt,
             count * vinfo->size);
   }
#elif 0
   unsigned i;

   draw_update_vertex_fetch(draw);
   for (i = 0; i < draw->vs.queue_nr; i += 4) {
      struct vertex_header *dests[4];
      unsigned elts[4];
      struct tgsi_exec_machine *machine = &draw->machine;
      int j, n = MIN2(4, draw->vs.queue_nr - i);

      for (j = 0; j < n; j++) {
         elts[j] = draw->vs.queue[i + j].elt;
         dests[j] = draw->vs.queue[i + j].vertex;
      }

      for ( ; j < 4; j++) {
	 elts[j] = elts[0];
         dests[j] = draw->vs.queue[i + j].vertex;
      }
      //fetch directly into dests
      draw->vertex_fetch.fetch_func(draw, machine, dests, count);
   }
#endif

   draw->vs.post_nr = draw->vs.queue_nr;
   draw->vs.queue_nr = 0;
}

void draw_do_flush( struct draw_context *draw, unsigned flags )
{
   static boolean flushing = FALSE;

   if (0)
      debug_printf("Flushing with %d verts, %d prims\n",
                   draw->vs.queue_nr,
                   draw->pq.queue_nr );

   if (!flushing) {
      flushing = TRUE;

   if (flags >= DRAW_FLUSH_SHADER_QUEUE) {
      if (draw->vs.queue_nr) {
         if (draw->rasterizer->bypass_vs)
            fetch_and_store(draw);
         else
            (*draw->shader_queue_flush)(draw);
      }

      if (flags >= DRAW_FLUSH_PRIM_QUEUE) {
	 if (draw->pq.queue_nr)
	    draw_prim_queue_flush(draw);

	 if (flags >= DRAW_FLUSH_VERTEX_CACHE) {
            draw_vertex_cache_invalidate(draw);

	    if (flags >= DRAW_FLUSH_STATE_CHANGE) {
               draw->pipeline.first->flush( draw->pipeline.first, flags );
               draw->pipeline.first = draw->pipeline.validate;
               draw->reduced_prim = ~0;
	    }
	 }
      }    
   }

      flushing = FALSE;
   }
}



/* Return a pointer to a freshly queued primitive header.  Ensure that
 * there is room in the vertex cache for a maximum of "nr_verts" new
 * vertices.  Flush primitive and/or vertex queues if necessary to
 * make space.
 */
static struct prim_header *get_queued_prim( struct draw_context *draw,
					    unsigned nr_verts )
{
   if (!draw_vertex_cache_check_space( draw, nr_verts )) {
//      debug_printf("v");
      draw_do_flush( draw, DRAW_FLUSH_VERTEX_CACHE );
   }
   else if (draw->pq.queue_nr == PRIM_QUEUE_LENGTH) {
//      debug_printf("p");
      draw_do_flush( draw, DRAW_FLUSH_PRIM_QUEUE );
   }

   assert(draw->pq.queue_nr < PRIM_QUEUE_LENGTH);

   return &draw->pq.queue[draw->pq.queue_nr++];
}



/**
 * Add a point to the primitive queue.
 * \param i0  index into user's vertex arrays
 */
static void do_point( struct draw_context *draw,
		      unsigned i0 )
{
   struct prim_header *prim = get_queued_prim( draw, 1 );
   
   prim->reset_line_stipple = 0;
   prim->edgeflags = 1;
   prim->pad = 0;
   prim->v[0] = draw->vcache.get_vertex( draw, i0 );
}


/**
 * Add a line to the primitive queue.
 * \param i0  index into user's vertex arrays
 * \param i1  index into user's vertex arrays
 */
static void do_line( struct draw_context *draw,
		     boolean reset_stipple,
		     unsigned i0,
		     unsigned i1 )
{
   struct prim_header *prim = get_queued_prim( draw, 2 );
   
   prim->reset_line_stipple = reset_stipple;
   prim->edgeflags = 1;
   prim->pad = 0;
   prim->v[0] = draw->vcache.get_vertex( draw, i0 );
   prim->v[1] = draw->vcache.get_vertex( draw, i1 );
}

/**
 * Add a triangle to the primitive queue.
 */
static void do_triangle( struct draw_context *draw,
			 unsigned i0,
			 unsigned i1,
			 unsigned i2 )
{
   struct prim_header *prim = get_queued_prim( draw, 3 );
   
//   _mesa_printf("tri %d %d %d\n", i0, i1, i2);
   prim->reset_line_stipple = 1;
   prim->edgeflags = ~0;
   prim->pad = 0;
   prim->v[0] = draw->vcache.get_vertex( draw, i0 );
   prim->v[1] = draw->vcache.get_vertex( draw, i1 );
   prim->v[2] = draw->vcache.get_vertex( draw, i2 );
}
			  
static void do_ef_triangle( struct draw_context *draw,
			    boolean reset_stipple,
			    unsigned ef_mask,
			    unsigned i0,
			    unsigned i1,
			    unsigned i2 )
{
   struct prim_header *prim = get_queued_prim( draw, 3 );
   struct vertex_header *v0 = draw->vcache.get_vertex( draw, i0 );
   struct vertex_header *v1 = draw->vcache.get_vertex( draw, i1 );
   struct vertex_header *v2 = draw->vcache.get_vertex( draw, i2 );

   prim->reset_line_stipple = reset_stipple;

   prim->edgeflags = ef_mask & ((v0->edgeflag << 0) | 
				(v1->edgeflag << 1) | 
				(v2->edgeflag << 2));
   prim->pad = 0;
   prim->v[0] = v0;
   prim->v[1] = v1;
   prim->v[2] = v2;
}


static void do_ef_quad( struct draw_context *draw,
		     unsigned v0,
		     unsigned v1,
		     unsigned v2,
		     unsigned v3 )
{
   const unsigned omitEdge2 = ~(1 << 1);
   const unsigned omitEdge3 = ~(1 << 2);
   do_ef_triangle( draw, 1, omitEdge2, v0, v1, v3 );
   do_ef_triangle( draw, 0, omitEdge3, v1, v2, v3 );
}

static void do_quad( struct draw_context *draw,
		     unsigned v0,
		     unsigned v1,
		     unsigned v2,
		     unsigned v3 )
{
   do_triangle( draw, v0, v1, v3 );
   do_triangle( draw, v1, v2, v3 );
}


/**
 * Main entrypoint to draw some number of points/lines/triangles
 */
static void
draw_prim( struct draw_context *draw, 
	   unsigned prim, unsigned start, unsigned count )
{
   unsigned i;
   boolean unfilled = (draw->rasterizer->fill_cw != PIPE_POLYGON_MODE_FILL ||
		       draw->rasterizer->fill_ccw != PIPE_POLYGON_MODE_FILL);

//   debug_printf("%s (%d) %d/%d\n", __FUNCTION__, draw->prim, start, count );

   switch (prim) {
   case PIPE_PRIM_POINTS:
      for (i = 0; i < count; i ++) {
	 do_point( draw,
		   start + i );
      }
      break;

   case PIPE_PRIM_LINES:
      for (i = 0; i+1 < count; i += 2) {
	 do_line( draw, 
		  TRUE,
		  start + i + 0,
		  start + i + 1);
      }
      break;

   case PIPE_PRIM_LINE_LOOP:  
      if (count >= 2) {
	 for (i = 1; i < count; i++) {
	    do_line( draw, 
		     i == 1, 	/* XXX: only if vb not split */
		     start + i - 1,
		     start + i );
	 }

	 do_line( draw, 
		  0,
		  start + count - 1,
		  start + 0 );
      }
      break;

   case PIPE_PRIM_LINE_STRIP:
      for (i = 1; i < count; i++) {
	 do_line( draw,
		  i == 1,
		  start + i - 1,
		  start + i );
      }
      break;

   case PIPE_PRIM_TRIANGLES:
      if (unfilled) {
	 for (i = 0; i+2 < count; i += 3) {
	    do_ef_triangle( draw,
			    1, 
			    ~0,
			    start + i + 0,
			    start + i + 1,
			    start + i + 2 );
	 }
      } 
      else {
	 for (i = 0; i+2 < count; i += 3) {
	    do_triangle( draw,
			 start + i + 0,
			 start + i + 1,
			 start + i + 2 );
	 }
      }
      break;

   case PIPE_PRIM_TRIANGLE_STRIP:
      for (i = 0; i+2 < count; i++) {
	 if (i & 1) {
	    do_triangle( draw,
			 start + i + 1,
			 start + i + 0,
			 start + i + 2 );
	 }
	 else {
	    do_triangle( draw,
			 start + i + 0,
			 start + i + 1,
			 start + i + 2 );
	 }
      }
      break;

   case PIPE_PRIM_TRIANGLE_FAN:
      if (count >= 3) {
	 for (i = 0; i+2 < count; i++) {
	    do_triangle( draw,
			 start + 0,
			 start + i + 1,
			 start + i + 2 );
	 }
      }
      break;


   case PIPE_PRIM_QUADS:
      if (unfilled) {
	 for (i = 0; i+3 < count; i += 4) {
	    do_ef_quad( draw,
			start + i + 0,
			start + i + 1,
			start + i + 2,
			start + i + 3);
	 }
      }
      else {
	 for (i = 0; i+3 < count; i += 4) {
	    do_quad( draw,
		     start + i + 0,
		     start + i + 1,
		     start + i + 2,
		     start + i + 3);
	 }
      }
      break;

   case PIPE_PRIM_QUAD_STRIP:
      if (unfilled) {
	 for (i = 0; i+3 < count; i += 2) {
	    do_ef_quad( draw,
			start + i + 2,
			start + i + 0,
			start + i + 1,
			start + i + 3);
	 }
      }
      else {
	 for (i = 0; i+3 < count; i += 2) {
	    do_quad( draw,
		     start + i + 2,
		     start + i + 0,
		     start + i + 1,
		     start + i + 3);
	 }
      }
      break;

   case PIPE_PRIM_POLYGON:
      if (unfilled) {
	 unsigned ef_mask = (1<<2) | (1<<0);

	 for (i = 0; i+2 < count; i++) {

            if (i + 3 >= count)
	       ef_mask |= (1<<1);

	    do_ef_triangle( draw,
			    i == 0,
			    ef_mask,
			    start + i + 1,
			    start + i + 2,
			    start + 0);

	    ef_mask &= ~(1<<2);
	 }
      }
      else {
	 for (i = 0; i+2 < count; i++) {
	    do_triangle( draw,
			 start + i + 1,
			 start + i + 2,
			 start + 0);
	 }
      }
      break;

   default:
      assert(0);
      break;
   }
}




/**
 * Draw vertex arrays
 * This is the main entrypoint into the drawing module.
 * \param prim  one of PIPE_PRIM_x
 * \param start  index of first vertex to draw
 * \param count  number of vertices to draw
 */
void
draw_arrays(struct draw_context *draw, unsigned prim,
            unsigned start, unsigned count)
{
   if (reduced_prim[prim] != draw->reduced_prim) {
      draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
      draw->reduced_prim = reduced_prim[prim];
   }

   /* drawing done here: */
   if (!draw->rasterizer->bypass_vs ||
       !draw_passthrough_arrays(draw, prim, start, count)) {
      /* we have to run the whole pipeline */
      draw_prim(draw, prim, start, count);
   }
}