Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Side by Side Diff: cc/output/shader.cc

Issue 2608863002: The great shader refactor: Merge fragment shaders into an uber-shader (Closed)
Patch Set: Set upstream branch... Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cc/output/shader.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include <vector> 10 #include <vector>
(...skipping 752 matching lines...) Expand 10 before | Expand all | Expand 10 after
763 DCHECK_LT(static_cast<size_t>(POS) + 2, arraysize(X)); \ 763 DCHECK_LT(static_cast<size_t>(POS) + 2, arraysize(X)); \
764 backdrop_location_ = locations[POS]; \ 764 backdrop_location_ = locations[POS]; \
765 original_backdrop_location_ = locations[POS + 1]; \ 765 original_backdrop_location_ = locations[POS + 1]; \
766 backdrop_rect_location_ = locations[POS + 2]; \ 766 backdrop_rect_location_ = locations[POS + 2]; \
767 } 767 }
768 768
769 FragmentShaderBase::FragmentShaderBase() {} 769 FragmentShaderBase::FragmentShaderBase() {}
770 770
771 std::string FragmentShaderBase::GetShaderString(TexCoordPrecision precision, 771 std::string FragmentShaderBase::GetShaderString(TexCoordPrecision precision,
772 SamplerType sampler) const { 772 SamplerType sampler) const {
773 // The AA shader values will use TexCoordPrecision.
774 if (has_aa_ && precision == TEX_COORD_PRECISION_NA)
775 precision = TEX_COORD_PRECISION_MEDIUM;
773 return SetFragmentTexCoordPrecision( 776 return SetFragmentTexCoordPrecision(
774 precision, SetFragmentSamplerType( 777 precision, SetFragmentSamplerType(
775 sampler, SetBlendModeFunctions(GetShaderSource()))); 778 sampler, SetBlendModeFunctions(GetShaderSource())));
776 } 779 }
777 780
778 void FragmentShaderBase::Init(GLES2Interface* context, 781 void FragmentShaderBase::Init(GLES2Interface* context,
779 unsigned program, 782 unsigned program,
780 int* base_uniform_index) { 783 int* base_uniform_index) {
781 std::vector<const char*> uniforms; 784 std::vector<const char*> uniforms;
782 std::vector<int> locations; 785 std::vector<int> locations;
783 if (has_blend_mode()) { 786 if (has_blend_mode()) {
784 uniforms.push_back("s_backdropTexture"); 787 uniforms.push_back("s_backdropTexture");
785 uniforms.push_back("s_originalBackdropTexture"); 788 uniforms.push_back("s_originalBackdropTexture");
786 uniforms.push_back("backdropRect"); 789 uniforms.push_back("backdropRect");
787 } 790 }
788 if (has_mask_sampler_) { 791 if (has_mask_sampler_) {
789 uniforms.push_back("s_mask"); 792 uniforms.push_back("s_mask");
790 uniforms.push_back("maskTexCoordScale"); 793 uniforms.push_back("maskTexCoordScale");
791 uniforms.push_back("maskTexCoordOffset"); 794 uniforms.push_back("maskTexCoordOffset");
792 } 795 }
793 if (has_color_matrix_) { 796 if (has_color_matrix_) {
794 uniforms.push_back("colorMatrix"); 797 uniforms.push_back("colorMatrix");
795 uniforms.push_back("colorOffset"); 798 uniforms.push_back("colorOffset");
796 } 799 }
797 if (has_sampler_)
798 uniforms.push_back("s_texture");
799 if (has_uniform_alpha_) 800 if (has_uniform_alpha_)
800 uniforms.push_back("alpha"); 801 uniforms.push_back("alpha");
801 if (has_background_color_) 802 if (has_background_color_)
802 uniforms.push_back("background_color"); 803 uniforms.push_back("background_color");
803 if (has_fragment_tex_transform_) 804 switch (input_color_type_) {
804 uniforms.push_back("fragmentTexTransform"); 805 case INPUT_COLOR_SOURCE_RGBA_TEXTURE:
805 if (has_uniform_color_) 806 uniforms.push_back("s_texture");
806 uniforms.push_back("color"); 807 if (has_rgba_fragment_tex_transform_)
808 uniforms.push_back("fragmentTexTransform");
809 break;
810 case INPUT_COLOR_SOURCE_UNIFORM:
811 uniforms.push_back("color");
812 break;
813 }
807 814
808 locations.resize(uniforms.size()); 815 locations.resize(uniforms.size());
809 816
810 GetProgramUniformLocations(context, program, uniforms.size(), uniforms.data(), 817 GetProgramUniformLocations(context, program, uniforms.size(), uniforms.data(),
811 locations.data(), base_uniform_index); 818 locations.data(), base_uniform_index);
812 819
813 size_t index = 0; 820 size_t index = 0;
814 if (has_blend_mode()) { 821 if (has_blend_mode()) {
815 backdrop_location_ = locations[index++]; 822 backdrop_location_ = locations[index++];
816 original_backdrop_location_ = locations[index++]; 823 original_backdrop_location_ = locations[index++];
817 backdrop_rect_location_ = locations[index++]; 824 backdrop_rect_location_ = locations[index++];
818 } 825 }
819 if (has_mask_sampler_) { 826 if (has_mask_sampler_) {
820 mask_sampler_location_ = locations[index++]; 827 mask_sampler_location_ = locations[index++];
821 mask_tex_coord_scale_location_ = locations[index++]; 828 mask_tex_coord_scale_location_ = locations[index++];
822 mask_tex_coord_offset_location_ = locations[index++]; 829 mask_tex_coord_offset_location_ = locations[index++];
823 } 830 }
824 if (has_color_matrix_) { 831 if (has_color_matrix_) {
825 color_matrix_location_ = locations[index++]; 832 color_matrix_location_ = locations[index++];
826 color_offset_location_ = locations[index++]; 833 color_offset_location_ = locations[index++];
827 } 834 }
828 if (has_sampler_)
829 sampler_location_ = locations[index++];
830 if (has_uniform_alpha_) 835 if (has_uniform_alpha_)
831 alpha_location_ = locations[index++]; 836 alpha_location_ = locations[index++];
832 if (has_background_color_) 837 if (has_background_color_)
833 background_color_location_ = locations[index++]; 838 background_color_location_ = locations[index++];
834 if (has_fragment_tex_transform_) 839 switch (input_color_type_) {
835 fragment_tex_transform_location_ = locations[index++]; 840 case INPUT_COLOR_SOURCE_RGBA_TEXTURE:
836 if (has_uniform_color_) 841 sampler_location_ = locations[index++];
837 color_location_ = locations[index++]; 842 if (has_rgba_fragment_tex_transform_)
843 fragment_tex_transform_location_ = locations[index++];
844 break;
845 case INPUT_COLOR_SOURCE_UNIFORM:
846 color_location_ = locations[index++];
847 break;
848 }
838 DCHECK_EQ(index, locations.size()); 849 DCHECK_EQ(index, locations.size());
839 } 850 }
840 851
841 void FragmentShaderBase::FillLocations(ShaderLocations* locations) const { 852 void FragmentShaderBase::FillLocations(ShaderLocations* locations) const {
842 if (has_blend_mode()) { 853 if (has_blend_mode()) {
843 locations->backdrop = backdrop_location_; 854 locations->backdrop = backdrop_location_;
844 locations->backdrop_rect = backdrop_rect_location_; 855 locations->backdrop_rect = backdrop_rect_location_;
845 } 856 }
846 if (mask_for_background()) 857 if (mask_for_background())
847 locations->original_backdrop = original_backdrop_location_; 858 locations->original_backdrop = original_backdrop_location_;
848 if (has_mask_sampler_) { 859 if (has_mask_sampler_) {
849 locations->mask_sampler = mask_sampler_location_; 860 locations->mask_sampler = mask_sampler_location_;
850 locations->mask_tex_coord_scale = mask_tex_coord_scale_location_; 861 locations->mask_tex_coord_scale = mask_tex_coord_scale_location_;
851 locations->mask_tex_coord_offset = mask_tex_coord_offset_location_; 862 locations->mask_tex_coord_offset = mask_tex_coord_offset_location_;
852 } 863 }
853 if (has_color_matrix_) { 864 if (has_color_matrix_) {
854 locations->color_matrix = color_matrix_location_; 865 locations->color_matrix = color_matrix_location_;
855 locations->color_offset = color_offset_location_; 866 locations->color_offset = color_offset_location_;
856 } 867 }
857 if (has_sampler_)
858 locations->sampler = sampler_location_;
859 if (has_uniform_alpha_) 868 if (has_uniform_alpha_)
860 locations->alpha = alpha_location_; 869 locations->alpha = alpha_location_;
870 switch (input_color_type_) {
871 case INPUT_COLOR_SOURCE_RGBA_TEXTURE:
872 locations->sampler = sampler_location_;
873 break;
874 case INPUT_COLOR_SOURCE_UNIFORM:
875 break;
876 }
861 } 877 }
862 878
863 std::string FragmentShaderBase::SetBlendModeFunctions( 879 std::string FragmentShaderBase::SetBlendModeFunctions(
864 const std::string& shader_string) const { 880 const std::string& shader_string) const {
865 if (shader_string.find("ApplyBlendMode") == std::string::npos) 881 if (shader_string.find("ApplyBlendMode") == std::string::npos)
866 return shader_string; 882 return shader_string;
867 883
868 if (!has_blend_mode()) { 884 if (!has_blend_mode()) {
869 return "#define ApplyBlendMode(X, Y) (X)\n" + shader_string; 885 return "#define ApplyBlendMode(X, Y) (X)\n" + shader_string;
870 } 886 }
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
1148 "result.rgb = set_luminance(dst.rgb * src.a," 1164 "result.rgb = set_luminance(dst.rgb * src.a,"
1149 " srcDstAlpha.a," 1165 " srcDstAlpha.a,"
1150 " srcDstAlpha.rgb);" 1166 " srcDstAlpha.rgb);"
1151 "result.rgb += (1.0 - src.a) * dst.rgb + (1.0 - dst.a) * src.rgb;"; 1167 "result.rgb += (1.0 - src.a) * dst.rgb + (1.0 - dst.a) * src.rgb;";
1152 case BLEND_MODE_NONE: 1168 case BLEND_MODE_NONE:
1153 NOTREACHED(); 1169 NOTREACHED();
1154 } 1170 }
1155 return "result = vec4(1.0, 0.0, 0.0, 1.0);"; 1171 return "result = vec4(1.0, 0.0, 0.0, 1.0);";
1156 } 1172 }
1157 1173
1158 std::string FragmentShaderRGBATexAlpha::GetShaderSource() const { 1174 std::string FragmentShaderBase::GetShaderSource() const {
1159 return SHADER0([]() { 1175 std::string header = "precision mediump float;\n";
1160 precision mediump float; 1176 std::string source = "void main() {\n";
1161 varying TexCoordPrecision vec2 v_texCoord;
1162 uniform SamplerType s_texture;
1163 uniform float alpha;
1164 void main() {
1165 vec4 texColor = TextureLookup(s_texture, v_texCoord);
1166 gl_FragColor = ApplyBlendMode(texColor * alpha, 0.0);
1167 }
1168 });
1169 }
1170 1177
1171 std::string FragmentShaderRGBATexColorMatrixAlpha::GetShaderSource() const { 1178 #define HDR(x) \
1172 return SHADER0([]() { 1179 do { \
1173 precision mediump float; 1180 header += x + std::string("\n"); \
1174 varying TexCoordPrecision vec2 v_texCoord; 1181 } while (0)
1175 uniform SamplerType s_texture; 1182 #define SRC(x) \
1176 uniform float alpha; 1183 do { \
1177 uniform mat4 colorMatrix; 1184 source += std::string(" ") + x + std::string("\n"); \
1178 uniform vec4 colorOffset; 1185 } while (0)
1179 void main() {
1180 vec4 texColor = TextureLookup(s_texture, v_texCoord);
1181 float nonZeroAlpha = max(texColor.a, 0.00001);
1182 texColor = vec4(texColor.rgb / nonZeroAlpha, nonZeroAlpha);
1183 texColor = colorMatrix * texColor + colorOffset;
1184 texColor.rgb *= texColor.a;
1185 texColor = clamp(texColor, 0.0, 1.0);
1186 gl_FragColor = ApplyBlendMode(texColor * alpha, 0.0);
1187 }
1188 });
1189 }
1190 1186
1191 std::string FragmentShaderRGBATexVaryingAlpha::GetShaderSource() const { 1187 // Read the input into vec4 texColor.
1192 return SHADER0([]() { 1188 switch (input_color_type_) {
1193 precision mediump float; 1189 case INPUT_COLOR_SOURCE_RGBA_TEXTURE:
1194 varying TexCoordPrecision vec2 v_texCoord; 1190 if (has_rgba_as_sampler_2d_)
1195 varying float v_alpha; 1191 HDR("uniform sampler2D s_texture;");
1196 uniform SamplerType s_texture; 1192 else
1197 void main() { 1193 HDR("uniform SamplerType s_texture;");
1198 vec4 texColor = TextureLookup(s_texture, v_texCoord); 1194 HDR("varying TexCoordPrecision vec2 v_texCoord;");
1199 gl_FragColor = texColor * v_alpha; 1195 if (has_rgba_fragment_tex_transform_) {
1200 } 1196 HDR("uniform TexCoordPrecision vec4 fragmentTexTransform;");
1201 }); 1197 SRC("// Transformed texture lookup");
1202 } 1198 SRC("TexCoordPrecision vec2 texCoord =");
1199 SRC(" clamp(v_texCoord, 0.0, 1.0) * fragmentTexTransform.zw +");
1200 SRC(" fragmentTexTransform.xy;");
1201 SRC("vec4 texColor = TextureLookup(s_texture, texCoord);");
1202 DCHECK(!has_rgba_as_sampler_2d_);
1203 } else {
1204 SRC("// Texture lookup");
1205 if (has_rgba_as_sampler_2d_)
enne (OOO) 2017/01/03 22:32:31 Do you need this conditional here? It seems like T
ccameron 2017/01/03 22:48:34 Some of the shaders ignore the SamplerType argumen
1206 SRC("vec4 texColor = texture2D(s_texture, v_texCoord);")
1207 else
1208 SRC("vec4 texColor = TextureLookup(s_texture, v_texCoord);")
1209 }
1210 break;
1211 case INPUT_COLOR_SOURCE_UNIFORM:
1212 DCHECK(!has_rgba_as_sampler_2d_);
1213 DCHECK(!has_rgba_fragment_tex_transform_);
1214 HDR("uniform vec4 color;");
1215 SRC("// Uniform color");
1216 SRC("vec4 texColor = color;");
1217 break;
1218 }
1219 // Apply the color matrix to texColor.
1220 if (has_color_matrix_) {
1221 HDR("uniform mat4 colorMatrix;");
1222 HDR("uniform vec4 colorOffset;");
1223 SRC("// Apply color matrix");
1224 SRC("float nonZeroAlpha = max(texColor.a, 0.00001);");
1225 SRC("texColor = vec4(texColor.rgb / nonZeroAlpha, nonZeroAlpha);");
1226 SRC("texColor = colorMatrix * texColor + colorOffset;");
1227 SRC("texColor.rgb *= texColor.a;");
1228 SRC("texColor = clamp(texColor, 0.0, 1.0);");
1229 }
1203 1230
1204 std::string FragmentShaderRGBATexPremultiplyAlpha::GetShaderSource() const { 1231 // Read the mask texture.
1205 return SHADER0([]() { 1232 if (has_mask_sampler_) {
1206 precision mediump float; 1233 HDR("uniform SamplerType s_mask;");
1207 varying TexCoordPrecision vec2 v_texCoord; 1234 HDR("uniform vec2 maskTexCoordScale;");
1208 varying float v_alpha; 1235 HDR("uniform vec2 maskTexCoordOffset;");
1209 uniform SamplerType s_texture; 1236 SRC("// Read the mask");
1210 void main() { 1237 SRC("TexCoordPrecision vec2 maskTexCoord =");
1211 vec4 texColor = TextureLookup(s_texture, v_texCoord); 1238 SRC(" vec2(maskTexCoordOffset.x + v_texCoord.x * maskTexCoordScale.x,");
1212 texColor.rgb *= texColor.a; 1239 SRC(" maskTexCoordOffset.y + v_texCoord.y * maskTexCoordScale.y);");
1213 gl_FragColor = texColor * v_alpha; 1240 SRC("vec4 maskColor = TextureLookup(s_mask, maskTexCoord);");
1214 } 1241 }
1215 });
1216 }
1217 1242
1218 std::string FragmentShaderTexBackgroundVaryingAlpha::GetShaderSource() const { 1243 // Compute AA.
1219 return SHADER0([]() { 1244 if (has_aa_) {
1220 precision mediump float; 1245 HDR("varying TexCoordPrecision vec4 edge_dist[2]; // 8 edge distances.");
1221 varying TexCoordPrecision vec2 v_texCoord; 1246 SRC("// Compute AA");
1222 varying float v_alpha; 1247 SRC("vec4 d4 = min(edge_dist[0], edge_dist[1]);");
1223 uniform vec4 background_color; 1248 SRC("vec2 d2 = min(d4.xz, d4.yw);");
1224 uniform SamplerType s_texture; 1249 SRC("float aa = clamp(gl_FragCoord.w * min(d2.x, d2.y), 0.0, 1.0);");
1225 void main() { 1250 }
1226 vec4 texColor = TextureLookup(s_texture, v_texCoord);
1227 texColor += background_color * (1.0 - texColor.a);
1228 gl_FragColor = texColor * v_alpha;
1229 }
1230 });
1231 }
1232 1251
1233 std::string FragmentShaderTexBackgroundPremultiplyAlpha::GetShaderSource() 1252 // Premultiply by alpha.
1234 const { 1253 if (has_premultiply_alpha_) {
1235 return SHADER0([]() { 1254 SRC("// Premultiply alpha");
1236 precision mediump float; 1255 SRC("texColor.rgb *= texColor.a;");
1237 varying TexCoordPrecision vec2 v_texCoord; 1256 }
1238 varying float v_alpha;
1239 uniform vec4 background_color;
1240 uniform SamplerType s_texture;
1241 void main() {
1242 vec4 texColor = TextureLookup(s_texture, v_texCoord);
1243 texColor.rgb *= texColor.a;
1244 texColor += background_color * (1.0 - texColor.a);
1245 gl_FragColor = texColor * v_alpha;
1246 }
1247 });
1248 }
1249 1257
1250 std::string FragmentShaderRGBATexOpaque::GetShaderSource() const { 1258 // Apply background texture.
1251 return SHADER0([]() { 1259 if (has_background_color_) {
1252 precision mediump float; 1260 HDR("uniform vec4 background_color;");
1253 varying TexCoordPrecision vec2 v_texCoord; 1261 SRC("// Apply uniform background color blending");
1254 uniform SamplerType s_texture; 1262 SRC("texColor += background_color * (1.0 - texColor.a);");
1255 void main() { 1263 }
1256 vec4 texColor = TextureLookup(s_texture, v_texCoord);
1257 gl_FragColor = vec4(texColor.rgb, 1.0);
1258 }
1259 });
1260 }
1261 1264
1262 std::string FragmentShaderRGBATex::GetShaderSource() const { 1265 // Apply swizzle.
1263 return SHADER0([]() { 1266 if (has_swizzle_) {
1264 precision mediump float; 1267 SRC("// Apply swizzle");
1265 varying TexCoordPrecision vec2 v_texCoord; 1268 SRC("texColor = texColor.bgra;\n");
1266 uniform SamplerType s_texture; 1269 }
1267 void main() { gl_FragColor = TextureLookup(s_texture, v_texCoord); }
1268 });
1269 }
1270 1270
1271 std::string FragmentShaderRGBATexSwizzleAlpha::GetShaderSource() const { 1271 // Include header text for alpha.
1272 return SHADER0([]() { 1272 if (has_uniform_alpha_) {
1273 precision mediump float; 1273 HDR("uniform float alpha;");
1274 varying TexCoordPrecision vec2 v_texCoord; 1274 }
1275 uniform SamplerType s_texture; 1275 if (has_varying_alpha_) {
1276 uniform float alpha; 1276 HDR("varying float v_alpha;");
1277 void main() { 1277 }
1278 vec4 texColor = TextureLookup(s_texture, v_texCoord);
1279 gl_FragColor =
1280 vec4(texColor.z, texColor.y, texColor.x, texColor.w) * alpha;
1281 }
1282 });
1283 }
1284 1278
1285 std::string FragmentShaderRGBATexSwizzleOpaque::GetShaderSource() const { 1279 // Apply uniform alpha, aa, varying alpha, and the mask.
1286 return SHADER0([]() { 1280 if (has_varying_alpha_ || has_aa_ || has_uniform_alpha_ ||
1287 precision mediump float; 1281 has_mask_sampler_) {
1288 varying TexCoordPrecision vec2 v_texCoord; 1282 SRC("// Apply alpha from uniform, varying, aa, and mask.");
1289 uniform SamplerType s_texture; 1283 std::string line = " texColor = texColor";
1290 void main() { 1284 if (has_varying_alpha_)
1291 vec4 texColor = TextureLookup(s_texture, v_texCoord); 1285 line += " * v_alpha";
1292 gl_FragColor = vec4(texColor.z, texColor.y, texColor.x, 1.0); 1286 if (has_uniform_alpha_)
1293 } 1287 line += " * alpha";
1294 }); 1288 if (has_aa_)
1295 } 1289 line += " * aa";
1290 if (has_mask_sampler_)
1291 line += " * maskColor.a";
1292 line += ";\n";
1293 source += line;
1294 }
1296 1295
1297 std::string FragmentShaderRGBATexAlphaAA::GetShaderSource() const { 1296 // Write the fragment color.
1298 return SHADER0([]() { 1297 SRC("// Write the fragment color");
1299 precision mediump float; 1298 switch (frag_color_mode_) {
1300 uniform SamplerType s_texture; 1299 case FRAG_COLOR_MODE_DEFAULT:
1301 uniform float alpha; 1300 DCHECK_EQ(blend_mode_, BLEND_MODE_NONE);
1302 varying TexCoordPrecision vec2 v_texCoord; 1301 SRC("gl_FragColor = texColor;");
1303 varying TexCoordPrecision vec4 edge_dist[2]; // 8 edge distances. 1302 break;
1304 void main() { 1303 case FRAG_COLOR_MODE_OPAQUE:
1305 vec4 texColor = TextureLookup(s_texture, v_texCoord); 1304 DCHECK_EQ(blend_mode_, BLEND_MODE_NONE);
1306 vec4 d4 = min(edge_dist[0], edge_dist[1]); 1305 SRC("gl_FragColor = vec4(texColor.rgb, 1.0);");
1307 vec2 d2 = min(d4.xz, d4.yw); 1306 break;
1308 float aa = clamp(gl_FragCoord.w * min(d2.x, d2.y), 0.0, 1.0); 1307 case FRAG_COLOR_MODE_APPLY_BLEND_MODE:
1309 gl_FragColor = ApplyBlendMode(texColor * alpha * aa, 0.0); 1308 if (has_mask_sampler_)
1310 } 1309 SRC("gl_FragColor = ApplyBlendMode(texColor, maskColor.w);");
1311 }); 1310 else
1312 } 1311 SRC("gl_FragColor = ApplyBlendMode(texColor, 0.0);");
1312 break;
1313 }
1314 source += "}\n";
1313 1315
1314 std::string FragmentShaderRGBATexClampAlphaAA::GetShaderSource() const { 1316 #undef HDR
1315 return SHADER0([]() { 1317 #undef SRC
1316 precision mediump float;
1317 uniform SamplerType s_texture;
1318 uniform float alpha;
1319 uniform TexCoordPrecision vec4 fragmentTexTransform;
1320 varying TexCoordPrecision vec2 v_texCoord;
1321 varying TexCoordPrecision vec4 edge_dist[2]; // 8 edge distances.
1322 void main() {
1323 TexCoordPrecision vec2 texCoord =
1324 clamp(v_texCoord, 0.0, 1.0) * fragmentTexTransform.zw +
1325 fragmentTexTransform.xy;
1326 vec4 texColor = TextureLookup(s_texture, texCoord);
1327 vec4 d4 = min(edge_dist[0], edge_dist[1]);
1328 vec2 d2 = min(d4.xz, d4.yw);
1329 float aa = clamp(gl_FragCoord.w * min(d2.x, d2.y), 0.0, 1.0);
1330 gl_FragColor = texColor * alpha * aa;
1331 }
1332 });
1333 }
1334 1318
1335 std::string FragmentShaderRGBATexClampSwizzleAlphaAA::GetShaderSource() const { 1319 return header + source;
1336 return SHADER0([]() {
1337 precision mediump float;
1338 uniform SamplerType s_texture;
1339 uniform float alpha;
1340 uniform TexCoordPrecision vec4 fragmentTexTransform;
1341 varying TexCoordPrecision vec2 v_texCoord;
1342 varying TexCoordPrecision vec4 edge_dist[2]; // 8 edge distances.
1343 void main() {
1344 TexCoordPrecision vec2 texCoord =
1345 clamp(v_texCoord, 0.0, 1.0) * fragmentTexTransform.zw +
1346 fragmentTexTransform.xy;
1347 vec4 texColor = TextureLookup(s_texture, texCoord);
1348 vec4 d4 = min(edge_dist[0], edge_dist[1]);
1349 vec2 d2 = min(d4.xz, d4.yw);
1350 float aa = clamp(gl_FragCoord.w * min(d2.x, d2.y), 0.0, 1.0);
1351 gl_FragColor =
1352 vec4(texColor.z, texColor.y, texColor.x, texColor.w) * alpha * aa;
1353 }
1354 });
1355 }
1356
1357 std::string FragmentShaderRGBATexAlphaMask::GetShaderSource() const {
1358 return SHADER0([]() {
1359 precision mediump float;
1360 varying TexCoordPrecision vec2 v_texCoord;
1361 uniform sampler2D s_texture;
1362 uniform SamplerType s_mask;
1363 uniform TexCoordPrecision vec2 maskTexCoordScale;
1364 uniform TexCoordPrecision vec2 maskTexCoordOffset;
1365 uniform float alpha;
1366 void main() {
1367 vec4 texColor = texture2D(s_texture, v_texCoord);
1368 TexCoordPrecision vec2 maskTexCoord =
1369 vec2(maskTexCoordOffset.x + v_texCoord.x * maskTexCoordScale.x,
1370 maskTexCoordOffset.y + v_texCoord.y * maskTexCoordScale.y);
1371 vec4 maskColor = TextureLookup(s_mask, maskTexCoord);
1372 gl_FragColor = ApplyBlendMode(
1373 texColor * alpha * maskColor.w, maskColor.w);
1374 }
1375 });
1376 }
1377
1378 std::string FragmentShaderRGBATexAlphaMaskAA::GetShaderSource() const {
1379 return SHADER0([]() {
1380 precision mediump float;
1381 uniform sampler2D s_texture;
1382 uniform SamplerType s_mask;
1383 uniform TexCoordPrecision vec2 maskTexCoordScale;
1384 uniform TexCoordPrecision vec2 maskTexCoordOffset;
1385 uniform float alpha;
1386 varying TexCoordPrecision vec2 v_texCoord;
1387 varying TexCoordPrecision vec4 edge_dist[2]; // 8 edge distances.
1388 void main() {
1389 vec4 texColor = texture2D(s_texture, v_texCoord);
1390 TexCoordPrecision vec2 maskTexCoord =
1391 vec2(maskTexCoordOffset.x + v_texCoord.x * maskTexCoordScale.x,
1392 maskTexCoordOffset.y + v_texCoord.y * maskTexCoordScale.y);
1393 vec4 maskColor = TextureLookup(s_mask, maskTexCoord);
1394 vec4 d4 = min(edge_dist[0], edge_dist[1]);
1395 vec2 d2 = min(d4.xz, d4.yw);
1396 float aa = clamp(gl_FragCoord.w * min(d2.x, d2.y), 0.0, 1.0);
1397 gl_FragColor = ApplyBlendMode(
1398 texColor * alpha * maskColor.w * aa, maskColor.w);
1399 }
1400 });
1401 }
1402
1403 std::string FragmentShaderRGBATexAlphaMaskColorMatrixAA::GetShaderSource()
1404 const {
1405 return SHADER0([]() {
1406 precision mediump float;
1407 uniform sampler2D s_texture;
1408 uniform SamplerType s_mask;
1409 uniform vec2 maskTexCoordScale;
1410 uniform vec2 maskTexCoordOffset;
1411 uniform mat4 colorMatrix;
1412 uniform vec4 colorOffset;
1413 uniform float alpha;
1414 varying TexCoordPrecision vec2 v_texCoord;
1415 varying TexCoordPrecision vec4 edge_dist[2]; // 8 edge distances.
1416 void main() {
1417 vec4 texColor = texture2D(s_texture, v_texCoord);
1418 float nonZeroAlpha = max(texColor.a, 0.00001);
1419 texColor = vec4(texColor.rgb / nonZeroAlpha, nonZeroAlpha);
1420 texColor = colorMatrix * texColor + colorOffset;
1421 texColor.rgb *= texColor.a;
1422 texColor = clamp(texColor, 0.0, 1.0);
1423 TexCoordPrecision vec2 maskTexCoord =
1424 vec2(maskTexCoordOffset.x + v_texCoord.x * maskTexCoordScale.x,
1425 maskTexCoordOffset.y + v_texCoord.y * maskTexCoordScale.y);
1426 vec4 maskColor = TextureLookup(s_mask, maskTexCoord);
1427 vec4 d4 = min(edge_dist[0], edge_dist[1]);
1428 vec2 d2 = min(d4.xz, d4.yw);
1429 float aa = clamp(gl_FragCoord.w * min(d2.x, d2.y), 0.0, 1.0);
1430 gl_FragColor = ApplyBlendMode(
1431 texColor * alpha * maskColor.w * aa, maskColor.w);
1432 }
1433 });
1434 }
1435
1436 std::string FragmentShaderRGBATexAlphaColorMatrixAA::GetShaderSource() const {
1437 return SHADER0([]() {
1438 precision mediump float;
1439 uniform SamplerType s_texture;
1440 uniform float alpha;
1441 uniform mat4 colorMatrix;
1442 uniform vec4 colorOffset;
1443 varying TexCoordPrecision vec2 v_texCoord;
1444 varying TexCoordPrecision vec4 edge_dist[2]; // 8 edge distances.
1445 void main() {
1446 vec4 texColor = TextureLookup(s_texture, v_texCoord);
1447 float nonZeroAlpha = max(texColor.a, 0.00001);
1448 texColor = vec4(texColor.rgb / nonZeroAlpha, nonZeroAlpha);
1449 texColor = colorMatrix * texColor + colorOffset;
1450 texColor.rgb *= texColor.a;
1451 texColor = clamp(texColor, 0.0, 1.0);
1452 vec4 d4 = min(edge_dist[0], edge_dist[1]);
1453 vec2 d2 = min(d4.xz, d4.yw);
1454 float aa = clamp(gl_FragCoord.w * min(d2.x, d2.y), 0.0, 1.0);
1455 gl_FragColor = ApplyBlendMode(texColor * alpha * aa, 0.0);
1456 }
1457 });
1458 }
1459
1460 std::string FragmentShaderRGBATexAlphaMaskColorMatrix::GetShaderSource() const {
1461 return SHADER0([]() {
1462 precision mediump float;
1463 varying TexCoordPrecision vec2 v_texCoord;
1464 uniform sampler2D s_texture;
1465 uniform SamplerType s_mask;
1466 uniform vec2 maskTexCoordScale;
1467 uniform vec2 maskTexCoordOffset;
1468 uniform mat4 colorMatrix;
1469 uniform vec4 colorOffset;
1470 uniform float alpha;
1471 void main() {
1472 vec4 texColor = texture2D(s_texture, v_texCoord);
1473 float nonZeroAlpha = max(texColor.a, 0.00001);
1474 texColor = vec4(texColor.rgb / nonZeroAlpha, nonZeroAlpha);
1475 texColor = colorMatrix * texColor + colorOffset;
1476 texColor.rgb *= texColor.a;
1477 texColor = clamp(texColor, 0.0, 1.0);
1478 TexCoordPrecision vec2 maskTexCoord =
1479 vec2(maskTexCoordOffset.x + v_texCoord.x * maskTexCoordScale.x,
1480 maskTexCoordOffset.y + v_texCoord.y * maskTexCoordScale.y);
1481 vec4 maskColor = TextureLookup(s_mask, maskTexCoord);
1482 gl_FragColor = ApplyBlendMode(
1483 texColor * alpha * maskColor.w, maskColor.w);
1484 }
1485 });
1486 } 1320 }
1487 1321
1488 FragmentShaderYUVVideo::FragmentShaderYUVVideo() 1322 FragmentShaderYUVVideo::FragmentShaderYUVVideo()
1489 : y_texture_location_(-1), 1323 : y_texture_location_(-1),
1490 u_texture_location_(-1), 1324 u_texture_location_(-1),
1491 v_texture_location_(-1), 1325 v_texture_location_(-1),
1492 uv_texture_location_(-1), 1326 uv_texture_location_(-1),
1493 a_texture_location_(-1), 1327 a_texture_location_(-1),
1494 lut_texture_location_(-1), 1328 lut_texture_location_(-1),
1495 alpha_location_(-1), 1329 alpha_location_(-1),
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1640 vec2 uv_clamped = 1474 vec2 uv_clamped =
1641 max(uv_clamp_rect.xy, min(uv_clamp_rect.zw, v_uvTexCoord)); 1475 max(uv_clamp_rect.xy, min(uv_clamp_rect.zw, v_uvTexCoord));
1642 vec3 yuv = vec3(y_raw, GetUV(uv_clamped)); 1476 vec3 yuv = vec3(y_raw, GetUV(uv_clamped));
1643 gl_FragColor = vec4(yuv2rgb(yuv), 1.0) * GetAlpha(ya_clamped); 1477 gl_FragColor = vec4(yuv2rgb(yuv), 1.0) * GetAlpha(ya_clamped);
1644 } 1478 }
1645 }); 1479 });
1646 1480
1647 return head + functions; 1481 return head + functions;
1648 } 1482 }
1649 1483
1650 std::string FragmentShaderColor::GetShaderSource() const {
1651 return SHADER0([]() {
1652 precision mediump float;
1653 uniform vec4 color;
1654 void main() { gl_FragColor = color; }
1655 });
1656 }
1657
1658 std::string FragmentShaderColorAA::GetShaderSource() const {
1659 return SHADER0([]() {
1660 precision mediump float;
1661 uniform vec4 color;
1662 varying vec4 edge_dist[2]; // 8 edge distances.
1663 void main() {
1664 vec4 d4 = min(edge_dist[0], edge_dist[1]);
1665 vec2 d2 = min(d4.xz, d4.yw);
1666 float aa = clamp(gl_FragCoord.w * min(d2.x, d2.y), 0.0, 1.0);
1667 gl_FragColor = color * aa;
1668 }
1669 });
1670 }
1671
1672 } // namespace cc 1484 } // namespace cc
OLDNEW
« no previous file with comments | « cc/output/shader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698