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