Add signalSyncPoint to the WebGraphicsContext3D command buffer impls.
[chromium-blink-merge.git] / cc / output / shader.cc
blob2226192d9af9a6840e64be959452c9df71154193
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 requested_precision, std::string shader_string) {
50 switch (requested_precision) {
51 case TexCoordPrecisionHigh:
52 DCHECK_NE(shader_string.find("TexCoordPrecision"), std::string::npos);
53 return
54 "#ifdef GL_FRAGMENT_PRECISION_HIGH\n"
55 " #define TexCoordPrecision highp\n"
56 "#else\n"
57 " #define TexCoordPrecision mediump\n"
58 "#endif\n" +
59 shader_string;
60 case TexCoordPrecisionMedium:
61 DCHECK_NE(shader_string.find("TexCoordPrecision"), std::string::npos);
62 return "#define TexCoordPrecision mediump\n" +
63 shader_string;
64 case TexCoordPrecisionNA:
65 DCHECK_EQ(shader_string.find("TexCoordPrecision"), std::string::npos);
66 DCHECK_EQ(shader_string.find("texture2D"), std::string::npos);
67 return shader_string;
69 return shader_string;
72 static std::string SetVertexTexCoordPrecision(const char* shader_string) {
73 // We unconditionally use highp in the vertex shader since
74 // we are unlikely to be vertex shader bound when drawing large quads.
75 // Also, some vertex shaders mutate the texture coordinate in such a
76 // way that the effective precision might be lower than expected.
77 return "#define TexCoordPrecision highp\n" +
78 std::string(shader_string);
81 TexCoordPrecision TexCoordPrecisionRequired(WebGraphicsContext3D* context,
82 int highp_threshold_min,
83 int x, int y) {
84 // Initialize range and precision with minimum spec values for when
85 // GetShaderPrecisionFormat is a test stub.
86 // TODO(brianderson): Implement better stubs of GetShaderPrecisionFormat
87 // 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 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 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 using_bind_uniform,
502 int* base_uniform_index) {
503 static const char* shader_uniforms[] = {
504 "s_texture",
505 "alpha",
506 "colorMatrix",
507 "colorOffset",
509 int locations[4];
511 GetProgramUniformLocations(context,
512 program,
513 shader_uniforms,
514 arraysize(shader_uniforms),
515 arraysize(locations),
516 locations,
517 using_bind_uniform,
518 base_uniform_index);
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 using_bind_uniform,
1006 int* base_uniform_index) {
1007 static const char* shader_uniforms[] = {
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 shader_uniforms,
1022 arraysize(shader_uniforms),
1023 arraysize(locations),
1024 locations,
1025 using_bind_uniform,
1026 base_uniform_index);
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,
1092 unsigned program,
1093 bool using_bind_uniform,
1094 int* base_uniform_index) {
1095 static const char* shader_uniforms[] = {
1096 "s_texture",
1097 "alpha",
1098 "edge",
1099 "colorMatrix",
1100 "colorOffset",
1102 int locations[5];
1104 GetProgramUniformLocations(context,
1105 program,
1106 shader_uniforms,
1107 arraysize(shader_uniforms),
1108 arraysize(locations),
1109 locations,
1110 using_bind_uniform,
1111 base_uniform_index);
1113 sampler_location_ = locations[0];
1114 alpha_location_ = locations[1];
1115 edge_location_ = locations[2];
1116 color_matrix_location_ = locations[3];
1117 color_offset_location_ = locations[4];
1118 DCHECK(sampler_location_ != -1 && alpha_location_ != -1 &&
1119 edge_location_ != -1 && color_matrix_location_ != -1 &&
1120 color_offset_location_ != -1);
1123 std::string FragmentShaderRGBATexAlphaColorMatrixAA::GetShaderString(
1124 TexCoordPrecision precision) const {
1125 return FRAGMENT_SHADER(
1126 precision mediump float;
1127 varying TexCoordPrecision vec2 v_texCoord;
1128 uniform sampler2D s_texture;
1129 uniform float alpha;
1130 uniform mat4 colorMatrix;
1131 uniform vec4 colorOffset;
1132 uniform vec3 edge[8];
1133 void main() {
1134 vec4 texColor = texture2D(s_texture, v_texCoord);
1135 float nonZeroAlpha = max(texColor.a, 0.00001);
1136 texColor = vec4(texColor.rgb / nonZeroAlpha, nonZeroAlpha);
1137 texColor = colorMatrix * texColor + colorOffset;
1138 texColor.rgb *= texColor.a;
1139 texColor = clamp(texColor, 0.0, 1.0);
1140 vec3 pos = vec3(gl_FragCoord.xy, 1);
1141 float a0 = clamp(dot(edge[0], pos), 0.0, 1.0);
1142 float a1 = clamp(dot(edge[1], pos), 0.0, 1.0);
1143 float a2 = clamp(dot(edge[2], pos), 0.0, 1.0);
1144 float a3 = clamp(dot(edge[3], pos), 0.0, 1.0);
1145 float a4 = clamp(dot(edge[4], pos), 0.0, 1.0);
1146 float a5 = clamp(dot(edge[5], pos), 0.0, 1.0);
1147 float a6 = clamp(dot(edge[6], pos), 0.0, 1.0);
1148 float a7 = clamp(dot(edge[7], pos), 0.0, 1.0);
1149 gl_FragColor = vec4(texColor.x, texColor.y, texColor.z, texColor.w) *
1150 alpha * min(min(a0, a2) * min(a1, a3), min(a4, a6) * min(a5, a7));
1152 ); // NOLINT(whitespace/parens)
1155 FragmentShaderRGBATexAlphaMaskColorMatrix::
1156 FragmentShaderRGBATexAlphaMaskColorMatrix()
1157 : sampler_location_(-1),
1158 mask_sampler_location_(-1),
1159 alpha_location_(-1),
1160 mask_tex_coord_scale_location_(-1) {}
1162 void FragmentShaderRGBATexAlphaMaskColorMatrix::Init(
1163 WebGraphicsContext3D* context,
1164 unsigned program,
1165 bool using_bind_uniform,
1166 int* base_uniform_index) {
1167 static const char* shader_uniforms[] = {
1168 "s_texture",
1169 "s_mask",
1170 "alpha",
1171 "maskTexCoordScale",
1172 "maskTexCoordOffset",
1173 "colorMatrix",
1174 "colorOffset",
1176 int locations[7];
1178 GetProgramUniformLocations(context,
1179 program,
1180 shader_uniforms,
1181 arraysize(shader_uniforms),
1182 arraysize(locations),
1183 locations,
1184 using_bind_uniform,
1185 base_uniform_index);
1187 sampler_location_ = locations[0];
1188 mask_sampler_location_ = locations[1];
1189 alpha_location_ = locations[2];
1190 mask_tex_coord_scale_location_ = locations[3];
1191 mask_tex_coord_offset_location_ = locations[4];
1192 color_matrix_location_ = locations[5];
1193 color_offset_location_ = locations[6];
1194 DCHECK(sampler_location_ != -1 && mask_sampler_location_ != -1 &&
1195 alpha_location_ != -1 && color_matrix_location_ != -1 &&
1196 color_offset_location_ != -1);
1199 std::string FragmentShaderRGBATexAlphaMaskColorMatrix::GetShaderString(
1200 TexCoordPrecision precision) const {
1201 return FRAGMENT_SHADER(
1202 precision mediump float;
1203 varying TexCoordPrecision vec2 v_texCoord;
1204 uniform sampler2D s_texture;
1205 uniform sampler2D s_mask;
1206 uniform vec2 maskTexCoordScale;
1207 uniform vec2 maskTexCoordOffset;
1208 uniform mat4 colorMatrix;
1209 uniform vec4 colorOffset;
1210 uniform float alpha;
1211 void main() {
1212 vec4 texColor = texture2D(s_texture, v_texCoord);
1213 float nonZeroAlpha = max(texColor.a, 0.00001);
1214 texColor = vec4(texColor.rgb / nonZeroAlpha, nonZeroAlpha);
1215 texColor = colorMatrix * texColor + colorOffset;
1216 texColor.rgb *= texColor.a;
1217 texColor = clamp(texColor, 0.0, 1.0);
1218 TexCoordPrecision vec2 maskTexCoord =
1219 vec2(maskTexCoordOffset.x + v_texCoord.x * maskTexCoordScale.x,
1220 maskTexCoordOffset.y + v_texCoord.y * maskTexCoordScale.y);
1221 vec4 maskColor = texture2D(s_mask, maskTexCoord);
1222 gl_FragColor = vec4(texColor.x, texColor.y, texColor.z, texColor.w) *
1223 alpha * maskColor.w;
1225 ); // NOLINT(whitespace/parens)
1228 FragmentShaderYUVVideo::FragmentShaderYUVVideo()
1229 : y_texture_location_(-1),
1230 u_texture_location_(-1),
1231 v_texture_location_(-1),
1232 alpha_location_(-1),
1233 yuv_matrix_location_(-1),
1234 yuv_adj_location_(-1) {}
1236 void FragmentShaderYUVVideo::Init(WebGraphicsContext3D* context,
1237 unsigned program,
1238 bool using_bind_uniform,
1239 int* base_uniform_index) {
1240 static const char* shader_uniforms[] = {
1241 "y_texture",
1242 "u_texture",
1243 "v_texture",
1244 "alpha",
1245 "yuv_matrix",
1246 "yuv_adj",
1248 int locations[6];
1250 GetProgramUniformLocations(context,
1251 program,
1252 shader_uniforms,
1253 arraysize(shader_uniforms),
1254 arraysize(locations),
1255 locations,
1256 using_bind_uniform,
1257 base_uniform_index);
1259 y_texture_location_ = locations[0];
1260 u_texture_location_ = locations[1];
1261 v_texture_location_ = locations[2];
1262 alpha_location_ = locations[3];
1263 yuv_matrix_location_ = locations[4];
1264 yuv_adj_location_ = locations[5];
1266 DCHECK(y_texture_location_ != -1 && u_texture_location_ != -1 &&
1267 v_texture_location_ != -1 && alpha_location_ != -1 &&
1268 yuv_matrix_location_ != -1 && yuv_adj_location_ != -1);
1271 std::string FragmentShaderYUVVideo::GetShaderString(
1272 TexCoordPrecision precision) const {
1273 return FRAGMENT_SHADER(
1274 precision mediump float;
1275 precision mediump int;
1276 varying TexCoordPrecision vec2 v_texCoord;
1277 uniform sampler2D y_texture;
1278 uniform sampler2D u_texture;
1279 uniform sampler2D v_texture;
1280 uniform float alpha;
1281 uniform vec3 yuv_adj;
1282 uniform mat3 yuv_matrix;
1283 void main() {
1284 float y_raw = texture2D(y_texture, v_texCoord).x;
1285 float u_unsigned = texture2D(u_texture, v_texCoord).x;
1286 float v_unsigned = texture2D(v_texture, v_texCoord).x;
1287 vec3 yuv = vec3(y_raw, u_unsigned, v_unsigned) + yuv_adj;
1288 vec3 rgb = yuv_matrix * yuv;
1289 gl_FragColor = vec4(rgb, float(1)) * alpha; // NOLINT
1291 ); // NOLINT(whitespace/parens)
1294 FragmentShaderColor::FragmentShaderColor()
1295 : color_location_(-1) {}
1297 void FragmentShaderColor::Init(WebGraphicsContext3D* context,
1298 unsigned program,
1299 bool using_bind_uniform,
1300 int* base_uniform_index) {
1301 static const char* shader_uniforms[] = {
1302 "color",
1304 int locations[1];
1306 GetProgramUniformLocations(context,
1307 program,
1308 shader_uniforms,
1309 arraysize(shader_uniforms),
1310 arraysize(locations),
1311 locations,
1312 using_bind_uniform,
1313 base_uniform_index);
1315 color_location_ = locations[0];
1316 DCHECK_NE(color_location_, -1);
1319 std::string FragmentShaderColor::GetShaderString(
1320 TexCoordPrecision precision) const {
1321 return FRAGMENT_SHADER(
1322 precision mediump float;
1323 uniform vec4 color;
1324 void main() {
1325 gl_FragColor = color;
1327 ); // NOLINT(whitespace/parens)
1330 FragmentShaderColorAA::FragmentShaderColorAA()
1331 : edge_location_(-1),
1332 color_location_(-1) {}
1334 void FragmentShaderColorAA::Init(WebGraphicsContext3D* context,
1335 unsigned program,
1336 bool using_bind_uniform,
1337 int* base_uniform_index) {
1338 static const char* shader_uniforms[] = {
1339 "edge",
1340 "color",
1342 int locations[2];
1344 GetProgramUniformLocations(context,
1345 program,
1346 shader_uniforms,
1347 arraysize(shader_uniforms),
1348 arraysize(locations),
1349 locations,
1350 using_bind_uniform,
1351 base_uniform_index);
1353 edge_location_ = locations[0];
1354 color_location_ = locations[1];
1355 DCHECK(edge_location_ != -1 && color_location_ != -1);
1358 std::string FragmentShaderColorAA::GetShaderString(
1359 TexCoordPrecision precision) const {
1360 return FRAGMENT_SHADER(
1361 precision mediump float;
1362 uniform vec4 color;
1363 uniform vec3 edge[8];
1364 void main() {
1365 vec3 pos = vec3(gl_FragCoord.xy, 1);
1366 float a0 = clamp(dot(edge[0], pos), 0.0, 1.0);
1367 float a1 = clamp(dot(edge[1], pos), 0.0, 1.0);
1368 float a2 = clamp(dot(edge[2], pos), 0.0, 1.0);
1369 float a3 = clamp(dot(edge[3], pos), 0.0, 1.0);
1370 float a4 = clamp(dot(edge[4], pos), 0.0, 1.0);
1371 float a5 = clamp(dot(edge[5], pos), 0.0, 1.0);
1372 float a6 = clamp(dot(edge[6], pos), 0.0, 1.0);
1373 float a7 = clamp(dot(edge[7], pos), 0.0, 1.0);
1374 gl_FragColor = color * min(min(a0, a2) * min(a1, a3),
1375 min(a4, a6) * min(a5, a7));
1377 ); // NOLINT(whitespace/parens)
1380 FragmentShaderCheckerboard::FragmentShaderCheckerboard()
1381 : alpha_location_(-1),
1382 tex_transform_location_(-1),
1383 frequency_location_(-1) {}
1385 void FragmentShaderCheckerboard::Init(WebGraphicsContext3D* context,
1386 unsigned program,
1387 bool using_bind_uniform,
1388 int* base_uniform_index) {
1389 static const char* shader_uniforms[] = {
1390 "alpha",
1391 "texTransform",
1392 "frequency",
1393 "color",
1395 int locations[4];
1397 GetProgramUniformLocations(context,
1398 program,
1399 shader_uniforms,
1400 arraysize(shader_uniforms),
1401 arraysize(locations),
1402 locations,
1403 using_bind_uniform,
1404 base_uniform_index);
1406 alpha_location_ = locations[0];
1407 tex_transform_location_ = locations[1];
1408 frequency_location_ = locations[2];
1409 color_location_ = locations[3];
1410 DCHECK(alpha_location_ != -1 && tex_transform_location_ != -1 &&
1411 frequency_location_ != -1 && color_location_ != -1);
1414 std::string FragmentShaderCheckerboard::GetShaderString(
1415 TexCoordPrecision precision) const {
1416 // Shader based on Example 13-17 of "OpenGL ES 2.0 Programming Guide"
1417 // by Munshi, Ginsburg, Shreiner.
1418 return FRAGMENT_SHADER(
1419 precision mediump float;
1420 precision mediump int;
1421 varying vec2 v_texCoord;
1422 uniform float alpha;
1423 uniform float frequency;
1424 uniform vec4 texTransform;
1425 uniform vec4 color;
1426 void main() {
1427 vec4 color1 = vec4(1.0, 1.0, 1.0, 1.0);
1428 vec4 color2 = color;
1429 vec2 texCoord =
1430 clamp(v_texCoord, 0.0, 1.0) * texTransform.zw + texTransform.xy;
1431 vec2 coord = mod(floor(texCoord * frequency * 2.0), 2.0);
1432 float picker = abs(coord.x - coord.y);
1433 gl_FragColor = mix(color1, color2, picker) * alpha;
1435 ); // NOLINT(whitespace/parens)
1438 } // namespace cc