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