jvanverth | 5053063 | 2015-04-27 10:36:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 8 | #include "GrPathProcessor.h" |
| 9 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 10 | #include "gl/GrGLGpu.h" |
jvanverth | cba99b8 | 2015-06-24 06:59:57 -0700 | [diff] [blame] | 11 | #include "glsl/GrGLSLCaps.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 12 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 13 | #include "glsl/GrGLSLProcessorTypes.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLUniformHandler.h" |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLVarying.h" |
jvanverth | cba99b8 | 2015-06-24 06:59:57 -0700 | [diff] [blame] | 16 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 17 | class GrGLPathProcessor : public GrGLSLPrimitiveProcessor { |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 18 | public: |
| 19 | GrGLPathProcessor() : fColor(GrColor_ILLEGAL) {} |
| 20 | |
| 21 | static void GenKey(const GrPathProcessor& pathProc, |
| 22 | const GrGLSLCaps&, |
| 23 | GrProcessorKeyBuilder* b) { |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 24 | b->add32(SkToInt(pathProc.overrides().readsColor()) | |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 25 | (SkToInt(pathProc.overrides().readsCoverage()) << 1) | |
| 26 | (SkToInt(pathProc.viewMatrix().hasPerspective()) << 2)); |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void emitCode(EmitArgs& args) override { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 30 | GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder; |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 31 | const GrPathProcessor& pathProc = args.fGP.cast<GrPathProcessor>(); |
| 32 | |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 33 | if (!pathProc.viewMatrix().hasPerspective()) { |
| 34 | args.fVaryingHandler->setNoPerspective(); |
| 35 | } |
| 36 | |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 37 | // emit transforms |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 38 | this->emitTransforms(args.fVaryingHandler, args.fTransformsIn, args.fTransformsOut); |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 39 | |
| 40 | // Setup uniform color |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 41 | if (pathProc.overrides().readsColor()) { |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 42 | const char* stagedLocalVarName; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 43 | fColorUniform = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, |
| 44 | kVec4f_GrSLType, |
| 45 | kDefault_GrSLPrecision, |
| 46 | "Color", |
| 47 | &stagedLocalVarName); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 48 | fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, stagedLocalVarName); |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // setup constant solid coverage |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 52 | if (pathProc.overrides().readsCoverage()) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 53 | fragBuilder->codeAppendf("%s = vec4(1);", args.fOutputCoverage); |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 57 | void emitTransforms(GrGLSLVaryingHandler* varyingHandler, |
| 58 | const TransformsIn& tin, |
| 59 | TransformsOut* tout) { |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 60 | tout->push_back_n(tin.count()); |
| 61 | fInstalledTransforms.push_back_n(tin.count()); |
| 62 | for (int i = 0; i < tin.count(); i++) { |
| 63 | const ProcCoords& coordTransforms = tin[i]; |
| 64 | fInstalledTransforms[i].push_back_n(coordTransforms.count()); |
| 65 | for (int t = 0; t < coordTransforms.count(); t++) { |
| 66 | GrSLType varyingType = |
| 67 | coordTransforms[t]->getMatrix().hasPerspective() ? kVec3f_GrSLType : |
| 68 | kVec2f_GrSLType; |
| 69 | |
| 70 | SkString strVaryingName("MatrixCoord"); |
| 71 | strVaryingName.appendf("_%i_%i", i, t); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 72 | GrGLSLVertToFrag v(varyingType); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 73 | GrGLVaryingHandler* glVaryingHandler = (GrGLVaryingHandler*) varyingHandler; |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 74 | fInstalledTransforms[i][t].fHandle = |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 75 | glVaryingHandler->addPathProcessingVarying(strVaryingName.c_str(), |
| 76 | &v).toIndex(); |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 77 | fInstalledTransforms[i][t].fType = varyingType; |
| 78 | |
bungeman | 85dc359 | 2016-02-09 11:32:56 -0800 | [diff] [blame] | 79 | (*tout)[i].emplace_back(SkString(v.fsIn()), varyingType); |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 84 | void setData(const GrGLSLProgramDataManager& pd, |
| 85 | const GrPrimitiveProcessor& primProc) override { |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 86 | const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>(); |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 87 | if (pathProc.overrides().readsColor() && pathProc.color() != fColor) { |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 88 | float c[4]; |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 89 | GrColorToRGBAFloat(pathProc.color(), c); |
| 90 | pd.set4fv(fColorUniform, 1, c); |
| 91 | fColor = pathProc.color(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void setTransformData(const GrPrimitiveProcessor& primProc, |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 96 | const GrGLSLProgramDataManager& pdman, |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 97 | int index, |
| 98 | const SkTArray<const GrCoordTransform*, true>& coordTransforms) override { |
| 99 | const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>(); |
egdaniel | fe8a839 | 2016-05-09 10:22:19 -0700 | [diff] [blame] | 100 | SkSTArray<2, VaryingTransform, true>& transforms = fInstalledTransforms[index]; |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 101 | int numTransforms = transforms.count(); |
| 102 | for (int t = 0; t < numTransforms; ++t) { |
| 103 | SkASSERT(transforms[t].fHandle.isValid()); |
| 104 | const SkMatrix& transform = GetTransformMatrix(pathProc.localMatrix(), |
| 105 | *coordTransforms[t]); |
| 106 | if (transforms[t].fCurrentValue.cheapEqualTo(transform)) { |
| 107 | continue; |
| 108 | } |
| 109 | transforms[t].fCurrentValue = transform; |
| 110 | |
| 111 | SkASSERT(transforms[t].fType == kVec2f_GrSLType || |
| 112 | transforms[t].fType == kVec3f_GrSLType); |
| 113 | unsigned components = transforms[t].fType == kVec2f_GrSLType ? 2 : 3; |
| 114 | pdman.setPathFragmentInputTransform(transforms[t].fHandle, components, transform); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | private: |
egdaniel | fe8a839 | 2016-05-09 10:22:19 -0700 | [diff] [blame] | 119 | typedef GrGLSLProgramDataManager::VaryingHandle VaryingHandle; |
| 120 | struct VaryingTransform : public Transform { |
| 121 | VaryingTransform() : Transform() {} |
| 122 | VaryingHandle fHandle; |
| 123 | }; |
| 124 | |
| 125 | SkSTArray<8, SkSTArray<2, VaryingTransform, true> > fInstalledTransforms; |
| 126 | |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 127 | UniformHandle fColorUniform; |
| 128 | GrColor fColor; |
| 129 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 130 | typedef GrGLSLPrimitiveProcessor INHERITED; |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 133 | GrPathProcessor::GrPathProcessor(GrColor color, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 134 | const GrXPOverridesForBatch& overrides, |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 135 | const SkMatrix& viewMatrix, |
| 136 | const SkMatrix& localMatrix) |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 137 | : fColor(color) |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 138 | , fViewMatrix(viewMatrix) |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 139 | , fLocalMatrix(localMatrix) |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 140 | , fOverrides(overrides) { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 141 | this->initClassID<GrPathProcessor>(); |
| 142 | } |
| 143 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 144 | void GrPathProcessor::getGLSLProcessorKey(const GrGLSLCaps& caps, |
| 145 | GrProcessorKeyBuilder* b) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 146 | GrGLPathProcessor::GenKey(*this, caps, b); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 147 | } |
| 148 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 149 | GrGLSLPrimitiveProcessor* GrPathProcessor::createGLSLInstance(const GrGLSLCaps& caps) const { |
jvanverth | 5053063 | 2015-04-27 10:36:27 -0700 | [diff] [blame] | 150 | SkASSERT(caps.pathRenderingSupport()); |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 151 | return new GrGLPathProcessor(); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 152 | } |