Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright © 2014 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 "main/mtypes.h"
  25. #include "main/macros.h"
  26. #include "main/context.h"
  27. #include "main/objectlabel.h"
  28. #include "main/shaderapi.h"
  29. #include "main/arrayobj.h"
  30. #include "main/bufferobj.h"
  31. #include "main/buffers.h"
  32. #include "main/blend.h"
  33. #include "main/enable.h"
  34. #include "main/depth.h"
  35. #include "main/stencil.h"
  36. #include "main/varray.h"
  37. #include "main/uniforms.h"
  38. #include "main/fbobject.h"
  39. #include "main/texobj.h"
  40.  
  41. #include "main/api_validate.h"
  42. #include "main/state.h"
  43.  
  44. #include "vbo/vbo_context.h"
  45.  
  46. #include "drivers/common/meta.h"
  47.  
  48. #include "brw_defines.h"
  49. #include "brw_context.h"
  50. #include "brw_draw.h"
  51. #include "intel_fbo.h"
  52. #include "intel_batchbuffer.h"
  53.  
  54. #include "brw_blorp.h"
  55.  
  56. struct brw_fast_clear_state {
  57.    GLuint vao;
  58.    GLuint vbo;
  59.    GLuint shader_prog;
  60.    GLint color_location;
  61. };
  62.  
  63. static bool
  64. brw_fast_clear_init(struct brw_context *brw)
  65. {
  66.    struct brw_fast_clear_state *clear;
  67.  
  68.    if (brw->fast_clear_state) {
  69.       clear = brw->fast_clear_state;
  70.       _mesa_BindVertexArray(clear->vao);
  71.       _mesa_BindBuffer(GL_ARRAY_BUFFER, clear->vbo);
  72.       return true;
  73.    }
  74.  
  75.    brw->fast_clear_state = clear = malloc(sizeof *clear);
  76.    if (clear == NULL)
  77.       return false;
  78.  
  79.    memset(clear, 0, sizeof *clear);
  80.    _mesa_GenVertexArrays(1, &clear->vao);
  81.    _mesa_BindVertexArray(clear->vao);
  82.    _mesa_GenBuffers(1, &clear->vbo);
  83.    _mesa_BindBuffer(GL_ARRAY_BUFFER, clear->vbo);
  84.    _mesa_VertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
  85.    _mesa_EnableVertexAttribArray(0);
  86.  
  87.    return true;
  88. }
  89.  
  90. static void
  91. brw_bind_rep_write_shader(struct brw_context *brw, float *color)
  92. {
  93.    const char *vs_source =
  94.       "#extension GL_AMD_vertex_shader_layer : enable\n"
  95.       "#extension GL_ARB_draw_instanced : enable\n"
  96.       "attribute vec4 position;\n"
  97.       "uniform int layer;\n"
  98.       "void main()\n"
  99.       "{\n"
  100.       "#ifdef GL_AMD_vertex_shader_layer\n"
  101.       "   gl_Layer = gl_InstanceID;\n"
  102.       "#endif\n"
  103.       "   gl_Position = position;\n"
  104.       "}\n";
  105.    const char *fs_source =
  106.       "uniform vec4 color;\n"
  107.       "void main()\n"
  108.       "{\n"
  109.       "   gl_FragColor = color;\n"
  110.       "}\n";
  111.  
  112.    GLuint vs, fs;
  113.    struct brw_fast_clear_state *clear = brw->fast_clear_state;
  114.    struct gl_context *ctx = &brw->ctx;
  115.  
  116.    if (clear->shader_prog) {
  117.       _mesa_UseProgram(clear->shader_prog);
  118.       _mesa_Uniform4fv(clear->color_location, 1, color);
  119.       return;
  120.    }
  121.  
  122.    vs = _mesa_meta_compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_source);
  123.    fs = _mesa_meta_compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_source);
  124.  
  125.    clear->shader_prog = _mesa_CreateProgram();
  126.    _mesa_AttachShader(clear->shader_prog, fs);
  127.    _mesa_DeleteShader(fs);
  128.    _mesa_AttachShader(clear->shader_prog, vs);
  129.    _mesa_DeleteShader(vs);
  130.    _mesa_BindAttribLocation(clear->shader_prog, 0, "position");
  131.    _mesa_ObjectLabel(GL_PROGRAM, clear->shader_prog, -1, "meta clear");
  132.    _mesa_LinkProgram(clear->shader_prog);
  133.  
  134.    clear->color_location =
  135.       _mesa_GetUniformLocation(clear->shader_prog, "color");
  136.  
  137.    _mesa_UseProgram(clear->shader_prog);
  138.    _mesa_Uniform4fv(clear->color_location, 1, color);
  139. }
  140.  
  141. void
  142. brw_meta_fast_clear_free(struct brw_context *brw)
  143. {
  144.    struct brw_fast_clear_state *clear = brw->fast_clear_state;
  145.    GET_CURRENT_CONTEXT(old_context);
  146.  
  147.    if (clear == NULL)
  148.       return;
  149.  
  150.    _mesa_make_current(&brw->ctx, NULL, NULL);
  151.  
  152.    _mesa_DeleteVertexArrays(1, &clear->vao);
  153.    _mesa_DeleteBuffers(1, &clear->vbo);
  154.    _mesa_DeleteProgram(clear->shader_prog);
  155.    free(clear);
  156.  
  157.    if (old_context)
  158.       _mesa_make_current(old_context, old_context->WinSysDrawBuffer, old_context->WinSysReadBuffer);
  159.    else
  160.       _mesa_make_current(NULL, NULL, NULL);
  161. }
  162.  
  163. struct rect {
  164.    int x0, y0, x1, y1;
  165. };
  166.  
  167. static void
  168. brw_draw_rectlist(struct gl_context *ctx, struct rect *rect, int num_instances)
  169. {
  170.    int start = 0, count = 3;
  171.    struct _mesa_prim prim;
  172.    float verts[6];
  173.  
  174.    verts[0] = rect->x1;
  175.    verts[1] = rect->y1;
  176.    verts[2] = rect->x0;
  177.    verts[3] = rect->y1;
  178.    verts[4] = rect->x0;
  179.    verts[5] = rect->y0;
  180.  
  181.    /* upload new vertex data */
  182.    _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts,
  183.                     GL_DYNAMIC_DRAW_ARB);
  184.  
  185.    if (ctx->NewState)
  186.       _mesa_update_state(ctx);
  187.  
  188.    vbo_bind_arrays(ctx);
  189.  
  190.    memset(&prim, 0, sizeof prim);
  191.    prim.begin = 1;
  192.    prim.end = 1;
  193.    prim.mode = BRW_PRIM_OFFSET + _3DPRIM_RECTLIST;
  194.    prim.num_instances = num_instances;
  195.    prim.start = start;
  196.    prim.count = count;
  197.  
  198.    /* Make sure our internal prim value doesn't clash with a valid GL value. */
  199.    assert(!_mesa_is_valid_prim_mode(ctx, prim.mode));
  200.  
  201.    brw_draw_prims(ctx, &prim, 1, NULL,
  202.                   GL_TRUE, start, start + count - 1,
  203.                   NULL, NULL);
  204. }
  205.  
  206. static void
  207. get_fast_clear_rect(struct brw_context *brw, struct gl_framebuffer *fb,
  208.                     struct intel_renderbuffer *irb, struct rect *rect)
  209. {
  210.    unsigned int x_align, y_align;
  211.    unsigned int x_scaledown, y_scaledown;
  212.  
  213.    if (irb->mt->msaa_layout == INTEL_MSAA_LAYOUT_NONE) {
  214.       /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
  215.        * Target(s)", beneath the "Fast Color Clear" bullet (p327):
  216.        *
  217.        *     Clear pass must have a clear rectangle that must follow
  218.        *     alignment rules in terms of pixels and lines as shown in the
  219.        *     table below. Further, the clear-rectangle height and width
  220.        *     must be multiple of the following dimensions. If the height
  221.        *     and width of the render target being cleared do not meet these
  222.        *     requirements, an MCS buffer can be created such that it
  223.        *     follows the requirement and covers the RT.
  224.        *
  225.        * The alignment size in the table that follows is related to the
  226.        * alignment size returned by intel_get_non_msrt_mcs_alignment(), but
  227.        * with X alignment multiplied by 16 and Y alignment multiplied by 32.
  228.        */
  229.       intel_get_non_msrt_mcs_alignment(brw, irb->mt, &x_align, &y_align);
  230.       x_align *= 16;
  231.       y_align *= 32;
  232.  
  233.       /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
  234.        * Target(s)", beneath the "Fast Color Clear" bullet (p327):
  235.        *
  236.        *     In order to optimize the performance MCS buffer (when bound to
  237.        *     1X RT) clear similarly to MCS buffer clear for MSRT case,
  238.        *     clear rect is required to be scaled by the following factors
  239.        *     in the horizontal and vertical directions:
  240.        *
  241.        * The X and Y scale down factors in the table that follows are each
  242.        * equal to half the alignment value computed above.
  243.        */
  244.       x_scaledown = x_align / 2;
  245.       y_scaledown = y_align / 2;
  246.  
  247.       /* From BSpec: 3D-Media-GPGPU Engine > 3D Pipeline > Pixel > Pixel
  248.        * Backend > MCS Buffer for Render Target(s) [DevIVB+] > Table "Color
  249.        * Clear of Non-MultiSampled Render Target Restrictions":
  250.        *
  251.        *   Clear rectangle must be aligned to two times the number of
  252.        *   pixels in the table shown below due to 16x16 hashing across the
  253.        *   slice.
  254.        */
  255.       x_align *= 2;
  256.       y_align *= 2;
  257.    } else {
  258.       /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
  259.        * Target(s)", beneath the "MSAA Compression" bullet (p326):
  260.        *
  261.        *     Clear pass for this case requires that scaled down primitive
  262.        *     is sent down with upper left co-ordinate to coincide with
  263.        *     actual rectangle being cleared. For MSAA, clear rectangle’s
  264.        *     height and width need to as show in the following table in
  265.        *     terms of (width,height) of the RT.
  266.        *
  267.        *     MSAA  Width of Clear Rect  Height of Clear Rect
  268.        *      4X     Ceil(1/8*width)      Ceil(1/2*height)
  269.        *      8X     Ceil(1/2*width)      Ceil(1/2*height)
  270.        *
  271.        * The text "with upper left co-ordinate to coincide with actual
  272.        * rectangle being cleared" is a little confusing--it seems to imply
  273.        * that to clear a rectangle from (x,y) to (x+w,y+h), one needs to
  274.        * feed the pipeline using the rectangle (x,y) to
  275.        * (x+Ceil(w/N),y+Ceil(h/2)), where N is either 2 or 8 depending on
  276.        * the number of samples.  Experiments indicate that this is not
  277.        * quite correct; actually, what the hardware appears to do is to
  278.        * align whatever rectangle is sent down the pipeline to the nearest
  279.        * multiple of 2x2 blocks, and then scale it up by a factor of N
  280.        * horizontally and 2 vertically.  So the resulting alignment is 4
  281.        * vertically and either 4 or 16 horizontally, and the scaledown
  282.        * factor is 2 vertically and either 2 or 8 horizontally.
  283.        */
  284.       switch (irb->mt->num_samples) {
  285.       case 2:
  286.       case 4:
  287.          x_scaledown = 8;
  288.          break;
  289.       case 8:
  290.          x_scaledown = 2;
  291.          break;
  292.       default:
  293.          unreachable("Unexpected sample count for fast clear");
  294.       }
  295.       y_scaledown = 2;
  296.       x_align = x_scaledown * 2;
  297.       y_align = y_scaledown * 2;
  298.    }
  299.  
  300.    rect->x0 = fb->_Xmin;
  301.    rect->x1 = fb->_Xmax;
  302.    if (fb->Name != 0) {
  303.       rect->y0 = fb->_Ymin;
  304.       rect->y1 = fb->_Ymax;
  305.    } else {
  306.       rect->y0 = fb->Height - fb->_Ymax;
  307.       rect->y1 = fb->Height - fb->_Ymin;
  308.    }
  309.  
  310.    rect->x0 = ROUND_DOWN_TO(rect->x0,  x_align) / x_scaledown;
  311.    rect->y0 = ROUND_DOWN_TO(rect->y0, y_align) / y_scaledown;
  312.    rect->x1 = ALIGN(rect->x1, x_align) / x_scaledown;
  313.    rect->y1 = ALIGN(rect->y1, y_align) / y_scaledown;
  314. }
  315.  
  316. static void
  317. get_buffer_rect(struct brw_context *brw, struct gl_framebuffer *fb,
  318.                 struct intel_renderbuffer *irb, struct rect *rect)
  319. {
  320.    rect->x0 = fb->_Xmin;
  321.    rect->x1 = fb->_Xmax;
  322.    if (fb->Name != 0) {
  323.       rect->y0 = fb->_Ymin;
  324.       rect->y1 = fb->_Ymax;
  325.    } else {
  326.       rect->y0 = fb->Height - fb->_Ymax;
  327.       rect->y1 = fb->Height - fb->_Ymin;
  328.    }
  329. }
  330.  
  331. /**
  332.  * Determine if fast color clear supports the given clear color.
  333.  *
  334.  * Fast color clear can only clear to color values of 1.0 or 0.0.  At the
  335.  * moment we only support floating point, unorm, and snorm buffers.
  336.  */
  337. static bool
  338. is_color_fast_clear_compatible(struct brw_context *brw,
  339.                                mesa_format format,
  340.                                const union gl_color_union *color)
  341. {
  342.    if (_mesa_is_format_integer_color(format))
  343.       return false;
  344.  
  345.    for (int i = 0; i < 4; i++) {
  346.       if (color->f[i] != 0.0 && color->f[i] != 1.0 &&
  347.           _mesa_format_has_color_component(format, i)) {
  348.          return false;
  349.       }
  350.    }
  351.    return true;
  352. }
  353.  
  354. /**
  355.  * Convert the given color to a bitfield suitable for ORing into DWORD 7 of
  356.  * SURFACE_STATE.
  357.  */
  358. static uint32_t
  359. compute_fast_clear_color_bits(const union gl_color_union *color)
  360. {
  361.    uint32_t bits = 0;
  362.    for (int i = 0; i < 4; i++) {
  363.       /* Testing for non-0 works for integer and float colors */
  364.       if (color->f[i] != 0.0)
  365.          bits |= 1 << (GEN7_SURFACE_CLEAR_COLOR_SHIFT + (3 - i));
  366.    }
  367.    return bits;
  368. }
  369.  
  370. static const uint32_t fast_clear_color[4] = { ~0, ~0, ~0, ~0 };
  371.  
  372. static void
  373. set_fast_clear_op(struct brw_context *brw, uint32_t op)
  374. {
  375.    /* Set op and dirty BRW_NEW_FRAGMENT_PROGRAM to make sure we re-emit
  376.     * 3DSTATE_PS.
  377.     */
  378.    brw->wm.fast_clear_op = op;
  379.    brw->ctx.NewDriverState |= BRW_NEW_FRAGMENT_PROGRAM;
  380. }
  381.  
  382. static void
  383. use_rectlist(struct brw_context *brw, bool enable)
  384. {
  385.    /* Set custom state to let us use _3DPRIM_RECTLIST and the replicated
  386.     * rendertarget write.  When we enable reclist mode, we disable the
  387.     * viewport transform, disable clipping, enable the rep16 write
  388.     * optimization and disable simd8 dispatch in the PS.
  389.     */
  390.    brw->sf.viewport_transform_enable = !enable;
  391.    brw->use_rep_send = enable;
  392.    brw->no_simd8 = enable;
  393.  
  394.    /* Dirty state to make sure we reemit the state packages affected by the
  395.     * custom state.  We dirty BRW_NEW_FRAGMENT_PROGRAM to emit 3DSTATE_PS for
  396.     * disabling simd8 dispatch, _NEW_LIGHT to emit 3DSTATE_SF for disabling
  397.     * the viewport transform and 3DSTATE_CLIP to disable clipping for the
  398.     * reclist primitive.  This is a little messy - it would be nicer to
  399.     * BRW_NEW_FAST_CLEAR flag or so, but we're out of brw state bits.  Dirty
  400.     * _NEW_BUFFERS to make sure we emit new SURFACE_STATE with the new fast
  401.     * clear color value.
  402.     */
  403.    brw->NewGLState |= _NEW_LIGHT | _NEW_BUFFERS;
  404.    brw->ctx.NewDriverState |= BRW_NEW_FRAGMENT_PROGRAM;
  405. }
  406.  
  407. bool
  408. brw_meta_fast_clear(struct brw_context *brw, struct gl_framebuffer *fb,
  409.                     GLbitfield buffers, bool partial_clear)
  410. {
  411.    struct gl_context *ctx = &brw->ctx;
  412.    mesa_format format;
  413.    enum { FAST_CLEAR, REP_CLEAR, PLAIN_CLEAR } clear_type;
  414.    GLbitfield plain_clear_buffers, meta_save, rep_clear_buffers, fast_clear_buffers;
  415.    struct rect fast_clear_rect, clear_rect;
  416.    int layers;
  417.  
  418.    fast_clear_buffers = rep_clear_buffers = plain_clear_buffers = 0;
  419.  
  420.    /* First we loop through the color draw buffers and determine which ones
  421.     * can be fast cleared, which ones can use the replicated write and which
  422.     * ones have to fall back to regular color clear.
  423.     */
  424.    for (unsigned buf = 0; buf < fb->_NumColorDrawBuffers; buf++) {
  425.       struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buf];
  426.       struct intel_renderbuffer *irb = intel_renderbuffer(rb);
  427.       int index = fb->_ColorDrawBufferIndexes[buf];
  428.  
  429.       /* Only clear the buffers present in the provided mask */
  430.       if (((1 << index) & buffers) == 0)
  431.          continue;
  432.  
  433.       /* If this is an ES2 context or GL_ARB_ES2_compatibility is supported,
  434.        * the framebuffer can be complete with some attachments missing.  In
  435.        * this case the _ColorDrawBuffers pointer will be NULL.
  436.        */
  437.       if (rb == NULL)
  438.          continue;
  439.  
  440.       clear_type = FAST_CLEAR;
  441.  
  442.       /* We don't have fast clear until gen7. */
  443.       if (brw->gen < 7)
  444.          clear_type = REP_CLEAR;
  445.  
  446.       if (irb->mt->fast_clear_state == INTEL_FAST_CLEAR_STATE_NO_MCS)
  447.          clear_type = REP_CLEAR;
  448.  
  449.       /* We can't do scissored fast clears because of the restrictions on the
  450.        * fast clear rectangle size.
  451.        */
  452.       if (partial_clear)
  453.          clear_type = REP_CLEAR;
  454.  
  455.       /* Fast clear is only supported for colors where all components are
  456.        * either 0 or 1.
  457.        */
  458.       format = _mesa_get_render_format(ctx, irb->mt->format);
  459.       if (!is_color_fast_clear_compatible(brw, format, &ctx->Color.ClearColor))
  460.          clear_type = REP_CLEAR;
  461.  
  462.       /* From the SNB PRM (Vol4_Part1):
  463.        *
  464.        *     "Replicated data (Message Type = 111) is only supported when
  465.        *      accessing tiled memory.  Using this Message Type to access
  466.        *      linear (untiled) memory is UNDEFINED."
  467.        */
  468.       if (irb->mt->tiling == I915_TILING_NONE) {
  469.          perf_debug("falling back to plain clear because buffers are untiled\n");
  470.          clear_type = PLAIN_CLEAR;
  471.       }
  472.  
  473.       /* Constant color writes ignore everything in blend and color calculator
  474.        * state.  This is not documented.
  475.        */
  476.       GLubyte *color_mask = ctx->Color.ColorMask[buf];
  477.       for (int i = 0; i < 4; i++) {
  478.          if (_mesa_format_has_color_component(irb->mt->format, i) &&
  479.              !color_mask[i]) {
  480.             perf_debug("falling back to plain clear because of color mask\n");
  481.             clear_type = PLAIN_CLEAR;
  482.          }
  483.       }
  484.  
  485.       /* Allocate the MCS for non MSRT surfaces now if we're doing a fast
  486.        * clear and we don't have the MCS yet.  On failure, fall back to
  487.        * replicated clear.
  488.        */
  489.       if (clear_type == FAST_CLEAR && irb->mt->mcs_mt == NULL)
  490.          if (!intel_miptree_alloc_non_msrt_mcs(brw, irb->mt))
  491.             clear_type = REP_CLEAR;
  492.  
  493.       switch (clear_type) {
  494.       case FAST_CLEAR:
  495.          irb->mt->fast_clear_color_value =
  496.             compute_fast_clear_color_bits(&ctx->Color.ClearColor);
  497.          irb->need_downsample = true;
  498.  
  499.          /* If the buffer is already in INTEL_FAST_CLEAR_STATE_CLEAR, the
  500.           * clear is redundant and can be skipped.  Only skip after we've
  501.           * updated the fast clear color above though.
  502.           */
  503.          if (irb->mt->fast_clear_state == INTEL_FAST_CLEAR_STATE_CLEAR)
  504.             continue;
  505.  
  506.          /* Set fast_clear_state to RESOLVED so we don't try resolve them when
  507.           * we draw, in case the mt is also bound as a texture.
  508.           */
  509.          irb->mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_RESOLVED;
  510.          irb->need_downsample = true;
  511.          fast_clear_buffers |= 1 << index;
  512.          get_fast_clear_rect(brw, fb, irb, &fast_clear_rect);
  513.          break;
  514.  
  515.       case REP_CLEAR:
  516.          rep_clear_buffers |= 1 << index;
  517.          get_buffer_rect(brw, fb, irb, &clear_rect);
  518.          break;
  519.  
  520.       case PLAIN_CLEAR:
  521.          plain_clear_buffers |= 1 << index;
  522.          get_buffer_rect(brw, fb, irb, &clear_rect);
  523.          continue;
  524.       }
  525.    }
  526.  
  527.    if (!(fast_clear_buffers | rep_clear_buffers)) {
  528.       if (plain_clear_buffers)
  529.          /* If we only have plain clears, skip the meta save/restore. */
  530.          goto out;
  531.       else
  532.          /* Nothing left to do.  This happens when we hit the redundant fast
  533.           * clear case above and nothing else.
  534.           */
  535.          return true;
  536.    }
  537.  
  538.    meta_save =
  539.       MESA_META_ALPHA_TEST |
  540.       MESA_META_BLEND |
  541.       MESA_META_DEPTH_TEST |
  542.       MESA_META_RASTERIZATION |
  543.       MESA_META_SHADER |
  544.       MESA_META_STENCIL_TEST |
  545.       MESA_META_VERTEX |
  546.       MESA_META_VIEWPORT |
  547.       MESA_META_CLIP |
  548.       MESA_META_CLAMP_FRAGMENT_COLOR |
  549.       MESA_META_MULTISAMPLE |
  550.       MESA_META_OCCLUSION_QUERY |
  551.       MESA_META_DRAW_BUFFERS;
  552.  
  553.    _mesa_meta_begin(ctx, meta_save);
  554.  
  555.    if (!brw_fast_clear_init(brw)) {
  556.       /* This is going to be hard to recover from, most likely out of memory.
  557.        * Bail and let meta try and (probably) fail for us.
  558.        */
  559.       plain_clear_buffers = buffers;
  560.       goto bail_to_meta;
  561.    }
  562.  
  563.    /* Clears never have the color clamped. */
  564.    if (ctx->Extensions.ARB_color_buffer_float)
  565.       _mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
  566.  
  567.    _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_FALSE);
  568.    _mesa_DepthMask(GL_FALSE);
  569.    _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_FALSE);
  570.  
  571.    use_rectlist(brw, true);
  572.  
  573.    layers = MAX2(1, fb->MaxNumLayers);
  574.    if (fast_clear_buffers) {
  575.       _mesa_meta_drawbuffers_from_bitfield(fast_clear_buffers);
  576.       brw_bind_rep_write_shader(brw, (float *) fast_clear_color);
  577.       set_fast_clear_op(brw, GEN7_PS_RENDER_TARGET_FAST_CLEAR_ENABLE);
  578.       brw_draw_rectlist(ctx, &fast_clear_rect, layers);
  579.       set_fast_clear_op(brw, 0);
  580.    }
  581.  
  582.    if (rep_clear_buffers) {
  583.       _mesa_meta_drawbuffers_from_bitfield(rep_clear_buffers);
  584.       brw_bind_rep_write_shader(brw, ctx->Color.ClearColor.f);
  585.       brw_draw_rectlist(ctx, &clear_rect, layers);
  586.    }
  587.  
  588.    /* Now set the mts we cleared to INTEL_FAST_CLEAR_STATE_CLEAR so we'll
  589.     * resolve them eventually.
  590.     */
  591.    for (unsigned buf = 0; buf < fb->_NumColorDrawBuffers; buf++) {
  592.       struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buf];
  593.       struct intel_renderbuffer *irb = intel_renderbuffer(rb);
  594.       int index = fb->_ColorDrawBufferIndexes[buf];
  595.  
  596.       if ((1 << index) & fast_clear_buffers)
  597.          irb->mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_CLEAR;
  598.    }
  599.  
  600.  bail_to_meta:
  601.    /* Dirty _NEW_BUFFERS so we reemit SURFACE_STATE which sets the fast clear
  602.     * color before resolve and sets irb->mt->fast_clear_state to UNRESOLVED if
  603.     * we render to it.
  604.     */
  605.    brw->NewGLState |= _NEW_BUFFERS;
  606.  
  607.  
  608.    /* Set the custom state back to normal and dirty the same bits as above */
  609.    use_rectlist(brw, false);
  610.  
  611.    _mesa_meta_end(ctx);
  612.  
  613.    /* From BSpec: Render Target Fast Clear:
  614.     *
  615.     *     After Render target fast clear, pipe-control with color cache
  616.     *     write-flush must be issued before sending any DRAW commands on that
  617.     *     render target.
  618.     */
  619.    intel_batchbuffer_emit_mi_flush(brw);
  620.  
  621.    /* If we had to fall back to plain clear for any buffers, clear those now
  622.     * by calling into meta.
  623.     */
  624.  out:
  625.    if (plain_clear_buffers)
  626.       _mesa_meta_glsl_Clear(&brw->ctx, plain_clear_buffers);
  627.  
  628.    return true;
  629. }
  630.  
  631. static void
  632. get_resolve_rect(struct brw_context *brw,
  633.                  struct intel_mipmap_tree *mt, struct rect *rect)
  634. {
  635.    unsigned x_align, y_align;
  636.    unsigned x_scaledown, y_scaledown;
  637.  
  638.    /* From the Ivy Bridge PRM, Vol2 Part1 11.9 "Render Target Resolve":
  639.     *
  640.     *     A rectangle primitive must be scaled down by the following factors
  641.     *     with respect to render target being resolved.
  642.     *
  643.     * The scaledown factors in the table that follows are related to the
  644.     * alignment size returned by intel_get_non_msrt_mcs_alignment() by a
  645.     * multiplier.  For IVB and HSW, we divide by two, for BDW we multiply
  646.     * by 8 and 16 and 8 and 8 for SKL.
  647.     */
  648.  
  649.    intel_get_non_msrt_mcs_alignment(brw, mt, &x_align, &y_align);
  650.    if (brw->gen >= 9) {
  651.       x_scaledown = x_align * 8;
  652.       y_scaledown = y_align * 8;
  653.    } else if (brw->gen >= 8) {
  654.       x_scaledown = x_align * 8;
  655.       y_scaledown = y_align * 16;
  656.    } else {
  657.       x_scaledown = x_align / 2;
  658.       y_scaledown = y_align / 2;
  659.    }
  660.    rect->x0 = rect->y0 = 0;
  661.    rect->x1 = ALIGN(mt->logical_width0, x_scaledown) / x_scaledown;
  662.    rect->y1 = ALIGN(mt->logical_height0, y_scaledown) / y_scaledown;
  663. }
  664.  
  665. void
  666. brw_meta_resolve_color(struct brw_context *brw,
  667.                        struct intel_mipmap_tree *mt)
  668. {
  669.    struct gl_context *ctx = &brw->ctx;
  670.    GLuint fbo, rbo;
  671.    struct rect rect;
  672.  
  673.    intel_batchbuffer_emit_mi_flush(brw);
  674.  
  675.    _mesa_meta_begin(ctx, MESA_META_ALL);
  676.  
  677.    _mesa_GenFramebuffers(1, &fbo);
  678.    rbo = brw_get_rb_for_slice(brw, mt, 0, 0, false);
  679.  
  680.    _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
  681.    _mesa_FramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER,
  682.                                  GL_COLOR_ATTACHMENT0,
  683.                                  GL_RENDERBUFFER, rbo);
  684.    _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0);
  685.  
  686.    brw_fast_clear_init(brw);
  687.  
  688.    use_rectlist(brw, true);
  689.  
  690.    brw_bind_rep_write_shader(brw, (float *) fast_clear_color);
  691.  
  692.    set_fast_clear_op(brw, GEN7_PS_RENDER_TARGET_RESOLVE_ENABLE);
  693.  
  694.    mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_RESOLVED;
  695.    get_resolve_rect(brw, mt, &rect);
  696.  
  697.    brw_draw_rectlist(ctx, &rect, 1);
  698.  
  699.    set_fast_clear_op(brw, 0);
  700.    use_rectlist(brw, false);
  701.  
  702.    _mesa_DeleteRenderbuffers(1, &rbo);
  703.    _mesa_DeleteFramebuffers(1, &fbo);
  704.  
  705.    _mesa_meta_end(ctx);
  706.  
  707.    /* We're typically called from intel_update_state() and we're supposed to
  708.     * return with the state all updated to what it was before
  709.     * brw_meta_resolve_color() was called.  The meta rendering will have
  710.     * messed up the state and we need to call _mesa_update_state() again to
  711.     * get back to where we were supposed to be when resolve was called.
  712.     */
  713.    if (ctx->NewState)
  714.       _mesa_update_state(ctx);
  715. }
  716.