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 "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3D.h"
13 #include "third_party/khronos/GLES2/gl2.h"
15 #define SHADER0(Src) #Src
16 #define VERTEX_SHADER(Src) setVertexTexCoordPrecision(SHADER0(Src))
17 #define FRAGMENT_SHADER(Src) setFragTexCoordPrecision(precision, SHADER0(Src))
19 using WebKit::WebGraphicsContext3D
;
25 static void GetProgramUniformLocations(WebGraphicsContext3D
* context
,
27 const char** shader_uniforms
,
31 bool using_bind_uniform
,
32 int* base_uniform_index
) {
33 for (size_t uniform_index
= 0; uniform_index
< count
; uniform_index
++) {
34 DCHECK(uniform_index
< max_locations
);
36 if (using_bind_uniform
) {
37 locations
[uniform_index
] = (*base_uniform_index
)++;
38 context
->bindUniformLocationCHROMIUM(program
,
39 locations
[uniform_index
],
40 shader_uniforms
[uniform_index
]);
42 locations
[uniform_index
] =
43 context
->getUniformLocation(program
, shader_uniforms
[uniform_index
]);
48 static std::string
setFragTexCoordPrecision(
49 TexCoordPrecision requestedPrecision
, std::string shaderString
)
51 switch (requestedPrecision
) {
52 case TexCoordPrecisionHigh
:
53 DCHECK_NE(shaderString
.find("TexCoordPrecision"), std::string::npos
);
54 return "#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
55 " #define TexCoordPrecision highp\n"
57 " #define TexCoordPrecision mediump\n"
60 case TexCoordPrecisionMedium
:
61 DCHECK_NE(shaderString
.find("TexCoordPrecision"), std::string::npos
);
62 return "#define TexCoordPrecision mediump\n" +
64 case TexCoordPrecisionNA
:
65 DCHECK_EQ(shaderString
.find("TexCoordPrecision"), std::string::npos
);
66 DCHECK_EQ(shaderString
.find("texture2D"), std::string::npos
);
72 static std::string
setVertexTexCoordPrecision(const char* shaderString
)
74 // We unconditionally use highp in the vertex shader since
75 // we are unlikely to be vertex shader bound when drawing large quads.
76 // Also, some vertex shaders mutate the texture coordinate in such a
77 // way that the effective precision might be lower than expected.
78 return "#define TexCoordPrecision highp\n" +
79 std::string(shaderString
);
82 TexCoordPrecision
TexCoordPrecisionRequired(WebGraphicsContext3D
* context
,
83 int highp_threshold_min
,
85 // Initialize range and precision with minimum spec values for when
86 // getShaderPrecisionFormat is a test stub.
87 // TODO: Implement better stubs of getShaderPrecisionFormat everywhere.
88 GLint range
[2] = { 14, 14 };
90 GLC(context
, context
->getShaderPrecisionFormat(GL_FRAGMENT_SHADER
,
93 int highp_threshold
= std::max(1 << precision
, highp_threshold_min
);
94 if (x
> highp_threshold
|| y
> highp_threshold
)
95 return TexCoordPrecisionHigh
;
96 return TexCoordPrecisionMedium
;
101 TexCoordPrecision
TexCoordPrecisionRequired(WebGraphicsContext3D
* context
,
102 int highp_threshold_min
,
103 const gfx::Point
& max_coordinate
) {
104 return TexCoordPrecisionRequired(context
, highp_threshold_min
,
105 max_coordinate
.x(), max_coordinate
.y());
108 TexCoordPrecision
TexCoordPrecisionRequired(WebGraphicsContext3D
* context
,
109 int highp_threshold_min
,
110 const gfx::Size
& max_size
) {
111 return TexCoordPrecisionRequired(context
, highp_threshold_min
,
112 max_size
.width(), max_size
.height());
115 VertexShaderPosTex::VertexShaderPosTex()
116 : matrix_location_(-1) {}
118 void VertexShaderPosTex::Init(WebGraphicsContext3D
* context
,
120 bool using_bind_uniform
,
121 int* base_uniform_index
) {
122 static const char* shader_uniforms
[] = {
127 GetProgramUniformLocations(context
,
130 arraysize(shader_uniforms
),
131 arraysize(locations
),
136 matrix_location_
= locations
[0];
137 DCHECK_NE(matrix_location_
, -1);
140 std::string
VertexShaderPosTex::GetShaderString() const {
141 return VERTEX_SHADER(
142 attribute vec4 a_position
;
143 attribute TexCoordPrecision vec2 a_texCoord
;
145 varying TexCoordPrecision vec2 v_texCoord
;
147 gl_Position
= matrix
* a_position
;
148 v_texCoord
= a_texCoord
;
150 ); // NOLINT(whitespace/parens)
153 VertexShaderPosTexYUVStretch::VertexShaderPosTexYUVStretch()
154 : matrix_location_(-1),
155 tex_scale_location_(-1) {}
157 void VertexShaderPosTexYUVStretch::Init(WebGraphicsContext3D
* context
,
159 bool using_bind_uniform
,
160 int* base_uniform_index
) {
161 static const char* shader_uniforms
[] = {
167 GetProgramUniformLocations(context
,
170 arraysize(shader_uniforms
),
171 arraysize(locations
),
176 matrix_location_
= locations
[0];
177 tex_scale_location_
= locations
[1];
178 DCHECK(matrix_location_
!= -1 && tex_scale_location_
!= -1);
181 std::string
VertexShaderPosTexYUVStretch::GetShaderString() const {
182 return VERTEX_SHADER(
183 precision mediump
float;
184 attribute vec4 a_position
;
185 attribute TexCoordPrecision vec2 a_texCoord
;
187 varying TexCoordPrecision vec2 v_texCoord
;
188 uniform TexCoordPrecision vec2 texScale
;
190 gl_Position
= matrix
* a_position
;
191 v_texCoord
= a_texCoord
* texScale
;
193 ); // NOLINT(whitespace/parens)
196 VertexShaderPos::VertexShaderPos()
197 : matrix_location_(-1) {}
199 void VertexShaderPos::Init(WebGraphicsContext3D
* context
,
201 bool using_bind_uniform
,
202 int* base_uniform_index
) {
203 static const char* shader_uniforms
[] = {
208 GetProgramUniformLocations(context
,
211 arraysize(shader_uniforms
),
212 arraysize(locations
),
217 matrix_location_
= locations
[0];
218 DCHECK_NE(matrix_location_
, -1);
221 std::string
VertexShaderPos::GetShaderString() const {
222 return VERTEX_SHADER(
223 attribute vec4 a_position
;
226 gl_Position
= matrix
* a_position
;
228 ); // NOLINT(whitespace/parens)
231 VertexShaderPosTexTransform::VertexShaderPosTexTransform()
232 : matrix_location_(-1),
233 tex_transform_location_(-1),
234 vertex_opacity_location_(-1) {}
236 void VertexShaderPosTexTransform::Init(WebGraphicsContext3D
* context
,
238 bool using_bind_uniform
,
239 int* base_uniform_index
) {
240 static const char* shader_uniforms
[] = {
247 GetProgramUniformLocations(context
,
250 arraysize(shader_uniforms
),
251 arraysize(locations
),
256 matrix_location_
= locations
[0];
257 tex_transform_location_
= locations
[1];
258 vertex_opacity_location_
= locations
[2];
259 DCHECK(matrix_location_
!= -1 && tex_transform_location_
!= -1 &&
260 vertex_opacity_location_
!= -1);
263 std::string
VertexShaderPosTexTransform::GetShaderString() const {
264 return VERTEX_SHADER(
265 attribute vec4 a_position
;
266 attribute TexCoordPrecision vec2 a_texCoord
;
267 attribute
float a_index
;
268 uniform mat4 matrix
[8];
269 uniform TexCoordPrecision vec4 texTransform
[8];
270 uniform
float opacity
[32];
271 varying TexCoordPrecision vec2 v_texCoord
;
272 varying
float v_alpha
;
274 gl_Position
= matrix
[int(a_index
* 0.25)] * a_position
; // NOLINT
275 TexCoordPrecision vec4 texTrans
=
276 texTransform
[int(a_index
* 0.25)]; // NOLINT
277 v_texCoord
= a_texCoord
* texTrans
.zw
+ texTrans
.xy
;
278 v_alpha
= opacity
[int(a_index
)]; // NOLINT
280 ); // NOLINT(whitespace/parens)
283 std::string
VertexShaderPosTexTransformFlip::GetShaderString() const {
284 return VERTEX_SHADER(
285 attribute vec4 a_position
;
286 attribute TexCoordPrecision vec2 a_texCoord
;
287 attribute
float a_index
;
288 uniform mat4 matrix
[8];
289 uniform TexCoordPrecision vec4 texTransform
[8];
290 uniform
float opacity
[32];
291 varying TexCoordPrecision vec2 v_texCoord
;
292 varying
float v_alpha
;
294 gl_Position
= matrix
[int(a_index
* 0.25)] * a_position
; // NOLINT
295 TexCoordPrecision vec4 texTrans
=
296 texTransform
[int(a_index
* 0.25)]; // NOLINT
297 v_texCoord
= a_texCoord
* texTrans
.zw
+ texTrans
.xy
;
298 v_texCoord
.y
= 1.0 - v_texCoord
.y
;
299 v_alpha
= opacity
[int(a_index
)]; // NOLINT
301 ); // NOLINT(whitespace/parens)
304 std::string
VertexShaderPosTexIdentity::GetShaderString() const {
305 return VERTEX_SHADER(
306 attribute vec4 a_position
;
307 varying TexCoordPrecision vec2 v_texCoord
;
309 gl_Position
= a_position
;
310 v_texCoord
= (a_position
.xy
+ vec2(1.0)) * 0.5;
312 ); // NOLINT(whitespace/parens)
315 VertexShaderQuad::VertexShaderQuad()
316 : matrix_location_(-1),
318 tex_scale_location_(-1) {}
320 void VertexShaderQuad::Init(WebGraphicsContext3D
* context
,
322 bool using_bind_uniform
,
323 int* base_uniform_index
) {
324 static const char* shader_uniforms
[] = {
331 GetProgramUniformLocations(context
,
334 arraysize(shader_uniforms
),
335 arraysize(locations
),
340 matrix_location_
= locations
[0];
341 point_location_
= locations
[1];
342 tex_scale_location_
= locations
[2];
343 DCHECK_NE(matrix_location_
, -1);
344 DCHECK_NE(point_location_
, -1);
345 DCHECK_NE(tex_scale_location_
, -1);
348 std::string
VertexShaderQuad::GetShaderString() const {
349 return VERTEX_SHADER(
350 attribute TexCoordPrecision vec4 a_position
;
351 attribute TexCoordPrecision vec2 a_texCoord
;
353 uniform TexCoordPrecision vec2 point
[4];
354 uniform TexCoordPrecision vec2 texScale
;
355 varying TexCoordPrecision vec2 v_texCoord
;
357 TexCoordPrecision vec2 complement
= abs(a_texCoord
- 1.0);
358 TexCoordPrecision vec4 pos
= vec4(0.0, 0.0, a_position
.z
, a_position
.w
);
359 pos
.xy
+= (complement
.x
* complement
.y
) * point
[0];
360 pos
.xy
+= (a_texCoord
.x
* complement
.y
) * point
[1];
361 pos
.xy
+= (a_texCoord
.x
* a_texCoord
.y
) * point
[2];
362 pos
.xy
+= (complement
.x
* a_texCoord
.y
) * point
[3];
363 gl_Position
= matrix
* pos
;
364 v_texCoord
= (pos
.xy
+ vec2(0.5)) * texScale
;
366 ); // NOLINT(whitespace/parens)
369 VertexShaderTile::VertexShaderTile()
370 : matrix_location_(-1),
372 vertex_tex_transform_location_(-1) {}
374 void VertexShaderTile::Init(WebGraphicsContext3D
* context
,
376 bool using_bind_uniform
,
377 int* base_uniform_index
) {
378 static const char* shader_uniforms
[] = {
381 "vertexTexTransform",
385 GetProgramUniformLocations(context
,
388 arraysize(shader_uniforms
),
389 arraysize(locations
),
394 matrix_location_
= locations
[0];
395 point_location_
= locations
[1];
396 vertex_tex_transform_location_
= locations
[2];
397 DCHECK(matrix_location_
!= -1 && point_location_
!= -1 &&
398 vertex_tex_transform_location_
!= -1);
401 std::string
VertexShaderTile::GetShaderString() const {
402 return VERTEX_SHADER(
403 attribute TexCoordPrecision vec4 a_position
;
404 attribute TexCoordPrecision vec2 a_texCoord
;
406 uniform TexCoordPrecision vec2 point
[4];
407 uniform TexCoordPrecision vec4 vertexTexTransform
;
408 varying TexCoordPrecision vec2 v_texCoord
;
410 TexCoordPrecision vec2 complement
= abs(a_texCoord
- 1.0);
411 TexCoordPrecision vec4 pos
= vec4(0.0, 0.0, a_position
.z
, a_position
.w
);
412 pos
.xy
+= (complement
.x
* complement
.y
) * point
[0];
413 pos
.xy
+= (a_texCoord
.x
* complement
.y
) * point
[1];
414 pos
.xy
+= (a_texCoord
.x
* a_texCoord
.y
) * point
[2];
415 pos
.xy
+= (complement
.x
* a_texCoord
.y
) * point
[3];
416 gl_Position
= matrix
* pos
;
417 v_texCoord
= pos
.xy
* vertexTexTransform
.zw
+ vertexTexTransform
.xy
;
419 ); // NOLINT(whitespace/parens)
422 VertexShaderVideoTransform::VertexShaderVideoTransform()
423 : matrix_location_(-1),
424 tex_matrix_location_(-1) {}
426 bool VertexShaderVideoTransform::Init(WebGraphicsContext3D
* context
,
428 bool using_bind_uniform
,
429 int* base_uniform_index
) {
430 static const char* shader_uniforms
[] = {
436 GetProgramUniformLocations(context
,
439 arraysize(shader_uniforms
),
440 arraysize(locations
),
445 matrix_location_
= locations
[0];
446 tex_matrix_location_
= locations
[1];
447 return matrix_location_
!= -1 && tex_matrix_location_
!= -1;
450 std::string
VertexShaderVideoTransform::GetShaderString() const {
451 return VERTEX_SHADER(
452 attribute vec4 a_position
;
453 attribute TexCoordPrecision vec2 a_texCoord
;
455 uniform TexCoordPrecision mat4 texMatrix
;
456 varying TexCoordPrecision vec2 v_texCoord
;
458 gl_Position
= matrix
* a_position
;
460 vec2(texMatrix
* vec4(a_texCoord
.x
, 1.0 - a_texCoord
.y
, 0.0, 1.0));
462 ); // NOLINT(whitespace/parens)
465 FragmentTexAlphaBinding::FragmentTexAlphaBinding()
466 : sampler_location_(-1),
467 alpha_location_(-1) {}
469 void FragmentTexAlphaBinding::Init(WebGraphicsContext3D
* context
,
471 bool using_bind_uniform
,
472 int* base_uniform_index
) {
473 static const char* shader_uniforms
[] = {
479 GetProgramUniformLocations(context
,
482 arraysize(shader_uniforms
),
483 arraysize(locations
),
488 sampler_location_
= locations
[0];
489 alpha_location_
= locations
[1];
490 DCHECK(sampler_location_
!= -1 && alpha_location_
!= -1);
493 FragmentTexColorMatrixAlphaBinding::FragmentTexColorMatrixAlphaBinding()
494 : sampler_location_(-1),
496 color_matrix_location_(-1),
497 color_offset_location_(-1) {}
499 void FragmentTexColorMatrixAlphaBinding::Init(WebGraphicsContext3D
* context
,
501 bool usingBindUniform
,
502 int* baseUniformIndex
) {
503 static const char* shaderUniforms
[] = {
511 GetProgramUniformLocations(context
,
514 arraysize(shaderUniforms
),
515 arraysize(locations
),
520 sampler_location_
= locations
[0];
521 alpha_location_
= locations
[1];
522 color_matrix_location_
= locations
[2];
523 color_offset_location_
= locations
[3];
524 DCHECK(sampler_location_
!= -1 && alpha_location_
!= -1 &&
525 color_matrix_location_
!= -1 && color_offset_location_
!= -1);
528 FragmentTexOpaqueBinding::FragmentTexOpaqueBinding()
529 : sampler_location_(-1) {}
531 void FragmentTexOpaqueBinding::Init(WebGraphicsContext3D
* context
,
533 bool using_bind_uniform
,
534 int* base_uniform_index
) {
535 static const char* shader_uniforms
[] = {
540 GetProgramUniformLocations(context
,
543 arraysize(shader_uniforms
),
544 arraysize(locations
),
549 sampler_location_
= locations
[0];
550 DCHECK_NE(sampler_location_
, -1);
553 FragmentShaderOESImageExternal::FragmentShaderOESImageExternal()
554 : sampler_location_(-1) {}
556 bool FragmentShaderOESImageExternal::Init(WebGraphicsContext3D
* context
,
558 bool using_bind_uniform
,
559 int* base_uniform_index
) {
560 static const char* shader_uniforms
[] = {
565 GetProgramUniformLocations(context
,
568 arraysize(shader_uniforms
),
569 arraysize(locations
),
574 sampler_location_
= locations
[0];
575 return sampler_location_
!= -1;
578 std::string
FragmentShaderOESImageExternal::GetShaderString(
579 TexCoordPrecision precision
) const {
580 // Cannot use the SHADER() macro because of the '#' char
581 return "#extension GL_OES_EGL_image_external : require\n" +
583 precision mediump
float;
584 varying TexCoordPrecision vec2 v_texCoord
;
585 uniform samplerExternalOES s_texture
;
587 vec4 texColor
= texture2D(s_texture
, v_texCoord
);
588 gl_FragColor
= vec4(texColor
.x
, texColor
.y
, texColor
.z
, texColor
.w
);
590 ); // NOLINT(whitespace/parens)
593 std::string
FragmentShaderRGBATexAlpha::GetShaderString(
594 TexCoordPrecision precision
) const {
595 return FRAGMENT_SHADER(
596 precision mediump
float;
597 varying TexCoordPrecision vec2 v_texCoord
;
598 uniform sampler2D s_texture
;
601 vec4 texColor
= texture2D(s_texture
, v_texCoord
);
602 gl_FragColor
= texColor
* alpha
;
604 ); // NOLINT(whitespace/parens)
607 std::string
FragmentShaderRGBATexColorMatrixAlpha::GetShaderString(
608 TexCoordPrecision precision
) const {
609 return FRAGMENT_SHADER(
610 precision mediump
float;
611 varying TexCoordPrecision vec2 v_texCoord
;
612 uniform sampler2D s_texture
;
614 uniform mat4 colorMatrix
;
615 uniform vec4 colorOffset
;
617 vec4 texColor
= texture2D(s_texture
, v_texCoord
);
618 float nonZeroAlpha
= max(texColor
.a
, 0.00001);
619 texColor
= vec4(texColor
.rgb
/ nonZeroAlpha
, nonZeroAlpha
);
620 texColor
= colorMatrix
* texColor
+ colorOffset
;
621 texColor
.rgb
*= texColor
.a
;
622 texColor
= clamp(texColor
, 0.0, 1.0);
623 gl_FragColor
= texColor
* alpha
;
625 ); // NOLINT(whitespace/parens)
628 std::string
FragmentShaderRGBATexVaryingAlpha::GetShaderString(
629 TexCoordPrecision precision
) const {
630 return FRAGMENT_SHADER(
631 precision mediump
float;
632 varying TexCoordPrecision vec2 v_texCoord
;
633 varying
float v_alpha
;
634 uniform sampler2D s_texture
;
636 vec4 texColor
= texture2D(s_texture
, v_texCoord
);
637 gl_FragColor
= texColor
* v_alpha
;
639 ); // NOLINT(whitespace/parens)
642 std::string
FragmentShaderRGBATexRectVaryingAlpha::GetShaderString(
643 TexCoordPrecision precision
) const {
644 return "#extension GL_ARB_texture_rectangle : require\n" +
646 precision mediump
float;
647 varying TexCoordPrecision vec2 v_texCoord
;
648 varying
float v_alpha
;
649 uniform sampler2DRect s_texture
;
651 vec4 texColor
= texture2DRect(s_texture
, v_texCoord
);
652 gl_FragColor
= texColor
* v_alpha
;
654 ); // NOLINT(whitespace/parens)
657 std::string
FragmentShaderRGBATexOpaque::GetShaderString(
658 TexCoordPrecision precision
) const {
659 return FRAGMENT_SHADER(
660 precision mediump
float;
661 varying TexCoordPrecision vec2 v_texCoord
;
662 uniform sampler2D s_texture
;
664 vec4 texColor
= texture2D(s_texture
, v_texCoord
);
665 gl_FragColor
= vec4(texColor
.rgb
, 1.0);
667 ); // NOLINT(whitespace/parens)
670 std::string
FragmentShaderRGBATex::GetShaderString(
671 TexCoordPrecision precision
) const {
672 return FRAGMENT_SHADER(
673 precision mediump
float;
674 varying TexCoordPrecision vec2 v_texCoord
;
675 uniform sampler2D s_texture
;
677 gl_FragColor
= texture2D(s_texture
, v_texCoord
);
679 ); // NOLINT(whitespace/parens)
682 std::string
FragmentShaderRGBATexSwizzleAlpha::GetShaderString(
683 TexCoordPrecision precision
) const {
684 return FRAGMENT_SHADER(
685 precision mediump
float;
686 varying TexCoordPrecision vec2 v_texCoord
;
687 uniform sampler2D s_texture
;
690 vec4 texColor
= texture2D(s_texture
, v_texCoord
);
692 vec4(texColor
.z
, texColor
.y
, texColor
.x
, texColor
.w
) * alpha
;
694 ); // NOLINT(whitespace/parens)
697 std::string
FragmentShaderRGBATexSwizzleOpaque::GetShaderString(
698 TexCoordPrecision precision
) const {
699 return FRAGMENT_SHADER(
700 precision mediump
float;
701 varying TexCoordPrecision vec2 v_texCoord
;
702 uniform sampler2D s_texture
;
704 vec4 texColor
= texture2D(s_texture
, v_texCoord
);
705 gl_FragColor
= vec4(texColor
.z
, texColor
.y
, texColor
.x
, 1.0);
707 ); // NOLINT(whitespace/parens)
710 FragmentShaderRGBATexAlphaAA::FragmentShaderRGBATexAlphaAA()
711 : sampler_location_(-1),
713 edge_location_(-1) {}
715 void FragmentShaderRGBATexAlphaAA::Init(WebGraphicsContext3D
* context
,
717 bool using_bind_uniform
,
718 int* base_uniform_index
) {
719 static const char* shader_uniforms
[] = {
726 GetProgramUniformLocations(context
,
729 arraysize(shader_uniforms
),
730 arraysize(locations
),
735 sampler_location_
= locations
[0];
736 alpha_location_
= locations
[1];
737 edge_location_
= locations
[2];
738 DCHECK(sampler_location_
!= -1 && alpha_location_
!= -1 &&
739 edge_location_
!= -1);
742 std::string
FragmentShaderRGBATexAlphaAA::GetShaderString(
743 TexCoordPrecision precision
) const {
744 return FRAGMENT_SHADER(
745 precision mediump
float;
746 varying TexCoordPrecision vec2 v_texCoord
;
747 uniform sampler2D s_texture
;
749 uniform vec3 edge
[8];
751 vec4 texColor
= texture2D(s_texture
, v_texCoord
);
752 vec3 pos
= vec3(gl_FragCoord
.xy
, 1);
753 float a0
= clamp(dot(edge
[0], pos
), 0.0, 1.0);
754 float a1
= clamp(dot(edge
[1], pos
), 0.0, 1.0);
755 float a2
= clamp(dot(edge
[2], pos
), 0.0, 1.0);
756 float a3
= clamp(dot(edge
[3], pos
), 0.0, 1.0);
757 float a4
= clamp(dot(edge
[4], pos
), 0.0, 1.0);
758 float a5
= clamp(dot(edge
[5], pos
), 0.0, 1.0);
759 float a6
= clamp(dot(edge
[6], pos
), 0.0, 1.0);
760 float a7
= clamp(dot(edge
[7], pos
), 0.0, 1.0);
761 gl_FragColor
= texColor
* alpha
* min(min(a0
, a2
) * min(a1
, a3
),
762 min(a4
, a6
) * min(a5
, a7
));
764 ); // NOLINT(whitespace/parens)
767 FragmentTexClampAlphaAABinding::FragmentTexClampAlphaAABinding()
768 : sampler_location_(-1),
770 fragment_tex_transform_location_(-1),
771 edge_location_(-1) {}
773 void FragmentTexClampAlphaAABinding::Init(WebGraphicsContext3D
* context
,
775 bool using_bind_uniform
,
776 int* base_uniform_index
) {
777 static const char* shader_uniforms
[] = {
780 "fragmentTexTransform",
785 GetProgramUniformLocations(context
,
788 arraysize(shader_uniforms
),
789 arraysize(locations
),
794 sampler_location_
= locations
[0];
795 alpha_location_
= locations
[1];
796 fragment_tex_transform_location_
= locations
[2];
797 edge_location_
= locations
[3];
798 DCHECK(sampler_location_
!= -1 && alpha_location_
!= -1 &&
799 fragment_tex_transform_location_
!= -1 && edge_location_
!= -1);
802 std::string
FragmentShaderRGBATexClampAlphaAA::GetShaderString(
803 TexCoordPrecision precision
) const {
804 return FRAGMENT_SHADER(
805 precision mediump
float;
806 varying TexCoordPrecision vec2 v_texCoord
;
807 uniform sampler2D s_texture
;
809 uniform TexCoordPrecision vec4 fragmentTexTransform
;
810 uniform vec3 edge
[8];
812 TexCoordPrecision vec2 texCoord
=
813 clamp(v_texCoord
, 0.0, 1.0) * fragmentTexTransform
.zw
+
814 fragmentTexTransform
.xy
;
815 vec4 texColor
= texture2D(s_texture
, texCoord
);
816 vec3 pos
= vec3(gl_FragCoord
.xy
, 1);
817 float a0
= clamp(dot(edge
[0], pos
), 0.0, 1.0);
818 float a1
= clamp(dot(edge
[1], pos
), 0.0, 1.0);
819 float a2
= clamp(dot(edge
[2], pos
), 0.0, 1.0);
820 float a3
= clamp(dot(edge
[3], pos
), 0.0, 1.0);
821 float a4
= clamp(dot(edge
[4], pos
), 0.0, 1.0);
822 float a5
= clamp(dot(edge
[5], pos
), 0.0, 1.0);
823 float a6
= clamp(dot(edge
[6], pos
), 0.0, 1.0);
824 float a7
= clamp(dot(edge
[7], pos
), 0.0, 1.0);
825 gl_FragColor
= texColor
* alpha
* min(min(a0
, a2
) * min(a1
, a3
),
826 min(a4
, a6
) * min(a5
, a7
));
828 ); // NOLINT(whitespace/parens)
831 std::string
FragmentShaderRGBATexClampSwizzleAlphaAA::GetShaderString(
832 TexCoordPrecision precision
) const {
833 return FRAGMENT_SHADER(
834 precision mediump
float;
835 varying TexCoordPrecision vec2 v_texCoord
;
836 uniform sampler2D s_texture
;
838 uniform TexCoordPrecision vec4 fragmentTexTransform
;
839 uniform vec3 edge
[8];
841 TexCoordPrecision vec2 texCoord
=
842 clamp(v_texCoord
, 0.0, 1.0) * fragmentTexTransform
.zw
+
843 fragmentTexTransform
.xy
;
844 vec4 texColor
= texture2D(s_texture
, texCoord
);
845 vec3 pos
= vec3(gl_FragCoord
.xy
, 1);
846 float a0
= clamp(dot(edge
[0], pos
), 0.0, 1.0);
847 float a1
= clamp(dot(edge
[1], pos
), 0.0, 1.0);
848 float a2
= clamp(dot(edge
[2], pos
), 0.0, 1.0);
849 float a3
= clamp(dot(edge
[3], pos
), 0.0, 1.0);
850 float a4
= clamp(dot(edge
[4], pos
), 0.0, 1.0);
851 float a5
= clamp(dot(edge
[5], pos
), 0.0, 1.0);
852 float a6
= clamp(dot(edge
[6], pos
), 0.0, 1.0);
853 float a7
= clamp(dot(edge
[7], pos
), 0.0, 1.0);
854 gl_FragColor
= vec4(texColor
.z
, texColor
.y
, texColor
.x
, texColor
.w
) *
855 alpha
* min(min(a0
, a2
) * min(a1
, a3
), min(a4
, a6
) * min(a5
, a7
));
857 ); // NOLINT(whitespace/parens)
860 FragmentShaderRGBATexAlphaMask::FragmentShaderRGBATexAlphaMask()
861 : sampler_location_(-1),
862 mask_sampler_location_(-1),
864 mask_tex_coord_scale_location_(-1) {}
866 void FragmentShaderRGBATexAlphaMask::Init(WebGraphicsContext3D
* context
,
868 bool using_bind_uniform
,
869 int* base_uniform_index
) {
870 static const char* shader_uniforms
[] = {
875 "maskTexCoordOffset",
879 GetProgramUniformLocations(context
,
882 arraysize(shader_uniforms
),
883 arraysize(locations
),
888 sampler_location_
= locations
[0];
889 mask_sampler_location_
= locations
[1];
890 alpha_location_
= locations
[2];
891 mask_tex_coord_scale_location_
= locations
[3];
892 mask_tex_coord_offset_location_
= locations
[4];
893 DCHECK(sampler_location_
!= -1 && mask_sampler_location_
!= -1 &&
894 alpha_location_
!= -1);
897 std::string
FragmentShaderRGBATexAlphaMask::GetShaderString(
898 TexCoordPrecision precision
) const {
899 return FRAGMENT_SHADER(
900 precision mediump
float;
901 varying TexCoordPrecision vec2 v_texCoord
;
902 uniform sampler2D s_texture
;
903 uniform sampler2D s_mask
;
904 uniform TexCoordPrecision vec2 maskTexCoordScale
;
905 uniform TexCoordPrecision vec2 maskTexCoordOffset
;
908 vec4 texColor
= texture2D(s_texture
, v_texCoord
);
909 TexCoordPrecision vec2 maskTexCoord
=
910 vec2(maskTexCoordOffset
.x
+ v_texCoord
.x
* maskTexCoordScale
.x
,
911 maskTexCoordOffset
.y
+ v_texCoord
.y
* maskTexCoordScale
.y
);
912 vec4 maskColor
= texture2D(s_mask
, maskTexCoord
);
913 gl_FragColor
= vec4(texColor
.x
, texColor
.y
,
914 texColor
.z
, texColor
.w
) * alpha
* maskColor
.w
;
916 ); // NOLINT(whitespace/parens)
919 FragmentShaderRGBATexAlphaMaskAA::FragmentShaderRGBATexAlphaMaskAA()
920 : sampler_location_(-1),
921 mask_sampler_location_(-1),
924 mask_tex_coord_scale_location_(-1) {}
926 void FragmentShaderRGBATexAlphaMaskAA::Init(WebGraphicsContext3D
* context
,
928 bool using_bind_uniform
,
929 int* base_uniform_index
) {
930 static const char* shader_uniforms
[] = {
936 "maskTexCoordOffset",
940 GetProgramUniformLocations(context
,
943 arraysize(shader_uniforms
),
944 arraysize(locations
),
949 sampler_location_
= locations
[0];
950 mask_sampler_location_
= locations
[1];
951 alpha_location_
= locations
[2];
952 edge_location_
= locations
[3];
953 mask_tex_coord_scale_location_
= locations
[4];
954 mask_tex_coord_offset_location_
= locations
[5];
955 DCHECK(sampler_location_
!= -1 && mask_sampler_location_
!= -1 &&
956 alpha_location_
!= -1 && edge_location_
!= -1);
959 std::string
FragmentShaderRGBATexAlphaMaskAA::GetShaderString(
960 TexCoordPrecision precision
) const {
961 return FRAGMENT_SHADER(
962 precision mediump
float;
963 varying TexCoordPrecision vec2 v_texCoord
;
964 uniform sampler2D s_texture
;
965 uniform sampler2D s_mask
;
966 uniform TexCoordPrecision vec2 maskTexCoordScale
;
967 uniform TexCoordPrecision vec2 maskTexCoordOffset
;
969 uniform vec3 edge
[8];
971 vec4 texColor
= texture2D(s_texture
, v_texCoord
);
972 TexCoordPrecision vec2 maskTexCoord
=
973 vec2(maskTexCoordOffset
.x
+ v_texCoord
.x
* maskTexCoordScale
.x
,
974 maskTexCoordOffset
.y
+ v_texCoord
.y
* maskTexCoordScale
.y
);
975 vec4 maskColor
= texture2D(s_mask
, maskTexCoord
);
976 vec3 pos
= vec3(gl_FragCoord
.xy
, 1);
977 float a0
= clamp(dot(edge
[0], pos
), 0.0, 1.0);
978 float a1
= clamp(dot(edge
[1], pos
), 0.0, 1.0);
979 float a2
= clamp(dot(edge
[2], pos
), 0.0, 1.0);
980 float a3
= clamp(dot(edge
[3], pos
), 0.0, 1.0);
981 float a4
= clamp(dot(edge
[4], pos
), 0.0, 1.0);
982 float a5
= clamp(dot(edge
[5], pos
), 0.0, 1.0);
983 float a6
= clamp(dot(edge
[6], pos
), 0.0, 1.0);
984 float a7
= clamp(dot(edge
[7], pos
), 0.0, 1.0);
985 gl_FragColor
= vec4(texColor
.x
, texColor
.y
, texColor
.z
, texColor
.w
) *
986 alpha
* maskColor
.w
* min(min(a0
, a2
) * min(a1
, a3
),
987 min(a4
, a6
) * min(a5
, a7
));
989 ); // NOLINT(whitespace/parens)
992 FragmentShaderRGBATexAlphaMaskColorMatrixAA::
993 FragmentShaderRGBATexAlphaMaskColorMatrixAA()
994 : sampler_location_(-1),
995 mask_sampler_location_(-1),
998 mask_tex_coord_scale_location_(-1),
999 color_matrix_location_(-1),
1000 color_offset_location_(-1) {}
1002 void FragmentShaderRGBATexAlphaMaskColorMatrixAA::Init(
1003 WebGraphicsContext3D
* context
,
1005 bool usingBindUniform
,
1006 int* baseUniformIndex
) {
1007 static const char* shaderUniforms
[] = {
1012 "maskTexCoordScale",
1013 "maskTexCoordOffset",
1019 GetProgramUniformLocations(context
,
1022 arraysize(shaderUniforms
),
1023 arraysize(locations
),
1028 sampler_location_
= locations
[0];
1029 mask_sampler_location_
= locations
[1];
1030 alpha_location_
= locations
[2];
1031 edge_location_
= locations
[3];
1032 mask_tex_coord_scale_location_
= locations
[4];
1033 mask_tex_coord_offset_location_
= locations
[5];
1034 color_matrix_location_
= locations
[6];
1035 color_offset_location_
= locations
[7];
1036 DCHECK(sampler_location_
!= -1 && mask_sampler_location_
!= -1 &&
1037 alpha_location_
!= -1 && edge_location_
!= -1 &&
1038 color_matrix_location_
!= -1 && color_offset_location_
!= -1);
1041 std::string
FragmentShaderRGBATexAlphaMaskColorMatrixAA::GetShaderString(
1042 TexCoordPrecision precision
) const {
1043 return FRAGMENT_SHADER(
1044 precision mediump
float;
1045 varying TexCoordPrecision vec2 v_texCoord
;
1046 uniform sampler2D s_texture
;
1047 uniform sampler2D s_mask
;
1048 uniform vec2 maskTexCoordScale
;
1049 uniform vec2 maskTexCoordOffset
;
1050 uniform mat4 colorMatrix
;
1051 uniform vec4 colorOffset
;
1052 uniform
float alpha
;
1053 uniform vec3 edge
[8];
1055 vec4 texColor
= texture2D(s_texture
, v_texCoord
);
1056 float nonZeroAlpha
= max(texColor
.a
, 0.00001);
1057 texColor
= vec4(texColor
.rgb
/ nonZeroAlpha
, nonZeroAlpha
);
1058 texColor
= colorMatrix
* texColor
+ colorOffset
;
1059 texColor
.rgb
*= texColor
.a
;
1060 texColor
= clamp(texColor
, 0.0, 1.0);
1061 TexCoordPrecision vec2 maskTexCoord
=
1062 vec2(maskTexCoordOffset
.x
+ v_texCoord
.x
* maskTexCoordScale
.x
,
1063 maskTexCoordOffset
.y
+ v_texCoord
.y
* maskTexCoordScale
.y
);
1064 vec4 maskColor
= texture2D(s_mask
, maskTexCoord
);
1065 vec3 pos
= vec3(gl_FragCoord
.xy
, 1);
1066 float a0
= clamp(dot(edge
[0], pos
), 0.0, 1.0);
1067 float a1
= clamp(dot(edge
[1], pos
), 0.0, 1.0);
1068 float a2
= clamp(dot(edge
[2], pos
), 0.0, 1.0);
1069 float a3
= clamp(dot(edge
[3], pos
), 0.0, 1.0);
1070 float a4
= clamp(dot(edge
[4], pos
), 0.0, 1.0);
1071 float a5
= clamp(dot(edge
[5], pos
), 0.0, 1.0);
1072 float a6
= clamp(dot(edge
[6], pos
), 0.0, 1.0);
1073 float a7
= clamp(dot(edge
[7], pos
), 0.0, 1.0);
1075 vec4(texColor
.x
, texColor
.y
, texColor
.z
, texColor
.w
) *
1076 alpha
* maskColor
.w
* min(min(a0
, a2
) * min(a1
, a3
), min(a4
, a6
) *
1079 ); // NOLINT(whitespace/parens)
1082 FragmentShaderRGBATexAlphaColorMatrixAA::
1083 FragmentShaderRGBATexAlphaColorMatrixAA()
1084 : sampler_location_(-1),
1085 alpha_location_(-1),
1087 color_matrix_location_(-1),
1088 color_offset_location_(-1) {}
1090 void FragmentShaderRGBATexAlphaColorMatrixAA::Init(
1091 WebGraphicsContext3D
* context
, unsigned program
, bool usingBindUniform
,
1092 int* baseUniformIndex
) {
1093 static const char* shaderUniforms
[] = {
1102 GetProgramUniformLocations(context
,
1105 arraysize(shaderUniforms
),
1106 arraysize(locations
),
1111 sampler_location_
= locations
[0];
1112 alpha_location_
= locations
[1];
1113 edge_location_
= locations
[2];
1114 color_matrix_location_
= locations
[3];
1115 color_offset_location_
= locations
[4];
1116 DCHECK(sampler_location_
!= -1 && alpha_location_
!= -1 &&
1117 edge_location_
!= -1 && color_matrix_location_
!= -1 &&
1118 color_offset_location_
!= -1);
1121 std::string
FragmentShaderRGBATexAlphaColorMatrixAA::GetShaderString(
1122 TexCoordPrecision precision
) const {
1123 return FRAGMENT_SHADER(
1124 precision mediump
float;
1125 varying TexCoordPrecision vec2 v_texCoord
;
1126 uniform sampler2D s_texture
;
1127 uniform
float alpha
;
1128 uniform mat4 colorMatrix
;
1129 uniform vec4 colorOffset
;
1130 uniform vec3 edge
[8];
1132 vec4 texColor
= texture2D(s_texture
, v_texCoord
);
1133 float nonZeroAlpha
= max(texColor
.a
, 0.00001);
1134 texColor
= vec4(texColor
.rgb
/ nonZeroAlpha
, nonZeroAlpha
);
1135 texColor
= colorMatrix
* texColor
+ colorOffset
;
1136 texColor
.rgb
*= texColor
.a
;
1137 texColor
= clamp(texColor
, 0.0, 1.0);
1138 vec3 pos
= vec3(gl_FragCoord
.xy
, 1);
1139 float a0
= clamp(dot(edge
[0], pos
), 0.0, 1.0);
1140 float a1
= clamp(dot(edge
[1], pos
), 0.0, 1.0);
1141 float a2
= clamp(dot(edge
[2], pos
), 0.0, 1.0);
1142 float a3
= clamp(dot(edge
[3], pos
), 0.0, 1.0);
1143 float a4
= clamp(dot(edge
[4], pos
), 0.0, 1.0);
1144 float a5
= clamp(dot(edge
[5], pos
), 0.0, 1.0);
1145 float a6
= clamp(dot(edge
[6], pos
), 0.0, 1.0);
1146 float a7
= clamp(dot(edge
[7], pos
), 0.0, 1.0);
1147 gl_FragColor
= vec4(texColor
.x
, texColor
.y
, texColor
.z
, texColor
.w
) *
1148 alpha
* min(min(a0
, a2
) * min(a1
, a3
), min(a4
, a6
) * min(a5
, a7
));
1150 ); // NOLINT(whitespace/parens)
1153 FragmentShaderRGBATexAlphaMaskColorMatrix::
1154 FragmentShaderRGBATexAlphaMaskColorMatrix()
1155 : sampler_location_(-1),
1156 mask_sampler_location_(-1),
1157 alpha_location_(-1),
1158 mask_tex_coord_scale_location_(-1) {}
1160 void FragmentShaderRGBATexAlphaMaskColorMatrix::Init(
1161 WebGraphicsContext3D
* context
, unsigned program
, bool usingBindUniform
,
1162 int* baseUniformIndex
) {
1163 static const char* shaderUniforms
[] = {
1167 "maskTexCoordScale",
1168 "maskTexCoordOffset",
1174 GetProgramUniformLocations(context
,
1177 arraysize(shaderUniforms
),
1178 arraysize(locations
),
1183 sampler_location_
= locations
[0];
1184 mask_sampler_location_
= locations
[1];
1185 alpha_location_
= locations
[2];
1186 mask_tex_coord_scale_location_
= locations
[3];
1187 mask_tex_coord_offset_location_
= locations
[4];
1188 color_matrix_location_
= locations
[5];
1189 color_offset_location_
= locations
[6];
1190 DCHECK(sampler_location_
!= -1 && mask_sampler_location_
!= -1 &&
1191 alpha_location_
!= -1 && color_matrix_location_
!= -1 &&
1192 color_offset_location_
!= -1);
1195 std::string
FragmentShaderRGBATexAlphaMaskColorMatrix::GetShaderString(
1196 TexCoordPrecision precision
) const {
1197 return FRAGMENT_SHADER(
1198 precision mediump
float;
1199 varying TexCoordPrecision vec2 v_texCoord
;
1200 uniform sampler2D s_texture
;
1201 uniform sampler2D s_mask
;
1202 uniform vec2 maskTexCoordScale
;
1203 uniform vec2 maskTexCoordOffset
;
1204 uniform mat4 colorMatrix
;
1205 uniform vec4 colorOffset
;
1206 uniform
float alpha
;
1208 vec4 texColor
= texture2D(s_texture
, v_texCoord
);
1209 float nonZeroAlpha
= max(texColor
.a
, 0.00001);
1210 texColor
= vec4(texColor
.rgb
/ nonZeroAlpha
, nonZeroAlpha
);
1211 texColor
= colorMatrix
* texColor
+ colorOffset
;
1212 texColor
.rgb
*= texColor
.a
;
1213 texColor
= clamp(texColor
, 0.0, 1.0);
1214 TexCoordPrecision vec2 maskTexCoord
=
1215 vec2(maskTexCoordOffset
.x
+ v_texCoord
.x
* maskTexCoordScale
.x
,
1216 maskTexCoordOffset
.y
+ v_texCoord
.y
* maskTexCoordScale
.y
);
1217 vec4 maskColor
= texture2D(s_mask
, maskTexCoord
);
1218 gl_FragColor
= vec4(texColor
.x
, texColor
.y
, texColor
.z
, texColor
.w
) *
1219 alpha
* maskColor
.w
;
1221 ); // NOLINT(whitespace/parens)
1224 FragmentShaderYUVVideo::FragmentShaderYUVVideo()
1225 : y_texture_location_(-1),
1226 u_texture_location_(-1),
1227 v_texture_location_(-1),
1228 alpha_location_(-1),
1229 yuv_matrix_location_(-1),
1230 yuv_adj_location_(-1) {}
1232 void FragmentShaderYUVVideo::Init(WebGraphicsContext3D
* context
,
1234 bool using_bind_uniform
,
1235 int* base_uniform_index
) {
1236 static const char* shader_uniforms
[] = {
1246 GetProgramUniformLocations(context
,
1249 arraysize(shader_uniforms
),
1250 arraysize(locations
),
1253 base_uniform_index
);
1255 y_texture_location_
= locations
[0];
1256 u_texture_location_
= locations
[1];
1257 v_texture_location_
= locations
[2];
1258 alpha_location_
= locations
[3];
1259 yuv_matrix_location_
= locations
[4];
1260 yuv_adj_location_
= locations
[5];
1262 DCHECK(y_texture_location_
!= -1 && u_texture_location_
!= -1 &&
1263 v_texture_location_
!= -1 && alpha_location_
!= -1 &&
1264 yuv_matrix_location_
!= -1 && yuv_adj_location_
!= -1);
1267 std::string
FragmentShaderYUVVideo::GetShaderString(
1268 TexCoordPrecision precision
) const {
1269 return FRAGMENT_SHADER(
1270 precision mediump
float;
1271 precision mediump
int;
1272 varying TexCoordPrecision vec2 v_texCoord
;
1273 uniform sampler2D y_texture
;
1274 uniform sampler2D u_texture
;
1275 uniform sampler2D v_texture
;
1276 uniform
float alpha
;
1277 uniform vec3 yuv_adj
;
1278 uniform mat3 yuv_matrix
;
1280 float y_raw
= texture2D(y_texture
, v_texCoord
).x
;
1281 float u_unsigned
= texture2D(u_texture
, v_texCoord
).x
;
1282 float v_unsigned
= texture2D(v_texture
, v_texCoord
).x
;
1283 vec3 yuv
= vec3(y_raw
, u_unsigned
, v_unsigned
) + yuv_adj
;
1284 vec3 rgb
= yuv_matrix
* yuv
;
1285 gl_FragColor
= vec4(rgb
, float(1)) * alpha
; // NOLINT
1287 ); // NOLINT(whitespace/parens)
1290 FragmentShaderColor::FragmentShaderColor()
1291 : color_location_(-1) {}
1293 void FragmentShaderColor::Init(WebGraphicsContext3D
* context
,
1295 bool using_bind_uniform
,
1296 int* base_uniform_index
) {
1297 static const char* shader_uniforms
[] = {
1302 GetProgramUniformLocations(context
,
1305 arraysize(shader_uniforms
),
1306 arraysize(locations
),
1309 base_uniform_index
);
1311 color_location_
= locations
[0];
1312 DCHECK_NE(color_location_
, -1);
1315 std::string
FragmentShaderColor::GetShaderString(
1316 TexCoordPrecision precision
) const {
1317 return FRAGMENT_SHADER(
1318 precision mediump
float;
1321 gl_FragColor
= color
;
1323 ); // NOLINT(whitespace/parens)
1326 FragmentShaderColorAA::FragmentShaderColorAA()
1327 : edge_location_(-1),
1328 color_location_(-1) {}
1330 void FragmentShaderColorAA::Init(WebGraphicsContext3D
* context
,
1332 bool using_bind_uniform
,
1333 int* base_uniform_index
) {
1334 static const char* shader_uniforms
[] = {
1340 GetProgramUniformLocations(context
,
1343 arraysize(shader_uniforms
),
1344 arraysize(locations
),
1347 base_uniform_index
);
1349 edge_location_
= locations
[0];
1350 color_location_
= locations
[1];
1351 DCHECK(edge_location_
!= -1 && color_location_
!= -1);
1354 std::string
FragmentShaderColorAA::GetShaderString(
1355 TexCoordPrecision precision
) const {
1356 return FRAGMENT_SHADER(
1357 precision mediump
float;
1359 uniform vec3 edge
[8];
1361 vec3 pos
= vec3(gl_FragCoord
.xy
, 1);
1362 float a0
= clamp(dot(edge
[0], pos
), 0.0, 1.0);
1363 float a1
= clamp(dot(edge
[1], pos
), 0.0, 1.0);
1364 float a2
= clamp(dot(edge
[2], pos
), 0.0, 1.0);
1365 float a3
= clamp(dot(edge
[3], pos
), 0.0, 1.0);
1366 float a4
= clamp(dot(edge
[4], pos
), 0.0, 1.0);
1367 float a5
= clamp(dot(edge
[5], pos
), 0.0, 1.0);
1368 float a6
= clamp(dot(edge
[6], pos
), 0.0, 1.0);
1369 float a7
= clamp(dot(edge
[7], pos
), 0.0, 1.0);
1370 gl_FragColor
= color
* min(min(a0
, a2
) * min(a1
, a3
),
1371 min(a4
, a6
) * min(a5
, a7
));
1373 ); // NOLINT(whitespace/parens)
1376 FragmentShaderCheckerboard::FragmentShaderCheckerboard()
1377 : alpha_location_(-1),
1378 tex_transform_location_(-1),
1379 frequency_location_(-1) {}
1381 void FragmentShaderCheckerboard::Init(WebGraphicsContext3D
* context
,
1383 bool using_bind_uniform
,
1384 int* base_uniform_index
) {
1385 static const char* shader_uniforms
[] = {
1393 GetProgramUniformLocations(context
,
1396 arraysize(shader_uniforms
),
1397 arraysize(locations
),
1400 base_uniform_index
);
1402 alpha_location_
= locations
[0];
1403 tex_transform_location_
= locations
[1];
1404 frequency_location_
= locations
[2];
1405 color_location_
= locations
[3];
1406 DCHECK(alpha_location_
!= -1 && tex_transform_location_
!= -1 &&
1407 frequency_location_
!= -1 && color_location_
!= -1);
1410 std::string
FragmentShaderCheckerboard::GetShaderString(
1411 TexCoordPrecision precision
) const {
1412 // Shader based on Example 13-17 of "OpenGL ES 2.0 Programming Guide"
1413 // by Munshi, Ginsburg, Shreiner.
1414 return FRAGMENT_SHADER(
1415 precision mediump
float;
1416 precision mediump
int;
1417 varying vec2 v_texCoord
;
1418 uniform
float alpha
;
1419 uniform
float frequency
;
1420 uniform vec4 texTransform
;
1423 vec4 color1
= vec4(1.0, 1.0, 1.0, 1.0);
1424 vec4 color2
= color
;
1426 clamp(v_texCoord
, 0.0, 1.0) * texTransform
.zw
+ texTransform
.xy
;
1427 vec2 coord
= mod(floor(texCoord
* frequency
* 2.0), 2.0);
1428 float picker
= abs(coord
.x
- coord
.y
);
1429 gl_FragColor
= mix(color1
, color2
, picker
) * alpha
;
1431 ); // NOLINT(whitespace/parens)