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