<commit_before>#version 300 es
// Copyright 2022 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// ES 3 fragment shader that:
// 1. samples HLG BT.2020 YUV from an external texture with uTexSampler, where
//    the sampler uses the EXT_YUV_target extension specified at
//    https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_YUV_target.txt,
// 2. Applies a YUV to RGB conversion using the specified color transform
//    uYuvToRgbColorTransform, yielding HLG BT.2020 RGB,
// 3. If uApplyHlgOetf is 1, outputs HLG BT.2020 RGB. If 0, outputs
//    linear BT.2020 RGB for intermediate shaders by applying the HLG OETF.
// 4. Copies this converted texture color to the current output.

#extension GL_OES_EGL_image_external : require
#extension GL_EXT_YUV_target : require
precision mediump float;
uniform __samplerExternal2DY2YEXT uTexSampler;
uniform mat3 uYuvToRgbColorTransform;
uniform float uApplyHlgOetf;
in vec2 vTexSamplingCoord;
out vec4 outColor;

// TODO(b/227624622): Consider using mediump to save precision, if it won't lead
//  to noticeable quantization errors.

// HLG OETF for one channel.
highp float hlgOetfSingleChannel(highp float hlgChannel) {
 // Specification:
 // https://www.khronos.org/registry/DataFormat/specs/1.3/dataformat.1.3.inline.html#TRANSFER_HLG
 // Reference implementation:
 // https://cs.android.com/android/platform/superproject/+/master:frameworks/native/libs/renderengine/gl/ProgramCache.cpp;l=529-543;drc=de09f10aa504fd8066370591a00c9ff1cafbb7fa
  const highp float a = 0.17883277;
  const highp float b = 0.28466892;
  const highp float c = 0.55991073;

  return hlgChannel <= 1.0 / 12.0 ? sqrt(3.0 * hlgChannel) :
      a * log(12.0 * hlgChannel - b) + c;
}

 // BT.2100-0 HLG OETF. Converts nonlinear relative display light to linear
 // signal values, both normalized to [0, 1].
highp vec4 hlgOetf(highp vec4 hlgColor) {
  return vec4(
    hlgOetfSingleChannel(hlgColor.r),
    hlgOetfSingleChannel(hlgColor.g),
    hlgOetfSingleChannel(hlgColor.b),
    hlgColor.a
  );
}

/** Convert YUV to RGBA. */
vec4 yuvToRgba(vec3 yuv) {
  const vec3 yuvOffset = vec3(0.0625, 0.5, 0.5);
  vec3 rgb = uYuvToRgbColorTransform * (yuv - yuvOffset);<commit_msg>HDR: Clamp YUV to RGB conversion.<commit_after>#version 300 es
// Copyright 2022 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// ES 3 fragment shader that:
// 1. samples HLG BT.2020 YUV from an external texture with uTexSampler, where
//    the sampler uses the EXT_YUV_target extension specified at
//    https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_YUV_target.txt,
// 2. Applies a YUV to RGB conversion using the specified color transform
//    uYuvToRgbColorTransform, yielding HLG BT.2020 RGB,
// 3. If uApplyHlgOetf is 1, outputs HLG BT.2020 RGB. If 0, outputs
//    linear BT.2020 RGB for intermediate shaders by applying the HLG OETF.
// 4. Copies this converted texture color to the current output.

#extension GL_OES_EGL_image_external : require
#extension GL_EXT_YUV_target : require
precision mediump float;
uniform __samplerExternal2DY2YEXT uTexSampler;
uniform mat3 uYuvToRgbColorTransform;
uniform float uApplyHlgOetf;
in vec2 vTexSamplingCoord;
out vec4 outColor;

// TODO(b/227624622): Consider using mediump to save precision, if it won't lead
//  to noticeable quantization errors.

// HLG OETF for one channel.
highp float hlgOetfSingleChannel(highp float hlgChannel) {
 // Specification:
 // https://www.khronos.org/registry/DataFormat/specs/1.3/dataformat.1.3.inline.html#TRANSFER_HLG
 // Reference implementation:
 // https://cs.android.com/android/platform/superproject/+/master:frameworks/native/libs/renderengine/gl/ProgramCache.cpp;l=529-543;drc=de09f10aa504fd8066370591a00c9ff1cafbb7fa
  const highp float a = 0.17883277;
  const highp float b = 0.28466892;
  const highp float c = 0.55991073;

  return hlgChannel <= 1.0 / 12.0 ? sqrt(3.0 * hlgChannel) :
      a * log(12.0 * hlgChannel - b) + c;
}

 // BT.2100-0 HLG OETF. Converts nonlinear relative display light to linear
 // signal values, both normalized to [0, 1].
highp vec4 hlgOetf(highp vec4 hlgColor) {
  return vec4(
    hlgOetfSingleChannel(hlgColor.r),
    hlgOetfSingleChannel(hlgColor.g),
    hlgOetfSingleChannel(hlgColor.b),
    hlgColor.a
  );
}

/** Convert YUV to RGBA. */
vec4 yuvToRgba(vec3 yuv) {
  const vec3 yuvOffset = vec3(0.0625, 0.5, 0.5);
  vec3 rgb = clamp(uYuvToRgbColorTransform * (yuv - yuvOffset), 0.0, 1.0);
{'fraction_non_alphanumeric': 0.09621993127147767, 'fraction_numerical': 0.0517272562850425, 'mean_word_length': 3.8296943231441047, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6822692', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1819, 'n_words': 586}