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