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"; |
sergeyv | 9c97e48 | 2016-12-12 16:14:11 -0800 | [diff] [blame] | 148 | const char* gFS_Uniforms_BitmapExternalSampler = |
| 149 | "uniform samplerExternalOES bitmapSampler;\n"; |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 150 | const char* gFS_Uniforms_ColorOp[3] = { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 151 | // None |
| 152 | "", |
| 153 | // Matrix |
| 154 | "uniform mat4 colorMatrix;\n" |
| 155 | "uniform vec4 colorMatrixVector;\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 156 | // PorterDuff |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 157 | "uniform vec4 colorBlend;\n" |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 158 | }; |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 159 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 160 | const char* gFS_Uniforms_HasRoundRectClip = |
| 161 | "uniform vec4 roundRectInnerRectLTRB;\n" |
| 162 | "uniform float roundRectRadius;\n"; |
| 163 | |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame^] | 164 | const char* gFS_Uniforms_ColorSpaceConversion = |
| 165 | // TODO: Should we use a 3D LUT to combine the matrix and transfer functions? |
| 166 | // 32x32x32 fp16 LUTs (for scRGB output) are large and heavy to generate... |
| 167 | "uniform mat3 colorSpaceMatrix;\n"; |
| 168 | |
| 169 | const char* gFS_Uniforms_TransferFunction[4] = { |
| 170 | // In this order: g, a, b, c, d, e, f |
| 171 | // See ColorSpace::TransferParameters |
| 172 | // We'll use hardware sRGB conversion as much as possible |
| 173 | "", |
| 174 | "uniform float transferFunction[7];\n", |
| 175 | "uniform float transferFunction[5];\n", |
| 176 | "uniform float transferFunctionGamma;\n" |
Romain Guy | 636afc1 | 2017-02-07 11:21:05 -0800 | [diff] [blame] | 177 | }; |
| 178 | |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame^] | 179 | const char* gFS_OETF[2] = { |
| 180 | R"__SHADER__( |
| 181 | vec4 OETF(const vec4 linear) { |
| 182 | return linear; |
| 183 | } |
| 184 | )__SHADER__", |
| 185 | // We expect linear data to be scRGB so we mirror the gamma function |
| 186 | R"__SHADER__( |
| 187 | vec4 OETF(const vec4 linear) { |
| 188 | return vec4(sign(linear.rgb) * OETF_sRGB(abs(linear.rgb)), linear.a); |
| 189 | } |
| 190 | )__SHADER__" |
| 191 | }; |
| 192 | |
| 193 | const char* gFS_ColorConvert[3] = { |
| 194 | // Just OETF |
| 195 | R"__SHADER__( |
| 196 | vec4 colorConvert(const vec4 color) { |
| 197 | return OETF(color); |
| 198 | } |
| 199 | )__SHADER__", |
| 200 | // Full color conversion for opaque bitmaps |
| 201 | R"__SHADER__( |
| 202 | vec4 colorConvert(const vec4 color) { |
| 203 | return OETF(vec4(colorSpaceMatrix * EOTF_Parametric(color.rgb), color.a)); |
| 204 | } |
| 205 | )__SHADER__", |
| 206 | // Full color conversion for translucent bitmaps |
| 207 | // Note: 0.5/256=0.0019 |
| 208 | R"__SHADER__( |
| 209 | vec4 colorConvert(in vec4 color) { |
| 210 | color.rgb /= color.a + 0.0019; |
| 211 | color = OETF(vec4(colorSpaceMatrix * EOTF_Parametric(color.rgb), color.a)); |
| 212 | color.rgb *= color.a + 0.0019; |
| 213 | return color; |
| 214 | } |
| 215 | )__SHADER__", |
| 216 | }; |
| 217 | |
| 218 | const char* gFS_sRGB_TransferFunctions = R"__SHADER__( |
Romain Guy | 636afc1 | 2017-02-07 11:21:05 -0800 | [diff] [blame] | 219 | float OETF_sRGB(const float linear) { |
| 220 | // IEC 61966-2-1:1999 |
| 221 | return linear <= 0.0031308 ? linear * 12.92 : (pow(linear, 1.0 / 2.4) * 1.055) - 0.055; |
| 222 | } |
| 223 | |
| 224 | vec3 OETF_sRGB(const vec3 linear) { |
| 225 | return vec3(OETF_sRGB(linear.r), OETF_sRGB(linear.g), OETF_sRGB(linear.b)); |
| 226 | } |
| 227 | |
| 228 | float EOTF_sRGB(float srgb) { |
| 229 | // IEC 61966-2-1:1999 |
| 230 | return srgb <= 0.04045 ? srgb / 12.92 : pow((srgb + 0.055) / 1.055, 2.4); |
| 231 | } |
| 232 | )__SHADER__"; |
| 233 | |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame^] | 234 | const char* gFS_TransferFunction[4] = { |
| 235 | // Conversion done by the texture unit (sRGB) |
| 236 | R"__SHADER__( |
| 237 | vec3 EOTF_Parametric(const vec3 x) { |
| 238 | return x; |
| 239 | } |
| 240 | )__SHADER__", |
| 241 | // Full transfer function |
| 242 | // TODO: We should probably use a 1D LUT (256x1 with texelFetch() since input is 8 bit) |
| 243 | // TODO: That would cause 3 dependent texture fetches. Is it worth it? |
| 244 | R"__SHADER__( |
| 245 | float EOTF_Parametric(float x) { |
| 246 | return x <= transferFunction[4] |
| 247 | ? transferFunction[3] * x + transferFunction[6] |
| 248 | : pow(transferFunction[1] * x + transferFunction[2], transferFunction[0]) |
| 249 | + transferFunction[5]; |
| 250 | } |
| 251 | |
| 252 | vec3 EOTF_Parametric(const vec3 x) { |
| 253 | return vec3(EOTF_Parametric(x.r), EOTF_Parametric(x.g), EOTF_Parametric(x.b)); |
| 254 | } |
| 255 | )__SHADER__", |
| 256 | // Limited transfer function, e = f = 0.0 |
| 257 | R"__SHADER__( |
| 258 | float EOTF_Parametric(float x) { |
| 259 | return x <= transferFunction[4] |
| 260 | ? transferFunction[3] * x |
| 261 | : pow(transferFunction[1] * x + transferFunction[2], transferFunction[0]); |
| 262 | } |
| 263 | |
| 264 | vec3 EOTF_Parametric(const vec3 x) { |
| 265 | return vec3(EOTF_Parametric(x.r), EOTF_Parametric(x.g), EOTF_Parametric(x.b)); |
| 266 | } |
| 267 | )__SHADER__", |
| 268 | // Gamma transfer function, e = f = 0.0 |
| 269 | R"__SHADER__( |
| 270 | vec3 EOTF_Parametric(const vec3 x) { |
| 271 | return vec3(pow(x.r, transferFunctionGamma), |
| 272 | pow(x.g, transferFunctionGamma), |
| 273 | pow(x.b, transferFunctionGamma)); |
| 274 | } |
| 275 | )__SHADER__" |
| 276 | }; |
| 277 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 278 | // Dithering must be done in the quantization space |
| 279 | // When we are writing to an sRGB framebuffer, we must do the following: |
Romain Guy | 636afc1 | 2017-02-07 11:21:05 -0800 | [diff] [blame] | 280 | // EOTF(OETF(color) + dither) |
Romain Guy | 0d86d7e | 2017-03-15 20:38:11 -0700 | [diff] [blame] | 281 | // The dithering pattern is generated with a triangle noise generator in the range [-1.0,1.0] |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 282 | // TODO: Handle linear fp16 render targets |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame^] | 283 | const char* gFS_GradientFunctions = R"__SHADER__( |
Romain Guy | 9fe7e16 | 2017-02-03 16:16:07 -0800 | [diff] [blame] | 284 | float triangleNoise(const highp vec2 n) { |
| 285 | highp vec2 p = fract(n * vec2(5.3987, 5.4421)); |
| 286 | p += dot(p.yx, p.xy + vec2(21.5351, 14.3137)); |
| 287 | highp float xy = p.x * p.y; |
| 288 | return fract(xy * 95.4307) + fract(xy * 75.04961) - 1.0; |
| 289 | } |
Romain Guy | 9fe7e16 | 2017-02-03 16:16:07 -0800 | [diff] [blame] | 290 | )__SHADER__"; |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame^] | 291 | |
| 292 | const char* gFS_GradientPreamble[2] = { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 293 | // Linear framebuffer |
Romain Guy | 6183c97 | 2017-03-16 12:24:55 -0700 | [diff] [blame] | 294 | R"__SHADER__( |
| 295 | vec4 dither(const vec4 color) { |
| 296 | return color + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0); |
| 297 | } |
| 298 | vec4 gradientMix(const vec4 a, const vec4 b, float v) { |
| 299 | vec4 c = mix(a, b, v); |
| 300 | return vec4(c.rgb * c.a, c.a); |
| 301 | } |
| 302 | )__SHADER__", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 303 | // sRGB framebuffer |
Romain Guy | 6183c97 | 2017-03-16 12:24:55 -0700 | [diff] [blame] | 304 | R"__SHADER__( |
| 305 | vec4 dither(const vec4 color) { |
| 306 | vec3 dithered = sqrt(color.rgb) + (triangleNoise(gl_FragCoord.xy * screenSize.xy) / 255.0); |
| 307 | return vec4(dithered * dithered, color.a); |
| 308 | } |
| 309 | vec4 gradientMixMix(const vec4 a, const vec4 b, float v) { |
| 310 | vec4 c = mix(a, b, v); |
| 311 | return vec4(c.rgb * c.a, c.a); |
| 312 | } |
| 313 | )__SHADER__", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 314 | }; |
| 315 | |
| 316 | // Uses luminance coefficients from Rec.709 to choose the appropriate gamma |
| 317 | // The gamma() function assumes that bright text will be displayed on a dark |
| 318 | // background and that dark text will be displayed on bright background |
| 319 | // The gamma coefficient is chosen to thicken or thin the text accordingly |
| 320 | // The dot product used to compute the luminance could be approximated with |
| 321 | // a simple max(color.r, color.g, color.b) |
Romain Guy | 9fe7e16 | 2017-02-03 16:16:07 -0800 | [diff] [blame] | 322 | const char* gFS_Gamma_Preamble = R"__SHADER__( |
| 323 | #define GAMMA (%.2f) |
| 324 | #define GAMMA_INV (%.2f) |
| 325 | |
| 326 | float gamma(float a, const vec3 color) { |
| 327 | float luminance = dot(color, vec3(0.2126, 0.7152, 0.0722)); |
| 328 | return pow(a, luminance < 0.5 ? GAMMA_INV : GAMMA); |
| 329 | } |
| 330 | )__SHADER__"; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 331 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 332 | const char* gFS_Main = |
| 333 | "\nvoid main(void) {\n" |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 334 | " vec4 fragColor;\n"; |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 335 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 336 | const char* gFS_Main_AddDither = |
| 337 | " fragColor = dither(fragColor);\n"; |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 338 | |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 339 | // General case |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 340 | const char* gFS_Main_FetchColor = |
| 341 | " fragColor = color;\n"; |
Romain Guy | 740bf2b | 2011-04-26 15:33:10 -0700 | [diff] [blame] | 342 | const char* gFS_Main_ModulateColor = |
| 343 | " fragColor *= color.a;\n"; |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 344 | const char* gFS_Main_ApplyVertexAlphaLinearInterp = |
Chris Craik | 6ebdc11 | 2012-08-31 18:24:33 -0700 | [diff] [blame] | 345 | " fragColor *= alpha;\n"; |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 346 | const char* gFS_Main_ApplyVertexAlphaShadowInterp = |
Chris Craik | 138c21f | 2016-04-28 16:59:42 -0700 | [diff] [blame] | 347 | // map alpha through shadow alpha sampler |
| 348 | " fragColor *= texture2D(baseSampler, vec2(alpha, 0.5)).a;\n"; |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 349 | const char* gFS_Main_FetchTexture[2] = { |
| 350 | // Don't modulate |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame^] | 351 | " fragColor = colorConvert(texture2D(baseSampler, outTexCoords));\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 352 | // Modulate |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame^] | 353 | " fragColor = color * colorConvert(texture2D(baseSampler, outTexCoords));\n" |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 354 | }; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 355 | const char* gFS_Main_FetchA8Texture[4] = { |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 356 | // Don't modulate |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 357 | " fragColor = texture2D(baseSampler, outTexCoords);\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 358 | " fragColor = texture2D(baseSampler, outTexCoords);\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 359 | // Modulate |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 360 | " fragColor = color * texture2D(baseSampler, outTexCoords).a;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 361 | " fragColor = color * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 362 | }; |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 363 | const char* gFS_Main_FetchGradient[6] = { |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 364 | // Linear |
Romain Guy | 320d46b | 2012-08-08 16:05:42 -0700 | [diff] [blame] | 365 | " vec4 gradientColor = texture2D(gradientSampler, linear);\n", |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 366 | |
Romain Guy | 6183c97 | 2017-03-16 12:24:55 -0700 | [diff] [blame] | 367 | " vec4 gradientColor = gradientMix(startColor, endColor, clamp(linear, 0.0, 1.0));\n", |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 368 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 369 | // Circular |
Romain Guy | 320d46b | 2012-08-08 16:05:42 -0700 | [diff] [blame] | 370 | " vec4 gradientColor = texture2D(gradientSampler, vec2(length(circular), 0.5));\n", |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 371 | |
Romain Guy | 6183c97 | 2017-03-16 12:24:55 -0700 | [diff] [blame] | 372 | " vec4 gradientColor = gradientMix(startColor, endColor, clamp(length(circular), 0.0, 1.0));\n", |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 373 | |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 374 | // Sweep |
Romain Guy | 6355347 | 2012-07-18 20:04:14 -0700 | [diff] [blame] | 375 | " 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] | 376 | " vec4 gradientColor = texture2D(gradientSampler, vec2(index - floor(index), 0.5));\n", |
Romain Guy | 211efea | 2012-07-31 21:16:07 -0700 | [diff] [blame] | 377 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 378 | " highp float index = atan(sweep.y, sweep.x) * 0.15915494309; // inv(2 * PI)\n" |
Romain Guy | 6183c97 | 2017-03-16 12:24:55 -0700 | [diff] [blame] | 379 | " vec4 gradientColor = gradientMix(startColor, endColor, clamp(index - floor(index), 0.0, 1.0));\n" |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 380 | }; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 381 | const char* gFS_Main_FetchBitmap = |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame^] | 382 | " vec4 bitmapColor = colorConvert(texture2D(bitmapSampler, outBitmapTexCoords));\n"; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 383 | const char* gFS_Main_FetchBitmapNpot = |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame^] | 384 | " vec4 bitmapColor = colorConvert(texture2D(bitmapSampler, wrap(outBitmapTexCoords)));\n"; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 385 | const char* gFS_Main_BlendShadersBG = |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 386 | " fragColor = blendShaders(gradientColor, bitmapColor)"; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 387 | const char* gFS_Main_BlendShadersGB = |
| 388 | " fragColor = blendShaders(bitmapColor, gradientColor)"; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 389 | const char* gFS_Main_BlendShaders_Modulate[6] = { |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 390 | // Don't modulate |
| 391 | ";\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 392 | ";\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 393 | // Modulate |
Chet Haase | 0990ffb | 2012-09-17 17:43:45 -0700 | [diff] [blame] | 394 | " * color.a;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 395 | " * color.a;\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 396 | // Modulate with alpha 8 texture |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 397 | " * texture2D(baseSampler, outTexCoords).a;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 398 | " * gamma(texture2D(baseSampler, outTexCoords).a, color.rgb);\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 399 | }; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 400 | const char* gFS_Main_GradientShader_Modulate[6] = { |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 401 | // Don't modulate |
| 402 | " fragColor = gradientColor;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 403 | " fragColor = gradientColor;\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 404 | // Modulate |
Chet Haase | 0990ffb | 2012-09-17 17:43:45 -0700 | [diff] [blame] | 405 | " fragColor = gradientColor * color.a;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 406 | " fragColor = gradientColor * color.a;\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 407 | // Modulate with alpha 8 texture |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 408 | " fragColor = gradientColor * texture2D(baseSampler, outTexCoords).a;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 409 | " fragColor = gradientColor * gamma(texture2D(baseSampler, outTexCoords).a, gradientColor.rgb);\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 410 | }; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 411 | const char* gFS_Main_BitmapShader_Modulate[6] = { |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 412 | // Don't modulate |
| 413 | " fragColor = bitmapColor;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 414 | " fragColor = bitmapColor;\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 415 | // Modulate |
Chet Haase | 0990ffb | 2012-09-17 17:43:45 -0700 | [diff] [blame] | 416 | " fragColor = bitmapColor * color.a;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 417 | " fragColor = bitmapColor * color.a;\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 418 | // Modulate with alpha 8 texture |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 419 | " fragColor = bitmapColor * texture2D(baseSampler, outTexCoords).a;\n", |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 420 | " fragColor = bitmapColor * gamma(texture2D(baseSampler, outTexCoords).a, bitmapColor.rgb);\n", |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 421 | }; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 422 | const char* gFS_Main_FragColor = |
| 423 | " gl_FragColor = fragColor;\n"; |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 424 | const char* gFS_Main_FragColor_HasColors = |
| 425 | " gl_FragColor *= outColors;\n"; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 426 | const char* gFS_Main_FragColor_Blend = |
| 427 | " gl_FragColor = blendFramebuffer(fragColor, gl_LastFragColor);\n"; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 428 | const char* gFS_Main_FragColor_Blend_Swap = |
| 429 | " gl_FragColor = blendFramebuffer(gl_LastFragColor, fragColor);\n"; |
Derek Sollenberger | 76d3a1b | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 430 | const char* gFS_Main_ApplyColorOp[3] = { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 431 | // None |
| 432 | "", |
| 433 | // Matrix |
Chris Craik | 73821c8 | 2014-09-16 17:32:13 -0700 | [diff] [blame] | 434 | " fragColor.rgb /= (fragColor.a + 0.0019);\n" // un-premultiply |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 435 | " fragColor *= colorMatrix;\n" |
Chris Craik | 73821c8 | 2014-09-16 17:32:13 -0700 | [diff] [blame] | 436 | " fragColor += colorMatrixVector;\n" |
| 437 | " fragColor.rgb *= (fragColor.a + 0.0019);\n", // re-premultiply |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 438 | // PorterDuff |
| 439 | " fragColor = blendColors(colorBlend, fragColor);\n" |
| 440 | }; |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 441 | |
| 442 | // Note: LTRB -> xyzw |
| 443 | const char* gFS_Main_FragColor_HasRoundRectClip = |
| 444 | " mediump vec2 fragToLT = roundRectInnerRectLTRB.xy - roundRectPos;\n" |
| 445 | " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTRB.zw;\n" |
Chris Craik | f99f320 | 2014-08-05 15:31:52 -0700 | [diff] [blame] | 446 | |
| 447 | // divide + multiply by 128 to avoid falling out of range in length() function |
| 448 | " mediump vec2 dist = max(max(fragToLT, fragFromRB), vec2(0.0, 0.0)) / 128.0;\n" |
| 449 | " mediump float linearDist = roundRectRadius - (length(dist) * 128.0);\n" |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 450 | " gl_FragColor *= clamp(linearDist, 0.0, 1.0);\n"; |
| 451 | |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 452 | const char* gFS_Main_DebugHighlight = |
| 453 | " gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n"; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 454 | const char* gFS_Footer = |
| 455 | "}\n\n"; |
| 456 | |
| 457 | /////////////////////////////////////////////////////////////////////////////// |
| 458 | // PorterDuff snippets |
| 459 | /////////////////////////////////////////////////////////////////////////////// |
| 460 | |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 461 | const char* gBlendOps[18] = { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 462 | // Clear |
| 463 | "return vec4(0.0, 0.0, 0.0, 0.0);\n", |
| 464 | // Src |
| 465 | "return src;\n", |
| 466 | // Dst |
| 467 | "return dst;\n", |
| 468 | // SrcOver |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 469 | "return src + dst * (1.0 - src.a);\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 470 | // DstOver |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 471 | "return dst + src * (1.0 - dst.a);\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 472 | // SrcIn |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 473 | "return src * dst.a;\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 474 | // DstIn |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 475 | "return dst * src.a;\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 476 | // SrcOut |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 477 | "return src * (1.0 - dst.a);\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 478 | // DstOut |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 479 | "return dst * (1.0 - src.a);\n", |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 480 | // SrcAtop |
| 481 | "return vec4(src.rgb * dst.a + (1.0 - src.a) * dst.rgb, dst.a);\n", |
| 482 | // DstAtop |
| 483 | "return vec4(dst.rgb * src.a + (1.0 - dst.a) * src.rgb, src.a);\n", |
| 484 | // Xor |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 485 | "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] | 486 | "src.a + dst.a - 2.0 * src.a * dst.a);\n", |
Derek Sollenberger | c0bf700 | 2015-03-06 13:48:27 -0500 | [diff] [blame] | 487 | // Plus |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 488 | "return min(src + dst, 1.0);\n", |
Derek Sollenberger | c0bf700 | 2015-03-06 13:48:27 -0500 | [diff] [blame] | 489 | // Modulate |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 490 | "return src * dst;\n", |
| 491 | // Screen |
| 492 | "return src + dst - src * dst;\n", |
| 493 | // Overlay |
| 494 | "return clamp(vec4(mix(" |
| 495 | "2.0 * src.rgb * dst.rgb + src.rgb * (1.0 - dst.a) + dst.rgb * (1.0 - src.a), " |
| 496 | "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), " |
| 497 | "step(dst.a, 2.0 * dst.rgb)), " |
| 498 | "src.a + dst.a - src.a * dst.a), 0.0, 1.0);\n", |
| 499 | // Darken |
| 500 | "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + " |
| 501 | "min(src.rgb * dst.a, dst.rgb * src.a), src.a + dst.a - src.a * dst.a);\n", |
| 502 | // Lighten |
| 503 | "return vec4(src.rgb * (1.0 - dst.a) + (1.0 - src.a) * dst.rgb + " |
| 504 | "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] | 505 | }; |
| 506 | |
| 507 | /////////////////////////////////////////////////////////////////////////////// |
| 508 | // Constructors/destructors |
| 509 | /////////////////////////////////////////////////////////////////////////////// |
| 510 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 511 | ProgramCache::ProgramCache(Extensions& extensions) |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 512 | : mHasES3(extensions.getMajorGlVersion() >= 3) |
Romain Guy | efb4b06 | 2017-02-27 11:00:04 -0800 | [diff] [blame] | 513 | , mHasLinearBlending(extensions.hasLinearBlending()) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | ProgramCache::~ProgramCache() { |
| 517 | clear(); |
| 518 | } |
| 519 | |
| 520 | /////////////////////////////////////////////////////////////////////////////// |
| 521 | // Cache management |
| 522 | /////////////////////////////////////////////////////////////////////////////// |
| 523 | |
| 524 | void ProgramCache::clear() { |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 525 | PROGRAM_LOGD("Clearing program cache"); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 526 | mCache.clear(); |
| 527 | } |
| 528 | |
| 529 | Program* ProgramCache::get(const ProgramDescription& description) { |
| 530 | programid key = description.key(); |
Chris Craik | 096b8d9 | 2013-03-01 11:08:11 -0800 | [diff] [blame] | 531 | if (key == (PROGRAM_KEY_TEXTURE | PROGRAM_KEY_A8_TEXTURE)) { |
| 532 | // program for A8, unmodulated, texture w/o shader (black text/path textures) is equivalent |
| 533 | // to standard texture program (bitmaps, patches). Consider them equivalent. |
| 534 | key = PROGRAM_KEY_TEXTURE; |
| 535 | } |
| 536 | |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 537 | auto iter = mCache.find(key); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 538 | Program* program = nullptr; |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 539 | if (iter == mCache.end()) { |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 540 | description.log("Could not find program"); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 541 | program = generateProgram(description, key); |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 542 | mCache[key] = std::unique_ptr<Program>(program); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 543 | } else { |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 544 | program = iter->second.get(); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 545 | } |
| 546 | return program; |
| 547 | } |
| 548 | |
| 549 | /////////////////////////////////////////////////////////////////////////////// |
| 550 | // Program generation |
| 551 | /////////////////////////////////////////////////////////////////////////////// |
| 552 | |
Andreas Gampe | 64bb413 | 2014-11-22 00:35:09 +0000 | [diff] [blame] | 553 | Program* ProgramCache::generateProgram(const ProgramDescription& description, programid key) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 554 | String8 vertexShader = generateVertexShader(description); |
| 555 | String8 fragmentShader = generateFragmentShader(description); |
| 556 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 557 | return new Program(description, vertexShader.string(), fragmentShader.string()); |
| 558 | } |
| 559 | |
| 560 | static inline size_t gradientIndex(const ProgramDescription& description) { |
| 561 | return description.gradientType * 2 + description.isSimpleGradient; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | String8 ProgramCache::generateVertexShader(const ProgramDescription& description) { |
| 565 | // Add attributes |
Chris Craik | 8bd68c6 | 2015-08-19 15:29:05 -0700 | [diff] [blame] | 566 | String8 shader(gVS_Header_Start); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 567 | if (description.hasTexture || description.hasExternalTexture) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 568 | shader.append(gVS_Header_Attributes_TexCoords); |
| 569 | } |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 570 | if (description.hasVertexAlpha) { |
| 571 | shader.append(gVS_Header_Attributes_VertexAlphaParameters); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 572 | } |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 573 | if (description.hasColors) { |
| 574 | shader.append(gVS_Header_Attributes_Colors); |
| 575 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 576 | // Uniforms |
| 577 | shader.append(gVS_Header_Uniforms); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 578 | if (description.hasTextureTransform) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 579 | shader.append(gVS_Header_Uniforms_TextureTransform); |
| 580 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 581 | if (description.hasGradient) { |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 582 | shader.append(gVS_Header_Uniforms_HasGradient); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 583 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 584 | if (description.hasBitmap) { |
| 585 | shader.append(gVS_Header_Uniforms_HasBitmap); |
| 586 | } |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 587 | if (description.hasRoundRectClip) { |
| 588 | shader.append(gVS_Header_Uniforms_HasRoundRectClip); |
| 589 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 590 | // Varyings |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 591 | if (description.hasTexture || description.hasExternalTexture) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 592 | shader.append(gVS_Header_Varyings_HasTexture); |
| 593 | } |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 594 | if (description.hasVertexAlpha) { |
| 595 | shader.append(gVS_Header_Varyings_HasVertexAlpha); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 596 | } |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 597 | if (description.hasColors) { |
| 598 | shader.append(gVS_Header_Varyings_HasColors); |
| 599 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 600 | if (description.hasGradient) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 601 | shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 602 | } |
| 603 | if (description.hasBitmap) { |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 604 | shader.append(gVS_Header_Varyings_HasBitmap); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 605 | } |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 606 | if (description.hasRoundRectClip) { |
| 607 | shader.append(gVS_Header_Varyings_HasRoundRectClip); |
| 608 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 609 | |
| 610 | // Begin the shader |
| 611 | shader.append(gVS_Main); { |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 612 | if (description.hasTextureTransform) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 613 | shader.append(gVS_Main_OutTransformedTexCoords); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 614 | } else if (description.hasTexture || description.hasExternalTexture) { |
| 615 | shader.append(gVS_Main_OutTexCoords); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 616 | } |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 617 | if (description.hasVertexAlpha) { |
| 618 | shader.append(gVS_Main_VertexAlpha); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 619 | } |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 620 | if (description.hasColors) { |
| 621 | shader.append(gVS_Main_OutColors); |
| 622 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 623 | if (description.hasBitmap) { |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 624 | shader.append(gVS_Main_OutBitmapTexCoords); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 625 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 626 | // Output transformed position |
| 627 | shader.append(gVS_Main_Position); |
Chet Haase | a1d12dd | 2012-09-21 14:50:14 -0700 | [diff] [blame] | 628 | if (description.hasGradient) { |
| 629 | shader.append(gVS_Main_OutGradient[gradientIndex(description)]); |
| 630 | } |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 631 | if (description.hasRoundRectClip) { |
| 632 | shader.append(gVS_Main_HasRoundRectClip); |
| 633 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 634 | } |
| 635 | // End the shader |
| 636 | shader.append(gVS_Footer); |
| 637 | |
| 638 | PROGRAM_LOGD("*** Generated vertex shader:\n\n%s", shader.string()); |
| 639 | |
| 640 | return shader; |
| 641 | } |
| 642 | |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 643 | static bool shaderOp(const ProgramDescription& description, String8& shader, |
| 644 | const int modulateOp, const char** snippets) { |
| 645 | int op = description.hasAlpha8Texture ? MODULATE_OP_MODULATE_A8 : modulateOp; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 646 | op = op * 2 + description.hasGammaCorrection; |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 647 | shader.append(snippets[op]); |
| 648 | return description.hasAlpha8Texture; |
| 649 | } |
| 650 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 651 | String8 ProgramCache::generateFragmentShader(const ProgramDescription& description) { |
Chris Craik | 8bd68c6 | 2015-08-19 15:29:05 -0700 | [diff] [blame] | 652 | String8 shader(gFS_Header_Start); |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 653 | |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame] | 654 | const bool blendFramebuffer = description.framebufferMode >= SkBlendMode::kPlus; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 655 | if (blendFramebuffer) { |
| 656 | shader.append(gFS_Header_Extension_FramebufferFetch); |
| 657 | } |
sergeyv | 9c97e48 | 2016-12-12 16:14:11 -0800 | [diff] [blame] | 658 | if (description.hasExternalTexture |
| 659 | || (description.hasBitmap && description.isShaderBitmapExternal)) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 660 | shader.append(gFS_Header_Extension_ExternalTexture); |
| 661 | } |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 662 | |
| 663 | shader.append(gFS_Header); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 664 | |
| 665 | // Varyings |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 666 | if (description.hasTexture || description.hasExternalTexture) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 667 | shader.append(gVS_Header_Varyings_HasTexture); |
| 668 | } |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 669 | if (description.hasVertexAlpha) { |
| 670 | shader.append(gVS_Header_Varyings_HasVertexAlpha); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 671 | } |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 672 | if (description.hasColors) { |
| 673 | shader.append(gVS_Header_Varyings_HasColors); |
| 674 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 675 | if (description.hasGradient) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 676 | shader.append(gVS_Header_Varyings_HasGradient[gradientIndex(description)]); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 677 | } |
| 678 | if (description.hasBitmap) { |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 679 | shader.append(gVS_Header_Varyings_HasBitmap); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 680 | } |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 681 | if (description.hasRoundRectClip) { |
| 682 | shader.append(gVS_Header_Varyings_HasRoundRectClip); |
| 683 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 684 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 685 | // Uniforms |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 686 | int modulateOp = MODULATE_OP_NO_MODULATE; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 687 | const bool singleColor = !description.hasTexture && !description.hasExternalTexture && |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 688 | !description.hasGradient && !description.hasBitmap; |
| 689 | |
| 690 | if (description.modulate || singleColor) { |
| 691 | shader.append(gFS_Uniforms_Color); |
| 692 | if (!singleColor) modulateOp = MODULATE_OP_MODULATE; |
| 693 | } |
Chris Craik | 138c21f | 2016-04-28 16:59:42 -0700 | [diff] [blame] | 694 | if (description.hasTexture || description.useShadowAlphaInterp) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 695 | shader.append(gFS_Uniforms_TextureSampler); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 696 | } else if (description.hasExternalTexture) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 697 | shader.append(gFS_Uniforms_ExternalTextureSampler); |
| 698 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 699 | if (description.hasGradient) { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 700 | shader.append(gFS_Uniforms_GradientSampler[description.isSimpleGradient]); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 701 | } |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 702 | if (description.hasRoundRectClip) { |
| 703 | shader.append(gFS_Uniforms_HasRoundRectClip); |
| 704 | } |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 705 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 706 | if (description.hasGammaCorrection) { |
| 707 | shader.appendFormat(gFS_Gamma_Preamble, Properties::textGamma, 1.0f / Properties::textGamma); |
| 708 | } |
| 709 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 710 | if (description.hasBitmap) { |
sergeyv | 9c97e48 | 2016-12-12 16:14:11 -0800 | [diff] [blame] | 711 | if (description.isShaderBitmapExternal) { |
| 712 | shader.append(gFS_Uniforms_BitmapExternalSampler); |
| 713 | } else { |
| 714 | shader.append(gFS_Uniforms_BitmapSampler); |
| 715 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 716 | } |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 717 | shader.append(gFS_Uniforms_ColorOp[static_cast<int>(description.colorOp)]); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 718 | |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame^] | 719 | if (description.hasColorSpaceConversion) { |
| 720 | shader.append(gFS_Uniforms_ColorSpaceConversion); |
| 721 | } |
| 722 | shader.append(gFS_Uniforms_TransferFunction[static_cast<int>(description.transferFunction)]); |
| 723 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 724 | // Generate required functions |
| 725 | if (description.hasGradient && description.hasBitmap) { |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 726 | generateBlend(shader, "blendShaders", description.shadersMode); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 727 | } |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 728 | if (description.colorOp == ProgramDescription::ColorFilterMode::Blend) { |
Romain Guy | 48daa54 | 2010-08-10 19:21:34 -0700 | [diff] [blame] | 729 | generateBlend(shader, "blendColors", description.colorMode); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 730 | } |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 731 | if (blendFramebuffer) { |
| 732 | generateBlend(shader, "blendFramebuffer", description.framebufferMode); |
| 733 | } |
sergeyv | 554ffeb | 2016-11-15 18:01:21 -0800 | [diff] [blame] | 734 | if (description.useShaderBasedWrap) { |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 735 | generateTextureWrap(shader, description.bitmapWrapS, description.bitmapWrapT); |
| 736 | } |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame^] | 737 | if (description.hasGradient || description.hasLinearTexture |
| 738 | || description.hasColorSpaceConversion) { |
| 739 | shader.append(gFS_sRGB_TransferFunctions); |
Romain Guy | 636afc1 | 2017-02-07 11:21:05 -0800 | [diff] [blame] | 740 | } |
| 741 | if (description.hasBitmap || ((description.hasTexture || description.hasExternalTexture) && |
| 742 | !description.hasAlpha8Texture)) { |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame^] | 743 | shader.append(gFS_TransferFunction[static_cast<int>(description.transferFunction)]); |
| 744 | shader.append(gFS_OETF[(description.hasLinearTexture || description.hasColorSpaceConversion) |
| 745 | && !mHasLinearBlending]); |
| 746 | shader.append(gFS_ColorConvert[description.hasColorSpaceConversion |
| 747 | ? 1 + description.hasTranslucentConversion : 0]); |
Romain Guy | 636afc1 | 2017-02-07 11:21:05 -0800 | [diff] [blame] | 748 | } |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 749 | if (description.hasGradient) { |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame^] | 750 | shader.append(gFS_GradientFunctions); |
| 751 | shader.append(gFS_GradientPreamble[mHasLinearBlending]); |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 752 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 753 | |
| 754 | // Begin the shader |
| 755 | shader.append(gFS_Main); { |
| 756 | // Stores the result in fragColor directly |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 757 | if (description.hasTexture || description.hasExternalTexture) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 758 | if (description.hasAlpha8Texture) { |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 759 | if (!description.hasGradient && !description.hasBitmap) { |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 760 | shader.append( |
| 761 | gFS_Main_FetchA8Texture[modulateOp * 2 + description.hasGammaCorrection]); |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 762 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 763 | } else { |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 764 | shader.append(gFS_Main_FetchTexture[modulateOp]); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 765 | } |
| 766 | } else { |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 767 | if (!description.hasGradient && !description.hasBitmap) { |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 768 | shader.append(gFS_Main_FetchColor); |
| 769 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 770 | } |
| 771 | if (description.hasGradient) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 772 | shader.append(gFS_Main_FetchGradient[gradientIndex(description)]); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 773 | } |
| 774 | if (description.hasBitmap) { |
sergeyv | 554ffeb | 2016-11-15 18:01:21 -0800 | [diff] [blame] | 775 | if (!description.useShaderBasedWrap) { |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 776 | shader.append(gFS_Main_FetchBitmap); |
| 777 | } else { |
| 778 | shader.append(gFS_Main_FetchBitmapNpot); |
| 779 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 780 | } |
Romain Guy | 740bf2b | 2011-04-26 15:33:10 -0700 | [diff] [blame] | 781 | bool applyModulate = false; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 782 | // Case when we have two shaders set |
| 783 | if (description.hasGradient && description.hasBitmap) { |
| 784 | if (description.isBitmapFirst) { |
| 785 | shader.append(gFS_Main_BlendShadersBG); |
| 786 | } else { |
| 787 | shader.append(gFS_Main_BlendShadersGB); |
| 788 | } |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 789 | applyModulate = shaderOp(description, shader, modulateOp, |
| 790 | gFS_Main_BlendShaders_Modulate); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 791 | } else { |
| 792 | if (description.hasGradient) { |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 793 | applyModulate = shaderOp(description, shader, modulateOp, |
| 794 | gFS_Main_GradientShader_Modulate); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 795 | } else if (description.hasBitmap) { |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 796 | applyModulate = shaderOp(description, shader, modulateOp, |
| 797 | gFS_Main_BitmapShader_Modulate); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 798 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 799 | } |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 800 | |
Romain Guy | 740bf2b | 2011-04-26 15:33:10 -0700 | [diff] [blame] | 801 | if (description.modulate && applyModulate) { |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 802 | shader.append(gFS_Main_ModulateColor); |
Romain Guy | 740bf2b | 2011-04-26 15:33:10 -0700 | [diff] [blame] | 803 | } |
Romain Guy | a938f56 | 2012-09-13 20:31:08 -0700 | [diff] [blame] | 804 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 805 | // Apply the color op if needed |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 806 | shader.append(gFS_Main_ApplyColorOp[static_cast<int>(description.colorOp)]); |
Chris Craik | 9f44a13 | 2012-09-13 18:34:55 -0700 | [diff] [blame] | 807 | |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 808 | if (description.hasVertexAlpha) { |
| 809 | if (description.useShadowAlphaInterp) { |
| 810 | shader.append(gFS_Main_ApplyVertexAlphaShadowInterp); |
Chris Craik | bf75945 | 2014-08-11 16:00:44 -0700 | [diff] [blame] | 811 | } else { |
Chris Craik | 91a8c7c | 2014-08-12 14:31:35 -0700 | [diff] [blame] | 812 | shader.append(gFS_Main_ApplyVertexAlphaLinearInterp); |
Chris Craik | bf75945 | 2014-08-11 16:00:44 -0700 | [diff] [blame] | 813 | } |
Chris Craik | 9f44a13 | 2012-09-13 18:34:55 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 816 | if (description.hasGradient) { |
| 817 | shader.append(gFS_Main_AddDither); |
| 818 | } |
| 819 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 820 | // Output the fragment |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 821 | if (!blendFramebuffer) { |
| 822 | shader.append(gFS_Main_FragColor); |
| 823 | } else { |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 824 | shader.append(!description.swapSrcDst ? |
| 825 | gFS_Main_FragColor_Blend : gFS_Main_FragColor_Blend_Swap); |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 826 | } |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 827 | if (description.hasColors) { |
| 828 | shader.append(gFS_Main_FragColor_HasColors); |
| 829 | } |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 830 | if (description.hasRoundRectClip) { |
| 831 | shader.append(gFS_Main_FragColor_HasRoundRectClip); |
| 832 | } |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 833 | if (description.hasDebugHighlight) { |
| 834 | shader.append(gFS_Main_DebugHighlight); |
| 835 | } |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 836 | } |
| 837 | // End the shader |
| 838 | shader.append(gFS_Footer); |
| 839 | |
Romain Guy | 91a8ec0 | 2017-02-08 07:45:11 -0800 | [diff] [blame] | 840 | #if DEBUG_PROGRAMS |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 841 | PROGRAM_LOGD("*** Generated fragment shader:\n\n"); |
| 842 | printLongString(shader); |
Romain Guy | 91a8ec0 | 2017-02-08 07:45:11 -0800 | [diff] [blame] | 843 | #endif |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 844 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 845 | return shader; |
| 846 | } |
| 847 | |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame] | 848 | void ProgramCache::generateBlend(String8& shader, const char* name, SkBlendMode mode) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 849 | shader.append("\nvec4 "); |
| 850 | shader.append(name); |
| 851 | shader.append("(vec4 src, vec4 dst) {\n"); |
| 852 | shader.append(" "); |
Mike Reed | c2f31df | 2016-10-28 17:21:45 -0400 | [diff] [blame] | 853 | shader.append(gBlendOps[(int)mode]); |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 854 | shader.append("}\n"); |
| 855 | } |
| 856 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 857 | void ProgramCache::generateTextureWrap(String8& shader, GLenum wrapS, GLenum wrapT) { |
Romain Guy | 6355347 | 2012-07-18 20:04:14 -0700 | [diff] [blame] | 858 | shader.append("\nhighp vec2 wrap(highp vec2 texCoords) {\n"); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 859 | if (wrapS == GL_MIRRORED_REPEAT) { |
Romain Guy | 6355347 | 2012-07-18 20:04:14 -0700 | [diff] [blame] | 860 | shader.append(" highp float xMod2 = mod(texCoords.x, 2.0);\n"); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 861 | shader.append(" if (xMod2 > 1.0) xMod2 = 2.0 - xMod2;\n"); |
| 862 | } |
| 863 | if (wrapT == GL_MIRRORED_REPEAT) { |
Romain Guy | 6355347 | 2012-07-18 20:04:14 -0700 | [diff] [blame] | 864 | shader.append(" highp float yMod2 = mod(texCoords.y, 2.0);\n"); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 865 | shader.append(" if (yMod2 > 1.0) yMod2 = 2.0 - yMod2;\n"); |
| 866 | } |
| 867 | shader.append(" return vec2("); |
| 868 | switch (wrapS) { |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 869 | case GL_CLAMP_TO_EDGE: |
| 870 | shader.append("texCoords.x"); |
| 871 | break; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 872 | case GL_REPEAT: |
| 873 | shader.append("mod(texCoords.x, 1.0)"); |
| 874 | break; |
| 875 | case GL_MIRRORED_REPEAT: |
| 876 | shader.append("xMod2"); |
| 877 | break; |
| 878 | } |
| 879 | shader.append(", "); |
| 880 | switch (wrapT) { |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 881 | case GL_CLAMP_TO_EDGE: |
| 882 | shader.append("texCoords.y"); |
| 883 | break; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 884 | case GL_REPEAT: |
| 885 | shader.append("mod(texCoords.y, 1.0)"); |
| 886 | break; |
| 887 | case GL_MIRRORED_REPEAT: |
| 888 | shader.append("yMod2"); |
| 889 | break; |
| 890 | } |
| 891 | shader.append(");\n"); |
| 892 | shader.append("}\n"); |
| 893 | } |
| 894 | |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 895 | void ProgramCache::printLongString(const String8& shader) const { |
| 896 | ssize_t index = 0; |
| 897 | ssize_t lastIndex = 0; |
| 898 | const char* str = shader.string(); |
| 899 | while ((index = shader.find("\n", index)) > -1) { |
| 900 | String8 line(str, index - lastIndex); |
| 901 | if (line.length() == 0) line.append("\n"); |
Chris Craik | 1c1c3fe | 2015-03-06 09:40:35 -0800 | [diff] [blame] | 902 | ALOGD("%s", line.string()); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 903 | index++; |
| 904 | str += (index - lastIndex); |
| 905 | lastIndex = index; |
| 906 | } |
| 907 | } |
| 908 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 909 | }; // namespace uirenderer |
| 910 | }; // namespace android |