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" |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 41 | #define COL_FILTER_UNI_NAME "uColorFilter" |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 42 | #define COL_MATRIX_UNI_NAME "uColorMatrix" |
| 43 | #define COL_MATRIX_VEC_UNI_NAME "uColorMatrixVec" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 44 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 45 | namespace { |
| 46 | inline void tex_attr_name(int coordIdx, GrStringBuilder* s) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 47 | *s = "aTexCoord"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 48 | s->appendS32(coordIdx); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 49 | } |
| 50 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 51 | inline const char* float_vector_type_str(int count) { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 52 | return GrGLShaderVar::TypeString(GrSLFloatVectorType(count)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 53 | } |
| 54 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 55 | inline const char* vector_all_coords(int count) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 56 | static const char* ALL[] = {"ERROR", "", ".xy", ".xyz", ".xyzw"}; |
| 57 | GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ALL)); |
| 58 | return ALL[count]; |
| 59 | } |
| 60 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 61 | inline const char* all_ones_vec(int count) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 62 | static const char* ONESVEC[] = {"ERROR", "1.0", "vec2(1,1)", |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 63 | "vec3(1,1,1)", "vec4(1,1,1,1)"}; |
| 64 | GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ONESVEC)); |
| 65 | return ONESVEC[count]; |
| 66 | } |
| 67 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 68 | inline const char* all_zeros_vec(int count) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 69 | static const char* ZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)", |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 70 | "vec3(0,0,0)", "vec4(0,0,0,0)"}; |
| 71 | GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ZEROSVEC)); |
| 72 | return ZEROSVEC[count]; |
| 73 | } |
| 74 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 75 | inline const char* declared_color_output_name() { return "fsColorOut"; } |
| 76 | inline const char* dual_source_output_name() { return "dualSourceOut"; } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 77 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 78 | inline void tex_matrix_name(int stage, GrStringBuilder* s) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 79 | *s = "uTexM"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 80 | s->appendS32(stage); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 81 | } |
| 82 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 83 | inline void normalized_texel_size_name(int stage, GrStringBuilder* s) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 84 | *s = "uTexelSize"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 85 | s->appendS32(stage); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 86 | } |
| 87 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 88 | inline void sampler_name(int stage, GrStringBuilder* s) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 89 | *s = "uSampler"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 90 | s->appendS32(stage); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 91 | } |
| 92 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 93 | inline void radial2_param_name(int stage, GrStringBuilder* s) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 94 | *s = "uRadial2Params"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 95 | s->appendS32(stage); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 96 | } |
| 97 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 98 | inline void tex_domain_name(int stage, GrStringBuilder* s) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 99 | *s = "uTexDom"; |
| 100 | s->appendS32(stage); |
| 101 | } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 102 | } |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 103 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 104 | GrGLProgram::GrGLProgram() { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | GrGLProgram::~GrGLProgram() { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 108 | } |
| 109 | |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 110 | void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 111 | GrBlendCoeff* dstCoeff) const { |
| 112 | switch (fProgramDesc.fDualSrcOutput) { |
| 113 | case ProgramDesc::kNone_DualSrcOutput: |
| 114 | break; |
| 115 | // the prog will write a coverage value to the secondary |
| 116 | // output and the dst is blended by one minus that value. |
| 117 | case ProgramDesc::kCoverage_DualSrcOutput: |
| 118 | case ProgramDesc::kCoverageISA_DualSrcOutput: |
| 119 | case ProgramDesc::kCoverageISC_DualSrcOutput: |
| 120 | *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_BlendCoeff; |
| 121 | break; |
| 122 | default: |
| 123 | GrCrash("Unexpected dual source blend output"); |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 128 | // assigns modulation of two vars to an output var |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 129 | // vars can be vec4s or floats (or one of each) |
| 130 | // result is always vec4 |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 131 | // if either var is "" then assign to the other var |
| 132 | // if both are "" then assign all ones |
| 133 | static inline void modulate_helper(const char* outputVar, |
| 134 | const char* var0, |
| 135 | const char* var1, |
| 136 | GrStringBuilder* code) { |
| 137 | GrAssert(NULL != outputVar); |
| 138 | GrAssert(NULL != var0); |
| 139 | GrAssert(NULL != var1); |
| 140 | GrAssert(NULL != code); |
| 141 | |
| 142 | bool has0 = '\0' != *var0; |
| 143 | bool has1 = '\0' != *var1; |
| 144 | |
| 145 | if (!has0 && !has1) { |
| 146 | code->appendf("\t%s = %s;\n", outputVar, all_ones_vec(4)); |
| 147 | } else if (!has0) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 148 | code->appendf("\t%s = vec4(%s);\n", outputVar, var1); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 149 | } else if (!has1) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 150 | code->appendf("\t%s = vec4(%s);\n", outputVar, var0); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 151 | } else { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 152 | code->appendf("\t%s = vec4(%s * %s);\n", outputVar, var0, var1); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | // assigns addition of two vars to an output var |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 157 | // vars can be vec4s or floats (or one of each) |
| 158 | // result is always vec4 |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 159 | // if either var is "" then assign to the other var |
| 160 | // if both are "" then assign all zeros |
| 161 | static inline void add_helper(const char* outputVar, |
| 162 | const char* var0, |
| 163 | const char* var1, |
| 164 | GrStringBuilder* code) { |
| 165 | GrAssert(NULL != outputVar); |
| 166 | GrAssert(NULL != var0); |
| 167 | GrAssert(NULL != var1); |
| 168 | GrAssert(NULL != code); |
| 169 | |
| 170 | bool has0 = '\0' != *var0; |
| 171 | bool has1 = '\0' != *var1; |
| 172 | |
| 173 | if (!has0 && !has1) { |
| 174 | code->appendf("\t%s = %s;\n", outputVar, all_zeros_vec(4)); |
| 175 | } else if (!has0) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 176 | code->appendf("\t%s = vec4(%s);\n", outputVar, var1); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 177 | } else if (!has1) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 178 | code->appendf("\t%s = vec4(%s);\n", outputVar, var0); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 179 | } else { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 180 | code->appendf("\t%s = vec4(%s + %s);\n", outputVar, var0, var1); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | // given two blend coeffecients determine whether the src |
| 185 | // and/or dst computation can be omitted. |
| 186 | static inline void needBlendInputs(SkXfermode::Coeff srcCoeff, |
| 187 | SkXfermode::Coeff dstCoeff, |
| 188 | bool* needSrcValue, |
| 189 | bool* needDstValue) { |
| 190 | if (SkXfermode::kZero_Coeff == srcCoeff) { |
| 191 | switch (dstCoeff) { |
| 192 | // these all read the src |
| 193 | case SkXfermode::kSC_Coeff: |
| 194 | case SkXfermode::kISC_Coeff: |
| 195 | case SkXfermode::kSA_Coeff: |
| 196 | case SkXfermode::kISA_Coeff: |
| 197 | *needSrcValue = true; |
| 198 | break; |
| 199 | default: |
| 200 | *needSrcValue = false; |
| 201 | break; |
| 202 | } |
| 203 | } else { |
| 204 | *needSrcValue = true; |
| 205 | } |
| 206 | if (SkXfermode::kZero_Coeff == dstCoeff) { |
| 207 | switch (srcCoeff) { |
| 208 | // these all read the dst |
| 209 | case SkXfermode::kDC_Coeff: |
| 210 | case SkXfermode::kIDC_Coeff: |
| 211 | case SkXfermode::kDA_Coeff: |
| 212 | case SkXfermode::kIDA_Coeff: |
| 213 | *needDstValue = true; |
| 214 | break; |
| 215 | default: |
| 216 | *needDstValue = false; |
| 217 | break; |
| 218 | } |
| 219 | } else { |
| 220 | *needDstValue = true; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 225 | * Create a blend_coeff * value string to be used in shader code. Sets empty |
| 226 | * string if result is trivially zero. |
| 227 | */ |
| 228 | static void blendTermString(GrStringBuilder* str, SkXfermode::Coeff coeff, |
| 229 | const char* src, const char* dst, |
| 230 | const char* value) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 231 | switch (coeff) { |
| 232 | case SkXfermode::kZero_Coeff: /** 0 */ |
| 233 | *str = ""; |
| 234 | break; |
| 235 | case SkXfermode::kOne_Coeff: /** 1 */ |
| 236 | *str = value; |
| 237 | break; |
| 238 | case SkXfermode::kSC_Coeff: |
| 239 | str->printf("(%s * %s)", src, value); |
| 240 | break; |
| 241 | case SkXfermode::kISC_Coeff: |
| 242 | str->printf("((%s - %s) * %s)", all_ones_vec(4), src, value); |
| 243 | break; |
| 244 | case SkXfermode::kDC_Coeff: |
| 245 | str->printf("(%s * %s)", dst, value); |
| 246 | break; |
| 247 | case SkXfermode::kIDC_Coeff: |
| 248 | str->printf("((%s - %s) * %s)", all_ones_vec(4), dst, value); |
| 249 | break; |
| 250 | case SkXfermode::kSA_Coeff: /** src alpha */ |
| 251 | str->printf("(%s.a * %s)", src, value); |
| 252 | break; |
| 253 | case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */ |
| 254 | str->printf("((1.0 - %s.a) * %s)", src, value); |
| 255 | break; |
| 256 | case SkXfermode::kDA_Coeff: /** dst alpha */ |
| 257 | str->printf("(%s.a * %s)", dst, value); |
| 258 | break; |
| 259 | case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */ |
| 260 | str->printf("((1.0 - %s.a) * %s)", dst, value); |
| 261 | break; |
| 262 | default: |
| 263 | GrCrash("Unexpected xfer coeff."); |
| 264 | break; |
| 265 | } |
| 266 | } |
| 267 | /** |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 268 | * Adds a line to the fragment shader code which modifies the color by |
| 269 | * the specified color filter. |
| 270 | */ |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 271 | static void addColorFilter(GrStringBuilder* fsCode, const char * outputVar, |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 272 | SkXfermode::Coeff uniformCoeff, |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 273 | SkXfermode::Coeff colorCoeff, |
| 274 | const char* inColor) { |
| 275 | GrStringBuilder colorStr, constStr; |
| 276 | blendTermString(&colorStr, colorCoeff, COL_FILTER_UNI_NAME, |
| 277 | inColor, inColor); |
| 278 | blendTermString(&constStr, uniformCoeff, COL_FILTER_UNI_NAME, |
| 279 | inColor, COL_FILTER_UNI_NAME); |
| 280 | |
| 281 | add_helper(outputVar, colorStr.c_str(), constStr.c_str(), fsCode); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 282 | } |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 283 | /** |
| 284 | * Adds code to the fragment shader code which modifies the color by |
| 285 | * the specified color matrix. |
| 286 | */ |
| 287 | static void addColorMatrix(GrStringBuilder* fsCode, const char * outputVar, |
| 288 | const char* inColor) { |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 289 | 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); |
| 290 | fsCode->appendf("\t%s.rgb *= %s.a;\n", outputVar, outputVar); |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 291 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 292 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 293 | void GrGLProgram::genEdgeCoverage(const GrGLContextInfo& gl, |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 294 | GrVertexLayout layout, |
| 295 | CachedData* programData, |
| 296 | GrStringBuilder* coverageVar, |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 297 | GrGLShaderBuilder* segments) const { |
bsalomon@google.com | 7ffe681 | 2012-05-11 17:32:43 +0000 | [diff] [blame] | 298 | if (layout & GrDrawTarget::kEdge_VertexLayoutBit) { |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 299 | const char *vsName, *fsName; |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 300 | segments->addVarying(kVec4f_GrSLType, "Edge", &vsName, &fsName); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 301 | segments->fVSAttrs.push_back().set(kVec4f_GrSLType, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 302 | GrGLShaderVar::kAttribute_TypeModifier, EDGE_ATTR_NAME); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 303 | segments->fVSCode.appendf("\t%s = " EDGE_ATTR_NAME ";\n", vsName); |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 304 | switch (fProgramDesc.fVertexEdgeType) { |
| 305 | case GrDrawState::kHairLine_EdgeType: |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 306 | 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] | 307 | segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 308 | break; |
| 309 | case GrDrawState::kQuad_EdgeType: |
vandebo@chromium.org | b9682d3 | 2012-02-21 18:53:39 +0000 | [diff] [blame] | 310 | segments->fFSCode.append("\tfloat edgeAlpha;\n"); |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 311 | // keep the derivative instructions outside the conditional |
| 312 | segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
| 313 | segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
| 314 | segments->fFSCode.appendf("\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsName, fsName); |
| 315 | // today we know z and w are in device space. We could use derivatives |
| 316 | segments->fFSCode.appendf("\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);\n", fsName, fsName); |
| 317 | segments->fFSCode.append ("\t} else {\n"); |
| 318 | segments->fFSCode.appendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n" |
| 319 | "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n", |
| 320 | fsName, fsName); |
| 321 | 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] | 322 | 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] | 323 | "\t}\n"); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 324 | if (kES2_GrGLBinding == gl.binding()) { |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 325 | segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n"); |
| 326 | } |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 327 | break; |
| 328 | case GrDrawState::kHairQuad_EdgeType: |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 329 | segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
| 330 | segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 331 | segments->fFSCode.appendf("\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n" |
| 332 | "\t 2.0*%s.x*duvdy.x - duvdy.y);\n", |
| 333 | fsName, fsName); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 334 | 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] | 335 | segments->fFSCode.append("\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n"); |
| 336 | segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 337 | if (kES2_GrGLBinding == gl.binding()) { |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +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::kCircle_EdgeType: |
| 342 | segments->fFSCode.append("\tfloat edgeAlpha;\n"); |
| 343 | segments->fFSCode.appendf("\tfloat d = distance(gl_FragCoord.xy, %s.xy);\n", fsName); |
| 344 | segments->fFSCode.appendf("\tfloat outerAlpha = smoothstep(d - 0.5, d + 0.5, %s.z);\n", fsName); |
| 345 | 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); |
| 346 | segments->fFSCode.append("\tedgeAlpha = outerAlpha * innerAlpha;\n"); |
| 347 | break; |
| 348 | default: |
| 349 | GrCrash("Unknown Edge Type!"); |
| 350 | break; |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 351 | } |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 352 | *coverageVar = "edgeAlpha"; |
| 353 | } else { |
| 354 | coverageVar->reset(); |
| 355 | } |
| 356 | } |
| 357 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 358 | namespace { |
| 359 | |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 360 | void genInputColor(GrGLProgram::ProgramDesc::ColorInput colorInput, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 361 | GrGLProgram::CachedData* programData, |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 362 | GrGLShaderBuilder* segments, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 363 | GrStringBuilder* inColor) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 364 | switch (colorInput) { |
| 365 | case GrGLProgram::ProgramDesc::kAttribute_ColorInput: { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 366 | segments->fVSAttrs.push_back().set(kVec4f_GrSLType, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 367 | GrGLShaderVar::kAttribute_TypeModifier, |
| 368 | COL_ATTR_NAME); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 369 | const char *vsName, *fsName; |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 370 | segments->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsName); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 371 | segments->fVSCode.appendf("\t%s = " COL_ATTR_NAME ";\n", vsName); |
| 372 | *inColor = fsName; |
| 373 | } break; |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 374 | case GrGLProgram::ProgramDesc::kUniform_ColorInput: |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 375 | segments->addUniform(GrGLShaderBuilder::kFragment_VariableLifetime, |
| 376 | kVec4f_GrSLType, COL_UNI_NAME); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 377 | programData->fUniLocations.fColorUni = kUseUniform; |
| 378 | *inColor = COL_UNI_NAME; |
| 379 | break; |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 380 | case GrGLProgram::ProgramDesc::kTransBlack_ColorInput: |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 381 | GrAssert(!"needComputedColor should be false."); |
| 382 | break; |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 383 | case GrGLProgram::ProgramDesc::kSolidWhite_ColorInput: |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 384 | break; |
| 385 | default: |
| 386 | GrCrash("Unknown color type."); |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 391 | void genAttributeCoverage(GrGLShaderBuilder* segments, |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 392 | GrStringBuilder* inOutCoverage) { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 393 | segments->fVSAttrs.push_back().set(kVec4f_GrSLType, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 394 | GrGLShaderVar::kAttribute_TypeModifier, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 395 | COV_ATTR_NAME); |
| 396 | const char *vsName, *fsName; |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 397 | segments->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &fsName); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 398 | segments->fVSCode.appendf("\t%s = " COV_ATTR_NAME ";\n", vsName); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 399 | if (inOutCoverage->size()) { |
| 400 | segments->fFSCode.appendf("\tvec4 attrCoverage = %s * %s;\n", |
| 401 | fsName, inOutCoverage->c_str()); |
| 402 | *inOutCoverage = "attrCoverage"; |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 403 | } else { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 404 | *inOutCoverage = fsName; |
| 405 | } |
| 406 | } |
| 407 | |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 408 | void genUniformCoverage(GrGLShaderBuilder* segments, |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 409 | GrGLProgram::CachedData* programData, |
| 410 | GrStringBuilder* inOutCoverage) { |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 411 | segments->addUniform(GrGLShaderBuilder::kFragment_VariableLifetime, |
| 412 | kVec4f_GrSLType, COV_UNI_NAME); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 413 | programData->fUniLocations.fCoverageUni = kUseUniform; |
| 414 | if (inOutCoverage->size()) { |
| 415 | segments->fFSCode.appendf("\tvec4 uniCoverage = %s * %s;\n", |
| 416 | COV_UNI_NAME, inOutCoverage->c_str()); |
| 417 | *inOutCoverage = "uniCoverage"; |
| 418 | } else { |
| 419 | *inOutCoverage = COV_UNI_NAME; |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 423 | } |
| 424 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 425 | void GrGLProgram::genGeometryShader(const GrGLContextInfo& gl, |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 426 | GrGLShaderBuilder* segments) const { |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 427 | #if GR_GL_EXPERIMENTAL_GS |
| 428 | if (fProgramDesc.fExperimentalGS) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 429 | GrAssert(gl.glslGeneration() >= k150_GrGLSLGeneration); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 430 | segments->fGSHeader.append("layout(triangles) in;\n" |
| 431 | "layout(triangle_strip, max_vertices = 6) out;\n"); |
| 432 | segments->fGSCode.append("void main() {\n" |
| 433 | "\tfor (int i = 0; i < 3; ++i) {\n" |
| 434 | "\t\tgl_Position = gl_in[i].gl_Position;\n"); |
| 435 | if (this->fProgramDesc.fEmitsPointSize) { |
| 436 | segments->fGSCode.append("\t\tgl_PointSize = 1.0;\n"); |
| 437 | } |
| 438 | GrAssert(segments->fGSInputs.count() == segments->fGSOutputs.count()); |
| 439 | int count = segments->fGSInputs.count(); |
| 440 | for (int i = 0; i < count; ++i) { |
| 441 | segments->fGSCode.appendf("\t\t%s = %s[i];\n", |
| 442 | segments->fGSOutputs[i].getName().c_str(), |
| 443 | segments->fGSInputs[i].getName().c_str()); |
| 444 | } |
| 445 | segments->fGSCode.append("\t\tEmitVertex();\n" |
| 446 | "\t}\n" |
| 447 | "\tEndPrimitive();\n" |
| 448 | "}\n"); |
| 449 | } |
| 450 | #endif |
| 451 | } |
| 452 | |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 453 | const char* GrGLProgram::adjustInColor(const GrStringBuilder& inColor) const { |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 454 | if (inColor.size()) { |
| 455 | return inColor.c_str(); |
| 456 | } else { |
| 457 | if (ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput) { |
| 458 | return all_ones_vec(4); |
| 459 | } else { |
| 460 | return all_zeros_vec(4); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 465 | // If this destructor is in the header file, we must include GrGLProgramStage |
| 466 | // instead of just forward-declaring it. |
| 467 | GrGLProgram::CachedData::~CachedData() { |
| 468 | for (int i = 0; i < GrDrawState::kNumStages; ++i) { |
| 469 | delete fCustomStage[i]; |
| 470 | } |
| 471 | } |
| 472 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 473 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 474 | bool GrGLProgram::genProgram(const GrGLContextInfo& gl, |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 475 | GrCustomStage** customStages, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 476 | GrGLProgram::CachedData* programData) const { |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 477 | GrGLShaderBuilder segments; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 478 | const uint32_t& layout = fProgramDesc.fVertexLayout; |
| 479 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 480 | programData->fUniLocations.reset(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 481 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 482 | #if GR_GL_EXPERIMENTAL_GS |
| 483 | segments.fUsesGS = fProgramDesc.fExperimentalGS; |
| 484 | #endif |
| 485 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 486 | SkXfermode::Coeff colorCoeff, uniformCoeff; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 487 | bool applyColorMatrix = SkToBool(fProgramDesc.fColorMatrixEnabled); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 488 | // The rest of transfer mode color filters have not been implemented |
| 489 | if (fProgramDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) { |
| 490 | GR_DEBUGCODE(bool success =) |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 491 | SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode> |
| 492 | (fProgramDesc.fColorFilterXfermode), |
| 493 | &uniformCoeff, &colorCoeff); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 494 | GR_DEBUGASSERT(success); |
| 495 | } else { |
| 496 | colorCoeff = SkXfermode::kOne_Coeff; |
| 497 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 498 | } |
| 499 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 500 | // no need to do the color filter / matrix at all if coverage is 0. The |
| 501 | // output color is scaled by the coverage. All the dual source outputs are |
| 502 | // scaled by the coverage as well. |
| 503 | if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fCoverageInput) { |
| 504 | colorCoeff = SkXfermode::kZero_Coeff; |
| 505 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 506 | applyColorMatrix = false; |
| 507 | } |
| 508 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 509 | // If we know the final color is going to be all zeros then we can |
| 510 | // simplify the color filter coeffecients. needComputedColor will then |
| 511 | // come out false below. |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 512 | if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fColorInput) { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 513 | colorCoeff = SkXfermode::kZero_Coeff; |
| 514 | if (SkXfermode::kDC_Coeff == uniformCoeff || |
| 515 | SkXfermode::kDA_Coeff == uniformCoeff) { |
| 516 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 517 | } else if (SkXfermode::kIDC_Coeff == uniformCoeff || |
| 518 | SkXfermode::kIDA_Coeff == uniformCoeff) { |
| 519 | uniformCoeff = SkXfermode::kOne_Coeff; |
| 520 | } |
| 521 | } |
| 522 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 523 | bool needColorFilterUniform; |
| 524 | bool needComputedColor; |
| 525 | needBlendInputs(uniformCoeff, colorCoeff, |
| 526 | &needColorFilterUniform, &needComputedColor); |
| 527 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 528 | // the dual source output has no canonical var name, have to |
| 529 | // declare an output, which is incompatible with gl_FragColor/gl_FragData. |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 530 | bool dualSourceOutputWritten = false; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 531 | segments.fHeader.printf(GrGetGLSLVersionDecl(gl.binding(), |
| 532 | gl.glslGeneration())); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 533 | |
| 534 | GrGLShaderVar colorOutput; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 535 | bool isColorDeclared = GrGLSLSetupFSColorOuput(gl.glslGeneration(), |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 536 | declared_color_output_name(), |
| 537 | &colorOutput); |
| 538 | if (isColorDeclared) { |
| 539 | segments.fFSOutputs.push_back(colorOutput); |
| 540 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 541 | |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 542 | segments.addUniform(GrGLShaderBuilder::kVertex_VariableLifetime, |
| 543 | kMat33f_GrSLType, VIEW_MATRIX_NAME); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 544 | programData->fUniLocations.fViewMatrixUni = kUseUniform; |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 545 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 546 | segments.fVSAttrs.push_back().set(kVec2f_GrSLType, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 547 | GrGLShaderVar::kAttribute_TypeModifier, POS_ATTR_NAME); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 548 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 549 | segments.fVSCode.append( |
| 550 | "void main() {\n" |
| 551 | "\tvec3 pos3 = " VIEW_MATRIX_NAME " * vec3("POS_ATTR_NAME", 1);\n" |
| 552 | "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n"); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 553 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 554 | // incoming color to current stage being processed. |
| 555 | GrStringBuilder inColor; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 556 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 557 | if (needComputedColor) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 558 | genInputColor((ProgramDesc::ColorInput) fProgramDesc.fColorInput, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 559 | programData, &segments, &inColor); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 560 | } |
| 561 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 562 | // we output point size in the GS if present |
| 563 | if (fProgramDesc.fEmitsPointSize && !segments.fUsesGS){ |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 564 | segments.fVSCode.append("\tgl_PointSize = 1.0;\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 565 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 566 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 567 | segments.fFSCode.append("void main() {\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 568 | |
| 569 | // 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] | 570 | GrStringBuilder texCoordAttrs[GrDrawState::kMaxTexCoords]; |
| 571 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 572 | if (GrDrawTarget::VertexUsesTexCoordIdx(t, layout)) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 573 | tex_attr_name(t, texCoordAttrs + t); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 574 | segments.fVSAttrs.push_back().set(kVec2f_GrSLType, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 575 | GrGLShaderVar::kAttribute_TypeModifier, |
| 576 | texCoordAttrs[t].c_str()); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 580 | /////////////////////////////////////////////////////////////////////////// |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 581 | // We need to convert generic effect representations to GL-specific |
| 582 | // backends so they can be accesseed in genStageCode() and in subsequent, |
| 583 | // uses of programData, but it's safest to do so below when we're *sure* |
| 584 | // we need them. |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 585 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 586 | programData->fCustomStage[s] = NULL; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 590 | // compute the final color |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 591 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 592 | // if we have color stages string them together, feeding the output color |
| 593 | // of each to the next and generating code for each stage. |
| 594 | if (needComputedColor) { |
| 595 | GrStringBuilder outColor; |
| 596 | for (int s = 0; s < fProgramDesc.fFirstCoverageStage; ++s) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 597 | if (fProgramDesc.fStages[s].isEnabled()) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 598 | // create var to hold stage result |
| 599 | outColor = "color"; |
| 600 | outColor.appendS32(s); |
| 601 | segments.fFSCode.appendf("\tvec4 %s;\n", outColor.c_str()); |
| 602 | |
| 603 | const char* inCoords; |
| 604 | // figure out what our input coords are |
| 605 | if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) & |
| 606 | layout) { |
| 607 | inCoords = POS_ATTR_NAME; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 608 | } else { |
| 609 | int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout); |
| 610 | // we better have input tex coordinates if stage is enabled. |
| 611 | GrAssert(tcIdx >= 0); |
| 612 | GrAssert(texCoordAttrs[tcIdx].size()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 613 | inCoords = texCoordAttrs[tcIdx].c_str(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 614 | } |
| 615 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 616 | if (NULL != customStages[s]) { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 617 | const GrProgramStageFactory& factory = |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 618 | customStages[s]->getFactory(); |
| 619 | programData->fCustomStage[s] = |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 620 | factory.createGLInstance(*customStages[s]); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 621 | } |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 622 | this->genStageCode(gl, |
| 623 | s, |
| 624 | fProgramDesc.fStages[s], |
| 625 | inColor.size() ? inColor.c_str() : NULL, |
| 626 | outColor.c_str(), |
| 627 | inCoords, |
| 628 | &segments, |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 629 | &programData->fUniLocations.fStages[s], |
| 630 | programData->fCustomStage[s]); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 631 | inColor = outColor; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 632 | } |
| 633 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 634 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 635 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 636 | // if have all ones or zeros for the "dst" input to the color filter then we |
| 637 | // may be able to make additional optimizations. |
| 638 | if (needColorFilterUniform && needComputedColor && !inColor.size()) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 639 | GrAssert(ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 640 | bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff || |
| 641 | SkXfermode::kIDA_Coeff == uniformCoeff; |
| 642 | if (uniformCoeffIsZero) { |
| 643 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 644 | bool bogus; |
| 645 | needBlendInputs(SkXfermode::kZero_Coeff, colorCoeff, |
| 646 | &needColorFilterUniform, &bogus); |
| 647 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 648 | } |
| 649 | if (needColorFilterUniform) { |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 650 | segments.addUniform(GrGLShaderBuilder::kFragment_VariableLifetime, |
| 651 | kVec4f_GrSLType, COL_FILTER_UNI_NAME); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 652 | programData->fUniLocations.fColorFilterUni = kUseUniform; |
| 653 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 654 | bool wroteFragColorZero = false; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 655 | if (SkXfermode::kZero_Coeff == uniformCoeff && |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 656 | SkXfermode::kZero_Coeff == colorCoeff && |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 657 | !applyColorMatrix) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 658 | segments.fFSCode.appendf("\t%s = %s;\n", |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 659 | colorOutput.getName().c_str(), |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 660 | all_zeros_vec(4)); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 661 | wroteFragColorZero = true; |
| 662 | } else if (SkXfermode::kDst_Mode != fProgramDesc.fColorFilterXfermode) { |
vandebo@chromium.org | b9682d3 | 2012-02-21 18:53:39 +0000 | [diff] [blame] | 663 | segments.fFSCode.append("\tvec4 filteredColor;\n"); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 664 | const char* color = adjustInColor(inColor); |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 665 | addColorFilter(&segments.fFSCode, "filteredColor", uniformCoeff, |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 666 | colorCoeff, color); |
| 667 | inColor = "filteredColor"; |
| 668 | } |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 669 | if (applyColorMatrix) { |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 670 | segments.addUniform(GrGLShaderBuilder::kFragment_VariableLifetime, |
| 671 | kMat44f_GrSLType, COL_MATRIX_UNI_NAME); |
| 672 | segments.addUniform(GrGLShaderBuilder::kFragment_VariableLifetime, |
| 673 | kVec4f_GrSLType, COL_MATRIX_VEC_UNI_NAME); |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 674 | programData->fUniLocations.fColorMatrixUni = kUseUniform; |
| 675 | programData->fUniLocations.fColorMatrixVecUni = kUseUniform; |
vandebo@chromium.org | b9682d3 | 2012-02-21 18:53:39 +0000 | [diff] [blame] | 676 | segments.fFSCode.append("\tvec4 matrixedColor;\n"); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 677 | const char* color = adjustInColor(inColor); |
| 678 | addColorMatrix(&segments.fFSCode, "matrixedColor", color); |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 679 | inColor = "matrixedColor"; |
| 680 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 681 | |
| 682 | /////////////////////////////////////////////////////////////////////////// |
| 683 | // compute the partial coverage (coverage stages and edge aa) |
| 684 | |
| 685 | GrStringBuilder inCoverage; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 686 | bool coverageIsZero = ProgramDesc::kTransBlack_ColorInput == |
| 687 | fProgramDesc.fCoverageInput; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 688 | // we don't need to compute coverage at all if we know the final shader |
| 689 | // output will be zero and we don't have a dual src blend output. |
| 690 | if (!wroteFragColorZero || |
| 691 | ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) { |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 692 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 693 | if (!coverageIsZero) { |
| 694 | this->genEdgeCoverage(gl, |
| 695 | layout, |
| 696 | programData, |
| 697 | &inCoverage, |
| 698 | &segments); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 699 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 700 | switch (fProgramDesc.fCoverageInput) { |
| 701 | case ProgramDesc::kSolidWhite_ColorInput: |
| 702 | // empty string implies solid white |
| 703 | break; |
| 704 | case ProgramDesc::kAttribute_ColorInput: |
| 705 | genAttributeCoverage(&segments, &inCoverage); |
| 706 | break; |
| 707 | case ProgramDesc::kUniform_ColorInput: |
| 708 | genUniformCoverage(&segments, programData, &inCoverage); |
| 709 | break; |
| 710 | default: |
| 711 | GrCrash("Unexpected input coverage."); |
| 712 | } |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 713 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 714 | GrStringBuilder outCoverage; |
| 715 | const int& startStage = fProgramDesc.fFirstCoverageStage; |
| 716 | for (int s = startStage; s < GrDrawState::kNumStages; ++s) { |
| 717 | if (fProgramDesc.fStages[s].isEnabled()) { |
| 718 | // create var to hold stage output |
| 719 | outCoverage = "coverage"; |
| 720 | outCoverage.appendS32(s); |
| 721 | segments.fFSCode.appendf("\tvec4 %s;\n", |
| 722 | outCoverage.c_str()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 723 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 724 | const char* inCoords; |
| 725 | // figure out what our input coords are |
| 726 | if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) & |
| 727 | layout) { |
| 728 | inCoords = POS_ATTR_NAME; |
| 729 | } else { |
| 730 | int tcIdx = |
| 731 | GrDrawTarget::VertexTexCoordsForStage(s, layout); |
| 732 | // we better have input tex coordinates if stage is |
| 733 | // enabled. |
| 734 | GrAssert(tcIdx >= 0); |
| 735 | GrAssert(texCoordAttrs[tcIdx].size()); |
| 736 | inCoords = texCoordAttrs[tcIdx].c_str(); |
| 737 | } |
| 738 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 739 | if (NULL != customStages[s]) { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 740 | const GrProgramStageFactory& factory = |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 741 | customStages[s]->getFactory(); |
| 742 | programData->fCustomStage[s] = |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 743 | factory.createGLInstance(*customStages[s]); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 744 | } |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 745 | this->genStageCode(gl, s, |
| 746 | fProgramDesc.fStages[s], |
| 747 | inCoverage.size() ? inCoverage.c_str() : NULL, |
| 748 | outCoverage.c_str(), |
| 749 | inCoords, |
| 750 | &segments, |
| 751 | &programData->fUniLocations.fStages[s], |
| 752 | programData->fCustomStage[s]); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 753 | inCoverage = outCoverage; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 754 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 755 | } |
| 756 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 757 | if (ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 758 | segments.fFSOutputs.push_back().set(kVec4f_GrSLType, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 759 | GrGLShaderVar::kOut_TypeModifier, |
| 760 | dual_source_output_name()); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 761 | bool outputIsZero = coverageIsZero; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 762 | GrStringBuilder coeff; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 763 | if (!outputIsZero && |
| 764 | ProgramDesc::kCoverage_DualSrcOutput != |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 765 | fProgramDesc.fDualSrcOutput && !wroteFragColorZero) { |
| 766 | if (!inColor.size()) { |
| 767 | outputIsZero = true; |
| 768 | } else { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 769 | if (fProgramDesc.fDualSrcOutput == |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 770 | ProgramDesc::kCoverageISA_DualSrcOutput) { |
| 771 | coeff.printf("(1 - %s.a)", inColor.c_str()); |
| 772 | } else { |
| 773 | coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str()); |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | if (outputIsZero) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 778 | segments.fFSCode.appendf("\t%s = %s;\n", |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 779 | dual_source_output_name(), |
| 780 | all_zeros_vec(4)); |
| 781 | } else { |
| 782 | modulate_helper(dual_source_output_name(), |
| 783 | coeff.c_str(), |
| 784 | inCoverage.c_str(), |
| 785 | &segments.fFSCode); |
| 786 | } |
| 787 | dualSourceOutputWritten = true; |
| 788 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 789 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 790 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 791 | /////////////////////////////////////////////////////////////////////////// |
| 792 | // combine color and coverage as frag color |
| 793 | |
| 794 | if (!wroteFragColorZero) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 795 | if (coverageIsZero) { |
| 796 | segments.fFSCode.appendf("\t%s = %s;\n", |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 797 | colorOutput.getName().c_str(), |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 798 | all_zeros_vec(4)); |
| 799 | } else { |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 800 | modulate_helper(colorOutput.getName().c_str(), |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 801 | inColor.c_str(), |
| 802 | inCoverage.c_str(), |
| 803 | &segments.fFSCode); |
| 804 | } |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 805 | if (ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig == |
| 806 | fProgramDesc.fOutputConfig) { |
| 807 | 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", |
| 808 | colorOutput.getName().c_str(), |
| 809 | colorOutput.getName().c_str(), |
| 810 | colorOutput.getName().c_str(), |
| 811 | colorOutput.getName().c_str(), |
| 812 | colorOutput.getName().c_str()); |
| 813 | } else if (ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig == |
| 814 | fProgramDesc.fOutputConfig) { |
| 815 | 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", |
| 816 | colorOutput.getName().c_str(), |
| 817 | colorOutput.getName().c_str(), |
| 818 | colorOutput.getName().c_str(), |
| 819 | colorOutput.getName().c_str(), |
| 820 | colorOutput.getName().c_str()); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 821 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 822 | } |
| 823 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 824 | segments.fVSCode.append("}\n"); |
| 825 | segments.fFSCode.append("}\n"); |
| 826 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 827 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 828 | // insert GS |
| 829 | #if GR_DEBUG |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 830 | this->genGeometryShader(gl, &segments); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 831 | #endif |
| 832 | |
| 833 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 834 | // compile and setup attribs and unis |
| 835 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 836 | if (!CompileShaders(gl, segments, programData)) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 837 | return false; |
| 838 | } |
| 839 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 840 | if (!this->bindOutputsAttribsAndLinkProgram(gl, texCoordAttrs, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 841 | isColorDeclared, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 842 | dualSourceOutputWritten, |
| 843 | programData)) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 844 | return false; |
| 845 | } |
| 846 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 847 | this->getUniformLocationsAndInitCache(gl, programData); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 848 | |
| 849 | return true; |
| 850 | } |
| 851 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 852 | namespace { |
| 853 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 854 | inline void expand_decls(const VarArray& vars, |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 855 | const GrGLContextInfo& gl, |
| 856 | GrStringBuilder* string) { |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 857 | const int count = vars.count(); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 858 | for (int i = 0; i < count; ++i) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 859 | vars[i].appendDecl(gl, string); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 863 | inline void print_shader(int stringCnt, |
| 864 | const char** strings, |
| 865 | int* stringLengths) { |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 866 | for (int i = 0; i < stringCnt; ++i) { |
| 867 | if (NULL == stringLengths || stringLengths[i] < 0) { |
| 868 | GrPrintf(strings[i]); |
| 869 | } else { |
| 870 | GrPrintf("%.*s", stringLengths[i], strings[i]); |
| 871 | } |
| 872 | } |
| 873 | } |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 874 | |
| 875 | typedef SkTArray<const char*, true> StrArray; |
| 876 | #define PREALLOC_STR_ARRAY(N) SkSTArray<(N), const char*, true> |
| 877 | |
| 878 | typedef SkTArray<int, true> LengthArray; |
| 879 | #define PREALLOC_LENGTH_ARRAY(N) SkSTArray<(N), int, true> |
| 880 | |
| 881 | // these shouldn't relocate |
| 882 | typedef GrTAllocator<GrStringBuilder> TempArray; |
| 883 | #define PREALLOC_TEMP_ARRAY(N) GrSTAllocator<(N), GrStringBuilder> |
| 884 | |
| 885 | inline void append_string(const GrStringBuilder& str, |
| 886 | StrArray* strings, |
| 887 | LengthArray* lengths) { |
| 888 | int length = (int) str.size(); |
| 889 | if (length) { |
| 890 | strings->push_back(str.c_str()); |
| 891 | lengths->push_back(length); |
| 892 | } |
| 893 | GrAssert(strings->count() == lengths->count()); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 894 | } |
| 895 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 896 | inline void append_decls(const VarArray& vars, |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 897 | const GrGLContextInfo& gl, |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 898 | StrArray* strings, |
| 899 | LengthArray* lengths, |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 900 | TempArray* temp) { |
| 901 | expand_decls(vars, gl, &temp->push_back()); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 902 | append_string(temp->back(), strings, lengths); |
| 903 | } |
| 904 | |
| 905 | } |
| 906 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 907 | bool GrGLProgram::CompileShaders(const GrGLContextInfo& gl, |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 908 | const GrGLShaderBuilder& segments, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 909 | CachedData* programData) { |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 910 | enum { kPreAllocStringCnt = 8 }; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 911 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 912 | PREALLOC_STR_ARRAY(kPreAllocStringCnt) strs; |
| 913 | PREALLOC_LENGTH_ARRAY(kPreAllocStringCnt) lengths; |
| 914 | PREALLOC_TEMP_ARRAY(kPreAllocStringCnt) temps; |
| 915 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 916 | GrStringBuilder unis; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 917 | GrStringBuilder inputs; |
| 918 | GrStringBuilder outputs; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 919 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 920 | append_string(segments.fHeader, &strs, &lengths); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 921 | append_decls(segments.fVSUnis, gl, &strs, &lengths, &temps); |
| 922 | append_decls(segments.fVSAttrs, gl, &strs, &lengths, &temps); |
| 923 | append_decls(segments.fVSOutputs, gl, &strs, &lengths, &temps); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 924 | append_string(segments.fVSCode, &strs, &lengths); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 925 | |
| 926 | #if PRINT_SHADERS |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 927 | print_shader(strs.count(), &strs[0], &lengths[0]); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 928 | GrPrintf("\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 929 | #endif |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 930 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 931 | programData->fVShaderID = |
| 932 | CompileShader(gl, GR_GL_VERTEX_SHADER, strs.count(), |
| 933 | &strs[0], &lengths[0]); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 934 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 935 | if (!programData->fVShaderID) { |
| 936 | return false; |
| 937 | } |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 938 | if (segments.fUsesGS) { |
| 939 | strs.reset(); |
| 940 | lengths.reset(); |
| 941 | temps.reset(); |
| 942 | append_string(segments.fHeader, &strs, &lengths); |
| 943 | append_string(segments.fGSHeader, &strs, &lengths); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 944 | append_decls(segments.fGSInputs, gl, &strs, &lengths, &temps); |
| 945 | append_decls(segments.fGSOutputs, gl, &strs, &lengths, &temps); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 946 | append_string(segments.fGSCode, &strs, &lengths); |
| 947 | #if PRINT_SHADERS |
| 948 | print_shader(strs.count(), &strs[0], &lengths[0]); |
| 949 | GrPrintf("\n"); |
| 950 | #endif |
| 951 | programData->fGShaderID = |
| 952 | CompileShader(gl, GR_GL_GEOMETRY_SHADER, strs.count(), |
| 953 | &strs[0], &lengths[0]); |
| 954 | } else { |
| 955 | programData->fGShaderID = 0; |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 956 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 957 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 958 | strs.reset(); |
| 959 | lengths.reset(); |
| 960 | temps.reset(); |
| 961 | |
| 962 | append_string(segments.fHeader, &strs, &lengths); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 963 | GrStringBuilder precisionStr(GrGetGLSLShaderPrecisionDecl(gl.binding())); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 964 | append_string(precisionStr, &strs, &lengths); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 965 | append_decls(segments.fFSUnis, gl, &strs, &lengths, &temps); |
| 966 | append_decls(segments.fFSInputs, gl, &strs, &lengths, &temps); |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 967 | // We shouldn't have declared outputs on 1.10 |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 968 | GrAssert(k110_GrGLSLGeneration != gl.glslGeneration() || |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 969 | segments.fFSOutputs.empty()); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 970 | append_decls(segments.fFSOutputs, gl, &strs, &lengths, &temps); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 971 | append_string(segments.fFSFunctions, &strs, &lengths); |
| 972 | append_string(segments.fFSCode, &strs, &lengths); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 973 | |
| 974 | #if PRINT_SHADERS |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 975 | print_shader(strs.count(), &strs[0], &lengths[0]); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 976 | GrPrintf("\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 977 | #endif |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 978 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 979 | programData->fFShaderID = |
| 980 | CompileShader(gl, GR_GL_FRAGMENT_SHADER, strs.count(), |
| 981 | &strs[0], &lengths[0]); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 982 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 983 | if (!programData->fFShaderID) { |
| 984 | return false; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 985 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 986 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 987 | return true; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 988 | } |
| 989 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 990 | #define GL_CALL(X) GR_GL_CALL(gl.interface(), X) |
| 991 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(gl.interface(), R, X) |
| 992 | |
| 993 | GrGLuint GrGLProgram::CompileShader(const GrGLContextInfo& gl, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 994 | GrGLenum type, |
| 995 | int stringCnt, |
| 996 | const char** strings, |
| 997 | int* stringLengths) { |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 998 | SK_TRACE_EVENT1("GrGLProgram::CompileShader", |
| 999 | "stringCount", SkStringPrintf("%i", stringCnt).c_str()); |
| 1000 | |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 1001 | GrGLuint shader; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1002 | GL_CALL_RET(shader, CreateShader(type)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1003 | if (0 == shader) { |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
| 1007 | GrGLint compiled = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1008 | GL_CALL(ShaderSource(shader, stringCnt, strings, stringLengths)); |
| 1009 | GL_CALL(CompileShader(shader)); |
| 1010 | GL_CALL(GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1011 | |
| 1012 | if (!compiled) { |
| 1013 | GrGLint infoLen = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1014 | GL_CALL(GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 1015 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1016 | if (infoLen > 0) { |
bsalomon@google.com | 79afcaa | 2011-09-14 14:29:18 +0000 | [diff] [blame] | 1017 | // retrieve length even though we don't need it to workaround |
| 1018 | // bug in chrome cmd buffer param validation. |
| 1019 | GrGLsizei length = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1020 | GL_CALL(GetShaderInfoLog(shader, infoLen+1, |
| 1021 | &length, (char*)log.get())); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1022 | print_shader(stringCnt, strings, stringLengths); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1023 | GrPrintf("\n%s", log.get()); |
| 1024 | } |
| 1025 | GrAssert(!"Shader compilation failed!"); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1026 | GL_CALL(DeleteShader(shader)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1027 | return 0; |
| 1028 | } |
| 1029 | return shader; |
| 1030 | } |
| 1031 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1032 | bool GrGLProgram::bindOutputsAttribsAndLinkProgram( |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1033 | const GrGLContextInfo& gl, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1034 | GrStringBuilder texCoordAttrNames[], |
| 1035 | bool bindColorOut, |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 1036 | bool bindDualSrcOut, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1037 | CachedData* programData) const { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1038 | GL_CALL_RET(programData->fProgramID, CreateProgram()); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1039 | if (!programData->fProgramID) { |
| 1040 | return false; |
| 1041 | } |
| 1042 | const GrGLint& progID = programData->fProgramID; |
| 1043 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1044 | GL_CALL(AttachShader(progID, programData->fVShaderID)); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1045 | if (programData->fGShaderID) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1046 | GL_CALL(AttachShader(progID, programData->fGShaderID)); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1047 | } |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1048 | GL_CALL(AttachShader(progID, programData->fFShaderID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1049 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1050 | if (bindColorOut) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1051 | GL_CALL(BindFragDataLocation(programData->fProgramID, |
| 1052 | 0, declared_color_output_name())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1053 | } |
| 1054 | if (bindDualSrcOut) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1055 | GL_CALL(BindFragDataLocationIndexed(programData->fProgramID, |
| 1056 | 0, 1, dual_source_output_name())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1059 | // Bind the attrib locations to same values for all shaders |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1060 | GL_CALL(BindAttribLocation(progID, PositionAttributeIdx(), POS_ATTR_NAME)); |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1061 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1062 | if (texCoordAttrNames[t].size()) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1063 | GL_CALL(BindAttribLocation(progID, |
| 1064 | TexCoordAttributeIdx(t), |
| 1065 | texCoordAttrNames[t].c_str())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1066 | } |
| 1067 | } |
| 1068 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1069 | GL_CALL(BindAttribLocation(progID, ColorAttributeIdx(), COL_ATTR_NAME)); |
| 1070 | GL_CALL(BindAttribLocation(progID, CoverageAttributeIdx(), COV_ATTR_NAME)); |
| 1071 | GL_CALL(BindAttribLocation(progID, EdgeAttributeIdx(), EDGE_ATTR_NAME)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1072 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1073 | GL_CALL(LinkProgram(progID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1074 | |
| 1075 | GrGLint linked = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1076 | GL_CALL(GetProgramiv(progID, GR_GL_LINK_STATUS, &linked)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1077 | if (!linked) { |
| 1078 | GrGLint infoLen = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1079 | GL_CALL(GetProgramiv(progID, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 1080 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1081 | if (infoLen > 0) { |
bsalomon@google.com | 79afcaa | 2011-09-14 14:29:18 +0000 | [diff] [blame] | 1082 | // retrieve length even though we don't need it to workaround |
| 1083 | // bug in chrome cmd buffer param validation. |
| 1084 | GrGLsizei length = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1085 | GL_CALL(GetProgramInfoLog(progID, |
| 1086 | infoLen+1, |
| 1087 | &length, |
| 1088 | (char*)log.get())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1089 | GrPrintf((char*)log.get()); |
| 1090 | } |
| 1091 | GrAssert(!"Error linking program"); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1092 | GL_CALL(DeleteProgram(progID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1093 | programData->fProgramID = 0; |
| 1094 | return false; |
| 1095 | } |
| 1096 | return true; |
| 1097 | } |
| 1098 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1099 | void GrGLProgram::getUniformLocationsAndInitCache(const GrGLContextInfo& gl, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1100 | CachedData* programData) const { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1101 | const GrGLint& progID = programData->fProgramID; |
| 1102 | |
| 1103 | if (kUseUniform == programData->fUniLocations.fViewMatrixUni) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1104 | GL_CALL_RET(programData->fUniLocations.fViewMatrixUni, |
| 1105 | GetUniformLocation(progID, VIEW_MATRIX_NAME)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1106 | GrAssert(kUnusedUniform != programData->fUniLocations.fViewMatrixUni); |
| 1107 | } |
| 1108 | if (kUseUniform == programData->fUniLocations.fColorUni) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1109 | GL_CALL_RET(programData->fUniLocations.fColorUni, |
| 1110 | GetUniformLocation(progID, COL_UNI_NAME)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1111 | GrAssert(kUnusedUniform != programData->fUniLocations.fColorUni); |
| 1112 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 1113 | if (kUseUniform == programData->fUniLocations.fColorFilterUni) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1114 | GL_CALL_RET(programData->fUniLocations.fColorFilterUni, |
| 1115 | GetUniformLocation(progID, COL_FILTER_UNI_NAME)); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 1116 | GrAssert(kUnusedUniform != programData->fUniLocations.fColorFilterUni); |
| 1117 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1118 | |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 1119 | if (kUseUniform == programData->fUniLocations.fColorMatrixUni) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1120 | GL_CALL_RET(programData->fUniLocations.fColorMatrixUni, |
| 1121 | GetUniformLocation(progID, COL_MATRIX_UNI_NAME)); |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | if (kUseUniform == programData->fUniLocations.fColorMatrixVecUni) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1125 | GL_CALL_RET(programData->fUniLocations.fColorMatrixVecUni, |
| 1126 | GetUniformLocation(progID, COL_MATRIX_VEC_UNI_NAME)); |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 1127 | } |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 1128 | if (kUseUniform == programData->fUniLocations.fCoverageUni) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1129 | GL_CALL_RET(programData->fUniLocations.fCoverageUni, |
| 1130 | GetUniformLocation(progID, COV_UNI_NAME)); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 1131 | GrAssert(kUnusedUniform != programData->fUniLocations.fCoverageUni); |
| 1132 | } |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 1133 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1134 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1135 | StageUniLocations& locations = programData->fUniLocations.fStages[s]; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 1136 | if (fProgramDesc.fStages[s].isEnabled()) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1137 | if (kUseUniform == locations.fTextureMatrixUni) { |
| 1138 | GrStringBuilder texMName; |
| 1139 | tex_matrix_name(s, &texMName); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1140 | GL_CALL_RET(locations.fTextureMatrixUni, |
| 1141 | GetUniformLocation(progID, texMName.c_str())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1142 | GrAssert(kUnusedUniform != locations.fTextureMatrixUni); |
| 1143 | } |
| 1144 | |
| 1145 | if (kUseUniform == locations.fSamplerUni) { |
| 1146 | GrStringBuilder samplerName; |
| 1147 | sampler_name(s, &samplerName); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1148 | GL_CALL_RET(locations.fSamplerUni, |
| 1149 | GetUniformLocation(progID,samplerName.c_str())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1150 | GrAssert(kUnusedUniform != locations.fSamplerUni); |
| 1151 | } |
| 1152 | |
| 1153 | if (kUseUniform == locations.fNormalizedTexelSizeUni) { |
| 1154 | GrStringBuilder texelSizeName; |
| 1155 | normalized_texel_size_name(s, &texelSizeName); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1156 | GL_CALL_RET(locations.fNormalizedTexelSizeUni, |
| 1157 | GetUniformLocation(progID, texelSizeName.c_str())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1158 | GrAssert(kUnusedUniform != locations.fNormalizedTexelSizeUni); |
| 1159 | } |
| 1160 | |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1161 | if (kUseUniform == locations.fTexDomUni) { |
| 1162 | GrStringBuilder texDomName; |
| 1163 | tex_domain_name(s, &texDomName); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1164 | GL_CALL_RET(locations.fTexDomUni, |
| 1165 | GetUniformLocation(progID, texDomName.c_str())); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1166 | GrAssert(kUnusedUniform != locations.fTexDomUni); |
| 1167 | } |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 1168 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1169 | if (NULL != programData->fCustomStage[s]) { |
| 1170 | programData->fCustomStage[s]-> |
| 1171 | initUniforms(gl.interface(), progID); |
| 1172 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1173 | } |
| 1174 | } |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1175 | GL_CALL(UseProgram(progID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1176 | |
| 1177 | // init sampler unis and set bogus values for state tracking |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1178 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1179 | if (kUnusedUniform != programData->fUniLocations.fStages[s].fSamplerUni) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1180 | GL_CALL(Uniform1i(programData->fUniLocations.fStages[s].fSamplerUni, s)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1181 | } |
| 1182 | programData->fTextureMatrices[s] = GrMatrix::InvalidMatrix(); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1183 | programData->fTextureWidth[s] = -1; |
| 1184 | programData->fTextureHeight[s] = -1; |
tomhudson@google.com | 24878f7 | 2012-03-22 14:44:46 +0000 | [diff] [blame] | 1185 | programData->fTextureDomain[s].setEmpty(); |
bsalomon@google.com | 890e3b5 | 2012-06-01 19:01:37 +0000 | [diff] [blame] | 1186 | // this is arbitrary, just initialize to something |
| 1187 | programData->fTextureOrientation[s] = |
| 1188 | GrGLTexture::kBottomUp_Orientation; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1189 | // Must not reset fStageOverride[] here. |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1190 | } |
| 1191 | programData->fViewMatrix = GrMatrix::InvalidMatrix(); |
| 1192 | programData->fColor = GrColor_ILLEGAL; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 1193 | programData->fColorFilterColor = GrColor_ILLEGAL; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1196 | //============================================================================ |
| 1197 | // Stage code generation |
| 1198 | //============================================================================ |
| 1199 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1200 | namespace { |
| 1201 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1202 | void gen2x2FS(int stageNum, |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 1203 | GrGLShaderBuilder* segments, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1204 | GrGLProgram::StageUniLocations* locations, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1205 | const char* samplerName, |
| 1206 | const char* texelSizeName, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1207 | const char* fsOutColor, |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 1208 | GrStringBuilder& texFunc) { |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1209 | locations->fNormalizedTexelSizeUni = kUseUniform; |
tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 1210 | if (segments->fComplexCoord) { |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1211 | // assign the coord to a var rather than compute 4x. |
| 1212 | GrStringBuilder coordVar("tCoord"); |
| 1213 | coordVar.appendS32(stageNum); |
| 1214 | segments->fFSCode.appendf("\t%s %s = %s;\n", |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 1215 | float_vector_type_str(segments->fCoordDims), |
tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 1216 | coordVar.c_str(), segments->fSampleCoords.c_str()); |
| 1217 | segments->fSampleCoords = coordVar; |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1218 | } |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 1219 | GrAssert(2 == segments->fCoordDims); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1220 | GrStringBuilder accumVar("accum"); |
| 1221 | accumVar.appendS32(stageNum); |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 1222 | segments->fFSCode.appendf("\tvec4 %s = %s(%s, %s + vec2(-%s.x,-%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName, segments->fSampleCoords.c_str(), texelSizeName, texelSizeName, segments->fSwizzle.c_str()); |
| 1223 | segments->fFSCode.appendf("\t%s += %s(%s, %s + vec2(+%s.x,-%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName, segments->fSampleCoords.c_str(), texelSizeName, texelSizeName, segments->fSwizzle.c_str()); |
| 1224 | segments->fFSCode.appendf("\t%s += %s(%s, %s + vec2(-%s.x,+%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName, segments->fSampleCoords.c_str(), texelSizeName, texelSizeName, segments->fSwizzle.c_str()); |
| 1225 | segments->fFSCode.appendf("\t%s += %s(%s, %s + vec2(+%s.x,+%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName, segments->fSampleCoords.c_str(), texelSizeName, texelSizeName, segments->fSwizzle.c_str()); |
| 1226 | segments->fFSCode.appendf("\t%s = .25 * %s%s;\n", fsOutColor, accumVar.c_str(), segments->fModulate.c_str()); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1227 | |
| 1228 | } |
| 1229 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1232 | void GrGLProgram::genStageCode(const GrGLContextInfo& gl, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1233 | int stageNum, |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1234 | const GrGLProgram::StageDesc& desc, |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1235 | const char* fsInColor, // NULL means no incoming color |
| 1236 | const char* fsOutColor, |
| 1237 | const char* vsInCoord, |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 1238 | GrGLShaderBuilder* segments, |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1239 | StageUniLocations* locations, |
| 1240 | GrGLProgramStage* customStage) const { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1241 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1242 | GrAssert(stageNum >= 0 && stageNum <= GrDrawState::kNumStages); |
| 1243 | GrAssert((desc.fInConfigFlags & StageDesc::kInConfigBitMask) == |
| 1244 | desc.fInConfigFlags); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1245 | |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 1246 | /// Vertex Shader Stuff |
| 1247 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1248 | // decide whether we need a matrix to transform texture coords |
| 1249 | // and whether the varying needs a perspective coord. |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1250 | const char* matName = NULL; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1251 | if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) { |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 1252 | segments->fVaryingDims = segments->fCoordDims; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1253 | } else { |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 1254 | GrStringBuilder texMatName; |
| 1255 | tex_matrix_name(stageNum, &texMatName); |
| 1256 | const GrGLShaderVar* mat = &segments->addUniform( |
| 1257 | GrGLShaderBuilder::kVertex_VariableLifetime, kMat33f_GrSLType, |
| 1258 | texMatName.c_str()); |
| 1259 | // Can't use texMatName.c_str() because it's on the stack! |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1260 | matName = mat->getName().c_str(); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 1261 | locations->fTextureMatrixUni = kUseUniform; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1262 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1263 | if (desc.fOptFlags & StageDesc::kNoPerspective_OptFlagBit) { |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 1264 | segments->fVaryingDims = segments->fCoordDims; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1265 | } else { |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 1266 | segments->fVaryingDims = segments->fCoordDims + 1; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1267 | } |
| 1268 | } |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 1269 | GrAssert(segments->fVaryingDims > 0); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1270 | |
tomhudson@google.com | 898e7b5 | 2012-06-01 20:42:15 +0000 | [diff] [blame^] | 1271 | // Must setup variables after computing segments->fVaryingDims |
| 1272 | if (NULL != customStage) { |
| 1273 | customStage->setupVariables(segments, stageNum); |
| 1274 | } |
| 1275 | |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 1276 | GrStringBuilder samplerName; |
| 1277 | sampler_name(stageNum, &samplerName); |
| 1278 | const GrGLShaderVar* sampler = &segments->addUniform( |
| 1279 | GrGLShaderBuilder::kFragment_VariableLifetime, kSampler2D_GrSLType, |
| 1280 | samplerName.c_str()); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1281 | locations->fSamplerUni = kUseUniform; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1282 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1283 | const char* texelSizeName = NULL; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1284 | if (StageDesc::k2x2_FetchMode == desc.fFetchMode) { |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 1285 | GrStringBuilder ntsName; |
| 1286 | normalized_texel_size_name(stageNum, &ntsName); |
| 1287 | texelSizeName = segments->addUniform( |
| 1288 | GrGLShaderBuilder::kFragment_VariableLifetime, |
| 1289 | kVec2f_GrSLType, ntsName.c_str()).getName().c_str(); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1292 | const char *varyingVSName, *varyingFSName; |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 1293 | segments->addVarying(GrSLFloatVectorType(segments->fVaryingDims), |
| 1294 | "Stage", |
| 1295 | stageNum, |
| 1296 | &varyingVSName, |
| 1297 | &varyingFSName); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1298 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1299 | if (!matName) { |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 1300 | GrAssert(segments->fVaryingDims == segments->fCoordDims); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1301 | segments->fVSCode.appendf("\t%s = %s;\n", varyingVSName, vsInCoord); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1302 | } else { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1303 | // varying = texMatrix * texCoord |
| 1304 | segments->fVSCode.appendf("\t%s = (%s * vec3(%s, 1))%s;\n", |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1305 | varyingVSName, matName, vsInCoord, |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 1306 | vector_all_coords(segments->fVaryingDims)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1309 | GrGLShaderVar* kernel = NULL; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1310 | const char* imageIncrementName = NULL; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1311 | if (NULL != customStage) { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 1312 | segments->fVSCode.appendf("\t{ // stage %d %s\n", |
| 1313 | stageNum, customStage->name()); |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 1314 | customStage->emitVS(segments, varyingVSName); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 1315 | segments->fVSCode.appendf("\t}\n"); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1318 | /// Fragment Shader Stuff |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1319 | |
tomhudson@google.com | 5440f06 | 2012-06-01 15:55:50 +0000 | [diff] [blame] | 1320 | segments->fSampleCoords = varyingFSName; |
| 1321 | |
| 1322 | GrGLShaderBuilder::SamplerMode sampleMode = |
| 1323 | GrGLShaderBuilder::kExplicitDivide_SamplerMode; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1324 | if (desc.fOptFlags & (StageDesc::kIdentityMatrix_OptFlagBit | |
| 1325 | StageDesc::kNoPerspective_OptFlagBit)) { |
tomhudson@google.com | 5440f06 | 2012-06-01 15:55:50 +0000 | [diff] [blame] | 1326 | sampleMode = GrGLShaderBuilder::kDefault_SamplerMode; |
tomhudson@google.com | 898e7b5 | 2012-06-01 20:42:15 +0000 | [diff] [blame^] | 1327 | } else if (NULL == customStage && |
tomhudson@google.com | 5440f06 | 2012-06-01 15:55:50 +0000 | [diff] [blame] | 1328 | StageDesc::kSingle_FetchMode == desc.fFetchMode) { |
| 1329 | sampleMode = GrGLShaderBuilder::kProj_SamplerMode; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1330 | } |
tomhudson@google.com | 5440f06 | 2012-06-01 15:55:50 +0000 | [diff] [blame] | 1331 | segments->setupTextureAccess(sampleMode, stageNum); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1332 | |
tomhudson@google.com | 898e7b5 | 2012-06-01 20:42:15 +0000 | [diff] [blame^] | 1333 | segments->computeSwizzle(desc.fInConfigFlags); |
| 1334 | segments->computeModulate(fsInColor); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1335 | |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1336 | static const uint32_t kMulByAlphaMask = |
| 1337 | (StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag | |
| 1338 | StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag); |
| 1339 | |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 1340 | if (desc.fOptFlags & StageDesc::kCustomTextureDomain_OptFlagBit) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1341 | GrStringBuilder texDomainName; |
| 1342 | tex_domain_name(stageNum, &texDomainName); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 1343 | const GrGLShaderVar* texDomain = |
| 1344 | &segments->addUniform( |
| 1345 | GrGLShaderBuilder::kFragment_VariableLifetime, |
| 1346 | kVec4f_GrSLType, texDomainName.c_str()); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1347 | GrStringBuilder coordVar("clampCoord"); |
| 1348 | segments->fFSCode.appendf("\t%s %s = clamp(%s, %s.xy, %s.zw);\n", |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 1349 | float_vector_type_str(segments->fCoordDims), |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1350 | coordVar.c_str(), |
tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 1351 | segments->fSampleCoords.c_str(), |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1352 | texDomainName.c_str(), |
| 1353 | texDomainName.c_str()); |
tomhudson@google.com | 040c41a | 2012-05-18 14:57:40 +0000 | [diff] [blame] | 1354 | segments->fSampleCoords = coordVar; |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1355 | locations->fTexDomUni = kUseUniform; |
| 1356 | } |
| 1357 | |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 1358 | // NOTE: GrGLProgramStages are now responsible for fetching |
| 1359 | if (NULL == customStage) { |
| 1360 | switch (desc.fFetchMode) { |
| 1361 | case StageDesc::k2x2_FetchMode: |
| 1362 | GrAssert(!(desc.fInConfigFlags & kMulByAlphaMask)); |
| 1363 | gen2x2FS(stageNum, segments, locations, |
tomhudson@google.com | 5440f06 | 2012-06-01 15:55:50 +0000 | [diff] [blame] | 1364 | samplerName.c_str(), texelSizeName, fsOutColor, |
| 1365 | segments->fTexFunc); |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 1366 | break; |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 1367 | default: |
| 1368 | if (desc.fInConfigFlags & kMulByAlphaMask) { |
| 1369 | // only one of the mul by alpha flags should be set |
| 1370 | GrAssert(GrIsPow2(kMulByAlphaMask & desc.fInConfigFlags)); |
| 1371 | GrAssert(!(desc.fInConfigFlags & |
| 1372 | StageDesc::kSmearAlpha_InConfigFlag)); |
| 1373 | GrAssert(!(desc.fInConfigFlags & |
| 1374 | StageDesc::kSmearRed_InConfigFlag)); |
| 1375 | segments->fFSCode.appendf("\t%s = %s(%s, %s)%s;\n", |
tomhudson@google.com | 5440f06 | 2012-06-01 15:55:50 +0000 | [diff] [blame] | 1376 | fsOutColor, |
| 1377 | segments->fTexFunc.c_str(), |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 1378 | samplerName.c_str(), |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 1379 | segments->fSampleCoords.c_str(), |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 1380 | segments->fSwizzle.c_str()); |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 1381 | if (desc.fInConfigFlags & |
| 1382 | StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag) { |
| 1383 | segments->fFSCode.appendf("\t%s = vec4(ceil(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n", |
| 1384 | fsOutColor, fsOutColor, fsOutColor, |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 1385 | fsOutColor, segments->fModulate.c_str()); |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 1386 | } else { |
| 1387 | segments->fFSCode.appendf("\t%s = vec4(floor(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n", |
| 1388 | fsOutColor, fsOutColor, fsOutColor, |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 1389 | fsOutColor, segments->fModulate.c_str()); |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 1390 | } |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1391 | } else { |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 1392 | segments->emitDefaultFetch(fsOutColor, samplerName.c_str()); |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1393 | } |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1394 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1395 | } |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1396 | |
| 1397 | if (NULL != customStage) { |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1398 | // Enclose custom code in a block to avoid namespace conflicts |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 1399 | segments->fFSCode.appendf("\t{ // stage %d %s \n", |
| 1400 | stageNum, customStage->name()); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 1401 | customStage->emitFS(segments, fsOutColor, fsInColor, |
| 1402 | samplerName.c_str()); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 1403 | segments->fFSCode.appendf("\t}\n"); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1404 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1405 | } |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1406 | |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1407 | |