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

Side by Side Diff: cc/output/program_binding.h

Issue 2628713002: The great shader refactor: Use ProgramKey to initialize shaders (Closed)
Patch Set: 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/gl_renderer.cc ('k') | cc/output/program_binding.cc » ('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 #ifndef CC_OUTPUT_PROGRAM_BINDING_H_ 5 #ifndef CC_OUTPUT_PROGRAM_BINDING_H_
6 #define CC_OUTPUT_PROGRAM_BINDING_H_ 6 #define CC_OUTPUT_PROGRAM_BINDING_H_
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 47
48 unsigned program_; 48 unsigned program_;
49 unsigned vertex_shader_id_; 49 unsigned vertex_shader_id_;
50 unsigned fragment_shader_id_; 50 unsigned fragment_shader_id_;
51 bool initialized_; 51 bool initialized_;
52 52
53 private: 53 private:
54 DISALLOW_COPY_AND_ASSIGN(ProgramBindingBase); 54 DISALLOW_COPY_AND_ASSIGN(ProgramBindingBase);
55 }; 55 };
56 56
57 enum ProgramType {
58 PROGRAM_TYPE_DEBUG_BORDER,
59 PROGRAM_TYPE_SOLID_COLOR,
60 PROGRAM_TYPE_TILE,
61 PROGRAM_TYPE_TEXTURE,
62 PROGRAM_TYPE_RENDER_PASS,
63 PROGRAM_TYPE_VIDEO_STREAM,
64 };
65
66 class CC_EXPORT ProgramKey {
67 public:
68 ProgramKey(const ProgramKey& other);
69 ~ProgramKey();
70
71 static ProgramKey DebugBorder();
72 static ProgramKey SolidColor(AAMode aa_mode);
73 static ProgramKey Tile(TexCoordPrecision precision,
74 SamplerType sampler,
75 AAMode aa_mode,
76 SwizzleMode swizzle_mode,
77 bool is_opaque);
78 static ProgramKey Texture(TexCoordPrecision precision,
79 SamplerType sampler,
80 PremultipliedAlphaMode premultiplied_alpha,
81 bool has_background_color);
82
83 // TODO(ccameron): Merge |mask_for_background| into MaskMode.
84 static ProgramKey RenderPass(TexCoordPrecision precision,
85 SamplerType sampler,
86 BlendMode blend_mode,
87 AAMode aa_mode,
88 MaskMode mask_mode,
89 bool mask_for_background,
90 bool has_color_matrix);
91 static ProgramKey VideoStream(TexCoordPrecision precision);
92
93 bool operator==(const ProgramKey& other) const;
94
95 private:
96 ProgramKey();
97 friend struct ProgramKeyHash;
98 template <class VertexShader, class FragmentShader>
99 friend class ProgramBinding;
100
101 ProgramType type_ = PROGRAM_TYPE_DEBUG_BORDER;
102 TexCoordPrecision precision_ = TEX_COORD_PRECISION_NA;
103 SamplerType sampler_ = SAMPLER_TYPE_NA;
104 BlendMode blend_mode_ = BLEND_MODE_NONE;
105 AAMode aa_mode_ = NO_AA;
106 SwizzleMode swizzle_mode_ = NO_SWIZZLE;
107 bool is_opaque_ = false;
108
109 PremultipliedAlphaMode premultiplied_alpha_ = PREMULTIPLIED_ALPHA;
110 bool has_background_color_ = false;
111
112 MaskMode mask_mode_ = NO_MASK;
113 bool mask_for_background_ = false;
114 bool has_color_matrix_ = false;
115 };
116
57 template <class VertexShader, class FragmentShader> 117 template <class VertexShader, class FragmentShader>
58 class ProgramBinding : public ProgramBindingBase { 118 class ProgramBinding : public ProgramBindingBase {
59 public: 119 public:
60 ProgramBinding() {} 120 ProgramBinding() {}
61 121
62 void Initialize(ContextProvider* context_provider, 122 void Initialize(ContextProvider* context_provider, const ProgramKey& key) {
63 TexCoordPrecision precision, 123 // Set parameters that are common to all sub-classes.
64 SamplerType sampler) { 124 vertex_shader_.aa_mode_ = key.aa_mode_;
65 Initialize(context_provider, precision, sampler, BLEND_MODE_NONE, false); 125 fragment_shader_.aa_mode_ = key.aa_mode_;
66 } 126 fragment_shader_.blend_mode_ = key.blend_mode_;
127 fragment_shader_.tex_coord_precision_ = key.precision_;
128 fragment_shader_.sampler_type_ = key.sampler_;
129 fragment_shader_.swizzle_mode_ = key.swizzle_mode_;
130 fragment_shader_.premultiply_alpha_mode_ = key.premultiplied_alpha_;
131 fragment_shader_.mask_mode_ = key.mask_mode_;
132 fragment_shader_.mask_for_background_ = key.mask_for_background_;
67 133
68 void Initialize(ContextProvider* context_provider, 134 switch (key.type_) {
69 TexCoordPrecision precision, 135 case PROGRAM_TYPE_DEBUG_BORDER:
70 SamplerType sampler, 136 InitializeDebugBorderProgram();
71 BlendMode blend_mode) { 137 break;
72 Initialize(context_provider, precision, sampler, blend_mode, false); 138 case PROGRAM_TYPE_SOLID_COLOR:
139 InitializeSolidColorProgram(key);
140 break;
141 case PROGRAM_TYPE_TILE:
142 InitializeTileProgram(key);
143 break;
144 case PROGRAM_TYPE_TEXTURE:
145 InitializeTextureProgram(key);
146 break;
147 case PROGRAM_TYPE_RENDER_PASS:
148 InitializeRenderPassProgram(key);
149 break;
150 case PROGRAM_TYPE_VIDEO_STREAM:
151 InitializeVideoStreamProgram(key);
152 break;
153 }
154 fragment_shader_.CheckSubclassProperties();
155 vertex_shader_.CheckSubclassProperties();
156 InitializeInternal(context_provider);
73 } 157 }
74 158
75 void InitializeVideoYUV(ContextProvider* context_provider, 159 void InitializeVideoYUV(ContextProvider* context_provider,
76 TexCoordPrecision precision, 160 TexCoordPrecision precision,
77 SamplerType sampler, 161 SamplerType sampler,
78 bool use_alpha_plane, 162 bool use_alpha_plane,
79 bool use_nv12, 163 bool use_nv12,
80 bool use_color_lut) { 164 bool use_color_lut) {
165 vertex_shader_.tex_coord_source_ = TEX_COORD_SOURCE_ATTRIBUTE;
166 vertex_shader_.has_matrix_ = true;
167 vertex_shader_.is_ya_uv_ = true;
168
81 fragment_shader_.use_alpha_texture_ = use_alpha_plane; 169 fragment_shader_.use_alpha_texture_ = use_alpha_plane;
82 fragment_shader_.use_nv12_ = use_nv12; 170 fragment_shader_.use_nv12_ = use_nv12;
83 fragment_shader_.use_color_lut_ = use_color_lut; 171 fragment_shader_.use_color_lut_ = use_color_lut;
84 Initialize(context_provider, precision, sampler);
85 }
86
87 void Initialize(ContextProvider* context_provider,
88 TexCoordPrecision precision,
89 SamplerType sampler,
90 BlendMode blend_mode,
91 bool mask_for_background) {
92 vertex_shader_.SetSubclassProperties();
93 fragment_shader_.SetSubclassProperties();
94 fragment_shader_.blend_mode_ = blend_mode;
95 fragment_shader_.mask_for_background_ = mask_for_background;
96 fragment_shader_.tex_coord_precision_ = precision; 172 fragment_shader_.tex_coord_precision_ = precision;
97 fragment_shader_.sampler_type_ = sampler; 173 fragment_shader_.sampler_type_ = sampler;
174
175 fragment_shader_.CheckSubclassProperties();
176 vertex_shader_.CheckSubclassProperties();
98 InitializeInternal(context_provider); 177 InitializeInternal(context_provider);
99 } 178 }
100 179
101 const VertexShader& vertex_shader() const { return vertex_shader_; } 180 const VertexShader& vertex_shader() const { return vertex_shader_; }
102 const FragmentShader& fragment_shader() const { return fragment_shader_; } 181 const FragmentShader& fragment_shader() const { return fragment_shader_; }
103 182
104 private: 183 private:
184 void InitializeDebugBorderProgram() {
185 // Initialize vertex program.
186 vertex_shader_.has_matrix_ = true;
187
188 // Initialize fragment program.
189 fragment_shader_.input_color_type_ = INPUT_COLOR_SOURCE_UNIFORM;
190 fragment_shader_.frag_color_mode_ = FRAG_COLOR_MODE_DEFAULT;
191 }
192
193 void InitializeSolidColorProgram(const ProgramKey& key) {
194 // Initialize vertex program.
195 vertex_shader_.position_source_ = POSITION_SOURCE_ATTRIBUTE_INDEXED_UNIFORM;
196 vertex_shader_.has_matrix_ = true;
197 #if defined(OS_ANDROID)
198 if (key.aa_mode_ == NO_AA)
199 vertex_shader_.has_dummy_variables_ = true;
200 #endif
201
202 // Initialize fragment program.
203 fragment_shader_.input_color_type_ = INPUT_COLOR_SOURCE_UNIFORM;
204 fragment_shader_.frag_color_mode_ = FRAG_COLOR_MODE_DEFAULT;
205 }
206
207 void InitializeTileProgram(const ProgramKey& key) {
208 // Initialize vertex program.
209 vertex_shader_.position_source_ = POSITION_SOURCE_ATTRIBUTE_INDEXED_UNIFORM;
210 vertex_shader_.tex_coord_transform_ = TEX_COORD_TRANSFORM_VEC4;
211 vertex_shader_.tex_coord_source_ = TEX_COORD_SOURCE_ATTRIBUTE;
212 vertex_shader_.has_matrix_ = true;
213
214 // Initialize fragment program.
215 if (key.is_opaque_) {
216 DCHECK_EQ(key.aa_mode_, NO_AA);
217 fragment_shader_.frag_color_mode_ = FRAG_COLOR_MODE_OPAQUE;
218 } else {
219 // TODO(ccameron): This branch shouldn't be needed (this is always
220 // BLEND_MODE_NONE).
221 if (key.aa_mode_ == NO_AA && key.swizzle_mode_ == NO_SWIZZLE)
222 fragment_shader_.frag_color_mode_ = FRAG_COLOR_MODE_APPLY_BLEND_MODE;
223 fragment_shader_.has_uniform_alpha_ = true;
224 }
225
226 // AA changes the texture coordinate mode (affecting both shaders).
227 if (key.aa_mode_ == USE_AA) {
228 vertex_shader_.tex_coord_source_ = TEX_COORD_SOURCE_POSITION;
229 vertex_shader_.aa_mode_ = USE_AA;
230 fragment_shader_.has_rgba_fragment_tex_transform_ = true;
231 }
232 }
233
234 void InitializeTextureProgram(const ProgramKey& key) {
235 // Initialize vertex program.
236 vertex_shader_.tex_coord_source_ = TEX_COORD_SOURCE_ATTRIBUTE;
237 vertex_shader_.tex_coord_transform_ = TEX_COORD_TRANSFORM_VEC4;
238 vertex_shader_.has_matrix_ = true;
239 vertex_shader_.has_vertex_opacity_ = true;
240 vertex_shader_.use_uniform_arrays_ = true;
241
242 // Initialize fragment program.
243 fragment_shader_.has_varying_alpha_ = true;
244 fragment_shader_.has_background_color_ = key.has_background_color_;
245 }
246
247 void InitializeRenderPassProgram(const ProgramKey& key) {
248 // Initialize vertex program.
249 vertex_shader_.has_matrix_ = true;
250 if (key.aa_mode_ == NO_AA) {
251 vertex_shader_.tex_coord_source_ = TEX_COORD_SOURCE_ATTRIBUTE;
252 vertex_shader_.tex_coord_transform_ = TEX_COORD_TRANSFORM_VEC4;
253 vertex_shader_.has_vertex_opacity_ = true;
254 vertex_shader_.use_uniform_arrays_ = true;
255 } else {
256 vertex_shader_.position_source_ =
257 POSITION_SOURCE_ATTRIBUTE_INDEXED_UNIFORM;
258 vertex_shader_.tex_coord_source_ = TEX_COORD_SOURCE_POSITION;
259 vertex_shader_.tex_coord_transform_ = TEX_COORD_TRANSFORM_TRANSLATED_VEC4;
260 }
261
262 // Initialize fragment program.
263 fragment_shader_.frag_color_mode_ = FRAG_COLOR_MODE_APPLY_BLEND_MODE;
264 fragment_shader_.has_uniform_alpha_ = true;
265 fragment_shader_.has_color_matrix_ = key.has_color_matrix_;
266 if (key.mask_mode_ == HAS_MASK) {
267 fragment_shader_.ignore_sampler_type_ = true;
268 } else {
269 DCHECK(!key.mask_for_background_);
270 }
271 }
272
273 void InitializeVideoStreamProgram(const ProgramKey& key) {
274 vertex_shader_.tex_coord_source_ = TEX_COORD_SOURCE_ATTRIBUTE;
275 vertex_shader_.tex_coord_transform_ = TEX_COORD_TRANSFORM_MATRIX;
276 vertex_shader_.has_matrix_ = true;
277
278 fragment_shader_.tex_coord_precision_ = key.precision_;
279 fragment_shader_.sampler_type_ = SAMPLER_TYPE_EXTERNAL_OES;
280 fragment_shader_.frag_color_mode_ = FRAG_COLOR_MODE_DEFAULT;
281 }
282
105 void InitializeInternal(ContextProvider* context_provider) { 283 void InitializeInternal(ContextProvider* context_provider) {
106 DCHECK(context_provider); 284 DCHECK(context_provider);
107 DCHECK(!initialized_); 285 DCHECK(!initialized_);
108 286
109 if (IsContextLost(context_provider->ContextGL())) 287 if (IsContextLost(context_provider->ContextGL()))
110 return; 288 return;
111 289
112 if (!ProgramBindingBase::Init(context_provider->ContextGL(), 290 if (!ProgramBindingBase::Init(context_provider->ContextGL(),
113 vertex_shader_.GetShaderString(), 291 vertex_shader_.GetShaderString(),
114 fragment_shader_.GetShaderString())) { 292 fragment_shader_.GetShaderString())) {
(...skipping 18 matching lines...) Expand all
133 311
134 VertexShader vertex_shader_; 312 VertexShader vertex_shader_;
135 FragmentShader fragment_shader_; 313 FragmentShader fragment_shader_;
136 314
137 DISALLOW_COPY_AND_ASSIGN(ProgramBinding); 315 DISALLOW_COPY_AND_ASSIGN(ProgramBinding);
138 }; 316 };
139 317
140 } // namespace cc 318 } // namespace cc
141 319
142 #endif // CC_OUTPUT_PROGRAM_BINDING_H_ 320 #endif // CC_OUTPUT_PROGRAM_BINDING_H_
OLDNEW
« no previous file with comments | « cc/output/gl_renderer.cc ('k') | cc/output/program_binding.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698