commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [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 | |
| 8 | #include "GrBezierEffect.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 9 | #include "GrShaderCaps.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 10 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 11 | #include "glsl/GrGLSLGeometryProcessor.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 12 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLUniformHandler.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLUtil.h" |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLVarying.h" |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 16 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 17 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 18 | class GrGLConicEffect : public GrGLSLGeometryProcessor { |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 19 | public: |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 20 | GrGLConicEffect(const GrGeometryProcessor&); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 21 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 22 | void onEmitCode(EmitArgs&, GrGPArgs*) override; |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 23 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 24 | static inline void GenKey(const GrGeometryProcessor&, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 25 | const GrShaderCaps&, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 26 | GrProcessorKeyBuilder*); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 27 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 28 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc, |
| 29 | FPCoordTransformIter&& transformIter) override { |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 30 | const GrConicEffect& ce = primProc.cast<GrConicEffect>(); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 31 | |
| 32 | if (!ce.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(ce.viewMatrix())) { |
| 33 | fViewMatrix = ce.viewMatrix(); |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 34 | float viewMatrix[3 * 3]; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 35 | GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 36 | pdman.setMatrix3f(fViewMatrixUniform, viewMatrix); |
| 37 | } |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 38 | |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 39 | if (ce.color() != fColor) { |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 40 | float c[4]; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 41 | GrColorToRGBAFloat(ce.color(), c); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 42 | pdman.set4fv(fColorUniform, 1, c); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 43 | fColor = ce.color(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 44 | } |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 45 | |
| 46 | if (ce.coverageScale() != 0xff && ce.coverageScale() != fCoverageScale) { |
| 47 | pdman.set1f(fCoverageScaleUniform, GrNormalizeByteToFloat(ce.coverageScale())); |
| 48 | fCoverageScale = ce.coverageScale(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 49 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 50 | this->setTransformDataHelper(ce.localMatrix(), pdman, &transformIter); |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 51 | } |
| 52 | |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 53 | private: |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 54 | SkMatrix fViewMatrix; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 55 | GrColor fColor; |
| 56 | uint8_t fCoverageScale; |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 57 | GrClipEdgeType fEdgeType; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 58 | UniformHandle fColorUniform; |
| 59 | UniformHandle fCoverageScaleUniform; |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 60 | UniformHandle fViewMatrixUniform; |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 61 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 62 | typedef GrGLSLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 63 | }; |
skia.committer@gmail.com | 44a77c8 | 2013-08-23 07:01:29 +0000 | [diff] [blame] | 64 | |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 65 | GrGLConicEffect::GrGLConicEffect(const GrGeometryProcessor& processor) |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 66 | : fViewMatrix(SkMatrix::InvalidMatrix()), fColor(GrColor_ILLEGAL), fCoverageScale(0xff) { |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 67 | const GrConicEffect& ce = processor.cast<GrConicEffect>(); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 68 | fEdgeType = ce.getEdgeType(); |
| 69 | } |
| 70 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 71 | void GrGLConicEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 72 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 73 | const GrConicEffect& gp = args.fGP.cast<GrConicEffect>(); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 74 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 75 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 76 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 77 | // emit attributes |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 78 | varyingHandler->emitAttributes(gp); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 79 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 80 | GrGLSLVertToFrag v(kFloat4_GrSLType); |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 81 | varyingHandler->addVarying("ConicCoeffs", &v); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 82 | vertBuilder->codeAppendf("%s = %s;", v.vsOut(), gp.inConicCoeffs()->fName); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 83 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 84 | GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 85 | // Setup pass through color |
Brian Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 86 | this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, &fColorUniform); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 87 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 88 | // Setup position |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 89 | this->writeOutputPosition(vertBuilder, |
| 90 | uniformHandler, |
| 91 | gpArgs, |
| 92 | gp.inPosition()->fName, |
| 93 | gp.viewMatrix(), |
| 94 | &fViewMatrixUniform); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 95 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 96 | // emit transforms with position |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 97 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 98 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 99 | uniformHandler, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 100 | gpArgs->fPositionVar, |
| 101 | gp.inPosition()->fName, |
| 102 | gp.localMatrix(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 103 | args.fFPCoordTransformHandler); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 104 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 105 | // TODO: we should check on the number of bits float and half provide and use the smallest one |
| 106 | // that suffices. Additionally we should assert that the upstream code only lets us get here if |
| 107 | // either float or half provides the required number of bits. |
robertphillips | 2eb1009 | 2015-12-11 04:59:36 -0800 | [diff] [blame] | 108 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 109 | GrShaderVar edgeAlpha("edgeAlpha", kFloat_GrSLType, 0); |
| 110 | GrShaderVar dklmdx("dklmdx", kFloat3_GrSLType, 0); |
| 111 | GrShaderVar dklmdy("dklmdy", kFloat3_GrSLType, 0); |
| 112 | GrShaderVar dfdx("dfdx", kFloat_GrSLType, 0); |
| 113 | GrShaderVar dfdy("dfdy", kFloat_GrSLType, 0); |
| 114 | GrShaderVar gF("gF", kFloat2_GrSLType, 0); |
| 115 | GrShaderVar gFM("gFM", kFloat_GrSLType, 0); |
| 116 | GrShaderVar func("func", kFloat_GrSLType, 0); |
robertphillips | 96afa52 | 2015-12-09 07:54:24 -0800 | [diff] [blame] | 117 | |
| 118 | fragBuilder->declAppend(edgeAlpha); |
| 119 | fragBuilder->declAppend(dklmdx); |
| 120 | fragBuilder->declAppend(dklmdy); |
| 121 | fragBuilder->declAppend(dfdx); |
| 122 | fragBuilder->declAppend(dfdy); |
| 123 | fragBuilder->declAppend(gF); |
| 124 | fragBuilder->declAppend(gFM); |
| 125 | fragBuilder->declAppend(func); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 126 | |
| 127 | switch (fEdgeType) { |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame^] | 128 | case GrClipEdgeType::kHairlineAA: { |
robertphillips | 96afa52 | 2015-12-09 07:54:24 -0800 | [diff] [blame] | 129 | fragBuilder->codeAppendf("%s = dFdx(%s.xyz);", dklmdx.c_str(), v.fsIn()); |
| 130 | fragBuilder->codeAppendf("%s = dFdy(%s.xyz);", dklmdy.c_str(), v.fsIn()); |
| 131 | fragBuilder->codeAppendf("%s = 2.0 * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;", |
| 132 | dfdx.c_str(), |
| 133 | v.fsIn(), dklmdx.c_str(), |
| 134 | v.fsIn(), dklmdx.c_str(), |
| 135 | v.fsIn(), dklmdx.c_str()); |
| 136 | fragBuilder->codeAppendf("%s = 2.0 * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;", |
| 137 | dfdy.c_str(), |
| 138 | v.fsIn(), dklmdy.c_str(), |
| 139 | v.fsIn(), dklmdy.c_str(), |
| 140 | v.fsIn(), dklmdy.c_str()); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 141 | fragBuilder->codeAppendf("%s = float2(%s, %s);", gF.c_str(), dfdx.c_str(), |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 142 | dfdy.c_str()); |
robertphillips | 96afa52 | 2015-12-09 07:54:24 -0800 | [diff] [blame] | 143 | fragBuilder->codeAppendf("%s = sqrt(dot(%s, %s));", |
| 144 | gFM.c_str(), gF.c_str(), gF.c_str()); |
| 145 | fragBuilder->codeAppendf("%s = %s.x*%s.x - %s.y*%s.z;", |
| 146 | func.c_str(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn()); |
| 147 | fragBuilder->codeAppendf("%s = abs(%s);", func.c_str(), func.c_str()); |
| 148 | fragBuilder->codeAppendf("%s = %s / %s;", |
| 149 | edgeAlpha.c_str(), func.c_str(), gFM.c_str()); |
| 150 | fragBuilder->codeAppendf("%s = max(1.0 - %s, 0.0);", |
| 151 | edgeAlpha.c_str(), edgeAlpha.c_str()); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 152 | // Add line below for smooth cubic ramp |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 153 | // fragBuilder->codeAppend("edgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);"); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 154 | break; |
| 155 | } |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame^] | 156 | case GrClipEdgeType::kFillAA: { |
robertphillips | 96afa52 | 2015-12-09 07:54:24 -0800 | [diff] [blame] | 157 | fragBuilder->codeAppendf("%s = dFdx(%s.xyz);", dklmdx.c_str(), v.fsIn()); |
| 158 | fragBuilder->codeAppendf("%s = dFdy(%s.xyz);", dklmdy.c_str(), v.fsIn()); |
| 159 | fragBuilder->codeAppendf("%s =" |
| 160 | "2.0 * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;", |
| 161 | dfdx.c_str(), |
| 162 | v.fsIn(), dklmdx.c_str(), |
| 163 | v.fsIn(), dklmdx.c_str(), |
| 164 | v.fsIn(), dklmdx.c_str()); |
| 165 | fragBuilder->codeAppendf("%s =" |
| 166 | "2.0 * %s.x * %s.x - %s.y * %s.z - %s.z * %s.y;", |
| 167 | dfdy.c_str(), |
| 168 | v.fsIn(), dklmdy.c_str(), |
| 169 | v.fsIn(), dklmdy.c_str(), |
| 170 | v.fsIn(), dklmdy.c_str()); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 171 | fragBuilder->codeAppendf("%s = float2(%s, %s);", gF.c_str(), dfdx.c_str(), |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 172 | dfdy.c_str()); |
robertphillips | 96afa52 | 2015-12-09 07:54:24 -0800 | [diff] [blame] | 173 | fragBuilder->codeAppendf("%s = sqrt(dot(%s, %s));", |
| 174 | gFM.c_str(), gF.c_str(), gF.c_str()); |
| 175 | fragBuilder->codeAppendf("%s = %s.x * %s.x - %s.y * %s.z;", |
| 176 | func.c_str(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn()); |
| 177 | fragBuilder->codeAppendf("%s = %s / %s;", |
| 178 | edgeAlpha.c_str(), func.c_str(), gFM.c_str()); |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 179 | fragBuilder->codeAppendf("%s = clamp(0.5 - %s, 0.0, 1.0);", |
robertphillips | 96afa52 | 2015-12-09 07:54:24 -0800 | [diff] [blame] | 180 | edgeAlpha.c_str(), edgeAlpha.c_str()); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 181 | // Add line below for smooth cubic ramp |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 182 | // fragBuilder->codeAppend("edgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);"); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 183 | break; |
| 184 | } |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame^] | 185 | case GrClipEdgeType::kFillBW: { |
robertphillips | 96afa52 | 2015-12-09 07:54:24 -0800 | [diff] [blame] | 186 | fragBuilder->codeAppendf("%s = %s.x * %s.x - %s.y * %s.z;", |
| 187 | edgeAlpha.c_str(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn()); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 188 | fragBuilder->codeAppendf("%s = float(%s < 0.0);", |
robertphillips | 96afa52 | 2015-12-09 07:54:24 -0800 | [diff] [blame] | 189 | edgeAlpha.c_str(), edgeAlpha.c_str()); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 190 | break; |
| 191 | } |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 192 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 193 | SK_ABORT("Shouldn't get here"); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 194 | } |
| 195 | |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 196 | // TODO should we really be doing this? |
| 197 | if (gp.coverageScale() != 0xff) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 198 | const char* coverageScale; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 199 | fCoverageScaleUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 200 | kFloat_GrSLType, |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 201 | "Coverage", |
| 202 | &coverageScale); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 203 | fragBuilder->codeAppendf("%s = half4(%s * %s);", |
robertphillips | 96afa52 | 2015-12-09 07:54:24 -0800 | [diff] [blame] | 204 | args.fOutputCoverage, coverageScale, edgeAlpha.c_str()); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 205 | } else { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 206 | fragBuilder->codeAppendf("%s = half4(%s);", args.fOutputCoverage, edgeAlpha.c_str()); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 207 | } |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 208 | } |
| 209 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 210 | void GrGLConicEffect::GenKey(const GrGeometryProcessor& gp, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 211 | const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 212 | GrProcessorKeyBuilder* b) { |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 213 | const GrConicEffect& ce = gp.cast<GrConicEffect>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 214 | uint32_t key = ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 215 | key |= 0xff != ce.coverageScale() ? 0x8 : 0x0; |
| 216 | key |= ce.usesLocalCoords() && ce.localMatrix().hasPerspective() ? 0x10 : 0x0; |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 217 | key |= ComputePosKey(ce.viewMatrix()) << 5; |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 218 | b->add32(key); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | ////////////////////////////////////////////////////////////////////////////// |
| 222 | |
| 223 | GrConicEffect::~GrConicEffect() {} |
| 224 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 225 | void GrConicEffect::getGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 226 | GrProcessorKeyBuilder* b) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 227 | GrGLConicEffect::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 230 | GrGLSLPrimitiveProcessor* GrConicEffect::createGLSLInstance(const GrShaderCaps&) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 231 | return new GrGLConicEffect(*this); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 232 | } |
| 233 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 234 | GrConicEffect::GrConicEffect(GrColor color, const SkMatrix& viewMatrix, uint8_t coverage, |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 235 | GrClipEdgeType edgeType, const SkMatrix& localMatrix, |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 236 | bool usesLocalCoords) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 237 | : INHERITED(kGrConicEffect_ClassID) |
| 238 | , fColor(color) |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 239 | , fViewMatrix(viewMatrix) |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 240 | , fLocalMatrix(viewMatrix) |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 241 | , fUsesLocalCoords(usesLocalCoords) |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 242 | , fCoverageScale(coverage) |
| 243 | , fEdgeType(edgeType) { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 244 | fInPosition = &this->addVertexAttrib("inPosition", kFloat2_GrVertexAttribType); |
| 245 | fInConicCoeffs = &this->addVertexAttrib("inConicCoeffs", kHalf4_GrVertexAttribType); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 246 | } |
| 247 | |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 248 | ////////////////////////////////////////////////////////////////////////////// |
| 249 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 250 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrConicEffect); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 251 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 252 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 253 | sk_sp<GrGeometryProcessor> GrConicEffect::TestCreate(GrProcessorTestData* d) { |
| 254 | sk_sp<GrGeometryProcessor> gp; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 255 | do { |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 256 | GrClipEdgeType edgeType = |
| 257 | static_cast<GrClipEdgeType>( |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame^] | 258 | d->fRandom->nextULessThan(kGrClipEdgeTypeCnt)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 259 | gp = GrConicEffect::Make(GrRandomColor(d->fRandom), GrTest::TestMatrix(d->fRandom), |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 260 | edgeType, *d->caps(), GrTest::TestMatrix(d->fRandom), |
Brian Salomon | 9ae32a2 | 2017-01-25 14:58:24 -0500 | [diff] [blame] | 261 | d->fRandom->nextBool()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 262 | } while (nullptr == gp); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 263 | return gp; |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 264 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 265 | #endif |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 266 | |
| 267 | ////////////////////////////////////////////////////////////////////////////// |
| 268 | // Quad |
| 269 | ////////////////////////////////////////////////////////////////////////////// |
| 270 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 271 | class GrGLQuadEffect : public GrGLSLGeometryProcessor { |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 272 | public: |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 273 | GrGLQuadEffect(const GrGeometryProcessor&); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 274 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 275 | void onEmitCode(EmitArgs&, GrGPArgs*) override; |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 276 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 277 | static inline void GenKey(const GrGeometryProcessor&, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 278 | const GrShaderCaps&, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 279 | GrProcessorKeyBuilder*); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 280 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 281 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc, |
| 282 | FPCoordTransformIter&& transformIter) override { |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 283 | const GrQuadEffect& qe = primProc.cast<GrQuadEffect>(); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 284 | |
| 285 | if (!qe.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(qe.viewMatrix())) { |
| 286 | fViewMatrix = qe.viewMatrix(); |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 287 | float viewMatrix[3 * 3]; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 288 | GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 289 | pdman.setMatrix3f(fViewMatrixUniform, viewMatrix); |
| 290 | } |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 291 | |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 292 | if (qe.color() != fColor) { |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 293 | float c[4]; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 294 | GrColorToRGBAFloat(qe.color(), c); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 295 | pdman.set4fv(fColorUniform, 1, c); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 296 | fColor = qe.color(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 297 | } |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 298 | |
| 299 | if (qe.coverageScale() != 0xff && qe.coverageScale() != fCoverageScale) { |
| 300 | pdman.set1f(fCoverageScaleUniform, GrNormalizeByteToFloat(qe.coverageScale())); |
| 301 | fCoverageScale = qe.coverageScale(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 302 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 303 | this->setTransformDataHelper(qe.localMatrix(), pdman, &transformIter); |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 304 | } |
| 305 | |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 306 | private: |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 307 | SkMatrix fViewMatrix; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 308 | GrColor fColor; |
| 309 | uint8_t fCoverageScale; |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 310 | GrClipEdgeType fEdgeType; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 311 | UniformHandle fColorUniform; |
| 312 | UniformHandle fCoverageScaleUniform; |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 313 | UniformHandle fViewMatrixUniform; |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 314 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 315 | typedef GrGLSLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 316 | }; |
skia.committer@gmail.com | 44a77c8 | 2013-08-23 07:01:29 +0000 | [diff] [blame] | 317 | |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 318 | GrGLQuadEffect::GrGLQuadEffect(const GrGeometryProcessor& processor) |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 319 | : fViewMatrix(SkMatrix::InvalidMatrix()), fColor(GrColor_ILLEGAL), fCoverageScale(0xff) { |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 320 | const GrQuadEffect& ce = processor.cast<GrQuadEffect>(); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 321 | fEdgeType = ce.getEdgeType(); |
| 322 | } |
| 323 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 324 | void GrGLQuadEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 325 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 326 | const GrQuadEffect& gp = args.fGP.cast<GrQuadEffect>(); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 327 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 328 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 329 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 330 | // emit attributes |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 331 | varyingHandler->emitAttributes(gp); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 332 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 333 | GrGLSLVertToFrag v(kHalf4_GrSLType); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 334 | varyingHandler->addVarying("HairQuadEdge", &v); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 335 | vertBuilder->codeAppendf("%s = %s;", v.vsOut(), gp.inHairQuadEdge()->fName); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 336 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 337 | GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 338 | // Setup pass through color |
Brian Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 339 | this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, &fColorUniform); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 340 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 341 | // Setup position |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 342 | this->writeOutputPosition(vertBuilder, |
| 343 | uniformHandler, |
| 344 | gpArgs, |
| 345 | gp.inPosition()->fName, |
| 346 | gp.viewMatrix(), |
| 347 | &fViewMatrixUniform); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 348 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 349 | // emit transforms with position |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 350 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 351 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 352 | uniformHandler, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 353 | gpArgs->fPositionVar, |
| 354 | gp.inPosition()->fName, |
| 355 | gp.localMatrix(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 356 | args.fFPCoordTransformHandler); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 357 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 358 | fragBuilder->codeAppendf("half edgeAlpha;"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 359 | |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 360 | switch (fEdgeType) { |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame^] | 361 | case GrClipEdgeType::kHairlineAA: { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 362 | fragBuilder->codeAppendf("half2 duvdx = dFdx(%s.xy);", v.fsIn()); |
| 363 | fragBuilder->codeAppendf("half2 duvdy = dFdy(%s.xy);", v.fsIn()); |
| 364 | fragBuilder->codeAppendf("half2 gF = half2(2.0 * %s.x * duvdx.x - duvdx.y," |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 365 | " 2.0 * %s.x * duvdy.x - duvdy.y);", |
| 366 | v.fsIn(), v.fsIn()); |
| 367 | fragBuilder->codeAppendf("edgeAlpha = (%s.x * %s.x - %s.y);", |
| 368 | v.fsIn(), v.fsIn(), v.fsIn()); |
| 369 | fragBuilder->codeAppend("edgeAlpha = sqrt(edgeAlpha * edgeAlpha / dot(gF, gF));"); |
| 370 | fragBuilder->codeAppend("edgeAlpha = max(1.0 - edgeAlpha, 0.0);"); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 371 | // Add line below for smooth cubic ramp |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 372 | // fragBuilder->codeAppend("edgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);"); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 373 | break; |
| 374 | } |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame^] | 375 | case GrClipEdgeType::kFillAA: { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 376 | fragBuilder->codeAppendf("half2 duvdx = dFdx(%s.xy);", v.fsIn()); |
| 377 | fragBuilder->codeAppendf("half2 duvdy = dFdy(%s.xy);", v.fsIn()); |
| 378 | fragBuilder->codeAppendf("half2 gF = half2(2.0 * %s.x * duvdx.x - duvdx.y," |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 379 | " 2.0 * %s.x * duvdy.x - duvdy.y);", |
| 380 | v.fsIn(), v.fsIn()); |
| 381 | fragBuilder->codeAppendf("edgeAlpha = (%s.x * %s.x - %s.y);", |
| 382 | v.fsIn(), v.fsIn(), v.fsIn()); |
| 383 | fragBuilder->codeAppend("edgeAlpha = edgeAlpha / sqrt(dot(gF, gF));"); |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 384 | fragBuilder->codeAppend("edgeAlpha = clamp(0.5 - edgeAlpha, 0.0, 1.0);"); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 385 | // Add line below for smooth cubic ramp |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 386 | // fragBuilder->codeAppend("edgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);"); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 387 | break; |
| 388 | } |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame^] | 389 | case GrClipEdgeType::kFillBW: { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 390 | fragBuilder->codeAppendf("edgeAlpha = (%s.x * %s.x - %s.y);", |
| 391 | v.fsIn(), v.fsIn(), v.fsIn()); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 392 | fragBuilder->codeAppend("edgeAlpha = half(edgeAlpha < 0.0);"); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 393 | break; |
| 394 | } |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 395 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 396 | SK_ABORT("Shouldn't get here"); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 397 | } |
| 398 | |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 399 | if (0xff != gp.coverageScale()) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 400 | const char* coverageScale; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 401 | fCoverageScaleUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 402 | kHalf_GrSLType, |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 403 | "Coverage", |
| 404 | &coverageScale); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 405 | fragBuilder->codeAppendf("%s = half4(%s * edgeAlpha);", args.fOutputCoverage, |
| 406 | coverageScale); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 407 | } else { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 408 | fragBuilder->codeAppendf("%s = half4(edgeAlpha);", args.fOutputCoverage); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 409 | } |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 410 | } |
| 411 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 412 | void GrGLQuadEffect::GenKey(const GrGeometryProcessor& gp, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 413 | const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 414 | GrProcessorKeyBuilder* b) { |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 415 | const GrQuadEffect& ce = gp.cast<GrQuadEffect>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 416 | uint32_t key = ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 417 | key |= ce.coverageScale() != 0xff ? 0x8 : 0x0; |
| 418 | key |= ce.usesLocalCoords() && ce.localMatrix().hasPerspective() ? 0x10 : 0x0; |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 419 | key |= ComputePosKey(ce.viewMatrix()) << 5; |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 420 | b->add32(key); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | ////////////////////////////////////////////////////////////////////////////// |
| 424 | |
| 425 | GrQuadEffect::~GrQuadEffect() {} |
| 426 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 427 | void GrQuadEffect::getGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 428 | GrProcessorKeyBuilder* b) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 429 | GrGLQuadEffect::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 430 | } |
| 431 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 432 | GrGLSLPrimitiveProcessor* GrQuadEffect::createGLSLInstance(const GrShaderCaps&) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 433 | return new GrGLQuadEffect(*this); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 434 | } |
| 435 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 436 | GrQuadEffect::GrQuadEffect(GrColor color, const SkMatrix& viewMatrix, uint8_t coverage, |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 437 | GrClipEdgeType edgeType, const SkMatrix& localMatrix, |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 438 | bool usesLocalCoords) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 439 | : INHERITED(kGrQuadEffect_ClassID) |
| 440 | , fColor(color) |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 441 | , fViewMatrix(viewMatrix) |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 442 | , fLocalMatrix(localMatrix) |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 443 | , fUsesLocalCoords(usesLocalCoords) |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 444 | , fCoverageScale(coverage) |
| 445 | , fEdgeType(edgeType) { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 446 | fInPosition = &this->addVertexAttrib("inPosition", kFloat2_GrVertexAttribType); |
| 447 | fInHairQuadEdge = &this->addVertexAttrib("inHairQuadEdge", kHalf4_GrVertexAttribType); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 448 | } |
| 449 | |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 450 | ////////////////////////////////////////////////////////////////////////////// |
| 451 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 452 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrQuadEffect); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 453 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 454 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 455 | sk_sp<GrGeometryProcessor> GrQuadEffect::TestCreate(GrProcessorTestData* d) { |
| 456 | sk_sp<GrGeometryProcessor> gp; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 457 | do { |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 458 | GrClipEdgeType edgeType = static_cast<GrClipEdgeType>( |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame^] | 459 | d->fRandom->nextULessThan(kGrClipEdgeTypeCnt)); |
Brian Salomon | 9ae32a2 | 2017-01-25 14:58:24 -0500 | [diff] [blame] | 460 | gp = GrQuadEffect::Make(GrRandomColor(d->fRandom), GrTest::TestMatrix(d->fRandom), edgeType, |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 461 | *d->caps(), GrTest::TestMatrix(d->fRandom), |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 462 | d->fRandom->nextBool()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 463 | } while (nullptr == gp); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 464 | return gp; |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 465 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 466 | #endif |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 467 | |
| 468 | ////////////////////////////////////////////////////////////////////////////// |
| 469 | // Cubic |
| 470 | ////////////////////////////////////////////////////////////////////////////// |
| 471 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 472 | class GrGLCubicEffect : public GrGLSLGeometryProcessor { |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 473 | public: |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 474 | GrGLCubicEffect(const GrGeometryProcessor&); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 475 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 476 | void onEmitCode(EmitArgs&, GrGPArgs*) override; |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 477 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 478 | static inline void GenKey(const GrGeometryProcessor&, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 479 | const GrShaderCaps&, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 480 | GrProcessorKeyBuilder*); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 481 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 482 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc, |
| 483 | FPCoordTransformIter&& transformIter) override { |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 484 | const GrCubicEffect& ce = primProc.cast<GrCubicEffect>(); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 485 | |
| 486 | if (!ce.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(ce.viewMatrix())) { |
| 487 | fViewMatrix = ce.viewMatrix(); |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 488 | float viewMatrix[3 * 3]; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 489 | GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 490 | pdman.setMatrix3f(fViewMatrixUniform, viewMatrix); |
| 491 | } |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 492 | |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 493 | if (!fDevKLMMatrix.cheapEqualTo(ce.devKLMMatrix())) { |
| 494 | fDevKLMMatrix = ce.devKLMMatrix(); |
| 495 | float devKLMMatrix[3 * 3]; |
| 496 | GrGLSLGetMatrix<3>(devKLMMatrix, fDevKLMMatrix); |
| 497 | pdman.setMatrix3f(fDevKLMUniform, devKLMMatrix); |
| 498 | } |
| 499 | |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 500 | if (ce.color() != fColor) { |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 501 | float c[4]; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 502 | GrColorToRGBAFloat(ce.color(), c); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 503 | pdman.set4fv(fColorUniform, 1, c); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 504 | fColor = ce.color(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 505 | } |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 506 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 507 | this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 508 | } |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 509 | |
| 510 | private: |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 511 | SkMatrix fViewMatrix; |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 512 | SkMatrix fDevKLMMatrix; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 513 | GrColor fColor; |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 514 | GrClipEdgeType fEdgeType; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 515 | UniformHandle fColorUniform; |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 516 | UniformHandle fViewMatrixUniform; |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 517 | UniformHandle fDevKLMUniform; |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 518 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 519 | typedef GrGLSLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 520 | }; |
skia.committer@gmail.com | 44a77c8 | 2013-08-23 07:01:29 +0000 | [diff] [blame] | 521 | |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 522 | GrGLCubicEffect::GrGLCubicEffect(const GrGeometryProcessor& processor) |
Robert Phillips | 888e9f2 | 2017-06-12 07:50:05 -0400 | [diff] [blame] | 523 | : fViewMatrix(SkMatrix::InvalidMatrix()) |
| 524 | , fDevKLMMatrix(SkMatrix::InvalidMatrix()) |
| 525 | , fColor(GrColor_ILLEGAL) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 526 | const GrCubicEffect& ce = processor.cast<GrCubicEffect>(); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 527 | fEdgeType = ce.getEdgeType(); |
| 528 | } |
| 529 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 530 | void GrGLCubicEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 531 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 532 | const GrCubicEffect& gp = args.fGP.cast<GrCubicEffect>(); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 533 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 534 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 535 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 536 | // emit attributes |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 537 | varyingHandler->emitAttributes(gp); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 538 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 539 | GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 540 | // Setup pass through color |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 541 | if (!gp.colorIgnored()) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 542 | this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, &fColorUniform); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 543 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 544 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 545 | // Setup position |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 546 | this->writeOutputPosition(vertBuilder, |
| 547 | uniformHandler, |
| 548 | gpArgs, |
| 549 | gp.inPosition()->fName, |
| 550 | gp.viewMatrix(), |
| 551 | &fViewMatrixUniform); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 552 | |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 553 | // Setup KLM |
| 554 | const char* devkLMMatrixName; |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 555 | fDevKLMUniform = uniformHandler->addUniform(kVertex_GrShaderFlag, kFloat3x3_GrSLType, "KLM", |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 556 | &devkLMMatrixName); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 557 | GrGLSLVertToFrag v(kFloat3_GrSLType); |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 558 | varyingHandler->addVarying("CubicCoeffs", &v); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 559 | vertBuilder->codeAppendf("%s = %s * float3(%s, 1);", |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 560 | v.vsOut(), devkLMMatrixName, gpArgs->fPositionVar.c_str()); |
| 561 | |
| 562 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 563 | GrGLSLVertToFrag gradCoeffs(kFloat4_GrSLType); |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame^] | 564 | if (GrClipEdgeType::kFillAA == fEdgeType || GrClipEdgeType::kHairlineAA == fEdgeType) { |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 565 | varyingHandler->addVarying("GradCoeffs", &gradCoeffs); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 566 | vertBuilder->codeAppendf("float k = %s[0], l = %s[1], m = %s[2];", |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 567 | v.vsOut(), v.vsOut(), v.vsOut()); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 568 | vertBuilder->codeAppendf("float2 gk = float2(%s[0][0], %s[1][0]), " |
| 569 | "gl = float2(%s[0][1], %s[1][1]), " |
| 570 | "gm = float2(%s[0][2], %s[1][2]);", |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 571 | devkLMMatrixName, devkLMMatrixName, devkLMMatrixName, |
| 572 | devkLMMatrixName, devkLMMatrixName, devkLMMatrixName); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 573 | vertBuilder->codeAppendf("%s = float4(3 * k * gk, -m * gl - l * gm);", |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 574 | gradCoeffs.vsOut()); |
| 575 | } |
| 576 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 577 | // emit transforms with position |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 578 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 579 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 580 | uniformHandler, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 581 | gpArgs->fPositionVar, |
| 582 | gp.inPosition()->fName, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 583 | args.fFPCoordTransformHandler); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 584 | |
egdaniel | 9bd5bbf | 2014-08-29 11:12:30 -0700 | [diff] [blame] | 585 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 586 | GrShaderVar edgeAlpha("edgeAlpha", kFloat_GrSLType, 0); |
| 587 | GrShaderVar gF("gF", kFloat2_GrSLType, 0); |
| 588 | GrShaderVar func("func", kFloat_GrSLType, 0); |
egdaniel | 9bd5bbf | 2014-08-29 11:12:30 -0700 | [diff] [blame] | 589 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 590 | fragBuilder->declAppend(edgeAlpha); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 591 | fragBuilder->declAppend(gF); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 592 | fragBuilder->declAppend(func); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 593 | |
| 594 | switch (fEdgeType) { |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame^] | 595 | case GrClipEdgeType::kHairlineAA: { |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 596 | fragBuilder->codeAppendf("%s = %s.x * %s.xy + %s.zw;", |
| 597 | gF.c_str(), v.fsIn(), gradCoeffs.fsIn(), gradCoeffs.fsIn()); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 598 | fragBuilder->codeAppendf("%s = %s.x * %s.x * %s.x - %s.y * %s.z;", |
| 599 | func.c_str(), v.fsIn(), v.fsIn(), |
| 600 | v.fsIn(), v.fsIn(), v.fsIn()); |
| 601 | fragBuilder->codeAppendf("%s = abs(%s);", func.c_str(), func.c_str()); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 602 | fragBuilder->codeAppendf("%s = %s * inversesqrt(dot(%s, %s));", |
| 603 | edgeAlpha.c_str(), func.c_str(), gF.c_str(), gF.c_str()); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 604 | fragBuilder->codeAppendf("%s = max(1.0 - %s, 0.0);", |
| 605 | edgeAlpha.c_str(), edgeAlpha.c_str()); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 606 | // Add line below for smooth cubic ramp |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 607 | // fragBuilder->codeAppendf("%s = %s * %s * (3.0 - 2.0 * %s);", |
egdaniel | 9bd5bbf | 2014-08-29 11:12:30 -0700 | [diff] [blame] | 608 | // edgeAlpha.c_str(), edgeAlpha.c_str(), edgeAlpha.c_str(), |
| 609 | // edgeAlpha.c_str()); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 610 | break; |
| 611 | } |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame^] | 612 | case GrClipEdgeType::kFillAA: { |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 613 | fragBuilder->codeAppendf("%s = %s.x * %s.xy + %s.zw;", |
| 614 | gF.c_str(), v.fsIn(), gradCoeffs.fsIn(), gradCoeffs.fsIn()); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 615 | fragBuilder->codeAppendf("%s = %s.x * %s.x * %s.x - %s.y * %s.z;", |
robertphillips | 96afa52 | 2015-12-09 07:54:24 -0800 | [diff] [blame] | 616 | func.c_str(), |
| 617 | v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn(), v.fsIn()); |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 618 | fragBuilder->codeAppendf("%s = %s * inversesqrt(dot(%s, %s));", |
| 619 | edgeAlpha.c_str(), func.c_str(), gF.c_str(), gF.c_str()); |
Greg Daniel | 8199d94 | 2017-03-14 10:20:24 -0400 | [diff] [blame] | 620 | fragBuilder->codeAppendf("%s = clamp(0.5 - %s, 0.0, 1.0);", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 621 | edgeAlpha.c_str(), edgeAlpha.c_str()); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 622 | // Add line below for smooth cubic ramp |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 623 | // fragBuilder->codeAppendf("%s = %s * %s * (3.0 - 2.0 * %s);", |
egdaniel | 9bd5bbf | 2014-08-29 11:12:30 -0700 | [diff] [blame] | 624 | // edgeAlpha.c_str(), edgeAlpha.c_str(), edgeAlpha.c_str(), |
| 625 | // edgeAlpha.c_str()); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 626 | break; |
| 627 | } |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame^] | 628 | case GrClipEdgeType::kFillBW: { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 629 | fragBuilder->codeAppendf("%s = %s.x * %s.x * %s.x - %s.y * %s.z;", |
| 630 | edgeAlpha.c_str(), v.fsIn(), v.fsIn(), |
| 631 | v.fsIn(), v.fsIn(), v.fsIn()); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 632 | fragBuilder->codeAppendf("%s = half(%s < 0.0);", edgeAlpha.c_str(), edgeAlpha.c_str()); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 633 | break; |
| 634 | } |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 635 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 636 | SK_ABORT("Shouldn't get here"); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 637 | } |
| 638 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 639 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 640 | fragBuilder->codeAppendf("%s = float4(%s);", args.fOutputCoverage, edgeAlpha.c_str()); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 641 | } |
| 642 | |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 643 | void GrGLCubicEffect::GenKey(const GrGeometryProcessor& gp, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 644 | const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 645 | GrProcessorKeyBuilder* b) { |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 646 | const GrCubicEffect& ce = gp.cast<GrCubicEffect>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 647 | uint32_t key = ce.isAntiAliased() ? (ce.isFilled() ? 0x0 : 0x1) : 0x2; |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 648 | key |= ComputePosKey(ce.viewMatrix()) << 5; |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 649 | b->add32(key); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | ////////////////////////////////////////////////////////////////////////////// |
| 653 | |
| 654 | GrCubicEffect::~GrCubicEffect() {} |
| 655 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 656 | void GrCubicEffect::getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 657 | GrGLCubicEffect::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 658 | } |
| 659 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 660 | GrGLSLPrimitiveProcessor* GrCubicEffect::createGLSLInstance(const GrShaderCaps&) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 661 | return new GrGLCubicEffect(*this); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 664 | GrCubicEffect::GrCubicEffect(GrColor color, const SkMatrix& viewMatrix, const SkMatrix& |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 665 | devKLMMatrix, GrClipEdgeType edgeType) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 666 | : INHERITED(kGrCubicEffect_ClassID) |
| 667 | , fColor(color) |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 668 | , fViewMatrix(viewMatrix) |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 669 | , fDevKLMMatrix(devKLMMatrix) |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 670 | , fEdgeType(edgeType) { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 671 | fInPosition = &this->addVertexAttrib("inPosition", kFloat2_GrVertexAttribType); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 672 | } |
| 673 | |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 674 | ////////////////////////////////////////////////////////////////////////////// |
| 675 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 676 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrCubicEffect); |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 677 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 678 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 679 | sk_sp<GrGeometryProcessor> GrCubicEffect::TestCreate(GrProcessorTestData* d) { |
| 680 | sk_sp<GrGeometryProcessor> gp; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 681 | do { |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 682 | GrClipEdgeType edgeType = |
| 683 | static_cast<GrClipEdgeType>( |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame^] | 684 | d->fRandom->nextULessThan(kGrClipEdgeTypeCnt)); |
Brian Salomon | 9ae32a2 | 2017-01-25 14:58:24 -0500 | [diff] [blame] | 685 | gp = GrCubicEffect::Make(GrRandomColor(d->fRandom), GrTest::TestMatrix(d->fRandom), |
Chris Dalton | febbffa | 2017-06-08 13:12:02 -0600 | [diff] [blame] | 686 | GrTest::TestMatrix(d->fRandom), d->fRandom->nextBool(), edgeType, |
| 687 | *d->caps()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 688 | } while (nullptr == gp); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 689 | return gp; |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 690 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 691 | #endif |