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