vulkan: Convert blend shader
authorBenjamin Otte <otte@redhat.com>
Sun, 21 May 2023 02:42:02 +0000 (04:42 +0200)
committerBenjamin Otte <otte@redhat.com>
Sun, 4 Jun 2023 17:42:01 +0000 (19:42 +0200)
Same work as crossfade shader pretty much.

gsk/vulkan/gskvulkanblendmodepipeline.c
gsk/vulkan/gskvulkanblendmodepipelineprivate.h
gsk/vulkan/gskvulkanrenderpass.c
gsk/vulkan/resources/blendmode.frag
gsk/vulkan/resources/blendmode.vert

index e9ea9c2dc701300fd083a60c392336deb3d4aebd..01e9ad214da1a0bb439b63898586c8b2df2d52da 100644 (file)
@@ -12,10 +12,12 @@ typedef struct _GskVulkanBlendModeInstance GskVulkanBlendModeInstance;
 struct _GskVulkanBlendModeInstance
 {
   float rect[4];
-  float start_tex_rect[4];
-  float end_tex_rect[4];
-  guint32 start_tex_id;
-  guint32 end_tex_id;
+  float top_rect[4];
+  float bottom_rect[4];
+  float top_tex_rect[4];
+  float bottom_tex_rect[4];
+  guint32 top_tex_id;
+  guint32 bottom_tex_id;
   guint32 blend_mode;
 };
 
@@ -42,30 +44,42 @@ gsk_vulkan_blend_mode_pipeline_get_input_state_create_info (GskVulkanPipeline *s
           .location = 1,
           .binding = 0,
           .format = VK_FORMAT_R32G32B32A32_SFLOAT,
-          .offset = G_STRUCT_OFFSET (GskVulkanBlendModeInstance, start_tex_rect),
+          .offset = G_STRUCT_OFFSET (GskVulkanBlendModeInstance, top_rect),
       },
       {
           .location = 2,
           .binding = 0,
           .format = VK_FORMAT_R32G32B32A32_SFLOAT,
-          .offset = G_STRUCT_OFFSET (GskVulkanBlendModeInstance, end_tex_rect),
+          .offset = G_STRUCT_OFFSET (GskVulkanBlendModeInstance, bottom_rect),
       },
       {
           .location = 3,
           .binding = 0,
-          .format = VK_FORMAT_R32_UINT,
-          .offset = G_STRUCT_OFFSET (GskVulkanBlendModeInstance, start_tex_id),
+          .format = VK_FORMAT_R32G32B32A32_SFLOAT,
+          .offset = G_STRUCT_OFFSET (GskVulkanBlendModeInstance, top_tex_rect),
       },
       {
           .location = 4,
           .binding = 0,
-          .format = VK_FORMAT_R32_UINT,
-          .offset = G_STRUCT_OFFSET (GskVulkanBlendModeInstance, end_tex_id),
+          .format = VK_FORMAT_R32G32B32A32_SFLOAT,
+          .offset = G_STRUCT_OFFSET (GskVulkanBlendModeInstance, bottom_tex_rect),
       },
       {
           .location = 5,
           .binding = 0,
           .format = VK_FORMAT_R32_UINT,
+          .offset = G_STRUCT_OFFSET (GskVulkanBlendModeInstance, top_tex_id),
+      },
+      {
+          .location = 6,
+          .binding = 0,
+          .format = VK_FORMAT_R32_UINT,
+          .offset = G_STRUCT_OFFSET (GskVulkanBlendModeInstance, bottom_tex_id),
+      },
+      {
+          .location = 7,
+          .binding = 0,
+          .format = VK_FORMAT_R32_UINT,
           .offset = G_STRUCT_OFFSET (GskVulkanBlendModeInstance, blend_mode),
       }
   };
