Mesa (master): wgl: Create third buffer when drawing to front buffer

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Wed Nov 18 10:43:33 UTC 2020


Module: Mesa
Branch: master
Commit: 584061bdcdbd1d450af68c00f4ab9516584d66cf
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=584061bdcdbd1d450af68c00f4ab9516584d66cf

Author: Louis-Francis Ratté-Boulianne <lfrb at collabora.com>
Date:   Sat May 23 16:45:08 2020 -0400

wgl: Create third buffer when drawing to front buffer

Reviewed-by: Charmaine Lee <charmainel at vmware.com>
Reviewed-by: Erik Faye-Lund <erik.faye-lund at collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7535>

---

 src/gallium/frontends/wgl/stw_st.c | 177 ++++++++++++++++++++++++++-----------
 1 file changed, 125 insertions(+), 52 deletions(-)

diff --git a/src/gallium/frontends/wgl/stw_st.c b/src/gallium/frontends/wgl/stw_st.c
index 6dabb2ae2d8..39de83ba419 100644
--- a/src/gallium/frontends/wgl/stw_st.c
+++ b/src/gallium/frontends/wgl/stw_st.c
@@ -45,6 +45,8 @@ struct stw_st_framebuffer {
 
    struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
    struct pipe_resource *msaa_textures[ST_ATTACHMENT_COUNT];
+   struct pipe_resource *back_texture;
+   bool needs_fake_front;
    unsigned texture_width, texture_height;
    unsigned texture_mask;
 };
@@ -71,6 +73,53 @@ stw_own_mutex(const CRITICAL_SECTION *cs)
    return ret;
 }
 
+static void
+stw_pipe_blit(struct pipe_context *pipe,
+              struct pipe_resource *dst,
+              struct pipe_resource *src)
+{
+   struct pipe_blit_info blit;
+
+   if (!dst || !src)
+      return;
+
+   /* From the GL spec, version 4.2, section 4.1.11 (Additional Multisample
+    *  Fragment Operations):
+    *
+    *      If a framebuffer object is not bound, after all operations have
+    *      been completed on the multisample buffer, the sample values for
+    *      each color in the multisample buffer are combined to produce a
+    *      single color value, and that value is written into the
+    *      corresponding color buffers selected by DrawBuffer or
+    *      DrawBuffers. An implementation may defer the writing of the color
+    *      buffers until a later time, but the state of the framebuffer must
+    *      behave as if the color buffers were updated as each fragment was
+    *      processed. The method of combination is not specified. If the
+    *      framebuffer contains sRGB values, then it is recommended that the
+    *      an average of sample values is computed in a linearized space, as
+    *      for blending (see section 4.1.7).
+    *
+    * In other words, to do a resolve operation in a linear space, we have
+    * to set sRGB formats if the original resources were sRGB, so don't use
+    * util_format_linear.
+    */
+
+   memset(&blit, 0, sizeof(blit));
+   blit.dst.resource = dst;
+   blit.dst.box.width = dst->width0;
+   blit.dst.box.height = dst->height0;
+   blit.dst.box.depth = 1;
+   blit.dst.format = dst->format;
+   blit.src.resource = src;
+   blit.src.box.width = src->width0;
+   blit.src.box.height = src->height0;
+   blit.src.box.depth = 1;
+   blit.src.format = src->format;
+   blit.mask = PIPE_MASK_RGBA;
+   blit.filter = PIPE_TEX_FILTER_NEAREST;
+
+   pipe->blit(pipe, &blit);
+}
 
 /**
  * Remove outdated textures and create the requested ones.
@@ -93,12 +142,22 @@ stw_st_framebuffer_validate_locked(struct st_context_iface *stctx,
    templ.array_size = 1;
    templ.last_level = 0;
 
+   /* As of now, the only stw_winsys_framebuffer implementation is
+    * d3d12_wgl_framebuffer and it doesn't support front buffer
+    * drawing. A fake front texture is needed to handle that scenario */
+   if (mask & ST_ATTACHMENT_FRONT_LEFT_MASK &&
+       stwfb->fb->winsys_framebuffer &&
+       stwfb->stvis.samples <= 1) {
+      stwfb->needs_fake_front = true;
+   }
+
    /* remove outdated textures */
    if (stwfb->texture_width != width || stwfb->texture_height != height) {
       for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
          pipe_resource_reference(&stwfb->msaa_textures[i], NULL);
          pipe_resource_reference(&stwfb->textures[i], NULL);
       }
