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

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

Issue 2697253002: color: Add support for shader generation (Closed)
Patch Set: More bits of precision 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') | ui/gfx/color_transform.h » ('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>
11 11
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "cc/output/static_geometry_binding.h" 14 #include "cc/output/static_geometry_binding.h"
15 #include "gpu/command_buffer/client/gles2_interface.h" 15 #include "gpu/command_buffer/client/gles2_interface.h"
16 #include "ui/gfx/color_transform.h"
16 #include "ui/gfx/geometry/point.h" 17 #include "ui/gfx/geometry/point.h"
17 #include "ui/gfx/geometry/size.h" 18 #include "ui/gfx/geometry/size.h"
18 19
19 template <size_t size> 20 template <size_t size>
20 std::string StripLambda(const char(&shader)[size]) { 21 std::string StripLambda(const char(&shader)[size]) {
21 // Must contain at least "[]() {}" and trailing null (included in size). 22 // Must contain at least "[]() {}" and trailing null (included in size).
22 static_assert(size >= 8, 23 static_assert(size >= 8,
23 "String passed to StripLambda must be at least 8 characters"); 24 "String passed to StripLambda must be at least 8 characters");
24 DCHECK_EQ(strncmp("[]() {", shader, 6), 0); 25 DCHECK_EQ(strncmp("[]() {", shader, 6), 0);
25 DCHECK_EQ(shader[size - 2], '}'); 26 DCHECK_EQ(shader[size - 2], '}');
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 if (uv_texture_mode_ == UV_TEXTURE_MODE_UV) 433 if (uv_texture_mode_ == UV_TEXTURE_MODE_UV)
433 uniforms.push_back("uv_texture"); 434 uniforms.push_back("uv_texture");
434 if (uv_texture_mode_ == UV_TEXTURE_MODE_U_V) { 435 if (uv_texture_mode_ == UV_TEXTURE_MODE_U_V) {
435 uniforms.push_back("u_texture"); 436 uniforms.push_back("u_texture");
436 uniforms.push_back("v_texture"); 437 uniforms.push_back("v_texture");
437 } 438 }
438 if (yuv_alpha_texture_mode_ == YUV_HAS_ALPHA_TEXTURE) 439 if (yuv_alpha_texture_mode_ == YUV_HAS_ALPHA_TEXTURE)
439 uniforms.push_back("a_texture"); 440 uniforms.push_back("a_texture");
440 uniforms.push_back("ya_clamp_rect"); 441 uniforms.push_back("ya_clamp_rect");
441 uniforms.push_back("uv_clamp_rect"); 442 uniforms.push_back("uv_clamp_rect");
442 uniforms.push_back("yuv_and_resource_matrix"); 443 uniforms.push_back("resource_multiplier");
444 uniforms.push_back("resource_offset");
443 break; 445 break;
444 case INPUT_COLOR_SOURCE_UNIFORM: 446 case INPUT_COLOR_SOURCE_UNIFORM:
445 uniforms.push_back("color"); 447 uniforms.push_back("color");
446 break; 448 break;
447 } 449 }
448 if (color_conversion_mode_ == COLOR_CONVERSION_MODE_LUT) { 450 if (color_conversion_mode_ == COLOR_CONVERSION_MODE_LUT) {
449 uniforms.push_back("lut_texture"); 451 uniforms.push_back("lut_texture");
450 uniforms.push_back("lut_size"); 452 uniforms.push_back("lut_size");
451 } 453 }
452 454
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 if (uv_texture_mode_ == UV_TEXTURE_MODE_UV) 487 if (uv_texture_mode_ == UV_TEXTURE_MODE_UV)
486 uv_texture_location_ = locations[index++]; 488 uv_texture_location_ = locations[index++];
487 if (uv_texture_mode_ == UV_TEXTURE_MODE_U_V) { 489 if (uv_texture_mode_ == UV_TEXTURE_MODE_U_V) {
488 u_texture_location_ = locations[index++]; 490 u_texture_location_ = locations[index++];
489 v_texture_location_ = locations[index++]; 491 v_texture_location_ = locations[index++];
490 } 492 }
491 if (yuv_alpha_texture_mode_ == YUV_HAS_ALPHA_TEXTURE) 493 if (yuv_alpha_texture_mode_ == YUV_HAS_ALPHA_TEXTURE)
492 a_texture_location_ = locations[index++]; 494 a_texture_location_ = locations[index++];
493 ya_clamp_rect_location_ = locations[index++]; 495 ya_clamp_rect_location_ = locations[index++];
494 uv_clamp_rect_location_ = locations[index++]; 496 uv_clamp_rect_location_ = locations[index++];
495 yuv_and_resource_matrix_location_ = locations[index++]; 497 resource_multiplier_location_ = locations[index++];
498 resource_offset_location_ = locations[index++];
496 break; 499 break;
497 case INPUT_COLOR_SOURCE_UNIFORM: 500 case INPUT_COLOR_SOURCE_UNIFORM:
498 color_location_ = locations[index++]; 501 color_location_ = locations[index++];
499 break; 502 break;
500 } 503 }
501 if (color_conversion_mode_ == COLOR_CONVERSION_MODE_LUT) { 504 if (color_conversion_mode_ == COLOR_CONVERSION_MODE_LUT) {
502 lut_texture_location_ = locations[index++]; 505 lut_texture_location_ = locations[index++];
503 lut_size_location_ = locations[index++]; 506 lut_size_location_ = locations[index++];
504 } 507 }
505 DCHECK_EQ(index, locations.size()); 508 DCHECK_EQ(index, locations.size());
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
855 if (uv_texture_mode_ == UV_TEXTURE_MODE_U_V) { 858 if (uv_texture_mode_ == UV_TEXTURE_MODE_U_V) {
856 HDR("uniform SamplerType u_texture;"); 859 HDR("uniform SamplerType u_texture;");
857 HDR("uniform SamplerType v_texture;"); 860 HDR("uniform SamplerType v_texture;");
858 SRC("texColor.y = TextureLookup(u_texture, uv_clamped).x;"); 861 SRC("texColor.y = TextureLookup(u_texture, uv_clamped).x;");
859 SRC("texColor.z = TextureLookup(v_texture, uv_clamped).x;"); 862 SRC("texColor.z = TextureLookup(v_texture, uv_clamped).x;");
860 } 863 }
861 if (yuv_alpha_texture_mode_ == YUV_HAS_ALPHA_TEXTURE) 864 if (yuv_alpha_texture_mode_ == YUV_HAS_ALPHA_TEXTURE)
862 HDR("uniform SamplerType a_texture;"); 865 HDR("uniform SamplerType a_texture;");
863 HDR("uniform vec4 ya_clamp_rect;"); 866 HDR("uniform vec4 ya_clamp_rect;");
864 HDR("uniform vec4 uv_clamp_rect;"); 867 HDR("uniform vec4 uv_clamp_rect;");
865 HDR("uniform mat4 yuv_and_resource_matrix;"); 868 HDR("uniform float resource_multiplier;");
869 HDR("uniform float resource_offset;");
866 HDR("varying TexCoordPrecision vec2 v_yaTexCoord;"); 870 HDR("varying TexCoordPrecision vec2 v_yaTexCoord;");
867 HDR("varying TexCoordPrecision vec2 v_uvTexCoord;"); 871 HDR("varying TexCoordPrecision vec2 v_uvTexCoord;");
868 SRC("texColor = yuv_and_resource_matrix * texColor;"); 872 SRC("texColor.xyz -= vec3(resource_offset);");
873 SRC("texColor.xyz *= resource_multiplier;");
869 break; 874 break;
870 case INPUT_COLOR_SOURCE_UNIFORM: 875 case INPUT_COLOR_SOURCE_UNIFORM:
871 DCHECK(!ignore_sampler_type_); 876 DCHECK(!ignore_sampler_type_);
872 DCHECK(!has_rgba_fragment_tex_transform_); 877 DCHECK(!has_rgba_fragment_tex_transform_);
873 HDR("uniform vec4 color;"); 878 HDR("uniform vec4 color;");
874 SRC("// Uniform color"); 879 SRC("// Uniform color");
875 SRC("vec4 texColor = color;"); 880 SRC("vec4 texColor = color;");
876 break; 881 break;
877 } 882 }
878 883
879 // Apply LUT based color conversion. 884 // Apply LUT based color conversion.
880 if (color_conversion_mode_ == COLOR_CONVERSION_MODE_LUT) { 885 switch (color_conversion_mode_) {
881 HDR("uniform sampler2D lut_texture;"); 886 case COLOR_CONVERSION_MODE_LUT:
882 HDR("uniform float lut_size;"); 887 HDR("uniform sampler2D lut_texture;");
883 HDR("vec4 LUT(sampler2D sampler, vec3 pos, float size) {"); 888 HDR("uniform float lut_size;");
884 HDR(" pos *= size - 1.0;"); 889 HDR("vec4 LUT(sampler2D sampler, vec3 pos, float size) {");
885 HDR(" // Select layer"); 890 HDR(" pos *= size - 1.0;");
886 HDR(" float layer = min(floor(pos.z), size - 2.0);"); 891 HDR(" // Select layer");
887 HDR(" // Compress the xy coordinates so they stay within"); 892 HDR(" float layer = min(floor(pos.z), size - 2.0);");
888 HDR(" // [0.5 .. 31.5] / N (assuming a LUT size of 17^3)"); 893 HDR(" // Compress the xy coordinates so they stay within");
889 HDR(" pos.xy = (pos.xy + vec2(0.5)) / size;"); 894 HDR(" // [0.5 .. 31.5] / N (assuming a LUT size of 17^3)");
890 HDR(" pos.y = (pos.y + layer) / size;"); 895 HDR(" pos.xy = (pos.xy + vec2(0.5)) / size;");
891 HDR(" return mix(LutLookup(sampler, pos.xy),"); 896 HDR(" pos.y = (pos.y + layer) / size;");
892 HDR(" LutLookup(sampler, pos.xy + vec2(0, 1.0 / size)),"); 897 HDR(" return mix(LutLookup(sampler, pos.xy),");
893 HDR(" pos.z - layer);"); 898 HDR(" LutLookup(sampler, pos.xy + vec2(0, 1.0 / size)),");
894 HDR("}"); 899 HDR(" pos.z - layer);");
895 SRC("texColor.xyz = LUT(lut_texture, texColor.xyz, lut_size).xyz;"); 900 HDR("}");
901 SRC("texColor.rgb = LUT(lut_texture, texColor.xyz, lut_size).xyz;");
902 break;
903 case COLOR_CONVERSION_MODE_SHADER:
904 header += color_transform_->GetShaderSource();
905 SRC("texColor.rgb = DoColorConversion(texColor.xyz);");
906 break;
907 case COLOR_CONVERSION_MODE_NONE:
908 break;
896 } 909 }
897 910
898 // Apply the color matrix to texColor. 911 // Apply the color matrix to texColor.
899 if (has_color_matrix_) { 912 if (has_color_matrix_) {
900 HDR("uniform mat4 colorMatrix;"); 913 HDR("uniform mat4 colorMatrix;");
901 HDR("uniform vec4 colorOffset;"); 914 HDR("uniform vec4 colorOffset;");
902 SRC("// Apply color matrix"); 915 SRC("// Apply color matrix");
903 SRC("float nonZeroAlpha = max(texColor.a, 0.00001);"); 916 SRC("float nonZeroAlpha = max(texColor.a, 0.00001);");
904 SRC("texColor = vec4(texColor.rgb / nonZeroAlpha, nonZeroAlpha);"); 917 SRC("texColor = vec4(texColor.rgb / nonZeroAlpha, nonZeroAlpha);");
905 SRC("texColor = colorMatrix * texColor + colorOffset;"); 918 SRC("texColor = colorMatrix * texColor + colorOffset;");
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
991 else 1004 else
992 SRC("gl_FragColor = ApplyBlendMode(texColor, 0.0);"); 1005 SRC("gl_FragColor = ApplyBlendMode(texColor, 0.0);");
993 break; 1006 break;
994 } 1007 }
995 source += "}\n"; 1008 source += "}\n";
996 1009
997 return header + source; 1010 return header + source;
998 } 1011 }
999 1012
1000 } // namespace cc 1013 } // namespace cc
OLDNEW
« no previous file with comments | « cc/output/shader.h ('k') | ui/gfx/color_transform.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698