Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 8 | #include "GrCCPathProcessor.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 9 | |
Chris Dalton | d925f2d | 2018-05-07 19:19:06 -0600 | [diff] [blame] | 10 | #include "GrGpuCommandBuffer.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 11 | #include "GrOnFlushResourceProvider.h" |
| 12 | #include "GrTexture.h" |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 13 | #include "ccpr/GrCCPerFlushResources.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 14 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 15 | #include "glsl/GrGLSLGeometryProcessor.h" |
| 16 | #include "glsl/GrGLSLProgramBuilder.h" |
| 17 | #include "glsl/GrGLSLVarying.h" |
| 18 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 19 | // Paths are drawn as octagons. Each point on the octagon is the intersection of two lines: one edge |
| 20 | // from the path's bounding box and one edge from its 45-degree bounding box. The below inputs |
| 21 | // define a vertex by the two edges that need to be intersected. Normals point out of the octagon, |
| 22 | // and the bounding boxes are sent in as instance attribs. |
| 23 | static constexpr float kOctoEdgeNorms[8 * 4] = { |
| 24 | // bbox // bbox45 |
| 25 | -1, 0, -1,+1, |
| 26 | -1, 0, -1,-1, |
| 27 | 0,-1, -1,-1, |
| 28 | 0,-1, +1,-1, |
| 29 | +1, 0, +1,-1, |
| 30 | +1, 0, +1,+1, |
| 31 | 0,+1, +1,+1, |
| 32 | 0,+1, -1,+1, |
| 33 | }; |
| 34 | |
| 35 | GR_DECLARE_STATIC_UNIQUE_KEY(gVertexBufferKey); |
| 36 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 37 | sk_sp<const GrGpuBuffer> GrCCPathProcessor::FindVertexBuffer(GrOnFlushResourceProvider* onFlushRP) { |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 38 | GR_DEFINE_STATIC_UNIQUE_KEY(gVertexBufferKey); |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 39 | return onFlushRP->findOrMakeStaticBuffer(GrGpuBufferType::kVertex, sizeof(kOctoEdgeNorms), |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 40 | kOctoEdgeNorms, gVertexBufferKey); |
| 41 | } |
| 42 | |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 43 | static constexpr uint16_t kRestartStrip = 0xffff; |
| 44 | |
| 45 | static constexpr uint16_t kOctoIndicesAsStrips[] = { |
| 46 | 1, 0, 2, 4, 3, kRestartStrip, // First half. |
| 47 | 5, 4, 6, 0, 7 // Second half. |
| 48 | }; |
| 49 | |
| 50 | static constexpr uint16_t kOctoIndicesAsTris[] = { |
| 51 | // First half. |
| 52 | 1, 0, 2, |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 53 | 0, 4, 2, |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 54 | 2, 4, 3, |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 55 | |
| 56 | // Second half. |
| 57 | 5, 4, 6, |
| 58 | 4, 0, 6, |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 59 | 6, 0, 7, |
| 60 | }; |
| 61 | |
| 62 | GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
| 63 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 64 | constexpr GrPrimitiveProcessor::Attribute GrCCPathProcessor::kInstanceAttribs[]; |
| 65 | constexpr GrPrimitiveProcessor::Attribute GrCCPathProcessor::kEdgeNormsAttrib; |
| 66 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 67 | sk_sp<const GrGpuBuffer> GrCCPathProcessor::FindIndexBuffer(GrOnFlushResourceProvider* onFlushRP) { |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 68 | GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 69 | if (onFlushRP->caps()->usePrimitiveRestart()) { |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 70 | return onFlushRP->findOrMakeStaticBuffer(GrGpuBufferType::kIndex, |
| 71 | sizeof(kOctoIndicesAsStrips), kOctoIndicesAsStrips, |
| 72 | gIndexBufferKey); |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 73 | } else { |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 74 | return onFlushRP->findOrMakeStaticBuffer(GrGpuBufferType::kIndex, |
| 75 | sizeof(kOctoIndicesAsTris), kOctoIndicesAsTris, |
| 76 | gIndexBufferKey); |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 77 | } |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 80 | GrCCPathProcessor::GrCCPathProcessor(const GrTextureProxy* atlas, |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 81 | const SkMatrix& viewMatrixIfUsingLocalCoords) |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 82 | : INHERITED(kGrCCPathProcessor_ClassID) |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 83 | , fAtlasAccess(atlas->textureType(), atlas->config(), GrSamplerState::Filter::kNearest, |
Greg Daniel | 0f70be8 | 2018-10-08 17:35:08 +0000 | [diff] [blame] | 84 | GrSamplerState::WrapMode::kClamp) |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 85 | , fAtlasSize(atlas->isize()) |
| 86 | , fAtlasOrigin(atlas->origin()) { |
| 87 | // TODO: Can we just assert that atlas has GrCCAtlas::kTextureOrigin and remove fAtlasOrigin? |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 88 | this->setInstanceAttributes(kInstanceAttribs, kNumInstanceAttribs); |
| 89 | SkASSERT(this->instanceStride() == sizeof(Instance)); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 90 | |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 91 | this->setVertexAttributes(&kEdgeNormsAttrib, 1); |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 92 | this->setTextureSamplerCnt(1); |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 93 | |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 94 | if (!viewMatrixIfUsingLocalCoords.invert(&fLocalMatrix)) { |
| 95 | fLocalMatrix.setIdentity(); |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 96 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 97 | } |
| 98 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 99 | class GLSLPathProcessor : public GrGLSLGeometryProcessor { |
| 100 | public: |
| 101 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override; |
| 102 | |
| 103 | private: |
| 104 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc, |
| 105 | FPCoordTransformIter&& transformIter) override { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 106 | const GrCCPathProcessor& proc = primProc.cast<GrCCPathProcessor>(); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 107 | pdman.set2f(fAtlasAdjustUniform, 1.0f / proc.atlasSize().fWidth, |
| 108 | 1.0f / proc.atlasSize().fHeight); |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 109 | this->setTransformDataHelper(proc.localMatrix(), pdman, &transformIter); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform; |
| 113 | |
| 114 | typedef GrGLSLGeometryProcessor INHERITED; |
| 115 | }; |
| 116 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 117 | GrGLSLPrimitiveProcessor* GrCCPathProcessor::createGLSLInstance(const GrShaderCaps&) const { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 118 | return new GLSLPathProcessor(); |
| 119 | } |
| 120 | |
Chris Dalton | d925f2d | 2018-05-07 19:19:06 -0600 | [diff] [blame] | 121 | void GrCCPathProcessor::drawPaths(GrOpFlushState* flushState, const GrPipeline& pipeline, |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 122 | const GrPipeline::FixedDynamicState* fixedDynamicState, |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 123 | const GrCCPerFlushResources& resources, int baseInstance, |
| 124 | int endInstance, const SkRect& bounds) const { |
Chris Dalton | d925f2d | 2018-05-07 19:19:06 -0600 | [diff] [blame] | 125 | const GrCaps& caps = flushState->caps(); |
| 126 | GrPrimitiveType primitiveType = caps.usePrimitiveRestart() |
| 127 | ? GrPrimitiveType::kTriangleStrip |
| 128 | : GrPrimitiveType::kTriangles; |
| 129 | int numIndicesPerInstance = caps.usePrimitiveRestart() |
| 130 | ? SK_ARRAY_COUNT(kOctoIndicesAsStrips) |
| 131 | : SK_ARRAY_COUNT(kOctoIndicesAsTris); |
| 132 | GrMesh mesh(primitiveType); |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 133 | auto enablePrimitiveRestart = GrPrimitiveRestart(flushState->caps().usePrimitiveRestart()); |
| 134 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 135 | mesh.setIndexedInstanced(resources.refIndexBuffer(), numIndicesPerInstance, |
| 136 | resources.refInstanceBuffer(), endInstance - baseInstance, |
| 137 | baseInstance, enablePrimitiveRestart); |
| 138 | mesh.setVertexData(resources.refVertexBuffer()); |
Chris Dalton | d925f2d | 2018-05-07 19:19:06 -0600 | [diff] [blame] | 139 | |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 140 | flushState->rtCommandBuffer()->draw(*this, pipeline, fixedDynamicState, nullptr, &mesh, 1, |
| 141 | bounds); |
Chris Dalton | d925f2d | 2018-05-07 19:19:06 -0600 | [diff] [blame] | 142 | } |
| 143 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 144 | void GLSLPathProcessor::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 145 | using InstanceAttribs = GrCCPathProcessor::InstanceAttribs; |
Chris Dalton | 7b04631 | 2018-02-02 11:06:30 -0700 | [diff] [blame] | 146 | using Interpolation = GrGLSLVaryingHandler::Interpolation; |
| 147 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 148 | const GrCCPathProcessor& proc = args.fGP.cast<GrCCPathProcessor>(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 149 | GrGLSLUniformHandler* uniHandler = args.fUniformHandler; |
| 150 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 151 | |
| 152 | const char* atlasAdjust; |
| 153 | fAtlasAdjustUniform = uniHandler->addUniform( |
| 154 | kVertex_GrShaderFlag, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 155 | kFloat2_GrSLType, "atlas_adjust", &atlasAdjust); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 156 | |
| 157 | varyingHandler->emitAttributes(proc); |
| 158 | |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 159 | GrGLSLVarying texcoord(kFloat3_GrSLType); |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 160 | GrGLSLVarying color(kHalf4_GrSLType); |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 161 | varyingHandler->addVarying("texcoord", &texcoord); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 162 | varyingHandler->addPassThroughAttribute(proc.getInstanceAttrib(InstanceAttribs::kColor), |
Chris Dalton | 7b04631 | 2018-02-02 11:06:30 -0700 | [diff] [blame] | 163 | args.fOutputColor, Interpolation::kCanBeFlat); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 164 | |
Chris Dalton | d0b8d93 | 2017-12-21 16:48:52 -0700 | [diff] [blame] | 165 | // The vertex shader bloats and intersects the devBounds and devBounds45 rectangles, in order to |
| 166 | // find an octagon that circumscribes the (bloated) path. |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 167 | GrGLSLVertexBuilder* v = args.fVertBuilder; |
| 168 | |
Chris Dalton | d0b8d93 | 2017-12-21 16:48:52 -0700 | [diff] [blame] | 169 | // Each vertex is the intersection of one edge from devBounds and one from devBounds45. |
| 170 | // 'N' holds the normals to these edges as column vectors. |
| 171 | // |
| 172 | // NOTE: "float2x2(float4)" is valid and equivalent to "float2x2(float4.xy, float4.zw)", |
| 173 | // however Intel compilers crash when we use the former syntax in this shader. |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 174 | v->codeAppendf("float2x2 N = float2x2(%s.xy, %s.zw);", proc.getEdgeNormsAttrib().name(), |
| 175 | proc.getEdgeNormsAttrib().name()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 176 | |
| 177 | // N[0] is the normal for the edge we are intersecting from the regular bounding box, pointing |
| 178 | // out of the octagon. |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 179 | v->codeAppendf("float4 devbounds = %s;", |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 180 | proc.getInstanceAttrib(InstanceAttribs::kDevBounds).name()); |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 181 | v->codeAppend ("float2 refpt = (0 == sk_VertexID >> 2)" |
| 182 | "? float2(min(devbounds.x, devbounds.z), devbounds.y)" |
| 183 | ": float2(max(devbounds.x, devbounds.z), devbounds.w);"); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 184 | |
| 185 | // N[1] is the normal for the edge we are intersecting from the 45-degree bounding box, pointing |
| 186 | // out of the octagon. |
Chris Dalton | 5cd6700 | 2018-04-30 11:04:40 -0600 | [diff] [blame] | 187 | v->codeAppendf("float2 refpt45 = (0 == ((sk_VertexID + 1) & (1 << 2))) ? %s.xy : %s.zw;", |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 188 | proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).name(), |
| 189 | proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).name()); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 190 | v->codeAppendf("refpt45 *= float2x2(.5,.5,-.5,.5);"); // transform back to device space. |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 191 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 192 | v->codeAppend ("float2 K = float2(dot(N[0], refpt), dot(N[1], refpt45));"); |
| 193 | v->codeAppendf("float2 octocoord = K * inverse(N);"); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 194 | |
Chris Dalton | 51e8b7e | 2018-10-09 21:18:45 -0600 | [diff] [blame] | 195 | // Round the octagon out to ensure we rasterize every pixel the path might touch. (Positive |
| 196 | // bloatdir means we should take the "ceil" and negative means to take the "floor".) |
| 197 | // |
| 198 | // NOTE: If we were just drawing a rect, ceil/floor would be enough. But since there are also |
| 199 | // diagonals in the octagon that cross through pixel centers, we need to outset by another |
| 200 | // quarter px to ensure those pixels get rasterized. |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 201 | v->codeAppend ("half2 bloatdir = (0 != N[0].x) " |
| 202 | "? half2(half(N[0].x), half(N[1].y))" |
| 203 | ": half2(half(N[1].x), half(N[0].y));"); |
Chris Dalton | 51e8b7e | 2018-10-09 21:18:45 -0600 | [diff] [blame] | 204 | v->codeAppend ("octocoord = (ceil(octocoord * bloatdir - 1e-4) + 0.25) * bloatdir;"); |
| 205 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 206 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "octocoord"); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 207 | |
| 208 | // Convert to atlas coordinates in order to do our texture lookup. |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 209 | v->codeAppendf("float2 atlascoord = octocoord + float2(%s);", |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 210 | proc.getInstanceAttrib(InstanceAttribs::kDevToAtlasOffset).name()); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 211 | if (kTopLeft_GrSurfaceOrigin == proc.atlasOrigin()) { |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 212 | v->codeAppendf("%s.xy = atlascoord * %s;", texcoord.vsOut(), atlasAdjust); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 213 | } else { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 214 | SkASSERT(kBottomLeft_GrSurfaceOrigin == proc.atlasOrigin()); |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 215 | v->codeAppendf("%s.xy = float2(atlascoord.x * %s.x, 1 - atlascoord.y * %s.y);", |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 216 | texcoord.vsOut(), atlasAdjust, atlasAdjust); |
| 217 | } |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 218 | // The third texture coordinate is -.5 for even-odd paths and +.5 for winding ones. |
| 219 | // ("right < left" indicates even-odd fill type.) |
| 220 | v->codeAppendf("%s.z = sign(devbounds.z - devbounds.x) * .5;", texcoord.vsOut()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 221 | |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 222 | this->emitTransforms(v, varyingHandler, uniHandler, GrShaderVar("octocoord", kFloat2_GrSLType), |
| 223 | proc.localMatrix(), args.fFPCoordTransformHandler); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 224 | |
| 225 | // Fragment shader. |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 226 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 227 | |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 228 | // Look up coverage count in the atlas. |
| 229 | f->codeAppend ("half coverage = "); |
| 230 | f->appendTextureLookup(args.fTexSamplers[0], SkStringPrintf("%s.xy", texcoord.fsIn()).c_str(), |
| 231 | kFloat2_GrSLType); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 232 | f->codeAppend (".a;"); |
| 233 | |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 234 | // Scale coverage count by .5. Make it negative for even-odd paths and positive for winding |
| 235 | // ones. Clamp winding coverage counts at 1.0 (i.e. min(coverage/2, .5)). |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 236 | f->codeAppendf("coverage = min(abs(coverage) * half(%s.z), .5);", texcoord.fsIn()); |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 237 | |
| 238 | // For negative values, this finishes the even-odd sawtooth function. Since positive (winding) |
| 239 | // values were clamped at "coverage/2 = .5", this only undoes the previous multiply by .5. |
| 240 | f->codeAppend ("coverage = 1 - abs(fract(coverage) * 2 - 1);"); |
| 241 | |
| 242 | f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 243 | } |