summaryrefslogtreecommitdiff
path: root/data
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2021-08-27 16:07:38 -0400
committerDavid Thompson <dthompson2@worcester.edu>2021-08-27 16:09:23 -0400
commit9b5d364add330fc18100491bf3c57316a27183f5 (patch)
tree7799b4b8ae3224a4bdd6e4ec82db796a12b4d5c7 /data
parent9b6c5e3ba42eace457ce96600fecd31109e976ab (diff)
graphics: path: Separate fill/stroke into different shaders.
Diffstat (limited to 'data')
-rw-r--r--data/shaders/path-fill-frag.glsl17
-rw-r--r--data/shaders/path-fill-vert.glsl22
-rw-r--r--data/shaders/path-frag.glsl93
-rw-r--r--data/shaders/path-stroke-frag.glsl77
-rw-r--r--data/shaders/path-stroke-vert.glsl (renamed from data/shaders/path-vert.glsl)17
5 files changed, 122 insertions, 104 deletions
diff --git a/data/shaders/path-fill-frag.glsl b/data/shaders/path-fill-frag.glsl
new file mode 100644
index 0000000..3af191f
--- /dev/null
+++ b/data/shaders/path-fill-frag.glsl
@@ -0,0 +1,17 @@
+// -*- mode: c -*-
+
+#ifdef GLSL330
+out vec4 fragColor;
+#else
+#define fragColor gl_FragColor
+#endif
+
+uniform vec4 color;
+
+void main(void) {
+ if (color.a <= 0.0) {
+ discard;
+ }
+
+ fragColor = color;
+}
diff --git a/data/shaders/path-fill-vert.glsl b/data/shaders/path-fill-vert.glsl
new file mode 100644
index 0000000..d95b71b
--- /dev/null
+++ b/data/shaders/path-fill-vert.glsl
@@ -0,0 +1,22 @@
+// -*- mode: c -*-
+
+#ifdef GLSL330
+layout (location = 0) in vec2 position;
+#elif defined(GLSL130)
+in vec2 position;
+#elif defined(GLSL120)
+attribute vec2 position;
+#endif
+
+uniform mat4 mvp;
+uniform vec4 color;
+
+void main(void) {
+ // Short-circuit because the fragments will just be discarded
+ // anyway.
+ if (color.a <= 0.0) {
+ gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
+ } else {
+ gl_Position = mvp * vec4(position.xy, 0.0, 1.0);
+ }
+}
diff --git a/data/shaders/path-frag.glsl b/data/shaders/path-frag.glsl
deleted file mode 100644
index 0288ae1..0000000
--- a/data/shaders/path-frag.glsl
+++ /dev/null
@@ -1,93 +0,0 @@
-// -*- mode: c -*-
-
-#ifdef GLSL330
-out vec4 fragColor;
-#endif
-
-#ifdef GLSL120
-varying vec2 fragTex;
-varying float fragStrokeLength;
-#else
-in vec2 fragTex;
-in float fragStrokeLength;
-#endif
-
-uniform int mode;
-uniform vec4 color;
-uniform float feather;
-uniform int strokeClosed;
-uniform float strokeWidth;
-uniform int strokeCap;
-uniform int strokeMiterStyle;
-uniform float strokeMiterLimit;
-
-float infinity = 1.0 / 0.0;
-
-void main(void) {
- if (color.a <= 0.0) {
- discard;
- }
-
- // fill mode
- if(mode == 0) {
-#ifdef GLSL330
- fragColor = color;
-#else
- gl_FragColor = color;
-#endif
- } else if(mode == 1) { // stroke mode
- float hw = strokeWidth / 2.0;
- float u = fragTex.x;
- float v = fragTex.y;
- float dx;
- float dy;
- float d;
-
- // Stroke caps.
- if (u < 0 || u > fragStrokeLength) {
- if (u < 0) {
- dx = abs(u);
- } else {
- dx = u - fragStrokeLength;
- }
- dy = abs(v);
-
- if (strokeCap == 0) { // none
- d = infinity;
- } else if (strokeCap == 1) { // butt
- d = max(dx + hw - 2 * feather, dy);
- } else if (strokeCap == 2) { // square
- d = max(dx, dy);
- } else if (strokeCap == 3) { // round
- d = sqrt(dx * dx + dy * dy);
- } else if (strokeCap == 4) { // triangle out
- d = dx + dy;
- } else if (strokeCap == 5) { // triangle in
- d = max(dy, hw - feather + dx - dy);
- }
- // Stroke inner/join
- } else {
- d = abs(v);
- }
-
- if(d <= hw) {
-#ifdef GLSL330
- fragColor = color;
-#else
- gl_FragColor = color;
-#endif
- } else {
- vec4 c = vec4(color.rgb, color.a * (1.0 - ((d - hw) / feather)));
-
- if (c.a <= 0.0) {
- discard;
- }
-
-#ifdef GLSL330
- fragColor = c;
-#else
- gl_FragColor = c;
-#endif
- }
- }
-}
diff --git a/data/shaders/path-stroke-frag.glsl b/data/shaders/path-stroke-frag.glsl
new file mode 100644
index 0000000..9682c8f
--- /dev/null
+++ b/data/shaders/path-stroke-frag.glsl
@@ -0,0 +1,77 @@
+// -*- mode: c -*-
+
+#ifdef GLSL330
+out vec4 fragColor;
+#else
+#define fragColor gl_FragColor
+#endif
+
+#ifdef GLSL120
+varying vec2 fragTex;
+varying float fragStrokeLength;
+#else
+in vec2 fragTex;
+in float fragStrokeLength;
+#endif
+
+uniform vec4 color;
+uniform float feather;
+uniform int strokeClosed;
+uniform float strokeWidth;
+uniform int strokeCap;
+uniform int strokeMiterStyle;
+uniform float strokeMiterLimit;
+
+float infinity = 1.0 / 0.0;
+
+void main(void) {
+ if (color.a <= 0.0) {
+ discard;
+ }
+
+ float hw = strokeWidth / 2.0;
+ float u = fragTex.x;
+ float v = fragTex.y;
+ float dx;
+ float dy;
+ float d;
+
+ // Stroke caps.
+ if (u < 0 || u > fragStrokeLength) {
+ if (u < 0) {
+ dx = abs(u);
+ } else {
+ dx = u - fragStrokeLength;
+ }
+ dy = abs(v);
+
+ if (strokeCap == 0) { // none
+ d = infinity;
+ } else if (strokeCap == 1) { // butt
+ d = max(dx + hw - 2 * feather, dy);
+ } else if (strokeCap == 2) { // square
+ d = max(dx, dy);
+ } else if (strokeCap == 3) { // round
+ d = sqrt(dx * dx + dy * dy);
+ } else if (strokeCap == 4) { // triangle out
+ d = dx + dy;
+ } else if (strokeCap == 5) { // triangle in
+ d = max(dy, hw - feather + dx - dy);
+ }
+ // Stroke inner/join
+ } else {
+ d = abs(v);
+ }
+
+ if(d <= hw) {
+ fragColor = color;
+ } else {
+ vec4 c = vec4(color.rgb, color.a * (1.0 - ((d - hw) / feather)));
+
+ if (c.a <= 0.0) {
+ discard;
+ }
+
+ fragColor = c;
+ }
+}
diff --git a/data/shaders/path-vert.glsl b/data/shaders/path-stroke-vert.glsl
index 38fa5d2..5915010 100644
--- a/data/shaders/path-vert.glsl
+++ b/data/shaders/path-stroke-vert.glsl
@@ -28,17 +28,12 @@ uniform int mode;
uniform int strokeClosed;
void main(void) {
- // Short-circuit because the fragments will just be discarded anyway.
- if (color.a <= 0.0) {
- gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
- return;
- }
-
- // Stroke specific setup.
- if (mode == 1) {
- fragStrokeLength = strokeLength;
- }
-
+ // Short-circuit because the fragments will just be discarded anyway.
+ if (color.a <= 0.0) {
+ gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
+ } else {
+ fragStrokeLength = strokeLength;
fragTex = tex;
gl_Position = mvp * vec4(position.xy, 0.0, 1.0);
+ }
}