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