Commit a94e1212 authored by ccameron's avatar ccameron Committed by Commit bot

The great shader refactor: Separate initialization from ctor

This moves initialization of parameters to SetSubclassProperties.
The next step is to clean up initialization to use a common program
descriptor. In that patch, I'll change these functions to
CheckSubclassProperties, so that I can DCHECK that I didn't change
anything.

BUG=667966
CQ_INCLUDE_TRYBOTS=master.tryserver.blink:linux_trusty_blink_rel

Review-Url: https://codereview.chromium.org/2626823002
Cr-Commit-Position: refs/heads/master@{#442761}
parent c9f9c596
...@@ -3658,10 +3658,9 @@ const GLRenderer::VideoYUVProgram* GLRenderer::GetVideoYUVProgram( ...@@ -3658,10 +3658,9 @@ const GLRenderer::VideoYUVProgram* GLRenderer::GetVideoYUVProgram(
[use_color_lut]; [use_color_lut];
if (!program->initialized()) { if (!program->initialized()) {
TRACE_EVENT0("cc", "GLRenderer::videoYUVProgram::initialize"); TRACE_EVENT0("cc", "GLRenderer::videoYUVProgram::initialize");
program->mutable_fragment_shader()->SetFeatures(use_alpha_plane, use_nv12, program->InitializeVideoYUV(output_surface_->context_provider(), precision,
use_color_lut); sampler, use_alpha_plane, use_nv12,
program->Initialize(output_surface_->context_provider(), precision, use_color_lut);
sampler);
} }
return program; return program;
} }
......
...@@ -62,16 +62,26 @@ class ProgramBinding : public ProgramBindingBase { ...@@ -62,16 +62,26 @@ class ProgramBinding : public ProgramBindingBase {
void Initialize(ContextProvider* context_provider, void Initialize(ContextProvider* context_provider,
TexCoordPrecision precision, TexCoordPrecision precision,
SamplerType sampler) { SamplerType sampler) {
return Initialize( Initialize(context_provider, precision, sampler, BLEND_MODE_NONE, false);
context_provider, precision, sampler, BLEND_MODE_NONE, false);
} }
void Initialize(ContextProvider* context_provider, void Initialize(ContextProvider* context_provider,
TexCoordPrecision precision, TexCoordPrecision precision,
SamplerType sampler, SamplerType sampler,
BlendMode blend_mode) { BlendMode blend_mode) {
return Initialize( Initialize(context_provider, precision, sampler, blend_mode, false);
context_provider, precision, sampler, blend_mode, false); }
void InitializeVideoYUV(ContextProvider* context_provider,
TexCoordPrecision precision,
SamplerType sampler,
bool use_alpha_plane,
bool use_nv12,
bool use_color_lut) {
fragment_shader_.use_alpha_texture_ = use_alpha_plane;
fragment_shader_.use_nv12_ = use_nv12;
fragment_shader_.use_color_lut_ = use_color_lut;
Initialize(context_provider, precision, sampler);
} }
void Initialize(ContextProvider* context_provider, void Initialize(ContextProvider* context_provider,
...@@ -79,19 +89,29 @@ class ProgramBinding : public ProgramBindingBase { ...@@ -79,19 +89,29 @@ class ProgramBinding : public ProgramBindingBase {
SamplerType sampler, SamplerType sampler,
BlendMode blend_mode, BlendMode blend_mode,
bool mask_for_background) { bool mask_for_background) {
vertex_shader_.SetSubclassProperties();
fragment_shader_.SetSubclassProperties();
fragment_shader_.blend_mode_ = blend_mode;
fragment_shader_.mask_for_background_ = mask_for_background;
fragment_shader_.tex_coord_precision_ = precision;
fragment_shader_.sampler_type_ = sampler;
InitializeInternal(context_provider);
}
const VertexShader& vertex_shader() const { return vertex_shader_; }
const FragmentShader& fragment_shader() const { return fragment_shader_; }
private:
void InitializeInternal(ContextProvider* context_provider) {
DCHECK(context_provider); DCHECK(context_provider);
DCHECK(!initialized_); DCHECK(!initialized_);
if (IsContextLost(context_provider->ContextGL())) if (IsContextLost(context_provider->ContextGL()))
return; return;
fragment_shader_.set_blend_mode(blend_mode); if (!ProgramBindingBase::Init(context_provider->ContextGL(),
fragment_shader_.set_mask_for_background(mask_for_background); vertex_shader_.GetShaderString(),
fragment_shader_.GetShaderString())) {
if (!ProgramBindingBase::Init(
context_provider->ContextGL(),
vertex_shader_.GetShaderString(),
fragment_shader_.GetShaderString(precision, sampler))) {
DCHECK(IsContextLost(context_provider->ContextGL())); DCHECK(IsContextLost(context_provider->ContextGL()));
return; return;
} }
...@@ -111,11 +131,6 @@ class ProgramBinding : public ProgramBindingBase { ...@@ -111,11 +131,6 @@ class ProgramBinding : public ProgramBindingBase {
initialized_ = true; initialized_ = true;
} }
const VertexShader& vertex_shader() const { return vertex_shader_; }
const FragmentShader& fragment_shader() const { return fragment_shader_; }
FragmentShader* mutable_fragment_shader() { return &fragment_shader_; }
private:
VertexShader vertex_shader_; VertexShader vertex_shader_;
FragmentShader fragment_shader_; FragmentShader fragment_shader_;
......
...@@ -198,7 +198,7 @@ void VertexShaderBase::Init(GLES2Interface* context, ...@@ -198,7 +198,7 @@ void VertexShaderBase::Init(GLES2Interface* context,
uniforms.push_back("matrix"); uniforms.push_back("matrix");
if (has_vertex_opacity_) if (has_vertex_opacity_)
uniforms.push_back("opacity"); uniforms.push_back("opacity");
if (has_aa_) { if (aa_mode_ == USE_AA) {
uniforms.push_back("viewport"); uniforms.push_back("viewport");
uniforms.push_back("edge"); uniforms.push_back("edge");
} }
...@@ -232,7 +232,7 @@ void VertexShaderBase::Init(GLES2Interface* context, ...@@ -232,7 +232,7 @@ void VertexShaderBase::Init(GLES2Interface* context,
matrix_location_ = locations[index++]; matrix_location_ = locations[index++];
if (has_vertex_opacity_) if (has_vertex_opacity_)
vertex_opacity_location_ = locations[index++]; vertex_opacity_location_ = locations[index++];
if (has_aa_) { if (aa_mode_ == USE_AA) {
viewport_location_ = locations[index++]; viewport_location_ = locations[index++];
edge_location_ = locations[index++]; edge_location_ = locations[index++];
} }
...@@ -298,7 +298,7 @@ std::string VertexShaderBase::GetShaderString() const { ...@@ -298,7 +298,7 @@ std::string VertexShaderBase::GetShaderString() const {
} }
// Compute the anti-aliasing edge distances. // Compute the anti-aliasing edge distances.
if (has_aa_) { if (aa_mode_ == USE_AA) {
HDR("uniform TexCoordPrecision vec3 edge[8];"); HDR("uniform TexCoordPrecision vec3 edge[8];");
HDR("uniform vec4 viewport;"); HDR("uniform vec4 viewport;");
HDR("varying TexCoordPrecision vec4 edge_dist[2]; // 8 edge distances."); HDR("varying TexCoordPrecision vec4 edge_dist[2]; // 8 edge distances.");
...@@ -395,14 +395,14 @@ std::string VertexShaderBase::GetShaderString() const { ...@@ -395,14 +395,14 @@ std::string VertexShaderBase::GetShaderString() const {
FragmentShaderBase::FragmentShaderBase() {} FragmentShaderBase::FragmentShaderBase() {}
std::string FragmentShaderBase::GetShaderString(TexCoordPrecision precision, std::string FragmentShaderBase::GetShaderString() const {
SamplerType sampler) const { TexCoordPrecision precision = tex_coord_precision_;
// The AA shader values will use TexCoordPrecision. // The AA shader values will use TexCoordPrecision.
if (has_aa_ && precision == TEX_COORD_PRECISION_NA) if (aa_mode_ == USE_AA && precision == TEX_COORD_PRECISION_NA)
precision = TEX_COORD_PRECISION_MEDIUM; precision = TEX_COORD_PRECISION_MEDIUM;
return SetFragmentTexCoordPrecision( return SetFragmentTexCoordPrecision(
precision, SetFragmentSamplerType( precision, SetFragmentSamplerType(
sampler, SetBlendModeFunctions(GetShaderSource()))); sampler_type_, SetBlendModeFunctions(GetShaderSource())));
} }
void FragmentShaderBase::Init(GLES2Interface* context, void FragmentShaderBase::Init(GLES2Interface* context,
...@@ -415,7 +415,7 @@ void FragmentShaderBase::Init(GLES2Interface* context, ...@@ -415,7 +415,7 @@ void FragmentShaderBase::Init(GLES2Interface* context,
uniforms.push_back("s_originalBackdropTexture"); uniforms.push_back("s_originalBackdropTexture");
uniforms.push_back("backdropRect"); uniforms.push_back("backdropRect");
} }
if (has_mask_sampler_) { if (mask_mode_ != NO_MASK) {
uniforms.push_back("s_mask"); uniforms.push_back("s_mask");
uniforms.push_back("maskTexCoordScale"); uniforms.push_back("maskTexCoordScale");
uniforms.push_back("maskTexCoordOffset"); uniforms.push_back("maskTexCoordOffset");
...@@ -450,7 +450,7 @@ void FragmentShaderBase::Init(GLES2Interface* context, ...@@ -450,7 +450,7 @@ void FragmentShaderBase::Init(GLES2Interface* context,
original_backdrop_location_ = locations[index++]; original_backdrop_location_ = locations[index++];
backdrop_rect_location_ = locations[index++]; backdrop_rect_location_ = locations[index++];
} }
if (has_mask_sampler_) { if (mask_mode_ != NO_MASK) {
mask_sampler_location_ = locations[index++]; mask_sampler_location_ = locations[index++];
mask_tex_coord_scale_location_ = locations[index++]; mask_tex_coord_scale_location_ = locations[index++];
mask_tex_coord_offset_location_ = locations[index++]; mask_tex_coord_offset_location_ = locations[index++];
...@@ -481,9 +481,9 @@ void FragmentShaderBase::FillLocations(ShaderLocations* locations) const { ...@@ -481,9 +481,9 @@ void FragmentShaderBase::FillLocations(ShaderLocations* locations) const {
locations->backdrop = backdrop_location_; locations->backdrop = backdrop_location_;
locations->backdrop_rect = backdrop_rect_location_; locations->backdrop_rect = backdrop_rect_location_;
} }
if (mask_for_background()) if (mask_for_background_)
locations->original_backdrop = original_backdrop_location_; locations->original_backdrop = original_backdrop_location_;
if (has_mask_sampler_) { if (mask_mode_ != NO_MASK) {
locations->mask_sampler = mask_sampler_location_; locations->mask_sampler = mask_sampler_location_;
locations->mask_tex_coord_scale = mask_tex_coord_scale_location_; locations->mask_tex_coord_scale = mask_tex_coord_scale_location_;
locations->mask_tex_coord_offset = mask_tex_coord_offset_location_; locations->mask_tex_coord_offset = mask_tex_coord_offset_location_;
...@@ -519,7 +519,7 @@ std::string FragmentShaderBase::SetBlendModeFunctions( ...@@ -519,7 +519,7 @@ std::string FragmentShaderBase::SetBlendModeFunctions(
}); });
std::string mixFunction; std::string mixFunction;
if (mask_for_background()) { if (mask_for_background_) {
mixFunction = SHADER0([]() { mixFunction = SHADER0([]() {
vec4 MixBackdrop(TexCoordPrecision vec2 bgTexCoord, float mask) { vec4 MixBackdrop(TexCoordPrecision vec2 bgTexCoord, float mask) {
vec4 backdrop = texture2D(s_backdropTexture, bgTexCoord); vec4 backdrop = texture2D(s_backdropTexture, bgTexCoord);
...@@ -847,7 +847,7 @@ std::string FragmentShaderBase::GetShaderSource() const { ...@@ -847,7 +847,7 @@ std::string FragmentShaderBase::GetShaderSource() const {
} }
// Read the mask texture. // Read the mask texture.
if (has_mask_sampler_) { if (mask_mode_ != NO_MASK) {
HDR("uniform SamplerType s_mask;"); HDR("uniform SamplerType s_mask;");
HDR("uniform vec2 maskTexCoordScale;"); HDR("uniform vec2 maskTexCoordScale;");
HDR("uniform vec2 maskTexCoordOffset;"); HDR("uniform vec2 maskTexCoordOffset;");
...@@ -859,7 +859,7 @@ std::string FragmentShaderBase::GetShaderSource() const { ...@@ -859,7 +859,7 @@ std::string FragmentShaderBase::GetShaderSource() const {
} }
// Compute AA. // Compute AA.
if (has_aa_) { if (aa_mode_ == USE_AA) {
HDR("varying TexCoordPrecision vec4 edge_dist[2]; // 8 edge distances."); HDR("varying TexCoordPrecision vec4 edge_dist[2]; // 8 edge distances.");
SRC("// Compute AA"); SRC("// Compute AA");
SRC("vec4 d4 = min(edge_dist[0], edge_dist[1]);"); SRC("vec4 d4 = min(edge_dist[0], edge_dist[1]);");
...@@ -868,7 +868,7 @@ std::string FragmentShaderBase::GetShaderSource() const { ...@@ -868,7 +868,7 @@ std::string FragmentShaderBase::GetShaderSource() const {
} }
// Premultiply by alpha. // Premultiply by alpha.
if (has_premultiply_alpha_) { if (premultiply_alpha_mode_ == NON_PREMULTIPLIED_ALPHA) {
SRC("// Premultiply alpha"); SRC("// Premultiply alpha");
SRC("texColor.rgb *= texColor.a;"); SRC("texColor.rgb *= texColor.a;");
} }
...@@ -881,7 +881,7 @@ std::string FragmentShaderBase::GetShaderSource() const { ...@@ -881,7 +881,7 @@ std::string FragmentShaderBase::GetShaderSource() const {
} }
// Apply swizzle. // Apply swizzle.
if (has_swizzle_) { if (swizzle_mode_ == DO_SWIZZLE) {
SRC("// Apply swizzle"); SRC("// Apply swizzle");
SRC("texColor = texColor.bgra;\n"); SRC("texColor = texColor.bgra;\n");
} }
...@@ -895,17 +895,17 @@ std::string FragmentShaderBase::GetShaderSource() const { ...@@ -895,17 +895,17 @@ std::string FragmentShaderBase::GetShaderSource() const {
} }
// Apply uniform alpha, aa, varying alpha, and the mask. // Apply uniform alpha, aa, varying alpha, and the mask.
if (has_varying_alpha_ || has_aa_ || has_uniform_alpha_ || if (has_varying_alpha_ || aa_mode_ == USE_AA || has_uniform_alpha_ ||
has_mask_sampler_) { mask_mode_ != NO_MASK) {
SRC("// Apply alpha from uniform, varying, aa, and mask."); SRC("// Apply alpha from uniform, varying, aa, and mask.");
std::string line = " texColor = texColor"; std::string line = " texColor = texColor";
if (has_varying_alpha_) if (has_varying_alpha_)
line += " * v_alpha"; line += " * v_alpha";
if (has_uniform_alpha_) if (has_uniform_alpha_)
line += " * alpha"; line += " * alpha";
if (has_aa_) if (aa_mode_ == USE_AA)
line += " * aa"; line += " * aa";
if (has_mask_sampler_) if (mask_mode_ != NO_MASK)
line += " * maskColor.a"; line += " * maskColor.a";
line += ";\n"; line += ";\n";
source += line; source += line;
...@@ -923,7 +923,7 @@ std::string FragmentShaderBase::GetShaderSource() const { ...@@ -923,7 +923,7 @@ std::string FragmentShaderBase::GetShaderSource() const {
SRC("gl_FragColor = vec4(texColor.rgb, 1.0);"); SRC("gl_FragColor = vec4(texColor.rgb, 1.0);");
break; break;
case FRAG_COLOR_MODE_APPLY_BLEND_MODE: case FRAG_COLOR_MODE_APPLY_BLEND_MODE:
if (has_mask_sampler_) if (mask_mode_ != NO_MASK)
SRC("gl_FragColor = ApplyBlendMode(texColor, maskColor.w);"); SRC("gl_FragColor = ApplyBlendMode(texColor, maskColor.w);");
else else
SRC("gl_FragColor = ApplyBlendMode(texColor, 0.0);"); SRC("gl_FragColor = ApplyBlendMode(texColor, 0.0);");
...@@ -936,14 +936,6 @@ std::string FragmentShaderBase::GetShaderSource() const { ...@@ -936,14 +936,6 @@ std::string FragmentShaderBase::GetShaderSource() const {
FragmentShaderYUVVideo::FragmentShaderYUVVideo() {} FragmentShaderYUVVideo::FragmentShaderYUVVideo() {}
void FragmentShaderYUVVideo::SetFeatures(bool use_alpha_texture,
bool use_nv12,
bool use_color_lut) {
use_alpha_texture_ = use_alpha_texture;
use_nv12_ = use_nv12;
use_color_lut_ = use_color_lut;
}
void FragmentShaderYUVVideo::Init(GLES2Interface* context, void FragmentShaderYUVVideo::Init(GLES2Interface* context,
unsigned program, unsigned program,
int* base_uniform_index) { int* base_uniform_index) {
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment