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