draw: switch over to draw_pt paths, will remove old code shortly
[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_vbuf.h"
37 #include "draw_vs.h"
38
39
40 struct draw_context *draw_create( void )
41 {
42 struct draw_context *draw = CALLOC_STRUCT( draw_context );
43 if (draw == NULL)
44 goto fail;
45
46 #if defined(__i386__) || defined(__386__)
47 draw->use_sse = GETENV( "GALLIUM_NOSSE" ) == NULL;
48 #else
49 draw->use_sse = FALSE;
50 #endif
51
52 /* create pipeline stages */
53 draw->pipeline.wide_line = draw_wide_line_stage( draw );
54 draw->pipeline.wide_point = draw_wide_point_stage( draw );
55 draw->pipeline.stipple = draw_stipple_stage( draw );
56 draw->pipeline.unfilled = draw_unfilled_stage( draw );
57 draw->pipeline.twoside = draw_twoside_stage( draw );
58 draw->pipeline.offset = draw_offset_stage( draw );
59 draw->pipeline.clip = draw_clip_stage( draw );
60 draw->pipeline.flatshade = draw_flatshade_stage( draw );
61 draw->pipeline.cull = draw_cull_stage( draw );
62 draw->pipeline.validate = draw_validate_stage( draw );
63 draw->pipeline.first = draw->pipeline.validate;
64
65 if (!draw->pipeline.wide_line ||
66 !draw->pipeline.wide_point ||
67 !draw->pipeline.stipple ||
68 !draw->pipeline.unfilled ||
69 !draw->pipeline.twoside ||
70 !draw->pipeline.offset ||
71 !draw->pipeline.clip ||
72 !draw->pipeline.flatshade ||
73 !draw->pipeline.cull ||
74 !draw->pipeline.validate)
75 goto fail;
76
77
78 ASSIGN_4V( draw->plane[0], -1, 0, 0, 1 );
79 ASSIGN_4V( draw->plane[1], 1, 0, 0, 1 );
80 ASSIGN_4V( draw->plane[2], 0, -1, 0, 1 );
81 ASSIGN_4V( draw->plane[3], 0, 1, 0, 1 );
82 ASSIGN_4V( draw->plane[4], 0, 0, 1, 1 ); /* yes these are correct */
83 ASSIGN_4V( draw->plane[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
84 draw->nr_planes = 6;
85
86 /* Statically allocate maximum sized vertices for the cache - could be cleverer...
87 */
88 {
89 char *tmp = align_malloc(VS_QUEUE_LENGTH * MAX_VERTEX_ALLOCATION, 16);
90 if (!tmp)
91 goto fail;
92
93 draw->vs.vertex_cache = tmp;
94 }
95
96 draw->shader_queue_flush = draw_vertex_shader_queue_flush;
97
98 /* these defaults are oriented toward the needs of softpipe */
99 draw->wide_point_threshold = 1000000.0; /* infinity */
100 draw->wide_line_threshold = 1.0;
101 draw->line_stipple = TRUE;
102 draw->point_sprite = TRUE;
103
104 draw->reduced_prim = ~0; /* != any of PIPE_PRIM_x */
105
106 draw_vertex_cache_invalidate( draw );
107 draw_set_mapped_element_buffer( draw, 0, NULL );
108
109 tgsi_exec_machine_init(&draw->machine);
110
111 /* FIXME: give this machine thing a proper constructor:
112 */
113 draw->machine.Inputs = align_malloc(PIPE_MAX_ATTRIBS * sizeof(struct tgsi_exec_vector), 16);
114 draw->machine.Outputs = align_malloc(PIPE_MAX_ATTRIBS * sizeof(struct tgsi_exec_vector), 16);
115
116
117 if (!draw_pt_init( draw ))
118 goto fail;
119
120 return draw;
121
122 fail:
123 draw_destroy( draw );
124 return NULL;
125 }
126
127
128 void draw_destroy( struct draw_context *draw )
129 {
130 if (!draw)
131 return;
132
133 if (draw->pipeline.wide_line)
134 draw->pipeline.wide_line->destroy( draw->pipeline.wide_line );
135 if (draw->pipeline.wide_point)
136 draw->pipeline.wide_point->destroy( draw->pipeline.wide_point );
137 if (draw->pipeline.stipple)
138 draw->pipeline.stipple->destroy( draw->pipeline.stipple );
139 if (draw->pipeline.unfilled)
140 draw->pipeline.unfilled->destroy( draw->pipeline.unfilled );
141 if (draw->pipeline.twoside)
142 draw->pipeline.twoside->destroy( draw->pipeline.twoside );
143 if (draw->pipeline.offset)
144 draw->pipeline.offset->destroy( draw->pipeline.offset );
145 if (draw->pipeline.clip)
146 draw->pipeline.clip->destroy( draw->pipeline.clip );
147 if (draw->pipeline.flatshade)
148 draw->pipeline.flatshade->destroy( draw->pipeline.flatshade );
149 if (draw->pipeline.cull)
150 draw->pipeline.cull->destroy( draw->pipeline.cull );
151 if (draw->pipeline.validate)
152 draw->pipeline.validate->destroy( draw->pipeline.validate );
153 if (draw->pipeline.aaline)
154 draw->pipeline.aaline->destroy( draw->pipeline.aaline );
155 if (draw->pipeline.aapoint)
156 draw->pipeline.aapoint->destroy( draw->pipeline.aapoint );
157 if (draw->pipeline.pstipple)
158 draw->pipeline.pstipple->destroy( draw->pipeline.pstipple );
159 if (draw->pipeline.rasterize)
160 draw->pipeline.rasterize->destroy( draw->pipeline.rasterize );
161
162 if (draw->machine.Inputs)
163 align_free(draw->machine.Inputs);
164 if (draw->machine.Outputs)
165 align_free(draw->machine.Outputs);
166 tgsi_exec_machine_free_data(&draw->machine);
167
168
169 if (draw->vs.vertex_cache)
170 align_free( draw->vs.vertex_cache ); /* Frees all the vertices. */
171
172 /* Not so fast -- we're just borrowing this at the moment.
173 *
174 if (draw->render)
175 draw->render->destroy( draw->render );
176 */
177
178 draw_pt_destroy( draw );
179
180 FREE( draw );
181 }
182
183
184
185 void draw_flush( struct draw_context *draw )
186 {
187 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
188 }
189
190
191
192 /**
193 * Register new primitive rasterization/rendering state.
194 * This causes the drawing pipeline to be rebuilt.
195 */
196 void draw_set_rasterizer_state( struct draw_context *draw,
197 const struct pipe_rasterizer_state *raster )
198 {
199 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
200
201 draw->rasterizer = raster;
202 }
203
204
205 /**
206 * Plug in the primitive rendering/rasterization stage (which is the last
207 * stage in the drawing pipeline).
208 * This is provided by the device driver.
209 */
210 void draw_set_rasterize_stage( struct draw_context *draw,
211 struct draw_stage *stage )
212 {
213 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
214
215 draw->pipeline.rasterize = stage;
216 }
217
218
219 /**
220 * Set the draw module's clipping state.
221 */
222 void draw_set_clip_state( struct draw_context *draw,
223 const struct pipe_clip_state *clip )
224 {
225 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
226
227 assert(clip->nr <= PIPE_MAX_CLIP_PLANES);
228 memcpy(&draw->plane[6], clip->ucp, clip->nr * sizeof(clip->ucp[0]));
229 draw->nr_planes = 6 + clip->nr;
230 }
231
232
233 /**
234 * Set the draw module's viewport state.
235 */
236 void draw_set_viewport_state( struct draw_context *draw,
237 const struct pipe_viewport_state *viewport )
238 {
239 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
240 draw->viewport = *viewport; /* struct copy */
241 draw->identity_viewport = (viewport->scale[0] == 1.0f &&
242 viewport->scale[1] == 1.0f &&
243 viewport->scale[2] == 1.0f &&
244 viewport->scale[3] == 1.0f &&
245 viewport->translate[0] == 0.0f &&
246 viewport->translate[1] == 0.0f &&
247 viewport->translate[2] == 0.0f &&
248 viewport->translate[3] == 0.0f);
249 }
250
251
252
253 void
254 draw_set_vertex_buffers(struct draw_context *draw,
255 unsigned count,
256 const struct pipe_vertex_buffer *buffers)
257 {
258 assert(count <= PIPE_MAX_ATTRIBS);
259
260 draw_do_flush( draw, DRAW_FLUSH_VERTEX_CACHE/*STATE_CHANGE*/ );
261
262 memcpy(draw->vertex_buffer, buffers, count * sizeof(buffers[0]));
263 draw->nr_vertex_buffers = count;
264 }
265
266
267 void
268 draw_set_vertex_elements(struct draw_context *draw,
269 unsigned count,
270 const struct pipe_vertex_element *elements)
271 {
272 assert(count <= PIPE_MAX_ATTRIBS);
273
274 draw_do_flush( draw, DRAW_FLUSH_VERTEX_CACHE/*STATE_CHANGE*/ );
275
276 memcpy(draw->vertex_element, elements, count * sizeof(elements[0]));
277 draw->nr_vertex_elements = count;
278 }
279
280
281 /**
282 * Tell drawing context where to find mapped vertex buffers.
283 */
284 void
285 draw_set_mapped_vertex_buffer(struct draw_context *draw,
286 unsigned attr, const void *buffer)
287 {
288 draw_do_flush( draw, DRAW_FLUSH_VERTEX_CACHE/*STATE_CHANGE*/ );
289 draw->user.vbuffer[attr] = buffer;
290 }
291
292
293 void
294 draw_set_mapped_constant_buffer(struct draw_context *draw,
295 const void *buffer)
296 {
297 draw_do_flush( draw, DRAW_FLUSH_VERTEX_CACHE/*STATE_CHANGE*/ );
298 draw->user.constants = buffer;
299 }
300
301
302 /**
303 * Tells the draw module to draw points with triangles if their size
304 * is greater than this threshold.
305 */
306 void
307 draw_wide_point_threshold(struct draw_context *draw, float threshold)
308 {
309 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
310 draw->wide_point_threshold = threshold;
311 }
312
313
314 /**
315 * Tells the draw module to draw lines with triangles if their width
316 * is greater than this threshold.
317 */
318 void
319 draw_wide_line_threshold(struct draw_context *draw, float threshold)
320 {
321 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
322 draw->wide_line_threshold = threshold;
323 }
324
325
326 /**
327 * Tells the draw module whether or not to implement line stipple.
328 */
329 void
330 draw_enable_line_stipple(struct draw_context *draw, boolean enable)
331 {
332 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
333 draw->line_stipple = enable;
334 }
335
336
337 /**
338 * Tells draw module whether to convert points to quads for sprite mode.
339 */
340 void
341 draw_enable_point_sprites(struct draw_context *draw, boolean enable)
342 {
343 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
344 draw->point_sprite = enable;
345 }
346
347
348 /**
349 * Ask the draw module for the location/slot of the given vertex attribute in
350 * a post-transformed vertex.
351 *
352 * With this function, drivers that use the draw module should have no reason
353 * to track the current vertex shader.
354 *
355 * Note that the draw module may sometimes generate vertices with extra
356 * attributes (such as texcoords for AA lines). The driver can call this
357 * function to find those attributes.
358 *
359 * Zero is returned if the attribute is not found since this is
360 * a don't care / undefined situtation. Returning -1 would be a bit more
361 * work for the drivers.
362 */
363 int
364 draw_find_vs_output(struct draw_context *draw,
365 uint semantic_name, uint semantic_index)
366 {
367 const struct draw_vertex_shader *vs = draw->vertex_shader;
368 uint i;
369 for (i = 0; i < vs->info.num_outputs; i++) {
370 if (vs->info.output_semantic_name[i] == semantic_name &&
371 vs->info.output_semantic_index[i] == semantic_index)
372 return i;
373 }
374
375 /* XXX there may be more than one extra vertex attrib.
376 * For example, simulated gl_FragCoord and gl_PointCoord.
377 */
378 if (draw->extra_vp_outputs.semantic_name == semantic_name &&
379 draw->extra_vp_outputs.semantic_index == semantic_index) {
380 return draw->extra_vp_outputs.slot;
381 }
382 return 0;
383 }
384
385
386 /**
387 * Return number of vertex shader outputs.
388 */
389 uint
390 draw_num_vs_outputs(struct draw_context *draw)
391 {
392 uint count = draw->vertex_shader->info.num_outputs;
393 if (draw->extra_vp_outputs.slot > 0)
394 count++;
395 return count;
396 }
397
398
399 /**
400 * Allocate space for temporary post-transform vertices, such as for clipping.
401 */
402 void draw_alloc_temp_verts( struct draw_stage *stage, unsigned nr )
403 {
404 assert(!stage->tmp);
405
406 stage->nr_tmps = nr;
407
408 if (nr) {
409 ubyte *store = (ubyte *) MALLOC( MAX_VERTEX_SIZE * nr );
410 unsigned i;
411
412 stage->tmp = (struct vertex_header **) MALLOC( sizeof(struct vertex_header *) * nr );
413
414 for (i = 0; i < nr; i++)
415 stage->tmp[i] = (struct vertex_header *)(store + i * MAX_VERTEX_SIZE);
416 }
417 }
418
419
420 void draw_free_temp_verts( struct draw_stage *stage )
421 {
422 if (stage->tmp) {
423 FREE( stage->tmp[0] );
424 FREE( stage->tmp );
425 stage->tmp = NULL;
426 }
427 }
428
429
430 boolean draw_use_sse(struct draw_context *draw)
431 {
432 return (boolean) draw->use_sse;
433 }
434
435
436 void draw_reset_vertex_ids(struct draw_context *draw)
437 {
438 struct draw_stage *stage = draw->pipeline.first;
439
440 while (stage) {
441 unsigned i;
442
443 for (i = 0; i < stage->nr_tmps; i++)
444 stage->tmp[i]->vertex_id = UNDEFINED_VERTEX_ID;
445
446 stage = stage->next;
447 }
448
449 draw_vertex_cache_reset_vertex_ids(draw); /* going away soon */
450 draw_pt_reset_vertex_ids(draw);
451 }
452
453
454 void draw_set_render( struct draw_context *draw,
455 struct vbuf_render *render )
456 {
457 draw->render = render;
458 }
459
460 void draw_set_edgeflags( struct draw_context *draw,
461 const unsigned *edgeflag )
462 {
463 draw->user.edgeflag = edgeflag;
464 }
465
466
467 boolean draw_get_edgeflag( struct draw_context *draw,
468 unsigned idx )
469 {
470 if (draw->user.edgeflag)
471 return (draw->user.edgeflag[idx/32] & (1 << (idx%32))) != 0;
472 else
473 return 1;
474 }
475