epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 10 | #include "GrGLProgram.h" |
| 11 | |
| 12 | #include "GrBinHashKey.h" |
| 13 | #include "GrGLConfig.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 14 | |
tomhudson@google.com | 0c8d93a | 2011-07-01 17:08:26 +0000 | [diff] [blame] | 15 | #include "SkTrace.h" |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 16 | #include "SkXfermode.h" |
| 17 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | const char* GrPrecision() { |
| 21 | if (GR_GL_SUPPORT_ES2) { |
| 22 | return "mediump"; |
| 23 | } else { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 24 | return " "; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 25 | } |
| 26 | } |
| 27 | |
| 28 | const char* GrShaderPrecision() { |
| 29 | if (GR_GL_SUPPORT_ES2) { |
| 30 | return "precision mediump float;\n"; |
| 31 | } else { |
| 32 | return ""; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | } // namespace |
| 37 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 38 | #define PRINT_SHADERS 0 |
| 39 | |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 40 | #if GR_GL_ATTRIBUTE_MATRICES |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 41 | #define VIEW_MATRIX_NAME "aViewM" |
| 42 | #else |
| 43 | #define VIEW_MATRIX_NAME "uViewM" |
| 44 | #endif |
| 45 | |
| 46 | #define POS_ATTR_NAME "aPosition" |
| 47 | #define COL_ATTR_NAME "aColor" |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 48 | #define COL_UNI_NAME "uColor" |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 49 | #define EDGES_UNI_NAME "uEdges" |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 50 | #define COL_FILTER_UNI_NAME "uColorFilter" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 51 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 52 | static inline void tex_attr_name(int coordIdx, GrStringBuilder* s) { |
| 53 | *s = "aTexCoord"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 54 | s->appendS32(coordIdx); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static inline const char* float_vector_type(int count) { |
| 58 | static const char* FLOAT_VECS[] = {"ERROR", "float", "vec2", "vec3", "vec4"}; |
| 59 | GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(FLOAT_VECS)); |
| 60 | return FLOAT_VECS[count]; |
| 61 | } |
| 62 | |
| 63 | static inline const char* vector_homog_coord(int count) { |
| 64 | static const char* HOMOGS[] = {"ERROR", "", ".y", ".z", ".w"}; |
| 65 | GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(HOMOGS)); |
| 66 | return HOMOGS[count]; |
| 67 | } |
| 68 | |
| 69 | static inline const char* vector_nonhomog_coords(int count) { |
| 70 | static const char* NONHOMOGS[] = {"ERROR", "", ".x", ".xy", ".xyz"}; |
| 71 | GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(NONHOMOGS)); |
| 72 | return NONHOMOGS[count]; |
| 73 | } |
| 74 | |
| 75 | static inline const char* vector_all_coords(int count) { |
| 76 | static const char* ALL[] = {"ERROR", "", ".xy", ".xyz", ".xyzw"}; |
| 77 | GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ALL)); |
| 78 | return ALL[count]; |
| 79 | } |
| 80 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 81 | static inline const char* all_ones_vec(int count) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 82 | static const char* ONESVEC[] = {"ERROR", "1.0", "vec2(1,1)", |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 83 | "vec3(1,1,1)", "vec4(1,1,1,1)"}; |
| 84 | GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ONESVEC)); |
| 85 | return ONESVEC[count]; |
| 86 | } |
| 87 | |
| 88 | static inline const char* all_zeros_vec(int count) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 89 | static const char* ZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)", |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 90 | "vec3(0,0,0)", "vec4(0,0,0,0)"}; |
| 91 | GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ZEROSVEC)); |
| 92 | return ZEROSVEC[count]; |
| 93 | } |
| 94 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 95 | static inline const char* declared_color_output_name() { return "fsColorOut"; } |
| 96 | static inline const char* dual_source_output_name() { return "dualSourceOut"; } |
| 97 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 98 | static void tex_matrix_name(int stage, GrStringBuilder* s) { |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 99 | #if GR_GL_ATTRIBUTE_MATRICES |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 100 | *s = "aTexM"; |
| 101 | #else |
| 102 | *s = "uTexM"; |
| 103 | #endif |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 104 | s->appendS32(stage); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 105 | } |
| 106 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 107 | static void normalized_texel_size_name(int stage, GrStringBuilder* s) { |
| 108 | *s = "uTexelSize"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 109 | s->appendS32(stage); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 110 | } |
| 111 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 112 | static void sampler_name(int stage, GrStringBuilder* s) { |
| 113 | *s = "uSampler"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 114 | s->appendS32(stage); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static void stage_varying_name(int stage, GrStringBuilder* s) { |
| 118 | *s = "vStage"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 119 | s->appendS32(stage); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static void radial2_param_name(int stage, GrStringBuilder* s) { |
| 123 | *s = "uRadial2Params"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 124 | s->appendS32(stage); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static void radial2_varying_name(int stage, GrStringBuilder* s) { |
| 128 | *s = "vB"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 129 | s->appendS32(stage); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 130 | } |
| 131 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 132 | static void convolve_param_names(int stage, GrStringBuilder* k, GrStringBuilder* i) { |
| 133 | *k = "uKernel"; |
| 134 | k->appendS32(stage); |
| 135 | *i = "uImageIncrement"; |
| 136 | i->appendS32(stage); |
| 137 | } |
| 138 | |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 139 | static void tex_domain_name(int stage, GrStringBuilder* s) { |
| 140 | *s = "uTexDom"; |
| 141 | s->appendS32(stage); |
| 142 | } |
| 143 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 144 | GrGLProgram::GrGLProgram() { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | GrGLProgram::~GrGLProgram() { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 148 | } |
| 149 | |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 150 | void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 151 | GrBlendCoeff* dstCoeff) const { |
| 152 | switch (fProgramDesc.fDualSrcOutput) { |
| 153 | case ProgramDesc::kNone_DualSrcOutput: |
| 154 | break; |
| 155 | // the prog will write a coverage value to the secondary |
| 156 | // output and the dst is blended by one minus that value. |
| 157 | case ProgramDesc::kCoverage_DualSrcOutput: |
| 158 | case ProgramDesc::kCoverageISA_DualSrcOutput: |
| 159 | case ProgramDesc::kCoverageISC_DualSrcOutput: |
| 160 | *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_BlendCoeff; |
| 161 | break; |
| 162 | default: |
| 163 | GrCrash("Unexpected dual source blend output"); |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 168 | void GrGLProgram::buildKey(GrBinHashKeyBuilder& key) const { |
| 169 | // Add stage configuration to the key |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 170 | key.keyData(reinterpret_cast<const uint32_t*>(&fProgramDesc), sizeof(ProgramDesc)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 171 | } |
| 172 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 173 | // assigns modulation of two vars to an output var |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 174 | // vars can be vec4s or floats (or one of each) |
| 175 | // result is always vec4 |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 176 | // if either var is "" then assign to the other var |
| 177 | // if both are "" then assign all ones |
| 178 | static inline void modulate_helper(const char* outputVar, |
| 179 | const char* var0, |
| 180 | const char* var1, |
| 181 | GrStringBuilder* code) { |
| 182 | GrAssert(NULL != outputVar); |
| 183 | GrAssert(NULL != var0); |
| 184 | GrAssert(NULL != var1); |
| 185 | GrAssert(NULL != code); |
| 186 | |
| 187 | bool has0 = '\0' != *var0; |
| 188 | bool has1 = '\0' != *var1; |
| 189 | |
| 190 | if (!has0 && !has1) { |
| 191 | code->appendf("\t%s = %s;\n", outputVar, all_ones_vec(4)); |
| 192 | } else if (!has0) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 193 | code->appendf("\t%s = vec4(%s);\n", outputVar, var1); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 194 | } else if (!has1) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 195 | code->appendf("\t%s = vec4(%s);\n", outputVar, var0); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 196 | } else { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 197 | code->appendf("\t%s = vec4(%s * %s);\n", outputVar, var0, var1); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | // assigns addition of two vars to an output var |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 202 | // vars can be vec4s or floats (or one of each) |
| 203 | // result is always vec4 |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 204 | // if either var is "" then assign to the other var |
| 205 | // if both are "" then assign all zeros |
| 206 | static inline void add_helper(const char* outputVar, |
| 207 | const char* var0, |
| 208 | const char* var1, |
| 209 | GrStringBuilder* code) { |
| 210 | GrAssert(NULL != outputVar); |
| 211 | GrAssert(NULL != var0); |
| 212 | GrAssert(NULL != var1); |
| 213 | GrAssert(NULL != code); |
| 214 | |
| 215 | bool has0 = '\0' != *var0; |
| 216 | bool has1 = '\0' != *var1; |
| 217 | |
| 218 | if (!has0 && !has1) { |
| 219 | code->appendf("\t%s = %s;\n", outputVar, all_zeros_vec(4)); |
| 220 | } else if (!has0) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 221 | code->appendf("\t%s = vec4(%s);\n", outputVar, var1); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 222 | } else if (!has1) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 223 | code->appendf("\t%s = vec4(%s);\n", outputVar, var0); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 224 | } else { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 225 | code->appendf("\t%s = vec4(%s + %s);\n", outputVar, var0, var1); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | // given two blend coeffecients determine whether the src |
| 230 | // and/or dst computation can be omitted. |
| 231 | static inline void needBlendInputs(SkXfermode::Coeff srcCoeff, |
| 232 | SkXfermode::Coeff dstCoeff, |
| 233 | bool* needSrcValue, |
| 234 | bool* needDstValue) { |
| 235 | if (SkXfermode::kZero_Coeff == srcCoeff) { |
| 236 | switch (dstCoeff) { |
| 237 | // these all read the src |
| 238 | case SkXfermode::kSC_Coeff: |
| 239 | case SkXfermode::kISC_Coeff: |
| 240 | case SkXfermode::kSA_Coeff: |
| 241 | case SkXfermode::kISA_Coeff: |
| 242 | *needSrcValue = true; |
| 243 | break; |
| 244 | default: |
| 245 | *needSrcValue = false; |
| 246 | break; |
| 247 | } |
| 248 | } else { |
| 249 | *needSrcValue = true; |
| 250 | } |
| 251 | if (SkXfermode::kZero_Coeff == dstCoeff) { |
| 252 | switch (srcCoeff) { |
| 253 | // these all read the dst |
| 254 | case SkXfermode::kDC_Coeff: |
| 255 | case SkXfermode::kIDC_Coeff: |
| 256 | case SkXfermode::kDA_Coeff: |
| 257 | case SkXfermode::kIDA_Coeff: |
| 258 | *needDstValue = true; |
| 259 | break; |
| 260 | default: |
| 261 | *needDstValue = false; |
| 262 | break; |
| 263 | } |
| 264 | } else { |
| 265 | *needDstValue = true; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | /** |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 270 | * Create a blend_coeff * value string to be used in shader code. Sets empty |
| 271 | * string if result is trivially zero. |
| 272 | */ |
| 273 | static void blendTermString(GrStringBuilder* str, SkXfermode::Coeff coeff, |
| 274 | const char* src, const char* dst, |
| 275 | const char* value) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 276 | switch (coeff) { |
| 277 | case SkXfermode::kZero_Coeff: /** 0 */ |
| 278 | *str = ""; |
| 279 | break; |
| 280 | case SkXfermode::kOne_Coeff: /** 1 */ |
| 281 | *str = value; |
| 282 | break; |
| 283 | case SkXfermode::kSC_Coeff: |
| 284 | str->printf("(%s * %s)", src, value); |
| 285 | break; |
| 286 | case SkXfermode::kISC_Coeff: |
| 287 | str->printf("((%s - %s) * %s)", all_ones_vec(4), src, value); |
| 288 | break; |
| 289 | case SkXfermode::kDC_Coeff: |
| 290 | str->printf("(%s * %s)", dst, value); |
| 291 | break; |
| 292 | case SkXfermode::kIDC_Coeff: |
| 293 | str->printf("((%s - %s) * %s)", all_ones_vec(4), dst, value); |
| 294 | break; |
| 295 | case SkXfermode::kSA_Coeff: /** src alpha */ |
| 296 | str->printf("(%s.a * %s)", src, value); |
| 297 | break; |
| 298 | case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */ |
| 299 | str->printf("((1.0 - %s.a) * %s)", src, value); |
| 300 | break; |
| 301 | case SkXfermode::kDA_Coeff: /** dst alpha */ |
| 302 | str->printf("(%s.a * %s)", dst, value); |
| 303 | break; |
| 304 | case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */ |
| 305 | str->printf("((1.0 - %s.a) * %s)", dst, value); |
| 306 | break; |
| 307 | default: |
| 308 | GrCrash("Unexpected xfer coeff."); |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | /** |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 313 | * Adds a line to the fragment shader code which modifies the color by |
| 314 | * the specified color filter. |
| 315 | */ |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 316 | static void addColorFilter(GrStringBuilder* fsCode, const char * outputVar, |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 317 | SkXfermode::Coeff uniformCoeff, |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 318 | SkXfermode::Coeff colorCoeff, |
| 319 | const char* inColor) { |
| 320 | GrStringBuilder colorStr, constStr; |
| 321 | blendTermString(&colorStr, colorCoeff, COL_FILTER_UNI_NAME, |
| 322 | inColor, inColor); |
| 323 | blendTermString(&constStr, uniformCoeff, COL_FILTER_UNI_NAME, |
| 324 | inColor, COL_FILTER_UNI_NAME); |
| 325 | |
| 326 | add_helper(outputVar, colorStr.c_str(), constStr.c_str(), fsCode); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 327 | } |
| 328 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 329 | bool GrGLProgram::genProgram(GrGLProgram::CachedData* programData) const { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 330 | |
| 331 | ShaderCodeSegments segments; |
| 332 | const uint32_t& layout = fProgramDesc.fVertexLayout; |
| 333 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 334 | programData->fUniLocations.reset(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 335 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 336 | SkXfermode::Coeff colorCoeff, uniformCoeff; |
| 337 | // The rest of transfer mode color filters have not been implemented |
| 338 | if (fProgramDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) { |
| 339 | GR_DEBUGCODE(bool success =) |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 340 | SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode> |
| 341 | (fProgramDesc.fColorFilterXfermode), |
| 342 | &uniformCoeff, &colorCoeff); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 343 | GR_DEBUGASSERT(success); |
| 344 | } else { |
| 345 | colorCoeff = SkXfermode::kOne_Coeff; |
| 346 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 347 | } |
| 348 | |
| 349 | bool needColorFilterUniform; |
| 350 | bool needComputedColor; |
| 351 | needBlendInputs(uniformCoeff, colorCoeff, |
| 352 | &needColorFilterUniform, &needComputedColor); |
| 353 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 354 | // the dual source output has no canonical var name, have to |
| 355 | // declare an output, which is incompatible with gl_FragColor/gl_FragData. |
| 356 | const char* fsColorOutput; |
| 357 | bool dualSourceOutputWritten = false; |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 358 | bool usingDeclaredOutputs = ProgramDesc::kNone_DualSrcOutput != |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 359 | fProgramDesc.fDualSrcOutput; |
| 360 | if (usingDeclaredOutputs) { |
| 361 | GrAssert(0 == segments.fHeader.size()); |
| 362 | segments.fHeader.printf("#version 150\n"); |
| 363 | fsColorOutput = declared_color_output_name(); |
| 364 | segments.fFSOutputs.appendf("out vec4 %s;\n", fsColorOutput); |
| 365 | } else { |
| 366 | fsColorOutput = "gl_FragColor"; |
| 367 | } |
| 368 | |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 369 | #if GR_GL_ATTRIBUTE_MATRICES |
| 370 | segments.fVSAttrs += "attribute mat3 " VIEW_MATRIX_NAME ";\n"; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 371 | programData->fUniLocations.fViewMatrixUni = kSetAsAttribute; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 372 | #else |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 373 | segments.fVSUnis += "uniform mat3 " VIEW_MATRIX_NAME ";\n"; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 374 | programData->fUniLocations.fViewMatrixUni = kUseUniform; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 375 | #endif |
| 376 | segments.fVSAttrs += "attribute vec2 " POS_ATTR_NAME ";\n"; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 377 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 378 | segments.fVSCode.append( |
| 379 | "void main() {\n" |
| 380 | "\tvec3 pos3 = " VIEW_MATRIX_NAME " * vec3("POS_ATTR_NAME", 1);\n" |
| 381 | "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n"); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 382 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 383 | // incoming color to current stage being processed. |
| 384 | GrStringBuilder inColor; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 385 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 386 | if (needComputedColor) { |
| 387 | switch (fProgramDesc.fColorType) { |
| 388 | case ProgramDesc::kAttribute_ColorType: |
| 389 | segments.fVSAttrs.append( "attribute vec4 " COL_ATTR_NAME ";\n"); |
| 390 | segments.fVaryings.append("varying vec4 vColor;\n"); |
| 391 | segments.fVSCode.append( "\tvColor = " COL_ATTR_NAME ";\n"); |
| 392 | inColor = "vColor"; |
| 393 | break; |
| 394 | case ProgramDesc::kUniform_ColorType: |
| 395 | segments.fFSUnis.append( "uniform vec4 " COL_UNI_NAME ";\n"); |
| 396 | programData->fUniLocations.fColorUni = kUseUniform; |
| 397 | inColor = COL_UNI_NAME; |
| 398 | break; |
| 399 | default: |
| 400 | GrAssert(ProgramDesc::kNone_ColorType == fProgramDesc.fColorType); |
| 401 | break; |
| 402 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 403 | } |
| 404 | |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 405 | if (fProgramDesc.fEmitsPointSize){ |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 406 | segments.fVSCode.append("\tgl_PointSize = 1.0;\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 407 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 408 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 409 | segments.fFSCode.append("void main() {\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 410 | |
| 411 | // add texture coordinates that are used to the list of vertex attr decls |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 412 | GrStringBuilder texCoordAttrs[GrDrawTarget::kMaxTexCoords]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 413 | for (int t = 0; t < GrDrawTarget::kMaxTexCoords; ++t) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 414 | if (GrDrawTarget::VertexUsesTexCoordIdx(t, layout)) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 415 | tex_attr_name(t, texCoordAttrs + t); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 416 | segments.fVSAttrs.appendf("attribute vec2 %s;\n", texCoordAttrs[t].c_str()); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 420 | /////////////////////////////////////////////////////////////////////////// |
| 421 | // compute the final color |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 422 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 423 | // if we have color stages string them together, feeding the output color |
| 424 | // of each to the next and generating code for each stage. |
| 425 | if (needComputedColor) { |
| 426 | GrStringBuilder outColor; |
| 427 | for (int s = 0; s < fProgramDesc.fFirstCoverageStage; ++s) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 428 | if (fProgramDesc.fStages[s].isEnabled()) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 429 | // create var to hold stage result |
| 430 | outColor = "color"; |
| 431 | outColor.appendS32(s); |
| 432 | segments.fFSCode.appendf("\tvec4 %s;\n", outColor.c_str()); |
| 433 | |
| 434 | const char* inCoords; |
| 435 | // figure out what our input coords are |
| 436 | if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) & |
| 437 | layout) { |
| 438 | inCoords = POS_ATTR_NAME; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 439 | } else { |
| 440 | int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout); |
| 441 | // we better have input tex coordinates if stage is enabled. |
| 442 | GrAssert(tcIdx >= 0); |
| 443 | GrAssert(texCoordAttrs[tcIdx].size()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 444 | inCoords = texCoordAttrs[tcIdx].c_str(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | genStageCode(s, |
| 448 | fProgramDesc.fStages[s], |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 449 | inColor.size() ? inColor.c_str() : NULL, |
| 450 | outColor.c_str(), |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 451 | inCoords, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 452 | &segments, |
| 453 | &programData->fUniLocations.fStages[s]); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 454 | inColor = outColor; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 457 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 458 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 459 | // if have all ones for the "dst" input to the color filter then we can make |
| 460 | // additional optimizations. |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 461 | if (needColorFilterUniform && !inColor.size() && |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 462 | (SkXfermode::kIDC_Coeff == uniformCoeff || |
| 463 | SkXfermode::kIDA_Coeff == uniformCoeff)) { |
| 464 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 465 | bool bogus; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 466 | needBlendInputs(SkXfermode::kZero_Coeff, colorCoeff, |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 467 | &needColorFilterUniform, &bogus); |
| 468 | } |
| 469 | if (needColorFilterUniform) { |
| 470 | segments.fFSUnis.append( "uniform vec4 " COL_FILTER_UNI_NAME ";\n"); |
| 471 | programData->fUniLocations.fColorFilterUni = kUseUniform; |
| 472 | } |
| 473 | |
| 474 | bool wroteFragColorZero = false; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 475 | if (SkXfermode::kZero_Coeff == uniformCoeff && |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 476 | SkXfermode::kZero_Coeff == colorCoeff) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 477 | segments.fFSCode.appendf("\t%s = %s;\n", |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 478 | fsColorOutput, |
| 479 | all_zeros_vec(4)); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 480 | wroteFragColorZero = true; |
| 481 | } else if (SkXfermode::kDst_Mode != fProgramDesc.fColorFilterXfermode) { |
| 482 | segments.fFSCode.appendf("\tvec4 filteredColor;\n"); |
| 483 | const char* color = inColor.size() ? inColor.c_str() : all_ones_vec(4); |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 484 | addColorFilter(&segments.fFSCode, "filteredColor", uniformCoeff, |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 485 | colorCoeff, color); |
| 486 | inColor = "filteredColor"; |
| 487 | } |
| 488 | |
| 489 | /////////////////////////////////////////////////////////////////////////// |
| 490 | // compute the partial coverage (coverage stages and edge aa) |
| 491 | |
| 492 | GrStringBuilder inCoverage; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 493 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 494 | // we don't need to compute coverage at all if we know the final shader |
| 495 | // output will be zero and we don't have a dual src blend output. |
| 496 | if (!wroteFragColorZero || |
| 497 | ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) { |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 498 | if (fProgramDesc.fEdgeAANumEdges > 0) { |
| 499 | segments.fFSUnis.append("uniform vec3 " EDGES_UNI_NAME "["); |
| 500 | segments.fFSUnis.appendS32(fProgramDesc.fEdgeAANumEdges); |
| 501 | segments.fFSUnis.append("];\n"); |
| 502 | programData->fUniLocations.fEdgesUni = kUseUniform; |
| 503 | int count = fProgramDesc.fEdgeAANumEdges; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 504 | segments.fFSCode.append( |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 505 | "\tvec3 pos = vec3(gl_FragCoord.xy, 1);\n"); |
| 506 | for (int i = 0; i < count; i++) { |
| 507 | segments.fFSCode.append("\tfloat a"); |
| 508 | segments.fFSCode.appendS32(i); |
| 509 | segments.fFSCode.append(" = clamp(dot(" EDGES_UNI_NAME "["); |
| 510 | segments.fFSCode.appendS32(i); |
| 511 | segments.fFSCode.append("], pos), 0.0, 1.0);\n"); |
| 512 | } |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 513 | if (fProgramDesc.fEdgeAAConcave && (count & 0x01) == 0) { |
| 514 | // For concave polys, we consider the edges in pairs. |
| 515 | segments.fFSFunctions.append("float cross2(vec2 a, vec2 b) {\n"); |
| 516 | segments.fFSFunctions.append("\treturn dot(a, vec2(b.y, -b.x));\n"); |
| 517 | segments.fFSFunctions.append("}\n"); |
| 518 | for (int i = 0; i < count; i += 2) { |
| 519 | segments.fFSCode.appendf("\tfloat eb%d;\n", i / 2); |
| 520 | segments.fFSCode.appendf("\tif (cross2(" EDGES_UNI_NAME "[%d].xy, " EDGES_UNI_NAME "[%d].xy) < 0.0) {\n", i, i + 1); |
| 521 | segments.fFSCode.appendf("\t\teb%d = a%d * a%d;\n", i / 2, i, i + 1); |
| 522 | segments.fFSCode.append("\t} else {\n"); |
| 523 | segments.fFSCode.appendf("\t\teb%d = a%d + a%d - a%d * a%d;\n", i / 2, i, i + 1, i, i + 1); |
| 524 | segments.fFSCode.append("\t}\n"); |
| 525 | } |
| 526 | segments.fFSCode.append("\tfloat edgeAlpha = "); |
| 527 | for (int i = 0; i < count / 2 - 1; i++) { |
| 528 | segments.fFSCode.appendf("min(eb%d, ", i); |
| 529 | } |
| 530 | segments.fFSCode.appendf("eb%d", count / 2 - 1); |
| 531 | for (int i = 0; i < count / 2 - 1; i++) { |
| 532 | segments.fFSCode.append(")"); |
| 533 | } |
| 534 | segments.fFSCode.append(";\n"); |
| 535 | } else { |
| 536 | segments.fFSCode.append("\tfloat edgeAlpha = "); |
| 537 | for (int i = 0; i < count - 1; i++) { |
| 538 | segments.fFSCode.appendf("min(a%d * a%d, ", i, i + 1); |
| 539 | } |
| 540 | segments.fFSCode.appendf("a%d * a0", count - 1); |
| 541 | for (int i = 0; i < count - 1; i++) { |
| 542 | segments.fFSCode.append(")"); |
| 543 | } |
| 544 | segments.fFSCode.append(";\n"); |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 545 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 546 | inCoverage = "edgeAlpha"; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | GrStringBuilder outCoverage; |
| 550 | const int& startStage = fProgramDesc.fFirstCoverageStage; |
| 551 | for (int s = startStage; s < GrDrawTarget::kNumStages; ++s) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 552 | if (fProgramDesc.fStages[s].isEnabled()) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 553 | // create var to hold stage output |
| 554 | outCoverage = "coverage"; |
| 555 | outCoverage.appendS32(s); |
| 556 | segments.fFSCode.appendf("\tvec4 %s;\n", outCoverage.c_str()); |
| 557 | |
| 558 | const char* inCoords; |
| 559 | // figure out what our input coords are |
| 560 | if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) & layout) { |
| 561 | inCoords = POS_ATTR_NAME; |
| 562 | } else { |
| 563 | int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout); |
| 564 | // we better have input tex coordinates if stage is enabled. |
| 565 | GrAssert(tcIdx >= 0); |
| 566 | GrAssert(texCoordAttrs[tcIdx].size()); |
| 567 | inCoords = texCoordAttrs[tcIdx].c_str(); |
| 568 | } |
| 569 | |
| 570 | genStageCode(s, |
| 571 | fProgramDesc.fStages[s], |
| 572 | inCoverage.size() ? inCoverage.c_str() : NULL, |
| 573 | outCoverage.c_str(), |
| 574 | inCoords, |
| 575 | &segments, |
| 576 | &programData->fUniLocations.fStages[s]); |
| 577 | inCoverage = outCoverage; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 578 | } |
| 579 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 580 | if (ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 581 | segments.fFSOutputs.appendf("out vec4 %s;\n", |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 582 | dual_source_output_name()); |
| 583 | bool outputIsZero = false; |
| 584 | GrStringBuilder coeff; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 585 | if (ProgramDesc::kCoverage_DualSrcOutput != |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 586 | fProgramDesc.fDualSrcOutput && !wroteFragColorZero) { |
| 587 | if (!inColor.size()) { |
| 588 | outputIsZero = true; |
| 589 | } else { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 590 | if (fProgramDesc.fDualSrcOutput == |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 591 | ProgramDesc::kCoverageISA_DualSrcOutput) { |
| 592 | coeff.printf("(1 - %s.a)", inColor.c_str()); |
| 593 | } else { |
| 594 | coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str()); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | if (outputIsZero) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 599 | segments.fFSCode.appendf("\t%s = %s;\n", |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 600 | dual_source_output_name(), |
| 601 | all_zeros_vec(4)); |
| 602 | } else { |
| 603 | modulate_helper(dual_source_output_name(), |
| 604 | coeff.c_str(), |
| 605 | inCoverage.c_str(), |
| 606 | &segments.fFSCode); |
| 607 | } |
| 608 | dualSourceOutputWritten = true; |
| 609 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 610 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 611 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 612 | /////////////////////////////////////////////////////////////////////////// |
| 613 | // combine color and coverage as frag color |
| 614 | |
| 615 | if (!wroteFragColorZero) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 616 | modulate_helper(fsColorOutput, |
| 617 | inColor.c_str(), |
| 618 | inCoverage.c_str(), |
| 619 | &segments.fFSCode); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 620 | } |
| 621 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 622 | segments.fVSCode.append("}\n"); |
| 623 | segments.fFSCode.append("}\n"); |
| 624 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 625 | /////////////////////////////////////////////////////////////////////////// |
| 626 | // compile and setup attribs and unis |
| 627 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 628 | if (!CompileFSAndVS(segments, programData)) { |
| 629 | return false; |
| 630 | } |
| 631 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 632 | if (!this->bindOutputsAttribsAndLinkProgram(texCoordAttrs, |
| 633 | usingDeclaredOutputs, |
| 634 | dualSourceOutputWritten, |
| 635 | programData)) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 636 | return false; |
| 637 | } |
| 638 | |
| 639 | this->getUniformLocationsAndInitCache(programData); |
| 640 | |
| 641 | return true; |
| 642 | } |
| 643 | |
| 644 | bool GrGLProgram::CompileFSAndVS(const ShaderCodeSegments& segments, |
| 645 | CachedData* programData) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 646 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 647 | static const int MAX_STRINGS = 6; |
| 648 | const char* strings[MAX_STRINGS]; |
| 649 | int lengths[MAX_STRINGS]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 650 | int stringCnt = 0; |
| 651 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 652 | if (segments.fHeader.size()) { |
| 653 | strings[stringCnt] = segments.fHeader.c_str(); |
| 654 | lengths[stringCnt] = segments.fHeader.size(); |
| 655 | ++stringCnt; |
| 656 | } |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 657 | if (segments.fVSUnis.size()) { |
| 658 | strings[stringCnt] = segments.fVSUnis.c_str(); |
| 659 | lengths[stringCnt] = segments.fVSUnis.size(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 660 | ++stringCnt; |
| 661 | } |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 662 | if (segments.fVSAttrs.size()) { |
| 663 | strings[stringCnt] = segments.fVSAttrs.c_str(); |
| 664 | lengths[stringCnt] = segments.fVSAttrs.size(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 665 | ++stringCnt; |
| 666 | } |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 667 | if (segments.fVaryings.size()) { |
| 668 | strings[stringCnt] = segments.fVaryings.c_str(); |
| 669 | lengths[stringCnt] = segments.fVaryings.size(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 670 | ++stringCnt; |
| 671 | } |
| 672 | |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 673 | GrAssert(segments.fVSCode.size()); |
| 674 | strings[stringCnt] = segments.fVSCode.c_str(); |
| 675 | lengths[stringCnt] = segments.fVSCode.size(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 676 | ++stringCnt; |
| 677 | |
| 678 | #if PRINT_SHADERS |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 679 | GrPrintf(segments.fHeader.c_str()); |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 680 | GrPrintf(segments.fVSUnis.c_str()); |
| 681 | GrPrintf(segments.fVSAttrs.c_str()); |
| 682 | GrPrintf(segments.fVaryings.c_str()); |
| 683 | GrPrintf(segments.fVSCode.c_str()); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 684 | GrPrintf("\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 685 | #endif |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 686 | GrAssert(stringCnt <= MAX_STRINGS); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 687 | programData->fVShaderID = CompileShader(GR_GL_VERTEX_SHADER, |
| 688 | stringCnt, |
| 689 | strings, |
| 690 | lengths); |
| 691 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 692 | if (!programData->fVShaderID) { |
| 693 | return false; |
| 694 | } |
| 695 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 696 | stringCnt = 0; |
| 697 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 698 | if (segments.fHeader.size()) { |
| 699 | strings[stringCnt] = segments.fHeader.c_str(); |
| 700 | lengths[stringCnt] = segments.fHeader.size(); |
| 701 | ++stringCnt; |
| 702 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 703 | if (strlen(GrShaderPrecision()) > 1) { |
| 704 | strings[stringCnt] = GrShaderPrecision(); |
| 705 | lengths[stringCnt] = strlen(GrShaderPrecision()); |
| 706 | ++stringCnt; |
| 707 | } |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 708 | if (segments.fFSUnis.size()) { |
| 709 | strings[stringCnt] = segments.fFSUnis.c_str(); |
| 710 | lengths[stringCnt] = segments.fFSUnis.size(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 711 | ++stringCnt; |
| 712 | } |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 713 | if (segments.fVaryings.size()) { |
| 714 | strings[stringCnt] = segments.fVaryings.c_str(); |
| 715 | lengths[stringCnt] = segments.fVaryings.size(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 716 | ++stringCnt; |
| 717 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 718 | if (segments.fFSOutputs.size()) { |
| 719 | strings[stringCnt] = segments.fFSOutputs.c_str(); |
| 720 | lengths[stringCnt] = segments.fFSOutputs.size(); |
| 721 | ++stringCnt; |
| 722 | } |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 723 | if (segments.fFSFunctions.size()) { |
| 724 | strings[stringCnt] = segments.fFSFunctions.c_str(); |
| 725 | lengths[stringCnt] = segments.fFSFunctions.size(); |
| 726 | ++stringCnt; |
| 727 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 728 | |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 729 | GrAssert(segments.fFSCode.size()); |
| 730 | strings[stringCnt] = segments.fFSCode.c_str(); |
| 731 | lengths[stringCnt] = segments.fFSCode.size(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 732 | ++stringCnt; |
| 733 | |
| 734 | #if PRINT_SHADERS |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 735 | GrPrintf(segments.fHeader.c_str()); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 736 | GrPrintf(GrShaderPrecision()); |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 737 | GrPrintf(segments.fFSUnis.c_str()); |
| 738 | GrPrintf(segments.fVaryings.c_str()); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 739 | GrPrintf(segments.fFSOutputs.c_str()); |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 740 | GrPrintf(segments.fFSFunctions.c_str()); |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 741 | GrPrintf(segments.fFSCode.c_str()); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 742 | GrPrintf("\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 743 | #endif |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 744 | GrAssert(stringCnt <= MAX_STRINGS); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 745 | programData->fFShaderID = CompileShader(GR_GL_FRAGMENT_SHADER, |
| 746 | stringCnt, |
| 747 | strings, |
| 748 | lengths); |
| 749 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 750 | if (!programData->fFShaderID) { |
| 751 | return false; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 752 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 753 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 754 | return true; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | GrGLuint GrGLProgram::CompileShader(GrGLenum type, |
| 758 | int stringCnt, |
| 759 | const char** strings, |
| 760 | int* stringLengths) { |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 761 | SK_TRACE_EVENT1("GrGLProgram::CompileShader", |
| 762 | "stringCount", SkStringPrintf("%i", stringCnt).c_str()); |
| 763 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 764 | GrGLuint shader = GR_GL(CreateShader(type)); |
| 765 | if (0 == shader) { |
| 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | GrGLint compiled = GR_GL_INIT_ZERO; |
| 770 | GR_GL(ShaderSource(shader, stringCnt, strings, stringLengths)); |
| 771 | GR_GL(CompileShader(shader)); |
| 772 | GR_GL(GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled)); |
| 773 | |
| 774 | if (!compiled) { |
| 775 | GrGLint infoLen = GR_GL_INIT_ZERO; |
| 776 | GR_GL(GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 777 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 778 | if (infoLen > 0) { |
| 779 | GR_GL(GetShaderInfoLog(shader, infoLen+1, NULL, (char*)log.get())); |
| 780 | for (int i = 0; i < stringCnt; ++i) { |
| 781 | if (NULL == stringLengths || stringLengths[i] < 0) { |
| 782 | GrPrintf(strings[i]); |
| 783 | } else { |
| 784 | GrPrintf("%.*s", stringLengths[i], strings[i]); |
| 785 | } |
| 786 | } |
| 787 | GrPrintf("\n%s", log.get()); |
| 788 | } |
| 789 | GrAssert(!"Shader compilation failed!"); |
| 790 | GR_GL(DeleteShader(shader)); |
| 791 | return 0; |
| 792 | } |
| 793 | return shader; |
| 794 | } |
| 795 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 796 | bool GrGLProgram::bindOutputsAttribsAndLinkProgram( |
| 797 | GrStringBuilder texCoordAttrNames[], |
| 798 | bool bindColorOut, |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 799 | bool bindDualSrcOut, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 800 | CachedData* programData) const { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 801 | programData->fProgramID = GR_GL(CreateProgram()); |
| 802 | if (!programData->fProgramID) { |
| 803 | return false; |
| 804 | } |
| 805 | const GrGLint& progID = programData->fProgramID; |
| 806 | |
| 807 | GR_GL(AttachShader(progID, programData->fVShaderID)); |
| 808 | GR_GL(AttachShader(progID, programData->fFShaderID)); |
| 809 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 810 | if (bindColorOut) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 811 | GR_GL(BindFragDataLocationIndexed(programData->fProgramID, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 812 | 0, 0, declared_color_output_name())); |
| 813 | } |
| 814 | if (bindDualSrcOut) { |
| 815 | GR_GL(BindFragDataLocationIndexed(programData->fProgramID, |
| 816 | 0, 1, dual_source_output_name())); |
| 817 | } |
| 818 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 819 | // Bind the attrib locations to same values for all shaders |
| 820 | GR_GL(BindAttribLocation(progID, PositionAttributeIdx(), POS_ATTR_NAME)); |
| 821 | for (int t = 0; t < GrDrawTarget::kMaxTexCoords; ++t) { |
| 822 | if (texCoordAttrNames[t].size()) { |
| 823 | GR_GL(BindAttribLocation(progID, |
| 824 | TexCoordAttributeIdx(t), |
| 825 | texCoordAttrNames[t].c_str())); |
| 826 | } |
| 827 | } |
| 828 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 829 | if (kSetAsAttribute == programData->fUniLocations.fViewMatrixUni) { |
| 830 | GR_GL(BindAttribLocation(progID, |
| 831 | ViewMatrixAttributeIdx(), |
| 832 | VIEW_MATRIX_NAME)); |
| 833 | } |
| 834 | |
| 835 | for (int s = 0; s < GrDrawTarget::kNumStages; ++s) { |
bsalomon@google.com | 72b4fcb | 2011-05-09 20:47:34 +0000 | [diff] [blame] | 836 | const StageUniLocations& unis = programData->fUniLocations.fStages[s]; |
| 837 | if (kSetAsAttribute == unis.fTextureMatrixUni) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 838 | GrStringBuilder matName; |
| 839 | tex_matrix_name(s, &matName); |
| 840 | GR_GL(BindAttribLocation(progID, |
| 841 | TextureMatrixAttributeIdx(s), |
| 842 | matName.c_str())); |
| 843 | } |
| 844 | } |
| 845 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 846 | GR_GL(BindAttribLocation(progID, ColorAttributeIdx(), COL_ATTR_NAME)); |
| 847 | |
| 848 | GR_GL(LinkProgram(progID)); |
| 849 | |
| 850 | GrGLint linked = GR_GL_INIT_ZERO; |
| 851 | GR_GL(GetProgramiv(progID, GR_GL_LINK_STATUS, &linked)); |
| 852 | if (!linked) { |
| 853 | GrGLint infoLen = GR_GL_INIT_ZERO; |
| 854 | GR_GL(GetProgramiv(progID, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 855 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 856 | if (infoLen > 0) { |
| 857 | GR_GL(GetProgramInfoLog(progID, |
| 858 | infoLen+1, |
| 859 | NULL, |
| 860 | (char*)log.get())); |
| 861 | GrPrintf((char*)log.get()); |
| 862 | } |
| 863 | GrAssert(!"Error linking program"); |
| 864 | GR_GL(DeleteProgram(progID)); |
| 865 | programData->fProgramID = 0; |
| 866 | return false; |
| 867 | } |
| 868 | return true; |
| 869 | } |
| 870 | |
| 871 | void GrGLProgram::getUniformLocationsAndInitCache(CachedData* programData) const { |
| 872 | const GrGLint& progID = programData->fProgramID; |
| 873 | |
| 874 | if (kUseUniform == programData->fUniLocations.fViewMatrixUni) { |
| 875 | programData->fUniLocations.fViewMatrixUni = |
| 876 | GR_GL(GetUniformLocation(progID, VIEW_MATRIX_NAME)); |
| 877 | GrAssert(kUnusedUniform != programData->fUniLocations.fViewMatrixUni); |
| 878 | } |
| 879 | if (kUseUniform == programData->fUniLocations.fColorUni) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 880 | programData->fUniLocations.fColorUni = |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 881 | GR_GL(GetUniformLocation(progID, COL_UNI_NAME)); |
| 882 | GrAssert(kUnusedUniform != programData->fUniLocations.fColorUni); |
| 883 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 884 | if (kUseUniform == programData->fUniLocations.fColorFilterUni) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 885 | programData->fUniLocations.fColorFilterUni = |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 886 | GR_GL(GetUniformLocation(progID, COL_FILTER_UNI_NAME)); |
| 887 | GrAssert(kUnusedUniform != programData->fUniLocations.fColorFilterUni); |
| 888 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 889 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 890 | if (kUseUniform == programData->fUniLocations.fEdgesUni) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 891 | programData->fUniLocations.fEdgesUni = |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 892 | GR_GL(GetUniformLocation(progID, EDGES_UNI_NAME)); |
| 893 | GrAssert(kUnusedUniform != programData->fUniLocations.fEdgesUni); |
| 894 | } else { |
| 895 | programData->fUniLocations.fEdgesUni = kUnusedUniform; |
| 896 | } |
| 897 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 898 | for (int s = 0; s < GrDrawTarget::kNumStages; ++s) { |
| 899 | StageUniLocations& locations = programData->fUniLocations.fStages[s]; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 900 | if (fProgramDesc.fStages[s].isEnabled()) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 901 | if (kUseUniform == locations.fTextureMatrixUni) { |
| 902 | GrStringBuilder texMName; |
| 903 | tex_matrix_name(s, &texMName); |
| 904 | locations.fTextureMatrixUni = GR_GL(GetUniformLocation( |
| 905 | progID, |
| 906 | texMName.c_str())); |
| 907 | GrAssert(kUnusedUniform != locations.fTextureMatrixUni); |
| 908 | } |
| 909 | |
| 910 | if (kUseUniform == locations.fSamplerUni) { |
| 911 | GrStringBuilder samplerName; |
| 912 | sampler_name(s, &samplerName); |
| 913 | locations.fSamplerUni = GR_GL(GetUniformLocation( |
| 914 | progID, |
| 915 | samplerName.c_str())); |
| 916 | GrAssert(kUnusedUniform != locations.fSamplerUni); |
| 917 | } |
| 918 | |
| 919 | if (kUseUniform == locations.fNormalizedTexelSizeUni) { |
| 920 | GrStringBuilder texelSizeName; |
| 921 | normalized_texel_size_name(s, &texelSizeName); |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 922 | locations.fNormalizedTexelSizeUni = |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 923 | GR_GL(GetUniformLocation(progID, texelSizeName.c_str())); |
| 924 | GrAssert(kUnusedUniform != locations.fNormalizedTexelSizeUni); |
| 925 | } |
| 926 | |
| 927 | if (kUseUniform == locations.fRadial2Uni) { |
| 928 | GrStringBuilder radial2ParamName; |
| 929 | radial2_param_name(s, &radial2ParamName); |
| 930 | locations.fRadial2Uni = GR_GL(GetUniformLocation( |
| 931 | progID, |
| 932 | radial2ParamName.c_str())); |
| 933 | GrAssert(kUnusedUniform != locations.fRadial2Uni); |
| 934 | } |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 935 | |
| 936 | if (kUseUniform == locations.fTexDomUni) { |
| 937 | GrStringBuilder texDomName; |
| 938 | tex_domain_name(s, &texDomName); |
| 939 | locations.fTexDomUni = GR_GL(GetUniformLocation( |
| 940 | progID, |
| 941 | texDomName.c_str())); |
| 942 | GrAssert(kUnusedUniform != locations.fTexDomUni); |
| 943 | } |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 944 | |
| 945 | GrStringBuilder kernelName, imageIncrementName; |
| 946 | convolve_param_names(s, &kernelName, &imageIncrementName); |
| 947 | if (kUseUniform == locations.fKernelUni) { |
| 948 | locations.fKernelUni = GR_GL(GetUniformLocation( |
| 949 | progID, |
| 950 | kernelName.c_str())); |
| 951 | GrAssert(kUnusedUniform != locations.fKernelUni); |
| 952 | } |
| 953 | |
| 954 | if (kUseUniform == locations.fImageIncrementUni) { |
| 955 | locations.fImageIncrementUni = GR_GL(GetUniformLocation( |
| 956 | progID, |
| 957 | imageIncrementName.c_str())); |
| 958 | GrAssert(kUnusedUniform != locations.fImageIncrementUni); |
| 959 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | GR_GL(UseProgram(progID)); |
| 963 | |
| 964 | // init sampler unis and set bogus values for state tracking |
| 965 | for (int s = 0; s < GrDrawTarget::kNumStages; ++s) { |
| 966 | if (kUnusedUniform != programData->fUniLocations.fStages[s].fSamplerUni) { |
| 967 | GR_GL(Uniform1i(programData->fUniLocations.fStages[s].fSamplerUni, s)); |
| 968 | } |
| 969 | programData->fTextureMatrices[s] = GrMatrix::InvalidMatrix(); |
| 970 | programData->fRadial2CenterX1[s] = GR_ScalarMax; |
| 971 | programData->fRadial2Radius0[s] = -GR_ScalarMax; |
| 972 | programData->fTextureWidth[s] = -1; |
| 973 | programData->fTextureHeight[s] = -1; |
| 974 | } |
| 975 | programData->fViewMatrix = GrMatrix::InvalidMatrix(); |
| 976 | programData->fColor = GrColor_ILLEGAL; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 977 | programData->fColorFilterColor = GrColor_ILLEGAL; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 978 | } |
| 979 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 980 | //============================================================================ |
| 981 | // Stage code generation |
| 982 | //============================================================================ |
| 983 | |
| 984 | void GrGLProgram::genStageCode(int stageNum, |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 985 | const GrGLProgram::StageDesc& desc, |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 986 | const char* fsInColor, // NULL means no incoming color |
| 987 | const char* fsOutColor, |
| 988 | const char* vsInCoord, |
| 989 | ShaderCodeSegments* segments, |
| 990 | StageUniLocations* locations) const { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 991 | |
| 992 | GrAssert(stageNum >= 0 && stageNum <= 9); |
| 993 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 994 | GrStringBuilder varyingName; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 995 | stage_varying_name(stageNum, &varyingName); |
| 996 | |
| 997 | // First decide how many coords are needed to access the texture |
| 998 | // Right now it's always 2 but we could start using 1D textures for |
| 999 | // gradients. |
| 1000 | static const int coordDims = 2; |
| 1001 | int varyingDims; |
| 1002 | /// Vertex Shader Stuff |
| 1003 | |
| 1004 | // decide whether we need a matrix to transform texture coords |
| 1005 | // and whether the varying needs a perspective coord. |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1006 | GrStringBuilder texMName; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1007 | tex_matrix_name(stageNum, &texMName); |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1008 | if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1009 | varyingDims = coordDims; |
| 1010 | } else { |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 1011 | #if GR_GL_ATTRIBUTE_MATRICES |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1012 | segments->fVSAttrs.appendf("attribute mat3 %s;\n", texMName.c_str()); |
| 1013 | locations->fTextureMatrixUni = kSetAsAttribute; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1014 | #else |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1015 | segments->fVSUnis.appendf("uniform mat3 %s;\n", texMName.c_str()); |
| 1016 | locations->fTextureMatrixUni = kUseUniform; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1017 | #endif |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1018 | if (desc.fOptFlags & StageDesc::kNoPerspective_OptFlagBit) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1019 | varyingDims = coordDims; |
| 1020 | } else { |
| 1021 | varyingDims = coordDims + 1; |
| 1022 | } |
| 1023 | } |
| 1024 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1025 | GrStringBuilder samplerName; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1026 | sampler_name(stageNum, &samplerName); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1027 | segments->fFSUnis.appendf("uniform sampler2D %s;\n", samplerName.c_str()); |
| 1028 | locations->fSamplerUni = kUseUniform; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1029 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1030 | GrStringBuilder texelSizeName; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1031 | if (StageDesc::k2x2_FetchMode == desc.fFetchMode) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1032 | normalized_texel_size_name(stageNum, &texelSizeName); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1033 | segments->fFSUnis.appendf("uniform vec2 %s;\n", texelSizeName.c_str()); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 1036 | segments->fVaryings.appendf("varying %s %s;\n", |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1037 | float_vector_type(varyingDims), varyingName.c_str()); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1038 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1039 | if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1040 | GrAssert(varyingDims == coordDims); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1041 | segments->fVSCode.appendf("\t%s = %s;\n", varyingName.c_str(), vsInCoord); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1042 | } else { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1043 | // varying = texMatrix * texCoord |
| 1044 | segments->fVSCode.appendf("\t%s = (%s * vec3(%s, 1))%s;\n", |
| 1045 | varyingName.c_str(), texMName.c_str(), |
| 1046 | vsInCoord, vector_all_coords(varyingDims)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1049 | GrStringBuilder radial2ParamsName; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1050 | radial2_param_name(stageNum, &radial2ParamsName); |
| 1051 | // for radial grads without perspective we can pass the linear |
| 1052 | // part of the quadratic as a varying. |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1053 | GrStringBuilder radial2VaryingName; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1054 | radial2_varying_name(stageNum, &radial2VaryingName); |
| 1055 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1056 | if (StageDesc::kRadial2Gradient_CoordMapping == desc.fCoordMapping || |
| 1057 | StageDesc::kRadial2GradientDegenerate_CoordMapping == desc.fCoordMapping) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1058 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 1059 | segments->fVSUnis.appendf("uniform %s float %s[6];\n", |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1060 | GrPrecision(), radial2ParamsName.c_str()); |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 1061 | segments->fFSUnis.appendf("uniform float %s[6];\n", |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1062 | radial2ParamsName.c_str()); |
| 1063 | locations->fRadial2Uni = kUseUniform; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1064 | |
| 1065 | // if there is perspective we don't interpolate this |
| 1066 | if (varyingDims == coordDims) { |
| 1067 | GrAssert(2 == coordDims); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1068 | segments->fVaryings.appendf("varying float %s;\n", radial2VaryingName.c_str()); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1069 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1070 | // r2Var = 2 * (r2Parm[2] * varCoord.x - r2Param[3]) |
| 1071 | segments->fVSCode.appendf("\t%s = 2.0 *(%s[2] * %s.x - %s[3]);\n", |
| 1072 | radial2VaryingName.c_str(), radial2ParamsName.c_str(), |
| 1073 | varyingName.c_str(), radial2ParamsName.c_str()); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 1077 | GrStringBuilder kernelName, kernelWidthName, imageIncrementName; |
| 1078 | convolve_param_names(stageNum, &kernelName, &imageIncrementName); |
| 1079 | |
| 1080 | if (ProgramDesc::StageDesc::kConvolution_FetchMode == desc.fFetchMode) { |
| 1081 | segments->fFSUnis.appendf("uniform float %s[%d];\n", |
| 1082 | kernelName.c_str(), desc.fKernelWidth); |
| 1083 | segments->fFSUnis.appendf("uniform vec2 %s;\n", |
| 1084 | imageIncrementName.c_str()); |
bsalomon@google.com | edc177d | 2011-08-05 15:46:40 +0000 | [diff] [blame] | 1085 | segments->fVSUnis.appendf("uniform %s vec2 %s;\n", |
| 1086 | GrPrecision(), |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 1087 | imageIncrementName.c_str()); |
| 1088 | locations->fKernelUni = kUseUniform; |
| 1089 | locations->fImageIncrementUni = kUseUniform; |
| 1090 | float scale = (desc.fKernelWidth - 1) * 0.5f; |
| 1091 | segments->fVSCode.appendf("\t%s -= vec2(%g, %g) * %s;\n", |
| 1092 | varyingName.c_str(), scale, scale, |
| 1093 | imageIncrementName.c_str()); |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame^] | 1094 | } |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 1095 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1096 | /// Fragment Shader Stuff |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1097 | GrStringBuilder fsCoordName; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1098 | // function used to access the shader, may be made projective |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1099 | GrStringBuilder texFunc("texture2D"); |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1100 | if (desc.fOptFlags & (StageDesc::kIdentityMatrix_OptFlagBit | |
| 1101 | StageDesc::kNoPerspective_OptFlagBit)) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1102 | GrAssert(varyingDims == coordDims); |
| 1103 | fsCoordName = varyingName; |
| 1104 | } else { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1105 | // if we have to do some special op on the varyings to get |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1106 | // our final tex coords then when in perspective we have to |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1107 | // do an explicit divide. Otherwise, we can use a Proj func. |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1108 | if (StageDesc::kIdentity_CoordMapping == desc.fCoordMapping && |
| 1109 | StageDesc::kSingle_FetchMode == desc.fFetchMode) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1110 | texFunc.append("Proj"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1111 | fsCoordName = varyingName; |
| 1112 | } else { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1113 | fsCoordName = "inCoord"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 1114 | fsCoordName.appendS32(stageNum); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1115 | segments->fFSCode.appendf("\t%s %s = %s%s / %s%s;\n", |
| 1116 | float_vector_type(coordDims), |
| 1117 | fsCoordName.c_str(), |
| 1118 | varyingName.c_str(), |
| 1119 | vector_nonhomog_coords(varyingDims), |
| 1120 | varyingName.c_str(), |
| 1121 | vector_homog_coord(varyingDims)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 1125 | GrStringBuilder sampleCoords; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1126 | bool complexCoord = false; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1127 | switch (desc.fCoordMapping) { |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1128 | case StageDesc::kIdentity_CoordMapping: |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1129 | sampleCoords = fsCoordName; |
| 1130 | break; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1131 | case StageDesc::kSweepGradient_CoordMapping: |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1132 | sampleCoords.printf("vec2(atan(- %s.y, - %s.x) * 0.1591549430918 + 0.5, 0.5)", fsCoordName.c_str(), fsCoordName.c_str()); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1133 | complexCoord = true; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1134 | break; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1135 | case StageDesc::kRadialGradient_CoordMapping: |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1136 | sampleCoords.printf("vec2(length(%s.xy), 0.5)", fsCoordName.c_str()); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1137 | complexCoord = true; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1138 | break; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1139 | case StageDesc::kRadial2Gradient_CoordMapping: { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1140 | GrStringBuilder cName("c"); |
| 1141 | GrStringBuilder ac4Name("ac4"); |
| 1142 | GrStringBuilder rootName("root"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1143 | |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 1144 | cName.appendS32(stageNum); |
| 1145 | ac4Name.appendS32(stageNum); |
| 1146 | rootName.appendS32(stageNum); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1147 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1148 | // if we were able to interpolate the linear component bVar is the varying |
| 1149 | // otherwise compute it |
| 1150 | GrStringBuilder bVar; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1151 | if (coordDims == varyingDims) { |
| 1152 | bVar = radial2VaryingName; |
| 1153 | GrAssert(2 == varyingDims); |
| 1154 | } else { |
| 1155 | GrAssert(3 == varyingDims); |
| 1156 | bVar = "b"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 1157 | bVar.appendS32(stageNum); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1158 | segments->fFSCode.appendf("\tfloat %s = 2.0 * (%s[2] * %s.x - %s[3]);\n", |
| 1159 | bVar.c_str(), radial2ParamsName.c_str(), |
| 1160 | fsCoordName.c_str(), radial2ParamsName.c_str()); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1163 | // c = (x^2)+(y^2) - params[4] |
| 1164 | segments->fFSCode.appendf("\tfloat %s = dot(%s, %s) - %s[4];\n", |
| 1165 | cName.c_str(), fsCoordName.c_str(), |
| 1166 | fsCoordName.c_str(), |
| 1167 | radial2ParamsName.c_str()); |
| 1168 | // ac4 = 4.0 * params[0] * c |
| 1169 | segments->fFSCode.appendf("\tfloat %s = %s[0] * 4.0 * %s;\n", |
| 1170 | ac4Name.c_str(), radial2ParamsName.c_str(), |
| 1171 | cName.c_str()); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1172 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1173 | // root = sqrt(b^2-4ac) |
| 1174 | // (abs to avoid exception due to fp precision) |
| 1175 | segments->fFSCode.appendf("\tfloat %s = sqrt(abs(%s*%s - %s));\n", |
| 1176 | rootName.c_str(), bVar.c_str(), bVar.c_str(), |
| 1177 | ac4Name.c_str()); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1178 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1179 | // x coord is: (-b + params[5] * sqrt(b^2-4ac)) * params[1] |
| 1180 | // y coord is 0.5 (texture is effectively 1D) |
| 1181 | sampleCoords.printf("vec2((-%s + %s[5] * %s) * %s[1], 0.5)", |
| 1182 | bVar.c_str(), radial2ParamsName.c_str(), |
| 1183 | rootName.c_str(), radial2ParamsName.c_str()); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1184 | complexCoord = true; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1185 | break;} |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1186 | case StageDesc::kRadial2GradientDegenerate_CoordMapping: { |
| 1187 | GrStringBuilder cName("c"); |
| 1188 | |
| 1189 | cName.appendS32(stageNum); |
| 1190 | |
| 1191 | // if we were able to interpolate the linear component bVar is the varying |
| 1192 | // otherwise compute it |
| 1193 | GrStringBuilder bVar; |
| 1194 | if (coordDims == varyingDims) { |
| 1195 | bVar = radial2VaryingName; |
| 1196 | GrAssert(2 == varyingDims); |
| 1197 | } else { |
| 1198 | GrAssert(3 == varyingDims); |
| 1199 | bVar = "b"; |
| 1200 | bVar.appendS32(stageNum); |
| 1201 | segments->fFSCode.appendf("\tfloat %s = 2.0 * (%s[2] * %s.x - %s[3]);\n", |
| 1202 | bVar.c_str(), radial2ParamsName.c_str(), |
| 1203 | fsCoordName.c_str(), radial2ParamsName.c_str()); |
| 1204 | } |
| 1205 | |
| 1206 | // c = (x^2)+(y^2) - params[4] |
| 1207 | segments->fFSCode.appendf("\tfloat %s = dot(%s, %s) - %s[4];\n", |
| 1208 | cName.c_str(), fsCoordName.c_str(), |
| 1209 | fsCoordName.c_str(), |
| 1210 | radial2ParamsName.c_str()); |
| 1211 | |
| 1212 | // x coord is: -c/b |
| 1213 | // y coord is 0.5 (texture is effectively 1D) |
| 1214 | sampleCoords.printf("vec2((-%s / %s), 0.5)", cName.c_str(), bVar.c_str()); |
| 1215 | complexCoord = true; |
| 1216 | break;} |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1217 | }; |
| 1218 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1219 | const char* smear; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1220 | if (desc.fModulation == StageDesc::kAlpha_Modulation) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1221 | smear = ".aaaa"; |
| 1222 | } else { |
| 1223 | smear = ""; |
| 1224 | } |
| 1225 | GrStringBuilder modulate; |
| 1226 | if (NULL != fsInColor) { |
| 1227 | modulate.printf(" * %s", fsInColor); |
| 1228 | } |
| 1229 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 1230 | if (desc.fOptFlags & |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1231 | StageDesc::kCustomTextureDomain_OptFlagBit) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1232 | GrStringBuilder texDomainName; |
| 1233 | tex_domain_name(stageNum, &texDomainName); |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 1234 | segments->fFSUnis.appendf("uniform %s %s;\n", |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1235 | float_vector_type(4), |
| 1236 | texDomainName.c_str()); |
| 1237 | GrStringBuilder coordVar("clampCoord"); |
| 1238 | segments->fFSCode.appendf("\t%s %s = clamp(%s, %s.xy, %s.zw);\n", |
| 1239 | float_vector_type(coordDims), |
| 1240 | coordVar.c_str(), |
| 1241 | sampleCoords.c_str(), |
| 1242 | texDomainName.c_str(), |
| 1243 | texDomainName.c_str()); |
| 1244 | sampleCoords = coordVar; |
| 1245 | locations->fTexDomUni = kUseUniform; |
| 1246 | } |
| 1247 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1248 | if (StageDesc::k2x2_FetchMode == desc.fFetchMode) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1249 | locations->fNormalizedTexelSizeUni = kUseUniform; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1250 | if (complexCoord) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1251 | // assign the coord to a var rather than compute 4x. |
| 1252 | GrStringBuilder coordVar("tCoord"); |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 1253 | coordVar.appendS32(stageNum); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1254 | segments->fFSCode.appendf("\t%s %s = %s;\n", |
| 1255 | float_vector_type(coordDims), |
| 1256 | coordVar.c_str(), sampleCoords.c_str()); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1257 | sampleCoords = coordVar; |
| 1258 | } |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1259 | GrAssert(2 == coordDims); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1260 | GrStringBuilder accumVar("accum"); |
| 1261 | accumVar.appendS32(stageNum); |
| 1262 | segments->fFSCode.appendf("\tvec4 %s = %s(%s, %s + vec2(-%s.x,-%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName.c_str(), sampleCoords.c_str(), texelSizeName.c_str(), texelSizeName.c_str(), smear); |
| 1263 | segments->fFSCode.appendf("\t%s += %s(%s, %s + vec2(+%s.x,-%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName.c_str(), sampleCoords.c_str(), texelSizeName.c_str(), texelSizeName.c_str(), smear); |
| 1264 | segments->fFSCode.appendf("\t%s += %s(%s, %s + vec2(-%s.x,+%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName.c_str(), sampleCoords.c_str(), texelSizeName.c_str(), texelSizeName.c_str(), smear); |
| 1265 | segments->fFSCode.appendf("\t%s += %s(%s, %s + vec2(+%s.x,+%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName.c_str(), sampleCoords.c_str(), texelSizeName.c_str(), texelSizeName.c_str(), smear); |
| 1266 | segments->fFSCode.appendf("\t%s = .25 * %s%s;\n", fsOutColor, accumVar.c_str(), modulate.c_str()); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 1267 | } else if (ProgramDesc::StageDesc::kConvolution_FetchMode == desc.fFetchMode) { |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame^] | 1268 | GrStringBuilder sumVar("sum"); |
| 1269 | sumVar.appendS32(stageNum); |
| 1270 | GrStringBuilder coordVar("coord"); |
| 1271 | coordVar.appendS32(stageNum); |
| 1272 | |
| 1273 | segments->fFSCode.appendf("\tvec4 %s = vec4(0, 0, 0, 0);\n", |
| 1274 | sumVar.c_str()); |
| 1275 | segments->fFSCode.appendf("\tvec2 %s = %s;\n", |
| 1276 | coordVar.c_str(), |
| 1277 | sampleCoords.c_str()); |
| 1278 | segments->fFSCode.appendf("\tfor (int i = 0; i < %d; i++) {\n", |
| 1279 | desc.fKernelWidth); |
| 1280 | segments->fFSCode.appendf("\t\t%s += %s(%s, %s)%s * %s[i];\n", |
| 1281 | sumVar.c_str(), texFunc.c_str(), |
| 1282 | samplerName.c_str(), coordVar.c_str(), smear, |
| 1283 | kernelName.c_str()); |
| 1284 | segments->fFSCode.appendf("\t\t%s += %s;\n", |
| 1285 | coordVar.c_str(), |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 1286 | imageIncrementName.c_str()); |
| 1287 | segments->fFSCode.appendf("\t}\n"); |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame^] | 1288 | segments->fFSCode.appendf("\t%s = %s%s;\n", fsOutColor, |
| 1289 | sumVar.c_str(), modulate.c_str()); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1290 | } else { |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame^] | 1291 | segments->fFSCode.appendf("\t%s = %s(%s, %s)%s%s;\n", |
| 1292 | fsOutColor, texFunc.c_str(), |
| 1293 | samplerName.c_str(), sampleCoords.c_str(), |
| 1294 | smear, modulate.c_str()); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1295 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1296 | } |