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.
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
;
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
]);
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
[] = {
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
56 attribute vec4 a_position
;
57 attribute vec2 a_texCoord
;
59 varying vec2 v_texCoord
;
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
[] = {
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
92 precision mediump
float;
93 attribute vec4 a_position
;
94 attribute vec2 a_texCoord
;
96 varying vec2 v_texCoord
;
97 uniform vec2 texScale
;
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
[] = {
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
127 attribute vec4 a_position
;
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
[] = {
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
160 attribute vec4 a_position
;
161 attribute vec2 a_texCoord
;
163 uniform vec4 texTransform
;
164 varying vec2 v_texCoord
;
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
182 attribute vec4 a_position
;
183 varying vec2 v_texCoord
;
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
[] = {
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
210 attribute vec4 a_position
;
211 attribute vec2 a_texCoord
;
213 uniform vec2 point
[4];
214 varying vec2 v_texCoord
;
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
[] = {
241 "vertexTexTransform",
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
256 attribute vec4 a_position
;
257 attribute vec2 a_texCoord
;
259 uniform vec2 point
[4];
260 uniform vec4 vertexTexTransform
;
261 varying vec2 v_texCoord
;
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
[] = {
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
300 attribute vec4 a_position
;
301 attribute vec2 a_texCoord
;
303 uniform mat4 texMatrix
;
304 varying vec2 v_texCoord
;
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
[] = {
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
[] = {
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
355 precision mediump
float;
356 varying vec2 v_texCoord
;
357 uniform sampler2D s_texture
;
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
[] = {
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"
389 " vec4 texColor = texture2D(s_texture, v_texCoord);\n"
390 " gl_FragColor = vec4(texColor.x, texColor.y, texColor.z, texColor.w);\n"
394 std::string
FragmentShaderRGBATexAlpha::getShaderString() const
397 precision mediump
float;
398 varying vec2 v_texCoord
;
399 uniform sampler2D s_texture
;
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"
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"
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"
435 " vec4 texColor = texture2DRect(s_texture, v_texCoord);\n"
436 " gl_FragColor = texColor * alpha;\n"
440 std::string
FragmentShaderRGBATexOpaque::getShaderString() const
443 precision mediump
float;
444 varying vec2 v_texCoord
;
445 uniform sampler2D s_texture
;
448 vec4 texColor
= texture2D(s_texture
, v_texCoord
);
449 gl_FragColor
= vec4(texColor
.rgb
, 1.0);
454 std::string
FragmentShaderRGBATex::getShaderString() const
457 precision mediump
float;
458 varying vec2 v_texCoord
;
459 uniform sampler2D s_texture
;
462 gl_FragColor
= texture2D(s_texture
, v_texCoord
);
467 std::string
FragmentShaderRGBATexSwizzleAlpha::getShaderString() const
470 precision mediump
float;
471 varying vec2 v_texCoord
;
472 uniform sampler2D s_texture
;
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
485 precision mediump
float;
486 varying vec2 v_texCoord
;
487 uniform sampler2D s_texture
;
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)
503 void FragmentShaderRGBATexAlphaAA::init(WebGraphicsContext3D
* context
, unsigned program
, bool usingBindUniform
, int* baseUniformIndex
)
505 static const char* shaderUniforms
[] = {
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
523 precision mediump
float;
524 varying vec2 v_texCoord
;
525 uniform sampler2D s_texture
;
527 uniform vec3 edge
[8];
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)
553 void FragmentTexClampAlphaAABinding::init(WebGraphicsContext3D
* context
, unsigned program
, bool usingBindUniform
, int* baseUniformIndex
)
555 static const char* shaderUniforms
[] = {
558 "fragmentTexTransform",
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
575 precision mediump
float;
576 varying vec2 v_texCoord
;
577 uniform sampler2D s_texture
;
579 uniform vec4 fragmentTexTransform
;
580 uniform vec3 edge
[8];
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
602 precision mediump
float;
603 varying vec2 v_texCoord
;
604 uniform sampler2D s_texture
;
606 uniform vec4 fragmentTexTransform
;
607 uniform vec3 edge
[8];
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
[] = {
641 "maskTexCoordOffset",
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
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
;
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)
680 , m_maskTexCoordScaleLocation(-1)
684 void FragmentShaderRGBATexAlphaMaskAA::init(WebGraphicsContext3D
* context
, unsigned program
, bool usingBindUniform
, int* baseUniformIndex
)
686 static const char* shaderUniforms
[] = {
692 "maskTexCoordOffset",
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
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
;
717 uniform vec3 edge
[8];
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
[] = {
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
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
;
782 uniform vec3 yuv_adj
;
783 uniform mat3 yuv_matrix
;
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
[] = {
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
817 precision mediump
float;
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
[] = {
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.
857 precision mediump
float;
858 precision mediump
int;
859 varying vec2 v_texCoord
;
861 uniform
float frequency
;
862 uniform vec4 texTransform
;
866 vec4 color1
= vec4(1.0, 1.0, 1.0, 1.0);
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
;