draw: move pt_pipeline code to draw_pipe.c
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe.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_private.h"
35 #include "draw/draw_pipe.h"
36
37
38
39 boolean draw_pipeline_init( struct draw_context *draw )
40 {
41 /* create pipeline stages */
42 draw->pipeline.wide_line = draw_wide_line_stage( draw );
43 draw->pipeline.wide_point = draw_wide_point_stage( draw );
44 draw->pipeline.stipple = draw_stipple_stage( draw );
45 draw->pipeline.unfilled = draw_unfilled_stage( draw );
46 draw->pipeline.twoside = draw_twoside_stage( draw );
47 draw->pipeline.offset = draw_offset_stage( draw );
48 draw->pipeline.clip = draw_clip_stage( draw );
49 draw->pipeline.flatshade = draw_flatshade_stage( draw );
50 draw->pipeline.cull = draw_cull_stage( draw );
51 draw->pipeline.validate = draw_validate_stage( draw );
52 draw->pipeline.first = draw->pipeline.validate;
53
54 if (!draw->pipeline.wide_line ||
55 !draw->pipeline.wide_point ||
56 !draw->pipeline.stipple ||
57 !draw->pipeline.unfilled ||
58 !draw->pipeline.twoside ||
59 !draw->pipeline.offset ||
60 !draw->pipeline.clip ||
61 !draw->pipeline.flatshade ||
62 !draw->pipeline.cull ||
63 !draw->pipeline.validate)
64 return FALSE;
65
66 /* these defaults are oriented toward the needs of softpipe */
67 draw->pipeline.wide_point_threshold = 1000000.0; /* infinity */
68 draw->pipeline.wide_line_threshold = 1.0;
69 draw->pipeline.line_stipple = TRUE;
70 draw->pipeline.point_sprite = TRUE;
71
72 return TRUE;
73 }
74
75
76 void draw_pipeline_destroy( struct draw_context *draw )
77 {
78 if (draw->pipeline.wide_line)
79 draw->pipeline.wide_line->destroy( draw->pipeline.wide_line );
80 if (draw->pipeline.wide_point)
81 draw->pipeline.wide_point->destroy( draw->pipeline.wide_point );
82 if (draw->pipeline.stipple)
83 draw->pipeline.stipple->destroy( draw->pipeline.stipple );
84 if (draw->pipeline.unfilled)
85 draw->pipeline.unfilled->destroy( draw->pipeline.unfilled );
86 if (draw->pipeline.twoside)
87 draw->pipeline.twoside->destroy( draw->pipeline.twoside );
88 if (draw->pipeline.offset)
89 draw->pipeline.offset->destroy( draw->pipeline.offset );
90 if (draw->pipeline.clip)
91 draw->pipeline.clip->destroy( draw->pipeline.clip );
92 if (draw->pipeline.flatshade)
93 draw->pipeline.flatshade->destroy( draw->pipeline.flatshade );
94 if (draw->pipeline.cull)
95 draw->pipeline.cull->destroy( draw->pipeline.cull );
96 if (draw->pipeline.validate)
97 draw->pipeline.validate->destroy( draw->pipeline.validate );
98 if (draw->pipeline.aaline)
99 draw->pipeline.aaline->destroy( draw->pipeline.aaline );
100 if (draw->pipeline.aapoint)
101 draw->pipeline.aapoint->destroy( draw->pipeline.aapoint );
102 if (draw->pipeline.pstipple)
103 draw->pipeline.pstipple->destroy( draw->pipeline.pstipple );
104 if (draw->pipeline.rasterize)
105 draw->pipeline.rasterize->destroy( draw->pipeline.rasterize );
106 }
107
108
109
110
111 /* This is only used for temporary verts.
112 */
113 #define MAX_VERTEX_SIZE ((2 + PIPE_MAX_SHADER_OUTPUTS) * 4 * sizeof(float))
114
115
116 /**
117 * Allocate space for temporary post-transform vertices, such as for clipping.
118 */
119 void draw_alloc_temp_verts( struct draw_stage *stage, unsigned nr )
120 {
121 assert(!stage->tmp);
122
123 stage->nr_tmps = nr;
124
125 if (nr) {
126 ubyte *store = (ubyte *) MALLOC( MAX_VERTEX_SIZE * nr );
127 unsigned i;
128
129 stage->tmp = (struct vertex_header **) MALLOC( sizeof(struct vertex_header *) * nr );
130
131 for (i = 0; i < nr; i++)
132 stage->tmp[i] = (struct vertex_header *)(store + i * MAX_VERTEX_SIZE);
133 }
134 }
135
136
137 void draw_free_temp_verts( struct draw_stage *stage )
138 {
139 if (stage->tmp) {
140 FREE( stage->tmp[0] );
141 FREE( stage->tmp );
142 stage->tmp = NULL;
143 }
144 }
145
146
147
148 static void do_point( struct draw_context *draw,
149 const char *v0 )
150 {
151 struct prim_header prim;
152
153 prim.reset_line_stipple = 0;
154 prim.edgeflags = 1;
155 prim.pad = 0;
156 prim.v[0] = (struct vertex_header *)v0;
157
158 draw->pipeline.first->point( draw->pipeline.first, &prim );
159 }
160
161
162 static void do_line( struct draw_context *draw,
163 const char *v0,
164 const char *v1 )
165 {
166 struct prim_header prim;
167
168 prim.reset_line_stipple = 1; /* fixme */
169 prim.edgeflags = 1;
170 prim.pad = 0;
171 prim.v[0] = (struct vertex_header *)v0;
172 prim.v[1] = (struct vertex_header *)v1;
173
174 draw->pipeline.first->line( draw->pipeline.first, &prim );
175 }
176
177
178 static void do_triangle( struct draw_context *draw,
179 char *v0,
180 char *v1,
181 char *v2 )
182 {
183 struct prim_header prim;
184
185 prim.v[0] = (struct vertex_header *)v0;
186 prim.v[1] = (struct vertex_header *)v1;
187 prim.v[2] = (struct vertex_header *)v2;
188 prim.reset_line_stipple = 1;
189 prim.edgeflags = ((prim.v[0]->edgeflag) |
190 (prim.v[1]->edgeflag << 1) |
191 (prim.v[2]->edgeflag << 2));
192 prim.pad = 0;
193
194 draw->pipeline.first->tri( draw->pipeline.first, &prim );
195 }
196
197
198 /* Reset vertex ids. This is basically a type of flush.
199 */
200 void draw_reset_vertex_ids(struct draw_context *draw)
201 {
202 struct draw_stage *stage = draw->pipeline.first;
203
204 while (stage) {
205 unsigned i;
206
207 for (i = 0; i < stage->nr_tmps; i++)
208 stage->tmp[i]->vertex_id = UNDEFINED_VERTEX_ID;
209
210 stage = stage->next;
211 }
212
213 if (draw->pipeline.verts)
214 {
215 unsigned i;
216 char *verts = draw->pipeline.verts;
217 unsigned stride = draw->pipeline.vertex_stride;
218
219 for (i = 0; i < draw->pipeline.vertex_count; i++) {
220 ((struct vertex_header *)verts)->vertex_id = UNDEFINED_VERTEX_ID;
221 verts += stride;
222 }
223 }
224 }
225
226
227 /* Code to run the pipeline on a fairly arbitary collection of vertices.
228 *
229 * Vertex headers must be pre-initialized with the
230 * UNDEFINED_VERTEX_ID, this code will cause that id to become
231 * overwritten, so it may have to be reset if there is the intention
232 * to reuse the vertices.
233 *
234 * This code provides a callback to reset the vertex id's which the
235 * draw_vbuf.c code uses when it has to perform a flush.
236 */
237 void draw_pipeline_run( struct draw_context *draw,
238 unsigned prim,
239 struct vertex_header *vertices,
240 unsigned vertex_count,
241 unsigned stride,
242 const ushort *elts,
243 unsigned count )
244 {
245 char *verts = (char *)vertices;
246 unsigned i;
247
248 draw->pipeline.verts = verts;
249 draw->pipeline.vertex_stride = stride;
250 draw->pipeline.vertex_count = vertex_count;
251
252 switch (prim) {
253 case PIPE_PRIM_POINTS:
254 for (i = 0; i < count; i++)
255 do_point( draw,
256 verts + stride * elts[i] );
257 break;
258 case PIPE_PRIM_LINES:
259 for (i = 0; i+1 < count; i += 2)
260 do_line( draw,
261 verts + stride * elts[i+0],
262 verts + stride * elts[i+1]);
263 break;
264 case PIPE_PRIM_TRIANGLES:
265 for (i = 0; i+2 < count; i += 3)
266 do_triangle( draw,
267 verts + stride * elts[i+0],
268 verts + stride * elts[i+1],
269 verts + stride * elts[i+2]);
270 break;
271 }
272
273 draw->pipeline.verts = NULL;
274 draw->pipeline.vertex_count = 0;
275 }
276