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 | |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 19 | #include "SkRTConf.h" |
| 20 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 21 | SK_DEFINE_INST_COUNT(GrGLProgram) |
| 22 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 23 | #define GL_CALL(X) GR_GL_CALL(fContextInfo.interface(), X) |
| 24 | #define GL_CALL_RET(R, X) GR_GL_CALL_RET(fContextInfo.interface(), R, X) |
| 25 | |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 26 | SK_CONF_DECLARE(bool, c_PrintShaders, "gpu.printShaders", false, "Print the source code for all shaders generated."); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 27 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 28 | #define COL_ATTR_NAME "aColor" |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 29 | #define COV_ATTR_NAME "aCoverage" |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 30 | #define EDGE_ATTR_NAME "aEdge" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 31 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 32 | namespace { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 33 | inline void tex_attr_name(int coordIdx, SkString* s) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 34 | *s = "aTexCoord"; |
bsalomon@google.com | fc29629 | 2011-05-06 13:53:47 +0000 | [diff] [blame] | 35 | s->appendS32(coordIdx); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 36 | } |
| 37 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 38 | inline const char* declared_color_output_name() { return "fsColorOut"; } |
| 39 | inline const char* dual_source_output_name() { return "dualSourceOut"; } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 40 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 41 | } |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 42 | |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 43 | void GrGLProgram::BuildDesc(const GrDrawState& drawState, |
| 44 | bool isPoints, |
| 45 | GrDrawState::BlendOptFlags blendOpts, |
| 46 | GrBlendCoeff srcCoeff, |
| 47 | GrBlendCoeff dstCoeff, |
| 48 | const GrGpuGL* gpu, |
| 49 | Desc* desc) { |
| 50 | |
| 51 | // This should already have been caught |
| 52 | GrAssert(!(GrDrawState::kSkipDraw_BlendOptFlag & blendOpts)); |
| 53 | |
| 54 | bool skipCoverage = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag); |
| 55 | |
| 56 | bool skipColor = SkToBool(blendOpts & (GrDrawState::kEmitTransBlack_BlendOptFlag | |
| 57 | GrDrawState::kEmitCoverage_BlendOptFlag)); |
| 58 | |
| 59 | // The descriptor is used as a cache key. Thus when a field of the |
| 60 | // descriptor will not affect program generation (because of the vertex |
| 61 | // layout in use or other descriptor field settings) it should be set |
| 62 | // to a canonical value to avoid duplicate programs with different keys. |
| 63 | |
| 64 | // Must initialize all fields or cache will have false negatives! |
| 65 | desc->fVertexLayout = drawState.getVertexLayout(); |
| 66 | |
| 67 | desc->fEmitsPointSize = isPoints; |
| 68 | |
| 69 | bool requiresAttributeColors = !skipColor && |
| 70 | SkToBool(desc->fVertexLayout & GrDrawState::kColor_VertexLayoutBit); |
| 71 | bool requiresAttributeCoverage = !skipCoverage && |
| 72 | SkToBool(desc->fVertexLayout & GrDrawState::kCoverage_VertexLayoutBit); |
| 73 | |
| 74 | // fColorInput/fCoverageInput records how colors are specified for the program So we strip the |
| 75 | // bits from the layout to avoid false negatives when searching for an existing program in the |
| 76 | // cache. |
| 77 | desc->fVertexLayout &= ~(GrDrawState::kColor_VertexLayoutBit | GrDrawState::kCoverage_VertexLayoutBit); |
| 78 | |
| 79 | desc->fColorFilterXfermode = skipColor ? |
| 80 | SkXfermode::kDst_Mode : |
| 81 | drawState.getColorFilterMode(); |
| 82 | |
| 83 | // no reason to do edge aa or look at per-vertex coverage if coverage is ignored |
| 84 | if (skipCoverage) { |
| 85 | desc->fVertexLayout &= ~(GrDrawState::kEdge_VertexLayoutBit | |
| 86 | GrDrawState::kCoverage_VertexLayoutBit); |
| 87 | } |
| 88 | |
| 89 | bool colorIsTransBlack = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag); |
| 90 | bool colorIsSolidWhite = (blendOpts & GrDrawState::kEmitCoverage_BlendOptFlag) || |
| 91 | (!requiresAttributeColors && 0xffffffff == drawState.getColor()); |
| 92 | if (colorIsTransBlack) { |
| 93 | desc->fColorInput = Desc::kTransBlack_ColorInput; |
| 94 | } else if (colorIsSolidWhite) { |
| 95 | desc->fColorInput = Desc::kSolidWhite_ColorInput; |
| 96 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) { |
| 97 | desc->fColorInput = Desc::kUniform_ColorInput; |
| 98 | } else { |
| 99 | desc->fColorInput = Desc::kAttribute_ColorInput; |
| 100 | } |
| 101 | |
| 102 | bool covIsSolidWhite = !requiresAttributeCoverage && 0xffffffff == drawState.getCoverage(); |
| 103 | |
| 104 | if (skipCoverage) { |
| 105 | desc->fCoverageInput = Desc::kTransBlack_ColorInput; |
| 106 | } else if (covIsSolidWhite) { |
| 107 | desc->fCoverageInput = Desc::kSolidWhite_ColorInput; |
| 108 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) { |
| 109 | desc->fCoverageInput = Desc::kUniform_ColorInput; |
| 110 | } else { |
| 111 | desc->fCoverageInput = Desc::kAttribute_ColorInput; |
| 112 | } |
| 113 | |
| 114 | int lastEnabledStage = -1; |
| 115 | |
| 116 | if (!skipCoverage && (desc->fVertexLayout & GrDrawState::kEdge_VertexLayoutBit)) { |
| 117 | desc->fVertexEdgeType = drawState.getVertexEdgeType(); |
| 118 | desc->fDiscardIfOutsideEdge = drawState.getStencil().doesWrite(); |
| 119 | } else { |
| 120 | // Use canonical values when edge-aa is not enabled to avoid program cache misses. |
| 121 | desc->fVertexEdgeType = GrDrawState::kHairLine_EdgeType; |
| 122 | desc->fDiscardIfOutsideEdge = false; |
| 123 | } |
| 124 | |
| 125 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 126 | |
| 127 | bool skip = s < drawState.getFirstCoverageStage() ? skipColor : skipCoverage; |
| 128 | if (!skip && drawState.isStageEnabled(s)) { |
| 129 | lastEnabledStage = s; |
| 130 | const GrEffectRef& effect = *drawState.getStage(s).getEffect(); |
| 131 | const GrBackendEffectFactory& factory = effect->getFactory(); |
| 132 | desc->fEffectKeys[s] = factory.glEffectKey(drawState.getStage(s), gpu->glCaps()); |
| 133 | } else { |
| 134 | desc->fEffectKeys[s] = 0; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | desc->fDualSrcOutput = Desc::kNone_DualSrcOutput; |
| 139 | |
| 140 | // Currently the experimental GS will only work with triangle prims (and it doesn't do anything |
| 141 | // other than pass through values from the VS to the FS anyway). |
| 142 | #if GR_GL_EXPERIMENTAL_GS |
| 143 | #if 0 |
| 144 | desc->fExperimentalGS = gpu->getCaps().geometryShaderSupport(); |
| 145 | #else |
| 146 | desc->fExperimentalGS = false; |
| 147 | #endif |
| 148 | #endif |
| 149 | |
| 150 | // We want to avoid generating programs with different "first cov stage" values when they would |
| 151 | // compute the same result. We set field in the desc to kNumStages when either there are no |
| 152 | // coverage stages or the distinction between coverage and color is immaterial. |
| 153 | int firstCoverageStage = GrDrawState::kNumStages; |
| 154 | desc->fFirstCoverageStage = GrDrawState::kNumStages; |
| 155 | bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage; |
| 156 | if (hasCoverage) { |
| 157 | firstCoverageStage = drawState.getFirstCoverageStage(); |
| 158 | } |
| 159 | |
| 160 | // other coverage inputs |
| 161 | if (!hasCoverage) { |
| 162 | hasCoverage = requiresAttributeCoverage || |
| 163 | (desc->fVertexLayout & GrDrawState::kEdge_VertexLayoutBit); |
| 164 | } |
| 165 | |
| 166 | if (hasCoverage) { |
| 167 | // color filter is applied between color/coverage computation |
| 168 | if (SkXfermode::kDst_Mode != desc->fColorFilterXfermode) { |
| 169 | desc->fFirstCoverageStage = firstCoverageStage; |
| 170 | } |
| 171 | |
| 172 | if (gpu->getCaps().dualSourceBlendingSupport() && |
| 173 | !(blendOpts & (GrDrawState::kEmitCoverage_BlendOptFlag | |
| 174 | GrDrawState::kCoverageAsAlpha_BlendOptFlag))) { |
| 175 | if (kZero_GrBlendCoeff == dstCoeff) { |
| 176 | // write the coverage value to second color |
| 177 | desc->fDualSrcOutput = Desc::kCoverage_DualSrcOutput; |
| 178 | desc->fFirstCoverageStage = firstCoverageStage; |
| 179 | } else if (kSA_GrBlendCoeff == dstCoeff) { |
| 180 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered. |
| 181 | desc->fDualSrcOutput = Desc::kCoverageISA_DualSrcOutput; |
| 182 | desc->fFirstCoverageStage = firstCoverageStage; |
| 183 | } else if (kSC_GrBlendCoeff == dstCoeff) { |
| 184 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially covered. |
| 185 | desc->fDualSrcOutput = Desc::kCoverageISC_DualSrcOutput; |
| 186 | desc->fFirstCoverageStage = firstCoverageStage; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 192 | GrGLProgram* GrGLProgram::Create(const GrGLContextInfo& gl, |
| 193 | const Desc& desc, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 194 | const GrEffectStage* stages[]) { |
| 195 | GrGLProgram* program = SkNEW_ARGS(GrGLProgram, (gl, desc, stages)); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 196 | if (!program->succeeded()) { |
| 197 | delete program; |
| 198 | program = NULL; |
| 199 | } |
| 200 | return program; |
| 201 | } |
| 202 | |
| 203 | GrGLProgram::GrGLProgram(const GrGLContextInfo& gl, |
| 204 | const Desc& desc, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 205 | const GrEffectStage* stages[]) |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 206 | : fContextInfo(gl) |
| 207 | , fUniformManager(gl) { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 208 | fDesc = desc; |
| 209 | fVShaderID = 0; |
| 210 | fGShaderID = 0; |
| 211 | fFShaderID = 0; |
| 212 | fProgramID = 0; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 213 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 214 | fColor = GrColor_ILLEGAL; |
| 215 | fColorFilterColor = GrColor_ILLEGAL; |
| 216 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 217 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 218 | fEffects[s] = NULL; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 219 | } |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 220 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 221 | this->genProgram(stages); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | GrGLProgram::~GrGLProgram() { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 225 | if (fVShaderID) { |
| 226 | GL_CALL(DeleteShader(fVShaderID)); |
| 227 | } |
| 228 | if (fGShaderID) { |
| 229 | GL_CALL(DeleteShader(fGShaderID)); |
| 230 | } |
| 231 | if (fFShaderID) { |
| 232 | GL_CALL(DeleteShader(fFShaderID)); |
| 233 | } |
| 234 | if (fProgramID) { |
| 235 | GL_CALL(DeleteProgram(fProgramID)); |
| 236 | } |
| 237 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 238 | for (int i = 0; i < GrDrawState::kNumStages; ++i) { |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 239 | delete fEffects[i]; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 240 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 241 | } |
| 242 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 243 | void GrGLProgram::abandon() { |
| 244 | fVShaderID = 0; |
| 245 | fGShaderID = 0; |
| 246 | fFShaderID = 0; |
| 247 | fProgramID = 0; |
| 248 | } |
| 249 | |
tomhudson@google.com | 0d3f1fb | 2011-06-01 19:27:31 +0000 | [diff] [blame] | 250 | void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 251 | GrBlendCoeff* dstCoeff) const { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 252 | switch (fDesc.fDualSrcOutput) { |
| 253 | case Desc::kNone_DualSrcOutput: |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 254 | break; |
| 255 | // the prog will write a coverage value to the secondary |
| 256 | // output and the dst is blended by one minus that value. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 257 | case Desc::kCoverage_DualSrcOutput: |
| 258 | case Desc::kCoverageISA_DualSrcOutput: |
| 259 | case Desc::kCoverageISC_DualSrcOutput: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 260 | *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_GrBlendCoeff; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 261 | break; |
| 262 | default: |
| 263 | GrCrash("Unexpected dual source blend output"); |
| 264 | break; |
| 265 | } |
| 266 | } |
| 267 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 268 | namespace { |
| 269 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 270 | // given two blend coeffecients determine whether the src |
| 271 | // and/or dst computation can be omitted. |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 272 | inline void need_blend_inputs(SkXfermode::Coeff srcCoeff, |
| 273 | SkXfermode::Coeff dstCoeff, |
| 274 | bool* needSrcValue, |
| 275 | bool* needDstValue) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 276 | if (SkXfermode::kZero_Coeff == srcCoeff) { |
| 277 | switch (dstCoeff) { |
| 278 | // these all read the src |
| 279 | case SkXfermode::kSC_Coeff: |
| 280 | case SkXfermode::kISC_Coeff: |
| 281 | case SkXfermode::kSA_Coeff: |
| 282 | case SkXfermode::kISA_Coeff: |
| 283 | *needSrcValue = true; |
| 284 | break; |
| 285 | default: |
| 286 | *needSrcValue = false; |
| 287 | break; |
| 288 | } |
| 289 | } else { |
| 290 | *needSrcValue = true; |
| 291 | } |
| 292 | if (SkXfermode::kZero_Coeff == dstCoeff) { |
| 293 | switch (srcCoeff) { |
| 294 | // these all read the dst |
| 295 | case SkXfermode::kDC_Coeff: |
| 296 | case SkXfermode::kIDC_Coeff: |
| 297 | case SkXfermode::kDA_Coeff: |
| 298 | case SkXfermode::kIDA_Coeff: |
| 299 | *needDstValue = true; |
| 300 | break; |
| 301 | default: |
| 302 | *needDstValue = false; |
| 303 | break; |
| 304 | } |
| 305 | } else { |
| 306 | *needDstValue = true; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 311 | * Create a blend_coeff * value string to be used in shader code. Sets empty |
| 312 | * string if result is trivially zero. |
| 313 | */ |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 314 | inline void blend_term_string(SkString* str, SkXfermode::Coeff coeff, |
| 315 | const char* src, const char* dst, |
| 316 | const char* value) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 317 | switch (coeff) { |
| 318 | case SkXfermode::kZero_Coeff: /** 0 */ |
| 319 | *str = ""; |
| 320 | break; |
| 321 | case SkXfermode::kOne_Coeff: /** 1 */ |
| 322 | *str = value; |
| 323 | break; |
| 324 | case SkXfermode::kSC_Coeff: |
| 325 | str->printf("(%s * %s)", src, value); |
| 326 | break; |
| 327 | case SkXfermode::kISC_Coeff: |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 328 | str->printf("((%s - %s) * %s)", GrGLSLOnesVecf(4), src, value); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 329 | break; |
| 330 | case SkXfermode::kDC_Coeff: |
| 331 | str->printf("(%s * %s)", dst, value); |
| 332 | break; |
| 333 | case SkXfermode::kIDC_Coeff: |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 334 | str->printf("((%s - %s) * %s)", GrGLSLOnesVecf(4), dst, value); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 335 | break; |
| 336 | case SkXfermode::kSA_Coeff: /** src alpha */ |
| 337 | str->printf("(%s.a * %s)", src, value); |
| 338 | break; |
| 339 | case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */ |
| 340 | str->printf("((1.0 - %s.a) * %s)", src, value); |
| 341 | break; |
| 342 | case SkXfermode::kDA_Coeff: /** dst alpha */ |
| 343 | str->printf("(%s.a * %s)", dst, value); |
| 344 | break; |
| 345 | case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */ |
| 346 | str->printf("((1.0 - %s.a) * %s)", dst, value); |
| 347 | break; |
| 348 | default: |
| 349 | GrCrash("Unexpected xfer coeff."); |
| 350 | break; |
| 351 | } |
| 352 | } |
| 353 | /** |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 354 | * Adds a line to the fragment shader code which modifies the color by |
| 355 | * the specified color filter. |
| 356 | */ |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 357 | void add_color_filter(SkString* fsCode, const char * outputVar, |
| 358 | SkXfermode::Coeff uniformCoeff, |
| 359 | SkXfermode::Coeff colorCoeff, |
| 360 | const char* filterColor, |
| 361 | const char* inColor) { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 362 | SkString colorStr, constStr; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 363 | blend_term_string(&colorStr, colorCoeff, filterColor, inColor, inColor); |
| 364 | blend_term_string(&constStr, uniformCoeff, filterColor, inColor, filterColor); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 365 | |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 366 | fsCode->appendf("\t%s = ", outputVar); |
| 367 | GrGLSLAdd4f(fsCode, colorStr.c_str(), constStr.c_str()); |
| 368 | fsCode->append(";\n"); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 369 | } |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 370 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 371 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 372 | bool GrGLProgram::genEdgeCoverage(SkString* coverageVar, |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 373 | GrGLShaderBuilder* builder) const { |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 374 | if (fDesc.fVertexLayout & GrDrawState::kEdge_VertexLayoutBit) { |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 375 | const char *vsName, *fsName; |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 376 | builder->addVarying(kVec4f_GrSLType, "Edge", &vsName, &fsName); |
| 377 | builder->fVSAttrs.push_back().set(kVec4f_GrSLType, |
| 378 | GrGLShaderVar::kAttribute_TypeModifier, |
| 379 | EDGE_ATTR_NAME); |
| 380 | builder->fVSCode.appendf("\t%s = " EDGE_ATTR_NAME ";\n", vsName); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 381 | switch (fDesc.fVertexEdgeType) { |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 382 | case GrDrawState::kHairLine_EdgeType: |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 383 | builder->fFSCode.appendf("\tfloat edgeAlpha = abs(dot(vec3(%s.xy,1), %s.xyz));\n", builder->fragmentPosition(), fsName); |
| 384 | builder->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 385 | break; |
| 386 | case GrDrawState::kQuad_EdgeType: |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 387 | builder->fFSCode.append("\tfloat edgeAlpha;\n"); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 388 | // keep the derivative instructions outside the conditional |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 389 | builder->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
| 390 | builder->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
| 391 | 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] | 392 | // 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] | 393 | builder->fFSCode.appendf("\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);\n", fsName, fsName); |
| 394 | builder->fFSCode.append ("\t} else {\n"); |
| 395 | builder->fFSCode.appendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n" |
| 396 | "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n", |
| 397 | fsName, fsName); |
| 398 | builder->fFSCode.appendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName); |
| 399 | builder->fFSCode.append("\t\tedgeAlpha = clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0);\n" |
| 400 | "\t}\n"); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 401 | if (kES2_GrGLBinding == fContextInfo.binding()) { |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 402 | builder->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n"); |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 403 | } |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 404 | break; |
| 405 | case GrDrawState::kHairQuad_EdgeType: |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 406 | builder->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
| 407 | builder->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
| 408 | builder->fFSCode.appendf("\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n" |
| 409 | "\t 2.0*%s.x*duvdy.x - duvdy.y);\n", |
| 410 | fsName, fsName); |
| 411 | builder->fFSCode.appendf("\tfloat edgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName); |
| 412 | builder->fFSCode.append("\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n"); |
| 413 | builder->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 414 | if (kES2_GrGLBinding == fContextInfo.binding()) { |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 415 | builder->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n"); |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 416 | } |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 417 | break; |
| 418 | case GrDrawState::kCircle_EdgeType: |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 419 | builder->fFSCode.append("\tfloat edgeAlpha;\n"); |
| 420 | builder->fFSCode.appendf("\tfloat d = distance(%s.xy, %s.xy);\n", builder->fragmentPosition(), fsName); |
| 421 | builder->fFSCode.appendf("\tfloat outerAlpha = smoothstep(d - 0.5, d + 0.5, %s.z);\n", fsName); |
| 422 | 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); |
| 423 | builder->fFSCode.append("\tedgeAlpha = outerAlpha * innerAlpha;\n"); |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 424 | break; |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 425 | case GrDrawState::kEllipse_EdgeType: |
| 426 | builder->fFSCode.append("\tfloat edgeAlpha;\n"); |
| 427 | builder->fFSCode.appendf("\tvec2 offset = (%s.xy - %s.xy);\n", builder->fragmentPosition(), fsName); |
| 428 | builder->fFSCode.appendf("\toffset.y *= %s.w;\n", fsName); |
| 429 | builder->fFSCode.append("\tfloat d = length(offset);\n"); |
| 430 | builder->fFSCode.appendf("\tedgeAlpha = smoothstep(d - 0.5, d + 0.5, %s.z);\n", fsName); |
| 431 | break; |
bsalomon@google.com | 93c9660 | 2012-04-27 13:05:21 +0000 | [diff] [blame] | 432 | default: |
| 433 | GrCrash("Unknown Edge Type!"); |
| 434 | break; |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 435 | } |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 436 | if (fDesc.fDiscardIfOutsideEdge) { |
robertphillips@google.com | ef818a4 | 2012-12-10 21:05:19 +0000 | [diff] [blame] | 437 | 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] | 438 | } |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 439 | *coverageVar = "edgeAlpha"; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 440 | return true; |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 441 | } else { |
| 442 | coverageVar->reset(); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 443 | return false; |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 447 | void GrGLProgram::genInputColor(GrGLShaderBuilder* builder, SkString* inColor) { |
| 448 | switch (fDesc.fColorInput) { |
| 449 | case GrGLProgram::Desc::kAttribute_ColorInput: { |
| 450 | builder->fVSAttrs.push_back().set(kVec4f_GrSLType, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 451 | GrGLShaderVar::kAttribute_TypeModifier, |
| 452 | COL_ATTR_NAME); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 453 | const char *vsName, *fsName; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 454 | builder->addVarying(kVec4f_GrSLType, "Color", &vsName, &fsName); |
| 455 | builder->fVSCode.appendf("\t%s = " COL_ATTR_NAME ";\n", vsName); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 456 | *inColor = fsName; |
| 457 | } break; |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 458 | case GrGLProgram::Desc::kUniform_ColorInput: { |
| 459 | const char* name; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 460 | fUniformHandles.fColorUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 461 | kVec4f_GrSLType, "Color", &name); |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 462 | *inColor = name; |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 463 | break; |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 464 | } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 465 | case GrGLProgram::Desc::kTransBlack_ColorInput: |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 466 | GrAssert(!"needComputedColor should be false."); |
| 467 | break; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 468 | case GrGLProgram::Desc::kSolidWhite_ColorInput: |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 469 | break; |
| 470 | default: |
| 471 | GrCrash("Unknown color type."); |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 476 | void GrGLProgram::genUniformCoverage(GrGLShaderBuilder* builder, SkString* inOutCoverage) { |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 477 | const char* covUniName; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 478 | fUniformHandles.fCoverageUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 479 | kVec4f_GrSLType, "Coverage", &covUniName); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 480 | if (inOutCoverage->size()) { |
| 481 | builder->fFSCode.appendf("\tvec4 uniCoverage = %s * %s;\n", |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 482 | covUniName, inOutCoverage->c_str()); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 483 | *inOutCoverage = "uniCoverage"; |
| 484 | } else { |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 485 | *inOutCoverage = covUniName; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
| 489 | namespace { |
| 490 | void gen_attribute_coverage(GrGLShaderBuilder* segments, |
| 491 | SkString* inOutCoverage) { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 492 | segments->fVSAttrs.push_back().set(kVec4f_GrSLType, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 493 | GrGLShaderVar::kAttribute_TypeModifier, |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 494 | COV_ATTR_NAME); |
| 495 | const char *vsName, *fsName; |
tomhudson@google.com | 23cb229 | 2012-05-30 18:26:03 +0000 | [diff] [blame] | 496 | segments->addVarying(kVec4f_GrSLType, "Coverage", &vsName, &fsName); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 497 | segments->fVSCode.appendf("\t%s = " COV_ATTR_NAME ";\n", vsName); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 498 | if (inOutCoverage->size()) { |
| 499 | segments->fFSCode.appendf("\tvec4 attrCoverage = %s * %s;\n", |
| 500 | fsName, inOutCoverage->c_str()); |
| 501 | *inOutCoverage = "attrCoverage"; |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 502 | } else { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 503 | *inOutCoverage = fsName; |
| 504 | } |
| 505 | } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 506 | } |
| 507 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 508 | void GrGLProgram::genGeometryShader(GrGLShaderBuilder* segments) const { |
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 | if (fDesc.fExperimentalGS) { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 511 | GrAssert(fContextInfo.glslGeneration() >= k150_GrGLSLGeneration); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 512 | segments->fGSHeader.append("layout(triangles) in;\n" |
| 513 | "layout(triangle_strip, max_vertices = 6) out;\n"); |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 514 | segments->fGSCode.append("\tfor (int i = 0; i < 3; ++i) {\n" |
| 515 | "\t\tgl_Position = gl_in[i].gl_Position;\n"); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 516 | if (fDesc.fEmitsPointSize) { |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 517 | segments->fGSCode.append("\t\tgl_PointSize = 1.0;\n"); |
| 518 | } |
| 519 | GrAssert(segments->fGSInputs.count() == segments->fGSOutputs.count()); |
| 520 | int count = segments->fGSInputs.count(); |
| 521 | for (int i = 0; i < count; ++i) { |
| 522 | segments->fGSCode.appendf("\t\t%s = %s[i];\n", |
| 523 | segments->fGSOutputs[i].getName().c_str(), |
| 524 | segments->fGSInputs[i].getName().c_str()); |
| 525 | } |
| 526 | segments->fGSCode.append("\t\tEmitVertex();\n" |
| 527 | "\t}\n" |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 528 | "\tEndPrimitive();\n"); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 529 | } |
| 530 | #endif |
| 531 | } |
| 532 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 533 | const char* GrGLProgram::adjustInColor(const SkString& inColor) const { |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 534 | if (inColor.size()) { |
| 535 | return inColor.c_str(); |
| 536 | } else { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 537 | if (Desc::kSolidWhite_ColorInput == fDesc.fColorInput) { |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 538 | return GrGLSLOnesVecf(4); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 539 | } else { |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 540 | return GrGLSLZerosVecf(4); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 545 | namespace { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 546 | // prints a shader using params similar to glShaderSource |
| 547 | void print_shader(GrGLint stringCnt, |
| 548 | const GrGLchar** strings, |
| 549 | GrGLint* stringLengths) { |
| 550 | for (int i = 0; i < stringCnt; ++i) { |
| 551 | if (NULL == stringLengths || stringLengths[i] < 0) { |
| 552 | GrPrintf(strings[i]); |
| 553 | } else { |
| 554 | GrPrintf("%.*s", stringLengths[i], strings[i]); |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | // Compiles a GL shader, returns shader ID or 0 if failed params have same meaning as glShaderSource |
| 560 | GrGLuint compile_shader(const GrGLContextInfo& gl, |
| 561 | GrGLenum type, |
| 562 | int stringCnt, |
| 563 | const char** strings, |
| 564 | int* stringLengths) { |
| 565 | SK_TRACE_EVENT1("GrGLProgram::CompileShader", |
| 566 | "stringCount", SkStringPrintf("%i", stringCnt).c_str()); |
| 567 | |
| 568 | GrGLuint shader; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 569 | GR_GL_CALL_RET(gl.interface(), shader, CreateShader(type)); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 570 | if (0 == shader) { |
| 571 | return 0; |
| 572 | } |
| 573 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 574 | const GrGLInterface* gli = gl.interface(); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 575 | GrGLint compiled = GR_GL_INIT_ZERO; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 576 | GR_GL_CALL(gli, ShaderSource(shader, stringCnt, strings, stringLengths)); |
| 577 | GR_GL_CALL(gli, CompileShader(shader)); |
| 578 | GR_GL_CALL(gli, GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled)); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 579 | |
| 580 | if (!compiled) { |
| 581 | GrGLint infoLen = GR_GL_INIT_ZERO; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 582 | 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] | 583 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
| 584 | if (infoLen > 0) { |
| 585 | // retrieve length even though we don't need it to workaround bug in chrome cmd buffer |
| 586 | // param validation. |
| 587 | GrGLsizei length = GR_GL_INIT_ZERO; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 588 | GR_GL_CALL(gli, GetShaderInfoLog(shader, infoLen+1, |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 589 | &length, (char*)log.get())); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 590 | print_shader(stringCnt, strings, stringLengths); |
| 591 | GrPrintf("\n%s", log.get()); |
| 592 | } |
| 593 | GrAssert(!"Shader compilation failed!"); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 594 | GR_GL_CALL(gli, DeleteShader(shader)); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 595 | return 0; |
| 596 | } |
| 597 | return shader; |
| 598 | } |
| 599 | |
| 600 | // helper version of above for when shader is already flattened into a single SkString |
| 601 | GrGLuint compile_shader(const GrGLContextInfo& gl, GrGLenum type, const SkString& shader) { |
| 602 | const GrGLchar* str = shader.c_str(); |
| 603 | int length = shader.size(); |
| 604 | return compile_shader(gl, type, 1, &str, &length); |
| 605 | } |
| 606 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | // compiles all the shaders from builder and stores the shader IDs |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 610 | bool GrGLProgram::compileShaders(const GrGLShaderBuilder& builder) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 611 | |
| 612 | SkString shader; |
| 613 | |
| 614 | builder.getShader(GrGLShaderBuilder::kVertex_ShaderType, &shader); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 615 | if (c_PrintShaders) { |
| 616 | GrPrintf(shader.c_str()); |
| 617 | GrPrintf("\n"); |
| 618 | } |
skia.committer@gmail.com | 7fc0e0a | 2013-01-15 02:01:40 +0000 | [diff] [blame] | 619 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 620 | if (!(fVShaderID = compile_shader(fContextInfo, GR_GL_VERTEX_SHADER, shader))) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 621 | return false; |
| 622 | } |
| 623 | |
| 624 | if (builder.fUsesGS) { |
| 625 | builder.getShader(GrGLShaderBuilder::kGeometry_ShaderType, &shader); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 626 | if (c_PrintShaders) { |
| 627 | GrPrintf(shader.c_str()); |
| 628 | GrPrintf("\n"); |
| 629 | } |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 630 | if (!(fGShaderID = compile_shader(fContextInfo, GR_GL_GEOMETRY_SHADER, shader))) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 631 | return false; |
| 632 | } |
| 633 | } else { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 634 | fGShaderID = 0; |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | builder.getShader(GrGLShaderBuilder::kFragment_ShaderType, &shader); |
humper@google.com | 7af56be | 2013-01-14 18:49:19 +0000 | [diff] [blame] | 638 | if (c_PrintShaders) { |
| 639 | GrPrintf(shader.c_str()); |
| 640 | GrPrintf("\n"); |
| 641 | } |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 642 | if (!(fFShaderID = compile_shader(fContextInfo, GR_GL_FRAGMENT_SHADER, shader))) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 643 | return false; |
| 644 | } |
| 645 | |
| 646 | return true; |
| 647 | } |
| 648 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 649 | bool GrGLProgram::genProgram(const GrEffectStage* stages[]) { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 650 | GrAssert(0 == fProgramID); |
| 651 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 652 | GrGLShaderBuilder builder(fContextInfo, fUniformManager); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 653 | const uint32_t& layout = fDesc.fVertexLayout; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 654 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 655 | #if GR_GL_EXPERIMENTAL_GS |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 656 | builder.fUsesGS = fDesc.fExperimentalGS; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 657 | #endif |
| 658 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 659 | SkXfermode::Coeff colorCoeff, uniformCoeff; |
| 660 | // The rest of transfer mode color filters have not been implemented |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 661 | if (fDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 662 | GR_DEBUGCODE(bool success =) |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 663 | SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode> |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 664 | (fDesc.fColorFilterXfermode), |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 665 | &uniformCoeff, &colorCoeff); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 666 | GR_DEBUGASSERT(success); |
| 667 | } else { |
| 668 | colorCoeff = SkXfermode::kOne_Coeff; |
| 669 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 670 | } |
| 671 | |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 672 | // no need to do the color filter if coverage is 0. The output color is scaled by the coverage. |
| 673 | // 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] | 674 | if (Desc::kTransBlack_ColorInput == fDesc.fCoverageInput) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 675 | colorCoeff = SkXfermode::kZero_Coeff; |
| 676 | uniformCoeff = SkXfermode::kZero_Coeff; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 677 | } |
| 678 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 679 | // 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] | 680 | // simplify the color filter coefficients. needComputedColor will then |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 681 | // come out false below. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 682 | if (Desc::kTransBlack_ColorInput == fDesc.fColorInput) { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 683 | colorCoeff = SkXfermode::kZero_Coeff; |
| 684 | if (SkXfermode::kDC_Coeff == uniformCoeff || |
| 685 | SkXfermode::kDA_Coeff == uniformCoeff) { |
| 686 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 687 | } else if (SkXfermode::kIDC_Coeff == uniformCoeff || |
| 688 | SkXfermode::kIDA_Coeff == uniformCoeff) { |
| 689 | uniformCoeff = SkXfermode::kOne_Coeff; |
| 690 | } |
| 691 | } |
| 692 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 693 | bool needColorFilterUniform; |
| 694 | bool needComputedColor; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 695 | need_blend_inputs(uniformCoeff, colorCoeff, |
| 696 | &needColorFilterUniform, &needComputedColor); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 697 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 698 | // the dual source output has no canonical var name, have to |
| 699 | // declare an output, which is incompatible with gl_FragColor/gl_FragData. |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 700 | bool dualSourceOutputWritten = false; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 701 | builder.fHeader.append(GrGetGLSLVersionDecl(fContextInfo.binding(), |
| 702 | fContextInfo.glslGeneration())); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 703 | |
| 704 | GrGLShaderVar colorOutput; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 705 | bool isColorDeclared = GrGLSLSetupFSColorOuput(fContextInfo.glslGeneration(), |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 706 | declared_color_output_name(), |
| 707 | &colorOutput); |
| 708 | if (isColorDeclared) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 709 | builder.fFSOutputs.push_back(colorOutput); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 710 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 711 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 712 | const char* viewMName; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 713 | fUniformHandles.fViewMatrixUni = builder.addUniform(GrGLShaderBuilder::kVertex_ShaderType, |
| 714 | kMat33f_GrSLType, "ViewM", &viewMName); |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 715 | |
skia.committer@gmail.com | e862d16 | 2012-10-31 02:01:18 +0000 | [diff] [blame] | 716 | |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 717 | builder.fVSCode.appendf("\tvec3 pos3 = %s * vec3(%s, 1);\n" |
bsalomon@google.com | d7bafb7 | 2012-10-22 14:30:50 +0000 | [diff] [blame] | 718 | "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n", |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 719 | viewMName, builder.positionAttribute().getName().c_str()); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 720 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 721 | // incoming color to current stage being processed. |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 722 | SkString inColor; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 723 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 724 | if (needComputedColor) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 725 | this->genInputColor(&builder, &inColor); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 726 | } |
| 727 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 728 | // we output point size in the GS if present |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 729 | if (fDesc.fEmitsPointSize && !builder.fUsesGS){ |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 730 | builder.fVSCode.append("\tgl_PointSize = 1.0;\n"); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 731 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 732 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 733 | // 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] | 734 | SkString texCoordAttrs[GrDrawState::kMaxTexCoords]; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 735 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 736 | if (GrDrawState::VertexUsesTexCoordIdx(t, layout)) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 737 | tex_attr_name(t, texCoordAttrs + t); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 738 | builder.fVSAttrs.push_back().set(kVec2f_GrSLType, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 739 | GrGLShaderVar::kAttribute_TypeModifier, |
| 740 | texCoordAttrs[t].c_str()); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 744 | /////////////////////////////////////////////////////////////////////////// |
| 745 | // compute the final color |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 746 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 747 | // if we have color stages string them together, feeding the output color |
| 748 | // of each to the next and generating code for each stage. |
| 749 | if (needComputedColor) { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 750 | SkString outColor; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 751 | for (int s = 0; s < fDesc.fFirstCoverageStage; ++s) { |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 752 | if (GrGLEffect::kNoEffectKey != fDesc.fEffectKeys[s]) { |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 753 | // create var to hold stage result |
| 754 | outColor = "color"; |
| 755 | outColor.appendS32(s); |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 756 | builder.fFSCode.appendf("\tvec4 %s;\n", outColor.c_str()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 757 | |
| 758 | const char* inCoords; |
| 759 | // figure out what our input coords are |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 760 | int tcIdx = GrDrawState::VertexTexCoordsForStage(s, layout); |
tomhudson@google.com | b213ed8 | 2012-06-25 15:22:12 +0000 | [diff] [blame] | 761 | if (tcIdx < 0) { |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 762 | inCoords = builder.positionAttribute().c_str(); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 763 | } else { |
tomhudson@google.com | b213ed8 | 2012-06-25 15:22:12 +0000 | [diff] [blame] | 764 | // must have input tex coordinates if stage is enabled. |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 765 | GrAssert(texCoordAttrs[tcIdx].size()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 766 | inCoords = texCoordAttrs[tcIdx].c_str(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 767 | } |
| 768 | |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 769 | builder.setCurrentStage(s); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 770 | fEffects[s] = builder.createAndEmitGLEffect(*stages[s], |
| 771 | fDesc.fEffectKeys[s], |
| 772 | inColor.size() ? inColor.c_str() : NULL, |
| 773 | outColor.c_str(), |
| 774 | inCoords, |
| 775 | &fUniformHandles.fSamplerUnis[s]); |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 776 | builder.setNonStage(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 777 | inColor = outColor; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 778 | } |
| 779 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 780 | } |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 781 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 782 | // if have all ones or zeros for the "dst" input to the color filter then we |
| 783 | // may be able to make additional optimizations. |
| 784 | if (needColorFilterUniform && needComputedColor && !inColor.size()) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 785 | GrAssert(Desc::kSolidWhite_ColorInput == fDesc.fColorInput); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 786 | bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff || |
| 787 | SkXfermode::kIDA_Coeff == uniformCoeff; |
| 788 | if (uniformCoeffIsZero) { |
| 789 | uniformCoeff = SkXfermode::kZero_Coeff; |
| 790 | bool bogus; |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 791 | need_blend_inputs(SkXfermode::kZero_Coeff, colorCoeff, |
| 792 | &needColorFilterUniform, &bogus); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 793 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 794 | } |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 795 | const char* colorFilterColorUniName = NULL; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 796 | if (needColorFilterUniform) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 797 | fUniformHandles.fColorFilterUni = builder.addUniform( |
| 798 | GrGLShaderBuilder::kFragment_ShaderType, |
| 799 | kVec4f_GrSLType, "FilterColor", |
| 800 | &colorFilterColorUniName); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 801 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 802 | bool wroteFragColorZero = false; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 803 | if (SkXfermode::kZero_Coeff == uniformCoeff && |
bsalomon@google.com | 67e78c9 | 2012-10-17 13:36:14 +0000 | [diff] [blame] | 804 | SkXfermode::kZero_Coeff == colorCoeff) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 805 | builder.fFSCode.appendf("\t%s = %s;\n", |
| 806 | colorOutput.getName().c_str(), |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 807 | GrGLSLZerosVecf(4)); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 808 | wroteFragColorZero = true; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 809 | } else if (SkXfermode::kDst_Mode != fDesc.fColorFilterXfermode) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 810 | builder.fFSCode.append("\tvec4 filteredColor;\n"); |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 811 | const char* color = adjustInColor(inColor); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 812 | add_color_filter(&builder.fFSCode, "filteredColor", uniformCoeff, |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 813 | colorCoeff, colorFilterColorUniName, color); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 814 | inColor = "filteredColor"; |
| 815 | } |
| 816 | |
| 817 | /////////////////////////////////////////////////////////////////////////// |
| 818 | // compute the partial coverage (coverage stages and edge aa) |
| 819 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 820 | SkString inCoverage; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 821 | bool coverageIsZero = Desc::kTransBlack_ColorInput == fDesc.fCoverageInput; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 822 | // we don't need to compute coverage at all if we know the final shader |
| 823 | // 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] | 824 | if (!wroteFragColorZero || Desc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) { |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 825 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 826 | if (!coverageIsZero) { |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 827 | bool inCoverageIsScalar = this->genEdgeCoverage(&inCoverage, &builder); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 828 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 829 | switch (fDesc.fCoverageInput) { |
| 830 | case Desc::kSolidWhite_ColorInput: |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 831 | // empty string implies solid white |
| 832 | break; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 833 | case Desc::kAttribute_ColorInput: |
| 834 | gen_attribute_coverage(&builder, &inCoverage); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 835 | inCoverageIsScalar = false; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 836 | break; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 837 | case Desc::kUniform_ColorInput: |
| 838 | this->genUniformCoverage(&builder, &inCoverage); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 839 | inCoverageIsScalar = false; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 840 | break; |
| 841 | default: |
| 842 | GrCrash("Unexpected input coverage."); |
| 843 | } |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 844 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 845 | SkString outCoverage; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 846 | const int& startStage = fDesc.fFirstCoverageStage; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 847 | for (int s = startStage; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 848 | if (fDesc.fEffectKeys[s]) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 849 | // create var to hold stage output |
| 850 | outCoverage = "coverage"; |
| 851 | outCoverage.appendS32(s); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 852 | builder.fFSCode.appendf("\tvec4 %s;\n", outCoverage.c_str()); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 853 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 854 | const char* inCoords; |
| 855 | // figure out what our input coords are |
tomhudson@google.com | b213ed8 | 2012-06-25 15:22:12 +0000 | [diff] [blame] | 856 | int tcIdx = |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 857 | GrDrawState::VertexTexCoordsForStage(s, layout); |
tomhudson@google.com | b213ed8 | 2012-06-25 15:22:12 +0000 | [diff] [blame] | 858 | if (tcIdx < 0) { |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 859 | inCoords = builder.positionAttribute().c_str(); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 860 | } else { |
tomhudson@google.com | b213ed8 | 2012-06-25 15:22:12 +0000 | [diff] [blame] | 861 | // must have input tex coordinates if stage is |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 862 | // enabled. |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 863 | GrAssert(texCoordAttrs[tcIdx].size()); |
| 864 | inCoords = texCoordAttrs[tcIdx].c_str(); |
| 865 | } |
| 866 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 867 | // 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] | 868 | // could pass a GrGLShaderVar) |
| 869 | if (inCoverageIsScalar) { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 870 | builder.fFSCode.appendf("\tvec4 %s4 = vec4(%s);\n", |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 871 | inCoverage.c_str(), inCoverage.c_str()); |
| 872 | inCoverage.append("4"); |
| 873 | } |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 874 | builder.setCurrentStage(s); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 875 | fEffects[s] = builder.createAndEmitGLEffect( |
| 876 | *stages[s], |
| 877 | fDesc.fEffectKeys[s], |
| 878 | inCoverage.size() ? inCoverage.c_str() : NULL, |
| 879 | outCoverage.c_str(), |
| 880 | inCoords, |
| 881 | &fUniformHandles.fSamplerUnis[s]); |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 882 | builder.setNonStage(); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 883 | inCoverage = outCoverage; |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 884 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 885 | } |
| 886 | } |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 887 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 888 | if (Desc::kNone_DualSrcOutput != fDesc.fDualSrcOutput) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 889 | builder.fFSOutputs.push_back().set(kVec4f_GrSLType, |
| 890 | GrGLShaderVar::kOut_TypeModifier, |
| 891 | dual_source_output_name()); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 892 | bool outputIsZero = coverageIsZero; |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 893 | SkString coeff; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 894 | if (!outputIsZero && |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 895 | Desc::kCoverage_DualSrcOutput != fDesc.fDualSrcOutput && !wroteFragColorZero) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 896 | if (!inColor.size()) { |
| 897 | outputIsZero = true; |
| 898 | } else { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 899 | if (Desc::kCoverageISA_DualSrcOutput == fDesc.fDualSrcOutput) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 900 | coeff.printf("(1 - %s.a)", inColor.c_str()); |
| 901 | } else { |
| 902 | coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str()); |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | if (outputIsZero) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 907 | builder.fFSCode.appendf("\t%s = %s;\n", |
| 908 | dual_source_output_name(), |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 909 | GrGLSLZerosVecf(4)); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 910 | } else { |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 911 | builder.fFSCode.appendf("\t%s =", dual_source_output_name()); |
| 912 | GrGLSLModulate4f(&builder.fFSCode, coeff.c_str(), inCoverage.c_str()); |
| 913 | builder.fFSCode.append(";\n"); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 914 | } |
| 915 | dualSourceOutputWritten = true; |
| 916 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 917 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 918 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 919 | /////////////////////////////////////////////////////////////////////////// |
| 920 | // combine color and coverage as frag color |
| 921 | |
| 922 | if (!wroteFragColorZero) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 923 | if (coverageIsZero) { |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 924 | builder.fFSCode.appendf("\t%s = %s;\n", |
| 925 | colorOutput.getName().c_str(), |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 926 | GrGLSLZerosVecf(4)); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 927 | } else { |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 928 | builder.fFSCode.appendf("\t%s = ", colorOutput.getName().c_str()); |
| 929 | GrGLSLModulate4f(&builder.fFSCode, inColor.c_str(), inCoverage.c_str()); |
| 930 | builder.fFSCode.append(";\n"); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 931 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 932 | } |
| 933 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 934 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 935 | // insert GS |
| 936 | #if GR_DEBUG |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 937 | this->genGeometryShader(&builder); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 938 | #endif |
| 939 | |
| 940 | /////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 941 | // compile and setup attribs and unis |
| 942 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 943 | if (!this->compileShaders(builder)) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 944 | return false; |
| 945 | } |
| 946 | |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 947 | if (!this->bindOutputsAttribsAndLinkProgram(builder, |
| 948 | texCoordAttrs, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 949 | isColorDeclared, |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 950 | dualSourceOutputWritten)) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 951 | return false; |
| 952 | } |
| 953 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 954 | builder.finished(fProgramID); |
| 955 | this->initSamplerUniforms(); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 956 | fUniformHandles.fRTHeightUni = builder.getRTHeightUniform(); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 957 | |
| 958 | return true; |
| 959 | } |
| 960 | |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 961 | bool GrGLProgram::bindOutputsAttribsAndLinkProgram(const GrGLShaderBuilder& builder, |
| 962 | SkString texCoordAttrNames[], |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 963 | bool bindColorOut, |
| 964 | bool bindDualSrcOut) { |
| 965 | GL_CALL_RET(fProgramID, CreateProgram()); |
| 966 | if (!fProgramID) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 967 | return false; |
| 968 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 969 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 970 | GL_CALL(AttachShader(fProgramID, fVShaderID)); |
| 971 | if (fGShaderID) { |
| 972 | GL_CALL(AttachShader(fProgramID, fGShaderID)); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 973 | } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 974 | GL_CALL(AttachShader(fProgramID, fFShaderID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 975 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 976 | if (bindColorOut) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 977 | GL_CALL(BindFragDataLocation(fProgramID, 0, declared_color_output_name())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 978 | } |
| 979 | if (bindDualSrcOut) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 980 | GL_CALL(BindFragDataLocationIndexed(fProgramID, 0, 1, dual_source_output_name())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 981 | } |
| 982 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 983 | // Bind the attrib locations to same values for all shaders |
bsalomon@google.com | 17504f5 | 2012-10-30 12:34:25 +0000 | [diff] [blame] | 984 | GL_CALL(BindAttribLocation(fProgramID, |
| 985 | PositionAttributeIdx(), |
| 986 | builder.positionAttribute().c_str())); |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 987 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 988 | if (texCoordAttrNames[t].size()) { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 989 | GL_CALL(BindAttribLocation(fProgramID, |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 990 | TexCoordAttributeIdx(t), |
| 991 | texCoordAttrNames[t].c_str())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 995 | GL_CALL(BindAttribLocation(fProgramID, ColorAttributeIdx(), COL_ATTR_NAME)); |
| 996 | GL_CALL(BindAttribLocation(fProgramID, CoverageAttributeIdx(), COV_ATTR_NAME)); |
| 997 | GL_CALL(BindAttribLocation(fProgramID, EdgeAttributeIdx(), EDGE_ATTR_NAME)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 998 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 999 | GL_CALL(LinkProgram(fProgramID)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1000 | |
| 1001 | GrGLint linked = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 1002 | GL_CALL(GetProgramiv(fProgramID, GR_GL_LINK_STATUS, &linked)); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1003 | if (!linked) { |
| 1004 | GrGLint infoLen = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 1005 | GL_CALL(GetProgramiv(fProgramID, GR_GL_INFO_LOG_LENGTH, &infoLen)); |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 1006 | SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1007 | if (infoLen > 0) { |
bsalomon@google.com | 79afcaa | 2011-09-14 14:29:18 +0000 | [diff] [blame] | 1008 | // retrieve length even though we don't need it to workaround |
| 1009 | // bug in chrome cmd buffer param validation. |
| 1010 | GrGLsizei length = GR_GL_INIT_ZERO; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 1011 | GL_CALL(GetProgramInfoLog(fProgramID, |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 1012 | infoLen+1, |
| 1013 | &length, |
| 1014 | (char*)log.get())); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1015 | GrPrintf((char*)log.get()); |
| 1016 | } |
| 1017 | GrAssert(!"Error linking program"); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 1018 | GL_CALL(DeleteProgram(fProgramID)); |
| 1019 | fProgramID = 0; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1020 | return false; |
| 1021 | } |
| 1022 | return true; |
| 1023 | } |
| 1024 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 1025 | void GrGLProgram::initSamplerUniforms() { |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 1026 | GL_CALL(UseProgram(fProgramID)); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 1027 | // We simply bind the uniforms to successive texture units beginning at 0. setData() assumes this |
| 1028 | // behavior. |
| 1029 | GrGLint texUnitIdx = 0; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1030 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 1031 | int numSamplers = fUniformHandles.fSamplerUnis[s].count(); |
| 1032 | for (int u = 0; u < numSamplers; ++u) { |
| 1033 | UniformHandle handle = fUniformHandles.fSamplerUnis[s][u]; |
| 1034 | if (GrGLUniformManager::kInvalidUniformHandle != handle) { |
| 1035 | fUniformManager.setSampler(handle, texUnitIdx); |
| 1036 | ++texUnitIdx; |
bsalomon@google.com | 0982d35 | 2012-07-31 15:33:25 +0000 | [diff] [blame] | 1037 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1038 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1039 | } |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 1042 | /////////////////////////////////////////////////////////////////////////////// |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1043 | |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 1044 | void GrGLProgram::setData(GrGpuGL* gpu, |
| 1045 | GrColor color, |
| 1046 | GrColor coverage, |
| 1047 | SharedGLState* sharedState) { |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 1048 | const GrDrawState& drawState = gpu->getDrawState(); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 1049 | |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 1050 | this->setColor(drawState, color, sharedState); |
| 1051 | this->setCoverage(drawState, coverage, sharedState); |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 1052 | this->setMatrixAndRenderTargetHeight(drawState); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 1053 | |
| 1054 | // Setup the SkXfermode::Mode-based colorfilter uniform if necessary |
| 1055 | if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fColorFilterUni && |
| 1056 | fColorFilterColor != drawState.getColorFilterColor()) { |
| 1057 | GrGLfloat c[4]; |
| 1058 | GrColorToRGBAFloat(drawState.getColorFilterColor(), c); |
| 1059 | fUniformManager.set4fv(fUniformHandles.fColorFilterUni, 0, 1, c); |
| 1060 | fColorFilterColor = drawState.getColorFilterColor(); |
| 1061 | } |
| 1062 | |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 1063 | GrGLint texUnitIdx = 0; |
bsalomon@google.com | 4285acc | 2012-10-22 14:11:24 +0000 | [diff] [blame] | 1064 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 1065 | if (NULL != fEffects[s]) { |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 1066 | const GrEffectStage& stage = drawState.getStage(s); |
| 1067 | GrAssert(NULL != stage.getEffect()); |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 1068 | fEffects[s]->setData(fUniformManager, stage); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 1069 | int numSamplers = fUniformHandles.fSamplerUnis[s].count(); |
| 1070 | for (int u = 0; u < numSamplers; ++u) { |
| 1071 | UniformHandle handle = fUniformHandles.fSamplerUnis[s][u]; |
| 1072 | if (GrGLUniformManager::kInvalidUniformHandle != handle) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 1073 | const GrTextureAccess& access = (*stage.getEffect())->textureAccess(u); |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 1074 | GrGLTexture* texture = static_cast<GrGLTexture*>(access.getTexture()); |
| 1075 | gpu->bindTexture(texUnitIdx, access.getParams(), texture); |
| 1076 | ++texUnitIdx; |
| 1077 | } |
| 1078 | } |
bsalomon@google.com | 4285acc | 2012-10-22 14:11:24 +0000 | [diff] [blame] | 1079 | } |
| 1080 | } |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 1081 | } |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 1082 | |
| 1083 | void GrGLProgram::setColor(const GrDrawState& drawState, |
| 1084 | GrColor color, |
| 1085 | SharedGLState* sharedState) { |
| 1086 | if (!(drawState.getVertexLayout() & GrDrawState::kColor_VertexLayoutBit)) { |
| 1087 | switch (fDesc.fColorInput) { |
| 1088 | case GrGLProgram::Desc::kAttribute_ColorInput: |
| 1089 | if (sharedState->fConstAttribColor != color) { |
| 1090 | // OpenGL ES only supports the float varieties of glVertexAttrib |
| 1091 | GrGLfloat c[4]; |
| 1092 | GrColorToRGBAFloat(color, c); |
| 1093 | GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(), c)); |
| 1094 | sharedState->fConstAttribColor = color; |
| 1095 | } |
| 1096 | break; |
| 1097 | case GrGLProgram::Desc::kUniform_ColorInput: |
| 1098 | if (fColor != color) { |
| 1099 | // OpenGL ES doesn't support unsigned byte varieties of glUniform |
| 1100 | GrGLfloat c[4]; |
| 1101 | GrColorToRGBAFloat(color, c); |
| 1102 | GrAssert(GrGLUniformManager::kInvalidUniformHandle != |
| 1103 | fUniformHandles.fColorUni); |
| 1104 | fUniformManager.set4fv(fUniformHandles.fColorUni, 0, 1, c); |
| 1105 | fColor = color; |
| 1106 | } |
| 1107 | break; |
| 1108 | case GrGLProgram::Desc::kSolidWhite_ColorInput: |
| 1109 | case GrGLProgram::Desc::kTransBlack_ColorInput: |
| 1110 | break; |
| 1111 | default: |
| 1112 | GrCrash("Unknown color type."); |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | void GrGLProgram::setCoverage(const GrDrawState& drawState, |
| 1118 | GrColor coverage, |
| 1119 | SharedGLState* sharedState) { |
| 1120 | if (!(drawState.getVertexLayout() & GrDrawState::kCoverage_VertexLayoutBit)) { |
| 1121 | switch (fDesc.fCoverageInput) { |
| 1122 | case Desc::kAttribute_ColorInput: |
| 1123 | if (sharedState->fConstAttribCoverage != coverage) { |
| 1124 | // OpenGL ES only supports the float varieties of glVertexAttrib |
| 1125 | GrGLfloat c[4]; |
| 1126 | GrColorToRGBAFloat(coverage, c); |
| 1127 | GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(), c)); |
| 1128 | sharedState->fConstAttribCoverage = coverage; |
| 1129 | } |
| 1130 | break; |
| 1131 | case Desc::kUniform_ColorInput: |
| 1132 | if (fCoverage != coverage) { |
| 1133 | // OpenGL ES doesn't support unsigned byte varieties of glUniform |
| 1134 | GrGLfloat c[4]; |
| 1135 | GrColorToRGBAFloat(coverage, c); |
| 1136 | GrAssert(GrGLUniformManager::kInvalidUniformHandle != |
| 1137 | fUniformHandles.fCoverageUni); |
| 1138 | fUniformManager.set4fv(fUniformHandles.fCoverageUni, 0, 1, c); |
| 1139 | fCoverage = coverage; |
| 1140 | } |
| 1141 | break; |
| 1142 | case Desc::kSolidWhite_ColorInput: |
| 1143 | case Desc::kTransBlack_ColorInput: |
| 1144 | break; |
| 1145 | default: |
| 1146 | GrCrash("Unknown coverage type."); |
| 1147 | } |
| 1148 | } |
| 1149 | } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 1150 | |
| 1151 | void GrGLProgram::setMatrixAndRenderTargetHeight(const GrDrawState& drawState) { |
| 1152 | const GrRenderTarget* rt = drawState.getRenderTarget(); |
| 1153 | SkISize size; |
| 1154 | size.set(rt->width(), rt->height()); |
| 1155 | |
| 1156 | // Load the RT height uniform if it is needed to y-flip gl_FragCoord. |
| 1157 | if (GrGLUniformManager::kInvalidUniformHandle != fUniformHandles.fRTHeightUni && |
| 1158 | fMatrixState.fRenderTargetSize.fHeight != size.fHeight) { |
| 1159 | fUniformManager.set1f(fUniformHandles.fRTHeightUni, SkIntToScalar(size.fHeight)); |
| 1160 | } |
| 1161 | |
| 1162 | if (fMatrixState.fRenderTargetOrigin != rt->origin() || |
| 1163 | !fMatrixState.fViewMatrix.cheapEqualTo(drawState.getViewMatrix()) || |
| 1164 | fMatrixState.fRenderTargetSize != size) { |
| 1165 | SkMatrix m; |
| 1166 | if (kBottomLeft_GrSurfaceOrigin == rt->origin()) { |
| 1167 | m.setAll( |
| 1168 | SkIntToScalar(2) / size.fWidth, 0, -SK_Scalar1, |
| 1169 | 0,-SkIntToScalar(2) / size.fHeight, SK_Scalar1, |
| 1170 | 0, 0, SkMatrix::I()[8]); |
| 1171 | } else { |
| 1172 | m.setAll( |
| 1173 | SkIntToScalar(2) / size.fWidth, 0, -SK_Scalar1, |
| 1174 | 0, SkIntToScalar(2) / size.fHeight,-SK_Scalar1, |
| 1175 | 0, 0, SkMatrix::I()[8]); |
| 1176 | } |
| 1177 | m.setConcat(m, drawState.getViewMatrix()); |
| 1178 | |
| 1179 | // ES doesn't allow you to pass true to the transpose param so we do our own transpose. |
| 1180 | GrGLfloat mt[] = { |
| 1181 | SkScalarToFloat(m[SkMatrix::kMScaleX]), |
| 1182 | SkScalarToFloat(m[SkMatrix::kMSkewY]), |
| 1183 | SkScalarToFloat(m[SkMatrix::kMPersp0]), |
| 1184 | SkScalarToFloat(m[SkMatrix::kMSkewX]), |
| 1185 | SkScalarToFloat(m[SkMatrix::kMScaleY]), |
| 1186 | SkScalarToFloat(m[SkMatrix::kMPersp1]), |
| 1187 | SkScalarToFloat(m[SkMatrix::kMTransX]), |
| 1188 | SkScalarToFloat(m[SkMatrix::kMTransY]), |
| 1189 | SkScalarToFloat(m[SkMatrix::kMPersp2]) |
| 1190 | }; |
| 1191 | fUniformManager.setMatrix3f(fUniformHandles.fViewMatrixUni, mt); |
| 1192 | fMatrixState.fViewMatrix = drawState.getViewMatrix(); |
| 1193 | fMatrixState.fRenderTargetSize = size; |
| 1194 | fMatrixState.fRenderTargetOrigin = rt->origin(); |
| 1195 | } |
| 1196 | } |