Reenable NullOpenerRedirectForksProcess, as the offending patch has been reverted
[chromium-blink-merge.git] / cc / shader.cc
bloba9a0877db05fb2067f316711b049eee7fda6b54b
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 uniform mat4 matrix;
163 uniform vec4 texTransform;
164 varying vec2 v_texCoord;
165 void main()
167 gl_Position = matrix * a_position;
168 v_texCoord = a_texCoord * texTransform.zw + texTransform.xy;
173 VertexShaderQuad::VertexShaderQuad()
174 : m_matrixLocation(-1)
175 , m_pointLocation(-1)
179 std::string VertexShaderPosTexIdentity::getShaderString() const
181 return SHADER(
182 attribute vec4 a_position;
183 varying vec2 v_texCoord;
184 void main()
186 gl_Position = a_position;
187 v_texCoord = (a_position.xy + vec2(1.0)) * 0.5;
192 void VertexShaderQuad::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
194 static const char* shaderUniforms[] = {
195 "matrix",
196 "point",
198 int locations[2];
200 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
202 m_matrixLocation = locations[0];
203 m_pointLocation = locations[1];
204 DCHECK(m_matrixLocation != -1 && m_pointLocation != -1);
207 std::string VertexShaderQuad::getShaderString() const
209 return SHADER(
210 attribute vec4 a_position;
211 attribute vec2 a_texCoord;
212 uniform mat4 matrix;
213 uniform vec2 point[4];
214 varying vec2 v_texCoord;
215 void main()
217 vec2 complement = abs(a_texCoord - 1.0);
218 vec4 pos = vec4(0.0, 0.0, a_position.z, a_position.w);
219 pos.xy += (complement.x * complement.y) * point[0];
220 pos.xy += (a_texCoord.x * complement.y) * point[1];
221 pos.xy += (a_texCoord.x * a_texCoord.y) * point[2];
222 pos.xy += (complement.x * a_texCoord.y) * point[3];
223 gl_Position = matrix * pos;
224 v_texCoord = pos.xy + vec2(0.5);
229 VertexShaderTile::VertexShaderTile()
230 : m_matrixLocation(-1)
231 , m_pointLocation(-1)
232 , m_vertexTexTransformLocation(-1)
236 void VertexShaderTile::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
238 static const char* shaderUniforms[] = {
239 "matrix",
240 "point",
241 "vertexTexTransform",
243 int locations[3];
245 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
247 m_matrixLocation = locations[0];
248 m_pointLocation = locations[1];
249 m_vertexTexTransformLocation = locations[2];
250 DCHECK(m_matrixLocation != -1 && m_pointLocation != -1 && m_vertexTexTransformLocation != -1);
253 std::string VertexShaderTile::getShaderString() const
255 return SHADER(
256 attribute vec4 a_position;
257 attribute vec2 a_texCoord;
258 uniform mat4 matrix;
259 uniform vec2 point[4];
260 uniform vec4 vertexTexTransform;
261 varying vec2 v_texCoord;
262 void main()
264 vec2 complement = abs(a_texCoord - 1.0);
265 vec4 pos = vec4(0.0, 0.0, a_position.z, a_position.w);
266 pos.xy += (complement.x * complement.y) * point[0];
267 pos.xy += (a_texCoord.x * complement.y) * point[1];
268 pos.xy += (a_texCoord.x * a_texCoord.y) * point[2];
269 pos.xy += (complement.x * a_texCoord.y) * point[3];
270 gl_Position = matrix * pos;
271 v_texCoord = pos.xy * vertexTexTransform.zw + vertexTexTransform.xy;
276 VertexShaderVideoTransform::VertexShaderVideoTransform()
277 : m_matrixLocation(-1)
278 , m_texMatrixLocation(-1)
282 bool VertexShaderVideoTransform::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
284 static const char* shaderUniforms[] = {
285 "matrix",
286 "texMatrix",
288 int locations[2];
290 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
292 m_matrixLocation = locations[0];
293 m_texMatrixLocation = locations[1];
294 return m_matrixLocation != -1 && m_texMatrixLocation != -1;
297 std::string VertexShaderVideoTransform::getShaderString() const
299 return SHADER(
300 attribute vec4 a_position;
301 attribute vec2 a_texCoord;
302 uniform mat4 matrix;
303 uniform mat4 texMatrix;
304 varying vec2 v_texCoord;
305 void main()
307 gl_Position = matrix * a_position;
308 v_texCoord = vec2(texMatrix * vec4(a_texCoord.x, 1.0 - a_texCoord.y, 0.0, 1.0));
313 FragmentTexAlphaBinding::FragmentTexAlphaBinding()
314 : m_samplerLocation(-1)
315 , m_alphaLocation(-1)
319 void FragmentTexAlphaBinding::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
321 static const char* shaderUniforms[] = {
322 "s_texture",
323 "alpha",
325 int locations[2];
327 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
329 m_samplerLocation = locations[0];
330 m_alphaLocation = locations[1];
331 DCHECK(m_samplerLocation != -1 && m_alphaLocation != -1);
334 FragmentTexOpaqueBinding::FragmentTexOpaqueBinding()
335 : m_samplerLocation(-1)
339 void FragmentTexOpaqueBinding::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
341 static const char* shaderUniforms[] = {
342 "s_texture",
344 int locations[1];
346 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
348 m_samplerLocation = locations[0];
349 DCHECK(m_samplerLocation != -1);
352 std::string FragmentShaderRGBATexFlipAlpha::getShaderString() const
354 return SHADER(
355 precision mediump float;
356 varying vec2 v_texCoord;
357 uniform sampler2D s_texture;
358 uniform float alpha;
359 void main()
361 vec4 texColor = texture2D(s_texture, vec2(v_texCoord.x, 1.0 - v_texCoord.y));
362 gl_FragColor = vec4(texColor.x, texColor.y, texColor.z, texColor.w) * alpha;
367 bool FragmentShaderOESImageExternal::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
369 static const char* shaderUniforms[] = {
370 "s_texture",
372 int locations[1];
374 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
376 m_samplerLocation = locations[0];
377 return m_samplerLocation != -1;
380 std::string FragmentShaderOESImageExternal::getShaderString() const
382 // Cannot use the SHADER() macro because of the '#' char
383 return "#extension GL_OES_EGL_image_external : require \n"
384 "precision mediump float;\n"
385 "varying vec2 v_texCoord;\n"
386 "uniform samplerExternalOES s_texture;\n"
387 "void main()\n"
388 "{\n"
389 " vec4 texColor = texture2D(s_texture, v_texCoord);\n"
390 " gl_FragColor = vec4(texColor.x, texColor.y, texColor.z, texColor.w);\n"
391 "}\n";
394 std::string FragmentShaderRGBATexAlpha::getShaderString() const
396 return SHADER(
397 precision mediump float;
398 varying vec2 v_texCoord;
399 uniform sampler2D s_texture;
400 uniform float alpha;
401 void main()
403 vec4 texColor = texture2D(s_texture, v_texCoord);
404 gl_FragColor = texColor * alpha;
409 std::string FragmentShaderRGBATexRectFlipAlpha::getShaderString() const
411 // This must be paired with VertexShaderPosTexTransform to pick up the texTransform uniform.
412 // The necessary #extension preprocessing directive breaks the SHADER and SHADER0 macros.
413 return "#extension GL_ARB_texture_rectangle : require\n"
414 "precision mediump float;\n"
415 "varying vec2 v_texCoord;\n"
416 "uniform vec4 texTransform;\n"
417 "uniform sampler2DRect s_texture;\n"
418 "uniform float alpha;\n"
419 "void main()\n"
420 "{\n"
421 " vec4 texColor = texture2DRect(s_texture, vec2(v_texCoord.x, texTransform.w - v_texCoord.y));\n"
422 " gl_FragColor = vec4(texColor.x, texColor.y, texColor.z, texColor.w) * alpha;\n"
423 "}\n";
426 std::string FragmentShaderRGBATexRectAlpha::getShaderString() const
428 return "#extension GL_ARB_texture_rectangle : require\n"
429 "precision mediump float;\n"
430 "varying vec2 v_texCoord;\n"
431 "uniform sampler2DRect s_texture;\n"
432 "uniform float alpha;\n"
433 "void main()\n"
434 "{\n"
435 " vec4 texColor = texture2DRect(s_texture, v_texCoord);\n"
436 " gl_FragColor = texColor * alpha;\n"
437 "}\n";
440 std::string FragmentShaderRGBATexOpaque::getShaderString() const
442 return SHADER(
443 precision mediump float;
444 varying vec2 v_texCoord;
445 uniform sampler2D s_texture;
446 void main()
448 vec4 texColor = texture2D(s_texture, v_texCoord);
449 gl_FragColor = vec4(texColor.rgb, 1.0);
454 std::string FragmentShaderRGBATex::getShaderString() const
456 return SHADER(
457 precision mediump float;
458 varying vec2 v_texCoord;
459 uniform sampler2D s_texture;
460 void main()
462 gl_FragColor = texture2D(s_texture, v_texCoord);
467 std::string FragmentShaderRGBATexSwizzleAlpha::getShaderString() const
469 return SHADER(
470 precision mediump float;
471 varying vec2 v_texCoord;
472 uniform sampler2D s_texture;
473 uniform float alpha;
474 void main()
476 vec4 texColor = texture2D(s_texture, v_texCoord);
477 gl_FragColor = vec4(texColor.z, texColor.y, texColor.x, texColor.w) * alpha;
482 std::string FragmentShaderRGBATexSwizzleOpaque::getShaderString() const
484 return SHADER(
485 precision mediump float;
486 varying vec2 v_texCoord;
487 uniform sampler2D s_texture;
488 void main()
490 vec4 texColor = texture2D(s_texture, v_texCoord);
491 gl_FragColor = vec4(texColor.z, texColor.y, texColor.x, 1.0);
496 FragmentShaderRGBATexAlphaAA::FragmentShaderRGBATexAlphaAA()
497 : m_samplerLocation(-1)
498 , m_alphaLocation(-1)
499 , m_edgeLocation(-1)
503 void FragmentShaderRGBATexAlphaAA::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
505 static const char* shaderUniforms[] = {
506 "s_texture",
507 "alpha",
508 "edge",
510 int locations[3];
512 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
514 m_samplerLocation = locations[0];
515 m_alphaLocation = locations[1];
516 m_edgeLocation = locations[2];
517 DCHECK(m_samplerLocation != -1 && m_alphaLocation != -1 && m_edgeLocation != -1);
520 std::string FragmentShaderRGBATexAlphaAA::getShaderString() const
522 return SHADER(
523 precision mediump float;
524 varying vec2 v_texCoord;
525 uniform sampler2D s_texture;
526 uniform float alpha;
527 uniform vec3 edge[8];
528 void main()
530 vec4 texColor = texture2D(s_texture, v_texCoord);
531 vec3 pos = vec3(gl_FragCoord.xy, 1);
532 float a0 = clamp(dot(edge[0], pos), 0.0, 1.0);
533 float a1 = clamp(dot(edge[1], pos), 0.0, 1.0);
534 float a2 = clamp(dot(edge[2], pos), 0.0, 1.0);
535 float a3 = clamp(dot(edge[3], pos), 0.0, 1.0);
536 float a4 = clamp(dot(edge[4], pos), 0.0, 1.0);
537 float a5 = clamp(dot(edge[5], pos), 0.0, 1.0);
538 float a6 = clamp(dot(edge[6], pos), 0.0, 1.0);
539 float a7 = clamp(dot(edge[7], pos), 0.0, 1.0);
540 gl_FragColor = texColor * alpha * min(min(a0, a2) * min(a1, a3), min(a4, a6) * min(a5, a7));
545 FragmentTexClampAlphaAABinding::FragmentTexClampAlphaAABinding()
546 : m_samplerLocation(-1)
547 , m_alphaLocation(-1)
548 , m_fragmentTexTransformLocation(-1)
549 , m_edgeLocation(-1)
553 void FragmentTexClampAlphaAABinding::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
555 static const char* shaderUniforms[] = {
556 "s_texture",
557 "alpha",
558 "fragmentTexTransform",
559 "edge",
561 int locations[4];
563 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
565 m_samplerLocation = locations[0];
566 m_alphaLocation = locations[1];
567 m_fragmentTexTransformLocation = locations[2];
568 m_edgeLocation = locations[3];
569 DCHECK(m_samplerLocation != -1 && m_alphaLocation != -1 && m_fragmentTexTransformLocation != -1 && m_edgeLocation != -1);
572 std::string FragmentShaderRGBATexClampAlphaAA::getShaderString() const
574 return SHADER(
575 precision mediump float;
576 varying vec2 v_texCoord;
577 uniform sampler2D s_texture;
578 uniform float alpha;
579 uniform vec4 fragmentTexTransform;
580 uniform vec3 edge[8];
581 void main()
583 vec2 texCoord = clamp(v_texCoord, 0.0, 1.0) * fragmentTexTransform.zw + fragmentTexTransform.xy;
584 vec4 texColor = texture2D(s_texture, texCoord);
585 vec3 pos = vec3(gl_FragCoord.xy, 1);
586 float a0 = clamp(dot(edge[0], pos), 0.0, 1.0);
587 float a1 = clamp(dot(edge[1], pos), 0.0, 1.0);
588 float a2 = clamp(dot(edge[2], pos), 0.0, 1.0);
589 float a3 = clamp(dot(edge[3], pos), 0.0, 1.0);
590 float a4 = clamp(dot(edge[4], pos), 0.0, 1.0);
591 float a5 = clamp(dot(edge[5], pos), 0.0, 1.0);
592 float a6 = clamp(dot(edge[6], pos), 0.0, 1.0);
593 float a7 = clamp(dot(edge[7], pos), 0.0, 1.0);
594 gl_FragColor = texColor * alpha * min(min(a0, a2) * min(a1, a3), min(a4, a6) * min(a5, a7));
599 std::string FragmentShaderRGBATexClampSwizzleAlphaAA::getShaderString() const
601 return SHADER(
602 precision mediump float;
603 varying vec2 v_texCoord;
604 uniform sampler2D s_texture;
605 uniform float alpha;
606 uniform vec4 fragmentTexTransform;
607 uniform vec3 edge[8];
608 void main()
610 vec2 texCoord = clamp(v_texCoord, 0.0, 1.0) * fragmentTexTransform.zw + fragmentTexTransform.xy;
611 vec4 texColor = texture2D(s_texture, texCoord);
612 vec3 pos = vec3(gl_FragCoord.xy, 1);
613 float a0 = clamp(dot(edge[0], pos), 0.0, 1.0);
614 float a1 = clamp(dot(edge[1], pos), 0.0, 1.0);
615 float a2 = clamp(dot(edge[2], pos), 0.0, 1.0);
616 float a3 = clamp(dot(edge[3], pos), 0.0, 1.0);
617 float a4 = clamp(dot(edge[4], pos), 0.0, 1.0);
618 float a5 = clamp(dot(edge[5], pos), 0.0, 1.0);
619 float a6 = clamp(dot(edge[6], pos), 0.0, 1.0);
620 float a7 = clamp(dot(edge[7], pos), 0.0, 1.0);
621 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));
626 FragmentShaderRGBATexAlphaMask::FragmentShaderRGBATexAlphaMask()
627 : m_samplerLocation(-1)
628 , m_maskSamplerLocation(-1)
629 , m_alphaLocation(-1)
630 , m_maskTexCoordScaleLocation(-1)
634 void FragmentShaderRGBATexAlphaMask::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
636 static const char* shaderUniforms[] = {
637 "s_texture",
638 "s_mask",
639 "alpha",
640 "maskTexCoordScale",
641 "maskTexCoordOffset",
643 int locations[5];
645 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
647 m_samplerLocation = locations[0];
648 m_maskSamplerLocation = locations[1];
649 m_alphaLocation = locations[2];
650 m_maskTexCoordScaleLocation = locations[3];
651 m_maskTexCoordOffsetLocation = locations[4];
652 DCHECK(m_samplerLocation != -1 && m_maskSamplerLocation != -1 && m_alphaLocation != -1);
655 std::string FragmentShaderRGBATexAlphaMask::getShaderString() const
657 return SHADER(
658 precision mediump float;
659 varying vec2 v_texCoord;
660 uniform sampler2D s_texture;
661 uniform sampler2D s_mask;
662 uniform vec2 maskTexCoordScale;
663 uniform vec2 maskTexCoordOffset;
664 uniform float alpha;
665 void main()
667 vec4 texColor = texture2D(s_texture, v_texCoord);
668 vec2 maskTexCoord = vec2(maskTexCoordOffset.x + v_texCoord.x * maskTexCoordScale.x, maskTexCoordOffset.y + v_texCoord.y * maskTexCoordScale.y);
669 vec4 maskColor = texture2D(s_mask, maskTexCoord);
670 gl_FragColor = vec4(texColor.x, texColor.y, texColor.z, texColor.w) * alpha * maskColor.w;
675 FragmentShaderRGBATexAlphaMaskAA::FragmentShaderRGBATexAlphaMaskAA()
676 : m_samplerLocation(-1)
677 , m_maskSamplerLocation(-1)
678 , m_alphaLocation(-1)
679 , m_edgeLocation(-1)
680 , m_maskTexCoordScaleLocation(-1)
684 void FragmentShaderRGBATexAlphaMaskAA::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
686 static const char* shaderUniforms[] = {
687 "s_texture",
688 "s_mask",
689 "alpha",
690 "edge",
691 "maskTexCoordScale",
692 "maskTexCoordOffset",
694 int locations[6];
696 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
698 m_samplerLocation = locations[0];
699 m_maskSamplerLocation = locations[1];
700 m_alphaLocation = locations[2];
701 m_edgeLocation = locations[3];
702 m_maskTexCoordScaleLocation = locations[4];
703 m_maskTexCoordOffsetLocation = locations[5];
704 DCHECK(m_samplerLocation != -1 && m_maskSamplerLocation != -1 && m_alphaLocation != -1 && m_edgeLocation != -1);
707 std::string FragmentShaderRGBATexAlphaMaskAA::getShaderString() const
709 return SHADER(
710 precision mediump float;
711 varying vec2 v_texCoord;
712 uniform sampler2D s_texture;
713 uniform sampler2D s_mask;
714 uniform vec2 maskTexCoordScale;
715 uniform vec2 maskTexCoordOffset;
716 uniform float alpha;
717 uniform vec3 edge[8];
718 void main()
720 vec4 texColor = texture2D(s_texture, v_texCoord);
721 vec2 maskTexCoord = vec2(maskTexCoordOffset.x + v_texCoord.x * maskTexCoordScale.x, maskTexCoordOffset.y + v_texCoord.y * maskTexCoordScale.y);
722 vec4 maskColor = texture2D(s_mask, maskTexCoord);
723 vec3 pos = vec3(gl_FragCoord.xy, 1);
724 float a0 = clamp(dot(edge[0], pos), 0.0, 1.0);
725 float a1 = clamp(dot(edge[1], pos), 0.0, 1.0);
726 float a2 = clamp(dot(edge[2], pos), 0.0, 1.0);
727 float a3 = clamp(dot(edge[3], pos), 0.0, 1.0);
728 float a4 = clamp(dot(edge[4], pos), 0.0, 1.0);
729 float a5 = clamp(dot(edge[5], pos), 0.0, 1.0);
730 float a6 = clamp(dot(edge[6], pos), 0.0, 1.0);
731 float a7 = clamp(dot(edge[7], pos), 0.0, 1.0);
732 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));
737 FragmentShaderYUVVideo::FragmentShaderYUVVideo()
738 : m_yTextureLocation(-1)
739 , m_uTextureLocation(-1)
740 , m_vTextureLocation(-1)
741 , m_alphaLocation(-1)
742 , m_yuvMatrixLocation(-1)
743 , m_yuvAdjLocation(-1)
747 void FragmentShaderYUVVideo::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
749 static const char* shaderUniforms[] = {
750 "y_texture",
751 "u_texture",
752 "v_texture",
753 "alpha",
754 "yuv_matrix",
755 "yuv_adj",
757 int locations[6];
759 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
761 m_yTextureLocation = locations[0];
762 m_uTextureLocation = locations[1];
763 m_vTextureLocation = locations[2];
764 m_alphaLocation = locations[3];
765 m_yuvMatrixLocation = locations[4];
766 m_yuvAdjLocation = locations[5];
768 DCHECK(m_yTextureLocation != -1 && m_uTextureLocation != -1 && m_vTextureLocation != -1
769 && m_alphaLocation != -1 && m_yuvMatrixLocation != -1 && m_yuvAdjLocation != -1);
772 std::string FragmentShaderYUVVideo::getShaderString() const
774 return SHADER(
775 precision mediump float;
776 precision mediump int;
777 varying vec2 v_texCoord;
778 uniform sampler2D y_texture;
779 uniform sampler2D u_texture;
780 uniform sampler2D v_texture;
781 uniform float alpha;
782 uniform vec3 yuv_adj;
783 uniform mat3 yuv_matrix;
784 void main()
786 float y_raw = texture2D(y_texture, v_texCoord).x;
787 float u_unsigned = texture2D(u_texture, v_texCoord).x;
788 float v_unsigned = texture2D(v_texture, v_texCoord).x;
789 vec3 yuv = vec3(y_raw, u_unsigned, v_unsigned) + yuv_adj;
790 vec3 rgb = yuv_matrix * yuv;
791 gl_FragColor = vec4(rgb, float(1)) * alpha;
796 FragmentShaderColor::FragmentShaderColor()
797 : m_colorLocation(-1)
801 void FragmentShaderColor::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
803 static const char* shaderUniforms[] = {
804 "color",
806 int locations[1];
808 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
810 m_colorLocation = locations[0];
811 DCHECK(m_colorLocation != -1);
814 std::string FragmentShaderColor::getShaderString() const
816 return SHADER(
817 precision mediump float;
818 uniform vec4 color;
819 void main()
821 gl_FragColor = color;
826 FragmentShaderCheckerboard::FragmentShaderCheckerboard()
827 : m_alphaLocation(-1)
828 , m_texTransformLocation(-1)
829 , m_frequencyLocation(-1)
833 void FragmentShaderCheckerboard::init(WebGraphicsContext3D* context, unsigned program, bool usingBindUniform, int* baseUniformIndex)
835 static const char* shaderUniforms[] = {
836 "alpha",
837 "texTransform",
838 "frequency",
839 "color",
841 int locations[4];
843 getProgramUniformLocations(context, program, shaderUniforms, arraysize(shaderUniforms), arraysize(locations), locations, usingBindUniform, baseUniformIndex);
845 m_alphaLocation = locations[0];
846 m_texTransformLocation = locations[1];
847 m_frequencyLocation = locations[2];
848 m_colorLocation = locations[3];
849 DCHECK(m_alphaLocation != -1 && m_texTransformLocation != -1 && m_frequencyLocation != -1 && m_colorLocation != -1);
852 std::string FragmentShaderCheckerboard::getShaderString() const
854 // Shader based on Example 13-17 of "OpenGL ES 2.0 Programming Guide"
855 // by Munshi, Ginsburg, Shreiner.
856 return SHADER(
857 precision mediump float;
858 precision mediump int;
859 varying vec2 v_texCoord;
860 uniform float alpha;
861 uniform float frequency;
862 uniform vec4 texTransform;
863 uniform vec4 color;
864 void main()
866 vec4 color1 = vec4(1.0, 1.0, 1.0, 1.0);
867 vec4 color2 = color;
868 vec2 texCoord = clamp(v_texCoord, 0.0, 1.0) * texTransform.zw + texTransform.xy;
869 vec2 coord = mod(floor(texCoord * frequency * 2.0), 2.0);
870 float picker = abs(coord.x - coord.y);
871 gl_FragColor = mix(color1, color2, picker) * alpha;
876 } // namespace cc