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" |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 11 | #include "GrEffect.h" |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 12 | #include "GrGLEffect.h" |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 13 | #include "GrGpuGL.h" |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 14 | #include "GrGLShaderVar.h" |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 15 | #include "GrBackendEffectFactory.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 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 26 | #define COL_ATTR_NAME "aColor" |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 27 | #define COV_ATTR_NAME "aCoverage" |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 28 | #define EDGE_ATTR_NAME "aEdge" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 29 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 30 | namespace { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 31 | inline void tex_attr_name(int coordIdx, SkString* s) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 32 | *s = "aTexCoord"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 33 | s->appendS32(coordIdx); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 34 | } |
| 35 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 36 | inline const char* declared_color_output_name() { return "fsColorOut"; } |
| 37 | inline const char* dual_source_output_name() { return "dualSourceOut"; } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 38 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 39 | } |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 40 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 41 | GrGLProgram* GrGLProgram::Create(const GrGLContextInfo& gl, |
| 42 | const Desc& desc, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 43 | const GrEffectStage* stages[]) { |
| 44 | GrGLProgram* program = SkNEW_ARGS(GrGLProgram, (gl, desc, stages)); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 45 | if (!program->succeeded()) { |
| 46 | delete program; |
| 47 | program = NULL; |
| 48 | } |
| 49 | return program; |
| 50 | } |
| 51 | |
| 52 | GrGLProgram::GrGLProgram(const GrGLContextInfo& gl, |
| 53 | const Desc& desc, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 54 | const GrEffectStage* stages[]) |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 55 | : fContextInfo(gl) |
| 56 | , fUniformManager(gl) { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 57 | fDesc = desc; |
| 58 | fVShaderID = 0; |
| 59 | fGShaderID = 0; |
| 60 | fFShaderID = 0; |
| 61 | fProgramID = 0; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 62 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 63 | fViewMatrix = SkMatrix::InvalidMatrix(); |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 64 | fViewportSize.set(-1, -1); |
| 65 | fColor = GrColor_ILLEGAL; |
| 66 | fColorFilterColor = GrColor_ILLEGAL; |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 67 | fRTHeight = -1; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 68 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 69 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 70 | fEffects[s] = NULL; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 71 | } |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 72 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 73 | this->genProgram(stages); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | GrGLProgram::~GrGLProgram() { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 77 | if (fVShaderID) { |
| 78 | GL_CALL(DeleteShader(fVShaderID)); |
| 79 | } |
| 80 | if (fGShaderID) { |
| 81 | GL_CALL(DeleteShader(fGShaderID)); |
| 82 | } |
| 83 | if (fFShaderID) { |
| 84 | GL_CALL(DeleteShader(fFShaderID)); |
| 85 | } |
| 86 | if (fProgramID) { |
| 87 | GL_CALL(DeleteProgram(fProgramID)); |
| 88 | } |
| 89 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 90 | for (int i = 0; i < GrDrawState::kNumStages; ++i) { |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 91 | delete fEffects[i]; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 92 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 93 | } |
| 94 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 95 | void GrGLProgram::abandon() { |
| 96 | fVShaderID = 0; |
| 97 | fGShaderID = 0; |
| 98 | fFShaderID = 0; |
| 99 | fProgramID = 0; |
| 100 | } |
| 101 | |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 102 | void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 103 | GrBlendCoeff* dstCoeff) const { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 104 | switch (fDesc.fDualSrcOutput) { |
| 105 | case Desc::kNone_DualSrcOutput: |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 106 | break; |
| 107 | // the prog will write a coverage value to the secondary |
| 108 | // output and the dst is blended by one minus that value. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 109 | case Desc::kCoverage_DualSrcOutput: |
| 110 | case Desc::kCoverageISA_DualSrcOutput: |
| 111 | case Desc::kCoverageISC_DualSrcOutput: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 112 | *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 113 | break; |
| 114 | default: |
| 115 | GrCrash("Unexpected dual source blend output"); |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 120 | namespace { |
| 121 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 122 | // given two blend coeffecients determine whether the src |
| 123 | // and/or dst computation can be omitted. |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 124 | inline void need_blend_inputs(SkXfermode::Coeff srcCoeff, |
| 125 | SkXfermode::Coeff dstCoeff, |
| 126 | bool* needSrcValue, |
| 127 | bool* needDstValue) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 128 | if (SkXfermode::kZero_Coeff == srcCoeff) { |
| 129 | switch (dstCoeff) { |
| 130 | // these all read the src |
| 131 | case SkXfermode::kSC_Coeff: |
| 132 | case SkXfermode::kISC_Coeff: |
| 133 | case SkXfermode::kSA_Coeff: |
| 134 | case SkXfermode::kISA_Coeff: |
| 135 | *needSrcValue = true; |
| 136 | break; |
| 137 | default: |
| 138 | *needSrcValue = false; |
| 139 | break; |
| 140 | } |
| 141 | } else { |
| 142 | *needSrcValue = true; |
| 143 | } |
| 144 | if (SkXfermode::kZero_Coeff == dstCoeff) { |
| 145 | switch (srcCoeff) { |
| 146 | // these all read the dst |
| 147 | case SkXfermode::kDC_Coeff: |
| 148 | case SkXfermode::kIDC_Coeff: |
| 149 | case SkXfermode::kDA_Coeff: |
| 150 | case SkXfermode::kIDA_Coeff: |
| 151 | *needDstValue = true; |
| 152 | break; |
| 153 | default: |
| 154 | *needDstValue = false; |
| 155 | break; |
| 156 | } |
| 157 | } else { |
| 158 | *needDstValue = true; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 163 | * Create a blend_coeff * value string to be used in shader code. Sets empty |
| 164 | * string if result is trivially zero. |
| 165 | */ |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 166 | inline void blend_term_string(SkString* str, SkXfermode::Coeff coeff, |
| 167 | const char* src, const char* dst, |
| 168 | const char* value) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 169 | switch (coeff) { |
| 170 | case SkXfermode::kZero_Coeff: /** 0 */ |
| 171 | *str = ""; |
| 172 | break; |
| 173 | case SkXfermode::kOne_Coeff: /** 1 */ |
| 174 | *str = value; |
| 175 | break; |
| 176 | case SkXfermode::kSC_Coeff: |
| 177 | str->printf("(%s * %s)", src, value); |
| 178 | break; |
| 179 | case SkXfermode::kISC_Coeff: |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 180 | str->printf("((%s - %s) * %s)", GrGLSLOnesVecf(4), src, value); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 181 | break; |
| 182 | case SkXfermode::kDC_Coeff: |
| 183 | str->printf("(%s * %s)", dst, value); |
| 184 | break; |
| 185 | case SkXfermode::kIDC_Coeff: |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 186 | str->printf("((%s - %s) * %s)", GrGLSLOnesVecf(4), dst, value); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 187 | break; |
| 188 | case SkXfermode::kSA_Coeff: /** src alpha */ |
| 189 | str->printf("(%s.a * %s)", src, value); |
| 190 | break; |
| 191 | case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */ |
| 192 | str->printf("((1.0 - %s.a) * %s)", src, value); |
| 193 | break; |
| 194 | case SkXfermode::kDA_Coeff: /** dst alpha */ |
| 195 | str->printf("(%s.a * %s)", dst, value); |
| 196 | break; |
| 197 | case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */ |
| 198 | str->printf("((1.0 - %s.a) * %s)", dst, value); |
| 199 | break; |
| 200 | default: |
| 201 | GrCrash("Unexpected xfer coeff."); |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | /** |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 206 | * Adds a line to the fragment shader code which modifies the color by |
| 207 | * the specified color filter. |
| 208 | */ |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 209 | void add_color_filter(SkString* fsCode, const char * outputVar, |
| 210 | SkXfermode::Coeff uniformCoeff, |
| 211 | SkXfermode::Coeff colorCoeff, |
| 212 | const char* filterColor, |
| 213 | const char* inColor) { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 214 | SkString colorStr, constStr; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 215 | blend_term_string(&colorStr, colorCoeff, filterColor, inColor, inColor); |
| 216 | blend_term_string(&constStr, uniformCoeff, filterColor, inColor, filterColor); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 217 | |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 218 | fsCode->appendf("\t%s = ", outputVar); |
| 219 | GrGLSLAdd4f(fsCode, colorStr.c_str(), constStr.c_str()); |
| 220 | fsCode->append(";\n"); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 221 | } |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 222 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 223 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 224 | bool GrGLProgram::genEdgeCoverage(SkString* coverageVar, |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 225 | GrGLShaderBuilder* builder) const { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 226 | if (fDesc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit) { |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 227 | const char *vsName, *fsName; |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 228 | builder->addVarying(kVec4f_GrSLType, "Edge", &vsName, &fsName); |
| 229 | builder->fVSAttrs.push_back().set(kVec4f_GrSLType, |
| 230 | GrGLShaderVar::kAttribute_TypeModifier, |
| 231 | EDGE_ATTR_NAME); |
| 232 | builder->fVSCode.appendf("\t%s = " EDGE_ATTR_NAME ";\n", vsName); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 233 | switch (fDesc.fVertexEdgeType) { |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 234 | case GrDrawState::kHairLine_EdgeType: |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 235 | builder->fFSCode.appendf("\tfloat edgeAlpha = abs(dot(vec3(%s.xy,1), %s.xyz));\n", builder->fragmentPosition(), fsName); |
| 236 | builder->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 237 | break; |
| 238 | case GrDrawState::kQuad_EdgeType: |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 239 | builder->fFSCode.append("\tfloat edgeAlpha;\n"); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 240 | // keep the derivative instructions outside the conditional |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 241 | builder->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
| 242 | builder->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
| 243 | builder->fFSCode.appendf("\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsName, fsName); |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 244 | // today we know z and w are in device space. We could use derivatives |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 245 | builder->fFSCode.appendf("\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);\n", fsName, fsName); |
| 246 | builder->fFSCode.append ("\t} else {\n"); |
| 247 | builder->fFSCode.appendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n" |
| 248 | "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n", |
| 249 | fsName, fsName); |
| 250 | builder->fFSCode.appendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName); |
| 251 | builder->fFSCode.append("\t\tedgeAlpha = clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0);\n" |
| 252 | "\t}\n"); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 253 | if (kES2_GrGLBinding == fContextInfo.binding()) { |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 254 | builder->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n"); |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 255 | } |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 256 | break; |
| 257 | case GrDrawState::kHairQuad_EdgeType: |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 258 | builder->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
| 259 | builder->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
| 260 | builder->fFSCode.appendf("\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n" |
| 261 | "\t 2.0*%s.x*duvdy.x - duvdy.y);\n", |
| 262 | fsName, fsName); |
| 263 | builder->fFSCode.appendf("\tfloat edgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName); |
| 264 | builder->fFSCode.append("\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n"); |
| 265 | builder->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 266 | if (kES2_GrGLBinding == fContextInfo.binding()) { |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 267 | builder->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n"); |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 268 | } |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 269 | break; |
| 270 | case GrDrawState::kCircle_EdgeType: |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 271 | builder->fFSCode.append("\tfloat edgeAlpha;\n"); |
| 272 | builder->fFSCode.appendf("\tfloat d = distance(%s.xy, %s.xy);\n", builder->fragmentPosition(), fsName); |
| 273 | builder->fFSCode.appendf("\tfloat outerAlpha = smoothstep(d - 0.5, d + 0.5, %s.z);\n", fsName); |
| 274 | builder->fFSCode.appendf("\tfloat innerAlpha = %s.w == 0.0 ? 1.0 : smoothstep(%s.w - 0.5, %s.w + 0.5, d);\n", fsName, fsName, fsName); |
| 275 | builder->fFSCode.append("\tedgeAlpha = outerAlpha * innerAlpha;\n"); |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 276 | break; |
| 277 | default: |
| 278 | GrCrash("Unknown Edge Type!"); |
| 279 | break; |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 280 | } |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 281 | if (fDesc.fDiscardIfOutsideEdge) { |
robertphillips@google.com | ef818a4 | 2012-12-10 21:05:19 +0000 | [diff] [blame] | 282 | builder->fFSCode.appendf("\tif (edgeAlpha <= 0.0) {\n\t\tdiscard;\n\t}\n"); |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 283 | } |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 284 | *coverageVar = "edgeAlpha"; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 285 | return true; |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 286 | } else { |
| 287 | coverageVar->reset(); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 288 | return false; |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 292 | void GrGLProgram::genInputColor(GrGLShaderBuilder* builder, SkString* inColor) { |
| 293 | switch (fDesc.fColorInput) { |
| 294 | case GrGLProgram::Desc::kAttribute_ColorInput: { |
| 295 | builder->fVSAttrs.push_back().set(kVec4f_GrSLType, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 296 | GrGLShaderVar::kAttribute_TypeModifier, |
| 297 | COL_ATTR_NAME); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 298 | const char *vsName, *fsName; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 299 | builder->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsName); |
| 300 | builder->fVSCode.appendf("\t%s = " COL_ATTR_NAME ";\n", vsName); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 301 | *inColor = fsName; |
| 302 | } break; |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 303 | case GrGLProgram::Desc::kUniform_ColorInput: { |
| 304 | const char* name; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 305 | fUniformHandles.fColorUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 306 | kVec4f_GrSLType, "Color", &name); |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 307 | *inColor = name; |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 308 | break; |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 309 | } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 310 | case GrGLProgram::Desc::kTransBlack_ColorInput: |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 311 | GrAssert(!"needComputedColor should be false."); |
| 312 | break; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 313 | case GrGLProgram::Desc::kSolidWhite_ColorInput: |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 314 | break; |
| 315 | default: |
| 316 | GrCrash("Unknown color type."); |
| 317 | break; |
| 318 | } |
| 319 | } |
| 320 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 321 | void GrGLProgram::genUniformCoverage(GrGLShaderBuilder* builder, SkString* inOutCoverage) { |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 322 | const char* covUniName; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 323 | fUniformHandles.fCoverageUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 324 | kVec4f_GrSLType, "Coverage", &covUniName); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 325 | if (inOutCoverage->size()) { |
| 326 | builder->fFSCode.appendf("\tvec4 uniCoverage = %s * %s;\n", |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 327 | covUniName, inOutCoverage->c_str()); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 328 | *inOutCoverage = "uniCoverage"; |
| 329 | } else { |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 330 | *inOutCoverage = covUniName; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
| 334 | namespace { |
| 335 | void gen_attribute_coverage(GrGLShaderBuilder* segments, |
| 336 | SkString* inOutCoverage) { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 337 | segments->fVSAttrs.push_back().set(kVec4f_GrSLType, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 338 | GrGLShaderVar::kAttribute_TypeModifier, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 339 | COV_ATTR_NAME); |
| 340 | const char *vsName, *fsName; |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 341 | segments->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &fsName); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 342 | segments->fVSCode.appendf("\t%s = " COV_ATTR_NAME ";\n", vsName); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 343 | if (inOutCoverage->size()) { |
| 344 | segments->fFSCode.appendf("\tvec4 attrCoverage = %s * %s;\n", |
| 345 | fsName, inOutCoverage->c_str()); |
| 346 | *inOutCoverage = "attrCoverage"; |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 347 | } else { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 348 | *inOutCoverage = fsName; |
| 349 | } |
| 350 | } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 351 | } |
| 352 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 353 | void GrGLProgram::genGeometryShader(GrGLShaderBuilder* segments) const { |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 354 | #if GR_GL_EXPERIMENTAL_GS |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 355 | if (fDesc.fExperimentalGS) { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 356 | GrAssert(fContextInfo.glslGeneration() >= k150_GrGLSLGeneration); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 357 | segments->fGSHeader.append("layout(triangles) in;\n" |
| 358 | "layout(triangle_strip, max_vertices = 6) out;\n"); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 359 | segments->fGSCode.append("\tfor (int i = 0; i < 3; ++i) {\n" |
| 360 | "\t\tgl_Position = gl_in[i].gl_Position;\n"); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 361 | if (fDesc.fEmitsPointSize) { |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 362 | segments->fGSCode.append("\t\tgl_PointSize = 1.0;\n"); |
| 363 | } |
| 364 | GrAssert(segments->fGSInputs.count() == segments->fGSOutputs.count()); |
| 365 | int count = segments->fGSInputs.count(); |
| 366 | for (int i = 0; i < count; ++i) { |
| 367 | segments->fGSCode.appendf("\t\t%s = %s[i];\n", |
| 368 | segments->fGSOutputs[i].getName().c_str(), |
| 369 | segments->fGSInputs[i].getName().c_str()); |
| 370 | } |
| 371 | segments->fGSCode.append("\t\tEmitVertex();\n" |
| 372 | "\t}\n" |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 373 | "\tEndPrimitive();\n"); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 374 | } |
| 375 | #endif |
| 376 | } |
| 377 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 378 | const char* GrGLProgram::adjustInColor(const SkString& inColor) const { |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 379 | if (inColor.size()) { |
| 380 | return inColor.c_str(); |
| 381 | } else { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 382 | if (Desc::kSolidWhite_ColorInput == fDesc.fColorInput) { |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 383 | return GrGLSLOnesVecf(4); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 384 | } else { |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 385 | return GrGLSLZerosVecf(4); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 390 | namespace { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 391 | // prints a shader using params similar to glShaderSource |
| 392 | void print_shader(GrGLint stringCnt, |
| 393 | const GrGLchar** strings, |
| 394 | GrGLint* stringLengths) { |
| 395 | for (int i = 0; i < stringCnt; ++i) { |
| 396 | if (NULL == stringLengths || stringLengths[i] < 0) { |
| 397 | GrPrintf(strings[i]); |
| 398 | } else { |
| 399 | GrPrintf("%.*s", stringLengths[i], strings[i]); |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | // Compiles a GL shader, returns shader ID or 0 if failed params have same meaning as glShaderSource |
| 405 | GrGLuint compile_shader(const GrGLContextInfo& gl, |
| 406 | GrGLenum type, |
| 407 | int stringCnt, |
| 408 | const char** strings, |
| 409 | int* stringLengths) { |
| 410 | SK_TRACE_EVENT1("GrGLProgram::CompileShader", |
| 411 | "stringCount", SkStringPrintf("%i", stringCnt).c_str()); |
| 412 | |
| 413 | GrGLuint shader; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 414 | GR_GL_CALL_RET(gl.interface(), shader, CreateShader(type)); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 415 | if (0 == shader) { |
| 416 | return 0; |
| 417 | } |
| 418 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 419 | const GrGLInterface* gli = gl.interface(); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 420 | GrGLint compiled = GR_GL_INIT_ZERO; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 421 | GR_GL_CALL(gli, ShaderSource(shader, stringCnt, strings, stringLengths)); |
| 422 | GR_GL_CALL(gli, CompileShader(shader)); |
| 423 | GR_GL_CALL(gli, GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled)); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 424 | |
| 425 | if (!compiled) { |
| 426 | GrGLint infoLen = GR_GL_INIT_ZERO; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 427 | 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] | 428 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 429 | if (infoLen > 0) { |
| 430 | // retrieve length even though we don't need it to workaround bug in chrome cmd buffer |
| 431 | // param validation. |
| 432 | GrGLsizei length = GR_GL_INIT_ZERO; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 433 | GR_GL_CALL(gli, GetShaderInfoLog(shader, infoLen+1, |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 434 | &length, (char*)log.get())); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 435 | print_shader(stringCnt, strings, stringLengths); |
| 436 | GrPrintf("\n%s", log.get()); |
| 437 | } |
| 438 | GrAssert(!"Shader compilation failed!"); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 439 | GR_GL_CALL(gli, DeleteShader(shader)); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | return shader; |
| 443 | } |
| 444 | |
| 445 | // helper version of above for when shader is already flattened into a single SkString |
| 446 | GrGLuint compile_shader(const GrGLContextInfo& gl, GrGLenum type, const SkString& shader) { |
| 447 | const GrGLchar* str = shader.c_str(); |
| 448 | int length = shader.size(); |
| 449 | return compile_shader(gl, type, 1, &str, &length); |
| 450 | } |
| 451 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | // compiles all the shaders from builder and stores the shader IDs |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 455 | bool GrGLProgram::compileShaders(const GrGLShaderBuilder& builder) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 456 | |
| 457 | SkString shader; |
| 458 | |
| 459 | builder.getShader(GrGLShaderBuilder::kVertex_ShaderType, &shader); |
| 460 | #if PRINT_SHADERS |
| 461 | GrPrintf(shader.c_str()); |
| 462 | GrPrintf("\n"); |
| 463 | #endif |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 464 | if (!(fVShaderID = compile_shader(fContextInfo, GR_GL_VERTEX_SHADER, shader))) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 465 | return false; |
| 466 | } |
| 467 | |
| 468 | if (builder.fUsesGS) { |
| 469 | builder.getShader(GrGLShaderBuilder::kGeometry_ShaderType, &shader); |
| 470 | #if PRINT_SHADERS |
| 471 | GrPrintf(shader.c_str()); |
| 472 | GrPrintf("\n"); |
| 473 | #endif |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 474 | if (!(fGShaderID = compile_shader(fContextInfo, GR_GL_GEOMETRY_SHADER, shader))) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 475 | return false; |
| 476 | } |
| 477 | } else { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 478 | fGShaderID = 0; |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | builder.getShader(GrGLShaderBuilder::kFragment_ShaderType, &shader); |
| 482 | #if PRINT_SHADERS |
| 483 | GrPrintf(shader.c_str()); |
| 484 | GrPrintf("\n"); |
| 485 | #endif |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 486 | if (!(fFShaderID = compile_shader(fContextInfo, GR_GL_FRAGMENT_SHADER, shader))) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 487 | return false; |
| 488 | } |
| 489 | |
| 490 | return true; |
| 491 | } |
| 492 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 493 | bool GrGLProgram::genProgram(const GrEffectStage* stages[]) { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 494 | GrAssert(0 == fProgramID); |
| 495 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 496 | GrGLShaderBuilder builder(fContextInfo, fUniformManager); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 497 | const uint32_t& layout = fDesc.fVertexLayout; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 498 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 499 | #if GR_GL_EXPERIMENTAL_GS |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 500 | builder.fUsesGS = fDesc.fExperimentalGS; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 501 | #endif |
| 502 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 503 | SkXfermode::Coeff colorCoeff, uniformCoeff; |
| 504 | // The rest of transfer mode color filters have not been implemented |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 505 | if (fDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 506 | GR_DEBUGCODE(bool success =) |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 507 | SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode> |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 508 | (fDesc.fColorFilterXfermode), |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 509 | &uniformCoeff, &colorCoeff); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 510 | GR_DEBUGASSERT(success); |
| 511 | } else { |
| 512 | colorCoeff = SkXfermode::kOne_Coeff; |
| 513 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 514 | } |
| 515 | |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 516 | // no need to do the color filter if coverage is 0. The output color is scaled by the coverage. |
| 517 | // All the dual source outputs are scaled by the coverage as well. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 518 | if (Desc::kTransBlack_ColorInput == fDesc.fCoverageInput) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 519 | colorCoeff = SkXfermode::kZero_Coeff; |
| 520 | uniformCoeff = SkXfermode::kZero_Coeff; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 521 | } |
| 522 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 523 | // If we know the final color is going to be all zeros then we can |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 524 | // simplify the color filter coefficients. needComputedColor will then |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 525 | // come out false below. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 526 | if (Desc::kTransBlack_ColorInput == fDesc.fColorInput) { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 527 | colorCoeff = SkXfermode::kZero_Coeff; |
| 528 | if (SkXfermode::kDC_Coeff == uniformCoeff || |
| 529 | SkXfermode::kDA_Coeff == uniformCoeff) { |
| 530 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 531 | } else if (SkXfermode::kIDC_Coeff == uniformCoeff || |
| 532 | SkXfermode::kIDA_Coeff == uniformCoeff) { |
| 533 | uniformCoeff = SkXfermode::kOne_Coeff; |
| 534 | } |
| 535 | } |
| 536 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 537 | bool needColorFilterUniform; |
| 538 | bool needComputedColor; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 539 | need_blend_inputs(uniformCoeff, colorCoeff, |
| 540 | &needColorFilterUniform, &needComputedColor); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 541 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 542 | // the dual source output has no canonical var name, have to |
| 543 | // declare an output, which is incompatible with gl_FragColor/gl_FragData. |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 544 | bool dualSourceOutputWritten = false; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 545 | builder.fHeader.append(GrGetGLSLVersionDecl(fContextInfo.binding(), |
| 546 | fContextInfo.glslGeneration())); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 547 | |
| 548 | GrGLShaderVar colorOutput; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 549 | bool isColorDeclared = GrGLSLSetupFSColorOuput(fContextInfo.glslGeneration(), |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 550 | declared_color_output_name(), |
| 551 | &colorOutput); |
| 552 | if (isColorDeclared) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 553 | builder.fFSOutputs.push_back(colorOutput); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 554 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 555 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 556 | const char* viewMName; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 557 | fUniformHandles.fViewMatrixUni = builder.addUniform(GrGLShaderBuilder::kVertex_ShaderType, |
| 558 | kMat33f_GrSLType, "ViewM", &viewMName); |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 559 | |
skia.committer@gmail.com | e862d16 | 2012-10-31 02:01:18 +0000 | [diff] [blame] | 560 | |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 561 | builder.fVSCode.appendf("\tvec3 pos3 = %s * vec3(%s, 1);\n" |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 562 | "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n", |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 563 | viewMName, builder.positionAttribute().getName().c_str()); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 564 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 565 | // incoming color to current stage being processed. |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 566 | SkString inColor; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 567 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 568 | if (needComputedColor) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 569 | this->genInputColor(&builder, &inColor); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 570 | } |
| 571 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 572 | // we output point size in the GS if present |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 573 | if (fDesc.fEmitsPointSize && !builder.fUsesGS){ |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 574 | builder.fVSCode.append("\tgl_PointSize = 1.0;\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 575 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 576 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 577 | // 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] | 578 | SkString texCoordAttrs[GrDrawState::kMaxTexCoords]; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 579 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 580 | if (GrDrawTarget::VertexUsesTexCoordIdx(t, layout)) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 581 | tex_attr_name(t, texCoordAttrs + t); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 582 | builder.fVSAttrs.push_back().set(kVec2f_GrSLType, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 583 | GrGLShaderVar::kAttribute_TypeModifier, |
| 584 | texCoordAttrs[t].c_str()); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 588 | /////////////////////////////////////////////////////////////////////////// |
| 589 | // compute the final color |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 590 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 591 | // if we have color stages string them together, feeding the output color |
| 592 | // of each to the next and generating code for each stage. |
| 593 | if (needComputedColor) { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 594 | SkString outColor; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 595 | for (int s = 0; s < fDesc.fFirstCoverageStage; ++s) { |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 596 | if (GrGLEffect::kNoEffectKey != fDesc.fEffectKeys[s]) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 597 | // create var to hold stage result |
| 598 | outColor = "color"; |
| 599 | outColor.appendS32(s); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 600 | builder.fFSCode.appendf("\tvec4 %s;\n", outColor.c_str()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 601 | |
| 602 | const char* inCoords; |
| 603 | // figure out what our input coords are |
tomhudson@google.com | b213ed8 | 2012-06-25 15:22:12 +0000 | [diff] [blame] | 604 | int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout); |
| 605 | if (tcIdx < 0) { |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 606 | inCoords = builder.positionAttribute().c_str(); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 607 | } else { |
tomhudson@google.com | b213ed8 | 2012-06-25 15:22:12 +0000 | [diff] [blame] | 608 | // must have input tex coordinates if stage is enabled. |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 609 | GrAssert(texCoordAttrs[tcIdx].size()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 610 | inCoords = texCoordAttrs[tcIdx].c_str(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 611 | } |
| 612 | |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 613 | builder.setCurrentStage(s); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 614 | fEffects[s] = builder.createAndEmitGLEffect(*stages[s], |
| 615 | fDesc.fEffectKeys[s], |
| 616 | inColor.size() ? inColor.c_str() : NULL, |
| 617 | outColor.c_str(), |
| 618 | inCoords, |
| 619 | &fUniformHandles.fSamplerUnis[s]); |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 620 | builder.setNonStage(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 621 | inColor = outColor; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 622 | } |
| 623 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 624 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 625 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 626 | // if have all ones or zeros for the "dst" input to the color filter then we |
| 627 | // may be able to make additional optimizations. |
| 628 | if (needColorFilterUniform && needComputedColor && !inColor.size()) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 629 | GrAssert(Desc::kSolidWhite_ColorInput == fDesc.fColorInput); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 630 | bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff || |
| 631 | SkXfermode::kIDA_Coeff == uniformCoeff; |
| 632 | if (uniformCoeffIsZero) { |
| 633 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 634 | bool bogus; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 635 | need_blend_inputs(SkXfermode::kZero_Coeff, colorCoeff, |
| 636 | &needColorFilterUniform, &bogus); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 637 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 638 | } |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 639 | const char* colorFilterColorUniName = NULL; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 640 | if (needColorFilterUniform) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 641 | fUniformHandles.fColorFilterUni = builder.addUniform( |
| 642 | GrGLShaderBuilder::kFragment_ShaderType, |
| 643 | kVec4f_GrSLType, "FilterColor", |
| 644 | &colorFilterColorUniName); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 645 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 646 | bool wroteFragColorZero = false; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 647 | if (SkXfermode::kZero_Coeff == uniformCoeff && |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 648 | SkXfermode::kZero_Coeff == colorCoeff) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 649 | builder.fFSCode.appendf("\t%s = %s;\n", |
| 650 | colorOutput.getName().c_str(), |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 651 | GrGLSLZerosVecf(4)); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 652 | wroteFragColorZero = true; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 653 | } else if (SkXfermode::kDst_Mode != fDesc.fColorFilterXfermode) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 654 | builder.fFSCode.append("\tvec4 filteredColor;\n"); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 655 | const char* color = adjustInColor(inColor); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 656 | add_color_filter(&builder.fFSCode, "filteredColor", uniformCoeff, |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 657 | colorCoeff, colorFilterColorUniName, color); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 658 | inColor = "filteredColor"; |
| 659 | } |
| 660 | |
| 661 | /////////////////////////////////////////////////////////////////////////// |
| 662 | // compute the partial coverage (coverage stages and edge aa) |
| 663 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 664 | SkString inCoverage; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 665 | bool coverageIsZero = Desc::kTransBlack_ColorInput == fDesc.fCoverageInput; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 666 | // we don't need to compute coverage at all if we know the final shader |
| 667 | // 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] | 668 | if (!wroteFragColorZero || Desc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) { |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 669 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 670 | if (!coverageIsZero) { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 671 | bool inCoverageIsScalar = this->genEdgeCoverage(&inCoverage, &builder); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 672 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 673 | switch (fDesc.fCoverageInput) { |
| 674 | case Desc::kSolidWhite_ColorInput: |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 675 | // empty string implies solid white |
| 676 | break; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 677 | case Desc::kAttribute_ColorInput: |
| 678 | gen_attribute_coverage(&builder, &inCoverage); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 679 | inCoverageIsScalar = false; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 680 | break; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 681 | case Desc::kUniform_ColorInput: |
| 682 | this->genUniformCoverage(&builder, &inCoverage); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 683 | inCoverageIsScalar = false; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 684 | break; |
| 685 | default: |
| 686 | GrCrash("Unexpected input coverage."); |
| 687 | } |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 688 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 689 | SkString outCoverage; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 690 | const int& startStage = fDesc.fFirstCoverageStage; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 691 | for (int s = startStage; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 692 | if (fDesc.fEffectKeys[s]) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 693 | // create var to hold stage output |
| 694 | outCoverage = "coverage"; |
| 695 | outCoverage.appendS32(s); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 696 | builder.fFSCode.appendf("\tvec4 %s;\n", outCoverage.c_str()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 697 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 698 | const char* inCoords; |
| 699 | // figure out what our input coords are |
tomhudson@google.com | b213ed8 | 2012-06-25 15:22:12 +0000 | [diff] [blame] | 700 | int tcIdx = |
| 701 | GrDrawTarget::VertexTexCoordsForStage(s, layout); |
| 702 | if (tcIdx < 0) { |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 703 | inCoords = builder.positionAttribute().c_str(); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 704 | } else { |
tomhudson@google.com | b213ed8 | 2012-06-25 15:22:12 +0000 | [diff] [blame] | 705 | // must have input tex coordinates if stage is |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 706 | // enabled. |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 707 | GrAssert(texCoordAttrs[tcIdx].size()); |
| 708 | inCoords = texCoordAttrs[tcIdx].c_str(); |
| 709 | } |
| 710 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 711 | // stages don't know how to deal with a scalar input. (Maybe they should. We |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 712 | // could pass a GrGLShaderVar) |
| 713 | if (inCoverageIsScalar) { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 714 | builder.fFSCode.appendf("\tvec4 %s4 = vec4(%s);\n", |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 715 | inCoverage.c_str(), inCoverage.c_str()); |
| 716 | inCoverage.append("4"); |
| 717 | } |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 718 | builder.setCurrentStage(s); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 719 | fEffects[s] = builder.createAndEmitGLEffect( |
| 720 | *stages[s], |
| 721 | fDesc.fEffectKeys[s], |
| 722 | inCoverage.size() ? inCoverage.c_str() : NULL, |
| 723 | outCoverage.c_str(), |
| 724 | inCoords, |
| 725 | &fUniformHandles.fSamplerUnis[s]); |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 726 | builder.setNonStage(); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 727 | inCoverage = outCoverage; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 728 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 731 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 732 | if (Desc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 733 | builder.fFSOutputs.push_back().set(kVec4f_GrSLType, |
| 734 | GrGLShaderVar::kOut_TypeModifier, |
| 735 | dual_source_output_name()); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 736 | bool outputIsZero = coverageIsZero; |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 737 | SkString coeff; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 738 | if (!outputIsZero && |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 739 | Desc::kCoverage_DualSrcOutput != fDesc.fDualSrcOutput && !wroteFragColorZero) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 740 | if (!inColor.size()) { |
| 741 | outputIsZero = true; |
| 742 | } else { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 743 | if (Desc::kCoverageISA_DualSrcOutput == fDesc.fDualSrcOutput) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 744 | coeff.printf("(1 - %s.a)", inColor.c_str()); |
| 745 | } else { |
| 746 | coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str()); |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | if (outputIsZero) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 751 | builder.fFSCode.appendf("\t%s = %s;\n", |
| 752 | dual_source_output_name(), |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 753 | GrGLSLZerosVecf(4)); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 754 | } else { |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 755 | builder.fFSCode.appendf("\t%s =", dual_source_output_name()); |
| 756 | GrGLSLModulate4f(&builder.fFSCode, coeff.c_str(), inCoverage.c_str()); |
| 757 | builder.fFSCode.append(";\n"); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 758 | } |
| 759 | dualSourceOutputWritten = true; |
| 760 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 761 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 762 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 763 | /////////////////////////////////////////////////////////////////////////// |
| 764 | // combine color and coverage as frag color |
| 765 | |
| 766 | if (!wroteFragColorZero) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 767 | if (coverageIsZero) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 768 | builder.fFSCode.appendf("\t%s = %s;\n", |
| 769 | colorOutput.getName().c_str(), |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 770 | GrGLSLZerosVecf(4)); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 771 | } else { |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 772 | builder.fFSCode.appendf("\t%s = ", colorOutput.getName().c_str()); |
| 773 | GrGLSLModulate4f(&builder.fFSCode, inColor.c_str(), inCoverage.c_str()); |
| 774 | builder.fFSCode.append(";\n"); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 775 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 776 | } |
| 777 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 778 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 779 | // insert GS |
| 780 | #if GR_DEBUG |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 781 | this->genGeometryShader(&builder); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 782 | #endif |
| 783 | |
| 784 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 785 | // compile and setup attribs and unis |
| 786 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 787 | if (!this->compileShaders(builder)) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 788 | return false; |
| 789 | } |
| 790 | |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 791 | if (!this->bindOutputsAttribsAndLinkProgram(builder, |
| 792 | texCoordAttrs, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 793 | isColorDeclared, |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 794 | dualSourceOutputWritten)) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 795 | return false; |
| 796 | } |
| 797 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 798 | builder.finished(fProgramID); |
| 799 | this->initSamplerUniforms(); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 800 | fUniformHandles.fRTHeightUni = builder.getRTHeightUniform(); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 801 | |
| 802 | return true; |
| 803 | } |
| 804 | |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 805 | bool GrGLProgram::bindOutputsAttribsAndLinkProgram(const GrGLShaderBuilder& builder, |
| 806 | SkString texCoordAttrNames[], |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 807 | bool bindColorOut, |
| 808 | bool bindDualSrcOut) { |
| 809 | GL_CALL_RET(fProgramID, CreateProgram()); |
| 810 | if (!fProgramID) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 811 | return false; |
| 812 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 813 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 814 | GL_CALL(AttachShader(fProgramID, fVShaderID)); |
| 815 | if (fGShaderID) { |
| 816 | GL_CALL(AttachShader(fProgramID, fGShaderID)); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 817 | } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 818 | GL_CALL(AttachShader(fProgramID, fFShaderID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 819 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 820 | if (bindColorOut) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 821 | GL_CALL(BindFragDataLocation(fProgramID, 0, declared_color_output_name())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 822 | } |
| 823 | if (bindDualSrcOut) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 824 | GL_CALL(BindFragDataLocationIndexed(fProgramID, 0, 1, dual_source_output_name())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 825 | } |
| 826 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 827 | // Bind the attrib locations to same values for all shaders |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 828 | GL_CALL(BindAttribLocation(fProgramID, |
| 829 | PositionAttributeIdx(), |
| 830 | builder.positionAttribute().c_str())); |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 831 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 832 | if (texCoordAttrNames[t].size()) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 833 | GL_CALL(BindAttribLocation(fProgramID, |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 834 | TexCoordAttributeIdx(t), |
| 835 | texCoordAttrNames[t].c_str())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 839 | GL_CALL(BindAttribLocation(fProgramID, ColorAttributeIdx(), COL_ATTR_NAME)); |
| 840 | GL_CALL(BindAttribLocation(fProgramID, CoverageAttributeIdx(), COV_ATTR_NAME)); |
| 841 | GL_CALL(BindAttribLocation(fProgramID, EdgeAttributeIdx(), EDGE_ATTR_NAME)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 842 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 843 | GL_CALL(LinkProgram(fProgramID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 844 | |
| 845 | GrGLint linked = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 846 | GL_CALL(GetProgramiv(fProgramID, GR_GL_LINK_STATUS, &linked)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 847 | if (!linked) { |
| 848 | GrGLint infoLen = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 849 | GL_CALL(GetProgramiv(fProgramID, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 850 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 851 | if (infoLen > 0) { |
bsalomon@google.com | 79afcaa | 2011-09-14 14:29:18 +0000 | [diff] [blame] | 852 | // retrieve length even though we don't need it to workaround |
| 853 | // bug in chrome cmd buffer param validation. |
| 854 | GrGLsizei length = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 855 | GL_CALL(GetProgramInfoLog(fProgramID, |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 856 | infoLen+1, |
| 857 | &length, |
| 858 | (char*)log.get())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 859 | GrPrintf((char*)log.get()); |
| 860 | } |
| 861 | GrAssert(!"Error linking program"); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 862 | GL_CALL(DeleteProgram(fProgramID)); |
| 863 | fProgramID = 0; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 864 | return false; |
| 865 | } |
| 866 | return true; |
| 867 | } |
| 868 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 869 | void GrGLProgram::initSamplerUniforms() { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 870 | GL_CALL(UseProgram(fProgramID)); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 871 | // We simply bind the uniforms to successive texture units beginning at 0. setData() assumes this |
| 872 | // behavior. |
| 873 | GrGLint texUnitIdx = 0; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 874 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 875 | int numSamplers = fUniformHandles.fSamplerUnis[s].count(); |
| 876 | for (int u = 0; u < numSamplers; ++u) { |
| 877 | UniformHandle handle = fUniformHandles.fSamplerUnis[s][u]; |
| 878 | if (GrGLUniformManager::kInvalidUniformHandle != handle) { |
| 879 | fUniformManager.setSampler(handle, texUnitIdx); |
| 880 | ++texUnitIdx; |
bsalomon@google.com | 0982d35 | 2012-07-31 15:33:25 +0000 | [diff] [blame] | 881 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 882 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 883 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 884 | } |
| 885 | |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 886 | /////////////////////////////////////////////////////////////////////////////// |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 887 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 888 | void GrGLProgram::setData(GrGpuGL* gpu) { |
| 889 | const GrDrawState& drawState = gpu->getDrawState(); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 890 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 891 | int rtHeight = drawState.getRenderTarget()->height(); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 892 | if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fRTHeightUni && |
| 893 | fRTHeight != rtHeight) { |
| 894 | fUniformManager.set1f(fUniformHandles.fRTHeightUni, SkIntToScalar(rtHeight)); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 895 | fRTHeight = rtHeight; |
| 896 | } |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 897 | GrGLint texUnitIdx = 0; |
bsalomon@google.com | 4285acc | 2012-10-22 14:11:24 +0000 | [diff] [blame] | 898 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 899 | if (NULL != fEffects[s]) { |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 900 | const GrEffectStage& stage = drawState.getStage(s); |
| 901 | GrAssert(NULL != stage.getEffect()); |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 902 | fEffects[s]->setData(fUniformManager, stage); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 903 | int numSamplers = fUniformHandles.fSamplerUnis[s].count(); |
| 904 | for (int u = 0; u < numSamplers; ++u) { |
| 905 | UniformHandle handle = fUniformHandles.fSamplerUnis[s][u]; |
| 906 | if (GrGLUniformManager::kInvalidUniformHandle != handle) { |
| 907 | const GrTextureAccess& access = stage.getEffect()->textureAccess(u); |
| 908 | GrGLTexture* texture = static_cast<GrGLTexture*>(access.getTexture()); |
| 909 | gpu->bindTexture(texUnitIdx, access.getParams(), texture); |
| 910 | ++texUnitIdx; |
| 911 | } |
| 912 | } |
bsalomon@google.com | 4285acc | 2012-10-22 14:11:24 +0000 | [diff] [blame] | 913 | } |
| 914 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 915 | } |