summaryrefslogtreecommitdiff
path: root/gsk/resources/vulkan/blendmode.frag
blob: 803a538d1ddedb084841a2bc4fdbf50a65b4163e (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#version 420 core

#include "clip.frag.glsl"

layout(location = 0) in vec2 inPos;
layout(location = 1) in vec2 inStartTexCoord;
layout(location = 2) in vec2 inEndTexCoord;
layout(location = 3) flat in uint inBlendMode;

layout(set = 0, binding = 0) uniform sampler2D startTexture;
layout(set = 1, binding = 0) uniform sampler2D endTexture;

layout(location = 0) out vec4 color;

vec3
multiply (vec3 source, vec3 backdrop, float opacity)
{
  vec3 result = source * backdrop;
  return mix (source, result, opacity);
}

vec3
difference (vec3 source, vec3 backdrop, float opacity)
{
  vec3 result = abs (source - backdrop);
  return mix (source, result, opacity);
}

vec3
screen (vec3 source, vec3 backdrop, float opacity)
{
  vec3 result = source + backdrop - source * backdrop;
  return mix (source, result, opacity);
}

float
hard_light (float source, float backdrop)
{
  if (source <= 0.5)
    return 2 * backdrop * source;
  else
    return 2 * (backdrop + source - backdrop * source) - 1;
}

vec3
hard_light (vec3 source, vec3 backdrop, float opacity)
{
  vec3 result = vec3 (hard_light (source.r, backdrop.r),
                      hard_light (source.g, backdrop.g),
                      hard_light (source.b, backdrop.b));
  return mix (source, result, opacity);
}

float
soft_light (float source, float backdrop)
{
  float db;

  if (backdrop <= 0.25)
    db = ((16 * backdrop - 12) * backdrop + 4) * backdrop;
  else
    db = sqrt (backdrop);

  if (source <= 0.5)
    return backdrop - (1 - 2 * source) * backdrop * (1 - backdrop);
  else
    return backdrop + (2 * source - 1) * (db - backdrop);
}

vec3
soft_light (vec3 source, vec3 backdrop, float opacity)
{
  vec3 result = vec3 (soft_light (source.r, backdrop.r),
                      soft_light (source.g, backdrop.g),
                      soft_light (source.b, backdrop.b));
  return mix (source, result, opacity);
}

vec3
overlay (vec3 source, vec3 backdrop, float opacity)
{
  return hard_light (backdrop, source, opacity);
}

vec3
darken (vec3 source, vec3 backdrop, float opacity)
{
  vec3 result = min (source, backdrop);
  return mix (source, result, opacity);
}

vec3
lighten (vec3 source, vec3 backdrop, float opacity)
{
  vec3 result = max (source, backdrop);
  return mix (source, result, opacity);
}

float
color_dodge (float source, float backdrop)
{
  return (source == 1.0) ? source : min (backdrop / (1.0 - source), 1.0);
}

vec3
color_dodge (vec3 source, vec3 backdrop, float opacity)
{
  vec3 result = vec3 (color_dodge (source.r, backdrop.r),
                      color_dodge (source.g, backdrop.g),
                      color_dodge (source.b, backdrop.b));
  return mix (source, result, opacity);
}


float
color_burn (float source, float backdrop)
{
  return (source == 0.0) ? source : max ((1.0 - ((1.0 - backdrop) / source)), 0.0);
}

vec3
color_burn (vec3 source, vec3 backdrop, float opacity)
{
  vec3 result = vec3 (color_burn (source.r, backdrop.r),
                      color_burn (source.g, backdrop.g),
                      color_burn (source.b, backdrop.b));
  return mix (source, result, opacity);
}

vec3
exclusion (vec3 source, vec3 backdrop, float opacity)
{
  vec3 result = backdrop + source - 2.0 * backdrop * source;
  return mix (source, result, opacity);
}

float
combine (float source, float backdrop)
{
  return source + backdrop * (1 - source);
}

void main()
{
  vec4 source = texture (startTexture, inStartTexCoord);
  vec4 backdrop = texture (endTexture, inEndTexCoord);
  vec3 rgb = vec3(1,0,0);
  float a = 1;

  a = combine (source.a, backdrop.a);

  if (inBlendMode == 0) // default
    {
      rgb = source.rgb;
      a = source.a;
    }
  else if (inBlendMode == 1)
    rgb = multiply (source.rgb, backdrop.rgb, backdrop.a);
  else if (inBlendMode == 2)
    rgb = screen (source.rgb, backdrop.rgb, backdrop.a);
  else if (inBlendMode == 3)
    rgb = overlay (source.rgb, backdrop.rgb, backdrop.a);
  else if (inBlendMode == 4)
    rgb = darken (source.rgb, backdrop.rgb, backdrop.a);
  else if (inBlendMode == 5)
    rgb = lighten (source.rgb, backdrop.rgb, backdrop.a);
  else if (inBlendMode == 6)
    rgb = color_dodge (source.rgb, backdrop.rgb, backdrop.a);
  else if (inBlendMode == 7)
    rgb = color_burn (source.rgb, backdrop.rgb, backdrop.a);
  else if (inBlendMode == 8)
    rgb = hard_light (source.rgb, backdrop.rgb, backdrop.a);
  else if (inBlendMode == 9)
    rgb = soft_light (source.rgb, backdrop.rgb, backdrop.a);
  else if (inBlendMode == 10)
    rgb = difference (source.rgb, backdrop.rgb, backdrop.a);
  else if (inBlendMode == 11)
    rgb = exclusion (source.rgb, backdrop.rgb, backdrop.a);
  else
    discard;

  color = clip (inPos, vec4 (rgb, a));
}