joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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. |
| 6 | */ |
| 7 | |
| 8 | #include "GrGeometryProcessor.h" |
| 9 | |
| 10 | #include "gl/GrGLGeometryProcessor.h" |
| 11 | #include "GrInvariantOutput.h" |
| 12 | |
| 13 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 14 | |
| 15 | void GrGeometryProcessor::getInvariantOutputColor(GrInitInvariantOutput* out) const { |
| 16 | if (fHasVertexColor) { |
| 17 | if (fOpaqueVertexColors) { |
| 18 | out->setUnknownOpaqueFourComponents(); |
| 19 | } else { |
| 20 | out->setUnknownFourComponents(); |
| 21 | } |
| 22 | } else { |
| 23 | out->setKnownFourComponents(fColor); |
| 24 | } |
| 25 | this->onGetInvariantOutputColor(out); |
| 26 | } |
| 27 | |
| 28 | void GrGeometryProcessor::getInvariantOutputCoverage(GrInitInvariantOutput* out) const { |
| 29 | this->onGetInvariantOutputCoverage(out); |
| 30 | } |
| 31 | |
| 32 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 33 | |
| 34 | #include "gl/builders/GrGLProgramBuilder.h" |
| 35 | |
| 36 | void GrGLGeometryProcessor::setupColorPassThrough(GrGLGPBuilder* pb, |
| 37 | GrGPInput inputType, |
| 38 | const char* outputName, |
| 39 | const GrGeometryProcessor::GrAttribute* colorAttr, |
| 40 | UniformHandle* colorUniform) { |
| 41 | GrGLGPFragmentBuilder* fs = pb->getFragmentShaderBuilder(); |
| 42 | if (kUniform_GrGPInput == inputType) { |
| 43 | SkASSERT(colorUniform); |
| 44 | const char* stagedLocalVarName; |
| 45 | *colorUniform = pb->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| 46 | kVec4f_GrSLType, |
| 47 | kDefault_GrSLPrecision, |
| 48 | "Color", |
| 49 | &stagedLocalVarName); |
| 50 | fs->codeAppendf("%s = %s;", outputName, stagedLocalVarName); |
| 51 | } else if (kAttribute_GrGPInput == inputType) { |
| 52 | SkASSERT(colorAttr); |
| 53 | pb->addPassThroughAttribute(colorAttr, outputName); |
| 54 | } else if (kAllOnes_GrGPInput == inputType) { |
| 55 | fs->codeAppendf("%s = vec4(1);", outputName); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 60 | |
| 61 | struct PathBatchTracker { |
| 62 | GrGPInput fInputColorType; |
| 63 | GrGPInput fInputCoverageType; |
| 64 | GrColor fColor; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 65 | bool fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | class GrGLPathProcessor : public GrGLGeometryProcessor { |
| 69 | public: |
| 70 | GrGLPathProcessor(const GrPathProcessor&, const GrBatchTracker&) |
| 71 | : fColor(GrColor_ILLEGAL) {} |
| 72 | |
| 73 | void emitCode(const EmitArgs& args) SK_OVERRIDE { |
| 74 | GrGLGPBuilder* pb = args.fPB; |
| 75 | GrGLGPFragmentBuilder* fs = args.fPB->getFragmentShaderBuilder(); |
| 76 | const PathBatchTracker& local = args.fBT.cast<PathBatchTracker>(); |
| 77 | |
| 78 | // Setup uniform color |
| 79 | if (kUniform_GrGPInput == local.fInputColorType) { |
| 80 | const char* stagedLocalVarName; |
| 81 | fColorUniform = pb->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| 82 | kVec4f_GrSLType, |
| 83 | kDefault_GrSLPrecision, |
| 84 | "Color", |
| 85 | &stagedLocalVarName); |
| 86 | fs->codeAppendf("%s = %s;", args.fOutputColor, stagedLocalVarName); |
| 87 | } |
| 88 | |
| 89 | // setup constant solid coverage |
| 90 | if (kAllOnes_GrGPInput == local.fInputCoverageType) { |
| 91 | fs->codeAppendf("%s = vec4(1);", args.fOutputCoverage); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static inline void GenKey(const GrPathProcessor&, |
| 96 | const GrBatchTracker& bt, |
| 97 | const GrGLCaps&, |
| 98 | GrProcessorKeyBuilder* b) { |
| 99 | const PathBatchTracker& local = bt.cast<PathBatchTracker>(); |
| 100 | b->add32(local.fInputColorType | local.fInputCoverageType << 16); |
| 101 | } |
| 102 | |
| 103 | void setData(const GrGLProgramDataManager& pdman, |
| 104 | const GrPrimitiveProcessor& primProc, |
| 105 | const GrBatchTracker& bt) SK_OVERRIDE { |
| 106 | const PathBatchTracker& local = bt.cast<PathBatchTracker>(); |
| 107 | if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) { |
| 108 | GrGLfloat c[4]; |
| 109 | GrColorToRGBAFloat(local.fColor, c); |
| 110 | pdman.set4fv(fColorUniform, 1, c); |
| 111 | fColor = local.fColor; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | UniformHandle fColorUniform; |
| 117 | GrColor fColor; |
| 118 | |
| 119 | typedef GrGLGeometryProcessor INHERITED; |
| 120 | }; |
| 121 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame^] | 122 | GrPathProcessor::GrPathProcessor(GrColor color, |
| 123 | const SkMatrix& viewMatrix, |
| 124 | const SkMatrix& localMatrix) |
| 125 | : INHERITED(viewMatrix, localMatrix) |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 126 | , fColor(color) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 127 | this->initClassID<GrPathProcessor>(); |
| 128 | } |
| 129 | |
| 130 | void GrPathProcessor::getInvariantOutputColor(GrInitInvariantOutput* out) const { |
| 131 | out->setKnownFourComponents(fColor); |
| 132 | } |
| 133 | |
| 134 | void GrPathProcessor::getInvariantOutputCoverage(GrInitInvariantOutput* out) const { |
| 135 | out->setKnownSingleComponent(0xff); |
| 136 | } |
| 137 | |
| 138 | void GrPathProcessor::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const { |
| 139 | PathBatchTracker* local = bt->cast<PathBatchTracker>(); |
| 140 | if (init.fColorIgnored) { |
| 141 | local->fInputColorType = kIgnored_GrGPInput; |
| 142 | local->fColor = GrColor_ILLEGAL; |
| 143 | } else { |
| 144 | local->fInputColorType = kUniform_GrGPInput; |
| 145 | local->fColor = GrColor_ILLEGAL == init.fOverrideColor ? this->color() : |
| 146 | init.fOverrideColor; |
| 147 | } |
| 148 | |
| 149 | local->fInputCoverageType = init.fCoverageIgnored ? kIgnored_GrGPInput : kAllOnes_GrGPInput; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 150 | local->fUsesLocalCoords = init.fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | bool GrPathProcessor::canMakeEqual(const GrBatchTracker& m, |
| 154 | const GrPrimitiveProcessor& that, |
| 155 | const GrBatchTracker& t) const { |
| 156 | if (this->classID() != that.classID() || !this->hasSameTextureAccesses(that)) { |
| 157 | return false; |
| 158 | } |
| 159 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame^] | 160 | if (!this->viewMatrix().cheapEqualTo(that.viewMatrix())) { |
| 161 | return false; |
| 162 | } |
| 163 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 164 | const PathBatchTracker& mine = m.cast<PathBatchTracker>(); |
| 165 | const PathBatchTracker& theirs = t.cast<PathBatchTracker>(); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 166 | return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords, |
| 167 | that, theirs.fUsesLocalCoords) && |
| 168 | CanCombineOutput(mine.fInputColorType, mine.fColor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 169 | theirs.fInputColorType, theirs.fColor) && |
| 170 | CanCombineOutput(mine.fInputCoverageType, 0xff, |
| 171 | theirs.fInputCoverageType, 0xff); |
| 172 | } |
| 173 | |
| 174 | void GrPathProcessor::getGLProcessorKey(const GrBatchTracker& bt, |
| 175 | const GrGLCaps& caps, |
| 176 | GrProcessorKeyBuilder* b) const { |
| 177 | GrGLPathProcessor::GenKey(*this, bt, caps, b); |
| 178 | } |
| 179 | |
| 180 | GrGLGeometryProcessor* GrPathProcessor::createGLInstance(const GrBatchTracker& bt) const { |
| 181 | return SkNEW_ARGS(GrGLPathProcessor, (*this, bt)); |
| 182 | } |