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