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