[gtk+] vulkan: Pass color rects in instance data



commit b04eecf860e8f6fe2503101d6bc8112c262939d9
Author: Benjamin Otte <otte redhat com>
Date:   Sun Dec 18 06:54:48 2016 +0100

    vulkan: Pass color rects in instance data
    
    This way, we don't need push constants or per-vertex data, we can render
    colored rectangles completely via instance data.

 gsk/gskvulkancolorpipeline.c         |   49 ++++++++++++++++++++--------------
 gsk/gskvulkancolorpipelineprivate.h  |    3 +-
 gsk/gskvulkanrenderpass.c            |    3 +-
 gsk/resources/vulkan/color.frag.glsl |    4 +-
 gsk/resources/vulkan/color.frag.spv  |  Bin 804 -> 672 bytes
 gsk/resources/vulkan/color.vert.glsl |   16 +++++++++-
 gsk/resources/vulkan/color.vert.spv  |  Bin 948 -> 1632 bytes
 7 files changed, 49 insertions(+), 26 deletions(-)
---
diff --git a/gsk/gskvulkancolorpipeline.c b/gsk/gskvulkancolorpipeline.c
index 6702537..ab6c3e5 100644
--- a/gsk/gskvulkancolorpipeline.c
+++ b/gsk/gskvulkancolorpipeline.c
@@ -7,12 +7,12 @@ struct _GskVulkanColorPipeline
   GObject parent_instance;
 };
 
-typedef struct _GskVulkanVertex GskVulkanVertex;
+typedef struct _GskVulkanColorInstance GskVulkanColorInstance;
 
-struct _GskVulkanVertex
+struct _GskVulkanColorInstance
 {
-  float x;
-  float y;
+  float rect[4];
+  float color[4];
 };
 
 G_DEFINE_TYPE (GskVulkanColorPipeline, gsk_vulkan_color_pipeline, GSK_TYPE_VULKAN_PIPELINE)
