gallium: fix line emit order for unfilled tris
[mesa.git] / src / gallium / auxiliary / draw / draw_context.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
34 #include "pipe/p_util.h"
35 #include "draw_context.h"
36 #include "draw_private.h"
37
38
39
40 struct draw_context *draw_create( void )
41 {
42 struct draw_context *draw = CALLOC_STRUCT( draw_context );
43
44 #if defined(__i386__) || defined(__386__)
45 draw->use_sse = GETENV( "GALLIUM_NOSSE" ) == NULL;
46 #else
47 draw->use_sse = FALSE;
48 #endif
49
50 /* create pipeline stages */
51 draw->pipeline.wide_line = draw_wide_line_stage( draw );
52 draw->pipeline.wide_point = draw_wide_point_stage( draw );
53 draw->pipeline.stipple = draw_stipple_stage( draw );
54 draw->pipeline.unfilled = draw_unfilled_stage( draw );
55 draw->pipeline.twoside = draw_twoside_stage( draw );
56 draw->pipeline.offset = draw_offset_stage( draw );
57 draw->pipeline.clip = draw_clip_stage( draw );
58 draw->pipeline.flatshade = draw_flatshade_stage( draw );
59 draw->pipeline.cull = draw_cull_stage( draw );
60 draw->pipeline.validate = draw_validate_stage( draw );
61 draw->pipeline.first = draw->pipeline.validate;
62
63 ASSIGN_4V( draw->plane[0], -1, 0, 0, 1 );
64 ASSIGN_4V( draw->plane[1], 1, 0, 0, 1 );
65 ASSIGN_4V( draw->plane[2], 0, -1, 0, 1 );
66 ASSIGN_4V( draw->plane[3], 0, 1, 0, 1 );
67 ASSIGN_4V( draw->plane[4], 0, 0, 1, 1 ); /* yes these are correct */
68 ASSIGN_4V( draw->plane[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
69 draw->nr_planes = 6;
70
71 /* Statically allocate maximum sized vertices for the cache - could be cleverer...
72 */
73 {
74 uint i;
75 const unsigned size = (MAX_VERTEX_SIZE + 0x0f) & ~0x0f;
76 char *tmp = align_malloc(Elements(draw->vs.queue) * size, 16);
77
78 for (i = 0; i < Elements(draw->vs.queue); i++)
79 draw->vs.queue[i].vertex = (struct vertex_header *)(tmp + i * size);
80 }
81
82 draw->shader_queue_flush = draw_vertex_shader_queue_flush;
83
84 /* these defaults are oriented toward the needs of softpipe */
85 draw->wide_point_threshold = 1000000.0; /* infinity */
86 draw->wide_line_threshold = 1.0;
87
88 draw->reduced_prim = ~0; /* != any of PIPE_PRIM_x */
89
90 draw_vertex_cache_invalidate( draw );
91 draw_set_mapped_element_buffer( draw, 0, NULL );
92
93 return draw;
94 }
95
96
97 void draw_destroy( struct draw_context *draw )
98 {
99 draw->pipeline.wide_line->destroy( draw->pipeline.wide_line );
100 draw->pipeline.wide_point->destroy( draw->pipeline.wide_point );
101 draw->pipeline.stipple->destroy( draw->pipeline.stipple );
102 draw->pipeline.unfilled->destroy( draw->pipeline.unfilled );
103 draw->pipeline.twoside->destroy( draw->pipeline.twoside );
104 draw->pipeline.offset->destroy( draw->pipeline.offset );
105 draw->pipeline.clip->destroy( draw->pipeline.clip );
106 draw->pipeline.flatshade->destroy( draw->pipeline.flatshade );
107 draw->pipeline.cull->destroy( draw->pipeline.cull );
108 draw->pipeline.validate->destroy( draw->pipeline.validate );
109 if (draw->pipeline.aaline)
110 draw->pipeline.aaline->destroy( draw->pipeline.aaline );
111 if (draw->pipeline.aapoint)
112 draw->pipeline.aapoint->destroy( draw->pipeline.aapoint );
113 if (draw->pipeline.rasterize)
114 draw->pipeline.rasterize->destroy( draw->pipeline.rasterize );
115 tgsi_exec_machine_free_data(&draw->machine);
116 align_free( draw->vs.queue[0].vertex ); /* Frees all the vertices. */
117 FREE( draw );
118 }
119
120
121
122 void draw_flush( struct draw_context *draw )
123 {
124 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
125 }
126
127
128
129 /**
130 * Register new primitive rasterization/rendering state.
131 * This causes the drawing pipeline to be rebuilt.
132 */
133 void draw_set_rasterizer_state( struct draw_context *draw,
134 const struct pipe_rasterizer_state *raster )
135 {
136 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
137
138 draw->rasterizer = raster;
139 }
140
141
142 /**
143 * Plug in the primitive rendering/rasterization stage (which is the last
144 * stage in the drawing pipeline).
145 * This is provided by the device driver.
146 */
147 void draw_set_rasterize_stage( struct draw_context *draw,
148 struct draw_stage *stage )
149 {
150 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
151
152 draw->pipeline.rasterize = stage;
153 }
154
155
156 /**
157 * Set the draw module's clipping state.
158 */
159 void draw_set_clip_state( struct draw_context *draw,
160 const struct pipe_clip_state *clip )
161 {
162 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
163
164 assert(clip->nr <= PIPE_MAX_CLIP_PLANES);
165 memcpy(&draw->plane[6], clip->ucp, clip->nr * sizeof(clip->ucp[0]));
166 draw->nr_planes = 6 + clip->nr;
167 }
168
169
170 /**
171 * Set the draw module's viewport state.
172 */
173 void draw_set_viewport_state( struct draw_context *draw,
174 const struct pipe_viewport_state *viewport )
175 {
176 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
177 draw->viewport = *viewport; /* struct copy */
178 }
179
180
181
182 void
183 draw_set_vertex_buffer(struct draw_context *draw,
184 unsigned attr,
185 const struct pipe_vertex_buffer *buffer)
186 {
187 draw_do_flush( draw, DRAW_FLUSH_VERTEX_CACHE/*STATE_CHANGE*/ );
188 assert(attr < PIPE_ATTRIB_MAX);
189 draw->vertex_buffer[attr] = *buffer;
190 }
191
192
193 void
194 draw_set_vertex_element(struct draw_context *draw,
195 unsigned attr,
196 const struct pipe_vertex_element *element)
197 {
198 draw_do_flush( draw, DRAW_FLUSH_VERTEX_CACHE/*STATE_CHANGE*/ );
199 assert(attr < PIPE_ATTRIB_MAX);
200 draw->vertex_element[attr] = *element;
201 }
202
203
204 /**
205 * Tell drawing context where to find mapped vertex buffers.
206 */
207 void
208 draw_set_mapped_vertex_buffer(struct draw_context *draw,
209 unsigned attr, const void *buffer)
210 {
211 draw_do_flush( draw, DRAW_FLUSH_VERTEX_CACHE/*STATE_CHANGE*/ );
212 draw->user.vbuffer[attr] = buffer;
213 }
214
215
216 void
217 draw_set_mapped_constant_buffer(struct draw_context *draw,
218 const void *buffer)
219 {
220 draw_do_flush( draw, DRAW_FLUSH_VERTEX_CACHE/*STATE_CHANGE*/ );
221 draw->user.constants = buffer;
222 }
223
224
225 /**
226 * Tells the draw module to draw points with triangles if their size
227 * is greater than this threshold.
228 */
229 void
230 draw_wide_point_threshold(struct draw_context *draw, float threshold)
231 {
232 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
233 draw->wide_point_threshold = threshold;
234 }
235
236
237 /**
238 * Tells the draw module to draw lines with triangles if their width
239 * is greater than this threshold.
240 */
241 void
242 draw_wide_line_threshold(struct draw_context *draw, float threshold)
243 {
244 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
245 draw->wide_line_threshold = threshold;
246 }
247
248
249 /**
250 * Ask the draw module for the location/slot of the given vertex attribute in
251 * a post-transformed vertex.
252 *
253 * With this function, drivers that use the draw module should have no reason
254 * to track the current vertex shader.
255 *
256 * Note that the draw module may sometimes generate vertices with extra
257 * attributes (such as texcoords for AA lines). The driver can call this
258 * function to find those attributes.
259 *
260 * Zero is returned if the attribute is not found since this is
261 * a don't care / undefined situtation. Returning -1 would be a bit more
262 * work for the drivers.
263 */
264 int
265 draw_find_vs_output(struct draw_context *draw,
266 uint semantic_name, uint semantic_index)
267 {
268 const struct draw_vertex_shader *vs = draw->vertex_shader;
269 uint i;
270 for (i = 0; i < vs->info.num_outputs; i++) {
271 if (vs->info.output_semantic_name[i] == semantic_name &&
272 vs->info.output_semantic_index[i] == semantic_index)
273 return i;
274 }
275
276 /* XXX there may be more than one extra vertex attrib.
277 * For example, simulated gl_FragCoord and gl_PointCoord.
278 */
279 if (draw->extra_vp_outputs.semantic_name == semantic_name &&
280 draw->extra_vp_outputs.semantic_index == semantic_index) {
281 return draw->extra_vp_outputs.slot;
282 }
283 return 0;
284 }
285
286
287 /**
288 * Return number of vertex shader outputs.
289 */
290 uint
291 draw_num_vs_outputs(struct draw_context *draw)
292 {
293 uint count = draw->vertex_shader->info.num_outputs;
294 if (draw->extra_vp_outputs.slot >= 0)
295 count++;
296 return count;
297 }
298
299
300 /**
301 * Allocate space for temporary post-transform vertices, such as for clipping.
302 */
303 void draw_alloc_temp_verts( struct draw_stage *stage, unsigned nr )
304 {
305 assert(!stage->tmp);
306
307 stage->nr_tmps = nr;
308
309 if (nr) {
310 ubyte *store = (ubyte *) MALLOC( MAX_VERTEX_SIZE * nr );
311 unsigned i;
312
313 stage->tmp = (struct vertex_header **) MALLOC( sizeof(struct vertex_header *) * nr );
314
315 for (i = 0; i < nr; i++)
316 stage->tmp[i] = (struct vertex_header *)(store + i * MAX_VERTEX_SIZE);
317 }
318 }
319
320
321 void draw_free_temp_verts( struct draw_stage *stage )
322 {
323 if (stage->tmp) {
324 FREE( stage->tmp[0] );
325 FREE( stage->tmp );
326 stage->tmp = NULL;
327 }
328 }
329
330
331 boolean draw_use_sse(struct draw_context *draw)
332 {
333 return (boolean) draw->use_sse;
334 }
335
336
337 void draw_reset_vertex_ids(struct draw_context *draw)
338 {
339 struct draw_stage *stage = draw->pipeline.first;
340
341 while (stage) {
342 unsigned i;
343
344 for (i = 0; i < stage->nr_tmps; i++)
345 stage->tmp[i]->vertex_id = UNDEFINED_VERTEX_ID;
346
347 stage = stage->next;
348 }
349
350 draw_vertex_cache_reset_vertex_ids(draw);
351 }