i915g: remove old bind_vertex/fragment_sampler_states() hooks
[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 if (draw->pipeline.aapoint)
568 draw_aapoint_prepare_outputs(draw, draw->pipeline.aapoint);
569 if (draw->pipeline.aaline)
570 draw_aaline_prepare_outputs(draw, draw->pipeline.aaline);
571 }
572
573 /**
574 * Ask the draw module for the location/slot of the given vertex attribute in
575 * a post-transformed vertex.
576 *
577 * With this function, drivers that use the draw module should have no reason
578 * to track the current vertex/geometry shader.
579 *
580 * Note that the draw module may sometimes generate vertices with extra
581 * attributes (such as texcoords for AA lines). The driver can call this
582 * function to find those attributes.
583 *
584 * -1 is returned if the attribute is not found since this is
585 * an undefined situation. Note, that zero is valid and can
586 * be used by any of the attributes, because position is not
587 * required to be attribute 0 or even at all present.
588 */
589 int
590 draw_find_shader_output(const struct draw_context *draw,
591 uint semantic_name, uint semantic_index)
592 {
593 const struct tgsi_shader_info *info = draw_get_shader_info(draw);
594 uint i;
595
596 for (i = 0; i < info->num_outputs; i++) {
597 if (info->output_semantic_name[i] == semantic_name &&
598 info->output_semantic_index[i] == semantic_index)
599 return i;
600 }
601
602 /* Search the extra vertex attributes */
603 for (i = 0; i < draw->extra_shader_outputs.num; i++) {
604 if (draw->extra_shader_outputs.semantic_name[i] == semantic_name &&
605 draw->extra_shader_outputs.semantic_index[i] == semantic_index) {
606 return draw->extra_shader_outputs.slot[i];
607 }
608 }
609
610 return -1;
611 }
612
613
614 /**
615 * Return total number of the shader outputs. This function is similar to
616 * draw_current_shader_outputs() but this function also counts any extra
617 * vertex/geometry output attributes that may be filled in by some draw
618 * stages (such as AA point, AA line).
619 *
620 * If geometry shader is present, its output will be returned,
621 * if not vertex shader is used.
622 */
623 uint
624 draw_num_shader_outputs(const struct draw_context *draw)
625 {
626 const struct tgsi_shader_info *info = draw_get_shader_info(draw);
627 uint count;
628
629 count = info->num_outputs;
630 count += draw->extra_shader_outputs.num;
631
632 return count;
633 }
634
635
636 /**
637 * Return total number of the vertex shader outputs. This function
638 * also counts any extra vertex output attributes that may
639 * be filled in by some draw stages (such as AA point, AA line,
640 * front face).
641 */
642 uint
643 draw_total_vs_outputs(const struct draw_context *draw)
644 {
645 const struct tgsi_shader_info *info = &draw->vs.vertex_shader->info;
646
647 return info->num_outputs + draw->extra_shader_outputs.num;;
648 }
649
650 /**
651 * Return total number of the geometry shader outputs. This function
652 * also counts any extra geometry output attributes that may
653 * be filled in by some draw stages (such as AA point, AA line, front
654 * face).
655 */
656 uint
657 draw_total_gs_outputs(const struct draw_context *draw)
658 {
659 const struct tgsi_shader_info *info;
660
661 if (!draw->gs.geometry_shader)
662 return 0;
663
664 info = &draw->gs.geometry_shader->info;
665
666 return info->num_outputs + draw->extra_shader_outputs.num;
667 }
668
669
670 /**
671 * Provide TGSI sampler objects for vertex/geometry shaders that use
672 * texture fetches. This state only needs to be set once per context.
673 * This might only be used by software drivers for the time being.
674 */
675 void
676 draw_texture_sampler(struct draw_context *draw,
677 uint shader,
678 struct tgsi_sampler *sampler)
679 {
680 if (shader == PIPE_SHADER_VERTEX) {
681 draw->vs.tgsi.sampler = sampler;
682 } else {
683 debug_assert(shader == PIPE_SHADER_GEOMETRY);
684 draw->gs.tgsi.sampler = sampler;
685 }
686 }
687
688
689
690
691 void draw_set_render( struct draw_context *draw,
692 struct vbuf_render *render )
693 {
694 draw->render = render;
695 }
696
697
698 /**
699 * Tell the draw module where vertex indexes/elements are located, and
700 * their size (in bytes).
701 *
702 * Note: the caller must apply the pipe_index_buffer::offset value to
703 * the address. The draw module doesn't do that.
704 */
705 void
706 draw_set_indexes(struct draw_context *draw,
707 const void *elements, unsigned elem_size,
708 unsigned elem_buffer_space)
709 {
710 assert(elem_size == 0 ||
711 elem_size == 1 ||
712 elem_size == 2 ||
713 elem_size == 4);
714 draw->pt.user.elts = elements;
715 draw->pt.user.eltSizeIB = elem_size;
716 if (elem_size)
717 draw->pt.user.eltMax = elem_buffer_space / elem_size;
718 else
719 draw->pt.user.eltMax = 0;
720 }
721
722
723 /* Revamp me please:
724 */
725 void draw_do_flush( struct draw_context *draw, unsigned flags )
726 {
727 if (!draw->suspend_flushing)
728 {
729 assert(!draw->flushing); /* catch inadvertant recursion */
730
731 draw->flushing = TRUE;
732
733 draw_pipeline_flush( draw, flags );
734
735 draw_pt_flush( draw, flags );
736
737 draw->flushing = FALSE;
738 }
739 }
740
741
742 /**
743 * Return the number of output attributes produced by the geometry
744 * shader, if present. If no geometry shader, return the number of
745 * outputs from the vertex shader.
746 * \sa draw_num_shader_outputs
747 */
748 uint
749 draw_current_shader_outputs(const struct draw_context *draw)
750 {
751 if (draw->gs.geometry_shader)
752 return draw->gs.num_gs_outputs;
753 return draw->vs.num_vs_outputs;
754 }
755
756
757 /**
758 * Return the index of the shader output which will contain the
759 * vertex position.
760 */
761 uint
762 draw_current_shader_position_output(const struct draw_context *draw)
763 {
764 if (draw->gs.geometry_shader)
765 return draw->gs.position_output;
766 return draw->vs.position_output;
767 }
768
769
770 /**
771 * Return the index of the shader output which will contain the
772 * viewport index.
773 */
774 uint
775 draw_current_shader_viewport_index_output(const struct draw_context *draw)
776 {
777 if (draw->gs.geometry_shader)
778 return draw->gs.geometry_shader->viewport_index_output;
779 return 0;
780 }
781
782 /**
783 * Returns true if there's a geometry shader bound and the geometry
784 * shader writes out a viewport index.
785 */
786 boolean
787 draw_current_shader_uses_viewport_index(const struct draw_context *draw)
788 {
789 if (draw->gs.geometry_shader)
790 return draw->gs.geometry_shader->info.writes_viewport_index;
791 return FALSE;
792 }
793
794
795 /**
796 * Return the index of the shader output which will contain the
797 * vertex position.
798 */
799 uint
800 draw_current_shader_clipvertex_output(const struct draw_context *draw)
801 {
802 return draw->vs.clipvertex_output;
803 }
804
805 uint
806 draw_current_shader_clipdistance_output(const struct draw_context *draw, int index)
807 {
808 debug_assert(index < PIPE_MAX_CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT);
809 if (draw->gs.geometry_shader)
810 return draw->gs.geometry_shader->clipdistance_output[index];
811 return draw->vs.clipdistance_output[index];
812 }
813
814
815 uint
816 draw_current_shader_num_written_clipdistances(const struct draw_context *draw)
817 {
818 if (draw->gs.geometry_shader)
819 return draw->gs.geometry_shader->info.num_written_clipdistance;
820 return draw->vs.vertex_shader->info.num_written_clipdistance;
821 }
822
823
824 uint
825 draw_current_shader_culldistance_output(const struct draw_context *draw, int index)
826 {
827 debug_assert(index < PIPE_MAX_CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT);
828 if (draw->gs.geometry_shader)
829 return draw->gs.geometry_shader->culldistance_output[index];
830 return draw->vs.vertex_shader->culldistance_output[index];
831 }
832
833 uint
834 draw_current_shader_num_written_culldistances(const struct draw_context *draw)
835 {
836 if (draw->gs.geometry_shader)
837 return draw->gs.geometry_shader->info.num_written_culldistance;
838 return draw->vs.vertex_shader->info.num_written_culldistance;
839 }
840
841 /**
842 * Return a pointer/handle for a driver/CSO rasterizer object which
843 * disabled culling, stippling, unfilled tris, etc.
844 * This is used by some pipeline stages (such as wide_point, aa_line
845 * and aa_point) which convert points/lines into triangles. In those
846 * cases we don't want to accidentally cull the triangles.
847 *
848 * \param scissor should the rasterizer state enable scissoring?
849 * \param flatshade should the rasterizer state use flat shading?
850 * \return rasterizer CSO handle
851 */
852 void *
853 draw_get_rasterizer_no_cull( struct draw_context *draw,
854 boolean scissor,
855 boolean flatshade )
856 {
857 if (!draw->rasterizer_no_cull[scissor][flatshade]) {
858 /* create now */
859 struct pipe_context *pipe = draw->pipe;
860 struct pipe_rasterizer_state rast;
861
862 memset(&rast, 0, sizeof(rast));
863 rast.scissor = scissor;
864 rast.flatshade = flatshade;
865 rast.front_ccw = 1;
866 rast.half_pixel_center = draw->rasterizer->half_pixel_center;
867 rast.bottom_edge_rule = draw->rasterizer->bottom_edge_rule;
868 rast.clip_halfz = draw->rasterizer->clip_halfz;
869
870 draw->rasterizer_no_cull[scissor][flatshade] =
871 pipe->create_rasterizer_state(pipe, &rast);
872 }
873 return draw->rasterizer_no_cull[scissor][flatshade];
874 }
875
876 void
877 draw_set_mapped_so_targets(struct draw_context *draw,
878 int num_targets,
879 struct draw_so_target *targets[PIPE_MAX_SO_BUFFERS])
880 {
881 int i;
882
883 for (i = 0; i < num_targets; i++)
884 draw->so.targets[i] = targets[i];
885 for (i = num_targets; i < PIPE_MAX_SO_BUFFERS; i++)
886 draw->so.targets[i] = NULL;
887
888 draw->so.num_targets = num_targets;
889 }
890
891 void
892 draw_set_sampler_views(struct draw_context *draw,
893 unsigned shader_stage,
894 struct pipe_sampler_view **views,
895 unsigned num)
896 {
897 unsigned i;
898
899 debug_assert(shader_stage < PIPE_SHADER_TYPES);
900 debug_assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
901
902 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
903
904 for (i = 0; i < num; ++i)
905 draw->sampler_views[shader_stage][i] = views[i];
906 for (i = num; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; ++i)
907 draw->sampler_views[shader_stage][i] = NULL;
908
909 draw->num_sampler_views[shader_stage] = num;
910 }
911
912 void
913 draw_set_samplers(struct draw_context *draw,
914 unsigned shader_stage,
915 struct pipe_sampler_state **samplers,
916 unsigned num)
917 {
918 unsigned i;
919
920 debug_assert(shader_stage < PIPE_SHADER_TYPES);
921 debug_assert(num <= PIPE_MAX_SAMPLERS);
922
923 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
924
925 for (i = 0; i < num; ++i)
926 draw->samplers[shader_stage][i] = samplers[i];
927 for (i = num; i < PIPE_MAX_SAMPLERS; ++i)
928 draw->samplers[shader_stage][i] = NULL;
929
930 draw->num_samplers[shader_stage] = num;
931
932 #ifdef HAVE_LLVM
933 if (draw->llvm)
934 draw_llvm_set_sampler_state(draw, shader_stage);
935 #endif
936 }
937
938 void
939 draw_set_mapped_texture(struct draw_context *draw,
940 unsigned shader_stage,
941 unsigned sview_idx,
942 uint32_t width, uint32_t height, uint32_t depth,
943 uint32_t first_level, uint32_t last_level,
944 const void *base_ptr,
945 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
946 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
947 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS])
948 {
949 #ifdef HAVE_LLVM
950 if (draw->llvm)
951 draw_llvm_set_mapped_texture(draw,
952 shader_stage,
953 sview_idx,
954 width, height, depth, first_level,
955 last_level, base_ptr,
956 row_stride, img_stride, mip_offsets);
957 #endif
958 }
959
960 /**
961 * XXX: Results for PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS because there are two
962 * different ways of setting textures, and drivers typically only support one.
963 */
964 int
965 draw_get_shader_param_no_llvm(unsigned shader, enum pipe_shader_cap param)
966 {
967 switch(shader) {
968 case PIPE_SHADER_VERTEX:
969 case PIPE_SHADER_GEOMETRY:
970 return tgsi_exec_get_shader_param(param);
971 default:
972 return 0;
973 }
974 }
975
976 /**
977 * XXX: Results for PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS because there are two
978 * different ways of setting textures, and drivers typically only support one.
979 */
980 int
981 draw_get_shader_param(unsigned shader, enum pipe_shader_cap param)
982 {
983
984 #ifdef HAVE_LLVM
985 if (draw_get_option_use_llvm()) {
986 switch(shader) {
987 case PIPE_SHADER_VERTEX:
988 case PIPE_SHADER_GEOMETRY:
989 return gallivm_get_shader_param(param);
990 default:
991 return 0;
992 }
993 }
994 #endif
995
996 return draw_get_shader_param_no_llvm(shader, param);
997 }
998
999 /**
1000 * Enables or disables collection of statistics.
1001 *
1002 * Draw module is capable of generating statistics for the vertex
1003 * processing pipeline. Collection of that data isn't free and so
1004 * it's disabled by default. The users of the module can enable
1005 * (or disable) this functionality through this function.
1006 * The actual data will be emitted through the VBUF interface,
1007 * the 'pipeline_statistics' callback to be exact.
1008 */
1009 void
1010 draw_collect_pipeline_statistics(struct draw_context *draw,
1011 boolean enable)
1012 {
1013 draw->collect_statistics = enable;
1014 }
1015
1016 /**
1017 * Computes clipper invocation statistics.
1018 *
1019 * Figures out how many primitives would have been
1020 * sent to the clipper given the specified
1021 * prim info data.
1022 */
1023 void
1024 draw_stats_clipper_primitives(struct draw_context *draw,
1025 const struct draw_prim_info *prim_info)
1026 {
1027 if (draw->collect_statistics) {
1028 unsigned start, i;
1029 for (start = i = 0;
1030 i < prim_info->primitive_count;
1031 start += prim_info->primitive_lengths[i], i++)
1032 {
1033 draw->statistics.c_invocations +=
1034 u_decomposed_prims_for_vertices(prim_info->prim,
1035 prim_info->primitive_lengths[i]);
1036 }
1037 }
1038 }
1039
1040
1041 /**
1042 * Returns true if the draw module will inject the frontface
1043 * info into the outputs.
1044 *
1045 * Given the specified primitive and rasterizer state
1046 * the function will figure out if the draw module
1047 * will inject the front-face information into shader
1048 * outputs. This is done to preserve the front-facing
1049 * info when decomposing primitives into wireframes.
1050 */
1051 boolean
1052 draw_will_inject_frontface(const struct draw_context *draw)
1053 {
1054 unsigned reduced_prim = u_reduced_prim(draw->pt.prim);
1055 const struct pipe_rasterizer_state *rast = draw->rasterizer;
1056
1057 if (reduced_prim != PIPE_PRIM_TRIANGLES) {
1058 return FALSE;
1059 }
1060
1061 return (rast &&
1062 (rast->fill_front != PIPE_POLYGON_MODE_FILL ||
1063 rast->fill_back != PIPE_POLYGON_MODE_FILL));
1064 }