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