@@ -115,12 +129,14 @@ gsk_vulkan_blend_mode_pipeline_new (GdkVulkanContext        *context,
 void
 gsk_vulkan_blend_mode_pipeline_collect_vertex_data (GskVulkanBlendModePipeline *pipeline,
                                                     guchar                     *data,
-                                                    guint32                     start_tex_id,
-                                                    guint32                     end_tex_id,
+                                                    guint32                     top_tex_id,
+                                                    guint32                     bottom_tex_id,
                                                     const graphene_point_t     *offset,
                                                     const graphene_rect_t      *bounds,
-                                                    const graphene_rect_t      *start_tex_rect,
-                                                    const graphene_rect_t      *end_tex_rect,
+                                                    const graphene_rect_t      *top_bounds,
+                                                    const graphene_rect_t      *bottom_bounds,
+                                                    const graphene_rect_t      *top_tex_rect,
+                                                    const graphene_rect_t      *bottom_tex_rect,
                                                     GskBlendMode                blend_mode)
 {
   GskVulkanBlendModeInstance *instance = (GskVulkanBlendModeInstance *) data;
@@ -130,18 +146,28 @@ gsk_vulkan_blend_mode_pipeline_collect_vertex_data (GskVulkanBlendModePipeline *
   instance->rect[2] = bounds->size.width;
   instance->rect[3] = bounds->size.height;
 
-  instance->start_tex_rect[0] = start_tex_rect->origin.x;
-  instance->start_tex_rect[1] = start_tex_rect->origin.y;
-  instance->start_tex_rect[2] = start_tex_rect->size.width;
-  instance->start_tex_rect[3] = start_tex_rect->size.height;
+  instance->top_rect[0] = top_bounds->origin.x + offset->x;
+  instance->top_rect[1] = top_bounds->origin.y + offset->y;
+  instance->top_rect[2] = top_bounds->size.width;
+  instance->top_rect[3] = top_bounds->size.height;
+
+  instance->bottom_rect[0] = bottom_bounds->origin.x + offset->x;
+  instance->bottom_rect[1] = bottom_bounds->origin.y + offset->y;
+  instance->bottom_rect[2] = bottom_bounds->size.width;
+  instance->bottom_rect[3] = bottom_bounds->size.height;
+
+  instance->top_tex_rect[0] = top_tex_rect->origin.x;
+  instance->top_tex_rect[1] = top_tex_rect->origin.y;
+  instance->top_tex_rect[2] = top_tex_rect->size.width;
+  instance->top_tex_rect[3] = top_tex_rect->size.height;
 
-  instance->end_tex_rect[0] = end_tex_rect->origin.x;
-  instance->end_tex_rect[1] = end_tex_rect->origin.y;
-  instance->end_tex_rect[2] = end_tex_rect->size.width;
-  instance->end_tex_rect[3] = end_tex_rect->size.height;
+  instance->bottom_tex_rect[0] = bottom_tex_rect->origin.x;
+  instance->bottom_tex_rect[1] = bottom_tex_rect->origin.y;
+  instance->bottom_tex_rect[2] = bottom_tex_rect->size.width;
+  instance->bottom_tex_rect[3] = bottom_tex_rect->size.height;
 
-  instance->start_tex_id = start_tex_id;
-  instance->end_tex_id = end_tex_id;
+  instance->top_tex_id = top_tex_id;
+  instance->bottom_tex_id = bottom_tex_id;
   instance->blend_mode = blend_mode;
 }
 
index 94d565d9752dd59e490f6fb6d1c80509bb41771e..e06f28ae90252b5e03eb06775516037f2773c254 100644 (file)
@@ -20,12 +20,14 @@ GskVulkanPipeline * gsk_vulkan_blend_mode_pipeline_new                 (GdkVulka
 
 void                gsk_vulkan_blend_mode_pipeline_collect_vertex_data (GskVulkanBlendModePipeline *pipeline,
                                                                         guchar                     *data,
-                                                                        guint32                     start_tex_id,
-                                                                        guint32                     end_tex_id,
+                                                                        guint32                     top_tex_id,
+                                                                        guint32                     bottom_tex_id,
                                                                         const graphene_point_t     *offset,
                                                                         const graphene_rect_t      *bounds,
-                                                                        const graphene_rect_t      *start_bounds,
-                                                                        const graphene_rect_t      *end_bounds,
+                                                                        const graphene_rect_t      *top_bounds,
+                                                                        const graphene_rect_t      *bottom_bounds,
+                                                                        const graphene_rect_t      *top_tex_rect,
+                                                                        const graphene_rect_t      *bottom_tex_rect,
                                                                         GskBlendMode                blend_mode);
 gsize               gsk_vulkan_blend_mode_pipeline_draw                (GskVulkanBlendModePipeline *pipeline,
                                                                         VkCommandBuffer             command_buffer,
index 2c43de901514c9b397dda0b95d416fd71f6e9657..7ce07858f3c2ae72d002cd808d594835d518de65 100644 (file)
@@ -1970,6 +1970,8 @@ gsk_vulkan_render_pass_collect_vertex_data (GskVulkanRenderPass *self,
                                                               op->render.descriptor_set_index2,
                                                               &op->render.offset,
                                                               &op->render.node->bounds,
+                                                              &gsk_blend_node_get_top_child (op->render.node)->bounds,
+                                                              &gsk_blend_node_get_bottom_child (op->render.node)->bounds,
                                                               &op->render.source_rect,
                                                               &op->render.source2_rect,
                                                               gsk_blend_node_get_blend_mode (op->render.node));
index ad1d9c98b31bdd7cb53ad9bf69f1e3e089e4b2ff..56ae22e7a3533c30687b52d617493a917f7c67c9 100644 (file)
@@ -2,15 +2,18 @@
 
 #include "common.frag.glsl"
 #include "clip.frag.glsl"
+#include "rect.frag.glsl"
 
 layout(location = 0) in vec2 inPos;
-layout(location = 1) in vec2 inStartTexCoord;
-layout(location = 2) in vec2 inEndTexCoord;
-layout(location = 3) flat in uint inStartTexId;
-layout(location = 4) flat in uint inEndTexId;
-layout(location = 5) flat in uint inBlendMode;
+layout(location = 1) in Rect inTopRect;
+layout(location = 2) in Rect inBottomRect;
+layout(location = 3) in vec2 inTopTexCoord;
+layout(location = 4) in vec2 inBottomTexCoord;
+layout(location = 5) flat in uint inTopTexId;
+layout(location = 6) flat in uint inBottomTexId;
+layout(location = 7) flat in uint inBlendMode;
 
-layout(location = 0) out vec4 outColor;
+layout(location = 0) out vec4 color;
 
 float
 combine (float source, float backdrop)
@@ -242,7 +245,7 @@ set_sat (vec3 c, float s)
 }
 
 vec4
-color (vec4 Cs, vec4 Cb)
+colorize (vec4 Cs, vec4 Cb)
 {
   vec3 B = set_lum (Cs.rgb, lum (Cb.rgb));
   return composite (Cs, Cb, B);
@@ -271,8 +274,12 @@ luminosity (vec4 Cs, vec4 Cb)
 
 void main()
 {
-  vec4 source = texture (textures[inStartTexId], inStartTexCoord);
-  vec4 backdrop = texture (textures[inEndTexId], inEndTexCoord);
+  float source_alpha = rect_coverage (inTopRect, inPos);
+  vec4 source = texture (textures[inTopTexId], inTopTexCoord) * source_alpha;
+  //source=vec4(1); //inTopTexCoord * 100 + 100) / 255, 0, 1);
+  float backdrop_alpha = rect_coverage (inBottomRect, inPos);
+  vec4 backdrop = texture (textures[inBottomTexId], inBottomTexCoord) * backdrop_alpha;
+  //backdrop=vec4(1, 0, 0, 1) * backdrop_alpha;
   vec4 result;
 
   if (inBlendMode == 0)
@@ -300,7 +307,7 @@ void main()
   else if (inBlendMode == 11)
     result = exclusion (source, backdrop);
   else if (inBlendMode == 12)
-    result = color (source, backdrop);
+    result = colorize (source, backdrop);
   else if (inBlendMode == 13)
     result = hue (source, backdrop);
   else if (inBlendMode == 14)
@@ -310,5 +317,5 @@ void main()
   else
     discard;
 
-  outColor = clip (inPos, result);
+  color = clip_scaled (inPos, result);
 }
index e0c13a5fe22bf1b13fa5853e6b89b4dfd14b0cde..338d4900e6c206dd302562eb42c5f86dcfb4fdf1 100644 (file)
@@ -1,45 +1,36 @@
 #version 420 core
 
-#include "clip.vert.glsl"
+#include "common.vert.glsl"
+#include "rect.vert.glsl"
 
 layout(location = 0) in vec4 inRect;
-layout(location = 1) in vec4 inStartTexRect;
-layout(location = 2) in vec4 inEndTexRect;
-layout(location = 3) in uint inStartTexId;
-layout(location = 4) in uint inEndTexId;
-layout(location = 5) in uint inBlendMode;
+layout(location = 1) in vec4 inTopRect;
+layout(location = 2) in vec4 inBottomRect;
+layout(location = 3) in vec4 inTopTexRect;
+layout(location = 4) in vec4 inBottomTexRect;
+layout(location = 5) in uint inTopTexId;
+layout(location = 6) in uint inBottomTexId;
+layout(location = 7) in uint inBlendMode;
 
 layout(location = 0) out vec2 outPos;
-layout(location = 1) out vec2 outStartTexCoord;
-layout(location = 2) out vec2 outEndTexCoord;
-layout(location = 3) flat out uint outStartTexId;
-layout(location = 4) flat out uint outEndTexId;
-layout(location = 5) flat out uint outBlendMode;
-
-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) };
+layout(location = 1) flat out Rect outTopRect;
+layout(location = 2) flat out Rect outBottomRect;
+layout(location = 3) out vec2 outTopTexCoord;
+layout(location = 4) out vec2 outBottomTexCoord;
+layout(location = 5) flat out uint outTopTexId;
+layout(location = 6) flat out uint outBottomTexId;
+layout(location = 7) flat out uint outBlendMode;
 
 void main() {
-  vec4 rect = clip (inRect);
-  vec2 pos = rect.xy + rect.zw * offsets[gl_VertexIndex];
-  gl_Position = push.mvp * vec4 (push.scale * pos, 0.0, 1.0);
+  Rect r = rect_from_gsk (inRect);
+  vec2 pos = set_position_from_rect (r);
 
   outPos = pos;
-
-  vec4 texrect = vec4((rect.xy - inRect.xy) / inRect.zw,
-                      rect.zw / inRect.zw);
-  vec4 starttexrect = vec4(inStartTexRect.xy + inStartTexRect.zw * texrect.xy,
-                           inStartTexRect.zw * texrect.zw);
-  vec4 endtexrect = vec4(inEndTexRect.xy + inEndTexRect.zw * texrect.xy,
-                         inEndTexRect.zw * texrect.zw);
-
-  outStartTexCoord = starttexrect.xy + starttexrect.zw * offsets[gl_VertexIndex];
-  outEndTexCoord = endtexrect.xy + endtexrect.zw * offsets[gl_VertexIndex];
-  outStartTexId = inStartTexId;
-  outEndTexId = inEndTexId;
+  outTopRect = rect_from_gsk (inTopRect);
+  outBottomRect = rect_from_gsk (inBottomRect);
+  outTopTexCoord = scale_tex_coord (pos, r, inTopTexRect);
+  outBottomTexCoord = scale_tex_coord (pos, r, inBottomTexRect);
+  outTopTexId = inTopTexId;
+  outBottomTexId = inBottomTexId;
   outBlendMode = inBlendMode;
 }