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 2005 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2016 FragmentShaderYUVVideo::FragmentShaderYUVVideo() | 2016 FragmentShaderYUVVideo::FragmentShaderYUVVideo() |
2017 : y_texture_location_(-1), | 2017 : y_texture_location_(-1), |
2018 u_texture_location_(-1), | 2018 u_texture_location_(-1), |
2019 v_texture_location_(-1), | 2019 v_texture_location_(-1), |
2020 uv_texture_location_(-1), | 2020 uv_texture_location_(-1), |
2021 a_texture_location_(-1), | 2021 a_texture_location_(-1), |
2022 lut_texture_location_(-1), | 2022 lut_texture_location_(-1), |
2023 alpha_location_(-1), | 2023 alpha_location_(-1), |
2024 yuv_matrix_location_(-1), | 2024 yuv_matrix_location_(-1), |
2025 yuv_adj_location_(-1), | 2025 yuv_adj_location_(-1), |
2026 inverse_max_input_value_location_(-1), | |
2027 ya_size_location_(-1), | |
2028 uv_size_location_(-1), | |
2026 ya_clamp_rect_location_(-1), | 2029 ya_clamp_rect_location_(-1), |
2027 uv_clamp_rect_location_(-1), | 2030 uv_clamp_rect_location_(-1), |
2028 resource_multiplier_location_(-1), | 2031 resource_multiplier_location_(-1), |
2029 resource_offset_location_(-1) {} | 2032 resource_offset_location_(-1) {} |
2030 | 2033 |
2031 void FragmentShaderYUVVideo::SetFeatures(bool use_alpha_texture, | 2034 void FragmentShaderYUVVideo::SetFeatures(bool use_alpha_texture, |
2032 bool use_nv12, | 2035 bool use_nv12, |
2033 bool use_color_lut) { | 2036 bool use_color_lut, |
2037 HighbitTexture highbit_texture) { | |
2034 use_alpha_texture_ = use_alpha_texture; | 2038 use_alpha_texture_ = use_alpha_texture; |
2035 use_nv12_ = use_nv12; | 2039 use_nv12_ = use_nv12; |
2036 use_color_lut_ = use_color_lut; | 2040 use_color_lut_ = use_color_lut; |
2041 highbit_texture_ = highbit_texture; | |
2037 } | 2042 } |
2038 | 2043 |
2039 void FragmentShaderYUVVideo::Init(GLES2Interface* context, | 2044 void FragmentShaderYUVVideo::Init(GLES2Interface* context, |
2040 unsigned program, | 2045 unsigned program, |
2041 int* base_uniform_index) { | 2046 int* base_uniform_index) { |
2042 static const char* uniforms[] = { | 2047 static const char* uniforms[] = { |
2043 "y_texture", | 2048 "y_texture", |
2044 "u_texture", | 2049 "u_texture", |
2045 "v_texture", | 2050 "v_texture", |
2046 "uv_texture", | 2051 "uv_texture", |
2047 "a_texture", | 2052 "a_texture", |
2048 "lut_texture", | 2053 "lut_texture", |
2054 "yuv_matrix", | |
2055 "yuv_adj", | |
2049 "resource_multiplier", | 2056 "resource_multiplier", |
2050 "resource_offset", | 2057 "resource_offset", |
2051 "yuv_matrix", | 2058 "inverse_max_input_value", |
2052 "yuv_adj", | 2059 "ya_size", |
2060 "uv_size", | |
2053 "alpha", | 2061 "alpha", |
2054 "ya_clamp_rect", | 2062 "ya_clamp_rect", |
2055 "uv_clamp_rect", | 2063 "uv_clamp_rect", |
2056 }; | 2064 }; |
2057 int locations[arraysize(uniforms)]; | 2065 int locations[arraysize(uniforms)]; |
2058 | 2066 |
2059 GetProgramUniformLocations(context, | 2067 GetProgramUniformLocations(context, |
2060 program, | 2068 program, |
2061 arraysize(uniforms), | 2069 arraysize(uniforms), |
2062 uniforms, | 2070 uniforms, |
2063 locations, | 2071 locations, |
2064 base_uniform_index); | 2072 base_uniform_index); |
2065 y_texture_location_ = locations[0]; | 2073 y_texture_location_ = locations[0]; |
2066 if (!use_nv12_) { | 2074 if (!use_nv12_) { |
2067 u_texture_location_ = locations[1]; | 2075 u_texture_location_ = locations[1]; |
2068 v_texture_location_ = locations[2]; | 2076 v_texture_location_ = locations[2]; |
2069 } else { | 2077 } else { |
2070 uv_texture_location_ = locations[3]; | 2078 uv_texture_location_ = locations[3]; |
2071 } | 2079 } |
2072 if (use_alpha_texture_) { | 2080 if (use_alpha_texture_) { |
2073 a_texture_location_ = locations[4]; | 2081 a_texture_location_ = locations[4]; |
2074 } | 2082 } |
2075 if (use_color_lut_) { | 2083 if (use_color_lut_) { |
2076 lut_texture_location_ = locations[5]; | 2084 lut_texture_location_ = locations[5]; |
2077 resource_multiplier_location_ = locations[6]; | |
2078 resource_offset_location_ = locations[7]; | |
2079 } else { | 2085 } else { |
2080 yuv_matrix_location_ = locations[8]; | 2086 yuv_matrix_location_ = locations[6]; |
2081 yuv_adj_location_ = locations[9]; | 2087 yuv_adj_location_ = locations[7]; |
2082 } | 2088 } |
2083 alpha_location_ = locations[10]; | 2089 if (highbit_texture_ == HIGHBIT_LUMINANCE_F16) { |
2084 ya_clamp_rect_location_ = locations[11]; | 2090 resource_multiplier_location_ = locations[8]; |
2085 uv_clamp_rect_location_ = locations[12]; | 2091 resource_offset_location_ = locations[9]; |
2092 } else if (highbit_texture_ == HIGHBIT_RG88) { | |
2093 inverse_max_input_value_location_ = locations[10]; | |
2094 ya_size_location_ = locations[11]; | |
2095 uv_size_location_ = locations[12]; | |
2096 } | |
2097 alpha_location_ = locations[13]; | |
2098 ya_clamp_rect_location_ = locations[14]; | |
2099 uv_clamp_rect_location_ = locations[15]; | |
2086 } | 2100 } |
2087 | 2101 |
2088 std::string FragmentShaderYUVVideo::GetShaderString(TexCoordPrecision precision, | 2102 std::string FragmentShaderYUVVideo::GetShaderString(TexCoordPrecision precision, |
2089 SamplerType sampler) const { | 2103 SamplerType sampler) const { |
2090 std::string head = SHADER0([]() { | 2104 std::string head = SHADER0([]() { |
2091 precision mediump float; | 2105 precision mediump float; |
2092 precision mediump int; | 2106 precision mediump int; |
2093 varying TexCoordPrecision vec2 v_yaTexCoord; | 2107 varying TexCoordPrecision vec2 v_yaTexCoord; |
2094 varying TexCoordPrecision vec2 v_uvTexCoord; | 2108 varying TexCoordPrecision vec2 v_uvTexCoord; |
2095 uniform SamplerType y_texture; | 2109 uniform SamplerType y_texture; |
2096 uniform float alpha; | 2110 uniform float alpha; |
2097 uniform vec4 ya_clamp_rect; | 2111 uniform vec4 ya_clamp_rect; |
2098 uniform vec4 uv_clamp_rect; | 2112 uniform vec4 uv_clamp_rect; |
2099 }); | 2113 }); |
2100 | 2114 |
2101 std::string functions = ""; | 2115 std::string functions = ""; |
2116 if (highbit_texture_ == HIGHBIT_RG88) { | |
2117 head += " uniform float inverse_max_input_value;\n"; | |
2118 head += " uniform vec2 ya_size;\n"; | |
2119 head += " uniform vec2 uv_size;\n"; | |
2120 functions += SHADER0([]() { | |
2121 float GetFloat(vec2 rg) { | |
2122 return ((rg.g * 65280.0) + (rg.r * 255.0)) * inverse_max_input_value; | |
hubbe
2016/09/29 23:36:51
We can save an instruction here by dividing invers
dshwang
2016/10/05 15:13:40
Done. now uniform name is resource_multiplier as y
| |
2123 } | |
2124 | |
2125 float RGTextureLookupBilinear(SamplerType sampler, vec2 tex_coord, | |
2126 vec2 tex_size) { | |
2127 vec2 snap_tex_coord = | |
2128 (floor(tex_coord * tex_size) + vec2(0.5)) / tex_size; | |
2129 vec2 unit_texel = 1.0 / tex_size; | |
2130 float s1 = GetFloat(TextureLookup(sampler, snap_tex_coord).xy); | |
hubbe
2016/09/29 23:36:51
I would probably just include the TextureLookup()
dshwang
2016/10/05 15:13:41
Done.
| |
2131 float s2 = GetFloat( | |
2132 TextureLookup(sampler, snap_tex_coord + vec2(unit_texel.x, 0.)).xy); | |
2133 float s3 = GetFloat( | |
2134 TextureLookup(sampler, snap_tex_coord + vec2(0., unit_texel.y)).xy); | |
2135 float s4 = | |
2136 GetFloat(TextureLookup(sampler, snap_tex_coord + unit_texel).xy); | |
2137 vec2 f = fract(tex_coord * tex_size); | |
2138 return mix(mix(s1, s2, f.x), mix(s3, s4, f.x), f.y); | |
2139 } | |
2140 | |
2141 float GetY(vec2 ya_clamped) { | |
2142 return RGTextureLookupBilinear(y_texture, ya_clamped, ya_size); | |
2143 } | |
2144 }); | |
2145 } else { | |
2146 functions += SHADER0([]() { | |
2147 float GetY(vec2 ya_clamped) { | |
2148 return TextureLookup(y_texture, ya_clamped).x; | |
2149 } | |
2150 }); | |
2151 } | |
2152 | |
2102 if (use_nv12_) { | 2153 if (use_nv12_) { |
2103 head += " uniform SamplerType uv_texture;\n"; | 2154 head += " uniform SamplerType uv_texture;\n"; |
2104 functions += SHADER0([]() { | 2155 functions += SHADER0([]() { |
2105 vec2 GetUV(vec2 uv_clamped) { | 2156 vec2 GetUV(vec2 uv_tex_coord) { |
2157 vec2 uv_clamped = | |
hubbe
2016/09/29 23:36:51
I think you forgot to undo this, GetUV() gets uv_c
dshwang
2016/10/05 15:13:41
oops, thx!
| |
2158 max(uv_clamp_rect.xy, min(uv_clamp_rect.zw, uv_tex_coord)); | |
2106 return TextureLookup(uv_texture, uv_clamped).xy; | 2159 return TextureLookup(uv_texture, uv_clamped).xy; |
2107 } | 2160 } |
2108 }); | 2161 }); |
2109 } else { | 2162 } else { |
2110 head += " uniform SamplerType u_texture;\n"; | 2163 head += " uniform SamplerType u_texture;\n"; |
2111 head += " uniform SamplerType v_texture;\n"; | 2164 head += " uniform SamplerType v_texture;\n"; |
2112 functions += SHADER0([]() { | 2165 if (highbit_texture_ == HIGHBIT_RG88) { |
2113 vec2 GetUV(vec2 uv_clamped) { | 2166 functions += SHADER0([]() { |
2114 return vec2(TextureLookup(u_texture, uv_clamped).x, | 2167 vec2 GetUV(vec2 uv_clamped) { |
2115 TextureLookup(v_texture, uv_clamped).x); | 2168 vec2 uv = |
2116 } | 2169 vec2(RGTextureLookupBilinear(u_texture, uv_clamped, uv_size), |
2117 }); | 2170 RGTextureLookupBilinear(v_texture, uv_clamped, uv_size)); |
2171 return uv; | |
2172 } | |
2173 }); | |
2174 } else { | |
2175 functions += SHADER0([]() { | |
2176 vec2 GetUV(vec2 uv_clamped) { | |
2177 vec2 uv = vec2(TextureLookup(u_texture, uv_clamped).x, | |
2178 TextureLookup(v_texture, uv_clamped).x); | |
2179 return uv; | |
2180 } | |
2181 }); | |
2182 } | |
2118 } | 2183 } |
2119 | 2184 |
2120 if (use_alpha_texture_) { | 2185 if (use_alpha_texture_) { |
2121 head += " uniform SamplerType a_texture;\n"; | 2186 head += " uniform SamplerType a_texture;\n"; |
2122 functions += SHADER0([]() { | 2187 functions += SHADER0([]() { |
2123 float GetAlpha(vec2 ya_clamped) { | 2188 float GetAlpha(vec2 ya_clamped) { |
2124 return alpha * TextureLookup(a_texture, ya_clamped).x; | 2189 return alpha * TextureLookup(a_texture, ya_clamped).x; |
2125 } | 2190 } |
2126 }); | 2191 }); |
2127 } else { | 2192 } else { |
2128 functions += SHADER0([]() { | 2193 functions += SHADER0([]() { |
2129 float GetAlpha(vec2 ya_clamped) { return alpha; } | 2194 float GetAlpha(vec2 ya_clamped) { return alpha; } |
2130 }); | 2195 }); |
2131 } | 2196 } |
2132 | 2197 |
2133 if (use_color_lut_) { | 2198 if (use_color_lut_) { |
2134 head += " uniform sampler2D lut_texture;\n"; | 2199 head += " uniform sampler2D lut_texture;\n"; |
2135 head += " uniform float resource_multiplier;\n"; | |
2136 head += " uniform float resource_offset;\n"; | |
2137 functions += SHADER0([]() { | 2200 functions += SHADER0([]() { |
2138 vec4 LUT(sampler2D sampler, vec3 pos, float size) { | 2201 vec4 LUT(sampler2D sampler, vec3 pos, float size) { |
2139 pos *= size - 1.0; | 2202 pos *= size - 1.0; |
2140 // Select layer | 2203 // Select layer |
2141 float layer = min(floor(pos.x), size - 2.0); | 2204 float layer = min(floor(pos.x), size - 2.0); |
2142 // Compress the yz coordinates so they stay within | 2205 // Compress the yz coordinates so they stay within |
2143 // [0.5 .. 31.5] / 32 (assuming a LUT size of 32^3) | 2206 // [0.5 .. 31.5] / 32 (assuming a LUT size of 32^3) |
2144 pos.yz = (pos.yz + vec2(0.5)) / size; | 2207 pos.yz = (pos.yz + vec2(0.5)) / size; |
2145 pos.z = (pos.z + layer) / size; | 2208 pos.z = (pos.z + layer) / size; |
2146 return mix(texture2D(sampler, pos.yz), | 2209 return mix(texture2D(sampler, pos.yz), |
2147 texture2D(sampler, pos.yz + vec2(0, 1.0 / size)), | 2210 texture2D(sampler, pos.yz + vec2(0, 1.0 / size)), |
2148 pos.x - layer); | 2211 pos.x - layer); |
2149 } | 2212 } |
2150 | |
2151 vec3 yuv2rgb(vec3 yuv) { | |
2152 yuv = (yuv - vec3(resource_offset)) * resource_multiplier; | |
2153 return LUT(lut_texture, yuv, 32.0).xyz; | |
2154 } | |
2155 }); | 2213 }); |
2214 if (highbit_texture_ == HIGHBIT_LUMINANCE_F16) { | |
2215 head += " uniform float resource_multiplier;\n"; | |
2216 head += " uniform float resource_offset;\n"; | |
2217 functions += SHADER0([]() { | |
2218 vec3 yuv2rgb(vec3 yuv) { | |
2219 yuv = (yuv - vec3(resource_offset)) * resource_multiplier; | |
2220 return LUT(lut_texture, yuv, 32.0).xyz; | |
2221 } | |
2222 }); | |
2223 } else { | |
2224 functions += SHADER0([]() { | |
2225 vec3 yuv2rgb(vec3 yuv) { return LUT(lut_texture, yuv, 32.0).xyz; } | |
2226 }); | |
2227 } | |
2156 } else { | 2228 } else { |
2157 head += " uniform mat3 yuv_matrix;\n"; | 2229 head += " uniform mat3 yuv_matrix;\n"; |
2158 head += " uniform vec3 yuv_adj;\n"; | 2230 head += " uniform vec3 yuv_adj;\n"; |
2159 functions += SHADER0([]() { | 2231 functions += SHADER0([]() { |
2160 vec3 yuv2rgb(vec3 yuv) { return yuv_matrix * (yuv + yuv_adj); } | 2232 vec3 yuv2rgb(vec3 yuv) { return yuv_matrix * (yuv + yuv_adj); } |
2161 }); | 2233 }); |
2162 } | 2234 } |
2163 | 2235 |
2164 functions += SHADER0([]() { | 2236 functions += SHADER0([]() { |
2165 void main() { | 2237 void main() { |
2166 vec2 ya_clamped = | 2238 vec2 ya_clamped = |
2167 max(ya_clamp_rect.xy, min(ya_clamp_rect.zw, v_yaTexCoord)); | 2239 max(ya_clamp_rect.xy, min(ya_clamp_rect.zw, v_yaTexCoord)); |
2168 float y_raw = TextureLookup(y_texture, ya_clamped).x; | 2240 float y_raw = GetY(ya_clamped); |
2169 vec2 uv_clamped = | 2241 vec2 uv_clamped = |
2170 max(uv_clamp_rect.xy, min(uv_clamp_rect.zw, v_uvTexCoord)); | 2242 max(uv_clamp_rect.xy, min(uv_clamp_rect.zw, v_uvTexCoord)); |
2171 vec3 yuv = vec3(y_raw, GetUV(uv_clamped)); | 2243 vec3 yuv = vec3(y_raw, GetUV(uv_clamped)); |
2172 gl_FragColor = vec4(yuv2rgb(yuv), 1.0) * GetAlpha(ya_clamped); | 2244 gl_FragColor = vec4(yuv2rgb(yuv), 1.0) * GetAlpha(ya_clamped); |
2173 } | 2245 } |
2174 }); | 2246 }); |
2175 | 2247 |
2176 return FRAGMENT_SHADER(head, functions); | 2248 return FRAGMENT_SHADER(head, functions); |
2177 } | 2249 } |
2178 | 2250 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2252 void main() { | 2324 void main() { |
2253 vec4 d4 = min(edge_dist[0], edge_dist[1]); | 2325 vec4 d4 = min(edge_dist[0], edge_dist[1]); |
2254 vec2 d2 = min(d4.xz, d4.yw); | 2326 vec2 d2 = min(d4.xz, d4.yw); |
2255 float aa = clamp(gl_FragCoord.w * min(d2.x, d2.y), 0.0, 1.0); | 2327 float aa = clamp(gl_FragCoord.w * min(d2.x, d2.y), 0.0, 1.0); |
2256 gl_FragColor = color * aa; | 2328 gl_FragColor = color * aa; |
2257 } | 2329 } |
2258 }); | 2330 }); |
2259 } | 2331 } |
2260 | 2332 |
2261 } // namespace cc | 2333 } // namespace cc |
OLD | NEW |