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