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