mirror of
https://gitlab.gnome.org/GNOME/gtk.git
synced 2024-12-27 06:00:22 +00:00
vulkan: Modernize blend pipeline
- Rename from blit => blend - Use instances - Add clip versions
This commit is contained in:
parent
2f5737cc1c
commit
f05f0377df
@ -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;
|
||||
GskVulkanBlendInstance *instance = (GskVulkanBlendInstance *) 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 };
|
||||
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;
|
||||
}
|
||||
|
@ -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 },
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
57
gsk/resources/vulkan/blend-clip-rounded.frag.glsl
Normal file
57
gsk/resources/vulkan/blend-clip-rounded.frag.glsl
Normal file
@ -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);
|
||||
}
|
BIN
gsk/resources/vulkan/blend-clip-rounded.frag.spv
Normal file
BIN
gsk/resources/vulkan/blend-clip-rounded.frag.spv
Normal file
Binary file not shown.
53
gsk/resources/vulkan/blend-clip-rounded.vert.glsl
Normal file
53
gsk/resources/vulkan/blend-clip-rounded.vert.glsl
Normal file
@ -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];
|
||||
}
|
BIN
gsk/resources/vulkan/blend-clip-rounded.vert.spv
Normal file
BIN
gsk/resources/vulkan/blend-clip-rounded.vert.spv
Normal file
Binary file not shown.
@ -8,5 +8,5 @@ layout(location = 0) out vec4 color;
|
||||
|
||||
void main()
|
||||
{
|
||||
color = texture (inTexture, inTexCoord);
|
||||
color = texture (inTexture, inTexCoord);
|
||||
}
|
46
gsk/resources/vulkan/blend-clip.vert.glsl
Normal file
46
gsk/resources/vulkan/blend-clip.vert.glsl
Normal file
@ -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];
|
||||
}
|
BIN
gsk/resources/vulkan/blend-clip.vert.spv
Normal file
BIN
gsk/resources/vulkan/blend-clip.vert.spv
Normal file
Binary file not shown.
12
gsk/resources/vulkan/blend.frag.glsl
Normal file
12
gsk/resources/vulkan/blend.frag.glsl
Normal file
@ -0,0 +1,12 @@
|
||||
#version 420 core
|
||||
|
||||
layout(location = 0) in vec2 inTexCoord;
|
||||
|
||||
layout(set = 0, binding = 0) uniform sampler2D inTexture;
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
|
||||
void main()
|
||||
{
|
||||
color = texture (inTexture, inTexCoord);
|
||||
}
|
BIN
gsk/resources/vulkan/blend.frag.spv
Normal file
BIN
gsk/resources/vulkan/blend.frag.spv
Normal file
Binary file not shown.
31
gsk/resources/vulkan/blend.vert.glsl
Normal file
31
gsk/resources/vulkan/blend.vert.glsl
Normal file
@ -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];
|
||||
}
|
BIN
gsk/resources/vulkan/blend.vert.spv
Normal file
BIN
gsk/resources/vulkan/blend.vert.spv
Normal file
Binary file not shown.
@ -1,22 +0,0 @@
|
||||
#version 420 core
|
||||
|
||||
layout(location = 0) in vec2 inPosition;
|
||||
layout(location = 1) in vec2 inTexCoord;
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
void main() {
|
||||
gl_Position = push.mvp * vec4 (inPosition, 0.0, 1.0);
|
||||
outTexCoord = inTexCoord;
|
||||
}
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user