iris: Drop iris_resource_alloc_separate_aux
[mesa.git] / src / gallium / drivers / svga / svga_pipe_blit.c
index 677d78ca71c61ef49abbdf0f897db47254961d18..a756509ce76246af440e41d89250f8f3f1255bc1 100644 (file)
@@ -32,7 +32,7 @@
 #include "svga_surface.h"
 
 //#include "util/u_blit_sw.h"
-#include "util/u_format.h"
+#include "util/format/u_format.h"
 #include "util/u_surface.h"
 
 #define FILE_DEBUG_FLAG DEBUG_BLIT
@@ -69,29 +69,28 @@ build_blit_info(struct pipe_resource *dst_tex,
             src_box->depth, &blit->dst.box);
 }
 
-
 /**
- * Copy an image between textures with the vgpu10 CopyRegion command.
+ * Copy when src texture and dst texture are same with IntraSurfaceCopy
+ * command.
  */
 static void
-copy_region_vgpu10(struct svga_context *svga, struct pipe_resource *src_tex,
+intra_surface_copy(struct svga_context *svga, struct pipe_resource *tex,
                     unsigned src_x, unsigned src_y, unsigned src_z,
-                    unsigned src_level, unsigned src_face,
-                    struct pipe_resource *dst_tex,
+                    unsigned level, unsigned layer_face,
                     unsigned dst_x, unsigned dst_y, unsigned dst_z,
-                    unsigned dst_level, unsigned dst_face,
                     unsigned width, unsigned height, unsigned depth)
 {
-   enum pipe_error ret;
-   uint32 srcSubResource, dstSubResource;
-   struct svga_texture *dtex, *stex;
    SVGA3dCopyBox box;
+   struct svga_texture *stex;
 
-   stex = svga_texture(src_tex);
-   dtex = svga_texture(dst_tex);
-
+   /*
+    * Makes sure we have flushed all buffered draw operations and also
+    * synchronizes all surfaces with any emulated surface views.
+    */
    svga_surfaces_flush(svga);
 
+   stex = svga_texture(tex);
+
    box.x = dst_x;
    box.y = dst_y;
    box.z = dst_z;
@@ -102,25 +101,46 @@ copy_region_vgpu10(struct svga_context *svga, struct pipe_resource *src_tex,
    box.srcy = src_y;
    box.srcz = src_z;
 
-   srcSubResource = src_face * (src_tex->last_level + 1) + src_level;
-   dstSubResource = dst_face * (dst_tex->last_level + 1) + dst_level;
-
-   ret = SVGA3D_vgpu10_PredCopyRegion(svga->swc,
-                                      dtex->handle, dstSubResource,
-                                      stex->handle, srcSubResource, &box);
-   if (ret != PIPE_OK) {
-      svga_context_flush(svga, NULL);
-      ret = SVGA3D_vgpu10_PredCopyRegion(svga->swc,
-                                         dtex->handle, dstSubResource,
-                                         stex->handle, srcSubResource, &box);
-      assert(ret == PIPE_OK);
-   }
+   SVGA_RETRY(svga, SVGA3D_vgpu10_IntraSurfaceCopy(svga->swc, stex->handle,
+                                                   level, layer_face,  &box));
+   /* Mark the texture subresource as rendered-to. */
+   svga_set_texture_rendered_to(stex, layer_face, level);
+}
+
+/**
+ * Copy an image between textures with the vgpu10 CopyRegion command.
+ */
+static void
+copy_region_vgpu10(struct svga_context *svga, struct pipe_resource *src_tex,
+                    unsigned src_x, unsigned src_y, unsigned src_z,
+                    unsigned src_level, unsigned src_layer_face,
+                    struct pipe_resource *dst_tex,
+                    unsigned dst_x, unsigned dst_y, unsigned dst_z,
+                    unsigned dst_level, unsigned dst_layer_face,
+                    unsigned width, unsigned height, unsigned depth)
+{
+   uint32 srcSubResource, dstSubResource;
+   struct svga_texture *dtex, *stex;
+
+   stex = svga_texture(src_tex);
+   dtex = svga_texture(dst_tex);
+
+   svga_surfaces_flush(svga);
+
+   srcSubResource = src_layer_face * (src_tex->last_level + 1) + src_level;
+   dstSubResource = dst_layer_face * (dst_tex->last_level + 1) + dst_level;
+
+   svga_texture_copy_region(svga, stex->handle, srcSubResource,
+                            src_x, src_y, src_z,
+                            dtex->handle, dstSubResource,
+                            dst_x, dst_y, dst_z,
+                            width, height, depth);
 
    /* Mark the texture subresource as defined. */
-   svga_define_texture_level(dtex, dst_face, dst_level);
+   svga_define_texture_level(dtex, dst_layer_face, dst_level);
 
    /* Mark the texture subresource as rendered-to. */
-   svga_set_texture_rendered_to(dtex, dst_face, dst_level);
+   svga_set_texture_rendered_to(dtex, dst_layer_face, dst_level);
 }
 
 
@@ -144,6 +164,22 @@ copy_region_fallback(struct svga_context *svga,
 }
 
 
+/**
+ * Whether the layer_face index is given by the Z coordinate.
+ */
+static bool
+has_layer_face_index_in_z(enum pipe_texture_target target)
+{
+   if (target == PIPE_TEXTURE_CUBE ||
+       target == PIPE_TEXTURE_1D_ARRAY ||
+       target == PIPE_TEXTURE_2D_ARRAY ||
+       target == PIPE_TEXTURE_CUBE_ARRAY)
+      return true;
+   else
+      return false;
+}
+
+
 /**
  * For some texture types, we need to move the z (slice) coordinate
  * to the layer value.  For example, to select the z=3 slice of a 2D ARRAY
@@ -154,8 +190,9 @@ adjust_z_layer(enum pipe_texture_target target,
                int z_in, unsigned *layer_out, unsigned *z_out)
 {
    if (target == PIPE_TEXTURE_CUBE ||
+       target == PIPE_TEXTURE_1D_ARRAY ||
        target == PIPE_TEXTURE_2D_ARRAY ||
-       target == PIPE_TEXTURE_1D_ARRAY) {
+       target == PIPE_TEXTURE_CUBE_ARRAY) {
       *layer_out = z_in;
       *z_out = 0;
    }
@@ -194,7 +231,8 @@ is_blending_enabled(struct svga_context *svga,
    if (svga->curr.blend) {
       if (svga->curr.blend->independent_blend_enable) {
          for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
-            if (svga->curr.framebuffer.cbufs[i]->texture == blit->dst.resource) {
+            struct pipe_surface *cbuf = svga->curr.framebuffer.cbufs[i];
+            if (cbuf && (cbuf->texture == blit->dst.resource)) {
                if (svga->curr.blend->rt[i].blend_enable) {
                   blend_enable = true;
                }
@@ -210,7 +248,6 @@ is_blending_enabled(struct svga_context *svga,
    return blend_enable;
 }
 
-
 /**
  * If GL_FRAMEBUFFER_SRGB is enabled, then output colorspace is
  * expected to be sRGB if blending is not enabled.
@@ -275,9 +312,17 @@ static bool
 can_blit_via_svga_copy_region(struct svga_context *svga,
                               const struct pipe_blit_info *blit_info)
 {
-   /* check that the blit src/dst regions are same size, no flipping, etc. */
-   if (blit_info->src.box.width != blit_info->dst.box.width ||
-       blit_info->src.box.height != blit_info->dst.box.height)
+   struct pipe_blit_info local_blit = *blit_info;
+
+   /* First basic checks to catch incompatibilities in new or locally unchecked
+    * struct pipe_blit_info members but bypass the format check here.
+    * Also since util_can_blit_via_copy_region() requires a dimension match,
+    * PIPE_FILTER_LINEAR should be equal to PIPE_FILTER_NEAREST.
+    */
+   local_blit.dst.format = local_blit.src.format;
+   if (local_blit.filter == PIPE_TEX_FILTER_LINEAR)
+      local_blit.filter = PIPE_TEX_FILTER_NEAREST;
+   if (!util_can_blit_via_copy_region(&local_blit, TRUE))
       return false;
 
    /* For depth+stencil formats, copy with mask != PIPE_MASK_ZS is not
@@ -287,17 +332,49 @@ can_blit_via_svga_copy_region(struct svga_context *svga,
       blit_info->mask != (PIPE_MASK_ZS))
      return false;
 
-   if (blit_info->alpha_blend ||
-       (svga->render_condition && blit_info->render_condition_enable) ||
-       blit_info->scissor_enable)
+   return check_blending_and_srgb_cond(svga, blit_info);
+}
+
+/**
+ * Check whether we can blit using the intra_surface_copy command.
+ */
+static bool
+can_blit_via_intra_surface_copy(struct svga_context *svga,
+                                const struct pipe_blit_info *blit_info)
+{
+   struct svga_winsys_screen *sws = svga_screen(svga->pipe.screen)->sws;
+   struct svga_texture *dtex, *stex;
+
+   if (!svga_have_vgpu10(svga))
       return false;
 
-   return check_blending_and_srgb_cond(svga, blit_info);
+   /* src surface cannot be multisample */
+   if (blit_info->src.resource->nr_samples > 1)
+      return false;
+
+   if (!sws->have_intra_surface_copy)
+      return false;
+
+   if (svga->render_condition && blit_info->render_condition_enable)
+      return false;
+
+   if (blit_info->src.level != blit_info->dst.level)
+      return false;
+
+   if (has_layer_face_index_in_z(blit_info->src.resource->target)){
+      if (blit_info->src.box.z != blit_info->dst.box.z)
+         return false;
+   }
+
+   stex = svga_texture(blit_info->src.resource);
+   dtex = svga_texture(blit_info->dst.resource);
+
+   return (stex->handle == dtex->handle);
 }
 
 
 /**
- * The state tracker implements some resource copies with blits (for
+ * the gallium frontend implements some resource copies with blits (for
  * GL_ARB_copy_image).  This function checks if we should really do the blit
  * with a VGPU10 CopyRegion command or software fallback (for incompatible
  * src/dst formats).
@@ -337,6 +414,24 @@ can_blit_via_surface_copy(struct svga_context *svga,
 {
    struct svga_texture *dtex, *stex;
 
+   /* Mimic the format tests in util_can_blit_via_copy_region(), but
+    * skip the other tests that have already been performed.
+    */
+   if (blit_info->src.format != blit_info->dst.format) {
+      const struct util_format_description *src_desc, *dst_desc;
+
+      src_desc = util_format_description(blit_info->src.resource->format);
+      dst_desc = util_format_description(blit_info->dst.resource->format);
+
+      if (blit_info->src.resource->format != blit_info->src.format ||
+          blit_info->dst.resource->format != blit_info->dst.format ||
+          !util_is_format_compatible(src_desc, dst_desc))
+         return false;
+   }
+
+   if (svga->render_condition && blit_info->render_condition_enable)
+      return false;
+
    /* can't copy between different resource types */
    if (svga_resource_type(blit_info->src.resource->target) !=
        svga_resource_type(blit_info->dst.resource->target))
@@ -363,27 +458,32 @@ static bool
 try_copy_region(struct svga_context *svga,
                 const struct pipe_blit_info *blit)
 {
-   unsigned src_face, src_z, dst_face, dst_z;
+   unsigned src_layer_face, src_z, dst_layer_face, dst_z;
 
    if (!can_blit_via_svga_copy_region(svga, blit))
       return false;
 
    adjust_z_layer(blit->src.resource->target, blit->src.box.z,
-                  &src_face, &src_z);
+                  &src_layer_face, &src_z);
 
    adjust_z_layer(blit->dst.resource->target, blit->dst.box.z,
-                  &dst_face, &dst_z);
+                  &dst_layer_face, &dst_z);
 
    if (can_blit_via_copy_region_vgpu10(svga, blit)) {
+      svga_toggle_render_condition(svga, blit->render_condition_enable, FALSE);
+
       copy_region_vgpu10(svga,
                          blit->src.resource,
                          blit->src.box.x, blit->src.box.y, src_z,
-                         blit->src.level, src_face,
+                         blit->src.level, src_layer_face,
                          blit->dst.resource,
                          blit->dst.box.x, blit->dst.box.y, dst_z,
-                         blit->dst.level, dst_face,
+                         blit->dst.level, dst_layer_face,
                          blit->src.box.width, blit->src.box.height,
                          blit->src.box.depth);
+
+      svga_toggle_render_condition(svga, blit->render_condition_enable, TRUE);
+
       return true;
    }
 
@@ -396,15 +496,26 @@ try_copy_region(struct svga_context *svga,
       svga_texture_copy_handle(svga,
                                stex->handle,
                                blit->src.box.x, blit->src.box.y, src_z,
-                               blit->src.level, src_face,
+                               blit->src.level, src_layer_face,
                                dtex->handle,
                                blit->dst.box.x, blit->dst.box.y, dst_z,
-                               blit->dst.level, dst_face,
+                               blit->dst.level, dst_layer_face,
                                blit->src.box.width, blit->src.box.height,
                                blit->src.box.depth);
 
-      svga_define_texture_level(dtex, dst_face, blit->dst.level);
-      svga_set_texture_rendered_to(dtex, dst_face, blit->dst.level);
+      svga_define_texture_level(dtex, dst_layer_face, blit->dst.level);
+      svga_set_texture_rendered_to(dtex, dst_layer_face, blit->dst.level);
+      return true;
+   }
+
+   if (can_blit_via_intra_surface_copy(svga, blit)) {
+      intra_surface_copy(svga,
+                         blit->src.resource,
+                         blit->src.box.x, blit->src.box.y, src_z,
+                         blit->src.level, src_layer_face,
+                         blit->dst.box.x, blit->dst.box.y, dst_z,
+                         blit->src.box.width, blit->src.box.height,
+                         blit->src.box.depth);
       return true;
    }
 
@@ -454,13 +565,28 @@ try_blit(struct svga_context *svga, const struct pipe_blit_info *blit_info)
    struct pipe_blit_info blit = *blit_info;
 
    SVGA_STATS_TIME_PUSH(sws, SVGA_STATS_TIME_BLITBLITTER);
-   
+
+   /**
+    * Avoid using util_blitter_blit() for these depth formats on non-vgpu10
+    * devices because these depth formats only support comparison mode
+    * and not ordinary sampling.
+    */
+   if (!svga_have_vgpu10(svga) && (blit.mask & PIPE_MASK_Z) &&
+       (svga_texture(dst)->key.format == SVGA3D_Z_D16 ||
+       svga_texture(dst)->key.format == SVGA3D_Z_D24X8 ||
+       svga_texture(dst)->key.format == SVGA3D_Z_D24S8)) {
+      ret = false;
+      goto done;
+  }
+
    /**
     * If format is srgb and blend is enabled then color values need
     * to be converted into linear format.
     */
-   if (is_blending_enabled(svga, &blit))
+   if (is_blending_enabled(svga, &blit)) {
       blit.src.format = util_format_linear(blit.src.format);
+      blit.dst.format = util_format_linear(blit.dst.format);
+   }
 
    /* Check if we can create shader resource view and
     * render target view for the quad blitter to work
@@ -496,11 +622,13 @@ try_blit(struct svga_context *svga, const struct pipe_blit_info *blit_info)
    util_blitter_save_vertex_elements(svga->blitter, (void*)svga->curr.velems);
    util_blitter_save_vertex_shader(svga->blitter, svga->curr.vs);
    util_blitter_save_geometry_shader(svga->blitter, svga->curr.user_gs);
+   util_blitter_save_tessctrl_shader(svga->blitter, svga->curr.tcs);
+   util_blitter_save_tesseval_shader(svga->blitter, svga->curr.tes);
    util_blitter_save_so_targets(svga->blitter, svga->num_so_targets,
                      (struct pipe_stream_output_target**)svga->so_targets);
    util_blitter_save_rasterizer(svga->blitter, (void*)svga->curr.rast);
-   util_blitter_save_viewport(svga->blitter, &svga->curr.viewport);
-   util_blitter_save_scissor(svga->blitter, &svga->curr.scissor);
+   util_blitter_save_viewport(svga->blitter, &svga->curr.viewport[0]);
+   util_blitter_save_scissor(svga->blitter, &svga->curr.scissor[0]);
    util_blitter_save_fragment_shader(svga->blitter, svga->curr.fs);
    util_blitter_save_blend(svga->blitter, (void*)svga->curr.blend);
    util_blitter_save_depth_stencil_alpha(svga->blitter,
@@ -514,8 +642,6 @@ try_blit(struct svga_context *svga, const struct pipe_blit_info *blit_info)
    util_blitter_save_fragment_sampler_views(svga->blitter,
                      svga->curr.num_sampler_views[PIPE_SHADER_FRAGMENT],
                      svga->curr.sampler_views[PIPE_SHADER_FRAGMENT]);
-   /*util_blitter_save_render_condition(svga->blitter, svga->render_cond_query,
-                                      svga->render_cond_cond, svga->render_cond_mode);*/
 
    if (!can_create_src_view) {
       struct pipe_resource template;
@@ -547,6 +673,7 @@ try_blit(struct svga_context *svga, const struct pipe_blit_info *blit_info)
                       blit.src.level, &blit.src.box,
                       &copy_region_blit);
       if (!try_copy_region(svga, &copy_region_blit)) {
+         debug_printf("svga: Source blit format conversion failed.\n");
          ret = false;
          goto done;
       }
@@ -577,8 +704,12 @@ try_blit(struct svga_context *svga, const struct pipe_blit_info *blit_info)
       blit.dst.resource = newDst;
    }
 
