[gtk+] vulkan: Modernize blend pipeline



commit f05f0377df567fc3d0211d4385b90dd1c951d9aa
Author: Benjamin Otte <otte redhat com>
Date:   Sun Jan 1 23:47:53 2017 +0100

    vulkan: Modernize blend pipeline
    
    - Rename from blit => blend
    - Use instances
    - Add clip versions

 gsk/gskvulkanblendpipeline.c                       |   48 ++++++++--------
 gsk/gskvulkanrender.c                              |    4 +-
 gsk/gskvulkanrenderpass.c                          |   22 ++++++--
 gsk/gskvulkanrenderprivate.h                       |    4 +-
 gsk/resources/vulkan/blend-clip-rounded.frag.glsl  |   57 ++++++++++++++++++++
 gsk/resources/vulkan/blend-clip-rounded.frag.spv   |  Bin 0 -> 5180 bytes
 gsk/resources/vulkan/blend-clip-rounded.vert.glsl  |   53 ++++++++++++++++++
 gsk/resources/vulkan/blend-clip-rounded.vert.spv   |  Bin 0 -> 4940 bytes
 .../{blit.frag.glsl => blend-clip.frag.glsl}       |    2 +-
 .../vulkan/{blit.frag.spv => blend-clip.frag.spv}  |  Bin 632 -> 632 bytes
 gsk/resources/vulkan/blend-clip.vert.glsl          |   46 ++++++++++++++++
 gsk/resources/vulkan/blend-clip.vert.spv           |  Bin 0 -> 4604 bytes
 .../vulkan/{blit.frag.glsl => blend.frag.glsl}     |    2 +-
 .../vulkan/{blit.frag.spv => blend.frag.spv}       |  Bin 632 -> 632 bytes
 gsk/resources/vulkan/blend.vert.glsl               |   31 +++++++++++
 gsk/resources/vulkan/blend.vert.spv                |  Bin 0 -> 1964 bytes
 gsk/resources/vulkan/blit.vert.glsl                |   22 --------
 gsk/resources/vulkan/blit.vert.spv                 |  Bin 1252 -> 0 bytes
 18 files changed, 236 insertions(+), 55 deletions(-)
---
diff --git a/gsk/gskvulkanblendpipeline.c b/gsk/gskvulkanblendpipeline.c
index 59dd971..8a98fbc 100644
--- a/gsk/gskvulkanblendpipeline.c
+++ b/gsk/gskvulkanblendpipeline.c
@@ -7,14 +7,12 @@ struct _GskVulkanBlendPipeline
   GObject parent_instance;
 };
 
-typedef struct _GskVulkanVertex GskVulkanVertex;
+typedef struct _GskVulkanBlendInstance GskVulkanBlendInstance;
 
-struct _GskVulkanVertex
+struct _GskVulkanBlendInstance
 {
-  float x;
-  float y;
-  float tex_x;
-  float tex_y;
+  float rect[4];
+  float tex_rect[4];
 };
 
 G_DEFINE_TYPE (GskVulkanBlendPipeline, gsk_vulkan_blend_pipeline, GSK_TYPE_VULKAN_PIPELINE)
