st/mesa: use fragment shader to copy stencil buffer
authorIndrajit Kumar Das <indrajit-kumar.das@amd.com>
Fri, 10 Jul 2020 06:15:22 +0000 (11:45 +0530)
committerMarge Bot <eric+marge@anholt.net>
Thu, 16 Jul 2020 13:30:50 +0000 (13:30 +0000)
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/5836>

src/mesa/state_tracker/st_cb_drawpixels.c

index 3f84b22154d8f5f921747c21e5f687d188e4e90c..0537c6d24c8e3bcbb7427f9b184a8db15102cf89 100644 (file)
@@ -1704,6 +1704,8 @@ st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
    GLboolean invertTex = GL_FALSE;
    GLint readX, readY, readW, readH;
    struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
    GLboolean invertTex = GL_FALSE;
    GLint readX, readY, readW, readH;
    struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
+   GLboolean write_stencil = GL_FALSE;
+   GLboolean write_depth = GL_FALSE;
 
    _mesa_update_draw_buffer_bounds(ctx, ctx->DrawBuffer);
 
 
    _mesa_update_draw_buffer_bounds(ctx, ctx->DrawBuffer);
 
@@ -1722,12 +1724,6 @@ st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
       return;
    }
 
       return;
    }
 
-   if (type == GL_STENCIL) {
-      /* can't use texturing to do stencil */
-      copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
-      return;
-   }
-
    /*
     * The subsequent code implements glCopyPixels by copying the source
     * pixels into a temporary texture that's then applied to a textured quad.
    /*
     * The subsequent code implements glCopyPixels by copying the source
     * pixels into a temporary texture that's then applied to a textured quad.
@@ -1757,13 +1753,15 @@ st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
        * into the constant buffer, we need to update them
        */
       st_upload_constants(st, &st->fp->Base);
        * into the constant buffer, we need to update them
        */
       st_upload_constants(st, &st->fp->Base);
-   }
-   else {
-      assert(type == GL_DEPTH);
+   } else if (type == GL_DEPTH) {
       rbRead = st_renderbuffer(ctx->ReadBuffer->
                                Attachment[BUFFER_DEPTH].Renderbuffer);
       rbRead = st_renderbuffer(ctx->ReadBuffer->
                                Attachment[BUFFER_DEPTH].Renderbuffer);
-
       driver_fp = get_drawpix_z_stencil_program(st, GL_TRUE, GL_FALSE);
       driver_fp = get_drawpix_z_stencil_program(st, GL_TRUE, GL_FALSE);
+   } else {
+      assert(type == GL_STENCIL);
+      rbRead = st_renderbuffer(ctx->ReadBuffer->
+                               Attachment[BUFFER_STENCIL].Renderbuffer);
+      driver_fp = get_drawpix_z_stencil_program(st, GL_FALSE, GL_TRUE);
    }
 
    /* Choose the format for the temporary texture. */
    }
 
    /* Choose the format for the temporary texture. */
@@ -1779,6 +1777,11 @@ st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
                                       GL_NONE, st->internal_target, 0, 0,
                                       srcBind, false, false);
       }
                                       GL_NONE, st->internal_target, 0, 0,
                                       srcBind, false, false);
       }
+      else if (type == GL_STENCIL) {
+         /* can't use texturing, fallback to copy */
+         copy_stencil_pixels(ctx, srcx, srcy, width, height, dstx, dsty);
+         return;
+      }
       else {
          assert(type == GL_COLOR);
 
       else {
          assert(type == GL_COLOR);
 
@@ -1854,6 +1857,23 @@ st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
       return;
    }
 
       return;
    }
 
+   /* Create a second sampler view to read stencil */
+   if (type == GL_STENCIL) {
+      write_stencil = GL_TRUE;
+      enum pipe_format stencil_format =
+         util_format_stencil_only(pt->format);
+      /* we should not be doing pixel map/transfer (see above) */
+      assert(num_sampler_view == 1);
+      sv[1] = st_create_texture_sampler_view_format(st->pipe, pt,
+                                                    stencil_format);
+      if (!sv[1]) {
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
+         pipe_resource_reference(&pt, NULL);
+         pipe_sampler_view_reference(&sv[0], NULL);
+         return;
+      }
+      num_sampler_view++;
+   }
    /* Copy the src region to the temporary texture. */
    {
       struct pipe_blit_info blit;
    /* Copy the src region to the temporary texture. */
    {
       struct pipe_blit_info blit;
@@ -1877,7 +1897,10 @@ st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
       blit.dst.box.width = readW;
       blit.dst.box.height = readH;
       blit.dst.box.depth = 1;
       blit.dst.box.width = readW;
       blit.dst.box.height = readH;
       blit.dst.box.depth = 1;
-      blit.mask = util_format_get_mask(pt->format) & ~PIPE_MASK_S;
+      if (type != GL_STENCIL)
+         blit.mask = util_format_get_mask(pt->format) & ~PIPE_MASK_S;
+      else
+         blit.mask = util_format_get_mask(pt->format) & ~PIPE_MASK_Z;
       blit.filter = PIPE_TEX_FILTER_NEAREST;
 
       pipe->blit(pipe, &blit);
       blit.filter = PIPE_TEX_FILTER_NEAREST;
 
       pipe->blit(pipe, &blit);
@@ -1894,7 +1917,7 @@ st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
                       st->passthrough_vs,
                       driver_fp, fpv,
                       ctx->Current.Attrib[VERT_ATTRIB_COLOR0],
                       st->passthrough_vs,
                       driver_fp, fpv,
                       ctx->Current.Attrib[VERT_ATTRIB_COLOR0],
-                      invertTex, GL_FALSE, GL_FALSE);
+                      invertTex, write_depth, write_stencil);
 
    pipe_resource_reference(&pt, NULL);
    pipe_sampler_view_reference(&sv[0], NULL);
 
    pipe_resource_reference(&pt, NULL);
    pipe_sampler_view_reference(&sv[0], NULL);