@@ -23,16 +23,22 @@ gsk_vulkan_color_pipeline_get_input_state_create_info (GskVulkanPipeline *self)
   static const VkVertexInputBindingDescription vertexBindingDescriptions[] = {
       {
           .binding = 0,
-          .stride = sizeof (GskVulkanVertex),
-          .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
+          .stride = sizeof (GskVulkanColorInstance),
+          .inputRate = VK_VERTEX_INPUT_RATE_INSTANCE
       }
   };
   static const VkVertexInputAttributeDescription vertexInputAttributeDescription[] = {
       {
           .location = 0,
           .binding = 0,
-          .format = VK_FORMAT_R32G32_SFLOAT,
+          .format = VK_FORMAT_R32G32B32A32_SFLOAT,
           .offset = 0,
+      },
+      {
+          .location = 1,
+          .binding = 0,
+          .format = VK_FORMAT_R32G32B32A32_SFLOAT,
+          .offset = G_STRUCT_OFFSET (GskVulkanColorInstance, color),
       }
   };
   static const VkPipelineVertexInputStateCreateInfo info = {
@@ -80,22 +86,25 @@ gsk_vulkan_color_pipeline_new (GskVulkanPipelineLayout *layout,
 gsize
 gsk_vulkan_color_pipeline_count_vertex_data (GskVulkanColorPipeline *pipeline)
 {
-  return sizeof (GskVulkanVertex) * 6;
+  return sizeof (GskVulkanColorInstance);
 }
 
 void
 gsk_vulkan_color_pipeline_collect_vertex_data (GskVulkanColorPipeline *pipeline,
                                                guchar                 *data,
-                                               const graphene_rect_t  *rect)
+                                               const graphene_rect_t  *rect,
+                                               const GdkRGBA          *color)
 {
-  GskVulkanVertex *vertices = (GskVulkanVertex *) data;
-
-  vertices[0] = (GskVulkanVertex) { rect->origin.x,                    rect->origin.y };
-  vertices[1] = (GskVulkanVertex) { rect->origin.x + rect->size.width, rect->origin.y };
-  vertices[2] = (GskVulkanVertex) { rect->origin.x,                    rect->origin.y + rect->size.height };
-  vertices[3] = (GskVulkanVertex) { rect->origin.x,                    rect->origin.y + rect->size.height };
-  vertices[4] = (GskVulkanVertex) { rect->origin.x + rect->size.width, rect->origin.y };
-  vertices[5] = (GskVulkanVertex) { rect->origin.x + rect->size.width, rect->origin.y + rect->size.height };
+  GskVulkanColorInstance *instance = (GskVulkanColorInstance *) 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->color[0] = pow (color->red, 2.2);
+  instance->color[1] = pow (color->green, 2.2);
+  instance->color[2] = pow (color->blue, 2.2);
+  instance->color[3] = color->alpha;
 }
 
 gsize
@@ -105,8 +114,8 @@ gsk_vulkan_color_pipeline_draw (GskVulkanColorPipeline *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/gskvulkancolorpipelineprivate.h b/gsk/gskvulkancolorpipelineprivate.h
index 4fbc22a..07e6382 100644
--- a/gsk/gskvulkancolorpipelineprivate.h
+++ b/gsk/gskvulkancolorpipelineprivate.h
@@ -20,7 +20,8 @@ GskVulkanPipeline *     gsk_vulkan_color_pipeline_new                   (GskVulk
 gsize                   gsk_vulkan_color_pipeline_count_vertex_data     (GskVulkanColorPipeline         
*pipeline);
 void                    gsk_vulkan_color_pipeline_collect_vertex_data   (GskVulkanColorPipeline         
*pipeline,
                                                                          guchar                         
*data,
-                                                                         const graphene_rect_t          
*rect);
+                                                                         const graphene_rect_t          
*rect,
+                                                                         const GdkRGBA                  
*color);
 gsize                   gsk_vulkan_color_pipeline_draw                  (GskVulkanColorPipeline         
*pipeline,
                                                                          VkCommandBuffer                 
command_buffer,
                                                                          gsize                           
offset,
diff --git a/gsk/gskvulkanrenderpass.c b/gsk/gskvulkanrenderpass.c
index 337b6ad..fed4e7c 100644
--- a/gsk/gskvulkanrenderpass.c
+++ b/gsk/gskvulkanrenderpass.c
@@ -333,7 +333,8 @@ gsk_vulkan_render_pass_collect_vertex_data (GskVulkanRenderPass *self,
             op->render.vertex_offset = offset + n_bytes;
             gsk_vulkan_color_pipeline_collect_vertex_data (GSK_VULKAN_COLOR_PIPELINE (op->render.pipeline),
                                                            data + n_bytes + offset,
-                                                           &bounds);
+                                                           &bounds,
+                                                           gsk_color_node_peek_color (op->render.node));
             n_bytes += op->render.vertex_count;
           }
           break;
diff --git a/gsk/resources/vulkan/color.frag.glsl b/gsk/resources/vulkan/color.frag.glsl
index fba9e47..cbac7ed 100644
--- a/gsk/resources/vulkan/color.frag.glsl
+++ b/gsk/resources/vulkan/color.frag.glsl
@@ -1,6 +1,6 @@
 #version 420 core
 
-layout(set = 0, binding = 0) uniform sampler2D inTexture;
+layout(location = 0) in vec4 inColor;
 
 layout(push_constant) uniform PushConstants {
     mat4 mvp;
@@ -11,5 +11,5 @@ layout(location = 0) out vec4 color;
 
 void main()
 {
-    color = push.color;
+    color = inColor;
 }
diff --git a/gsk/resources/vulkan/color.frag.spv b/gsk/resources/vulkan/color.frag.spv
index beee876..2865771 100644
Binary files a/gsk/resources/vulkan/color.frag.spv and b/gsk/resources/vulkan/color.frag.spv differ
diff --git a/gsk/resources/vulkan/color.vert.glsl b/gsk/resources/vulkan/color.vert.glsl
index 849d5cd..78bbfe1 100644
--- a/gsk/resources/vulkan/color.vert.glsl
+++ b/gsk/resources/vulkan/color.vert.glsl
@@ -1,15 +1,27 @@
 #version 420 core
 
-layout(location = 0) in vec2 inPosition;
+layout(location = 0) in vec4 inRect;
+layout(location = 1) in vec4 inColor;
 
 layout(push_constant) uniform PushConstants {
     mat4 mvp;
 } push;
 
+layout(location = 0) out vec4 outColor;
+
 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() {
-  gl_Position = push.mvp * vec4 (inPosition, 0.0, 1.0);
+  vec2 pos = inRect.xy + inRect.zw * offsets[gl_VertexIndex];
+  gl_Position = push.mvp * vec4 (pos, 0.0, 1.0);
+  outColor = inColor;
 }
diff --git a/gsk/resources/vulkan/color.vert.spv b/gsk/resources/vulkan/color.vert.spv
index a7ca96a..f4a8791 100644
Binary files a/gsk/resources/vulkan/color.vert.spv and b/gsk/resources/vulkan/color.vert.spv differ


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