blob: 6d431b69fc33ffa56cf3a0636dd581ade54a48da [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
Peiyong Lin13effd12018-07-24 17:01:47 -070080void ProgramCache::primeCache(bool useColorManagement) {
Riley Andrewsa51fafc2014-09-29 13:29:40 -070081 uint32_t shaderCount = 0;
Chia-I Wub027f802017-11-29 14:00:52 -080082 uint32_t keyMask = Key::BLEND_MASK | Key::OPACITY_MASK | Key::ALPHA_MASK | Key::TEXTURE_MASK;
Riley Andrewsa51fafc2014-09-29 13:29:40 -070083 // Prime the cache for all combinations of the above masks,
84 // leaving off the experimental color matrix mask options.
85
86 nsecs_t timeBefore = systemTime();
87 for (uint32_t keyVal = 0; keyVal <= keyMask; keyVal++) {
88 Key shaderKey;
89 shaderKey.set(keyMask, keyVal);
90 uint32_t tex = shaderKey.getTextureTarget();
Chia-I Wub027f802017-11-29 14:00:52 -080091 if (tex != Key::TEXTURE_OFF && tex != Key::TEXTURE_EXT && tex != Key::TEXTURE_2D) {
Riley Andrewsa51fafc2014-09-29 13:29:40 -070092 continue;
93 }
Chia-I Wu56d7b0a2018-10-01 15:13:11 -070094 if (mCache.count(shaderKey) == 0) {
95 mCache.emplace(shaderKey, generateProgram(shaderKey));
Riley Andrewsa51fafc2014-09-29 13:29:40 -070096 shaderCount++;
97 }
98 }
Chia-I Wu93e14df2018-06-04 10:10:17 -070099
100 // Prime for sRGB->P3 conversion
Peiyong Lin13effd12018-07-24 17:01:47 -0700101 if (useColorManagement) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700102 Key shaderKey;
103 shaderKey.set(Key::BLEND_MASK | Key::TEXTURE_MASK | Key::OUTPUT_TRANSFORM_MATRIX_MASK |
104 Key::INPUT_TF_MASK | Key::OUTPUT_TF_MASK,
105 Key::BLEND_PREMULT | Key::TEXTURE_EXT | Key::OUTPUT_TRANSFORM_MATRIX_ON |
106 Key::INPUT_TF_SRGB | Key::OUTPUT_TF_SRGB);
107 for (int i = 0; i < 4; i++) {
108 shaderKey.set(Key::OPACITY_MASK,
109 (i & 1) ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT);
110 shaderKey.set(Key::ALPHA_MASK, (i & 2) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE);
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700111 if (mCache.count(shaderKey) == 0) {
112 mCache.emplace(shaderKey, generateProgram(shaderKey));
Chia-I Wu93e14df2018-06-04 10:10:17 -0700113 shaderCount++;
114 }
115 }
116 }
117
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700118 nsecs_t timeAfter = systemTime();
119 float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6;
120 ALOGD("shader cache generated - %u shaders in %f ms\n", shaderCount, compileTimeMs);
121}
122
Mathias Agopian3f844832013-08-07 21:24:32 -0700123ProgramCache::Key ProgramCache::computeKey(const Description& description) {
124 Key needs;
125 needs.set(Key::TEXTURE_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700126 !description.textureEnabled
Chia-I Wub027f802017-11-29 14:00:52 -0800127 ? Key::TEXTURE_OFF
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700128 : description.texture.getTextureTarget() == GL_TEXTURE_EXTERNAL_OES
Chia-I Wub027f802017-11-29 14:00:52 -0800129 ? Key::TEXTURE_EXT
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700130 : description.texture.getTextureTarget() == GL_TEXTURE_2D
Chia-I Wub027f802017-11-29 14:00:52 -0800131 ? Key::TEXTURE_2D
132 : Key::TEXTURE_OFF)
133 .set(Key::ALPHA_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700134 (description.color.a < 1) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE)
Chia-I Wub027f802017-11-29 14:00:52 -0800135 .set(Key::BLEND_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700136 description.isPremultipliedAlpha ? Key::BLEND_PREMULT : Key::BLEND_NORMAL)
Chia-I Wub027f802017-11-29 14:00:52 -0800137 .set(Key::OPACITY_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700138 description.isOpaque ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT)
Peiyong Lina296b0c2018-04-30 16:55:29 -0700139 .set(Key::Key::INPUT_TRANSFORM_MATRIX_MASK,
140 description.hasInputTransformMatrix() ?
141 Key::INPUT_TRANSFORM_MATRIX_ON : Key::INPUT_TRANSFORM_MATRIX_OFF)
142 .set(Key::Key::OUTPUT_TRANSFORM_MATRIX_MASK,
Chia-I Wud49d6692018-06-27 07:17:41 +0800143 description.hasOutputTransformMatrix() || description.hasColorMatrix() ?
Peiyong Lina296b0c2018-04-30 16:55:29 -0700144 Key::OUTPUT_TRANSFORM_MATRIX_ON : Key::OUTPUT_TRANSFORM_MATRIX_OFF);
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800145
Chia-I Wu131d3762018-01-11 14:35:27 -0800146 needs.set(Key::Y410_BT2020_MASK,
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700147 description.isY410BT2020 ? Key::Y410_BT2020_ON : Key::Y410_BT2020_OFF);
Chia-I Wu131d3762018-01-11 14:35:27 -0800148
Peiyong Lina296b0c2018-04-30 16:55:29 -0700149 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700150 switch (description.inputTransferFunction) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800151 case Description::TransferFunction::LINEAR:
152 default:
153 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR);
154 break;
155 case Description::TransferFunction::SRGB:
156 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB);
157 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800158 case Description::TransferFunction::ST2084:
159 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_ST2084);
160 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700161 case Description::TransferFunction::HLG:
162 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_HLG);
163 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800164 }
165
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700166 switch (description.outputTransferFunction) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800167 case Description::TransferFunction::LINEAR:
168 default:
169 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR);
170 break;
171 case Description::TransferFunction::SRGB:
172 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB);
173 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800174 case Description::TransferFunction::ST2084:
175 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_ST2084);
176 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700177 case Description::TransferFunction::HLG:
178 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_HLG);
179 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800180 }
181 }
182
Mathias Agopian3f844832013-08-07 21:24:32 -0700183 return needs;
184}
185
Peiyong Lin2542cb02018-04-30 17:29:53 -0700186// Generate EOTF that converts signal values to relative display light,
187// both normalized to [0, 1].
188void ProgramCache::generateEOTF(Formatter& fs, const Key& needs) {
189 switch (needs.getInputTF()) {
190 case Key::INPUT_TF_SRGB:
191 fs << R"__SHADER__(
192 float EOTF_sRGB(float srgb) {
193 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
194 }
195
196 vec3 EOTF_sRGB(const vec3 srgb) {
197 return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
198 }
199
200 vec3 EOTF(const vec3 srgb) {
201 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
202 }
203 )__SHADER__";
204 break;
205 case Key::INPUT_TF_ST2084:
206 fs << R"__SHADER__(
207 vec3 EOTF(const highp vec3 color) {
208 const highp float m1 = (2610.0 / 4096.0) / 4.0;
209 const highp float m2 = (2523.0 / 4096.0) * 128.0;
210 const highp float c1 = (3424.0 / 4096.0);
211 const highp float c2 = (2413.0 / 4096.0) * 32.0;
212 const highp float c3 = (2392.0 / 4096.0) * 32.0;
213
214 highp vec3 tmp = pow(color, 1.0 / vec3(m2));
215 tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp);
216 return pow(tmp, 1.0 / vec3(m1));
217 }
218 )__SHADER__";
219 break;
220 case Key::INPUT_TF_HLG:
221 fs << R"__SHADER__(
222 highp float EOTF_channel(const highp float channel) {
223 const highp float a = 0.17883277;
224 const highp float b = 0.28466892;
225 const highp float c = 0.55991073;
226 return channel <= 0.5 ? channel * channel / 3.0 :
227 (exp((channel - c) / a) + b) / 12.0;
228 }
229
230 vec3 EOTF(const highp vec3 color) {
231 return vec3(EOTF_channel(color.r), EOTF_channel(color.g),
232 EOTF_channel(color.b));
233 }
234 )__SHADER__";
235 break;
236 default:
237 fs << R"__SHADER__(
238 vec3 EOTF(const vec3 linear) {
239 return linear;
240 }
241 )__SHADER__";
242 break;
243 }
244}
245
Peiyong Lin53f320e2018-04-23 17:31:06 -0700246void ProgramCache::generateToneMappingProcess(Formatter& fs, const Key& needs) {
247 // Convert relative light to absolute light.
248 switch (needs.getInputTF()) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700249 case Key::INPUT_TF_ST2084:
250 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700251 highp vec3 ScaleLuminance(highp vec3 color) {
252 return color * 10000.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700253 }
254 )__SHADER__";
255 break;
256 case Key::INPUT_TF_HLG:
257 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700258 highp vec3 ScaleLuminance(highp vec3 color) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700259 // The formula is:
260 // alpha * pow(Y, gamma - 1.0) * color + beta;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700261 // where alpha is 1000.0, gamma is 1.2, beta is 0.0.
262 return color * 1000.0 * pow(color.y, 0.2);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700263 }
264 )__SHADER__";
265 break;
266 default:
267 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700268 highp vec3 ScaleLuminance(highp vec3 color) {
269 return color * displayMaxLuminance;
270 }
271 )__SHADER__";
272 break;
273 }
274
275 // Tone map absolute light to display luminance range.
276 switch (needs.getInputTF()) {
277 case Key::INPUT_TF_ST2084:
278 case Key::INPUT_TF_HLG:
279 switch (needs.getOutputTF()) {
280 case Key::OUTPUT_TF_HLG:
281 // Right now when mixed PQ and HLG contents are presented,
282 // HLG content will always be converted to PQ. However, for
283 // completeness, we simply clamp the value to [0.0, 1000.0].
284 fs << R"__SHADER__(
285 highp vec3 ToneMap(highp vec3 color) {
286 return clamp(color, 0.0, 1000.0);
287 }
288 )__SHADER__";
289 break;
290 case Key::OUTPUT_TF_ST2084:
291 fs << R"__SHADER__(
292 highp vec3 ToneMap(highp vec3 color) {
293 return color;
294 }
295 )__SHADER__";
296 break;
297 default:
298 fs << R"__SHADER__(
299 highp vec3 ToneMap(highp vec3 color) {
300 const float maxMasteringLumi = 1000.0;
301 const float maxContentLumi = 1000.0;
302 const float maxInLumi = min(maxMasteringLumi, maxContentLumi);
303 float maxOutLumi = displayMaxLuminance;
304
305 float nits = color.y;
306
307 // clamp to max input luminance
308 nits = clamp(nits, 0.0, maxInLumi);
309
310 // scale [0.0, maxInLumi] to [0.0, maxOutLumi]
311 if (maxInLumi <= maxOutLumi) {
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700312 return color * (maxOutLumi / maxInLumi);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700313 } else {
314 // three control points
315 const float x0 = 10.0;
316 const float y0 = 17.0;
317 float x1 = maxOutLumi * 0.75;
318 float y1 = x1;
319 float x2 = x1 + (maxInLumi - x1) / 2.0;
320 float y2 = y1 + (maxOutLumi - y1) * 0.75;
321
322 // horizontal distances between the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700323 float h12 = x2 - x1;
324 float h23 = maxInLumi - x2;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700325 // tangents at the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700326 float m1 = (y2 - y1) / h12;
327 float m3 = (maxOutLumi - y2) / h23;
328 float m2 = (m1 + m3) / 2.0;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700329
330 if (nits < x0) {
331 // scale [0.0, x0] to [0.0, y0] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700332 float slope = y0 / x0;
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700333 return color * slope;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700334 } else if (nits < x1) {
335 // scale [x0, x1] to [y0, y1] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700336 float slope = (y1 - y0) / (x1 - x0);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700337 nits = y0 + (nits - x0) * slope;
338 } else if (nits < x2) {
339 // scale [x1, x2] to [y1, y2] using Hermite interp
340 float t = (nits - x1) / h12;
341 nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) +
342 (y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t;
343 } else {
344 // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp
345 float t = (nits - x2) / h23;
346 nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) +
347 (maxOutLumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t;
348 }
349 }
350
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700351 // color.y is greater than x0 and is thus non-zero
352 return color * (nits / color.y);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700353 }
354 )__SHADER__";
355 break;
356 }
357 break;
358 default:
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700359 // inverse tone map; the output luminance can be up to maxOutLumi.
Peiyong Lin53f320e2018-04-23 17:31:06 -0700360 fs << R"__SHADER__(
361 highp vec3 ToneMap(highp vec3 color) {
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700362 const float maxOutLumi = 3000.0;
363
364 const float x0 = 5.0;
365 const float y0 = 2.5;
366 float x1 = displayMaxLuminance * 0.7;
367 float y1 = maxOutLumi * 0.15;
368 float x2 = displayMaxLuminance * 0.9;
369 float y2 = maxOutLumi * 0.45;
370 float x3 = displayMaxLuminance;
371 float y3 = maxOutLumi;
372
373 float c1 = y1 / 3.0;
374 float c2 = y2 / 2.0;
375 float c3 = y3 / 1.5;
376
377 float nits = color.y;
378
379 float scale;
380 if (nits <= x0) {
381 // scale [0.0, x0] to [0.0, y0] linearly
382 const float slope = y0 / x0;
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700383 return color * slope;
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700384 } else if (nits <= x1) {
385 // scale [x0, x1] to [y0, y1] using a curve
386 float t = (nits - x0) / (x1 - x0);
387 nits = (1.0 - t) * (1.0 - t) * y0 + 2.0 * (1.0 - t) * t * c1 + t * t * y1;
388 } else if (nits <= x2) {
389 // scale [x1, x2] to [y1, y2] using a curve
390 float t = (nits - x1) / (x2 - x1);
391 nits = (1.0 - t) * (1.0 - t) * y1 + 2.0 * (1.0 - t) * t * c2 + t * t * y2;
392 } else {
393 // scale [x2, x3] to [y2, y3] using a curve
394 float t = (nits - x2) / (x3 - x2);
395 nits = (1.0 - t) * (1.0 - t) * y2 + 2.0 * (1.0 - t) * t * c3 + t * t * y3;
396 }
397
Chia-I Wue01fc2c2018-10-03 11:32:27 -0700398 // color.y is greater than x0 and is thus non-zero
399 return color * (nits / color.y);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700400 }
401 )__SHADER__";
402 break;
403 }
Peiyong Lin53f320e2018-04-23 17:31:06 -0700404
405 // convert absolute light to relative light.
406 switch (needs.getOutputTF()) {
407 case Key::OUTPUT_TF_ST2084:
408 fs << R"__SHADER__(
409 highp vec3 NormalizeLuminance(highp vec3 color) {
410 return color / 10000.0;
411 }
412 )__SHADER__";
413 break;
414 case Key::OUTPUT_TF_HLG:
415 fs << R"__SHADER__(
416 highp vec3 NormalizeLuminance(highp vec3 color) {
417 return color / 1000.0 * pow(color.y / 1000.0, -0.2 / 1.2);
418 }
419 )__SHADER__";
420 break;
421 default:
422 fs << R"__SHADER__(
423 highp vec3 NormalizeLuminance(highp vec3 color) {
424 return color / displayMaxLuminance;
425 }
426 )__SHADER__";
427 break;
428 }
429}
430
431// Generate OOTF that modifies the relative scence light to relative display light.
432void ProgramCache::generateOOTF(Formatter& fs, const ProgramCache::Key& needs) {
433 if (!needs.needsToneMapping()) {
434 fs << R"__SHADER__(
435 highp vec3 OOTF(const highp vec3 color) {
436 return color;
437 }
438 )__SHADER__";
439 } else {
440 generateToneMappingProcess(fs, needs);
441 fs << R"__SHADER__(
442 highp vec3 OOTF(const highp vec3 color) {
443 return NormalizeLuminance(ToneMap(ScaleLuminance(color)));
444 }
445 )__SHADER__";
446 }
Peiyong Lin2542cb02018-04-30 17:29:53 -0700447}
448
449// Generate OETF that converts relative display light to signal values,
450// both normalized to [0, 1]
451void ProgramCache::generateOETF(Formatter& fs, const Key& needs) {
452 switch (needs.getOutputTF()) {
453 case Key::OUTPUT_TF_SRGB:
454 fs << R"__SHADER__(
455 float OETF_sRGB(const float linear) {
456 return linear <= 0.0031308 ?
457 linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
458 }
459
460 vec3 OETF_sRGB(const vec3 linear) {
461 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
462 }
463
464 vec3 OETF(const vec3 linear) {
465 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
466 }
467 )__SHADER__";
468 break;
469 case Key::OUTPUT_TF_ST2084:
470 fs << R"__SHADER__(
471 vec3 OETF(const vec3 linear) {
Peiyong Lin834be492018-05-18 15:12:55 -0700472 const highp float m1 = (2610.0 / 4096.0) / 4.0;
473 const highp float m2 = (2523.0 / 4096.0) * 128.0;
474 const highp float c1 = (3424.0 / 4096.0);
475 const highp float c2 = (2413.0 / 4096.0) * 32.0;
476 const highp float c3 = (2392.0 / 4096.0) * 32.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700477
Peiyong Lin834be492018-05-18 15:12:55 -0700478 highp vec3 tmp = pow(linear, vec3(m1));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700479 tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp);
480 return pow(tmp, vec3(m2));
481 }
482 )__SHADER__";
483 break;
484 case Key::OUTPUT_TF_HLG:
485 fs << R"__SHADER__(
486 highp float OETF_channel(const highp float channel) {
487 const highp float a = 0.17883277;
488 const highp float b = 0.28466892;
489 const highp float c = 0.55991073;
490 return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) :
491 a * log(12.0 * channel - b) + c;
492 }
493
494 vec3 OETF(const highp vec3 color) {
495 return vec3(OETF_channel(color.r), OETF_channel(color.g),
496 OETF_channel(color.b));
497 }
498 )__SHADER__";
499 break;
500 default:
501 fs << R"__SHADER__(
502 vec3 OETF(const vec3 linear) {
503 return linear;
504 }
505 )__SHADER__";
506 break;
507 }
508}
509
Mathias Agopian3f844832013-08-07 21:24:32 -0700510String8 ProgramCache::generateVertexShader(const Key& needs) {
511 Formatter vs;
512 if (needs.isTexturing()) {
Chia-I Wub027f802017-11-29 14:00:52 -0800513 vs << "attribute vec4 texCoords;"
514 << "varying vec2 outTexCoords;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700515 }
516 vs << "attribute vec4 position;"
517 << "uniform mat4 projection;"
518 << "uniform mat4 texture;"
Chia-I Wub027f802017-11-29 14:00:52 -0800519 << "void main(void) {" << indent << "gl_Position = projection * position;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700520 if (needs.isTexturing()) {
521 vs << "outTexCoords = (texture * texCoords).st;";
522 }
523 vs << dedent << "}";
524 return vs.getString();
525}
526
527String8 ProgramCache::generateFragmentShader(const Key& needs) {
528 Formatter fs;
529 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
530 fs << "#extension GL_OES_EGL_image_external : require";
531 }
Mathias Agopian458197d2013-08-15 14:56:51 -0700532
533 // default precision is required-ish in fragment shaders
534 fs << "precision mediump float;";
535
Mathias Agopian3f844832013-08-07 21:24:32 -0700536 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
537 fs << "uniform samplerExternalOES sampler;"
538 << "varying vec2 outTexCoords;";
539 } else if (needs.getTextureTarget() == Key::TEXTURE_2D) {
540 fs << "uniform sampler2D sampler;"
541 << "varying vec2 outTexCoords;";
chaviw13fdc492017-06-27 12:40:18 -0700542 }
543
544 if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700545 fs << "uniform vec4 color;";
546 }
chaviw13fdc492017-06-27 12:40:18 -0700547
Chia-I Wu131d3762018-01-11 14:35:27 -0800548 if (needs.isY410BT2020()) {
549 fs << R"__SHADER__(
550 vec3 convertY410BT2020(const vec3 color) {
551 const vec3 offset = vec3(0.0625, 0.5, 0.5);
552 const mat3 transform = mat3(
553 vec3(1.1678, 1.1678, 1.1678),
554 vec3( 0.0, -0.1878, 2.1481),
555 vec3(1.6836, -0.6523, 0.0));
556 // Y is in G, U is in R, and V is in B
557 return clamp(transform * (color.grb - offset), 0.0, 1.0);
558 }
559 )__SHADER__";
560 }
561
Peiyong Lina296b0c2018-04-30 16:55:29 -0700562 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Peiyong Lin53f320e2018-04-23 17:31:06 -0700563 // Currently, display maximum luminance is needed when doing tone mapping.
564 if (needs.needsToneMapping()) {
Chia-I Wu8bbacdf2018-05-04 15:19:37 -0700565 fs << "uniform float displayMaxLuminance;";
Peiyong Linfb069302018-04-25 14:34:31 -0700566 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700567
Peiyong Lina296b0c2018-04-30 16:55:29 -0700568 if (needs.hasInputTransformMatrix()) {
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700569 fs << "uniform mat4 inputTransformMatrix;";
Peiyong Lina296b0c2018-04-30 16:55:29 -0700570 fs << R"__SHADER__(
571 highp vec3 InputTransform(const highp vec3 color) {
Chia-I Wu3a4f4012018-10-03 11:10:12 -0700572 return clamp(vec3(inputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700573 }
574 )__SHADER__";
575 } else {
576 fs << R"__SHADER__(
577 highp vec3 InputTransform(const highp vec3 color) {
578 return color;
579 }
580 )__SHADER__";
581 }
582
583 // the transformation from a wider colorspace to a narrower one can
584 // result in >1.0 or <0.0 pixel values
585 if (needs.hasOutputTransformMatrix()) {
586 fs << "uniform mat4 outputTransformMatrix;";
587 fs << R"__SHADER__(
588 highp vec3 OutputTransform(const highp vec3 color) {
589 return clamp(vec3(outputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
590 }
591 )__SHADER__";
592 } else {
593 fs << R"__SHADER__(
594 highp vec3 OutputTransform(const highp vec3 color) {
595 return clamp(color, 0.0, 1.0);
596 }
597 )__SHADER__";
598 }
599
Peiyong Lin2542cb02018-04-30 17:29:53 -0700600 generateEOTF(fs, needs);
601 generateOOTF(fs, needs);
602 generateOETF(fs, needs);
Romain Guy88d37dd2017-05-26 17:57:05 -0700603 }
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800604
Mathias Agopian3f844832013-08-07 21:24:32 -0700605 fs << "void main(void) {" << indent;
606 if (needs.isTexturing()) {
607 fs << "gl_FragColor = texture2D(sampler, outTexCoords);";
Chia-I Wu131d3762018-01-11 14:35:27 -0800608 if (needs.isY410BT2020()) {
609 fs << "gl_FragColor.rgb = convertY410BT2020(gl_FragColor.rgb);";
610 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700611 } else {
chaviw13fdc492017-06-27 12:40:18 -0700612 fs << "gl_FragColor.rgb = color.rgb;";
613 fs << "gl_FragColor.a = 1.0;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700614 }
Mathias Agopian2eaefe12013-08-14 16:33:27 -0700615 if (needs.isOpaque()) {
616 fs << "gl_FragColor.a = 1.0;";
617 }
chaviw13fdc492017-06-27 12:40:18 -0700618 if (needs.hasAlpha()) {
619 // modulate the current alpha value with alpha set
Mathias Agopian3f844832013-08-07 21:24:32 -0700620 if (needs.isPremultiplied()) {
621 // ... and the color too if we're premultiplied
chaviw13fdc492017-06-27 12:40:18 -0700622 fs << "gl_FragColor *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700623 } else {
chaviw13fdc492017-06-27 12:40:18 -0700624 fs << "gl_FragColor.a *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700625 }
626 }
Mathias Agopianff2ed702013-09-01 21:36:12 -0700627
Peiyong Lina296b0c2018-04-30 16:55:29 -0700628 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Mathias Agopianff2ed702013-09-01 21:36:12 -0700629 if (!needs.isOpaque() && needs.isPremultiplied()) {
630 // un-premultiply if needed before linearization
Romain Guy88d37dd2017-05-26 17:57:05 -0700631 // avoid divide by 0 by adding 0.5/256 to the alpha channel
632 fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700633 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700634 fs << "gl_FragColor.rgb = OETF(OutputTransform(OOTF(InputTransform(EOTF(gl_FragColor.rgb)))));";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700635 if (!needs.isOpaque() && needs.isPremultiplied()) {
636 // and re-premultiply if needed after gamma correction
Romain Guy88d37dd2017-05-26 17:57:05 -0700637 fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700638 }
639 }
640
Mathias Agopian3f844832013-08-07 21:24:32 -0700641 fs << dedent << "}";
642 return fs.getString();
643}
644
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700645std::unique_ptr<Program> ProgramCache::generateProgram(const Key& needs) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700646 ATRACE_CALL();
647
Mathias Agopian3f844832013-08-07 21:24:32 -0700648 // vertex shader
649 String8 vs = generateVertexShader(needs);
650
651 // fragment shader
652 String8 fs = generateFragmentShader(needs);
653
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700654 return std::make_unique<Program>(needs, vs.string(), fs.string());
Mathias Agopian3f844832013-08-07 21:24:32 -0700655}
656
657void ProgramCache::useProgram(const Description& description) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700658 // generate the key for the shader based on the description
659 Key needs(computeKey(description));
660
Chia-I Wub027f802017-11-29 14:00:52 -0800661 // look-up the program in the cache
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700662 auto it = mCache.find(needs);
663 if (it == mCache.end()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700664 // we didn't find our program, so generate one...
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700665 nsecs_t time = systemTime();
666 it = mCache.emplace(needs, generateProgram(needs)).first;
667 time = systemTime() - time;
Mathias Agopian3f844832013-08-07 21:24:32 -0700668
Chia-I Wu93e14df2018-06-04 10:10:17 -0700669 ALOGV(">>> generated new program: needs=%08X, time=%u ms (%zu programs)", needs.mKey,
670 uint32_t(ns2ms(time)), mCache.size());
Mathias Agopian3f844832013-08-07 21:24:32 -0700671 }
672
673 // here we have a suitable program for this description
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700674 std::unique_ptr<Program>& program = it->second;
Mathias Agopian3f844832013-08-07 21:24:32 -0700675 if (program->isValid()) {
676 program->use();
677 program->setUniforms(description);
678 }
679}
680
Peiyong Lin833074a2018-08-28 11:53:54 -0700681} // namespace gl
682} // namespace renderengine
683} // namespace android