OLD | NEW |
1 // Copyright 2011 The Chromium Authors. All rights reserved. | 1 // Copyright 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/output/shader.h" | 5 #include "cc/output/shader.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
(...skipping 2002 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2013 if (mask_for_background()) | 2013 if (mask_for_background()) |
2014 locations->original_backdrop = original_backdrop_location(); | 2014 locations->original_backdrop = original_backdrop_location(); |
2015 } | 2015 } |
2016 | 2016 |
2017 FragmentShaderYUVVideo::FragmentShaderYUVVideo() | 2017 FragmentShaderYUVVideo::FragmentShaderYUVVideo() |
2018 : y_texture_location_(-1), | 2018 : y_texture_location_(-1), |
2019 u_texture_location_(-1), | 2019 u_texture_location_(-1), |
2020 v_texture_location_(-1), | 2020 v_texture_location_(-1), |
2021 uv_texture_location_(-1), | 2021 uv_texture_location_(-1), |
2022 a_texture_location_(-1), | 2022 a_texture_location_(-1), |
| 2023 lut_texture_location_(-1), |
2023 alpha_location_(-1), | 2024 alpha_location_(-1), |
2024 yuv_matrix_location_(-1), | 2025 yuv_matrix_location_(-1), |
2025 yuv_adj_location_(-1), | 2026 yuv_adj_location_(-1), |
2026 ya_clamp_rect_location_(-1), | 2027 ya_clamp_rect_location_(-1), |
2027 uv_clamp_rect_location_(-1) {} | 2028 uv_clamp_rect_location_(-1), |
| 2029 resource_multiplier_location_(-1), |
| 2030 resource_offset_location_(-1) {} |
2028 | 2031 |
2029 void FragmentShaderYUVVideo::SetFeatures(bool use_alpha_texture, | 2032 void FragmentShaderYUVVideo::SetFeatures(bool use_alpha_texture, |
2030 bool use_nv12) { | 2033 bool use_nv12, |
| 2034 bool use_color_lut) { |
2031 use_alpha_texture_ = use_alpha_texture; | 2035 use_alpha_texture_ = use_alpha_texture; |
2032 use_nv12_ = use_nv12; | 2036 use_nv12_ = use_nv12; |
| 2037 use_color_lut_ = use_color_lut; |
2033 } | 2038 } |
2034 | 2039 |
2035 void FragmentShaderYUVVideo::Init(GLES2Interface* context, | 2040 void FragmentShaderYUVVideo::Init(GLES2Interface* context, |
2036 unsigned program, | 2041 unsigned program, |
2037 int* base_uniform_index) { | 2042 int* base_uniform_index) { |
2038 static const char* uniforms[] = { | 2043 static const char* uniforms[] = { |
2039 "y_texture", "u_texture", "v_texture", "uv_texture", "a_texture", | 2044 "y_texture", |
2040 "alpha", "yuv_matrix", "yuv_adj", "ya_clamp_rect", "uv_clamp_rect"}; | 2045 "u_texture", |
| 2046 "v_texture", |
| 2047 "uv_texture", |
| 2048 "a_texture", |
| 2049 "lut_texture", |
| 2050 "resource_multiplier", |
| 2051 "resource_offset", |
| 2052 "yuv_matrix", |
| 2053 "yuv_adj", |
| 2054 "alpha", |
| 2055 "ya_clamp_rect", |
| 2056 "uv_clamp_rect", |
| 2057 }; |
2041 int locations[arraysize(uniforms)]; | 2058 int locations[arraysize(uniforms)]; |
2042 | 2059 |
2043 GetProgramUniformLocations(context, | 2060 GetProgramUniformLocations(context, |
2044 program, | 2061 program, |
2045 arraysize(uniforms), | 2062 arraysize(uniforms), |
2046 uniforms, | 2063 uniforms, |
2047 locations, | 2064 locations, |
2048 base_uniform_index); | 2065 base_uniform_index); |
2049 y_texture_location_ = locations[0]; | 2066 y_texture_location_ = locations[0]; |
2050 u_texture_location_ = locations[1]; | 2067 if (!use_nv12_) { |
2051 v_texture_location_ = locations[2]; | 2068 u_texture_location_ = locations[1]; |
2052 uv_texture_location_ = locations[3]; | 2069 v_texture_location_ = locations[2]; |
2053 a_texture_location_ = locations[4]; | 2070 } else { |
2054 alpha_location_ = locations[5]; | 2071 uv_texture_location_ = locations[3]; |
2055 yuv_matrix_location_ = locations[6]; | 2072 } |
2056 yuv_adj_location_ = locations[7]; | 2073 if (use_alpha_texture_) { |
2057 ya_clamp_rect_location_ = locations[8]; | 2074 a_texture_location_ = locations[4]; |
2058 uv_clamp_rect_location_ = locations[9]; | 2075 } |
| 2076 if (use_color_lut_) { |
| 2077 lut_texture_location_ = locations[5]; |
| 2078 resource_multiplier_location_ = locations[6]; |
| 2079 resource_offset_location_ = locations[7]; |
| 2080 } else { |
| 2081 yuv_matrix_location_ = locations[8]; |
| 2082 yuv_adj_location_ = locations[9]; |
| 2083 } |
| 2084 alpha_location_ = locations[10]; |
| 2085 ya_clamp_rect_location_ = locations[11]; |
| 2086 uv_clamp_rect_location_ = locations[12]; |
2059 } | 2087 } |
2060 | 2088 |
2061 std::string FragmentShaderYUVVideo::GetShaderString(TexCoordPrecision precision, | 2089 std::string FragmentShaderYUVVideo::GetShaderString(TexCoordPrecision precision, |
2062 SamplerType sampler) const { | 2090 SamplerType sampler) const { |
2063 std::string head = SHADER0([]() { | 2091 std::string head = SHADER0([]() { |
2064 precision mediump float; | 2092 precision mediump float; |
2065 precision mediump int; | 2093 precision mediump int; |
2066 varying TexCoordPrecision vec2 v_yaTexCoord; | 2094 varying TexCoordPrecision vec2 v_yaTexCoord; |
2067 varying TexCoordPrecision vec2 v_uvTexCoord; | 2095 varying TexCoordPrecision vec2 v_uvTexCoord; |
2068 uniform SamplerType y_texture; | 2096 uniform SamplerType y_texture; |
2069 uniform float alpha; | 2097 uniform float alpha; |
2070 uniform vec3 yuv_adj; | |
2071 uniform mat3 yuv_matrix; | |
2072 uniform vec4 ya_clamp_rect; | 2098 uniform vec4 ya_clamp_rect; |
2073 uniform vec4 uv_clamp_rect; | 2099 uniform vec4 uv_clamp_rect; |
2074 }); | 2100 }); |
| 2101 |
| 2102 std::string functions = ""; |
2075 if (use_nv12_) { | 2103 if (use_nv12_) { |
2076 head += " uniform SamplerType uv_texture;\n"; | 2104 head += " uniform SamplerType uv_texture;\n"; |
2077 } else { | 2105 functions += SHADER0([]() { |
2078 head += " uniform SamplerType u_texture;\n"; | |
2079 head += " uniform SamplerType v_texture;\n"; | |
2080 } | |
2081 if (use_alpha_texture_) { | |
2082 head += " uniform SamplerType a_texture;\n"; | |
2083 } | |
2084 | |
2085 std::string main = SHADER0([]() { | |
2086 void main() { | |
2087 vec2 ya_clamped = | |
2088 max(ya_clamp_rect.xy, min(ya_clamp_rect.zw, v_yaTexCoord)); | |
2089 float y_raw = TextureLookup(y_texture, ya_clamped).x; | |
2090 vec2 uv_clamped = | |
2091 max(uv_clamp_rect.xy, min(uv_clamp_rect.zw, v_uvTexCoord)); | |
2092 vec2 uv_unsigned = GetUV(uv_clamped); | |
2093 vec3 yuv = vec3(y_raw, uv_unsigned) + yuv_adj; | |
2094 vec3 rgb = yuv_matrix * yuv; | |
2095 gl_FragColor = vec4(rgb, 1.0) * GetAlpha(ya_clamped); | |
2096 } | |
2097 }); | |
2098 | |
2099 std::string get_uv; | |
2100 if (use_nv12_) { | |
2101 get_uv = SHADER0([]() { | |
2102 vec2 GetUV(vec2 uv_clamped) { | 2106 vec2 GetUV(vec2 uv_clamped) { |
2103 return TextureLookup(uv_texture, uv_clamped).xy; | 2107 return TextureLookup(uv_texture, uv_clamped).xy; |
2104 } | 2108 } |
2105 }); | 2109 }); |
2106 } else { | 2110 } else { |
2107 get_uv = SHADER0([]() { | 2111 head += " uniform SamplerType u_texture;\n"; |
| 2112 head += " uniform SamplerType v_texture;\n"; |
| 2113 functions += SHADER0([]() { |
2108 vec2 GetUV(vec2 uv_clamped) { | 2114 vec2 GetUV(vec2 uv_clamped) { |
2109 return vec2(TextureLookup(u_texture, uv_clamped).x, | 2115 return vec2(TextureLookup(u_texture, uv_clamped).x, |
2110 TextureLookup(v_texture, uv_clamped).x); | 2116 TextureLookup(v_texture, uv_clamped).x); |
2111 } | 2117 } |
2112 }); | 2118 }); |
2113 } | 2119 } |
2114 | 2120 |
2115 std::string get_alpha; | |
2116 if (use_alpha_texture_) { | 2121 if (use_alpha_texture_) { |
2117 get_alpha = SHADER0([]() { | 2122 head += " uniform SamplerType a_texture;\n"; |
| 2123 functions += SHADER0([]() { |
2118 float GetAlpha(vec2 ya_clamped) { | 2124 float GetAlpha(vec2 ya_clamped) { |
2119 return alpha * TextureLookup(a_texture, ya_clamped).x; | 2125 return alpha * TextureLookup(a_texture, ya_clamped).x; |
2120 } | 2126 } |
2121 }); | 2127 }); |
2122 } else { | 2128 } else { |
2123 get_alpha = SHADER0([]() { | 2129 functions += SHADER0([]() { |
2124 float GetAlpha(vec2 ya_clamped) { return alpha; } | 2130 float GetAlpha(vec2 ya_clamped) { return alpha; } |
2125 }); | 2131 }); |
2126 } | 2132 } |
2127 | 2133 |
2128 return FRAGMENT_SHADER(head, get_uv + get_alpha + main); | 2134 if (use_color_lut_) { |
| 2135 head += " uniform sampler2D lut_texture;\n"; |
| 2136 head += " uniform float resource_multiplier;\n"; |
| 2137 head += " uniform float resource_offset;\n"; |
| 2138 functions += SHADER0([]() { |
| 2139 vec4 LUT(sampler2D sampler, vec3 pos, float size) { |
| 2140 pos *= size - 1.0; |
| 2141 // Select layer |
| 2142 float layer = min(floor(pos.x), size - 2.0); |
| 2143 // Compress the yz coordinates so they stay within |
| 2144 // [0.5 .. 31.5] / 32 (assuming a LUT size of 32^3) |
| 2145 pos.yz = (pos.yz + vec2(0.5)) / size; |
| 2146 pos.z = (pos.z + layer) / size; |
| 2147 return mix(texture2D(sampler, pos.yz), |
| 2148 texture2D(sampler, pos.yz + vec2(0, 1.0 / size)), |
| 2149 pos.x - layer); |
| 2150 } |
| 2151 |
| 2152 vec3 yuv2rgb(vec3 yuv) { |
| 2153 yuv = yuv * resource_multiplier - vec3(resource_offset); |
| 2154 return LUT(lut_texture, yuv, 32.0).xyz; |
| 2155 } |
| 2156 }); |
| 2157 } else { |
| 2158 head += " uniform mat3 yuv_matrix;\n"; |
| 2159 head += " uniform vec3 yuv_adj;\n"; |
| 2160 functions += SHADER0([]() { |
| 2161 vec3 yuv2rgb(vec3 yuv) { return yuv_matrix * (yuv + yuv_adj); } |
| 2162 }); |
| 2163 } |
| 2164 |
| 2165 functions += SHADER0([]() { |
| 2166 void main() { |
| 2167 vec2 ya_clamped = |
| 2168 max(ya_clamp_rect.xy, min(ya_clamp_rect.zw, v_yaTexCoord)); |
| 2169 float y_raw = TextureLookup(y_texture, ya_clamped).x; |
| 2170 vec2 uv_clamped = |
| 2171 max(uv_clamp_rect.xy, min(uv_clamp_rect.zw, v_uvTexCoord)); |
| 2172 vec3 yuv = vec3(y_raw, GetUV(uv_clamped)); |
| 2173 gl_FragColor = vec4(yuv2rgb(yuv), 1.0) * GetAlpha(ya_clamped); |
| 2174 } |
| 2175 }); |
| 2176 |
| 2177 return FRAGMENT_SHADER(head, functions); |
2129 } | 2178 } |
2130 | 2179 |
2131 FragmentShaderColor::FragmentShaderColor() : color_location_(-1) { | 2180 FragmentShaderColor::FragmentShaderColor() : color_location_(-1) { |
2132 } | 2181 } |
2133 | 2182 |
2134 void FragmentShaderColor::Init(GLES2Interface* context, | 2183 void FragmentShaderColor::Init(GLES2Interface* context, |
2135 unsigned program, | 2184 unsigned program, |
2136 int* base_uniform_index) { | 2185 int* base_uniform_index) { |
2137 static const char* uniforms[] = { | 2186 static const char* uniforms[] = { |
2138 "color", | 2187 "color", |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2204 void main() { | 2253 void main() { |
2205 vec4 d4 = min(edge_dist[0], edge_dist[1]); | 2254 vec4 d4 = min(edge_dist[0], edge_dist[1]); |
2206 vec2 d2 = min(d4.xz, d4.yw); | 2255 vec2 d2 = min(d4.xz, d4.yw); |
2207 float aa = clamp(gl_FragCoord.w * min(d2.x, d2.y), 0.0, 1.0); | 2256 float aa = clamp(gl_FragCoord.w * min(d2.x, d2.y), 0.0, 1.0); |
2208 gl_FragColor = color * aa; | 2257 gl_FragColor = color * aa; |
2209 } | 2258 } |
2210 }); | 2259 }); |
2211 } | 2260 } |
2212 | 2261 |
2213 } // namespace cc | 2262 } // namespace cc |
OLD | NEW |