+   svga_toggle_render_condition(svga, blit.render_condition_enable, FALSE);
+
    util_blitter_blit(svga->blitter, &blit);
 
+   svga_toggle_render_condition(svga, blit.render_condition_enable, TRUE);
+
    if (blit.dst.resource != dst) {
       struct pipe_blit_info copy_region_blit;
 
@@ -589,13 +720,14 @@ try_blit(struct svga_context *svga, const struct pipe_blit_info *blit_info)
        * A temporary resource was created for the blit, we need to
        * copy from the temporary resource back to the original destination.
        */
-      build_blit_info(blit.dst.resource,
+      build_blit_info(dst,
                       blit.dst.level, blit.dst.box.x,
                       blit.dst.box.y, blit.dst.box.z,
                       newDst,
                       blit.dst.level, &blit.dst.box,
                       &copy_region_blit);
       if (!try_copy_region(svga, &copy_region_blit)) {
+         debug_printf("svga: Destination blit format conversion failed.\n");
          ret = false;
          goto done;
       }
@@ -622,6 +754,13 @@ try_cpu_copy_region(struct svga_context *svga,
 {
    if (util_can_blit_via_copy_region(blit, TRUE) ||
        util_can_blit_via_copy_region(blit, FALSE)) {
+
+      if (svga->render_condition && blit->render_condition_enable) {
+         debug_warning("CPU copy_region doesn't support "
+                       "conditional rendering.\n");
+         return false;
+      }
+
       copy_region_fallback(svga, blit->dst.resource,
                            blit->dst.level,
                            blit->dst.box.x, blit->dst.box.y,
@@ -658,14 +797,12 @@ svga_blit(struct pipe_context *pipe,
    if (try_copy_region(svga, blit))
       goto done;
 
-   /* FIXME: Ideally, we should call try_blit() before try_cpu_copy_region(),
-    * however that breaks piglit test gl-1.0-scissor-copypixels.
-    */
-   if (try_cpu_copy_region(svga, blit))
+   if (try_blit(svga, blit))
       goto done;
 
-   if (try_blit(svga, blit))
+   if (!try_cpu_copy_region(svga, blit))
       debug_printf("svga: Blit failed.\n");
+   
 done:
    SVGA_STATS_TIME_POP(sws);  /* SVGA_STATS_TIME_BLIT */
    (void) sws;
@@ -692,23 +829,17 @@ svga_resource_copy_region(struct pipe_context *pipe,
    if (dst_tex->target == PIPE_BUFFER && src_tex->target == PIPE_BUFFER) {
       /* can't copy within the same buffer, unfortunately */
       if (svga_have_vgpu10(svga) && src_tex != dst_tex) {
-         enum pipe_error ret;
          struct svga_winsys_surface *src_surf;
          struct svga_winsys_surface *dst_surf;
          struct svga_buffer *dbuffer = svga_buffer(dst_tex);
+         struct svga_buffer *sbuffer = svga_buffer(src_tex);
 
-         src_surf = svga_buffer_handle(svga, src_tex);
-         dst_surf = svga_buffer_handle(svga, dst_tex);
-
-         ret = SVGA3D_vgpu10_BufferCopy(svga->swc, src_surf, dst_surf,
-                                        src_box->x, dstx, src_box->width);
-         if (ret != PIPE_OK) {
-            svga_context_flush(svga, NULL);
-            ret = SVGA3D_vgpu10_BufferCopy(svga->swc, src_surf, dst_surf,
-                                           src_box->x, dstx, src_box->width);
-            assert(ret == PIPE_OK);
-         }
+         src_surf = svga_buffer_handle(svga, src_tex, sbuffer->bind_flags);
+         dst_surf = svga_buffer_handle(svga, dst_tex, dbuffer->bind_flags);
 
+         SVGA_RETRY(svga, SVGA3D_vgpu10_BufferCopy(svga->swc, src_surf,
+                                                   dst_surf, src_box->x, dstx,
+                                                   src_box->width));
          dbuffer->dirty = TRUE;
       }
       else {
@@ -727,9 +858,12 @@ svga_resource_copy_region(struct pipe_context *pipe,
 
       /* Blits are format-converting which is not what we want, so perform a
        * strict format-check.
-       * FIXME: Also blits appear broken with 3D source textures.
+       * FIXME: Need to figure out why srgb blits (tf2) and
+       * 3D blits (piglit) are broken here. Perhaps we set up the
+       * struct pipe_blit_info incorrectly.
        */
       if (src_tex->format == dst_tex->format &&
+          !util_format_is_srgb(src_tex->format) &&
           svga_resource_type(src_tex->target) != SVGA3D_RESOURCE_TEXTURE3D &&
           try_blit(svga, &blit))
          goto done;