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 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 10 | #include "SkRefCnt.h" |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 11 | #include "glsl/GrGLSLColorSpaceXformHelper.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" |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 14 | #include "glsl/GrGLSLVertexGeoBuilder.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 { |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 26 | kColorAttribute_GPFlag = 0x1, |
| 27 | kColorAttributeIsSkColor_GPFlag = 0x2, |
| 28 | kLocalCoordAttribute_GPFlag = 0x4, |
| 29 | kCoverageAttribute_GPFlag = 0x8, |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 30 | |
| 31 | kLinearizeColorAttribute_GPFlag = 0x10, |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 34 | class DefaultGeoProc : public GrGeometryProcessor { |
| 35 | public: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 36 | static sk_sp<GrGeometryProcessor> Make(uint32_t gpTypeFlags, |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 37 | GrColor color, |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 38 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 39 | const SkMatrix& viewMatrix, |
| 40 | const SkMatrix& localMatrix, |
| 41 | bool localCoordsWillBeRead, |
| 42 | uint8_t coverage) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 43 | return sk_sp<GrGeometryProcessor>(new DefaultGeoProc( |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 44 | gpTypeFlags, color, std::move(colorSpaceXform), viewMatrix, localMatrix, coverage, |
| 45 | localCoordsWillBeRead)); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 46 | } |
| 47 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 48 | const char* name() const override { return "DefaultGeometryProcessor"; } |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 49 | |
joshualitt | 71c9260 | 2015-01-14 08:12:47 -0800 | [diff] [blame] | 50 | const Attribute* inPosition() const { return fInPosition; } |
| 51 | const Attribute* inColor() const { return fInColor; } |
| 52 | const Attribute* inLocalCoords() const { return fInLocalCoords; } |
| 53 | const Attribute* inCoverage() const { return fInCoverage; } |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 54 | GrColor color() const { return fColor; } |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 55 | bool hasVertexColor() const { return SkToBool(fInColor); } |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 56 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 57 | const SkMatrix& localMatrix() const { return fLocalMatrix; } |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 58 | bool localCoordsWillBeRead() const { return fLocalCoordsWillBeRead; } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 59 | uint8_t coverage() const { return fCoverage; } |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 60 | bool hasVertexCoverage() const { return SkToBool(fInCoverage); } |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 61 | bool linearizeColor() const { |
| 62 | // Linearization should only happen with SkColor |
| 63 | bool linearize = SkToBool(fFlags & kLinearizeColorAttribute_GPFlag); |
| 64 | SkASSERT(!linearize || (fFlags & kColorAttributeIsSkColor_GPFlag)); |
| 65 | return linearize; |
| 66 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 67 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 68 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 69 | public: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 70 | GLSLProcessor() |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 71 | : fViewMatrix(SkMatrix::InvalidMatrix()), fColor(GrColor_ILLEGAL), fCoverage(0xff) {} |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 72 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 73 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 74 | const DefaultGeoProc& gp = args.fGP.cast<DefaultGeoProc>(); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 75 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 76 | GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 77 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 78 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 79 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 80 | // emit attributes |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 81 | varyingHandler->emitAttributes(gp); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 82 | |
| 83 | // Setup pass through color |
Brian Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 84 | if (gp.hasVertexColor()) { |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 85 | GrGLSLVarying varying(kHalf4_GrSLType); |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 86 | varyingHandler->addVarying("color", &varying); |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 87 | |
| 88 | // There are several optional steps to process the color. Start with the attribute: |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 89 | vertBuilder->codeAppendf("half4 color = %s;", gp.inColor()->fName); |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 90 | |
| 91 | // Linearize |
| 92 | if (gp.linearizeColor()) { |
| 93 | SkString srgbFuncName; |
| 94 | static const GrShaderVar gSrgbArgs[] = { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 95 | GrShaderVar("x", kHalf_GrSLType), |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 96 | }; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 97 | vertBuilder->emitFunction(kHalf_GrSLType, |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 98 | "srgb_to_linear", |
| 99 | SK_ARRAY_COUNT(gSrgbArgs), |
| 100 | gSrgbArgs, |
| 101 | "return (x <= 0.04045) ? (x / 12.92) " |
| 102 | ": pow((x + 0.055) / 1.055, 2.4);", |
| 103 | &srgbFuncName); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 104 | vertBuilder->codeAppendf("color = half4(%s(%s.r), %s(%s.g), %s(%s.b), %s.a);", |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 105 | srgbFuncName.c_str(), gp.inColor()->fName, |
| 106 | srgbFuncName.c_str(), gp.inColor()->fName, |
| 107 | srgbFuncName.c_str(), gp.inColor()->fName, |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 108 | gp.inColor()->fName); |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 109 | } |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 110 | |
| 111 | // For SkColor, do a red/blue swap and premul |
| 112 | if (gp.fFlags & kColorAttributeIsSkColor_GPFlag) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 113 | vertBuilder->codeAppend("color = half4(color.a * color.bgr, color.a);"); |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // Do color-correction to destination gamut |
| 117 | if (gp.linearizeColor()) { |
| 118 | fColorSpaceHelper.emitCode(uniformHandler, gp.fColorSpaceXform.get(), |
| 119 | kVertex_GrShaderFlag); |
| 120 | if (fColorSpaceHelper.isValid()) { |
| 121 | SkString xformedColor; |
| 122 | vertBuilder->appendColorGamutXform(&xformedColor, "color", |
| 123 | &fColorSpaceHelper); |
| 124 | vertBuilder->codeAppendf("color = %s;", xformedColor.c_str()); |
| 125 | } |
| 126 | } |
| 127 | vertBuilder->codeAppendf("%s = color;\n", varying.vsOut()); |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 128 | fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, varying.fsIn()); |
Brian Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 129 | } else { |
| 130 | this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, |
| 131 | &fColorUniform); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 132 | } |
| 133 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 134 | // Setup position |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 135 | this->writeOutputPosition(vertBuilder, |
| 136 | uniformHandler, |
| 137 | gpArgs, |
| 138 | gp.inPosition()->fName, |
| 139 | gp.viewMatrix(), |
| 140 | &fViewMatrixUniform); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 141 | |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 142 | if (gp.hasExplicitLocalCoords()) { |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 143 | // emit transforms with explicit local coords |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 144 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 145 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 146 | uniformHandler, |
Brian Salomon | 04460cc | 2017-12-06 14:47:42 -0500 | [diff] [blame] | 147 | gp.inLocalCoords()->asShaderVar(), |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 148 | gp.localMatrix(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 149 | args.fFPCoordTransformHandler); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 150 | } else { |
| 151 | // emit transforms with position |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 152 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 153 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 154 | uniformHandler, |
Brian Salomon | 04460cc | 2017-12-06 14:47:42 -0500 | [diff] [blame] | 155 | gp.inPosition()->asShaderVar(), |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 156 | gp.localMatrix(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 157 | args.fFPCoordTransformHandler); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 158 | } |
| 159 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 160 | // Setup coverage as pass through |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 161 | if (gp.hasVertexCoverage()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 162 | fragBuilder->codeAppendf("half alpha = 1.0;"); |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 163 | varyingHandler->addPassThroughAttribute(gp.inCoverage(), "alpha"); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 164 | fragBuilder->codeAppendf("%s = half4(alpha);", args.fOutputCoverage); |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 165 | } else if (gp.coverage() == 0xff) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 166 | fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 167 | } else { |
| 168 | const char* fragCoverage; |
| 169 | fCoverageUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 170 | kHalf_GrSLType, |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 171 | "Coverage", |
| 172 | &fragCoverage); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 173 | fragBuilder->codeAppendf("%s = half4(%s);", args.fOutputCoverage, fragCoverage); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 174 | } |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 175 | } |
| 176 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 177 | static inline void GenKey(const GrGeometryProcessor& gp, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 178 | const GrShaderCaps&, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 179 | GrProcessorKeyBuilder* b) { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 180 | const DefaultGeoProc& def = gp.cast<DefaultGeoProc>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 181 | uint32_t key = def.fFlags; |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 182 | key |= (def.coverage() == 0xff) ? 0x10 : 0; |
| 183 | key |= (def.localCoordsWillBeRead() && def.localMatrix().hasPerspective()) ? 0x20 : 0x0; |
| 184 | key |= ComputePosKey(def.viewMatrix()) << 20; |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 185 | b->add32(key); |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 186 | if (def.linearizeColor()) { |
| 187 | b->add32(GrColorSpaceXform::XformKey(def.fColorSpaceXform.get())); |
| 188 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 189 | } |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 190 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 191 | void setData(const GrGLSLProgramDataManager& pdman, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 192 | const GrPrimitiveProcessor& gp, |
| 193 | FPCoordTransformIter&& transformIter) override { |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 194 | const DefaultGeoProc& dgp = gp.cast<DefaultGeoProc>(); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 195 | |
| 196 | if (!dgp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dgp.viewMatrix())) { |
| 197 | fViewMatrix = dgp.viewMatrix(); |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 198 | float viewMatrix[3 * 3]; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 199 | GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 200 | pdman.setMatrix3f(fViewMatrixUniform, viewMatrix); |
| 201 | } |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 202 | |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 203 | if (dgp.color() != fColor && !dgp.hasVertexColor()) { |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 204 | float c[4]; |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 205 | GrColorToRGBAFloat(dgp.color(), c); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 206 | pdman.set4fv(fColorUniform, 1, c); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 207 | fColor = dgp.color(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 208 | } |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 209 | |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 210 | if (dgp.coverage() != fCoverage && !dgp.hasVertexCoverage()) { |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 211 | pdman.set1f(fCoverageUniform, GrNormalizeByteToFloat(dgp.coverage())); |
| 212 | fCoverage = dgp.coverage(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 213 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 214 | this->setTransformDataHelper(dgp.fLocalMatrix, pdman, &transformIter); |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 215 | |
| 216 | if (dgp.linearizeColor() && dgp.fColorSpaceXform) { |
| 217 | fColorSpaceHelper.setData(pdman, dgp.fColorSpaceXform.get()); |
| 218 | } |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 219 | } |
| 220 | |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 221 | private: |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 222 | SkMatrix fViewMatrix; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 223 | GrColor fColor; |
| 224 | uint8_t fCoverage; |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 225 | UniformHandle fViewMatrixUniform; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 226 | UniformHandle fColorUniform; |
| 227 | UniformHandle fCoverageUniform; |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 228 | GrGLSLColorSpaceXformHelper fColorSpaceHelper; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 229 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 230 | typedef GrGLSLGeometryProcessor INHERITED; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 231 | }; |
| 232 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 233 | void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override { |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 234 | GLSLProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 237 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override { |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 238 | return new GLSLProcessor(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 239 | } |
| 240 | |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 241 | private: |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 242 | DefaultGeoProc(uint32_t gpTypeFlags, |
| 243 | GrColor color, |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 244 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 245 | const SkMatrix& viewMatrix, |
| 246 | const SkMatrix& localMatrix, |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 247 | uint8_t coverage, |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 248 | bool localCoordsWillBeRead) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 249 | : INHERITED(kDefaultGeoProc_ClassID) |
| 250 | , fColor(color) |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 251 | , fViewMatrix(viewMatrix) |
| 252 | , fLocalMatrix(localMatrix) |
| 253 | , fCoverage(coverage) |
| 254 | , fFlags(gpTypeFlags) |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 255 | , fLocalCoordsWillBeRead(localCoordsWillBeRead) |
| 256 | , fColorSpaceXform(std::move(colorSpaceXform)) { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 257 | fInPosition = &this->addVertexAttrib("inPosition", kFloat2_GrVertexAttribType); |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 258 | if (fFlags & kColorAttribute_GPFlag) { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 259 | fInColor = &this->addVertexAttrib("inColor", kUByte4_norm_GrVertexAttribType); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 260 | } |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 261 | if (fFlags & kLocalCoordAttribute_GPFlag) { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 262 | fInLocalCoords = &this->addVertexAttrib("inLocalCoord", kFloat2_GrVertexAttribType); |
bsalomon | cc6aeee | 2016-09-12 12:02:11 -0700 | [diff] [blame] | 263 | this->setHasExplicitLocalCoords(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 264 | } |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 265 | if (fFlags & kCoverageAttribute_GPFlag) { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 266 | fInCoverage = &this->addVertexAttrib("inCoverage", kHalf_GrVertexAttribType); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 267 | } |
| 268 | } |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 269 | |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 270 | const Attribute* fInPosition = nullptr; |
| 271 | const Attribute* fInColor = nullptr; |
| 272 | const Attribute* fInLocalCoords = nullptr; |
| 273 | const Attribute* fInCoverage = nullptr; |
joshualitt | 88c23fc | 2015-05-13 14:18:07 -0700 | [diff] [blame] | 274 | GrColor fColor; |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 275 | SkMatrix fViewMatrix; |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 276 | SkMatrix fLocalMatrix; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 277 | uint8_t fCoverage; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 278 | uint32_t fFlags; |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 279 | bool fLocalCoordsWillBeRead; |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 280 | sk_sp<GrColorSpaceXform> fColorSpaceXform; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 281 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 282 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 283 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 284 | typedef GrGeometryProcessor INHERITED; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 285 | }; |
| 286 | |
| 287 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DefaultGeoProc); |
| 288 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 289 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 290 | sk_sp<GrGeometryProcessor> DefaultGeoProc::TestCreate(GrProcessorTestData* d) { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 291 | uint32_t flags = 0; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 292 | if (d->fRandom->nextBool()) { |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 293 | flags |= kColorAttribute_GPFlag; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 294 | } |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 295 | if (d->fRandom->nextBool()) { |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 296 | flags |= kColorAttributeIsSkColor_GPFlag; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 297 | } |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 298 | if (d->fRandom->nextBool()) { |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 299 | flags |= kCoverageAttribute_GPFlag; |
| 300 | } |
| 301 | if (d->fRandom->nextBool()) { |
| 302 | flags |= kLocalCoordAttribute_GPFlag; |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 303 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 304 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 305 | return DefaultGeoProc::Make(flags, |
| 306 | GrRandomColor(d->fRandom), |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 307 | GrTest::TestColorXform(d->fRandom), |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 308 | GrTest::TestMatrix(d->fRandom), |
| 309 | GrTest::TestMatrix(d->fRandom), |
| 310 | d->fRandom->nextBool(), |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 311 | GrRandomCoverage(d->fRandom)); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 312 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 313 | #endif |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 314 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 315 | sk_sp<GrGeometryProcessor> GrDefaultGeoProcFactory::Make(const Color& color, |
| 316 | const Coverage& coverage, |
| 317 | const LocalCoords& localCoords, |
| 318 | const SkMatrix& viewMatrix) { |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 319 | uint32_t flags = 0; |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 320 | if (Color::kPremulGrColorAttribute_Type == color.fType) { |
| 321 | flags |= kColorAttribute_GPFlag; |
| 322 | } else if (Color::kUnpremulSkColorAttribute_Type == color.fType) { |
| 323 | flags |= kColorAttribute_GPFlag | kColorAttributeIsSkColor_GPFlag; |
| 324 | } |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 325 | if (color.fLinearize) { |
| 326 | // It only makes sense to linearize SkColors (which are always sRGB). GrColor values should |
| 327 | // have been linearized and gamut-converted during paint conversion |
| 328 | SkASSERT(Color::kUnpremulSkColorAttribute_Type == color.fType); |
| 329 | flags |= kLinearizeColorAttribute_GPFlag; |
| 330 | } |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 331 | flags |= coverage.fType == Coverage::kAttribute_Type ? kCoverageAttribute_GPFlag : 0; |
| 332 | flags |= localCoords.fType == LocalCoords::kHasExplicit_Type ? kLocalCoordAttribute_GPFlag : 0; |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 333 | |
| 334 | uint8_t inCoverage = coverage.fCoverage; |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 335 | bool localCoordsWillBeRead = localCoords.fType != LocalCoords::kUnused_Type; |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 336 | |
| 337 | GrColor inColor = color.fColor; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 338 | return DefaultGeoProc::Make(flags, |
| 339 | inColor, |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 340 | color.fColorSpaceXform, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 341 | viewMatrix, |
| 342 | localCoords.fMatrix ? *localCoords.fMatrix : SkMatrix::I(), |
| 343 | localCoordsWillBeRead, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 344 | inCoverage); |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 345 | } |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 346 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 347 | sk_sp<GrGeometryProcessor> GrDefaultGeoProcFactory::MakeForDeviceSpace( |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 348 | const Color& color, |
| 349 | const Coverage& coverage, |
| 350 | const LocalCoords& localCoords, |
| 351 | const SkMatrix& viewMatrix) { |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 352 | SkMatrix invert = SkMatrix::I(); |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 353 | if (LocalCoords::kUnused_Type != localCoords.fType) { |
| 354 | SkASSERT(LocalCoords::kUsePosition_Type == localCoords.fType); |
| 355 | if (!viewMatrix.isIdentity() && !viewMatrix.invert(&invert)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 356 | return nullptr; |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 357 | } |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 358 | |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 359 | if (localCoords.hasLocalMatrix()) { |
| 360 | invert.preConcat(*localCoords.fMatrix); |
| 361 | } |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | LocalCoords inverted(LocalCoords::kUsePosition_Type, &invert); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 365 | return Make(color, coverage, inverted, SkMatrix::I()); |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 366 | } |