Depend on stored sync session GUID for Android.
[chromium-blink-merge.git] / cc / shader.cc
blobc84d88aeccc948abe96257d14778f0a843d6cb20
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/shader.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include <public/WebGraphicsContext3D.h>
11 #define SHADER0(Src) #Src
12 #define SHADER(Src) SHADER0(Src)
14 using WebKit::WebGraphicsContext3D;
16 namespace cc {
18 namespace {
20 static void getProgramUniformLocations(WebGraphicsContext3D* context, unsigned program, const char** shaderUniforms, size_t count, size_t maxLocations, int* locations, bool usingBindUniform, int* baseUniformIndex)
22 for (size_t uniformIndex = 0; uniformIndex < count; uniformIndex ++) {
23 DCHECK(uniformIndex < maxLocations);
25 if (usingBindUniform) {
26 locations[uniformIndex] = (*baseUniformIndex)++;
27 context->bindUniformLocationCHROMIUM(program, locations[uniformIndex], shaderUniforms[uniformIndex]);
28 } else
29 locations[uniformIndex] = context->getUniformLocation(program, shaderUniforms[uniformIndex]);
35 VertexShaderPosTex::VertexShaderPosTex()
36 : m_matrixLocation(-1)
40 void VertexShaderPosTex::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
42 static const char* shaderUniforms[] = {
43 "matrix",
45 int locations[1];
47 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
49 m_matrixLocation = locations[0];
50 DCHECK(m_matrixLocation != -1);
53 std::string VertexShaderPosTex::getShaderString() const
55 return SHADER(
56 attribute vec4 a_position;
57 attribute vec2 a_texCoord;
58 uniform mat4 matrix;
59 varying vec2 v_texCoord;
60 void main()
62 gl_Position = matrix * a_position;
63 v_texCoord = a_texCoord;
68 VertexShaderPosTexYUVStretch::VertexShaderPosTexYUVStretch()
69 : m_matrixLocation(-1)
70 , m_texScaleLocation(-1)
74 void VertexShaderPosTexYUVStretch::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
76 static const char* shaderUniforms[] = {
77 "matrix",
78 "texScale",
80 int locations[2];
82 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
84 m_matrixLocation = locations[0];
85 m_texScaleLocation = locations[1];
86 DCHECK(m_matrixLocation != -1 && m_texScaleLocation != -1);
89 std::string VertexShaderPosTexYUVStretch::getShaderString() const
91 return SHADER(
92 precision mediump float;
93 attribute vec4 a_position;
94 attribute vec2 a_texCoord;
95 uniform mat4 matrix;
96 varying vec2 v_texCoord;
97 uniform vec2 texScale;
98 void main()
100 gl_Position = matrix * a_position;
101 v_texCoord = a_texCoord * texScale;
106 VertexShaderPos::VertexShaderPos()
107 : m_matrixLocation(-1)
111 void VertexShaderPos::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
113 static const char* shaderUniforms[] = {
114 "matrix",
116 int locations[1];
118 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
120 m_matrixLocation = locations[0];
121 DCHECK(m_matrixLocation != -1);
124 std::string VertexShaderPos::getShaderString() const
126 return SHADER(
127 attribute vec4 a_position;
128 uniform mat4 matrix;
129 void main()
131 gl_Position = matrix * a_position;
136 VertexShaderPosTexTransform::VertexShaderPosTexTransform()
137 : m_matrixLocation(-1)
138 , m_texTransformLocation(-1)
142 void VertexShaderPosTexTransform::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
144 static const char* shaderUniforms[] = {
145 "matrix",
146 "texTransform",
148 int locations[2];
150 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
152 m_matrixLocation = locations[0];
153 m_texTransformLocation = locations[1];
154 DCHECK(m_matrixLocation != -1 && m_texTransformLocation != -1);
157 std::string VertexShaderPosTexTransform::getShaderString() const
159 return SHADER(
160 attribute vec4 a_position;
161 attribute vec2 a_texCoord;
162 attribute float a_index;
163 uniform mat4 matrix[8];
164 uniform vec4 texTransform[8];
165 varying vec2 v_texCoord;
166 void main()
168 gl_Position = matrix[int(a_index)] * a_position;
169 v_texCoord = a_texCoord * texTransform[int(a_index)].zw + texTransform[int(a_index)].xy;
174 VertexShaderQuad::VertexShaderQuad()
175 : m_matrixLocation(-1)
176 , m_pointLocation(-1)
180 std::string VertexShaderPosTexIdentity::getShaderString() const
182 return SHADER(
183 attribute vec4 a_position;
184 varying vec2 v_texCoord;
185 void main()
187 gl_Position = a_position;
188 v_texCoord = (a_position.xy + vec2(1.0)) * 0.5;
193 void VertexShaderQuad::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
195 static const char* shaderUniforms[] = {
196 "matrix",
197 "point",
199 int locations[2];
201 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
203 m_matrixLocation = locations[0];
204 m_pointLocation = locations[1];
205 DCHECK(m_matrixLocation != -1 && m_pointLocation != -1);
208 std::string VertexShaderQuad::getShaderString() const
210 return SHADER(
211 attribute vec4 a_position;
212 attribute vec2 a_texCoord;
213 uniform mat4 matrix;
214 uniform vec2 point[4];
215 varying vec2 v_texCoord;
216 void main()
218 vec2 complement = abs(a_texCoord - 1.0);
219 vec4 pos = vec4(0.0, 0.0, a_position.z, a_position.w);
220 pos.xy += (complement.x * complement.y) * point[0];
221 pos.xy += (a_texCoord.x * complement.y) * point[1];
222 pos.xy += (a_texCoord.x * a_texCoord.y) * point[2];
223 pos.xy += (complement.x * a_texCoord.y) * point[3];
224 gl_Position = matrix * pos;
225 v_texCoord = pos.xy + vec2(0.5);
230 VertexShaderTile::VertexShaderTile()
231 : m_matrixLocation(-1)
232 , m_pointLocation(-1)
233 , m_vertexTexTransformLocation(-1)
237 void VertexShaderTile::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
239 static const char* shaderUniforms[] = {
240 "matrix",
241 "point",
242 "vertexTexTransform",
244 int locations[3];
246 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
248 m_matrixLocation = locations[0];
249 m_pointLocation = locations[1];
250 m_vertexTexTransformLocation = locations[2];
251 DCHECK(m_matrixLocation != -1 && m_pointLocation != -1 && m_vertexTexTransformLocation != -1);
254 std::string VertexShaderTile::getShaderString() const
256 return SHADER(
257 attribute vec4 a_position;
258 attribute vec2 a_texCoord;
259 uniform mat4 matrix;
260 uniform vec2 point[4];
261 uniform vec4 vertexTexTransform;
262 varying vec2 v_texCoord;
263 void main()
265 vec2 complement = abs(a_texCoord - 1.0);
266 vec4 pos = vec4(0.0, 0.0, a_position.z, a_position.w);
267 pos.xy += (complement.x * complement.y) * point[0];
268 pos.xy += (a_texCoord.x * complement.y) * point[1];
269 pos.xy += (a_texCoord.x * a_texCoord.y) * point[2];
270 pos.xy += (complement.x * a_texCoord.y) * point[3];
271 gl_Position = matrix * pos;
272 v_texCoord = pos.xy * vertexTexTransform.zw + vertexTexTransform.xy;
277 VertexShaderVideoTransform::VertexShaderVideoTransform()
278 : m_matrixLocation(-1)
279 , m_texMatrixLocation(-1)
283 bool VertexShaderVideoTransform::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
285 static const char* shaderUniforms[] = {
286 "matrix",
287 "texMatrix",
289 int locations[2];
291 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
293 m_matrixLocation = locations[0];
294 m_texMatrixLocation = locations[1];
295 return m_matrixLocation != -1 && m_texMatrixLocation != -1;
298 std::string VertexShaderVideoTransform::getShaderString() const
300 return SHADER(
301 attribute vec4 a_position;
302 attribute vec2 a_texCoord;
303 uniform mat4 matrix;
304 uniform mat4 texMatrix;
305 varying vec2 v_texCoord;
306 void main()
308 gl_Position = matrix * a_position;
309 v_texCoord = vec2(texMatrix * vec4(a_texCoord.x, 1.0 - a_texCoord.y, 0.0, 1.0));
314 FragmentTexAlphaBinding::FragmentTexAlphaBinding()
315 : m_samplerLocation(-1)
316 , m_alphaLocation(-1)
320 void FragmentTexAlphaBinding::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
322 static const char* shaderUniforms[] = {
323 "s_texture",
324 "alpha",
326 int locations[2];
328 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
330 m_samplerLocation = locations[0];
331 m_alphaLocation = locations[1];
332 DCHECK(m_samplerLocation != -1 && m_alphaLocation != -1);
335 FragmentTexOpaqueBinding::FragmentTexOpaqueBinding()
336 : m_samplerLocation(-1)
340 void FragmentTexOpaqueBinding::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
342 static const char* shaderUniforms[] = {
343 "s_texture",
345 int locations[1];
347 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
349 m_samplerLocation = locations[0];
350 DCHECK(m_samplerLocation != -1);
353 std::string FragmentShaderRGBATexFlipAlpha::getShaderString() const
355 return SHADER(
356 precision mediump float;
357 varying vec2 v_texCoord;
358 uniform sampler2D s_texture;
359 uniform float alpha;
360 void main()
362 vec4 texColor = texture2D(s_texture, vec2(v_texCoord.x, 1.0 - v_texCoord.y));
363 gl_FragColor = vec4(texColor.x, texColor.y, texColor.z, texColor.w) * alpha;
368 bool FragmentShaderOESImageExternal::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
370 static const char* shaderUniforms[] = {
371 "s_texture",
373 int locations[1];
375 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
377 m_samplerLocation = locations[0];
378 return m_samplerLocation != -1;
381 std::string FragmentShaderOESImageExternal::getShaderString() const
383 // Cannot use the SHADER() macro because of the '#' char
384 return "#extension GL_OES_EGL_image_external : require \n"
385 "precision mediump float;\n"
386 "varying vec2 v_texCoord;\n"
387 "uniform samplerExternalOES s_texture;\n"
388 "void main()\n"
389 "{\n"
390 " vec4 texColor = texture2D(s_texture, v_texCoord);\n"
391 " gl_FragColor = vec4(texColor.x, texColor.y, texColor.z, texColor.w);\n"
392 "}\n";
395 std::string FragmentShaderRGBATexAlpha::getShaderString() const
397 return SHADER(
398 precision mediump float;
399 varying vec2 v_texCoord;
400 uniform sampler2D s_texture;
401 uniform float alpha;
402 void main()
404 vec4 texColor = texture2D(s_texture, v_texCoord);
405 gl_FragColor = texColor * alpha;
410 std::string FragmentShaderRGBATexRectFlipAlpha::getShaderString() const
412 // This must be paired with VertexShaderPosTexTransform to pick up the texTransform uniform.
413 // The necessary #extension preprocessing directive breaks the SHADER and SHADER0 macros.
414 return "#extension GL_ARB_texture_rectangle : require\n"
415 "precision mediump float;\n"
416 "varying vec2 v_texCoord;\n"
417 "uniform vec4 texTransform;\n"
418 "uniform sampler2DRect s_texture;\n"
419 "uniform float alpha;\n"
420 "void main()\n"
421 "{\n"
422 " vec4 texColor = texture2DRect(s_texture, vec2(v_texCoord.x, texTransform.w - v_texCoord.y));\n"
423 " gl_FragColor = vec4(texColor.x, texColor.y, texColor.z, texColor.w) * alpha;\n"
424 "}\n";
427 std::string FragmentShaderRGBATexRectAlpha::getShaderString() const
429 return "#extension GL_ARB_texture_rectangle : require\n"
430 "precision mediump float;\n"
431 "varying vec2 v_texCoord;\n"
432 "uniform sampler2DRect s_texture;\n"
433 "uniform float alpha;\n"
434 "void main()\n"
435 "{\n"
436 " vec4 texColor = texture2DRect(s_texture, v_texCoord);\n"
437 " gl_FragColor = texColor * alpha;\n"
438 "}\n";
441 std::string FragmentShaderRGBATexOpaque::getShaderString() const
443 return SHADER(
444 precision mediump float;
445 varying vec2 v_texCoord;
446 uniform sampler2D s_texture;
447 void main()
449 vec4 texColor = texture2D(s_texture, v_texCoord);
450 gl_FragColor = vec4(texColor.rgb, 1.0);
455 std::string FragmentShaderRGBATex::getShaderString() const
457 return SHADER(
458 precision mediump float;
459 varying vec2 v_texCoord;
460 uniform sampler2D s_texture;
461 void main()
463 gl_FragColor = texture2D(s_texture, v_texCoord);
468 std::string FragmentShaderRGBATexSwizzleAlpha::getShaderString() const
470 return SHADER(
471 precision mediump float;
472 varying vec2 v_texCoord;
473 uniform sampler2D s_texture;
474 uniform float alpha;
475 void main()
477 vec4 texColor = texture2D(s_texture, v_texCoord);
478 gl_FragColor = vec4(texColor.z, texColor.y, texColor.x, texColor.w) * alpha;
483 std::string FragmentShaderRGBATexSwizzleOpaque::getShaderString() const
485 return SHADER(
486 precision mediump float;
487 varying vec2 v_texCoord;
488 uniform sampler2D s_texture;
489 void main()
491 vec4 texColor = texture2D(s_texture, v_texCoord);
492 gl_FragColor = vec4(texColor.z, texColor.y, texColor.x, 1.0);
497 FragmentShaderRGBATexAlphaAA::FragmentShaderRGBATexAlphaAA()
498 : m_samplerLocation(-1)
499 , m_alphaLocation(-1)
500 , m_edgeLocation(-1)
504 void FragmentShaderRGBATexAlphaAA::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
506 static const char* shaderUniforms[] = {
507 "s_texture",
508 "alpha",
509 "edge",
511 int locations[3];
513 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
515 m_samplerLocation = locations[0];
516 m_alphaLocation = locations[1];
517 m_edgeLocation = locations[2];
518 DCHECK(m_samplerLocation != -1 && m_alphaLocation != -1 && m_edgeLocation != -1);
521 std::string FragmentShaderRGBATexAlphaAA::getShaderString() const
523 return SHADER(
524 precision mediump float;
525 varying vec2 v_texCoord;
526 uniform sampler2D s_texture;
527 uniform float alpha;
528 uniform vec3 edge[8];
529 void main()
531 vec4 texColor = texture2D(s_texture, v_texCoord);
532 vec3 pos = vec3(gl_FragCoord.xy, 1);
533 float a0 = clamp(dot(edge[0], pos), 0.0, 1.0);
534 float a1 = clamp(dot(edge[1], pos), 0.0, 1.0);
535 float a2 = clamp(dot(edge[2], pos), 0.0, 1.0);
536 float a3 = clamp(dot(edge[3], pos), 0.0, 1.0);
537 float a4 = clamp(dot(edge[4], pos), 0.0, 1.0);
538 float a5 = clamp(dot(edge[5], pos), 0.0, 1.0);
539 float a6 = clamp(dot(edge[6], pos), 0.0, 1.0);
540 float a7 = clamp(dot(edge[7], pos), 0.0, 1.0);
541 gl_FragColor = texColor * alpha * min(min(a0, a2) * min(a1, a3), min(a4, a6) * min(a5, a7));
546 FragmentTexClampAlphaAABinding::FragmentTexClampAlphaAABinding()
547 : m_samplerLocation(-1)
548 , m_alphaLocation(-1)
549 , m_fragmentTexTransformLocation(-1)
550 , m_edgeLocation(-1)
554 void FragmentTexClampAlphaAABinding::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
556 static const char* shaderUniforms[] = {
557 "s_texture",
558 "alpha",
559 "fragmentTexTransform",
560 "edge",
562 int locations[4];
564 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
566 m_samplerLocation = locations[0];
567 m_alphaLocation = locations[1];
568 m_fragmentTexTransformLocation = locations[2];
569 m_edgeLocation = locations[3];
570 DCHECK(m_samplerLocation != -1 && m_alphaLocation != -1 && m_fragmentTexTransformLocation != -1 && m_edgeLocation != -1);
573 std::string FragmentShaderRGBATexClampAlphaAA::getShaderString() const
575 return SHADER(
576 precision mediump float;
577 varying vec2 v_texCoord;
578 uniform sampler2D s_texture;
579 uniform float alpha;
580 uniform vec4 fragmentTexTransform;
581 uniform vec3 edge[8];
582 void main()
584 vec2 texCoord = clamp(v_texCoord, 0.0, 1.0) * fragmentTexTransform.zw + fragmentTexTransform.xy;
585 vec4 texColor = texture2D(s_texture, texCoord);
586 vec3 pos = vec3(gl_FragCoord.xy, 1);
587 float a0 = clamp(dot(edge[0], pos), 0.0, 1.0);
588 float a1 = clamp(dot(edge[1], pos), 0.0, 1.0);
589 float a2 = clamp(dot(edge[2], pos), 0.0, 1.0);
590 float a3 = clamp(dot(edge[3], pos), 0.0, 1.0);
591 float a4 = clamp(dot(edge[4], pos), 0.0, 1.0);
592 float a5 = clamp(dot(edge[5], pos), 0.0, 1.0);
593 float a6 = clamp(dot(edge[6], pos), 0.0, 1.0);
594 float a7 = clamp(dot(edge[7], pos), 0.0, 1.0);
595 gl_FragColor = texColor * alpha * min(min(a0, a2) * min(a1, a3), min(a4, a6) * min(a5, a7));
600 std::string FragmentShaderRGBATexClampSwizzleAlphaAA::getShaderString() const
602 return SHADER(
603 precision mediump float;
604 varying vec2 v_texCoord;
605 uniform sampler2D s_texture;
606 uniform float alpha;
607 uniform vec4 fragmentTexTransform;
608 uniform vec3 edge[8];
609 void main()
611 vec2 texCoord = clamp(v_texCoord, 0.0, 1.0) * fragmentTexTransform.zw + fragmentTexTransform.xy;
612 vec4 texColor = texture2D(s_texture, texCoord);
613 vec3 pos = vec3(gl_FragCoord.xy, 1);
614 float a0 = clamp(dot(edge[0], pos), 0.0, 1.0);
615 float a1 = clamp(dot(edge[1], pos), 0.0, 1.0);
616 float a2 = clamp(dot(edge[2], pos), 0.0, 1.0);
617 float a3 = clamp(dot(edge[3], pos), 0.0, 1.0);
618 float a4 = clamp(dot(edge[4], pos), 0.0, 1.0);
619 float a5 = clamp(dot(edge[5], pos), 0.0, 1.0);
620 float a6 = clamp(dot(edge[6], pos), 0.0, 1.0);
621 float a7 = clamp(dot(edge[7], pos), 0.0, 1.0);
622 gl_FragColor = vec4(texColor.z, texColor.y, texColor.x, texColor.w) * alpha * min(min(a0, a2) * min(a1, a3), min(a4, a6) * min(a5, a7));
627 FragmentShaderRGBATexAlphaMask::FragmentShaderRGBATexAlphaMask()
628 : m_samplerLocation(-1)
629 , m_maskSamplerLocation(-1)
630 , m_alphaLocation(-1)
631 , m_maskTexCoordScaleLocation(-1)
635 void FragmentShaderRGBATexAlphaMask::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
637 static const char* shaderUniforms[] = {
638 "s_texture",
639 "s_mask",
640 "alpha",
641 "maskTexCoordScale",
642 "maskTexCoordOffset",
644 int locations[5];
646 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
648 m_samplerLocation = locations[0];
649 m_maskSamplerLocation = locations[1];
650 m_alphaLocation = locations[2];
651 m_maskTexCoordScaleLocation = locations[3];
652 m_maskTexCoordOffsetLocation = locations[4];
653 DCHECK(m_samplerLocation != -1 && m_maskSamplerLocation != -1 && m_alphaLocation != -1);
656 std::string FragmentShaderRGBATexAlphaMask::getShaderString() const
658 return SHADER(
659 precision mediump float;
660 varying vec2 v_texCoord;
661 uniform sampler2D s_texture;
662 uniform sampler2D s_mask;
663 uniform vec2 maskTexCoordScale;
664 uniform vec2 maskTexCoordOffset;
665 uniform float alpha;
666 void main()
668 vec4 texColor = texture2D(s_texture, v_texCoord);
669 vec2 maskTexCoord = vec2(maskTexCoordOffset.x + v_texCoord.x * maskTexCoordScale.x, maskTexCoordOffset.y + v_texCoord.y * maskTexCoordScale.y);
670 vec4 maskColor = texture2D(s_mask, maskTexCoord);
671 gl_FragColor = vec4(texColor.x, texColor.y, texColor.z, texColor.w) * alpha * maskColor.w;
676 FragmentShaderRGBATexAlphaMaskAA::FragmentShaderRGBATexAlphaMaskAA()
677 : m_samplerLocation(-1)
678 , m_maskSamplerLocation(-1)
679 , m_alphaLocation(-1)
680 , m_edgeLocation(-1)
681 , m_maskTexCoordScaleLocation(-1)
685 void FragmentShaderRGBATexAlphaMaskAA::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
687 static const char* shaderUniforms[] = {
688 "s_texture",
689 "s_mask",
690 "alpha",
691 "edge",
692 "maskTexCoordScale",
693 "maskTexCoordOffset",
695 int locations[6];
697 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
699 m_samplerLocation = locations[0];
700 m_maskSamplerLocation = locations[1];
701 m_alphaLocation = locations[2];
702 m_edgeLocation = locations[3];
703 m_maskTexCoordScaleLocation = locations[4];
704 m_maskTexCoordOffsetLocation = locations[5];
705 DCHECK(m_samplerLocation != -1 && m_maskSamplerLocation != -1 && m_alphaLocation != -1 && m_edgeLocation != -1);
708 std::string FragmentShaderRGBATexAlphaMaskAA::getShaderString() const
710 return SHADER(
711 precision mediump float;
712 varying vec2 v_texCoord;
713 uniform sampler2D s_texture;
714 uniform sampler2D s_mask;
715 uniform vec2 maskTexCoordScale;
716 uniform vec2 maskTexCoordOffset;
717 uniform float alpha;
718 uniform vec3 edge[8];
719 void main()
721 vec4 texColor = texture2D(s_texture, v_texCoord);
722 vec2 maskTexCoord = vec2(maskTexCoordOffset.x + v_texCoord.x * maskTexCoordScale.x, maskTexCoordOffset.y + v_texCoord.y * maskTexCoordScale.y);
723 vec4 maskColor = texture2D(s_mask, maskTexCoord);
724 vec3 pos = vec3(gl_FragCoord.xy, 1);
725 float a0 = clamp(dot(edge[0], pos), 0.0, 1.0);
726 float a1 = clamp(dot(edge[1], pos), 0.0, 1.0);
727 float a2 = clamp(dot(edge[2], pos), 0.0, 1.0);
728 float a3 = clamp(dot(edge[3], pos), 0.0, 1.0);
729 float a4 = clamp(dot(edge[4], pos), 0.0, 1.0);
730 float a5 = clamp(dot(edge[5], pos), 0.0, 1.0);
731 float a6 = clamp(dot(edge[6], pos), 0.0, 1.0);
732 float a7 = clamp(dot(edge[7], pos), 0.0, 1.0);
733 gl_FragColor = vec4(texColor.x, texColor.y, texColor.z, texColor.w) * alpha * maskColor.w * min(min(a0, a2) * min(a1, a3), min(a4, a6) * min(a5, a7));
738 FragmentShaderYUVVideo::FragmentShaderYUVVideo()
739 : m_yTextureLocation(-1)
740 , m_uTextureLocation(-1)
741 , m_vTextureLocation(-1)
742 , m_alphaLocation(-1)
743 , m_yuvMatrixLocation(-1)
744 , m_yuvAdjLocation(-1)
748 void FragmentShaderYUVVideo::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
750 static const char* shaderUniforms[] = {
751 "y_texture",
752 "u_texture",
753 "v_texture",
754 "alpha",
755 "yuv_matrix",
756 "yuv_adj",
758 int locations[6];
760 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
762 m_yTextureLocation = locations[0];
763 m_uTextureLocation = locations[1];
764 m_vTextureLocation = locations[2];
765 m_alphaLocation = locations[3];
766 m_yuvMatrixLocation = locations[4];
767 m_yuvAdjLocation = locations[5];
769 DCHECK(m_yTextureLocation != -1 && m_uTextureLocation != -1 && m_vTextureLocation != -1
770 && m_alphaLocation != -1 && m_yuvMatrixLocation != -1 && m_yuvAdjLocation != -1);
773 std::string FragmentShaderYUVVideo::getShaderString() const
775 return SHADER(
776 precision mediump float;
777 precision mediump int;
778 varying vec2 v_texCoord;
779 uniform sampler2D y_texture;
780 uniform sampler2D u_texture;
781 uniform sampler2D v_texture;
782 uniform float alpha;
783 uniform vec3 yuv_adj;
784 uniform mat3 yuv_matrix;
785 void main()
787 float y_raw = texture2D(y_texture, v_texCoord).x;
788 float u_unsigned = texture2D(u_texture, v_texCoord).x;
789 float v_unsigned = texture2D(v_texture, v_texCoord).x;
790 vec3 yuv = vec3(y_raw, u_unsigned, v_unsigned) + yuv_adj;
791 vec3 rgb = yuv_matrix * yuv;
792 gl_FragColor = vec4(rgb, float(1)) * alpha;
797 FragmentShaderColor::FragmentShaderColor()
798 : m_colorLocation(-1)
802 void FragmentShaderColor::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
804 static const char* shaderUniforms[] = {
805 "color",
807 int locations[1];
809 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
811 m_colorLocation = locations[0];
812 DCHECK(m_colorLocation != -1);
815 std::string FragmentShaderColor::getShaderString() const
817 return SHADER(
818 precision mediump float;
819 uniform vec4 color;
820 void main()
822 gl_FragColor = color;
827 FragmentShaderCheckerboard::FragmentShaderCheckerboard()
828 : m_alphaLocation(-1)
829 , m_texTransformLocation(-1)
830 , m_frequencyLocation(-1)
834 void FragmentShaderCheckerboard::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
836 static const char* shaderUniforms[] = {
837 "alpha",
838 "texTransform",
839 "frequency",
840 "color",
842 int locations[4];
844 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
846 m_alphaLocation = locations[0];
847 m_texTransformLocation = locations[1];
848 m_frequencyLocation = locations[2];
849 m_colorLocation = locations[3];
850 DCHECK(m_alphaLocation != -1 && m_texTransformLocation != -1 && m_frequencyLocation != -1 && m_colorLocation != -1);
853 std::string FragmentShaderCheckerboard::getShaderString() const
855 // Shader based on Example 13-17 of "OpenGL ES 2.0 Programming Guide"
856 // by Munshi, Ginsburg, Shreiner.
857 return SHADER(
858 precision mediump float;
859 precision mediump int;
860 varying vec2 v_texCoord;
861 uniform float alpha;
862 uniform float frequency;
863 uniform vec4 texTransform;
864 uniform vec4 color;
865 void main()
867 vec4 color1 = vec4(1.0, 1.0, 1.0, 1.0);
868 vec4 color2 = color;
869 vec2 texCoord = clamp(v_texCoord, 0.0, 1.0) * texTransform.zw + texTransform.xy;
870 vec2 coord = mod(floor(texCoord * frequency * 2.0), 2.0);
871 float picker = abs(coord.x - coord.y);
872 gl_FragColor = mix(color1, color2, picker) * alpha;
877 } // namespace cc