Compare commits

...

1 Commits

Author SHA1 Message Date
Matthias Clasen
baf90f0c04 A somewhat working blur shader
This is a very direct implementation of a Gaussian blur,
without any optimizations.
2017-09-04 22:33:27 -04:00
17 changed files with 383 additions and 0 deletions

129
gsk/gskvulkanblurpipeline.c Normal file
View File

@@ -0,0 +1,129 @@
#include "config.h"
#include "gskvulkanblurpipelineprivate.h"
struct _GskVulkanBlurPipeline
{
GObject parent_instance;
};
typedef struct _GskVulkanBlurInstance GskVulkanBlurInstance;
struct _GskVulkanBlurInstance
{
float rect[4];
float tex_rect[4];
float blur_radius;
};
G_DEFINE_TYPE (GskVulkanBlurPipeline, gsk_vulkan_blur_pipeline, GSK_TYPE_VULKAN_PIPELINE)
static const VkPipelineVertexInputStateCreateInfo *
gsk_vulkan_blur_pipeline_get_input_state_create_info (GskVulkanPipeline *self)
{
static const VkVertexInputBindingDescription vertexBindingDescriptions[] = {
{
.binding = 0,
.stride = sizeof (GskVulkanBlurInstance),
.inputRate = VK_VERTEX_INPUT_RATE_INSTANCE
}
};
static const VkVertexInputAttributeDescription vertexInputAttributeDescription[] = {
{
.location = 0,
.binding = 0,
.format = VK_FORMAT_R32G32B32A32_SFLOAT,
.offset = 0,
},
{
.location = 1,
.binding = 0,
.format = VK_FORMAT_R32G32B32A32_SFLOAT,
.offset = G_STRUCT_OFFSET (GskVulkanBlurInstance, tex_rect),
},
{
.location = 2,
.binding = 0,
.format = VK_FORMAT_R32_SFLOAT,
.offset = G_STRUCT_OFFSET (GskVulkanBlurInstance, blur_radius),
}
};
static const VkPipelineVertexInputStateCreateInfo info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
.vertexBindingDescriptionCount = G_N_ELEMENTS (vertexBindingDescriptions),
.pVertexBindingDescriptions = vertexBindingDescriptions,
.vertexAttributeDescriptionCount = G_N_ELEMENTS (vertexInputAttributeDescription),
.pVertexAttributeDescriptions = vertexInputAttributeDescription
};
return &info;
}
static void
gsk_vulkan_blur_pipeline_finalize (GObject *gobject)
{
//GskVulkanBlurPipeline *self = GSK_VULKAN_BLUR_PIPELINE (gobject);
G_OBJECT_CLASS (gsk_vulkan_blur_pipeline_parent_class)->finalize (gobject);
}
static void
gsk_vulkan_blur_pipeline_class_init (GskVulkanBlurPipelineClass *klass)
{
GskVulkanPipelineClass *pipeline_class = GSK_VULKAN_PIPELINE_CLASS (klass);
G_OBJECT_CLASS (klass)->finalize = gsk_vulkan_blur_pipeline_finalize;
pipeline_class->get_input_state_create_info = gsk_vulkan_blur_pipeline_get_input_state_create_info;
}
static void
gsk_vulkan_blur_pipeline_init (GskVulkanBlurPipeline *self)
{
}
GskVulkanPipeline *
gsk_vulkan_blur_pipeline_new (GskVulkanPipelineLayout *layout,
const char *shader_name,
VkRenderPass render_pass)
{
return gsk_vulkan_pipeline_new (GSK_TYPE_VULKAN_BLUR_PIPELINE, layout, shader_name, render_pass);
}
gsize
gsk_vulkan_blur_pipeline_count_vertex_data (GskVulkanBlurPipeline *pipeline)
{
return sizeof (GskVulkanBlurInstance);
}
void
gsk_vulkan_blur_pipeline_collect_vertex_data (GskVulkanBlurPipeline *pipeline,
guchar *data,
const graphene_rect_t *rect,
double blur_radius)
{
GskVulkanBlurInstance *instance = (GskVulkanBlurInstance *) 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;
instance->blur_radius = blur_radius;
}
gsize
gsk_vulkan_blur_pipeline_draw (GskVulkanBlurPipeline *pipeline,
VkCommandBuffer command_buffer,
gsize offset,
gsize n_commands)
{
vkCmdDraw (command_buffer,
6, n_commands,
0, offset);
return n_commands;
}

