• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_inlines.h"
38 #include "util/u_helpers.h"
39 #include "util/u_prim.h"
40 #include "util/format/u_format.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 #include "draw_tess.h"
47 
48 #ifdef DRAW_LLVM_AVAILABLE
49 #include "gallivm/lp_bld_init.h"
50 #include "gallivm/lp_bld_limits.h"
51 #include "draw_llvm.h"
52 
53 boolean
draw_get_option_use_llvm(void)54 draw_get_option_use_llvm(void)
55 {
56    return debug_get_bool_option("DRAW_USE_LLVM", TRUE);
57 }
58 #else
59 boolean
draw_get_option_use_llvm(void)60 draw_get_option_use_llvm(void)
61 {
62    return FALSE;
63 }
64 #endif
65 
66 bool
draw_has_llvm(void)67 draw_has_llvm(void)
68 {
69 #ifdef DRAW_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 *
draw_create_context(struct pipe_context * pipe,void * context,boolean try_llvm)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 #ifdef DRAW_LLVM_AVAILABLE
88    if (try_llvm && draw_get_option_use_llvm()) {
89       draw->llvm = draw_llvm_create(draw, (LLVMContextRef)context);
90    }
91 #endif
92 
93    draw->pipe = pipe;
94    draw->constant_buffer_stride = (sizeof(float) * 4);
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 *
draw_create(struct pipe_context * pipe)116 draw_create(struct pipe_context *pipe)
117 {
118    return draw_create_context(pipe, NULL, TRUE);
119 }
120 
121 
122 #ifdef DRAW_LLVM_AVAILABLE
123 struct draw_context *
draw_create_with_llvm_context(struct pipe_context * pipe,void * context)124 draw_create_with_llvm_context(struct pipe_context *pipe,
125                               void *context)
126 {
127    return draw_create_context(pipe, context, TRUE);
128 }
129 #endif
130 
131 /**
132  * Create a new draw context, without LLVM JIT.
133  */
134 struct draw_context *
draw_create_no_llvm(struct pipe_context * pipe)135 draw_create_no_llvm(struct pipe_context *pipe)
136 {
137    return draw_create_context(pipe, NULL, FALSE);
138 }
139 
140 
draw_init(struct draw_context * draw)141 boolean draw_init(struct draw_context *draw)
142 {
143    /*
144     * Note that several functions compute the clipmask of the predefined
145     * formats with hardcoded formulas instead of using these. So modifications
146     * here must be reflected there too.
147     */
148 
149    ASSIGN_4V( draw->plane[0], -1,  0,  0, 1 );
150    ASSIGN_4V( draw->plane[1],  1,  0,  0, 1 );
151    ASSIGN_4V( draw->plane[2],  0, -1,  0, 1 );
152    ASSIGN_4V( draw->plane[3],  0,  1,  0, 1 );
153    ASSIGN_4V( draw->plane[4],  0,  0,  1, 1 ); /* yes these are correct */
154    ASSIGN_4V( draw->plane[5],  0,  0, -1, 1 ); /* mesa's a bit wonky */
155    draw->clip_xy = TRUE;
156    draw->clip_z = TRUE;
157 
158    draw->pt.user.planes = (float (*) [DRAW_TOTAL_CLIP_PLANES][4]) &(draw->plane[0]);
159    draw->pt.user.eltMax = ~0;
160 
161    if (!draw_pipeline_init( draw ))
162       return FALSE;
163 
164    if (!draw_pt_init( draw ))
165       return FALSE;
166 
167    if (!draw_vs_init( draw ))
168       return FALSE;
169 
170    if (!draw_gs_init( draw ))
171       return FALSE;
172 
173    draw->quads_always_flatshade_last = !draw->pipe->screen->get_param(
174       draw->pipe->screen, PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION);
175 
176    draw->floating_point_depth = false;
177 
178    return TRUE;
179 }
180 
181 /*
182  * Called whenever we're starting to draw a new instance.
183  * Some internal structures don't want to have to reset internal
184  * members on each invocation (because their state might have to persist
185  * between multiple primitive restart rendering call) but might have to
186  * for each new instance.
187  * This is particularly the case for primitive id's in geometry shader.
188  */
draw_new_instance(struct draw_context * draw)189 void draw_new_instance(struct draw_context *draw)
190 {
191    draw_geometry_shader_new_instance(draw->gs.geometry_shader);
192    draw_prim_assembler_new_instance(draw->ia);
193 }
194 
195 
draw_destroy(struct draw_context * draw)196 void draw_destroy( struct draw_context *draw )
197 {
198    struct pipe_context *pipe;
199    unsigned i, j, k;
200 
201    if (!draw)
202       return;
203 
204    pipe = draw->pipe;
205 
206    /* free any rasterizer CSOs that we may have created.
207     */
208    for (i = 0; i < 2; i++) {
209       for (j = 0; j < 2; j++) {
210          for (k = 0; k < 2; k++) {
211             if (draw->rasterizer_no_cull[i][j][k]) {
212                pipe->delete_rasterizer_state(pipe, draw->rasterizer_no_cull[i][j][k]);
213             }
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 DRAW_LLVM_AVAILABLE
233    if (draw->llvm)
234       draw_llvm_destroy( draw->llvm );
235 #endif
236 
237    FREE( draw );
238 }
239 
240 
241 
draw_flush(struct draw_context * draw)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  */
draw_set_zs_format(struct draw_context * draw,enum pipe_format format)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
draw_is_vs_window_space(struct draw_context * draw)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
draw_update_clip_flags(struct draw_context * draw)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
draw_update_viewport_flags(struct draw_context * draw)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  */
draw_set_rasterizer_state(struct draw_context * draw,const struct pipe_rasterizer_state * raster,void * rast_handle)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  */
draw_set_driver_clipping(struct draw_context * draw,boolean bypass_clip_xy,boolean bypass_clip_z,boolean guard_band_xy,boolean bypass_clip_points)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  */
draw_set_rasterize_stage(struct draw_context * draw,struct draw_stage * stage)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  */
draw_set_clip_state(struct draw_context * draw,const struct pipe_clip_state * clip)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  */
draw_set_viewport_states(struct draw_context * draw,unsigned start_slot,unsigned num_viewports,const struct pipe_viewport_state * vps)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    assert(start_slot < PIPE_MAX_VIEWPORTS);
390    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
draw_set_vertex_buffers(struct draw_context * draw,unsigned start_slot,unsigned count,unsigned unbind_num_trailing_slots,const struct pipe_vertex_buffer * buffers)408 draw_set_vertex_buffers(struct draw_context *draw,
409                         unsigned start_slot, unsigned count,
410                         unsigned unbind_num_trailing_slots,
411                         const struct pipe_vertex_buffer *buffers)
412 {
413    assert(start_slot + count <= PIPE_MAX_ATTRIBS);
414 
415    util_set_vertex_buffers_count(draw->pt.vertex_buffer,
416                                  &draw->pt.nr_vertex_buffers,
417                                  buffers, start_slot, count,
418                                  unbind_num_trailing_slots, false);
419 }
420 
421 
422 void
draw_set_vertex_elements(struct draw_context * draw,unsigned count,const struct pipe_vertex_element * elements)423 draw_set_vertex_elements(struct draw_context *draw,
424                          unsigned count,
425                          const struct pipe_vertex_element *elements)
426 {
427    assert(count <= PIPE_MAX_ATTRIBS);
428 
429    /* We could improve this by only flushing the frontend and the fetch part
430     * of the middle. This would avoid recalculating the emit keys.*/
431    draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
432 
433    memcpy(draw->pt.vertex_element, elements, count * sizeof(elements[0]));
434    draw->pt.nr_vertex_elements = count;
435 }
436 
437 
438 /**
439  * Tell drawing context where to find mapped vertex buffers.
440  */
441 void
draw_set_mapped_vertex_buffer(struct draw_context * draw,unsigned attr,const void * buffer,size_t size)442 draw_set_mapped_vertex_buffer(struct draw_context *draw,
443                               unsigned attr, const void *buffer,
444                               size_t size)
445 {
446    draw->pt.user.vbuffer[attr].map  = buffer;
447    draw->pt.user.vbuffer[attr].size = size;
448 }
449 
450 
451 void
draw_set_mapped_constant_buffer(struct draw_context * draw,enum pipe_shader_type shader_type,unsigned slot,const void * buffer,unsigned size)452 draw_set_mapped_constant_buffer(struct draw_context *draw,
453                                 enum pipe_shader_type shader_type,
454                                 unsigned slot,
455                                 const void *buffer,
456                                 unsigned size )
457 {
458    assert(shader_type == PIPE_SHADER_VERTEX ||
459                 shader_type == PIPE_SHADER_GEOMETRY ||
460                 shader_type == PIPE_SHADER_TESS_CTRL ||
461                 shader_type == PIPE_SHADER_TESS_EVAL);
462    assert(slot < PIPE_MAX_CONSTANT_BUFFERS);
463 
464    draw_do_flush(draw, DRAW_FLUSH_PARAMETER_CHANGE);
465 
466    switch (shader_type) {
467    case PIPE_SHADER_VERTEX:
468       draw->pt.user.vs_constants[slot] = buffer;
469       draw->pt.user.vs_constants_size[slot] = size;
470       break;
471    case PIPE_SHADER_GEOMETRY:
472       draw->pt.user.gs_constants[slot] = buffer;
473       draw->pt.user.gs_constants_size[slot] = size;
474       break;
475    case PIPE_SHADER_TESS_CTRL:
476       draw->pt.user.tcs_constants[slot] = buffer;
477       draw->pt.user.tcs_constants_size[slot] = size;
478       break;
479    case PIPE_SHADER_TESS_EVAL:
480       draw->pt.user.tes_constants[slot] = buffer;
481       draw->pt.user.tes_constants_size[slot] = size;
482       break;
483    default:
484       assert(0 && "invalid shader type in draw_set_mapped_constant_buffer");
485    }
486 }
487 
488 void
draw_set_mapped_shader_buffer(struct draw_context * draw,enum pipe_shader_type shader_type,unsigned slot,const void * buffer,unsigned size)489 draw_set_mapped_shader_buffer(struct draw_context *draw,
490                               enum pipe_shader_type shader_type,
491                               unsigned slot,
492                               const void *buffer,
493                               unsigned size )
494 {
495    assert(shader_type == PIPE_SHADER_VERTEX ||
496                 shader_type == PIPE_SHADER_GEOMETRY ||
497                 shader_type == PIPE_SHADER_TESS_CTRL ||
498                 shader_type == PIPE_SHADER_TESS_EVAL);
499    assert(slot < PIPE_MAX_SHADER_BUFFERS);
500 
501    draw_do_flush(draw, DRAW_FLUSH_PARAMETER_CHANGE);
502 
503    switch (shader_type) {
504    case PIPE_SHADER_VERTEX:
505       draw->pt.user.vs_ssbos[slot] = buffer;
506       draw->pt.user.vs_ssbos_size[slot] = size;
507       break;
508    case PIPE_SHADER_GEOMETRY:
509       draw->pt.user.gs_ssbos[slot] = buffer;
510       draw->pt.user.gs_ssbos_size[slot] = size;
511       break;
512    case PIPE_SHADER_TESS_CTRL:
513       draw->pt.user.tcs_ssbos[slot] = buffer;
514       draw->pt.user.tcs_ssbos_size[slot] = size;
515       break;
516    case PIPE_SHADER_TESS_EVAL:
517       draw->pt.user.tes_ssbos[slot] = buffer;
518       draw->pt.user.tes_ssbos_size[slot] = size;
519       break;
520    default:
521       assert(0 && "invalid shader type in draw_set_mapped_shader_buffer");
522    }
523 }
524 
525 /**
526  * Tells the draw module to draw points with triangles if their size
527  * is greater than this threshold.
528  */
529 void
draw_wide_point_threshold(struct draw_context * draw,float threshold)530 draw_wide_point_threshold(struct draw_context *draw, float threshold)
531 {
532    draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
533    draw->pipeline.wide_point_threshold = threshold;
534 }
535 
536 
537 /**
538  * Should the draw module handle point->quad conversion for drawing sprites?
539  */
540 void
draw_wide_point_sprites(struct draw_context * draw,boolean draw_sprite)541 draw_wide_point_sprites(struct draw_context *draw, boolean draw_sprite)
542 {
543    draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
544    draw->pipeline.wide_point_sprites = draw_sprite;
545 }
546 
547 
548 /**
549  * Tells the draw module to draw lines with triangles if their width
550  * is greater than this threshold.
551  */
552 void
draw_wide_line_threshold(struct draw_context * draw,float threshold)553 draw_wide_line_threshold(struct draw_context *draw, float threshold)
554 {
555    draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
556    draw->pipeline.wide_line_threshold = roundf(threshold);
557 }
558 
559 
560 /**
561  * Tells the draw module whether or not to implement line stipple.
562  */
563 void
draw_enable_line_stipple(struct draw_context * draw,boolean enable)564 draw_enable_line_stipple(struct draw_context *draw, boolean enable)
565 {
566    draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
567    draw->pipeline.line_stipple = enable;
568 }
569 
570 
571 /**
572  * Tells draw module whether to convert points to quads for sprite mode.
573  */
574 void
draw_enable_point_sprites(struct draw_context * draw,boolean enable)575 draw_enable_point_sprites(struct draw_context *draw, boolean enable)
576 {
577    draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
578    draw->pipeline.point_sprite = enable;
579 }
580 
581 
582 
583 /**
584  * Allocate an extra vertex/geometry shader vertex attribute, if it doesn't
585  * exist already.
586  *
587  * This is used by some of the optional draw module stages such
588  * as wide_point which may need to allocate additional generic/texcoord
589  * attributes.
590  */
591 int
draw_alloc_extra_vertex_attrib(struct draw_context * draw,uint semantic_name,uint semantic_index)592 draw_alloc_extra_vertex_attrib(struct draw_context *draw,
593                                uint semantic_name, uint semantic_index)
594 {
595    int slot;
596    uint num_outputs;
597    uint n;
598 
599    slot = draw_find_shader_output(draw, semantic_name, semantic_index);
600    if (slot >= 0) {
601       return slot;
602    }
603 
604    num_outputs = draw_current_shader_outputs(draw);
605    n = draw->extra_shader_outputs.num;
606 
607    assert(n < ARRAY_SIZE(draw->extra_shader_outputs.semantic_name));
608 
609    draw->extra_shader_outputs.semantic_name[n] = semantic_name;
610    draw->extra_shader_outputs.semantic_index[n] = semantic_index;
611    draw->extra_shader_outputs.slot[n] = num_outputs + n;
612    draw->extra_shader_outputs.num++;
613 
614    return draw->extra_shader_outputs.slot[n];
615 }
616 
617 
618 /**
619  * Remove all extra vertex attributes that were allocated with
620  * draw_alloc_extra_vertex_attrib().
621  */
622 void
draw_remove_extra_vertex_attribs(struct draw_context * draw)623 draw_remove_extra_vertex_attribs(struct draw_context *draw)
624 {
625    draw->extra_shader_outputs.num = 0;
626 }
627 
628 
629 /**
630  * If a geometry shader is present, return its info, else the vertex shader's
631  * info.
632  */
633 struct tgsi_shader_info *
draw_get_shader_info(const struct draw_context * draw)634 draw_get_shader_info(const struct draw_context *draw)
635 {
636 
637    if (draw->gs.geometry_shader) {
638       return &draw->gs.geometry_shader->info;
639    } else if (draw->tes.tess_eval_shader) {
640       return &draw->tes.tess_eval_shader->info;
641    } else {
642       return &draw->vs.vertex_shader->info;
643    }
644 }
645 
646 /**
647  * Prepare outputs slots from the draw module
648  *
649  * Certain parts of the draw module can emit additional
650  * outputs that can be quite useful to the backends, a good
651  * example of it is the process of decomposing primitives
652  * into wireframes (aka. lines) which normally would lose
653  * the face-side information, but using this method we can
654  * inject another shader output which passes the original
655  * face side information to the backend.
656  */
657 void
draw_prepare_shader_outputs(struct draw_context * draw)658 draw_prepare_shader_outputs(struct draw_context *draw)
659 {
660    draw_remove_extra_vertex_attribs(draw);
661    draw_prim_assembler_prepare_outputs(draw->ia);
662    draw_unfilled_prepare_outputs(draw, draw->pipeline.unfilled);
663    if (draw->pipeline.aapoint)
664       draw_aapoint_prepare_outputs(draw, draw->pipeline.aapoint);
665    if (draw->pipeline.aaline)
666       draw_aaline_prepare_outputs(draw, draw->pipeline.aaline);
667 }
668 
669 /**
670  * Ask the draw module for the location/slot of the given vertex attribute in
671  * a post-transformed vertex.
672  *
673  * With this function, drivers that use the draw module should have no reason
674  * to track the current vertex/geometry shader.
675  *
676  * Note that the draw module may sometimes generate vertices with extra
677  * attributes (such as texcoords for AA lines).  The driver can call this
678  * function to find those attributes.
679  *
680  * -1 is returned if the attribute is not found since this is
681  * an undefined situation. Note, that zero is valid and can
682  * be used by any of the attributes, because position is not
683  * required to be attribute 0 or even at all present.
684  */
685 int
draw_find_shader_output(const struct draw_context * draw,uint semantic_name,uint semantic_index)686 draw_find_shader_output(const struct draw_context *draw,
687                         uint semantic_name, uint semantic_index)
688 {
689    const struct tgsi_shader_info *info = draw_get_shader_info(draw);
690    uint i;
691 
692    for (i = 0; i < info->num_outputs; i++) {
693       if (info->output_semantic_name[i] == semantic_name &&
694           info->output_semantic_index[i] == semantic_index)
695          return i;
696    }
697 
698    /* Search the extra vertex attributes */
699    for (i = 0; i < draw->extra_shader_outputs.num; i++) {
700       if (draw->extra_shader_outputs.semantic_name[i] == semantic_name &&
701           draw->extra_shader_outputs.semantic_index[i] == semantic_index) {
702          return draw->extra_shader_outputs.slot[i];
703       }
704    }
705 
706    return -1;
707 }
708 
709 
710 /**
711  * Return total number of the shader outputs.  This function is similar to
712  * draw_current_shader_outputs() but this function also counts any extra
713  * vertex/geometry output attributes that may be filled in by some draw
714  * stages (such as AA point, AA line).
715  *
716  * If geometry shader is present, its output will be returned,
717  * if not vertex shader is used.
718  */
719 uint
draw_num_shader_outputs(const struct draw_context * draw)720 draw_num_shader_outputs(const struct draw_context *draw)
721 {
722    const struct tgsi_shader_info *info = draw_get_shader_info(draw);
723    uint count;
724 
725    count = info->num_outputs;
726    count += draw->extra_shader_outputs.num;
727 
728    return count;
729 }
730 
731 
732 /**
733  * Return total number of the vertex shader outputs.  This function
734  * also counts any extra vertex output attributes that may
735  * be filled in by some draw stages (such as AA point, AA line,
736  * front face).
737  */
738 uint
draw_total_vs_outputs(const struct draw_context * draw)739 draw_total_vs_outputs(const struct draw_context *draw)
740 {
741    const struct tgsi_shader_info *info = &draw->vs.vertex_shader->info;
742 
743    return info->num_outputs + draw->extra_shader_outputs.num;
744 }
745 
746 /**
747  * Return total number of the geometry shader outputs. This function
748  * also counts any extra geometry output attributes that may
749  * be filled in by some draw stages (such as AA point, AA line, front
750  * face).
751  */
752 uint
draw_total_gs_outputs(const struct draw_context * draw)753 draw_total_gs_outputs(const struct draw_context *draw)
754 {
755    const struct tgsi_shader_info *info;
756 
757    if (!draw->gs.geometry_shader)
758       return 0;
759 
760    info = &draw->gs.geometry_shader->info;
761 
762    return info->num_outputs + draw->extra_shader_outputs.num;
763 }
764 
765 /**
766  * Return total number of the tess ctrl shader outputs.
767  */
768 uint
draw_total_tcs_outputs(const struct draw_context * draw)769 draw_total_tcs_outputs(const struct draw_context *draw)
770 {
771    const struct tgsi_shader_info *info;
772 
773    if (!draw->tcs.tess_ctrl_shader)
774       return 0;
775 
776    info = &draw->tcs.tess_ctrl_shader->info;
777 
778    return info->num_outputs;
779 }
780 
781 /**
782  * Return total number of the tess eval shader outputs.
783  */
784 uint
draw_total_tes_outputs(const struct draw_context * draw)785 draw_total_tes_outputs(const struct draw_context *draw)
786 {
787    const struct tgsi_shader_info *info;
788 
789    if (!draw->tes.tess_eval_shader)
790       return 0;
791 
792    info = &draw->tes.tess_eval_shader->info;
793 
794    return info->num_outputs + draw->extra_shader_outputs.num;
795 }
796 
797 /**
798  * Provide TGSI sampler objects for vertex/geometry shaders that use
799  * texture fetches.  This state only needs to be set once per context.
800  * This might only be used by software drivers for the time being.
801  */
802 void
draw_texture_sampler(struct draw_context * draw,enum pipe_shader_type shader,struct tgsi_sampler * sampler)803 draw_texture_sampler(struct draw_context *draw,
804                      enum pipe_shader_type shader,
805                      struct tgsi_sampler *sampler)
806 {
807    switch (shader) {
808    case PIPE_SHADER_VERTEX:
809       draw->vs.tgsi.sampler = sampler;
810       break;
811    case PIPE_SHADER_GEOMETRY:
812       draw->gs.tgsi.sampler = sampler;
813       break;
814    case PIPE_SHADER_TESS_CTRL:
815       draw->tcs.tgsi.sampler = sampler;
816       break;
817    case PIPE_SHADER_TESS_EVAL:
818       draw->tes.tgsi.sampler = sampler;
819       break;
820    default:
821       assert(0);
822       break;
823    }
824 }
825 
826 /**
827  * Provide TGSI image objects for vertex/geometry shaders that use
828  * texture fetches.  This state only needs to be set once per context.
829  * This might only be used by software drivers for the time being.
830  */
831 void
draw_image(struct draw_context * draw,enum pipe_shader_type shader,struct tgsi_image * image)832 draw_image(struct draw_context *draw,
833            enum pipe_shader_type shader,
834            struct tgsi_image *image)
835 {
836    switch (shader) {
837    case PIPE_SHADER_VERTEX:
838       draw->vs.tgsi.image = image;
839       break;
840    case PIPE_SHADER_GEOMETRY:
841       draw->gs.tgsi.image = image;
842       break;
843    case PIPE_SHADER_TESS_CTRL:
844       draw->tcs.tgsi.image = image;
845       break;
846    case PIPE_SHADER_TESS_EVAL:
847       draw->tes.tgsi.image = image;
848       break;
849    default:
850       assert(0);
851       break;
852    }
853 }
854 
855 /**
856  * Provide TGSI buffer objects for vertex/geometry shaders that use
857  * load/store/atomic ops.  This state only needs to be set once per context.
858  * This might only be used by software drivers for the time being.
859  */
860 void
draw_buffer(struct draw_context * draw,enum pipe_shader_type shader,struct tgsi_buffer * buffer)861 draw_buffer(struct draw_context *draw,
862             enum pipe_shader_type shader,
863             struct tgsi_buffer *buffer)
864 {
865    switch (shader) {
866    case PIPE_SHADER_VERTEX:
867       draw->vs.tgsi.buffer = buffer;
868       break;
869    case PIPE_SHADER_GEOMETRY:
870       draw->gs.tgsi.buffer = buffer;
871       break;
872    case PIPE_SHADER_TESS_CTRL:
873       draw->tcs.tgsi.buffer = buffer;
874       break;
875    case PIPE_SHADER_TESS_EVAL:
876       draw->tes.tgsi.buffer = buffer;
877       break;
878    default:
879       assert(0);
880       break;
881    }
882 }
883 
884 
draw_set_render(struct draw_context * draw,struct vbuf_render * render)885 void draw_set_render( struct draw_context *draw,
886 		      struct vbuf_render *render )
887 {
888    draw->render = render;
889 }
890 
891 
892 /**
893  * Tell the draw module where vertex indexes/elements are located, and
894  * their size (in bytes).
895  */
896 void
draw_set_indexes(struct draw_context * draw,const void * elements,unsigned elem_size,unsigned elem_buffer_space)897 draw_set_indexes(struct draw_context *draw,
898                  const void *elements, unsigned elem_size,
899                  unsigned elem_buffer_space)
900 {
901    assert(elem_size == 0 ||
902           elem_size == 1 ||
903           elem_size == 2 ||
904           elem_size == 4);
905    draw->pt.user.elts = elements;
906    draw->pt.user.eltSizeIB = elem_size;
907    if (elem_size)
908       draw->pt.user.eltMax = elem_buffer_space / elem_size;
909    else
910       draw->pt.user.eltMax = 0;
911 }
912 
913 
914 /* Revamp me please:
915  */
draw_do_flush(struct draw_context * draw,unsigned flags)916 void draw_do_flush( struct draw_context *draw, unsigned flags )
917 {
918    if (!draw->suspend_flushing)
919    {
920       assert(!draw->flushing); /* catch inadvertant recursion */
921 
922       draw->flushing = TRUE;
923 
924       draw_pipeline_flush( draw, flags );
925 
926       draw_pt_flush( draw, flags );
927 
928       draw->flushing = FALSE;
929    }
930 }
931 
932 
933 /**
934  * Return the number of output attributes produced by the geometry
935  * shader, if present.  If no geometry shader, return the number of
936  * outputs from the vertex shader.
937  * \sa draw_num_shader_outputs
938  */
939 uint
draw_current_shader_outputs(const struct draw_context * draw)940 draw_current_shader_outputs(const struct draw_context *draw)
941 {
942    if (draw->gs.geometry_shader)
943       return draw->gs.num_gs_outputs;
944    if (draw->tes.tess_eval_shader)
945       return draw->tes.num_tes_outputs;
946    return draw->vs.num_vs_outputs;
947 }
948 
949 
950 /**
951  * Return the index of the shader output which will contain the
952  * vertex position.
953  */
954 uint
draw_current_shader_position_output(const struct draw_context * draw)955 draw_current_shader_position_output(const struct draw_context *draw)
956 {
957    if (draw->gs.geometry_shader)
958       return draw->gs.position_output;
959    if (draw->tes.tess_eval_shader)
960       return draw->tes.position_output;
961    return draw->vs.position_output;
962 }
963 
964 
965 /**
966  * Return the index of the shader output which will contain the
967  * viewport index.
968  */
969 uint
draw_current_shader_viewport_index_output(const struct draw_context * draw)970 draw_current_shader_viewport_index_output(const struct draw_context *draw)
971 {
972    if (draw->gs.geometry_shader)
973       return draw->gs.geometry_shader->viewport_index_output;
974    else if (draw->tes.tess_eval_shader)
975       return draw->tes.tess_eval_shader->viewport_index_output;
976    return draw->vs.vertex_shader->viewport_index_output;
977 }
978 
979 /**
980  * Returns true if there's a geometry shader bound and the geometry
981  * shader writes out a viewport index.
982  */
983 boolean
draw_current_shader_uses_viewport_index(const struct draw_context * draw)984 draw_current_shader_uses_viewport_index(const struct draw_context *draw)
985 {
986    if (draw->gs.geometry_shader)
987       return draw->gs.geometry_shader->info.writes_viewport_index;
988    else if (draw->tes.tess_eval_shader)
989       return draw->tes.tess_eval_shader->info.writes_viewport_index;
990    return draw->vs.vertex_shader->info.writes_viewport_index;
991 }
992 
993 
994 /**
995  * Return the index of the shader output which will contain the
996  * clip vertex position.
997  * Note we don't support clipvertex output in the gs. For clipping
998  * to work correctly hence we return ordinary position output instead.
999  */
1000 uint
draw_current_shader_clipvertex_output(const struct draw_context * draw)1001 draw_current_shader_clipvertex_output(const struct draw_context *draw)
1002 {
1003    if (draw->gs.geometry_shader)
1004       return draw->gs.clipvertex_output;
1005    if (draw->tes.tess_eval_shader)
1006       return draw->tes.clipvertex_output;
1007    return draw->vs.clipvertex_output;
1008 }
1009 
1010 uint
draw_current_shader_ccdistance_output(const struct draw_context * draw,int index)1011 draw_current_shader_ccdistance_output(const struct draw_context *draw, int index)
1012 {
1013    assert(index < PIPE_MAX_CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT);
1014    if (draw->gs.geometry_shader)
1015       return draw->gs.geometry_shader->ccdistance_output[index];
1016    if (draw->tes.tess_eval_shader)
1017       return draw->tes.tess_eval_shader->ccdistance_output[index];
1018    return draw->vs.ccdistance_output[index];
1019 }
1020 
1021 
1022 uint
draw_current_shader_num_written_clipdistances(const struct draw_context * draw)1023 draw_current_shader_num_written_clipdistances(const struct draw_context *draw)
1024 {
1025    if (draw->gs.geometry_shader)
1026       return draw->gs.geometry_shader->info.num_written_clipdistance;
1027    if (draw->tes.tess_eval_shader)
1028       return draw->tes.tess_eval_shader->info.num_written_clipdistance;
1029    return draw->vs.vertex_shader->info.num_written_clipdistance;
1030 }
1031 
1032 uint
draw_current_shader_num_written_culldistances(const struct draw_context * draw)1033 draw_current_shader_num_written_culldistances(const struct draw_context *draw)
1034 {
1035    if (draw->gs.geometry_shader)
1036       return draw->gs.geometry_shader->info.num_written_culldistance;
1037    if (draw->tes.tess_eval_shader)
1038       return draw->tes.tess_eval_shader->info.num_written_culldistance;
1039    return draw->vs.vertex_shader->info.num_written_culldistance;
1040 }
1041 
1042 /**
1043  * Return a pointer/handle for a driver/CSO rasterizer object which
1044  * disabled culling, stippling, unfilled tris, etc.
1045  * This is used by some pipeline stages (such as wide_point, aa_line
1046  * and aa_point) which convert points/lines into triangles.  In those
1047  * cases we don't want to accidentally cull the triangles.
1048  *
1049  * \param scissor  should the rasterizer state enable scissoring?
1050  * \param flatshade  should the rasterizer state use flat shading?
1051  * \return  rasterizer CSO handle
1052  */
1053 void *
draw_get_rasterizer_no_cull(struct draw_context * draw,const struct pipe_rasterizer_state * base_rast)1054 draw_get_rasterizer_no_cull( struct draw_context *draw,
1055                              const struct pipe_rasterizer_state *base_rast )
1056 {
1057    if (!draw->rasterizer_no_cull[base_rast->scissor][base_rast->flatshade][base_rast->rasterizer_discard]) {
1058       /* create now */
1059       struct pipe_context *pipe = draw->pipe;
1060       struct pipe_rasterizer_state rast;
1061 
1062       memset(&rast, 0, sizeof(rast));
1063       rast.scissor = base_rast->scissor;
1064       rast.flatshade = base_rast->flatshade;
1065       rast.rasterizer_discard = base_rast->rasterizer_discard;
1066       rast.front_ccw = 1;
1067       rast.half_pixel_center = draw->rasterizer->half_pixel_center;
1068       rast.bottom_edge_rule = draw->rasterizer->bottom_edge_rule;
1069       rast.clip_halfz = draw->rasterizer->clip_halfz;
1070 
1071       draw->rasterizer_no_cull[base_rast->scissor][base_rast->flatshade][base_rast->rasterizer_discard] =
1072          pipe->create_rasterizer_state(pipe, &rast);
1073    }
1074    return draw->rasterizer_no_cull[base_rast->scissor][base_rast->flatshade][base_rast->rasterizer_discard];
1075 }
1076 
1077 void
draw_set_mapped_so_targets(struct draw_context * draw,int num_targets,struct draw_so_target * targets[PIPE_MAX_SO_BUFFERS])1078 draw_set_mapped_so_targets(struct draw_context *draw,
1079                            int num_targets,
1080                            struct draw_so_target *targets[PIPE_MAX_SO_BUFFERS])
1081 {
1082    int i;
1083 
1084    draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
1085 
1086    for (i = 0; i < num_targets; i++)
1087       draw->so.targets[i] = targets[i];
1088    for (i = num_targets; i < PIPE_MAX_SO_BUFFERS; i++)
1089       draw->so.targets[i] = NULL;
1090 
1091    draw->so.num_targets = num_targets;
1092 }
1093 
1094 void
draw_set_sampler_views(struct draw_context * draw,enum pipe_shader_type shader_stage,struct pipe_sampler_view ** views,unsigned num)1095 draw_set_sampler_views(struct draw_context *draw,
1096                        enum pipe_shader_type shader_stage,
1097                        struct pipe_sampler_view **views,
1098                        unsigned num)
1099 {
1100    unsigned i;
1101 
1102    assert(shader_stage < PIPE_SHADER_TYPES);
1103    assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
1104 
1105    draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
1106 
1107    for (i = 0; i < num; ++i)
1108       draw->sampler_views[shader_stage][i] = views[i];
1109    for (i = num; i < draw->num_sampler_views[shader_stage]; ++i)
1110       draw->sampler_views[shader_stage][i] = NULL;
1111 
1112    draw->num_sampler_views[shader_stage] = num;
1113 }
1114 
1115 void
draw_set_samplers(struct draw_context * draw,enum pipe_shader_type shader_stage,struct pipe_sampler_state ** samplers,unsigned num)1116 draw_set_samplers(struct draw_context *draw,
1117                   enum pipe_shader_type shader_stage,
1118                   struct pipe_sampler_state **samplers,
1119                   unsigned num)
1120 {
1121    unsigned i;
1122 
1123    assert(shader_stage < PIPE_SHADER_TYPES);
1124    assert(num <= PIPE_MAX_SAMPLERS);
1125 
1126    draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
1127 
1128    for (i = 0; i < num; ++i)
1129       draw->samplers[shader_stage][i] = samplers[i];
1130    for (i = num; i < PIPE_MAX_SAMPLERS; ++i)
1131       draw->samplers[shader_stage][i] = NULL;
1132 
1133    draw->num_samplers[shader_stage] = num;
1134 
1135 #ifdef DRAW_LLVM_AVAILABLE
1136    if (draw->llvm)
1137       draw_llvm_set_sampler_state(draw, shader_stage);
1138 #endif
1139 }
1140 
1141 void
draw_set_images(struct draw_context * draw,enum pipe_shader_type shader_stage,struct pipe_image_view * views,unsigned num)1142 draw_set_images(struct draw_context *draw,
1143                 enum pipe_shader_type shader_stage,
1144                 struct pipe_image_view *views,
1145                 unsigned num)
1146 {
1147    unsigned i;
1148 
1149    assert(shader_stage < PIPE_SHADER_TYPES);
1150    assert(num <= PIPE_MAX_SHADER_IMAGES);
1151 
1152    draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
1153 
1154    for (i = 0; i < num; ++i)
1155       draw->images[shader_stage][i] = &views[i];
1156    for (i = num; i < draw->num_sampler_views[shader_stage]; ++i)
1157       draw->images[shader_stage][i] = NULL;
1158 
1159    draw->num_images[shader_stage] = num;
1160 }
1161 
1162 void
draw_set_mapped_texture(struct draw_context * draw,enum pipe_shader_type shader_stage,unsigned sview_idx,uint32_t width,uint32_t height,uint32_t depth,uint32_t first_level,uint32_t last_level,uint32_t num_samples,uint32_t sample_stride,const void * base_ptr,uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS])1163 draw_set_mapped_texture(struct draw_context *draw,
1164                         enum pipe_shader_type shader_stage,
1165                         unsigned sview_idx,
1166                         uint32_t width, uint32_t height, uint32_t depth,
1167                         uint32_t first_level, uint32_t last_level,
1168                         uint32_t num_samples,
1169                         uint32_t sample_stride,
1170                         const void *base_ptr,
1171                         uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
1172                         uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
1173                         uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS])
1174 {
1175 #ifdef DRAW_LLVM_AVAILABLE
1176    if (draw->llvm)
1177       draw_llvm_set_mapped_texture(draw,
1178                                    shader_stage,
1179                                    sview_idx,
1180                                    width, height, depth, first_level,
1181                                    last_level, num_samples, sample_stride, base_ptr,
1182                                    row_stride, img_stride, mip_offsets);
1183 #endif
1184 }
1185 
1186 void
draw_set_mapped_image(struct draw_context * draw,enum pipe_shader_type shader_stage,unsigned idx,uint32_t width,uint32_t height,uint32_t depth,const void * base_ptr,uint32_t row_stride,uint32_t img_stride,uint32_t num_samples,uint32_t sample_stride)1187 draw_set_mapped_image(struct draw_context *draw,
1188                       enum pipe_shader_type shader_stage,
1189                       unsigned idx,
1190                       uint32_t width, uint32_t height, uint32_t depth,
1191                       const void *base_ptr,
1192                       uint32_t row_stride,
1193                       uint32_t img_stride,
1194                       uint32_t num_samples,
1195                       uint32_t sample_stride)
1196 {
1197 #ifdef DRAW_LLVM_AVAILABLE
1198    if (draw->llvm)
1199       draw_llvm_set_mapped_image(draw,
1200                                  shader_stage,
1201                                  idx,
1202                                  width, height, depth,
1203                                  base_ptr,
1204                                  row_stride, img_stride,
1205                                  num_samples, sample_stride);
1206 #endif
1207 }
1208 
1209 /**
1210  * XXX: Results for PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS because there are two
1211  * different ways of setting textures, and drivers typically only support one.
1212  */
1213 int
draw_get_shader_param_no_llvm(enum pipe_shader_type shader,enum pipe_shader_cap param)1214 draw_get_shader_param_no_llvm(enum pipe_shader_type shader,
1215                               enum pipe_shader_cap param)
1216 {
1217    switch(shader) {
1218    case PIPE_SHADER_VERTEX:
1219    case PIPE_SHADER_GEOMETRY:
1220       return tgsi_exec_get_shader_param(param);
1221    default:
1222       return 0;
1223    }
1224 }
1225 
1226 /**
1227  * XXX: Results for PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS because there are two
1228  * different ways of setting textures, and drivers typically only support one.
1229  * Drivers requesting a draw context explicitly without llvm must call
1230  * draw_get_shader_param_no_llvm instead.
1231  */
1232 int
draw_get_shader_param(enum pipe_shader_type shader,enum pipe_shader_cap param)1233 draw_get_shader_param(enum pipe_shader_type shader, enum pipe_shader_cap param)
1234 {
1235 
1236 #ifdef DRAW_LLVM_AVAILABLE
1237    if (draw_get_option_use_llvm()) {
1238       switch(shader) {
1239       case PIPE_SHADER_VERTEX:
1240       case PIPE_SHADER_GEOMETRY:
1241       case PIPE_SHADER_TESS_CTRL:
1242       case PIPE_SHADER_TESS_EVAL:
1243          return gallivm_get_shader_param(param);
1244       default:
1245          return 0;
1246       }
1247    }
1248 #endif
1249 
1250    return draw_get_shader_param_no_llvm(shader, param);
1251 }
1252 
1253 /**
1254  * Enables or disables collection of statistics.
1255  *
1256  * Draw module is capable of generating statistics for the vertex
1257  * processing pipeline. Collection of that data isn't free and so
1258  * it's disabled by default. The users of the module can enable
1259  * (or disable) this functionality through this function.
1260  * The actual data will be emitted through the VBUF interface,
1261  * the 'pipeline_statistics' callback to be exact.
1262  */
1263 void
draw_collect_pipeline_statistics(struct draw_context * draw,boolean enable)1264 draw_collect_pipeline_statistics(struct draw_context *draw,
1265                                  boolean enable)
1266 {
1267    draw->collect_statistics = enable;
1268 }
1269 
1270 /**
1271  * Enable/disable primitives generated gathering.
1272  */
draw_collect_primitives_generated(struct draw_context * draw,bool enable)1273 void draw_collect_primitives_generated(struct draw_context *draw,
1274                                        bool enable)
1275 {
1276    draw->collect_primgen = enable;
1277 }
1278 
1279 /**
1280  * Computes clipper invocation statistics.
1281  *
1282  * Figures out how many primitives would have been
1283  * sent to the clipper given the specified
1284  * prim info data.
1285  */
1286 void
draw_stats_clipper_primitives(struct draw_context * draw,const struct draw_prim_info * prim_info)1287 draw_stats_clipper_primitives(struct draw_context *draw,
1288                               const struct draw_prim_info *prim_info)
1289 {
1290    if (draw->collect_statistics) {
1291       unsigned i;
1292       for (i = 0; i < prim_info->primitive_count; i++) {
1293          draw->statistics.c_invocations +=
1294             u_decomposed_prims_for_vertices(prim_info->prim,
1295                                             prim_info->primitive_lengths[i]);
1296       }
1297    }
1298 }
1299 
1300 
1301 /**
1302  * Returns true if the draw module will inject the frontface
1303  * info into the outputs.
1304  *
1305  * Given the specified primitive and rasterizer state
1306  * the function will figure out if the draw module
1307  * will inject the front-face information into shader
1308  * outputs. This is done to preserve the front-facing
1309  * info when decomposing primitives into wireframes.
1310  */
1311 boolean
draw_will_inject_frontface(const struct draw_context * draw)1312 draw_will_inject_frontface(const struct draw_context *draw)
1313 {
1314    unsigned reduced_prim = u_reduced_prim(draw->pt.prim);
1315    const struct pipe_rasterizer_state *rast = draw->rasterizer;
1316 
1317    if (reduced_prim != PIPE_PRIM_TRIANGLES) {
1318       return FALSE;
1319    }
1320 
1321    return (rast &&
1322            (rast->fill_front != PIPE_POLYGON_MODE_FILL ||
1323             rast->fill_back != PIPE_POLYGON_MODE_FILL));
1324 }
1325 
1326 void
draw_set_tess_state(struct draw_context * draw,const float default_outer_level[4],const float default_inner_level[2])1327 draw_set_tess_state(struct draw_context *draw,
1328 		    const float default_outer_level[4],
1329 		    const float default_inner_level[2])
1330 {
1331    for (unsigned i = 0; i < 4; i++)
1332       draw->default_outer_tess_level[i] = default_outer_level[i];
1333    for (unsigned i = 0; i < 2; i++)
1334       draw->default_inner_tess_level[i] = default_inner_level[i];
1335 }
1336 
1337 void
draw_set_disk_cache_callbacks(struct draw_context * draw,void * data_cookie,void (* find_shader)(void * cookie,struct lp_cached_code * cache,unsigned char ir_sha1_cache_key[20]),void (* insert_shader)(void * cookie,struct lp_cached_code * cache,unsigned char ir_sha1_cache_key[20]))1338 draw_set_disk_cache_callbacks(struct draw_context *draw,
1339                               void *data_cookie,
1340                               void (*find_shader)(void *cookie,
1341                                                   struct lp_cached_code *cache,
1342                                                   unsigned char ir_sha1_cache_key[20]),
1343                               void (*insert_shader)(void *cookie,
1344                                                     struct lp_cached_code *cache,
1345                                                     unsigned char ir_sha1_cache_key[20]))
1346 {
1347    draw->disk_cache_find_shader = find_shader;
1348    draw->disk_cache_insert_shader = insert_shader;
1349    draw->disk_cache_cookie = data_cookie;
1350 }
1351 
draw_set_constant_buffer_stride(struct draw_context * draw,unsigned num_bytes)1352 void draw_set_constant_buffer_stride(struct draw_context *draw, unsigned num_bytes)
1353 {
1354    draw->constant_buffer_stride = num_bytes;
1355 }
1356