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