Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright © 2009 Intel Corporation
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice (including the next
  12.  * paragraph) shall be included in all copies or substantial portions of the
  13.  * Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21.  * IN THE SOFTWARE.
  22.  *
  23.  * Authors:
  24.  *    Eric Anholt <eric@anholt.net>
  25.  *
  26.  */
  27.  
  28. #include "brw_context.h"
  29. #include "brw_state.h"
  30. #include "brw_defines.h"
  31. #include "brw_util.h"
  32. #include "main/macros.h"
  33. #include "main/fbobject.h"
  34. #include "intel_batchbuffer.h"
  35.  
  36. /**
  37.  * Determine the appropriate attribute override value to store into the
  38.  * 3DSTATE_SF structure for a given fragment shader attribute.  The attribute
  39.  * override value contains two pieces of information: the location of the
  40.  * attribute in the VUE (relative to urb_entry_read_offset, see below), and a
  41.  * flag indicating whether to "swizzle" the attribute based on the direction
  42.  * the triangle is facing.
  43.  *
  44.  * If an attribute is "swizzled", then the given VUE location is used for
  45.  * front-facing triangles, and the VUE location that immediately follows is
  46.  * used for back-facing triangles.  We use this to implement the mapping from
  47.  * gl_FrontColor/gl_BackColor to gl_Color.
  48.  *
  49.  * urb_entry_read_offset is the offset into the VUE at which the SF unit is
  50.  * being instructed to begin reading attribute data.  It can be set to a
  51.  * nonzero value to prevent the SF unit from wasting time reading elements of
  52.  * the VUE that are not needed by the fragment shader.  It is measured in
  53.  * 256-bit increments.
  54.  */
  55. uint32_t
  56. get_attr_override(const struct brw_vue_map *vue_map, int urb_entry_read_offset,
  57.                   int fs_attr, bool two_side_color, uint32_t *max_source_attr)
  58. {
  59.    if (fs_attr == VARYING_SLOT_POS) {
  60.       /* This attribute will be overwritten by the fragment shader's
  61.        * interpolation code (see emit_interp() in brw_wm_fp.c), so just let it
  62.        * reference the first available attribute.
  63.        */
  64.       return 0;
  65.    }
  66.  
  67.    /* Find the VUE slot for this attribute. */
  68.    int slot = vue_map->varying_to_slot[fs_attr];
  69.  
  70.    /* If there was only a back color written but not front, use back
  71.     * as the color instead of undefined
  72.     */
  73.    if (slot == -1 && fs_attr == VARYING_SLOT_COL0)
  74.       slot = vue_map->varying_to_slot[VARYING_SLOT_BFC0];
  75.    if (slot == -1 && fs_attr == VARYING_SLOT_COL1)
  76.       slot = vue_map->varying_to_slot[VARYING_SLOT_BFC1];
  77.  
  78.    if (slot == -1) {
  79.       /* This attribute does not exist in the VUE--that means that the vertex
  80.        * shader did not write to it.  This means that either:
  81.        *
  82.        * (a) This attribute is a texture coordinate, and it is going to be
  83.        * replaced with point coordinates (as a consequence of a call to
  84.        * glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE)), so the
  85.        * hardware will ignore whatever attribute override we supply.
  86.        *
  87.        * (b) This attribute is read by the fragment shader but not written by
  88.        * the vertex shader, so its value is undefined.  Therefore the
  89.        * attribute override we supply doesn't matter.
  90.        *
  91.        * In either case the attribute override we supply doesn't matter, so
  92.        * just reference the first available attribute.
  93.        */
  94.       return 0;
  95.    }
  96.  
  97.    /* Compute the location of the attribute relative to urb_entry_read_offset.
  98.     * Each increment of urb_entry_read_offset represents a 256-bit value, so
  99.     * it counts for two 128-bit VUE slots.
  100.     */
  101.    int source_attr = slot - 2 * urb_entry_read_offset;
  102.    assert(source_attr >= 0 && source_attr < 32);
  103.  
  104.    /* If we are doing two-sided color, and the VUE slot following this one
  105.     * represents a back-facing color, then we need to instruct the SF unit to
  106.     * do back-facing swizzling.
  107.     */
  108.    bool swizzling = two_side_color &&
  109.       ((vue_map->slot_to_varying[slot] == VARYING_SLOT_COL0 &&
  110.         vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC0) ||
  111.        (vue_map->slot_to_varying[slot] == VARYING_SLOT_COL1 &&
  112.         vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC1));
  113.  
  114.    /* Update max_source_attr.  If swizzling, the SF will read this slot + 1. */
  115.    if (*max_source_attr < source_attr + swizzling)
  116.       *max_source_attr = source_attr + swizzling;
  117.  
  118.    if (swizzling) {
  119.       return source_attr |
  120.          (ATTRIBUTE_SWIZZLE_INPUTATTR_FACING << ATTRIBUTE_SWIZZLE_SHIFT);
  121.    }
  122.  
  123.    return source_attr;
  124. }
  125.  
  126. static void
  127. upload_sf_state(struct brw_context *brw)
  128. {
  129.    struct gl_context *ctx = &brw->ctx;
  130.    /* BRW_NEW_FRAGMENT_PROGRAM */
  131.    uint32_t num_outputs = _mesa_bitcount_64(brw->fragment_program->Base.InputsRead);
  132.    /* _NEW_LIGHT */
  133.    bool shade_model_flat = ctx->Light.ShadeModel == GL_FLAT;
  134.    uint32_t dw1, dw2, dw3, dw4, dw16, dw17;
  135.    int i;
  136.    /* _NEW_BUFFER */
  137.    bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
  138.    bool multisampled_fbo = ctx->DrawBuffer->Visual.samples > 1;
  139.  
  140.    int attr = 0, input_index = 0;
  141.    int urb_entry_read_offset = 1;
  142.    float point_size;
  143.    uint16_t attr_overrides[VARYING_SLOT_MAX];
  144.    uint32_t point_sprite_origin;
  145.  
  146.    dw1 = GEN6_SF_SWIZZLE_ENABLE | num_outputs << GEN6_SF_NUM_OUTPUTS_SHIFT;
  147.  
  148.    dw2 = GEN6_SF_STATISTICS_ENABLE |
  149.          GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
  150.  
  151.    dw3 = 0;
  152.    dw4 = 0;
  153.    dw16 = 0;
  154.    dw17 = 0;
  155.  
  156.    /* _NEW_POLYGON */
  157.    if ((ctx->Polygon.FrontFace == GL_CCW) ^ render_to_fbo)
  158.       dw2 |= GEN6_SF_WINDING_CCW;
  159.  
  160.    if (ctx->Polygon.OffsetFill)
  161.        dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
  162.  
  163.    if (ctx->Polygon.OffsetLine)
  164.        dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
  165.  
  166.    if (ctx->Polygon.OffsetPoint)
  167.        dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
  168.  
  169.    switch (ctx->Polygon.FrontMode) {
  170.    case GL_FILL:
  171.        dw2 |= GEN6_SF_FRONT_SOLID;
  172.        break;
  173.  
  174.    case GL_LINE:
  175.        dw2 |= GEN6_SF_FRONT_WIREFRAME;
  176.        break;
  177.  
  178.    case GL_POINT:
  179.        dw2 |= GEN6_SF_FRONT_POINT;
  180.        break;
  181.  
  182.    default:
  183.        assert(0);
  184.        break;
  185.    }
  186.  
  187.    switch (ctx->Polygon.BackMode) {
  188.    case GL_FILL:
  189.        dw2 |= GEN6_SF_BACK_SOLID;
  190.        break;
  191.  
  192.    case GL_LINE:
  193.        dw2 |= GEN6_SF_BACK_WIREFRAME;
  194.        break;
  195.  
  196.    case GL_POINT:
  197.        dw2 |= GEN6_SF_BACK_POINT;
  198.        break;
  199.  
  200.    default:
  201.        assert(0);
  202.        break;
  203.    }
  204.  
  205.    /* _NEW_SCISSOR */
  206.    if (ctx->Scissor.Enabled)
  207.       dw3 |= GEN6_SF_SCISSOR_ENABLE;
  208.  
  209.    /* _NEW_POLYGON */
  210.    if (ctx->Polygon.CullFlag) {
  211.       switch (ctx->Polygon.CullFaceMode) {
  212.       case GL_FRONT:
  213.          dw3 |= GEN6_SF_CULL_FRONT;
  214.          break;
  215.       case GL_BACK:
  216.          dw3 |= GEN6_SF_CULL_BACK;
  217.          break;
  218.       case GL_FRONT_AND_BACK:
  219.          dw3 |= GEN6_SF_CULL_BOTH;
  220.          break;
  221.       default:
  222.          assert(0);
  223.          break;
  224.       }
  225.    } else {
  226.       dw3 |= GEN6_SF_CULL_NONE;
  227.    }
  228.  
  229.    /* _NEW_LINE */
  230.    {
  231.       uint32_t line_width_u3_7 = U_FIXED(CLAMP(ctx->Line.Width, 0.0, 7.99), 7);
  232.       /* TODO: line width of 0 is not allowed when MSAA enabled */
  233.       if (line_width_u3_7 == 0)
  234.          line_width_u3_7 = 1;
  235.       dw3 |= line_width_u3_7 << GEN6_SF_LINE_WIDTH_SHIFT;
  236.    }
  237.    if (ctx->Line.SmoothFlag) {
  238.       dw3 |= GEN6_SF_LINE_AA_ENABLE;
  239.       dw3 |= GEN6_SF_LINE_AA_MODE_TRUE;
  240.       dw3 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
  241.    }
  242.    /* _NEW_MULTISAMPLE */
  243.    if (multisampled_fbo && ctx->Multisample.Enabled)
  244.       dw3 |= GEN6_SF_MSRAST_ON_PATTERN;
  245.  
  246.    /* _NEW_PROGRAM | _NEW_POINT */
  247.    if (!(ctx->VertexProgram.PointSizeEnabled ||
  248.          ctx->Point._Attenuated))
  249.       dw4 |= GEN6_SF_USE_STATE_POINT_WIDTH;
  250.  
  251.    /* Clamp to ARB_point_parameters user limits */
  252.    point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
  253.  
  254.    /* Clamp to the hardware limits and convert to fixed point */
  255.    dw4 |= U_FIXED(CLAMP(point_size, 0.125, 255.875), 3);
  256.  
  257.    /*
  258.     * Window coordinates in an FBO are inverted, which means point
  259.     * sprite origin must be inverted, too.
  260.     */
  261.    if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo) {
  262.       point_sprite_origin = GEN6_SF_POINT_SPRITE_LOWERLEFT;
  263.    } else {
  264.       point_sprite_origin = GEN6_SF_POINT_SPRITE_UPPERLEFT;
  265.    }
  266.    dw1 |= point_sprite_origin;
  267.  
  268.    /* _NEW_LIGHT */
  269.    if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
  270.       dw4 |=
  271.          (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
  272.          (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
  273.          (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
  274.    } else {
  275.       dw4 |=
  276.          (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
  277.    }
  278.  
  279.    /* Create the mapping from the FS inputs we produce to the VS outputs
  280.     * they source from.
  281.     */
  282.    uint32_t max_source_attr = 0;
  283.    for (; attr < VARYING_SLOT_MAX; attr++) {
  284.       enum glsl_interp_qualifier interp_qualifier =
  285.          brw->fragment_program->InterpQualifier[attr];
  286.       bool is_gl_Color = attr == VARYING_SLOT_COL0 || attr == VARYING_SLOT_COL1;
  287.  
  288.       if (!(brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(attr)))
  289.          continue;
  290.  
  291.       /* _NEW_POINT */
  292.       if (ctx->Point.PointSprite &&
  293.           (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7) &&
  294.           ctx->Point.CoordReplace[attr - VARYING_SLOT_TEX0]) {
  295.          dw16 |= (1 << input_index);
  296.       }
  297.  
  298.       if (attr == VARYING_SLOT_PNTC)
  299.          dw16 |= (1 << input_index);
  300.  
  301.       /* flat shading */
  302.       if (interp_qualifier == INTERP_QUALIFIER_FLAT ||
  303.           (shade_model_flat && is_gl_Color &&
  304.            interp_qualifier == INTERP_QUALIFIER_NONE))
  305.          dw17 |= (1 << input_index);
  306.  
  307.       /* The hardware can only do the overrides on 16 overrides at a
  308.        * time, and the other up to 16 have to be lined up so that the
  309.        * input index = the output index.  We'll need to do some
  310.        * tweaking to make sure that's the case.
  311.        */
  312.       assert(input_index < 16 || attr == input_index);
  313.  
  314.       /* BRW_NEW_VUE_MAP_GEOM_OUT | _NEW_LIGHT | _NEW_PROGRAM */
  315.       attr_overrides[input_index++] =
  316.          get_attr_override(&brw->vue_map_geom_out,
  317.                            urb_entry_read_offset, attr,
  318.                            ctx->VertexProgram._TwoSideEnabled,
  319.                            &max_source_attr);
  320.    }
  321.  
  322.    for (; input_index < VARYING_SLOT_MAX; input_index++)
  323.       attr_overrides[input_index] = 0;
  324.  
  325.    /* From the Sandy Bridge PRM, Volume 2, Part 1, documentation for
  326.     * 3DSTATE_SF DWord 1 bits 15:11, "Vertex URB Entry Read Length":
  327.     *
  328.     * "This field should be set to the minimum length required to read the
  329.     *  maximum source attribute.  The maximum source attribute is indicated
  330.     *  by the maximum value of the enabled Attribute # Source Attribute if
  331.     *  Attribute Swizzle Enable is set, Number of Output Attributes-1 if
  332.     *  enable is not set.
  333.     *  read_length = ceiling((max_source_attr + 1) / 2)
  334.     *
  335.     *  [errata] Corruption/Hang possible if length programmed larger than
  336.     *  recommended"
  337.     */
  338.    uint32_t urb_entry_read_length = ALIGN(max_source_attr + 1, 2) / 2;
  339.       dw1 |= urb_entry_read_length << GEN6_SF_URB_ENTRY_READ_LENGTH_SHIFT |
  340.              urb_entry_read_offset << GEN6_SF_URB_ENTRY_READ_OFFSET_SHIFT;
  341.  
  342.    BEGIN_BATCH(20);
  343.    OUT_BATCH(_3DSTATE_SF << 16 | (20 - 2));
  344.    OUT_BATCH(dw1);
  345.    OUT_BATCH(dw2);
  346.    OUT_BATCH(dw3);
  347.    OUT_BATCH(dw4);
  348.    OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant.  copied from gen4 */
  349.    OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
  350.    OUT_BATCH_F(0.0); /* XXX: global depth offset clamp */
  351.    for (i = 0; i < 8; i++) {
  352.       OUT_BATCH(attr_overrides[i * 2] | attr_overrides[i * 2 + 1] << 16);
  353.    }
  354.    OUT_BATCH(dw16); /* point sprite texcoord bitmask */
  355.    OUT_BATCH(dw17); /* constant interp bitmask */
  356.    OUT_BATCH(0); /* wrapshortest enables 0-7 */
  357.    OUT_BATCH(0); /* wrapshortest enables 8-15 */
  358.    ADVANCE_BATCH();
  359. }
  360.  
  361. const struct brw_tracked_state gen6_sf_state = {
  362.    .dirty = {
  363.       .mesa  = (_NEW_LIGHT |
  364.                 _NEW_PROGRAM |
  365.                 _NEW_POLYGON |
  366.                 _NEW_LINE |
  367.                 _NEW_SCISSOR |
  368.                 _NEW_BUFFERS |
  369.                 _NEW_POINT |
  370.                 _NEW_MULTISAMPLE),
  371.       .brw   = (BRW_NEW_CONTEXT |
  372.                 BRW_NEW_FRAGMENT_PROGRAM |
  373.                 BRW_NEW_VUE_MAP_GEOM_OUT)
  374.    },
  375.    .emit = upload_sf_state,
  376. };
  377.