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