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