junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "GrGLProgram.h" |
| 9 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 10 | #include "GrAllocator.h" |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 11 | #include "GrCustomStage.h" |
| 12 | #include "GrGLProgramStage.h" |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 13 | #include "gl/GrGLShaderBuilder.h" |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 14 | #include "GrGLShaderVar.h" |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 15 | #include "GrProgramStageFactory.h" |
tomhudson@google.com | 0c8d93a | 2011-07-01 17:08:26 +0000 | [diff] [blame] | 16 | #include "SkTrace.h" |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 17 | #include "SkXfermode.h" |
| 18 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 19 | SK_DEFINE_INST_COUNT(GrGLProgram) |
| 20 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 21 | #define GL_CALL(X) GR_GL_CALL(fContextInfo.interface(), X) |
| 22 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fContextInfo.interface(), R, X) |
| 23 | |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 24 | #define PRINT_SHADERS 0 |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 25 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 26 | typedef GrGLProgram::Desc::StageDesc StageDesc; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 27 | |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 28 | #define VIEW_MATRIX_NAME "uViewM" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 29 | |
| 30 | #define POS_ATTR_NAME "aPosition" |
| 31 | #define COL_ATTR_NAME "aColor" |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 32 | #define COV_ATTR_NAME "aCoverage" |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 33 | #define EDGE_ATTR_NAME "aEdge" |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 34 | #define COL_UNI_NAME "uColor" |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 35 | #define COV_UNI_NAME "uCoverage" |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 36 | #define COL_FILTER_UNI_NAME "uColorFilter" |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 37 | #define COL_MATRIX_UNI_NAME "uColorMatrix" |
| 38 | #define COL_MATRIX_VEC_UNI_NAME "uColorMatrixVec" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 39 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 40 | namespace { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 41 | inline void tex_attr_name(int coordIdx, SkString* s) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 42 | *s = "aTexCoord"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 43 | s->appendS32(coordIdx); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 44 | } |
| 45 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 46 | inline const char* float_vector_type_str(int count) { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 47 | return GrGLShaderVar::TypeString(GrSLFloatVectorType(count)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 48 | } |
| 49 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 50 | inline const char* vector_all_coords(int count) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 51 | static const char* ALL[] = {"ERROR", "", ".xy", ".xyz", ".xyzw"}; |
| 52 | GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ALL)); |
| 53 | return ALL[count]; |
| 54 | } |
| 55 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 56 | inline const char* all_ones_vec(int count) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 57 | static const char* ONESVEC[] = {"ERROR", "1.0", "vec2(1,1)", |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 58 | "vec3(1,1,1)", "vec4(1,1,1,1)"}; |
| 59 | GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ONESVEC)); |
| 60 | return ONESVEC[count]; |
| 61 | } |
| 62 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 63 | inline const char* all_zeros_vec(int count) { |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 64 | static const char* ZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)", |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 65 | "vec3(0,0,0)", "vec4(0,0,0,0)"}; |
| 66 | GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ZEROSVEC)); |
| 67 | return ZEROSVEC[count]; |
| 68 | } |
| 69 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 70 | inline const char* declared_color_output_name() { return "fsColorOut"; } |
| 71 | inline const char* dual_source_output_name() { return "dualSourceOut"; } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 72 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 73 | inline void tex_matrix_name(int stage, SkString* s) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 74 | *s = "uTexM"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 75 | s->appendS32(stage); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 76 | } |
| 77 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 78 | inline void sampler_name(int stage, SkString* s) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 79 | *s = "uSampler"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 80 | s->appendS32(stage); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 81 | } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 82 | } |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 83 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 84 | GrGLProgram* GrGLProgram::Create(const GrGLContextInfo& gl, |
| 85 | const Desc& desc, |
| 86 | GrCustomStage** customStages) { |
| 87 | GrGLProgram* program = SkNEW_ARGS(GrGLProgram, (gl, desc, customStages)); |
| 88 | if (!program->succeeded()) { |
| 89 | delete program; |
| 90 | program = NULL; |
| 91 | } |
| 92 | return program; |
| 93 | } |
| 94 | |
| 95 | GrGLProgram::GrGLProgram(const GrGLContextInfo& gl, |
| 96 | const Desc& desc, |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 97 | GrCustomStage** customStages) |
| 98 | : fContextInfo(gl) |
| 99 | , fUniformManager(gl) { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 100 | fDesc = desc; |
| 101 | fVShaderID = 0; |
| 102 | fGShaderID = 0; |
| 103 | fFShaderID = 0; |
| 104 | fProgramID = 0; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 105 | |
| 106 | fViewMatrix = GrMatrix::InvalidMatrix(); |
| 107 | fViewportSize.set(-1, -1); |
| 108 | fColor = GrColor_ILLEGAL; |
| 109 | fColorFilterColor = GrColor_ILLEGAL; |
| 110 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 111 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 112 | fProgramStage[s] = NULL; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 113 | fTextureMatrices[s] = GrMatrix::InvalidMatrix(); |
| 114 | // this is arbitrary, just initialize to something |
| 115 | fTextureOrientation[s] = GrGLTexture::kBottomUp_Orientation; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 116 | } |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 117 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 118 | this->genProgram(customStages); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | GrGLProgram::~GrGLProgram() { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 122 | if (fVShaderID) { |
| 123 | GL_CALL(DeleteShader(fVShaderID)); |
| 124 | } |
| 125 | if (fGShaderID) { |
| 126 | GL_CALL(DeleteShader(fGShaderID)); |
| 127 | } |
| 128 | if (fFShaderID) { |
| 129 | GL_CALL(DeleteShader(fFShaderID)); |
| 130 | } |
| 131 | if (fProgramID) { |
| 132 | GL_CALL(DeleteProgram(fProgramID)); |
| 133 | } |
| 134 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 135 | for (int i = 0; i < GrDrawState::kNumStages; ++i) { |
| 136 | delete fProgramStage[i]; |
| 137 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 138 | } |
| 139 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 140 | void GrGLProgram::abandon() { |
| 141 | fVShaderID = 0; |
| 142 | fGShaderID = 0; |
| 143 | fFShaderID = 0; |
| 144 | fProgramID = 0; |
| 145 | } |
| 146 | |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 147 | void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 148 | GrBlendCoeff* dstCoeff) const { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 149 | switch (fDesc.fDualSrcOutput) { |
| 150 | case Desc::kNone_DualSrcOutput: |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 151 | break; |
| 152 | // the prog will write a coverage value to the secondary |
| 153 | // output and the dst is blended by one minus that value. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 154 | case Desc::kCoverage_DualSrcOutput: |
| 155 | case Desc::kCoverageISA_DualSrcOutput: |
| 156 | case Desc::kCoverageISC_DualSrcOutput: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 157 | *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 158 | break; |
| 159 | default: |
| 160 | GrCrash("Unexpected dual source blend output"); |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 165 | // assigns modulation of two vars to an output var |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 166 | // vars can be vec4s or floats (or one of each) |
| 167 | // result is always vec4 |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 168 | // if either var is "" then assign to the other var |
| 169 | // if both are "" then assign all ones |
| 170 | static inline void modulate_helper(const char* outputVar, |
| 171 | const char* var0, |
| 172 | const char* var1, |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 173 | SkString* code) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 174 | GrAssert(NULL != outputVar); |
| 175 | GrAssert(NULL != var0); |
| 176 | GrAssert(NULL != var1); |
| 177 | GrAssert(NULL != code); |
| 178 | |
| 179 | bool has0 = '\0' != *var0; |
| 180 | bool has1 = '\0' != *var1; |
| 181 | |
| 182 | if (!has0 && !has1) { |
| 183 | code->appendf("\t%s = %s;\n", outputVar, all_ones_vec(4)); |
| 184 | } else if (!has0) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 185 | code->appendf("\t%s = vec4(%s);\n", outputVar, var1); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 186 | } else if (!has1) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 187 | code->appendf("\t%s = vec4(%s);\n", outputVar, var0); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 188 | } else { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 189 | code->appendf("\t%s = vec4(%s * %s);\n", outputVar, var0, var1); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | // assigns addition of two vars to an output var |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 194 | // vars can be vec4s or floats (or one of each) |
| 195 | // result is always vec4 |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 196 | // if either var is "" then assign to the other var |
| 197 | // if both are "" then assign all zeros |
| 198 | static inline void add_helper(const char* outputVar, |
| 199 | const char* var0, |
| 200 | const char* var1, |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 201 | SkString* code) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 202 | GrAssert(NULL != outputVar); |
| 203 | GrAssert(NULL != var0); |
| 204 | GrAssert(NULL != var1); |
| 205 | GrAssert(NULL != code); |
| 206 | |
| 207 | bool has0 = '\0' != *var0; |
| 208 | bool has1 = '\0' != *var1; |
| 209 | |
| 210 | if (!has0 && !has1) { |
| 211 | code->appendf("\t%s = %s;\n", outputVar, all_zeros_vec(4)); |
| 212 | } else if (!has0) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 213 | code->appendf("\t%s = vec4(%s);\n", outputVar, var1); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 214 | } else if (!has1) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 215 | code->appendf("\t%s = vec4(%s);\n", outputVar, var0); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 216 | } else { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 217 | code->appendf("\t%s = vec4(%s + %s);\n", outputVar, var0, var1); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | // given two blend coeffecients determine whether the src |
| 222 | // and/or dst computation can be omitted. |
| 223 | static inline void needBlendInputs(SkXfermode::Coeff srcCoeff, |
| 224 | SkXfermode::Coeff dstCoeff, |
| 225 | bool* needSrcValue, |
| 226 | bool* needDstValue) { |
| 227 | if (SkXfermode::kZero_Coeff == srcCoeff) { |
| 228 | switch (dstCoeff) { |
| 229 | // these all read the src |
| 230 | case SkXfermode::kSC_Coeff: |
| 231 | case SkXfermode::kISC_Coeff: |
| 232 | case SkXfermode::kSA_Coeff: |
| 233 | case SkXfermode::kISA_Coeff: |
| 234 | *needSrcValue = true; |
| 235 | break; |
| 236 | default: |
| 237 | *needSrcValue = false; |
| 238 | break; |
| 239 | } |
| 240 | } else { |
| 241 | *needSrcValue = true; |
| 242 | } |
| 243 | if (SkXfermode::kZero_Coeff == dstCoeff) { |
| 244 | switch (srcCoeff) { |
| 245 | // these all read the dst |
| 246 | case SkXfermode::kDC_Coeff: |
| 247 | case SkXfermode::kIDC_Coeff: |
| 248 | case SkXfermode::kDA_Coeff: |
| 249 | case SkXfermode::kIDA_Coeff: |
| 250 | *needDstValue = true; |
| 251 | break; |
| 252 | default: |
| 253 | *needDstValue = false; |
| 254 | break; |
| 255 | } |
| 256 | } else { |
| 257 | *needDstValue = true; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 262 | * Create a blend_coeff * value string to be used in shader code. Sets empty |
| 263 | * string if result is trivially zero. |
| 264 | */ |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 265 | static void blendTermString(SkString* str, SkXfermode::Coeff coeff, |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 266 | const char* src, const char* dst, |
| 267 | const char* value) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 268 | switch (coeff) { |
| 269 | case SkXfermode::kZero_Coeff: /** 0 */ |
| 270 | *str = ""; |
| 271 | break; |
| 272 | case SkXfermode::kOne_Coeff: /** 1 */ |
| 273 | *str = value; |
| 274 | break; |
| 275 | case SkXfermode::kSC_Coeff: |
| 276 | str->printf("(%s * %s)", src, value); |
| 277 | break; |
| 278 | case SkXfermode::kISC_Coeff: |
| 279 | str->printf("((%s - %s) * %s)", all_ones_vec(4), src, value); |
| 280 | break; |
| 281 | case SkXfermode::kDC_Coeff: |
| 282 | str->printf("(%s * %s)", dst, value); |
| 283 | break; |
| 284 | case SkXfermode::kIDC_Coeff: |
| 285 | str->printf("((%s - %s) * %s)", all_ones_vec(4), dst, value); |
| 286 | break; |
| 287 | case SkXfermode::kSA_Coeff: /** src alpha */ |
| 288 | str->printf("(%s.a * %s)", src, value); |
| 289 | break; |
| 290 | case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */ |
| 291 | str->printf("((1.0 - %s.a) * %s)", src, value); |
| 292 | break; |
| 293 | case SkXfermode::kDA_Coeff: /** dst alpha */ |
| 294 | str->printf("(%s.a * %s)", dst, value); |
| 295 | break; |
| 296 | case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */ |
| 297 | str->printf("((1.0 - %s.a) * %s)", dst, value); |
| 298 | break; |
| 299 | default: |
| 300 | GrCrash("Unexpected xfer coeff."); |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | /** |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 305 | * Adds a line to the fragment shader code which modifies the color by |
| 306 | * the specified color filter. |
| 307 | */ |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 308 | static void addColorFilter(SkString* fsCode, const char * outputVar, |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 309 | SkXfermode::Coeff uniformCoeff, |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 310 | SkXfermode::Coeff colorCoeff, |
| 311 | const char* inColor) { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 312 | SkString colorStr, constStr; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 313 | blendTermString(&colorStr, colorCoeff, COL_FILTER_UNI_NAME, |
| 314 | inColor, inColor); |
| 315 | blendTermString(&constStr, uniformCoeff, COL_FILTER_UNI_NAME, |
| 316 | inColor, COL_FILTER_UNI_NAME); |
| 317 | |
| 318 | add_helper(outputVar, colorStr.c_str(), constStr.c_str(), fsCode); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 319 | } |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 320 | /** |
| 321 | * Adds code to the fragment shader code which modifies the color by |
| 322 | * the specified color matrix. |
| 323 | */ |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 324 | static void addColorMatrix(SkString* fsCode, const char * outputVar, |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 325 | const char* inColor) { |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 326 | 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); |
| 327 | fsCode->appendf("\t%s.rgb *= %s.a;\n", outputVar, outputVar); |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 328 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 329 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 330 | void GrGLProgram::genEdgeCoverage(SkString* coverageVar, |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 331 | GrGLShaderBuilder* segments) const { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 332 | if (fDesc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit) { |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 333 | const char *vsName, *fsName; |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 334 | segments->addVarying(kVec4f_GrSLType, "Edge", &vsName, &fsName); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 335 | segments->fVSAttrs.push_back().set(kVec4f_GrSLType, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 336 | GrGLShaderVar::kAttribute_TypeModifier, EDGE_ATTR_NAME); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 337 | segments->fVSCode.appendf("\t%s = " EDGE_ATTR_NAME ";\n", vsName); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 338 | switch (fDesc.fVertexEdgeType) { |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 339 | case GrDrawState::kHairLine_EdgeType: |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 340 | 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] | 341 | segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 342 | break; |
| 343 | case GrDrawState::kQuad_EdgeType: |
vandebo@chromium.org | b9682d3 | 2012-02-21 18:53:39 +0000 | [diff] [blame] | 344 | segments->fFSCode.append("\tfloat edgeAlpha;\n"); |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 345 | // keep the derivative instructions outside the conditional |
| 346 | segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
| 347 | segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
| 348 | segments->fFSCode.appendf("\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsName, fsName); |
| 349 | // today we know z and w are in device space. We could use derivatives |
| 350 | segments->fFSCode.appendf("\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);\n", fsName, fsName); |
| 351 | segments->fFSCode.append ("\t} else {\n"); |
| 352 | segments->fFSCode.appendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n" |
| 353 | "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n", |
| 354 | fsName, fsName); |
| 355 | segments->fFSCode.appendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName); |
vandebo@chromium.org | b9682d3 | 2012-02-21 18:53:39 +0000 | [diff] [blame] | 356 | segments->fFSCode.append("\t\tedgeAlpha = clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0);\n" |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 357 | "\t}\n"); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 358 | if (kES2_GrGLBinding == fContextInfo.binding()) { |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 359 | segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n"); |
| 360 | } |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 361 | break; |
| 362 | case GrDrawState::kHairQuad_EdgeType: |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 363 | segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
| 364 | segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 365 | segments->fFSCode.appendf("\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n" |
| 366 | "\t 2.0*%s.x*duvdy.x - duvdy.y);\n", |
| 367 | fsName, fsName); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 368 | 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] | 369 | segments->fFSCode.append("\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n"); |
| 370 | segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 371 | if (kES2_GrGLBinding == fContextInfo.binding()) { |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 372 | segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n"); |
| 373 | } |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 374 | break; |
| 375 | case GrDrawState::kCircle_EdgeType: |
| 376 | segments->fFSCode.append("\tfloat edgeAlpha;\n"); |
| 377 | segments->fFSCode.appendf("\tfloat d = distance(gl_FragCoord.xy, %s.xy);\n", fsName); |
| 378 | segments->fFSCode.appendf("\tfloat outerAlpha = smoothstep(d - 0.5, d + 0.5, %s.z);\n", fsName); |
| 379 | segments->fFSCode.appendf("\tfloat innerAlpha = %s.w == 0.0 ? 1.0 : smoothstep(%s.w - 0.5, %s.w + 0.5, d);\n", fsName, fsName, fsName); |
| 380 | segments->fFSCode.append("\tedgeAlpha = outerAlpha * innerAlpha;\n"); |
| 381 | break; |
| 382 | default: |
| 383 | GrCrash("Unknown Edge Type!"); |
| 384 | break; |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 385 | } |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 386 | *coverageVar = "edgeAlpha"; |
| 387 | } else { |
| 388 | coverageVar->reset(); |
| 389 | } |
| 390 | } |
| 391 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 392 | void GrGLProgram::genInputColor(GrGLShaderBuilder* builder, SkString* inColor) { |
| 393 | switch (fDesc.fColorInput) { |
| 394 | case GrGLProgram::Desc::kAttribute_ColorInput: { |
| 395 | builder->fVSAttrs.push_back().set(kVec4f_GrSLType, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 396 | GrGLShaderVar::kAttribute_TypeModifier, |
| 397 | COL_ATTR_NAME); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 398 | const char *vsName, *fsName; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 399 | builder->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsName); |
| 400 | builder->fVSCode.appendf("\t%s = " COL_ATTR_NAME ";\n", vsName); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 401 | *inColor = fsName; |
| 402 | } break; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 403 | case GrGLProgram::Desc::kUniform_ColorInput: |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 404 | fUniforms.fColorUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 405 | kVec4f_GrSLType, COL_UNI_NAME); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 406 | *inColor = COL_UNI_NAME; |
| 407 | break; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 408 | case GrGLProgram::Desc::kTransBlack_ColorInput: |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 409 | GrAssert(!"needComputedColor should be false."); |
| 410 | break; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 411 | case GrGLProgram::Desc::kSolidWhite_ColorInput: |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 412 | break; |
| 413 | default: |
| 414 | GrCrash("Unknown color type."); |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 419 | void GrGLProgram::genUniformCoverage(GrGLShaderBuilder* builder, SkString* inOutCoverage) { |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 420 | fUniforms.fCoverageUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 421 | kVec4f_GrSLType, COV_UNI_NAME); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 422 | if (inOutCoverage->size()) { |
| 423 | builder->fFSCode.appendf("\tvec4 uniCoverage = %s * %s;\n", |
| 424 | COV_UNI_NAME, inOutCoverage->c_str()); |
| 425 | *inOutCoverage = "uniCoverage"; |
| 426 | } else { |
| 427 | *inOutCoverage = COV_UNI_NAME; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | namespace { |
| 432 | void gen_attribute_coverage(GrGLShaderBuilder* segments, |
| 433 | SkString* inOutCoverage) { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 434 | segments->fVSAttrs.push_back().set(kVec4f_GrSLType, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 435 | GrGLShaderVar::kAttribute_TypeModifier, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 436 | COV_ATTR_NAME); |
| 437 | const char *vsName, *fsName; |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 438 | segments->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &fsName); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 439 | segments->fVSCode.appendf("\t%s = " COV_ATTR_NAME ";\n", vsName); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 440 | if (inOutCoverage->size()) { |
| 441 | segments->fFSCode.appendf("\tvec4 attrCoverage = %s * %s;\n", |
| 442 | fsName, inOutCoverage->c_str()); |
| 443 | *inOutCoverage = "attrCoverage"; |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 444 | } else { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 445 | *inOutCoverage = fsName; |
| 446 | } |
| 447 | } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 448 | } |
| 449 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 450 | void GrGLProgram::genGeometryShader(GrGLShaderBuilder* segments) const { |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 451 | #if GR_GL_EXPERIMENTAL_GS |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 452 | if (fDesc.fExperimentalGS) { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 453 | GrAssert(fContextInfo.glslGeneration() >= k150_GrGLSLGeneration); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 454 | segments->fGSHeader.append("layout(triangles) in;\n" |
| 455 | "layout(triangle_strip, max_vertices = 6) out;\n"); |
| 456 | segments->fGSCode.append("void main() {\n" |
| 457 | "\tfor (int i = 0; i < 3; ++i) {\n" |
| 458 | "\t\tgl_Position = gl_in[i].gl_Position;\n"); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 459 | if (fDesc.fEmitsPointSize) { |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 460 | segments->fGSCode.append("\t\tgl_PointSize = 1.0;\n"); |
| 461 | } |
| 462 | GrAssert(segments->fGSInputs.count() == segments->fGSOutputs.count()); |
| 463 | int count = segments->fGSInputs.count(); |
| 464 | for (int i = 0; i < count; ++i) { |
| 465 | segments->fGSCode.appendf("\t\t%s = %s[i];\n", |
| 466 | segments->fGSOutputs[i].getName().c_str(), |
| 467 | segments->fGSInputs[i].getName().c_str()); |
| 468 | } |
| 469 | segments->fGSCode.append("\t\tEmitVertex();\n" |
| 470 | "\t}\n" |
| 471 | "\tEndPrimitive();\n" |
| 472 | "}\n"); |
| 473 | } |
| 474 | #endif |
| 475 | } |
| 476 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 477 | const char* GrGLProgram::adjustInColor(const SkString& inColor) const { |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 478 | if (inColor.size()) { |
| 479 | return inColor.c_str(); |
| 480 | } else { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 481 | if (Desc::kSolidWhite_ColorInput == fDesc.fColorInput) { |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 482 | return all_ones_vec(4); |
| 483 | } else { |
| 484 | return all_zeros_vec(4); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 489 | namespace { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 490 | // prints a shader using params similar to glShaderSource |
| 491 | void print_shader(GrGLint stringCnt, |
| 492 | const GrGLchar** strings, |
| 493 | GrGLint* stringLengths) { |
| 494 | for (int i = 0; i < stringCnt; ++i) { |
| 495 | if (NULL == stringLengths || stringLengths[i] < 0) { |
| 496 | GrPrintf(strings[i]); |
| 497 | } else { |
| 498 | GrPrintf("%.*s", stringLengths[i], strings[i]); |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | // Compiles a GL shader, returns shader ID or 0 if failed params have same meaning as glShaderSource |
| 504 | GrGLuint compile_shader(const GrGLContextInfo& gl, |
| 505 | GrGLenum type, |
| 506 | int stringCnt, |
| 507 | const char** strings, |
| 508 | int* stringLengths) { |
| 509 | SK_TRACE_EVENT1("GrGLProgram::CompileShader", |
| 510 | "stringCount", SkStringPrintf("%i", stringCnt).c_str()); |
| 511 | |
| 512 | GrGLuint shader; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 513 | GR_GL_CALL_RET(gl.interface(), shader, CreateShader(type)); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 514 | if (0 == shader) { |
| 515 | return 0; |
| 516 | } |
| 517 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 518 | const GrGLInterface* gli = gl.interface(); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 519 | GrGLint compiled = GR_GL_INIT_ZERO; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 520 | GR_GL_CALL(gli, ShaderSource(shader, stringCnt, strings, stringLengths)); |
| 521 | GR_GL_CALL(gli, CompileShader(shader)); |
| 522 | GR_GL_CALL(gli, GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled)); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 523 | |
| 524 | if (!compiled) { |
| 525 | GrGLint infoLen = GR_GL_INIT_ZERO; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 526 | GR_GL_CALL(gli, GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 527 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 528 | if (infoLen > 0) { |
| 529 | // retrieve length even though we don't need it to workaround bug in chrome cmd buffer |
| 530 | // param validation. |
| 531 | GrGLsizei length = GR_GL_INIT_ZERO; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 532 | GR_GL_CALL(gli, GetShaderInfoLog(shader, infoLen+1, |
| 533 | &length, (char*)log.get())); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 534 | print_shader(stringCnt, strings, stringLengths); |
| 535 | GrPrintf("\n%s", log.get()); |
| 536 | } |
| 537 | GrAssert(!"Shader compilation failed!"); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 538 | GR_GL_CALL(gli, DeleteShader(shader)); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 539 | return 0; |
| 540 | } |
| 541 | return shader; |
| 542 | } |
| 543 | |
| 544 | // helper version of above for when shader is already flattened into a single SkString |
| 545 | GrGLuint compile_shader(const GrGLContextInfo& gl, GrGLenum type, const SkString& shader) { |
| 546 | const GrGLchar* str = shader.c_str(); |
| 547 | int length = shader.size(); |
| 548 | return compile_shader(gl, type, 1, &str, &length); |
| 549 | } |
| 550 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | // compiles all the shaders from builder and stores the shader IDs |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 554 | bool GrGLProgram::compileShaders(const GrGLShaderBuilder& builder) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 555 | |
| 556 | SkString shader; |
| 557 | |
| 558 | builder.getShader(GrGLShaderBuilder::kVertex_ShaderType, &shader); |
| 559 | #if PRINT_SHADERS |
| 560 | GrPrintf(shader.c_str()); |
| 561 | GrPrintf("\n"); |
| 562 | #endif |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 563 | if (!(fVShaderID = compile_shader(fContextInfo, GR_GL_VERTEX_SHADER, shader))) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 564 | return false; |
| 565 | } |
| 566 | |
| 567 | if (builder.fUsesGS) { |
| 568 | builder.getShader(GrGLShaderBuilder::kGeometry_ShaderType, &shader); |
| 569 | #if PRINT_SHADERS |
| 570 | GrPrintf(shader.c_str()); |
| 571 | GrPrintf("\n"); |
| 572 | #endif |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 573 | if (!(fGShaderID = compile_shader(fContextInfo, GR_GL_GEOMETRY_SHADER, shader))) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 574 | return false; |
| 575 | } |
| 576 | } else { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 577 | fGShaderID = 0; |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | builder.getShader(GrGLShaderBuilder::kFragment_ShaderType, &shader); |
| 581 | #if PRINT_SHADERS |
| 582 | GrPrintf(shader.c_str()); |
| 583 | GrPrintf("\n"); |
| 584 | #endif |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 585 | if (!(fFShaderID = compile_shader(fContextInfo, GR_GL_FRAGMENT_SHADER, shader))) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 586 | return false; |
| 587 | } |
| 588 | |
| 589 | return true; |
| 590 | } |
| 591 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 592 | bool GrGLProgram::genProgram(GrCustomStage** customStages) { |
| 593 | GrAssert(0 == fProgramID); |
| 594 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 595 | GrGLShaderBuilder builder(fContextInfo, fUniformManager); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 596 | const uint32_t& layout = fDesc.fVertexLayout; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 597 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 598 | #if GR_GL_EXPERIMENTAL_GS |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 599 | builder.fUsesGS = fDesc.fExperimentalGS; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 600 | #endif |
| 601 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 602 | SkXfermode::Coeff colorCoeff, uniformCoeff; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 603 | bool applyColorMatrix = SkToBool(fDesc.fColorMatrixEnabled); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 604 | // The rest of transfer mode color filters have not been implemented |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 605 | if (fDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 606 | GR_DEBUGCODE(bool success =) |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 607 | SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode> |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 608 | (fDesc.fColorFilterXfermode), |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 609 | &uniformCoeff, &colorCoeff); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 610 | GR_DEBUGASSERT(success); |
| 611 | } else { |
| 612 | colorCoeff = SkXfermode::kOne_Coeff; |
| 613 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 614 | } |
| 615 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 616 | // no need to do the color filter / matrix at all if coverage is 0. The |
| 617 | // output color is scaled by the coverage. All the dual source outputs are |
| 618 | // scaled by the coverage as well. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 619 | if (Desc::kTransBlack_ColorInput == fDesc.fCoverageInput) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 620 | colorCoeff = SkXfermode::kZero_Coeff; |
| 621 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 622 | applyColorMatrix = false; |
| 623 | } |
| 624 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 625 | // If we know the final color is going to be all zeros then we can |
| 626 | // simplify the color filter coeffecients. needComputedColor will then |
| 627 | // come out false below. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 628 | if (Desc::kTransBlack_ColorInput == fDesc.fColorInput) { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 629 | colorCoeff = SkXfermode::kZero_Coeff; |
| 630 | if (SkXfermode::kDC_Coeff == uniformCoeff || |
| 631 | SkXfermode::kDA_Coeff == uniformCoeff) { |
| 632 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 633 | } else if (SkXfermode::kIDC_Coeff == uniformCoeff || |
| 634 | SkXfermode::kIDA_Coeff == uniformCoeff) { |
| 635 | uniformCoeff = SkXfermode::kOne_Coeff; |
| 636 | } |
| 637 | } |
| 638 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 639 | bool needColorFilterUniform; |
| 640 | bool needComputedColor; |
| 641 | needBlendInputs(uniformCoeff, colorCoeff, |
| 642 | &needColorFilterUniform, &needComputedColor); |
| 643 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 644 | // the dual source output has no canonical var name, have to |
| 645 | // declare an output, which is incompatible with gl_FragColor/gl_FragData. |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 646 | bool dualSourceOutputWritten = false; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 647 | builder.fHeader.append(GrGetGLSLVersionDecl(fContextInfo.binding(), |
| 648 | fContextInfo.glslGeneration())); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 649 | |
| 650 | GrGLShaderVar colorOutput; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 651 | bool isColorDeclared = GrGLSLSetupFSColorOuput(fContextInfo.glslGeneration(), |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 652 | declared_color_output_name(), |
| 653 | &colorOutput); |
| 654 | if (isColorDeclared) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 655 | builder.fFSOutputs.push_back(colorOutput); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 656 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 657 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 658 | fUniforms.fViewMatrixUni = builder.addUniform(GrGLShaderBuilder::kVertex_ShaderType, |
| 659 | kMat33f_GrSLType, VIEW_MATRIX_NAME); |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 660 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 661 | builder.fVSAttrs.push_back().set(kVec2f_GrSLType, |
| 662 | GrGLShaderVar::kAttribute_TypeModifier, |
| 663 | POS_ATTR_NAME); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 664 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 665 | builder.fVSCode.append("void main() {\n" |
| 666 | "\tvec3 pos3 = " VIEW_MATRIX_NAME " * vec3("POS_ATTR_NAME", 1);\n" |
| 667 | "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n"); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 668 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 669 | // incoming color to current stage being processed. |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 670 | SkString inColor; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 671 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 672 | if (needComputedColor) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 673 | this->genInputColor(&builder, &inColor); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 674 | } |
| 675 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 676 | // we output point size in the GS if present |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 677 | if (fDesc.fEmitsPointSize && !builder.fUsesGS){ |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 678 | builder.fVSCode.append("\tgl_PointSize = 1.0;\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 679 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 680 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 681 | builder.fFSCode.append("void main() {\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 682 | |
| 683 | // add texture coordinates that are used to the list of vertex attr decls |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 684 | SkString texCoordAttrs[GrDrawState::kMaxTexCoords]; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 685 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 686 | if (GrDrawTarget::VertexUsesTexCoordIdx(t, layout)) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 687 | tex_attr_name(t, texCoordAttrs + t); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 688 | builder.fVSAttrs.push_back().set(kVec2f_GrSLType, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 689 | GrGLShaderVar::kAttribute_TypeModifier, |
| 690 | texCoordAttrs[t].c_str()); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 694 | /////////////////////////////////////////////////////////////////////////// |
| 695 | // compute the final color |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 696 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 697 | // if we have color stages string them together, feeding the output color |
| 698 | // of each to the next and generating code for each stage. |
| 699 | if (needComputedColor) { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 700 | SkString outColor; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 701 | for (int s = 0; s < fDesc.fFirstCoverageStage; ++s) { |
| 702 | if (fDesc.fStages[s].isEnabled()) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 703 | // create var to hold stage result |
| 704 | outColor = "color"; |
| 705 | outColor.appendS32(s); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 706 | builder.fFSCode.appendf("\tvec4 %s;\n", outColor.c_str()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 707 | |
| 708 | const char* inCoords; |
| 709 | // figure out what our input coords are |
tomhudson@google.com | b213ed8 | 2012-06-25 15:22:12 +0000 | [diff] [blame] | 710 | int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout); |
| 711 | if (tcIdx < 0) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 712 | inCoords = POS_ATTR_NAME; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 713 | } else { |
tomhudson@google.com | b213ed8 | 2012-06-25 15:22:12 +0000 | [diff] [blame] | 714 | // must have input tex coordinates if stage is enabled. |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 715 | GrAssert(texCoordAttrs[tcIdx].size()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 716 | inCoords = texCoordAttrs[tcIdx].c_str(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 717 | } |
| 718 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 719 | if (NULL != customStages[s]) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 720 | const GrProgramStageFactory& factory = customStages[s]->getFactory(); |
| 721 | fProgramStage[s] = factory.createGLInstance(*customStages[s]); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 722 | } |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 723 | this->genStageCode(s, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 724 | inColor.size() ? inColor.c_str() : NULL, |
| 725 | outColor.c_str(), |
| 726 | inCoords, |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 727 | &builder); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 728 | inColor = outColor; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 731 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 732 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 733 | // if have all ones or zeros for the "dst" input to the color filter then we |
| 734 | // may be able to make additional optimizations. |
| 735 | if (needColorFilterUniform && needComputedColor && !inColor.size()) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 736 | GrAssert(Desc::kSolidWhite_ColorInput == fDesc.fColorInput); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 737 | bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff || |
| 738 | SkXfermode::kIDA_Coeff == uniformCoeff; |
| 739 | if (uniformCoeffIsZero) { |
| 740 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 741 | bool bogus; |
| 742 | needBlendInputs(SkXfermode::kZero_Coeff, colorCoeff, |
| 743 | &needColorFilterUniform, &bogus); |
| 744 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 745 | } |
| 746 | if (needColorFilterUniform) { |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 747 | fUniforms.fColorFilterUni = builder.addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 748 | kVec4f_GrSLType, COL_FILTER_UNI_NAME); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 749 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 750 | bool wroteFragColorZero = false; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 751 | if (SkXfermode::kZero_Coeff == uniformCoeff && |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 752 | SkXfermode::kZero_Coeff == colorCoeff && |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 753 | !applyColorMatrix) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 754 | builder.fFSCode.appendf("\t%s = %s;\n", |
| 755 | colorOutput.getName().c_str(), |
| 756 | all_zeros_vec(4)); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 757 | wroteFragColorZero = true; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 758 | } else if (SkXfermode::kDst_Mode != fDesc.fColorFilterXfermode) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 759 | builder.fFSCode.append("\tvec4 filteredColor;\n"); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 760 | const char* color = adjustInColor(inColor); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 761 | addColorFilter(&builder.fFSCode, "filteredColor", uniformCoeff, |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 762 | colorCoeff, color); |
| 763 | inColor = "filteredColor"; |
| 764 | } |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 765 | if (applyColorMatrix) { |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 766 | fUniforms.fColorMatrixUni = builder.addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 767 | kMat44f_GrSLType, COL_MATRIX_UNI_NAME); |
| 768 | fUniforms.fColorMatrixVecUni = builder.addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 769 | kVec4f_GrSLType, COL_MATRIX_VEC_UNI_NAME); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 770 | builder.fFSCode.append("\tvec4 matrixedColor;\n"); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 771 | const char* color = adjustInColor(inColor); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 772 | addColorMatrix(&builder.fFSCode, "matrixedColor", color); |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 773 | inColor = "matrixedColor"; |
| 774 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 775 | |
| 776 | /////////////////////////////////////////////////////////////////////////// |
| 777 | // compute the partial coverage (coverage stages and edge aa) |
| 778 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 779 | SkString inCoverage; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 780 | bool coverageIsZero = Desc::kTransBlack_ColorInput == fDesc.fCoverageInput; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 781 | // we don't need to compute coverage at all if we know the final shader |
| 782 | // output will be zero and we don't have a dual src blend output. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 783 | if (!wroteFragColorZero || Desc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) { |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 784 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 785 | if (!coverageIsZero) { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 786 | this->genEdgeCoverage(&inCoverage, &builder); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 787 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 788 | switch (fDesc.fCoverageInput) { |
| 789 | case Desc::kSolidWhite_ColorInput: |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 790 | // empty string implies solid white |
| 791 | break; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 792 | case Desc::kAttribute_ColorInput: |
| 793 | gen_attribute_coverage(&builder, &inCoverage); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 794 | break; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 795 | case Desc::kUniform_ColorInput: |
| 796 | this->genUniformCoverage(&builder, &inCoverage); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 797 | break; |
| 798 | default: |
| 799 | GrCrash("Unexpected input coverage."); |
| 800 | } |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 801 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 802 | SkString outCoverage; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 803 | const int& startStage = fDesc.fFirstCoverageStage; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 804 | for (int s = startStage; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 805 | if (fDesc.fStages[s].isEnabled()) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 806 | // create var to hold stage output |
| 807 | outCoverage = "coverage"; |
| 808 | outCoverage.appendS32(s); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 809 | builder.fFSCode.appendf("\tvec4 %s;\n", |
| 810 | outCoverage.c_str()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 811 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 812 | const char* inCoords; |
| 813 | // figure out what our input coords are |
tomhudson@google.com | b213ed8 | 2012-06-25 15:22:12 +0000 | [diff] [blame] | 814 | int tcIdx = |
| 815 | GrDrawTarget::VertexTexCoordsForStage(s, layout); |
| 816 | if (tcIdx < 0) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 817 | inCoords = POS_ATTR_NAME; |
| 818 | } else { |
tomhudson@google.com | b213ed8 | 2012-06-25 15:22:12 +0000 | [diff] [blame] | 819 | // must have input tex coordinates if stage is |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 820 | // enabled. |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 821 | GrAssert(texCoordAttrs[tcIdx].size()); |
| 822 | inCoords = texCoordAttrs[tcIdx].c_str(); |
| 823 | } |
| 824 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 825 | if (NULL != customStages[s]) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 826 | const GrProgramStageFactory& factory = customStages[s]->getFactory(); |
| 827 | fProgramStage[s] = factory.createGLInstance(*customStages[s]); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 828 | } |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 829 | this->genStageCode(s, |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 830 | inCoverage.size() ? inCoverage.c_str() : NULL, |
| 831 | outCoverage.c_str(), |
| 832 | inCoords, |
| 833 | &builder); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 834 | inCoverage = outCoverage; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 835 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 836 | } |
| 837 | } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 838 | if (Desc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 839 | builder.fFSOutputs.push_back().set(kVec4f_GrSLType, |
| 840 | GrGLShaderVar::kOut_TypeModifier, |
| 841 | dual_source_output_name()); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 842 | bool outputIsZero = coverageIsZero; |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 843 | SkString coeff; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 844 | if (!outputIsZero && |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 845 | Desc::kCoverage_DualSrcOutput != fDesc.fDualSrcOutput && !wroteFragColorZero) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 846 | if (!inColor.size()) { |
| 847 | outputIsZero = true; |
| 848 | } else { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 849 | if (Desc::kCoverageISA_DualSrcOutput == fDesc.fDualSrcOutput) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 850 | coeff.printf("(1 - %s.a)", inColor.c_str()); |
| 851 | } else { |
| 852 | coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str()); |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | if (outputIsZero) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 857 | builder.fFSCode.appendf("\t%s = %s;\n", |
| 858 | dual_source_output_name(), |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 859 | all_zeros_vec(4)); |
| 860 | } else { |
| 861 | modulate_helper(dual_source_output_name(), |
| 862 | coeff.c_str(), |
| 863 | inCoverage.c_str(), |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 864 | &builder.fFSCode); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 865 | } |
| 866 | dualSourceOutputWritten = true; |
| 867 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 868 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 869 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 870 | /////////////////////////////////////////////////////////////////////////// |
| 871 | // combine color and coverage as frag color |
| 872 | |
| 873 | if (!wroteFragColorZero) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 874 | if (coverageIsZero) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 875 | builder.fFSCode.appendf("\t%s = %s;\n", |
| 876 | colorOutput.getName().c_str(), |
| 877 | all_zeros_vec(4)); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 878 | } else { |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 879 | modulate_helper(colorOutput.getName().c_str(), |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 880 | inColor.c_str(), |
| 881 | inCoverage.c_str(), |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 882 | &builder.fFSCode); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 883 | } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 884 | if (Desc::kUnpremultiplied_RoundDown_OutputConfig == fDesc.fOutputConfig) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 885 | builder.fFSCode.appendf("\t%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.rgb / %s.a * 255.0)/255.0, %s.a);\n", |
| 886 | colorOutput.getName().c_str(), |
| 887 | colorOutput.getName().c_str(), |
| 888 | colorOutput.getName().c_str(), |
| 889 | colorOutput.getName().c_str(), |
| 890 | colorOutput.getName().c_str()); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 891 | } else if (Desc::kUnpremultiplied_RoundUp_OutputConfig == fDesc.fOutputConfig) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 892 | builder.fFSCode.appendf("\t%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.rgb / %s.a * 255.0)/255.0, %s.a);\n", |
| 893 | colorOutput.getName().c_str(), |
| 894 | colorOutput.getName().c_str(), |
| 895 | colorOutput.getName().c_str(), |
| 896 | colorOutput.getName().c_str(), |
| 897 | colorOutput.getName().c_str()); |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 898 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 899 | } |
| 900 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 901 | builder.fVSCode.append("}\n"); |
| 902 | builder.fFSCode.append("}\n"); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 903 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 904 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 905 | // insert GS |
| 906 | #if GR_DEBUG |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 907 | this->genGeometryShader(&builder); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 908 | #endif |
| 909 | |
| 910 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 911 | // compile and setup attribs and unis |
| 912 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 913 | if (!this->compileShaders(builder)) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 914 | return false; |
| 915 | } |
| 916 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 917 | if (!this->bindOutputsAttribsAndLinkProgram(texCoordAttrs, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 918 | isColorDeclared, |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 919 | dualSourceOutputWritten)) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 920 | return false; |
| 921 | } |
| 922 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 923 | builder.finished(fProgramID); |
| 924 | this->initSamplerUniforms(); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 925 | |
| 926 | return true; |
| 927 | } |
| 928 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 929 | bool GrGLProgram::bindOutputsAttribsAndLinkProgram(SkString texCoordAttrNames[], |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 930 | bool bindColorOut, |
| 931 | bool bindDualSrcOut) { |
| 932 | GL_CALL_RET(fProgramID, CreateProgram()); |
| 933 | if (!fProgramID) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 934 | return false; |
| 935 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 936 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 937 | GL_CALL(AttachShader(fProgramID, fVShaderID)); |
| 938 | if (fGShaderID) { |
| 939 | GL_CALL(AttachShader(fProgramID, fGShaderID)); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 940 | } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 941 | GL_CALL(AttachShader(fProgramID, fFShaderID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 942 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 943 | if (bindColorOut) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 944 | GL_CALL(BindFragDataLocation(fProgramID, 0, declared_color_output_name())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 945 | } |
| 946 | if (bindDualSrcOut) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 947 | GL_CALL(BindFragDataLocationIndexed(fProgramID, 0, 1, dual_source_output_name())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 948 | } |
| 949 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 950 | // Bind the attrib locations to same values for all shaders |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 951 | GL_CALL(BindAttribLocation(fProgramID, PositionAttributeIdx(), POS_ATTR_NAME)); |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 952 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 953 | if (texCoordAttrNames[t].size()) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 954 | GL_CALL(BindAttribLocation(fProgramID, |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 955 | TexCoordAttributeIdx(t), |
| 956 | texCoordAttrNames[t].c_str())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 960 | GL_CALL(BindAttribLocation(fProgramID, ColorAttributeIdx(), COL_ATTR_NAME)); |
| 961 | GL_CALL(BindAttribLocation(fProgramID, CoverageAttributeIdx(), COV_ATTR_NAME)); |
| 962 | GL_CALL(BindAttribLocation(fProgramID, EdgeAttributeIdx(), EDGE_ATTR_NAME)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 963 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 964 | GL_CALL(LinkProgram(fProgramID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 965 | |
| 966 | GrGLint linked = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 967 | GL_CALL(GetProgramiv(fProgramID, GR_GL_LINK_STATUS, &linked)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 968 | if (!linked) { |
| 969 | GrGLint infoLen = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 970 | GL_CALL(GetProgramiv(fProgramID, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 971 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 972 | if (infoLen > 0) { |
bsalomon@google.com | 79afcaa | 2011-09-14 14:29:18 +0000 | [diff] [blame] | 973 | // retrieve length even though we don't need it to workaround |
| 974 | // bug in chrome cmd buffer param validation. |
| 975 | GrGLsizei length = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 976 | GL_CALL(GetProgramInfoLog(fProgramID, |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 977 | infoLen+1, |
| 978 | &length, |
| 979 | (char*)log.get())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 980 | GrPrintf((char*)log.get()); |
| 981 | } |
| 982 | GrAssert(!"Error linking program"); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 983 | GL_CALL(DeleteProgram(fProgramID)); |
| 984 | fProgramID = 0; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 985 | return false; |
| 986 | } |
| 987 | return true; |
| 988 | } |
| 989 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 990 | void GrGLProgram::initSamplerUniforms() { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 991 | GL_CALL(UseProgram(fProgramID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 992 | // init sampler unis and set bogus values for state tracking |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 993 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 994 | if (GrGLUniformManager::kInvalidUniformHandle != fUniforms.fStages[s].fSamplerUni) { |
| 995 | fUniformManager.setSampler(fUniforms.fStages[s].fSamplerUni, s); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 996 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 997 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 998 | } |
| 999 | |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 1000 | /////////////////////////////////////////////////////////////////////////////// |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1001 | // Stage code generation |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1002 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 1003 | void GrGLProgram::genStageCode(int stageNum, |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 1004 | const char* fsInColor, // NULL means no incoming color |
| 1005 | const char* fsOutColor, |
| 1006 | const char* vsInCoord, |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 1007 | GrGLShaderBuilder* segments) { |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1008 | GrAssert(stageNum >= 0 && stageNum <= GrDrawState::kNumStages); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 1009 | |
| 1010 | const GrGLProgram::StageDesc& desc = fDesc.fStages[stageNum]; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 1011 | StageUniforms& uniforms = fUniforms.fStages[stageNum]; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 1012 | GrGLProgramStage* customStage = fProgramStage[stageNum]; |
| 1013 | |
| 1014 | GrAssert((desc.fInConfigFlags & StageDesc::kInConfigBitMask) == desc.fInConfigFlags); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1015 | |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 1016 | /// Vertex Shader Stuff |
| 1017 | |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 1018 | // decide whether we need a matrix to transform texture coords and whether the varying needs a |
| 1019 | // perspective coord. |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1020 | const char* matName = NULL; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1021 | if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) { |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 1022 | segments->fVaryingDims = segments->fCoordDims; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1023 | } else { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 1024 | SkString texMatName; |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 1025 | tex_matrix_name(stageNum, &texMatName); |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 1026 | uniforms.fTextureMatrixUni = segments->addUniform(GrGLShaderBuilder::kVertex_ShaderType, |
| 1027 | kMat33f_GrSLType, texMatName.c_str()); |
| 1028 | const GrGLShaderVar& mat = segments->getUniformVariable(uniforms.fTextureMatrixUni); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 1029 | // Can't use texMatName.c_str() because it's on the stack! |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 1030 | matName = mat.getName().c_str(); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1031 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1032 | if (desc.fOptFlags & StageDesc::kNoPerspective_OptFlagBit) { |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 1033 | segments->fVaryingDims = segments->fCoordDims; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1034 | } else { |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 1035 | segments->fVaryingDims = segments->fCoordDims + 1; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1036 | } |
| 1037 | } |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 1038 | GrAssert(segments->fVaryingDims > 0); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1039 | |
tomhudson@google.com | 898e7b5 | 2012-06-01 20:42:15 +0000 | [diff] [blame] | 1040 | // Must setup variables after computing segments->fVaryingDims |
| 1041 | if (NULL != customStage) { |
| 1042 | customStage->setupVariables(segments, stageNum); |
| 1043 | } |
| 1044 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 1045 | SkString samplerName; |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 1046 | sampler_name(stageNum, &samplerName); |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 1047 | uniforms.fSamplerUni = segments->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 1048 | kSampler2D_GrSLType, samplerName.c_str()); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1049 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1050 | const char *varyingVSName, *varyingFSName; |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 1051 | segments->addVarying(GrSLFloatVectorType(segments->fVaryingDims), |
| 1052 | "Stage", |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 1053 | stageNum, |
| 1054 | &varyingVSName, |
| 1055 | &varyingFSName); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1056 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1057 | if (!matName) { |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 1058 | GrAssert(segments->fVaryingDims == segments->fCoordDims); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1059 | segments->fVSCode.appendf("\t%s = %s;\n", varyingVSName, vsInCoord); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1060 | } else { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1061 | // varying = texMatrix * texCoord |
| 1062 | segments->fVSCode.appendf("\t%s = (%s * vec3(%s, 1))%s;\n", |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1063 | varyingVSName, matName, vsInCoord, |
tomhudson@google.com | 9c639a4 | 2012-05-14 19:58:06 +0000 | [diff] [blame] | 1064 | vector_all_coords(segments->fVaryingDims)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1067 | if (NULL != customStage) { |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 1068 | segments->fVSCode.appendf("\t{ // stage %d %s\n", |
| 1069 | stageNum, customStage->name()); |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 1070 | customStage->emitVS(segments, varyingVSName); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 1071 | segments->fVSCode.appendf("\t}\n"); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1074 | /// Fragment Shader Stuff |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1075 | |
tomhudson@google.com | 5440f06 | 2012-06-01 15:55:50 +0000 | [diff] [blame] | 1076 | segments->fSampleCoords = varyingFSName; |
| 1077 | |
| 1078 | GrGLShaderBuilder::SamplerMode sampleMode = |
| 1079 | GrGLShaderBuilder::kExplicitDivide_SamplerMode; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 1080 | if (desc.fOptFlags & (StageDesc::kIdentityMatrix_OptFlagBit | |
| 1081 | StageDesc::kNoPerspective_OptFlagBit)) { |
tomhudson@google.com | 5440f06 | 2012-06-01 15:55:50 +0000 | [diff] [blame] | 1082 | sampleMode = GrGLShaderBuilder::kDefault_SamplerMode; |
bsalomon@google.com | d2ae1fa | 2012-06-04 20:06:02 +0000 | [diff] [blame] | 1083 | } else if (NULL == customStage) { |
tomhudson@google.com | 5440f06 | 2012-06-01 15:55:50 +0000 | [diff] [blame] | 1084 | sampleMode = GrGLShaderBuilder::kProj_SamplerMode; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1085 | } |
tomhudson@google.com | 5440f06 | 2012-06-01 15:55:50 +0000 | [diff] [blame] | 1086 | segments->setupTextureAccess(sampleMode, stageNum); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1087 | |
tomhudson@google.com | 898e7b5 | 2012-06-01 20:42:15 +0000 | [diff] [blame] | 1088 | segments->computeSwizzle(desc.fInConfigFlags); |
| 1089 | segments->computeModulate(fsInColor); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1090 | |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1091 | static const uint32_t kMulByAlphaMask = |
| 1092 | (StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag | |
| 1093 | StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag); |
| 1094 | |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 1095 | // NOTE: GrGLProgramStages are now responsible for fetching |
| 1096 | if (NULL == customStage) { |
bsalomon@google.com | d2ae1fa | 2012-06-04 20:06:02 +0000 | [diff] [blame] | 1097 | if (desc.fInConfigFlags & kMulByAlphaMask) { |
| 1098 | // only one of the mul by alpha flags should be set |
| 1099 | GrAssert(GrIsPow2(kMulByAlphaMask & desc.fInConfigFlags)); |
| 1100 | GrAssert(!(desc.fInConfigFlags & |
| 1101 | StageDesc::kSmearAlpha_InConfigFlag)); |
| 1102 | GrAssert(!(desc.fInConfigFlags & |
| 1103 | StageDesc::kSmearRed_InConfigFlag)); |
| 1104 | segments->fFSCode.appendf("\t%s = %s(%s, %s)%s;\n", |
| 1105 | fsOutColor, |
| 1106 | segments->fTexFunc.c_str(), |
| 1107 | samplerName.c_str(), |
| 1108 | segments->fSampleCoords.c_str(), |
| 1109 | segments->fSwizzle.c_str()); |
| 1110 | if (desc.fInConfigFlags & |
| 1111 | StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag) { |
| 1112 | segments->fFSCode.appendf("\t%s = vec4(ceil(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n", |
| 1113 | fsOutColor, fsOutColor, fsOutColor, |
| 1114 | fsOutColor, segments->fModulate.c_str()); |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1115 | } else { |
bsalomon@google.com | d2ae1fa | 2012-06-04 20:06:02 +0000 | [diff] [blame] | 1116 | segments->fFSCode.appendf("\t%s = vec4(floor(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n", |
| 1117 | fsOutColor, fsOutColor, fsOutColor, |
| 1118 | fsOutColor, segments->fModulate.c_str()); |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1119 | } |
bsalomon@google.com | d2ae1fa | 2012-06-04 20:06:02 +0000 | [diff] [blame] | 1120 | } else { |
| 1121 | segments->emitDefaultFetch(fsOutColor, samplerName.c_str()); |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 1122 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1123 | } |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1124 | |
| 1125 | if (NULL != customStage) { |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1126 | // Enclose custom code in a block to avoid namespace conflicts |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 1127 | segments->fFSCode.appendf("\t{ // stage %d %s \n", |
| 1128 | stageNum, customStage->name()); |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 1129 | customStage->emitFS(segments, fsOutColor, fsInColor, |
| 1130 | samplerName.c_str()); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 1131 | segments->fFSCode.appendf("\t}\n"); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1132 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1133 | } |