View File

@@ -0,0 +1,32 @@
#ifndef __GSK_VULKAN_BLUR_PIPELINE_PRIVATE_H__
#define __GSK_VULKAN_BLUR_PIPELINE_PRIVATE_H__
#include <graphene.h>
#include "gskvulkanpipelineprivate.h"
G_BEGIN_DECLS
typedef struct _GskVulkanBlurPipelineLayout GskVulkanBlurPipelineLayout;
#define GSK_TYPE_VULKAN_BLUR_PIPELINE (gsk_vulkan_blur_pipeline_get_type ())
G_DECLARE_FINAL_TYPE (GskVulkanBlurPipeline, gsk_vulkan_blur_pipeline, GSK, VULKAN_BLUR_PIPELINE, GskVulkanPipeline)
GskVulkanPipeline * gsk_vulkan_blur_pipeline_new (GskVulkanPipelineLayout *layout,
const char *shader_name,
VkRenderPass render_pass);
gsize gsk_vulkan_blur_pipeline_count_vertex_data (GskVulkanBlurPipeline *pipeline);
void gsk_vulkan_blur_pipeline_collect_vertex_data (GskVulkanBlurPipeline *pipeline,
guchar *data,
const graphene_rect_t *rect,
double radius);
gsize gsk_vulkan_blur_pipeline_draw (GskVulkanBlurPipeline *pipeline,
VkCommandBuffer command_buffer,
gsize offset,
gsize n_commands);
G_END_DECLS
#endif /* __GSK_VULKAN_BLUR_PIPELINE_PRIVATE_H__ */

View File

@@ -11,6 +11,7 @@
#include "gskvulkanrenderpassprivate.h"
#include "gskvulkanblendpipelineprivate.h"
#include "gskvulkanblurpipelineprivate.h"
#include "gskvulkanborderpipelineprivate.h"
#include "gskvulkanboxshadowpipelineprivate.h"
#include "gskvulkancolorpipelineprivate.h"
@@ -340,6 +341,9 @@ gsk_vulkan_render_get_pipeline (GskVulkanRender *self,
{ "outset-shadow", gsk_vulkan_box_shadow_pipeline_new },
{ "outset-shadow-clip", gsk_vulkan_box_shadow_pipeline_new },
{ "outset-shadow-clip-rounded", gsk_vulkan_box_shadow_pipeline_new },
{ "blur", gsk_vulkan_blur_pipeline_new },
{ "blur-clip", gsk_vulkan_blur_pipeline_new },
{ "blur-clip-rounded", gsk_vulkan_blur_pipeline_new },
};
g_return_val_if_fail (type < GSK_VULKAN_N_PIPELINES, NULL);

View File

