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