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

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

Issue 2674763003: cc: Separate YUV from LUT color conversion (Closed)
Patch Set: More rebaselines Created 3 years, 10 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') | third_party/WebKit/LayoutTests/TestExpectations » ('j') | 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 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 if (uv_texture_mode_ == UV_TEXTURE_MODE_UV) 428 if (uv_texture_mode_ == UV_TEXTURE_MODE_UV)
429 uniforms.push_back("uv_texture"); 429 uniforms.push_back("uv_texture");
430 if (uv_texture_mode_ == UV_TEXTURE_MODE_U_V) { 430 if (uv_texture_mode_ == UV_TEXTURE_MODE_U_V) {
431 uniforms.push_back("u_texture"); 431 uniforms.push_back("u_texture");
432 uniforms.push_back("v_texture"); 432 uniforms.push_back("v_texture");
433 } 433 }
434 if (yuv_alpha_texture_mode_ == YUV_HAS_ALPHA_TEXTURE) 434 if (yuv_alpha_texture_mode_ == YUV_HAS_ALPHA_TEXTURE)
435 uniforms.push_back("a_texture"); 435 uniforms.push_back("a_texture");
436 uniforms.push_back("ya_clamp_rect"); 436 uniforms.push_back("ya_clamp_rect");
437 uniforms.push_back("uv_clamp_rect"); 437 uniforms.push_back("uv_clamp_rect");
438 if (color_conversion_mode_ == COLOR_CONVERSION_MODE_LUT_FROM_YUV) { 438 uniforms.push_back("yuv_and_resource_matrix");
439 uniforms.push_back("lut_texture");
440 uniforms.push_back("lut_size");
441 uniforms.push_back("resource_multiplier");
442 uniforms.push_back("resource_offset");
443 } else {
444 uniforms.push_back("yuv_matrix");
445 uniforms.push_back("yuv_adj");
446 }
447 break; 439 break;
448 case INPUT_COLOR_SOURCE_UNIFORM: 440 case INPUT_COLOR_SOURCE_UNIFORM:
449 uniforms.push_back("color"); 441 uniforms.push_back("color");
450 break; 442 break;
451 } 443 }
444 if (color_conversion_mode_ == COLOR_CONVERSION_MODE_LUT) {
445 uniforms.push_back("lut_texture");
446 uniforms.push_back("lut_size");
447 }
452 448
453 locations.resize(uniforms.size()); 449 locations.resize(uniforms.size());
454 450
455 GetProgramUniformLocations(context, program, uniforms.size(), uniforms.data(), 451 GetProgramUniformLocations(context, program, uniforms.size(), uniforms.data(),
456 locations.data(), base_uniform_index); 452 locations.data(), base_uniform_index);
457 453
458 size_t index = 0; 454 size_t index = 0;
459 if (has_blend_mode()) { 455 if (has_blend_mode()) {
460 backdrop_location_ = locations[index++]; 456 backdrop_location_ = locations[index++];
461 original_backdrop_location_ = locations[index++]; 457 original_backdrop_location_ = locations[index++];
(...skipping 23 matching lines...) Expand all
485 if (uv_texture_mode_ == UV_TEXTURE_MODE_UV) 481 if (uv_texture_mode_ == UV_TEXTURE_MODE_UV)
486 uv_texture_location_ = locations[index++]; 482 uv_texture_location_ = locations[index++];
487 if (uv_texture_mode_ == UV_TEXTURE_MODE_U_V) { 483 if (uv_texture_mode_ == UV_TEXTURE_MODE_U_V) {
488 u_texture_location_ = locations[index++]; 484 u_texture_location_ = locations[index++];
489 v_texture_location_ = locations[index++]; 485 v_texture_location_ = locations[index++];
490 } 486 }
491 if (yuv_alpha_texture_mode_ == YUV_HAS_ALPHA_TEXTURE) 487 if (yuv_alpha_texture_mode_ == YUV_HAS_ALPHA_TEXTURE)
492 a_texture_location_ = locations[index++]; 488 a_texture_location_ = locations[index++];
493 ya_clamp_rect_location_ = locations[index++]; 489 ya_clamp_rect_location_ = locations[index++];
494 uv_clamp_rect_location_ = locations[index++]; 490 uv_clamp_rect_location_ = locations[index++];
495 if (color_conversion_mode_ == COLOR_CONVERSION_MODE_LUT_FROM_YUV) { 491 yuv_and_resource_matrix_location_ = locations[index++];
496 lut_texture_location_ = locations[index++];
497 lut_size_location_ = locations[index++];
498 resource_multiplier_location_ = locations[index++];
499 resource_offset_location_ = locations[index++];
500 } else {
501 yuv_matrix_location_ = locations[index++];
502 yuv_adj_location_ = locations[index++];
503 }
504 break; 492 break;
505 case INPUT_COLOR_SOURCE_UNIFORM: 493 case INPUT_COLOR_SOURCE_UNIFORM:
506 color_location_ = locations[index++]; 494 color_location_ = locations[index++];
507 break; 495 break;
508 } 496 }
497 if (color_conversion_mode_ == COLOR_CONVERSION_MODE_LUT) {
498 lut_texture_location_ = locations[index++];
499 lut_size_location_ = locations[index++];
500 }
509 DCHECK_EQ(index, locations.size()); 501 DCHECK_EQ(index, locations.size());
510 } 502 }
511 503
512 std::string FragmentShader::SetBlendModeFunctions( 504 std::string FragmentShader::SetBlendModeFunctions(
513 const std::string& shader_string) const { 505 const std::string& shader_string) const {
514 if (shader_string.find("ApplyBlendMode") == std::string::npos) 506 if (shader_string.find("ApplyBlendMode") == std::string::npos)
515 return shader_string; 507 return shader_string;
516 508
517 if (!has_blend_mode()) { 509 if (!has_blend_mode()) {
518 return "#define ApplyBlendMode(X, Y) (X)\n" + shader_string; 510 return "#define ApplyBlendMode(X, Y) (X)\n" + shader_string;
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
842 break; 834 break;
843 case INPUT_COLOR_SOURCE_YUV_TEXTURES: 835 case INPUT_COLOR_SOURCE_YUV_TEXTURES:
844 // Compute the clamped texture coordinates for the YA and UV textures. 836 // Compute the clamped texture coordinates for the YA and UV textures.
845 HDR("uniform SamplerType y_texture;"); 837 HDR("uniform SamplerType y_texture;");
846 SRC("// YUV texture lookup and conversion to RGB."); 838 SRC("// YUV texture lookup and conversion to RGB.");
847 SRC("vec2 ya_clamped ="); 839 SRC("vec2 ya_clamped =");
848 SRC(" max(ya_clamp_rect.xy, min(ya_clamp_rect.zw, v_yaTexCoord));"); 840 SRC(" max(ya_clamp_rect.xy, min(ya_clamp_rect.zw, v_yaTexCoord));");
849 SRC("vec2 uv_clamped ="); 841 SRC("vec2 uv_clamped =");
850 SRC(" max(uv_clamp_rect.xy, min(uv_clamp_rect.zw, v_uvTexCoord));"); 842 SRC(" max(uv_clamp_rect.xy, min(uv_clamp_rect.zw, v_uvTexCoord));");
851 // Read the Y and UV or U and V textures into |yuv|. 843 // Read the Y and UV or U and V textures into |yuv|.
852 SRC("vec3 yuv;"); 844 SRC("vec4 texColor;");
853 SRC("yuv.x = TextureLookup(y_texture, ya_clamped).x;"); 845 SRC("texColor.w = 1.0;");
846 SRC("texColor.x = TextureLookup(y_texture, ya_clamped).x;");
854 if (uv_texture_mode_ == UV_TEXTURE_MODE_UV) { 847 if (uv_texture_mode_ == UV_TEXTURE_MODE_UV) {
855 HDR("uniform SamplerType uv_texture;"); 848 HDR("uniform SamplerType uv_texture;");
856 SRC("yuv.yz = TextureLookup(uv_texture, uv_clamped).xy;"); 849 SRC("texColor.yz = TextureLookup(uv_texture, uv_clamped).xy;");
857 } 850 }
858 if (uv_texture_mode_ == UV_TEXTURE_MODE_U_V) { 851 if (uv_texture_mode_ == UV_TEXTURE_MODE_U_V) {
859 HDR("uniform SamplerType u_texture;"); 852 HDR("uniform SamplerType u_texture;");
860 HDR("uniform SamplerType v_texture;"); 853 HDR("uniform SamplerType v_texture;");
861 SRC("yuv.y = TextureLookup(u_texture, uv_clamped).x;"); 854 SRC("texColor.y = TextureLookup(u_texture, uv_clamped).x;");
862 SRC("yuv.z = TextureLookup(v_texture, uv_clamped).x;"); 855 SRC("texColor.z = TextureLookup(v_texture, uv_clamped).x;");
863 } 856 }
864 if (yuv_alpha_texture_mode_ == YUV_HAS_ALPHA_TEXTURE) 857 if (yuv_alpha_texture_mode_ == YUV_HAS_ALPHA_TEXTURE)
865 HDR("uniform SamplerType a_texture;"); 858 HDR("uniform SamplerType a_texture;");
866 HDR("uniform vec4 ya_clamp_rect;"); 859 HDR("uniform vec4 ya_clamp_rect;");
867 HDR("uniform vec4 uv_clamp_rect;"); 860 HDR("uniform vec4 uv_clamp_rect;");
868 // Convert YUV to RGB. 861 HDR("uniform mat4 yuv_and_resource_matrix;");
869 if (color_conversion_mode_ == COLOR_CONVERSION_MODE_LUT_FROM_YUV) {
870 HDR("uniform sampler2D lut_texture;");
871 HDR("uniform float lut_size;");
872 HDR("uniform float resource_multiplier;");
873 HDR("uniform float resource_offset;");
874 HDR("vec4 LUT(sampler2D sampler, vec3 pos, float size) {");
875 HDR(" pos *= size - 1.0;");
876 HDR(" // Select layer");
877 HDR(" float layer = min(floor(pos.z), size - 2.0);");
878 HDR(" // Compress the xy coordinates so they stay within");
879 HDR(" // [0.5 .. 31.5] / N (assuming a LUT size of 17^3)");
880 HDR(" pos.xy = (pos.xy + vec2(0.5)) / size;");
881 HDR(" pos.y = (pos.y + layer) / size;");
882 HDR(" return mix(texture2D(sampler, pos.xy),");
883 HDR(" texture2D(sampler, pos.xy + vec2(0, 1.0 / size)),");
884 HDR(" pos.z - layer);");
885 HDR("}");
886 HDR("vec3 yuv2rgb(vec3 yuv) {");
887 HDR(" yuv = (yuv - vec3(resource_offset)) * resource_multiplier;");
888 HDR(" return LUT(lut_texture, yuv, lut_size).xyz;");
889 HDR("}");
890 } else {
891 HDR("uniform mat3 yuv_matrix;");
892 HDR("uniform vec3 yuv_adj;");
893 HDR("vec3 yuv2rgb(vec3 yuv) {");
894 HDR(" return yuv_matrix * (yuv + yuv_adj);");
895 HDR("}");
896 }
897 HDR("varying TexCoordPrecision vec2 v_yaTexCoord;"); 862 HDR("varying TexCoordPrecision vec2 v_yaTexCoord;");
898 HDR("varying TexCoordPrecision vec2 v_uvTexCoord;"); 863 HDR("varying TexCoordPrecision vec2 v_uvTexCoord;");
899 SRC("vec4 texColor = vec4(yuv2rgb(yuv), 1.0);"); 864 SRC("texColor = yuv_and_resource_matrix * texColor;");
900 break; 865 break;
901 case INPUT_COLOR_SOURCE_UNIFORM: 866 case INPUT_COLOR_SOURCE_UNIFORM:
902 DCHECK(!ignore_sampler_type_); 867 DCHECK(!ignore_sampler_type_);
903 DCHECK(!has_rgba_fragment_tex_transform_); 868 DCHECK(!has_rgba_fragment_tex_transform_);
904 HDR("uniform vec4 color;"); 869 HDR("uniform vec4 color;");
905 SRC("// Uniform color"); 870 SRC("// Uniform color");
906 SRC("vec4 texColor = color;"); 871 SRC("vec4 texColor = color;");
907 break; 872 break;
908 } 873 }
874
875 // Apply LUT based color conversion.
876 if (color_conversion_mode_ == COLOR_CONVERSION_MODE_LUT) {
877 HDR("uniform sampler2D lut_texture;");
878 HDR("uniform float lut_size;");
879 HDR("vec4 LUT(sampler2D sampler, vec3 pos, float size) {");
880 HDR(" pos *= size - 1.0;");
881 HDR(" // Select layer");
882 HDR(" float layer = min(floor(pos.z), size - 2.0);");
883 HDR(" // Compress the xy coordinates so they stay within");
884 HDR(" // [0.5 .. 31.5] / N (assuming a LUT size of 17^3)");
885 HDR(" pos.xy = (pos.xy + vec2(0.5)) / size;");
886 HDR(" pos.y = (pos.y + layer) / size;");
887 HDR(" return mix(texture2D(sampler, pos.xy),");
888 HDR(" texture2D(sampler, pos.xy + vec2(0, 1.0 / size)),");
889 HDR(" pos.z - layer);");
890 HDR("}");
891 SRC("texColor.xyz = LUT(lut_texture, texColor.xyz, lut_size).xyz;");
892 }
893
909 // Apply the color matrix to texColor. 894 // Apply the color matrix to texColor.
910 if (has_color_matrix_) { 895 if (has_color_matrix_) {
911 HDR("uniform mat4 colorMatrix;"); 896 HDR("uniform mat4 colorMatrix;");
912 HDR("uniform vec4 colorOffset;"); 897 HDR("uniform vec4 colorOffset;");
913 SRC("// Apply color matrix"); 898 SRC("// Apply color matrix");
914 SRC("float nonZeroAlpha = max(texColor.a, 0.00001);"); 899 SRC("float nonZeroAlpha = max(texColor.a, 0.00001);");
915 SRC("texColor = vec4(texColor.rgb / nonZeroAlpha, nonZeroAlpha);"); 900 SRC("texColor = vec4(texColor.rgb / nonZeroAlpha, nonZeroAlpha);");
916 SRC("texColor = colorMatrix * texColor + colorOffset;"); 901 SRC("texColor = colorMatrix * texColor + colorOffset;");
917 SRC("texColor.rgb *= texColor.a;"); 902 SRC("texColor.rgb *= texColor.a;");
918 SRC("texColor = clamp(texColor, 0.0, 1.0);"); 903 SRC("texColor = clamp(texColor, 0.0, 1.0);");
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1002 else 987 else
1003 SRC("gl_FragColor = ApplyBlendMode(texColor, 0.0);"); 988 SRC("gl_FragColor = ApplyBlendMode(texColor, 0.0);");
1004 break; 989 break;
1005 } 990 }
1006 source += "}\n"; 991 source += "}\n";
1007 992
1008 return header + source; 993 return header + source;
1009 } 994 }
1010 995
1011 } // namespace cc 996 } // namespace cc
OLDNEW
« no previous file with comments | « cc/output/shader.h ('k') | third_party/WebKit/LayoutTests/TestExpectations » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698