blob: 1e7e93cbe6b8ab085f7e05911bca47aa26995b40 [file] [log] [blame]
Mathias Agopian3f844832013-08-07 21:24:32 -07001/*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chia-I Wu93e14df2018-06-04 10:10:17 -070017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Peiyong Lin833074a2018-08-28 11:53:54 -070019#include "ProgramCache.h"
Peiyong Lincbc184f2018-08-22 13:24:10 -070020
Mathias Agopian3f844832013-08-07 21:24:32 -070021#include <GLES2/gl2.h>
22#include <GLES2/gl2ext.h>
Chia-I Wu56d7b0a2018-10-01 15:13:11 -070023#include <log/log.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070024#include <renderengine/private/Description.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070025#include <utils/String8.h>
Chia-I Wu93e14df2018-06-04 10:10:17 -070026#include <utils/Trace.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070027#include "Program.h"
28
29ANDROID_SINGLETON_STATIC_INSTANCE(android::renderengine::gl::ProgramCache)
Mathias Agopian3f844832013-08-07 21:24:32 -070030
Mathias Agopian3f844832013-08-07 21:24:32 -070031namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -070032namespace renderengine {
33namespace gl {
Mathias Agopian3f844832013-08-07 21:24:32 -070034
Mathias Agopian3f844832013-08-07 21:24:32 -070035/*
36 * A simple formatter class to automatically add the endl and
37 * manage the indentation.
38 */
39
40class Formatter;
41static Formatter& indent(Formatter& f);
42static Formatter& dedent(Formatter& f);
43
44class Formatter {
45 String8 mString;
46 int mIndent;
47 typedef Formatter& (*FormaterManipFunc)(Formatter&);
48 friend Formatter& indent(Formatter& f);
49 friend Formatter& dedent(Formatter& f);
Chia-I Wub027f802017-11-29 14:00:52 -080050
Mathias Agopian3f844832013-08-07 21:24:32 -070051public:
Andy McFadden892f22d2013-08-15 10:05:01 -070052 Formatter() : mIndent(0) {}
53
Chia-I Wub027f802017-11-29 14:00:52 -080054 String8 getString() const { return mString; }
Mathias Agopian3f844832013-08-07 21:24:32 -070055
Chia-I Wub027f802017-11-29 14:00:52 -080056 friend Formatter& operator<<(Formatter& out, const char* in) {
57 for (int i = 0; i < out.mIndent; i++) {
Mathias Agopian3f844832013-08-07 21:24:32 -070058 out.mString.append(" ");
59 }
60 out.mString.append(in);
61 out.mString.append("\n");
62 return out;
63 }
Chia-I Wub027f802017-11-29 14:00:52 -080064 friend inline Formatter& operator<<(Formatter& out, const String8& in) {
65 return operator<<(out, in.string());
Mathias Agopian3f844832013-08-07 21:24:32 -070066 }
67 friend inline Formatter& operator<<(Formatter& to, FormaterManipFunc func) {
68 return (*func)(to);
69 }
70};
71Formatter& indent(Formatter& f) {
72 f.mIndent++;
73 return f;
74}
75Formatter& dedent(Formatter& f) {
76 f.mIndent--;
77 return f;
78}
79
Chia-I Wu93e14df2018-06-04 10:10:17 -070080ProgramCache::ProgramCache() {}
Mathias Agopian3f844832013-08-07 21:24:32 -070081
Chia-I Wub027f802017-11-29 14:00:52 -080082ProgramCache::~ProgramCache() {}
Mathias Agopian3f844832013-08-07 21:24:32 -070083
Peiyong Lin13effd12018-07-24 17:01:47 -070084void ProgramCache::primeCache(bool useColorManagement) {
Riley Andrewsa51fafc2014-09-29 13:29:40 -070085 uint32_t shaderCount = 0;
Chia-I Wub027f802017-11-29 14:00:52 -080086 uint32_t keyMask = Key::BLEND_MASK | Key::OPACITY_MASK | Key::ALPHA_MASK | Key::TEXTURE_MASK;
Riley Andrewsa51fafc2014-09-29 13:29:40 -070087 // Prime the cache for all combinations of the above masks,
88 // leaving off the experimental color matrix mask options.
89
90 nsecs_t timeBefore = systemTime();
91 for (uint32_t keyVal = 0; keyVal <= keyMask; keyVal++) {
92 Key shaderKey;
93 shaderKey.set(keyMask, keyVal);
94 uint32_t tex = shaderKey.getTextureTarget();
Chia-I Wub027f802017-11-29 14:00:52 -080095 if (tex != Key::TEXTURE_OFF && tex != Key::TEXTURE_EXT && tex != Key::TEXTURE_2D) {
Riley Andrewsa51fafc2014-09-29 13:29:40 -070096 continue;
97 }
Chia-I Wu56d7b0a2018-10-01 15:13:11 -070098 if (mCache.count(shaderKey) == 0) {
99 mCache.emplace(shaderKey, generateProgram(shaderKey));
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700100 shaderCount++;
101 }
102 }
Chia-I Wu93e14df2018-06-04 10:10:17 -0700103
104 // Prime for sRGB->P3 conversion
Peiyong Lin13effd12018-07-24 17:01:47 -0700105 if (useColorManagement) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700106 Key shaderKey;
107 shaderKey.set(Key::BLEND_MASK | Key::TEXTURE_MASK | Key::OUTPUT_TRANSFORM_MATRIX_MASK |
108 Key::INPUT_TF_MASK | Key::OUTPUT_TF_MASK,
109 Key::BLEND_PREMULT | Key::TEXTURE_EXT | Key::OUTPUT_TRANSFORM_MATRIX_ON |
110 Key::INPUT_TF_SRGB | Key::OUTPUT_TF_SRGB);
111 for (int i = 0; i < 4; i++) {
112 shaderKey.set(Key::OPACITY_MASK,
113 (i & 1) ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT);
114 shaderKey.set(Key::ALPHA_MASK, (i & 2) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE);
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700115 if (mCache.count(shaderKey) == 0) {
116 mCache.emplace(shaderKey, generateProgram(shaderKey));
Chia-I Wu93e14df2018-06-04 10:10:17 -0700117 shaderCount++;
118 }
119 }
120 }
121
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700122 nsecs_t timeAfter = systemTime();
123 float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6;
124 ALOGD("shader cache generated - %u shaders in %f ms\n", shaderCount, compileTimeMs);
125}
126
Mathias Agopian3f844832013-08-07 21:24:32 -0700127ProgramCache::Key ProgramCache::computeKey(const Description& description) {
128 Key needs;
129 needs.set(Key::TEXTURE_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700130 !description.textureEnabled
Chia-I Wub027f802017-11-29 14:00:52 -0800131 ? Key::TEXTURE_OFF
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700132 : description.texture.getTextureTarget() == GL_TEXTURE_EXTERNAL_OES
Chia-I Wub027f802017-11-29 14:00:52 -0800133 ? Key::TEXTURE_EXT
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700134 : description.texture.getTextureTarget() == GL_TEXTURE_2D
Chia-I Wub027f802017-11-29 14:00:52 -0800135 ? Key::TEXTURE_2D
136 : Key::TEXTURE_OFF)
137 .set(Key::ALPHA_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700138 (description.color.a < 1) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE)
Chia-I Wub027f802017-11-29 14:00:52 -0800139 .set(Key::BLEND_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700140 description.isPremultipliedAlpha ? Key::BLEND_PREMULT : Key::BLEND_NORMAL)
Chia-I Wub027f802017-11-29 14:00:52 -0800141 .set(Key::OPACITY_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700142 description.isOpaque ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT)
Peiyong Lina296b0c2018-04-30 16:55:29 -0700143 .set(Key::Key::INPUT_TRANSFORM_MATRIX_MASK,
144 description.hasInputTransformMatrix() ?
145 Key::INPUT_TRANSFORM_MATRIX_ON : Key::INPUT_TRANSFORM_MATRIX_OFF)
146 .set(Key::Key::OUTPUT_TRANSFORM_MATRIX_MASK,
Chia-I Wud49d6692018-06-27 07:17:41 +0800147 description.hasOutputTransformMatrix() || description.hasColorMatrix() ?
Peiyong Lina296b0c2018-04-30 16:55:29 -0700148 Key::OUTPUT_TRANSFORM_MATRIX_ON : Key::OUTPUT_TRANSFORM_MATRIX_OFF);
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800149
Chia-I Wu131d3762018-01-11 14:35:27 -0800150 needs.set(Key::Y410_BT2020_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700151 description.isY410BT2020 ? Key::Y410_BT2020_ON : Key::Y410_BT2020_OFF);
Chia-I Wu131d3762018-01-11 14:35:27 -0800152
Peiyong Lina296b0c2018-04-30 16:55:29 -0700153 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700154 switch (description.inputTransferFunction) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800155 case Description::TransferFunction::LINEAR:
156 default:
157 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR);
158 break;
159 case Description::TransferFunction::SRGB:
160 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB);
161 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800162 case Description::TransferFunction::ST2084:
163 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_ST2084);
164 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700165 case Description::TransferFunction::HLG:
166 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_HLG);
167 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800168 }
169
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700170 switch (description.outputTransferFunction) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800171 case Description::TransferFunction::LINEAR:
172 default:
173 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR);
174 break;
175 case Description::TransferFunction::SRGB:
176 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB);
177 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800178 case Description::TransferFunction::ST2084:
179 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_ST2084);
180 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700181 case Description::TransferFunction::HLG:
182 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_HLG);
183 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800184 }
185 }
186
Mathias Agopian3f844832013-08-07 21:24:32 -0700187 return needs;
188}
189
Peiyong Lin2542cb02018-04-30 17:29:53 -0700190// Generate EOTF that converts signal values to relative display light,
191// both normalized to [0, 1].
192void ProgramCache::generateEOTF(Formatter& fs, const Key& needs) {
193 switch (needs.getInputTF()) {
194 case Key::INPUT_TF_SRGB:
195 fs << R"__SHADER__(
196 float EOTF_sRGB(float srgb) {
197 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
198 }
199
200 vec3 EOTF_sRGB(const vec3 srgb) {
201 return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
202 }
203
204 vec3 EOTF(const vec3 srgb) {
205 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
206 }
207 )__SHADER__";
208 break;
209 case Key::INPUT_TF_ST2084:
210 fs << R"__SHADER__(
211 vec3 EOTF(const highp vec3 color) {
212 const highp float m1 = (2610.0 / 4096.0) / 4.0;
213 const highp float m2 = (2523.0 / 4096.0) * 128.0;
214 const highp float c1 = (3424.0 / 4096.0);
215 const highp float c2 = (2413.0 / 4096.0) * 32.0;
216 const highp float c3 = (2392.0 / 4096.0) * 32.0;
217
218 highp vec3 tmp = pow(color, 1.0 / vec3(m2));
219 tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp);
220 return pow(tmp, 1.0 / vec3(m1));
221 }
222 )__SHADER__";
223 break;
224 case Key::INPUT_TF_HLG:
225 fs << R"__SHADER__(
226 highp float EOTF_channel(const highp float channel) {
227 const highp float a = 0.17883277;
228 const highp float b = 0.28466892;
229 const highp float c = 0.55991073;
230 return channel <= 0.5 ? channel * channel / 3.0 :
231 (exp((channel - c) / a) + b) / 12.0;
232 }
233
234 vec3 EOTF(const highp vec3 color) {
235 return vec3(EOTF_channel(color.r), EOTF_channel(color.g),
236 EOTF_channel(color.b));
237 }
238 )__SHADER__";
239 break;
240 default:
241 fs << R"__SHADER__(
242 vec3 EOTF(const vec3 linear) {
243 return linear;
244 }
245 )__SHADER__";
246 break;
247 }
248}
249
Peiyong Lin53f320e2018-04-23 17:31:06 -0700250void ProgramCache::generateToneMappingProcess(Formatter& fs, const Key& needs) {
251 // Convert relative light to absolute light.
252 switch (needs.getInputTF()) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700253 case Key::INPUT_TF_ST2084:
254 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700255 highp vec3 ScaleLuminance(highp vec3 color) {
256 return color * 10000.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700257 }
258 )__SHADER__";
259 break;
260 case Key::INPUT_TF_HLG:
261 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700262 highp vec3 ScaleLuminance(highp vec3 color) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700263 // The formula is:
264 // alpha * pow(Y, gamma - 1.0) * color + beta;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700265 // where alpha is 1000.0, gamma is 1.2, beta is 0.0.
266 return color * 1000.0 * pow(color.y, 0.2);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700267 }
268 )__SHADER__";
269 break;
270 default:
271 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700272 highp vec3 ScaleLuminance(highp vec3 color) {
273 return color * displayMaxLuminance;
274 }
275 )__SHADER__";
276 break;
277 }
278
279 // Tone map absolute light to display luminance range.
280 switch (needs.getInputTF()) {
281 case Key::INPUT_TF_ST2084:
282 case Key::INPUT_TF_HLG:
283 switch (needs.getOutputTF()) {
284 case Key::OUTPUT_TF_HLG:
285 // Right now when mixed PQ and HLG contents are presented,
286 // HLG content will always be converted to PQ. However, for
287 // completeness, we simply clamp the value to [0.0, 1000.0].
288 fs << R"__SHADER__(
289 highp vec3 ToneMap(highp vec3 color) {
290 return clamp(color, 0.0, 1000.0);
291 }
292 )__SHADER__";
293 break;
294 case Key::OUTPUT_TF_ST2084:
295 fs << R"__SHADER__(
296 highp vec3 ToneMap(highp vec3 color) {
297 return color;
298 }
299 )__SHADER__";
300 break;
301 default:
302 fs << R"__SHADER__(
303 highp vec3 ToneMap(highp vec3 color) {
304 const float maxMasteringLumi = 1000.0;
305 const float maxContentLumi = 1000.0;
306 const float maxInLumi = min(maxMasteringLumi, maxContentLumi);
307 float maxOutLumi = displayMaxLuminance;
308
309 float nits = color.y;
310
311 // clamp to max input luminance
312 nits = clamp(nits, 0.0, maxInLumi);
313
314 // scale [0.0, maxInLumi] to [0.0, maxOutLumi]
315 if (maxInLumi <= maxOutLumi) {
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700316 return color * (maxOutLumi / maxInLumi);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700317 } else {
318 // three control points
319 const float x0 = 10.0;
320 const float y0 = 17.0;
321 float x1 = maxOutLumi * 0.75;
322 float y1 = x1;
323 float x2 = x1 + (maxInLumi - x1) / 2.0;
324 float y2 = y1 + (maxOutLumi - y1) * 0.75;
325
326 // horizontal distances between the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700327 float h12 = x2 - x1;
328 float h23 = maxInLumi - x2;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700329 // tangents at the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700330 float m1 = (y2 - y1) / h12;
331 float m3 = (maxOutLumi - y2) / h23;
332 float m2 = (m1 + m3) / 2.0;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700333
334 if (nits < x0) {
335 // scale [0.0, x0] to [0.0, y0] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700336 float slope = y0 / x0;
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700337 return color * slope;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700338 } else if (nits < x1) {
339 // scale [x0, x1] to [y0, y1] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700340 float slope = (y1 - y0) / (x1 - x0);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700341 nits = y0 + (nits - x0) * slope;
342 } else if (nits < x2) {
343 // scale [x1, x2] to [y1, y2] using Hermite interp
344 float t = (nits - x1) / h12;
345 nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) +
346 (y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t;
347 } else {
348 // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp
349 float t = (nits - x2) / h23;
350 nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) +
351 (maxOutLumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t;
352 }
353 }
354
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700355 // color.y is greater than x0 and is thus non-zero
356 return color * (nits / color.y);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700357 }
358 )__SHADER__";
359 break;
360 }
361 break;
362 default:
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700363 // inverse tone map; the output luminance can be up to maxOutLumi.
Peiyong Lin53f320e2018-04-23 17:31:06 -0700364 fs << R"__SHADER__(
365 highp vec3 ToneMap(highp vec3 color) {
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700366 const float maxOutLumi = 3000.0;
367
368 const float x0 = 5.0;
369 const float y0 = 2.5;
370 float x1 = displayMaxLuminance * 0.7;
371 float y1 = maxOutLumi * 0.15;
372 float x2 = displayMaxLuminance * 0.9;
373 float y2 = maxOutLumi * 0.45;
374 float x3 = displayMaxLuminance;
375 float y3 = maxOutLumi;
376
377 float c1 = y1 / 3.0;
378 float c2 = y2 / 2.0;
379 float c3 = y3 / 1.5;
380
381 float nits = color.y;
382
383 float scale;
384 if (nits <= x0) {
385 // scale [0.0, x0] to [0.0, y0] linearly
386 const float slope = y0 / x0;
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700387 return color * slope;
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700388 } else if (nits <= x1) {
389 // scale [x0, x1] to [y0, y1] using a curve
390 float t = (nits - x0) / (x1 - x0);
391 nits = (1.0 - t) * (1.0 - t) * y0 + 2.0 * (1.0 - t) * t * c1 + t * t * y1;
392 } else if (nits <= x2) {
393 // scale [x1, x2] to [y1, y2] using a curve
394 float t = (nits - x1) / (x2 - x1);
395 nits = (1.0 - t) * (1.0 - t) * y1 + 2.0 * (1.0 - t) * t * c2 + t * t * y2;
396 } else {
397 // scale [x2, x3] to [y2, y3] using a curve
398 float t = (nits - x2) / (x3 - x2);
399 nits = (1.0 - t) * (1.0 - t) * y2 + 2.0 * (1.0 - t) * t * c3 + t * t * y3;
400 }
401
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700402 // color.y is greater than x0 and is thus non-zero
403 return color * (nits / color.y);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700404 }
405 )__SHADER__";
406 break;
407 }
Peiyong Lin53f320e2018-04-23 17:31:06 -0700408
409 // convert absolute light to relative light.
410 switch (needs.getOutputTF()) {
411 case Key::OUTPUT_TF_ST2084:
412 fs << R"__SHADER__(
413 highp vec3 NormalizeLuminance(highp vec3 color) {
414 return color / 10000.0;
415 }
416 )__SHADER__";
417 break;
418 case Key::OUTPUT_TF_HLG:
419 fs << R"__SHADER__(
420 highp vec3 NormalizeLuminance(highp vec3 color) {
421 return color / 1000.0 * pow(color.y / 1000.0, -0.2 / 1.2);
422 }
423 )__SHADER__";
424 break;
425 default:
426 fs << R"__SHADER__(
427 highp vec3 NormalizeLuminance(highp vec3 color) {
428 return color / displayMaxLuminance;
429 }
430 )__SHADER__";
431 break;
432 }
433}
434
435// Generate OOTF that modifies the relative scence light to relative display light.
436void ProgramCache::generateOOTF(Formatter& fs, const ProgramCache::Key& needs) {
437 if (!needs.needsToneMapping()) {
438 fs << R"__SHADER__(
439 highp vec3 OOTF(const highp vec3 color) {
440 return color;
441 }
442 )__SHADER__";
443 } else {
444 generateToneMappingProcess(fs, needs);
445 fs << R"__SHADER__(
446 highp vec3 OOTF(const highp vec3 color) {
447 return NormalizeLuminance(ToneMap(ScaleLuminance(color)));
448 }
449 )__SHADER__";
450 }
Peiyong Lin2542cb02018-04-30 17:29:53 -0700451}
452
453// Generate OETF that converts relative display light to signal values,
454// both normalized to [0, 1]
455void ProgramCache::generateOETF(Formatter& fs, const Key& needs) {
456 switch (needs.getOutputTF()) {
457 case Key::OUTPUT_TF_SRGB:
458 fs << R"__SHADER__(
459 float OETF_sRGB(const float linear) {
460 return linear <= 0.0031308 ?
461 linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
462 }
463
464 vec3 OETF_sRGB(const vec3 linear) {
465 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
466 }
467
468 vec3 OETF(const vec3 linear) {
469 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
470 }
471 )__SHADER__";
472 break;
473 case Key::OUTPUT_TF_ST2084:
474 fs << R"__SHADER__(
475 vec3 OETF(const vec3 linear) {
Peiyong Lin834be492018-05-18 15:12:55 -0700476 const highp float m1 = (2610.0 / 4096.0) / 4.0;
477 const highp float m2 = (2523.0 / 4096.0) * 128.0;
478 const highp float c1 = (3424.0 / 4096.0);
479 const highp float c2 = (2413.0 / 4096.0) * 32.0;
480 const highp float c3 = (2392.0 / 4096.0) * 32.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700481
Peiyong Lin834be492018-05-18 15:12:55 -0700482 highp vec3 tmp = pow(linear, vec3(m1));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700483 tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp);
484 return pow(tmp, vec3(m2));
485 }
486 )__SHADER__";
487 break;
488 case Key::OUTPUT_TF_HLG:
489 fs << R"__SHADER__(
490 highp float OETF_channel(const highp float channel) {
491 const highp float a = 0.17883277;
492 const highp float b = 0.28466892;
493 const highp float c = 0.55991073;
494 return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) :
495 a * log(12.0 * channel - b) + c;
496 }
497
498 vec3 OETF(const highp vec3 color) {
499 return vec3(OETF_channel(color.r), OETF_channel(color.g),
500 OETF_channel(color.b));
501 }
502 )__SHADER__";
503 break;
504 default:
505 fs << R"__SHADER__(
506 vec3 OETF(const vec3 linear) {
507 return linear;
508 }
509 )__SHADER__";
510 break;
511 }
512}
513
Mathias Agopian3f844832013-08-07 21:24:32 -0700514String8 ProgramCache::generateVertexShader(const Key& needs) {
515 Formatter vs;
516 if (needs.isTexturing()) {
Chia-I Wub027f802017-11-29 14:00:52 -0800517 vs << "attribute vec4 texCoords;"
518 << "varying vec2 outTexCoords;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700519 }
520 vs << "attribute vec4 position;"
521 << "uniform mat4 projection;"
522 << "uniform mat4 texture;"
Chia-I Wub027f802017-11-29 14:00:52 -0800523 << "void main(void) {" << indent << "gl_Position = projection * position;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700524 if (needs.isTexturing()) {
525 vs << "outTexCoords = (texture * texCoords).st;";
526 }
527 vs << dedent << "}";
528 return vs.getString();
529}
530
531String8 ProgramCache::generateFragmentShader(const Key& needs) {
532 Formatter fs;
533 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
534 fs << "#extension GL_OES_EGL_image_external : require";
535 }
Mathias Agopian458197d2013-08-15 14:56:51 -0700536
537 // default precision is required-ish in fragment shaders
538 fs << "precision mediump float;";
539
Mathias Agopian3f844832013-08-07 21:24:32 -0700540 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
541 fs << "uniform samplerExternalOES sampler;"
542 << "varying vec2 outTexCoords;";
543 } else if (needs.getTextureTarget() == Key::TEXTURE_2D) {
544 fs << "uniform sampler2D sampler;"
545 << "varying vec2 outTexCoords;";
chaviw13fdc492017-06-27 12:40:18 -0700546 }
547
548 if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700549 fs << "uniform vec4 color;";
550 }
chaviw13fdc492017-06-27 12:40:18 -0700551
Chia-I Wu131d3762018-01-11 14:35:27 -0800552 if (needs.isY410BT2020()) {
553 fs << R"__SHADER__(
554 vec3 convertY410BT2020(const vec3 color) {
555 const vec3 offset = vec3(0.0625, 0.5, 0.5);
556 const mat3 transform = mat3(
557 vec3(1.1678, 1.1678, 1.1678),
558 vec3( 0.0, -0.1878, 2.1481),
559 vec3(1.6836, -0.6523, 0.0));
560 // Y is in G, U is in R, and V is in B
561 return clamp(transform * (color.grb - offset), 0.0, 1.0);
562 }
563 )__SHADER__";
564 }
565
Peiyong Lina296b0c2018-04-30 16:55:29 -0700566 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Peiyong Lin53f320e2018-04-23 17:31:06 -0700567 // Currently, display maximum luminance is needed when doing tone mapping.
568 if (needs.needsToneMapping()) {
Chia-I Wu8bbacdf2018-05-04 15:19:37 -0700569 fs << "uniform float displayMaxLuminance;";
Peiyong Linfb069302018-04-25 14:34:31 -0700570 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700571
Peiyong Lina296b0c2018-04-30 16:55:29 -0700572 if (needs.hasInputTransformMatrix()) {
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700573 fs << "uniform mat4 inputTransformMatrix;";
Peiyong Lina296b0c2018-04-30 16:55:29 -0700574 fs << R"__SHADER__(
575 highp vec3 InputTransform(const highp vec3 color) {
Chia-I Wu3a4f4012018-10-03 11:10:12 -0700576 return clamp(vec3(inputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700577 }
578 )__SHADER__";
579 } else {
580 fs << R"__SHADER__(
581 highp vec3 InputTransform(const highp vec3 color) {
582 return color;
583 }
584 )__SHADER__";
585 }
586
587 // the transformation from a wider colorspace to a narrower one can
588 // result in >1.0 or <0.0 pixel values
589 if (needs.hasOutputTransformMatrix()) {
590 fs << "uniform mat4 outputTransformMatrix;";
591 fs << R"__SHADER__(
592 highp vec3 OutputTransform(const highp vec3 color) {
593 return clamp(vec3(outputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
594 }
595 )__SHADER__";
596 } else {
597 fs << R"__SHADER__(
598 highp vec3 OutputTransform(const highp vec3 color) {
599 return clamp(color, 0.0, 1.0);
600 }
601 )__SHADER__";
602 }
603
Peiyong Lin2542cb02018-04-30 17:29:53 -0700604 generateEOTF(fs, needs);
605 generateOOTF(fs, needs);
606 generateOETF(fs, needs);
Romain Guy88d37dd2017-05-26 17:57:05 -0700607 }
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800608
Mathias Agopian3f844832013-08-07 21:24:32 -0700609 fs << "void main(void) {" << indent;
610 if (needs.isTexturing()) {
611 fs << "gl_FragColor = texture2D(sampler, outTexCoords);";
Chia-I Wu131d3762018-01-11 14:35:27 -0800612 if (needs.isY410BT2020()) {
613 fs << "gl_FragColor.rgb = convertY410BT2020(gl_FragColor.rgb);";
614 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700615 } else {
chaviw13fdc492017-06-27 12:40:18 -0700616 fs << "gl_FragColor.rgb = color.rgb;";
617 fs << "gl_FragColor.a = 1.0;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700618 }
Mathias Agopian2eaefe12013-08-14 16:33:27 -0700619 if (needs.isOpaque()) {
620 fs << "gl_FragColor.a = 1.0;";
621 }
chaviw13fdc492017-06-27 12:40:18 -0700622 if (needs.hasAlpha()) {
623 // modulate the current alpha value with alpha set
Mathias Agopian3f844832013-08-07 21:24:32 -0700624 if (needs.isPremultiplied()) {
625 // ... and the color too if we're premultiplied
chaviw13fdc492017-06-27 12:40:18 -0700626 fs << "gl_FragColor *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700627 } else {
chaviw13fdc492017-06-27 12:40:18 -0700628 fs << "gl_FragColor.a *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700629 }
630 }
Mathias Agopianff2ed702013-09-01 21:36:12 -0700631
Peiyong Lina296b0c2018-04-30 16:55:29 -0700632 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Mathias Agopianff2ed702013-09-01 21:36:12 -0700633 if (!needs.isOpaque() && needs.isPremultiplied()) {
634 // un-premultiply if needed before linearization
Romain Guy88d37dd2017-05-26 17:57:05 -0700635 // avoid divide by 0 by adding 0.5/256 to the alpha channel
636 fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700637 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700638 fs << "gl_FragColor.rgb = OETF(OutputTransform(OOTF(InputTransform(EOTF(gl_FragColor.rgb)))));";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700639 if (!needs.isOpaque() && needs.isPremultiplied()) {
640 // and re-premultiply if needed after gamma correction
Romain Guy88d37dd2017-05-26 17:57:05 -0700641 fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700642 }
643 }
644
Mathias Agopian3f844832013-08-07 21:24:32 -0700645 fs << dedent << "}";
646 return fs.getString();
647}
648
649Program* ProgramCache::generateProgram(const Key& needs) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700650 ATRACE_CALL();
651
Mathias Agopian3f844832013-08-07 21:24:32 -0700652 // vertex shader
653 String8 vs = generateVertexShader(needs);
654
655 // fragment shader
656 String8 fs = generateFragmentShader(needs);
657
658 Program* program = new Program(needs, vs.string(), fs.string());
659 return program;
660}
661
662void ProgramCache::useProgram(const Description& description) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700663 // generate the key for the shader based on the description
664 Key needs(computeKey(description));
665
Chia-I Wub027f802017-11-29 14:00:52 -0800666 // look-up the program in the cache
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700667 auto it = mCache.find(needs);
668 if (it == mCache.end()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700669 // we didn't find our program, so generate one...
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700670 nsecs_t time = systemTime();
671 it = mCache.emplace(needs, generateProgram(needs)).first;
672 time = systemTime() - time;
Mathias Agopian3f844832013-08-07 21:24:32 -0700673
Chia-I Wu93e14df2018-06-04 10:10:17 -0700674 ALOGV(">>> generated new program: needs=%08X, time=%u ms (%zu programs)", needs.mKey,
675 uint32_t(ns2ms(time)), mCache.size());
Mathias Agopian3f844832013-08-07 21:24:32 -0700676 }
677
678 // here we have a suitable program for this description
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700679 Program* program = it->second;
Mathias Agopian3f844832013-08-07 21:24:32 -0700680 if (program->isValid()) {
681 program->use();
682 program->setUniforms(description);
683 }
684}
685
Peiyong Lin833074a2018-08-28 11:53:54 -0700686} // namespace gl
687} // namespace renderengine
688} // namespace android