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