Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 2014 LunarG, Inc.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included
  14.  * in all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  19.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22.  * DEALINGS IN THE SOFTWARE.
  23.  *
  24.  * Authors:
  25.  *    Chia-I Wu <olv@lunarg.com>
  26.  */
  27.  
  28. #include "core/ilo_state_3d.h"
  29. #include "util/u_draw.h"
  30. #include "util/u_pack_color.h"
  31.  
  32. #include "ilo_draw.h"
  33. #include "ilo_state.h"
  34. #include "ilo_blit.h"
  35. #include "ilo_blitter.h"
  36.  
  37. /**
  38.  * Set the states that are invariant between all ops.
  39.  */
  40. static bool
  41. ilo_blitter_set_invariants(struct ilo_blitter *blitter)
  42. {
  43.    struct pipe_vertex_element velem;
  44.    struct pipe_viewport_state vp;
  45.  
  46.    if (blitter->initialized)
  47.       return true;
  48.  
  49.    /* only vertex X and Y */
  50.    memset(&velem, 0, sizeof(velem));
  51.    velem.src_format = PIPE_FORMAT_R32G32_FLOAT;
  52.    ilo_gpe_init_ve(blitter->ilo->dev, 1, &velem, &blitter->ve);
  53.  
  54.    /* generate VUE header */
  55.    ilo_gpe_init_ve_nosrc(blitter->ilo->dev,
  56.          GEN6_VFCOMP_STORE_0, /* Reserved */
  57.          GEN6_VFCOMP_STORE_0, /* Render Target Array Index */
  58.          GEN6_VFCOMP_STORE_0, /* Viewport Index */
  59.          GEN6_VFCOMP_STORE_0, /* Point Width */
  60.          &blitter->ve.nosrc_cso);
  61.    blitter->ve.prepend_nosrc_cso = true;
  62.  
  63.    /* a rectangle has 3 vertices in a RECTLIST */
  64.    util_draw_init_info(&blitter->draw);
  65.    blitter->draw.mode = ILO_PRIM_RECTANGLES;
  66.    blitter->draw.count = 3;
  67.  
  68.    /**
  69.     * From the Haswell PRM, volume 7, page 615:
  70.     *
  71.     *     "The clear value must be between the min and max depth values
  72.     *     (inclusive) defined in the CC_VIEWPORT."
  73.     *
  74.     * Even though clipping and viewport transformation will be disabled, we
  75.     * still need to set up the viewport states.
  76.     */
  77.    memset(&vp, 0, sizeof(vp));
  78.    vp.scale[0] = 1.0f;
  79.    vp.scale[1] = 1.0f;
  80.    vp.scale[2] = 1.0f;
  81.    ilo_gpe_set_viewport_cso(blitter->ilo->dev, &vp, &blitter->viewport);
  82.  
  83.    blitter->initialized = true;
  84.  
  85.    return true;
  86. }
  87.  
  88. static void
  89. ilo_blitter_set_op(struct ilo_blitter *blitter,
  90.                    enum ilo_blitter_rectlist_op op)
  91. {
  92.    blitter->op = op;
  93. }
  94.  
  95. /**
  96.  * Set the rectangle primitive.
  97.  */
  98. static void
  99. ilo_blitter_set_rectlist(struct ilo_blitter *blitter,
  100.                          unsigned x, unsigned y,
  101.                          unsigned width, unsigned height)
  102. {
  103.    /*
  104.     * From the Sandy Bridge PRM, volume 2 part 1, page 11:
  105.     *
  106.     *     "(RECTLIST) A list of independent rectangles, where only 3 vertices
  107.     *      are provided per rectangle object, with the fourth vertex implied
  108.     *      by the definition of a rectangle. V0=LowerRight, V1=LowerLeft,
  109.     *      V2=UpperLeft. Implied V3 = V0- V1+V2."
  110.     */
  111.    blitter->vertices[0][0] = (float) (x + width);
  112.    blitter->vertices[0][1] = (float) (y + height);
  113.    blitter->vertices[1][0] = (float) x;
  114.    blitter->vertices[1][1] = (float) (y + height);
  115.    blitter->vertices[2][0] = (float) x;
  116.    blitter->vertices[2][1] = (float) y;
  117. }
  118.  
  119. static void
  120. ilo_blitter_set_clear_values(struct ilo_blitter *blitter,
  121.                              uint32_t depth, ubyte stencil)
  122. {
  123.    blitter->depth_clear_value = depth;
  124.    blitter->cc.stencil_ref.ref_value[0] = stencil;
  125. }
  126.  
  127. static void
  128. ilo_blitter_set_dsa(struct ilo_blitter *blitter,
  129.                     const struct pipe_depth_stencil_alpha_state *state)
  130. {
  131.    ilo_gpe_init_dsa(blitter->ilo->dev, state, &blitter->dsa);
  132. }
  133.  
  134. static void
  135. ilo_blitter_set_fb(struct ilo_blitter *blitter,
  136.                    struct pipe_resource *res, unsigned level,
  137.                    const struct ilo_surface_cso *cso)
  138. {
  139.    struct ilo_texture *tex = ilo_texture(res);
  140.  
  141.    blitter->fb.width = u_minify(tex->image.width0, level);
  142.    blitter->fb.height = u_minify(tex->image.height0, level);
  143.  
  144.    blitter->fb.num_samples = res->nr_samples;
  145.    if (!blitter->fb.num_samples)
  146.       blitter->fb.num_samples = 1;
  147.  
  148.    memcpy(&blitter->fb.dst, cso, sizeof(*cso));
  149. }
  150.  
  151. static void
  152. ilo_blitter_set_fb_from_surface(struct ilo_blitter *blitter,
  153.                                 struct pipe_surface *surf)
  154. {
  155.    ilo_blitter_set_fb(blitter, surf->texture, surf->u.tex.level,
  156.          (const struct ilo_surface_cso *) surf);
  157. }
  158.  
  159. static void
  160. ilo_blitter_set_fb_from_resource(struct ilo_blitter *blitter,
  161.                                  struct pipe_resource *res,
  162.                                  enum pipe_format format,
  163.                                  unsigned level, unsigned slice)
  164. {
  165.    struct pipe_surface templ, *surf;
  166.  
  167.    memset(&templ, 0, sizeof(templ));
  168.    templ.format = format;
  169.    templ.u.tex.level = level;
  170.    templ.u.tex.first_layer = slice;
  171.    templ.u.tex.last_layer = slice;
  172.  
  173.    /* if we did not call create_surface(), it would never fail */
  174.    surf = blitter->ilo->base.create_surface(&blitter->ilo->base, res, &templ);
  175.    assert(surf);
  176.  
  177.    ilo_blitter_set_fb(blitter, res, level,
  178.          (const struct ilo_surface_cso *) surf);
  179.  
  180.    pipe_surface_reference(&surf, NULL);
  181. }
  182.  
  183. static void
  184. ilo_blitter_set_uses(struct ilo_blitter *blitter, uint32_t uses)
  185. {
  186.    blitter->uses = uses;
  187. }
  188.  
  189. static void
  190. hiz_align_fb(struct ilo_blitter *blitter)
  191. {
  192.    unsigned align_w, align_h;
  193.  
  194.    switch (blitter->op) {
  195.    case ILO_BLITTER_RECTLIST_CLEAR_ZS:
  196.    case ILO_BLITTER_RECTLIST_RESOLVE_Z:
  197.       break;
  198.    default:
  199.       return;
  200.       break;
  201.    }
  202.  
  203.    /*
  204.     * From the Sandy Bridge PRM, volume 2 part 1, page 313-314:
  205.     *
  206.     *     "A rectangle primitive representing the clear area is delivered. The
  207.     *      primitive must adhere to the following restrictions on size:
  208.     *
  209.     *      - If Number of Multisamples is NUMSAMPLES_1, the rectangle must be
  210.     *        aligned to an 8x4 pixel block relative to the upper left corner
  211.     *        of the depth buffer, and contain an integer number of these pixel
  212.     *        blocks, and all 8x4 pixels must be lit.
  213.     *
  214.     *      - If Number of Multisamples is NUMSAMPLES_4, the rectangle must be
  215.     *        aligned to a 4x2 pixel block (8x4 sample block) relative to the
  216.     *        upper left corner of the depth buffer, and contain an integer
  217.     *        number of these pixel blocks, and all samples of the 4x2 pixels
  218.     *        must be lit
  219.     *
  220.     *      - If Number of Multisamples is NUMSAMPLES_8, the rectangle must be
  221.     *        aligned to a 2x2 pixel block (8x4 sample block) relative to the
  222.     *        upper left corner of the depth buffer, and contain an integer
  223.     *        number of these pixel blocks, and all samples of the 2x2 pixels
  224.     *        must be list."
  225.     *
  226.     *     "The following is required when performing a depth buffer resolve:
  227.     *
  228.     *      - A rectangle primitive of the same size as the previous depth
  229.     *        buffer clear operation must be delivered, and depth buffer state
  230.     *        cannot have changed since the previous depth buffer clear
  231.     *        operation."
  232.     */
  233.    switch (blitter->fb.num_samples) {
  234.    case 1:
  235.       align_w = 8;
  236.       align_h = 4;
  237.       break;
  238.    case 2:
  239.       align_w = 4;
  240.       align_h = 4;
  241.       break;
  242.    case 4:
  243.       align_w = 4;
  244.       align_h = 2;
  245.       break;
  246.    case 8:
  247.    default:
  248.       align_w = 2;
  249.       align_h = 2;
  250.       break;
  251.    }
  252.  
  253.    if (blitter->fb.width % align_w || blitter->fb.height % align_h) {
  254.       blitter->fb.width = align(blitter->fb.width, align_w);
  255.       blitter->fb.height = align(blitter->fb.height, align_h);
  256.    }
  257. }
  258.  
  259. static void
  260. hiz_emit_rectlist(struct ilo_blitter *blitter)
  261. {
  262.    hiz_align_fb(blitter);
  263.  
  264.    ilo_blitter_set_rectlist(blitter, 0, 0,
  265.          blitter->fb.width, blitter->fb.height);
  266.  
  267.    ilo_draw_rectlist(blitter->ilo);
  268. }
  269.  
  270. static bool
  271. hiz_can_clear_zs(const struct ilo_blitter *blitter,
  272.                  const struct ilo_texture *tex)
  273. {
  274.    /*
  275.     * From the Sandy Bridge PRM, volume 2 part 1, page 314:
  276.     *
  277.     *     "Several cases exist where Depth Buffer Clear cannot be enabled (the
  278.     *      legacy method of clearing must be performed):
  279.     *
  280.     *      - If the depth buffer format is D32_FLOAT_S8X24_UINT or
  281.     *        D24_UNORM_S8_UINT.
  282.     *
  283.     *      - If stencil test is enabled but the separate stencil buffer is
  284.     *        disabled.
  285.     *
  286.     *      - [DevSNB-A{W/A}]: ...
  287.     *
  288.     *      - [DevSNB{W/A}]: When depth buffer format is D16_UNORM and the
  289.     *        width of the map (LOD0) is not multiple of 16, fast clear
  290.     *        optimization must be disabled."
  291.     *
  292.     * From the Ivy Bridge PRM, volume 2 part 1, page 313:
  293.     *
  294.     *     "Several cases exist where Depth Buffer Clear cannot be enabled (the
  295.     *      legacy method of clearing must be performed):
  296.     *
  297.     *      - If the depth buffer format is D32_FLOAT_S8X24_UINT or
  298.     *        D24_UNORM_S8_UINT.
  299.     *
  300.     *      - If stencil test is enabled but the separate stencil buffer is
  301.     *        disabled."
  302.     *
  303.     * The truth is when HiZ is enabled, separate stencil is also enabled on
  304.     * all GENs.  The depth buffer format cannot be combined depth/stencil.
  305.     */
  306.    switch (tex->image.format) {
  307.    case PIPE_FORMAT_Z16_UNORM:
  308.       if (ilo_dev_gen(blitter->ilo->dev) == ILO_GEN(6) &&
  309.           tex->base.width0 % 16)
  310.          return false;
  311.       break;
  312.    case PIPE_FORMAT_Z24_UNORM_S8_UINT:
  313.    case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
  314.       assert(!"HiZ with combined depth/stencil");
  315.       return false;
  316.       break;
  317.    default:
  318.       break;
  319.    }
  320.  
  321.    return true;
  322. }
  323.  
  324. bool
  325. ilo_blitter_rectlist_clear_zs(struct ilo_blitter *blitter,
  326.                               struct pipe_surface *zs,
  327.                               unsigned clear_flags,
  328.                               double depth, unsigned stencil)
  329. {
  330.    struct ilo_texture *tex = ilo_texture(zs->texture);
  331.    struct pipe_depth_stencil_alpha_state dsa_state;
  332.    uint32_t uses, clear_value;
  333.  
  334.    if (!ilo_image_can_enable_aux(&tex->image, zs->u.tex.level))
  335.       return false;
  336.  
  337.    if (!hiz_can_clear_zs(blitter, tex))
  338.       return false;
  339.  
  340.    if (ilo_dev_gen(blitter->ilo->dev) >= ILO_GEN(8))
  341.       clear_value = fui(depth);
  342.    else
  343.       clear_value = util_pack_z(tex->image.format, depth);
  344.  
  345.    ilo_blit_resolve_surface(blitter->ilo, zs,
  346.          ILO_TEXTURE_RENDER_WRITE | ILO_TEXTURE_CLEAR);
  347.    ilo_texture_set_slice_clear_value(tex, zs->u.tex.level,
  348.          zs->u.tex.first_layer,
  349.          zs->u.tex.last_layer - zs->u.tex.first_layer + 1,
  350.          clear_value);
  351.  
  352.    /*
  353.     * From the Sandy Bridge PRM, volume 2 part 1, page 313-314:
  354.     *
  355.     *     "- Depth Test Enable must be disabled and Depth Buffer Write Enable
  356.     *        must be enabled (if depth is being cleared).
  357.     *
  358.     *      - Stencil buffer clear can be performed at the same time by
  359.     *        enabling Stencil Buffer Write Enable.  Stencil Test Enable must
  360.     *        be enabled and Stencil Pass Depth Pass Op set to REPLACE, and the
  361.     *        clear value that is placed in the stencil buffer is the Stencil
  362.     *        Reference Value from COLOR_CALC_STATE.
  363.     *
  364.     *      - Note also that stencil buffer clear can be performed without
  365.     *        depth buffer clear. For stencil only clear, Depth Test Enable and
  366.     *        Depth Buffer Write Enable must be disabled.
  367.     *
  368.     *      - [DevSNB] errata: For stencil buffer only clear, the previous
  369.     *        depth clear value must be delivered during the clear."
  370.     */
  371.    memset(&dsa_state, 0, sizeof(dsa_state));
  372.  
  373.    if (clear_flags & PIPE_CLEAR_DEPTH)
  374.       dsa_state.depth.writemask = true;
  375.  
  376.    if (clear_flags & PIPE_CLEAR_STENCIL) {
  377.       dsa_state.stencil[0].enabled = true;
  378.       dsa_state.stencil[0].func = PIPE_FUNC_ALWAYS;
  379.       dsa_state.stencil[0].fail_op = PIPE_STENCIL_OP_KEEP;
  380.       dsa_state.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
  381.       dsa_state.stencil[0].zfail_op = PIPE_STENCIL_OP_KEEP;
  382.  
  383.       /*
  384.        * From the Ivy Bridge PRM, volume 2 part 1, page 277:
  385.        *
  386.        *     "Additionally the following must be set to the correct values.
  387.        *
  388.        *      - DEPTH_STENCIL_STATE::Stencil Write Mask must be 0xFF
  389.        *      - DEPTH_STENCIL_STATE::Stencil Test Mask must be 0xFF
  390.        *      - DEPTH_STENCIL_STATE::Back Face Stencil Write Mask must be 0xFF
  391.        *      - DEPTH_STENCIL_STATE::Back Face Stencil Test Mask must be 0xFF"
  392.        */
  393.       dsa_state.stencil[0].valuemask = 0xff;
  394.       dsa_state.stencil[0].writemask = 0xff;
  395.       dsa_state.stencil[1].valuemask = 0xff;
  396.       dsa_state.stencil[1].writemask = 0xff;
  397.    }
  398.  
  399.    ilo_blitter_set_invariants(blitter);
  400.    ilo_blitter_set_op(blitter, ILO_BLITTER_RECTLIST_CLEAR_ZS);
  401.  
  402.    ilo_blitter_set_dsa(blitter, &dsa_state);
  403.    ilo_blitter_set_clear_values(blitter, clear_value, (ubyte) stencil);
  404.    ilo_blitter_set_fb_from_surface(blitter, zs);
  405.  
  406.    uses = ILO_BLITTER_USE_DSA;
  407.    if (clear_flags & PIPE_CLEAR_DEPTH)
  408.       uses |= ILO_BLITTER_USE_VIEWPORT | ILO_BLITTER_USE_FB_DEPTH;
  409.    if (clear_flags & PIPE_CLEAR_STENCIL)
  410.       uses |= ILO_BLITTER_USE_CC | ILO_BLITTER_USE_FB_STENCIL;
  411.    ilo_blitter_set_uses(blitter, uses);
  412.  
  413.    hiz_emit_rectlist(blitter);
  414.  
  415.    return true;
  416. }
  417.  
  418. void
  419. ilo_blitter_rectlist_resolve_z(struct ilo_blitter *blitter,
  420.                                struct pipe_resource *res,
  421.                                unsigned level, unsigned slice)
  422. {
  423.    struct ilo_texture *tex = ilo_texture(res);
  424.    struct pipe_depth_stencil_alpha_state dsa_state;
  425.    const struct ilo_texture_slice *s =
  426.       ilo_texture_get_slice(tex, level, slice);
  427.  
  428.    if (!ilo_image_can_enable_aux(&tex->image, level))
  429.       return;
  430.  
  431.    /*
  432.     * From the Sandy Bridge PRM, volume 2 part 1, page 314:
  433.     *
  434.     *     "Depth Test Enable must be enabled with the Depth Test Function set
  435.     *      to NEVER. Depth Buffer Write Enable must be enabled. Stencil Test
  436.     *      Enable and Stencil Buffer Write Enable must be disabled."
  437.     */
  438.    memset(&dsa_state, 0, sizeof(dsa_state));
  439.    dsa_state.depth.writemask = true;
  440.    dsa_state.depth.enabled = true;
  441.    dsa_state.depth.func = PIPE_FUNC_NEVER;
  442.  
  443.    ilo_blitter_set_invariants(blitter);
  444.    ilo_blitter_set_op(blitter, ILO_BLITTER_RECTLIST_RESOLVE_Z);
  445.  
  446.    ilo_blitter_set_dsa(blitter, &dsa_state);
  447.    ilo_blitter_set_clear_values(blitter, s->clear_value, 0);
  448.    ilo_blitter_set_fb_from_resource(blitter, res, res->format, level, slice);
  449.    ilo_blitter_set_uses(blitter,
  450.          ILO_BLITTER_USE_DSA | ILO_BLITTER_USE_FB_DEPTH);
  451.  
  452.    hiz_emit_rectlist(blitter);
  453. }
  454.  
  455. void
  456. ilo_blitter_rectlist_resolve_hiz(struct ilo_blitter *blitter,
  457.                                  struct pipe_resource *res,
  458.                                  unsigned level, unsigned slice)
  459. {
  460.    struct ilo_texture *tex = ilo_texture(res);
  461.    struct pipe_depth_stencil_alpha_state dsa_state;
  462.  
  463.    if (!ilo_image_can_enable_aux(&tex->image, level))
  464.       return;
  465.  
  466.    /*
  467.     * From the Sandy Bridge PRM, volume 2 part 1, page 315:
  468.     *
  469.     *     "(Hierarchical Depth Buffer Resolve) Depth Test Enable must be
  470.     *      disabled. Depth Buffer Write Enable must be enabled. Stencil Test
  471.     *      Enable and Stencil Buffer Write Enable must be disabled."
  472.     */
  473.    memset(&dsa_state, 0, sizeof(dsa_state));
  474.    dsa_state.depth.writemask = true;
  475.  
  476.    ilo_blitter_set_invariants(blitter);
  477.    ilo_blitter_set_op(blitter, ILO_BLITTER_RECTLIST_RESOLVE_HIZ);
  478.  
  479.    ilo_blitter_set_dsa(blitter, &dsa_state);
  480.    ilo_blitter_set_fb_from_resource(blitter, res, res->format, level, slice);
  481.    ilo_blitter_set_uses(blitter,
  482.          ILO_BLITTER_USE_DSA | ILO_BLITTER_USE_FB_DEPTH);
  483.  
  484.    hiz_emit_rectlist(blitter);
  485. }
  486.