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