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

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

Issue 2612823003: The great shader refactor: Delete all of the subclasses (Closed)
Patch Set: The great shader refactor: Delete all of the subclasses 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/program_binding.h ('k') | cc/output/shader.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_SHADER_H_ 5 #ifndef CC_OUTPUT_SHADER_H_
6 #define CC_OUTPUT_SHADER_H_ 6 #define CC_OUTPUT_SHADER_H_
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/macros.h" 10 #include "base/macros.h"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 55
56 // Position source for the vertex shader. 56 // Position source for the vertex shader.
57 enum PositionSource { 57 enum PositionSource {
58 // The position is read directly from the position attribute. 58 // The position is read directly from the position attribute.
59 POSITION_SOURCE_ATTRIBUTE, 59 POSITION_SOURCE_ATTRIBUTE,
60 // The position is read by attribute index into a uniform array for xy, and 60 // The position is read by attribute index into a uniform array for xy, and
61 // getting zw from the attribute. 61 // getting zw from the attribute.
62 POSITION_SOURCE_ATTRIBUTE_INDEXED_UNIFORM, 62 POSITION_SOURCE_ATTRIBUTE_INDEXED_UNIFORM,
63 }; 63 };
64 64
65 enum AAMode {
66 NO_AA = 0,
67 USE_AA = 1,
68 };
69
70 enum SwizzleMode {
71 NO_SWIZZLE = 0,
72 DO_SWIZZLE = 1,
73 };
74
75 enum PremultipliedAlphaMode {
76 PREMULTIPLIED_ALPHA = 0,
77 NON_PREMULTIPLIED_ALPHA = 1,
78 };
79
65 enum SamplerType { 80 enum SamplerType {
66 SAMPLER_TYPE_NA = 0, 81 SAMPLER_TYPE_NA = 0,
67 SAMPLER_TYPE_2D = 1, 82 SAMPLER_TYPE_2D = 1,
68 SAMPLER_TYPE_2D_RECT = 2, 83 SAMPLER_TYPE_2D_RECT = 2,
69 SAMPLER_TYPE_EXTERNAL_OES = 3, 84 SAMPLER_TYPE_EXTERNAL_OES = 3,
70 LAST_SAMPLER_TYPE = 3 85 LAST_SAMPLER_TYPE = 3
71 }; 86 };
72 87
73 enum BlendMode { 88 enum BlendMode {
74 BLEND_MODE_NONE, 89 BLEND_MODE_NONE,
(...skipping 27 matching lines...) Expand all
102 FRAG_COLOR_MODE_OPAQUE, 117 FRAG_COLOR_MODE_OPAQUE,
103 FRAG_COLOR_MODE_APPLY_BLEND_MODE, 118 FRAG_COLOR_MODE_APPLY_BLEND_MODE,
104 }; 119 };
105 120
106 enum MaskMode { 121 enum MaskMode {
107 NO_MASK = 0, 122 NO_MASK = 0,
108 HAS_MASK = 1, 123 HAS_MASK = 1,
109 LAST_MASK_VALUE = HAS_MASK 124 LAST_MASK_VALUE = HAS_MASK
110 }; 125 };
111 126
127 enum OpacityMode {
128 NOT_OPAQUE = 0,
129 IS_OPAQUE = 1,
130 };
131
112 struct ShaderLocations { 132 struct ShaderLocations {
113 ShaderLocations(); 133 ShaderLocations();
114 134
115 int sampler = -1; 135 int sampler = -1;
116 int quad = -1; 136 int quad = -1;
117 int edge = -1; 137 int edge = -1;
118 int viewport = -1; 138 int viewport = -1;
119 int mask_sampler = -1; 139 int mask_sampler = -1;
120 int mask_tex_coord_scale = -1; 140 int mask_tex_coord_scale = -1;
121 int mask_tex_coord_offset = -1; 141 int mask_tex_coord_offset = -1;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 int matrix_location() const { return matrix_location_; } 188 int matrix_location() const { return matrix_location_; }
169 189
170 int vertex_opacity_location() const { return vertex_opacity_location_; } 190 int vertex_opacity_location() const { return vertex_opacity_location_; }
171 191
172 int viewport_location() const { return viewport_location_; } 192 int viewport_location() const { return viewport_location_; }
173 int edge_location() const { return edge_location_; } 193 int edge_location() const { return edge_location_; }
174 194
175 int quad_location() const { return quad_location_; } 195 int quad_location() const { return quad_location_; }
176 196
177 protected: 197 protected:
198 template <class FragmentShader>
199 friend class ProgramBinding;
200
178 // Use arrays of uniforms for matrix, texTransform, and opacity. 201 // Use arrays of uniforms for matrix, texTransform, and opacity.
179 bool use_uniform_arrays_ = false; 202 bool use_uniform_arrays_ = false;
180 203
181 PositionSource position_source_ = POSITION_SOURCE_ATTRIBUTE; 204 PositionSource position_source_ = POSITION_SOURCE_ATTRIBUTE;
182 TexCoordSource tex_coord_source_ = TEX_COORD_SOURCE_NONE; 205 TexCoordSource tex_coord_source_ = TEX_COORD_SOURCE_NONE;
183 TexCoordTransform tex_coord_transform_ = TEX_COORD_TRANSFORM_NONE; 206 TexCoordTransform tex_coord_transform_ = TEX_COORD_TRANSFORM_NONE;
184 207
185 // Used only with TEX_COORD_TRANSFORM_VEC4. 208 // Used only with TEX_COORD_TRANSFORM_VEC4.
186 int vertex_tex_transform_location_ = -1; 209 int vertex_tex_transform_location_ = -1;
187 210
(...skipping 20 matching lines...) Expand all
208 bool has_dummy_variables_ = false; 231 bool has_dummy_variables_ = false;
209 232
210 bool has_vertex_opacity_ = false; 233 bool has_vertex_opacity_ = false;
211 int vertex_opacity_location_ = -1; 234 int vertex_opacity_location_ = -1;
212 235
213 bool has_aa_ = false; 236 bool has_aa_ = false;
214 int viewport_location_ = -1; 237 int viewport_location_ = -1;
215 int edge_location_ = -1; 238 int edge_location_ = -1;
216 }; 239 };
217 240
218 class VertexShaderPosTex : public VertexShaderBase {
219 public:
220 VertexShaderPosTex() {
221 tex_coord_source_ = TEX_COORD_SOURCE_ATTRIBUTE;
222 has_matrix_ = true;
223 }
224 };
225
226 class VertexShaderPosTexYUVStretchOffset : public VertexShaderBase {
227 public:
228 VertexShaderPosTexYUVStretchOffset() {
229 tex_coord_source_ = TEX_COORD_SOURCE_ATTRIBUTE;
230 has_matrix_ = true;
231 is_ya_uv_ = true;
232 }
233 };
234
235 class VertexShaderPos : public VertexShaderBase {
236 public:
237 VertexShaderPos() { has_matrix_ = true; }
238 };
239
240 class VertexShaderPosTexTransform : public VertexShaderBase {
241 public:
242 VertexShaderPosTexTransform() {
243 tex_coord_source_ = TEX_COORD_SOURCE_ATTRIBUTE;
244 tex_coord_transform_ = TEX_COORD_TRANSFORM_VEC4;
245 has_matrix_ = true;
246 has_vertex_opacity_ = true;
247 use_uniform_arrays_ = true;
248 }
249 };
250
251 class VertexShaderQuad : public VertexShaderBase {
252 public:
253 VertexShaderQuad() {
254 position_source_ = POSITION_SOURCE_ATTRIBUTE_INDEXED_UNIFORM;
255 has_matrix_ = true;
256 #if defined(OS_ANDROID)
257 has_dummy_variables_ = true;
258 #endif
259 }
260 };
261
262 class VertexShaderQuadAA : public VertexShaderBase {
263 public:
264 VertexShaderQuadAA() {
265 position_source_ = POSITION_SOURCE_ATTRIBUTE_INDEXED_UNIFORM;
266 has_matrix_ = true;
267 has_aa_ = true;
268 }
269 };
270
271 class VertexShaderQuadTexTransformAA : public VertexShaderBase {
272 public:
273 VertexShaderQuadTexTransformAA() {
274 position_source_ = POSITION_SOURCE_ATTRIBUTE_INDEXED_UNIFORM;
275 tex_coord_source_ = TEX_COORD_SOURCE_POSITION;
276 tex_coord_transform_ = TEX_COORD_TRANSFORM_TRANSLATED_VEC4;
277 has_matrix_ = true;
278 has_aa_ = true;
279 }
280 };
281
282 class VertexShaderTile : public VertexShaderBase {
283 public:
284 VertexShaderTile() {
285 position_source_ = POSITION_SOURCE_ATTRIBUTE_INDEXED_UNIFORM;
286 tex_coord_source_ = TEX_COORD_SOURCE_ATTRIBUTE;
287 tex_coord_transform_ = TEX_COORD_TRANSFORM_VEC4;
288 has_matrix_ = true;
289 }
290 };
291
292 class VertexShaderTileAA : public VertexShaderBase {
293 public:
294 VertexShaderTileAA() {
295 position_source_ = POSITION_SOURCE_ATTRIBUTE_INDEXED_UNIFORM;
296 tex_coord_source_ = TEX_COORD_SOURCE_POSITION;
297 tex_coord_transform_ = TEX_COORD_TRANSFORM_VEC4;
298 has_matrix_ = true;
299 has_aa_ = true;
300 }
301 };
302
303 class VertexShaderVideoTransform : public VertexShaderBase {
304 public:
305 VertexShaderVideoTransform() {
306 tex_coord_source_ = TEX_COORD_SOURCE_ATTRIBUTE;
307 tex_coord_transform_ = TEX_COORD_TRANSFORM_MATRIX;
308 has_matrix_ = true;
309 }
310 };
311
312 class FragmentShaderBase { 241 class FragmentShaderBase {
313 public: 242 public:
314 virtual void Init(gpu::gles2::GLES2Interface* context, 243 virtual void Init(gpu::gles2::GLES2Interface* context,
315 unsigned program, 244 unsigned program,
316 int* base_uniform_index); 245 int* base_uniform_index);
317 std::string GetShaderString(TexCoordPrecision precision, 246 std::string GetShaderString() const;
318 SamplerType sampler) const;
319 void FillLocations(ShaderLocations* locations) const; 247 void FillLocations(ShaderLocations* locations) const;
320 248
321 BlendMode blend_mode() const { return blend_mode_; }
322 void set_blend_mode(BlendMode blend_mode) { blend_mode_ = blend_mode; }
323 bool has_blend_mode() const { return blend_mode_ != BLEND_MODE_NONE; }
324 void set_mask_for_background(bool mask_for_background) {
325 mask_for_background_ = mask_for_background;
326 }
327 bool mask_for_background() const { return mask_for_background_; }
328
329 int sampler_location() const { return sampler_location_; } 249 int sampler_location() const { return sampler_location_; }
330 int alpha_location() const { return alpha_location_; } 250 int alpha_location() const { return alpha_location_; }
331 int color_location() const { return color_location_; } 251 int color_location() const { return color_location_; }
332 int background_color_location() const { return background_color_location_; } 252 int background_color_location() const { return background_color_location_; }
333 int fragment_tex_transform_location() const { 253 int fragment_tex_transform_location() const {
334 return fragment_tex_transform_location_; 254 return fragment_tex_transform_location_;
335 } 255 }
336 256
337 protected: 257 protected:
338 FragmentShaderBase(); 258 FragmentShaderBase();
339 virtual std::string GetShaderSource() const; 259 virtual std::string GetShaderSource() const;
260 bool has_blend_mode() const { return blend_mode_ != BLEND_MODE_NONE; }
340 261
341 std::string SetBlendModeFunctions(const std::string& shader_string) const; 262 std::string SetBlendModeFunctions(const std::string& shader_string) const;
342 263
343 // Settings that are modified by sub-classes. 264 // Settings that are modified by sub-classes.
344 bool has_aa_ = false; 265 bool has_aa_ = false;
345 bool has_varying_alpha_ = false; 266 bool has_varying_alpha_ = false;
346 bool has_swizzle_ = false; 267 bool has_swizzle_ = false;
347 bool has_premultiply_alpha_ = false; 268 bool has_premultiply_alpha_ = false;
348 FragColorMode frag_color_mode_ = FRAG_COLOR_MODE_DEFAULT; 269 FragColorMode frag_color_mode_ = FRAG_COLOR_MODE_DEFAULT;
349 InputColorSource input_color_type_ = INPUT_COLOR_SOURCE_RGBA_TEXTURE; 270 InputColorSource input_color_type_ = INPUT_COLOR_SOURCE_RGBA_TEXTURE;
(...skipping 25 matching lines...) Expand all
375 bool has_color_matrix_ = false; 296 bool has_color_matrix_ = false;
376 int color_matrix_location_ = -1; 297 int color_matrix_location_ = -1;
377 int color_offset_location_ = -1; 298 int color_offset_location_ = -1;
378 299
379 bool has_uniform_alpha_ = false; 300 bool has_uniform_alpha_ = false;
380 int alpha_location_ = -1; 301 int alpha_location_ = -1;
381 302
382 bool has_background_color_ = false; 303 bool has_background_color_ = false;
383 int background_color_location_ = -1; 304 int background_color_location_ = -1;
384 305
385 private: 306 TexCoordPrecision tex_coord_precision_ = TEX_COORD_PRECISION_NA;
307 SamplerType sampler_type_ = SAMPLER_TYPE_NA;
386 BlendMode blend_mode_ = BLEND_MODE_NONE; 308 BlendMode blend_mode_ = BLEND_MODE_NONE;
387 bool mask_for_background_ = false; 309 bool mask_for_background_ = false;
388 310
311 private:
312 template <class FragmentShader>
313 friend class ProgramBinding;
314
389 std::string GetHelperFunctions() const; 315 std::string GetHelperFunctions() const;
390 std::string GetBlendFunction() const; 316 std::string GetBlendFunction() const;
391 std::string GetBlendFunctionBodyForRGB() const; 317 std::string GetBlendFunctionBodyForRGB() const;
392 318
393 DISALLOW_COPY_AND_ASSIGN(FragmentShaderBase); 319 DISALLOW_COPY_AND_ASSIGN(FragmentShaderBase);
394 }; 320 };
395 321
396 class FragmentShaderRGBATexVaryingAlpha : public FragmentShaderBase {
397 public:
398 FragmentShaderRGBATexVaryingAlpha() {
399 has_varying_alpha_ = true;
400 frag_color_mode_ = FRAG_COLOR_MODE_DEFAULT;
401 }
402 };
403
404 class FragmentShaderRGBATexPremultiplyAlpha : public FragmentShaderBase {
405 public:
406 FragmentShaderRGBATexPremultiplyAlpha() {
407 has_varying_alpha_ = true;
408 has_premultiply_alpha_ = true;
409 frag_color_mode_ = FRAG_COLOR_MODE_DEFAULT;
410 }
411 };
412
413 class FragmentShaderTexBackgroundVaryingAlpha : public FragmentShaderBase {
414 public:
415 FragmentShaderTexBackgroundVaryingAlpha() {
416 has_background_color_ = true;
417 has_varying_alpha_ = true;
418 frag_color_mode_ = FRAG_COLOR_MODE_DEFAULT;
419 }
420 };
421
422 class FragmentShaderTexBackgroundPremultiplyAlpha : public FragmentShaderBase {
423 public:
424 FragmentShaderTexBackgroundPremultiplyAlpha() {
425 has_background_color_ = true;
426 has_varying_alpha_ = true;
427 has_premultiply_alpha_ = true;
428 frag_color_mode_ = FRAG_COLOR_MODE_DEFAULT;
429 }
430 };
431
432 class FragmentShaderRGBATexAlpha : public FragmentShaderBase {
433 public:
434 FragmentShaderRGBATexAlpha() {
435 has_uniform_alpha_ = true;
436 frag_color_mode_ = FRAG_COLOR_MODE_APPLY_BLEND_MODE;
437 }
438 };
439
440 class FragmentShaderRGBATexColorMatrixAlpha : public FragmentShaderBase {
441 public:
442 FragmentShaderRGBATexColorMatrixAlpha() {
443 has_uniform_alpha_ = true;
444 has_color_matrix_ = true;
445 frag_color_mode_ = FRAG_COLOR_MODE_APPLY_BLEND_MODE;
446 }
447 };
448
449 class FragmentShaderRGBATexOpaque : public FragmentShaderBase {
450 public:
451 FragmentShaderRGBATexOpaque() { frag_color_mode_ = FRAG_COLOR_MODE_OPAQUE; }
452 };
453
454 class FragmentShaderRGBATex : public FragmentShaderBase {
455 public:
456 FragmentShaderRGBATex() { frag_color_mode_ = FRAG_COLOR_MODE_DEFAULT; }
457 };
458
459 // Swizzles the red and blue component of sampled texel with alpha.
460 class FragmentShaderRGBATexSwizzleAlpha : public FragmentShaderBase {
461 public:
462 FragmentShaderRGBATexSwizzleAlpha() {
463 has_uniform_alpha_ = true;
464 has_swizzle_ = true;
465 frag_color_mode_ = FRAG_COLOR_MODE_DEFAULT;
466 }
467 };
468
469 // Swizzles the red and blue component of sampled texel without alpha.
470 class FragmentShaderRGBATexSwizzleOpaque : public FragmentShaderBase {
471 public:
472 FragmentShaderRGBATexSwizzleOpaque() {
473 has_swizzle_ = true;
474 frag_color_mode_ = FRAG_COLOR_MODE_OPAQUE;
475 }
476 };
477
478 class FragmentShaderRGBATexAlphaAA : public FragmentShaderBase {
479 public:
480 FragmentShaderRGBATexAlphaAA() {
481 has_aa_ = true;
482 has_uniform_alpha_ = true;
483 frag_color_mode_ = FRAG_COLOR_MODE_APPLY_BLEND_MODE;
484 }
485 };
486
487 class FragmentShaderRGBATexClampAlphaAA : public FragmentShaderBase {
488 public:
489 FragmentShaderRGBATexClampAlphaAA() {
490 has_aa_ = true;
491 has_uniform_alpha_ = true;
492 has_rgba_fragment_tex_transform_ = true;
493 frag_color_mode_ = FRAG_COLOR_MODE_DEFAULT;
494 }
495 };
496
497 // Swizzles the red and blue component of sampled texel.
498 class FragmentShaderRGBATexClampSwizzleAlphaAA : public FragmentShaderBase {
499 public:
500 FragmentShaderRGBATexClampSwizzleAlphaAA() {
501 has_aa_ = true;
502 has_uniform_alpha_ = true;
503 has_rgba_fragment_tex_transform_ = true;
504 has_swizzle_ = true;
505 frag_color_mode_ = FRAG_COLOR_MODE_DEFAULT;
506 }
507 };
508
509 class FragmentShaderRGBATexAlphaMask : public FragmentShaderBase {
510 public:
511 FragmentShaderRGBATexAlphaMask() {
512 has_uniform_alpha_ = true;
513 has_mask_sampler_ = true;
514 frag_color_mode_ = FRAG_COLOR_MODE_APPLY_BLEND_MODE;
515 ignore_sampler_type_ = true;
516 }
517 };
518
519 class FragmentShaderRGBATexAlphaMaskAA : public FragmentShaderBase {
520 public:
521 FragmentShaderRGBATexAlphaMaskAA() {
522 has_aa_ = true;
523 has_uniform_alpha_ = true;
524 has_mask_sampler_ = true;
525 frag_color_mode_ = FRAG_COLOR_MODE_APPLY_BLEND_MODE;
526 ignore_sampler_type_ = true;
527 }
528 };
529
530 class FragmentShaderRGBATexAlphaMaskColorMatrixAA : public FragmentShaderBase {
531 public:
532 FragmentShaderRGBATexAlphaMaskColorMatrixAA() {
533 has_aa_ = true;
534 has_uniform_alpha_ = true;
535 has_mask_sampler_ = true;
536 has_color_matrix_ = true;
537 frag_color_mode_ = FRAG_COLOR_MODE_APPLY_BLEND_MODE;
538 ignore_sampler_type_ = true;
539 }
540 };
541
542 class FragmentShaderRGBATexAlphaColorMatrixAA : public FragmentShaderBase {
543 public:
544 FragmentShaderRGBATexAlphaColorMatrixAA() {
545 has_aa_ = true;
546 has_uniform_alpha_ = true;
547 has_color_matrix_ = true;
548 frag_color_mode_ = FRAG_COLOR_MODE_APPLY_BLEND_MODE;
549 }
550 };
551
552 class FragmentShaderRGBATexAlphaMaskColorMatrix : public FragmentShaderBase {
553 public:
554 FragmentShaderRGBATexAlphaMaskColorMatrix() {
555 has_uniform_alpha_ = true;
556 has_mask_sampler_ = true;
557 has_color_matrix_ = true;
558 frag_color_mode_ = FRAG_COLOR_MODE_APPLY_BLEND_MODE;
559 ignore_sampler_type_ = true;
560 }
561 };
562
563 class FragmentShaderYUVVideo : public FragmentShaderBase { 322 class FragmentShaderYUVVideo : public FragmentShaderBase {
564 public: 323 public:
565 FragmentShaderYUVVideo(); 324 FragmentShaderYUVVideo();
566 void SetFeatures(bool use_alpha_texture, bool use_nv12, bool use_color_lut); 325 void SetFeatures(bool use_alpha_texture, bool use_nv12, bool use_color_lut);
567 326
568 void Init(gpu::gles2::GLES2Interface* context, 327 void Init(gpu::gles2::GLES2Interface* context,
569 unsigned program, 328 unsigned program,
570 int* base_uniform_index) override; 329 int* base_uniform_index) override;
571 int y_texture_location() const { return y_texture_location_; } 330 int y_texture_location() const { return y_texture_location_; }
572 int u_texture_location() const { return u_texture_location_; } 331 int u_texture_location() const { return u_texture_location_; }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 FragmentShaderColorAA() { 378 FragmentShaderColorAA() {
620 input_color_type_ = INPUT_COLOR_SOURCE_UNIFORM; 379 input_color_type_ = INPUT_COLOR_SOURCE_UNIFORM;
621 has_aa_ = true; 380 has_aa_ = true;
622 frag_color_mode_ = FRAG_COLOR_MODE_DEFAULT; 381 frag_color_mode_ = FRAG_COLOR_MODE_DEFAULT;
623 } 382 }
624 }; 383 };
625 384
626 } // namespace cc 385 } // namespace cc
627 386
628 #endif // CC_OUTPUT_SHADER_H_ 387 #endif // CC_OUTPUT_SHADER_H_
OLDNEW
« no previous file with comments | « cc/output/program_binding.h ('k') | cc/output/shader.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698