+      pipe_resource_reference(&stwfb->back_texture, NULL);
 
       if (stwfb->fb->winsys_framebuffer) {
          templ.nr_samples = templ.nr_storage_samples = 1;
@@ -159,6 +218,36 @@ stw_st_framebuffer_validate_locked(struct st_context_iface *stctx,
       }
    }
 
+   /* When a fake front buffer is needed for drawing, we use the back buffer
+    * as it reduces the number of blits needed:
+    *
+    *   - When flushing the front buffer, we only need to swap the real buffers
+    *   - When swapping buffers, we can safely overwrite the fake front buffer
+    *     as it is now invalid anyway.
+    *
+    * A new texture is created to store the back buffer content.
+    */
+   if (stwfb->needs_fake_front) {
+      if (!stwfb->back_texture) {
+         templ.format = stwfb->stvis.color_format;
+         templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
+         templ.nr_samples = templ.nr_storage_samples = 1;
+
+         stwfb->back_texture =
+            stw_dev->screen->resource_create(stw_dev->screen, &templ);
+
+         /* TODO Only blit if there is something currently drawn on the back buffer */
+         stw_pipe_blit(stctx->pipe,
+                       stwfb->back_texture,
+                       stwfb->textures[ST_ATTACHMENT_BACK_LEFT]);
+      }
+
+      /* Copying front texture content to fake front texture (back texture) */
+      stw_pipe_blit(stctx->pipe,
+                    stwfb->textures[ST_ATTACHMENT_BACK_LEFT],
+                    stwfb->textures[ST_ATTACHMENT_FRONT_LEFT]);
+   }
+
    stwfb->texture_width = width;
    stwfb->texture_height = height;
    stwfb->texture_mask = mask;
