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