@@ -7,6 +7,7 @@
#include "gskrenderer.h"
#include "gskroundedrectprivate.h"
#include "gskvulkanblendpipelineprivate.h"
#include "gskvulkanblurpipelineprivate.h"
#include "gskvulkanborderpipelineprivate.h"
#include "gskvulkanboxshadowpipelineprivate.h"
#include "gskvulkanclipprivate.h"
@@ -31,6 +32,7 @@ typedef enum {
GSK_VULKAN_OP_COLOR,
GSK_VULKAN_OP_LINEAR_GRADIENT,
GSK_VULKAN_OP_OPACITY,
GSK_VULKAN_OP_BLUR,
GSK_VULKAN_OP_COLOR_MATRIX,
GSK_VULKAN_OP_BORDER,
GSK_VULKAN_OP_INSET_SHADOW,
@@ -231,6 +233,20 @@ gsk_vulkan_render_pass_add_node (GskVulkanRenderPass *self,
g_array_append_val (self->render_ops, op);
return;
case GSK_BLUR_NODE:
if (gsk_vulkan_clip_contains_rect (&constants->clip, &node->bounds))
pipeline_type = GSK_VULKAN_PIPELINE_BLUR;
else if (constants->clip.type == GSK_VULKAN_CLIP_RECT)
pipeline_type = GSK_VULKAN_PIPELINE_BLUR_CLIP;
else if (constants->clip.type == GSK_VULKAN_CLIP_ROUNDED_CIRCULAR)
pipeline_type = GSK_VULKAN_PIPELINE_BLUR_CLIP_ROUNDED;
else
FALLBACK ("Blur nodes can't deal with clip type %u\n", constants->clip.type);
op.type = GSK_VULKAN_OP_BLUR;
op.render.pipeline = gsk_vulkan_render_get_pipeline (render, pipeline_type);
g_array_append_val (self->render_ops, op);
return;
case GSK_COLOR_MATRIX_NODE:
if (gsk_vulkan_clip_contains_rect (&constants->clip, &node->bounds))
pipeline_type = GSK_VULKAN_PIPELINE_COLOR_MATRIX;
@@ -543,6 +559,18 @@ gsk_vulkan_render_pass_upload (GskVulkanRenderPass *self,
}
break;
case GSK_VULKAN_OP_BLUR:
{
GskRenderNode *child = gsk_blur_node_get_child (op->render.node);
op->render.source = gsk_vulkan_render_pass_get_node_as_texture (self,
render,
uploader,
child,
&child->bounds);
}
break;
case GSK_VULKAN_OP_COLOR_MATRIX:
{
GskRenderNode *child = gsk_color_matrix_node_get_child (op->render.node);
@@ -607,6 +635,11 @@ gsk_vulkan_render_pass_count_vertex_data (GskVulkanRenderPass *self)
n_bytes += op->render.vertex_count;
break;
case GSK_VULKAN_OP_BLUR:
op->render.vertex_count = gsk_vulkan_blur_pipeline_count_vertex_data (GSK_VULKAN_BLUR_PIPELINE (op->render.pipeline));
n_bytes += op->render.vertex_count;
break;
case GSK_VULKAN_OP_BORDER:
op->render.vertex_count = gsk_vulkan_border_pipeline_count_vertex_data (GSK_VULKAN_BORDER_PIPELINE (op->render.pipeline));
n_bytes += op->render.vertex_count;
@@ -707,6 +740,17 @@ gsk_vulkan_render_pass_collect_vertex_data (GskVulkanRenderPass *self,
}
break;
case GSK_VULKAN_OP_BLUR:
{
op->render.vertex_offset = offset + n_bytes;
gsk_vulkan_blur_pipeline_collect_vertex_data (GSK_VULKAN_BLUR_PIPELINE (op->render.pipeline),
data + n_bytes + offset,
&op->render.node->bounds,
gsk_blur_node_get_radius (op->render.node));
n_bytes += op->render.vertex_count;
}
break;
case GSK_VULKAN_OP_COLOR_MATRIX:
{
op->render.vertex_offset = offset + n_bytes;
@@ -792,6 +836,7 @@ gsk_vulkan_render_pass_reserve_descriptor_sets (GskVulkanRenderPass *self,
case GSK_VULKAN_OP_SURFACE:
case GSK_VULKAN_OP_TEXTURE:
case GSK_VULKAN_OP_OPACITY:
case GSK_VULKAN_OP_BLUR:
case GSK_VULKAN_OP_COLOR_MATRIX:
op->render.descriptor_set_index = gsk_vulkan_render_reserve_descriptor_set (render, op->render.source);
break;
@@ -899,6 +944,39 @@ gsk_vulkan_render_pass_draw (GskVulkanRenderPass *self,
current_draw_index, 1);
break;
case GSK_VULKAN_OP_BLUR:
if (current_pipeline != op->render.pipeline)
{
current_pipeline = op->render.pipeline;
vkCmdBindPipeline (command_buffer,
VK_PIPELINE_BIND_POINT_GRAPHICS,
gsk_vulkan_pipeline_get_pipeline (current_pipeline));
vkCmdBindVertexBuffers (command_buffer,
0,
1,
(VkBuffer[1]) {
gsk_vulkan_buffer_get_buffer (vertex_buffer)
},
(VkDeviceSize[1]) { op->render.vertex_offset });
current_draw_index = 0;
}
vkCmdBindDescriptorSets (command_buffer,
VK_PIPELINE_BIND_POINT_GRAPHICS,
gsk_vulkan_pipeline_layout_get_pipeline_layout (layout),
0,
1,
(VkDescriptorSet[1]) {
gsk_vulkan_render_get_descriptor_set (render, op->render.descriptor_set_index)
},
0,
NULL);
current_draw_index += gsk_vulkan_blur_pipeline_draw (GSK_VULKAN_BLUR_PIPELINE (current_pipeline),
command_buffer,
current_draw_index, 1);
break;
case GSK_VULKAN_OP_COLOR:
if (current_pipeline != op->render.pipeline)
{

View File

@@ -31,6 +31,9 @@ typedef enum {
GSK_VULKAN_PIPELINE_OUTSET_SHADOW,
GSK_VULKAN_PIPELINE_OUTSET_SHADOW_CLIP,
GSK_VULKAN_PIPELINE_OUTSET_SHADOW_CLIP_ROUNDED,
GSK_VULKAN_PIPELINE_BLUR,
GSK_VULKAN_PIPELINE_BLUR_CLIP,
GSK_VULKAN_PIPELINE_BLUR_CLIP_ROUNDED,
/* add more */
GSK_VULKAN_N_PIPELINES
} GskVulkanPipelineType;

View File

@@ -50,6 +50,7 @@ gsk_private_vulkan_compiled_shaders = []
if have_vulkan
gsk_private_sources += files([
'gskvulkanblendpipeline.c',
'gskvulkanblurpipeline.c',
'gskvulkanborderpipeline.c',
'gskvulkanboxshadowpipeline.c',
'gskvulkanbuffer.c',

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,50 @@
#version 420 core
#include "clip.frag.glsl"
layout(location = 0) in vec2 inPos;
layout(location = 1) in flat vec2 inSize;
layout(location = 2) in vec2 inTexCoord;
layout(location = 3) in float inRadius;
layout(set = 0, binding = 0) uniform sampler2D inTexture;
layout(location = 0) out vec4 color;
const int samples_x = 15; // must be odd
const int samples_y = 15; // must be odd
const int half_samples_x = samples_x / 2;
const int half_samples_y = samples_y / 2;
float Gaussian (float sigma, float x)
{
return exp ( - (x * x) / (2.0 * sigma * sigma));
}
vec4 blur_pixel (in vec2 uv)
{
float total = 0.0;
vec4 ret = vec4 (0);
float pixel_size_x = (1.0 / inSize.x);
float pixel_size_y = (1.0 / inSize.y);
for (int y = 0; y < samples_y; ++y)
{
float fy = Gaussian (inRadius, float(y) - float(half_samples_x));
float offset_y = float(y - half_samples_y) * pixel_size_y;
for (int x = 0; x < samples_x; ++x)
{
float fx = Gaussian (inRadius, float(x) - float(half_samples_x));
float offset_x = float(x - half_samples_x) * pixel_size_x;
total += fx * fy;
ret += texture(inTexture, uv + vec2(offset_x, offset_y)) * fx * fy;
}
}
return ret / total;
}
void main()
{
color = clip (inPos, blur_pixel (inTexCoord));
}

View File

@@ -0,0 +1,13 @@
#version 420 core
#include "clip.frag.glsl"
layout(location = 0) in vec2 inPos;
layout(location = 1) in float inRadius;
layout(location = 0) out vec4 color;
void main()
{
color = clip (inPos, vec4(1, 0, 0, 0));
}

Binary file not shown.

View File

@@ -0,0 +1,41 @@
#version 420 core
#include "clip.vert.glsl"
layout(location = 0) in vec4 inRect;
layout(location = 1) in vec4 inTexRect;
layout(location = 2) in float inRadius;
layout(location = 0) out vec2 outPos;
layout(location = 1) out flat vec2 outSize;
layout(location = 2) out vec2 outTexCoord;
layout(location = 3) out flat float outRadius;
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() {
vec4 rect = clip (inRect);
vec2 pos = rect.xy + rect.zw * offsets[gl_VertexIndex];
gl_Position = push.mvp * vec4 (pos, 0.0, 1.0);
outPos = pos;
outSize = inRect.zw;
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];
outRadius = inRadius;
}

View File

@@ -0,0 +1,30 @@
#version 420 core
#include "clip.vert.glsl"
layout(location = 0) in vec4 inRect;
layout(location = 1) in float inRadius;
layout(location = 0) out vec2 outPos;
layout(location = 1) out flat float outRadius;
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() {
vec4 rect = clip (inRect);
vec2 pos = rect.xy + rect.zw * offsets[gl_VertexIndex];
gl_Position = push.mvp * vec4 (pos, 0.0, 1.0);
outPos = pos;
outRadius = inRadius;
}

Binary file not shown.

View File

@@ -8,6 +8,7 @@
gsk_private_vulkan_fragment_shaders = [
'blend.frag',
'blur.frag',
'border.frag',
'color.frag',
'color-matrix.frag',
@@ -18,6 +19,7 @@ gsk_private_vulkan_fragment_shaders = [
gsk_private_vulkan_vertex_shaders = [
'blend.vert',
'blur.vert',
'border.vert',
'color.vert',
'color-matrix.vert',