forked from jpbruyere/vkvg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvkvg_main.frag
159 lines (140 loc) · 4.77 KB
/
vkvg_main.frag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright (c) 2018-2022 Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
#ifdef VKVG_ENABLE_VK_SCALAR_BLOCK_LAYOUT
#extension GL_EXT_scalar_block_layout : enable
#endif
layout (set=0, binding = 0) uniform sampler2DArray fontMap;
layout (set=1, binding = 0) uniform sampler2D source;
#if defined(GL_EXT_scalar_block_layout) && defined(VKVG_ENABLE_VK_SCALAR_BLOCK_LAYOUT)
layout (scalar, set=2, binding = 0) uniform _uboGrad {
vec4 colors[16];
float stops[16];
vec4 cp[2];
uint count;
}uboGrad;
#define COLORSTOP(i) uboGrad.stops[i]
#else
layout (set=2, binding = 0) uniform _uboGrad {
vec4 colors[16];
vec4 stops[16];
vec4 cp[2];
uint count;
}uboGrad;
#define COLORSTOP(i) uboGrad.stops[i].r
#endif
layout (location = 0) in vec3 inFontUV; //if it is a text drawing, inFontUV.z hold fontMap layer
layout (location = 1) in vec4 inSrc; //source bounds or color depending on pattern type
layout (location = 2) in flat int inPatType; //pattern type
layout (location = 3) in flat float inOpacity;
layout (location = 4) in mat3x2 inMat;
layout (location = 0) out vec4 outFragColor;
layout (constant_id = 0) const int NUM_SAMPLES = 8;
#define SOLID 0
#define SURFACE 1
#define LINEAR 2
#define RADIAL 3
#define MESH 4
#define RASTER_SOURCE 5
void main()
{
vec4 c = inSrc;
switch(inPatType){
case SURFACE:
vec2 p = (gl_FragCoord.xy - inSrc.xy);
vec2 uv = vec2(
inMat[0][0] * p.x + inMat[1][0] * p.y + inMat[2][0],
inMat[0][1] * p.x + inMat[1][1] * p.y + inMat[2][1]
);
c = texture (source, uv / inSrc.zw);
break;
case LINEAR:
float dist = 1;
vec2 p0 = uboGrad.cp[0].xy / inSrc.xy;
vec2 p1 = uboGrad.cp[0].zw / inSrc.xy;
p = gl_FragCoord.xy / inSrc.xy;
float l = length (p1 - p0);
vec2 u = normalize (p1 - p0);
if (u.y == 0)
if (u.x < 0)
dist = -(p.x-p0.x) / l;
else
dist = (p.x-p0.x) / l;
else {
float m = -u.x / u.y;
float bb = p0.y - m * p0.x;
dist =((p.y - m * p.x - bb) / sqrt (1 + m * m)) / l;
if (u.y < 0)
dist = - dist;
}
c = mix(uboGrad.colors[0], uboGrad.colors[1], smoothstep(COLORSTOP(0), COLORSTOP(1), dist));
for ( int i=1; i<uboGrad.count-1; ++i )
c = mix(c, uboGrad.colors[i+1], smoothstep(COLORSTOP(i), COLORSTOP(i+1), dist));
break;
case RADIAL:
p = gl_FragCoord.xy / inSrc.xy;
vec2 c0 = uboGrad.cp[0].xy / inSrc.xy;
vec2 c1 = uboGrad.cp[1].xy / inSrc.xy;
float r0 = uboGrad.cp[0].z / inSrc.x;
float r1 = uboGrad.cp[1].z / inSrc.x;
/// APPLY FOCUS MODIFIER
//project a point on the circle such that it passes through the focus and through the coord,
//and then get the distance of the focus from that point.
//that is the effective gradient length
float gradLength = 1.0;
vec2 diff =c0 - c1;
vec2 rayDir = normalize(p - c0);
float a = dot(rayDir, rayDir);
float b = 2.0 * dot(rayDir, diff);
float cc = dot(diff, diff) - r1 * r1;
float disc = b * b - 4.0 * a * cc;
if (disc >= 0.0)
{
float t = (-b + sqrt(abs(disc))) / (2.0 * a);
vec2 projection = c0 + rayDir * t;
gradLength = distance(projection, c0)-r0;
}
else
{
//gradient is undefined for this coordinate
}
/// OUTPUT
float grad = (distance(p, c0)-r0) / gradLength ;
c = mix (uboGrad.colors[0], uboGrad.colors[1], smoothstep(COLORSTOP(0),COLORSTOP(1), grad));
for (int i=2; i < uboGrad.count; i++ )
c = mix(c, uboGrad.colors[i], smoothstep(COLORSTOP(i-1),COLORSTOP(i), grad));
break;
}
if (inFontUV.z >= 0.0)
c *= texture(fontMap, inFontUV).r;
#ifdef VKVG_PREMULT_ALPHA
c *= inOpacity;
#else
c.a *= inOpacity;
#endif
outFragColor = c;
}
void op_CLEAR () {
outFragColor = vec4 (0);
}