draw: implement proper primitive assembler as a pipeline stage
[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 "util/u_inlines.h"
39 #include "util/u_helpers.h"
40 #include "util/u_prim.h"
41 #include "draw_context.h"
42 #include "draw_pipe.h"
43 #include "draw_vs.h"
44 #include "draw_gs.h"
45
46 #if HAVE_LLVM
47 #include "gallivm/lp_bld_init.h"
48 #include "gallivm/lp_bld_limits.h"
49 #include "draw_llvm.h"
50
51 boolean
52 draw_get_option_use_llvm(void)
53 {
54 static boolean first = TRUE;
55 static boolean value;
56 if (first) {
57 first = FALSE;
58 value = debug_get_bool_option("DRAW_USE_LLVM", TRUE);
59
60 #ifdef PIPE_ARCH_X86
61 util_cpu_detect();
62 /* require SSE2 due to LLVM PR6960. XXX Might be fixed by now? */
63 if (!util_cpu_caps.has_sse2)
64 value = FALSE;
65 #endif
66 }
67 return value;
68 }
69 #endif
70
71
72 /**
73 * Create new draw module context with gallivm state for LLVM JIT.
74 */
75 static struct draw_context *
76 draw_create_context(struct pipe_context *pipe, boolean try_llvm)
77 {
78 struct draw_context *draw = CALLOC_STRUCT( draw_context );
79 if (draw == NULL)
80 goto err_out;
81
82 /* we need correct cpu caps for disabling denorms in draw_vbo() */
83 util_cpu_detect();
84
85 #if HAVE_LLVM
86 if (try_llvm && draw_get_option_use_llvm()) {
87 draw->llvm = draw_llvm_create(draw);
88 if (!draw->llvm)
89 goto err_destroy;
90 }
91 #endif
92
93 draw->pipe = pipe;
94
95 if (!draw_init(draw))
96 goto err_destroy;
97
98 return draw;
99
100 err_destroy:
101 draw_destroy( draw );
102 err_out:
103 return NULL;
104 }
105
106
107 /**
108 * Create new draw module context, with LLVM JIT.
109 */
110 struct draw_context *
111 draw_create(struct pipe_context *pipe)
112 {
113 return draw_create_context(pipe, TRUE);
114 }
115
116
117 /**
118 * Create a new draw context, without LLVM JIT.
119 */
120 struct draw_context *
121 draw_create_no_llvm(struct pipe_context *pipe)
122 {
123 return draw_create_context(pipe, FALSE);
124 }
125
126
127 boolean draw_init(struct draw_context *draw)
128 {
129 /*
130 * Note that several functions compute the clipmask of the predefined
131 * formats with hardcoded formulas instead of using these. So modifications
132 * here must be reflected there too.
133 */
134
135 ASSIGN_4V( draw->plane[0], -1, 0, 0, 1 );
136 ASSIGN_4V( draw->plane[1], 1, 0, 0, 1 );
137 ASSIGN_4V( draw->plane[2], 0, -1, 0, 1 );
138 ASSIGN_4V( draw->plane[3], 0, 1, 0, 1 );
139 ASSIGN_4V( draw->plane[4], 0, 0, 1, 1 ); /* yes these are correct */
140 ASSIGN_4V( draw->plane[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
141 draw->clip_xy = TRUE;
142 draw->clip_z = TRUE;
143
144 draw->pt.user.planes = (float (*) [DRAW_TOTAL_CLIP_PLANES][4]) &(draw->plane[0]);
145 draw->pt.user.eltMax = ~0;
146
147 if (!draw_pipeline_init( draw ))
148 return FALSE;
149
150 if (!draw_pt_init( draw ))
151 return FALSE;
152
153 if (!draw_vs_init( draw ))
154 return FALSE;
155
156 if (!draw_gs_init( draw ))
157 return FALSE;
158
159 draw->quads_always_flatshade_last = !draw->pipe->screen->get_param(
160 draw->pipe->screen, PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION);
161
162 return TRUE;
163 }
164
165 /*
166 * Called whenever we're starting to draw a new instance.
167 * Some internal structures don't want to have to reset internal
168 * members on each invocation (because their state might have to persist
169 * between multiple primitive restart rendering call) but might have to
170 * for each new instance.
171 * This is particularly the case for primitive id's in geometry shader.
172 */
173 void draw_new_instance(struct draw_context *draw)
174 {
175 draw_geometry_shader_new_instance(draw->gs.geometry_shader);
176 }
177
178
179 void draw_destroy( struct draw_context *draw )
180 {
181 struct pipe_context *pipe;
182 unsigned i, j;
183
184 if (!draw)
185 return;
186
187 pipe = draw->pipe;
188
189 /* free any rasterizer CSOs that we may have created.
190 */
191 for (i = 0; i < 2; i++) {
192 for (j = 0; j < 2; j++) {
193 if (draw->rasterizer_no_cull[i][j]) {
194 pipe->delete_rasterizer_state(pipe, draw->rasterizer_no_cull[i][j]);
195 }
196 }
197 }
198
199 for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
200 pipe_resource_reference(&draw->pt.vertex_buffer[i].buffer, NULL);
201 }
202
203 /* Not so fast -- we're just borrowing this at the moment.
204 *
205 if (draw->render)
206 draw->render->destroy( draw->render );
207 */
208
209 draw_pipeline_destroy( draw );
210 draw_pt_destroy( draw );
211 draw_vs_destroy( draw );
212 draw_gs_destroy( draw );
213 #ifdef HAVE_LLVM
214 if (draw->llvm)
215 draw_llvm_destroy( draw->llvm );
216 #endif
217
218 FREE( draw );
219 }
220
221
222
223 void draw_flush( struct draw_context *draw )
224 {
225 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
226 }
227
228
229 /**
230 * Specify the Minimum Resolvable Depth factor for polygon offset.
231 * This factor potentially depends on the number of Z buffer bits,
232 * the rasterization algorithm and the arithmetic performed on Z
233 * values between vertex shading and rasterization. It will vary
234 * from one driver to another.
235 */
236 void draw_set_mrd(struct draw_context *draw, double mrd)
237 {
238 draw->mrd = mrd;
239 }
240
241
242 static void update_clip_flags( struct draw_context *draw )
243 {
244 draw->clip_xy = !draw->driver.bypass_clip_xy;
245 draw->guard_band_xy = (!draw->driver.bypass_clip_xy &&
246 draw->driver.guard_band_xy);
247 draw->clip_z = (!draw->driver.bypass_clip_z &&
248 draw->rasterizer && draw->rasterizer->depth_clip);
249 draw->clip_user = draw->rasterizer &&
250 draw->rasterizer->clip_plane_enable != 0;
251 }
252
253 /**
254 * Register new primitive rasterization/rendering state.
255 * This causes the drawing pipeline to be rebuilt.
256 */
257 void draw_set_rasterizer_state( struct draw_context *draw,
258 const struct pipe_rasterizer_state *raster,
259 void *rast_handle )
260 {
261 if (!draw->suspend_flushing) {
262 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
263
264 draw->rasterizer = raster;
265 draw->rast_handle = rast_handle;
266 update_clip_flags(draw);
267 }
268 }
269
270 /* With a little more work, llvmpipe will be able to turn this off and
271 * do its own x/y clipping.
272 *
273 * Some hardware can turn off clipping altogether - in particular any
274 * hardware with a TNL unit can do its own clipping, even if it is
275 * relying on the draw module for some other reason.
276 */
277 void draw_set_driver_clipping( struct draw_context *draw,
278 boolean bypass_clip_xy,
279 boolean bypass_clip_z,
280 boolean guard_band_xy)
281 {
282 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
283
284 draw->driver.bypass_clip_xy = bypass_clip_xy;
285 draw->driver.bypass_clip_z = bypass_clip_z;
286 draw->driver.guard_band_xy = guard_band_xy;
287 update_clip_flags(draw);
288 }
289
290
291 /**
292 * Plug in the primitive rendering/rasterization stage (which is the last
293 * stage in the drawing pipeline).
294 * This is provided by the device driver.
295 */
296 void draw_set_rasterize_stage( struct draw_context *draw,
297 struct draw_stage *stage )
298 {
299 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
300
301 draw->pipeline.rasterize = stage;
302 }
303
304
305 /**
306 * Set the draw module's clipping state.
307 */
308 void draw_set_clip_state( struct draw_context *draw,
309 const struct pipe_clip_state *clip )
310 {
311 draw_do_flush(draw, DRAW_FLUSH_PARAMETER_CHANGE);
312
313 memcpy(&draw->plane[6], clip->ucp, sizeof(clip->ucp));
314 }
315
316
317 /**
318 * Set the draw module's viewport state.
319 */
320 void draw_set_viewport_states( struct draw_context *draw,
321 unsigned start_slot,
322 unsigned num_viewports,
323 const struct pipe_viewport_state *vps )
324 {
325 const struct pipe_viewport_state *viewport = vps;
326 draw_do_flush(draw, DRAW_FLUSH_PARAMETER_CHANGE);
327
328 debug_assert(start_slot < PIPE_MAX_VIEWPORTS);
329 debug_assert((start_slot + num_viewports) <= PIPE_MAX_VIEWPORTS);
330
331 memcpy(draw->viewports + start_slot, vps,
332 sizeof(struct pipe_viewport_state) * num_viewports);
333
334 draw->identity_viewport = (num_viewports == 1) &&
335 (viewport->scale[0] == 1.0f &&
336 viewport->scale[1] == 1.0f &&
337 viewport->scale[2] == 1.0f &&
338 viewport->scale[3] == 1.0f &&
339 viewport->translate[0] == 0.0f &&
340 viewport->translate[1] == 0.0f &&
341 viewport->translate[2] == 0.0f &&
342 viewport->translate[3] == 0.0f);
343 }
344
345
346
347 void
348 draw_set_vertex_buffers(struct draw_context *draw,
349 unsigned start_slot, unsigned count,
350 const struct pipe_vertex_buffer *buffers)
351 {
352 assert(start_slot + count <= PIPE_MAX_ATTRIBS);
353
354 util_set_vertex_buffers_count(draw->pt.vertex_buffer,
355 &draw->pt.nr_vertex_buffers,
356 buffers, start_slot, count);
357 }
358
359
360 void
361 draw_set_vertex_elements(struct draw_context *draw,
362 unsigned count,
363 const struct pipe_vertex_element *elements)
364 {
365 assert(count <= PIPE_MAX_ATTRIBS);
366
367 /* We could improve this by only flushing the frontend and the fetch part
368 * of the middle. This would avoid recalculating the emit keys.*/
369 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
370
371 memcpy(draw->pt.vertex_element, elements, count * sizeof(elements[0]));
372 draw->pt.nr_vertex_elements = count;
373 }
374
375
376 /**
377 * Tell drawing context where to find mapped vertex buffers.
378 */
379 void
380 draw_set_mapped_vertex_buffer(struct draw_context *draw,
381 unsigned attr, const void *buffer,
382 size_t size)
383 {
384 draw->pt.user.vbuffer[attr].map = buffer;
385 draw->pt.user.vbuffer[attr].size = size;
386 }
387
388
389 void
390 draw_set_mapped_constant_buffer(struct draw_context *draw,
391 unsigned shader_type,
392 unsigned slot,
393 const void *buffer,
394 unsigned size )
395 {
396 debug_assert(shader_type == PIPE_SHADER_VERTEX ||
397 shader_type == PIPE_SHADER_GEOMETRY);
398 debug_assert(slot < PIPE_MAX_CONSTANT_BUFFERS);
399
400 draw_do_flush(draw, DRAW_FLUSH_PARAMETER_CHANGE);
401
402 switch (shader_type) {
403 case PIPE_SHADER_VERTEX:
404 draw->pt.user.vs_constants[slot] = buffer;
405 draw->pt.user.vs_constants_size[slot] = size;
406 break;
407 case PIPE_SHADER_GEOMETRY:
408 draw->pt.user.gs_constants[slot] = buffer;
409 draw->pt.user.gs_constants_size[slot] = size;
410 break;
411 default:
412 assert(0 && "invalid shader type in draw_set_mapped_constant_buffer");
413 }
414 }
415
416
417 /**
418 * Tells the draw module to draw points with triangles if their size
419 * is greater than this threshold.
420 */
421 void
422 draw_wide_point_threshold(struct draw_context *draw, float threshold)
423 {
424 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
425 draw->pipeline.wide_point_threshold = threshold;
426 }
427
428
429 /**
430 * Should the draw module handle point->quad conversion for drawing sprites?
431 */
432 void
433 draw_wide_point_sprites(struct draw_context *draw, boolean draw_sprite)
434 {
435 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
436 draw->pipeline.wide_point_sprites = draw_sprite;
437 }
438
439
440 /**
441 * Tells the draw module to draw lines with triangles if their width
442 * is greater than this threshold.
443 */
444 void
445 draw_wide_line_threshold(struct draw_context *draw, float threshold)
446 {
447 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
448 draw->pipeline.wide_line_threshold = roundf(threshold);
449 }
450
451
452 /**
453 * Tells the draw module whether or not to implement line stipple.
454 */
455 void
456 draw_enable_line_stipple(struct draw_context *draw, boolean enable)
457 {
458 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
459 draw->pipeline.line_stipple = enable;
460 }
461
462
463 /**
464 * Tells draw module whether to convert points to quads for sprite mode.
465 */
466 void
467 draw_enable_point_sprites(struct draw_context *draw, boolean enable)
468 {
469 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
470 draw->pipeline.point_sprite = enable;
471 }
472
473
474 void
475 draw_set_force_passthrough( struct draw_context *draw, boolean enable )
476 {
477 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
478 draw->force_passthrough = enable;
479 }
480
481
482
483 /**
484 * Allocate an extra vertex/geometry shader vertex attribute, if it doesn't
485 * exist already.
486 *
487 * This is used by some of the optional draw module stages such
488 * as wide_point which may need to allocate additional generic/texcoord
489 * attributes.
490 */
491 int
492 draw_alloc_extra_vertex_attrib(struct draw_context *draw,
493 uint semantic_name, uint semantic_index)
494 {
495 int slot;
496 uint num_outputs;
497 uint n;
498
499 slot = draw_find_shader_output(draw, semantic_name, semantic_index);
500 if (slot >= 0) {
501 return slot;
502 }
503
504 num_outputs = draw_current_shader_outputs(draw);
505 n = draw->extra_shader_outputs.num;
506
507 assert(n < Elements(draw->extra_shader_outputs.semantic_name));
508
509 draw->extra_shader_outputs.semantic_name[n] = semantic_name;
510 draw->extra_shader_outputs.semantic_index[n] = semantic_index;
511 draw->extra_shader_outputs.slot[n] = num_outputs + n;
512 draw->extra_shader_outputs.num++;
513
514 return draw->extra_shader_outputs.slot[n];
515 }
516
517
518 /**
519 * Remove all extra vertex attributes that were allocated with
520 * draw_alloc_extra_vertex_attrib().
521 */
522 void
523 draw_remove_extra_vertex_attribs(struct draw_context *draw)
524 {
525 draw->extra_shader_outputs.num = 0;
526 }
527
528
529 /**
530 * If a geometry shader is present, return its info, else the vertex shader's
531 * info.
532 */
533 struct tgsi_shader_info *
534 draw_get_shader_info(const struct draw_context *draw)
535 {
536
537 if (draw->gs.geometry_shader) {
538 return &draw->gs.geometry_shader->info;
539 } else {
540 return &draw->vs.vertex_shader->info;
541 }
542 }
543
544 /**
545 * Prepare outputs slots from the draw module
546 *
547 * Certain parts of the draw module can emit additional
548 * outputs that can be quite useful to the backends, a good
549 * example of it is the process of decomposing primitives
550 * into wireframes (aka. lines) which normally would lose
551 * the face-side information, but using this method we can
552 * inject another shader output which passes the original
553 * face side information to the backend.
554 */
555 void
556 draw_prepare_shader_outputs(struct draw_context *draw)
557 {
558 draw_ia_prepare_outputs(draw, draw->pipeline.ia);
559 draw_unfilled_prepare_outputs(draw, draw->pipeline.unfilled);
560 }
561
562 /**
563 * Ask the draw module for the location/slot of the given vertex attribute in
564 * a post-transformed vertex.
565 *
566 * With this function, drivers that use the draw module should have no reason
567 * to track the current vertex/geometry shader.
568 *
569 * Note that the draw module may sometimes generate vertices with extra
570 * attributes (such as texcoords for AA lines). The driver can call this
571 * function to find those attributes.
572 *
573 * -1 is returned if the attribute is not found since this is
574 * an undefined situation. Note, that zero is valid and can
575 * be used by any of the attributes, because position is not
576 * required to be attribute 0 or even at all present.
577 */
578 int
579 draw_find_shader_output(const struct draw_context *draw,
580 uint semantic_name, uint semantic_index)
581 {
582 const struct tgsi_shader_info *info = draw_get_shader_info(draw);
583 uint i;
584
585 for (i = 0; i < info->num_outputs; i++) {
586 if (info->output_semantic_name[i] == semantic_name &&
587 info->output_semantic_index[i] == semantic_index)
588 return i;
589 }
590
591 /* Search the extra vertex attributes */
592 for (i = 0; i < draw->extra_shader_outputs.num; i++) {
593 if (draw->extra_shader_outputs.semantic_name[i] == semantic_name &&
594 draw->extra_shader_outputs.semantic_index[i] == semantic_index) {
595 return draw->extra_shader_outputs.slot[i];
596 }
597 }
598
599 return -1;
600 }
601
602
603 /**
604 * Return total number of the shader outputs. This function is similar to
605 * draw_current_shader_outputs() but this function also counts any extra
606 * vertex/geometry output attributes that may be filled in by some draw
607 * stages (such as AA point, AA line).
608 *
609 * If geometry shader is present, its output will be returned,
610 * if not vertex shader is used.
611 */
612 uint
613 draw_num_shader_outputs(const struct draw_context *draw)
614 {
615 const struct tgsi_shader_info *info = draw_get_shader_info(draw);
616 uint count;
617
618 count = info->num_outputs;
619 count += draw->extra_shader_outputs.num;
620
621 return count;
622 }
623
624
625 /**
626 * Return total number of the vertex shader outputs. This function
627 * also counts any extra vertex output attributes that may
628 * be filled in by some draw stages (such as AA point, AA line,
629 * front face).
630 */
631 uint
632 draw_total_vs_outputs(const struct draw_context *draw)
633 {
634 const struct tgsi_shader_info *info = &draw->vs.vertex_shader->info;
635
636 return info->num_outputs + draw->extra_shader_outputs.num;;
637 }
638
639 /**
640 * Return total number of the geometry shader outputs. This function
641 * also counts any extra geometry output attributes that may
642 * be filled in by some draw stages (such as AA point, AA line, front
643 * face).
644 */
645 uint
646 draw_total_gs_outputs(const struct draw_context *draw)
647 {
648 const struct tgsi_shader_info *info;
649
650 if (!draw->gs.geometry_shader)
651 return 0;
652
653 info = &draw->gs.geometry_shader->info;
654
655 return info->num_outputs + draw->extra_shader_outputs.num;
656 }
657
658
659 /**
660 * Provide TGSI sampler objects for vertex/geometry shaders that use
661 * texture fetches. This state only needs to be set once per context.
662 * This might only be used by software drivers for the time being.
663 */
664 void
665 draw_texture_sampler(struct draw_context *draw,
666 uint shader,
667 struct tgsi_sampler *sampler)
668 {
669 if (shader == PIPE_SHADER_VERTEX) {
670 draw->vs.tgsi.sampler = sampler;
671 } else {
672 debug_assert(shader == PIPE_SHADER_GEOMETRY);
673 draw->gs.tgsi.sampler = sampler;
674 }
675 }
676
677
678
679
680 void draw_set_render( struct draw_context *draw,
681 struct vbuf_render *render )
682 {
683 draw->render = render;
684 }
685
686
687 /**
688 * Tell the draw module where vertex indexes/elements are located, and
689 * their size (in bytes).
690 *
691 * Note: the caller must apply the pipe_index_buffer::offset value to
692 * the address. The draw module doesn't do that.
693 */
694 void
695 draw_set_indexes(struct draw_context *draw,
696 const void *elements, unsigned elem_size,
697 unsigned elem_buffer_space)
698 {
699 assert(elem_size == 0 ||
700 elem_size == 1 ||
701 elem_size == 2 ||
702 elem_size == 4);
703 draw->pt.user.elts = elements;
704 draw->pt.user.eltSizeIB = elem_size;
705 if (elem_size)
706 draw->pt.user.eltMax = elem_buffer_space / elem_size;
707 else
708 draw->pt.user.eltMax = 0;
709 }
710
711
712 /* Revamp me please:
713 */
714 void draw_do_flush( struct draw_context *draw, unsigned flags )
715 {
716 if (!draw->suspend_flushing)
717 {
718 assert(!draw->flushing); /* catch inadvertant recursion */
719
720 draw->flushing = TRUE;
721
722 draw_pipeline_flush( draw, flags );
723
724 draw_pt_flush( draw, flags );
725
726 draw->flushing = FALSE;
727 }
728 }
729
730
731 /**
732 * Return the number of output attributes produced by the geometry
733 * shader, if present. If no geometry shader, return the number of
734 * outputs from the vertex shader.
735 * \sa draw_num_shader_outputs
736 */
737 uint
738 draw_current_shader_outputs(const struct draw_context *draw)
739 {
740 if (draw->gs.geometry_shader)
741 return draw->gs.num_gs_outputs;
742 return draw->vs.num_vs_outputs;
743 }
744
745
746 /**
747 * Return the index of the shader output which will contain the
748 * vertex position.
749 */
750 uint
751 draw_current_shader_position_output(const struct draw_context *draw)
752 {
753 if (draw->gs.geometry_shader)
754 return draw->gs.position_output;
755 return draw->vs.position_output;
756 }
757
758
759 /**
760 * Return the index of the shader output which will contain the
761 * viewport index.
762 */
763 uint
764 draw_current_shader_viewport_index_output(const struct draw_context *draw)
765 {
766 if (draw->gs.geometry_shader)
767 return draw->gs.geometry_shader->viewport_index_output;
768 return 0;
769 }
770
771 /**
772 * Returns true if there's a geometry shader bound and the geometry
773 * shader writes out a viewport index.
774 */
775 boolean
776 draw_current_shader_uses_viewport_index(const struct draw_context *draw)
777 {
778 if (draw->gs.geometry_shader)
779 return draw->gs.geometry_shader->info.writes_viewport_index;
780 return FALSE;
781 }
782
783
784 /**
785 * Return the index of the shader output which will contain the
786 * vertex position.
787 */
788 uint
789 draw_current_shader_clipvertex_output(const struct draw_context *draw)
790 {
791 return draw->vs.clipvertex_output;
792 }
793
794 uint
795 draw_current_shader_clipdistance_output(const struct draw_context *draw, int index)
796 {
797 debug_assert(index < PIPE_MAX_CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT);
798 if (draw->gs.geometry_shader)
799 return draw->gs.geometry_shader->clipdistance_output[index];
800 return draw->vs.clipdistance_output[index];
801 }
802
803
804 uint
805 draw_current_shader_num_written_clipdistances(const struct draw_context *draw)
806 {
807 if (draw->gs.geometry_shader)
808 return draw->gs.geometry_shader->info.num_written_clipdistance;
809 return draw->vs.vertex_shader->info.num_written_clipdistance;
810 }
811
812
813 uint
814 draw_current_shader_culldistance_output(const struct draw_context *draw, int index)
815 {
816 debug_assert(index < PIPE_MAX_CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT);
817 if (draw->gs.geometry_shader)
818 return draw->gs.geometry_shader->culldistance_output[index];
819 return draw->vs.vertex_shader->culldistance_output[index];
820 }
821
822 uint
823 draw_current_shader_num_written_culldistances(const struct draw_context *draw)
824 {
825 if (draw->gs.geometry_shader)
826 return draw->gs.geometry_shader->info.num_written_culldistance;
827 return draw->vs.vertex_shader->info.num_written_culldistance;
828 }
829
830 /**
831 * Return a pointer/handle for a driver/CSO rasterizer object which
832 * disabled culling, stippling, unfilled tris, etc.
833 * This is used by some pipeline stages (such as wide_point, aa_line
834 * and aa_point) which convert points/lines into triangles. In those
835 * cases we don't want to accidentally cull the triangles.
836 *
837 * \param scissor should the rasterizer state enable scissoring?
838 * \param flatshade should the rasterizer state use flat shading?
839 * \return rasterizer CSO handle
840 */
841 void *
842 draw_get_rasterizer_no_cull( struct draw_context *draw,
843 boolean scissor,
844 boolean flatshade )
845 {
846 if (!draw->rasterizer_no_cull[scissor][flatshade]) {
847 /* create now */
848 struct pipe_context *pipe = draw->pipe;
849 struct pipe_rasterizer_state rast;
850
851 memset(&rast, 0, sizeof(rast));
852 rast.scissor = scissor;
853 rast.flatshade = flatshade;
854 rast.front_ccw = 1;
855 rast.half_pixel_center = draw->rasterizer->half_pixel_center;
856 rast.bottom_edge_rule = draw->rasterizer->bottom_edge_rule;
857 rast.clip_halfz = draw->rasterizer->clip_halfz;
858
859 draw->rasterizer_no_cull[scissor][flatshade] =
860 pipe->create_rasterizer_state(pipe, &rast);
861 }
862 return draw->rasterizer_no_cull[scissor][flatshade];
863 }
864
865 void
866 draw_set_mapped_so_targets(struct draw_context *draw,
867 int num_targets,
868 struct draw_so_target *targets[PIPE_MAX_SO_BUFFERS])
869 {
870 int i;
871
872 for (i = 0; i < num_targets; i++)
873 draw->so.targets[i] = targets[i];
874 for (i = num_targets; i < PIPE_MAX_SO_BUFFERS; i++)
875 draw->so.targets[i] = NULL;
876
877 draw->so.num_targets = num_targets;
878 }
879
880 void
881 draw_set_sampler_views(struct draw_context *draw,
882 unsigned shader_stage,
883 struct pipe_sampler_view **views,
884 unsigned num)
885 {
886 unsigned i;
887
888 debug_assert(shader_stage < PIPE_SHADER_TYPES);
889 debug_assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
890
891 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
892
893 for (i = 0; i < num; ++i)
894 draw->sampler_views[shader_stage][i] = views[i];
895 for (i = num; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; ++i)
896 draw->sampler_views[shader_stage][i] = NULL;
897
898 draw->num_sampler_views[shader_stage] = num;
899 }
900
901 void
902 draw_set_samplers(struct draw_context *draw,
903 unsigned shader_stage,
904 struct pipe_sampler_state **samplers,
905 unsigned num)
906 {
907 unsigned i;
908
909 debug_assert(shader_stage < PIPE_SHADER_TYPES);
910 debug_assert(num <= PIPE_MAX_SAMPLERS);
911
912 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
913
914 for (i = 0; i < num; ++i)
915 draw->samplers[shader_stage][i] = samplers[i];
916 for (i = num; i < PIPE_MAX_SAMPLERS; ++i)
917 draw->samplers[shader_stage][i] = NULL;
918
919 draw->num_samplers[shader_stage] = num;
920
921 #ifdef HAVE_LLVM
922 if (draw->llvm)
923 draw_llvm_set_sampler_state(draw, shader_stage);
924 #endif
925 }
926
927 void
928 draw_set_mapped_texture(struct draw_context *draw,
929 unsigned shader_stage,
930 unsigned sview_idx,
931 uint32_t width, uint32_t height, uint32_t depth,
932 uint32_t first_level, uint32_t last_level,
933 const void *base_ptr,
934 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
935 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
936 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS])
937 {
938 #ifdef HAVE_LLVM
939 if (draw->llvm)
940 draw_llvm_set_mapped_texture(draw,
941 shader_stage,
942 sview_idx,
943 width, height, depth, first_level,
944 last_level, base_ptr,
945 row_stride, img_stride, mip_offsets);
946 #endif
947 }
948
949 /**
950 * XXX: Results for PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS because there are two
951 * different ways of setting textures, and drivers typically only support one.
952 */
953 int
954 draw_get_shader_param_no_llvm(unsigned shader, enum pipe_shader_cap param)
955 {
956 switch(shader) {
957 case PIPE_SHADER_VERTEX:
958 case PIPE_SHADER_GEOMETRY:
959 return tgsi_exec_get_shader_param(param);
960 default:
961 return 0;
962 }
963 }
964
965 /**
966 * XXX: Results for PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS because there are two
967 * different ways of setting textures, and drivers typically only support one.
968 */
969 int
970 draw_get_shader_param(unsigned shader, enum pipe_shader_cap param)
971 {
972
973 #ifdef HAVE_LLVM
974 if (draw_get_option_use_llvm()) {
975 switch(shader) {
976 case PIPE_SHADER_VERTEX:
977 case PIPE_SHADER_GEOMETRY:
978 return gallivm_get_shader_param(param);
979 default:
980 return 0;
981 }
982 }
983 #endif
984
985 return draw_get_shader_param_no_llvm(shader, param);
986 }
987
988 /**
989 * Enables or disables collection of statistics.
990 *
991 * Draw module is capable of generating statistics for the vertex
992 * processing pipeline. Collection of that data isn't free and so
993 * it's disabled by default. The users of the module can enable
994 * (or disable) this functionality through this function.
995 * The actual data will be emitted through the VBUF interface,
996 * the 'pipeline_statistics' callback to be exact.
997 */
998 void
999 draw_collect_pipeline_statistics(struct draw_context *draw,
1000 boolean enable)
1001 {
1002 draw->collect_statistics = enable;
1003 }
1004
1005 /**
1006 * Computes clipper invocation statistics.
1007 *
1008 * Figures out how many primitives would have been
1009 * sent to the clipper given the specified
1010 * prim info data.
1011 */
1012 void
1013 draw_stats_clipper_primitives(struct draw_context *draw,
1014 const struct draw_prim_info *prim_info)
1015 {
1016 if (draw->collect_statistics) {
1017 unsigned start, i;
1018 for (start = i = 0;
1019 i < prim_info->primitive_count;
1020 start += prim_info->primitive_lengths[i], i++)
1021 {
1022 draw->statistics.c_invocations +=
1023 u_decomposed_prims_for_vertices(prim_info->prim,
1024 prim_info->primitive_lengths[i]);
1025 }
1026 }
1027 }
1028
1029
1030 /**
1031 * Returns true if the draw module will inject the frontface
1032 * info into the outputs.
1033 *
1034 * Given the specified primitive and rasterizer state
1035 * the function will figure out if the draw module
1036 * will inject the front-face information into shader
1037 * outputs. This is done to preserve the front-facing
1038 * info when decomposing primitives into wireframes.
1039 */
1040 boolean
1041 draw_will_inject_frontface(const struct draw_context *draw)
1042 {
1043 unsigned reduced_prim = u_reduced_prim(draw->pt.prim);
1044 const struct pipe_rasterizer_state *rast = draw->rasterizer;
1045
1046 if (reduced_prim != PIPE_PRIM_TRIANGLES) {
1047 return FALSE;
1048 }
1049
1050 return (rast &&
1051 (rast->fill_front != PIPE_POLYGON_MODE_FILL ||
1052 rast->fill_back != PIPE_POLYGON_MODE_FILL));
1053 }