blob: a19c1f1dcca951efae97303e6f72e7a4c72dfaa5 [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>
Peiyong Lin833074a2018-08-28 11:53:54 -070023#include <renderengine/private/Description.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070024#include <utils/String8.h>
Chia-I Wu93e14df2018-06-04 10:10:17 -070025#include <utils/Trace.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070026#include "Program.h"
27
28ANDROID_SINGLETON_STATIC_INSTANCE(android::renderengine::gl::ProgramCache)
Mathias Agopian3f844832013-08-07 21:24:32 -070029
Mathias Agopian3f844832013-08-07 21:24:32 -070030namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -070031namespace renderengine {
32namespace gl {
Mathias Agopian3f844832013-08-07 21:24:32 -070033
Mathias Agopian3f844832013-08-07 21:24:32 -070034/*
35 * A simple formatter class to automatically add the endl and
36 * manage the indentation.
37 */
38
39class Formatter;
40static Formatter& indent(Formatter& f);
41static Formatter& dedent(Formatter& f);
42
43class Formatter {
44 String8 mString;
45 int mIndent;
46 typedef Formatter& (*FormaterManipFunc)(Formatter&);
47 friend Formatter& indent(Formatter& f);
48 friend Formatter& dedent(Formatter& f);
Chia-I Wub027f802017-11-29 14:00:52 -080049
Mathias Agopian3f844832013-08-07 21:24:32 -070050public:
Andy McFadden892f22d2013-08-15 10:05:01 -070051 Formatter() : mIndent(0) {}
52
Chia-I Wub027f802017-11-29 14:00:52 -080053 String8 getString() const { return mString; }
Mathias Agopian3f844832013-08-07 21:24:32 -070054
Chia-I Wub027f802017-11-29 14:00:52 -080055 friend Formatter& operator<<(Formatter& out, const char* in) {
56 for (int i = 0; i < out.mIndent; i++) {
Mathias Agopian3f844832013-08-07 21:24:32 -070057 out.mString.append(" ");
58 }
59 out.mString.append(in);
60 out.mString.append("\n");
61 return out;
62 }
Chia-I Wub027f802017-11-29 14:00:52 -080063 friend inline Formatter& operator<<(Formatter& out, const String8& in) {
64 return operator<<(out, in.string());
Mathias Agopian3f844832013-08-07 21:24:32 -070065 }
66 friend inline Formatter& operator<<(Formatter& to, FormaterManipFunc func) {
67 return (*func)(to);
68 }
69};
70Formatter& indent(Formatter& f) {
71 f.mIndent++;
72 return f;
73}
74Formatter& dedent(Formatter& f) {
75 f.mIndent--;
76 return f;
77}
78
Chia-I Wu93e14df2018-06-04 10:10:17 -070079ProgramCache::ProgramCache() {}
Mathias Agopian3f844832013-08-07 21:24:32 -070080
Chia-I Wub027f802017-11-29 14:00:52 -080081ProgramCache::~ProgramCache() {}
Mathias Agopian3f844832013-08-07 21:24:32 -070082
Peiyong Lin13effd12018-07-24 17:01:47 -070083void ProgramCache::primeCache(bool useColorManagement) {
Riley Andrewsa51fafc2014-09-29 13:29:40 -070084 uint32_t shaderCount = 0;
Chia-I Wub027f802017-11-29 14:00:52 -080085 uint32_t keyMask = Key::BLEND_MASK | Key::OPACITY_MASK | Key::ALPHA_MASK | Key::TEXTURE_MASK;
Riley Andrewsa51fafc2014-09-29 13:29:40 -070086 // Prime the cache for all combinations of the above masks,
87 // leaving off the experimental color matrix mask options.
88
89 nsecs_t timeBefore = systemTime();
90 for (uint32_t keyVal = 0; keyVal <= keyMask; keyVal++) {
91 Key shaderKey;
92 shaderKey.set(keyMask, keyVal);
93 uint32_t tex = shaderKey.getTextureTarget();
Chia-I Wub027f802017-11-29 14:00:52 -080094 if (tex != Key::TEXTURE_OFF && tex != Key::TEXTURE_EXT && tex != Key::TEXTURE_2D) {
Riley Andrewsa51fafc2014-09-29 13:29:40 -070095 continue;
96 }
97 Program* program = mCache.valueFor(shaderKey);
Peiyong Lin566a3b42018-01-09 18:22:43 -080098 if (program == nullptr) {
Riley Andrewsa51fafc2014-09-29 13:29:40 -070099 program = generateProgram(shaderKey);
100 mCache.add(shaderKey, program);
101 shaderCount++;
102 }
103 }
Chia-I Wu93e14df2018-06-04 10:10:17 -0700104
105 // Prime for sRGB->P3 conversion
Peiyong Lin13effd12018-07-24 17:01:47 -0700106 if (useColorManagement) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700107 Key shaderKey;
108 shaderKey.set(Key::BLEND_MASK | Key::TEXTURE_MASK | Key::OUTPUT_TRANSFORM_MATRIX_MASK |
109 Key::INPUT_TF_MASK | Key::OUTPUT_TF_MASK,
110 Key::BLEND_PREMULT | Key::TEXTURE_EXT | Key::OUTPUT_TRANSFORM_MATRIX_ON |
111 Key::INPUT_TF_SRGB | Key::OUTPUT_TF_SRGB);
112 for (int i = 0; i < 4; i++) {
113 shaderKey.set(Key::OPACITY_MASK,
114 (i & 1) ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT);
115 shaderKey.set(Key::ALPHA_MASK, (i & 2) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE);
116 Program* program = mCache.valueFor(shaderKey);
117 if (program == nullptr) {
118 program = generateProgram(shaderKey);
119 mCache.add(shaderKey, program);
120 shaderCount++;
121 }
122 }
123 }
124
Riley Andrewsa51fafc2014-09-29 13:29:40 -0700125 nsecs_t timeAfter = systemTime();
126 float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6;
127 ALOGD("shader cache generated - %u shaders in %f ms\n", shaderCount, compileTimeMs);
128}
129
Mathias Agopian3f844832013-08-07 21:24:32 -0700130ProgramCache::Key ProgramCache::computeKey(const Description& description) {
131 Key needs;
132 needs.set(Key::TEXTURE_MASK,
Chia-I Wub027f802017-11-29 14:00:52 -0800133 !description.mTextureEnabled
134 ? Key::TEXTURE_OFF
135 : description.mTexture.getTextureTarget() == GL_TEXTURE_EXTERNAL_OES
136 ? Key::TEXTURE_EXT
137 : description.mTexture.getTextureTarget() == GL_TEXTURE_2D
138 ? Key::TEXTURE_2D
139 : Key::TEXTURE_OFF)
140 .set(Key::ALPHA_MASK,
141 (description.mColor.a < 1) ? Key::ALPHA_LT_ONE : Key::ALPHA_EQ_ONE)
142 .set(Key::BLEND_MASK,
143 description.mPremultipliedAlpha ? Key::BLEND_PREMULT : Key::BLEND_NORMAL)
144 .set(Key::OPACITY_MASK,
145 description.mOpaque ? Key::OPACITY_OPAQUE : Key::OPACITY_TRANSLUCENT)
Peiyong Lina296b0c2018-04-30 16:55:29 -0700146 .set(Key::Key::INPUT_TRANSFORM_MATRIX_MASK,
147 description.hasInputTransformMatrix() ?
148 Key::INPUT_TRANSFORM_MATRIX_ON : Key::INPUT_TRANSFORM_MATRIX_OFF)
149 .set(Key::Key::OUTPUT_TRANSFORM_MATRIX_MASK,
Chia-I Wud49d6692018-06-27 07:17:41 +0800150 description.hasOutputTransformMatrix() || description.hasColorMatrix() ?
Peiyong Lina296b0c2018-04-30 16:55:29 -0700151 Key::OUTPUT_TRANSFORM_MATRIX_ON : Key::OUTPUT_TRANSFORM_MATRIX_OFF);
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800152
Chia-I Wu131d3762018-01-11 14:35:27 -0800153 needs.set(Key::Y410_BT2020_MASK,
154 description.mY410BT2020 ? Key::Y410_BT2020_ON : Key::Y410_BT2020_OFF);
155
Peiyong Lina296b0c2018-04-30 16:55:29 -0700156 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800157 switch (description.mInputTransferFunction) {
158 case Description::TransferFunction::LINEAR:
159 default:
160 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_LINEAR);
161 break;
162 case Description::TransferFunction::SRGB:
163 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_SRGB);
164 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800165 case Description::TransferFunction::ST2084:
166 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_ST2084);
167 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700168 case Description::TransferFunction::HLG:
169 needs.set(Key::INPUT_TF_MASK, Key::INPUT_TF_HLG);
170 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800171 }
172
173 switch (description.mOutputTransferFunction) {
174 case Description::TransferFunction::LINEAR:
175 default:
176 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_LINEAR);
177 break;
178 case Description::TransferFunction::SRGB:
179 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_SRGB);
180 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800181 case Description::TransferFunction::ST2084:
182 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_ST2084);
183 break;
Peiyong Lina3fb7d62018-04-11 17:41:47 -0700184 case Description::TransferFunction::HLG:
185 needs.set(Key::OUTPUT_TF_MASK, Key::OUTPUT_TF_HLG);
186 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800187 }
188 }
189
Mathias Agopian3f844832013-08-07 21:24:32 -0700190 return needs;
191}
192
Peiyong Lin2542cb02018-04-30 17:29:53 -0700193// Generate EOTF that converts signal values to relative display light,
194// both normalized to [0, 1].
195void ProgramCache::generateEOTF(Formatter& fs, const Key& needs) {
196 switch (needs.getInputTF()) {
197 case Key::INPUT_TF_SRGB:
198 fs << R"__SHADER__(
199 float EOTF_sRGB(float srgb) {
200 return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4);
201 }
202
203 vec3 EOTF_sRGB(const vec3 srgb) {
204 return vec3(EOTF_sRGB(srgb.r), EOTF_sRGB(srgb.g), EOTF_sRGB(srgb.b));
205 }
206
207 vec3 EOTF(const vec3 srgb) {
208 return sign(srgb.rgb) * EOTF_sRGB(abs(srgb.rgb));
209 }
210 )__SHADER__";
211 break;
212 case Key::INPUT_TF_ST2084:
213 fs << R"__SHADER__(
214 vec3 EOTF(const highp vec3 color) {
215 const highp float m1 = (2610.0 / 4096.0) / 4.0;
216 const highp float m2 = (2523.0 / 4096.0) * 128.0;
217 const highp float c1 = (3424.0 / 4096.0);
218 const highp float c2 = (2413.0 / 4096.0) * 32.0;
219 const highp float c3 = (2392.0 / 4096.0) * 32.0;
220
221 highp vec3 tmp = pow(color, 1.0 / vec3(m2));
222 tmp = max(tmp - c1, 0.0) / (c2 - c3 * tmp);
223 return pow(tmp, 1.0 / vec3(m1));
224 }
225 )__SHADER__";
226 break;
227 case Key::INPUT_TF_HLG:
228 fs << R"__SHADER__(
229 highp float EOTF_channel(const highp float channel) {
230 const highp float a = 0.17883277;
231 const highp float b = 0.28466892;
232 const highp float c = 0.55991073;
233 return channel <= 0.5 ? channel * channel / 3.0 :
234 (exp((channel - c) / a) + b) / 12.0;
235 }
236
237 vec3 EOTF(const highp vec3 color) {
238 return vec3(EOTF_channel(color.r), EOTF_channel(color.g),
239 EOTF_channel(color.b));
240 }
241 )__SHADER__";
242 break;
243 default:
244 fs << R"__SHADER__(
245 vec3 EOTF(const vec3 linear) {
246 return linear;
247 }
248 )__SHADER__";
249 break;
250 }
251}
252
Peiyong Lin53f320e2018-04-23 17:31:06 -0700253void ProgramCache::generateToneMappingProcess(Formatter& fs, const Key& needs) {
254 // Convert relative light to absolute light.
255 switch (needs.getInputTF()) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700256 case Key::INPUT_TF_ST2084:
257 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700258 highp vec3 ScaleLuminance(highp vec3 color) {
259 return color * 10000.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700260 }
261 )__SHADER__";
262 break;
263 case Key::INPUT_TF_HLG:
264 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700265 highp vec3 ScaleLuminance(highp vec3 color) {
Peiyong Lin2542cb02018-04-30 17:29:53 -0700266 // The formula is:
267 // alpha * pow(Y, gamma - 1.0) * color + beta;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700268 // where alpha is 1000.0, gamma is 1.2, beta is 0.0.
269 return color * 1000.0 * pow(color.y, 0.2);
Peiyong Lin2542cb02018-04-30 17:29:53 -0700270 }
271 )__SHADER__";
272 break;
273 default:
274 fs << R"__SHADER__(
Peiyong Lin53f320e2018-04-23 17:31:06 -0700275 highp vec3 ScaleLuminance(highp vec3 color) {
276 return color * displayMaxLuminance;
277 }
278 )__SHADER__";
279 break;
280 }
281
282 // Tone map absolute light to display luminance range.
283 switch (needs.getInputTF()) {
284 case Key::INPUT_TF_ST2084:
285 case Key::INPUT_TF_HLG:
286 switch (needs.getOutputTF()) {
287 case Key::OUTPUT_TF_HLG:
288 // Right now when mixed PQ and HLG contents are presented,
289 // HLG content will always be converted to PQ. However, for
290 // completeness, we simply clamp the value to [0.0, 1000.0].
291 fs << R"__SHADER__(
292 highp vec3 ToneMap(highp vec3 color) {
293 return clamp(color, 0.0, 1000.0);
294 }
295 )__SHADER__";
296 break;
297 case Key::OUTPUT_TF_ST2084:
298 fs << R"__SHADER__(
299 highp vec3 ToneMap(highp vec3 color) {
300 return color;
301 }
302 )__SHADER__";
303 break;
304 default:
305 fs << R"__SHADER__(
306 highp vec3 ToneMap(highp vec3 color) {
307 const float maxMasteringLumi = 1000.0;
308 const float maxContentLumi = 1000.0;
309 const float maxInLumi = min(maxMasteringLumi, maxContentLumi);
310 float maxOutLumi = displayMaxLuminance;
311
312 float nits = color.y;
313
314 // clamp to max input luminance
315 nits = clamp(nits, 0.0, maxInLumi);
316
317 // scale [0.0, maxInLumi] to [0.0, maxOutLumi]
318 if (maxInLumi <= maxOutLumi) {
319 nits *= maxOutLumi / maxInLumi;
320 } else {
321 // three control points
322 const float x0 = 10.0;
323 const float y0 = 17.0;
324 float x1 = maxOutLumi * 0.75;
325 float y1 = x1;
326 float x2 = x1 + (maxInLumi - x1) / 2.0;
327 float y2 = y1 + (maxOutLumi - y1) * 0.75;
328
329 // horizontal distances between the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700330 float h12 = x2 - x1;
331 float h23 = maxInLumi - x2;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700332 // tangents at the last three control points
Peiyong Linf2410b62018-05-14 16:31:17 -0700333 float m1 = (y2 - y1) / h12;
334 float m3 = (maxOutLumi - y2) / h23;
335 float m2 = (m1 + m3) / 2.0;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700336
337 if (nits < x0) {
338 // scale [0.0, x0] to [0.0, y0] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700339 float slope = y0 / x0;
Peiyong Lin53f320e2018-04-23 17:31:06 -0700340 nits *= slope;
341 } else if (nits < x1) {
342 // scale [x0, x1] to [y0, y1] linearly
Peiyong Linf2410b62018-05-14 16:31:17 -0700343 float slope = (y1 - y0) / (x1 - x0);
Peiyong Lin53f320e2018-04-23 17:31:06 -0700344 nits = y0 + (nits - x0) * slope;
345 } else if (nits < x2) {
346 // scale [x1, x2] to [y1, y2] using Hermite interp
347 float t = (nits - x1) / h12;
348 nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) +
349 (y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t;
350 } else {
351 // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp
352 float t = (nits - x2) / h23;
353 nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) +
354 (maxOutLumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t;
355 }
356 }
357
358 return color * (nits / max(1e-6, color.y));
359 }
360 )__SHADER__";
361 break;
362 }
363 break;
364 default:
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700365 // inverse tone map; the output luminance can be up to maxOutLumi.
Peiyong Lin53f320e2018-04-23 17:31:06 -0700366 fs << R"__SHADER__(
367 highp vec3 ToneMap(highp vec3 color) {
Peiyong Lin9a1b6552018-05-23 16:52:29 -0700368 const float maxOutLumi = 3000.0;
369
370 const float x0 = 5.0;
371 const float y0 = 2.5;
372 float x1 = displayMaxLuminance * 0.7;
373 float y1 = maxOutLumi * 0.15;
374 float x2 = displayMaxLuminance * 0.9;
375 float y2 = maxOutLumi * 0.45;
376 float x3 = displayMaxLuminance;
377 float y3 = maxOutLumi;
378
379 float c1 = y1 / 3.0;
380 float c2 = y2 / 2.0;
381 float c3 = y3 / 1.5;
382
383 float nits = color.y;
384
385 float scale;
386 if (nits <= x0) {
387 // scale [0.0, x0] to [0.0, y0] linearly
388 const float slope = y0 / x0;
389 nits *= slope;
390 } else if (nits <= x1) {
391 // scale [x0, x1] to [y0, y1] using a curve
392 float t = (nits - x0) / (x1 - x0);
393 nits = (1.0 - t) * (1.0 - t) * y0 + 2.0 * (1.0 - t) * t * c1 + t * t * y1;
394 } else if (nits <= x2) {
395 // scale [x1, x2] to [y1, y2] using a curve
396 float t = (nits - x1) / (x2 - x1);
397 nits = (1.0 - t) * (1.0 - t) * y1 + 2.0 * (1.0 - t) * t * c2 + t * t * y2;
398 } else {
399 // scale [x2, x3] to [y2, y3] using a curve
400 float t = (nits - x2) / (x3 - x2);
401 nits = (1.0 - t) * (1.0 - t) * y2 + 2.0 * (1.0 - t) * t * c3 + t * t * y3;
402 }
403
404 return color * (nits / max(1e-6, color.y));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700405 }
406 )__SHADER__";
407 break;
408 }
Peiyong Lin53f320e2018-04-23 17:31:06 -0700409
410 // convert absolute light to relative light.
411 switch (needs.getOutputTF()) {
412 case Key::OUTPUT_TF_ST2084:
413 fs << R"__SHADER__(
414 highp vec3 NormalizeLuminance(highp vec3 color) {
415 return color / 10000.0;
416 }
417 )__SHADER__";
418 break;
419 case Key::OUTPUT_TF_HLG:
420 fs << R"__SHADER__(
421 highp vec3 NormalizeLuminance(highp vec3 color) {
422 return color / 1000.0 * pow(color.y / 1000.0, -0.2 / 1.2);
423 }
424 )__SHADER__";
425 break;
426 default:
427 fs << R"__SHADER__(
428 highp vec3 NormalizeLuminance(highp vec3 color) {
429 return color / displayMaxLuminance;
430 }
431 )__SHADER__";
432 break;
433 }
434}
435
436// Generate OOTF that modifies the relative scence light to relative display light.
437void ProgramCache::generateOOTF(Formatter& fs, const ProgramCache::Key& needs) {
438 if (!needs.needsToneMapping()) {
439 fs << R"__SHADER__(
440 highp vec3 OOTF(const highp vec3 color) {
441 return color;
442 }
443 )__SHADER__";
444 } else {
445 generateToneMappingProcess(fs, needs);
446 fs << R"__SHADER__(
447 highp vec3 OOTF(const highp vec3 color) {
448 return NormalizeLuminance(ToneMap(ScaleLuminance(color)));
449 }
450 )__SHADER__";
451 }
Peiyong Lin2542cb02018-04-30 17:29:53 -0700452}
453
454// Generate OETF that converts relative display light to signal values,
455// both normalized to [0, 1]
456void ProgramCache::generateOETF(Formatter& fs, const Key& needs) {
457 switch (needs.getOutputTF()) {
458 case Key::OUTPUT_TF_SRGB:
459 fs << R"__SHADER__(
460 float OETF_sRGB(const float linear) {
461 return linear <= 0.0031308 ?
462 linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055;
463 }
464
465 vec3 OETF_sRGB(const vec3 linear) {
466 return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b));
467 }
468
469 vec3 OETF(const vec3 linear) {
470 return sign(linear.rgb) * OETF_sRGB(abs(linear.rgb));
471 }
472 )__SHADER__";
473 break;
474 case Key::OUTPUT_TF_ST2084:
475 fs << R"__SHADER__(
476 vec3 OETF(const vec3 linear) {
Peiyong Lin834be492018-05-18 15:12:55 -0700477 const highp float m1 = (2610.0 / 4096.0) / 4.0;
478 const highp float m2 = (2523.0 / 4096.0) * 128.0;
479 const highp float c1 = (3424.0 / 4096.0);
480 const highp float c2 = (2413.0 / 4096.0) * 32.0;
481 const highp float c3 = (2392.0 / 4096.0) * 32.0;
Peiyong Lin2542cb02018-04-30 17:29:53 -0700482
Peiyong Lin834be492018-05-18 15:12:55 -0700483 highp vec3 tmp = pow(linear, vec3(m1));
Peiyong Lin2542cb02018-04-30 17:29:53 -0700484 tmp = (c1 + c2 * tmp) / (1.0 + c3 * tmp);
485 return pow(tmp, vec3(m2));
486 }
487 )__SHADER__";
488 break;
489 case Key::OUTPUT_TF_HLG:
490 fs << R"__SHADER__(
491 highp float OETF_channel(const highp float channel) {
492 const highp float a = 0.17883277;
493 const highp float b = 0.28466892;
494 const highp float c = 0.55991073;
495 return channel <= 1.0 / 12.0 ? sqrt(3.0 * channel) :
496 a * log(12.0 * channel - b) + c;
497 }
498
499 vec3 OETF(const highp vec3 color) {
500 return vec3(OETF_channel(color.r), OETF_channel(color.g),
501 OETF_channel(color.b));
502 }
503 )__SHADER__";
504 break;
505 default:
506 fs << R"__SHADER__(
507 vec3 OETF(const vec3 linear) {
508 return linear;
509 }
510 )__SHADER__";
511 break;
512 }
513}
514
Mathias Agopian3f844832013-08-07 21:24:32 -0700515String8 ProgramCache::generateVertexShader(const Key& needs) {
516 Formatter vs;
517 if (needs.isTexturing()) {
Chia-I Wub027f802017-11-29 14:00:52 -0800518 vs << "attribute vec4 texCoords;"
519 << "varying vec2 outTexCoords;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700520 }
521 vs << "attribute vec4 position;"
522 << "uniform mat4 projection;"
523 << "uniform mat4 texture;"
Chia-I Wub027f802017-11-29 14:00:52 -0800524 << "void main(void) {" << indent << "gl_Position = projection * position;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700525 if (needs.isTexturing()) {
526 vs << "outTexCoords = (texture * texCoords).st;";
527 }
528 vs << dedent << "}";
529 return vs.getString();
530}
531
532String8 ProgramCache::generateFragmentShader(const Key& needs) {
533 Formatter fs;
534 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
535 fs << "#extension GL_OES_EGL_image_external : require";
536 }
Mathias Agopian458197d2013-08-15 14:56:51 -0700537
538 // default precision is required-ish in fragment shaders
539 fs << "precision mediump float;";
540
Mathias Agopian3f844832013-08-07 21:24:32 -0700541 if (needs.getTextureTarget() == Key::TEXTURE_EXT) {
542 fs << "uniform samplerExternalOES sampler;"
543 << "varying vec2 outTexCoords;";
544 } else if (needs.getTextureTarget() == Key::TEXTURE_2D) {
545 fs << "uniform sampler2D sampler;"
546 << "varying vec2 outTexCoords;";
chaviw13fdc492017-06-27 12:40:18 -0700547 }
548
549 if (needs.getTextureTarget() == Key::TEXTURE_OFF || needs.hasAlpha()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700550 fs << "uniform vec4 color;";
551 }
chaviw13fdc492017-06-27 12:40:18 -0700552
Chia-I Wu131d3762018-01-11 14:35:27 -0800553 if (needs.isY410BT2020()) {
554 fs << R"__SHADER__(
555 vec3 convertY410BT2020(const vec3 color) {
556 const vec3 offset = vec3(0.0625, 0.5, 0.5);
557 const mat3 transform = mat3(
558 vec3(1.1678, 1.1678, 1.1678),
559 vec3( 0.0, -0.1878, 2.1481),
560 vec3(1.6836, -0.6523, 0.0));
561 // Y is in G, U is in R, and V is in B
562 return clamp(transform * (color.grb - offset), 0.0, 1.0);
563 }
564 )__SHADER__";
565 }
566
Peiyong Lina296b0c2018-04-30 16:55:29 -0700567 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Peiyong Lin53f320e2018-04-23 17:31:06 -0700568 // Currently, display maximum luminance is needed when doing tone mapping.
569 if (needs.needsToneMapping()) {
Chia-I Wu8bbacdf2018-05-04 15:19:37 -0700570 fs << "uniform float displayMaxLuminance;";
Peiyong Linfb069302018-04-25 14:34:31 -0700571 }
Romain Guy88d37dd2017-05-26 17:57:05 -0700572
Peiyong Lina296b0c2018-04-30 16:55:29 -0700573 if (needs.hasInputTransformMatrix()) {
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700574 fs << "uniform mat4 inputTransformMatrix;";
Peiyong Lina296b0c2018-04-30 16:55:29 -0700575 fs << R"__SHADER__(
576 highp vec3 InputTransform(const highp vec3 color) {
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700577 return vec3(inputTransformMatrix * vec4(color, 1.0));
Peiyong Lina296b0c2018-04-30 16:55:29 -0700578 }
579 )__SHADER__";
580 } else {
581 fs << R"__SHADER__(
582 highp vec3 InputTransform(const highp vec3 color) {
583 return color;
584 }
585 )__SHADER__";
586 }
587
588 // the transformation from a wider colorspace to a narrower one can
589 // result in >1.0 or <0.0 pixel values
590 if (needs.hasOutputTransformMatrix()) {
591 fs << "uniform mat4 outputTransformMatrix;";
592 fs << R"__SHADER__(
593 highp vec3 OutputTransform(const highp vec3 color) {
594 return clamp(vec3(outputTransformMatrix * vec4(color, 1.0)), 0.0, 1.0);
595 }
596 )__SHADER__";
597 } else {
598 fs << R"__SHADER__(
599 highp vec3 OutputTransform(const highp vec3 color) {
600 return clamp(color, 0.0, 1.0);
601 }
602 )__SHADER__";
603 }
604
Peiyong Lin2542cb02018-04-30 17:29:53 -0700605 generateEOTF(fs, needs);
606 generateOOTF(fs, needs);
607 generateOETF(fs, needs);
Romain Guy88d37dd2017-05-26 17:57:05 -0700608 }
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800609
Mathias Agopian3f844832013-08-07 21:24:32 -0700610 fs << "void main(void) {" << indent;
611 if (needs.isTexturing()) {
612 fs << "gl_FragColor = texture2D(sampler, outTexCoords);";
Chia-I Wu131d3762018-01-11 14:35:27 -0800613 if (needs.isY410BT2020()) {
614 fs << "gl_FragColor.rgb = convertY410BT2020(gl_FragColor.rgb);";
615 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700616 } else {
chaviw13fdc492017-06-27 12:40:18 -0700617 fs << "gl_FragColor.rgb = color.rgb;";
618 fs << "gl_FragColor.a = 1.0;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700619 }
Mathias Agopian2eaefe12013-08-14 16:33:27 -0700620 if (needs.isOpaque()) {
621 fs << "gl_FragColor.a = 1.0;";
622 }
chaviw13fdc492017-06-27 12:40:18 -0700623 if (needs.hasAlpha()) {
624 // modulate the current alpha value with alpha set
Mathias Agopian3f844832013-08-07 21:24:32 -0700625 if (needs.isPremultiplied()) {
626 // ... and the color too if we're premultiplied
chaviw13fdc492017-06-27 12:40:18 -0700627 fs << "gl_FragColor *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700628 } else {
chaviw13fdc492017-06-27 12:40:18 -0700629 fs << "gl_FragColor.a *= color.a;";
Mathias Agopian3f844832013-08-07 21:24:32 -0700630 }
631 }
Mathias Agopianff2ed702013-09-01 21:36:12 -0700632
Peiyong Lina296b0c2018-04-30 16:55:29 -0700633 if (needs.hasTransformMatrix() || (needs.getInputTF() != needs.getOutputTF())) {
Mathias Agopianff2ed702013-09-01 21:36:12 -0700634 if (!needs.isOpaque() && needs.isPremultiplied()) {
635 // un-premultiply if needed before linearization
Romain Guy88d37dd2017-05-26 17:57:05 -0700636 // avoid divide by 0 by adding 0.5/256 to the alpha channel
637 fs << "gl_FragColor.rgb = gl_FragColor.rgb / (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700638 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700639 fs << "gl_FragColor.rgb = OETF(OutputTransform(OOTF(InputTransform(EOTF(gl_FragColor.rgb)))));";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700640 if (!needs.isOpaque() && needs.isPremultiplied()) {
641 // and re-premultiply if needed after gamma correction
Romain Guy88d37dd2017-05-26 17:57:05 -0700642 fs << "gl_FragColor.rgb = gl_FragColor.rgb * (gl_FragColor.a + 0.0019);";
Mathias Agopianff2ed702013-09-01 21:36:12 -0700643 }
644 }
645
Mathias Agopian3f844832013-08-07 21:24:32 -0700646 fs << dedent << "}";
647 return fs.getString();
648}
649
650Program* ProgramCache::generateProgram(const Key& needs) {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700651 ATRACE_CALL();
652
Mathias Agopian3f844832013-08-07 21:24:32 -0700653 // vertex shader
654 String8 vs = generateVertexShader(needs);
655
656 // fragment shader
657 String8 fs = generateFragmentShader(needs);
658
659 Program* program = new Program(needs, vs.string(), fs.string());
660 return program;
661}
662
663void ProgramCache::useProgram(const Description& description) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700664 // generate the key for the shader based on the description
665 Key needs(computeKey(description));
666
Chia-I Wub027f802017-11-29 14:00:52 -0800667 // look-up the program in the cache
Mathias Agopian3f844832013-08-07 21:24:32 -0700668 Program* program = mCache.valueFor(needs);
Peiyong Lin566a3b42018-01-09 18:22:43 -0800669 if (program == nullptr) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700670 // we didn't find our program, so generate one...
671 nsecs_t time = -systemTime();
672 program = generateProgram(needs);
673 mCache.add(needs, program);
674 time += systemTime();
675
Chia-I Wu93e14df2018-06-04 10:10:17 -0700676 ALOGV(">>> generated new program: needs=%08X, time=%u ms (%zu programs)", needs.mKey,
677 uint32_t(ns2ms(time)), mCache.size());
Mathias Agopian3f844832013-08-07 21:24:32 -0700678 }
679
680 // here we have a suitable program for this description
681 if (program->isValid()) {
682 program->use();
683 program->setUniforms(description);
684 }
685}
686
Peiyong Lin833074a2018-08-28 11:53:54 -0700687} // namespace gl
688} // namespace renderengine
689} // namespace android