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