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;
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          if (draw->rasterizer_no_cull[i][j]) {
215             pipe->delete_rasterizer_state(pipe, draw->rasterizer_no_cull[i][j]);
216          }
217       }
218    }
219 
220    for (i = 0; i < draw->pt.nr_vertex_buffers; i++)
221       pipe_vertex_buffer_unreference(&draw->pt.vertex_buffer[i]);
222 
223    /* Not so fast -- we're just borrowing this at the moment.
224     *
225    if (draw->render)
226       draw->render->destroy( draw->render );
227    */
228 
229    draw_prim_assembler_destroy(draw->ia);
230    draw_pipeline_destroy( draw );
231    draw_pt_destroy( draw );
232    draw_vs_destroy( draw );
233    draw_gs_destroy( draw );
234 #ifdef LLVM_AVAILABLE
235    if (draw->llvm)
236       draw_llvm_destroy( draw->llvm );
237 #endif
238 
239    FREE( draw );
240 }
241 
242 
243 
draw_flush(struct draw_context * draw)244 void draw_flush( struct draw_context *draw )
245 {
246    draw_do_flush( draw, DRAW_FLUSH_BACKEND );
247 }
248 
249 
250 /**
251  * Specify the depth stencil format for the draw pipeline. This function
252  * determines the Minimum Resolvable Depth factor for polygon offset.
253  * This factor potentially depends on the number of Z buffer bits,
254  * the rasterization algorithm and the arithmetic performed on Z
255  * values between vertex shading and rasterization.
256  */
draw_set_zs_format(struct draw_context * draw,enum pipe_format format)257 void draw_set_zs_format(struct draw_context *draw, enum pipe_format format)
258 {
259    const struct util_format_description *desc = util_format_description(format);
260 
261    draw->floating_point_depth =
262       (util_get_depth_format_type(desc) == UTIL_FORMAT_TYPE_FLOAT);
263 
264    draw->mrd = util_get_depth_format_mrd(desc);
265 }
266 
267 
268 static bool
draw_is_vs_window_space(struct draw_context * draw)269 draw_is_vs_window_space(struct draw_context *draw)
270 {
271    if (draw->vs.vertex_shader) {
272       struct tgsi_shader_info *info = &draw->vs.vertex_shader->info;
273 
274       return info->properties[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION] != 0;
275    }
276    return false;
277 }
278 
279 
280 void
draw_update_clip_flags(struct draw_context * draw)281 draw_update_clip_flags(struct draw_context *draw)
282 {
283    bool window_space = draw_is_vs_window_space(draw);
284 
285    draw->clip_xy = !draw->driver.bypass_clip_xy && !window_space;
286    draw->guard_band_xy = (!draw->driver.bypass_clip_xy &&
287                           draw->driver.guard_band_xy);
288    draw->clip_z = (!draw->driver.bypass_clip_z &&
289                    draw->rasterizer && draw->rasterizer->depth_clip_near) &&
290                   !window_space;
291    draw->clip_user = draw->rasterizer &&
292                      draw->rasterizer->clip_plane_enable != 0 &&
293                      !window_space;
294    draw->guard_band_points_xy = draw->guard_band_xy ||
295                                 (draw->driver.bypass_clip_points &&
296                                 (draw->rasterizer &&
297                                  draw->rasterizer->point_tri_clip));
298 }
299 
300 
301 void
draw_update_viewport_flags(struct draw_context * draw)302 draw_update_viewport_flags(struct draw_context *draw)
303 {
304    bool window_space = draw_is_vs_window_space(draw);
305 
306    draw->bypass_viewport = window_space || draw->identity_viewport;
307 }
308 
309 
310 /**
311  * Register new primitive rasterization/rendering state.
312  * This causes the drawing pipeline to be rebuilt.
313  */
draw_set_rasterizer_state(struct draw_context * draw,const struct pipe_rasterizer_state * raster,void * rast_handle)314 void draw_set_rasterizer_state( struct draw_context *draw,
315                                 const struct pipe_rasterizer_state *raster,
316                                 void *rast_handle )
317 {
318    if (!draw->suspend_flushing) {
319       draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
320 
321       draw->rasterizer = raster;
322       draw->rast_handle = rast_handle;
323       draw_update_clip_flags(draw);
324    }
325 }
326 
327 /* With a little more work, llvmpipe will be able to turn this off and
328  * do its own x/y clipping.
329  *
330  * Some hardware can turn off clipping altogether - in particular any
331  * hardware with a TNL unit can do its own clipping, even if it is
332  * relying on the draw module for some other reason.
333  * Setting bypass_clip_points to achieve d3d-style point clipping (the driver
334  * will need to do the "vp scissoring") _requires_ the driver to implement
335  * wide points / point sprites itself (points will still be clipped if rasterizer
336  * point_tri_clip isn't set). Only relevant if bypass_clip_xy isn't set.
337  */
draw_set_driver_clipping(struct draw_context * draw,boolean bypass_clip_xy,boolean bypass_clip_z,boolean guard_band_xy,boolean bypass_clip_points)338 void draw_set_driver_clipping( struct draw_context *draw,
339                                boolean bypass_clip_xy,
340                                boolean bypass_clip_z,
341                                boolean guard_band_xy,
342                                boolean bypass_clip_points)
343 {
344    draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
345 
346    draw->driver.bypass_clip_xy = bypass_clip_xy;
347    draw->driver.bypass_clip_z = bypass_clip_z;
348    draw->driver.guard_band_xy = guard_band_xy;
349    draw->driver.bypass_clip_points = bypass_clip_points;
350    draw_update_clip_flags(draw);
351 }
352 
353 
354 /**
355  * Plug in the primitive rendering/rasterization stage (which is the last
356  * stage in the drawing pipeline).
357  * This is provided by the device driver.
358  */
draw_set_rasterize_stage(struct draw_context * draw,struct draw_stage * stage)359 void draw_set_rasterize_stage( struct draw_context *draw,
360                                struct draw_stage *stage )
361 {
362    draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
363 
364    draw->pipeline.rasterize = stage;
365 }
366 
367 
368 /**
369  * Set the draw module's clipping state.
370  */
draw_set_clip_state(struct draw_context * draw,const struct pipe_clip_state * clip)371 void draw_set_clip_state( struct draw_context *draw,
372                           const struct pipe_clip_state *clip )
373 {
374    draw_do_flush(draw, DRAW_FLUSH_PARAMETER_CHANGE);
375 
376    memcpy(&draw->plane[6], clip->ucp, sizeof(clip->ucp));
377 }
378 
379 
380 /**
381  * Set the draw module's viewport state.
382  */
draw_set_viewport_states(struct draw_context * draw,unsigned start_slot,unsigned num_viewports,const struct pipe_viewport_state * vps)383 void draw_set_viewport_states( struct draw_context *draw,
384                                unsigned start_slot,
385                                unsigned num_viewports,
386                                const struct pipe_viewport_state *vps )
387 {
388    const struct pipe_viewport_state *viewport = vps;
389    draw_do_flush(draw, DRAW_FLUSH_PARAMETER_CHANGE);
390 
391    debug_assert(start_slot < PIPE_MAX_VIEWPORTS);
392    debug_assert((start_slot + num_viewports) <= PIPE_MAX_VIEWPORTS);
393 
394    memcpy(draw->viewports + start_slot, vps,
395           sizeof(struct pipe_viewport_state) * num_viewports);
396 
397    draw->identity_viewport = (num_viewports == 1) &&
398       (viewport->scale[0] == 1.0f &&
399        viewport->scale[1] == 1.0f &&
400        viewport->scale[2] == 1.0f &&
401        viewport->translate[0] == 0.0f &&
402        viewport->translate[1] == 0.0f &&
403        viewport->translate[2] == 0.0f);
404    draw_update_viewport_flags(draw);
405 }
406 
407 
408 
409 void
draw_set_vertex_buffers(struct draw_context * draw,unsigned start_slot,unsigned count,const struct pipe_vertex_buffer * buffers)410 draw_set_vertex_buffers(struct draw_context *draw,
411                         unsigned start_slot, unsigned count,
412                         const struct pipe_vertex_buffer *buffers)
413 {
414    assert(start_slot + count <= PIPE_MAX_ATTRIBS);
415 
416    util_set_vertex_buffers_count(draw->pt.vertex_buffer,
417                                  &draw->pt.nr_vertex_buffers,
418                                  buffers, start_slot, count);
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    debug_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    debug_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    debug_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    debug_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 void
draw_set_force_passthrough(struct draw_context * draw,boolean enable)583 draw_set_force_passthrough( struct draw_context *draw, boolean enable )
584 {
585    draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
586    draw->force_passthrough = enable;
587 }
588 
589 
590 
591 /**
592  * Allocate an extra vertex/geometry shader vertex attribute, if it doesn't
593  * exist already.
594  *
595  * This is used by some of the optional draw module stages such
596  * as wide_point which may need to allocate additional generic/texcoord
597  * attributes.
598  */
599 int
draw_alloc_extra_vertex_attrib(struct draw_context * draw,uint semantic_name,uint semantic_index)600 draw_alloc_extra_vertex_attrib(struct draw_context *draw,
601                                uint semantic_name, uint semantic_index)
602 {
603    int slot;
604    uint num_outputs;
605    uint n;
606 
607    slot = draw_find_shader_output(draw, semantic_name, semantic_index);
608    if (slot >= 0) {
609       return slot;
610    }
611 
612    num_outputs = draw_current_shader_outputs(draw);
613    n = draw->extra_shader_outputs.num;
614 
615    assert(n < ARRAY_SIZE(draw->extra_shader_outputs.semantic_name));
616 
617    draw->extra_shader_outputs.semantic_name[n] = semantic_name;
618    draw->extra_shader_outputs.semantic_index[n] = semantic_index;
619    draw->extra_shader_outputs.slot[n] = num_outputs + n;
620    draw->extra_shader_outputs.num++;
621 
622    return draw->extra_shader_outputs.slot[n];
623 }
624 
625 
626 /**
627  * Remove all extra vertex attributes that were allocated with
628  * draw_alloc_extra_vertex_attrib().
629  */
630 void
draw_remove_extra_vertex_attribs(struct draw_context * draw)631 draw_remove_extra_vertex_attribs(struct draw_context *draw)
632 {
633    draw->extra_shader_outputs.num = 0;
634 }
635 
636 
637 /**
638  * If a geometry shader is present, return its info, else the vertex shader's
639  * info.
640  */
641 struct tgsi_shader_info *
draw_get_shader_info(const struct draw_context * draw)642 draw_get_shader_info(const struct draw_context *draw)
643 {
644 
645    if (draw->gs.geometry_shader) {
646       return &draw->gs.geometry_shader->info;
647    } else if (draw->tes.tess_eval_shader) {
648       return &draw->tes.tess_eval_shader->info;
649    } else {
650       return &draw->vs.vertex_shader->info;
651    }
652 }
653 
654 /**
655  * Prepare outputs slots from the draw module
656  *
657  * Certain parts of the draw module can emit additional
658  * outputs that can be quite useful to the backends, a good
659  * example of it is the process of decomposing primitives
660  * into wireframes (aka. lines) which normally would lose
661  * the face-side information, but using this method we can
662  * inject another shader output which passes the original
663  * face side information to the backend.
664  */
665 void
draw_prepare_shader_outputs(struct draw_context * draw)666 draw_prepare_shader_outputs(struct draw_context *draw)
667 {
668    draw_remove_extra_vertex_attribs(draw);
669    draw_prim_assembler_prepare_outputs(draw->ia);
670    draw_unfilled_prepare_outputs(draw, draw->pipeline.unfilled);
671    if (draw->pipeline.aapoint)
672       draw_aapoint_prepare_outputs(draw, draw->pipeline.aapoint);
673    if (draw->pipeline.aaline)
674       draw_aaline_prepare_outputs(draw, draw->pipeline.aaline);
675 }
676 
677 /**
678  * Ask the draw module for the location/slot of the given vertex attribute in
679  * a post-transformed vertex.
680  *
681  * With this function, drivers that use the draw module should have no reason
682  * to track the current vertex/geometry shader.
683  *
684  * Note that the draw module may sometimes generate vertices with extra
685  * attributes (such as texcoords for AA lines).  The driver can call this
686  * function to find those attributes.
687  *
688  * -1 is returned if the attribute is not found since this is
689  * an undefined situation. Note, that zero is valid and can
690  * be used by any of the attributes, because position is not
691  * required to be attribute 0 or even at all present.
692  */
693 int
draw_find_shader_output(const struct draw_context * draw,uint semantic_name,uint semantic_index)694 draw_find_shader_output(const struct draw_context *draw,
695                         uint semantic_name, uint semantic_index)
696 {
697    const struct tgsi_shader_info *info = draw_get_shader_info(draw);
698    uint i;
699 
700    for (i = 0; i < info->num_outputs; i++) {
701       if (info->output_semantic_name[i] == semantic_name &&
702           info->output_semantic_index[i] == semantic_index)
703          return i;
704    }
705 
706    /* Search the extra vertex attributes */
707    for (i = 0; i < draw->extra_shader_outputs.num; i++) {
708       if (draw->extra_shader_outputs.semantic_name[i] == semantic_name &&
709           draw->extra_shader_outputs.semantic_index[i] == semantic_index) {
710          return draw->extra_shader_outputs.slot[i];
711       }
712    }
713 
714    return -1;
715 }
716 
717 
718 /**
719  * Return total number of the shader outputs.  This function is similar to
720  * draw_current_shader_outputs() but this function also counts any extra
721  * vertex/geometry output attributes that may be filled in by some draw
722  * stages (such as AA point, AA line).
723  *
724  * If geometry shader is present, its output will be returned,
725  * if not vertex shader is used.
726  */
727 uint
draw_num_shader_outputs(const struct draw_context * draw)728 draw_num_shader_outputs(const struct draw_context *draw)
729 {
730    const struct tgsi_shader_info *info = draw_get_shader_info(draw);
731    uint count;
732 
733    count = info->num_outputs;
734    count += draw->extra_shader_outputs.num;
735 
736    return count;
737 }
738 
739 
740 /**
741  * Return total number of the vertex shader outputs.  This function
742  * also counts any extra vertex output attributes that may
743  * be filled in by some draw stages (such as AA point, AA line,
744  * front face).
745  */
746 uint
draw_total_vs_outputs(const struct draw_context * draw)747 draw_total_vs_outputs(const struct draw_context *draw)
748 {
749    const struct tgsi_shader_info *info = &draw->vs.vertex_shader->info;
750 
751    return info->num_outputs + draw->extra_shader_outputs.num;
752 }
753 
754 /**
755  * Return total number of the geometry shader outputs. This function
756  * also counts any extra geometry output attributes that may
757  * be filled in by some draw stages (such as AA point, AA line, front
758  * face).
759  */
760 uint
draw_total_gs_outputs(const struct draw_context * draw)761 draw_total_gs_outputs(const struct draw_context *draw)
762 {
763    const struct tgsi_shader_info *info;
764 
765    if (!draw->gs.geometry_shader)
766       return 0;
767 
768    info = &draw->gs.geometry_shader->info;
769 
770    return info->num_outputs + draw->extra_shader_outputs.num;
771 }
772 
773 /**
774  * Return total number of the tess ctrl shader outputs.
775  */
776 uint
draw_total_tcs_outputs(const struct draw_context * draw)777 draw_total_tcs_outputs(const struct draw_context *draw)
778 {
779    const struct tgsi_shader_info *info;
780 
781    if (!draw->tcs.tess_ctrl_shader)
782       return 0;
783 
784    info = &draw->tcs.tess_ctrl_shader->info;
785 
786    return info->num_outputs;
787 }
788 
789 /**
790  * Return total number of the tess eval shader outputs.
791  */
792 uint
draw_total_tes_outputs(const struct draw_context * draw)793 draw_total_tes_outputs(const struct draw_context *draw)
794 {
795    const struct tgsi_shader_info *info;
796 
797    if (!draw->tes.tess_eval_shader)
798       return 0;
799 
800    info = &draw->tes.tess_eval_shader->info;
801 
802    return info->num_outputs + draw->extra_shader_outputs.num;
803 }
804 
805 /**
806  * Provide TGSI sampler objects for vertex/geometry shaders that use
807  * texture fetches.  This state only needs to be set once per context.
808  * This might only be used by software drivers for the time being.
809  */
810 void
draw_texture_sampler(struct draw_context * draw,enum pipe_shader_type shader,struct tgsi_sampler * sampler)811 draw_texture_sampler(struct draw_context *draw,
812                      enum pipe_shader_type shader,
813                      struct tgsi_sampler *sampler)
814 {
815    switch (shader) {
816    case PIPE_SHADER_VERTEX:
817       draw->vs.tgsi.sampler = sampler;
818       break;
819    case PIPE_SHADER_GEOMETRY:
820       draw->gs.tgsi.sampler = sampler;
821       break;
822    case PIPE_SHADER_TESS_CTRL:
823       draw->tcs.tgsi.sampler = sampler;
824       break;
825    case PIPE_SHADER_TESS_EVAL:
826       draw->tes.tgsi.sampler = sampler;
827       break;
828    default:
829       assert(0);
830       break;
831    }
832 }
833 
834 /**
835  * Provide TGSI image objects for vertex/geometry shaders that use
836  * texture fetches.  This state only needs to be set once per context.
837  * This might only be used by software drivers for the time being.
838  */
839 void
draw_image(struct draw_context * draw,enum pipe_shader_type shader,struct tgsi_image * image)840 draw_image(struct draw_context *draw,
841            enum pipe_shader_type shader,
842            struct tgsi_image *image)
843 {
844    switch (shader) {
845    case PIPE_SHADER_VERTEX:
846       draw->vs.tgsi.image = image;
847       break;
848    case PIPE_SHADER_GEOMETRY:
849       draw->gs.tgsi.image = image;
850       break;
851    case PIPE_SHADER_TESS_CTRL:
852       draw->tcs.tgsi.image = image;
853       break;
854    case PIPE_SHADER_TESS_EVAL:
855       draw->tes.tgsi.image = image;
856       break;
857    default:
858       assert(0);
859       break;
860    }
861 }
862 
863 /**
864  * Provide TGSI buffer objects for vertex/geometry shaders that use
865  * load/store/atomic ops.  This state only needs to be set once per context.
866  * This might only be used by software drivers for the time being.
867  */
868 void
draw_buffer(struct draw_context * draw,enum pipe_shader_type shader,struct tgsi_buffer * buffer)869 draw_buffer(struct draw_context *draw,
870             enum pipe_shader_type shader,
871             struct tgsi_buffer *buffer)
872 {
873    switch (shader) {
874    case PIPE_SHADER_VERTEX:
875       draw->vs.tgsi.buffer = buffer;
876       break;
877    case PIPE_SHADER_GEOMETRY:
878       draw->gs.tgsi.buffer = buffer;
879       break;
880    case PIPE_SHADER_TESS_CTRL:
881       draw->tcs.tgsi.buffer = buffer;
882       break;
883    case PIPE_SHADER_TESS_EVAL:
884       draw->tes.tgsi.buffer = buffer;
885       break;
886    default:
887       assert(0);
888       break;
889    }
890 }
891 
892 
draw_set_render(struct draw_context * draw,struct vbuf_render * render)893 void draw_set_render( struct draw_context *draw,
894 		      struct vbuf_render *render )
895 {
896    draw->render = render;
897 }
898 
899 
900 /**
901  * Tell the draw module where vertex indexes/elements are located, and
902  * their size (in bytes).
903  */
904 void
draw_set_indexes(struct draw_context * draw,const void * elements,unsigned elem_size,unsigned elem_buffer_space)905 draw_set_indexes(struct draw_context *draw,
906                  const void *elements, unsigned elem_size,
907                  unsigned elem_buffer_space)
908 {
909    assert(elem_size == 0 ||
910           elem_size == 1 ||
911           elem_size == 2 ||
912           elem_size == 4);
913    draw->pt.user.elts = elements;
914    draw->pt.user.eltSizeIB = elem_size;
915    if (elem_size)
916       draw->pt.user.eltMax = elem_buffer_space / elem_size;
917    else
918       draw->pt.user.eltMax = 0;
919 }
920 
921 
922 /* Revamp me please:
923  */
draw_do_flush(struct draw_context * draw,unsigned flags)924 void draw_do_flush( struct draw_context *draw, unsigned flags )
925 {
926    if (!draw->suspend_flushing)
927    {
928       assert(!draw->flushing); /* catch inadvertant recursion */
929 
930       draw->flushing = TRUE;
931 
932       draw_pipeline_flush( draw, flags );
933 
934       draw_pt_flush( draw, flags );
935 
936       draw->flushing = FALSE;
937    }
938 }
939 
940 
941 /**
942  * Return the number of output attributes produced by the geometry
943  * shader, if present.  If no geometry shader, return the number of
944  * outputs from the vertex shader.
945  * \sa draw_num_shader_outputs
946  */
947 uint
draw_current_shader_outputs(const struct draw_context * draw)948 draw_current_shader_outputs(const struct draw_context *draw)
949 {
950    if (draw->gs.geometry_shader)
951       return draw->gs.num_gs_outputs;
952    return draw->vs.num_vs_outputs;
953 }
954 
955 
956 /**
957  * Return the index of the shader output which will contain the
958  * vertex position.
959  */
960 uint
draw_current_shader_position_output(const struct draw_context * draw)961 draw_current_shader_position_output(const struct draw_context *draw)
962 {
963    if (draw->gs.geometry_shader)
964       return draw->gs.position_output;
965    if (draw->tes.tess_eval_shader)
966       return draw->tes.position_output;
967    return draw->vs.position_output;
968 }
969 
970 
971 /**
972  * Return the index of the shader output which will contain the
973  * viewport index.
974  */
975 uint
draw_current_shader_viewport_index_output(const struct draw_context * draw)976 draw_current_shader_viewport_index_output(const struct draw_context *draw)
977 {
978    if (draw->gs.geometry_shader)
979       return draw->gs.geometry_shader->viewport_index_output;
980    else if (draw->tes.tess_eval_shader)
981       return draw->tes.tess_eval_shader->viewport_index_output;
982    return draw->vs.vertex_shader->viewport_index_output;
983 }
984 
985 /**
986  * Returns true if there's a geometry shader bound and the geometry
987  * shader writes out a viewport index.
988  */
989 boolean
draw_current_shader_uses_viewport_index(const struct draw_context * draw)990 draw_current_shader_uses_viewport_index(const struct draw_context *draw)
991 {
992    if (draw->gs.geometry_shader)
993       return draw->gs.geometry_shader->info.writes_viewport_index;
994    return draw->vs.vertex_shader->info.writes_viewport_index;
995 }
996 
997 
998 /**
999  * Return the index of the shader output which will contain the
1000  * clip vertex position.
1001  * Note we don't support clipvertex output in the gs. For clipping
1002  * to work correctly hence we return ordinary position output instead.
1003  */
1004 uint
draw_current_shader_clipvertex_output(const struct draw_context * draw)1005 draw_current_shader_clipvertex_output(const struct draw_context *draw)
1006 {
1007    if (draw->gs.geometry_shader)
1008       return draw->gs.position_output;
1009    if (draw->tes.tess_eval_shader)
1010       return draw->tes.position_output;
1011    return draw->vs.clipvertex_output;
1012 }
1013 
1014 uint
draw_current_shader_ccdistance_output(const struct draw_context * draw,int index)1015 draw_current_shader_ccdistance_output(const struct draw_context *draw, int index)
1016 {
1017    debug_assert(index < PIPE_MAX_CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT);
1018    if (draw->gs.geometry_shader)
1019       return draw->gs.geometry_shader->ccdistance_output[index];
1020    if (draw->tes.tess_eval_shader)
1021       return draw->tes.tess_eval_shader->ccdistance_output[index];
1022    return draw->vs.ccdistance_output[index];
1023 }
1024 
1025 
1026 uint
draw_current_shader_num_written_clipdistances(const struct draw_context * draw)1027 draw_current_shader_num_written_clipdistances(const struct draw_context *draw)
1028 {
1029    if (draw->gs.geometry_shader)
1030       return draw->gs.geometry_shader->info.num_written_clipdistance;
1031    if (draw->tes.tess_eval_shader)
1032       return draw->tes.tess_eval_shader->info.num_written_clipdistance;
1033    return draw->vs.vertex_shader->info.num_written_clipdistance;
1034 }
1035 
1036 uint
draw_current_shader_num_written_culldistances(const struct draw_context * draw)1037 draw_current_shader_num_written_culldistances(const struct draw_context *draw)
1038 {
1039    if (draw->gs.geometry_shader)
1040       return draw->gs.geometry_shader->info.num_written_culldistance;
1041    if (draw->tes.tess_eval_shader)
1042       return draw->tes.tess_eval_shader->info.num_written_culldistance;
1043    return draw->vs.vertex_shader->info.num_written_culldistance;
1044 }
1045 
1046 /**
1047  * Return a pointer/handle for a driver/CSO rasterizer object which
1048  * disabled culling, stippling, unfilled tris, etc.
1049  * This is used by some pipeline stages (such as wide_point, aa_line
1050  * and aa_point) which convert points/lines into triangles.  In those
1051  * cases we don't want to accidentally cull the triangles.
1052  *
1053  * \param scissor  should the rasterizer state enable scissoring?
1054  * \param flatshade  should the rasterizer state use flat shading?
1055  * \return  rasterizer CSO handle
1056  */
1057 void *
draw_get_rasterizer_no_cull(struct draw_context * draw,boolean scissor,boolean flatshade)1058 draw_get_rasterizer_no_cull( struct draw_context *draw,
1059                              boolean scissor,
1060                              boolean flatshade )
1061 {
1062    if (!draw->rasterizer_no_cull[scissor][flatshade]) {
1063       /* create now */
1064       struct pipe_context *pipe = draw->pipe;
1065       struct pipe_rasterizer_state rast;
1066 
1067       memset(&rast, 0, sizeof(rast));
1068       rast.scissor = scissor;
1069       rast.flatshade = flatshade;
1070       rast.front_ccw = 1;
1071       rast.half_pixel_center = draw->rasterizer->half_pixel_center;
1072       rast.bottom_edge_rule = draw->rasterizer->bottom_edge_rule;
1073       rast.clip_halfz = draw->rasterizer->clip_halfz;
1074 
1075       draw->rasterizer_no_cull[scissor][flatshade] =
1076          pipe->create_rasterizer_state(pipe, &rast);
1077    }
1078    return draw->rasterizer_no_cull[scissor][flatshade];
1079 }
1080 
1081 void
draw_set_mapped_so_targets(struct draw_context * draw,int num_targets,struct draw_so_target * targets[PIPE_MAX_SO_BUFFERS])1082 draw_set_mapped_so_targets(struct draw_context *draw,
1083                            int num_targets,
1084                            struct draw_so_target *targets[PIPE_MAX_SO_BUFFERS])
1085 {
1086    int i;
1087 
1088    draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
1089 
1090    for (i = 0; i < num_targets; i++)
1091       draw->so.targets[i] = targets[i];
1092    for (i = num_targets; i < PIPE_MAX_SO_BUFFERS; i++)
1093       draw->so.targets[i] = NULL;
1094 
1095    draw->so.num_targets = num_targets;
1096 }
1097 
1098 void
draw_set_sampler_views(struct draw_context * draw,enum pipe_shader_type shader_stage,struct pipe_sampler_view ** views,unsigned num)1099 draw_set_sampler_views(struct draw_context *draw,
1100                        enum pipe_shader_type shader_stage,
1101                        struct pipe_sampler_view **views,
1102                        unsigned num)
1103 {
1104    unsigned i;
1105 
1106    debug_assert(shader_stage < PIPE_SHADER_TYPES);
1107    debug_assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
1108 
1109    draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
1110 
1111    for (i = 0; i < num; ++i)
1112       draw->sampler_views[shader_stage][i] = views[i];
1113    for (i = num; i < draw->num_sampler_views[shader_stage]; ++i)
1114       draw->sampler_views[shader_stage][i] = NULL;
1115 
1116    draw->num_sampler_views[shader_stage] = num;
1117 }
1118 
1119 void
draw_set_samplers(struct draw_context * draw,enum pipe_shader_type shader_stage,struct pipe_sampler_state ** samplers,unsigned num)1120 draw_set_samplers(struct draw_context *draw,
1121                   enum pipe_shader_type shader_stage,
1122                   struct pipe_sampler_state **samplers,
1123                   unsigned num)
1124 {
1125    unsigned i;
1126 
1127    debug_assert(shader_stage < PIPE_SHADER_TYPES);
1128    debug_assert(num <= PIPE_MAX_SAMPLERS);
1129 
1130    draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
1131 
1132    for (i = 0; i < num; ++i)
1133       draw->samplers[shader_stage][i] = samplers[i];
1134    for (i = num; i < PIPE_MAX_SAMPLERS; ++i)
1135       draw->samplers[shader_stage][i] = NULL;
1136 
1137    draw->num_samplers[shader_stage] = num;
1138 
1139 #ifdef LLVM_AVAILABLE
1140    if (draw->llvm)
1141       draw_llvm_set_sampler_state(draw, shader_stage);
1142 #endif
1143 }
1144 
1145 void
draw_set_images(struct draw_context * draw,enum pipe_shader_type shader_stage,struct pipe_image_view * views,unsigned num)1146 draw_set_images(struct draw_context *draw,
1147                 enum pipe_shader_type shader_stage,
1148                 struct pipe_image_view *views,
1149                 unsigned num)
1150 {
1151    unsigned i;
1152 
1153    debug_assert(shader_stage < PIPE_SHADER_TYPES);
1154    debug_assert(num <= PIPE_MAX_SHADER_IMAGES);
1155 
1156    draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
1157 
1158    for (i = 0; i < num; ++i)
1159       draw->images[shader_stage][i] = &views[i];
1160    for (i = num; i < draw->num_sampler_views[shader_stage]; ++i)
1161       draw->images[shader_stage][i] = NULL;
1162 
1163    draw->num_images[shader_stage] = num;
1164 }
1165 
1166 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])1167 draw_set_mapped_texture(struct draw_context *draw,
1168                         enum pipe_shader_type shader_stage,
1169                         unsigned sview_idx,
1170                         uint32_t width, uint32_t height, uint32_t depth,
1171                         uint32_t first_level, uint32_t last_level,
1172                         uint32_t num_samples,
1173                         uint32_t sample_stride,
1174                         const void *base_ptr,
1175                         uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
1176                         uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
1177                         uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS])
1178 {
1179 #ifdef LLVM_AVAILABLE
1180    if (draw->llvm)
1181       draw_llvm_set_mapped_texture(draw,
1182                                    shader_stage,
1183                                    sview_idx,
1184                                    width, height, depth, first_level,
1185                                    last_level, num_samples, sample_stride, base_ptr,
1186                                    row_stride, img_stride, mip_offsets);
1187 #endif
1188 }
1189 
1190 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)1191 draw_set_mapped_image(struct draw_context *draw,
1192                       enum pipe_shader_type shader_stage,
1193                       unsigned idx,
1194                       uint32_t width, uint32_t height, uint32_t depth,
1195                       const void *base_ptr,
1196                       uint32_t row_stride,
1197                       uint32_t img_stride,
1198                       uint32_t num_samples,
1199                       uint32_t sample_stride)
1200 {
1201 #ifdef LLVM_AVAILABLE
1202    if (draw->llvm)
1203       draw_llvm_set_mapped_image(draw,
1204                                  shader_stage,
1205                                  idx,
1206                                  width, height, depth,
1207                                  base_ptr,
1208                                  row_stride, img_stride,
1209                                  num_samples, sample_stride);
1210 #endif
1211 }
1212 
1213 /**
1214  * XXX: Results for PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS because there are two
1215  * different ways of setting textures, and drivers typically only support one.
1216  */
1217 int
draw_get_shader_param_no_llvm(enum pipe_shader_type shader,enum pipe_shader_cap param)1218 draw_get_shader_param_no_llvm(enum pipe_shader_type shader,
1219                               enum pipe_shader_cap param)
1220 {
1221    switch(shader) {
1222    case PIPE_SHADER_VERTEX:
1223    case PIPE_SHADER_GEOMETRY:
1224       return tgsi_exec_get_shader_param(param);
1225    default:
1226       return 0;
1227    }
1228 }
1229 
1230 /**
1231  * XXX: Results for PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS because there are two
1232  * different ways of setting textures, and drivers typically only support one.
1233  * Drivers requesting a draw context explicitly without llvm must call
1234  * draw_get_shader_param_no_llvm instead.
1235  */
1236 int
draw_get_shader_param(enum pipe_shader_type shader,enum pipe_shader_cap param)1237 draw_get_shader_param(enum pipe_shader_type shader, enum pipe_shader_cap param)
1238 {
1239 
1240 #ifdef LLVM_AVAILABLE
1241    if (draw_get_option_use_llvm()) {
1242       switch(shader) {
1243       case PIPE_SHADER_VERTEX:
1244       case PIPE_SHADER_GEOMETRY:
1245       case PIPE_SHADER_TESS_CTRL:
1246       case PIPE_SHADER_TESS_EVAL:
1247          return gallivm_get_shader_param(param);
1248       default:
1249          return 0;
1250       }
1251    }
1252 #endif
1253 
1254    return draw_get_shader_param_no_llvm(shader, param);
1255 }
1256 
1257 /**
1258  * Enables or disables collection of statistics.
1259  *
1260  * Draw module is capable of generating statistics for the vertex
1261  * processing pipeline. Collection of that data isn't free and so
1262  * it's disabled by default. The users of the module can enable
1263  * (or disable) this functionality through this function.
1264  * The actual data will be emitted through the VBUF interface,
1265  * the 'pipeline_statistics' callback to be exact.
1266  */
1267 void
draw_collect_pipeline_statistics(struct draw_context * draw,boolean enable)1268 draw_collect_pipeline_statistics(struct draw_context *draw,
1269                                  boolean enable)
1270 {
1271    draw->collect_statistics = enable;
1272 }
1273 
1274 /**
1275  * Enable/disable primitives generated gathering.
1276  */
draw_collect_primitives_generated(struct draw_context * draw,bool enable)1277 void draw_collect_primitives_generated(struct draw_context *draw,
1278                                        bool enable)
1279 {
1280    draw->collect_primgen = enable;
1281 }
1282 
1283 /**
1284  * Computes clipper invocation statistics.
1285  *
1286  * Figures out how many primitives would have been
1287  * sent to the clipper given the specified
1288  * prim info data.
1289  */
1290 void
draw_stats_clipper_primitives(struct draw_context * draw,const struct draw_prim_info * prim_info)1291 draw_stats_clipper_primitives(struct draw_context *draw,
1292                               const struct draw_prim_info *prim_info)
1293 {
1294    if (draw->collect_statistics) {
1295       unsigned i;
1296       for (i = 0; i < prim_info->primitive_count; i++) {
1297          draw->statistics.c_invocations +=
1298             u_decomposed_prims_for_vertices(prim_info->prim,
1299                                             prim_info->primitive_lengths[i]);
1300       }
1301    }
1302 }
1303 
1304 
1305 /**
1306  * Returns true if the draw module will inject the frontface
1307  * info into the outputs.
1308  *
1309  * Given the specified primitive and rasterizer state
1310  * the function will figure out if the draw module
1311  * will inject the front-face information into shader
1312  * outputs. This is done to preserve the front-facing
1313  * info when decomposing primitives into wireframes.
1314  */
1315 boolean
draw_will_inject_frontface(const struct draw_context * draw)1316 draw_will_inject_frontface(const struct draw_context *draw)
1317 {
1318    unsigned reduced_prim = u_reduced_prim(draw->pt.prim);
1319    const struct pipe_rasterizer_state *rast = draw->rasterizer;
1320 
1321    if (reduced_prim != PIPE_PRIM_TRIANGLES) {
1322       return FALSE;
1323    }
1324 
1325    return (rast &&
1326            (rast->fill_front != PIPE_POLYGON_MODE_FILL ||
1327             rast->fill_back != PIPE_POLYGON_MODE_FILL));
1328 }
1329 
1330 void
draw_set_tess_state(struct draw_context * draw,const float default_outer_level[4],const float default_inner_level[2])1331 draw_set_tess_state(struct draw_context *draw,
1332 		    const float default_outer_level[4],
1333 		    const float default_inner_level[2])
1334 {
1335    for (unsigned i = 0; i < 4; i++)
1336       draw->default_outer_tess_level[i] = default_outer_level[i];
1337    for (unsigned i = 0; i < 2; i++)
1338       draw->default_inner_tess_level[i] = default_inner_level[i];
1339 }
1340 
1341 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]))1342 draw_set_disk_cache_callbacks(struct draw_context *draw,
1343                               void *data_cookie,
1344                               void (*find_shader)(void *cookie,
1345                                                   struct lp_cached_code *cache,
1346                                                   unsigned char ir_sha1_cache_key[20]),
1347                               void (*insert_shader)(void *cookie,
1348                                                     struct lp_cached_code *cache,
1349                                                     unsigned char ir_sha1_cache_key[20]))
1350 {
1351    draw->disk_cache_find_shader = find_shader;
1352    draw->disk_cache_insert_shader = insert_shader;
1353    draw->disk_cache_cookie = data_cookie;
1354 }
1355 
draw_set_constant_buffer_stride(struct draw_context * draw,unsigned num_bytes)1356 void draw_set_constant_buffer_stride(struct draw_context *draw, unsigned num_bytes)
1357 {
1358    draw->constant_buffer_stride = num_bytes;
1359 }
1360