Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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