blob: 3dc6601d290d9542fb88bc2b98cdb67b26fbb269 [file] [log] [blame]
jvanverth50530632015-04-27 10:36:27 -07001/*
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
joshualitt8072caa2015-02-12 14:20:52 -08008#include "GrPathProcessor.h"
9
Brian Salomon94efbf52016-11-29 13:43:05 -050010#include "GrShaderCaps.h"
joshualitt8072caa2015-02-12 14:20:52 -080011#include "gl/GrGLGpu.h"
egdaniel2d721d32015-11-11 13:06:05 -080012#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel7ea439b2015-12-03 09:20:44 -080013#include "glsl/GrGLSLUniformHandler.h"
egdaniel0eafe792015-11-20 14:01:22 -080014#include "glsl/GrGLSLVarying.h"
jvanverthcba99b82015-06-24 06:59:57 -070015
egdaniele659a582015-11-13 09:55:43 -080016class GrGLPathProcessor : public GrGLSLPrimitiveProcessor {
joshualitt102081a2015-09-11 11:52:17 -070017public:
18 GrGLPathProcessor() : fColor(GrColor_ILLEGAL) {}
19
20 static void GenKey(const GrPathProcessor& pathProc,
Brian Salomon94efbf52016-11-29 13:43:05 -050021 const GrShaderCaps&,
joshualitt102081a2015-09-11 11:52:17 -070022 GrProcessorKeyBuilder* b) {
Brian Salomonbfd51832017-01-04 13:22:08 -050023 b->add32(SkToInt(pathProc.viewMatrix().hasPerspective()));
joshualitt102081a2015-09-11 11:52:17 -070024 }
25
26 void emitCode(EmitArgs& args) override {
Chris Dalton60283612018-02-14 13:38:14 -070027 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt102081a2015-09-11 11:52:17 -070028 const GrPathProcessor& pathProc = args.fGP.cast<GrPathProcessor>();
29
cdaltonc08f1962016-02-12 12:14:06 -080030 if (!pathProc.viewMatrix().hasPerspective()) {
31 args.fVaryingHandler->setNoPerspective();
32 }
33
joshualitt102081a2015-09-11 11:52:17 -070034 // emit transforms
bsalomona624bf32016-09-20 09:12:47 -070035 this->emitTransforms(args.fVaryingHandler, args.fFPCoordTransformHandler);
joshualitt102081a2015-09-11 11:52:17 -070036
37 // Setup uniform color
Brian Salomonbfd51832017-01-04 13:22:08 -050038 const char* stagedLocalVarName;
39 fColorUniform = args.fUniformHandler->addUniform(kFragment_GrShaderFlag,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040040 kHalf4_GrSLType,
Brian Salomonbfd51832017-01-04 13:22:08 -050041 "Color",
42 &stagedLocalVarName);
43 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, stagedLocalVarName);
joshualitt102081a2015-09-11 11:52:17 -070044
45 // setup constant solid coverage
Ethan Nicholasf7b88202017-09-18 14:10:39 -040046 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
joshualitt102081a2015-09-11 11:52:17 -070047 }
48
egdaniel0eafe792015-11-20 14:01:22 -080049 void emitTransforms(GrGLSLVaryingHandler* varyingHandler,
bsalomona624bf32016-09-20 09:12:47 -070050 FPCoordTransformHandler* transformHandler) {
51 int i = 0;
52 while (const GrCoordTransform* coordTransform = transformHandler->nextCoordTransform()) {
53 GrSLType varyingType =
Ethan Nicholasf7b88202017-09-18 14:10:39 -040054 coordTransform->getMatrix().hasPerspective() ? kHalf3_GrSLType
55 : kHalf2_GrSLType;
joshualitt102081a2015-09-11 11:52:17 -070056
bsalomona624bf32016-09-20 09:12:47 -070057 SkString strVaryingName;
58 strVaryingName.printf("TransformedCoord_%d", i);
Chris Dalton27372882017-12-08 13:34:21 -070059 GrGLSLVarying v(varyingType);
bsalomona624bf32016-09-20 09:12:47 -070060 GrGLVaryingHandler* glVaryingHandler = (GrGLVaryingHandler*) varyingHandler;
61 fInstalledTransforms.push_back().fHandle =
62 glVaryingHandler->addPathProcessingVarying(strVaryingName.c_str(),
63 &v).toIndex();
64 fInstalledTransforms.back().fType = varyingType;
joshualitt102081a2015-09-11 11:52:17 -070065
bsalomona624bf32016-09-20 09:12:47 -070066 transformHandler->specifyCoordsForCurrCoordTransform(SkString(v.fsIn()), varyingType);
67 ++i;
joshualitt102081a2015-09-11 11:52:17 -070068 }
69 }
70
egdaniel018fb622015-10-28 07:26:40 -070071 void setData(const GrGLSLProgramDataManager& pd,
bsalomona624bf32016-09-20 09:12:47 -070072 const GrPrimitiveProcessor& primProc,
73 FPCoordTransformIter&& transformIter) override {
joshualitt102081a2015-09-11 11:52:17 -070074 const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>();
Brian Salomonbfd51832017-01-04 13:22:08 -050075 if (pathProc.color() != fColor) {
egdaniel018fb622015-10-28 07:26:40 -070076 float c[4];
joshualitt102081a2015-09-11 11:52:17 -070077 GrColorToRGBAFloat(pathProc.color(), c);
78 pd.set4fv(fColorUniform, 1, c);
79 fColor = pathProc.color();
80 }
joshualitt102081a2015-09-11 11:52:17 -070081
bsalomona624bf32016-09-20 09:12:47 -070082 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)) {
joshualitt102081a2015-09-11 11:52:17 -070087 continue;
88 }
bsalomona624bf32016-09-20 09:12:47 -070089 fInstalledTransforms[t].fCurrentValue = m;
joshualitt102081a2015-09-11 11:52:17 -070090
Ethan Nicholasf7b88202017-09-18 14:10:39 -040091 SkASSERT(fInstalledTransforms[t].fType == kHalf2_GrSLType ||
92 fInstalledTransforms[t].fType == kHalf3_GrSLType);
93 unsigned components = fInstalledTransforms[t].fType == kHalf2_GrSLType ? 2 : 3;
bsalomona624bf32016-09-20 09:12:47 -070094 pd.setPathFragmentInputTransform(fInstalledTransforms[t].fHandle, components, m);
95 ++t;
joshualitt102081a2015-09-11 11:52:17 -070096 }
97 }
98
99private:
egdanielfe8a8392016-05-09 10:22:19 -0700100 typedef GrGLSLProgramDataManager::VaryingHandle VaryingHandle;
bsalomon790c90b2016-09-12 12:56:58 -0700101 struct TransformVarying {
egdanielfe8a8392016-05-09 10:22:19 -0700102 VaryingHandle fHandle;
bsalomon790c90b2016-09-12 12:56:58 -0700103 SkMatrix fCurrentValue = SkMatrix::InvalidMatrix();
104 GrSLType fType = kVoid_GrSLType;
egdanielfe8a8392016-05-09 10:22:19 -0700105 };
106
bsalomona624bf32016-09-20 09:12:47 -0700107 SkTArray<TransformVarying, true> fInstalledTransforms;
egdanielfe8a8392016-05-09 10:22:19 -0700108
joshualitt102081a2015-09-11 11:52:17 -0700109 UniformHandle fColorUniform;
110 GrColor fColor;
111
egdaniele659a582015-11-13 09:55:43 -0800112 typedef GrGLSLPrimitiveProcessor INHERITED;
joshualitt102081a2015-09-11 11:52:17 -0700113};
114
joshualitt8072caa2015-02-12 14:20:52 -0800115GrPathProcessor::GrPathProcessor(GrColor color,
116 const SkMatrix& viewMatrix,
117 const SkMatrix& localMatrix)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400118 : INHERITED(kGrPathProcessor_ClassID)
119 , fColor(color)
Brian Salomon92aee3d2016-12-21 09:20:25 -0500120 , fViewMatrix(viewMatrix)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400121 , fLocalMatrix(localMatrix) {}
joshualitt8072caa2015-02-12 14:20:52 -0800122
Brian Salomon94efbf52016-11-29 13:43:05 -0500123void GrPathProcessor::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800124 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700125 GrGLPathProcessor::GenKey(*this, caps, b);
joshualitt8072caa2015-02-12 14:20:52 -0800126}
127
Brian Salomon94efbf52016-11-29 13:43:05 -0500128GrGLSLPrimitiveProcessor* GrPathProcessor::createGLSLInstance(const GrShaderCaps& caps) const {
jvanverth50530632015-04-27 10:36:27 -0700129 SkASSERT(caps.pathRenderingSupport());
joshualitt465283c2015-09-11 08:19:35 -0700130 return new GrGLPathProcessor();
joshualitt8072caa2015-02-12 14:20:52 -0800131}