epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 10 | #include "GrGLProgram.h" |
| 11 | |
tomhudson@google.com | dd182cb | 2012-02-10 21:01:00 +0000 | [diff] [blame] | 12 | #include "../GrAllocator.h" |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 13 | #include "GrGLShaderVar.h" |
tomhudson@google.com | 0c8d93a | 2011-07-01 17:08:26 +0000 | [diff] [blame] | 14 | #include "SkTrace.h" |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 15 | #include "SkXfermode.h" |
| 16 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 17 | namespace { |
| 18 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 19 | enum { |
| 20 | /// Used to mark a StageUniLocation field that should be bound |
| 21 | /// to a uniform during getUniformLocationsAndInitCache(). |
| 22 | kUseUniform = 2000 |
| 23 | }; |
| 24 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 25 | } // namespace |
| 26 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 27 | #define PRINT_SHADERS 0 |
| 28 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 29 | typedef GrTAllocator<GrGLShaderVar> VarArray; |
| 30 | |
| 31 | // number of each input/output type in a single allocation block |
| 32 | static const int gVarsPerBlock = 8; |
| 33 | // except FS outputs where we expect 2 at most. |
| 34 | static const int gMaxFSOutputs = 2; |
| 35 | |
| 36 | struct ShaderCodeSegments { |
| 37 | ShaderCodeSegments() |
| 38 | : fVSUnis(gVarsPerBlock) |
| 39 | , fVSAttrs(gVarsPerBlock) |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 40 | , fVSOutputs(gVarsPerBlock) |
| 41 | , fGSInputs(gVarsPerBlock) |
| 42 | , fGSOutputs(gVarsPerBlock) |
| 43 | , fFSInputs(gVarsPerBlock) |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 44 | , fFSUnis(gVarsPerBlock) |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 45 | , fFSOutputs(gMaxFSOutputs) |
| 46 | , fUsesGS(false) {} |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 47 | GrStringBuilder fHeader; // VS+FS, GLSL version, etc |
| 48 | VarArray fVSUnis; |
| 49 | VarArray fVSAttrs; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 50 | VarArray fVSOutputs; |
| 51 | VarArray fGSInputs; |
| 52 | VarArray fGSOutputs; |
| 53 | VarArray fFSInputs; |
| 54 | GrStringBuilder fGSHeader; // layout qualifiers specific to GS |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 55 | VarArray fFSUnis; |
| 56 | VarArray fFSOutputs; |
| 57 | GrStringBuilder fFSFunctions; |
| 58 | GrStringBuilder fVSCode; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 59 | GrStringBuilder fGSCode; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 60 | GrStringBuilder fFSCode; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 61 | |
| 62 | bool fUsesGS; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 65 | typedef GrGLProgram::ProgramDesc::StageDesc StageDesc; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 66 | |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 67 | #if GR_GL_ATTRIBUTE_MATRICES |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 68 | #define VIEW_MATRIX_NAME "aViewM" |
| 69 | #else |
| 70 | #define VIEW_MATRIX_NAME "uViewM" |
| 71 | #endif |
| 72 | |
| 73 | #define POS_ATTR_NAME "aPosition" |
| 74 | #define COL_ATTR_NAME "aColor" |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 75 | #define COV_ATTR_NAME "aCoverage" |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 76 | #define EDGE_ATTR_NAME "aEdge" |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 77 | #define COL_UNI_NAME "uColor" |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 78 | #define COV_UNI_NAME "uCoverage" |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 79 | #define EDGES_UNI_NAME "uEdges" |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 80 | #define COL_FILTER_UNI_NAME "uColorFilter" |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 81 | #define COL_MATRIX_UNI_NAME "uColorMatrix" |
| 82 | #define COL_MATRIX_VEC_UNI_NAME "uColorMatrixVec" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 83 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 84 | namespace { |
| 85 | inline void tex_attr_name(int coordIdx, GrStringBuilder* s) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 86 | *s = "aTexCoord"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 87 | s->appendS32(coordIdx); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 88 | } |
| 89 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 90 | inline 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 | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 426 | void GrGLProgram::genEdgeCoverage(const GrGLContextInfo& gl, |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 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"); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 506 | if (kES2_GrGLBinding == gl.binding()) { |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 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 | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 519 | if (kES2_GrGLBinding == gl.binding()) { |
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 | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 599 | void GrGLProgram::genGeometryShader(const GrGLContextInfo& gl, |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 600 | ShaderCodeSegments* segments) const { |
| 601 | #if GR_GL_EXPERIMENTAL_GS |
| 602 | if (fProgramDesc.fExperimentalGS) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 603 | GrAssert(gl.glslGeneration() >= k150_GrGLSLGeneration); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 604 | segments->fGSHeader.append("layout(triangles) in;\n" |
| 605 | "layout(triangle_strip, max_vertices = 6) out;\n"); |
| 606 | segments->fGSCode.append("void main() {\n" |
| 607 | "\tfor (int i = 0; i < 3; ++i) {\n" |
| 608 | "\t\tgl_Position = gl_in[i].gl_Position;\n"); |
| 609 | if (this->fProgramDesc.fEmitsPointSize) { |
| 610 | segments->fGSCode.append("\t\tgl_PointSize = 1.0;\n"); |
| 611 | } |
| 612 | GrAssert(segments->fGSInputs.count() == segments->fGSOutputs.count()); |
| 613 | int count = segments->fGSInputs.count(); |
| 614 | for (int i = 0; i < count; ++i) { |
| 615 | segments->fGSCode.appendf("\t\t%s = %s[i];\n", |
| 616 | segments->fGSOutputs[i].getName().c_str(), |
| 617 | segments->fGSInputs[i].getName().c_str()); |
| 618 | } |
| 619 | segments->fGSCode.append("\t\tEmitVertex();\n" |
| 620 | "\t}\n" |
| 621 | "\tEndPrimitive();\n" |
| 622 | "}\n"); |
| 623 | } |
| 624 | #endif |
| 625 | } |
| 626 | |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 627 | const char* GrGLProgram::adjustInColor(const GrStringBuilder& inColor) const { |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 628 | if (inColor.size()) { |
| 629 | return inColor.c_str(); |
| 630 | } else { |
| 631 | if (ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput) { |
| 632 | return all_ones_vec(4); |
| 633 | } else { |
| 634 | return all_zeros_vec(4); |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 639 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 640 | bool GrGLProgram::genProgram(const GrGLContextInfo& gl, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 641 | GrGLProgram::CachedData* programData) const { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 642 | |
| 643 | ShaderCodeSegments segments; |
| 644 | const uint32_t& layout = fProgramDesc.fVertexLayout; |
| 645 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 646 | programData->fUniLocations.reset(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 647 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 648 | #if GR_GL_EXPERIMENTAL_GS |
| 649 | segments.fUsesGS = fProgramDesc.fExperimentalGS; |
| 650 | #endif |
| 651 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 652 | SkXfermode::Coeff colorCoeff, uniformCoeff; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 653 | bool applyColorMatrix = SkToBool(fProgramDesc.fColorMatrixEnabled); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 654 | // The rest of transfer mode color filters have not been implemented |
| 655 | if (fProgramDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) { |
| 656 | GR_DEBUGCODE(bool success =) |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 657 | SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode> |
| 658 | (fProgramDesc.fColorFilterXfermode), |
| 659 | &uniformCoeff, &colorCoeff); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 660 | GR_DEBUGASSERT(success); |
| 661 | } else { |
| 662 | colorCoeff = SkXfermode::kOne_Coeff; |
| 663 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 664 | } |
| 665 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 666 | // no need to do the color filter / matrix at all if coverage is 0. The |
| 667 | // output color is scaled by the coverage. All the dual source outputs are |
| 668 | // scaled by the coverage as well. |
| 669 | if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fCoverageInput) { |
| 670 | colorCoeff = SkXfermode::kZero_Coeff; |
| 671 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 672 | applyColorMatrix = false; |
| 673 | } |
| 674 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 675 | // If we know the final color is going to be all zeros then we can |
| 676 | // simplify the color filter coeffecients. needComputedColor will then |
| 677 | // come out false below. |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 678 | if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fColorInput) { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 679 | colorCoeff = SkXfermode::kZero_Coeff; |
| 680 | if (SkXfermode::kDC_Coeff == uniformCoeff || |
| 681 | SkXfermode::kDA_Coeff == uniformCoeff) { |
| 682 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 683 | } else if (SkXfermode::kIDC_Coeff == uniformCoeff || |
| 684 | SkXfermode::kIDA_Coeff == uniformCoeff) { |
| 685 | uniformCoeff = SkXfermode::kOne_Coeff; |
| 686 | } |
| 687 | } |
| 688 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 689 | bool needColorFilterUniform; |
| 690 | bool needComputedColor; |
| 691 | needBlendInputs(uniformCoeff, colorCoeff, |
| 692 | &needColorFilterUniform, &needComputedColor); |
| 693 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 694 | // the dual source output has no canonical var name, have to |
| 695 | // declare an output, which is incompatible with gl_FragColor/gl_FragData. |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 696 | bool dualSourceOutputWritten = false; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 697 | segments.fHeader.printf(GrGetGLSLVersionDecl(gl.binding(), |
| 698 | gl.glslGeneration())); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 699 | |
| 700 | GrGLShaderVar colorOutput; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 701 | bool isColorDeclared = GrGLSLSetupFSColorOuput(gl.glslGeneration(), |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 702 | declared_color_output_name(), |
| 703 | &colorOutput); |
| 704 | if (isColorDeclared) { |
| 705 | segments.fFSOutputs.push_back(colorOutput); |
| 706 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 707 | |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 708 | #if GR_GL_ATTRIBUTE_MATRICES |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 709 | segments.fVSAttrs.push_back().set(GrGLShaderVar::kMat33f_Type, |
| 710 | GrGLShaderVar::kAttribute_TypeModifier, VIEW_MATRIX_NAME); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 711 | programData->fUniLocations.fViewMatrixUni = kSetAsAttribute; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 712 | #else |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 713 | segments.fVSUnis.push_back().set(GrGLShaderVar::kMat33f_Type, |
| 714 | GrGLShaderVar::kUniform_TypeModifier, VIEW_MATRIX_NAME); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 715 | programData->fUniLocations.fViewMatrixUni = kUseUniform; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 716 | #endif |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 717 | segments.fVSAttrs.push_back().set(GrGLShaderVar::kVec2f_Type, |
| 718 | GrGLShaderVar::kAttribute_TypeModifier, POS_ATTR_NAME); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 719 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 720 | segments.fVSCode.append( |
| 721 | "void main() {\n" |
| 722 | "\tvec3 pos3 = " VIEW_MATRIX_NAME " * vec3("POS_ATTR_NAME", 1);\n" |
| 723 | "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n"); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 724 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 725 | // incoming color to current stage being processed. |
| 726 | GrStringBuilder inColor; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 727 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 728 | if (needComputedColor) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 729 | genInputColor((ProgramDesc::ColorInput) fProgramDesc.fColorInput, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 730 | programData, &segments, &inColor); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 731 | } |
| 732 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 733 | // we output point size in the GS if present |
| 734 | if (fProgramDesc.fEmitsPointSize && !segments.fUsesGS){ |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 735 | segments.fVSCode.append("\tgl_PointSize = 1.0;\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 736 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 737 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 738 | segments.fFSCode.append("void main() {\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 739 | |
| 740 | // 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] | 741 | GrStringBuilder texCoordAttrs[GrDrawState::kMaxTexCoords]; |
| 742 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 743 | if (GrDrawTarget::VertexUsesTexCoordIdx(t, layout)) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 744 | tex_attr_name(t, texCoordAttrs + t); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 745 | segments.fVSAttrs.push_back().set(GrGLShaderVar::kVec2f_Type, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 746 | GrGLShaderVar::kAttribute_TypeModifier, |
| 747 | texCoordAttrs[t].c_str()); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 751 | /////////////////////////////////////////////////////////////////////////// |
| 752 | // compute the final color |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 753 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 754 | // if we have color stages string them together, feeding the output color |
| 755 | // of each to the next and generating code for each stage. |
| 756 | if (needComputedColor) { |
| 757 | GrStringBuilder outColor; |
| 758 | for (int s = 0; s < fProgramDesc.fFirstCoverageStage; ++s) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 759 | if (fProgramDesc.fStages[s].isEnabled()) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 760 | // create var to hold stage result |
| 761 | outColor = "color"; |
| 762 | outColor.appendS32(s); |
| 763 | segments.fFSCode.appendf("\tvec4 %s;\n", outColor.c_str()); |
| 764 | |
| 765 | const char* inCoords; |
| 766 | // figure out what our input coords are |
| 767 | if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) & |
| 768 | layout) { |
| 769 | inCoords = POS_ATTR_NAME; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 770 | } else { |
| 771 | int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout); |
| 772 | // we better have input tex coordinates if stage is enabled. |
| 773 | GrAssert(tcIdx >= 0); |
| 774 | GrAssert(texCoordAttrs[tcIdx].size()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 775 | inCoords = texCoordAttrs[tcIdx].c_str(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 776 | } |
| 777 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 778 | this->genStageCode(gl, |
| 779 | s, |
| 780 | fProgramDesc.fStages[s], |
| 781 | inColor.size() ? inColor.c_str() : NULL, |
| 782 | outColor.c_str(), |
| 783 | inCoords, |
| 784 | &segments, |
| 785 | &programData->fUniLocations.fStages[s]); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 786 | inColor = outColor; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 787 | } |
| 788 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 789 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 790 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 791 | // if have all ones or zeros for the "dst" input to the color filter then we |
| 792 | // may be able to make additional optimizations. |
| 793 | if (needColorFilterUniform && needComputedColor && !inColor.size()) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 794 | GrAssert(ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 795 | bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff || |
| 796 | SkXfermode::kIDA_Coeff == uniformCoeff; |
| 797 | if (uniformCoeffIsZero) { |
| 798 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 799 | bool bogus; |
| 800 | needBlendInputs(SkXfermode::kZero_Coeff, colorCoeff, |
| 801 | &needColorFilterUniform, &bogus); |
| 802 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 803 | } |
| 804 | if (needColorFilterUniform) { |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 805 | segments.fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 806 | GrGLShaderVar::kUniform_TypeModifier, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 807 | COL_FILTER_UNI_NAME); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 808 | programData->fUniLocations.fColorFilterUni = kUseUniform; |
| 809 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 810 | bool wroteFragColorZero = false; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 811 | if (SkXfermode::kZero_Coeff == uniformCoeff && |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 812 | SkXfermode::kZero_Coeff == colorCoeff && |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 813 | !applyColorMatrix) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 814 | segments.fFSCode.appendf("\t%s = %s;\n", |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 815 | colorOutput.getName().c_str(), |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 816 | all_zeros_vec(4)); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 817 | wroteFragColorZero = true; |
| 818 | } else if (SkXfermode::kDst_Mode != fProgramDesc.fColorFilterXfermode) { |
| 819 | segments.fFSCode.appendf("\tvec4 filteredColor;\n"); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 820 | const char* color = adjustInColor(inColor); |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 821 | addColorFilter(&segments.fFSCode, "filteredColor", uniformCoeff, |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 822 | colorCoeff, color); |
| 823 | inColor = "filteredColor"; |
| 824 | } |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 825 | if (applyColorMatrix) { |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 826 | segments.fFSUnis.push_back().set(GrGLShaderVar::kMat44f_Type, |
| 827 | GrGLShaderVar::kUniform_TypeModifier, |
| 828 | COL_MATRIX_UNI_NAME); |
| 829 | segments.fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type, |
| 830 | GrGLShaderVar::kUniform_TypeModifier, |
| 831 | COL_MATRIX_VEC_UNI_NAME); |
| 832 | programData->fUniLocations.fColorMatrixUni = kUseUniform; |
| 833 | programData->fUniLocations.fColorMatrixVecUni = kUseUniform; |
| 834 | segments.fFSCode.appendf("\tvec4 matrixedColor;\n"); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 835 | const char* color = adjustInColor(inColor); |
| 836 | addColorMatrix(&segments.fFSCode, "matrixedColor", color); |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 837 | inColor = "matrixedColor"; |
| 838 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 839 | |
| 840 | /////////////////////////////////////////////////////////////////////////// |
| 841 | // compute the partial coverage (coverage stages and edge aa) |
| 842 | |
| 843 | GrStringBuilder inCoverage; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 844 | bool coverageIsZero = ProgramDesc::kTransBlack_ColorInput == |
| 845 | fProgramDesc.fCoverageInput; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 846 | // we don't need to compute coverage at all if we know the final shader |
| 847 | // output will be zero and we don't have a dual src blend output. |
| 848 | if (!wroteFragColorZero || |
| 849 | ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) { |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 850 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 851 | if (!coverageIsZero) { |
| 852 | this->genEdgeCoverage(gl, |
| 853 | layout, |
| 854 | programData, |
| 855 | &inCoverage, |
| 856 | &segments); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 857 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 858 | switch (fProgramDesc.fCoverageInput) { |
| 859 | case ProgramDesc::kSolidWhite_ColorInput: |
| 860 | // empty string implies solid white |
| 861 | break; |
| 862 | case ProgramDesc::kAttribute_ColorInput: |
| 863 | genAttributeCoverage(&segments, &inCoverage); |
| 864 | break; |
| 865 | case ProgramDesc::kUniform_ColorInput: |
| 866 | genUniformCoverage(&segments, programData, &inCoverage); |
| 867 | break; |
| 868 | default: |
| 869 | GrCrash("Unexpected input coverage."); |
| 870 | } |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 871 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 872 | GrStringBuilder outCoverage; |
| 873 | const int& startStage = fProgramDesc.fFirstCoverageStage; |
| 874 | for (int s = startStage; s < GrDrawState::kNumStages; ++s) { |
| 875 | if (fProgramDesc.fStages[s].isEnabled()) { |
| 876 | // create var to hold stage output |
| 877 | outCoverage = "coverage"; |
| 878 | outCoverage.appendS32(s); |
| 879 | segments.fFSCode.appendf("\tvec4 %s;\n", |
| 880 | outCoverage.c_str()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 881 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 882 | const char* inCoords; |
| 883 | // figure out what our input coords are |
| 884 | if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) & |
| 885 | layout) { |
| 886 | inCoords = POS_ATTR_NAME; |
| 887 | } else { |
| 888 | int tcIdx = |
| 889 | GrDrawTarget::VertexTexCoordsForStage(s, layout); |
| 890 | // we better have input tex coordinates if stage is |
| 891 | // enabled. |
| 892 | GrAssert(tcIdx >= 0); |
| 893 | GrAssert(texCoordAttrs[tcIdx].size()); |
| 894 | inCoords = texCoordAttrs[tcIdx].c_str(); |
| 895 | } |
| 896 | |
| 897 | genStageCode(gl, s, |
| 898 | fProgramDesc.fStages[s], |
| 899 | inCoverage.size() ? inCoverage.c_str() : NULL, |
| 900 | outCoverage.c_str(), |
| 901 | inCoords, |
| 902 | &segments, |
| 903 | &programData->fUniLocations.fStages[s]); |
| 904 | inCoverage = outCoverage; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 905 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 906 | } |
| 907 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 908 | if (ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) { |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 909 | segments.fFSOutputs.push_back().set(GrGLShaderVar::kVec4f_Type, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 910 | GrGLShaderVar::kOut_TypeModifier, |
| 911 | dual_source_output_name()); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 912 | bool outputIsZero = coverageIsZero; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 913 | GrStringBuilder coeff; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 914 | if (!outputIsZero && |
| 915 | ProgramDesc::kCoverage_DualSrcOutput != |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 916 | fProgramDesc.fDualSrcOutput && !wroteFragColorZero) { |
| 917 | if (!inColor.size()) { |
| 918 | outputIsZero = true; |
| 919 | } else { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 920 | if (fProgramDesc.fDualSrcOutput == |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 921 | ProgramDesc::kCoverageISA_DualSrcOutput) { |
| 922 | coeff.printf("(1 - %s.a)", inColor.c_str()); |
| 923 | } else { |
| 924 | coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str()); |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | if (outputIsZero) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 929 | segments.fFSCode.appendf("\t%s = %s;\n", |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 930 | dual_source_output_name(), |
| 931 | all_zeros_vec(4)); |
| 932 | } else { |
| 933 | modulate_helper(dual_source_output_name(), |
| 934 | coeff.c_str(), |
| 935 | inCoverage.c_str(), |
| 936 | &segments.fFSCode); |
| 937 | } |
| 938 | dualSourceOutputWritten = true; |
| 939 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 940 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 941 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 942 | /////////////////////////////////////////////////////////////////////////// |
| 943 | // combine color and coverage as frag color |
| 944 | |
| 945 | if (!wroteFragColorZero) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 946 | if (coverageIsZero) { |
| 947 | segments.fFSCode.appendf("\t%s = %s;\n", |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 948 | colorOutput.getName().c_str(), |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 949 | all_zeros_vec(4)); |
| 950 | } else { |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 951 | modulate_helper(colorOutput.getName().c_str(), |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 952 | inColor.c_str(), |
| 953 | inCoverage.c_str(), |
| 954 | &segments.fFSCode); |
| 955 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 956 | if (ProgramDesc::kNo_OutputPM == fProgramDesc.fOutputPM) { |
bsalomon@google.com | e63799d | 2011-11-09 14:14:38 +0000 | [diff] [blame] | 957 | 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] | 958 | colorOutput.getName().c_str(), |
| 959 | colorOutput.getName().c_str(), |
| 960 | colorOutput.getName().c_str(), |
| 961 | colorOutput.getName().c_str(), |
| 962 | colorOutput.getName().c_str()); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 963 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 964 | } |
| 965 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 966 | segments.fVSCode.append("}\n"); |
| 967 | segments.fFSCode.append("}\n"); |
| 968 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 969 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 970 | // insert GS |
| 971 | #if GR_DEBUG |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 972 | this->genGeometryShader(gl, &segments); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 973 | #endif |
| 974 | |
| 975 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 976 | // compile and setup attribs and unis |
| 977 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 978 | if (!CompileShaders(gl, segments, programData)) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 979 | return false; |
| 980 | } |
| 981 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 982 | if (!this->bindOutputsAttribsAndLinkProgram(gl, texCoordAttrs, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 983 | isColorDeclared, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 984 | dualSourceOutputWritten, |
| 985 | programData)) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 986 | return false; |
| 987 | } |
| 988 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 989 | this->getUniformLocationsAndInitCache(gl, programData); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 990 | |
| 991 | return true; |
| 992 | } |
| 993 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 994 | namespace { |
| 995 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 996 | inline void expand_decls(const VarArray& vars, |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 997 | const GrGLContextInfo& gl, |
| 998 | GrStringBuilder* string) { |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 999 | const int count = vars.count(); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1000 | for (int i = 0; i < count; ++i) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1001 | vars[i].appendDecl(gl, string); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1005 | inline void print_shader(int stringCnt, |
| 1006 | const char** strings, |
| 1007 | int* stringLengths) { |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1008 | for (int i = 0; i < stringCnt; ++i) { |
| 1009 | if (NULL == stringLengths || stringLengths[i] < 0) { |
| 1010 | GrPrintf(strings[i]); |
| 1011 | } else { |
| 1012 | GrPrintf("%.*s", stringLengths[i], strings[i]); |
| 1013 | } |
| 1014 | } |
| 1015 | } |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1016 | |
| 1017 | typedef SkTArray<const char*, true> StrArray; |
| 1018 | #define PREALLOC_STR_ARRAY(N) SkSTArray<(N), const char*, true> |
| 1019 | |
| 1020 | typedef SkTArray<int, true> LengthArray; |
| 1021 | #define PREALLOC_LENGTH_ARRAY(N) SkSTArray<(N), int, true> |
| 1022 | |
| 1023 | // these shouldn't relocate |
| 1024 | typedef GrTAllocator<GrStringBuilder> TempArray; |
| 1025 | #define PREALLOC_TEMP_ARRAY(N) GrSTAllocator<(N), GrStringBuilder> |
| 1026 | |
| 1027 | inline void append_string(const GrStringBuilder& str, |
| 1028 | StrArray* strings, |
| 1029 | LengthArray* lengths) { |
| 1030 | int length = (int) str.size(); |
| 1031 | if (length) { |
| 1032 | strings->push_back(str.c_str()); |
| 1033 | lengths->push_back(length); |
| 1034 | } |
| 1035 | GrAssert(strings->count() == lengths->count()); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1038 | inline void append_decls(const VarArray& vars, |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1039 | const GrGLContextInfo& gl, |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1040 | StrArray* strings, |
| 1041 | LengthArray* lengths, |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1042 | TempArray* temp) { |
| 1043 | expand_decls(vars, gl, &temp->push_back()); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1044 | append_string(temp->back(), strings, lengths); |
| 1045 | } |
| 1046 | |
| 1047 | } |
| 1048 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1049 | bool GrGLProgram::CompileShaders(const GrGLContextInfo& gl, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1050 | const ShaderCodeSegments& segments, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1051 | CachedData* programData) { |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1052 | enum { kPreAllocStringCnt = 8 }; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1053 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1054 | PREALLOC_STR_ARRAY(kPreAllocStringCnt) strs; |
| 1055 | PREALLOC_LENGTH_ARRAY(kPreAllocStringCnt) lengths; |
| 1056 | PREALLOC_TEMP_ARRAY(kPreAllocStringCnt) temps; |
| 1057 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1058 | GrStringBuilder unis; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1059 | GrStringBuilder inputs; |
| 1060 | GrStringBuilder outputs; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1061 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1062 | append_string(segments.fHeader, &strs, &lengths); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1063 | append_decls(segments.fVSUnis, gl, &strs, &lengths, &temps); |
| 1064 | append_decls(segments.fVSAttrs, gl, &strs, &lengths, &temps); |
| 1065 | append_decls(segments.fVSOutputs, gl, &strs, &lengths, &temps); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1066 | append_string(segments.fVSCode, &strs, &lengths); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1067 | |
| 1068 | #if PRINT_SHADERS |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1069 | print_shader(strs.count(), &strs[0], &lengths[0]); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1070 | GrPrintf("\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1071 | #endif |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1072 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1073 | programData->fVShaderID = |
| 1074 | CompileShader(gl, GR_GL_VERTEX_SHADER, strs.count(), |
| 1075 | &strs[0], &lengths[0]); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1076 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1077 | if (!programData->fVShaderID) { |
| 1078 | return false; |
| 1079 | } |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1080 | if (segments.fUsesGS) { |
| 1081 | strs.reset(); |
| 1082 | lengths.reset(); |
| 1083 | temps.reset(); |
| 1084 | append_string(segments.fHeader, &strs, &lengths); |
| 1085 | append_string(segments.fGSHeader, &strs, &lengths); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1086 | append_decls(segments.fGSInputs, gl, &strs, &lengths, &temps); |
| 1087 | append_decls(segments.fGSOutputs, gl, &strs, &lengths, &temps); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1088 | append_string(segments.fGSCode, &strs, &lengths); |
| 1089 | #if PRINT_SHADERS |
| 1090 | print_shader(strs.count(), &strs[0], &lengths[0]); |
| 1091 | GrPrintf("\n"); |
| 1092 | #endif |
| 1093 | programData->fGShaderID = |
| 1094 | CompileShader(gl, GR_GL_GEOMETRY_SHADER, strs.count(), |
| 1095 | &strs[0], &lengths[0]); |
| 1096 | } else { |
| 1097 | programData->fGShaderID = 0; |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 1098 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1099 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1100 | strs.reset(); |
| 1101 | lengths.reset(); |
| 1102 | temps.reset(); |
| 1103 | |
| 1104 | append_string(segments.fHeader, &strs, &lengths); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1105 | GrStringBuilder precisionStr(GrGetGLSLShaderPrecisionDecl(gl.binding())); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1106 | append_string(precisionStr, &strs, &lengths); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1107 | append_decls(segments.fFSUnis, gl, &strs, &lengths, &temps); |
| 1108 | append_decls(segments.fFSInputs, gl, &strs, &lengths, &temps); |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 1109 | // We shouldn't have declared outputs on 1.10 |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1110 | GrAssert(k110_GrGLSLGeneration != gl.glslGeneration() || |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 1111 | segments.fFSOutputs.empty()); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1112 | append_decls(segments.fFSOutputs, gl, &strs, &lengths, &temps); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1113 | append_string(segments.fFSFunctions, &strs, &lengths); |
| 1114 | append_string(segments.fFSCode, &strs, &lengths); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1115 | |
| 1116 | #if PRINT_SHADERS |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1117 | print_shader(strs.count(), &strs[0], &lengths[0]); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1118 | GrPrintf("\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1119 | #endif |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1120 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1121 | programData->fFShaderID = |
| 1122 | CompileShader(gl, GR_GL_FRAGMENT_SHADER, strs.count(), |
| 1123 | &strs[0], &lengths[0]); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1124 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1125 | if (!programData->fFShaderID) { |
| 1126 | return false; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1127 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1128 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1129 | return true; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1132 | #define GL_CALL(X) GR_GL_CALL(gl.interface(), X) |
| 1133 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(gl.interface(), R, X) |
| 1134 | |
| 1135 | GrGLuint GrGLProgram::CompileShader(const GrGLContextInfo& gl, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1136 | GrGLenum type, |
| 1137 | int stringCnt, |
| 1138 | const char** strings, |
| 1139 | int* stringLengths) { |
tomhudson@google.com | 278cbb4 | 2011-06-30 19:37:01 +0000 | [diff] [blame] | 1140 | SK_TRACE_EVENT1("GrGLProgram::CompileShader", |
| 1141 | "stringCount", SkStringPrintf("%i", stringCnt).c_str()); |
| 1142 | |
bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 1143 | GrGLuint shader; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1144 | GL_CALL_RET(shader, CreateShader(type)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1145 | if (0 == shader) { |
| 1146 | return 0; |
| 1147 | } |
| 1148 | |
| 1149 | GrGLint compiled = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1150 | GL_CALL(ShaderSource(shader, stringCnt, strings, stringLengths)); |
| 1151 | GL_CALL(CompileShader(shader)); |
| 1152 | GL_CALL(GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1153 | |
| 1154 | if (!compiled) { |
| 1155 | GrGLint infoLen = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1156 | GL_CALL(GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 1157 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1158 | if (infoLen > 0) { |
bsalomon@google.com | 79afcaa | 2011-09-14 14:29:18 +0000 | [diff] [blame] | 1159 | // retrieve length even though we don't need it to workaround |
| 1160 | // bug in chrome cmd buffer param validation. |
| 1161 | GrGLsizei length = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1162 | GL_CALL(GetShaderInfoLog(shader, infoLen+1, |
| 1163 | &length, (char*)log.get())); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1164 | print_shader(stringCnt, strings, stringLengths); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1165 | GrPrintf("\n%s", log.get()); |
| 1166 | } |
| 1167 | GrAssert(!"Shader compilation failed!"); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1168 | GL_CALL(DeleteShader(shader)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1169 | return 0; |
| 1170 | } |
| 1171 | return shader; |
| 1172 | } |
| 1173 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1174 | bool GrGLProgram::bindOutputsAttribsAndLinkProgram( |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1175 | const GrGLContextInfo& gl, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1176 | GrStringBuilder texCoordAttrNames[], |
| 1177 | bool bindColorOut, |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 1178 | bool bindDualSrcOut, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1179 | CachedData* programData) const { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1180 | GL_CALL_RET(programData->fProgramID, CreateProgram()); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1181 | if (!programData->fProgramID) { |
| 1182 | return false; |
| 1183 | } |
| 1184 | const GrGLint& progID = programData->fProgramID; |
| 1185 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1186 | GL_CALL(AttachShader(progID, programData->fVShaderID)); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1187 | if (programData->fGShaderID) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1188 | GL_CALL(AttachShader(progID, programData->fGShaderID)); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1189 | } |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1190 | GL_CALL(AttachShader(progID, programData->fFShaderID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1191 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1192 | if (bindColorOut) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1193 | GL_CALL(BindFragDataLocation(programData->fProgramID, |
| 1194 | 0, declared_color_output_name())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1195 | } |
| 1196 | if (bindDualSrcOut) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1197 | GL_CALL(BindFragDataLocationIndexed(programData->fProgramID, |
| 1198 | 0, 1, dual_source_output_name())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1201 | // Bind the attrib locations to same values for all shaders |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1202 | GL_CALL(BindAttribLocation(progID, PositionAttributeIdx(), POS_ATTR_NAME)); |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1203 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1204 | if (texCoordAttrNames[t].size()) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1205 | GL_CALL(BindAttribLocation(progID, |
| 1206 | TexCoordAttributeIdx(t), |
| 1207 | texCoordAttrNames[t].c_str())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1208 | } |
| 1209 | } |
| 1210 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1211 | if (kSetAsAttribute == programData->fUniLocations.fViewMatrixUni) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1212 | GL_CALL(BindAttribLocation(progID, |
| 1213 | ViewMatrixAttributeIdx(), |
| 1214 | VIEW_MATRIX_NAME)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1217 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 72b4fcb | 2011-05-09 20:47:34 +0000 | [diff] [blame] | 1218 | const StageUniLocations& unis = programData->fUniLocations.fStages[s]; |
| 1219 | if (kSetAsAttribute == unis.fTextureMatrixUni) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1220 | GrStringBuilder matName; |
| 1221 | tex_matrix_name(s, &matName); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1222 | GL_CALL(BindAttribLocation(progID, |
| 1223 | TextureMatrixAttributeIdx(s), |
| 1224 | matName.c_str())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1225 | } |
| 1226 | } |
| 1227 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1228 | GL_CALL(BindAttribLocation(progID, ColorAttributeIdx(), COL_ATTR_NAME)); |
| 1229 | GL_CALL(BindAttribLocation(progID, CoverageAttributeIdx(), COV_ATTR_NAME)); |
| 1230 | GL_CALL(BindAttribLocation(progID, EdgeAttributeIdx(), EDGE_ATTR_NAME)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1231 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1232 | GL_CALL(LinkProgram(progID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1233 | |
| 1234 | GrGLint linked = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1235 | GL_CALL(GetProgramiv(progID, GR_GL_LINK_STATUS, &linked)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1236 | if (!linked) { |
| 1237 | GrGLint infoLen = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1238 | GL_CALL(GetProgramiv(progID, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 1239 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1240 | if (infoLen > 0) { |
bsalomon@google.com | 79afcaa | 2011-09-14 14:29:18 +0000 | [diff] [blame] | 1241 | // retrieve length even though we don't need it to workaround |
| 1242 | // bug in chrome cmd buffer param validation. |
| 1243 | GrGLsizei length = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1244 | GL_CALL(GetProgramInfoLog(progID, |
| 1245 | infoLen+1, |
| 1246 | &length, |
| 1247 | (char*)log.get())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1248 | GrPrintf((char*)log.get()); |
| 1249 | } |
| 1250 | GrAssert(!"Error linking program"); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1251 | GL_CALL(DeleteProgram(progID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1252 | programData->fProgramID = 0; |
| 1253 | return false; |
| 1254 | } |
| 1255 | return true; |
| 1256 | } |
| 1257 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1258 | void GrGLProgram::getUniformLocationsAndInitCache(const GrGLContextInfo& gl, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1259 | CachedData* programData) const { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1260 | const GrGLint& progID = programData->fProgramID; |
| 1261 | |
| 1262 | if (kUseUniform == programData->fUniLocations.fViewMatrixUni) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1263 | GL_CALL_RET(programData->fUniLocations.fViewMatrixUni, |
| 1264 | GetUniformLocation(progID, VIEW_MATRIX_NAME)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1265 | GrAssert(kUnusedUniform != programData->fUniLocations.fViewMatrixUni); |
| 1266 | } |
| 1267 | if (kUseUniform == programData->fUniLocations.fColorUni) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1268 | GL_CALL_RET(programData->fUniLocations.fColorUni, |
| 1269 | GetUniformLocation(progID, COL_UNI_NAME)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1270 | GrAssert(kUnusedUniform != programData->fUniLocations.fColorUni); |
| 1271 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 1272 | if (kUseUniform == programData->fUniLocations.fColorFilterUni) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1273 | GL_CALL_RET(programData->fUniLocations.fColorFilterUni, |
| 1274 | GetUniformLocation(progID, COL_FILTER_UNI_NAME)); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 1275 | GrAssert(kUnusedUniform != programData->fUniLocations.fColorFilterUni); |
| 1276 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1277 | |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 1278 | if (kUseUniform == programData->fUniLocations.fColorMatrixUni) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1279 | GL_CALL_RET(programData->fUniLocations.fColorMatrixUni, |
| 1280 | GetUniformLocation(progID, COL_MATRIX_UNI_NAME)); |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | if (kUseUniform == programData->fUniLocations.fColorMatrixVecUni) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1284 | GL_CALL_RET(programData->fUniLocations.fColorMatrixVecUni, |
| 1285 | GetUniformLocation(progID, COL_MATRIX_VEC_UNI_NAME)); |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 1286 | } |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 1287 | if (kUseUniform == programData->fUniLocations.fCoverageUni) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1288 | GL_CALL_RET(programData->fUniLocations.fCoverageUni, |
| 1289 | GetUniformLocation(progID, COV_UNI_NAME)); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 1290 | GrAssert(kUnusedUniform != programData->fUniLocations.fCoverageUni); |
| 1291 | } |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 1292 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 1293 | if (kUseUniform == programData->fUniLocations.fEdgesUni) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1294 | GL_CALL_RET(programData->fUniLocations.fEdgesUni, |
| 1295 | GetUniformLocation(progID, EDGES_UNI_NAME)); |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 1296 | GrAssert(kUnusedUniform != programData->fUniLocations.fEdgesUni); |
| 1297 | } else { |
| 1298 | programData->fUniLocations.fEdgesUni = kUnusedUniform; |
| 1299 | } |
| 1300 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1301 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1302 | StageUniLocations& locations = programData->fUniLocations.fStages[s]; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 1303 | if (fProgramDesc.fStages[s].isEnabled()) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1304 | if (kUseUniform == locations.fTextureMatrixUni) { |
| 1305 | GrStringBuilder texMName; |
| 1306 | tex_matrix_name(s, &texMName); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1307 | GL_CALL_RET(locations.fTextureMatrixUni, |
| 1308 | GetUniformLocation(progID, texMName.c_str())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1309 | GrAssert(kUnusedUniform != locations.fTextureMatrixUni); |
| 1310 | } |
| 1311 | |
| 1312 | if (kUseUniform == locations.fSamplerUni) { |
| 1313 | GrStringBuilder samplerName; |
| 1314 | sampler_name(s, &samplerName); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1315 | GL_CALL_RET(locations.fSamplerUni, |
| 1316 | GetUniformLocation(progID,samplerName.c_str())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1317 | GrAssert(kUnusedUniform != locations.fSamplerUni); |
| 1318 | } |
| 1319 | |
| 1320 | if (kUseUniform == locations.fNormalizedTexelSizeUni) { |
| 1321 | GrStringBuilder texelSizeName; |
| 1322 | normalized_texel_size_name(s, &texelSizeName); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1323 | GL_CALL_RET(locations.fNormalizedTexelSizeUni, |
| 1324 | GetUniformLocation(progID, texelSizeName.c_str())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1325 | GrAssert(kUnusedUniform != locations.fNormalizedTexelSizeUni); |
| 1326 | } |
| 1327 | |
| 1328 | if (kUseUniform == locations.fRadial2Uni) { |
| 1329 | GrStringBuilder radial2ParamName; |
| 1330 | radial2_param_name(s, &radial2ParamName); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1331 | GL_CALL_RET(locations.fRadial2Uni, |
| 1332 | GetUniformLocation(progID, radial2ParamName.c_str())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1333 | GrAssert(kUnusedUniform != locations.fRadial2Uni); |
| 1334 | } |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1335 | |
| 1336 | if (kUseUniform == locations.fTexDomUni) { |
| 1337 | GrStringBuilder texDomName; |
| 1338 | tex_domain_name(s, &texDomName); |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1339 | GL_CALL_RET(locations.fTexDomUni, |
| 1340 | GetUniformLocation(progID, texDomName.c_str())); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1341 | GrAssert(kUnusedUniform != locations.fTexDomUni); |
| 1342 | } |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 1343 | |
| 1344 | GrStringBuilder kernelName, imageIncrementName; |
| 1345 | convolve_param_names(s, &kernelName, &imageIncrementName); |
| 1346 | if (kUseUniform == locations.fKernelUni) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1347 | GL_CALL_RET(locations.fKernelUni, |
| 1348 | GetUniformLocation(progID, kernelName.c_str())); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 1349 | GrAssert(kUnusedUniform != locations.fKernelUni); |
| 1350 | } |
| 1351 | |
| 1352 | if (kUseUniform == locations.fImageIncrementUni) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1353 | GL_CALL_RET(locations.fImageIncrementUni, |
| 1354 | GetUniformLocation(progID, |
| 1355 | imageIncrementName.c_str())); |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 1356 | GrAssert(kUnusedUniform != locations.fImageIncrementUni); |
| 1357 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1358 | } |
| 1359 | } |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1360 | GL_CALL(UseProgram(progID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1361 | |
| 1362 | // init sampler unis and set bogus values for state tracking |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1363 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1364 | if (kUnusedUniform != programData->fUniLocations.fStages[s].fSamplerUni) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1365 | GL_CALL(Uniform1i(programData->fUniLocations.fStages[s].fSamplerUni, s)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1366 | } |
| 1367 | programData->fTextureMatrices[s] = GrMatrix::InvalidMatrix(); |
| 1368 | programData->fRadial2CenterX1[s] = GR_ScalarMax; |
| 1369 | programData->fRadial2Radius0[s] = -GR_ScalarMax; |
| 1370 | programData->fTextureWidth[s] = -1; |
| 1371 | programData->fTextureHeight[s] = -1; |
| 1372 | } |
| 1373 | programData->fViewMatrix = GrMatrix::InvalidMatrix(); |
| 1374 | programData->fColor = GrColor_ILLEGAL; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 1375 | programData->fColorFilterColor = GrColor_ILLEGAL; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1378 | //============================================================================ |
| 1379 | // Stage code generation |
| 1380 | //============================================================================ |
| 1381 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1382 | namespace { |
| 1383 | |
| 1384 | bool isRadialMapping(GrGLProgram::StageDesc::CoordMapping mapping) { |
| 1385 | return |
| 1386 | (GrGLProgram::StageDesc::kRadial2Gradient_CoordMapping == mapping || |
| 1387 | GrGLProgram::StageDesc::kRadial2GradientDegenerate_CoordMapping == mapping); |
| 1388 | } |
| 1389 | |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1390 | GrGLShaderVar* genRadialVS(int stageNum, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1391 | ShaderCodeSegments* segments, |
| 1392 | GrGLProgram::StageUniLocations* locations, |
| 1393 | const char** radial2VaryingVSName, |
| 1394 | const char** radial2VaryingFSName, |
| 1395 | const char* varyingVSName, |
| 1396 | int varyingDims, int coordDims) { |
| 1397 | |
| 1398 | GrGLShaderVar* radial2FSParams = &segments->fFSUnis.push_back(); |
| 1399 | radial2FSParams->setType(GrGLShaderVar::kFloat_Type); |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 1400 | radial2FSParams->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1401 | radial2FSParams->setArrayCount(6); |
| 1402 | radial2_param_name(stageNum, radial2FSParams->accessName()); |
| 1403 | segments->fVSUnis.push_back(*radial2FSParams).setEmitPrecision(true); |
| 1404 | |
| 1405 | locations->fRadial2Uni = kUseUniform; |
| 1406 | |
| 1407 | // for radial grads without perspective we can pass the linear |
| 1408 | // part of the quadratic as a varying. |
| 1409 | if (varyingDims == coordDims) { |
| 1410 | GrAssert(2 == coordDims); |
| 1411 | append_varying(GrGLShaderVar::kFloat_Type, |
| 1412 | "Radial2BCoeff", |
| 1413 | stageNum, |
| 1414 | segments, |
| 1415 | radial2VaryingVSName, |
| 1416 | radial2VaryingFSName); |
| 1417 | |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1418 | GrStringBuilder radial2p2; |
| 1419 | GrStringBuilder radial2p3; |
| 1420 | radial2FSParams->appendArrayAccess(2, &radial2p2); |
| 1421 | radial2FSParams->appendArrayAccess(3, &radial2p3); |
| 1422 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1423 | // r2Var = 2 * (r2Parm[2] * varCoord.x - r2Param[3]) |
| 1424 | const char* r2ParamName = radial2FSParams->getName().c_str(); |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1425 | segments->fVSCode.appendf("\t%s = 2.0 *(%s * %s.x - %s);\n", |
| 1426 | *radial2VaryingVSName, radial2p2.c_str(), |
| 1427 | varyingVSName, radial2p3.c_str()); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1430 | return radial2FSParams; |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | bool genRadial2GradientCoordMapping(int stageNum, |
| 1434 | ShaderCodeSegments* segments, |
| 1435 | const char* radial2VaryingFSName, |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1436 | GrGLShaderVar* radial2Params, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1437 | GrStringBuilder& sampleCoords, |
| 1438 | GrStringBuilder& fsCoordName, |
| 1439 | int varyingDims, |
| 1440 | int coordDims) { |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1441 | GrStringBuilder cName("c"); |
| 1442 | GrStringBuilder ac4Name("ac4"); |
| 1443 | GrStringBuilder rootName("root"); |
| 1444 | |
| 1445 | cName.appendS32(stageNum); |
| 1446 | ac4Name.appendS32(stageNum); |
| 1447 | rootName.appendS32(stageNum); |
| 1448 | |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1449 | GrStringBuilder radial2p0; |
| 1450 | GrStringBuilder radial2p1; |
| 1451 | GrStringBuilder radial2p2; |
| 1452 | GrStringBuilder radial2p3; |
| 1453 | GrStringBuilder radial2p4; |
| 1454 | GrStringBuilder radial2p5; |
| 1455 | radial2Params->appendArrayAccess(0, &radial2p0); |
| 1456 | radial2Params->appendArrayAccess(1, &radial2p1); |
| 1457 | radial2Params->appendArrayAccess(2, &radial2p2); |
| 1458 | radial2Params->appendArrayAccess(3, &radial2p3); |
| 1459 | radial2Params->appendArrayAccess(4, &radial2p4); |
| 1460 | radial2Params->appendArrayAccess(5, &radial2p5); |
| 1461 | |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1462 | // if we were able to interpolate the linear component bVar is the varying |
| 1463 | // otherwise compute it |
| 1464 | GrStringBuilder bVar; |
| 1465 | if (coordDims == varyingDims) { |
| 1466 | bVar = radial2VaryingFSName; |
| 1467 | GrAssert(2 == varyingDims); |
| 1468 | } else { |
| 1469 | GrAssert(3 == varyingDims); |
| 1470 | bVar = "b"; |
| 1471 | bVar.appendS32(stageNum); |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1472 | segments->fFSCode.appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n", |
| 1473 | bVar.c_str(), radial2p2.c_str(), |
| 1474 | fsCoordName.c_str(), radial2p3.c_str()); |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | // c = (x^2)+(y^2) - params[4] |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1478 | segments->fFSCode.appendf("\tfloat %s = dot(%s, %s) - %s;\n", |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1479 | cName.c_str(), fsCoordName.c_str(), |
| 1480 | fsCoordName.c_str(), |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1481 | radial2p4.c_str()); |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1482 | // ac4 = 4.0 * params[0] * c |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1483 | segments->fFSCode.appendf("\tfloat %s = %s * 4.0 * %s;\n", |
| 1484 | ac4Name.c_str(), radial2p0.c_str(), |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1485 | cName.c_str()); |
| 1486 | |
| 1487 | // root = sqrt(b^2-4ac) |
| 1488 | // (abs to avoid exception due to fp precision) |
| 1489 | segments->fFSCode.appendf("\tfloat %s = sqrt(abs(%s*%s - %s));\n", |
| 1490 | rootName.c_str(), bVar.c_str(), bVar.c_str(), |
| 1491 | ac4Name.c_str()); |
| 1492 | |
| 1493 | // x coord is: (-b + params[5] * sqrt(b^2-4ac)) * params[1] |
| 1494 | // y coord is 0.5 (texture is effectively 1D) |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1495 | sampleCoords.printf("vec2((-%s + %s * %s) * %s, 0.5)", |
| 1496 | bVar.c_str(), radial2p5.c_str(), |
| 1497 | rootName.c_str(), radial2p1.c_str()); |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1498 | return true; |
| 1499 | } |
| 1500 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1501 | bool genRadial2GradientDegenerateCoordMapping(int stageNum, |
| 1502 | ShaderCodeSegments* segments, |
| 1503 | const char* radial2VaryingFSName, |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1504 | GrGLShaderVar* radial2Params, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1505 | GrStringBuilder& sampleCoords, |
| 1506 | GrStringBuilder& fsCoordName, |
| 1507 | int varyingDims, |
| 1508 | int coordDims) { |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1509 | GrStringBuilder cName("c"); |
| 1510 | |
| 1511 | cName.appendS32(stageNum); |
| 1512 | |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1513 | GrStringBuilder radial2p2; |
| 1514 | GrStringBuilder radial2p3; |
| 1515 | GrStringBuilder radial2p4; |
| 1516 | radial2Params->appendArrayAccess(2, &radial2p2); |
| 1517 | radial2Params->appendArrayAccess(3, &radial2p3); |
| 1518 | radial2Params->appendArrayAccess(4, &radial2p4); |
| 1519 | |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1520 | // if we were able to interpolate the linear component bVar is the varying |
| 1521 | // otherwise compute it |
| 1522 | GrStringBuilder bVar; |
| 1523 | if (coordDims == varyingDims) { |
| 1524 | bVar = radial2VaryingFSName; |
| 1525 | GrAssert(2 == varyingDims); |
| 1526 | } else { |
| 1527 | GrAssert(3 == varyingDims); |
| 1528 | bVar = "b"; |
| 1529 | bVar.appendS32(stageNum); |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1530 | segments->fFSCode.appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n", |
| 1531 | bVar.c_str(), radial2p2.c_str(), |
| 1532 | fsCoordName.c_str(), radial2p3.c_str()); |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | // c = (x^2)+(y^2) - params[4] |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1536 | segments->fFSCode.appendf("\tfloat %s = dot(%s, %s) - %s;\n", |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1537 | cName.c_str(), fsCoordName.c_str(), |
| 1538 | fsCoordName.c_str(), |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1539 | radial2p4.c_str()); |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1540 | |
| 1541 | // x coord is: -c/b |
| 1542 | // y coord is 0.5 (texture is effectively 1D) |
| 1543 | sampleCoords.printf("vec2((-%s / %s), 0.5)", cName.c_str(), bVar.c_str()); |
| 1544 | return true; |
| 1545 | } |
| 1546 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1547 | void gen2x2FS(int stageNum, |
| 1548 | ShaderCodeSegments* segments, |
| 1549 | GrGLProgram::StageUniLocations* locations, |
| 1550 | GrStringBuilder* sampleCoords, |
| 1551 | const char* samplerName, |
| 1552 | const char* texelSizeName, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1553 | const char* swizzle, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1554 | const char* fsOutColor, |
| 1555 | GrStringBuilder& texFunc, |
| 1556 | GrStringBuilder& modulate, |
| 1557 | bool complexCoord, |
| 1558 | int coordDims) { |
| 1559 | locations->fNormalizedTexelSizeUni = kUseUniform; |
| 1560 | if (complexCoord) { |
| 1561 | // assign the coord to a var rather than compute 4x. |
| 1562 | GrStringBuilder coordVar("tCoord"); |
| 1563 | coordVar.appendS32(stageNum); |
| 1564 | segments->fFSCode.appendf("\t%s %s = %s;\n", |
| 1565 | float_vector_type_str(coordDims), |
| 1566 | coordVar.c_str(), sampleCoords->c_str()); |
| 1567 | *sampleCoords = coordVar; |
| 1568 | } |
| 1569 | GrAssert(2 == coordDims); |
| 1570 | GrStringBuilder accumVar("accum"); |
| 1571 | accumVar.appendS32(stageNum); |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1572 | 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); |
| 1573 | 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); |
| 1574 | 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); |
| 1575 | 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] | 1576 | segments->fFSCode.appendf("\t%s = .25 * %s%s;\n", fsOutColor, accumVar.c_str(), modulate.c_str()); |
| 1577 | |
| 1578 | } |
| 1579 | |
| 1580 | void genConvolutionVS(int stageNum, |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 1581 | const StageDesc& desc, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1582 | ShaderCodeSegments* segments, |
| 1583 | GrGLProgram::StageUniLocations* locations, |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1584 | GrGLShaderVar** kernel, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1585 | const char** imageIncrementName, |
| 1586 | const char* varyingVSName) { |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1587 | //GrGLShaderVar* kernel = &segments->fFSUnis.push_back(); |
| 1588 | *kernel = &segments->fFSUnis.push_back(); |
| 1589 | (*kernel)->setType(GrGLShaderVar::kFloat_Type); |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 1590 | (*kernel)->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier); |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1591 | (*kernel)->setArrayCount(desc.fKernelWidth); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1592 | GrGLShaderVar* imgInc = &segments->fFSUnis.push_back(); |
| 1593 | imgInc->setType(GrGLShaderVar::kVec2f_Type); |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 1594 | imgInc->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1595 | |
| 1596 | convolve_param_names(stageNum, |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1597 | (*kernel)->accessName(), |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1598 | imgInc->accessName()); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1599 | *imageIncrementName = imgInc->getName().c_str(); |
| 1600 | |
| 1601 | // need image increment in both VS and FS |
| 1602 | segments->fVSUnis.push_back(*imgInc).setEmitPrecision(true); |
| 1603 | |
| 1604 | locations->fKernelUni = kUseUniform; |
| 1605 | locations->fImageIncrementUni = kUseUniform; |
| 1606 | float scale = (desc.fKernelWidth - 1) * 0.5f; |
| 1607 | segments->fVSCode.appendf("\t%s -= vec2(%g, %g) * %s;\n", |
| 1608 | varyingVSName, scale, scale, |
| 1609 | *imageIncrementName); |
| 1610 | } |
| 1611 | |
| 1612 | void genConvolutionFS(int stageNum, |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 1613 | const StageDesc& desc, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1614 | ShaderCodeSegments* segments, |
| 1615 | const char* samplerName, |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1616 | GrGLShaderVar* kernel, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1617 | const char* swizzle, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1618 | const char* imageIncrementName, |
| 1619 | const char* fsOutColor, |
| 1620 | GrStringBuilder& sampleCoords, |
| 1621 | GrStringBuilder& texFunc, |
| 1622 | GrStringBuilder& modulate) { |
| 1623 | GrStringBuilder sumVar("sum"); |
| 1624 | sumVar.appendS32(stageNum); |
| 1625 | GrStringBuilder coordVar("coord"); |
| 1626 | coordVar.appendS32(stageNum); |
| 1627 | |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1628 | GrStringBuilder kernelIndex; |
| 1629 | kernel->appendArrayAccess("i", &kernelIndex); |
| 1630 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1631 | segments->fFSCode.appendf("\tvec4 %s = vec4(0, 0, 0, 0);\n", |
| 1632 | sumVar.c_str()); |
| 1633 | segments->fFSCode.appendf("\tvec2 %s = %s;\n", |
| 1634 | coordVar.c_str(), |
| 1635 | sampleCoords.c_str()); |
| 1636 | segments->fFSCode.appendf("\tfor (int i = 0; i < %d; i++) {\n", |
| 1637 | desc.fKernelWidth); |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1638 | 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] | 1639 | sumVar.c_str(), texFunc.c_str(), |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1640 | samplerName, coordVar.c_str(), swizzle, |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1641 | kernelIndex.c_str()); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1642 | segments->fFSCode.appendf("\t\t%s += %s;\n", |
| 1643 | coordVar.c_str(), |
| 1644 | imageIncrementName); |
| 1645 | segments->fFSCode.appendf("\t}\n"); |
| 1646 | segments->fFSCode.appendf("\t%s = %s%s;\n", fsOutColor, |
| 1647 | sumVar.c_str(), modulate.c_str()); |
| 1648 | } |
| 1649 | |
| 1650 | } |
| 1651 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame^] | 1652 | void GrGLProgram::genStageCode(const GrGLContextInfo& gl, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 1653 | int stageNum, |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1654 | const GrGLProgram::StageDesc& desc, |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1655 | const char* fsInColor, // NULL means no incoming color |
| 1656 | const char* fsOutColor, |
| 1657 | const char* vsInCoord, |
| 1658 | ShaderCodeSegments* segments, |
| 1659 | StageUniLocations* locations) const { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1660 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1661 | GrAssert(stageNum >= 0 && stageNum <= GrDrawState::kNumStages); |
| 1662 | GrAssert((desc.fInConfigFlags & StageDesc::kInConfigBitMask) == |
| 1663 | desc.fInConfigFlags); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1664 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1665 | // First decide how many coords are needed to access the texture |
| 1666 | // Right now it's always 2 but we could start using 1D textures for |
| 1667 | // gradients. |
| 1668 | static const int coordDims = 2; |
| 1669 | int varyingDims; |
| 1670 | /// Vertex Shader Stuff |
| 1671 | |
| 1672 | // decide whether we need a matrix to transform texture coords |
| 1673 | // and whether the varying needs a perspective coord. |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1674 | const char* matName = NULL; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1675 | if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1676 | varyingDims = coordDims; |
| 1677 | } else { |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1678 | GrGLShaderVar* mat; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 1679 | #if GR_GL_ATTRIBUTE_MATRICES |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1680 | mat = &segments->fVSAttrs.push_back(); |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 1681 | mat->setTypeModifier(GrGLShaderVar::kAttribute_TypeModifier); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1682 | locations->fTextureMatrixUni = kSetAsAttribute; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1683 | #else |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1684 | mat = &segments->fVSUnis.push_back(); |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 1685 | mat->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1686 | locations->fTextureMatrixUni = kUseUniform; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1687 | #endif |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1688 | tex_matrix_name(stageNum, mat->accessName()); |
| 1689 | mat->setType(GrGLShaderVar::kMat33f_Type); |
| 1690 | matName = mat->getName().c_str(); |
| 1691 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1692 | if (desc.fOptFlags & StageDesc::kNoPerspective_OptFlagBit) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1693 | varyingDims = coordDims; |
| 1694 | } else { |
| 1695 | varyingDims = coordDims + 1; |
| 1696 | } |
| 1697 | } |
| 1698 | |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 1699 | segments->fFSUnis.push_back().set(GrGLShaderVar::kSampler2D_Type, |
| 1700 | GrGLShaderVar::kUniform_TypeModifier, ""); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1701 | sampler_name(stageNum, segments->fFSUnis.back().accessName()); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1702 | locations->fSamplerUni = kUseUniform; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1703 | const char* samplerName = segments->fFSUnis.back().getName().c_str(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1704 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1705 | const char* texelSizeName = NULL; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1706 | if (StageDesc::k2x2_FetchMode == desc.fFetchMode) { |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 1707 | segments->fFSUnis.push_back().set(GrGLShaderVar::kVec2f_Type, |
| 1708 | GrGLShaderVar::kUniform_TypeModifier, ""); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1709 | normalized_texel_size_name(stageNum, segments->fFSUnis.back().accessName()); |
| 1710 | texelSizeName = segments->fFSUnis.back().getName().c_str(); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1713 | const char *varyingVSName, *varyingFSName; |
| 1714 | append_varying(float_vector_type(varyingDims), |
| 1715 | "Stage", |
| 1716 | stageNum, |
| 1717 | segments, |
| 1718 | &varyingVSName, |
| 1719 | &varyingFSName); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1720 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1721 | if (!matName) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1722 | GrAssert(varyingDims == coordDims); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1723 | segments->fVSCode.appendf("\t%s = %s;\n", varyingVSName, vsInCoord); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1724 | } else { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1725 | // varying = texMatrix * texCoord |
| 1726 | segments->fVSCode.appendf("\t%s = (%s * vec3(%s, 1))%s;\n", |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1727 | varyingVSName, matName, vsInCoord, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1728 | vector_all_coords(varyingDims)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1731 | GrGLShaderVar* radial2Params = NULL; |
| 1732 | const char* radial2VaryingVSName = NULL; |
| 1733 | const char* radial2VaryingFSName = NULL; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1734 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1735 | if (isRadialMapping((StageDesc::CoordMapping) desc.fCoordMapping)) { |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1736 | radial2Params = genRadialVS(stageNum, segments, |
| 1737 | locations, |
| 1738 | &radial2VaryingVSName, |
| 1739 | &radial2VaryingFSName, |
| 1740 | varyingVSName, |
| 1741 | varyingDims, coordDims); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1744 | GrGLShaderVar* kernel = NULL; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1745 | const char* imageIncrementName = NULL; |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 1746 | if (StageDesc::kConvolution_FetchMode == desc.fFetchMode) { |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1747 | genConvolutionVS(stageNum, desc, segments, locations, |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1748 | &kernel, &imageIncrementName, varyingVSName); |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 1749 | } |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 1750 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1751 | /// Fragment Shader Stuff |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1752 | GrStringBuilder fsCoordName; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1753 | // function used to access the shader, may be made projective |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1754 | GrStringBuilder texFunc("texture2D"); |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1755 | if (desc.fOptFlags & (StageDesc::kIdentityMatrix_OptFlagBit | |
| 1756 | StageDesc::kNoPerspective_OptFlagBit)) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1757 | GrAssert(varyingDims == coordDims); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1758 | fsCoordName = varyingFSName; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1759 | } else { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1760 | // 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] | 1761 | // our final tex coords then when in perspective we have to |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1762 | // do an explicit divide. Otherwise, we can use a Proj func. |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1763 | if (StageDesc::kIdentity_CoordMapping == desc.fCoordMapping && |
| 1764 | StageDesc::kSingle_FetchMode == desc.fFetchMode) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1765 | texFunc.append("Proj"); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1766 | fsCoordName = varyingFSName; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1767 | } else { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1768 | fsCoordName = "inCoord"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 1769 | fsCoordName.appendS32(stageNum); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1770 | segments->fFSCode.appendf("\t%s %s = %s%s / %s%s;\n", |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1771 | GrGLShaderVar::TypeString(float_vector_type(coordDims)), |
| 1772 | fsCoordName.c_str(), |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1773 | varyingFSName, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1774 | vector_nonhomog_coords(varyingDims), |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1775 | varyingFSName, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1776 | vector_homog_coord(varyingDims)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1777 | } |
| 1778 | } |
| 1779 | |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 1780 | GrStringBuilder sampleCoords; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1781 | bool complexCoord = false; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1782 | switch (desc.fCoordMapping) { |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1783 | case StageDesc::kIdentity_CoordMapping: |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1784 | sampleCoords = fsCoordName; |
| 1785 | break; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1786 | case StageDesc::kSweepGradient_CoordMapping: |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1787 | 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] | 1788 | complexCoord = true; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1789 | break; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1790 | case StageDesc::kRadialGradient_CoordMapping: |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1791 | sampleCoords.printf("vec2(length(%s.xy), 0.5)", fsCoordName.c_str()); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1792 | complexCoord = true; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1793 | break; |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1794 | case StageDesc::kRadial2Gradient_CoordMapping: |
| 1795 | complexCoord = genRadial2GradientCoordMapping( |
| 1796 | stageNum, segments, |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1797 | radial2VaryingFSName, radial2Params, |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1798 | sampleCoords, fsCoordName, |
| 1799 | varyingDims, coordDims); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1800 | |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1801 | break; |
| 1802 | case StageDesc::kRadial2GradientDegenerate_CoordMapping: |
| 1803 | complexCoord = genRadial2GradientDegenerateCoordMapping( |
| 1804 | stageNum, segments, |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1805 | radial2VaryingFSName, radial2Params, |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1806 | sampleCoords, fsCoordName, |
| 1807 | varyingDims, coordDims); |
| 1808 | break; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1809 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1810 | }; |
| 1811 | |
tomhudson@google.com | f74ad8c | 2011-11-09 22:15:08 +0000 | [diff] [blame] | 1812 | const char* swizzle = ""; |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1813 | if (desc.fInConfigFlags & StageDesc::kSwapRAndB_InConfigFlag) { |
| 1814 | GrAssert(!(desc.fInConfigFlags & StageDesc::kSmearAlpha_InConfigFlag)); |
| 1815 | swizzle = ".bgra"; |
| 1816 | } else if (desc.fInConfigFlags & StageDesc::kSmearAlpha_InConfigFlag) { |
| 1817 | GrAssert(!(desc.fInConfigFlags & |
| 1818 | StageDesc::kMulRGBByAlpha_InConfigFlag)); |
| 1819 | swizzle = ".aaaa"; |
| 1820 | } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1821 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1822 | GrStringBuilder modulate; |
| 1823 | if (NULL != fsInColor) { |
| 1824 | modulate.printf(" * %s", fsInColor); |
| 1825 | } |
| 1826 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 1827 | if (desc.fOptFlags & |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1828 | StageDesc::kCustomTextureDomain_OptFlagBit) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1829 | GrStringBuilder texDomainName; |
| 1830 | tex_domain_name(stageNum, &texDomainName); |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 1831 | segments->fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type, |
| 1832 | GrGLShaderVar::kUniform_TypeModifier, texDomainName); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1833 | GrStringBuilder coordVar("clampCoord"); |
| 1834 | 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] | 1835 | float_vector_type_str(coordDims), |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 1836 | coordVar.c_str(), |
| 1837 | sampleCoords.c_str(), |
| 1838 | texDomainName.c_str(), |
| 1839 | texDomainName.c_str()); |
| 1840 | sampleCoords = coordVar; |
| 1841 | locations->fTexDomUni = kUseUniform; |
| 1842 | } |
| 1843 | |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1844 | switch (desc.fFetchMode) { |
| 1845 | case StageDesc::k2x2_FetchMode: |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1846 | GrAssert(!(desc.fInConfigFlags & |
| 1847 | StageDesc::kMulRGBByAlpha_InConfigFlag)); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1848 | gen2x2FS(stageNum, segments, locations, &sampleCoords, |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 1849 | samplerName, texelSizeName, swizzle, fsOutColor, |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1850 | texFunc, modulate, complexCoord, coordDims); |
| 1851 | break; |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 1852 | case StageDesc::kConvolution_FetchMode: |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1853 | GrAssert(!(desc.fInConfigFlags & |
| 1854 | StageDesc::kMulRGBByAlpha_InConfigFlag)); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 1855 | genConvolutionFS(stageNum, desc, segments, |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 1856 | samplerName, kernel, swizzle, imageIncrementName, fsOutColor, |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1857 | sampleCoords, texFunc, modulate); |
| 1858 | break; |
| 1859 | default: |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1860 | if (desc.fInConfigFlags & StageDesc::kMulRGBByAlpha_InConfigFlag) { |
| 1861 | GrAssert(!(desc.fInConfigFlags & |
| 1862 | StageDesc::kSmearAlpha_InConfigFlag)); |
| 1863 | segments->fFSCode.appendf("\t%s = %s(%s, %s)%s;\n", |
| 1864 | fsOutColor, texFunc.c_str(), |
| 1865 | samplerName, sampleCoords.c_str(), |
| 1866 | swizzle); |
| 1867 | segments->fFSCode.appendf("\t%s = vec4(%s.rgb*%s.a,%s.a)%s;\n", |
| 1868 | fsOutColor, fsOutColor, fsOutColor, |
| 1869 | fsOutColor, modulate.c_str()); |
| 1870 | } else { |
| 1871 | segments->fFSCode.appendf("\t%s = %s(%s, %s)%s%s;\n", |
| 1872 | fsOutColor, texFunc.c_str(), |
| 1873 | samplerName, sampleCoords.c_str(), |
| 1874 | swizzle, modulate.c_str()); |
| 1875 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1876 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1877 | } |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1878 | |
tomhudson@google.com | 5960d00 | 2011-10-19 20:21:48 +0000 | [diff] [blame] | 1879 | |