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