Skip to content

Instantly share code, notes, and snippets.

@davexunit
Last active February 18, 2023 19:43
Show Gist options
  • Save davexunit/83e1ed3d71ea11f48e30fc8cc0d568ec to your computer and use it in GitHub Desktop.
Save davexunit/83e1ed3d71ea11f48e30fc8cc0d568ec to your computer and use it in GitHub Desktop.
vector path shaders
(define-vertex-shader stroke-vertex
(in vec2 position)
(in vec2 tex)
(in float stroke-length)
(out vec2 frag-tex)
(out float frag-stroke-length)
(uniform mat4 mvp)
(uniform vec4 color)
;; Short-circuit because the fragments will just be discarded
;; anyway.
(if (<= (-> color w) 0.0)
(outputs
(vertex:position (vec4 0.0 0.0 0.0 1.0)))
(outputs
(vertex:position (* mvp (vec4 (-> position x) (-> position y) 0.0 1.0)))
(frag-tex tex)
(frag-stroke-length stroke-length))))
(define-fragment-shader stroke-fragment
(in vec2 frag-tex)
(in float frag-stroke-length)
(out vec4 frag-color)
(uniform vec4 color)
(uniform float feather)
(uniform int stroke-closed)
(uniform float stroke-width)
(uniform int stroke-cap)
(uniform float stroke-miter-limit)
(if (<= (-> color w) 0.0)
(discard)
(let* ((infinity (/ 1.0 0.0))
(hw (/ stroke-width 2.0))
(u (-> frag-tex x))
(v (-> frag-tex y))
(dx (if (< u 0.0) (abs u) (- u frag-stroke-length)))
(dy (abs v))
;; Signed distance function that applies end cap styles.
(d (if (or (< u 0.0) (> u frag-stroke-length))
(case stroke-cap
((0) ; none
infinity)
((1) ; butt (hehe)
(max (- (+ dx hw) (* 2.0 feather)) dy))
((2) ; square
(max dx dy))
((3) ; round
(sqrt (+ (* dx dx) (* dy dy))))
((4) ; triangle out
(+ dx dy))
((5) ; triangle in
(max dy (- hw (+ feather dx) dy)))
(else ; unknown
0.0))
dy))
(color* (if (<= d hw)
color
(vec4 (-> color x)
(-> color y)
(-> color z)
(* (-> color w)
(- 1.0 (/ (- d hw) feather)))))))
(if (<= (-> color* w) 0.0)
(outputs)
(outputs
(frag-color color*))))))
(define-graphics-variable stroke-shader
(compile-shader stroke-vertex stroke-fragment))
(define-vertex-shader fill-vertex
(in vec2 position)
(out vec2 frag-position)
(uniform mat4 mvp)
(uniform vec4 color)
(uniform mat3 gradient-matrix)
;; Short-circuit because the fragments will just be discarded
;; anyway.
(if (<= (-> color w) 0.0)
(outputs
(vertex:position (vec4 0.0 0.0 0.0 1.0)))
(let* ((x (-> position x))
(y (-> position y))
(fp (* gradient-matrix (vec3 x y 1.0))))
(outputs
(vertex:position (* mvp (vec4 x y 0.0 1.0)))
(frag-position (vec2 (-> fp x) (-> fp y)))))))
(define-fragment-shader fill-fragment
(in vec2 frag-position)
(out vec4 frag-color)
(uniform int mode)
(uniform vec4 color)
(uniform vec4 end-color)
(uniform vec2 gradient-range)
(uniform float radial-gradient-ratio)
(define (gradient-mix x)
(let* ((start (-> gradient-range x))
(end (-> gradient-range y))
(t (clamp (/ (- x start) (- end start))
0.0 1.0)))
(mix color end-color t)))
(if (<= (-> color w) 0.0)
(discard)
(outputs
(frag-color
(case mode
((0) ; solid color
color)
((1) ; linear gradient
(gradient-mix (-> frag-position x)))
((2) ; radial gradient
(let ((p (* frag-position
(vec2 1.0 radial-gradient-ratio))))
(gradient-mix (length p))))
(else
(vec4 0.0 0.0 0.0 0.0)))))))
(define-graphics-variable fill-shader
(compile-shader fill-vertex fill-fragment))
in vec2 V17;
out vec4 V1;
uniform int V21;
uniform vec4 V19;
uniform vec4 V22;
uniform vec2 V23;
uniform float V24;
void V11(in float V7, out vec4 V48) {
float V49 = V23.x;
float V8 = V49;
float V50 = V23.y;
float V9 = V50;
float V51 = V7 - V8;
float V52 = V9 - V8;
float V53 = V51 / V52;
float V54 = 0.0;
float V55 = 1.0;
float V56 = clamp(V53, V54, V55);
float V10 = V56;
vec4 V57 = mix(V19, V22, V10);
vec4 V58 = V57;
vec4 V59 = V58;
vec4 V60 = V59;
V48 = V60;
}
void main() {
float V61 = V19.w;
float V62 = 0.0;
bool V63 = V61 <= V62;
if(V63) {
discard;
} else {
int V12 = V21;
vec4 V64;
int V65 = 0;
bool V66 = V12 == V65;
bool V13 = V66;
bool V67;
if(V13) {
V67 = V13;
} else {
bool V68 = false;
V67 = V68;
}
bool V69 = V67;
if(V69) {
V64 = V19;
} else {
vec4 V70;
int V71 = 1;
bool V72 = V12 == V71;
bool V14 = V72;
bool V73;
if(V14) {
V73 = V14;
} else {
bool V74 = false;
V73 = V74;
}
bool V75 = V73;
if(V75) {
float V76 = V17.x;
vec4 V77;
V11(V76, V77);
V70 = V77;
} else {
vec4 V78;
int V79 = 2;
bool V80 = V12 == V79;
bool V15 = V80;
bool V81;
if(V15) {
V81 = V15;
} else {
bool V82 = false;
V81 = V82;
}
bool V83 = V81;
if(V83) {
float V84 = 1.0;
vec2 V85 = vec2(V84, V24);
vec2 V86 = V17 * V85;
vec2 V16 = V86;
float V87 = length(V16);
vec4 V88;
V11(V87, V88);
vec4 V89 = V88;
V78 = V89;
} else {
float V90 = 0.0;
float V91 = 0.0;
float V92 = 0.0;
float V93 = 0.0;
vec4 V94 = vec4(V90, V91, V92, V93);
V78 = V94;
}
V70 = V78;
}
V64 = V70;
}
vec4 V95 = V64;
V1 = V95;
}
}
in vec2 V0;
out vec2 V17;
uniform mat4 V18;
uniform vec4 V19;
uniform mat3 V20;
void main() {
float V25 = V19.w;
float V26 = 0.0;
bool V27 = V25 <= V26;
if(V27) {
float V28 = 0.0;
float V29 = 0.0;
float V30 = 0.0;
float V31 = 1.0;
vec4 V32 = vec4(V28, V29, V30, V31);
gl_Position = V32;
} else {
float V33 = V0.x;
float V5 = V33;
float V34 = V0.y;
float V6 = V34;
float V35 = 1.0;
vec3 V36 = vec3(V5, V6, V35);
vec3 V37 = V20 * V36;
vec3 V7 = V37;
float V38 = 0.0;
float V39 = 1.0;
vec4 V40 = vec4(V5, V6, V38, V39);
vec4 V41 = V18 * V40;
gl_Position = V41;
float V42 = V7.x;
float V43 = V7.y;
vec2 V44 = vec2(V42, V43);
V17 = V44;
}
}
in vec2 V26;
in float V27;
out vec4 V2;
uniform vec4 V29;
uniform float V30;
uniform int V31;
uniform float V32;
uniform int V33;
uniform float V34;
void main() {
float V49 = V29.w;
float V50 = 0.0;
bool V51 = V49 <= V50;
if(V51) {
discard;
} else {
float V52 = 2.0;
float V53 = V32 / V52;
float V10 = V53;
float V54 = V26.x;
float V11 = V54;
float V55 = V26.y;
float V12 = V55;
float V56;
float V57 = 0.0;
bool V58 = V11 < V57;
if(V58) {
float V59 = abs(V11);
V56 = V59;
} else {
float V60 = V11 - V27;
V56 = V60;
}
float V13 = V56;
float V61 = abs(V12);
float V14 = V61;
float V62;
float V63 = 0.0;
bool V64 = V11 < V63;
bool V15 = V64;
bool V65;
if(V15) {
V65 = V15;
} else {
bool V66 = V11 > V27;
bool V16 = V66;
bool V67;
if(V16) {
V67 = V16;
} else {
bool V68 = false;
V67 = V68;
}
bool V69 = V67;
V65 = V69;
}
bool V70 = V65;
if(V70) {
int V17 = V33;
float V71;
int V72 = 0;
bool V73 = V17 == V72;
bool V18 = V73;
bool V74;
if(V18) {
V74 = V18;
} else {
bool V75 = false;
V74 = V75;
}
bool V76 = V74;
if(V76) {
float V77 = 1.0 / 0.0;
V71 = V77;
} else {
float V78;
int V79 = 1;
bool V80 = V17 == V79;
bool V19 = V80;
bool V81;
if(V19) {
V81 = V19;
} else {
bool V82 = false;
V81 = V82;
}
bool V83 = V81;
if(V83) {
float V84 = V13 + V10;
float V85 = 2.0;
float V86 = V85 * V30;
float V87 = V84 - V86;
float V88 = max(V87, V14);
V78 = V88;
} else {
float V89;
int V90 = 2;
bool V91 = V17 == V90;
bool V20 = V91;
bool V92;
if(V20) {
V92 = V20;
} else {
bool V93 = false;
V92 = V93;
}
bool V94 = V92;
if(V94) {
float V95 = max(V13, V14);
V89 = V95;
} else {
float V96;
int V97 = 3;
bool V98 = V17 == V97;
bool V21 = V98;
bool V99;
if(V21) {
V99 = V21;
} else {
bool V100 = false;
V99 = V100;
}
bool V101 = V99;
if(V101) {
float V102 = V13 * V13;
float V103 = V14 * V14;
float V104 = V102 + V103;
float V105 = sqrt(V104);
V96 = V105;
} else {
float V106;
int V107 = 4;
bool V108 = V17 == V107;
bool V22 = V108;
bool V109;
if(V22) {
V109 = V22;
} else {
bool V110 = false;
V109 = V110;
}
bool V111 = V109;
if(V111) {
float V112 = V13 + V14;
V106 = V112;
} else {
float V113;
int V114 = 5;
bool V115 = V17 == V114;
bool V23 = V115;
bool V116;
if(V23) {
V116 = V23;
} else {
bool V117 = false;
V116 = V117;
}
bool V118 = V116;
if(V118) {
float V119 = V30 + V13;
float V120 = V119 - V14;
float V121 = V10 - V120;
float V122 = max(V14, V121);
V113 = V122;
} else {
float V123 = 0.0;
V113 = V123;
}
V106 = V113;
}
V96 = V106;
}
V89 = V96;
}
V78 = V89;
}
V71 = V78;
}
float V124 = V71;
V62 = V124;
} else {
V62 = V14;
}
float V24 = V62;
vec4 V125;
bool V126 = V24 <= V10;
if(V126) {
V125 = V29;
} else {
float V127 = V29.x;
float V128 = V29.y;
float V129 = V29.z;
float V130 = V29.w;
float V131 = 1.0;
float V132 = V24 - V10;
float V133 = V132 / V30;
float V134 = V131 - V133;
float V135 = V130 * V134;
vec4 V136 = vec4(V127, V128, V129, V135);
V125 = V136;
}
vec4 V25 = V125;
float V137 = V29.w;
float V138 = 0.0;
bool V139 = V137 <= V138;
if(V139) {
discard;
} else {
V2 = V25;
}
}
}
in vec2 V0;
in vec2 V1;
in float V2;
out vec2 V26;
out float V27;
uniform mat4 V28;
uniform vec4 V29;
void main() {
float V35 = V29.w;
float V36 = 0.0;
bool V37 = V35 <= V36;
if(V37) {
float V38 = 0.0;
float V39 = 0.0;
float V40 = 0.0;
float V41 = 1.0;
vec4 V42 = vec4(V38, V39, V40, V41);
gl_Position = V42;
} else {
float V43 = V0.x;
float V44 = V0.y;
float V45 = 0.0;
float V46 = 1.0;
vec4 V47 = vec4(V43, V44, V45, V46);
vec4 V48 = V28 * V47;
gl_Position = V48;
V26 = V1;
V27 = V2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment