joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 "GrDefaultGeoProcFactory.h" |
| 9 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 10 | #include "GrInvariantOutput.h" |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 11 | #include "SkRefCnt.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 12 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLGeometryProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLVertexShaderBuilder.h" |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLVarying.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 16 | #include "glsl/GrGLSLUniformHandler.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 17 | #include "glsl/GrGLSLUtil.h" |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * The default Geometry Processor simply takes position and multiplies it by the uniform view |
| 21 | * matrix. It also leaves coverage untouched. Behind the scenes, we may add per vertex color or |
| 22 | * local coords. |
| 23 | */ |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 24 | |
| 25 | enum GPFlag { |
| 26 | kColor_GPFlag = 0x1, |
| 27 | kLocalCoord_GPFlag = 0x2, |
| 28 | kCoverage_GPFlag= 0x4, |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 31 | class DefaultGeoProc : public GrGeometryProcessor { |
| 32 | public: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 33 | static sk_sp<GrGeometryProcessor> Make(uint32_t gpTypeFlags, |
| 34 | GrColor color, |
| 35 | const SkMatrix& viewMatrix, |
| 36 | const SkMatrix& localMatrix, |
| 37 | bool localCoordsWillBeRead, |
| 38 | bool coverageWillBeIgnored, |
| 39 | uint8_t coverage) { |
| 40 | return sk_sp<GrGeometryProcessor>(new DefaultGeoProc( |
| 41 | gpTypeFlags, color, viewMatrix, localMatrix, coverage, |
| 42 | localCoordsWillBeRead, coverageWillBeIgnored)); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 43 | } |
| 44 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 45 | const char* name() const override { return "DefaultGeometryProcessor"; } |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 46 | |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 47 | const Attribute* inPosition() const { return fInPosition; } |
| 48 | const Attribute* inColor() const { return fInColor; } |
| 49 | const Attribute* inLocalCoords() const { return fInLocalCoords; } |
| 50 | const Attribute* inCoverage() const { return fInCoverage; } |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 51 | GrColor color() const { return fColor; } |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 52 | bool colorIgnored() const { return GrColor_ILLEGAL == fColor; } |
| 53 | bool hasVertexColor() const { return SkToBool(fInColor); } |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 54 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 55 | const SkMatrix& localMatrix() const { return fLocalMatrix; } |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 56 | bool localCoordsWillBeRead() const { return fLocalCoordsWillBeRead; } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 57 | uint8_t coverage() const { return fCoverage; } |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 58 | bool coverageWillBeIgnored() const { return fCoverageWillBeIgnored; } |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 59 | bool hasVertexCoverage() const { return SkToBool(fInCoverage); } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 60 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 61 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 62 | public: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 63 | GLSLProcessor() |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 64 | : fViewMatrix(SkMatrix::InvalidMatrix()), fColor(GrColor_ILLEGAL), fCoverage(0xff) {} |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 65 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 66 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 67 | const DefaultGeoProc& gp = args.fGP.cast<DefaultGeoProc>(); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 68 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 69 | GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 70 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 71 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 72 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 73 | // emit attributes |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 74 | varyingHandler->emitAttributes(gp); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 75 | |
| 76 | // Setup pass through color |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 77 | if (!gp.colorIgnored()) { |
| 78 | if (gp.hasVertexColor()) { |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 79 | varyingHandler->addPassThroughAttribute(gp.inColor(), args.fOutputColor); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 80 | } else { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 81 | this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, |
| 82 | &fColorUniform); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 86 | // Setup position |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 87 | this->setupPosition(vertBuilder, |
| 88 | uniformHandler, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 89 | gpArgs, |
| 90 | gp.inPosition()->fName, |
| 91 | gp.viewMatrix(), |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 92 | &fViewMatrixUniform); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 93 | |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 94 | if (gp.hasExplicitLocalCoords()) { |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 95 | // emit transforms with explicit local coords |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 96 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 97 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 98 | uniformHandler, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 99 | gpArgs->fPositionVar, |
| 100 | gp.inLocalCoords()->fName, |
| 101 | gp.localMatrix(), |
bsalomon | 56b7dc4 | 2016-09-19 09:26:40 -0700 | [diff] [blame] | 102 | args.fTransformsIn, |
| 103 | args.fTransformsOut); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 104 | } else { |
| 105 | // emit transforms with position |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 106 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 107 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 108 | uniformHandler, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 109 | gpArgs->fPositionVar, |
| 110 | gp.inPosition()->fName, |
| 111 | gp.localMatrix(), |
bsalomon | 56b7dc4 | 2016-09-19 09:26:40 -0700 | [diff] [blame] | 112 | args.fTransformsIn, |
| 113 | args.fTransformsOut); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 114 | } |
| 115 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 116 | // Setup coverage as pass through |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 117 | if (!gp.coverageWillBeIgnored()) { |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 118 | if (gp.hasVertexCoverage()) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 119 | fragBuilder->codeAppendf("float alpha = 1.0;"); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 120 | varyingHandler->addPassThroughAttribute(gp.inCoverage(), "alpha"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 121 | fragBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 122 | } else if (gp.coverage() == 0xff) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 123 | fragBuilder->codeAppendf("%s = vec4(1);", args.fOutputCoverage); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 124 | } else { |
| 125 | const char* fragCoverage; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 126 | fCoverageUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| 127 | kFloat_GrSLType, |
| 128 | kDefault_GrSLPrecision, |
| 129 | "Coverage", |
| 130 | &fragCoverage); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 131 | fragBuilder->codeAppendf("%s = vec4(%s);", args.fOutputCoverage, fragCoverage); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 132 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 133 | } |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 134 | } |
| 135 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 136 | static inline void GenKey(const GrGeometryProcessor& gp, |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 137 | const GrGLSLCaps&, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 138 | GrProcessorKeyBuilder* b) { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 139 | const DefaultGeoProc& def = gp.cast<DefaultGeoProc>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 140 | uint32_t key = def.fFlags; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 141 | key |= def.colorIgnored() << 8; |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 142 | key |= def.coverageWillBeIgnored() << 9; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 143 | key |= def.hasVertexColor() << 10; |
| 144 | key |= def.hasVertexCoverage() << 11; |
| 145 | key |= def.coverage() == 0xff ? 0x1 << 12 : 0; |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 146 | key |= def.localCoordsWillBeRead() && def.localMatrix().hasPerspective() ? 0x1 << 24 : |
| 147 | 0x0; |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 148 | key |= ComputePosKey(def.viewMatrix()) << 25; |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 149 | b->add32(key); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 150 | } |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 151 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 152 | void setData(const GrGLSLProgramDataManager& pdman, |
bsalomon | 56b7dc4 | 2016-09-19 09:26:40 -0700 | [diff] [blame] | 153 | const GrPrimitiveProcessor& gp) override { |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 154 | const DefaultGeoProc& dgp = gp.cast<DefaultGeoProc>(); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 155 | |
| 156 | if (!dgp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dgp.viewMatrix())) { |
| 157 | fViewMatrix = dgp.viewMatrix(); |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 158 | float viewMatrix[3 * 3]; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 159 | GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 160 | pdman.setMatrix3f(fViewMatrixUniform, viewMatrix); |
| 161 | } |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 162 | |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 163 | if (dgp.color() != fColor && !dgp.hasVertexColor()) { |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 164 | float c[4]; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 165 | GrColorToRGBAFloat(dgp.color(), c); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 166 | pdman.set4fv(fColorUniform, 1, c); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 167 | fColor = dgp.color(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 168 | } |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 169 | |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 170 | if (!dgp.coverageWillBeIgnored() && |
| 171 | dgp.coverage() != fCoverage && !dgp.hasVertexCoverage()) { |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 172 | pdman.set1f(fCoverageUniform, GrNormalizeByteToFloat(dgp.coverage())); |
| 173 | fCoverage = dgp.coverage(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 174 | } |
bsalomon | 56b7dc4 | 2016-09-19 09:26:40 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void setTransformData(const GrPrimitiveProcessor& primProc, |
| 178 | const GrGLSLProgramDataManager& pdman, |
| 179 | int index, |
| 180 | const SkTArray<const GrCoordTransform*, true>& transforms) override { |
| 181 | this->setTransformDataHelper(primProc.cast<DefaultGeoProc>().fLocalMatrix, pdman, index, |
| 182 | transforms); |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 183 | } |
| 184 | |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 185 | private: |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 186 | SkMatrix fViewMatrix; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 187 | GrColor fColor; |
| 188 | uint8_t fCoverage; |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 189 | UniformHandle fViewMatrixUniform; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 190 | UniformHandle fColorUniform; |
| 191 | UniformHandle fCoverageUniform; |
| 192 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 193 | typedef GrGLSLGeometryProcessor INHERITED; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 194 | }; |
| 195 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 196 | void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { |
| 197 | GLSLProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 198 | } |
| 199 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 200 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override { |
| 201 | return new GLSLProcessor(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 202 | } |
| 203 | |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 204 | private: |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 205 | DefaultGeoProc(uint32_t gpTypeFlags, |
| 206 | GrColor color, |
| 207 | const SkMatrix& viewMatrix, |
| 208 | const SkMatrix& localMatrix, |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 209 | uint8_t coverage, |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 210 | bool localCoordsWillBeRead, |
| 211 | bool coverageWillBeIgnored) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 212 | : fInPosition(nullptr) |
| 213 | , fInColor(nullptr) |
| 214 | , fInLocalCoords(nullptr) |
| 215 | , fInCoverage(nullptr) |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 216 | , fColor(color) |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 217 | , fViewMatrix(viewMatrix) |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 218 | , fLocalMatrix(localMatrix) |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 219 | , fCoverage(coverage) |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 220 | , fFlags(gpTypeFlags) |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 221 | , fLocalCoordsWillBeRead(localCoordsWillBeRead) |
| 222 | , fCoverageWillBeIgnored(coverageWillBeIgnored) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 223 | this->initClassID<DefaultGeoProc>(); |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 224 | bool hasColor = SkToBool(gpTypeFlags & kColor_GPFlag); |
| 225 | bool hasExplicitLocalCoords = SkToBool(gpTypeFlags & kLocalCoord_GPFlag); |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 226 | bool hasCoverage = SkToBool(gpTypeFlags & kCoverage_GPFlag); |
bsalomon | 6cb807b | 2016-08-17 11:33:39 -0700 | [diff] [blame] | 227 | fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType, |
| 228 | kHigh_GrSLPrecision); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 229 | if (hasColor) { |
bsalomon | 6cb807b | 2016-08-17 11:33:39 -0700 | [diff] [blame] | 230 | fInColor = &this->addVertexAttrib("inColor", kVec4ub_GrVertexAttribType); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 231 | } |
bsalomon | cc6aeee | 2016-09-12 12:02:11 -0700 | [diff] [blame] | 232 | if (hasExplicitLocalCoords) { |
bsalomon | 6cb807b | 2016-08-17 11:33:39 -0700 | [diff] [blame] | 233 | fInLocalCoords = &this->addVertexAttrib("inLocalCoord", kVec2f_GrVertexAttribType); |
bsalomon | cc6aeee | 2016-09-12 12:02:11 -0700 | [diff] [blame] | 234 | this->setHasExplicitLocalCoords(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 235 | } |
| 236 | if (hasCoverage) { |
bsalomon | 6cb807b | 2016-08-17 11:33:39 -0700 | [diff] [blame] | 237 | fInCoverage = &this->addVertexAttrib("inCoverage", kFloat_GrVertexAttribType); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 238 | } |
| 239 | } |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 240 | |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 241 | const Attribute* fInPosition; |
| 242 | const Attribute* fInColor; |
| 243 | const Attribute* fInLocalCoords; |
| 244 | const Attribute* fInCoverage; |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 245 | GrColor fColor; |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 246 | SkMatrix fViewMatrix; |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 247 | SkMatrix fLocalMatrix; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 248 | uint8_t fCoverage; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 249 | uint32_t fFlags; |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 250 | bool fLocalCoordsWillBeRead; |
| 251 | bool fCoverageWillBeIgnored; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 252 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 253 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 254 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 255 | typedef GrGeometryProcessor INHERITED; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DefaultGeoProc); |
| 259 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 260 | sk_sp<GrGeometryProcessor> DefaultGeoProc::TestCreate(GrProcessorTestData* d) { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 261 | uint32_t flags = 0; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 262 | if (d->fRandom->nextBool()) { |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 263 | flags |= kColor_GPFlag; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 264 | } |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 265 | if (d->fRandom->nextBool()) { |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 266 | flags |= kCoverage_GPFlag; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 267 | } |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 268 | if (d->fRandom->nextBool()) { |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 269 | flags |= kLocalCoord_GPFlag; |
| 270 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 271 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 272 | return DefaultGeoProc::Make(flags, |
| 273 | GrRandomColor(d->fRandom), |
| 274 | GrTest::TestMatrix(d->fRandom), |
| 275 | GrTest::TestMatrix(d->fRandom), |
| 276 | d->fRandom->nextBool(), |
| 277 | d->fRandom->nextBool(), |
| 278 | GrRandomCoverage(d->fRandom)); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 279 | } |
| 280 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 281 | sk_sp<GrGeometryProcessor> GrDefaultGeoProcFactory::Make(const Color& color, |
| 282 | const Coverage& coverage, |
| 283 | const LocalCoords& localCoords, |
| 284 | const SkMatrix& viewMatrix) { |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 285 | uint32_t flags = 0; |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 286 | flags |= color.fType == Color::kAttribute_Type ? kColor_GPFlag : 0; |
| 287 | flags |= coverage.fType == Coverage::kAttribute_Type ? kCoverage_GPFlag : 0; |
| 288 | flags |= localCoords.fType == LocalCoords::kHasExplicit_Type ? kLocalCoord_GPFlag : 0; |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 289 | |
| 290 | uint8_t inCoverage = coverage.fCoverage; |
| 291 | bool coverageWillBeIgnored = coverage.fType == Coverage::kNone_Type; |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 292 | bool localCoordsWillBeRead = localCoords.fType != LocalCoords::kUnused_Type; |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 293 | |
| 294 | GrColor inColor = color.fColor; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 295 | return DefaultGeoProc::Make(flags, |
| 296 | inColor, |
| 297 | viewMatrix, |
| 298 | localCoords.fMatrix ? *localCoords.fMatrix : SkMatrix::I(), |
| 299 | localCoordsWillBeRead, |
| 300 | coverageWillBeIgnored, |
| 301 | inCoverage); |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 302 | } |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 303 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 304 | sk_sp<GrGeometryProcessor> GrDefaultGeoProcFactory::MakeForDeviceSpace( |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 305 | const Color& color, |
| 306 | const Coverage& coverage, |
| 307 | const LocalCoords& localCoords, |
| 308 | const SkMatrix& viewMatrix) { |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 309 | SkMatrix invert = SkMatrix::I(); |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 310 | if (LocalCoords::kUnused_Type != localCoords.fType) { |
| 311 | SkASSERT(LocalCoords::kUsePosition_Type == localCoords.fType); |
| 312 | if (!viewMatrix.isIdentity() && !viewMatrix.invert(&invert)) { |
| 313 | SkDebugf("Could not invert\n"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 314 | return nullptr; |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 315 | } |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 316 | |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 317 | if (localCoords.hasLocalMatrix()) { |
| 318 | invert.preConcat(*localCoords.fMatrix); |
| 319 | } |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | LocalCoords inverted(LocalCoords::kUsePosition_Type, &invert); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 323 | return Make(color, coverage, inverted, SkMatrix::I()); |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 324 | } |