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