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
3// found in the LICENSE file.
4
5#ifndef CC_OUTPUT_SHADER_H_
6#define CC_OUTPUT_SHADER_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "cc/base/cc_export.h"
12
13namespace gfx {
14class Point;
15class Size;
16}
17
18namespace gpu {
19namespace gles2 {
20class GLES2Interface;
21}
22}
23
24namespace cc {
25
26enum TexCoordPrecision {
27  TexCoordPrecisionNA = 0,
28  TexCoordPrecisionMedium = 1,
29  TexCoordPrecisionHigh = 2,
30  NumTexCoordPrecisions = 3
31};
32
33enum SamplerType {
34  SamplerTypeNA = 0,
35  SamplerType2D = 1,
36  SamplerType2DRect = 2,
37  SamplerTypeExternalOES = 3,
38  NumSamplerTypes = 4
39};
40
41// Note: The highp_threshold_cache must be provided by the caller to make
42// the caching multi-thread/context safe in an easy low-overhead manner.
43// The caller must make sure to clear highp_threshold_cache to 0, so it can be
44// reinitialized, if a new or different context is used.
45CC_EXPORT TexCoordPrecision
46    TexCoordPrecisionRequired(gpu::gles2::GLES2Interface* context,
47                              int* highp_threshold_cache,
48                              int highp_threshold_min,
49                              const gfx::Point& max_coordinate);
50
51CC_EXPORT TexCoordPrecision TexCoordPrecisionRequired(
52    gpu::gles2::GLES2Interface* context,
53    int *highp_threshold_cache,
54    int highp_threshold_min,
55    const gfx::Size& max_size);
56
57class VertexShaderPosTex {
58 public:
59  VertexShaderPosTex();
60
61  void Init(gpu::gles2::GLES2Interface* context,
62            unsigned program,
63            int* base_uniform_index);
64  std::string GetShaderString() const;
65
66  int matrix_location() const { return matrix_location_; }
67
68 private:
69  int matrix_location_;
70
71  DISALLOW_COPY_AND_ASSIGN(VertexShaderPosTex);
72};
73
74class VertexShaderPosTexYUVStretchOffset {
75 public:
76  VertexShaderPosTexYUVStretchOffset();
77
78  void Init(gpu::gles2::GLES2Interface* context,
79            unsigned program,
80            int* base_uniform_index);
81  std::string GetShaderString() const;
82
83  int matrix_location() const { return matrix_location_; }
84  int tex_scale_location() const { return tex_scale_location_; }
85  int tex_offset_location() const { return tex_offset_location_; }
86
87 private:
88  int matrix_location_;
89  int tex_scale_location_;
90  int tex_offset_location_;
91
92  DISALLOW_COPY_AND_ASSIGN(VertexShaderPosTexYUVStretchOffset);
93};
94
95class VertexShaderPos {
96 public:
97  VertexShaderPos();
98
99  void Init(gpu::gles2::GLES2Interface* context,
100            unsigned program,
101            int* base_uniform_index);
102  std::string GetShaderString() const;
103
104  int matrix_location() const { return matrix_location_; }
105
106 private:
107  int matrix_location_;
108
109  DISALLOW_COPY_AND_ASSIGN(VertexShaderPos);
110};
111
112class VertexShaderPosTexIdentity {
113 public:
114  void Init(gpu::gles2::GLES2Interface* context,
115            unsigned program,
116            int* base_uniform_index) {}
117  std::string GetShaderString() const;
118};
119
120class VertexShaderPosTexTransform {
121 public:
122  VertexShaderPosTexTransform();
123
124  void Init(gpu::gles2::GLES2Interface* context,
125            unsigned program,
126            int* base_uniform_index);
127  std::string GetShaderString() const;
128
129  int matrix_location() const { return matrix_location_; }
130  int tex_transform_location() const { return tex_transform_location_; }
131  int vertex_opacity_location() const { return vertex_opacity_location_; }
132
133 private:
134  int matrix_location_;
135  int tex_transform_location_;
136  int vertex_opacity_location_;
137
138  DISALLOW_COPY_AND_ASSIGN(VertexShaderPosTexTransform);
139};
140
141class VertexShaderQuad {
142 public:
143  VertexShaderQuad();
144
145  void Init(gpu::gles2::GLES2Interface* context,
146           unsigned program,
147           int* base_uniform_index);
148  std::string GetShaderString() const;
149
150  int matrix_location() const { return matrix_location_; }
151  int viewport_location() const { return -1; }
152  int quad_location() const { return quad_location_; }
153  int edge_location() const { return -1; }
154
155 private:
156  int matrix_location_;
157  int quad_location_;
158
159  DISALLOW_COPY_AND_ASSIGN(VertexShaderQuad);
160};
161
162class VertexShaderQuadAA {
163 public:
164  VertexShaderQuadAA();
165
166  void Init(gpu::gles2::GLES2Interface* context,
167           unsigned program,
168           int* base_uniform_index);
169  std::string GetShaderString() const;
170
171  int matrix_location() const { return matrix_location_; }
172  int viewport_location() const { return viewport_location_; }
173  int quad_location() const { return quad_location_; }
174  int edge_location() const { return edge_location_; }
175
176 private:
177  int matrix_location_;
178  int viewport_location_;
179  int quad_location_;
180  int edge_location_;
181
182  DISALLOW_COPY_AND_ASSIGN(VertexShaderQuadAA);
183};
184
185
186class VertexShaderQuadTexTransformAA {
187 public:
188  VertexShaderQuadTexTransformAA();
189
190  void Init(gpu::gles2::GLES2Interface* context,
191           unsigned program,
192           int* base_uniform_index);
193  std::string GetShaderString() const;
194
195  int matrix_location() const { return matrix_location_; }
196  int viewport_location() const { return viewport_location_; }
197  int quad_location() const { return quad_location_; }
198  int edge_location() const { return edge_location_; }
199  int tex_transform_location() const { return tex_transform_location_; }
200
201 private:
202  int matrix_location_;
203  int viewport_location_;
204  int quad_location_;
205  int edge_location_;
206  int tex_transform_location_;
207
208  DISALLOW_COPY_AND_ASSIGN(VertexShaderQuadTexTransformAA);
209};
210
211class VertexShaderTile {
212 public:
213  VertexShaderTile();
214
215  void Init(gpu::gles2::GLES2Interface* context,
216            unsigned program,
217            int* base_uniform_index);
218  std::string GetShaderString() const;
219
220  int matrix_location() const { return matrix_location_; }
221  int viewport_location() const { return -1; }
222  int quad_location() const { return quad_location_; }
223  int edge_location() const { return -1; }
224  int vertex_tex_transform_location() const {
225    return vertex_tex_transform_location_;
226  }
227
228 private:
229  int matrix_location_;
230  int quad_location_;
231  int vertex_tex_transform_location_;
232
233  DISALLOW_COPY_AND_ASSIGN(VertexShaderTile);
234};
235
236class VertexShaderTileAA {
237 public:
238  VertexShaderTileAA();
239
240  void Init(gpu::gles2::GLES2Interface* context,
241            unsigned program,
242            int* base_uniform_index);
243  std::string GetShaderString() const;
244
245  int matrix_location() const { return matrix_location_; }
246  int viewport_location() const { return viewport_location_; }
247  int quad_location() const { return quad_location_; }
248  int edge_location() const { return edge_location_; }
249  int vertex_tex_transform_location() const {
250    return vertex_tex_transform_location_;
251  }
252
253 private:
254  int matrix_location_;
255  int viewport_location_;
256  int quad_location_;
257  int edge_location_;
258  int vertex_tex_transform_location_;
259
260  DISALLOW_COPY_AND_ASSIGN(VertexShaderTileAA);
261};
262
263class VertexShaderVideoTransform {
264 public:
265  VertexShaderVideoTransform();
266
267  void Init(gpu::gles2::GLES2Interface* context,
268            unsigned program,
269            int* base_uniform_index);
270  std::string GetShaderString() const;
271
272  int matrix_location() const { return matrix_location_; }
273  int tex_matrix_location() const { return tex_matrix_location_; }
274
275 private:
276  int matrix_location_;
277  int tex_matrix_location_;
278
279  DISALLOW_COPY_AND_ASSIGN(VertexShaderVideoTransform);
280};
281
282class FragmentTexAlphaBinding {
283 public:
284  FragmentTexAlphaBinding();
285
286  void Init(gpu::gles2::GLES2Interface* context,
287            unsigned program,
288            int* base_uniform_index);
289  int alpha_location() const { return alpha_location_; }
290  int fragment_tex_transform_location() const { return -1; }
291  int sampler_location() const { return sampler_location_; }
292
293 private:
294  int sampler_location_;
295  int alpha_location_;
296
297  DISALLOW_COPY_AND_ASSIGN(FragmentTexAlphaBinding);
298};
299
300class FragmentTexColorMatrixAlphaBinding {
301 public:
302    FragmentTexColorMatrixAlphaBinding();
303
304    void Init(gpu::gles2::GLES2Interface* context,
305              unsigned program,
306              int* base_uniform_index);
307    int alpha_location() const { return alpha_location_; }
308    int color_matrix_location() const { return color_matrix_location_; }
309    int color_offset_location() const { return color_offset_location_; }
310    int fragment_tex_transform_location() const { return -1; }
311    int sampler_location() const { return sampler_location_; }
312
313 private:
314    int sampler_location_;
315    int alpha_location_;
316    int color_matrix_location_;
317    int color_offset_location_;
318};
319
320class FragmentTexOpaqueBinding {
321 public:
322  FragmentTexOpaqueBinding();
323
324  void Init(gpu::gles2::GLES2Interface* context,
325            unsigned program,
326            int* base_uniform_index);
327  int alpha_location() const { return -1; }
328  int fragment_tex_transform_location() const { return -1; }
329  int background_color_location() const { return -1; }
330  int sampler_location() const { return sampler_location_; }
331
332 private:
333  int sampler_location_;
334
335  DISALLOW_COPY_AND_ASSIGN(FragmentTexOpaqueBinding);
336};
337
338class FragmentTexBackgroundBinding {
339 public:
340  FragmentTexBackgroundBinding();
341
342  void Init(gpu::gles2::GLES2Interface* context,
343            unsigned program,
344            int* base_uniform_index);
345  int background_color_location() const { return background_color_location_; }
346  int sampler_location() const { return sampler_location_; }
347
348 private:
349  int background_color_location_;
350  int sampler_location_;
351
352  DISALLOW_COPY_AND_ASSIGN(FragmentTexBackgroundBinding);
353};
354
355class FragmentShaderRGBATexVaryingAlpha : public FragmentTexOpaqueBinding {
356 public:
357  std::string GetShaderString(
358      TexCoordPrecision precision, SamplerType sampler) const;
359};
360
361class FragmentShaderRGBATexPremultiplyAlpha : public FragmentTexOpaqueBinding {
362 public:
363  std::string GetShaderString(
364      TexCoordPrecision precision, SamplerType sampler) const;
365};
366
367class FragmentShaderTexBackgroundVaryingAlpha
368    : public FragmentTexBackgroundBinding {
369 public:
370  std::string GetShaderString(
371      TexCoordPrecision precision, SamplerType sampler) const;
372};
373
374class FragmentShaderTexBackgroundPremultiplyAlpha
375    : public FragmentTexBackgroundBinding {
376 public:
377  std::string GetShaderString(
378      TexCoordPrecision precision, SamplerType sampler) const;
379};
380
381class FragmentShaderRGBATexAlpha : public FragmentTexAlphaBinding {
382 public:
383  std::string GetShaderString(
384      TexCoordPrecision precision, SamplerType sampler) const;
385};
386
387class FragmentShaderRGBATexColorMatrixAlpha
388    : public FragmentTexColorMatrixAlphaBinding {
389 public:
390    std::string GetShaderString(
391        TexCoordPrecision precision, SamplerType sampler) const;
392};
393
394class FragmentShaderRGBATexOpaque : public FragmentTexOpaqueBinding {
395 public:
396  std::string GetShaderString(
397      TexCoordPrecision precision, SamplerType sampler) const;
398};
399
400class FragmentShaderRGBATex : public FragmentTexOpaqueBinding {
401 public:
402  std::string GetShaderString(
403      TexCoordPrecision precision, SamplerType sampler) const;
404};
405
406// Swizzles the red and blue component of sampled texel with alpha.
407class FragmentShaderRGBATexSwizzleAlpha : public FragmentTexAlphaBinding {
408 public:
409  std::string GetShaderString(
410      TexCoordPrecision precision, SamplerType sampler) const;
411};
412
413// Swizzles the red and blue component of sampled texel without alpha.
414class FragmentShaderRGBATexSwizzleOpaque : public FragmentTexOpaqueBinding {
415 public:
416  std::string GetShaderString(
417      TexCoordPrecision precision, SamplerType sampler) const;
418};
419
420class FragmentShaderRGBATexAlphaAA {
421 public:
422  FragmentShaderRGBATexAlphaAA();
423
424  void Init(gpu::gles2::GLES2Interface* context,
425            unsigned program,
426            int* base_uniform_index);
427  std::string GetShaderString(
428      TexCoordPrecision precision, SamplerType sampler) const;
429
430  int alpha_location() const { return alpha_location_; }
431  int sampler_location() const { return sampler_location_; }
432
433 private:
434  int sampler_location_;
435  int alpha_location_;
436
437  DISALLOW_COPY_AND_ASSIGN(FragmentShaderRGBATexAlphaAA);
438};
439
440class FragmentTexClampAlphaAABinding {
441 public:
442  FragmentTexClampAlphaAABinding();
443
444  void Init(gpu::gles2::GLES2Interface* context,
445            unsigned program,
446            int* base_uniform_index);
447  int alpha_location() const { return alpha_location_; }
448  int sampler_location() const { return sampler_location_; }
449  int fragment_tex_transform_location() const {
450    return fragment_tex_transform_location_;
451  }
452
453 private:
454  int sampler_location_;
455  int alpha_location_;
456  int fragment_tex_transform_location_;
457
458  DISALLOW_COPY_AND_ASSIGN(FragmentTexClampAlphaAABinding);
459};
460
461class FragmentShaderRGBATexClampAlphaAA
462    : public FragmentTexClampAlphaAABinding {
463 public:
464  std::string GetShaderString(
465      TexCoordPrecision precision, SamplerType sampler) const;
466};
467
468// Swizzles the red and blue component of sampled texel.
469class FragmentShaderRGBATexClampSwizzleAlphaAA
470    : public FragmentTexClampAlphaAABinding {
471 public:
472  std::string GetShaderString(
473      TexCoordPrecision precision, SamplerType sampler) const;
474};
475
476class FragmentShaderRGBATexAlphaMask {
477 public:
478  FragmentShaderRGBATexAlphaMask();
479  std::string GetShaderString(
480      TexCoordPrecision precision, SamplerType sampler) const;
481
482  void Init(gpu::gles2::GLES2Interface* context,
483            unsigned program,
484            int* base_uniform_index);
485  int alpha_location() const { return alpha_location_; }
486  int sampler_location() const { return sampler_location_; }
487  int mask_sampler_location() const { return mask_sampler_location_; }
488  int mask_tex_coord_scale_location() const {
489    return mask_tex_coord_scale_location_;
490  }
491  int mask_tex_coord_offset_location() const {
492    return mask_tex_coord_offset_location_;
493  }
494
495 private:
496  int sampler_location_;
497  int mask_sampler_location_;
498  int alpha_location_;
499  int mask_tex_coord_scale_location_;
500  int mask_tex_coord_offset_location_;
501
502  DISALLOW_COPY_AND_ASSIGN(FragmentShaderRGBATexAlphaMask);
503};
504
505class FragmentShaderRGBATexAlphaMaskAA {
506 public:
507  FragmentShaderRGBATexAlphaMaskAA();
508  std::string GetShaderString(
509      TexCoordPrecision precision, SamplerType sampler) const;
510
511  void Init(gpu::gles2::GLES2Interface* context,
512            unsigned program,
513            int* base_uniform_index);
514  int alpha_location() const { return alpha_location_; }
515  int sampler_location() const { return sampler_location_; }
516  int mask_sampler_location() const { return mask_sampler_location_; }
517  int mask_tex_coord_scale_location() const {
518    return mask_tex_coord_scale_location_;
519  }
520  int mask_tex_coord_offset_location() const {
521    return mask_tex_coord_offset_location_;
522  }
523
524 private:
525  int sampler_location_;
526  int mask_sampler_location_;
527  int alpha_location_;
528  int mask_tex_coord_scale_location_;
529  int mask_tex_coord_offset_location_;
530
531  DISALLOW_COPY_AND_ASSIGN(FragmentShaderRGBATexAlphaMaskAA);
532};
533
534class FragmentShaderRGBATexAlphaMaskColorMatrixAA {
535 public:
536  FragmentShaderRGBATexAlphaMaskColorMatrixAA();
537  std::string GetShaderString(
538      TexCoordPrecision precision, SamplerType sampler) const;
539
540  void Init(gpu::gles2::GLES2Interface* context,
541            unsigned program,
542            int* base_uniform_index);
543  int alpha_location() const { return alpha_location_; }
544  int sampler_location() const { return sampler_location_; }
545  int mask_sampler_location() const { return mask_sampler_location_; }
546  int mask_tex_coord_scale_location() const {
547    return mask_tex_coord_scale_location_;
548  }
549  int mask_tex_coord_offset_location() const {
550    return mask_tex_coord_offset_location_;
551  }
552  int color_matrix_location() const { return color_matrix_location_; }
553  int color_offset_location() const { return color_offset_location_; }
554
555 private:
556  int sampler_location_;
557  int mask_sampler_location_;
558  int alpha_location_;
559  int mask_tex_coord_scale_location_;
560  int mask_tex_coord_offset_location_;
561  int color_matrix_location_;
562  int color_offset_location_;
563};
564
565class FragmentShaderRGBATexAlphaColorMatrixAA {
566 public:
567  FragmentShaderRGBATexAlphaColorMatrixAA();
568  std::string GetShaderString(
569      TexCoordPrecision precision, SamplerType sampler) const;
570
571  void Init(gpu::gles2::GLES2Interface* context,
572            unsigned program,
573            int* base_uniform_index);
574  int alpha_location() const { return alpha_location_; }
575  int sampler_location() const { return sampler_location_; }
576  int color_matrix_location() const { return color_matrix_location_; }
577  int color_offset_location() const { return color_offset_location_; }
578
579 private:
580  int sampler_location_;
581  int alpha_location_;
582  int color_matrix_location_;
583  int color_offset_location_;
584};
585
586class FragmentShaderRGBATexAlphaMaskColorMatrix {
587 public:
588  FragmentShaderRGBATexAlphaMaskColorMatrix();
589  std::string GetShaderString(
590      TexCoordPrecision precision, SamplerType sampler) const;
591
592  void Init(gpu::gles2::GLES2Interface* context,
593            unsigned program,
594            int* base_uniform_index);
595  int alpha_location() const { return alpha_location_; }
596  int sampler_location() const { return sampler_location_; }
597  int mask_sampler_location() const { return mask_sampler_location_; }
598  int mask_tex_coord_scale_location() const {
599    return mask_tex_coord_scale_location_;
600  }
601  int mask_tex_coord_offset_location() const {
602    return mask_tex_coord_offset_location_;
603  }
604  int color_matrix_location() const { return color_matrix_location_; }
605  int color_offset_location() const { return color_offset_location_; }
606
607 private:
608  int sampler_location_;
609  int mask_sampler_location_;
610  int alpha_location_;
611  int mask_tex_coord_scale_location_;
612  int mask_tex_coord_offset_location_;
613  int color_matrix_location_;
614  int color_offset_location_;
615};
616
617class FragmentShaderYUVVideo {
618 public:
619  FragmentShaderYUVVideo();
620  std::string GetShaderString(
621      TexCoordPrecision precision, SamplerType sampler) const;
622
623  void Init(gpu::gles2::GLES2Interface* context,
624            unsigned program,
625            int* base_uniform_index);
626  int y_texture_location() const { return y_texture_location_; }
627  int u_texture_location() const { return u_texture_location_; }
628  int v_texture_location() const { return v_texture_location_; }
629  int alpha_location() const { return alpha_location_; }
630  int yuv_matrix_location() const { return yuv_matrix_location_; }
631  int yuv_adj_location() const { return yuv_adj_location_; }
632
633 private:
634  int y_texture_location_;
635  int u_texture_location_;
636  int v_texture_location_;
637  int alpha_location_;
638  int yuv_matrix_location_;
639  int yuv_adj_location_;
640
641  DISALLOW_COPY_AND_ASSIGN(FragmentShaderYUVVideo);
642};
643
644
645class FragmentShaderYUVAVideo {
646 public:
647  FragmentShaderYUVAVideo();
648  std::string GetShaderString(
649      TexCoordPrecision precision, SamplerType sampler) const;
650
651  void Init(gpu::gles2::GLES2Interface* context,
652            unsigned program,
653            int* base_uniform_index);
654
655  int y_texture_location() const { return y_texture_location_; }
656  int u_texture_location() const { return u_texture_location_; }
657  int v_texture_location() const { return v_texture_location_; }
658  int a_texture_location() const { return a_texture_location_; }
659  int alpha_location() const { return alpha_location_; }
660  int yuv_matrix_location() const { return yuv_matrix_location_; }
661  int yuv_adj_location() const { return yuv_adj_location_; }
662
663 private:
664  int y_texture_location_;
665  int u_texture_location_;
666  int v_texture_location_;
667  int a_texture_location_;
668  int alpha_location_;
669  int yuv_matrix_location_;
670  int yuv_adj_location_;
671
672  DISALLOW_COPY_AND_ASSIGN(FragmentShaderYUVAVideo);
673};
674
675class FragmentShaderColor {
676 public:
677  FragmentShaderColor();
678  std::string GetShaderString(
679      TexCoordPrecision precision, SamplerType sampler) const;
680
681  void Init(gpu::gles2::GLES2Interface* context,
682            unsigned program,
683            int* base_uniform_index);
684  int color_location() const { return color_location_; }
685
686 private:
687  int color_location_;
688
689  DISALLOW_COPY_AND_ASSIGN(FragmentShaderColor);
690};
691
692class FragmentShaderColorAA {
693 public:
694  FragmentShaderColorAA();
695  std::string GetShaderString(
696      TexCoordPrecision precision, SamplerType sampler) const;
697
698  void Init(gpu::gles2::GLES2Interface* context,
699            unsigned program,
700            int* base_uniform_index);
701  int color_location() const { return color_location_; }
702
703 private:
704  int color_location_;
705
706  DISALLOW_COPY_AND_ASSIGN(FragmentShaderColorAA);
707};
708
709class FragmentShaderCheckerboard {
710 public:
711  FragmentShaderCheckerboard();
712  std::string GetShaderString(
713      TexCoordPrecision precision, SamplerType sampler) const;
714
715  void Init(gpu::gles2::GLES2Interface* context,
716            unsigned program,
717            int* base_uniform_index);
718  int alpha_location() const { return alpha_location_; }
719  int tex_transform_location() const { return tex_transform_location_; }
720  int frequency_location() const { return frequency_location_; }
721  int color_location() const { return color_location_; }
722
723 private:
724  int alpha_location_;
725  int tex_transform_location_;
726  int frequency_location_;
727  int color_location_;
728
729  DISALLOW_COPY_AND_ASSIGN(FragmentShaderCheckerboard);
730};
731
732}  // namespace cc
733
734#endif  // CC_OUTPUT_SHADER_H_
735