[gtk/wip/chergert/glyphy: 2/2] gsk/gl: import Glyphy demo shaders




commit b492d35b6f0c8db26eaa92096e981193071a852a
Author: Christian Hergert <chergert redhat com>
Date:   Wed Mar 16 17:13:25 2022 -0700

    gsk/gl: import Glyphy demo shaders
    
    This brings in the "demo" shaders from Glyphy which are the part meant to
    bridge between the Glyphy common/sdf shaders and the target application.
    Nothing has been modified in them yet, and modifications will be necessary.
    
    Most likely, we'll want to bridge these to use the vertex attributes that
    we use in all of the other shaders for projection, modelview, etc.
    
    Currently, no uniforms are even defined in the gskglprograms.defs and that
    will be needed so that we can apply them while processing text nodes.
    
    This is just a first step to get things moving forward.

 gsk/gl/gskglprograms.defs          | 13 ++++++
 gsk/gl/resources/glyphy.atlas.glsl | 16 +++++++
 gsk/gl/resources/glyphy.fs.glsl    | 88 ++++++++++++++++++++++++++++++++++++++
 gsk/gl/resources/glyphy.vs.glsl    | 25 +++++++++++
 gsk/meson.build                    |  3 ++
 5 files changed, 145 insertions(+)
---
diff --git a/gsk/gl/gskglprograms.defs b/gsk/gl/gskglprograms.defs
index 1ff99fab89..f0e3e84760 100644
--- a/gsk/gl/gskglprograms.defs
+++ b/gsk/gl/gskglprograms.defs
@@ -82,3 +82,16 @@ GSK_GL_DEFINE_PROGRAM (unblurred_outset_shadow,
                        GSK_GL_ADD_UNIFORM (1, UNBLURRED_OUTSET_SHADOW_SPREAD, u_spread)
                        GSK_GL_ADD_UNIFORM (2, UNBLURRED_OUTSET_SHADOW_OFFSET, u_offset)
                        GSK_GL_ADD_UNIFORM (3, UNBLURRED_OUTSET_SHADOW_OUTLINE_RECT, u_outline_rect))
