draw: move some pipeline-specific code & state to draw_pipe.[ch]
[mesa.git] / src / gallium / auxiliary / draw / draw_pt_pipeline.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 "pipe/p_util.h"
34 #include "draw/draw_context.h"
35 #include "draw/draw_private.h"
36 #include "draw/draw_vertex.h"
37 #include "draw/draw_pt.h"
38 #include "draw/draw_pipe.h"
39
40 static void do_point( struct draw_context *draw,
41 const char *v0 )
42 {
43 struct prim_header prim;
44
45 prim.reset_line_stipple = 0;
46 prim.edgeflags = 1;
47 prim.pad = 0;
48 prim.v[0] = (struct vertex_header *)v0;
49
50 draw->pipeline.first->point( draw->pipeline.first, &prim );
51 }
52
53
54 static void do_line( struct draw_context *draw,
55 const char *v0,
56 const char *v1 )
57 {
58 struct prim_header prim;
59
60 prim.reset_line_stipple = 1; /* fixme */
61 prim.edgeflags = 1;
62 prim.pad = 0;
63 prim.v[0] = (struct vertex_header *)v0;
64 prim.v[1] = (struct vertex_header *)v1;
65
66 draw->pipeline.first->line( draw->pipeline.first, &prim );
67 }
68
69
70 static void do_triangle( struct draw_context *draw,
71 char *v0,
72 char *v1,
73 char *v2 )
74 {
75 struct prim_header prim;
76
77 prim.v[0] = (struct vertex_header *)v0;
78 prim.v[1] = (struct vertex_header *)v1;
79 prim.v[2] = (struct vertex_header *)v2;
80 prim.reset_line_stipple = 1;
81 prim.edgeflags = ((prim.v[0]->edgeflag) |
82 (prim.v[1]->edgeflag << 1) |
83 (prim.v[2]->edgeflag << 2));
84 prim.pad = 0;
85
86 if (0) debug_printf("tri ef: %d %d %d\n",
87 prim.v[0]->edgeflag,
88 prim.v[1]->edgeflag,
89 prim.v[2]->edgeflag);
90
91 draw->pipeline.first->tri( draw->pipeline.first, &prim );
92 }
93
94
95
96 void draw_pt_reset_vertex_ids( struct draw_context *draw )
97 {
98 unsigned i;
99 char *verts = draw->pt.pipeline.verts;
100 unsigned stride = draw->pt.pipeline.vertex_stride;
101
102 for (i = 0; i < draw->pt.pipeline.vertex_count; i++) {
103 ((struct vertex_header *)verts)->vertex_id = UNDEFINED_VERTEX_ID;
104 verts += stride;
105 }
106 }
107
108
109 /* Code to run the pipeline on a fairly arbitary collection of vertices.
110 *
111 * Vertex headers must be pre-initialized with the
112 * UNDEFINED_VERTEX_ID, this code will cause that id to become
113 * overwritten, so it may have to be reset if there is the intention
114 * to reuse the vertices.
115 *
116 * This code provides a callback to reset the vertex id's which the
117 * draw_vbuf.c code uses when it has to perform a flush.
118 */
119 void draw_pt_run_pipeline( struct draw_context *draw,
120 unsigned prim,
121 struct vertex_header *pipeline_verts,
122 unsigned vertex_count,
123 unsigned stride,
124 const ushort *elts,
125 unsigned count )
126 {
127 char *verts = (char *)pipeline_verts;
128 unsigned i;
129
130 draw->pt.pipeline.verts = verts;
131 draw->pt.pipeline.vertex_stride = stride;
132 draw->pt.pipeline.vertex_count = vertex_count;
133
134 switch (prim) {
135 case PIPE_PRIM_POINTS:
136 for (i = 0; i < count; i++)
137 do_point( draw,
138 verts + stride * elts[i] );
139 break;
140 case PIPE_PRIM_LINES:
141 for (i = 0; i+1 < count; i += 2)
142 do_line( draw,
143 verts + stride * elts[i+0],
144 verts + stride * elts[i+1]);
145 break;
146 case PIPE_PRIM_TRIANGLES:
147 for (i = 0; i+2 < count; i += 3)
148 do_triangle( draw,
149 verts + stride * elts[i+0],
150 verts + stride * elts[i+1],
151 verts + stride * elts[i+2]);
152 break;
153 }
154
155 draw->pt.pipeline.verts = NULL;
156 draw->pt.pipeline.vertex_count = 0;
157 }
158