// -*- Mode: glsl; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40; -*- // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. // This file is compiled by genshaders.py, use genshaders.sh, no arguments necessary. // The compiled shaders will be in LayerManagerOGLShaders.h. // This file must be compiled after editing, it is not compiled as part of the // build process. // // Syntax: // // // comments (only at the start of a line) // // (@ is used because # is valid inside GLSL) // // multi-line: // @define FOO // ... // @end // // single: // @define FOO 123 // // $FOO$ to paste // // To generate a constant string named ShaderName (shader name should not // use '<', '>', ',', ':', except for parameters, see below): // @shader ShaderName // ... // @end // // @shader may have a single parameter with multiple values using // an empty value is allowed. The shader is expanded for // each value, in the body of the shader will be expanded to // the current value, as will the declaration of the parameter. The name of // defines may include <...> and this should work as expected. // // For example: // // @Shader // string name = ""; // @end // // will be expanded to // // @ShaderName1 // string name = "Name1"; // @end // @ShaderName2 // string name = "Name2"; // @end // // This will be straightaway compiled to two constant strings named // ShaderName1 and ShaderName2. // @define VERTEX_SHADER_HEADER<> /* Vertex Shader */ uniform mat4 uMatrixProj; uniform mat4 uLayerQuadTransform; uniform mat4 uLayerTransform; uniform vec4 uRenderTargetOffset; attribute vec4 aVertexCoord; attribute vec2 aTexCoord; #ifdef GL_ES // for tiling, texcoord can be greater than the lowfp range varying mediump vec2 vTexCoord; #else varying vec2 vTexCoord; #endif @end @define VERTEX_SHADER_HEADER $VERTEX_SHADER_HEADER<>$ uniform mat4 uMaskQuadTransform; varying vec2 vMaskCoord; @end @define VERTEX_SHADER_HEADER $VERTEX_SHADER_HEADER<>$ uniform mat4 uMaskQuadTransform; varying vec3 vMaskCoord; @end @define VERTEX_MASK_STUFF<> @end @define VERTEX_MASK_STUFF vMaskCoord = (uMaskQuadTransform * finalPosition).xy; @end @define VERTEX_MASK_STUFF vMaskCoord.xy = (uMaskQuadTransform * vec4(finalPosition.xyz, 1.0)).xy; // correct for perspective correct interpolation, see comment in D3D10 shader vMaskCoord.z = 1.0; vMaskCoord *= finalPosition.w; @end // This is a basic Layer vertex shader. It's used for all // Layer programs. @shader sLayerVS $VERTEX_SHADER_HEADER$ void main() { vec4 finalPosition = aVertexCoord; finalPosition = uLayerQuadTransform * finalPosition; finalPosition = uLayerTransform * finalPosition; finalPosition.xyz /= finalPosition.w; $VERTEX_MASK_STUFF$ finalPosition = finalPosition - uRenderTargetOffset; finalPosition.xyz *= finalPosition.w; finalPosition = uMatrixProj * finalPosition; vTexCoord = aTexCoord; gl_Position = finalPosition; } @end /* * Fragment shaders */ @define FRAGMENT_SHADER_HEADER /* Fragment Shader */ #ifdef GL_ES precision lowp float; #endif @end // fragment shader header for layers @define LAYER_FRAGMENT<> $FRAGMENT_SHADER_HEADER$ #ifndef NO_LAYER_OPACITY uniform float uLayerOpacity; #endif #ifdef GL_ES // for tiling, texcoord can be greater than the lowfp range varying mediump vec2 vTexCoord; #else varying vec2 vTexCoord; #endif @end // fragment shader header for layers with masks @define LAYER_FRAGMENT $LAYER_FRAGMENT<>$ varying vec2 vMaskCoord; uniform sampler2D uMaskTexture; @end // fragment shader header for layers with masks and 3D transforms @define LAYER_FRAGMENT $LAYER_FRAGMENT<>$ varying vec3 vMaskCoord; uniform sampler2D uMaskTexture; @end @define FRAGMENT_CALC_MASK<> float mask = 1.0; @end @define FRAGMENT_CALC_MASK float mask = texture2D(uMaskTexture, vMaskCoord).r; @end @define FRAGMENT_CALC_MASK vec2 maskCoords = vMaskCoord.xy / vMaskCoord.z; float mask = texture2D(uMaskTexture, maskCoords).r; @end // Solid color rendering. // texcoords are ignored (no texture to sample). // The layer opacity is baked in to the color. @shader sSolidColorLayerFS #define NO_LAYER_OPACITY 1 $LAYER_FRAGMENT$ uniform vec4 uRenderColor; void main() { $FRAGMENT_CALC_MASK$ gl_FragColor = mask * uRenderColor; } @end // Single texture in RGBA format @shader sRGBATextureLayerFS $LAYER_FRAGMENT$ uniform sampler2D uTexture; void main() { $FRAGMENT_CALC_MASK$ gl_FragColor = texture2D(uTexture, vTexCoord) * uLayerOpacity * mask; } @end // Single texture in RGBA format, but with a Rect texture. // Container layer needs this to render a FBO group. @shader sRGBARectTextureLayerFS #extension GL_ARB_texture_rectangle : enable $LAYER_FRAGMENT$ /* This should not be used on GL ES */ #ifndef GL_ES uniform sampler2DRect uTexture; uniform vec2 uTexCoordMultiplier; void main() { $FRAGMENT_CALC_MASK$ gl_FragColor = texture2DRect(uTexture, vec2(vTexCoord * uTexCoordMultiplier)) * uLayerOpacity * mask; } #else void main() { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); } #endif @end // Single texture in BGRA format (via swizzle) @shader sBGRATextureLayerFS $LAYER_FRAGMENT$ uniform sampler2D uTexture; void main() { $FRAGMENT_CALC_MASK$ gl_FragColor = texture2D(uTexture, vTexCoord).bgra * uLayerOpacity * mask; } @end // Single texture in RGBX format @shader sRGBXTextureLayerFS $LAYER_FRAGMENT$ uniform sampler2D uTexture; void main() { $FRAGMENT_CALC_MASK$ gl_FragColor = vec4(texture2D(uTexture, vTexCoord).rgb, 1.0) * uLayerOpacity * mask; } @end // Single texture in BGRX format (via swizzle) @shader sBGRXTextureLayerFS $LAYER_FRAGMENT$ uniform sampler2D uTexture; void main() { $FRAGMENT_CALC_MASK$ gl_FragColor = vec4(texture2D(uTexture, vTexCoord).bgr, 1.0) * uLayerOpacity * mask; } @end // Three textures, representing YCbCr planes of a video image @shader sYCbCrTextureLayerFS $LAYER_FRAGMENT$ #ifdef GL_ES precision mediump float; #endif uniform sampler2D uYTexture; uniform sampler2D uCbTexture; uniform sampler2D uCrTexture; void main() { vec4 yuv; vec4 color; yuv.r = texture2D(uCrTexture, vTexCoord).r - 0.5; yuv.g = texture2D(uYTexture, vTexCoord).r - 0.0625; yuv.b = texture2D(uCbTexture, vTexCoord).r - 0.5; color.r = yuv.g * 1.164 + yuv.r * 1.596; color.g = yuv.g * 1.164 - 0.813 * yuv.r - 0.391 * yuv.b; color.b = yuv.g * 1.164 + yuv.b * 2.018; color.a = 1.0; $FRAGMENT_CALC_MASK$ gl_FragColor = color * uLayerOpacity * mask; } @end // Two textures and two passes for component alpha rendering @shader sComponentPass1FS $LAYER_FRAGMENT$ uniform sampler2D uBlackTexture; uniform sampler2D uWhiteTexture; void main() { vec3 onBlack = texture2D(uBlackTexture, vTexCoord).bgr; vec3 onWhite = texture2D(uWhiteTexture, vTexCoord).bgr; vec4 alphas = (1.0 - onWhite + onBlack).rgbg; $FRAGMENT_CALC_MASK$ gl_FragColor = alphas * uLayerOpacity * mask; } @end @shader sComponentPass2FS $LAYER_FRAGMENT$ uniform sampler2D uBlackTexture; uniform sampler2D uWhiteTexture; void main() { vec3 onBlack = texture2D(uBlackTexture, vTexCoord).bgr; vec3 onWhite = texture2D(uWhiteTexture, vTexCoord).bgr; vec4 alphas = (1.0 - onWhite + onBlack).rgbg; $FRAGMENT_CALC_MASK$ gl_FragColor = vec4(onBlack, alphas.a) * uLayerOpacity * mask; } @end // // The "Copy" program is used for blitting a texture to a destination // with no transforms or any other manipulation. They're used for // blitting the contents of a FBO-rendered texture to a destination. // // There are two variants of the fragment shader: one that uses 2D // textures and one that uses 2DRect textures (for when // EXT_TEXTURE_RECTANGLE is used for FBOs). // // On GL ES, EXT_TEXTURE_RECTANGLE isn't available, so we still // compile the shader but have it render pure red. It should never // be used. // @shader sCopyVS /* Vertex Shader */ attribute vec4 aVertexCoord; attribute vec2 aTexCoord; varying vec2 vTexCoord; void main() { gl_Position = aVertexCoord; vTexCoord = aTexCoord; } @end @shader sCopy2DFS $FRAGMENT_SHADER_HEADER$ varying vec2 vTexCoord; uniform sampler2D uTexture; void main() { gl_FragColor = texture2D(uTexture, vTexCoord); } @end @shader sCopy2DRectFS #extension GL_ARB_texture_rectangle : enable $FRAGMENT_SHADER_HEADER$ varying vec2 vTexCoord; uniform vec2 uTexCoordMultiplier; #ifndef GL_ES uniform sampler2DRect uTexture; void main() { gl_FragColor = texture2DRect(uTexture, vTexCoord * uTexCoordMultiplier); } #else void main() { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); } #endif @end