Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1.  
  2. #include "main/glheader.h"
  3. #include "main/macros.h"
  4. #include "main/mtypes.h"
  5. #include "main/enums.h"
  6. #include "main/bufferobj.h"
  7. #include "main/context.h"
  8. #include "main/formats.h"
  9. #include "main/image.h"
  10. #include "main/pbo.h"
  11. #include "main/renderbuffer.h"
  12. #include "main/texcompress.h"
  13. #include "main/texgetimage.h"
  14. #include "main/texobj.h"
  15. #include "main/teximage.h"
  16. #include "main/texstore.h"
  17.  
  18. #include "drivers/common/meta.h"
  19.  
  20. #include "intel_mipmap_tree.h"
  21. #include "intel_buffer_objects.h"
  22. #include "intel_batchbuffer.h"
  23. #include "intel_tex.h"
  24. #include "intel_blit.h"
  25. #include "intel_fbo.h"
  26. #include "intel_image.h"
  27. #include "intel_tiled_memcpy.h"
  28. #include "brw_context.h"
  29.  
  30. #define FILE_DEBUG_FLAG DEBUG_TEXTURE
  31.  
  32. /* Work back from the specified level of the image to the baselevel and create a
  33.  * miptree of that size.
  34.  */
  35. struct intel_mipmap_tree *
  36. intel_miptree_create_for_teximage(struct brw_context *brw,
  37.                                   struct intel_texture_object *intelObj,
  38.                                   struct intel_texture_image *intelImage,
  39.                                   bool expect_accelerated_upload)
  40. {
  41.    GLuint lastLevel;
  42.    int width, height, depth;
  43.    GLuint i;
  44.  
  45.    intel_miptree_get_dimensions_for_image(&intelImage->base.Base,
  46.                                           &width, &height, &depth);
  47.  
  48.    DBG("%s\n", __func__);
  49.  
  50.    /* Figure out image dimensions at start level. */
  51.    for (i = intelImage->base.Base.Level; i > 0; i--) {
  52.       width <<= 1;
  53.       if (height != 1)
  54.          height <<= 1;
  55.       if (depth != 1)
  56.          depth <<= 1;
  57.    }
  58.  
  59.    /* Guess a reasonable value for lastLevel.  This is probably going
  60.     * to be wrong fairly often and might mean that we have to look at
  61.     * resizable buffers, or require that buffers implement lazy
  62.     * pagetable arrangements.
  63.     */
  64.    if ((intelObj->base.Sampler.MinFilter == GL_NEAREST ||
  65.         intelObj->base.Sampler.MinFilter == GL_LINEAR) &&
  66.        intelImage->base.Base.Level == 0 &&
  67.        !intelObj->base.GenerateMipmap) {
  68.       lastLevel = 0;
  69.    } else {
  70.       lastLevel = _mesa_get_tex_max_num_levels(intelObj->base.Target,
  71.                                                width, height, depth) - 1;
  72.    }
  73.  
  74.    return intel_miptree_create(brw,
  75.                                intelObj->base.Target,
  76.                                intelImage->base.Base.TexFormat,
  77.                                0,
  78.                                lastLevel,
  79.                                width,
  80.                                height,
  81.                                depth,
  82.                                expect_accelerated_upload,
  83.                                intelImage->base.Base.NumSamples,
  84.                                INTEL_MIPTREE_TILING_ANY,
  85.                                false);
  86. }
  87.  
  88. static void
  89. intelTexImage(struct gl_context * ctx,
  90.               GLuint dims,
  91.               struct gl_texture_image *texImage,
  92.               GLenum format, GLenum type, const void *pixels,
  93.               const struct gl_pixelstore_attrib *unpack)
  94. {
  95.    struct intel_texture_image *intelImage = intel_texture_image(texImage);
  96.    bool ok;
  97.  
  98.    bool tex_busy = intelImage->mt && drm_intel_bo_busy(intelImage->mt->bo);
  99.  
  100.    DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
  101.        __func__, _mesa_get_format_name(texImage->TexFormat),
  102.        _mesa_lookup_enum_by_nr(texImage->TexObject->Target),
  103.        _mesa_lookup_enum_by_nr(format), _mesa_lookup_enum_by_nr(type),
  104.        texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
  105.  
  106.    /* Allocate storage for texture data. */
  107.    if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
  108.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
  109.       return;
  110.    }
  111.  
  112.    assert(intelImage->mt);
  113.  
  114.    ok = _mesa_meta_pbo_TexSubImage(ctx, dims, texImage, 0, 0, 0,
  115.                                    texImage->Width, texImage->Height,
  116.                                    texImage->Depth,
  117.                                    format, type, pixels,
  118.                                    false /*allocate_storage*/,
  119.                                    tex_busy, unpack);
  120.    if (ok)
  121.       return;
  122.  
  123.    ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
  124.                                        0, 0, 0, /*x,y,z offsets*/
  125.                                        texImage->Width,
  126.                                        texImage->Height,
  127.                                        texImage->Depth,
  128.                                        format, type, pixels, unpack,
  129.                                        false /*allocate_storage*/);
  130.    if (ok)
  131.       return;
  132.  
  133.    DBG("%s: upload image %dx%dx%d pixels %p\n",
  134.        __func__, texImage->Width, texImage->Height, texImage->Depth,
  135.        pixels);
  136.  
  137.    _mesa_store_teximage(ctx, dims, texImage,
  138.                         format, type, pixels, unpack);
  139. }
  140.  
  141.  
  142. /**
  143.  * Binds a BO to a texture image, as if it was uploaded by glTexImage2D().
  144.  *
  145.  * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
  146.  */
  147. static void
  148. intel_set_texture_image_bo(struct gl_context *ctx,
  149.                            struct gl_texture_image *image,
  150.                            drm_intel_bo *bo,
  151.                            GLenum target,
  152.                            GLenum internalFormat,
  153.                            mesa_format format,
  154.                            uint32_t offset,
  155.                            GLuint width, GLuint height,
  156.                            GLuint pitch,
  157.                            GLuint tile_x, GLuint tile_y,
  158.                            bool disable_aux_buffers)
  159. {
  160.    struct brw_context *brw = brw_context(ctx);
  161.    struct intel_texture_image *intel_image = intel_texture_image(image);
  162.    struct gl_texture_object *texobj = image->TexObject;
  163.    struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
  164.    uint32_t draw_x, draw_y;
  165.  
  166.    _mesa_init_teximage_fields(&brw->ctx, image,
  167.                               width, height, 1,
  168.                               0, internalFormat, format);
  169.  
  170.    ctx->Driver.FreeTextureImageBuffer(ctx, image);
  171.  
  172.    intel_image->mt = intel_miptree_create_for_bo(brw, bo, image->TexFormat,
  173.                                                  0, width, height, 1, pitch,
  174.                                                  disable_aux_buffers);
  175.    if (intel_image->mt == NULL)
  176.        return;
  177.    intel_image->mt->target = target;
  178.    intel_image->mt->total_width = width;
  179.    intel_image->mt->total_height = height;
  180.    intel_image->mt->level[0].slice[0].x_offset = tile_x;
  181.    intel_image->mt->level[0].slice[0].y_offset = tile_y;
  182.  
  183.    intel_miptree_get_tile_offsets(intel_image->mt, 0, 0, &draw_x, &draw_y);
  184.  
  185.    /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
  186.     * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
  187.     * trouble resolving back to destination image due to alignment issues.
  188.     */
  189.    if (!brw->has_surface_tile_offset &&
  190.        (draw_x != 0 || draw_y != 0)) {
  191.       _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
  192.       intel_miptree_release(&intel_image->mt);
  193.       return;
  194.    }
  195.  
  196.    intel_texobj->needs_validate = true;
  197.  
  198.    intel_image->mt->offset = offset;
  199.    assert(pitch % intel_image->mt->cpp == 0);
  200.    intel_image->base.RowStride = pitch / intel_image->mt->cpp;
  201.  
  202.    /* Immediately validate the image to the object. */
  203.    intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
  204. }
  205.  
  206. void
  207. intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
  208.                    GLint texture_format,
  209.                    __DRIdrawable *dPriv)
  210. {
  211.    struct gl_framebuffer *fb = dPriv->driverPrivate;
  212.    struct brw_context *brw = pDRICtx->driverPrivate;
  213.    struct gl_context *ctx = &brw->ctx;
  214.    struct intel_renderbuffer *rb;
  215.    struct gl_texture_object *texObj;
  216.    struct gl_texture_image *texImage;
  217.    int level = 0, internalFormat = 0;
  218.    mesa_format texFormat = MESA_FORMAT_NONE;
  219.  
  220.    texObj = _mesa_get_current_tex_object(ctx, target);
  221.  
  222.    if (!texObj)
  223.       return;
  224.  
  225.    if (dPriv->lastStamp != dPriv->dri2.stamp ||
  226.        !pDRICtx->driScreenPriv->dri2.useInvalidate)
  227.       intel_update_renderbuffers(pDRICtx, dPriv);
  228.  
  229.    rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
  230.    /* If the miptree isn't set, then intel_update_renderbuffers was unable
  231.     * to get the BO for the drawable from the window system.
  232.     */
  233.    if (!rb || !rb->mt)
  234.       return;
  235.  
  236.    if (rb->mt->cpp == 4) {
  237.       if (texture_format == __DRI_TEXTURE_FORMAT_RGB) {
  238.          internalFormat = GL_RGB;
  239.          texFormat = MESA_FORMAT_B8G8R8X8_UNORM;
  240.       }
  241.       else {
  242.          internalFormat = GL_RGBA;
  243.          texFormat = MESA_FORMAT_B8G8R8A8_UNORM;
  244.       }
  245.    } else if (rb->mt->cpp == 2) {
  246.       internalFormat = GL_RGB;
  247.       texFormat = MESA_FORMAT_B5G6R5_UNORM;
  248.    }
  249.  
  250.    _mesa_lock_texture(&brw->ctx, texObj);
  251.    texImage = _mesa_get_tex_image(ctx, texObj, target, level);
  252.    intel_miptree_make_shareable(brw, rb->mt);
  253.    intel_set_texture_image_bo(ctx, texImage, rb->mt->bo, target,
  254.                               internalFormat, texFormat, 0,
  255.                               rb->Base.Base.Width,
  256.                               rb->Base.Base.Height,
  257.                               rb->mt->pitch,
  258.                               0, 0,
  259.                               false /*disable_aux_buffers*/);
  260.    _mesa_unlock_texture(&brw->ctx, texObj);
  261. }
  262.  
  263. static GLboolean
  264. intel_bind_renderbuffer_tex_image(struct gl_context *ctx,
  265.                                   struct gl_renderbuffer *rb,
  266.                                   struct gl_texture_image *image)
  267. {
  268.    struct intel_renderbuffer *irb = intel_renderbuffer(rb);
  269.    struct intel_texture_image *intel_image = intel_texture_image(image);
  270.    struct gl_texture_object *texobj = image->TexObject;
  271.    struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
  272.  
  273.    /* We can only handle RB allocated with AllocRenderbufferStorage, or
  274.     * window-system renderbuffers.
  275.     */
  276.    assert(!rb->TexImage);
  277.  
  278.    if (!irb->mt)
  279.       return false;
  280.  
  281.    _mesa_lock_texture(ctx, texobj);
  282.    _mesa_init_teximage_fields(ctx, image,
  283.                               rb->Width, rb->Height, 1,
  284.                               0, rb->InternalFormat, rb->Format);
  285.    image->NumSamples = rb->NumSamples;
  286.  
  287.    intel_miptree_reference(&intel_image->mt, irb->mt);
  288.  
  289.    /* Immediately validate the image to the object. */
  290.    intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
  291.  
  292.    intel_texobj->needs_validate = true;
  293.    _mesa_unlock_texture(ctx, texobj);
  294.  
  295.    return true;
  296. }
  297.  
  298. void
  299. intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
  300. {
  301.    /* The old interface didn't have the format argument, so copy our
  302.     * implementation's behavior at the time.
  303.     */
  304.    intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
  305. }
  306.  
  307. static void
  308. intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
  309.                               struct gl_texture_object *texObj,
  310.                               struct gl_texture_image *texImage,
  311.                               GLeglImageOES image_handle)
  312. {
  313.    struct brw_context *brw = brw_context(ctx);
  314.    __DRIscreen *screen;
  315.    __DRIimage *image;
  316.  
  317.    screen = brw->intelScreen->driScrnPriv;
  318.    image = screen->dri2.image->lookupEGLImage(screen, image_handle,
  319.                                               screen->loaderPrivate);
  320.    if (image == NULL)
  321.       return;
  322.  
  323.    /* We support external textures only for EGLImages created with
  324.     * EGL_EXT_image_dma_buf_import. We may lift that restriction in the future.
  325.     */
  326.    if (target == GL_TEXTURE_EXTERNAL_OES && !image->dma_buf_imported) {
  327.       _mesa_error(ctx, GL_INVALID_OPERATION,
  328.             "glEGLImageTargetTexture2DOES(external target is enabled only "
  329.                "for images created with EGL_EXT_image_dma_buf_import");
  330.       return;
  331.    }
  332.  
  333.    /* Disallow depth/stencil textures: we don't have a way to pass the
  334.     * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
  335.     */
  336.    if (image->has_depthstencil) {
  337.       _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
  338.       return;
  339.    }
  340.  
  341.    /* Disable creation of the texture's aux buffers because the driver exposes
  342.     * no EGL API to manage them. That is, there is no API for resolving the aux
  343.     * buffer's content to the main buffer nor for invalidating the aux buffer's
  344.     * content.
  345.     */
  346.    intel_set_texture_image_bo(ctx, texImage, image->bo,
  347.                               target, image->internal_format,
  348.                               image->format, image->offset,
  349.                               image->width,  image->height,
  350.                               image->pitch,
  351.                               image->tile_x, image->tile_y,
  352.                               true /*disable_aux_buffers*/);
  353. }
  354.  
  355. /**
  356.  * \brief A fast path for glGetTexImage.
  357.  *
  358.  * \see intel_readpixels_tiled_memcpy()
  359.  */
  360. bool
  361. intel_gettexsubimage_tiled_memcpy(struct gl_context *ctx,
  362.                                   struct gl_texture_image *texImage,
  363.                                   GLint xoffset, GLint yoffset,
  364.                                   GLsizei width, GLsizei height,
  365.                                   GLenum format, GLenum type,
  366.                                   GLvoid *pixels,
  367.                                   const struct gl_pixelstore_attrib *packing)
  368. {
  369.    struct brw_context *brw = brw_context(ctx);
  370.    struct intel_texture_image *image = intel_texture_image(texImage);
  371.    int dst_pitch;
  372.  
  373.    /* The miptree's buffer. */
  374.    drm_intel_bo *bo;
  375.  
  376.    int error = 0;
  377.  
  378.    uint32_t cpp;
  379.    mem_copy_fn mem_copy = NULL;
  380.  
  381.    /* This fastpath is restricted to specific texture types:
  382.     * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
  383.     * more types.
  384.     *
  385.     * FINISHME: The restrictions below on packing alignment and packing row
  386.     * length are likely unneeded now because we calculate the destination stride
  387.     * with _mesa_image_row_stride. However, before removing the restrictions
  388.     * we need tests.
  389.     */
  390.    if (!brw->has_llc ||
  391.        !(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) ||
  392.        !(texImage->TexObject->Target == GL_TEXTURE_2D ||
  393.          texImage->TexObject->Target == GL_TEXTURE_RECTANGLE) ||
  394.        pixels == NULL ||
  395.        _mesa_is_bufferobj(packing->BufferObj) ||
  396.        packing->Alignment > 4 ||
  397.        packing->SkipPixels > 0 ||
  398.        packing->SkipRows > 0 ||
  399.        (packing->RowLength != 0 && packing->RowLength != width) ||
  400.        packing->SwapBytes ||
  401.        packing->LsbFirst ||
  402.        packing->Invert)
  403.       return false;
  404.  
  405.    /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
  406.     * function doesn't set the last channel to 1.
  407.     */
  408.    if (texImage->TexFormat == MESA_FORMAT_B8G8R8X8_UNORM ||
  409.        texImage->TexFormat == MESA_FORMAT_R8G8B8X8_UNORM)
  410.       return false;
  411.  
  412.    if (!intel_get_memcpy(texImage->TexFormat, format, type, &mem_copy, &cpp,
  413.                          INTEL_DOWNLOAD))
  414.       return false;
  415.  
  416.    /* If this is a nontrivial texture view, let another path handle it instead. */
  417.    if (texImage->TexObject->MinLayer)
  418.       return false;
  419.  
  420.    if (!image->mt ||
  421.        (image->mt->tiling != I915_TILING_X &&
  422.        image->mt->tiling != I915_TILING_Y)) {
  423.       /* The algorithm is written only for X- or Y-tiled memory. */
  424.       return false;
  425.    }
  426.  
  427.    /* Since we are going to write raw data to the miptree, we need to resolve
  428.     * any pending fast color clears before we start.
  429.     */
  430.    intel_miptree_resolve_color(brw, image->mt);
  431.  
  432.    bo = image->mt->bo;
  433.  
  434.    if (drm_intel_bo_references(brw->batch.bo, bo)) {
  435.       perf_debug("Flushing before mapping a referenced bo.\n");
  436.       intel_batchbuffer_flush(brw);
  437.    }
  438.  
  439.    error = brw_bo_map(brw, bo, false /* write enable */, "miptree");
  440.    if (error) {
  441.       DBG("%s: failed to map bo\n", __func__);
  442.       return false;
  443.    }
  444.  
  445.    dst_pitch = _mesa_image_row_stride(packing, width, format, type);
  446.  
  447.    DBG("%s: level=%d x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
  448.        "mesa_format=0x%x tiling=%d "
  449.        "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
  450.        __func__, texImage->Level, xoffset, yoffset, width, height,
  451.        format, type, texImage->TexFormat, image->mt->tiling,
  452.        packing->Alignment, packing->RowLength, packing->SkipPixels,
  453.        packing->SkipRows);
  454.  
  455.    int level = texImage->Level + texImage->TexObject->MinLevel;
  456.  
  457.    /* Adjust x and y offset based on miplevel */
  458.    xoffset += image->mt->level[level].level_x;
  459.    yoffset += image->mt->level[level].level_y;
  460.  
  461.    tiled_to_linear(
  462.       xoffset * cpp, (xoffset + width) * cpp,
  463.       yoffset, yoffset + height,
  464.       pixels - (ptrdiff_t) yoffset * dst_pitch - (ptrdiff_t) xoffset * cpp,
  465.       bo->virtual,
  466.       dst_pitch, image->mt->pitch,
  467.       brw->has_swizzling,
  468.       image->mt->tiling,
  469.       mem_copy
  470.    );
  471.  
  472.    drm_intel_bo_unmap(bo);
  473.    return true;
  474. }
  475.  
  476. static void
  477. intel_get_tex_image(struct gl_context *ctx,
  478.                     GLenum format, GLenum type, GLvoid *pixels,
  479.                     struct gl_texture_image *texImage) {
  480.    struct brw_context *brw = brw_context(ctx);
  481.    bool ok;
  482.  
  483.    DBG("%s\n", __func__);
  484.  
  485.    if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
  486.       if (_mesa_meta_pbo_GetTexSubImage(ctx, 3, texImage, 0, 0, 0,
  487.                                         texImage->Width, texImage->Height,
  488.                                         texImage->Depth, format, type,
  489.                                         pixels, &ctx->Pack)) {
  490.          /* Flush to guarantee coherency between the render cache and other
  491.           * caches the PBO could potentially be bound to after this point.
  492.           * See the related comment in intelReadPixels() for a more detailed
  493.           * explanation.
  494.           */
  495.          intel_batchbuffer_emit_mi_flush(brw);
  496.          return;
  497.       }
  498.  
  499.       perf_debug("%s: fallback to CPU mapping in PBO case\n", __func__);
  500.    }
  501.  
  502.    ok = intel_gettexsubimage_tiled_memcpy(ctx, texImage, 0, 0,
  503.                                           texImage->Width, texImage->Height,
  504.                                           format, type, pixels, &ctx->Pack);
  505.  
  506.    if(ok)
  507.       return;
  508.  
  509.    _mesa_meta_GetTexImage(ctx, format, type, pixels, texImage);
  510.  
  511.    DBG("%s - DONE\n", __func__);
  512. }
  513.  
  514. void
  515. intelInitTextureImageFuncs(struct dd_function_table *functions)
  516. {
  517.    functions->TexImage = intelTexImage;
  518.    functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
  519.    functions->BindRenderbufferTexImage = intel_bind_renderbuffer_tex_image;
  520.    functions->GetTexImage = intel_get_tex_image;
  521. }
  522.