Subversion Repositories Kolibri OS

Rev

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

  1. /**************************************************************************
  2.  *
  3.  * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
  4.  * All Rights Reserved.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the
  8.  * "Software"), to deal in the Software without restriction, including
  9.  * without limitation the rights to use, copy, modify, merge, publish,
  10.  * distribute, sub license, and/or sell copies of the Software, and to
  11.  * permit persons to whom the Software is furnished to do so, subject to
  12.  * the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice (including the
  15.  * next paragraph) shall be included in all copies or substantial portions
  16.  * of the Software.
  17.  *
  18.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  21.  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
  22.  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23.  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24.  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  *
  26.  **************************************************************************/
  27.  
  28.  
  29. #include "main/enums.h"
  30. #include "main/imports.h"
  31. #include "main/macros.h"
  32. #include "main/mtypes.h"
  33. #include "main/fbobject.h"
  34. #include "main/framebuffer.h"
  35. #include "main/renderbuffer.h"
  36. #include "main/context.h"
  37. #include "main/teximage.h"
  38. #include "main/image.h"
  39.  
  40. #include "swrast/swrast.h"
  41. #include "drivers/common/meta.h"
  42.  
  43. #include "intel_batchbuffer.h"
  44. #include "intel_buffers.h"
  45. #include "intel_blit.h"
  46. #include "intel_fbo.h"
  47. #include "intel_mipmap_tree.h"
  48. #include "intel_regions.h"
  49. #include "intel_tex.h"
  50. #include "brw_context.h"
  51.  
  52. #define FILE_DEBUG_FLAG DEBUG_FBO
  53.  
  54. /**
  55.  * Create a new framebuffer object.
  56.  */
  57. static struct gl_framebuffer *
  58. intel_new_framebuffer(struct gl_context * ctx, GLuint name)
  59. {
  60.    /* Only drawable state in intel_framebuffer at this time, just use Mesa's
  61.     * class
  62.     */
  63.    return _mesa_new_framebuffer(ctx, name);
  64. }
  65.  
  66.  
  67. /** Called by gl_renderbuffer::Delete() */
  68. static void
  69. intel_delete_renderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
  70. {
  71.    struct intel_renderbuffer *irb = intel_renderbuffer(rb);
  72.  
  73.    ASSERT(irb);
  74.  
  75.    intel_miptree_release(&irb->mt);
  76.  
  77.    _mesa_delete_renderbuffer(ctx, rb);
  78. }
  79.  
  80. /**
  81.  * \see dd_function_table::MapRenderbuffer
  82.  */
  83. static void
  84. intel_map_renderbuffer(struct gl_context *ctx,
  85.                        struct gl_renderbuffer *rb,
  86.                        GLuint x, GLuint y, GLuint w, GLuint h,
  87.                        GLbitfield mode,
  88.                        GLubyte **out_map,
  89.                        GLint *out_stride)
  90. {
  91.    struct brw_context *brw = brw_context(ctx);
  92.    struct swrast_renderbuffer *srb = (struct swrast_renderbuffer *)rb;
  93.    struct intel_renderbuffer *irb = intel_renderbuffer(rb);
  94.    void *map;
  95.    int stride;
  96.  
  97.    if (srb->Buffer) {
  98.       /* this is a malloc'd renderbuffer (accum buffer), not an irb */
  99.       GLint bpp = _mesa_get_format_bytes(rb->Format);
  100.       GLint rowStride = srb->RowStride;
  101.       *out_map = (GLubyte *) srb->Buffer + y * rowStride + x * bpp;
  102.       *out_stride = rowStride;
  103.       return;
  104.    }
  105.  
  106.    intel_prepare_render(brw);
  107.  
  108.    /* For a window-system renderbuffer, we need to flip the mapping we receive
  109.     * upside-down.  So we need to ask for a rectangle on flipped vertically, and
  110.     * we then return a pointer to the bottom of it with a negative stride.
  111.     */
  112.    if (rb->Name == 0) {
  113.       y = rb->Height - y - h;
  114.    }
  115.  
  116.    intel_miptree_map(brw, irb->mt, irb->mt_level, irb->mt_layer,
  117.                      x, y, w, h, mode, &map, &stride);
  118.  
  119.    if (rb->Name == 0) {
  120.       map += (h - 1) * stride;
  121.       stride = -stride;
  122.    }
  123.  
  124.    DBG("%s: rb %d (%s) mt mapped: (%d, %d) (%dx%d) -> %p/%d\n",
  125.        __FUNCTION__, rb->Name, _mesa_get_format_name(rb->Format),
  126.        x, y, w, h, map, stride);
  127.  
  128.    *out_map = map;
  129.    *out_stride = stride;
  130. }
  131.  
  132. /**
  133.  * \see dd_function_table::UnmapRenderbuffer
  134.  */
  135. static void
  136. intel_unmap_renderbuffer(struct gl_context *ctx,
  137.                          struct gl_renderbuffer *rb)
  138. {
  139.    struct brw_context *brw = brw_context(ctx);
  140.    struct swrast_renderbuffer *srb = (struct swrast_renderbuffer *)rb;
  141.    struct intel_renderbuffer *irb = intel_renderbuffer(rb);
  142.  
  143.    DBG("%s: rb %d (%s)\n", __FUNCTION__,
  144.        rb->Name, _mesa_get_format_name(rb->Format));
  145.  
  146.    if (srb->Buffer) {
  147.       /* this is a malloc'd renderbuffer (accum buffer) */
  148.       /* nothing to do */
  149.       return;
  150.    }
  151.  
  152.    intel_miptree_unmap(brw, irb->mt, irb->mt_level, irb->mt_layer);
  153. }
  154.  
  155.  
  156. /**
  157.  * Round up the requested multisample count to the next supported sample size.
  158.  */
  159. unsigned
  160. intel_quantize_num_samples(struct intel_screen *intel, unsigned num_samples)
  161. {
  162.    switch (intel->gen) {
  163.    case 6:
  164.       /* Gen6 supports only 4x multisampling. */
  165.       if (num_samples > 0)
  166.          return 4;
  167.       else
  168.          return 0;
  169.    case 7:
  170.       /* Gen7 supports 4x and 8x multisampling. */
  171.       if (num_samples > 4)
  172.          return 8;
  173.       else if (num_samples > 0)
  174.          return 4;
  175.       else
  176.          return 0;
  177.       return 0;
  178.    default:
  179.       /* MSAA unsupported. */
  180.       return 0;
  181.    }
  182. }
  183.  
  184.  
  185. /**
  186.  * Called via glRenderbufferStorageEXT() to set the format and allocate
  187.  * storage for a user-created renderbuffer.
  188.  */
  189. static GLboolean
  190. intel_alloc_renderbuffer_storage(struct gl_context * ctx, struct gl_renderbuffer *rb,
  191.                                  GLenum internalFormat,
  192.                                  GLuint width, GLuint height)
  193. {
  194.    struct brw_context *brw = brw_context(ctx);
  195.    struct intel_screen *screen = brw->intelScreen;
  196.    struct intel_renderbuffer *irb = intel_renderbuffer(rb);
  197.    rb->NumSamples = intel_quantize_num_samples(screen, rb->NumSamples);
  198.  
  199.    switch (internalFormat) {
  200.    default:
  201.       /* Use the same format-choice logic as for textures.
  202.        * Renderbuffers aren't any different from textures for us,
  203.        * except they're less useful because you can't texture with
  204.        * them.
  205.        */
  206.       rb->Format = ctx->Driver.ChooseTextureFormat(ctx, GL_TEXTURE_2D,
  207.                                                    internalFormat,
  208.                                                    GL_NONE, GL_NONE);
  209.       break;
  210.    case GL_STENCIL_INDEX:
  211.    case GL_STENCIL_INDEX1_EXT:
  212.    case GL_STENCIL_INDEX4_EXT:
  213.    case GL_STENCIL_INDEX8_EXT:
  214.    case GL_STENCIL_INDEX16_EXT:
  215.       /* These aren't actual texture formats, so force them here. */
  216.       if (brw->has_separate_stencil) {
  217.          rb->Format = MESA_FORMAT_S8;
  218.       } else {
  219.          assert(!brw->must_use_separate_stencil);
  220.          rb->Format = MESA_FORMAT_S8_Z24;
  221.       }
  222.       break;
  223.    }
  224.  
  225.    rb->Width = width;
  226.    rb->Height = height;
  227.    rb->_BaseFormat = _mesa_base_fbo_format(ctx, internalFormat);
  228.  
  229.    intel_miptree_release(&irb->mt);
  230.  
  231.    DBG("%s: %s: %s (%dx%d)\n", __FUNCTION__,
  232.        _mesa_lookup_enum_by_nr(internalFormat),
  233.        _mesa_get_format_name(rb->Format), width, height);
  234.  
  235.    if (width == 0 || height == 0)
  236.       return true;
  237.  
  238.    irb->mt = intel_miptree_create_for_renderbuffer(brw, rb->Format,
  239.                                                    width, height,
  240.                                                    rb->NumSamples);
  241.    if (!irb->mt)
  242.       return false;
  243.  
  244.    return true;
  245. }
  246.  
  247.  
  248. static void
  249. intel_image_target_renderbuffer_storage(struct gl_context *ctx,
  250.                                         struct gl_renderbuffer *rb,
  251.                                         void *image_handle)
  252. {
  253.    struct brw_context *brw = brw_context(ctx);
  254.    struct intel_renderbuffer *irb;
  255.    __DRIscreen *screen;
  256.    __DRIimage *image;
  257.  
  258.    screen = brw->intelScreen->driScrnPriv;
  259.    image = screen->dri2.image->lookupEGLImage(screen, image_handle,
  260.                                               screen->loaderPrivate);
  261.    if (image == NULL)
  262.       return;
  263.  
  264.    /* __DRIimage is opaque to the core so it has to be checked here */
  265.    switch (image->format) {
  266.    case MESA_FORMAT_RGBA8888_REV:
  267.       _mesa_error(ctx, GL_INVALID_OPERATION,
  268.             "glEGLImageTargetRenderbufferStorage(unsupported image format");
  269.       return;
  270.       break;
  271.    default:
  272.       break;
  273.    }
  274.  
  275.    irb = intel_renderbuffer(rb);
  276.    intel_miptree_release(&irb->mt);
  277.    irb->mt = intel_miptree_create_for_bo(brw,
  278.                                          image->region->bo,
  279.                                          image->format,
  280.                                          image->offset,
  281.                                          image->region->width,
  282.                                          image->region->height,
  283.                                          image->region->pitch,
  284.                                          image->region->tiling);
  285.    if (!irb->mt)
  286.       return;
  287.  
  288.    rb->InternalFormat = image->internal_format;
  289.    rb->Width = image->region->width;
  290.    rb->Height = image->region->height;
  291.    rb->Format = image->format;
  292.    rb->_BaseFormat = _mesa_base_fbo_format(ctx, image->internal_format);
  293.    rb->NeedsFinishRenderTexture = true;
  294. }
  295.  
  296. /**
  297.  * Called by _mesa_resize_framebuffer() for each hardware renderbuffer when a
  298.  * window system framebuffer is resized.
  299.  *
  300.  * Any actual buffer reallocations for hardware renderbuffers (which would
  301.  * have triggered _mesa_resize_framebuffer()) were done by
  302.  * intel_process_dri2_buffer().
  303.  */
  304. static GLboolean
  305. intel_alloc_window_storage(struct gl_context * ctx, struct gl_renderbuffer *rb,
  306.                            GLenum internalFormat, GLuint width, GLuint height)
  307. {
  308.    ASSERT(rb->Name == 0);
  309.    rb->Width = width;
  310.    rb->Height = height;
  311.    rb->InternalFormat = internalFormat;
  312.  
  313.    return true;
  314. }
  315.  
  316. /** Dummy function for gl_renderbuffer::AllocStorage() */
  317. static GLboolean
  318. intel_nop_alloc_storage(struct gl_context * ctx, struct gl_renderbuffer *rb,
  319.                         GLenum internalFormat, GLuint width, GLuint height)
  320. {
  321.    _mesa_problem(ctx, "intel_op_alloc_storage should never be called.");
  322.    return false;
  323. }
  324.  
  325. /**
  326.  * Create a new intel_renderbuffer which corresponds to an on-screen window,
  327.  * not a user-created renderbuffer.
  328.  *
  329.  * \param num_samples must be quantized.
  330.  */
  331. struct intel_renderbuffer *
  332. intel_create_renderbuffer(gl_format format, unsigned num_samples)
  333. {
  334.    struct intel_renderbuffer *irb;
  335.    struct gl_renderbuffer *rb;
  336.  
  337.    ENTER();
  338.  
  339.    GET_CURRENT_CONTEXT(ctx);
  340.  
  341.    irb = CALLOC_STRUCT(intel_renderbuffer);
  342.    if (!irb) {
  343.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "creating renderbuffer");
  344.       return NULL;
  345.    }
  346.  
  347.    rb = &irb->Base.Base;
  348.  
  349.    _mesa_init_renderbuffer(rb, 0);
  350.    rb->ClassID = INTEL_RB_CLASS;
  351.    rb->_BaseFormat = _mesa_get_format_base_format(format);
  352.    rb->Format = format;
  353.    rb->InternalFormat = rb->_BaseFormat;
  354.    rb->NumSamples = num_samples;
  355.  
  356.    /* intel-specific methods */
  357.    rb->Delete = intel_delete_renderbuffer;
  358.    rb->AllocStorage = intel_alloc_window_storage;
  359.  
  360.     LEAVE();
  361.  
  362.    return irb;
  363. }
  364.  
  365. /**
  366.  * Private window-system buffers (as opposed to ones shared with the display
  367.  * server created with intel_create_renderbuffer()) are most similar in their
  368.  * handling to user-created renderbuffers, but they have a resize handler that
  369.  * may be called at intel_update_renderbuffers() time.
  370.  *
  371.  * \param num_samples must be quantized.
  372.  */
  373. struct intel_renderbuffer *
  374. intel_create_private_renderbuffer(gl_format format, unsigned num_samples)
  375. {
  376.    struct intel_renderbuffer *irb;
  377.  
  378.    irb = intel_create_renderbuffer(format, num_samples);
  379.    irb->Base.Base.AllocStorage = intel_alloc_renderbuffer_storage;
  380.  
  381.    return irb;
  382. }
  383.  
  384. /**
  385.  * Create a new renderbuffer object.
  386.  * Typically called via glBindRenderbufferEXT().
  387.  */
  388. static struct gl_renderbuffer *
  389. intel_new_renderbuffer(struct gl_context * ctx, GLuint name)
  390. {
  391.    struct intel_renderbuffer *irb;
  392.    struct gl_renderbuffer *rb;
  393.  
  394.    irb = CALLOC_STRUCT(intel_renderbuffer);
  395.    if (!irb) {
  396.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "creating renderbuffer");
  397.       return NULL;
  398.    }
  399.  
  400.    rb = &irb->Base.Base;
  401.  
  402.    _mesa_init_renderbuffer(rb, name);
  403.    rb->ClassID = INTEL_RB_CLASS;
  404.  
  405.    /* intel-specific methods */
  406.    rb->Delete = intel_delete_renderbuffer;
  407.    rb->AllocStorage = intel_alloc_renderbuffer_storage;
  408.    /* span routines set in alloc_storage function */
  409.  
  410.    return rb;
  411. }
  412.  
  413. static bool
  414. intel_renderbuffer_update_wrapper(struct brw_context *brw,
  415.                                   struct intel_renderbuffer *irb,
  416.                                   struct gl_texture_image *image,
  417.                                   uint32_t layer)
  418. {
  419.    struct gl_renderbuffer *rb = &irb->Base.Base;
  420.    struct intel_texture_image *intel_image = intel_texture_image(image);
  421.    struct intel_mipmap_tree *mt = intel_image->mt;
  422.    int level = image->Level;
  423.  
  424.    rb->Depth = image->Depth;
  425.  
  426.    rb->AllocStorage = intel_nop_alloc_storage;
  427.  
  428.    intel_miptree_check_level_layer(mt, level, layer);
  429.    irb->mt_level = level;
  430.  
  431.    switch (mt->msaa_layout) {
  432.       case INTEL_MSAA_LAYOUT_UMS:
  433.       case INTEL_MSAA_LAYOUT_CMS:
  434.          irb->mt_layer = layer * mt->num_samples;
  435.          break;
  436.  
  437.       default:
  438.          irb->mt_layer = layer;
  439.    }
  440.  
  441.    intel_miptree_reference(&irb->mt, mt);
  442.  
  443.    intel_renderbuffer_set_draw_offset(irb);
  444.  
  445.    if (mt->hiz_mt == NULL && brw_is_hiz_depth_format(brw, rb->Format)) {
  446.       intel_miptree_alloc_hiz(brw, mt);
  447.       if (!mt->hiz_mt)
  448.          return false;
  449.    }
  450.  
  451.    return true;
  452. }
  453.  
  454. void
  455. intel_renderbuffer_set_draw_offset(struct intel_renderbuffer *irb)
  456. {
  457.    unsigned int dst_x, dst_y;
  458.  
  459.    /* compute offset of the particular 2D image within the texture region */
  460.    intel_miptree_get_image_offset(irb->mt,
  461.                                   irb->mt_level,
  462.                                   irb->mt_layer,
  463.                                   &dst_x, &dst_y);
  464.  
  465.    irb->draw_x = dst_x;
  466.    irb->draw_y = dst_y;
  467. }
  468.  
  469. /**
  470.  * Called by glFramebufferTexture[123]DEXT() (and other places) to
  471.  * prepare for rendering into texture memory.  This might be called
  472.  * many times to choose different texture levels, cube faces, etc
  473.  * before intel_finish_render_texture() is ever called.
  474.  */
  475. static void
  476. intel_render_texture(struct gl_context * ctx,
  477.                      struct gl_framebuffer *fb,
  478.                      struct gl_renderbuffer_attachment *att)
  479. {
  480.    struct brw_context *brw = brw_context(ctx);
  481.    struct gl_renderbuffer *rb = att->Renderbuffer;
  482.    struct intel_renderbuffer *irb = intel_renderbuffer(rb);
  483.    struct gl_texture_image *image = rb->TexImage;
  484.    struct intel_texture_image *intel_image = intel_texture_image(image);
  485.    struct intel_mipmap_tree *mt = intel_image->mt;
  486.    int layer;
  487.  
  488.    (void) fb;
  489.  
  490.    if (att->CubeMapFace > 0) {
  491.       assert(att->Zoffset == 0);
  492.       layer = att->CubeMapFace;
  493.    } else {
  494.       layer = att->Zoffset;
  495.    }
  496.  
  497.    if (!intel_image->mt) {
  498.       /* Fallback on drawing to a texture that doesn't have a miptree
  499.        * (has a border, width/height 0, etc.)
  500.        */
  501.       _swrast_render_texture(ctx, fb, att);
  502.       return;
  503.    }
  504.  
  505.    intel_miptree_check_level_layer(mt, att->TextureLevel, layer);
  506.  
  507.    if (!intel_renderbuffer_update_wrapper(brw, irb, image, layer)) {
  508.        _swrast_render_texture(ctx, fb, att);
  509.        return;
  510.    }
  511.  
  512.    DBG("Begin render %s texture tex=%u w=%d h=%d d=%d refcount=%d\n",
  513.        _mesa_get_format_name(image->TexFormat),
  514.        att->Texture->Name, image->Width, image->Height, image->Depth,
  515.        rb->RefCount);
  516. }
  517.  
  518.  
  519. /**
  520.  * Called by Mesa when rendering to a texture is done.
  521.  */
  522. static void
  523. intel_finish_render_texture(struct gl_context * ctx, struct gl_renderbuffer *rb)
  524. {
  525.    struct brw_context *brw = brw_context(ctx);
  526.  
  527.    DBG("Finish render %s texture\n", _mesa_get_format_name(rb->Format));
  528.  
  529.    /* Since we've (probably) rendered to the texture and will (likely) use
  530.     * it in the texture domain later on in this batchbuffer, flush the
  531.     * batch.  Once again, we wish for a domain tracker in libdrm to cover
  532.     * usage inside of a batchbuffer like GEM does in the kernel.
  533.     */
  534.    intel_batchbuffer_emit_mi_flush(brw);
  535. }
  536.  
  537. #define fbo_incomplete(fb, ...) do {                                          \
  538.       static GLuint msg_id = 0;                                               \
  539.       if (unlikely(ctx->Const.ContextFlags & GL_CONTEXT_FLAG_DEBUG_BIT)) {    \
  540.          _mesa_gl_debug(ctx, &msg_id,                                         \
  541.                         MESA_DEBUG_TYPE_OTHER,                                \
  542.                         MESA_DEBUG_SEVERITY_MEDIUM,                           \
  543.                         __VA_ARGS__);                                         \
  544.       }                                                                       \
  545.       DBG(__VA_ARGS__);                                                       \
  546.       fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;                               \
  547.    } while (0)
  548.  
  549. /**
  550.  * Do additional "completeness" testing of a framebuffer object.
  551.  */
  552. static void
  553. intel_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
  554. {
  555.    struct brw_context *brw = brw_context(ctx);
  556.    struct intel_renderbuffer *depthRb =
  557.       intel_get_renderbuffer(fb, BUFFER_DEPTH);
  558.    struct intel_renderbuffer *stencilRb =
  559.       intel_get_renderbuffer(fb, BUFFER_STENCIL);
  560.    struct intel_mipmap_tree *depth_mt = NULL, *stencil_mt = NULL;
  561.    int i;
  562.  
  563.    DBG("%s() on fb %p (%s)\n", __FUNCTION__,
  564.        fb, (fb == ctx->DrawBuffer ? "drawbuffer" :
  565.             (fb == ctx->ReadBuffer ? "readbuffer" : "other buffer")));
  566.  
  567.    if (depthRb)
  568.       depth_mt = depthRb->mt;
  569.    if (stencilRb) {
  570.       stencil_mt = stencilRb->mt;
  571.       if (stencil_mt->stencil_mt)
  572.          stencil_mt = stencil_mt->stencil_mt;
  573.    }
  574.  
  575.    if (depth_mt && stencil_mt) {
  576.       if (depth_mt == stencil_mt) {
  577.          /* For true packed depth/stencil (not faked on prefers-separate-stencil
  578.           * hardware) we need to be sure they're the same level/layer, since
  579.           * we'll be emitting a single packet describing the packed setup.
  580.           */
  581.          if (depthRb->mt_level != stencilRb->mt_level ||
  582.              depthRb->mt_layer != stencilRb->mt_layer) {
  583.             fbo_incomplete(fb,
  584.                            "FBO incomplete: depth image level/layer %d/%d != "
  585.                            "stencil image %d/%d\n",
  586.                            depthRb->mt_level,
  587.                            depthRb->mt_layer,
  588.                            stencilRb->mt_level,
  589.                            stencilRb->mt_layer);
  590.          }
  591.       } else {
  592.          if (!brw->has_separate_stencil) {
  593.             fbo_incomplete(fb, "FBO incomplete: separate stencil "
  594.                            "unsupported\n");
  595.          }
  596.          if (stencil_mt->format != MESA_FORMAT_S8) {
  597.             fbo_incomplete(fb, "FBO incomplete: separate stencil is %s "
  598.                            "instead of S8\n",
  599.                            _mesa_get_format_name(stencil_mt->format));
  600.          }
  601.          if (brw->gen < 7 && !intel_renderbuffer_has_hiz(depthRb)) {
  602.             /* Before Gen7, separate depth and stencil buffers can be used
  603.              * only if HiZ is enabled. From the Sandybridge PRM, Volume 2,
  604.              * Part 1, Bit 3DSTATE_DEPTH_BUFFER.SeparateStencilBufferEnable:
  605.              *     [DevSNB]: This field must be set to the same value (enabled
  606.              *     or disabled) as Hierarchical Depth Buffer Enable.
  607.              */
  608.             fbo_incomplete(fb, "FBO incomplete: separate stencil "
  609.                            "without HiZ\n");
  610.          }
  611.       }
  612.    }
  613.  
  614.    for (i = 0; i < Elements(fb->Attachment); i++) {
  615.       struct gl_renderbuffer *rb;
  616.       struct intel_renderbuffer *irb;
  617.  
  618.       if (fb->Attachment[i].Type == GL_NONE)
  619.          continue;
  620.  
  621.       /* A supported attachment will have a Renderbuffer set either
  622.        * from being a Renderbuffer or being a texture that got the
  623.        * intel_wrap_texture() treatment.
  624.        */
  625.       rb = fb->Attachment[i].Renderbuffer;
  626.       if (rb == NULL) {
  627.          fbo_incomplete(fb, "FBO incomplete: attachment without "
  628.                         "renderbuffer\n");
  629.          continue;
  630.       }
  631.  
  632.       if (fb->Attachment[i].Type == GL_TEXTURE) {
  633.          if (rb->TexImage->Border) {
  634.             fbo_incomplete(fb, "FBO incomplete: texture with border\n");
  635.             continue;
  636.          }
  637.       }
  638.  
  639.       irb = intel_renderbuffer(rb);
  640.       if (irb == NULL) {
  641.          fbo_incomplete(fb, "FBO incomplete: software rendering "
  642.                         "renderbuffer\n");
  643.          continue;
  644.       }
  645.  
  646.       if (!brw_render_target_supported(brw, rb)) {
  647.          fbo_incomplete(fb, "FBO incomplete: Unsupported HW "
  648.                         "texture/renderbuffer format attached: %s\n",
  649.                         _mesa_get_format_name(intel_rb_format(irb)));
  650.       }
  651.    }
  652. }
  653.  
  654. /**
  655.  * Try to do a glBlitFramebuffer using glCopyTexSubImage2D
  656.  * We can do this when the dst renderbuffer is actually a texture and
  657.  * there is no scaling, mirroring or scissoring.
  658.  *
  659.  * \return new buffer mask indicating the buffers left to blit using the
  660.  *         normal path.
  661.  */
  662. static GLbitfield
  663. intel_blit_framebuffer_with_blitter(struct gl_context *ctx,
  664.                                     GLint srcX0, GLint srcY0,
  665.                                     GLint srcX1, GLint srcY1,
  666.                                     GLint dstX0, GLint dstY0,
  667.                                     GLint dstX1, GLint dstY1,
  668.                                     GLbitfield mask, GLenum filter)
  669. {
  670.    struct brw_context *brw = brw_context(ctx);
  671.  
  672.    /* Sync up the state of window system buffers.  We need to do this before
  673.     * we go looking for the buffers.
  674.     */
  675.    intel_prepare_render(brw);
  676.  
  677.    if (mask & GL_COLOR_BUFFER_BIT) {
  678.       GLint i;
  679.       const struct gl_framebuffer *drawFb = ctx->DrawBuffer;
  680.       const struct gl_framebuffer *readFb = ctx->ReadBuffer;
  681.       struct gl_renderbuffer *src_rb = readFb->_ColorReadBuffer;
  682.       struct intel_renderbuffer *src_irb = intel_renderbuffer(src_rb);
  683.  
  684.       if (!src_irb) {
  685.          perf_debug("glBlitFramebuffer(): missing src renderbuffer.  "
  686.                     "Falling back to software rendering.\n");
  687.          return mask;
  688.       }
  689.  
  690.       /* If the source and destination are the same size with no mirroring,
  691.        * the rectangles are within the size of the texture and there is no
  692.        * scissor, then we can probably use the blit engine.
  693.        */
  694.       if (!(srcX0 - srcX1 == dstX0 - dstX1 &&
  695.             srcY0 - srcY1 == dstY0 - dstY1 &&
  696.             srcX1 >= srcX0 &&
  697.             srcY1 >= srcY0 &&
  698.             srcX0 >= 0 && srcX1 <= readFb->Width &&
  699.             srcY0 >= 0 && srcY1 <= readFb->Height &&
  700.             dstX0 >= 0 && dstX1 <= drawFb->Width &&
  701.             dstY0 >= 0 && dstY1 <= drawFb->Height &&
  702.             !ctx->Scissor.Enabled)) {
  703.          perf_debug("glBlitFramebuffer(): non-1:1 blit.  "
  704.                     "Falling back to software rendering.\n");
  705.          return mask;
  706.       }
  707.  
  708.       /* Blit to all active draw buffers.  We don't do any pre-checking,
  709.        * because we assume that copying to MRTs is rare, and failure midway
  710.        * through copying is even more rare.  Even if it was to occur, it's
  711.        * safe to let meta start the copy over from scratch, because
  712.        * glBlitFramebuffer completely overwrites the destination pixels, and
  713.        * results are undefined if any destination pixels have a dependency on
  714.        * source pixels.
  715.        */
  716.       for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
  717.          struct gl_renderbuffer *dst_rb = ctx->DrawBuffer->_ColorDrawBuffers[i];
  718.          struct intel_renderbuffer *dst_irb = intel_renderbuffer(dst_rb);
  719.  
  720.          if (!dst_irb) {
  721.             perf_debug("glBlitFramebuffer(): missing dst renderbuffer.  "
  722.                        "Falling back to software rendering.\n");
  723.             return mask;
  724.          }
  725.  
  726.          gl_format src_format = _mesa_get_srgb_format_linear(src_rb->Format);
  727.          gl_format dst_format = _mesa_get_srgb_format_linear(dst_rb->Format);
  728.          if (src_format != dst_format) {
  729.             perf_debug("glBlitFramebuffer(): unsupported blit from %s to %s.  "
  730.                        "Falling back to software rendering.\n",
  731.                        _mesa_get_format_name(src_format),
  732.                        _mesa_get_format_name(dst_format));
  733.             return mask;
  734.          }
  735.  
  736.          if (!intel_miptree_blit(brw,
  737.                                  src_irb->mt,
  738.                                  src_irb->mt_level, src_irb->mt_layer,
  739.                                  srcX0, srcY0, src_rb->Name == 0,
  740.                                  dst_irb->mt,
  741.                                  dst_irb->mt_level, dst_irb->mt_layer,
  742.                                  dstX0, dstY0, dst_rb->Name == 0,
  743.                                  dstX1 - dstX0, dstY1 - dstY0, GL_COPY)) {
  744.             perf_debug("glBlitFramebuffer(): unknown blit failure.  "
  745.                        "Falling back to software rendering.\n");
  746.             return mask;
  747.          }
  748.       }
  749.  
  750.       mask &= ~GL_COLOR_BUFFER_BIT;
  751.    }
  752.  
  753.    return mask;
  754. }
  755.  
  756. static void
  757. intel_blit_framebuffer(struct gl_context *ctx,
  758.                        GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
  759.                        GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
  760.                        GLbitfield mask, GLenum filter)
  761. {
  762.    mask = brw_blorp_framebuffer(brw_context(ctx),
  763.                                 srcX0, srcY0, srcX1, srcY1,
  764.                                 dstX0, dstY0, dstX1, dstY1,
  765.                                 mask, filter);
  766.    if (mask == 0x0)
  767.       return;
  768.  
  769.    /* Try using the BLT engine. */
  770.    mask = intel_blit_framebuffer_with_blitter(ctx,
  771.                                               srcX0, srcY0, srcX1, srcY1,
  772.                                               dstX0, dstY0, dstX1, dstY1,
  773.                                               mask, filter);
  774.    if (mask == 0x0)
  775.       return;
  776.  
  777.  
  778.    _mesa_meta_BlitFramebuffer(ctx,
  779.                               srcX0, srcY0, srcX1, srcY1,
  780.                               dstX0, dstY0, dstX1, dstY1,
  781.                               mask, filter);
  782. }
  783.  
  784. /**
  785.  * This is a no-op except on multisample buffers shared with DRI2.
  786.  */
  787. void
  788. intel_renderbuffer_set_needs_downsample(struct intel_renderbuffer *irb)
  789. {
  790.    if (irb->mt && irb->mt->singlesample_mt)
  791.       irb->mt->need_downsample = true;
  792. }
  793.  
  794. /**
  795.  * Does the renderbuffer have hiz enabled?
  796.  */
  797. bool
  798. intel_renderbuffer_has_hiz(struct intel_renderbuffer *irb)
  799. {
  800.    return intel_miptree_slice_has_hiz(irb->mt, irb->mt_level, irb->mt_layer);
  801. }
  802.  
  803. void
  804. intel_renderbuffer_set_needs_hiz_resolve(struct intel_renderbuffer *irb)
  805. {
  806.    if (irb->mt) {
  807.       intel_miptree_slice_set_needs_hiz_resolve(irb->mt,
  808.                                                 irb->mt_level,
  809.                                                 irb->mt_layer);
  810.    }
  811. }
  812.  
  813. void
  814. intel_renderbuffer_set_needs_depth_resolve(struct intel_renderbuffer *irb)
  815. {
  816.    if (irb->mt) {
  817.       intel_miptree_slice_set_needs_depth_resolve(irb->mt,
  818.                                                   irb->mt_level,
  819.                                                   irb->mt_layer);
  820.    }
  821. }
  822.  
  823. bool
  824. intel_renderbuffer_resolve_hiz(struct brw_context *brw,
  825.                                struct intel_renderbuffer *irb)
  826. {
  827.    if (irb->mt)
  828.       return intel_miptree_slice_resolve_hiz(brw,
  829.                                              irb->mt,
  830.                                              irb->mt_level,
  831.                                              irb->mt_layer);
  832.  
  833.    return false;
  834. }
  835.  
  836. bool
  837. intel_renderbuffer_resolve_depth(struct brw_context *brw,
  838.                                  struct intel_renderbuffer *irb)
  839. {
  840.    if (irb->mt)
  841.       return intel_miptree_slice_resolve_depth(brw,
  842.                                                irb->mt,
  843.                                                irb->mt_level,
  844.                                                irb->mt_layer);
  845.  
  846.    return false;
  847. }
  848.  
  849. void
  850. intel_renderbuffer_move_to_temp(struct brw_context *brw,
  851.                                 struct intel_renderbuffer *irb,
  852.                                 bool invalidate)
  853. {
  854.    struct gl_renderbuffer *rb =&irb->Base.Base;
  855.    struct intel_texture_image *intel_image = intel_texture_image(rb->TexImage);
  856.    struct intel_mipmap_tree *new_mt;
  857.    int width, height, depth;
  858.  
  859.    intel_miptree_get_dimensions_for_image(rb->TexImage, &width, &height, &depth);
  860.  
  861.    new_mt = intel_miptree_create(brw, rb->TexImage->TexObject->Target,
  862.                                  intel_image->base.Base.TexFormat,
  863.                                  intel_image->base.Base.Level,
  864.                                  intel_image->base.Base.Level,
  865.                                  width, height, depth,
  866.                                  true,
  867.                                  irb->mt->num_samples,
  868.                                  INTEL_MIPTREE_TILING_ANY);
  869.  
  870.    if (brw_is_hiz_depth_format(brw, new_mt->format)) {
  871.       intel_miptree_alloc_hiz(brw, new_mt);
  872.    }
  873.  
  874.    intel_miptree_copy_teximage(brw, intel_image, new_mt, invalidate);
  875.  
  876.    intel_miptree_reference(&irb->mt, intel_image->mt);
  877.    intel_renderbuffer_set_draw_offset(irb);
  878.    intel_miptree_release(&new_mt);
  879. }
  880.  
  881. /**
  882.  * Do one-time context initializations related to GL_EXT_framebuffer_object.
  883.  * Hook in device driver functions.
  884.  */
  885. void
  886. intel_fbo_init(struct brw_context *brw)
  887. {
  888.    struct dd_function_table *dd = &brw->ctx.Driver;
  889.    dd->NewFramebuffer = intel_new_framebuffer;
  890.    dd->NewRenderbuffer = intel_new_renderbuffer;
  891.    dd->MapRenderbuffer = intel_map_renderbuffer;
  892.    dd->UnmapRenderbuffer = intel_unmap_renderbuffer;
  893.    dd->RenderTexture = intel_render_texture;
  894.    dd->FinishRenderTexture = intel_finish_render_texture;
  895.    dd->ValidateFramebuffer = intel_validate_framebuffer;
  896.    dd->BlitFramebuffer = intel_blit_framebuffer;
  897.    dd->EGLImageTargetRenderbufferStorage =
  898.       intel_image_target_renderbuffer_storage;
  899. }
  900.