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.
5 #ifndef CC_OUTPUT_SHADER_H_
6 #define CC_OUTPUT_SHADER_H_
10 #include "base/basictypes.h"
11 #include "cc/base/cc_export.h"
26 enum TexCoordPrecision
{
27 TexCoordPrecisionNA
= 0,
28 TexCoordPrecisionMedium
= 1,
29 TexCoordPrecisionHigh
= 2,
30 NumTexCoordPrecisions
= 3
36 SamplerType2DRect
= 2,
37 SamplerTypeExternalOES
= 3,
62 // Note: The highp_threshold_cache must be provided by the caller to make
63 // the caching multi-thread/context safe in an easy low-overhead manner.
64 // The caller must make sure to clear highp_threshold_cache to 0, so it can be
65 // reinitialized, if a new or different context is used.
66 CC_EXPORT TexCoordPrecision
67 TexCoordPrecisionRequired(gpu::gles2::GLES2Interface
* context
,
68 int* highp_threshold_cache
,
69 int highp_threshold_min
,
70 const gfx::Point
& max_coordinate
);
72 CC_EXPORT TexCoordPrecision
TexCoordPrecisionRequired(
73 gpu::gles2::GLES2Interface
* context
,
74 int *highp_threshold_cache
,
75 int highp_threshold_min
,
76 const gfx::Size
& max_size
);
78 class VertexShaderPosTex
{
82 void Init(gpu::gles2::GLES2Interface
* context
,
84 int* base_uniform_index
);
85 std::string
GetShaderString() const;
87 int matrix_location() const { return matrix_location_
; }
92 DISALLOW_COPY_AND_ASSIGN(VertexShaderPosTex
);
95 class VertexShaderPosTexYUVStretchOffset
{
97 VertexShaderPosTexYUVStretchOffset();
99 void Init(gpu::gles2::GLES2Interface
* context
,
101 int* base_uniform_index
);
102 std::string
GetShaderString() const;
104 int matrix_location() const { return matrix_location_
; }
105 int tex_scale_location() const { return tex_scale_location_
; }
106 int tex_offset_location() const { return tex_offset_location_
; }
109 int matrix_location_
;
110 int tex_scale_location_
;
111 int tex_offset_location_
;
113 DISALLOW_COPY_AND_ASSIGN(VertexShaderPosTexYUVStretchOffset
);
116 class VertexShaderPos
{
120 void Init(gpu::gles2::GLES2Interface
* context
,
122 int* base_uniform_index
);
123 std::string
GetShaderString() const;
125 int matrix_location() const { return matrix_location_
; }
128 int matrix_location_
;
130 DISALLOW_COPY_AND_ASSIGN(VertexShaderPos
);
133 class VertexShaderPosTexIdentity
{
135 void Init(gpu::gles2::GLES2Interface
* context
,
137 int* base_uniform_index
) {}
138 std::string
GetShaderString() const;
141 class VertexShaderPosTexTransform
{
143 VertexShaderPosTexTransform();
145 void Init(gpu::gles2::GLES2Interface
* context
,
147 int* base_uniform_index
);
148 std::string
GetShaderString() const;
150 int matrix_location() const { return matrix_location_
; }
151 int tex_transform_location() const { return tex_transform_location_
; }
152 int vertex_opacity_location() const { return vertex_opacity_location_
; }
155 int matrix_location_
;
156 int tex_transform_location_
;
157 int vertex_opacity_location_
;
159 DISALLOW_COPY_AND_ASSIGN(VertexShaderPosTexTransform
);
162 class VertexShaderQuad
{
166 void Init(gpu::gles2::GLES2Interface
* context
,
168 int* base_uniform_index
);
169 std::string
GetShaderString() const;
171 int matrix_location() const { return matrix_location_
; }
172 int viewport_location() const { return -1; }
173 int quad_location() const { return quad_location_
; }
174 int edge_location() const { return -1; }
177 int matrix_location_
;
180 DISALLOW_COPY_AND_ASSIGN(VertexShaderQuad
);
183 class VertexShaderQuadAA
{
185 VertexShaderQuadAA();
187 void Init(gpu::gles2::GLES2Interface
* context
,
189 int* base_uniform_index
);
190 std::string
GetShaderString() const;
192 int matrix_location() const { return matrix_location_
; }
193 int viewport_location() const { return viewport_location_
; }
194 int quad_location() const { return quad_location_
; }
195 int edge_location() const { return edge_location_
; }
198 int matrix_location_
;
199 int viewport_location_
;
203 DISALLOW_COPY_AND_ASSIGN(VertexShaderQuadAA
);
207 class VertexShaderQuadTexTransformAA
{
209 VertexShaderQuadTexTransformAA();
211 void Init(gpu::gles2::GLES2Interface
* context
,
213 int* base_uniform_index
);
214 std::string
GetShaderString() const;
216 int matrix_location() const { return matrix_location_
; }
217 int viewport_location() const { return viewport_location_
; }
218 int quad_location() const { return quad_location_
; }
219 int edge_location() const { return edge_location_
; }
220 int tex_transform_location() const { return tex_transform_location_
; }
223 int matrix_location_
;
224 int viewport_location_
;
227 int tex_transform_location_
;
229 DISALLOW_COPY_AND_ASSIGN(VertexShaderQuadTexTransformAA
);
232 class VertexShaderTile
{
236 void Init(gpu::gles2::GLES2Interface
* context
,
238 int* base_uniform_index
);
239 std::string
GetShaderString() const;
241 int matrix_location() const { return matrix_location_
; }
242 int viewport_location() const { return -1; }
243 int quad_location() const { return quad_location_
; }
244 int edge_location() const { return -1; }
245 int vertex_tex_transform_location() const {
246 return vertex_tex_transform_location_
;
250 int matrix_location_
;
252 int vertex_tex_transform_location_
;
254 DISALLOW_COPY_AND_ASSIGN(VertexShaderTile
);
257 class VertexShaderTileAA
{
259 VertexShaderTileAA();
261 void Init(gpu::gles2::GLES2Interface
* context
,
263 int* base_uniform_index
);
264 std::string
GetShaderString() const;
266 int matrix_location() const { return matrix_location_
; }
267 int viewport_location() const { return viewport_location_
; }
268 int quad_location() const { return quad_location_
; }
269 int edge_location() const { return edge_location_
; }
270 int vertex_tex_transform_location() const {
271 return vertex_tex_transform_location_
;
275 int matrix_location_
;
276 int viewport_location_
;
279 int vertex_tex_transform_location_
;
281 DISALLOW_COPY_AND_ASSIGN(VertexShaderTileAA
);
284 class VertexShaderVideoTransform
{
286 VertexShaderVideoTransform();
288 void Init(gpu::gles2::GLES2Interface
* context
,
290 int* base_uniform_index
);
291 std::string
GetShaderString() const;
293 int matrix_location() const { return matrix_location_
; }
294 int tex_matrix_location() const { return tex_matrix_location_
; }
297 int matrix_location_
;
298 int tex_matrix_location_
;
300 DISALLOW_COPY_AND_ASSIGN(VertexShaderVideoTransform
);
303 class FragmentTexBlendMode
{
305 int backdrop_location() const { return backdrop_location_
; }
306 int backdrop_rect_location() const { return backdrop_rect_location_
; }
308 BlendMode
blend_mode() const { return blend_mode_
; }
309 void set_blend_mode(BlendMode blend_mode
) { blend_mode_
= blend_mode
; }
310 bool has_blend_mode() const { return blend_mode_
!= BlendModeNone
; }
313 FragmentTexBlendMode();
315 std::string
SetBlendModeFunctions(std::string shader_string
) const;
317 int backdrop_location_
;
318 int backdrop_rect_location_
;
321 BlendMode blend_mode_
;
323 std::string
GetHelperFunctions() const;
324 std::string
GetBlendFunction() const;
325 std::string
GetBlendFunctionBodyForRGB() const;
328 class FragmentTexAlphaBinding
: public FragmentTexBlendMode
{
330 FragmentTexAlphaBinding();
332 void Init(gpu::gles2::GLES2Interface
* context
,
334 int* base_uniform_index
);
335 int alpha_location() const { return alpha_location_
; }
336 int fragment_tex_transform_location() const { return -1; }
337 int sampler_location() const { return sampler_location_
; }
340 int sampler_location_
;
343 DISALLOW_COPY_AND_ASSIGN(FragmentTexAlphaBinding
);
346 class FragmentTexColorMatrixAlphaBinding
: public FragmentTexBlendMode
{
348 FragmentTexColorMatrixAlphaBinding();
350 void Init(gpu::gles2::GLES2Interface
* context
,
352 int* base_uniform_index
);
353 int alpha_location() const { return alpha_location_
; }
354 int color_matrix_location() const { return color_matrix_location_
; }
355 int color_offset_location() const { return color_offset_location_
; }
356 int fragment_tex_transform_location() const { return -1; }
357 int sampler_location() const { return sampler_location_
; }
360 int sampler_location_
;
362 int color_matrix_location_
;
363 int color_offset_location_
;
366 class FragmentTexOpaqueBinding
: public FragmentTexBlendMode
{
368 FragmentTexOpaqueBinding();
370 void Init(gpu::gles2::GLES2Interface
* context
,
372 int* base_uniform_index
);
373 int alpha_location() const { return -1; }
374 int fragment_tex_transform_location() const { return -1; }
375 int background_color_location() const { return -1; }
376 int sampler_location() const { return sampler_location_
; }
379 int sampler_location_
;
381 DISALLOW_COPY_AND_ASSIGN(FragmentTexOpaqueBinding
);
384 class FragmentTexBackgroundBinding
: public FragmentTexBlendMode
{
386 FragmentTexBackgroundBinding();
388 void Init(gpu::gles2::GLES2Interface
* context
,
390 int* base_uniform_index
);
391 int background_color_location() const { return background_color_location_
; }
392 int sampler_location() const { return sampler_location_
; }
395 int background_color_location_
;
396 int sampler_location_
;
398 DISALLOW_COPY_AND_ASSIGN(FragmentTexBackgroundBinding
);
401 class FragmentShaderRGBATexVaryingAlpha
: public FragmentTexOpaqueBinding
{
403 std::string
GetShaderString(
404 TexCoordPrecision precision
, SamplerType sampler
) const;
407 class FragmentShaderRGBATexPremultiplyAlpha
: public FragmentTexOpaqueBinding
{
409 std::string
GetShaderString(
410 TexCoordPrecision precision
, SamplerType sampler
) const;
413 class FragmentShaderTexBackgroundVaryingAlpha
414 : public FragmentTexBackgroundBinding
{
416 std::string
GetShaderString(
417 TexCoordPrecision precision
, SamplerType sampler
) const;
420 class FragmentShaderTexBackgroundPremultiplyAlpha
421 : public FragmentTexBackgroundBinding
{
423 std::string
GetShaderString(
424 TexCoordPrecision precision
, SamplerType sampler
) const;
427 class FragmentShaderRGBATexAlpha
: public FragmentTexAlphaBinding
{
429 std::string
GetShaderString(
430 TexCoordPrecision precision
, SamplerType sampler
) const;
433 class FragmentShaderRGBATexColorMatrixAlpha
434 : public FragmentTexColorMatrixAlphaBinding
{
436 std::string
GetShaderString(
437 TexCoordPrecision precision
, SamplerType sampler
) const;
440 class FragmentShaderRGBATexOpaque
: public FragmentTexOpaqueBinding
{
442 std::string
GetShaderString(
443 TexCoordPrecision precision
, SamplerType sampler
) const;
446 class FragmentShaderRGBATex
: public FragmentTexOpaqueBinding
{
448 std::string
GetShaderString(
449 TexCoordPrecision precision
, SamplerType sampler
) const;
452 // Swizzles the red and blue component of sampled texel with alpha.
453 class FragmentShaderRGBATexSwizzleAlpha
: public FragmentTexAlphaBinding
{
455 std::string
GetShaderString(
456 TexCoordPrecision precision
, SamplerType sampler
) const;
459 // Swizzles the red and blue component of sampled texel without alpha.
460 class FragmentShaderRGBATexSwizzleOpaque
: public FragmentTexOpaqueBinding
{
462 std::string
GetShaderString(
463 TexCoordPrecision precision
, SamplerType sampler
) const;
466 class FragmentShaderRGBATexAlphaAA
: public FragmentTexBlendMode
{
468 FragmentShaderRGBATexAlphaAA();
470 void Init(gpu::gles2::GLES2Interface
* context
,
472 int* base_uniform_index
);
473 std::string
GetShaderString(
474 TexCoordPrecision precision
, SamplerType sampler
) const;
476 int alpha_location() const { return alpha_location_
; }
477 int sampler_location() const { return sampler_location_
; }
480 int sampler_location_
;
483 DISALLOW_COPY_AND_ASSIGN(FragmentShaderRGBATexAlphaAA
);
486 class FragmentTexClampAlphaAABinding
: public FragmentTexBlendMode
{
488 FragmentTexClampAlphaAABinding();
490 void Init(gpu::gles2::GLES2Interface
* context
,
492 int* base_uniform_index
);
493 int alpha_location() const { return alpha_location_
; }
494 int sampler_location() const { return sampler_location_
; }
495 int fragment_tex_transform_location() const {
496 return fragment_tex_transform_location_
;
500 int sampler_location_
;
502 int fragment_tex_transform_location_
;
504 DISALLOW_COPY_AND_ASSIGN(FragmentTexClampAlphaAABinding
);
507 class FragmentShaderRGBATexClampAlphaAA
508 : public FragmentTexClampAlphaAABinding
{
510 std::string
GetShaderString(
511 TexCoordPrecision precision
, SamplerType sampler
) const;
514 // Swizzles the red and blue component of sampled texel.
515 class FragmentShaderRGBATexClampSwizzleAlphaAA
516 : public FragmentTexClampAlphaAABinding
{
518 std::string
GetShaderString(
519 TexCoordPrecision precision
, SamplerType sampler
) const;
522 class FragmentShaderRGBATexAlphaMask
: public FragmentTexBlendMode
{
524 FragmentShaderRGBATexAlphaMask();
525 std::string
GetShaderString(
526 TexCoordPrecision precision
, SamplerType sampler
) const;
528 void Init(gpu::gles2::GLES2Interface
* context
,
530 int* base_uniform_index
);
531 int alpha_location() const { return alpha_location_
; }
532 int sampler_location() const { return sampler_location_
; }
533 int mask_sampler_location() const { return mask_sampler_location_
; }
534 int mask_tex_coord_scale_location() const {
535 return mask_tex_coord_scale_location_
;
537 int mask_tex_coord_offset_location() const {
538 return mask_tex_coord_offset_location_
;
542 int sampler_location_
;
543 int mask_sampler_location_
;
545 int mask_tex_coord_scale_location_
;
546 int mask_tex_coord_offset_location_
;
548 DISALLOW_COPY_AND_ASSIGN(FragmentShaderRGBATexAlphaMask
);
551 class FragmentShaderRGBATexAlphaMaskAA
: public FragmentTexBlendMode
{
553 FragmentShaderRGBATexAlphaMaskAA();
554 std::string
GetShaderString(
555 TexCoordPrecision precision
, SamplerType sampler
) const;
557 void Init(gpu::gles2::GLES2Interface
* context
,
559 int* base_uniform_index
);
560 int alpha_location() const { return alpha_location_
; }
561 int sampler_location() const { return sampler_location_
; }
562 int mask_sampler_location() const { return mask_sampler_location_
; }
563 int mask_tex_coord_scale_location() const {
564 return mask_tex_coord_scale_location_
;
566 int mask_tex_coord_offset_location() const {
567 return mask_tex_coord_offset_location_
;
571 int sampler_location_
;
572 int mask_sampler_location_
;
574 int mask_tex_coord_scale_location_
;
575 int mask_tex_coord_offset_location_
;
577 DISALLOW_COPY_AND_ASSIGN(FragmentShaderRGBATexAlphaMaskAA
);
580 class FragmentShaderRGBATexAlphaMaskColorMatrixAA
581 : public FragmentTexBlendMode
{
583 FragmentShaderRGBATexAlphaMaskColorMatrixAA();
584 std::string
GetShaderString(
585 TexCoordPrecision precision
, SamplerType sampler
) const;
587 void Init(gpu::gles2::GLES2Interface
* context
,
589 int* base_uniform_index
);
590 int alpha_location() const { return alpha_location_
; }
591 int sampler_location() const { return sampler_location_
; }
592 int mask_sampler_location() const { return mask_sampler_location_
; }
593 int mask_tex_coord_scale_location() const {
594 return mask_tex_coord_scale_location_
;
596 int mask_tex_coord_offset_location() const {
597 return mask_tex_coord_offset_location_
;
599 int color_matrix_location() const { return color_matrix_location_
; }
600 int color_offset_location() const { return color_offset_location_
; }
603 int sampler_location_
;
604 int mask_sampler_location_
;
606 int mask_tex_coord_scale_location_
;
607 int mask_tex_coord_offset_location_
;
608 int color_matrix_location_
;
609 int color_offset_location_
;
612 class FragmentShaderRGBATexAlphaColorMatrixAA
: public FragmentTexBlendMode
{
614 FragmentShaderRGBATexAlphaColorMatrixAA();
615 std::string
GetShaderString(
616 TexCoordPrecision precision
, SamplerType sampler
) const;
618 void Init(gpu::gles2::GLES2Interface
* context
,
620 int* base_uniform_index
);
621 int alpha_location() const { return alpha_location_
; }
622 int sampler_location() const { return sampler_location_
; }
623 int color_matrix_location() const { return color_matrix_location_
; }
624 int color_offset_location() const { return color_offset_location_
; }
627 int sampler_location_
;
629 int color_matrix_location_
;
630 int color_offset_location_
;
633 class FragmentShaderRGBATexAlphaMaskColorMatrix
: public FragmentTexBlendMode
{
635 FragmentShaderRGBATexAlphaMaskColorMatrix();
636 std::string
GetShaderString(
637 TexCoordPrecision precision
, SamplerType sampler
) const;
639 void Init(gpu::gles2::GLES2Interface
* context
,
641 int* base_uniform_index
);
642 int alpha_location() const { return alpha_location_
; }
643 int sampler_location() const { return sampler_location_
; }
644 int mask_sampler_location() const { return mask_sampler_location_
; }
645 int mask_tex_coord_scale_location() const {
646 return mask_tex_coord_scale_location_
;
648 int mask_tex_coord_offset_location() const {
649 return mask_tex_coord_offset_location_
;
651 int color_matrix_location() const { return color_matrix_location_
; }
652 int color_offset_location() const { return color_offset_location_
; }
655 int sampler_location_
;
656 int mask_sampler_location_
;
658 int mask_tex_coord_scale_location_
;
659 int mask_tex_coord_offset_location_
;
660 int color_matrix_location_
;
661 int color_offset_location_
;
664 class FragmentShaderYUVVideo
: public FragmentTexBlendMode
{
666 FragmentShaderYUVVideo();
667 std::string
GetShaderString(
668 TexCoordPrecision precision
, SamplerType sampler
) const;
670 void Init(gpu::gles2::GLES2Interface
* context
,
672 int* base_uniform_index
);
673 int y_texture_location() const { return y_texture_location_
; }
674 int u_texture_location() const { return u_texture_location_
; }
675 int v_texture_location() const { return v_texture_location_
; }
676 int alpha_location() const { return alpha_location_
; }
677 int yuv_matrix_location() const { return yuv_matrix_location_
; }
678 int yuv_adj_location() const { return yuv_adj_location_
; }
681 int y_texture_location_
;
682 int u_texture_location_
;
683 int v_texture_location_
;
685 int yuv_matrix_location_
;
686 int yuv_adj_location_
;
688 DISALLOW_COPY_AND_ASSIGN(FragmentShaderYUVVideo
);
691 class FragmentShaderYUVAVideo
: public FragmentTexBlendMode
{
693 FragmentShaderYUVAVideo();
694 std::string
GetShaderString(
695 TexCoordPrecision precision
, SamplerType sampler
) const;
697 void Init(gpu::gles2::GLES2Interface
* context
,
699 int* base_uniform_index
);
701 int y_texture_location() const { return y_texture_location_
; }
702 int u_texture_location() const { return u_texture_location_
; }
703 int v_texture_location() const { return v_texture_location_
; }
704 int a_texture_location() const { return a_texture_location_
; }
705 int alpha_location() const { return alpha_location_
; }
706 int yuv_matrix_location() const { return yuv_matrix_location_
; }
707 int yuv_adj_location() const { return yuv_adj_location_
; }
710 int y_texture_location_
;
711 int u_texture_location_
;
712 int v_texture_location_
;
713 int a_texture_location_
;
715 int yuv_matrix_location_
;
716 int yuv_adj_location_
;
718 DISALLOW_COPY_AND_ASSIGN(FragmentShaderYUVAVideo
);
721 class FragmentShaderColor
: public FragmentTexBlendMode
{
723 FragmentShaderColor();
724 std::string
GetShaderString(
725 TexCoordPrecision precision
, SamplerType sampler
) const;
727 void Init(gpu::gles2::GLES2Interface
* context
,
729 int* base_uniform_index
);
730 int color_location() const { return color_location_
; }
735 DISALLOW_COPY_AND_ASSIGN(FragmentShaderColor
);
738 class FragmentShaderColorAA
: public FragmentTexBlendMode
{
740 FragmentShaderColorAA();
741 std::string
GetShaderString(
742 TexCoordPrecision precision
, SamplerType sampler
) const;
744 void Init(gpu::gles2::GLES2Interface
* context
,
746 int* base_uniform_index
);
747 int color_location() const { return color_location_
; }
752 DISALLOW_COPY_AND_ASSIGN(FragmentShaderColorAA
);
755 class FragmentShaderCheckerboard
: public FragmentTexBlendMode
{
757 FragmentShaderCheckerboard();
758 std::string
GetShaderString(
759 TexCoordPrecision precision
, SamplerType sampler
) const;
761 void Init(gpu::gles2::GLES2Interface
* context
,
763 int* base_uniform_index
);
764 int alpha_location() const { return alpha_location_
; }
765 int tex_transform_location() const { return tex_transform_location_
; }
766 int frequency_location() const { return frequency_location_
; }
767 int color_location() const { return color_location_
; }
771 int tex_transform_location_
;
772 int frequency_location_
;
775 DISALLOW_COPY_AND_ASSIGN(FragmentShaderCheckerboard
);
780 #endif // CC_OUTPUT_SHADER_H_