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 #include "cc/output/shader.h"
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "cc/output/gl_renderer.h" // For the GLC() macro.
12 #include "gpu/command_buffer/client/gles2_interface.h"
14 template <size_t size
>
15 std::string
StripLambda(const char(&shader
)[size
]) {
16 // Must contain at least "[]() {}" and trailing null (included in size).
17 static_assert(size
>= 8,
18 "String passed to StripLambda must be at least 8 characters");
19 DCHECK_EQ(strncmp("[]() {", shader
, 6), 0);
20 DCHECK_EQ(shader
[size
- 2], '}');
21 return std::string(shader
+ 6, shader
+ size
- 2);
24 // Shaders are passed in with lambda syntax, which tricks clang-format into
25 // handling them correctly. StipLambda removes this.
26 #define SHADER0(Src) StripLambda(#Src)
27 #define VERTEX_SHADER(Head, Body) SetVertexTexCoordPrecision(Head + Body)
28 #define FRAGMENT_SHADER(Head, Body) \
29 SetFragmentTexCoordPrecision( \
31 SetFragmentSamplerType(sampler, SetBlendModeFunctions(Head + Body)))
33 using gpu::gles2::GLES2Interface
;
39 static void GetProgramUniformLocations(GLES2Interface
* context
,
42 const char** uniforms
,
44 int* base_uniform_index
) {
45 for (size_t i
= 0; i
< count
; i
++) {
46 locations
[i
] = (*base_uniform_index
)++;
47 context
->BindUniformLocationCHROMIUM(program
, locations
[i
], uniforms
[i
]);
51 static std::string
SetFragmentTexCoordPrecision(
52 TexCoordPrecision requested_precision
,
53 std::string shader_string
) {
54 switch (requested_precision
) {
55 case TEX_COORD_PRECISION_HIGH
:
56 DCHECK_NE(shader_string
.find("TexCoordPrecision"), std::string::npos
);
57 return "#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
58 " #define TexCoordPrecision highp\n"
60 " #define TexCoordPrecision mediump\n"
63 case TEX_COORD_PRECISION_MEDIUM
:
64 DCHECK_NE(shader_string
.find("TexCoordPrecision"), std::string::npos
);
65 return "#define TexCoordPrecision mediump\n" + shader_string
;
66 case TEX_COORD_PRECISION_NA
:
67 DCHECK_EQ(shader_string
.find("TexCoordPrecision"), std::string::npos
);
68 DCHECK_EQ(shader_string
.find("texture2D"), std::string::npos
);
69 DCHECK_EQ(shader_string
.find("texture2DRect"), std::string::npos
);
78 static std::string
SetVertexTexCoordPrecision(
79 const std::string
& shader_string
) {
80 // We unconditionally use highp in the vertex shader since
81 // we are unlikely to be vertex shader bound when drawing large quads.
82 // Also, some vertex shaders mutate the texture coordinate in such a
83 // way that the effective precision might be lower than expected.
84 return "#define TexCoordPrecision highp\n" + shader_string
;
87 TexCoordPrecision
TexCoordPrecisionRequired(GLES2Interface
* context
,
88 int* highp_threshold_cache
,
89 int highp_threshold_min
,
92 if (*highp_threshold_cache
== 0) {
93 // Initialize range and precision with minimum spec values for when
94 // GetShaderPrecisionFormat is a test stub.
95 // TODO(brianderson): Implement better stubs of GetShaderPrecisionFormat
97 GLint range
[2] = {14, 14};
100 context
->GetShaderPrecisionFormat(
101 GL_FRAGMENT_SHADER
, GL_MEDIUM_FLOAT
, range
, &precision
));
102 *highp_threshold_cache
= 1 << precision
;
105 int highp_threshold
= std::max(*highp_threshold_cache
, highp_threshold_min
);
106 if (x
> highp_threshold
|| y
> highp_threshold
)
107 return TEX_COORD_PRECISION_HIGH
;
108 return TEX_COORD_PRECISION_MEDIUM
;
111 static std::string
SetFragmentSamplerType(SamplerType requested_type
,
112 std::string shader_string
) {
113 switch (requested_type
) {
114 case SAMPLER_TYPE_2D
:
115 DCHECK_NE(shader_string
.find("SamplerType"), std::string::npos
);
116 DCHECK_NE(shader_string
.find("TextureLookup"), std::string::npos
);
117 return "#define SamplerType sampler2D\n"
118 "#define TextureLookup texture2D\n" +
120 case SAMPLER_TYPE_2D_RECT
:
121 DCHECK_NE(shader_string
.find("SamplerType"), std::string::npos
);
122 DCHECK_NE(shader_string
.find("TextureLookup"), std::string::npos
);
123 return "#extension GL_ARB_texture_rectangle : require\n"
124 "#define SamplerType sampler2DRect\n"
125 "#define TextureLookup texture2DRect\n" +
127 case SAMPLER_TYPE_EXTERNAL_OES
:
128 DCHECK_NE(shader_string
.find("SamplerType"), std::string::npos
);
129 DCHECK_NE(shader_string
.find("TextureLookup"), std::string::npos
);
130 return "#extension GL_OES_EGL_image_external : require\n"
131 "#define SamplerType samplerExternalOES\n"
132 "#define TextureLookup texture2D\n" +
134 case SAMPLER_TYPE_NA
:
135 DCHECK_EQ(shader_string
.find("SamplerType"), std::string::npos
);
136 DCHECK_EQ(shader_string
.find("TextureLookup"), std::string::npos
);
137 return shader_string
;
142 return shader_string
;
147 ShaderLocations::ShaderLocations() {
150 TexCoordPrecision
TexCoordPrecisionRequired(GLES2Interface
* context
,
151 int* highp_threshold_cache
,
152 int highp_threshold_min
,
153 const gfx::Point
& max_coordinate
) {
154 return TexCoordPrecisionRequired(context
,
155 highp_threshold_cache
,
161 TexCoordPrecision
TexCoordPrecisionRequired(GLES2Interface
* context
,
162 int* highp_threshold_cache
,
163 int highp_threshold_min
,
164 const gfx::Size
& max_size
) {
165 return TexCoordPrecisionRequired(context
,
166 highp_threshold_cache
,
172 VertexShaderPosTex::VertexShaderPosTex() : matrix_location_(-1) {
175 void VertexShaderPosTex::Init(GLES2Interface
* context
,
177 int* base_uniform_index
) {
178 static const char* uniforms
[] = {
181 int locations
[arraysize(uniforms
)];
183 GetProgramUniformLocations(context
,
189 matrix_location_
= locations
[0];
192 std::string
VertexShaderPosTex::GetShaderString() const {
193 return VERTEX_SHADER(GetShaderHead(), GetShaderBody());
196 std::string
VertexShaderPosTex::GetShaderHead() {
197 return SHADER0([]() {
198 attribute vec4 a_position
;
199 attribute TexCoordPrecision vec2 a_texCoord
;
201 varying TexCoordPrecision vec2 v_texCoord
;
205 std::string
VertexShaderPosTex::GetShaderBody() {
206 return SHADER0([]() {
208 gl_Position
= matrix
* a_position
;
209 v_texCoord
= a_texCoord
;
214 VertexShaderPosTexYUVStretchOffset::VertexShaderPosTexYUVStretchOffset()
215 : matrix_location_(-1), tex_scale_location_(-1), tex_offset_location_(-1) {
218 void VertexShaderPosTexYUVStretchOffset::Init(GLES2Interface
* context
,
220 int* base_uniform_index
) {
221 static const char* uniforms
[] = {
222 "matrix", "texScale", "texOffset",
224 int locations
[arraysize(uniforms
)];
226 GetProgramUniformLocations(context
,
232 matrix_location_
= locations
[0];
233 tex_scale_location_
= locations
[1];
234 tex_offset_location_
= locations
[2];
237 std::string
VertexShaderPosTexYUVStretchOffset::GetShaderString() const {
238 return VERTEX_SHADER(GetShaderHead(), GetShaderBody());
241 std::string
VertexShaderPosTexYUVStretchOffset::GetShaderHead() {
242 return SHADER0([]() {
243 precision mediump
float;
244 attribute vec4 a_position
;
245 attribute TexCoordPrecision vec2 a_texCoord
;
247 varying TexCoordPrecision vec2 v_texCoord
;
248 uniform TexCoordPrecision vec2 texScale
;
249 uniform TexCoordPrecision vec2 texOffset
;
253 std::string
VertexShaderPosTexYUVStretchOffset::GetShaderBody() {
254 return SHADER0([]() {
256 gl_Position
= matrix
* a_position
;
257 v_texCoord
= a_texCoord
* texScale
+ texOffset
;
262 VertexShaderPos::VertexShaderPos() : matrix_location_(-1) {
265 void VertexShaderPos::Init(GLES2Interface
* context
,
267 int* base_uniform_index
) {
268 static const char* uniforms
[] = {
271 int locations
[arraysize(uniforms
)];
273 GetProgramUniformLocations(context
,
279 matrix_location_
= locations
[0];
282 std::string
VertexShaderPos::GetShaderString() const {
283 return VERTEX_SHADER(GetShaderHead(), GetShaderBody());
286 std::string
VertexShaderPos::GetShaderHead() {
287 return SHADER0([]() {
288 attribute vec4 a_position
;
293 std::string
VertexShaderPos::GetShaderBody() {
294 return SHADER0([]() {
295 void main() { gl_Position
= matrix
* a_position
; }
299 VertexShaderPosTexTransform::VertexShaderPosTexTransform()
300 : matrix_location_(-1),
301 tex_transform_location_(-1),
302 vertex_opacity_location_(-1) {
305 void VertexShaderPosTexTransform::Init(GLES2Interface
* context
,
307 int* base_uniform_index
) {
308 static const char* uniforms
[] = {
309 "matrix", "texTransform", "opacity",
311 int locations
[arraysize(uniforms
)];
313 GetProgramUniformLocations(context
,
319 matrix_location_
= locations
[0];
320 tex_transform_location_
= locations
[1];
321 vertex_opacity_location_
= locations
[2];
324 std::string
VertexShaderPosTexTransform::GetShaderString() const {
325 return VERTEX_SHADER(GetShaderHead(), GetShaderBody());
328 std::string
VertexShaderPosTexTransform::GetShaderHead() {
329 return SHADER0([]() {
330 attribute vec4 a_position
;
331 attribute TexCoordPrecision vec2 a_texCoord
;
332 attribute
float a_index
;
333 uniform mat4 matrix
[8];
334 uniform TexCoordPrecision vec4 texTransform
[8];
335 uniform
float opacity
[32];
336 varying TexCoordPrecision vec2 v_texCoord
;
337 varying
float v_alpha
;
341 std::string
VertexShaderPosTexTransform::GetShaderBody() {
342 return SHADER0([]() {
344 int quad_index
= int(a_index
* 0.25); // NOLINT
345 gl_Position
= matrix
[quad_index
] * a_position
;
346 TexCoordPrecision vec4 texTrans
= texTransform
[quad_index
];
347 v_texCoord
= a_texCoord
* texTrans
.zw
+ texTrans
.xy
;
348 v_alpha
= opacity
[int(a_index
)]; // NOLINT
353 void VertexShaderPosTexTransform::FillLocations(
354 ShaderLocations
* locations
) const {
355 locations
->matrix
= matrix_location();
356 locations
->tex_transform
= tex_transform_location();
359 std::string
VertexShaderPosTexIdentity::GetShaderString() const {
360 return VERTEX_SHADER(GetShaderHead(), GetShaderBody());
363 std::string
VertexShaderPosTexIdentity::GetShaderHead() {
364 return SHADER0([]() {
365 attribute vec4 a_position
;
366 varying TexCoordPrecision vec2 v_texCoord
;
370 std::string
VertexShaderPosTexIdentity::GetShaderBody() {
371 return SHADER0([]() {
373 gl_Position
= a_position
;
374 v_texCoord
= (a_position
.xy
+ vec2(1.0)) * 0.5;
379 VertexShaderQuad::VertexShaderQuad()
380 : matrix_location_(-1), quad_location_(-1) {
383 void VertexShaderQuad::Init(GLES2Interface
* context
,
385 int* base_uniform_index
) {
386 static const char* uniforms
[] = {
389 int locations
[arraysize(uniforms
)];
391 GetProgramUniformLocations(context
,
397 matrix_location_
= locations
[0];
398 quad_location_
= locations
[1];
401 std::string
VertexShaderQuad::GetShaderString() const {
402 return VERTEX_SHADER(GetShaderHead(), GetShaderBody());
405 std::string
VertexShaderQuad::GetShaderHead() {
406 #if defined(OS_ANDROID)
407 // TODO(epenner): Find the cause of this 'quad' uniform
408 // being missing if we don't add dummy variables.
409 // http://crbug.com/240602
410 return SHADER0([]() {
411 attribute TexCoordPrecision vec4 a_position
;
412 attribute
float a_index
;
414 uniform TexCoordPrecision vec2 quad
[4];
415 uniform TexCoordPrecision vec2 dummy_uniform
;
416 varying TexCoordPrecision vec2 dummy_varying
;
419 return SHADER0([]() {
420 attribute TexCoordPrecision vec4 a_position
;
421 attribute
float a_index
;
423 uniform TexCoordPrecision vec2 quad
[4];
428 std::string
VertexShaderQuad::GetShaderBody() {
429 #if defined(OS_ANDROID)
430 return SHADER0([]() {
432 vec2 pos
= quad
[int(a_index
)]; // NOLINT
433 gl_Position
= matrix
* vec4(pos
, a_position
.z
, a_position
.w
);
434 dummy_varying
= dummy_uniform
;
438 return SHADER0([]() {
440 vec2 pos
= quad
[int(a_index
)]; // NOLINT
441 gl_Position
= matrix
* vec4(pos
, a_position
.z
, a_position
.w
);
447 VertexShaderQuadAA::VertexShaderQuadAA()
448 : matrix_location_(-1),
449 viewport_location_(-1),
454 void VertexShaderQuadAA::Init(GLES2Interface
* context
,
456 int* base_uniform_index
) {
457 static const char* uniforms
[] = {
458 "matrix", "viewport", "quad", "edge",
460 int locations
[arraysize(uniforms
)];
462 GetProgramUniformLocations(context
,
468 matrix_location_
= locations
[0];
469 viewport_location_
= locations
[1];
470 quad_location_
= locations
[2];
471 edge_location_
= locations
[3];
474 std::string
VertexShaderQuadAA::GetShaderString() const {
475 return VERTEX_SHADER(GetShaderHead(), GetShaderBody());
478 std::string
VertexShaderQuadAA::GetShaderHead() {
479 return SHADER0([]() {
480 attribute TexCoordPrecision vec4 a_position
;
481 attribute
float a_index
;
483 uniform vec4 viewport
;
484 uniform TexCoordPrecision vec2 quad
[4];
485 uniform TexCoordPrecision vec3 edge
[8];
486 varying TexCoordPrecision vec4 edge_dist
[2]; // 8 edge distances.
490 std::string
VertexShaderQuadAA::GetShaderBody() {
491 return SHADER0([]() {
493 vec2 pos
= quad
[int(a_index
)]; // NOLINT
494 gl_Position
= matrix
* vec4(pos
, a_position
.z
, a_position
.w
);
495 vec2 ndc_pos
= 0.5 * (1.0 + gl_Position
.xy
/ gl_Position
.w
);
496 vec3 screen_pos
= vec3(viewport
.xy
+ viewport
.zw
* ndc_pos
, 1.0);
497 edge_dist
[0] = vec4(dot(edge
[0], screen_pos
), dot(edge
[1], screen_pos
),
498 dot(edge
[2], screen_pos
), dot(edge
[3], screen_pos
)) *
500 edge_dist
[1] = vec4(dot(edge
[4], screen_pos
), dot(edge
[5], screen_pos
),
501 dot(edge
[6], screen_pos
), dot(edge
[7], screen_pos
)) *
507 VertexShaderQuadTexTransformAA::VertexShaderQuadTexTransformAA()
508 : matrix_location_(-1),
509 viewport_location_(-1),
512 tex_transform_location_(-1) {
515 void VertexShaderQuadTexTransformAA::Init(GLES2Interface
* context
,
517 int* base_uniform_index
) {
518 static const char* uniforms
[] = {
519 "matrix", "viewport", "quad", "edge", "texTrans",
521 int locations
[arraysize(uniforms
)];
523 GetProgramUniformLocations(context
,
529 matrix_location_
= locations
[0];
530 viewport_location_
= locations
[1];
531 quad_location_
= locations
[2];
532 edge_location_
= locations
[3];
533 tex_transform_location_
= locations
[4];
536 std::string
VertexShaderQuadTexTransformAA::GetShaderString() const {
537 return VERTEX_SHADER(GetShaderHead(), GetShaderBody());
540 std::string
VertexShaderQuadTexTransformAA::GetShaderHead() {
541 return SHADER0([]() {
542 attribute TexCoordPrecision vec4 a_position
;
543 attribute
float a_index
;
545 uniform vec4 viewport
;
546 uniform TexCoordPrecision vec2 quad
[4];
547 uniform TexCoordPrecision vec3 edge
[8];
548 uniform TexCoordPrecision vec4 texTrans
;
549 varying TexCoordPrecision vec2 v_texCoord
;
550 varying TexCoordPrecision vec4 edge_dist
[2]; // 8 edge distances.
554 std::string
VertexShaderQuadTexTransformAA::GetShaderBody() {
555 return SHADER0([]() {
557 vec2 pos
= quad
[int(a_index
)]; // NOLINT
558 gl_Position
= matrix
* vec4(pos
, a_position
.z
, a_position
.w
);
559 vec2 ndc_pos
= 0.5 * (1.0 + gl_Position
.xy
/ gl_Position
.w
);
560 vec3 screen_pos
= vec3(viewport
.xy
+ viewport
.zw
* ndc_pos
, 1.0);
561 edge_dist
[0] = vec4(dot(edge
[0], screen_pos
), dot(edge
[1], screen_pos
),
562 dot(edge
[2], screen_pos
), dot(edge
[3], screen_pos
)) *
564 edge_dist
[1] = vec4(dot(edge
[4], screen_pos
), dot(edge
[5], screen_pos
),
565 dot(edge
[6], screen_pos
), dot(edge
[7], screen_pos
)) *
567 v_texCoord
= (pos
.xy
+ vec2(0.5)) * texTrans
.zw
+ texTrans
.xy
;
572 void VertexShaderQuadTexTransformAA::FillLocations(
573 ShaderLocations
* locations
) const {
574 locations
->quad
= quad_location();
575 locations
->edge
= edge_location();
576 locations
->viewport
= viewport_location();
577 locations
->matrix
= matrix_location();
578 locations
->tex_transform
= tex_transform_location();
582 VertexShaderTile::VertexShaderTile()
583 : matrix_location_(-1),
585 vertex_tex_transform_location_(-1) {
588 void VertexShaderTile::Init(GLES2Interface
* context
,
590 int* base_uniform_index
) {
591 static const char* uniforms
[] = {
592 "matrix", "quad", "vertexTexTransform",
594 int locations
[arraysize(uniforms
)];
596 GetProgramUniformLocations(context
,
602 matrix_location_
= locations
[0];
603 quad_location_
= locations
[1];
604 vertex_tex_transform_location_
= locations
[2];
607 std::string
VertexShaderTile::GetShaderString() const {
608 return VERTEX_SHADER(GetShaderHead(), GetShaderBody());
611 std::string
VertexShaderTile::GetShaderHead() {
612 return SHADER0([]() {
613 attribute TexCoordPrecision vec4 a_position
;
614 attribute TexCoordPrecision vec2 a_texCoord
;
615 attribute
float a_index
;
617 uniform TexCoordPrecision vec2 quad
[4];
618 uniform TexCoordPrecision vec4 vertexTexTransform
;
619 varying TexCoordPrecision vec2 v_texCoord
;
623 std::string
VertexShaderTile::GetShaderBody() {
624 return SHADER0([]() {
626 vec2 pos
= quad
[int(a_index
)]; // NOLINT
627 gl_Position
= matrix
* vec4(pos
, a_position
.z
, a_position
.w
);
628 v_texCoord
= a_texCoord
* vertexTexTransform
.zw
+ vertexTexTransform
.xy
;
633 VertexShaderTileAA::VertexShaderTileAA()
634 : matrix_location_(-1),
635 viewport_location_(-1),
638 vertex_tex_transform_location_(-1) {
641 void VertexShaderTileAA::Init(GLES2Interface
* context
,
643 int* base_uniform_index
) {
644 static const char* uniforms
[] = {
645 "matrix", "viewport", "quad", "edge", "vertexTexTransform",
647 int locations
[arraysize(uniforms
)];
649 GetProgramUniformLocations(context
,
655 matrix_location_
= locations
[0];
656 viewport_location_
= locations
[1];
657 quad_location_
= locations
[2];
658 edge_location_
= locations
[3];
659 vertex_tex_transform_location_
= locations
[4];
662 std::string
VertexShaderTileAA::GetShaderString() const {
663 return VERTEX_SHADER(GetShaderHead(), GetShaderBody());
666 std::string
VertexShaderTileAA::GetShaderHead() {
667 return SHADER0([]() {
668 attribute TexCoordPrecision vec4 a_position
;
669 attribute
float a_index
;
671 uniform vec4 viewport
;
672 uniform TexCoordPrecision vec2 quad
[4];
673 uniform TexCoordPrecision vec3 edge
[8];
674 uniform TexCoordPrecision vec4 vertexTexTransform
;
675 varying TexCoordPrecision vec2 v_texCoord
;
676 varying TexCoordPrecision vec4 edge_dist
[2]; // 8 edge distances.
680 std::string
VertexShaderTileAA::GetShaderBody() {
681 return SHADER0([]() {
683 vec2 pos
= quad
[int(a_index
)]; // NOLINT
684 gl_Position
= matrix
* vec4(pos
, a_position
.z
, a_position
.w
);
685 vec2 ndc_pos
= 0.5 * (1.0 + gl_Position
.xy
/ gl_Position
.w
);
686 vec3 screen_pos
= vec3(viewport
.xy
+ viewport
.zw
* ndc_pos
, 1.0);
687 edge_dist
[0] = vec4(dot(edge
[0], screen_pos
), dot(edge
[1], screen_pos
),
688 dot(edge
[2], screen_pos
), dot(edge
[3], screen_pos
)) *
690 edge_dist
[1] = vec4(dot(edge
[4], screen_pos
), dot(edge
[5], screen_pos
),
691 dot(edge
[6], screen_pos
), dot(edge
[7], screen_pos
)) *
693 v_texCoord
= pos
.xy
* vertexTexTransform
.zw
+ vertexTexTransform
.xy
;
698 VertexShaderVideoTransform::VertexShaderVideoTransform()
699 : matrix_location_(-1), tex_matrix_location_(-1) {
702 void VertexShaderVideoTransform::Init(GLES2Interface
* context
,
704 int* base_uniform_index
) {
705 static const char* uniforms
[] = {
706 "matrix", "texMatrix",
708 int locations
[arraysize(uniforms
)];
710 GetProgramUniformLocations(context
,
716 matrix_location_
= locations
[0];
717 tex_matrix_location_
= locations
[1];
720 std::string
VertexShaderVideoTransform::GetShaderString() const {
721 return VERTEX_SHADER(GetShaderHead(), GetShaderBody());
724 std::string
VertexShaderVideoTransform::GetShaderHead() {
725 return SHADER0([]() {
726 attribute vec4 a_position
;
727 attribute TexCoordPrecision vec2 a_texCoord
;
729 uniform TexCoordPrecision mat4 texMatrix
;
730 varying TexCoordPrecision vec2 v_texCoord
;
734 std::string
VertexShaderVideoTransform::GetShaderBody() {
735 return SHADER0([]() {
737 gl_Position
= matrix
* a_position
;
739 vec2(texMatrix
* vec4(a_texCoord
.x
, 1.0 - a_texCoord
.y
, 0.0, 1.0));
744 #define BLEND_MODE_UNIFORMS "s_backdropTexture", \
745 "s_originalBackdropTexture", \
747 #define UNUSED_BLEND_MODE_UNIFORMS (!has_blend_mode() ? 3 : 0)
748 #define BLEND_MODE_SET_LOCATIONS(X, POS) \
749 if (has_blend_mode()) { \
750 DCHECK_LT(static_cast<size_t>(POS) + 2, arraysize(X)); \
751 backdrop_location_ = locations[POS]; \
752 original_backdrop_location_ = locations[POS + 1]; \
753 backdrop_rect_location_ = locations[POS + 2]; \
756 FragmentTexBlendMode::FragmentTexBlendMode()
757 : backdrop_location_(-1),
758 original_backdrop_location_(-1),
759 backdrop_rect_location_(-1),
760 blend_mode_(BLEND_MODE_NONE
),
761 mask_for_background_(false) {
764 std::string
FragmentTexBlendMode::SetBlendModeFunctions(
765 std::string shader_string
) const {
766 if (shader_string
.find("ApplyBlendMode") == std::string::npos
)
767 return shader_string
;
769 if (!has_blend_mode()) {
770 return "#define ApplyBlendMode(X, Y) (X)\n" + shader_string
;
773 static const std::string kUniforms
= SHADER0([]() {
774 uniform sampler2D s_backdropTexture
;
775 uniform sampler2D s_originalBackdropTexture
;
776 uniform TexCoordPrecision vec4 backdropRect
;
779 std::string mixFunction
;
780 if (mask_for_background()) {
781 mixFunction
= SHADER0([]() {
782 vec4
MixBackdrop(TexCoordPrecision vec2 bgTexCoord
, float mask
) {
783 vec4 backdrop
= texture2D(s_backdropTexture
, bgTexCoord
);
784 vec4 original_backdrop
=
785 texture2D(s_originalBackdropTexture
, bgTexCoord
);
786 return mix(original_backdrop
, backdrop
, mask
);
790 mixFunction
= SHADER0([]() {
791 vec4
MixBackdrop(TexCoordPrecision vec2 bgTexCoord
, float mask
) {
792 return texture2D(s_backdropTexture
, bgTexCoord
);
797 static const std::string kFunctionApplyBlendMode
= SHADER0([]() {
798 vec4
GetBackdropColor(float mask
) {
799 TexCoordPrecision vec2 bgTexCoord
= gl_FragCoord
.xy
- backdropRect
.xy
;
800 bgTexCoord
.x
/= backdropRect
.z
;
801 bgTexCoord
.y
/= backdropRect
.w
;
802 return MixBackdrop(bgTexCoord
, mask
);
805 vec4
ApplyBlendMode(vec4 src
, float mask
) {
806 vec4 dst
= GetBackdropColor(mask
);
807 return Blend(src
, dst
);
811 return "precision mediump float;" + GetHelperFunctions() +
812 GetBlendFunction() + kUniforms
+ mixFunction
+
813 kFunctionApplyBlendMode
+ shader_string
;
816 std::string
FragmentTexBlendMode::GetHelperFunctions() const {
817 static const std::string kFunctionHardLight
= SHADER0([]() {
818 vec3
hardLight(vec4 src
, vec4 dst
) {
821 (2.0 * src
.r
<= src
.a
)
822 ? (2.0 * src
.r
* dst
.r
)
823 : (src
.a
* dst
.a
- 2.0 * (dst
.a
- dst
.r
) * (src
.a
- src
.r
));
825 (2.0 * src
.g
<= src
.a
)
826 ? (2.0 * src
.g
* dst
.g
)
827 : (src
.a
* dst
.a
- 2.0 * (dst
.a
- dst
.g
) * (src
.a
- src
.g
));
829 (2.0 * src
.b
<= src
.a
)
830 ? (2.0 * src
.b
* dst
.b
)
831 : (src
.a
* dst
.a
- 2.0 * (dst
.a
- dst
.b
) * (src
.a
- src
.b
));
832 result
.rgb
+= src
.rgb
* (1.0 - dst
.a
) + dst
.rgb
* (1.0 - src
.a
);
837 static const std::string kFunctionColorDodgeComponent
= SHADER0([]() {
838 float getColorDodgeComponent(float srcc
, float srca
, float dstc
,
841 return srcc
* (1.0 - dsta
);
842 float d
= srca
- srcc
;
844 return srca
* dsta
+ srcc
* (1.0 - dsta
) + dstc
* (1.0 - srca
);
845 d
= min(dsta
, dstc
* srca
/ d
);
846 return d
* srca
+ srcc
* (1.0 - dsta
) + dstc
* (1.0 - srca
);
850 static const std::string kFunctionColorBurnComponent
= SHADER0([]() {
851 float getColorBurnComponent(float srcc
, float srca
, float dstc
,
854 return srca
* dsta
+ srcc
* (1.0 - dsta
) + dstc
* (1.0 - srca
);
856 return dstc
* (1.0 - srca
);
857 float d
= max(0.0, dsta
- (dsta
- dstc
) * srca
/ srcc
);
858 return srca
* d
+ srcc
* (1.0 - dsta
) + dstc
* (1.0 - srca
);
862 static const std::string kFunctionSoftLightComponentPosDstAlpha
=
864 float getSoftLightComponent(float srcc
, float srca
, float dstc
,
866 if (2.0 * srcc
<= srca
) {
867 return (dstc
* dstc
* (srca
- 2.0 * srcc
)) / dsta
+
868 (1.0 - dsta
) * srcc
+ dstc
* (-srca
+ 2.0 * srcc
+ 1.0);
869 } else if (4.0 * dstc
<= dsta
) {
870 float DSqd
= dstc
* dstc
;
871 float DCub
= DSqd
* dstc
;
872 float DaSqd
= dsta
* dsta
;
873 float DaCub
= DaSqd
* dsta
;
874 return (-DaCub
* srcc
+
875 DaSqd
* (srcc
- dstc
* (3.0 * srca
- 6.0 * srcc
- 1.0)) +
876 12.0 * dsta
* DSqd
* (srca
- 2.0 * srcc
) -
877 16.0 * DCub
* (srca
- 2.0 * srcc
)) /
880 return -sqrt(dsta
* dstc
) * (srca
- 2.0 * srcc
) - dsta
* srcc
+
881 dstc
* (srca
- 2.0 * srcc
+ 1.0) + srcc
;
886 static const std::string kFunctionLum
= SHADER0([]() {
887 float luminance(vec3 color
) { return dot(vec3(0.3, 0.59, 0.11), color
); }
889 vec3
set_luminance(vec3 hueSat
, float alpha
, vec3 lumColor
) {
890 float diff
= luminance(lumColor
- hueSat
);
891 vec3 outColor
= hueSat
+ diff
;
892 float outLum
= luminance(outColor
);
893 float minComp
= min(min(outColor
.r
, outColor
.g
), outColor
.b
);
894 float maxComp
= max(max(outColor
.r
, outColor
.g
), outColor
.b
);
895 if (minComp
< 0.0 && outLum
!= minComp
) {
897 ((outColor
- vec3(outLum
, outLum
, outLum
)) * outLum
) /
900 if (maxComp
> alpha
&& maxComp
!= outLum
) {
903 ((outColor
- vec3(outLum
, outLum
, outLum
)) * (alpha
- outLum
)) /
910 static const std::string kFunctionSat
= SHADER0([]() {
911 float saturation(vec3 color
) {
912 return max(max(color
.r
, color
.g
), color
.b
) -
913 min(min(color
.r
, color
.g
), color
.b
);
916 vec3
set_saturation_helper(float minComp
, float midComp
, float maxComp
,
918 if (minComp
< maxComp
) {
921 result
.g
= sat
* (midComp
- minComp
) / (maxComp
- minComp
);
925 return vec3(0, 0, 0);
929 vec3
set_saturation(vec3 hueLumColor
, vec3 satColor
) {
930 float sat
= saturation(satColor
);
931 if (hueLumColor
.r
<= hueLumColor
.g
) {
932 if (hueLumColor
.g
<= hueLumColor
.b
) {
933 hueLumColor
.rgb
= set_saturation_helper(hueLumColor
.r
, hueLumColor
.g
,
935 } else if (hueLumColor
.r
<= hueLumColor
.b
) {
936 hueLumColor
.rbg
= set_saturation_helper(hueLumColor
.r
, hueLumColor
.b
,
939 hueLumColor
.brg
= set_saturation_helper(hueLumColor
.b
, hueLumColor
.r
,
942 } else if (hueLumColor
.r
<= hueLumColor
.b
) {
943 hueLumColor
.grb
= set_saturation_helper(hueLumColor
.g
, hueLumColor
.r
,
945 } else if (hueLumColor
.g
<= hueLumColor
.b
) {
946 hueLumColor
.gbr
= set_saturation_helper(hueLumColor
.g
, hueLumColor
.b
,
949 hueLumColor
.bgr
= set_saturation_helper(hueLumColor
.b
, hueLumColor
.g
,
956 switch (blend_mode_
) {
957 case BLEND_MODE_OVERLAY
:
958 case BLEND_MODE_HARD_LIGHT
:
959 return kFunctionHardLight
;
960 case BLEND_MODE_COLOR_DODGE
:
961 return kFunctionColorDodgeComponent
;
962 case BLEND_MODE_COLOR_BURN
:
963 return kFunctionColorBurnComponent
;
964 case BLEND_MODE_SOFT_LIGHT
:
965 return kFunctionSoftLightComponentPosDstAlpha
;
967 case BLEND_MODE_SATURATION
:
968 return kFunctionLum
+ kFunctionSat
;
969 case BLEND_MODE_COLOR
:
970 case BLEND_MODE_LUMINOSITY
:
973 return std::string();
977 std::string
FragmentTexBlendMode::GetBlendFunction() const {
978 return "vec4 Blend(vec4 src, vec4 dst) {"
980 " result.a = src.a + (1.0 - src.a) * dst.a;" +
981 GetBlendFunctionBodyForRGB() +
986 std::string
FragmentTexBlendMode::GetBlendFunctionBodyForRGB() const {
987 switch (blend_mode_
) {
988 case BLEND_MODE_NORMAL
:
989 return "result.rgb = src.rgb + dst.rgb * (1.0 - src.a);";
990 case BLEND_MODE_SCREEN
:
991 return "result.rgb = src.rgb + (1.0 - src.rgb) * dst.rgb;";
992 case BLEND_MODE_LIGHTEN
:
993 return "result.rgb = max((1.0 - src.a) * dst.rgb + src.rgb,"
994 " (1.0 - dst.a) * src.rgb + dst.rgb);";
995 case BLEND_MODE_OVERLAY
:
996 return "result.rgb = hardLight(dst, src);";
997 case BLEND_MODE_DARKEN
:
998 return "result.rgb = min((1.0 - src.a) * dst.rgb + src.rgb,"
999 " (1.0 - dst.a) * src.rgb + dst.rgb);";
1000 case BLEND_MODE_COLOR_DODGE
:
1001 return "result.r = getColorDodgeComponent(src.r, src.a, dst.r, dst.a);"
1002 "result.g = getColorDodgeComponent(src.g, src.a, dst.g, dst.a);"
1003 "result.b = getColorDodgeComponent(src.b, src.a, dst.b, dst.a);";
1004 case BLEND_MODE_COLOR_BURN
:
1005 return "result.r = getColorBurnComponent(src.r, src.a, dst.r, dst.a);"
1006 "result.g = getColorBurnComponent(src.g, src.a, dst.g, dst.a);"
1007 "result.b = getColorBurnComponent(src.b, src.a, dst.b, dst.a);";
1008 case BLEND_MODE_HARD_LIGHT
:
1009 return "result.rgb = hardLight(src, dst);";
1010 case BLEND_MODE_SOFT_LIGHT
:
1011 return "if (0.0 == dst.a) {"
1012 " result.rgb = src.rgb;"
1014 " result.r = getSoftLightComponent(src.r, src.a, dst.r, dst.a);"
1015 " result.g = getSoftLightComponent(src.g, src.a, dst.g, dst.a);"
1016 " result.b = getSoftLightComponent(src.b, src.a, dst.b, dst.a);"
1018 case BLEND_MODE_DIFFERENCE
:
1019 return "result.rgb = src.rgb + dst.rgb -"
1020 " 2.0 * min(src.rgb * dst.a, dst.rgb * src.a);";
1021 case BLEND_MODE_EXCLUSION
:
1022 return "result.rgb = dst.rgb + src.rgb - 2.0 * dst.rgb * src.rgb;";
1023 case BLEND_MODE_MULTIPLY
:
1024 return "result.rgb = (1.0 - src.a) * dst.rgb +"
1025 " (1.0 - dst.a) * src.rgb + src.rgb * dst.rgb;";
1026 case BLEND_MODE_HUE
:
1027 return "vec4 dstSrcAlpha = dst * src.a;"
1029 " set_luminance(set_saturation(src.rgb * dst.a,"
1030 " dstSrcAlpha.rgb),"
1032 " dstSrcAlpha.rgb);"
1033 "result.rgb += (1.0 - src.a) * dst.rgb + (1.0 - dst.a) * src.rgb;";
1034 case BLEND_MODE_SATURATION
:
1035 return "vec4 dstSrcAlpha = dst * src.a;"
1036 "result.rgb = set_luminance(set_saturation(dstSrcAlpha.rgb,"
1037 " src.rgb * dst.a),"
1039 " dstSrcAlpha.rgb);"
1040 "result.rgb += (1.0 - src.a) * dst.rgb + (1.0 - dst.a) * src.rgb;";
1041 case BLEND_MODE_COLOR
:
1042 return "vec4 srcDstAlpha = src * dst.a;"
1043 "result.rgb = set_luminance(srcDstAlpha.rgb,"
1045 " dst.rgb * src.a);"
1046 "result.rgb += (1.0 - src.a) * dst.rgb + (1.0 - dst.a) * src.rgb;";
1047 case BLEND_MODE_LUMINOSITY
:
1048 return "vec4 srcDstAlpha = src * dst.a;"
1049 "result.rgb = set_luminance(dst.rgb * src.a,"
1051 " srcDstAlpha.rgb);"
1052 "result.rgb += (1.0 - src.a) * dst.rgb + (1.0 - dst.a) * src.rgb;";
1053 case BLEND_MODE_NONE
:
1056 return "result = vec4(1.0, 0.0, 0.0, 1.0);";
1059 FragmentTexAlphaBinding::FragmentTexAlphaBinding()
1060 : sampler_location_(-1), alpha_location_(-1) {
1063 void FragmentTexAlphaBinding::Init(GLES2Interface
* context
,
1065 int* base_uniform_index
) {
1066 static const char* uniforms
[] = {
1067 "s_texture", "alpha", BLEND_MODE_UNIFORMS
,
1069 int locations
[arraysize(uniforms
)];
1071 GetProgramUniformLocations(context
,
1073 arraysize(uniforms
) - UNUSED_BLEND_MODE_UNIFORMS
,
1076 base_uniform_index
);
1077 sampler_location_
= locations
[0];
1078 alpha_location_
= locations
[1];
1079 BLEND_MODE_SET_LOCATIONS(locations
, 2);
1082 FragmentTexColorMatrixAlphaBinding::FragmentTexColorMatrixAlphaBinding()
1083 : sampler_location_(-1),
1084 alpha_location_(-1),
1085 color_matrix_location_(-1),
1086 color_offset_location_(-1) {
1089 void FragmentTexColorMatrixAlphaBinding::Init(GLES2Interface
* context
,
1091 int* base_uniform_index
) {
1092 static const char* uniforms
[] = {
1093 "s_texture", "alpha", "colorMatrix", "colorOffset", BLEND_MODE_UNIFORMS
,
1095 int locations
[arraysize(uniforms
)];
1097 GetProgramUniformLocations(context
,
1099 arraysize(uniforms
) - UNUSED_BLEND_MODE_UNIFORMS
,
1102 base_uniform_index
);
1103 sampler_location_
= locations
[0];
1104 alpha_location_
= locations
[1];
1105 color_matrix_location_
= locations
[2];
1106 color_offset_location_
= locations
[3];
1107 BLEND_MODE_SET_LOCATIONS(locations
, 4);
1110 FragmentTexOpaqueBinding::FragmentTexOpaqueBinding() : sampler_location_(-1) {
1113 void FragmentTexOpaqueBinding::Init(GLES2Interface
* context
,
1115 int* base_uniform_index
) {
1116 static const char* uniforms
[] = {
1119 int locations
[arraysize(uniforms
)];
1121 GetProgramUniformLocations(context
,
1123 arraysize(uniforms
),
1126 base_uniform_index
);
1127 sampler_location_
= locations
[0];
1130 std::string
FragmentShaderRGBATexAlpha::GetShaderString(
1131 TexCoordPrecision precision
,
1132 SamplerType sampler
) const {
1133 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
1136 std::string
FragmentShaderRGBATexAlpha::GetShaderHead() {
1137 return SHADER0([]() {
1138 precision mediump
float;
1139 varying TexCoordPrecision vec2 v_texCoord
;
1140 uniform SamplerType s_texture
;
1141 uniform
float alpha
;
1145 std::string
FragmentShaderRGBATexAlpha::GetShaderBody() {
1146 return SHADER0([]() {
1148 vec4 texColor
= TextureLookup(s_texture
, v_texCoord
);
1149 gl_FragColor
= ApplyBlendMode(texColor
* alpha
, 0.0);
1154 void FragmentShaderRGBATexAlpha::FillLocations(
1155 ShaderLocations
* locations
) const {
1156 locations
->sampler
= sampler_location();
1157 locations
->alpha
= alpha_location();
1158 locations
->backdrop
= backdrop_location();
1159 locations
->backdrop_rect
= backdrop_rect_location();
1162 std::string
FragmentShaderRGBATexColorMatrixAlpha::GetShaderString(
1163 TexCoordPrecision precision
,
1164 SamplerType sampler
) const {
1165 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
1168 std::string
FragmentShaderRGBATexColorMatrixAlpha::GetShaderHead() {
1169 return SHADER0([]() {
1170 precision mediump
float;
1171 varying TexCoordPrecision vec2 v_texCoord
;
1172 uniform SamplerType s_texture
;
1173 uniform
float alpha
;
1174 uniform mat4 colorMatrix
;
1175 uniform vec4 colorOffset
;
1179 std::string
FragmentShaderRGBATexColorMatrixAlpha::GetShaderBody() {
1180 return SHADER0([]() {
1182 vec4 texColor
= TextureLookup(s_texture
, v_texCoord
);
1183 float nonZeroAlpha
= max(texColor
.a
, 0.00001);
1184 texColor
= vec4(texColor
.rgb
/ nonZeroAlpha
, nonZeroAlpha
);
1185 texColor
= colorMatrix
* texColor
+ colorOffset
;
1186 texColor
.rgb
*= texColor
.a
;
1187 texColor
= clamp(texColor
, 0.0, 1.0);
1188 gl_FragColor
= ApplyBlendMode(texColor
* alpha
, 0.0);
1193 void FragmentShaderRGBATexColorMatrixAlpha::FillLocations(
1194 ShaderLocations
* locations
) const {
1195 locations
->sampler
= sampler_location();
1196 locations
->alpha
= alpha_location();
1197 locations
->color_matrix
= color_matrix_location();
1198 locations
->color_offset
= color_offset_location();
1199 locations
->backdrop
= backdrop_location();
1200 locations
->backdrop_rect
= backdrop_rect_location();
1203 std::string
FragmentShaderRGBATexVaryingAlpha::GetShaderString(
1204 TexCoordPrecision precision
,
1205 SamplerType sampler
) const {
1206 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
1209 std::string
FragmentShaderRGBATexVaryingAlpha::GetShaderHead() {
1210 return SHADER0([]() {
1211 precision mediump
float;
1212 varying TexCoordPrecision vec2 v_texCoord
;
1213 varying
float v_alpha
;
1214 uniform SamplerType s_texture
;
1218 std::string
FragmentShaderRGBATexVaryingAlpha::GetShaderBody() {
1219 return SHADER0([]() {
1221 vec4 texColor
= TextureLookup(s_texture
, v_texCoord
);
1222 gl_FragColor
= texColor
* v_alpha
;
1227 std::string
FragmentShaderRGBATexPremultiplyAlpha::GetShaderString(
1228 TexCoordPrecision precision
,
1229 SamplerType sampler
) const {
1230 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
1233 std::string
FragmentShaderRGBATexPremultiplyAlpha::GetShaderHead() {
1234 return SHADER0([]() {
1235 precision mediump
float;
1236 varying TexCoordPrecision vec2 v_texCoord
;
1237 varying
float v_alpha
;
1238 uniform SamplerType s_texture
;
1242 std::string
FragmentShaderRGBATexPremultiplyAlpha::GetShaderBody() {
1243 return SHADER0([]() {
1245 vec4 texColor
= TextureLookup(s_texture
, v_texCoord
);
1246 texColor
.rgb
*= texColor
.a
;
1247 gl_FragColor
= texColor
* v_alpha
;
1252 FragmentTexBackgroundBinding::FragmentTexBackgroundBinding()
1253 : background_color_location_(-1), sampler_location_(-1) {
1256 void FragmentTexBackgroundBinding::Init(GLES2Interface
* context
,
1258 int* base_uniform_index
) {
1259 static const char* uniforms
[] = {
1260 "s_texture", "background_color",
1262 int locations
[arraysize(uniforms
)];
1264 GetProgramUniformLocations(context
,
1266 arraysize(uniforms
),
1269 base_uniform_index
);
1271 sampler_location_
= locations
[0];
1272 DCHECK_NE(sampler_location_
, -1);
1274 background_color_location_
= locations
[1];
1275 DCHECK_NE(background_color_location_
, -1);
1278 std::string
FragmentShaderTexBackgroundVaryingAlpha::GetShaderString(
1279 TexCoordPrecision precision
,
1280 SamplerType sampler
) const {
1281 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
1284 std::string
FragmentShaderTexBackgroundVaryingAlpha::GetShaderHead() {
1285 return SHADER0([]() {
1286 precision mediump
float;
1287 varying TexCoordPrecision vec2 v_texCoord
;
1288 varying
float v_alpha
;
1289 uniform vec4 background_color
;
1290 uniform SamplerType s_texture
;
1294 std::string
FragmentShaderTexBackgroundVaryingAlpha::GetShaderBody() {
1295 return SHADER0([]() {
1297 vec4 texColor
= TextureLookup(s_texture
, v_texCoord
);
1298 texColor
+= background_color
* (1.0 - texColor
.a
);
1299 gl_FragColor
= texColor
* v_alpha
;
1304 std::string
FragmentShaderTexBackgroundPremultiplyAlpha::GetShaderString(
1305 TexCoordPrecision precision
,
1306 SamplerType sampler
) const {
1307 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
1310 std::string
FragmentShaderTexBackgroundPremultiplyAlpha::GetShaderHead() {
1311 return SHADER0([]() {
1312 precision mediump
float;
1313 varying TexCoordPrecision vec2 v_texCoord
;
1314 varying
float v_alpha
;
1315 uniform vec4 background_color
;
1316 uniform SamplerType s_texture
;
1320 std::string
FragmentShaderTexBackgroundPremultiplyAlpha::GetShaderBody() {
1321 return SHADER0([]() {
1323 vec4 texColor
= TextureLookup(s_texture
, v_texCoord
);
1324 texColor
.rgb
*= texColor
.a
;
1325 texColor
+= background_color
* (1.0 - texColor
.a
);
1326 gl_FragColor
= texColor
* v_alpha
;
1331 std::string
FragmentShaderRGBATexOpaque::GetShaderString(
1332 TexCoordPrecision precision
,
1333 SamplerType sampler
) const {
1334 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
1337 std::string
FragmentShaderRGBATexOpaque::GetShaderHead() {
1338 return SHADER0([]() {
1339 precision mediump
float;
1340 varying TexCoordPrecision vec2 v_texCoord
;
1341 uniform SamplerType s_texture
;
1345 std::string
FragmentShaderRGBATexOpaque::GetShaderBody() {
1346 return SHADER0([]() {
1348 vec4 texColor
= TextureLookup(s_texture
, v_texCoord
);
1349 gl_FragColor
= vec4(texColor
.rgb
, 1.0);
1354 std::string
FragmentShaderRGBATex::GetShaderString(TexCoordPrecision precision
,
1355 SamplerType sampler
) const {
1356 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
1359 std::string
FragmentShaderRGBATex::GetShaderHead() {
1360 return SHADER0([]() {
1361 precision mediump
float;
1362 varying TexCoordPrecision vec2 v_texCoord
;
1363 uniform SamplerType s_texture
;
1367 std::string
FragmentShaderRGBATex::GetShaderBody() {
1368 return SHADER0([]() {
1369 void main() { gl_FragColor
= TextureLookup(s_texture
, v_texCoord
); }
1373 std::string
FragmentShaderRGBATexSwizzleAlpha::GetShaderString(
1374 TexCoordPrecision precision
,
1375 SamplerType sampler
) const {
1376 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
1379 std::string
FragmentShaderRGBATexSwizzleAlpha::GetShaderHead() {
1380 return SHADER0([]() {
1381 precision mediump
float;
1382 varying TexCoordPrecision vec2 v_texCoord
;
1383 uniform SamplerType s_texture
;
1384 uniform
float alpha
;
1388 std::string
FragmentShaderRGBATexSwizzleAlpha::GetShaderBody() {
1389 return SHADER0([]() {
1391 vec4 texColor
= TextureLookup(s_texture
, v_texCoord
);
1393 vec4(texColor
.z
, texColor
.y
, texColor
.x
, texColor
.w
) * alpha
;
1398 std::string
FragmentShaderRGBATexSwizzleOpaque::GetShaderString(
1399 TexCoordPrecision precision
,
1400 SamplerType sampler
) const {
1401 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
1404 std::string
FragmentShaderRGBATexSwizzleOpaque::GetShaderHead() {
1405 return SHADER0([]() {
1406 precision mediump
float;
1407 varying TexCoordPrecision vec2 v_texCoord
;
1408 uniform SamplerType s_texture
;
1412 std::string
FragmentShaderRGBATexSwizzleOpaque::GetShaderBody() {
1413 return SHADER0([]() {
1415 vec4 texColor
= TextureLookup(s_texture
, v_texCoord
);
1416 gl_FragColor
= vec4(texColor
.z
, texColor
.y
, texColor
.x
, 1.0);
1421 FragmentShaderRGBATexAlphaAA::FragmentShaderRGBATexAlphaAA()
1422 : sampler_location_(-1), alpha_location_(-1) {
1425 void FragmentShaderRGBATexAlphaAA::Init(GLES2Interface
* context
,
1427 int* base_uniform_index
) {
1428 static const char* uniforms
[] = {
1429 "s_texture", "alpha", BLEND_MODE_UNIFORMS
,
1431 int locations
[arraysize(uniforms
)];
1433 GetProgramUniformLocations(context
,
1435 arraysize(uniforms
) - UNUSED_BLEND_MODE_UNIFORMS
,
1438 base_uniform_index
);
1439 sampler_location_
= locations
[0];
1440 alpha_location_
= locations
[1];
1441 BLEND_MODE_SET_LOCATIONS(locations
, 2);
1444 std::string
FragmentShaderRGBATexAlphaAA::GetShaderString(
1445 TexCoordPrecision precision
,
1446 SamplerType sampler
) const {
1447 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
1450 std::string
FragmentShaderRGBATexAlphaAA::GetShaderHead() {
1451 return SHADER0([]() {
1452 precision mediump
float;
1453 uniform SamplerType s_texture
;
1454 uniform
float alpha
;
1455 varying TexCoordPrecision vec2 v_texCoord
;
1456 varying TexCoordPrecision vec4 edge_dist
[2]; // 8 edge distances.
1460 std::string
FragmentShaderRGBATexAlphaAA::GetShaderBody() {
1461 return SHADER0([]() {
1463 vec4 texColor
= TextureLookup(s_texture
, v_texCoord
);
1464 vec4 d4
= min(edge_dist
[0], edge_dist
[1]);
1465 vec2 d2
= min(d4
.xz
, d4
.yw
);
1466 float aa
= clamp(gl_FragCoord
.w
* min(d2
.x
, d2
.y
), 0.0, 1.0);
1467 gl_FragColor
= ApplyBlendMode(texColor
* alpha
* aa
, 0.0);
1472 void FragmentShaderRGBATexAlphaAA::FillLocations(
1473 ShaderLocations
* locations
) const {
1474 locations
->sampler
= sampler_location();
1475 locations
->alpha
= alpha_location();
1476 locations
->backdrop
= backdrop_location();
1477 locations
->backdrop_rect
= backdrop_rect_location();
1480 FragmentTexClampAlphaAABinding::FragmentTexClampAlphaAABinding()
1481 : sampler_location_(-1),
1482 alpha_location_(-1),
1483 fragment_tex_transform_location_(-1) {
1486 void FragmentTexClampAlphaAABinding::Init(GLES2Interface
* context
,
1488 int* base_uniform_index
) {
1489 static const char* uniforms
[] = {
1490 "s_texture", "alpha", "fragmentTexTransform",
1492 int locations
[arraysize(uniforms
)];
1494 GetProgramUniformLocations(context
,
1496 arraysize(uniforms
),
1499 base_uniform_index
);
1500 sampler_location_
= locations
[0];
1501 alpha_location_
= locations
[1];
1502 fragment_tex_transform_location_
= locations
[2];
1505 std::string
FragmentShaderRGBATexClampAlphaAA::GetShaderString(
1506 TexCoordPrecision precision
,
1507 SamplerType sampler
) const {
1508 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
1511 std::string
FragmentShaderRGBATexClampAlphaAA::GetShaderHead() {
1512 return SHADER0([]() {
1513 precision mediump
float;
1514 uniform SamplerType s_texture
;
1515 uniform
float alpha
;
1516 uniform TexCoordPrecision vec4 fragmentTexTransform
;
1517 varying TexCoordPrecision vec2 v_texCoord
;
1518 varying TexCoordPrecision vec4 edge_dist
[2]; // 8 edge distances.
1522 std::string
FragmentShaderRGBATexClampAlphaAA::GetShaderBody() {
1523 return SHADER0([]() {
1525 TexCoordPrecision vec2 texCoord
=
1526 clamp(v_texCoord
, 0.0, 1.0) * fragmentTexTransform
.zw
+
1527 fragmentTexTransform
.xy
;
1528 vec4 texColor
= TextureLookup(s_texture
, texCoord
);
1529 vec4 d4
= min(edge_dist
[0], edge_dist
[1]);
1530 vec2 d2
= min(d4
.xz
, d4
.yw
);
1531 float aa
= clamp(gl_FragCoord
.w
* min(d2
.x
, d2
.y
), 0.0, 1.0);
1532 gl_FragColor
= texColor
* alpha
* aa
;
1537 std::string
FragmentShaderRGBATexClampSwizzleAlphaAA::GetShaderString(
1538 TexCoordPrecision precision
,
1539 SamplerType sampler
) const {
1540 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
1543 std::string
FragmentShaderRGBATexClampSwizzleAlphaAA::GetShaderHead() {
1544 return SHADER0([]() {
1545 precision mediump
float;
1546 uniform SamplerType s_texture
;
1547 uniform
float alpha
;
1548 uniform TexCoordPrecision vec4 fragmentTexTransform
;
1549 varying TexCoordPrecision vec2 v_texCoord
;
1550 varying TexCoordPrecision vec4 edge_dist
[2]; // 8 edge distances.
1554 std::string
FragmentShaderRGBATexClampSwizzleAlphaAA::GetShaderBody() {
1555 return SHADER0([]() {
1557 TexCoordPrecision vec2 texCoord
=
1558 clamp(v_texCoord
, 0.0, 1.0) * fragmentTexTransform
.zw
+
1559 fragmentTexTransform
.xy
;
1560 vec4 texColor
= TextureLookup(s_texture
, texCoord
);
1561 vec4 d4
= min(edge_dist
[0], edge_dist
[1]);
1562 vec2 d2
= min(d4
.xz
, d4
.yw
);
1563 float aa
= clamp(gl_FragCoord
.w
* min(d2
.x
, d2
.y
), 0.0, 1.0);
1565 vec4(texColor
.z
, texColor
.y
, texColor
.x
, texColor
.w
) * alpha
* aa
;
1570 FragmentShaderRGBATexAlphaMask::FragmentShaderRGBATexAlphaMask()
1571 : sampler_location_(-1),
1572 mask_sampler_location_(-1),
1573 alpha_location_(-1),
1574 mask_tex_coord_scale_location_(-1) {
1577 void FragmentShaderRGBATexAlphaMask::Init(GLES2Interface
* context
,
1579 int* base_uniform_index
) {
1580 static const char* uniforms
[] = {
1584 "maskTexCoordScale",
1585 "maskTexCoordOffset",
1586 BLEND_MODE_UNIFORMS
,
1588 int locations
[arraysize(uniforms
)];
1590 GetProgramUniformLocations(context
,
1592 arraysize(uniforms
) - UNUSED_BLEND_MODE_UNIFORMS
,
1595 base_uniform_index
);
1596 sampler_location_
= locations
[0];
1597 mask_sampler_location_
= locations
[1];
1598 alpha_location_
= locations
[2];
1599 mask_tex_coord_scale_location_
= locations
[3];
1600 mask_tex_coord_offset_location_
= locations
[4];
1601 BLEND_MODE_SET_LOCATIONS(locations
, 5);
1604 std::string
FragmentShaderRGBATexAlphaMask::GetShaderString(
1605 TexCoordPrecision precision
,
1606 SamplerType sampler
) const {
1607 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
1610 std::string
FragmentShaderRGBATexAlphaMask::GetShaderHead() {
1611 return SHADER0([]() {
1612 precision mediump
float;
1613 varying TexCoordPrecision vec2 v_texCoord
;
1614 uniform sampler2D s_texture
;
1615 uniform SamplerType s_mask
;
1616 uniform TexCoordPrecision vec2 maskTexCoordScale
;
1617 uniform TexCoordPrecision vec2 maskTexCoordOffset
;
1618 uniform
float alpha
;
1622 std::string
FragmentShaderRGBATexAlphaMask::GetShaderBody() {
1623 return SHADER0([]() {
1625 vec4 texColor
= texture2D(s_texture
, v_texCoord
);
1626 TexCoordPrecision vec2 maskTexCoord
=
1627 vec2(maskTexCoordOffset
.x
+ v_texCoord
.x
* maskTexCoordScale
.x
,
1628 maskTexCoordOffset
.y
+ v_texCoord
.y
* maskTexCoordScale
.y
);
1629 vec4 maskColor
= TextureLookup(s_mask
, maskTexCoord
);
1630 gl_FragColor
= ApplyBlendMode(
1631 texColor
* alpha
* maskColor
.w
, maskColor
.w
);
1636 void FragmentShaderRGBATexAlphaMask::FillLocations(
1637 ShaderLocations
* locations
) const {
1638 locations
->sampler
= sampler_location();
1639 locations
->mask_sampler
= mask_sampler_location();
1640 locations
->mask_tex_coord_scale
= mask_tex_coord_scale_location();
1641 locations
->mask_tex_coord_offset
= mask_tex_coord_offset_location();
1642 locations
->alpha
= alpha_location();
1643 locations
->backdrop
= backdrop_location();
1644 locations
->backdrop_rect
= backdrop_rect_location();
1645 if (mask_for_background())
1646 locations
->original_backdrop
= original_backdrop_location();
1649 FragmentShaderRGBATexAlphaMaskAA::FragmentShaderRGBATexAlphaMaskAA()
1650 : sampler_location_(-1),
1651 mask_sampler_location_(-1),
1652 alpha_location_(-1),
1653 mask_tex_coord_scale_location_(-1),
1654 mask_tex_coord_offset_location_(-1) {
1657 void FragmentShaderRGBATexAlphaMaskAA::Init(GLES2Interface
* context
,
1659 int* base_uniform_index
) {
1660 static const char* uniforms
[] = {
1664 "maskTexCoordScale",
1665 "maskTexCoordOffset",
1666 BLEND_MODE_UNIFORMS
,
1668 int locations
[arraysize(uniforms
)];
1670 GetProgramUniformLocations(context
,
1672 arraysize(uniforms
) - UNUSED_BLEND_MODE_UNIFORMS
,
1675 base_uniform_index
);
1676 sampler_location_
= locations
[0];
1677 mask_sampler_location_
= locations
[1];
1678 alpha_location_
= locations
[2];
1679 mask_tex_coord_scale_location_
= locations
[3];
1680 mask_tex_coord_offset_location_
= locations
[4];
1681 BLEND_MODE_SET_LOCATIONS(locations
, 5);
1684 std::string
FragmentShaderRGBATexAlphaMaskAA::GetShaderString(
1685 TexCoordPrecision precision
,
1686 SamplerType sampler
) const {
1687 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
1690 std::string
FragmentShaderRGBATexAlphaMaskAA::GetShaderHead() {
1691 return SHADER0([]() {
1692 precision mediump
float;
1693 uniform sampler2D s_texture
;
1694 uniform SamplerType s_mask
;
1695 uniform TexCoordPrecision vec2 maskTexCoordScale
;
1696 uniform TexCoordPrecision vec2 maskTexCoordOffset
;
1697 uniform
float alpha
;
1698 varying TexCoordPrecision vec2 v_texCoord
;
1699 varying TexCoordPrecision vec4 edge_dist
[2]; // 8 edge distances.
1703 std::string
FragmentShaderRGBATexAlphaMaskAA::GetShaderBody() {
1704 return SHADER0([]() {
1706 vec4 texColor
= texture2D(s_texture
, v_texCoord
);
1707 TexCoordPrecision vec2 maskTexCoord
=
1708 vec2(maskTexCoordOffset
.x
+ v_texCoord
.x
* maskTexCoordScale
.x
,
1709 maskTexCoordOffset
.y
+ v_texCoord
.y
* maskTexCoordScale
.y
);
1710 vec4 maskColor
= TextureLookup(s_mask
, maskTexCoord
);
1711 vec4 d4
= min(edge_dist
[0], edge_dist
[1]);
1712 vec2 d2
= min(d4
.xz
, d4
.yw
);
1713 float aa
= clamp(gl_FragCoord
.w
* min(d2
.x
, d2
.y
), 0.0, 1.0);
1714 gl_FragColor
= ApplyBlendMode(
1715 texColor
* alpha
* maskColor
.w
* aa
, maskColor
.w
);
1720 void FragmentShaderRGBATexAlphaMaskAA::FillLocations(
1721 ShaderLocations
* locations
) const {
1722 locations
->sampler
= sampler_location();
1723 locations
->mask_sampler
= mask_sampler_location();
1724 locations
->mask_tex_coord_scale
= mask_tex_coord_scale_location();
1725 locations
->mask_tex_coord_offset
= mask_tex_coord_offset_location();
1726 locations
->alpha
= alpha_location();
1727 locations
->backdrop
= backdrop_location();
1728 locations
->backdrop_rect
= backdrop_rect_location();
1729 if (mask_for_background())
1730 locations
->original_backdrop
= original_backdrop_location();
1733 FragmentShaderRGBATexAlphaMaskColorMatrixAA::
1734 FragmentShaderRGBATexAlphaMaskColorMatrixAA()
1735 : sampler_location_(-1),
1736 mask_sampler_location_(-1),
1737 alpha_location_(-1),
1738 mask_tex_coord_scale_location_(-1),
1739 color_matrix_location_(-1),
1740 color_offset_location_(-1) {
1743 void FragmentShaderRGBATexAlphaMaskColorMatrixAA::Init(
1744 GLES2Interface
* context
,
1746 int* base_uniform_index
) {
1747 static const char* uniforms
[] = {
1751 "maskTexCoordScale",
1752 "maskTexCoordOffset",
1755 BLEND_MODE_UNIFORMS
,
1757 int locations
[arraysize(uniforms
)];
1759 GetProgramUniformLocations(context
,
1761 arraysize(uniforms
) - UNUSED_BLEND_MODE_UNIFORMS
,
1764 base_uniform_index
);
1765 sampler_location_
= locations
[0];
1766 mask_sampler_location_
= locations
[1];
1767 alpha_location_
= locations
[2];
1768 mask_tex_coord_scale_location_
= locations
[3];
1769 mask_tex_coord_offset_location_
= locations
[4];
1770 color_matrix_location_
= locations
[5];
1771 color_offset_location_
= locations
[6];
1772 BLEND_MODE_SET_LOCATIONS(locations
, 7);
1775 std::string
FragmentShaderRGBATexAlphaMaskColorMatrixAA::GetShaderString(
1776 TexCoordPrecision precision
,
1777 SamplerType sampler
) const {
1778 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
1781 std::string
FragmentShaderRGBATexAlphaMaskColorMatrixAA::GetShaderHead() {
1782 return SHADER0([]() {
1783 precision mediump
float;
1784 uniform sampler2D s_texture
;
1785 uniform SamplerType s_mask
;
1786 uniform vec2 maskTexCoordScale
;
1787 uniform vec2 maskTexCoordOffset
;
1788 uniform mat4 colorMatrix
;
1789 uniform vec4 colorOffset
;
1790 uniform
float alpha
;
1791 varying TexCoordPrecision vec2 v_texCoord
;
1792 varying TexCoordPrecision vec4 edge_dist
[2]; // 8 edge distances.
1796 std::string
FragmentShaderRGBATexAlphaMaskColorMatrixAA::GetShaderBody() {
1797 return SHADER0([]() {
1799 vec4 texColor
= texture2D(s_texture
, v_texCoord
);
1800 float nonZeroAlpha
= max(texColor
.a
, 0.00001);
1801 texColor
= vec4(texColor
.rgb
/ nonZeroAlpha
, nonZeroAlpha
);
1802 texColor
= colorMatrix
* texColor
+ colorOffset
;
1803 texColor
.rgb
*= texColor
.a
;
1804 texColor
= clamp(texColor
, 0.0, 1.0);
1805 TexCoordPrecision vec2 maskTexCoord
=
1806 vec2(maskTexCoordOffset
.x
+ v_texCoord
.x
* maskTexCoordScale
.x
,
1807 maskTexCoordOffset
.y
+ v_texCoord
.y
* maskTexCoordScale
.y
);
1808 vec4 maskColor
= TextureLookup(s_mask
, maskTexCoord
);
1809 vec4 d4
= min(edge_dist
[0], edge_dist
[1]);
1810 vec2 d2
= min(d4
.xz
, d4
.yw
);
1811 float aa
= clamp(gl_FragCoord
.w
* min(d2
.x
, d2
.y
), 0.0, 1.0);
1812 gl_FragColor
= ApplyBlendMode(
1813 texColor
* alpha
* maskColor
.w
* aa
, maskColor
.w
);
1818 void FragmentShaderRGBATexAlphaMaskColorMatrixAA::FillLocations(
1819 ShaderLocations
* locations
) const {
1820 locations
->sampler
= sampler_location();
1821 locations
->alpha
= alpha_location();
1822 locations
->mask_sampler
= mask_sampler_location();
1823 locations
->mask_tex_coord_scale
= mask_tex_coord_scale_location();
1824 locations
->mask_tex_coord_offset
= mask_tex_coord_offset_location();
1825 locations
->color_matrix
= color_matrix_location();
1826 locations
->color_offset
= color_offset_location();
1827 locations
->backdrop
= backdrop_location();
1828 locations
->backdrop_rect
= backdrop_rect_location();
1829 if (mask_for_background())
1830 locations
->original_backdrop
= original_backdrop_location();
1833 FragmentShaderRGBATexAlphaColorMatrixAA::
1834 FragmentShaderRGBATexAlphaColorMatrixAA()
1835 : sampler_location_(-1),
1836 alpha_location_(-1),
1837 color_matrix_location_(-1),
1838 color_offset_location_(-1) {
1841 void FragmentShaderRGBATexAlphaColorMatrixAA::Init(GLES2Interface
* context
,
1843 int* base_uniform_index
) {
1844 static const char* uniforms
[] = {
1845 "s_texture", "alpha", "colorMatrix", "colorOffset", BLEND_MODE_UNIFORMS
,
1847 int locations
[arraysize(uniforms
)];
1849 GetProgramUniformLocations(context
,
1851 arraysize(uniforms
) - UNUSED_BLEND_MODE_UNIFORMS
,
1854 base_uniform_index
);
1855 sampler_location_
= locations
[0];
1856 alpha_location_
= locations
[1];
1857 color_matrix_location_
= locations
[2];
1858 color_offset_location_
= locations
[3];
1859 BLEND_MODE_SET_LOCATIONS(locations
, 4);
1862 std::string
FragmentShaderRGBATexAlphaColorMatrixAA::GetShaderString(
1863 TexCoordPrecision precision
,
1864 SamplerType sampler
) const {
1865 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
1868 std::string
FragmentShaderRGBATexAlphaColorMatrixAA::GetShaderHead() {
1869 return SHADER0([]() {
1870 precision mediump
float;
1871 uniform SamplerType s_texture
;
1872 uniform
float alpha
;
1873 uniform mat4 colorMatrix
;
1874 uniform vec4 colorOffset
;
1875 varying TexCoordPrecision vec2 v_texCoord
;
1876 varying TexCoordPrecision vec4 edge_dist
[2]; // 8 edge distances.
1880 std::string
FragmentShaderRGBATexAlphaColorMatrixAA::GetShaderBody() {
1881 return SHADER0([]() {
1883 vec4 texColor
= TextureLookup(s_texture
, v_texCoord
);
1884 float nonZeroAlpha
= max(texColor
.a
, 0.00001);
1885 texColor
= vec4(texColor
.rgb
/ nonZeroAlpha
, nonZeroAlpha
);
1886 texColor
= colorMatrix
* texColor
+ colorOffset
;
1887 texColor
.rgb
*= texColor
.a
;
1888 texColor
= clamp(texColor
, 0.0, 1.0);
1889 vec4 d4
= min(edge_dist
[0], edge_dist
[1]);
1890 vec2 d2
= min(d4
.xz
, d4
.yw
);
1891 float aa
= clamp(gl_FragCoord
.w
* min(d2
.x
, d2
.y
), 0.0, 1.0);
1892 gl_FragColor
= ApplyBlendMode(texColor
* alpha
* aa
, 0.0);
1897 void FragmentShaderRGBATexAlphaColorMatrixAA::FillLocations(
1898 ShaderLocations
* locations
) const {
1899 locations
->sampler
= sampler_location();
1900 locations
->alpha
= alpha_location();
1901 locations
->color_matrix
= color_matrix_location();
1902 locations
->color_offset
= color_offset_location();
1903 locations
->backdrop
= backdrop_location();
1904 locations
->backdrop_rect
= backdrop_rect_location();
1907 FragmentShaderRGBATexAlphaMaskColorMatrix::
1908 FragmentShaderRGBATexAlphaMaskColorMatrix()
1909 : sampler_location_(-1),
1910 mask_sampler_location_(-1),
1911 alpha_location_(-1),
1912 mask_tex_coord_scale_location_(-1) {
1915 void FragmentShaderRGBATexAlphaMaskColorMatrix::Init(GLES2Interface
* context
,
1917 int* base_uniform_index
) {
1918 static const char* uniforms
[] = {
1922 "maskTexCoordScale",
1923 "maskTexCoordOffset",
1926 BLEND_MODE_UNIFORMS
,
1928 int locations
[arraysize(uniforms
)];
1930 GetProgramUniformLocations(context
,
1932 arraysize(uniforms
) - UNUSED_BLEND_MODE_UNIFORMS
,
1935 base_uniform_index
);
1936 sampler_location_
= locations
[0];
1937 mask_sampler_location_
= locations
[1];
1938 alpha_location_
= locations
[2];
1939 mask_tex_coord_scale_location_
= locations
[3];
1940 mask_tex_coord_offset_location_
= locations
[4];
1941 color_matrix_location_
= locations
[5];
1942 color_offset_location_
= locations
[6];
1943 BLEND_MODE_SET_LOCATIONS(locations
, 7);
1946 std::string
FragmentShaderRGBATexAlphaMaskColorMatrix::GetShaderString(
1947 TexCoordPrecision precision
,
1948 SamplerType sampler
) const {
1949 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
1952 std::string
FragmentShaderRGBATexAlphaMaskColorMatrix::GetShaderHead() {
1953 return SHADER0([]() {
1954 precision mediump
float;
1955 varying TexCoordPrecision vec2 v_texCoord
;
1956 uniform sampler2D s_texture
;
1957 uniform SamplerType s_mask
;
1958 uniform vec2 maskTexCoordScale
;
1959 uniform vec2 maskTexCoordOffset
;
1960 uniform mat4 colorMatrix
;
1961 uniform vec4 colorOffset
;
1962 uniform
float alpha
;
1966 std::string
FragmentShaderRGBATexAlphaMaskColorMatrix::GetShaderBody() {
1967 return SHADER0([]() {
1969 vec4 texColor
= texture2D(s_texture
, v_texCoord
);
1970 float nonZeroAlpha
= max(texColor
.a
, 0.00001);
1971 texColor
= vec4(texColor
.rgb
/ nonZeroAlpha
, nonZeroAlpha
);
1972 texColor
= colorMatrix
* texColor
+ colorOffset
;
1973 texColor
.rgb
*= texColor
.a
;
1974 texColor
= clamp(texColor
, 0.0, 1.0);
1975 TexCoordPrecision vec2 maskTexCoord
=
1976 vec2(maskTexCoordOffset
.x
+ v_texCoord
.x
* maskTexCoordScale
.x
,
1977 maskTexCoordOffset
.y
+ v_texCoord
.y
* maskTexCoordScale
.y
);
1978 vec4 maskColor
= TextureLookup(s_mask
, maskTexCoord
);
1979 gl_FragColor
= ApplyBlendMode(
1980 texColor
* alpha
* maskColor
.w
, maskColor
.w
);
1985 void FragmentShaderRGBATexAlphaMaskColorMatrix::FillLocations(
1986 ShaderLocations
* locations
) const {
1987 locations
->sampler
= sampler_location();
1988 locations
->mask_sampler
= mask_sampler_location();
1989 locations
->mask_tex_coord_scale
= mask_tex_coord_scale_location();
1990 locations
->mask_tex_coord_offset
= mask_tex_coord_offset_location();
1991 locations
->alpha
= alpha_location();
1992 locations
->color_matrix
= color_matrix_location();
1993 locations
->color_offset
= color_offset_location();
1994 locations
->backdrop
= backdrop_location();
1995 locations
->backdrop_rect
= backdrop_rect_location();
1996 if (mask_for_background())
1997 locations
->original_backdrop
= original_backdrop_location();
2000 FragmentShaderYUVVideo::FragmentShaderYUVVideo()
2001 : y_texture_location_(-1),
2002 u_texture_location_(-1),
2003 v_texture_location_(-1),
2004 alpha_location_(-1),
2005 yuv_matrix_location_(-1),
2006 yuv_adj_location_(-1),
2007 clamp_rect_location_(-1) {
2010 void FragmentShaderYUVVideo::Init(GLES2Interface
* context
,
2012 int* base_uniform_index
) {
2013 static const char* uniforms
[] = {"y_texture",
2020 int locations
[arraysize(uniforms
)];
2022 GetProgramUniformLocations(context
,
2024 arraysize(uniforms
),
2027 base_uniform_index
);
2028 y_texture_location_
= locations
[0];
2029 u_texture_location_
= locations
[1];
2030 v_texture_location_
= locations
[2];
2031 alpha_location_
= locations
[3];
2032 yuv_matrix_location_
= locations
[4];
2033 yuv_adj_location_
= locations
[5];
2034 clamp_rect_location_
= locations
[6];
2037 std::string
FragmentShaderYUVVideo::GetShaderString(TexCoordPrecision precision
,
2038 SamplerType sampler
) const {
2039 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
2042 std::string
FragmentShaderYUVVideo::GetShaderHead() {
2043 return SHADER0([]() {
2044 precision mediump
float;
2045 precision mediump
int;
2046 varying TexCoordPrecision vec2 v_texCoord
;
2047 uniform SamplerType y_texture
;
2048 uniform SamplerType u_texture
;
2049 uniform SamplerType v_texture
;
2050 uniform
float alpha
;
2051 uniform vec3 yuv_adj
;
2052 uniform mat3 yuv_matrix
;
2053 uniform vec4 clamp_rect
;
2057 std::string
FragmentShaderYUVVideo::GetShaderBody() {
2058 return SHADER0([]() {
2060 vec2 clamped
= max(clamp_rect
.xy
, min(clamp_rect
.zw
, v_texCoord
));
2061 float y_raw
= TextureLookup(y_texture
, clamped
).x
;
2062 float u_unsigned
= TextureLookup(u_texture
, clamped
).x
;
2063 float v_unsigned
= TextureLookup(v_texture
, clamped
).x
;
2064 vec3 yuv
= vec3(y_raw
, u_unsigned
, v_unsigned
) + yuv_adj
;
2065 vec3 rgb
= yuv_matrix
* yuv
;
2066 gl_FragColor
= vec4(rgb
, 1.0) * alpha
;
2071 FragmentShaderYUVAVideo::FragmentShaderYUVAVideo()
2072 : y_texture_location_(-1),
2073 u_texture_location_(-1),
2074 v_texture_location_(-1),
2075 a_texture_location_(-1),
2076 alpha_location_(-1),
2077 yuv_matrix_location_(-1),
2078 yuv_adj_location_(-1) {
2081 void FragmentShaderYUVAVideo::Init(GLES2Interface
* context
,
2083 int* base_uniform_index
) {
2084 static const char* uniforms
[] = {
2094 int locations
[arraysize(uniforms
)];
2096 GetProgramUniformLocations(context
,
2098 arraysize(uniforms
),
2101 base_uniform_index
);
2102 y_texture_location_
= locations
[0];
2103 u_texture_location_
= locations
[1];
2104 v_texture_location_
= locations
[2];
2105 a_texture_location_
= locations
[3];
2106 alpha_location_
= locations
[4];
2107 yuv_matrix_location_
= locations
[5];
2108 yuv_adj_location_
= locations
[6];
2109 clamp_rect_location_
= locations
[7];
2112 std::string
FragmentShaderYUVAVideo::GetShaderString(
2113 TexCoordPrecision precision
,
2114 SamplerType sampler
) const {
2115 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
2118 std::string
FragmentShaderYUVAVideo::GetShaderHead() {
2119 return SHADER0([]() {
2120 precision mediump
float;
2121 precision mediump
int;
2122 varying TexCoordPrecision vec2 v_texCoord
;
2123 uniform SamplerType y_texture
;
2124 uniform SamplerType u_texture
;
2125 uniform SamplerType v_texture
;
2126 uniform SamplerType a_texture
;
2127 uniform
float alpha
;
2128 uniform vec3 yuv_adj
;
2129 uniform mat3 yuv_matrix
;
2130 uniform vec4 clamp_rect
;
2134 std::string
FragmentShaderYUVAVideo::GetShaderBody() {
2135 return SHADER0([]() {
2137 vec2 clamped
= max(clamp_rect
.xy
, min(clamp_rect
.zw
, v_texCoord
));
2138 float y_raw
= TextureLookup(y_texture
, clamped
).x
;
2139 float u_unsigned
= TextureLookup(u_texture
, clamped
).x
;
2140 float v_unsigned
= TextureLookup(v_texture
, clamped
).x
;
2141 float a_raw
= TextureLookup(a_texture
, clamped
).x
;
2142 vec3 yuv
= vec3(y_raw
, u_unsigned
, v_unsigned
) + yuv_adj
;
2143 vec3 rgb
= yuv_matrix
* yuv
;
2144 gl_FragColor
= vec4(rgb
, 1.0) * (alpha
* a_raw
);
2149 FragmentShaderColor::FragmentShaderColor() : color_location_(-1) {
2152 void FragmentShaderColor::Init(GLES2Interface
* context
,
2154 int* base_uniform_index
) {
2155 static const char* uniforms
[] = {
2158 int locations
[arraysize(uniforms
)];
2160 GetProgramUniformLocations(context
,
2162 arraysize(uniforms
),
2165 base_uniform_index
);
2166 color_location_
= locations
[0];
2169 std::string
FragmentShaderColor::GetShaderString(TexCoordPrecision precision
,
2170 SamplerType sampler
) const {
2171 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
2174 std::string
FragmentShaderColor::GetShaderHead() {
2175 return SHADER0([]() {
2176 precision mediump
float;
2181 std::string
FragmentShaderColor::GetShaderBody() {
2182 return SHADER0([]() {
2183 void main() { gl_FragColor
= color
; }
2187 FragmentShaderColorAA::FragmentShaderColorAA() : color_location_(-1) {
2190 void FragmentShaderColorAA::Init(GLES2Interface
* context
,
2192 int* base_uniform_index
) {
2193 static const char* uniforms
[] = {
2196 int locations
[arraysize(uniforms
)];
2198 GetProgramUniformLocations(context
,
2200 arraysize(uniforms
),
2203 base_uniform_index
);
2204 color_location_
= locations
[0];
2207 std::string
FragmentShaderColorAA::GetShaderString(TexCoordPrecision precision
,
2208 SamplerType sampler
) const {
2209 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
2212 std::string
FragmentShaderColorAA::GetShaderHead() {
2213 return SHADER0([]() {
2214 precision mediump
float;
2216 varying vec4 edge_dist
[2]; // 8 edge distances.
2220 std::string
FragmentShaderColorAA::GetShaderBody() {
2221 return SHADER0([]() {
2223 vec4 d4
= min(edge_dist
[0], edge_dist
[1]);
2224 vec2 d2
= min(d4
.xz
, d4
.yw
);
2225 float aa
= clamp(gl_FragCoord
.w
* min(d2
.x
, d2
.y
), 0.0, 1.0);
2226 gl_FragColor
= color
* aa
;
2231 FragmentShaderCheckerboard::FragmentShaderCheckerboard()
2232 : alpha_location_(-1),
2233 tex_transform_location_(-1),
2234 frequency_location_(-1) {
2237 void FragmentShaderCheckerboard::Init(GLES2Interface
* context
,
2239 int* base_uniform_index
) {
2240 static const char* uniforms
[] = {
2241 "alpha", "texTransform", "frequency", "color",
2243 int locations
[arraysize(uniforms
)];
2245 GetProgramUniformLocations(context
,
2247 arraysize(uniforms
),
2250 base_uniform_index
);
2251 alpha_location_
= locations
[0];
2252 tex_transform_location_
= locations
[1];
2253 frequency_location_
= locations
[2];
2254 color_location_
= locations
[3];
2257 std::string
FragmentShaderCheckerboard::GetShaderString(
2258 TexCoordPrecision precision
,
2259 SamplerType sampler
) const {
2260 return FRAGMENT_SHADER(GetShaderHead(), GetShaderBody());
2263 std::string
FragmentShaderCheckerboard::GetShaderHead() {
2264 return SHADER0([]() {
2265 precision mediump
float;
2266 precision mediump
int;
2267 varying vec2 v_texCoord
;
2268 uniform
float alpha
;
2269 uniform
float frequency
;
2270 uniform vec4 texTransform
;
2275 std::string
FragmentShaderCheckerboard::GetShaderBody() {
2276 // Shader based on Example 13-17 of "OpenGL ES 2.0 Programming Guide"
2277 // by Munshi, Ginsburg, Shreiner.
2278 return SHADER0([]() {
2280 vec4 color1
= vec4(1.0, 1.0, 1.0, 1.0);
2281 vec4 color2
= color
;
2283 clamp(v_texCoord
, 0.0, 1.0) * texTransform
.zw
+ texTransform
.xy
;
2284 vec2 coord
= mod(floor(texCoord
* frequency
* 2.0), 2.0);
2285 float picker
= abs(coord
.x
- coord
.y
); // NOLINT
2286 gl_FragColor
= mix(color1
, color2
, picker
) * alpha
;