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