Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Copyright © 2011 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.  
  24. #include "brw_context.h"
  25. #include "brw_state.h"
  26. #include "brw_defines.h"
  27. #include "brw_util.h"
  28. #include "main/macros.h"
  29. #include "main/fbobject.h"
  30. #include "intel_batchbuffer.h"
  31.  
  32. static void
  33. upload_sbe_state(struct brw_context *brw)
  34. {
  35.    struct gl_context *ctx = &brw->ctx;
  36.    /* BRW_NEW_FRAGMENT_PROGRAM */
  37.    uint32_t num_outputs = _mesa_bitcount_64(brw->fragment_program->Base.InputsRead);
  38.    /* _NEW_LIGHT */
  39.    bool shade_model_flat = ctx->Light.ShadeModel == GL_FLAT;
  40.    uint32_t dw1, dw10, dw11;
  41.    int i;
  42.    int attr = 0, input_index = 0;
  43.    int urb_entry_read_offset = 1;
  44.    uint16_t attr_overrides[VARYING_SLOT_MAX];
  45.    /* _NEW_BUFFERS */
  46.    bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
  47.    uint32_t point_sprite_origin;
  48.  
  49.    /* FINISHME: Attribute Swizzle Control Mode? */
  50.    dw1 = GEN7_SBE_SWIZZLE_ENABLE | num_outputs << GEN7_SBE_NUM_OUTPUTS_SHIFT;
  51.  
  52.    /* _NEW_POINT
  53.     *
  54.     * Window coordinates in an FBO are inverted, which means point
  55.     * sprite origin must be inverted.
  56.     */
  57.    if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo) {
  58.       point_sprite_origin = GEN6_SF_POINT_SPRITE_LOWERLEFT;
  59.    } else {
  60.       point_sprite_origin = GEN6_SF_POINT_SPRITE_UPPERLEFT;
  61.    }
  62.    dw1 |= point_sprite_origin;
  63.  
  64.  
  65.    dw10 = 0;
  66.    dw11 = 0;
  67.  
  68.    /* Create the mapping from the FS inputs we produce to the VS outputs
  69.     * they source from.
  70.     */
  71.    uint32_t max_source_attr = 0;
  72.    for (; attr < VARYING_SLOT_MAX; attr++) {
  73.       enum glsl_interp_qualifier interp_qualifier =
  74.          brw->fragment_program->InterpQualifier[attr];
  75.       bool is_gl_Color = attr == VARYING_SLOT_COL0 || attr == VARYING_SLOT_COL1;
  76.  
  77.       if (!(brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(attr)))
  78.          continue;
  79.  
  80.       if (ctx->Point.PointSprite &&
  81.           attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7 &&
  82.           ctx->Point.CoordReplace[attr - VARYING_SLOT_TEX0]) {
  83.          dw10 |= (1 << input_index);
  84.       }
  85.  
  86.       if (attr == VARYING_SLOT_PNTC)
  87.          dw10 |= (1 << input_index);
  88.  
  89.       /* flat shading */
  90.       if (interp_qualifier == INTERP_QUALIFIER_FLAT ||
  91.           (shade_model_flat && is_gl_Color &&
  92.            interp_qualifier == INTERP_QUALIFIER_NONE))
  93.          dw11 |= (1 << input_index);
  94.  
  95.       /* The hardware can only do the overrides on 16 overrides at a
  96.        * time, and the other up to 16 have to be lined up so that the
  97.        * input index = the output index.  We'll need to do some
  98.        * tweaking to make sure that's the case.
  99.        */
  100.       assert(input_index < 16 || attr == input_index);
  101.  
  102.       /* BRW_NEW_VUE_MAP_GEOM_OUT | _NEW_LIGHT | _NEW_PROGRAM */
  103.       attr_overrides[input_index++] =
  104.          get_attr_override(&brw->vue_map_geom_out,
  105.                            urb_entry_read_offset, attr,
  106.                            ctx->VertexProgram._TwoSideEnabled,
  107.                            &max_source_attr);
  108.    }
  109.  
  110.    /* From the Ivy Bridge PRM, Volume 2, Part 1, documentation for
  111.     * 3DSTATE_SBE DWord 1 bits 15:11, "Vertex URB Entry Read Length":
  112.     *
  113.     * "This field should be set to the minimum length required to read the
  114.     *  maximum source attribute.  The maximum source attribute is indicated
  115.     *  by the maximum value of the enabled Attribute # Source Attribute if
  116.     *  Attribute Swizzle Enable is set, Number of Output Attributes-1 if
  117.     *  enable is not set.
  118.     *
  119.     *  read_length = ceiling((max_source_attr + 1) / 2)"
  120.     */
  121.    uint32_t urb_entry_read_length = ALIGN(max_source_attr + 1, 2) / 2;
  122.    dw1 |= urb_entry_read_length << GEN7_SBE_URB_ENTRY_READ_LENGTH_SHIFT |
  123.           urb_entry_read_offset << GEN7_SBE_URB_ENTRY_READ_OFFSET_SHIFT;
  124.  
  125.    for (; input_index < VARYING_SLOT_MAX; input_index++)
  126.       attr_overrides[input_index] = 0;
  127.  
  128.    BEGIN_BATCH(14);
  129.    OUT_BATCH(_3DSTATE_SBE << 16 | (14 - 2));
  130.    OUT_BATCH(dw1);
  131.  
  132.    /* Output dwords 2 through 9 */
  133.    for (i = 0; i < 8; i++) {
  134.       OUT_BATCH(attr_overrides[i * 2] | attr_overrides[i * 2 + 1] << 16);
  135.    }
  136.  
  137.    OUT_BATCH(dw10); /* point sprite texcoord bitmask */
  138.    OUT_BATCH(dw11); /* constant interp bitmask */
  139.    OUT_BATCH(0); /* wrapshortest enables 0-7 */
  140.    OUT_BATCH(0); /* wrapshortest enables 8-15 */
  141.    ADVANCE_BATCH();
  142. }
  143.  
  144. const struct brw_tracked_state gen7_sbe_state = {
  145.    .dirty = {
  146.       .mesa  = (_NEW_BUFFERS |
  147.                 _NEW_LIGHT |
  148.                 _NEW_POINT |
  149.                 _NEW_PROGRAM),
  150.       .brw   = (BRW_NEW_CONTEXT |
  151.                 BRW_NEW_FRAGMENT_PROGRAM |
  152.                 BRW_NEW_VUE_MAP_GEOM_OUT)
  153.    },
  154.    .emit = upload_sbe_state,
  155. };
  156.  
  157. static void
  158. upload_sf_state(struct brw_context *brw)
  159. {
  160.    struct gl_context *ctx = &brw->ctx;
  161.    uint32_t dw1, dw2, dw3;
  162.    float point_size;
  163.    /* _NEW_BUFFERS */
  164.    bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
  165.    bool multisampled_fbo = ctx->DrawBuffer->Visual.samples > 1;
  166.  
  167.    dw1 = GEN6_SF_STATISTICS_ENABLE |
  168.          GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
  169.  
  170.    /* _NEW_BUFFERS */
  171.    dw1 |= (brw_depthbuffer_format(brw) << GEN7_SF_DEPTH_BUFFER_SURFACE_FORMAT_SHIFT);
  172.  
  173.    /* _NEW_POLYGON */
  174.    if ((ctx->Polygon.FrontFace == GL_CCW) ^ render_to_fbo)
  175.       dw1 |= GEN6_SF_WINDING_CCW;
  176.  
  177.    if (ctx->Polygon.OffsetFill)
  178.        dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
  179.  
  180.    if (ctx->Polygon.OffsetLine)
  181.        dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
  182.  
  183.    if (ctx->Polygon.OffsetPoint)
  184.        dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
  185.  
  186.    switch (ctx->Polygon.FrontMode) {
  187.    case GL_FILL:
  188.        dw1 |= GEN6_SF_FRONT_SOLID;
  189.        break;
  190.  
  191.    case GL_LINE:
  192.        dw1 |= GEN6_SF_FRONT_WIREFRAME;
  193.        break;
  194.  
  195.    case GL_POINT:
  196.        dw1 |= GEN6_SF_FRONT_POINT;
  197.        break;
  198.  
  199.    default:
  200.        assert(0);
  201.        break;
  202.    }
  203.  
  204.    switch (ctx->Polygon.BackMode) {
  205.    case GL_FILL:
  206.        dw1 |= GEN6_SF_BACK_SOLID;
  207.        break;
  208.  
  209.    case GL_LINE:
  210.        dw1 |= GEN6_SF_BACK_WIREFRAME;
  211.        break;
  212.  
  213.    case GL_POINT:
  214.        dw1 |= GEN6_SF_BACK_POINT;
  215.        break;
  216.  
  217.    default:
  218.        assert(0);
  219.        break;
  220.    }
  221.  
  222.    dw2 = 0;
  223.  
  224.    if (ctx->Polygon.CullFlag) {
  225.       switch (ctx->Polygon.CullFaceMode) {
  226.       case GL_FRONT:
  227.          dw2 |= GEN6_SF_CULL_FRONT;
  228.          break;
  229.       case GL_BACK:
  230.          dw2 |= GEN6_SF_CULL_BACK;
  231.          break;
  232.       case GL_FRONT_AND_BACK:
  233.          dw2 |= GEN6_SF_CULL_BOTH;
  234.          break;
  235.       default:
  236.          assert(0);
  237.          break;
  238.       }
  239.    } else {
  240.       dw2 |= GEN6_SF_CULL_NONE;
  241.    }
  242.  
  243.    /* _NEW_SCISSOR */
  244.    if (ctx->Scissor.Enabled)
  245.       dw2 |= GEN6_SF_SCISSOR_ENABLE;
  246.  
  247.    /* _NEW_LINE */
  248.    {
  249.       uint32_t line_width_u3_7 = U_FIXED(CLAMP(ctx->Line.Width, 0.0, 7.99), 7);
  250.       /* TODO: line width of 0 is not allowed when MSAA enabled */
  251.       if (line_width_u3_7 == 0)
  252.          line_width_u3_7 = 1;
  253.       dw2 |= line_width_u3_7 << GEN6_SF_LINE_WIDTH_SHIFT;
  254.    }
  255.    if (ctx->Line.SmoothFlag) {
  256.       dw2 |= GEN6_SF_LINE_AA_ENABLE;
  257.       dw2 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
  258.    }
  259.    if (ctx->Line.StippleFlag && brw->is_haswell) {
  260.       dw2 |= HSW_SF_LINE_STIPPLE_ENABLE;
  261.    }
  262.    /* _NEW_MULTISAMPLE */
  263.    if (multisampled_fbo && ctx->Multisample.Enabled)
  264.       dw2 |= GEN6_SF_MSRAST_ON_PATTERN;
  265.  
  266.    /* FINISHME: Last Pixel Enable?  Vertex Sub Pixel Precision Select?
  267.     */
  268.  
  269.    dw3 = GEN6_SF_LINE_AA_MODE_TRUE;
  270.  
  271.    /* _NEW_PROGRAM | _NEW_POINT */
  272.    if (!(ctx->VertexProgram.PointSizeEnabled || ctx->Point._Attenuated))
  273.       dw3 |= GEN6_SF_USE_STATE_POINT_WIDTH;
  274.  
  275.    /* Clamp to ARB_point_parameters user limits */
  276.    point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
  277.  
  278.    /* Clamp to the hardware limits and convert to fixed point */
  279.    dw3 |= U_FIXED(CLAMP(point_size, 0.125, 255.875), 3);
  280.  
  281.    /* _NEW_LIGHT */
  282.    if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
  283.       dw3 |=
  284.          (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
  285.          (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
  286.          (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
  287.    } else {
  288.       dw3 |= (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
  289.    }
  290.  
  291.    BEGIN_BATCH(7);
  292.    OUT_BATCH(_3DSTATE_SF << 16 | (7 - 2));
  293.    OUT_BATCH(dw1);
  294.    OUT_BATCH(dw2);
  295.    OUT_BATCH(dw3);
  296.    OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant.  copied from gen4 */
  297.    OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
  298.    OUT_BATCH_F(0.0); /* XXX: global depth offset clamp */
  299.    ADVANCE_BATCH();
  300. }
  301.  
  302. const struct brw_tracked_state gen7_sf_state = {
  303.    .dirty = {
  304.       .mesa  = (_NEW_LIGHT |
  305.                 _NEW_PROGRAM |
  306.                 _NEW_POLYGON |
  307.                 _NEW_LINE |
  308.                 _NEW_SCISSOR |
  309.                 _NEW_BUFFERS |
  310.                 _NEW_POINT |
  311.                 _NEW_MULTISAMPLE),
  312.       .brw   = BRW_NEW_CONTEXT,
  313.       .cache = CACHE_NEW_VS_PROG
  314.    },
  315.    .emit = upload_sf_state,
  316. };
  317.