Merge branch 'ngl-less-shaders' into 'master'

Assorted ngl improvements

See merge request GNOME/gtk!3295
This commit is contained in:
Matthias Clasen 2021-03-14 21:04:37 +00:00
commit 24ec2cc6a6
22 changed files with 520 additions and 456 deletions

View File

@ -40,6 +40,7 @@ gsk_private_ngl_shaders = [
'ngl/resources/blend.glsl',
'ngl/resources/repeat.glsl',
'ngl/resources/custom.glsl',
'ngl/resources/filled_border.glsl',
]
gsk_public_sources = files([

View File

@ -1010,6 +1010,12 @@ gsk_ngl_command_queue_execute (GskNglCommandQueue *self,
sizeof (GskNglDrawVertex),
(void *) G_STRUCT_OFFSET (GskNglDrawVertex, color));
/* 3 = color2 location */
glEnableVertexAttribArray (3);
glVertexAttribPointer (3, 4, GL_FLOAT, GL_FALSE,
sizeof (GskNglDrawVertex),
(void *) G_STRUCT_OFFSET (GskNglDrawVertex, color2));
/* Setup initial scissor clip */
if (scissor != NULL)
{

View File

@ -348,6 +348,7 @@ gsk_ngl_driver_load_programs (GskNglDriver *self,
gsk_ngl_compiler_bind_attribute (compiler, "aPosition", 0);
gsk_ngl_compiler_bind_attribute (compiler, "aUv", 1);
gsk_ngl_compiler_bind_attribute (compiler, "aColor", 2);
gsk_ngl_compiler_bind_attribute (compiler, "aColor2", 3);
/* Use XMacros to register all of our programs and their uniforms */
#define GSK_NGL_NO_UNIFORMS
@ -1046,6 +1047,7 @@ gsk_ngl_driver_lookup_shader (GskNglDriver *self,
gsk_ngl_compiler_bind_attribute (compiler, "aPosition", 0);
gsk_ngl_compiler_bind_attribute (compiler, "aUv", 1);
gsk_ngl_compiler_bind_attribute (compiler, "aColor", 2);
gsk_ngl_compiler_bind_attribute (compiler, "aColor2", 3);
if ((program = gsk_ngl_compiler_compile (compiler, NULL, "", error)))
{

View File

@ -15,8 +15,8 @@ GSK_NGL_DEFINE_PROGRAM (blur,
GSK_NGL_DEFINE_PROGRAM (border,
"/org/gtk/libgsk/ngl/border.glsl",
GSK_NGL_ADD_UNIFORM (2, BORDER_WIDTHS, u_widths)
GSK_NGL_ADD_UNIFORM (3, BORDER_OUTLINE_RECT, u_outline_rect))
GSK_NGL_ADD_UNIFORM (1, BORDER_WIDTHS, u_widths)
GSK_NGL_ADD_UNIFORM (2, BORDER_OUTLINE_RECT, u_outline_rect))
GSK_NGL_DEFINE_PROGRAM (color,
"/org/gtk/libgsk/ngl/color.glsl",
@ -42,11 +42,16 @@ GSK_NGL_DEFINE_PROGRAM (cross_fade,
GSK_NGL_ADD_UNIFORM (1, CROSS_FADE_PROGRESS, u_progress)
GSK_NGL_ADD_UNIFORM (2, CROSS_FADE_SOURCE2, u_source2))
GSK_NGL_DEFINE_PROGRAM (filled_border,
"/org/gtk/libgsk/ngl/filled_border.glsl",
GSK_NGL_ADD_UNIFORM (1, FILLED_BORDER_WIDTHS, u_widths)
GSK_NGL_ADD_UNIFORM (2, FILLED_BORDER_OUTLINE_RECT, u_outline_rect))
GSK_NGL_DEFINE_PROGRAM (inset_shadow,
"/org/gtk/libgsk/ngl/inset_shadow.glsl",
GSK_NGL_ADD_UNIFORM (2, INSET_SHADOW_SPREAD, u_spread)
GSK_NGL_ADD_UNIFORM (3, INSET_SHADOW_OFFSET, u_offset)
GSK_NGL_ADD_UNIFORM (4, INSET_SHADOW_OUTLINE_RECT, u_outline_rect))
GSK_NGL_ADD_UNIFORM (1, INSET_SHADOW_SPREAD, u_spread)
GSK_NGL_ADD_UNIFORM (2, INSET_SHADOW_OFFSET, u_offset)
GSK_NGL_ADD_UNIFORM (3, INSET_SHADOW_OUTLINE_RECT, u_outline_rect))
GSK_NGL_DEFINE_PROGRAM (linear_gradient,
"/org/gtk/libgsk/ngl/linear_gradient.glsl",
@ -57,7 +62,7 @@ GSK_NGL_DEFINE_PROGRAM (linear_gradient,
GSK_NGL_DEFINE_PROGRAM (outset_shadow,
"/org/gtk/libgsk/ngl/outset_shadow.glsl",
GSK_NGL_ADD_UNIFORM (2, OUTSET_SHADOW_OUTLINE_RECT, u_outline_rect))
GSK_NGL_ADD_UNIFORM (1, OUTSET_SHADOW_OUTLINE_RECT, u_outline_rect))
GSK_NGL_DEFINE_PROGRAM (radial_gradient,
"/org/gtk/libgsk/ngl/radial_gradient.glsl",
@ -74,6 +79,6 @@ GSK_NGL_DEFINE_PROGRAM (repeat,
GSK_NGL_DEFINE_PROGRAM (unblurred_outset_shadow,
"/org/gtk/libgsk/ngl/unblurred_outset_shadow.glsl",
GSK_NGL_ADD_UNIFORM (2, UNBLURRED_OUTSET_SHADOW_SPREAD, u_spread)
GSK_NGL_ADD_UNIFORM (3, UNBLURRED_OUTSET_SHADOW_OFFSET, u_offset)
GSK_NGL_ADD_UNIFORM (4, UNBLURRED_OUTSET_SHADOW_OUTLINE_RECT, u_outline_rect))
GSK_NGL_ADD_UNIFORM (1, UNBLURRED_OUTSET_SHADOW_SPREAD, u_spread)
GSK_NGL_ADD_UNIFORM (2, UNBLURRED_OUTSET_SHADOW_OFFSET, u_offset)
GSK_NGL_ADD_UNIFORM (3, UNBLURRED_OUTSET_SHADOW_OUTLINE_RECT, u_outline_rect))

File diff suppressed because it is too large Load Diff

View File

@ -56,6 +56,7 @@ struct _GskNglDrawVertex
float position[2];
float uv[2];
float color[4];
float color2[4];
};
G_END_DECLS

View File

@ -310,5 +310,5 @@ void main() {
else
discard;
gskSetOutputColor(result * u_alpha);
gskSetScaledOutputColor(result, u_alpha);
}

View File

@ -13,5 +13,5 @@ void main() {
void main() {
vec4 diffuse = GskTexture(u_source, vUv);
gskSetOutputColor(diffuse * u_alpha);
gskSetScaledOutputColor(diffuse, u_alpha);
}

View File

@ -11,7 +11,7 @@ _OUT_ _GSK_ROUNDED_RECT_UNIFORM_ transformed_inside_outline;
void main() {
gl_Position = u_projection * u_modelview * vec4(aPosition, 0.0, 1.0);
final_color = gsk_premultiply(aColor) * u_alpha;
final_color = gsk_scaled_premultiply(aColor, u_alpha);
GskRoundedRect outside = gsk_create_rect(u_outline_rect);
GskRoundedRect inside = gsk_rounded_rect_shrink (outside, u_widths);
@ -39,5 +39,5 @@ void main() {
gsk_rounded_rect_coverage(gsk_decode_rect(transformed_inside_outline), frag),
0.0, 1.0);
gskSetOutputColor(final_color * alpha);
gskSetScaledOutputColor(final_color, alpha);
}

View File

@ -6,7 +6,7 @@ _OUT_ vec4 final_color;
void main() {
gl_Position = u_projection * u_modelview * vec4(aPosition, 0.0, 1.0);
final_color = gsk_premultiply(aColor) * u_alpha;
final_color = gsk_scaled_premultiply(aColor, u_alpha);
}
// FRAGMENT_SHADER:

View File

@ -23,7 +23,5 @@ void main() {
color = u_color_matrix * color + u_color_offset;
color = clamp(color, 0.0, 1.0);
color.rgb *= color.a;
gskSetOutputColor(color * u_alpha);
gskSetOutputColor(gsk_scaled_premultiply(color, u_alpha));
}

View File

@ -12,12 +12,12 @@ void main() {
// We use this shader for both plain glyphs (used as mask)
// and color glpyhs (used as source). The renderer sets
// aColor to vec4(-1) for color glyhs.
if (distance(aColor, vec4(-1)) < 0.001)
if (distance(aColor,vec4(-1)) < 0.1)
use_color = 0.0;
else
use_color = 1.0;
final_color = gsk_premultiply(aColor) * u_alpha;
final_color = gsk_scaled_premultiply(aColor, u_alpha);
}
// FRAGMENT_SHADER:
@ -29,8 +29,5 @@ _IN_ float use_color;
void main() {
vec4 diffuse = GskTexture(u_source, vUv);
if (use_color > 0.0)
gskSetOutputColor(final_color * diffuse.a);
else
gskSetOutputColor(diffuse * u_alpha);
gskSetOutputColor(mix(diffuse * u_alpha, final_color * diffuse.a, use_color));
}

View File

@ -18,6 +18,8 @@ void main() {
// FRAGMENT_SHADER:
// conic_gradient.glsl
#define MAX_COLOR_STOPS 6
#ifdef GSK_LEGACY
uniform int u_num_color_stops;
#else
@ -25,7 +27,7 @@ uniform highp int u_num_color_stops; // Why? Because it works like this.
#endif
uniform vec4 u_geometry;
uniform float u_color_stops[6 * 5];
uniform float u_color_stops[MAX_COLOR_STOPS * 5];
_NOPERSPECTIVE_ _IN_ vec2 coord;
@ -50,27 +52,31 @@ void main() {
// fract() does the modulo here, so now we have progress
// into the current conic
float offset = fract(angle * u_geometry.z + u_geometry.w);
float curr_offset;
float next_offset;
if (offset < get_offset(0)) {
next_offset = get_offset(0);
if (offset < next_offset) {
gskSetOutputColor(gsk_scaled_premultiply(get_color(0), u_alpha));
return;
}
int n = u_num_color_stops - 1;
for (int i = 0; i < n; i++) {
float curr_offset = get_offset(i);
float next_offset = get_offset(i + 1);
if (offset >= get_offset(u_num_color_stops - 1)) {
gskSetOutputColor(gsk_scaled_premultiply(get_color(u_num_color_stops - 1), u_alpha));
return;
}
if (offset >= curr_offset && offset < next_offset) {
for (int i = 0; i < MAX_COLOR_STOPS; i++) {
curr_offset = next_offset;
next_offset = get_offset(i + 1);
if (offset < next_offset) {
float f = (offset - curr_offset) / (next_offset - curr_offset);
vec4 curr_color = gsk_premultiply(get_color(i));
vec4 next_color = gsk_premultiply(get_color(i + 1));
vec4 color = mix(curr_color, next_color, f);
gskSetOutputColor(color * u_alpha);
gskSetScaledOutputColor(color, u_alpha);
return;
}
}
gskSetOutputColor(gsk_scaled_premultiply(get_color(n), u_alpha));
}

View File

@ -0,0 +1,47 @@
// VERTEX_SHADER:
// filled_border.glsl
uniform vec4 u_widths;
uniform vec4[3] u_outline_rect;
_OUT_ vec4 outer_color;
_OUT_ vec4 inner_color;
_OUT_ _GSK_ROUNDED_RECT_UNIFORM_ transformed_outside_outline;
_OUT_ _GSK_ROUNDED_RECT_UNIFORM_ transformed_inside_outline;
void main() {
gl_Position = u_projection * u_modelview * vec4(aPosition, 0.0, 1.0);
outer_color = gsk_scaled_premultiply(aColor, u_alpha);
inner_color = gsk_scaled_premultiply(aColor2, u_alpha);
GskRoundedRect outside = gsk_create_rect(u_outline_rect);
GskRoundedRect inside = gsk_rounded_rect_shrink (outside, u_widths);
gsk_rounded_rect_transform(outside, u_modelview);
gsk_rounded_rect_transform(inside, u_modelview);
gsk_rounded_rect_encode(outside, transformed_outside_outline);
gsk_rounded_rect_encode(inside, transformed_inside_outline);
}
// FRAGMENT_SHADER:
// filled_border.glsl
uniform vec4[3] u_outline_rect;
_IN_ vec4 outer_color;
_IN_ vec4 inner_color;
_IN_ _GSK_ROUNDED_RECT_UNIFORM_ transformed_outside_outline;
_IN_ _GSK_ROUNDED_RECT_UNIFORM_ transformed_inside_outline;
void main() {
vec2 frag = gsk_get_frag_coord();
float outer_coverage = gsk_rounded_rect_coverage(gsk_decode_rect(transformed_outside_outline), frag);
float inner_coverage = gsk_rounded_rect_coverage(gsk_decode_rect(transformed_inside_outline), frag);
float alpha = clamp(outer_coverage - inner_coverage, 0.0, 1.0);
float alpha2 = clamp(inner_coverage, 0.0, 1.0);
gskSetOutputColor((outer_color * alpha) + (inner_color * alpha2));
}

View File

@ -12,7 +12,7 @@ _OUT_ _GSK_ROUNDED_RECT_UNIFORM_ transformed_inside_outline;
void main() {
gl_Position = u_projection * u_modelview * vec4(aPosition, 0.0, 1.0);
final_color = gsk_premultiply(aColor) * u_alpha;
final_color = gsk_scaled_premultiply(aColor, u_alpha);
GskRoundedRect outside = gsk_create_rect(u_outline_rect);
GskRoundedRect inside = gsk_rounded_rect_shrink(outside, vec4(u_spread));
@ -40,5 +40,5 @@ void main() {
gsk_rounded_rect_coverage(gsk_decode_rect(transformed_inside_outline), frag),
0.0, 1.0);
gskSetOutputColor(final_color * alpha);
gskSetScaledOutputColor(final_color, alpha);
}

View File

@ -42,13 +42,15 @@ void main() {
// FRAGMENT_SHADER:
// linear_gradient.glsl
#define MAX_COLOR_STOPS 6
#ifdef GSK_LEGACY
uniform int u_num_color_stops;
#else
uniform highp int u_num_color_stops; // Why? Because it works like this.
#endif
uniform float u_color_stops[6 * 5];
uniform float u_color_stops[MAX_COLOR_STOPS * 5];
uniform bool u_repeat;
_NOPERSPECTIVE_ _IN_ vec4 info;
@ -68,31 +70,35 @@ vec4 get_color(int index) {
void main() {
float offset = dot(info.xy, info.zw);
float curr_offset;
float next_offset;
if (u_repeat) {
offset = fract(offset);
}
if (offset < get_offset(0)) {
next_offset = get_offset(0);
if (offset < next_offset) {
gskSetOutputColor(gsk_scaled_premultiply(get_color(0), u_alpha));
return;
}
int n = u_num_color_stops - 1;
for (int i = 0; i < n; i++) {
float curr_offset = get_offset(i);
float next_offset = get_offset(i + 1);
if (offset >= get_offset(u_num_color_stops - 1)) {
gskSetOutputColor(gsk_scaled_premultiply(get_color(u_num_color_stops - 1), u_alpha));
return;
}
if (offset >= curr_offset && offset < next_offset) {
for (int i = 0; i < MAX_COLOR_STOPS; i++) {
curr_offset = next_offset;
next_offset = get_offset(i + 1);
if (offset < next_offset) {
float f = (offset - curr_offset) / (next_offset - curr_offset);
vec4 curr_color = gsk_premultiply(get_color(i));
vec4 next_color = gsk_premultiply(get_color(i + 1));
vec4 color = mix(curr_color, next_color, f);
gskSetOutputColor(color * u_alpha);
gskSetScaledOutputColor(color, u_alpha);
return;
}
}
gskSetOutputColor(gsk_scaled_premultiply(get_color(n), u_alpha));
}

View File

@ -11,7 +11,7 @@ void main() {
vUv = vec2(aUv.x, aUv.y);
final_color = gsk_premultiply(aColor) * u_alpha;
final_color = gsk_scaled_premultiply(aColor, u_alpha);
GskRoundedRect outline = gsk_create_rect(u_outline_rect);
gsk_rounded_rect_transform(outline, u_modelview);
@ -30,7 +30,5 @@ void main() {
alpha *= (1.0 - clamp(gsk_rounded_rect_coverage(gsk_decode_rect(transformed_outline), frag), 0.0, 1.0));
vec4 color = final_color * alpha;
gskSetOutputColor(color);
gskSetScaledOutputColor(final_color, alpha);
}

View File

@ -123,11 +123,35 @@ void gskSetOutputColor(vec4 color) {
#if defined(NO_CLIP)
result = color;
#elif defined(RECT_CLIP)
result = color * gsk_rect_coverage(gsk_get_bounds(u_clip_rect),
float coverage = gsk_rect_coverage(gsk_get_bounds(u_clip_rect),
gsk_get_frag_coord());
result = color * coverage;
#else
result = color * gsk_rounded_rect_coverage(gsk_create_rect(u_clip_rect),
float coverage = gsk_rounded_rect_coverage(gsk_create_rect(u_clip_rect),
gsk_get_frag_coord());
result = color * coverage;
#endif
#if defined(GSK_GLES) || defined(GSK_LEGACY)
gl_FragColor = result;
#else
outputColor = result;
#endif
}
void gskSetScaledOutputColor(vec4 color, float alpha) {
vec4 result;
#if defined(NO_CLIP)
result = color * alpha;
#elif defined(RECT_CLIP)
float coverage = gsk_rect_coverage(gsk_get_bounds(u_clip_rect),
gsk_get_frag_coord());
result = color * (alpha * coverage);
#else
float coverage = gsk_rounded_rect_coverage(gsk_create_rect(u_clip_rect),
gsk_get_frag_coord());
result = color * (alpha * coverage);
#endif
#if defined(GSK_GLES) || defined(GSK_LEGACY)

View File

@ -6,11 +6,13 @@ uniform float u_alpha;
attribute vec2 aPosition;
attribute vec2 aUv;
attribute vec4 aColor;
attribute vec4 aColor2;
_OUT_ vec2 vUv;
#else
_IN_ vec2 aPosition;
_IN_ vec2 aUv;
_IN_ vec4 aColor;
_IN_ vec4 aColor2;
_OUT_ vec2 vUv;
#endif

View File

@ -20,6 +20,8 @@ void main() {
// FRAGMENT_SHADER:
// radial_gradient.glsl
#define MAX_COLOR_STOPS 6
#ifdef GSK_LEGACY
uniform int u_num_color_stops;
#else
@ -28,7 +30,7 @@ uniform highp int u_num_color_stops;
uniform bool u_repeat;
uniform vec2 u_range;
uniform float u_color_stops[6 * 5];
uniform float u_color_stops[MAX_COLOR_STOPS * 5];
_NOPERSPECTIVE_ _IN_ vec2 coord;
@ -48,31 +50,35 @@ vec4 get_color(int index) {
void main() {
// Reverse scale
float offset = length(coord) * u_range.x + u_range.y;
float curr_offset;
float next_offset;
if (u_repeat) {
offset = fract(offset);
}
if (offset < get_offset(0)) {
next_offset = get_offset(0);
if (offset < next_offset) {
gskSetOutputColor(gsk_scaled_premultiply(get_color(0), u_alpha));
return;
}
int n = u_num_color_stops - 1;
for (int i = 0; i < n; i++) {
float curr_offset = get_offset(i);
float next_offset = get_offset(i + 1);
if (offset >= get_offset(u_num_color_stops - 1)) {
gskSetOutputColor(gsk_scaled_premultiply(get_color(u_num_color_stops - 1), u_alpha));
return;
}
if (offset >= curr_offset && offset < next_offset) {
for (int i = 0; i < MAX_COLOR_STOPS; i++) {
curr_offset = next_offset;
next_offset = get_offset(i + 1);
if (offset < next_offset) {
float f = (offset - curr_offset) / (next_offset - curr_offset);
vec4 curr_color = gsk_premultiply(get_color(i));
vec4 next_color = gsk_premultiply(get_color(i + 1));
vec4 color = mix(curr_color, next_color, f);
gskSetOutputColor(color * u_alpha);
gskSetScaledOutputColor(color, u_alpha);
return;
}
}
gskSetOutputColor(gsk_scaled_premultiply(get_color(n), u_alpha));
}

View File

@ -40,5 +40,5 @@ void main() {
vec4 diffuse = GskTexture(u_source, tp);
gskSetOutputColor(diffuse * u_alpha);
gskSetScaledOutputColor(diffuse, u_alpha);
}

View File

@ -39,6 +39,6 @@ void main() {
gsk_rounded_rect_coverage(gsk_decode_rect(transformed_inside_outline), frag),
0.0, 1.0);
gskSetOutputColor(final_color * alpha);
gskSetScaledOutputColor(final_color, alpha);
}