1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
33 #include "pipe/p_debug.h"
35 #include "draw_private.h"
36 #include "draw_context.h"
46 static unsigned reduced_prim
[PIPE_PRIM_POLYGON
+ 1] = {
60 static void draw_prim_queue_flush( struct draw_context
*draw
)
65 debug_printf("Flushing with %d prims, %d verts\n",
66 draw
->pq
.queue_nr
, draw
->vs
.queue_nr
);
68 assert (draw
->pq
.queue_nr
!= 0);
70 /* NOTE: we cannot save draw->pipeline->first in a local var because
71 * draw->pipeline->first is often changed by the first call to tri(),
74 if (draw
->rasterizer
->line_stipple_enable
) {
75 switch (draw
->reduced_prim
) {
77 for (i
= 0; i
< draw
->pq
.queue_nr
; i
++) {
78 if (draw
->pq
.queue
[i
].reset_line_stipple
)
79 draw
->pipeline
.first
->reset_stipple_counter( draw
->pipeline
.first
);
81 draw
->pipeline
.first
->tri( draw
->pipeline
.first
, &draw
->pq
.queue
[i
] );
85 for (i
= 0; i
< draw
->pq
.queue_nr
; i
++) {
86 if (draw
->pq
.queue
[i
].reset_line_stipple
)
87 draw
->pipeline
.first
->reset_stipple_counter( draw
->pipeline
.first
);
89 draw
->pipeline
.first
->line( draw
->pipeline
.first
, &draw
->pq
.queue
[i
] );
93 draw
->pipeline
.first
->reset_stipple_counter( draw
->pipeline
.first
);
94 for (i
= 0; i
< draw
->pq
.queue_nr
; i
++)
95 draw
->pipeline
.first
->point( draw
->pipeline
.first
, &draw
->pq
.queue
[i
] );
100 switch (draw
->reduced_prim
) {
102 for (i
= 0; i
< draw
->pq
.queue_nr
; i
++)
103 draw
->pipeline
.first
->tri( draw
->pipeline
.first
, &draw
->pq
.queue
[i
] );
106 for (i
= 0; i
< draw
->pq
.queue_nr
; i
++)
107 draw
->pipeline
.first
->line( draw
->pipeline
.first
, &draw
->pq
.queue
[i
] );
110 for (i
= 0; i
< draw
->pq
.queue_nr
; i
++)
111 draw
->pipeline
.first
->point( draw
->pipeline
.first
, &draw
->pq
.queue
[i
] );
116 draw
->pq
.queue_nr
= 0;
117 draw_vertex_cache_unreference( draw
);
122 void draw_do_flush( struct draw_context
*draw
, unsigned flags
)
124 static boolean flushing
= FALSE
;
127 debug_printf("Flushing with %d verts, %d prims\n",
134 if (flags
>= DRAW_FLUSH_SHADER_QUEUE
) {
135 if (draw
->vs
.queue_nr
)
136 (*draw
->shader_queue_flush
)(draw
);
138 if (flags
>= DRAW_FLUSH_PRIM_QUEUE
) {
139 if (draw
->pq
.queue_nr
)
140 draw_prim_queue_flush(draw
);
142 if (flags
>= DRAW_FLUSH_VERTEX_CACHE
) {
143 draw_vertex_cache_invalidate(draw
);
145 if (flags
>= DRAW_FLUSH_STATE_CHANGE
) {
146 draw
->pipeline
.first
->flush( draw
->pipeline
.first
, flags
);
147 draw
->pipeline
.first
= draw
->pipeline
.validate
;
148 draw
->reduced_prim
= ~0;
160 /* Return a pointer to a freshly queued primitive header. Ensure that
161 * there is room in the vertex cache for a maximum of "nr_verts" new
162 * vertices. Flush primitive and/or vertex queues if necessary to
165 static struct prim_header
*get_queued_prim( struct draw_context
*draw
,
168 if (!draw_vertex_cache_check_space( draw
, nr_verts
)) {
169 // debug_printf("v");
170 draw_do_flush( draw
, DRAW_FLUSH_VERTEX_CACHE
);
172 else if (draw
->pq
.queue_nr
== PRIM_QUEUE_LENGTH
) {
173 // debug_printf("p");
174 draw_do_flush( draw
, DRAW_FLUSH_PRIM_QUEUE
);
177 assert(draw
->pq
.queue_nr
< PRIM_QUEUE_LENGTH
);
179 return &draw
->pq
.queue
[draw
->pq
.queue_nr
++];
185 * Add a point to the primitive queue.
186 * \param i0 index into user's vertex arrays
188 static void do_point( struct draw_context
*draw
,
191 struct prim_header
*prim
= get_queued_prim( draw
, 1 );
193 prim
->reset_line_stipple
= 0;
196 prim
->v
[0] = draw
->vcache
.get_vertex( draw
, i0
);
201 * Add a line to the primitive queue.
202 * \param i0 index into user's vertex arrays
203 * \param i1 index into user's vertex arrays
205 static void do_line( struct draw_context
*draw
,
206 boolean reset_stipple
,
210 struct prim_header
*prim
= get_queued_prim( draw
, 2 );
212 prim
->reset_line_stipple
= reset_stipple
;
215 prim
->v
[0] = draw
->vcache
.get_vertex( draw
, i0
);
216 prim
->v
[1] = draw
->vcache
.get_vertex( draw
, i1
);
220 * Add a triangle to the primitive queue.
222 static void do_triangle( struct draw_context
*draw
,
227 struct prim_header
*prim
= get_queued_prim( draw
, 3 );
229 prim
->reset_line_stipple
= 1;
230 prim
->edgeflags
= ~0;
232 prim
->v
[0] = draw
->vcache
.get_vertex( draw
, i0
);
233 prim
->v
[1] = draw
->vcache
.get_vertex( draw
, i1
);
234 prim
->v
[2] = draw
->vcache
.get_vertex( draw
, i2
);
237 static void do_ef_triangle( struct draw_context
*draw
,
238 boolean reset_stipple
,
244 struct prim_header
*prim
= get_queued_prim( draw
, 3 );
245 struct vertex_header
*v0
= draw
->vcache
.get_vertex( draw
, i0
);
246 struct vertex_header
*v1
= draw
->vcache
.get_vertex( draw
, i1
);
247 struct vertex_header
*v2
= draw
->vcache
.get_vertex( draw
, i2
);
249 prim
->reset_line_stipple
= reset_stipple
;
251 prim
->edgeflags
= ef_mask
& ((v0
->edgeflag
<< 0) |
252 (v1
->edgeflag
<< 1) |
253 (v2
->edgeflag
<< 2));
261 static void do_ef_quad( struct draw_context
*draw
,
267 const unsigned omitEdge2
= ~(1 << 1);
268 const unsigned omitEdge3
= ~(1 << 2);
269 do_ef_triangle( draw
, 1, omitEdge2
, v0
, v1
, v3
);
270 do_ef_triangle( draw
, 0, omitEdge3
, v1
, v2
, v3
);
273 static void do_quad( struct draw_context
*draw
,
279 do_triangle( draw
, v0
, v1
, v3
);
280 do_triangle( draw
, v1
, v2
, v3
);
285 * Main entrypoint to draw some number of points/lines/triangles
288 draw_prim( struct draw_context
*draw
,
289 unsigned prim
, unsigned start
, unsigned count
)
292 boolean unfilled
= (draw
->rasterizer
->fill_cw
!= PIPE_POLYGON_MODE_FILL
||
293 draw
->rasterizer
->fill_ccw
!= PIPE_POLYGON_MODE_FILL
);
295 // debug_printf("%s (%d) %d/%d\n", __FUNCTION__, draw->prim, start, count );
298 case PIPE_PRIM_POINTS
:
299 for (i
= 0; i
< count
; i
++) {
305 case PIPE_PRIM_LINES
:
306 for (i
= 0; i
+1 < count
; i
+= 2) {
314 case PIPE_PRIM_LINE_LOOP
:
316 for (i
= 1; i
< count
; i
++) {
318 i
== 1, /* XXX: only if vb not split */
330 case PIPE_PRIM_LINE_STRIP
:
331 for (i
= 1; i
< count
; i
++) {
339 case PIPE_PRIM_TRIANGLES
:
341 for (i
= 0; i
+2 < count
; i
+= 3) {
342 do_ef_triangle( draw
,
351 for (i
= 0; i
+2 < count
; i
+= 3) {
360 case PIPE_PRIM_TRIANGLE_STRIP
:
361 for (i
= 0; i
+2 < count
; i
++) {
377 case PIPE_PRIM_TRIANGLE_FAN
:
379 for (i
= 0; i
+2 < count
; i
++) {
389 case PIPE_PRIM_QUADS
:
391 for (i
= 0; i
+3 < count
; i
+= 4) {
400 for (i
= 0; i
+3 < count
; i
+= 4) {
410 case PIPE_PRIM_QUAD_STRIP
:
412 for (i
= 0; i
+3 < count
; i
+= 2) {
421 for (i
= 0; i
+3 < count
; i
+= 2) {
431 case PIPE_PRIM_POLYGON
:
433 unsigned ef_mask
= (1<<2) | (1<<0);
435 for (i
= 0; i
+2 < count
; i
++) {
440 do_ef_triangle( draw
,
451 for (i
= 0; i
+2 < count
; i
++) {
471 * This is the main entrypoint into the drawing module.
472 * \param prim one of PIPE_PRIM_x
473 * \param start index of first vertex to draw
474 * \param count number of vertices to draw
477 draw_arrays(struct draw_context
*draw
, unsigned prim
,
478 unsigned start
, unsigned count
)
480 if (reduced_prim
[prim
] != draw
->reduced_prim
) {
481 draw_do_flush( draw
, DRAW_FLUSH_STATE_CHANGE
);
482 draw
->reduced_prim
= reduced_prim
[prim
];
485 /* drawing done here: */
486 draw_prim(draw
, prim
, start
, count
);