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