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