+
+GSK_GL_DEFINE_PROGRAM (glyphy,
+                       GSK_GL_SHADER_JOINED (VERTEX,
+                                             GSK_GL_SHADER_RESOURCE ("glyphy.vs.glsl"),
+                                             NULL)
+                       GSK_GL_SHADER_JOINED (FRAGMENT,
+                                             GSK_GL_SHADER_RESOURCE ("glyphy.atlas.glsl"),
+                                             GSK_GL_SHADER_STRING (glyphy_common_shader_source ()),
+                                             GSK_GL_SHADER_STRING ("#define GLYPHY_SDF_PSEUDO_DISTANCE 1\n"),
+                                             GSK_GL_SHADER_STRING (glyphy_sdf_shader_source ()),
+                                             GSK_GL_SHADER_RESOURCE ("glyphy.fs.glsl"),
+                                             NULL),
+                       GSK_GL_NO_UNIFORMS)
diff --git a/gsk/gl/resources/glyphy.atlas.glsl b/gsk/gl/resources/glyphy.atlas.glsl
new file mode 100644
index 0000000000..ec6bc1af7b
--- /dev/null
+++ b/gsk/gl/resources/glyphy.atlas.glsl
@@ -0,0 +1,16 @@
+uniform sampler2D u_atlas_tex;
+uniform ivec4 u_atlas_info;
+
+#define GLYPHY_TEXTURE1D_EXTRA_DECLS , sampler2D _tex, ivec4 _atlas_info, ivec2 _atlas_pos
+#define GLYPHY_TEXTURE1D_EXTRA_ARGS , _tex, _atlas_info, _atlas_pos
+#define GLYPHY_DEMO_EXTRA_ARGS , u_atlas_tex, u_atlas_info, gi.atlas_pos
+
+vec4
+glyphy_texture1D_func (int offset GLYPHY_TEXTURE1D_EXTRA_DECLS)
+{
+  ivec2 item_geom = _atlas_info.zw;
+  vec2 pos = (vec2 (_atlas_pos.xy * item_geom +
+                  ivec2 (mod (float (offset), float (item_geom.x)), offset / item_geom.x)) +
+             + vec2 (.5, .5)) / vec2(_atlas_info.xy);
+  return texture2D (_tex, pos);
+}
diff --git a/gsk/gl/resources/glyphy.fs.glsl b/gsk/gl/resources/glyphy.fs.glsl
new file mode 100644
index 0000000000..0ea4cdf495
--- /dev/null
+++ b/gsk/gl/resources/glyphy.fs.glsl
@@ -0,0 +1,88 @@
+// FRAGMENT_SHADER:
+// glyphy.fs.glsl
+
+uniform float u_contrast;
+uniform float u_gamma_adjust;
+uniform float u_outline_thickness;
+uniform bool  u_outline;
+uniform float u_boldness;
+uniform bool  u_debug;
+
+varying vec4 v_glyph;
+
+
+#define SQRT2_2 0.70710678118654757 /* 1 / sqrt(2.) */
+#define SQRT2   1.4142135623730951
+
+struct glyph_info_t {
+  ivec2 nominal_size;
+  ivec2 atlas_pos;
+};
+
+glyph_info_t
+glyph_info_decode (vec4 v)
+{
+  glyph_info_t gi;
+  gi.nominal_size = (ivec2 (mod (v.zw, 256.)) + 2) / 4;
+  gi.atlas_pos = ivec2 (v_glyph.zw) / 256;
+  return gi;
+}
+
+
+float
+antialias (float d)
+{
+  return smoothstep (-.75, +.75, d);
+}
+
+void
+main()
+{
+  vec2 p = v_glyph.xy;
+  glyph_info_t gi = glyph_info_decode (v_glyph);
+
+  /* isotropic antialiasing */
+  vec2 dpdx = dFdx (p);
+  vec2 dpdy = dFdy (p);
+  float m = length (vec2 (length (dpdx), length (dpdy))) * SQRT2_2;
+
+  vec4 color = vec4 (0,0,0,1);
+
+  float gsdist = glyphy_sdf (p, gi.nominal_size GLYPHY_DEMO_EXTRA_ARGS);
+  float sdist = gsdist / m * u_contrast;
+
+  if (!u_debug) {
+    sdist -= u_boldness * 10.;
+    if (u_outline)
+      sdist = abs (sdist) - u_outline_thickness * .5;
+    if (sdist > 1.)
+      discard;
+    float alpha = antialias (-sdist);
+    if (u_gamma_adjust != 1.)
+      alpha = pow (alpha, 1./u_gamma_adjust);
+    color = vec4 (color.rgb,color.a * alpha);
+  } else {
+    color = vec4 (0,0,0,0);
+
+    // Color the inside of the glyph a light red
+    color += vec4 (.5,0,0,.5) * smoothstep (1., -1., sdist);
+
+    float udist = abs (sdist);
+    float gudist = abs (gsdist);
+    // Color the outline red
+    color += vec4 (1,0,0,1) * smoothstep (2., 1., udist);
+    // Color the distance field in green
+    if (!glyphy_isinf (udist))
+      color += vec4(0,.4,0,.4 - (abs(gsdist) / max(float(gi.nominal_size.x), float(gi.nominal_size.y))) * 
4.);
+
+    float pdist = glyphy_point_dist (p, gi.nominal_size GLYPHY_DEMO_EXTRA_ARGS);
+    // Color points green
+    color = mix (vec4 (0,1,0,.5), color, smoothstep (.05, .06, pdist));
+
+    glyphy_arc_list_t arc_list = glyphy_arc_list (p, gi.nominal_size GLYPHY_DEMO_EXTRA_ARGS);
+    // Color the number of endpoints per cell blue
+    color += vec4 (0,0,1,.1) * float(arc_list.num_endpoints) * 32./255.;
+  }
+
+  gl_FragColor = color;
+}
diff --git a/gsk/gl/resources/glyphy.vs.glsl b/gsk/gl/resources/glyphy.vs.glsl
new file mode 100644
index 0000000000..38521b3b47
--- /dev/null
+++ b/gsk/gl/resources/glyphy.vs.glsl
@@ -0,0 +1,25 @@
+// VERTEX_SHADER:
+// glyphy.vs.glsl
+
+uniform mat4 u_matViewProjection;
+
+attribute vec4 a_glyph_vertex;
+
+varying vec4 v_glyph;
+
+vec4
+glyph_vertex_transcode (vec2 v)
+{
+  ivec2 g = ivec2 (v);
+  ivec2 corner = ivec2 (mod (v, 2.));
+  g /= 2;
+  ivec2 nominal_size = ivec2 (mod (vec2(g), 64.));
+  return vec4 (corner * nominal_size, g * 4);
+}
+
+void
+main()
+{
+  gl_Position = u_matViewProjection * vec4 (a_glyph_vertex.xy, 0, 1);
+  v_glyph = glyph_vertex_transcode (a_glyph_vertex.zw);
+}
diff --git a/gsk/meson.build b/gsk/meson.build
index 99020f48fd..1e51fd482a 100644
--- a/gsk/meson.build
+++ b/gsk/meson.build
@@ -19,6 +19,9 @@ gsk_private_gl_shaders = [
   'gl/resources/repeat.glsl',
   'gl/resources/custom.glsl',
   'gl/resources/filled_border.glsl',
+  'gl/resources/glyphy.atlas.glsl',
+  'gl/resources/glyphy.fs.glsl',
+  'gl/resources/glyphy.vs.glsl',
 ]
 
 gsk_public_sources = files([


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