@@ -180,7 +269,7 @@ stw_st_framebuffer_validate(struct st_context_iface *stctx,
 
    stw_framebuffer_lock(stwfb->fb);
 
-   if (stwfb->fb->must_resize || (statt_mask & ~stwfb->texture_mask)) {
+   if (stwfb->fb->must_resize || stwfb->needs_fake_front || (statt_mask & ~stwfb->texture_mask)) {
       stw_st_framebuffer_validate_locked(stctx, &stwfb->base,
             stwfb->fb->width, stwfb->fb->height, statt_mask);
       stwfb->fb->must_resize = FALSE;
@@ -190,62 +279,25 @@ stw_st_framebuffer_validate(struct st_context_iface *stctx,
       stwfb->stvis.samples > 1 ? stwfb->msaa_textures
                                : stwfb->textures;
 
-   for (i = 0; i < count; i++)
-      pipe_resource_reference(&out[i], textures[statts[i]]);
+   for (i = 0; i < count; i++) {
+      struct pipe_resource *texture;
+
+      if (stwfb->needs_fake_front) {
+         if (statts[i] == ST_ATTACHMENT_FRONT_LEFT)
+            texture = textures[ST_ATTACHMENT_BACK_LEFT]; /* Fake front buffer */
+         else if (statts[i] == ST_ATTACHMENT_BACK_LEFT)
+            texture = stwfb->back_texture;
+      } else {
+         texture = textures[statts[i]];
+      }
+      pipe_resource_reference(&out[i], texture);
+   }
 
    stw_framebuffer_unlock(stwfb->fb);
 
    return true;
 }
 
-static void
-stw_pipe_blit(struct pipe_context *pipe,
-              struct pipe_resource *dst,
-              struct pipe_resource *src)
-{
-   struct pipe_blit_info blit;
-
-   if (!dst || !src)
-      return;
-
-   /* From the GL spec, version 4.2, section 4.1.11 (Additional Multisample
-    *  Fragment Operations):
-    *
-    *      If a framebuffer object is not bound, after all operations have
-    *      been completed on the multisample buffer, the sample values for
-    *      each color in the multisample buffer are combined to produce a
-    *      single color value, and that value is written into the
-    *      corresponding color buffers selected by DrawBuffer or
-    *      DrawBuffers. An implementation may defer the writing of the color
-    *      buffers until a later time, but the state of the framebuffer must
-    *      behave as if the color buffers were updated as each fragment was
-    *      processed. The method of combination is not specified. If the
-    *      framebuffer contains sRGB values, then it is recommended that the
-    *      an average of sample values is computed in a linearized space, as
-    *      for blending (see section 4.1.7).
-    *
-    * In other words, to do a resolve operation in a linear space, we have
-    * to set sRGB formats if the original resources were sRGB, so don't use
-    * util_format_linear.
-    */
-
-   memset(&blit, 0, sizeof(blit));
-   blit.dst.resource = dst;
-   blit.dst.box.width = dst->width0;
-   blit.dst.box.height = dst->height0;
-   blit.dst.box.depth = 1;
-   blit.dst.format = dst->format;
-   blit.src.resource = src;
-   blit.src.box.width = src->width0;
-   blit.src.box.height = src->height0;
-   blit.src.box.depth = 1;
-   blit.src.format = src->format;
-   blit.mask = PIPE_MASK_RGBA;
-   blit.filter = PIPE_TEX_FILTER_NEAREST;
-
-   pipe->blit(pipe, &blit);
-}
-
 struct notify_before_flush_cb_args {
    struct st_context_iface *stctx;
    struct stw_st_framebuffer *stwfb;
@@ -266,6 +318,11 @@ notify_before_flush_cb(void* _args)
                     args->stwfb->msaa_textures[ST_ATTACHMENT_BACK_LEFT]);
 
       /* FRONT_LEFT is resolved in flush_frontbuffer. */
+   } else if (args->stwfb->back_texture) {
+      /* Fake front texture is dirty ?? */
+      stw_pipe_blit(pipe,
+                    args->stwfb->textures[ST_ATTACHMENT_BACK_LEFT],
+                    args->stwfb->back_texture);
    }
 
    if (args->stwfb->textures[ST_ATTACHMENT_BACK_LEFT])
@@ -336,9 +393,19 @@ stw_st_framebuffer_flush_front(struct st_context_iface *stctx,
 
    stw_framebuffer_lock(stwfb->fb);
 
+   /* Resolve the front buffer. */
    if (stwfb->stvis.samples > 1) {
-      /* Resolve the front buffer. */
       stw_pipe_blit(pipe, stwfb->textures[statt], stwfb->msaa_textures[statt]);
+   } else if (stwfb->needs_fake_front) {
+      struct pipe_resource *ptex;
+
+      /* swap the textures */
+      ptex = stwfb->textures[ST_ATTACHMENT_FRONT_LEFT];
+      stwfb->textures[ST_ATTACHMENT_FRONT_LEFT] = stwfb->textures[ST_ATTACHMENT_BACK_LEFT];
+      stwfb->textures[ST_ATTACHMENT_BACK_LEFT] = ptex;
+
+      /* fake front texture is now invalid */
+      p_atomic_inc(&stwfb->base.stamp);
    }
 
    if (stwfb->textures[statt])
@@ -396,6 +463,7 @@ stw_st_destroy_framebuffer_locked(struct st_framebuffer_iface *stfb)
       pipe_resource_reference(&stwfb->msaa_textures[i], NULL);
       pipe_resource_reference(&stwfb->textures[i], NULL);
    }
+   pipe_resource_reference(&stwfb->back_texture, NULL);
 
    /* Notify the st manager that the framebuffer interface is no
     * longer valid.
@@ -427,6 +495,11 @@ stw_st_swap_framebuffer_locked(HDC hdc, struct st_context_iface *stctx,
    stwfb->msaa_textures[front] = stwfb->msaa_textures[back];
    stwfb->msaa_textures[back] = ptex;
 
+
+   /* Fake front texture is now dirty */
+   if (stwfb->needs_fake_front)
+      p_atomic_inc(&stwfb->base.stamp);
+
    /* convert to mask */
    front = 1 << front;
    back = 1 << back;



More information about the mesa-commit mailing list