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