Subversion Repositories Kolibri OS

Rev

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(struct brw_context *brw)
  34. {
  35.    struct gl_context *ctx = &brw->ctx;
  36.    /* BRW_NEW_FS_PROG_DATA */
  37.    uint32_t num_outputs = brw->wm.prog_data->num_varying_inputs;
  38.    uint16_t attr_overrides[VARYING_SLOT_MAX];
  39.    uint32_t urb_entry_read_length;
  40.    uint32_t point_sprite_enables;
  41.    uint32_t flat_enables;
  42.    int sbe_cmd_length;
  43.  
  44.    uint32_t dw1 =
  45.       GEN7_SBE_SWIZZLE_ENABLE |
  46.       num_outputs << GEN7_SBE_NUM_OUTPUTS_SHIFT;
  47.    uint32_t dw4 = 0;
  48.    uint32_t dw5 = 0;
  49.  
  50.    /* _NEW_BUFFERS */
  51.    bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
  52.  
  53.    /* _NEW_POINT
  54.     *
  55.     * Window coordinates in an FBO are inverted, which means point
  56.     * sprite origin must be inverted.
  57.     */
  58.    if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo)
  59.       dw1 |= GEN6_SF_POINT_SPRITE_LOWERLEFT;
  60.    else
  61.       dw1 |= GEN6_SF_POINT_SPRITE_UPPERLEFT;
  62.  
  63.    /* BRW_NEW_VUE_MAP_GEOM_OUT | BRW_NEW_FRAGMENT_PROGRAM |
  64.     * _NEW_POINT | _NEW_LIGHT | _NEW_PROGRAM | BRW_NEW_FS_PROG_DATA
  65.     */
  66.    calculate_attr_overrides(brw, attr_overrides,
  67.                             &point_sprite_enables,
  68.                             &flat_enables,
  69.                             &urb_entry_read_length);
  70.  
  71.    /* Typically, the URB entry read length and offset should be programmed in
  72.     * 3DSTATE_VS and 3DSTATE_GS; SBE inherits it from the last active stage
  73.     * which produces geometry.  However, we don't know the proper value until
  74.     * we call calculate_attr_overrides().
  75.     *
  76.     * To fit with our existing code, we override the inherited values and
  77.     * specify it here directly, as we did on previous generations.
  78.     */
  79.    dw1 |=
  80.       urb_entry_read_length << GEN7_SBE_URB_ENTRY_READ_LENGTH_SHIFT |
  81.       BRW_SF_URB_ENTRY_READ_OFFSET << GEN8_SBE_URB_ENTRY_READ_OFFSET_SHIFT |
  82.       GEN8_SBE_FORCE_URB_ENTRY_READ_LENGTH |
  83.       GEN8_SBE_FORCE_URB_ENTRY_READ_OFFSET;
  84.  
  85.    if (brw->gen == 8) {
  86.       sbe_cmd_length = 4;
  87.    } else {
  88.       sbe_cmd_length = 6;
  89.  
  90.       /* prepare the active component dwords */
  91.       int input_index = 0;
  92.       for (int attr = 0; attr < VARYING_SLOT_MAX; attr++) {
  93.          if (!(brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(attr)))
  94.             continue;
  95.  
  96.          assert(input_index < 32);
  97.  
  98.          if (input_index < 16)
  99.             dw4 |= (GEN9_SBE_ACTIVE_COMPONENT_XYZW << (input_index << 1));
  100.          else
  101.             dw5 |= (GEN9_SBE_ACTIVE_COMPONENT_XYZW << ((input_index - 16) << 1));
  102.  
  103.          ++input_index;
  104.       }
  105.    }
  106.    BEGIN_BATCH(sbe_cmd_length);
  107.    OUT_BATCH(_3DSTATE_SBE << 16 | (sbe_cmd_length - 2));
  108.    OUT_BATCH(dw1);
  109.    OUT_BATCH(point_sprite_enables);
  110.    OUT_BATCH(flat_enables);
  111.    if (sbe_cmd_length >= 6) {
  112.       OUT_BATCH(dw4);
  113.       OUT_BATCH(dw5);
  114.    }
  115.    ADVANCE_BATCH();
  116.  
  117.    BEGIN_BATCH(11);
  118.    OUT_BATCH(_3DSTATE_SBE_SWIZ << 16 | (11 - 2));
  119.  
  120.    /* Output DWords 1 through 8: */
  121.    for (int i = 0; i < 8; i++) {
  122.       OUT_BATCH(attr_overrides[i * 2] | attr_overrides[i * 2 + 1] << 16);
  123.    }
  124.  
  125.    OUT_BATCH(0); /* wrapshortest enables 0-7 */
  126.    OUT_BATCH(0); /* wrapshortest enables 8-15 */
  127.    ADVANCE_BATCH();
  128. }
  129.  
  130. const struct brw_tracked_state gen8_sbe_state = {
  131.    .dirty = {
  132.       .mesa  = _NEW_BUFFERS |
  133.                _NEW_LIGHT |
  134.                _NEW_POINT |
  135.                _NEW_PROGRAM,
  136.       .brw   = BRW_NEW_CONTEXT |
  137.                BRW_NEW_FRAGMENT_PROGRAM |
  138.                BRW_NEW_FS_PROG_DATA |
  139.                BRW_NEW_VUE_MAP_GEOM_OUT,
  140.    },
  141.    .emit = upload_sbe,
  142. };
  143.  
  144. static void
  145. upload_sf(struct brw_context *brw)
  146. {
  147.    struct gl_context *ctx = &brw->ctx;
  148.    uint32_t dw1 = 0, dw2 = 0, dw3 = 0;
  149.    float point_size;
  150.  
  151.    dw1 = GEN6_SF_STATISTICS_ENABLE;
  152.  
  153.    if (brw->sf.viewport_transform_enable)
  154.        dw1 |= GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
  155.  
  156.    /* _NEW_LINE */
  157.    /* OpenGL dictates that line width should be rounded to the nearest
  158.     * integer
  159.     */
  160.    float line_width =
  161.       roundf(CLAMP(ctx->Line.Width, 0.0, ctx->Const.MaxLineWidth));
  162.    uint32_t line_width_u3_7 = U_FIXED(line_width, 7);
  163.    if (line_width_u3_7 == 0)
  164.       line_width_u3_7 = 1;
  165.    if (brw->gen >= 9 || brw->is_cherryview) {
  166.       dw1 |= line_width_u3_7 << GEN9_SF_LINE_WIDTH_SHIFT;
  167.    } else {
  168.       dw2 |= line_width_u3_7 << GEN6_SF_LINE_WIDTH_SHIFT;
  169.    }
  170.  
  171.    if (ctx->Line.SmoothFlag) {
  172.       dw2 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
  173.    }
  174.  
  175.    /* Clamp to ARB_point_parameters user limits */
  176.    point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
  177.  
  178.    /* Clamp to the hardware limits and convert to fixed point */
  179.    dw3 |= U_FIXED(CLAMP(point_size, 0.125, 255.875), 3);
  180.  
  181.    /* _NEW_PROGRAM | _NEW_POINT */
  182.    if (!(ctx->VertexProgram.PointSizeEnabled || ctx->Point._Attenuated))
  183.       dw3 |= GEN6_SF_USE_STATE_POINT_WIDTH;
  184.  
  185.    /* _NEW_POINT | _NEW_MULTISAMPLE */
  186.    if ((ctx->Point.SmoothFlag || ctx->Multisample._Enabled) &&
  187.        !ctx->Point.PointSprite) {
  188.       dw3 |= GEN8_SF_SMOOTH_POINT_ENABLE;
  189.    }
  190.  
  191.    dw3 |= GEN6_SF_LINE_AA_MODE_TRUE;
  192.  
  193.    /* _NEW_LIGHT */
  194.    if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
  195.       dw3 |= (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
  196.              (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
  197.              (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
  198.    } else {
  199.       dw3 |= (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
  200.    }
  201.  
  202.    BEGIN_BATCH(4);
  203.    OUT_BATCH(_3DSTATE_SF << 16 | (4 - 2));
  204.    OUT_BATCH(dw1);
  205.    OUT_BATCH(dw2);
  206.    OUT_BATCH(dw3);
  207.    ADVANCE_BATCH();
  208. }
  209.  
  210. const struct brw_tracked_state gen8_sf_state = {
  211.    .dirty = {
  212.       .mesa  = _NEW_LIGHT |
  213.                _NEW_PROGRAM |
  214.                _NEW_LINE |
  215.                _NEW_MULTISAMPLE |
  216.                _NEW_POINT,
  217.       .brw   = BRW_NEW_CONTEXT,
  218.    },
  219.    .emit = upload_sf,
  220. };
  221.  
  222. static void
  223. upload_raster(struct brw_context *brw)
  224. {
  225.    struct gl_context *ctx = &brw->ctx;
  226.    uint32_t dw1 = 0;
  227.  
  228.    /* _NEW_BUFFERS */
  229.    bool render_to_fbo = _mesa_is_user_fbo(brw->ctx.DrawBuffer);
  230.  
  231.    /* _NEW_POLYGON */
  232.    if (ctx->Polygon._FrontBit == render_to_fbo)
  233.       dw1 |= GEN8_RASTER_FRONT_WINDING_CCW;
  234.  
  235.    if (ctx->Polygon.CullFlag) {
  236.       switch (ctx->Polygon.CullFaceMode) {
  237.       case GL_FRONT:
  238.          dw1 |= GEN8_RASTER_CULL_FRONT;
  239.          break;
  240.       case GL_BACK:
  241.          dw1 |= GEN8_RASTER_CULL_BACK;
  242.          break;
  243.       case GL_FRONT_AND_BACK:
  244.          dw1 |= GEN8_RASTER_CULL_BOTH;
  245.          break;
  246.       default:
  247.          unreachable("not reached");
  248.       }
  249.    } else {
  250.       dw1 |= GEN8_RASTER_CULL_NONE;
  251.    }
  252.  
  253.    /* _NEW_POINT */
  254.    if (ctx->Point.SmoothFlag)
  255.       dw1 |= GEN8_RASTER_SMOOTH_POINT_ENABLE;
  256.  
  257.    if (ctx->Multisample._Enabled)
  258.       dw1 |= GEN8_RASTER_API_MULTISAMPLE_ENABLE;
  259.  
  260.    if (ctx->Polygon.OffsetFill)
  261.       dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
  262.  
  263.    if (ctx->Polygon.OffsetLine)
  264.       dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
  265.  
  266.    if (ctx->Polygon.OffsetPoint)
  267.       dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
  268.  
  269.    switch (ctx->Polygon.FrontMode) {
  270.    case GL_FILL:
  271.       dw1 |= GEN6_SF_FRONT_SOLID;
  272.       break;
  273.    case GL_LINE:
  274.       dw1 |= GEN6_SF_FRONT_WIREFRAME;
  275.       break;
  276.    case GL_POINT:
  277.       dw1 |= GEN6_SF_FRONT_POINT;
  278.       break;
  279.  
  280.    default:
  281.       unreachable("not reached");
  282.    }
  283.  
  284.    switch (ctx->Polygon.BackMode) {
  285.    case GL_FILL:
  286.       dw1 |= GEN6_SF_BACK_SOLID;
  287.       break;
  288.    case GL_LINE:
  289.       dw1 |= GEN6_SF_BACK_WIREFRAME;
  290.       break;
  291.    case GL_POINT:
  292.       dw1 |= GEN6_SF_BACK_POINT;
  293.       break;
  294.    default:
  295.       unreachable("not reached");
  296.    }
  297.  
  298.    /* _NEW_LINE */
  299.    if (ctx->Line.SmoothFlag)
  300.       dw1 |= GEN8_RASTER_LINE_AA_ENABLE;
  301.  
  302.    /* _NEW_SCISSOR */
  303.    if (ctx->Scissor.EnableFlags)
  304.       dw1 |= GEN8_RASTER_SCISSOR_ENABLE;
  305.  
  306.    /* _NEW_TRANSFORM */
  307.    if (!ctx->Transform.DepthClamp) {
  308.       if (brw->gen >= 9) {
  309.          dw1 |= GEN9_RASTER_VIEWPORT_Z_NEAR_CLIP_TEST_ENABLE |
  310.                 GEN9_RASTER_VIEWPORT_Z_FAR_CLIP_TEST_ENABLE;
  311.       } else {
  312.          dw1 |= GEN8_RASTER_VIEWPORT_Z_CLIP_TEST_ENABLE;
  313.       }
  314.    }
  315.  
  316.    BEGIN_BATCH(5);
  317.    OUT_BATCH(_3DSTATE_RASTER << 16 | (5 - 2));
  318.    OUT_BATCH(dw1);
  319.    OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant.  copied from gen4 */
  320.    OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
  321.    OUT_BATCH_F(ctx->Polygon.OffsetClamp); /* global depth offset clamp */
  322.    ADVANCE_BATCH();
  323. }
  324.  
  325. const struct brw_tracked_state gen8_raster_state = {
  326.    .dirty = {
  327.       .mesa  = _NEW_BUFFERS |
  328.                _NEW_LINE |
  329.                _NEW_MULTISAMPLE |
  330.                _NEW_POINT |
  331.                _NEW_POLYGON |
  332.                _NEW_SCISSOR |
  333.                _NEW_TRANSFORM,
  334.       .brw   = BRW_NEW_CONTEXT,
  335.    },
  336.    .emit = upload_raster,
  337. };
  338.