@@ -25,22 +23,22 @@ gsk_vulkan_blend_pipeline_get_input_state_create_info (GskVulkanPipeline *self)
   static const VkVertexInputBindingDescription vertexBindingDescriptions[] = {
       {
           .binding = 0,
-          .stride = 4 * sizeof (float),
-          .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
+          .stride = sizeof (GskVulkanBlendInstance),
+          .inputRate = VK_VERTEX_INPUT_RATE_INSTANCE
       }
   };
   static const VkVertexInputAttributeDescription vertexInputAttributeDescription[] = {
       {
           .location = 0,
           .binding = 0,
-          .format = VK_FORMAT_R32G32_SFLOAT,
-          .offset = 0,
+          .format = VK_FORMAT_R32G32B32A32_SFLOAT,
+          .offset = G_STRUCT_OFFSET (GskVulkanBlendInstance, rect),
       },
       {
           .location = 1,
           .binding = 0,
-          .format = VK_FORMAT_R32G32_SFLOAT,
-          .offset = 2 * sizeof (float),
+          .format = VK_FORMAT_R32G32B32A32_SFLOAT,
+          .offset = G_STRUCT_OFFSET (GskVulkanBlendInstance, tex_rect),
       }
   };
   static const VkPipelineVertexInputStateCreateInfo info = {
@@ -88,7 +86,7 @@ gsk_vulkan_blend_pipeline_new (GskVulkanPipelineLayout *layout,
 gsize
 gsk_vulkan_blend_pipeline_count_vertex_data (GskVulkanBlendPipeline *pipeline)
 {
-  return sizeof (GskVulkanVertex) * 6;
+  return sizeof (GskVulkanBlendInstance);
 }
 
 void
@@ -96,14 +94,16 @@ gsk_vulkan_blend_pipeline_collect_vertex_data (GskVulkanBlendPipeline *pipeline,
                                                guchar                 *data,
                                                const graphene_rect_t  *rect)
 {
-  GskVulkanVertex *vertices = (GskVulkanVertex *) data;
-
-  vertices[0] = (GskVulkanVertex) { rect->origin.x,                    rect->origin.y,                     
0.0, 0.0 };
-  vertices[1] = (GskVulkanVertex) { rect->origin.x + rect->size.width, rect->origin.y,                     
1.0, 0.0 };
-  vertices[2] = (GskVulkanVertex) { rect->origin.x,                    rect->origin.y + rect->size.height, 
0.0, 1.0 };
-  vertices[3] = (GskVulkanVertex) { rect->origin.x,                    rect->origin.y + rect->size.height, 
0.0, 1.0 };
-  vertices[4] = (GskVulkanVertex) { rect->origin.x + rect->size.width, rect->origin.y,                     
1.0, 0.0 };
-  vertices[5] = (GskVulkanVertex) { rect->origin.x + rect->size.width, rect->origin.y + rect->size.height, 
1.0, 1.0 };
+  GskVulkanBlendInstance *instance = (GskVulkanBlendInstance *) data;
+
+  instance->rect[0] = rect->origin.x;
+  instance->rect[1] = rect->origin.y;
+  instance->rect[2] = rect->size.width;
+  instance->rect[3] = rect->size.height;
+  instance->tex_rect[0] = 0.0;
+  instance->tex_rect[1] = 0.0;
+  instance->tex_rect[2] = 1.0;
+  instance->tex_rect[3] = 1.0;
 }
 
 gsize
@@ -113,8 +113,8 @@ gsk_vulkan_blend_pipeline_draw (GskVulkanBlendPipeline *pipeline,
                                 gsize                   n_commands)
 {
   vkCmdDraw (command_buffer,
-             n_commands * 6, 1,
-             offset, 0);
+             6, n_commands,
+             0, offset);
 
-  return n_commands * 6;
+  return n_commands;
 }
diff --git a/gsk/gskvulkanrender.c b/gsk/gskvulkanrender.c
index 35e2dce..1a4a86a 100644
--- a/gsk/gskvulkanrender.c
+++ b/gsk/gskvulkanrender.c
@@ -314,7 +314,9 @@ gsk_vulkan_render_get_pipeline (GskVulkanRender       *self,
     const char *name;
     GskVulkanPipeline * (* create_func) (GskVulkanPipelineLayout *layout, const char *name, VkRenderPass 
render_pass);
   } pipeline_info[GSK_VULKAN_N_PIPELINES] = {
-    { "blit", gsk_vulkan_blend_pipeline_new },
+    { "blend", gsk_vulkan_blend_pipeline_new },
+    { "blend-clip", gsk_vulkan_blend_pipeline_new },
+    { "blend-clip-rounded", gsk_vulkan_blend_pipeline_new },
     { "color", gsk_vulkan_color_pipeline_new },
     { "color-clip", gsk_vulkan_color_pipeline_new },
     { "color-clip-rounded", gsk_vulkan_color_pipeline_new },
diff --git a/gsk/gskvulkanrenderpass.c b/gsk/gskvulkanrenderpass.c
index 68b9e48..c500d90 100644
--- a/gsk/gskvulkanrenderpass.c
+++ b/gsk/gskvulkanrenderpass.c
@@ -122,18 +122,30 @@ gsk_vulkan_render_pass_add_node (GskVulkanRenderPass           *self,
     case GSK_CAIRO_NODE:
       if (gsk_cairo_node_get_surface (node) == NULL)
         return;
-      if (!gsk_vulkan_clip_contains_rect (&constants->clip, &node->bounds))
+      if (gsk_vulkan_clip_contains_rect (&constants->clip, &node->bounds))
+        pipeline_type = GSK_VULKAN_PIPELINE_BLEND;
+      else if (constants->clip.type == GSK_VULKAN_CLIP_RECT)
+        pipeline_type = GSK_VULKAN_PIPELINE_BLEND_CLIP;
+      else if (constants->clip.type == GSK_VULKAN_CLIP_ROUNDED_CIRCULAR)
+        pipeline_type = GSK_VULKAN_PIPELINE_BLEND_CLIP_ROUNDED;
+      else
         FALLBACK ("Cairo nodes can't deal with clip type %u\n", constants->clip.type);
       op.type = GSK_VULKAN_OP_SURFACE;
-      op.render.pipeline = gsk_vulkan_render_get_pipeline (render, GSK_VULKAN_PIPELINE_BLIT);
+      op.render.pipeline = gsk_vulkan_render_get_pipeline (render, pipeline_type);
       g_array_append_val (self->render_ops, op);
       return;
 
     case GSK_TEXTURE_NODE:
-      if (!gsk_vulkan_clip_contains_rect (&constants->clip, &node->bounds))
+      if (gsk_vulkan_clip_contains_rect (&constants->clip, &node->bounds))
+        pipeline_type = GSK_VULKAN_PIPELINE_BLEND;
+      else if (constants->clip.type == GSK_VULKAN_CLIP_RECT)
+        pipeline_type = GSK_VULKAN_PIPELINE_BLEND_CLIP;
+      else if (constants->clip.type == GSK_VULKAN_CLIP_ROUNDED_CIRCULAR)
+        pipeline_type = GSK_VULKAN_PIPELINE_BLEND_CLIP_ROUNDED;
+      else
         FALLBACK ("Texture nodes can't deal with clip type %u\n", constants->clip.type);
       op.type = GSK_VULKAN_OP_TEXTURE;
-      op.render.pipeline = gsk_vulkan_render_get_pipeline (render, GSK_VULKAN_PIPELINE_BLIT);
+      op.render.pipeline = gsk_vulkan_render_get_pipeline (render, pipeline_type);
       g_array_append_val (self->render_ops, op);
       return;
 
@@ -292,7 +304,7 @@ fallback:
         g_assert_not_reached ();
         return;
     }
-  op.render.pipeline = gsk_vulkan_render_get_pipeline (render, GSK_VULKAN_PIPELINE_BLIT);
+  op.render.pipeline = gsk_vulkan_render_get_pipeline (render, GSK_VULKAN_PIPELINE_BLEND);
   g_array_append_val (self->render_ops, op);
 }
 #undef FALLBACK
diff --git a/gsk/gskvulkanrenderprivate.h b/gsk/gskvulkanrenderprivate.h
index cffce7f..6bfb8b0 100644
--- a/gsk/gskvulkanrenderprivate.h
+++ b/gsk/gskvulkanrenderprivate.h
@@ -10,7 +10,9 @@
 G_BEGIN_DECLS
 
 typedef enum {
-  GSK_VULKAN_PIPELINE_BLIT,
+  GSK_VULKAN_PIPELINE_BLEND,
+  GSK_VULKAN_PIPELINE_BLEND_CLIP,
+  GSK_VULKAN_PIPELINE_BLEND_CLIP_ROUNDED,
   GSK_VULKAN_PIPELINE_COLOR,
   GSK_VULKAN_PIPELINE_COLOR_CLIP,
   GSK_VULKAN_PIPELINE_COLOR_CLIP_ROUNDED,
diff --git a/gsk/resources/vulkan/blend-clip-rounded.frag.glsl 
b/gsk/resources/vulkan/blend-clip-rounded.frag.glsl
new file mode 100644
index 0000000..fb1c148
--- /dev/null
+++ b/gsk/resources/vulkan/blend-clip-rounded.frag.glsl
@@ -0,0 +1,57 @@
+#version 420 core
+
+struct RoundedRect {
+  vec4 bounds;
+  vec4 corners;
+};
+
+layout(location = 0) in vec2 inPos;
+layout(location = 1) in vec2 inTexCoord;
+layout(location = 2) in flat vec4 inClipBounds;
+layout(location = 3) in flat vec4 inClipWidths;
+
+layout(set = 0, binding = 0) uniform sampler2D inTexture;
+
+layout(location = 0) out vec4 color;
+
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+float clip(vec2 pos, RoundedRect r) {
+  vec2 ref_tl = r.bounds.xy + vec2( r.corners.x,  r.corners.x);
+  vec2 ref_tr = r.bounds.zy + vec2(-r.corners.y,  r.corners.y);
+  vec2 ref_br = r.bounds.zw + vec2(-r.corners.z, -r.corners.z);
+  vec2 ref_bl = r.bounds.xw + vec2( r.corners.w, -r.corners.w);
+  
+  float d_tl = distance(pos, ref_tl);
+  float d_tr = distance(pos, ref_tr);
+  float d_br = distance(pos, ref_br);
+  float d_bl = distance(pos, ref_bl);
+
+  float pixels_per_fragment = length(fwidth(pos.xy));
+  float nudge = 0.5 * pixels_per_fragment;
+  vec4 distances = vec4(d_tl, d_tr, d_br, d_bl) - r.corners + nudge;
+
+  bvec4 is_out = bvec4(pos.x < ref_tl.x && pos.y < ref_tl.y,
+                       pos.x > ref_tr.x && pos.y < ref_tr.y,
+                       pos.x > ref_br.x && pos.y > ref_br.y,
+                       pos.x < ref_bl.x && pos.y > ref_bl.y);
+
+  float distance_from_border = dot(vec4(is_out),
+                                   max(vec4(0.0, 0.0, 0.0, 0.0), distances));
+
+  // Move the distance back into pixels.
+  distance_from_border /= pixels_per_fragment;
+  // Apply a more gradual fade out to transparent.
+  //distance_from_border -= 0.5;
+
+  return 1.0 - smoothstep(0.0, 1.0, distance_from_border);
+}
+
+void main()
+{
+  RoundedRect r = RoundedRect(vec4(inClipBounds.xy, inClipBounds.xy + inClipBounds.zw), inClipWidths);
+
+  color = texture (inTexture, inTexCoord) * clip (inPos, r);
+}
diff --git a/gsk/resources/vulkan/blend-clip-rounded.frag.spv 
b/gsk/resources/vulkan/blend-clip-rounded.frag.spv
new file mode 100644
index 0000000..aa407c2
Binary files /dev/null and b/gsk/resources/vulkan/blend-clip-rounded.frag.spv differ
diff --git a/gsk/resources/vulkan/blend-clip-rounded.vert.glsl 
b/gsk/resources/vulkan/blend-clip-rounded.vert.glsl
new file mode 100644
index 0000000..c680c65
--- /dev/null
+++ b/gsk/resources/vulkan/blend-clip-rounded.vert.glsl
@@ -0,0 +1,53 @@
+#version 420 core
+
+layout(location = 0) in vec4 inRect;
+layout(location = 1) in vec4 inTexRect;
+
+layout(push_constant) uniform PushConstants {
+    mat4 mvp;
+    vec4 clip_bounds;
+    vec4 clip_widths;
+    vec4 clip_heights;
+} push;
+
+layout(location = 0) out vec2 outPos;
+layout(location = 1) out vec2 outTexCoord;
+layout(location = 2) out flat vec4 outClipBounds;
+layout(location = 3) out flat vec4 outClipWidths;
+
+out gl_PerVertex {
+  vec4 gl_Position;
+};
+
+vec2 offsets[6] = { vec2(0.0, 0.0),
+                    vec2(1.0, 0.0),
+                    vec2(0.0, 1.0),
+                    vec2(0.0, 1.0),
+                    vec2(1.0, 0.0),
+                    vec2(1.0, 1.0) };
+
+vec4 intersect(vec4 a, vec4 b)
+{
+  a = vec4(a.xy, a.xy + a.zw);
+  b = vec4(b.xy, b.xy + b.zw);
+  vec4 result = vec4(max(a.xy, b.xy), min(a.zw, b.zw));
+  if (any (greaterThanEqual (result.xy, result.zw)))
+    return vec4(0.0,0.0,0.0,0.0);
+  return vec4(result.xy, result.zw - result.xy);
+}
+
+void main() {
+  vec4 rect = intersect(inRect, push.clip_bounds);
+  vec2 pos = rect.xy + rect.zw * offsets[gl_VertexIndex];
+  gl_Position = push.mvp * vec4 (pos, 0.0, 1.0);
+
+  outPos = pos;
+  outClipBounds = push.clip_bounds;
+  outClipWidths = push.clip_widths;
+
+  vec4 texrect = vec4((rect.xy - inRect.xy) / inRect.zw,
+                      rect.zw / inRect.zw);
+  texrect = vec4(inTexRect.xy + inTexRect.zw * texrect.xy,
+                 inTexRect.zw * texrect.zw);
+  outTexCoord = texrect.xy + texrect.zw * offsets[gl_VertexIndex];
+}
diff --git a/gsk/resources/vulkan/blend-clip-rounded.vert.spv 
b/gsk/resources/vulkan/blend-clip-rounded.vert.spv
new file mode 100644
index 0000000..0c27a6b
Binary files /dev/null and b/gsk/resources/vulkan/blend-clip-rounded.vert.spv differ
diff --git a/gsk/resources/vulkan/blit.frag.glsl b/gsk/resources/vulkan/blend-clip.frag.glsl
similarity index 79%
copy from gsk/resources/vulkan/blit.frag.glsl
copy to gsk/resources/vulkan/blend-clip.frag.glsl
index 5d7c818..4575c49 100644
--- a/gsk/resources/vulkan/blit.frag.glsl
+++ b/gsk/resources/vulkan/blend-clip.frag.glsl
@@ -8,5 +8,5 @@ layout(location = 0) out vec4 color;
 
 void main()
 {
-    color = texture (inTexture, inTexCoord);
+  color = texture (inTexture, inTexCoord);
 }
diff --git a/gsk/resources/vulkan/blend-clip.vert.glsl b/gsk/resources/vulkan/blend-clip.vert.glsl
new file mode 100644
index 0000000..0d66a2d
--- /dev/null
+++ b/gsk/resources/vulkan/blend-clip.vert.glsl
@@ -0,0 +1,46 @@
+#version 420 core
+
+layout(location = 0) in vec4 inRect;
+layout(location = 1) in vec4 inTexRect;
+
+layout(push_constant) uniform PushConstants {
+    mat4 mvp;
+    vec4 clip_bounds;
+    vec4 clip_widths;
+    vec4 clip_heights;
+} push;
+
+layout(location = 0) out vec2 outTexCoord;
+
+out gl_PerVertex {
+  vec4 gl_Position;
+};
+
+vec2 offsets[6] = { vec2(0.0, 0.0),
+                    vec2(1.0, 0.0),
+                    vec2(0.0, 1.0),
+                    vec2(0.0, 1.0),
+                    vec2(1.0, 0.0),
+                    vec2(1.0, 1.0) };
+
+vec4 intersect(vec4 a, vec4 b)
+{
+  a = vec4(a.xy, a.xy + a.zw);
+  b = vec4(b.xy, b.xy + b.zw);
+  vec4 result = vec4(max(a.xy, b.xy), min(a.zw, b.zw));
+  if (any (greaterThanEqual (result.xy, result.zw)))
+    return vec4(0.0,0.0,0.0,0.0);
+  return vec4(result.xy, result.zw - result.xy);
+}
+
+void main() {
+  vec4 rect = intersect(inRect, push.clip_bounds);
+  vec2 pos = rect.xy + rect.zw * offsets[gl_VertexIndex];
+  gl_Position = push.mvp * vec4 (pos, 0.0, 1.0);
+
+  vec4 texrect = vec4((rect.xy - inRect.xy) / inRect.zw,
+                      rect.zw / inRect.zw);
+  texrect = vec4(inTexRect.xy + inTexRect.zw * texrect.xy,
+                 inTexRect.zw * texrect.zw);
+  outTexCoord = texrect.xy + texrect.zw * offsets[gl_VertexIndex];
+}
diff --git a/gsk/resources/vulkan/blend-clip.vert.spv b/gsk/resources/vulkan/blend-clip.vert.spv
new file mode 100644
index 0000000..a12f3c8
Binary files /dev/null and b/gsk/resources/vulkan/blend-clip.vert.spv differ
diff --git a/gsk/resources/vulkan/blit.frag.glsl b/gsk/resources/vulkan/blend.frag.glsl
similarity index 79%
rename from gsk/resources/vulkan/blit.frag.glsl
rename to gsk/resources/vulkan/blend.frag.glsl
index 5d7c818..4575c49 100644
--- a/gsk/resources/vulkan/blit.frag.glsl
+++ b/gsk/resources/vulkan/blend.frag.glsl
@@ -8,5 +8,5 @@ layout(location = 0) out vec4 color;
 
 void main()
 {
-    color = texture (inTexture, inTexCoord);
+  color = texture (inTexture, inTexCoord);
 }
diff --git a/gsk/resources/vulkan/blend.vert.glsl b/gsk/resources/vulkan/blend.vert.glsl
new file mode 100644
index 0000000..30c1474
--- /dev/null
+++ b/gsk/resources/vulkan/blend.vert.glsl
@@ -0,0 +1,31 @@
+#version 420 core
+
+layout(location = 0) in vec4 inRect;
+layout(location = 1) in vec4 inTexRect;
+
+layout(push_constant) uniform PushConstants {
+    mat4 mvp;
+    vec4 clip_bounds;
+    vec4 clip_widths;
+    vec4 clip_heights;
+} push;
+
+layout(location = 0) out vec2 outTexCoord;
+
+out gl_PerVertex {
+  vec4 gl_Position;
+};
+
+vec2 offsets[6] = { vec2(0.0, 0.0),
+                    vec2(1.0, 0.0),
+                    vec2(0.0, 1.0),
+                    vec2(0.0, 1.0),
+                    vec2(1.0, 0.0),
+                    vec2(1.0, 1.0) };
+
+void main() {
+  vec2 pos = inRect.xy + inRect.zw * offsets[gl_VertexIndex];
+  gl_Position = push.mvp * vec4 (pos, 0.0, 1.0);
+
+  outTexCoord = inTexRect.xy + inTexRect.zw * offsets[gl_VertexIndex];
+}
diff --git a/gsk/resources/vulkan/blend.vert.spv b/gsk/resources/vulkan/blend.vert.spv
new file mode 100644
index 0000000..dafd915
Binary files /dev/null and b/gsk/resources/vulkan/blend.vert.spv differ


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]