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