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