-
Notifications
You must be signed in to change notification settings - Fork 58
/
gl_shader.cpp
240 lines (220 loc) · 5.6 KB
/
gl_shader.cpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// This file is part of Desktop App Toolkit,
// a set of libraries for developing nice desktop applications.
//
// For license and copyright information please follow this link:
// https://github.com/desktop-app/legal/blob/master/LEGAL
//
#include "ui/gl/gl_shader.h"
#include "ui/gl/gl_image.h"
#include "base/debug_log.h"
#include <QtGui/QOpenGLContext>
namespace Ui::GL {
[[nodiscard]] bool IsOpenGLES() {
const auto current = QOpenGLContext::currentContext();
Assert(current != nullptr);
return (current->format().renderableType() == QSurfaceFormat::OpenGLES);
}
QString VertexShader(const std::vector<ShaderPart> &parts) {
const auto version = IsOpenGLES()
? QString("#version 100\nprecision highp float;\n")
: QString("#version 120\n");
const auto accumulate = [&](auto proj) {
return ranges::accumulate(parts, QString(), std::plus<>(), proj);
};
return version + R"(
attribute vec2 position;
)" + accumulate(&ShaderPart::header) + R"(
void main() {
vec4 result = vec4(position, 0., 1.);
)" + accumulate(&ShaderPart::body) + R"(
gl_Position = result;
}
)";
}
QString FragmentShader(const std::vector<ShaderPart> &parts) {
const auto version = IsOpenGLES()
? QString("#version 100\nprecision highp float;\n")
: QString("#version 120\n");
const auto accumulate = [&](auto proj) {
return ranges::accumulate(parts, QString(), std::plus<>(), proj);
};
return version + accumulate(&ShaderPart::header) + R"(
void main() {
vec4 result = vec4(0., 0., 0., 0.);
)" + accumulate(&ShaderPart::body) + R"(
gl_FragColor = result;
}
)";
}
ShaderPart VertexPassTextureCoord(char prefix) {
const auto name = prefix + QString("_texcoord");
return {
.header = R"(
attribute vec2 )" + name + R"(In;
varying vec2 )" + name + ";\n",
.body = R"(
)" + name + " = " + name + "In;\n",
};
}
ShaderPart FragmentSampleARGB32Texture() {
return {
.header = R"(
varying vec2 v_texcoord;
uniform sampler2D s_texture;
)",
.body = R"(
result = texture2D(s_texture, v_texcoord);
)" + (kSwizzleRedBlue
? R"(
result = vec4(result.b, result.g, result.r, result.a);
)" : QString()),
};
}
QString FragmentYUV2RGB() {
return R"(
result = vec4(
1.164 * y + 1.596 * v,
1.164 * y - 0.392 * u - 0.813 * v,
1.164 * y + 2.17 * u,
1.);
)";
}
ShaderPart FragmentSampleYUV420Texture() {
return {
.header = R"(
varying vec2 v_texcoord;
uniform sampler2D y_texture;
uniform sampler2D u_texture;
uniform sampler2D v_texture;
)",
.body = R"(
float y = texture2D(y_texture, v_texcoord).a - 0.0625;
float u = texture2D(u_texture, v_texcoord).a - 0.5;
float v = texture2D(v_texture, v_texcoord).a - 0.5;
)" + FragmentYUV2RGB(),
};
}
ShaderPart FragmentSampleNV12Texture() {
return {
.header = R"(
varying vec2 v_texcoord;
uniform sampler2D y_texture;
uniform sampler2D uv_texture;
)",
.body = R"(
float y = texture2D(y_texture, v_texcoord).a - 0.0625;
vec2 uv = texture2D(uv_texture, v_texcoord).rg - vec2(0.5, 0.5);
float u = uv.x;
float v = uv.y;
)" + FragmentYUV2RGB(),
};
}
ShaderPart FragmentGlobalOpacity() {
return {
.header = R"(
uniform float g_opacity;
)",
.body = R"(
result *= g_opacity;
)",
};
}
ShaderPart VertexViewportTransform() {
return {
.header = R"(
uniform vec2 viewport;
vec4 transform(vec4 position) {
return vec4(
vec2(-1, -1) + 2. * position.xy / viewport,
position.z,
position.w);
}
)",
.body = R"(
result = transform(result);
)",
};
}
ShaderPart FragmentRoundCorners() {
return {
.header = R"(
uniform vec4 roundRect;
uniform vec2 radiusOutline;
uniform vec4 roundBg;
uniform vec4 outlineFg;
vec2 roundedCorner() {
vec2 rectHalf = roundRect.zw / 2.;
vec2 rectCenter = roundRect.xy + rectHalf;
vec2 fromRectCenter = abs(gl_FragCoord.xy - rectCenter);
vec2 vectorRadius = radiusOutline.xx + vec2(0.5, 0.5);
vec2 fromCenterWithRadius = fromRectCenter + vectorRadius;
vec2 fromRoundingCenter = max(fromCenterWithRadius, rectHalf)
- rectHalf;
float rounded = length(fromRoundingCenter) - radiusOutline.x;
float outline = rounded + radiusOutline.y;
return vec2(
1. - smoothstep(0., 1., rounded),
1. - (smoothstep(0., 1., outline) * outlineFg.a));
}
)",
.body = R"(
vec2 roundOutline = roundedCorner();
result = result * roundOutline.y
+ vec4(outlineFg.rgb, 1) * (1. - roundOutline.y);
result = result * roundOutline.x + roundBg * (1. - roundOutline.x);
)",
};
}
ShaderPart FragmentStaticColor() {
return {
.header = R"(
uniform vec4 s_color;
)",
.body = R"(
result = s_color;
)",
};
}
not_null<QOpenGLShader*> MakeShader(
not_null<QOpenGLShaderProgram*> program,
QOpenGLShader::ShaderType type,
const QString &source) {
const auto result = new QOpenGLShader(type, program);
if (!result->compileSourceCode(source)) {
LOG(("Shader Compilation Failed: %1, error %2.").arg(
source,
result->log()));
}
program->addShader(result);
return result;
}
Program LinkProgram(
not_null<QOpenGLShaderProgram*> program,
std::variant<QString, not_null<QOpenGLShader*>> vertex,
std::variant<QString, not_null<QOpenGLShader*>> fragment) {
const auto vertexAsSource = v::is<QString>(vertex);
const auto v = vertexAsSource
? MakeShader(
program,
QOpenGLShader::Vertex,
v::get<QString>(vertex))
: v::get<not_null<QOpenGLShader*>>(vertex);
if (!vertexAsSource) {
program->addShader(v);
}
const auto fragmentAsSource = v::is<QString>(fragment);
const auto f = fragmentAsSource
? MakeShader(
program,
QOpenGLShader::Fragment,
v::get<QString>(fragment))
: v::get<not_null<QOpenGLShader*>>(fragment);
if (!fragmentAsSource) {
program->addShader(f);
}
if (!program->link()) {
LOG(("Shader Link Failed: %1.").arg(program->log()));
}
return { v, f };
}
} // namespace Ui::GL