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 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 10 | #include "GrShaderCaps.h" |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 11 | #include "gl/GrGLGpu.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 12 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLUniformHandler.h" |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLVarying.h" |
jvanverth | cba99b8 | 2015-06-24 06:59:57 -0700 | [diff] [blame] | 15 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 16 | class GrGLPathProcessor : public GrGLSLPrimitiveProcessor { |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 17 | public: |
| 18 | GrGLPathProcessor() : fColor(GrColor_ILLEGAL) {} |
| 19 | |
| 20 | static void GenKey(const GrPathProcessor& pathProc, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 21 | const GrShaderCaps&, |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 22 | GrProcessorKeyBuilder* b) { |
Brian Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 23 | b->add32(SkToInt(pathProc.viewMatrix().hasPerspective())); |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | void emitCode(EmitArgs& args) override { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 27 | GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder; |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 28 | const GrPathProcessor& pathProc = args.fGP.cast<GrPathProcessor>(); |
| 29 | |
cdalton | c08f196 | 2016-02-12 12:14:06 -0800 | [diff] [blame] | 30 | if (!pathProc.viewMatrix().hasPerspective()) { |
| 31 | args.fVaryingHandler->setNoPerspective(); |
| 32 | } |
| 33 | |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 34 | // emit transforms |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 35 | this->emitTransforms(args.fVaryingHandler, args.fFPCoordTransformHandler); |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 36 | |
| 37 | // Setup uniform color |
Brian Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 38 | const char* stagedLocalVarName; |
| 39 | fColorUniform = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 40 | kHalf4_GrSLType, |
Brian Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 41 | "Color", |
| 42 | &stagedLocalVarName); |
| 43 | fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, stagedLocalVarName); |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 44 | |
| 45 | // setup constant solid coverage |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 46 | fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 47 | } |
| 48 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 49 | void emitTransforms(GrGLSLVaryingHandler* varyingHandler, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 50 | FPCoordTransformHandler* transformHandler) { |
| 51 | int i = 0; |
| 52 | while (const GrCoordTransform* coordTransform = transformHandler->nextCoordTransform()) { |
| 53 | GrSLType varyingType = |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 54 | coordTransform->getMatrix().hasPerspective() ? kHalf3_GrSLType |
| 55 | : kHalf2_GrSLType; |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 56 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 57 | SkString strVaryingName; |
| 58 | strVaryingName.printf("TransformedCoord_%d", i); |
| 59 | GrGLSLVertToFrag v(varyingType); |
| 60 | GrGLVaryingHandler* glVaryingHandler = (GrGLVaryingHandler*) varyingHandler; |
| 61 | fInstalledTransforms.push_back().fHandle = |
| 62 | glVaryingHandler->addPathProcessingVarying(strVaryingName.c_str(), |
| 63 | &v).toIndex(); |
| 64 | fInstalledTransforms.back().fType = varyingType; |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 65 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 66 | transformHandler->specifyCoordsForCurrCoordTransform(SkString(v.fsIn()), varyingType); |
| 67 | ++i; |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 71 | void setData(const GrGLSLProgramDataManager& pd, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 72 | const GrPrimitiveProcessor& primProc, |
| 73 | FPCoordTransformIter&& transformIter) override { |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 74 | const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>(); |
Brian Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 75 | if (pathProc.color() != fColor) { |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 76 | float c[4]; |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 77 | GrColorToRGBAFloat(pathProc.color(), c); |
| 78 | pd.set4fv(fColorUniform, 1, c); |
| 79 | fColor = pathProc.color(); |
| 80 | } |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 81 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 82 | int t = 0; |
| 83 | while (const GrCoordTransform* coordTransform = transformIter.next()) { |
| 84 | SkASSERT(fInstalledTransforms[t].fHandle.isValid()); |
| 85 | const SkMatrix& m = GetTransformMatrix(pathProc.localMatrix(), *coordTransform); |
| 86 | if (fInstalledTransforms[t].fCurrentValue.cheapEqualTo(m)) { |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 87 | continue; |
| 88 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 89 | fInstalledTransforms[t].fCurrentValue = m; |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 90 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 91 | SkASSERT(fInstalledTransforms[t].fType == kHalf2_GrSLType || |
| 92 | fInstalledTransforms[t].fType == kHalf3_GrSLType); |
| 93 | unsigned components = fInstalledTransforms[t].fType == kHalf2_GrSLType ? 2 : 3; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 94 | pd.setPathFragmentInputTransform(fInstalledTransforms[t].fHandle, components, m); |
| 95 | ++t; |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | private: |
egdaniel | fe8a839 | 2016-05-09 10:22:19 -0700 | [diff] [blame] | 100 | typedef GrGLSLProgramDataManager::VaryingHandle VaryingHandle; |
bsalomon | 790c90b | 2016-09-12 12:56:58 -0700 | [diff] [blame] | 101 | struct TransformVarying { |
egdaniel | fe8a839 | 2016-05-09 10:22:19 -0700 | [diff] [blame] | 102 | VaryingHandle fHandle; |
bsalomon | 790c90b | 2016-09-12 12:56:58 -0700 | [diff] [blame] | 103 | SkMatrix fCurrentValue = SkMatrix::InvalidMatrix(); |
| 104 | GrSLType fType = kVoid_GrSLType; |
egdaniel | fe8a839 | 2016-05-09 10:22:19 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 107 | SkTArray<TransformVarying, true> fInstalledTransforms; |
egdaniel | fe8a839 | 2016-05-09 10:22:19 -0700 | [diff] [blame] | 108 | |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 109 | UniformHandle fColorUniform; |
| 110 | GrColor fColor; |
| 111 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 112 | typedef GrGLSLPrimitiveProcessor INHERITED; |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 115 | GrPathProcessor::GrPathProcessor(GrColor color, |
| 116 | const SkMatrix& viewMatrix, |
| 117 | const SkMatrix& localMatrix) |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 118 | : fColor(color) |
| 119 | , fViewMatrix(viewMatrix) |
Brian Salomon | ecea86a | 2017-01-04 13:25:17 -0500 | [diff] [blame] | 120 | , fLocalMatrix(localMatrix) { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 121 | this->initClassID<GrPathProcessor>(); |
| 122 | } |
| 123 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 124 | void GrPathProcessor::getGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 125 | GrProcessorKeyBuilder* b) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 126 | GrGLPathProcessor::GenKey(*this, caps, b); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 129 | GrGLSLPrimitiveProcessor* GrPathProcessor::createGLSLInstance(const GrShaderCaps& caps) const { |
jvanverth | 5053063 | 2015-04-27 10:36:27 -0700 | [diff] [blame] | 130 | SkASSERT(caps.pathRenderingSupport()); |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 131 | return new GrGLPathProcessor(); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 132 | } |