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