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