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