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