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