Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 17 | #include <utils/String8.h> |
| 18 | |
Romain Guy | a60c388 | 2011-08-01 15:28:16 -0700 | [diff] [blame] | 19 | #include "Caches.h" |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 20 | #include "ProgramCache.h" |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 21 | #include "Properties.h" |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | namespace uirenderer { |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 27 | // Defines |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | |
| 30 | #define MODULATE_OP_NO_MODULATE 0 |
| 31 | #define MODULATE_OP_MODULATE 1 |
| 32 | #define MODULATE_OP_MODULATE_A8 2 |
| 33 | |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 34 | #define STR(x) STR1(x) |
| 35 | #define STR1(x) #x |
| 36 | |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 37 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 38 | // Vertex shaders snippets |
| 39 | /////////////////////////////////////////////////////////////////////////////// |
| 40 | |
Chris Craik | 8bd68c6 | 2015-08-19 15:29:05 -0700 | [diff] [blame] | 41 | const char* gVS_Header_Start = |
| 42 | "#version 100\n" |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 43 | "attribute vec4 position;\n"; |
| 44 | const char* gVS_Header_Attributes_TexCoords = |
| 45 | "attribute vec2 texCoords;\n"; |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 46 | const char* gVS_Header_Attributes_Colors = |
| 47 | "attribute vec4 colors;\n"; |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 48 | const char* gVS_Header_Attributes_VertexAlphaParameters = |
Chris Craik | 6ebdc11 | 2012-08-31 18:24:33 -0700 | [diff] [blame] | 49 | "attribute float vtxAlpha;\n"; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 50 | const char* gVS_Header_Uniforms_TextureTransform = |
| 51 | "uniform mat4 mainTextureTransform;\n"; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 52 | const char* gVS_Header_Uniforms = |
Romain Guy | 39284b7 | 2012-09-26 16:39:40 -0700 | [diff] [blame] | 53 | "uniform mat4 projection;\n" \ |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 54 | "uniform mat4 transform;\n"; |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 55 | const char* gVS_Header_Uniforms_HasGradient = |
| 56 | "uniform mat4 screenSpace;\n"; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 57 | const char* gVS_Header_Uniforms_HasBitmap = |
| 58 | "uniform mat4 textureTransform;\n" |
Romain Guy | 80bbfb1 | 2011-03-23 16:56:28 -0700 | [diff] [blame] | 59 | "uniform mediump vec2 textureDimension;\n"; |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 60 | const char* gVS_Header_Uniforms_HasRoundRectClip = |
| 61 | "uniform mat4 roundRectInvTransform;\n"; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 62 | const char* gVS_Header_Varyings_HasTexture = |
| 63 | "varying vec2 outTexCoords;\n"; |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 64 | const char* gVS_Header_Varyings_HasColors = |
| 65 | "varying vec4 outColors;\n"; |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 66 | const char* gVS_Header_Varyings_HasVertexAlpha = |
Chris Craik | 6ebdc11 | 2012-08-31 18:24:33 -0700 | [diff] [blame] | 67 | "varying float alpha;\n"; |
Romain Guy | 6355347 | 2012-07-18 20:04:14 -0700 | [diff] [blame] | 68 | const char* gVS_Header_Varyings_HasBitmap = |
| 69 | "varying highp vec2 outBitmapTexCoords;\n"; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 70 | const char* gVS_Header_Varyings_HasGradient[6] = { |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 71 | // Linear |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 72 | "varying highp vec2 linear;\n", |
| 73 | "varying float linear;\n", |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 74 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 75 | // Circular |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 76 | "varying highp vec2 circular;\n", |
| 77 | "varying highp vec2 circular;\n", |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 78 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 79 | // Sweep |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 80 | "varying highp vec2 sweep;\n", |
| 81 | "varying highp vec2 sweep;\n", |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 82 | }; |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 83 | const char* gVS_Header_Varyings_HasRoundRectClip = |
Chris Craik | 68a73e8 | 2014-08-29 17:06:27 -0700 | [diff] [blame] | 84 | "varying highp vec2 roundRectPos;\n"; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 85 | const char* gVS_Main = |
| 86 | "\nvoid main(void) {\n"; |
| 87 | const char* gVS_Main_OutTexCoords = |
| 88 | " outTexCoords = texCoords;\n"; |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 89 | const char* gVS_Main_OutColors = |
| 90 | " outColors = colors;\n"; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 91 | const char* gVS_Main_OutTransformedTexCoords = |
| 92 | " outTexCoords = (mainTextureTransform * vec4(texCoords, 0.0, 1.0)).xy;\n"; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 93 | const char* gVS_Main_OutGradient[6] = { |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 94 | // Linear |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 95 | " linear = vec2((screenSpace * position).x, 0.5);\n", |
| 96 | " linear = (screenSpace * position).x;\n", |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 97 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 98 | // Circular |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 99 | " circular = (screenSpace * position).xy;\n", |
| 100 | " circular = (screenSpace * position).xy;\n", |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 101 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 102 | // Sweep |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 103 | " sweep = (screenSpace * position).xy;\n", |
Chet Haase | a1d12dd | 2012-09-21 14:50:14 -0700 | [diff] [blame] | 104 | " sweep = (screenSpace * position).xy;\n" |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 105 | }; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 106 | const char* gVS_Main_OutBitmapTexCoords = |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 107 | " outBitmapTexCoords = (textureTransform * position).xy * textureDimension;\n"; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 108 | const char* gVS_Main_Position = |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 109 | " vec4 transformedPosition = projection * transform * position;\n" |
| 110 | " gl_Position = transformedPosition;\n"; |
Chris Craik | bf75945 | 2014-08-11 16:00:44 -0700 | [diff] [blame] | 111 | |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 112 | const char* gVS_Main_VertexAlpha = |
Chris Craik | 6ebdc11 | 2012-08-31 18:24:33 -0700 | [diff] [blame] | 113 | " alpha = vtxAlpha;\n"; |
Chris Craik | bf75945 | 2014-08-11 16:00:44 -0700 | [diff] [blame] | 114 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 115 | const char* gVS_Main_HasRoundRectClip = |
| 116 | " roundRectPos = (roundRectInvTransform * transformedPosition).xy;\n"; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 117 | const char* gVS_Footer = |
| 118 | "}\n\n"; |
| 119 | |
| 120 | /////////////////////////////////////////////////////////////////////////////// |
| 121 | // Fragment shaders snippets |
| 122 | /////////////////////////////////////////////////////////////////////////////// |
| 123 | |
Chris Craik | 8bd68c6 | 2015-08-19 15:29:05 -0700 | [diff] [blame] | 124 | const char* gFS_Header_Start = |
| 125 | "#version 100\n"; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 126 | const char* gFS_Header_Extension_FramebufferFetch = |
| 127 | "#extension GL_NV_shader_framebuffer_fetch : enable\n\n"; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 128 | const char* gFS_Header_Extension_ExternalTexture = |
| 129 | "#extension GL_OES_EGL_image_external : require\n\n"; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 130 | const char* gFS_Header = |
| 131 | "precision mediump float;\n\n"; |
| 132 | const char* gFS_Uniforms_Color = |
| 133 | "uniform vec4 color;\n"; |
| 134 | const char* gFS_Uniforms_TextureSampler = |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 135 | "uniform sampler2D baseSampler;\n"; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 136 | const char* gFS_Uniforms_ExternalTextureSampler = |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 137 | "uniform samplerExternalOES baseSampler;\n"; |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 138 | const char* gFS_Uniforms_GradientSampler[2] = { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 139 | "uniform vec2 screenSize;\n" |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 140 | "uniform sampler2D gradientSampler;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 141 | |
| 142 | "uniform vec2 screenSize;\n" |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 143 | "uniform vec4 startColor;\n" |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 144 | "uniform vec4 endColor;\n" |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 145 | }; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 146 | const char* gFS_Uniforms_BitmapSampler = |
| 147 | "uniform sampler2D bitmapSampler;\n"; |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 148 | const char* gFS_Uniforms_ColorOp[3] = { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 149 | // None |
| 150 | "", |
| 151 | // Matrix |
| 152 | "uniform mat4 colorMatrix;\n" |
| 153 | "uniform vec4 colorMatrixVector;\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 154 | // PorterDuff |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 155 | "uniform vec4 colorBlend;\n" |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 156 | }; |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 157 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 158 | const char* gFS_Uniforms_HasRoundRectClip = |
| 159 | "uniform vec4 roundRectInnerRectLTRB;\n" |
| 160 | "uniform float roundRectRadius;\n"; |
| 161 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 162 | // Dithering must be done in the quantization space |
| 163 | // When we are writing to an sRGB framebuffer, we must do the following: |
| 164 | // EOCF(OECF(color) + dither) |
| 165 | // We approximate the transfer functions with gamma 2.0 to avoid branches and pow() |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 166 | // The dithering pattern is generated with a triangle noise generator in the range [-0.0,1.0] |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 167 | // TODO: Handle linear fp16 render targets |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 168 | const char* gFS_Gradient_Functions = |
| 169 | "\nfloat triangleNoise(const highp vec2 n) {\n" |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 170 | " highp vec2 p = fract(n * vec2(5.3987, 5.4421));\n" |
| 171 | " p += dot(p.yx, p.xy + vec2(21.5351, 14.3137));\n" |
| 172 | " highp float xy = p.x * p.y;\n" |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 173 | " return fract(xy * 95.4307) + fract(xy * 75.04961) - 1.0;\n" |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 174 | "}\n"; |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 175 | const char* gFS_Gradient_Preamble[2] = { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 176 | // Linear framebuffer |
| 177 | "\nvec4 dither(const vec4 color) {\n" |
Romain Guy | a0ed6f0 | 2016-12-12 18:21:32 -0800 | [diff] [blame^] | 178 | " return vec4(color.rgb + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0), color.a);\n" |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 179 | "}\n" |
| 180 | "\nvec4 gammaMix(const vec4 a, const vec4 b, float v) {\n" |
Romain Guy | a0ed6f0 | 2016-12-12 18:21:32 -0800 | [diff] [blame^] | 181 | " vec4 c = pow(mix(a, b, v), vec4(vec3(1.0 / 2.2), 1.0));\n" |
| 182 | " return vec4(c.rgb * c.a, c.a);\n" |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 183 | "}\n", |
| 184 | // sRGB framebuffer |
| 185 | "\nvec4 dither(const vec4 color) {\n" |
| 186 | " vec3 dithered = sqrt(color.rgb) + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0);\n" |
| 187 | " return vec4(dithered * dithered, color.a);\n" |
| 188 | "}\n" |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 189 | "\nvec4 gammaMix(const vec4 a, const vec4 b, float v) {\n" |
Romain Guy | a0ed6f0 | 2016-12-12 18:21:32 -0800 | [diff] [blame^] | 190 | " vec4 c = mix(a, b, v);\n" |
| 191 | " return vec4(c.rgb * c.a, c.a);\n" |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 192 | "}\n" |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | // Uses luminance coefficients from Rec.709 to choose the appropriate gamma |
| 196 | // The gamma() function assumes that bright text will be displayed on a dark |
| 197 | // background and that dark text will be displayed on bright background |
| 198 | // The gamma coefficient is chosen to thicken or thin the text accordingly |
| 199 | // The dot product used to compute the luminance could be approximated with |
| 200 | // a simple max(color.r, color.g, color.b) |
| 201 | const char* gFS_Gamma_Preamble = |
| 202 | "\n#define GAMMA (%.2f)\n" |
| 203 | "#define GAMMA_INV (%.2f)\n" |
| 204 | "\nfloat gamma(float a, const vec3 color) {\n" |
| 205 | " float luminance = dot(color, vec3(0.2126, 0.7152, 0.0722));\n" |
| 206 | " return pow(a, luminance < 0.5 ? GAMMA_INV : GAMMA);\n" |
| 207 | "}\n"; |
| 208 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 209 | const char* gFS_Main = |
| 210 | "\nvoid main(void) {\n" |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 211 | " vec4 fragColor;\n"; |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 212 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 213 | const char* gFS_Main_AddDither = |
| 214 | " fragColor = dither(fragColor);\n"; |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 215 | |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 216 | // Fast cases |
| 217 | const char* gFS_Fast_SingleColor = |
| 218 | "\nvoid main(void) {\n" |
| 219 | " gl_FragColor = color;\n" |
| 220 | "}\n\n"; |
| 221 | const char* gFS_Fast_SingleTexture = |
| 222 | "\nvoid main(void) {\n" |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 223 | " gl_FragColor = texture2D(baseSampler, outTexCoords);\n" |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 224 | "}\n\n"; |
| 225 | const char* gFS_Fast_SingleModulateTexture = |
| 226 | "\nvoid main(void) {\n" |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 227 | " gl_FragColor = color.a * texture2D(baseSampler, outTexCoords);\n" |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 228 | "}\n\n"; |
| 229 | const char* gFS_Fast_SingleA8Texture = |
| 230 | "\nvoid main(void) {\n" |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 231 | " gl_FragColor = texture2D(baseSampler, outTexCoords);\n" |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 232 | "}\n\n"; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 233 | const char* gFS_Fast_SingleA8Texture_ApplyGamma = |
| 234 | "\nvoid main(void) {\n" |
| 235 | " gl_FragColor = vec4(0.0, 0.0, 0.0, pow(texture2D(baseSampler, outTexCoords).a, GAMMA));\n" |
| 236 | "}\n\n"; |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 237 | const char* gFS_Fast_SingleModulateA8Texture = |
| 238 | "\nvoid main(void) {\n" |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 239 | " gl_FragColor = color * texture2D(baseSampler, outTexCoords).a;\n" |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 240 | "}\n\n"; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 241 | const char* gFS_Fast_SingleModulateA8Texture_ApplyGamma = |
| 242 | "\nvoid main(void) {\n" |
| 243 | " gl_FragColor = color * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n" |
| 244 | "}\n\n"; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 245 | const char* gFS_Fast_SingleGradient[2] = { |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 246 | "\nvoid main(void) {\n" |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 247 | " gl_FragColor = dither(texture2D(gradientSampler, linear));\n" |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 248 | "}\n\n", |
| 249 | "\nvoid main(void) {\n" |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 250 | " gl_FragColor = dither(gammaMix(startColor, endColor, clamp(linear, 0.0, 1.0)));\n" |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 251 | "}\n\n", |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 252 | }; |
| 253 | const char* gFS_Fast_SingleModulateGradient[2] = { |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 254 | "\nvoid main(void) {\n" |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 255 | " gl_FragColor = dither(color.a * texture2D(gradientSampler, linear));\n" |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 256 | "}\n\n", |
| 257 | "\nvoid main(void) {\n" |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 258 | " gl_FragColor = dither(color.a * gammaMix(startColor, endColor, clamp(linear, 0.0, 1.0)));\n" |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 259 | "}\n\n" |
| 260 | }; |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 261 | |
| 262 | // General case |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 263 | const char* gFS_Main_FetchColor = |
| 264 | " fragColor = color;\n"; |
Romain Guy | 740bf2b | 2011-04-26 15:33:10 -0700 | [diff] [blame] | 265 | const char* gFS_Main_ModulateColor = |
| 266 | " fragColor *= color.a;\n"; |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 267 | const char* gFS_Main_ApplyVertexAlphaLinearInterp = |
Chris Craik | 6ebdc11 | 2012-08-31 18:24:33 -0700 | [diff] [blame] | 268 | " fragColor *= alpha;\n"; |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 269 | const char* gFS_Main_ApplyVertexAlphaShadowInterp = |
Chris Craik | 138c21f | 2016-04-28 16:59:42 -0700 | [diff] [blame] | 270 | // map alpha through shadow alpha sampler |
| 271 | " fragColor *= texture2D(baseSampler, vec2(alpha, 0.5)).a;\n"; |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 272 | const char* gFS_Main_FetchTexture[2] = { |
| 273 | // Don't modulate |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 274 | " fragColor = texture2D(baseSampler, outTexCoords);\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 275 | // Modulate |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 276 | " fragColor = color * texture2D(baseSampler, outTexCoords);\n" |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 277 | }; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 278 | const char* gFS_Main_FetchA8Texture[4] = { |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 279 | // Don't modulate |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 280 | " fragColor = texture2D(baseSampler, outTexCoords);\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 281 | " fragColor = texture2D(baseSampler, outTexCoords);\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 282 | // Modulate |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 283 | " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 284 | " fragColor = color * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 285 | }; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 286 | const char* gFS_Main_FetchGradient[6] = { |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 287 | // Linear |
Romain Guy | 320d46b | 2012-08-08 16:05:42 -0700 | [diff] [blame] | 288 | " vec4 gradientColor = texture2D(gradientSampler, linear);\n", |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 289 | |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 290 | " vec4 gradientColor = gammaMix(startColor, endColor, clamp(linear, 0.0, 1.0));\n", |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 291 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 292 | // Circular |
Romain Guy | 320d46b | 2012-08-08 16:05:42 -0700 | [diff] [blame] | 293 | " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n", |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 294 | |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 295 | " vec4 gradientColor = gammaMix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n", |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 296 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 297 | // Sweep |
Romain Guy | 6355347 | 2012-07-18 20:04:14 -0700 | [diff] [blame] | 298 | " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n" |
Romain Guy | 320d46b | 2012-08-08 16:05:42 -0700 | [diff] [blame] | 299 | " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n", |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 300 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 301 | " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n" |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 302 | " vec4 gradientColor = gammaMix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n" |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 303 | }; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 304 | const char* gFS_Main_FetchBitmap = |
| 305 | " vec4 bitmapColor = texture2D(bitmapSampler, outBitmapTexCoords);\n"; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 306 | const char* gFS_Main_FetchBitmapNpot = |
| 307 | " vec4 bitmapColor = texture2D(bitmapSampler, wrap(outBitmapTexCoords));\n"; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 308 | const char* gFS_Main_BlendShadersBG = |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 309 | " fragColor = blendShaders(gradientColor, bitmapColor)"; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 310 | const char* gFS_Main_BlendShadersGB = |
| 311 | " fragColor = blendShaders(bitmapColor, gradientColor)"; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 312 | const char* gFS_Main_BlendShaders_Modulate[6] = { |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 313 | // Don't modulate |
| 314 | ";\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 315 | ";\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 316 | // Modulate |
Chet Haase | 0990ffb | 2012-09-17 17:43:45 -0700 | [diff] [blame] | 317 | " * color.a;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 318 | " * color.a;\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 319 | // Modulate with alpha 8 texture |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 320 | " * texture2D(baseSampler, outTexCoords).a;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 321 | " * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 322 | }; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 323 | const char* gFS_Main_GradientShader_Modulate[6] = { |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 324 | // Don't modulate |
| 325 | " fragColor = gradientColor;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 326 | " fragColor = gradientColor;\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 327 | // Modulate |
Chet Haase | 0990ffb | 2012-09-17 17:43:45 -0700 | [diff] [blame] | 328 | " fragColor = gradientColor * color.a;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 329 | " fragColor = gradientColor * color.a;\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 330 | // Modulate with alpha 8 texture |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 331 | " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 332 | " fragColor = gradientColor * gamma(texture2D(baseSampler, outTexCoords).a, gradientColor.rgb);\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 333 | }; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 334 | const char* gFS_Main_BitmapShader_Modulate[6] = { |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 335 | // Don't modulate |
| 336 | " fragColor = bitmapColor;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 337 | " fragColor = bitmapColor;\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 338 | // Modulate |
Chet Haase | 0990ffb | 2012-09-17 17:43:45 -0700 | [diff] [blame] | 339 | " fragColor = bitmapColor * color.a;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 340 | " fragColor = bitmapColor * color.a;\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 341 | // Modulate with alpha 8 texture |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 342 | " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 343 | " fragColor = bitmapColor * gamma(texture2D(baseSampler, outTexCoords).a, bitmapColor.rgb);\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 344 | }; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 345 | const char* gFS_Main_FragColor = |
| 346 | " gl_FragColor = fragColor;\n"; |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 347 | const char* gFS_Main_FragColor_HasColors = |
| 348 | " gl_FragColor *= outColors;\n"; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 349 | const char* gFS_Main_FragColor_Blend = |
| 350 | " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n"; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 351 | const char* gFS_Main_FragColor_Blend_Swap = |
| 352 | " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n"; |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 353 | const char* gFS_Main_ApplyColorOp[3] = { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 354 | // None |
| 355 | "", |
| 356 | // Matrix |
Chris Craik | 73821c8 | 2014-09-16 17:32:13 -0700 | [diff] [blame] | 357 | " fragColor.rgb /= (fragColor.a + 0.0019);\n" // un-premultiply |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 358 | " fragColor *= colorMatrix;\n" |
Chris Craik | 73821c8 | 2014-09-16 17:32:13 -0700 | [diff] [blame] | 359 | " fragColor += colorMatrixVector;\n" |
| 360 | " fragColor.rgb *= (fragColor.a + 0.0019);\n", // re-premultiply |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 361 | // PorterDuff |
| 362 | " fragColor = blendColors(colorBlend, fragColor);\n" |
| 363 | }; |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 364 | |
| 365 | // Note: LTRB -> xyzw |
| 366 | const char* gFS_Main_FragColor_HasRoundRectClip = |
| 367 | " mediump vec2 fragToLT = roundRectInnerRectLTRB.xy - roundRectPos;\n" |
| 368 | " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTRB.zw;\n" |
Chris Craik | f99f320 | 2014-08-05 15:31:52 -0700 | [diff] [blame] | 369 | |
| 370 | // divide + multiply by 128 to avoid falling out of range in length() function |
| 371 | " mediump vec2 dist = max(max(fragToLT, fragFromRB), vec2(0.0, 0.0)) / 128.0;\n" |
| 372 | " mediump float linearDist = roundRectRadius - (length(dist) * 128.0);\n" |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 373 | " gl_FragColor *= clamp(linearDist, 0.0, 1.0);\n"; |
| 374 | |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 375 | const char* gFS_Main_DebugHighlight = |
| 376 | " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n"; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 377 | const char* gFS_Footer = |
| 378 | "}\n\n"; |
| 379 | |
| 380 | /////////////////////////////////////////////////////////////////////////////// |
| 381 | // PorterDuff snippets |
| 382 | /////////////////////////////////////////////////////////////////////////////// |
| 383 | |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 384 | const char* gBlendOps[18] = { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 385 | // Clear |
| 386 | "return vec4(0.0, 0.0, 0.0, 0.0);\n", |
| 387 | // Src |
| 388 | "return src;\n", |
| 389 | // Dst |
| 390 | "return dst;\n", |
| 391 | // SrcOver |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 392 | "return src + dst * (1.0 - src.a);\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 393 | // DstOver |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 394 | "return dst + src * (1.0 - dst.a);\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 395 | // SrcIn |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 396 | "return src * dst.a;\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 397 | // DstIn |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 398 | "return dst * src.a;\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 399 | // SrcOut |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 400 | "return src * (1.0 - dst.a);\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 401 | // DstOut |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 402 | "return dst * (1.0 - src.a);\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 403 | // SrcAtop |
| 404 | "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n", |
| 405 | // DstAtop |
| 406 | "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n", |
| 407 | // Xor |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 408 | "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb, " |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 409 | "src.a + dst.a - 2.0 * src.a * dst.a);\n", |
Derek Sollenberger | c0bf700 | 2015-03-06 13:48:27 -0500 | [diff] [blame] | 410 | // Plus |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 411 | "return min(src + dst, 1.0);\n", |
Derek Sollenberger | c0bf700 | 2015-03-06 13:48:27 -0500 | [diff] [blame] | 412 | // Modulate |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 413 | "return src * dst;\n", |
| 414 | // Screen |
| 415 | "return src + dst - src * dst;\n", |
| 416 | // Overlay |
| 417 | "return clamp(vec4(mix(" |
| 418 | "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), " |
| 419 | "src.a * dst.a - 2.0 * (dst.a - dst.rgb) * (src.a - src.rgb) + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), " |
| 420 | "step(dst.a, 2.0 * dst.rgb)), " |
| 421 | "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n", |
| 422 | // Darken |
| 423 | "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + " |
| 424 | "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n", |
| 425 | // Lighten |
| 426 | "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + " |
| 427 | "max(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 428 | }; |
| 429 | |
| 430 | /////////////////////////////////////////////////////////////////////////////// |
| 431 | // Constructors/destructors |
| 432 | /////////////////////////////////////////////////////////////////////////////// |
| 433 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 434 | ProgramCache::ProgramCache(Extensions& extensions) |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 435 | : mHasES3(extensions.getMajorGlVersion() >= 3) |
| 436 | , mHasSRGB(extensions.hasSRGB()) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | ProgramCache::~ProgramCache() { |
| 440 | clear(); |
| 441 | } |
| 442 | |
| 443 | /////////////////////////////////////////////////////////////////////////////// |
| 444 | // Cache management |
| 445 | /////////////////////////////////////////////////////////////////////////////// |
| 446 | |
| 447 | void ProgramCache::clear() { |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 448 | PROGRAM_LOGD("Clearing program cache"); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 449 | mCache.clear(); |
| 450 | } |
| 451 | |
| 452 | Program* ProgramCache::get(const ProgramDescription& description) { |
| 453 | programid key = description.key(); |
Chris Craik | 096b8d9 | 2013-03-01 11:08:11 -0800 | [diff] [blame] | 454 | if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) { |
| 455 | // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent |
| 456 | // to standard texture program (bitmaps, patches). Consider them equivalent. |
| 457 | key = PROGRAM_KEY_TEXTURE; |
| 458 | } |
| 459 | |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 460 | auto iter = mCache.find(key); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 461 | Program* program = nullptr; |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 462 | if (iter == mCache.end()) { |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 463 | description.log("Could not find program"); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 464 | program = generateProgram(description, key); |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 465 | mCache[key] = std::unique_ptr<Program>(program); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 466 | } else { |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 467 | program = iter->second.get(); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 468 | } |
| 469 | return program; |
| 470 | } |
| 471 | |
| 472 | /////////////////////////////////////////////////////////////////////////////// |
| 473 | // Program generation |
| 474 | /////////////////////////////////////////////////////////////////////////////// |
| 475 | |
Andreas Gampe | 64bb413 | 2014-11-22 00:35:09 +0000 | [diff] [blame] | 476 | Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 477 | String8 vertexShader = generateVertexShader(description); |
| 478 | String8 fragmentShader = generateFragmentShader(description); |
| 479 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 480 | return new Program(description, vertexShader.string(), fragmentShader.string()); |
| 481 | } |
| 482 | |
| 483 | static inline size_t gradientIndex(const ProgramDescription& description) { |
| 484 | return description.gradientType * 2 + description.isSimpleGradient; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | String8 ProgramCache::generateVertexShader(const ProgramDescription& description) { |
| 488 | // Add attributes |
Chris Craik | 8bd68c6 | 2015-08-19 15:29:05 -0700 | [diff] [blame] | 489 | String8 shader(gVS_Header_Start); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 490 | if (description.hasTexture || description.hasExternalTexture) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 491 | shader.append(gVS_Header_Attributes_TexCoords); |
| 492 | } |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 493 | if (description.hasVertexAlpha) { |
| 494 | shader.append(gVS_Header_Attributes_VertexAlphaParameters); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 495 | } |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 496 | if (description.hasColors) { |
| 497 | shader.append(gVS_Header_Attributes_Colors); |
| 498 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 499 | // Uniforms |
| 500 | shader.append(gVS_Header_Uniforms); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 501 | if (description.hasTextureTransform) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 502 | shader.append(gVS_Header_Uniforms_TextureTransform); |
| 503 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 504 | if (description.hasGradient) { |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 505 | shader.append(gVS_Header_Uniforms_HasGradient); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 506 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 507 | if (description.hasBitmap) { |
| 508 | shader.append(gVS_Header_Uniforms_HasBitmap); |
| 509 | } |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 510 | if (description.hasRoundRectClip) { |
| 511 | shader.append(gVS_Header_Uniforms_HasRoundRectClip); |
| 512 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 513 | // Varyings |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 514 | if (description.hasTexture || description.hasExternalTexture) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 515 | shader.append(gVS_Header_Varyings_HasTexture); |
| 516 | } |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 517 | if (description.hasVertexAlpha) { |
| 518 | shader.append(gVS_Header_Varyings_HasVertexAlpha); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 519 | } |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 520 | if (description.hasColors) { |
| 521 | shader.append(gVS_Header_Varyings_HasColors); |
| 522 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 523 | if (description.hasGradient) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 524 | shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 525 | } |
| 526 | if (description.hasBitmap) { |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 527 | shader.append(gVS_Header_Varyings_HasBitmap); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 528 | } |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 529 | if (description.hasRoundRectClip) { |
| 530 | shader.append(gVS_Header_Varyings_HasRoundRectClip); |
| 531 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 532 | |
| 533 | // Begin the shader |
| 534 | shader.append(gVS_Main); { |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 535 | if (description.hasTextureTransform) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 536 | shader.append(gVS_Main_OutTransformedTexCoords); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 537 | } else if (description.hasTexture || description.hasExternalTexture) { |
| 538 | shader.append(gVS_Main_OutTexCoords); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 539 | } |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 540 | if (description.hasVertexAlpha) { |
| 541 | shader.append(gVS_Main_VertexAlpha); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 542 | } |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 543 | if (description.hasColors) { |
| 544 | shader.append(gVS_Main_OutColors); |
| 545 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 546 | if (description.hasBitmap) { |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 547 | shader.append(gVS_Main_OutBitmapTexCoords); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 548 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 549 | // Output transformed position |
| 550 | shader.append(gVS_Main_Position); |
Chet Haase | a1d12dd | 2012-09-21 14:50:14 -0700 | [diff] [blame] | 551 | if (description.hasGradient) { |
| 552 | shader.append(gVS_Main_OutGradient[gradientIndex(description)]); |
| 553 | } |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 554 | if (description.hasRoundRectClip) { |
| 555 | shader.append(gVS_Main_HasRoundRectClip); |
| 556 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 557 | } |
| 558 | // End the shader |
| 559 | shader.append(gVS_Footer); |
| 560 | |
| 561 | PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string()); |
| 562 | |
| 563 | return shader; |
| 564 | } |
| 565 | |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 566 | static bool shaderOp(const ProgramDescription& description, String8& shader, |
| 567 | const int modulateOp, const char** snippets) { |
| 568 | int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 569 | op = op * 2 + description.hasGammaCorrection; |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 570 | shader.append(snippets[op]); |
| 571 | return description.hasAlpha8Texture; |
| 572 | } |
| 573 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 574 | String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) { |
Chris Craik | 8bd68c6 | 2015-08-19 15:29:05 -0700 | [diff] [blame] | 575 | String8 shader(gFS_Header_Start); |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 576 | |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame] | 577 | const bool blendFramebuffer = description.framebufferMode >= SkBlendMode::kPlus; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 578 | if (blendFramebuffer) { |
| 579 | shader.append(gFS_Header_Extension_FramebufferFetch); |
| 580 | } |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 581 | if (description.hasExternalTexture) { |
| 582 | shader.append(gFS_Header_Extension_ExternalTexture); |
| 583 | } |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 584 | |
| 585 | shader.append(gFS_Header); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 586 | |
| 587 | // Varyings |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 588 | if (description.hasTexture || description.hasExternalTexture) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 589 | shader.append(gVS_Header_Varyings_HasTexture); |
| 590 | } |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 591 | if (description.hasVertexAlpha) { |
| 592 | shader.append(gVS_Header_Varyings_HasVertexAlpha); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 593 | } |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 594 | if (description.hasColors) { |
| 595 | shader.append(gVS_Header_Varyings_HasColors); |
| 596 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 597 | if (description.hasGradient) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 598 | shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 599 | } |
| 600 | if (description.hasBitmap) { |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 601 | shader.append(gVS_Header_Varyings_HasBitmap); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 602 | } |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 603 | if (description.hasRoundRectClip) { |
| 604 | shader.append(gVS_Header_Varyings_HasRoundRectClip); |
| 605 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 606 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 607 | // Uniforms |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 608 | int modulateOp = MODULATE_OP_NO_MODULATE; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 609 | const bool singleColor = !description.hasTexture && !description.hasExternalTexture && |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 610 | !description.hasGradient && !description.hasBitmap; |
| 611 | |
| 612 | if (description.modulate || singleColor) { |
| 613 | shader.append(gFS_Uniforms_Color); |
| 614 | if (!singleColor) modulateOp = MODULATE_OP_MODULATE; |
| 615 | } |
Chris Craik | 138c21f | 2016-04-28 16:59:42 -0700 | [diff] [blame] | 616 | if (description.hasTexture || description.useShadowAlphaInterp) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 617 | shader.append(gFS_Uniforms_TextureSampler); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 618 | } else if (description.hasExternalTexture) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 619 | shader.append(gFS_Uniforms_ExternalTextureSampler); |
| 620 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 621 | if (description.hasGradient) { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 622 | shader.append(gFS_Uniforms_GradientSampler[description.isSimpleGradient]); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 623 | } |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 624 | if (description.hasRoundRectClip) { |
| 625 | shader.append(gFS_Uniforms_HasRoundRectClip); |
| 626 | } |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 627 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 628 | if (description.hasGammaCorrection) { |
| 629 | shader.appendFormat(gFS_Gamma_Preamble, Properties::textGamma, 1.0f / Properties::textGamma); |
| 630 | } |
| 631 | |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 632 | // Optimization for common cases |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 633 | if (!description.hasVertexAlpha |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 634 | && !blendFramebuffer |
| 635 | && !description.hasColors |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 636 | && description.colorOp == ProgramDescription::ColorFilterMode::None |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 637 | && !description.hasDebugHighlight |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 638 | && !description.hasRoundRectClip) { |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 639 | bool fast = false; |
| 640 | |
| 641 | const bool noShader = !description.hasGradient && !description.hasBitmap; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 642 | const bool singleTexture = (description.hasTexture || description.hasExternalTexture) && |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 643 | !description.hasAlpha8Texture && noShader; |
| 644 | const bool singleA8Texture = description.hasTexture && |
| 645 | description.hasAlpha8Texture && noShader; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 646 | const bool singleGradient = !description.hasTexture && !description.hasExternalTexture && |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 647 | description.hasGradient && !description.hasBitmap && |
| 648 | description.gradientType == ProgramDescription::kGradientLinear; |
| 649 | |
| 650 | if (singleColor) { |
| 651 | shader.append(gFS_Fast_SingleColor); |
| 652 | fast = true; |
| 653 | } else if (singleTexture) { |
| 654 | if (!description.modulate) { |
| 655 | shader.append(gFS_Fast_SingleTexture); |
| 656 | } else { |
| 657 | shader.append(gFS_Fast_SingleModulateTexture); |
| 658 | } |
| 659 | fast = true; |
| 660 | } else if (singleA8Texture) { |
| 661 | if (!description.modulate) { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 662 | if (description.hasGammaCorrection) { |
| 663 | shader.append(gFS_Fast_SingleA8Texture_ApplyGamma); |
| 664 | } else { |
| 665 | shader.append(gFS_Fast_SingleA8Texture); |
| 666 | } |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 667 | } else { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 668 | if (description.hasGammaCorrection) { |
| 669 | shader.append(gFS_Fast_SingleModulateA8Texture_ApplyGamma); |
| 670 | } else { |
| 671 | shader.append(gFS_Fast_SingleModulateA8Texture); |
| 672 | } |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 673 | } |
| 674 | fast = true; |
| 675 | } else if (singleGradient) { |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 676 | shader.append(gFS_Gradient_Functions); |
| 677 | shader.append(gFS_Gradient_Preamble[mHasSRGB]); |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 678 | if (!description.modulate) { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 679 | shader.append(gFS_Fast_SingleGradient[description.isSimpleGradient]); |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 680 | } else { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 681 | shader.append(gFS_Fast_SingleModulateGradient[description.isSimpleGradient]); |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 682 | } |
| 683 | fast = true; |
| 684 | } |
| 685 | |
| 686 | if (fast) { |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 687 | #if DEBUG_PROGRAMS |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 688 | PROGRAM_LOGD("*** Fast case:\n"); |
| 689 | PROGRAM_LOGD("*** Generated fragment shader:\n\n"); |
| 690 | printLongString(shader); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 691 | #endif |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 692 | |
| 693 | return shader; |
| 694 | } |
| 695 | } |
| 696 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 697 | if (description.hasBitmap) { |
| 698 | shader.append(gFS_Uniforms_BitmapSampler); |
| 699 | } |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 700 | shader.append(gFS_Uniforms_ColorOp[static_cast<int>(description.colorOp)]); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 701 | |
| 702 | // Generate required functions |
| 703 | if (description.hasGradient && description.hasBitmap) { |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 704 | generateBlend(shader, "blendShaders", description.shadersMode); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 705 | } |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 706 | if (description.colorOp == ProgramDescription::ColorFilterMode::Blend) { |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 707 | generateBlend(shader, "blendColors", description.colorMode); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 708 | } |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 709 | if (blendFramebuffer) { |
| 710 | generateBlend(shader, "blendFramebuffer", description.framebufferMode); |
| 711 | } |
sergeyv | 554ffeb | 2016-11-15 18:01:21 -0800 | [diff] [blame] | 712 | if (description.useShaderBasedWrap) { |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 713 | generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT); |
| 714 | } |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 715 | if (description.hasGradient) { |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 716 | shader.append(gFS_Gradient_Functions); |
| 717 | shader.append(gFS_Gradient_Preamble[mHasSRGB]); |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 718 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 719 | |
| 720 | // Begin the shader |
| 721 | shader.append(gFS_Main); { |
| 722 | // Stores the result in fragColor directly |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 723 | if (description.hasTexture || description.hasExternalTexture) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 724 | if (description.hasAlpha8Texture) { |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 725 | if (!description.hasGradient && !description.hasBitmap) { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 726 | shader.append( |
| 727 | gFS_Main_FetchA8Texture[modulateOp * 2 + description.hasGammaCorrection]); |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 728 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 729 | } else { |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 730 | shader.append(gFS_Main_FetchTexture[modulateOp]); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 731 | } |
| 732 | } else { |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 733 | if (!description.hasGradient && !description.hasBitmap) { |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 734 | shader.append(gFS_Main_FetchColor); |
| 735 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 736 | } |
| 737 | if (description.hasGradient) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 738 | shader.append(gFS_Main_FetchGradient[gradientIndex(description)]); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 739 | } |
| 740 | if (description.hasBitmap) { |
sergeyv | 554ffeb | 2016-11-15 18:01:21 -0800 | [diff] [blame] | 741 | if (!description.useShaderBasedWrap) { |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 742 | shader.append(gFS_Main_FetchBitmap); |
| 743 | } else { |
| 744 | shader.append(gFS_Main_FetchBitmapNpot); |
| 745 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 746 | } |
Romain Guy | 740bf2b | 2011-04-26 15:33:10 -0700 | [diff] [blame] | 747 | bool applyModulate = false; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 748 | // Case when we have two shaders set |
| 749 | if (description.hasGradient && description.hasBitmap) { |
| 750 | if (description.isBitmapFirst) { |
| 751 | shader.append(gFS_Main_BlendShadersBG); |
| 752 | } else { |
| 753 | shader.append(gFS_Main_BlendShadersGB); |
| 754 | } |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 755 | applyModulate = shaderOp(description, shader, modulateOp, |
| 756 | gFS_Main_BlendShaders_Modulate); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 757 | } else { |
| 758 | if (description.hasGradient) { |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 759 | applyModulate = shaderOp(description, shader, modulateOp, |
| 760 | gFS_Main_GradientShader_Modulate); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 761 | } else if (description.hasBitmap) { |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 762 | applyModulate = shaderOp(description, shader, modulateOp, |
| 763 | gFS_Main_BitmapShader_Modulate); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 764 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 765 | } |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 766 | |
Romain Guy | 740bf2b | 2011-04-26 15:33:10 -0700 | [diff] [blame] | 767 | if (description.modulate && applyModulate) { |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 768 | shader.append(gFS_Main_ModulateColor); |
Romain Guy | 740bf2b | 2011-04-26 15:33:10 -0700 | [diff] [blame] | 769 | } |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 770 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 771 | // Apply the color op if needed |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 772 | shader.append(gFS_Main_ApplyColorOp[static_cast<int>(description.colorOp)]); |
Chris Craik | 9f44a13 | 2012-09-13 18:34:55 -0700 | [diff] [blame] | 773 | |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 774 | if (description.hasVertexAlpha) { |
| 775 | if (description.useShadowAlphaInterp) { |
| 776 | shader.append(gFS_Main_ApplyVertexAlphaShadowInterp); |
Chris Craik | bf75945 | 2014-08-11 16:00:44 -0700 | [diff] [blame] | 777 | } else { |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 778 | shader.append(gFS_Main_ApplyVertexAlphaLinearInterp); |
Chris Craik | bf75945 | 2014-08-11 16:00:44 -0700 | [diff] [blame] | 779 | } |
Chris Craik | 9f44a13 | 2012-09-13 18:34:55 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 782 | if (description.hasGradient) { |
| 783 | shader.append(gFS_Main_AddDither); |
| 784 | } |
| 785 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 786 | // Output the fragment |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 787 | if (!blendFramebuffer) { |
| 788 | shader.append(gFS_Main_FragColor); |
| 789 | } else { |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 790 | shader.append(!description.swapSrcDst ? |
| 791 | gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap); |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 792 | } |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 793 | if (description.hasColors) { |
| 794 | shader.append(gFS_Main_FragColor_HasColors); |
| 795 | } |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 796 | if (description.hasRoundRectClip) { |
| 797 | shader.append(gFS_Main_FragColor_HasRoundRectClip); |
| 798 | } |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 799 | if (description.hasDebugHighlight) { |
| 800 | shader.append(gFS_Main_DebugHighlight); |
| 801 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 802 | } |
| 803 | // End the shader |
| 804 | shader.append(gFS_Footer); |
| 805 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 806 | #if DEBUG_PROGRAMS |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 807 | PROGRAM_LOGD("*** Generated fragment shader:\n\n"); |
| 808 | printLongString(shader); |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 809 | #endif |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 810 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 811 | return shader; |
| 812 | } |
| 813 | |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame] | 814 | void ProgramCache::generateBlend(String8& shader, const char* name, SkBlendMode mode) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 815 | shader.append("\nvec4 "); |
| 816 | shader.append(name); |
| 817 | shader.append("(vec4 src, vec4 dst) {\n"); |
| 818 | shader.append(" "); |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame] | 819 | shader.append(gBlendOps[(int)mode]); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 820 | shader.append("}\n"); |
| 821 | } |
| 822 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 823 | void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) { |
Romain Guy | 6355347 | 2012-07-18 20:04:14 -0700 | [diff] [blame] | 824 | shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n"); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 825 | if (wrapS == GL_MIRRORED_REPEAT) { |
Romain Guy | 6355347 | 2012-07-18 20:04:14 -0700 | [diff] [blame] | 826 | shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n"); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 827 | shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n"); |
| 828 | } |
| 829 | if (wrapT == GL_MIRRORED_REPEAT) { |
Romain Guy | 6355347 | 2012-07-18 20:04:14 -0700 | [diff] [blame] | 830 | shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n"); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 831 | shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n"); |
| 832 | } |
| 833 | shader.append(" return vec2("); |
| 834 | switch (wrapS) { |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 835 | case GL_CLAMP_TO_EDGE: |
| 836 | shader.append("texCoords.x"); |
| 837 | break; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 838 | case GL_REPEAT: |
| 839 | shader.append("mod(texCoords.x, 1.0)"); |
| 840 | break; |
| 841 | case GL_MIRRORED_REPEAT: |
| 842 | shader.append("xMod2"); |
| 843 | break; |
| 844 | } |
| 845 | shader.append(", "); |
| 846 | switch (wrapT) { |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 847 | case GL_CLAMP_TO_EDGE: |
| 848 | shader.append("texCoords.y"); |
| 849 | break; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 850 | case GL_REPEAT: |
| 851 | shader.append("mod(texCoords.y, 1.0)"); |
| 852 | break; |
| 853 | case GL_MIRRORED_REPEAT: |
| 854 | shader.append("yMod2"); |
| 855 | break; |
| 856 | } |
| 857 | shader.append(");\n"); |
| 858 | shader.append("}\n"); |
| 859 | } |
| 860 | |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 861 | void ProgramCache::printLongString(const String8& shader) const { |
| 862 | ssize_t index = 0; |
| 863 | ssize_t lastIndex = 0; |
| 864 | const char* str = shader.string(); |
| 865 | while ((index = shader.find("\n", index)) > -1) { |
| 866 | String8 line(str, index - lastIndex); |
| 867 | if (line.length() == 0) line.append("\n"); |
Chris Craik | 1c1c3fe | 2015-03-06 09:40:35 -0800 | [diff] [blame] | 868 | ALOGD("%s", line.string()); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 869 | index++; |
| 870 | str += (index - lastIndex); |
| 871 | lastIndex = index; |
| 872 | } |
| 873 | } |
| 874 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 875 | }; // namespace uirenderer |
| 876 | }; // namespace android |