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