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