Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / cc / output / shader.cc
blob7afa8a321ea28d0ecbbe9366019565f4ea816895
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"
7 #include <algorithm>
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;
21 namespace cc {
23 namespace {
25 static void GetProgramUniformLocations(WebGraphicsContext3D* context,
26 unsigned program,
27 const char** shader_uniforms,
28 size_t count,
29 size_t max_locations,
30 int* locations,
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]);
41 } else {
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"
56 "#else\n"
57 " #define TexCoordPrecision mediump\n"
58 "#endif\n" +
59 shaderString;
60 case TexCoordPrecisionMedium:
61 DCHECK_NE(shaderString.find("TexCoordPrecision"), std::string::npos);
62 return "#define TexCoordPrecision mediump\n" +
63 shaderString;
64 case TexCoordPrecisionNA:
65 DCHECK_EQ(shaderString.find("TexCoordPrecision"), std::string::npos);
66 DCHECK_EQ(shaderString.find("texture2D"), std::string::npos);
67 return shaderString;
69 return shaderString;
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,
84 int x, int y) {
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 };
89 GLint precision = 10;
90 GLC(context, context->getShaderPrecisionFormat(GL_FRAGMENT_SHADER,
91 GL_MEDIUM_FLOAT,
92 range, &precision));
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;
99 } // namespace
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,
119 unsigned program,
120 bool using_bind_uniform,
121 int* base_uniform_index) {
122 static const char* shader_uniforms[] = {
123 "matrix",
125 int locations[1];
127 GetProgramUniformLocations(context,
128 program,
129 shader_uniforms,
130 arraysize(shader_uniforms),
131 arraysize(locations),
132 locations,
133 using_bind_uniform,
134 base_uniform_index);
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;
144 uniform mat4 matrix;
145 varying TexCoordPrecision vec2 v_texCoord;
146 void main() {
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,
158 unsigned program,
159 bool using_bind_uniform,
160 int* base_uniform_index) {
161 static const char* shader_uniforms[] = {
162 "matrix",
163 "texScale",
165 int locations[2];
167 GetProgramUniformLocations(context,
168 program,
169 shader_uniforms,
170 arraysize(shader_uniforms),
171 arraysize(locations),
172 locations,
173 using_bind_uniform,
174 base_uniform_index);
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;
186 uniform mat4 matrix;
187 varying TexCoordPrecision vec2 v_texCoord;
188 uniform TexCoordPrecision vec2 texScale;
189 void main() {
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,
200 unsigned program,
201 bool using_bind_uniform,
202 int* base_uniform_index) {
203 static const char* shader_uniforms[] = {
204 "matrix",
206 int locations[1];
208 GetProgramUniformLocations(context,
209 program,
210 shader_uniforms,
211 arraysize(shader_uniforms),
212 arraysize(locations),
213 locations,
214 using_bind_uniform,
215 base_uniform_index);
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;
224 uniform mat4 matrix;
225 void main() {
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,
237 unsigned program,
238 bool using_bind_uniform,
239 int* base_uniform_index) {
240 static const char* shader_uniforms[] = {
241 "matrix",
242 "texTransform",
243 "opacity",
245 int locations[3];
247 GetProgramUniformLocations(context,
248 program,
249 shader_uniforms,
250 arraysize(shader_uniforms),
251 arraysize(locations),
252 locations,
253 using_bind_uniform,
254 base_uniform_index);
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;
273 void main() {
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;
293 void main() {
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;
308 void main() {
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),
317 point_location_(-1),
318 tex_scale_location_(-1) {}
320 void VertexShaderQuad::Init(WebGraphicsContext3D* context,
321 unsigned program,
322 bool using_bind_uniform,
323 int* base_uniform_index) {
324 static const char* shader_uniforms[] = {
325 "matrix",
326 "point",
327 "texScale",
329 int locations[3];
331 GetProgramUniformLocations(context,
332 program,
333 shader_uniforms,
334 arraysize(shader_uniforms),
335 arraysize(locations),
336 locations,
337 using_bind_uniform,
338 base_uniform_index);
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;
352 uniform mat4 matrix;
353 uniform TexCoordPrecision vec2 point[4];
354 uniform TexCoordPrecision vec2 texScale;
355 varying TexCoordPrecision vec2 v_texCoord;
356 void main() {
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),
371 point_location_(-1),
372 vertex_tex_transform_location_(-1) {}
374 void VertexShaderTile::Init(WebGraphicsContext3D* context,
375 unsigned program,
376 bool using_bind_uniform,
377 int* base_uniform_index) {
378 static const char* shader_uniforms[] = {
379 "matrix",
380 "point",
381 "vertexTexTransform",
383 int locations[3];
385 GetProgramUniformLocations(context,
386 program,
387 shader_uniforms,
388 arraysize(shader_uniforms),
389 arraysize(locations),
390 locations,
391 using_bind_uniform,
392 base_uniform_index);
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;
405 uniform mat4 matrix;
406 uniform TexCoordPrecision vec2 point[4];
407 uniform TexCoordPrecision vec4 vertexTexTransform;
408 varying TexCoordPrecision vec2 v_texCoord;
409 void main() {
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,
427 unsigned program,
428 bool using_bind_uniform,
429 int* base_uniform_index) {
430 static const char* shader_uniforms[] = {
431 "matrix",
432 "texMatrix",
434 int locations[2];
436 GetProgramUniformLocations(context,
437 program,
438 shader_uniforms,
439 arraysize(shader_uniforms),
440 arraysize(locations),
441 locations,
442 using_bind_uniform,
443 base_uniform_index);
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;
454 uniform mat4 matrix;
455 uniform TexCoordPrecision mat4 texMatrix;
456 varying TexCoordPrecision vec2 v_texCoord;
457 void main() {
458 gl_Position = matrix * a_position;
459 v_texCoord =
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,
470 unsigned program,
471 bool using_bind_uniform,
472 int* base_uniform_index) {
473 static const char* shader_uniforms[] = {
474 "s_texture",
475 "alpha",
477 int locations[2];
479 GetProgramUniformLocations(context,
480 program,
481 shader_uniforms,
482 arraysize(shader_uniforms),
483 arraysize(locations),
484 locations,
485 using_bind_uniform,
486 base_uniform_index);
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),
495 alpha_location_(-1),
496 color_matrix_location_(-1),
497 color_offset_location_(-1) {}
499 void FragmentTexColorMatrixAlphaBinding::Init(WebGraphicsContext3D* context,
500 unsigned program,
501 bool usingBindUniform,
502 int* baseUniformIndex) {
503 static const char* shaderUniforms[] = {
504 "s_texture",
505 "alpha",
506 "colorMatrix",
507 "colorOffset",
509 int locations[4];
511 GetProgramUniformLocations(context,
512 program,
513 shaderUniforms,
514 arraysize(shaderUniforms),
515 arraysize(locations),
516 locations,
517 usingBindUniform,
518 baseUniformIndex);
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,
532 unsigned program,
533 bool using_bind_uniform,
534 int* base_uniform_index) {
535 static const char* shader_uniforms[] = {
536 "s_texture",
538 int locations[1];
540 GetProgramUniformLocations(context,
541 program,
542 shader_uniforms,
543 arraysize(shader_uniforms),
544 arraysize(locations),
545 locations,
546 using_bind_uniform,
547 base_uniform_index);
549 sampler_location_ = locations[0];
550 DCHECK_NE(sampler_location_, -1);
553 FragmentShaderOESImageExternal::FragmentShaderOESImageExternal()
554 : sampler_location_(-1) {}
556 bool FragmentShaderOESImageExternal::Init(WebGraphicsContext3D* context,
557 unsigned program,
558 bool using_bind_uniform,
559 int* base_uniform_index) {
560 static const char* shader_uniforms[] = {
561 "s_texture",
563 int locations[1];
565 GetProgramUniformLocations(context,
566 program,
567 shader_uniforms,
568 arraysize(shader_uniforms),
569 arraysize(locations),
570 locations,
571 using_bind_uniform,
572 base_uniform_index);
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" +
582 FRAGMENT_SHADER(
583 precision mediump float;
584 varying TexCoordPrecision vec2 v_texCoord;
585 uniform samplerExternalOES s_texture;
586 void main() {
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;
599 uniform float alpha;
600 void main() {
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;
613 uniform float alpha;
614 uniform mat4 colorMatrix;
615 uniform vec4 colorOffset;
616 void main() {
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;
635 void main() {
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" +
645 FRAGMENT_SHADER(
646 precision mediump float;
647 varying TexCoordPrecision vec2 v_texCoord;
648 varying float v_alpha;
649 uniform sampler2DRect s_texture;
650 void main() {
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;
663 void main() {
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;
676 void main() {
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;
688 uniform float alpha;
689 void main() {
690 vec4 texColor = texture2D(s_texture, v_texCoord);
691 gl_FragColor =
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;
703 void main() {
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),
712 alpha_location_(-1),
713 edge_location_(-1) {}
715 void FragmentShaderRGBATexAlphaAA::Init(WebGraphicsContext3D* context,
716 unsigned program,
717 bool using_bind_uniform,
718 int* base_uniform_index) {
719 static const char* shader_uniforms[] = {
720 "s_texture",
721 "alpha",
722 "edge",
724 int locations[3];
726 GetProgramUniformLocations(context,
727 program,
728 shader_uniforms,
729 arraysize(shader_uniforms),
730 arraysize(locations),
731 locations,
732 using_bind_uniform,
733 base_uniform_index);
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;
748 uniform float alpha;
749 uniform vec3 edge[8];
750 void main() {
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),
769 alpha_location_(-1),
770 fragment_tex_transform_location_(-1),
771 edge_location_(-1) {}
773 void FragmentTexClampAlphaAABinding::Init(WebGraphicsContext3D* context,
774 unsigned program,
775 bool using_bind_uniform,
776 int* base_uniform_index) {
777 static const char* shader_uniforms[] = {
778 "s_texture",
779 "alpha",
780 "fragmentTexTransform",
781 "edge",
783 int locations[4];
785 GetProgramUniformLocations(context,
786 program,
787 shader_uniforms,
788 arraysize(shader_uniforms),
789 arraysize(locations),
790 locations,
791 using_bind_uniform,
792 base_uniform_index);
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;
808 uniform float alpha;
809 uniform TexCoordPrecision vec4 fragmentTexTransform;
810 uniform vec3 edge[8];
811 void main() {
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;
837 uniform float alpha;
838 uniform TexCoordPrecision vec4 fragmentTexTransform;
839 uniform vec3 edge[8];
840 void main() {
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),
863 alpha_location_(-1),
864 mask_tex_coord_scale_location_(-1) {}
866 void FragmentShaderRGBATexAlphaMask::Init(WebGraphicsContext3D* context,
867 unsigned program,
868 bool using_bind_uniform,
869 int* base_uniform_index) {
870 static const char* shader_uniforms[] = {
871 "s_texture",
872 "s_mask",
873 "alpha",
874 "maskTexCoordScale",
875 "maskTexCoordOffset",
877 int locations[5];
879 GetProgramUniformLocations(context,
880 program,
881 shader_uniforms,
882 arraysize(shader_uniforms),
883 arraysize(locations),
884 locations,
885 using_bind_uniform,
886 base_uniform_index);
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;
906 uniform float alpha;
907 void main() {
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),
922 alpha_location_(-1),
923 edge_location_(-1),
924 mask_tex_coord_scale_location_(-1) {}
926 void FragmentShaderRGBATexAlphaMaskAA::Init(WebGraphicsContext3D* context,
927 unsigned program,
928 bool using_bind_uniform,
929 int* base_uniform_index) {
930 static const char* shader_uniforms[] = {
931 "s_texture",
932 "s_mask",
933 "alpha",
934 "edge",
935 "maskTexCoordScale",
936 "maskTexCoordOffset",
938 int locations[6];
940 GetProgramUniformLocations(context,
941 program,
942 shader_uniforms,
943 arraysize(shader_uniforms),
944 arraysize(locations),
945 locations,
946 using_bind_uniform,
947 base_uniform_index);
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;
968 uniform float alpha;
969 uniform vec3 edge[8];
970 void main() {
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),
996 alpha_location_(-1),
997 edge_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,
1004 unsigned program,
1005 bool usingBindUniform,
1006 int* baseUniformIndex) {
1007 static const char* shaderUniforms[] = {
1008 "s_texture",
1009 "s_mask",
1010 "alpha",
1011 "edge",
1012 "maskTexCoordScale",
1013 "maskTexCoordOffset",
1014 "colorMatrix",
1015 "colorOffset",
1017 int locations[8];
1019 GetProgramUniformLocations(context,
1020 program,
1021 shaderUniforms,
1022 arraysize(shaderUniforms),
1023 arraysize(locations),
1024 locations,
1025 usingBindUniform,
1026 baseUniformIndex);
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];
1054 void main() {
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);
1074 gl_FragColor =
1075 vec4(texColor.x, texColor.y, texColor.z, texColor.w) *
1076 alpha * maskColor.w * min(min(a0, a2) * min(a1, a3), min(a4, a6) *
1077 min(a5, a7));
1079 ); // NOLINT(whitespace/parens)
1082 FragmentShaderRGBATexAlphaColorMatrixAA::
1083 FragmentShaderRGBATexAlphaColorMatrixAA()
1084 : sampler_location_(-1),
1085 alpha_location_(-1),
1086 edge_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[] = {
1094 "s_texture",
1095 "alpha",
1096 "edge",
1097 "colorMatrix",
1098 "colorOffset",
1100 int locations[5];
1102 GetProgramUniformLocations(context,
1103 program,
1104 shaderUniforms,
1105 arraysize(shaderUniforms),
1106 arraysize(locations),
1107 locations,
1108 usingBindUniform,
1109 baseUniformIndex);
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];
1131 void main() {
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[] = {
1164 "s_texture",
1165 "s_mask",
1166 "alpha",
1167 "maskTexCoordScale",
1168 "maskTexCoordOffset",
1169 "colorMatrix",
1170 "colorOffset",
1172 int locations[7];
1174 GetProgramUniformLocations(context,
1175 program,
1176 shaderUniforms,
1177 arraysize(shaderUniforms),
1178 arraysize(locations),
1179 locations,
1180 usingBindUniform,
1181 baseUniformIndex);
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;
1207 void main() {
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,
1233 unsigned program,
1234 bool using_bind_uniform,
1235 int* base_uniform_index) {
1236 static const char* shader_uniforms[] = {
1237 "y_texture",
1238 "u_texture",
1239 "v_texture",
1240 "alpha",
1241 "yuv_matrix",
1242 "yuv_adj",
1244 int locations[6];
1246 GetProgramUniformLocations(context,
1247 program,
1248 shader_uniforms,
1249 arraysize(shader_uniforms),
1250 arraysize(locations),
1251 locations,
1252 using_bind_uniform,
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;
1279 void main() {
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,
1294 unsigned program,
1295 bool using_bind_uniform,
1296 int* base_uniform_index) {
1297 static const char* shader_uniforms[] = {
1298 "color",
1300 int locations[1];
1302 GetProgramUniformLocations(context,
1303 program,
1304 shader_uniforms,
1305 arraysize(shader_uniforms),
1306 arraysize(locations),
1307 locations,
1308 using_bind_uniform,
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;
1319 uniform vec4 color;
1320 void main() {
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,
1331 unsigned program,
1332 bool using_bind_uniform,
1333 int* base_uniform_index) {
1334 static const char* shader_uniforms[] = {
1335 "edge",
1336 "color",
1338 int locations[2];
1340 GetProgramUniformLocations(context,
1341 program,
1342 shader_uniforms,
1343 arraysize(shader_uniforms),
1344 arraysize(locations),
1345 locations,
1346 using_bind_uniform,
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;
1358 uniform vec4 color;
1359 uniform vec3 edge[8];
1360 void main() {
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,
1382 unsigned program,
1383 bool using_bind_uniform,
1384 int* base_uniform_index) {
1385 static const char* shader_uniforms[] = {
1386 "alpha",
1387 "texTransform",
1388 "frequency",
1389 "color",
1391 int locations[4];
1393 GetProgramUniformLocations(context,
1394 program,
1395 shader_uniforms,
1396 arraysize(shader_uniforms),
1397 arraysize(locations),
1398 locations,
1399 using_bind_uniform,
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;
1421 uniform vec4 color;
1422 void main() {
1423 vec4 color1 = vec4(1.0, 1.0, 1.0, 1.0);
1424 vec4 color2 = color;
1425 vec2 texCoord =
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)
1434 } // namespace cc