Merge remote branch 'origin/master' into nv50-compiler
[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_context.h"
35 #include "util/u_memory.h"
36 #include "util/u_math.h"
37 #include "util/u_cpu_detect.h"
38 #include "draw_context.h"
39 #include "draw_vs.h"
40 #include "draw_gs.h"
41
42 #if HAVE_LLVM
43 #include "gallivm/lp_bld_init.h"
44 #include "draw_llvm.h"
45
46 static boolean
47 draw_get_option_use_llvm(void)
48 {
49 static boolean first = TRUE;
50 static boolean value;
51 if (first) {
52 first = FALSE;
53 value = debug_get_bool_option("DRAW_USE_LLVM", TRUE);
54
55 #ifdef PIPE_ARCH_X86
56 util_cpu_detect();
57 /* require SSE2 due to LLVM PR6960. */
58 if (!util_cpu_caps.has_sse2)
59 value = FALSE;
60 #endif
61 }
62 return value;
63 }
64 #endif
65
66 struct draw_context *draw_create( struct pipe_context *pipe )
67 {
68 struct draw_context *draw = CALLOC_STRUCT( draw_context );
69 if (draw == NULL)
70 goto fail;
71
72 #if HAVE_LLVM
73 if(draw_get_option_use_llvm())
74 {
75 lp_build_init();
76 assert(lp_build_engine);
77 draw->engine = lp_build_engine;
78 draw->llvm = draw_llvm_create(draw);
79 }
80 #endif
81
82 if (!draw_init(draw))
83 goto fail;
84
85 draw->pipe = pipe;
86
87 return draw;
88
89 fail:
90 draw_destroy( draw );
91 return NULL;
92 }
93
94 boolean draw_init(struct draw_context *draw)
95 {
96 /*
97 * Note that several functions compute the clipmask of the predefined
98 * formats with hardcoded formulas instead of using these. So modifications
99 * here must be reflected there too.
100 */
101
102 ASSIGN_4V( draw->plane[0], -1, 0, 0, 1 );
103 ASSIGN_4V( draw->plane[1], 1, 0, 0, 1 );
104 ASSIGN_4V( draw->plane[2], 0, -1, 0, 1 );
105 ASSIGN_4V( draw->plane[3], 0, 1, 0, 1 );
106 ASSIGN_4V( draw->plane[4], 0, 0, 1, 1 ); /* yes these are correct */
107 ASSIGN_4V( draw->plane[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
108 draw->nr_planes = 6;
109 draw->clip_xy = 1;
110 draw->clip_z = 1;
111
112
113 draw->reduced_prim = ~0; /* != any of PIPE_PRIM_x */
114
115
116 if (!draw_pipeline_init( draw ))
117 return FALSE;
118
119 if (!draw_pt_init( draw ))
120 return FALSE;
121
122 if (!draw_vs_init( draw ))
123 return FALSE;
124
125 if (!draw_gs_init( draw ))
126 return FALSE;
127
128 return TRUE;
129 }
130
131
132 void draw_destroy( struct draw_context *draw )
133 {
134 struct pipe_context *pipe;
135 int i, j;
136
137 if (!draw)
138 return;
139
140 pipe = draw->pipe;
141
142 /* free any rasterizer CSOs that we may have created.
143 */
144 for (i = 0; i < 2; i++) {
145 for (j = 0; j < 2; j++) {
146 if (draw->rasterizer_no_cull[i][j]) {
147 pipe->delete_rasterizer_state(pipe, draw->rasterizer_no_cull[i][j]);
148 }
149 }
150 }
151
152 /* Not so fast -- we're just borrowing this at the moment.
153 *
154 if (draw->render)
155 draw->render->destroy( draw->render );
156 */
157
158 draw_pipeline_destroy( draw );
159 draw_pt_destroy( draw );
160 draw_vs_destroy( draw );
161 draw_gs_destroy( draw );
162 #ifdef HAVE_LLVM
163 if(draw->llvm)
164 draw_llvm_destroy( draw->llvm );
165 #endif
166
167 FREE( draw );
168 }
169
170
171
172 void draw_flush( struct draw_context *draw )
173 {
174 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
175 }
176
177
178 /**
179 * Specify the Minimum Resolvable Depth factor for polygon offset.
180 * This factor potentially depends on the number of Z buffer bits,
181 * the rasterization algorithm and the arithmetic performed on Z
182 * values between vertex shading and rasterization. It will vary
183 * from one driver to another.
184 */
185 void draw_set_mrd(struct draw_context *draw, double mrd)
186 {
187 draw->mrd = mrd;
188 }
189
190
191 static void update_clip_flags( struct draw_context *draw )
192 {
193 draw->clip_xy = !draw->driver.bypass_clip_xy;
194 draw->clip_z = (!draw->driver.bypass_clip_z &&
195 !draw->depth_clamp);
196 draw->clip_user = (draw->nr_planes > 6);
197 }
198
199 /**
200 * Register new primitive rasterization/rendering state.
201 * This causes the drawing pipeline to be rebuilt.
202 */
203 void draw_set_rasterizer_state( struct draw_context *draw,
204 const struct pipe_rasterizer_state *raster,
205 void *rast_handle )
206 {
207 if (!draw->suspend_flushing) {
208 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
209
210 draw->rasterizer = raster;
211 draw->rast_handle = rast_handle;
212
213 }
214 }
215
216 /* With a little more work, llvmpipe will be able to turn this off and
217 * do its own x/y clipping.
218 *
219 * Some hardware can turn off clipping altogether - in particular any
220 * hardware with a TNL unit can do its own clipping, even if it is
221 * relying on the draw module for some other reason.
222 */
223 void draw_set_driver_clipping( struct draw_context *draw,
224 boolean bypass_clip_xy,
225 boolean bypass_clip_z )
226 {
227 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
228
229 draw->driver.bypass_clip_xy = bypass_clip_xy;
230 draw->driver.bypass_clip_z = bypass_clip_z;
231 update_clip_flags(draw);
232 }
233
234
235 /**
236 * Plug in the primitive rendering/rasterization stage (which is the last
237 * stage in the drawing pipeline).
238 * This is provided by the device driver.
239 */
240 void draw_set_rasterize_stage( struct draw_context *draw,
241 struct draw_stage *stage )
242 {
243 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
244
245 draw->pipeline.rasterize = stage;
246 }
247
248
249 /**
250 * Set the draw module's clipping state.
251 */
252 void draw_set_clip_state( struct draw_context *draw,
253 const struct pipe_clip_state *clip )
254 {
255 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
256
257 assert(clip->nr <= PIPE_MAX_CLIP_PLANES);
258 memcpy(&draw->plane[6], clip->ucp, clip->nr * sizeof(clip->ucp[0]));
259 draw->nr_planes = 6 + clip->nr;
260 draw->depth_clamp = clip->depth_clamp;
261
262 update_clip_flags(draw);
263 }
264
265
266 /**
267 * Set the draw module's viewport state.
268 */
269 void draw_set_viewport_state( struct draw_context *draw,
270 const struct pipe_viewport_state *viewport )
271 {
272 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
273 draw->viewport = *viewport; /* struct copy */
274 draw->identity_viewport = (viewport->scale[0] == 1.0f &&
275 viewport->scale[1] == 1.0f &&
276 viewport->scale[2] == 1.0f &&
277 viewport->scale[3] == 1.0f &&
278 viewport->translate[0] == 0.0f &&
279 viewport->translate[1] == 0.0f &&
280 viewport->translate[2] == 0.0f &&
281 viewport->translate[3] == 0.0f);
282
283 draw_vs_set_viewport( draw, viewport );
284 }
285
286
287
288 void
289 draw_set_vertex_buffers(struct draw_context *draw,
290 unsigned count,
291 const struct pipe_vertex_buffer *buffers)
292 {
293 assert(count <= PIPE_MAX_ATTRIBS);
294
295 memcpy(draw->pt.vertex_buffer, buffers, count * sizeof(buffers[0]));
296 draw->pt.nr_vertex_buffers = count;
297 }
298
299
300 void
301 draw_set_vertex_elements(struct draw_context *draw,
302 unsigned count,
303 const struct pipe_vertex_element *elements)
304 {
305 assert(count <= PIPE_MAX_ATTRIBS);
306
307 memcpy(draw->pt.vertex_element, elements, count * sizeof(elements[0]));
308 draw->pt.nr_vertex_elements = count;
309 }
310
311
312 /**
313 * Tell drawing context where to find mapped vertex buffers.
314 */
315 void
316 draw_set_mapped_vertex_buffer(struct draw_context *draw,
317 unsigned attr, const void *buffer)
318 {
319 draw->pt.user.vbuffer[attr] = buffer;
320 }
321
322
323 void
324 draw_set_mapped_constant_buffer(struct draw_context *draw,
325 unsigned shader_type,
326 unsigned slot,
327 const void *buffer,
328 unsigned size )
329 {
330 debug_assert(shader_type == PIPE_SHADER_VERTEX ||
331 shader_type == PIPE_SHADER_GEOMETRY);
332 debug_assert(slot < PIPE_MAX_CONSTANT_BUFFERS);
333
334 switch (shader_type) {
335 case PIPE_SHADER_VERTEX:
336 draw->pt.user.vs_constants[slot] = buffer;
337 draw->pt.user.vs_constants_size[slot] = size;
338 draw_vs_set_constants(draw, slot, buffer, size);
339 break;
340 case PIPE_SHADER_GEOMETRY:
341 draw->pt.user.gs_constants[slot] = buffer;
342 draw->pt.user.gs_constants_size[slot] = size;
343 draw_gs_set_constants(draw, slot, buffer, size);
344 break;
345 default:
346 assert(0 && "invalid shader type in draw_set_mapped_constant_buffer");
347 }
348 }
349
350
351 /**
352 * Tells the draw module to draw points with triangles if their size
353 * is greater than this threshold.
354 */
355 void
356 draw_wide_point_threshold(struct draw_context *draw, float threshold)
357 {
358 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
359 draw->pipeline.wide_point_threshold = threshold;
360 }
361
362
363 /**
364 * Should the draw module handle point->quad conversion for drawing sprites?
365 */
366 void
367 draw_wide_point_sprites(struct draw_context *draw, boolean draw_sprite)
368 {
369 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
370 draw->pipeline.wide_point_sprites = draw_sprite;
371 }
372
373
374 /**
375 * Tells the draw module to draw lines with triangles if their width
376 * is greater than this threshold.
377 */
378 void
379 draw_wide_line_threshold(struct draw_context *draw, float threshold)
380 {
381 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
382 draw->pipeline.wide_line_threshold = threshold;
383 }
384
385
386 /**
387 * Tells the draw module whether or not to implement line stipple.
388 */
389 void
390 draw_enable_line_stipple(struct draw_context *draw, boolean enable)
391 {
392 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
393 draw->pipeline.line_stipple = enable;
394 }
395
396
397 /**
398 * Tells draw module whether to convert points to quads for sprite mode.
399 */
400 void
401 draw_enable_point_sprites(struct draw_context *draw, boolean enable)
402 {
403 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
404 draw->pipeline.point_sprite = enable;
405 }
406
407
408 void
409 draw_set_force_passthrough( struct draw_context *draw, boolean enable )
410 {
411 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
412 draw->force_passthrough = enable;
413 }
414
415
416 /**
417 * Ask the draw module for the location/slot of the given vertex attribute in
418 * a post-transformed vertex.
419 *
420 * With this function, drivers that use the draw module should have no reason
421 * to track the current vertex/geometry shader.
422 *
423 * Note that the draw module may sometimes generate vertices with extra
424 * attributes (such as texcoords for AA lines). The driver can call this
425 * function to find those attributes.
426 *
427 * Zero is returned if the attribute is not found since this is
428 * a don't care / undefined situtation. Returning -1 would be a bit more
429 * work for the drivers.
430 */
431 int
432 draw_find_shader_output(const struct draw_context *draw,
433 uint semantic_name, uint semantic_index)
434 {
435 const struct draw_vertex_shader *vs = draw->vs.vertex_shader;
436 const struct draw_geometry_shader *gs = draw->gs.geometry_shader;
437 uint i;
438 const struct tgsi_shader_info *info = &vs->info;
439
440 if (gs)
441 info = &gs->info;
442
443 for (i = 0; i < info->num_outputs; i++) {
444 if (info->output_semantic_name[i] == semantic_name &&
445 info->output_semantic_index[i] == semantic_index)
446 return i;
447 }
448
449 /* XXX there may be more than one extra vertex attrib.
450 * For example, simulated gl_FragCoord and gl_PointCoord.
451 */
452 if (draw->extra_shader_outputs.semantic_name == semantic_name &&
453 draw->extra_shader_outputs.semantic_index == semantic_index) {
454 return draw->extra_shader_outputs.slot;
455 }
456
457 return 0;
458 }
459
460
461 /**
462 * Return total number of the shader outputs. This function is similar to
463 * draw_current_shader_outputs() but this function also counts any extra
464 * vertex/geometry output attributes that may be filled in by some draw
465 * stages (such as AA point, AA line).
466 *
467 * If geometry shader is present, its output will be returned,
468 * if not vertex shader is used.
469 */
470 uint
471 draw_num_shader_outputs(const struct draw_context *draw)
472 {
473 uint count = draw->vs.vertex_shader->info.num_outputs;
474
475 /* If a geometry shader is present, its outputs go to the
476 * driver, else the vertex shader's outputs.
477 */
478 if (draw->gs.geometry_shader)
479 count = draw->gs.geometry_shader->info.num_outputs;
480
481 if (draw->extra_shader_outputs.slot > 0)
482 count++;
483 return count;
484 }
485
486
487 /**
488 * Provide TGSI sampler objects for vertex/geometry shaders that use
489 * texture fetches.
490 * This might only be used by software drivers for the time being.
491 */
492 void
493 draw_texture_samplers(struct draw_context *draw,
494 uint shader,
495 uint num_samplers,
496 struct tgsi_sampler **samplers)
497 {
498 if (shader == PIPE_SHADER_VERTEX) {
499 draw->vs.num_samplers = num_samplers;
500 draw->vs.samplers = samplers;
501 } else {
502 debug_assert(shader == PIPE_SHADER_GEOMETRY);
503 draw->gs.num_samplers = num_samplers;
504 draw->gs.samplers = samplers;
505 }
506 }
507
508
509
510
511 void draw_set_render( struct draw_context *draw,
512 struct vbuf_render *render )
513 {
514 draw->render = render;
515 }
516
517
518 void
519 draw_set_index_buffer(struct draw_context *draw,
520 const struct pipe_index_buffer *ib)
521 {
522 if (ib)
523 memcpy(&draw->pt.index_buffer, ib, sizeof(draw->pt.index_buffer));
524 else
525 memset(&draw->pt.index_buffer, 0, sizeof(draw->pt.index_buffer));
526 }
527
528
529 /**
530 * Tell drawing context where to find mapped index/element buffer.
531 */
532 void
533 draw_set_mapped_index_buffer(struct draw_context *draw,
534 const void *elements)
535 {
536 draw->pt.user.elts = elements;
537 }
538
539
540 /* Revamp me please:
541 */
542 void draw_do_flush( struct draw_context *draw, unsigned flags )
543 {
544 if (!draw->suspend_flushing)
545 {
546 assert(!draw->flushing); /* catch inadvertant recursion */
547
548 draw->flushing = TRUE;
549
550 draw_pipeline_flush( draw, flags );
551
552 draw->reduced_prim = ~0; /* is reduced_prim needed any more? */
553
554 draw->flushing = FALSE;
555 }
556 }
557
558
559 /**
560 * Return the number of output attributes produced by the geometry
561 * shader, if present. If no geometry shader, return the number of
562 * outputs from the vertex shader.
563 * \sa draw_num_shader_outputs
564 */
565 uint
566 draw_current_shader_outputs(const struct draw_context *draw)
567 {
568 if (draw->gs.geometry_shader)
569 return draw->gs.num_gs_outputs;
570 return draw->vs.num_vs_outputs;
571 }
572
573
574 /**
575 * Return the index of the shader output which will contain the
576 * vertex position.
577 */
578 uint
579 draw_current_shader_position_output(const struct draw_context *draw)
580 {
581 if (draw->gs.geometry_shader)
582 return draw->gs.position_output;
583 return draw->vs.position_output;
584 }
585
586
587 /**
588 * Return a pointer/handle for a driver/CSO rasterizer object which
589 * disabled culling, stippling, unfilled tris, etc.
590 * This is used by some pipeline stages (such as wide_point, aa_line
591 * and aa_point) which convert points/lines into triangles. In those
592 * cases we don't want to accidentally cull the triangles.
593 *
594 * \param scissor should the rasterizer state enable scissoring?
595 * \param flatshade should the rasterizer state use flat shading?
596 * \return rasterizer CSO handle
597 */
598 void *
599 draw_get_rasterizer_no_cull( struct draw_context *draw,
600 boolean scissor,
601 boolean flatshade )
602 {
603 if (!draw->rasterizer_no_cull[scissor][flatshade]) {
604 /* create now */
605 struct pipe_context *pipe = draw->pipe;
606 struct pipe_rasterizer_state rast;
607
608 memset(&rast, 0, sizeof(rast));
609 rast.scissor = scissor;
610 rast.flatshade = flatshade;
611 rast.front_ccw = 1;
612 rast.gl_rasterization_rules = draw->rasterizer->gl_rasterization_rules;
613
614 draw->rasterizer_no_cull[scissor][flatshade] =
615 pipe->create_rasterizer_state(pipe, &rast);
616 }
617 return draw->rasterizer_no_cull[scissor][flatshade];
618 }
619
620 void
621 draw_set_mapped_so_buffers(struct draw_context *draw,
622 void *buffers[PIPE_MAX_SO_BUFFERS],
623 unsigned num_buffers)
624 {
625 int i;
626
627 for (i = 0; i < num_buffers; ++i) {
628 draw->so.buffers[i] = buffers[i];
629 }
630 draw->so.num_buffers = num_buffers;
631 }
632
633 void
634 draw_set_so_state(struct draw_context *draw,
635 struct pipe_stream_output_state *state)
636 {
637 memcpy(&draw->so.state,
638 state,
639 sizeof(struct pipe_stream_output_state));
640 }
641
642 void
643 draw_set_sampler_views(struct draw_context *draw,
644 struct pipe_sampler_view **views,
645 unsigned num)
646 {
647 unsigned i;
648
649 debug_assert(num <= PIPE_MAX_VERTEX_SAMPLERS);
650
651 for (i = 0; i < num; ++i)
652 draw->sampler_views[i] = views[i];
653 for (i = num; i < PIPE_MAX_VERTEX_SAMPLERS; ++i)
654 draw->sampler_views[i] = NULL;
655
656 draw->num_sampler_views = num;
657 }
658
659 void
660 draw_set_samplers(struct draw_context *draw,
661 struct pipe_sampler_state **samplers,
662 unsigned num)
663 {
664 unsigned i;
665
666 debug_assert(num <= PIPE_MAX_VERTEX_SAMPLERS);
667
668 for (i = 0; i < num; ++i)
669 draw->samplers[i] = samplers[i];
670 for (i = num; i < PIPE_MAX_VERTEX_SAMPLERS; ++i)
671 draw->samplers[i] = NULL;
672
673 draw->num_samplers = num;
674 }
675
676 void
677 draw_set_mapped_texture(struct draw_context *draw,
678 unsigned sampler_idx,
679 uint32_t width, uint32_t height, uint32_t depth,
680 uint32_t last_level,
681 uint32_t row_stride[DRAW_MAX_TEXTURE_LEVELS],
682 uint32_t img_stride[DRAW_MAX_TEXTURE_LEVELS],
683 const void *data[DRAW_MAX_TEXTURE_LEVELS])
684 {
685 #ifdef HAVE_LLVM
686 if(draw->llvm)
687 draw_llvm_set_mapped_texture(draw,
688 sampler_idx,
689 width, height, depth, last_level,
690 row_stride, img_stride, data);
691 #endif
692 }