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