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