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" |
| 13 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 14 | #include "glsl/GrGLSLGeometryProcessor.h" |
| 15 | #include "glsl/GrGLSLProgramBuilder.h" |
| 16 | #include "glsl/GrGLSLVarying.h" |
| 17 | |
| 18 | // Slightly undershoot an AA bloat radius of 0.5 so vertices that fall on integer boundaries don't |
| 19 | // accidentally reach into neighboring path masks within the atlas. |
| 20 | constexpr float kAABloatRadius = 0.491111f; |
| 21 | |
| 22 | // Paths are drawn as octagons. Each point on the octagon is the intersection of two lines: one edge |
| 23 | // from the path's bounding box and one edge from its 45-degree bounding box. The below inputs |
| 24 | // define a vertex by the two edges that need to be intersected. Normals point out of the octagon, |
| 25 | // and the bounding boxes are sent in as instance attribs. |
| 26 | static constexpr float kOctoEdgeNorms[8 * 4] = { |
| 27 | // bbox // bbox45 |
| 28 | -1, 0, -1,+1, |
| 29 | -1, 0, -1,-1, |
| 30 | 0,-1, -1,-1, |
| 31 | 0,-1, +1,-1, |
| 32 | +1, 0, +1,-1, |
| 33 | +1, 0, +1,+1, |
| 34 | 0,+1, +1,+1, |
| 35 | 0,+1, -1,+1, |
| 36 | }; |
| 37 | |
| 38 | GR_DECLARE_STATIC_UNIQUE_KEY(gVertexBufferKey); |
| 39 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 40 | sk_sp<const GrBuffer> GrCCPathProcessor::FindVertexBuffer(GrOnFlushResourceProvider* onFlushRP) { |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 41 | GR_DEFINE_STATIC_UNIQUE_KEY(gVertexBufferKey); |
| 42 | return onFlushRP->findOrMakeStaticBuffer(kVertex_GrBufferType, sizeof(kOctoEdgeNorms), |
| 43 | kOctoEdgeNorms, gVertexBufferKey); |
| 44 | } |
| 45 | |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 46 | static constexpr uint16_t kRestartStrip = 0xffff; |
| 47 | |
| 48 | static constexpr uint16_t kOctoIndicesAsStrips[] = { |
| 49 | 1, 0, 2, 4, 3, kRestartStrip, // First half. |
| 50 | 5, 4, 6, 0, 7 // Second half. |
| 51 | }; |
| 52 | |
| 53 | static constexpr uint16_t kOctoIndicesAsTris[] = { |
| 54 | // First half. |
| 55 | 1, 0, 2, |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 56 | 0, 4, 2, |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 57 | 2, 4, 3, |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 58 | |
| 59 | // Second half. |
| 60 | 5, 4, 6, |
| 61 | 4, 0, 6, |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 62 | 6, 0, 7, |
| 63 | }; |
| 64 | |
| 65 | GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
| 66 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 67 | sk_sp<const GrBuffer> 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()) { |
| 70 | return onFlushRP->findOrMakeStaticBuffer(kIndex_GrBufferType, sizeof(kOctoIndicesAsStrips), |
| 71 | kOctoIndicesAsStrips, gIndexBufferKey); |
| 72 | } else { |
| 73 | return onFlushRP->findOrMakeStaticBuffer(kIndex_GrBufferType, sizeof(kOctoIndicesAsTris), |
| 74 | kOctoIndicesAsTris, gIndexBufferKey); |
| 75 | } |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 78 | GrCCPathProcessor::GrCCPathProcessor(GrResourceProvider* resourceProvider, |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 79 | sk_sp<GrTextureProxy> atlas, |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 80 | const SkMatrix& viewMatrixIfUsingLocalCoords) |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 81 | : INHERITED(kGrCCPathProcessor_ClassID) |
Chris Dalton | a3e9271 | 2017-12-04 11:45:51 -0700 | [diff] [blame] | 82 | , fAtlasAccess(std::move(atlas), GrSamplerState::Filter::kNearest, |
| 83 | GrSamplerState::WrapMode::kClamp, kFragment_GrShaderFlag) { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 84 | this->addInstanceAttrib("devbounds", kFloat4_GrVertexAttribType); |
| 85 | this->addInstanceAttrib("devbounds45", kFloat4_GrVertexAttribType); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame^] | 86 | this->addInstanceAttrib("dev_to_atlas_offset", kInt2_GrVertexAttribType); |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 87 | this->addInstanceAttrib("color", kUByte4_norm_GrVertexAttribType); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 88 | |
| 89 | SkASSERT(offsetof(Instance, fDevBounds) == |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 90 | this->getInstanceAttrib(InstanceAttribs::kDevBounds).offsetInRecord()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 91 | SkASSERT(offsetof(Instance, fDevBounds45) == |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 92 | this->getInstanceAttrib(InstanceAttribs::kDevBounds45).offsetInRecord()); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame^] | 93 | SkASSERT(offsetof(Instance, fDevToAtlasOffset) == |
| 94 | this->getInstanceAttrib(InstanceAttribs::kDevToAtlasOffset).offsetInRecord()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 95 | SkASSERT(offsetof(Instance, fColor) == |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 96 | this->getInstanceAttrib(InstanceAttribs::kColor).offsetInRecord()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 97 | SkASSERT(sizeof(Instance) == this->getInstanceStride()); |
| 98 | |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 99 | GR_STATIC_ASSERT(4 == kNumInstanceAttribs); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 100 | |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 101 | this->addVertexAttrib("edge_norms", kFloat4_GrVertexAttribType); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 102 | |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 103 | fAtlasAccess.instantiate(resourceProvider); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 104 | this->addTextureSampler(&fAtlasAccess); |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 105 | |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 106 | if (!viewMatrixIfUsingLocalCoords.invert(&fLocalMatrix)) { |
| 107 | fLocalMatrix.setIdentity(); |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 108 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 109 | } |
| 110 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 111 | class GLSLPathProcessor : public GrGLSLGeometryProcessor { |
| 112 | public: |
| 113 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override; |
| 114 | |
| 115 | private: |
| 116 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc, |
| 117 | FPCoordTransformIter&& transformIter) override { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 118 | const GrCCPathProcessor& proc = primProc.cast<GrCCPathProcessor>(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 119 | pdman.set2f(fAtlasAdjustUniform, 1.0f / proc.atlas()->width(), |
| 120 | 1.0f / proc.atlas()->height()); |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 121 | this->setTransformDataHelper(proc.localMatrix(), pdman, &transformIter); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform; |
| 125 | |
| 126 | typedef GrGLSLGeometryProcessor INHERITED; |
| 127 | }; |
| 128 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 129 | GrGLSLPrimitiveProcessor* GrCCPathProcessor::createGLSLInstance(const GrShaderCaps&) const { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 130 | return new GLSLPathProcessor(); |
| 131 | } |
| 132 | |
Chris Dalton | d925f2d | 2018-05-07 19:19:06 -0600 | [diff] [blame] | 133 | void GrCCPathProcessor::drawPaths(GrOpFlushState* flushState, const GrPipeline& pipeline, |
| 134 | const GrBuffer* indexBuffer, const GrBuffer* vertexBuffer, |
| 135 | GrBuffer* instanceBuffer, int baseInstance, int endInstance, |
| 136 | const SkRect& bounds) const { |
| 137 | const GrCaps& caps = flushState->caps(); |
| 138 | GrPrimitiveType primitiveType = caps.usePrimitiveRestart() |
| 139 | ? GrPrimitiveType::kTriangleStrip |
| 140 | : GrPrimitiveType::kTriangles; |
| 141 | int numIndicesPerInstance = caps.usePrimitiveRestart() |
| 142 | ? SK_ARRAY_COUNT(kOctoIndicesAsStrips) |
| 143 | : SK_ARRAY_COUNT(kOctoIndicesAsTris); |
| 144 | GrMesh mesh(primitiveType); |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 145 | auto enablePrimitiveRestart = GrPrimitiveRestart(flushState->caps().usePrimitiveRestart()); |
| 146 | |
Chris Dalton | d925f2d | 2018-05-07 19:19:06 -0600 | [diff] [blame] | 147 | mesh.setIndexedInstanced(indexBuffer, numIndicesPerInstance, instanceBuffer, |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 148 | endInstance - baseInstance, baseInstance, enablePrimitiveRestart); |
Chris Dalton | d925f2d | 2018-05-07 19:19:06 -0600 | [diff] [blame] | 149 | mesh.setVertexData(vertexBuffer); |
| 150 | |
| 151 | flushState->rtCommandBuffer()->draw(pipeline, *this, &mesh, nullptr, 1, bounds); |
| 152 | } |
| 153 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 154 | void GLSLPathProcessor::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 155 | using InstanceAttribs = GrCCPathProcessor::InstanceAttribs; |
Chris Dalton | 7b04631 | 2018-02-02 11:06:30 -0700 | [diff] [blame] | 156 | using Interpolation = GrGLSLVaryingHandler::Interpolation; |
| 157 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 158 | const GrCCPathProcessor& proc = args.fGP.cast<GrCCPathProcessor>(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 159 | GrGLSLUniformHandler* uniHandler = args.fUniformHandler; |
| 160 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 161 | |
| 162 | const char* atlasAdjust; |
| 163 | fAtlasAdjustUniform = uniHandler->addUniform( |
| 164 | kVertex_GrShaderFlag, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 165 | kFloat2_GrSLType, "atlas_adjust", &atlasAdjust); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 166 | |
| 167 | varyingHandler->emitAttributes(proc); |
| 168 | |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 169 | GrGLSLVarying texcoord(kFloat3_GrSLType); |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 170 | GrGLSLVarying color(kHalf4_GrSLType); |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 171 | varyingHandler->addVarying("texcoord", &texcoord); |
Chris Dalton | 7b04631 | 2018-02-02 11:06:30 -0700 | [diff] [blame] | 172 | varyingHandler->addPassThroughAttribute(&proc.getInstanceAttrib(InstanceAttribs::kColor), |
| 173 | args.fOutputColor, Interpolation::kCanBeFlat); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 174 | |
Chris Dalton | d0b8d93 | 2017-12-21 16:48:52 -0700 | [diff] [blame] | 175 | // The vertex shader bloats and intersects the devBounds and devBounds45 rectangles, in order to |
| 176 | // find an octagon that circumscribes the (bloated) path. |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 177 | GrGLSLVertexBuilder* v = args.fVertBuilder; |
| 178 | |
Chris Dalton | d0b8d93 | 2017-12-21 16:48:52 -0700 | [diff] [blame] | 179 | // Each vertex is the intersection of one edge from devBounds and one from devBounds45. |
| 180 | // 'N' holds the normals to these edges as column vectors. |
| 181 | // |
| 182 | // NOTE: "float2x2(float4)" is valid and equivalent to "float2x2(float4.xy, float4.zw)", |
| 183 | // 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] | 184 | v->codeAppendf("float2x2 N = float2x2(%s.xy, %s.zw);", proc.getEdgeNormsAttrib().name(), |
| 185 | proc.getEdgeNormsAttrib().name()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 186 | |
| 187 | // N[0] is the normal for the edge we are intersecting from the regular bounding box, pointing |
| 188 | // out of the octagon. |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 189 | v->codeAppendf("float4 devbounds = %s;", |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 190 | proc.getInstanceAttrib(InstanceAttribs::kDevBounds).name()); |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 191 | v->codeAppend ("float2 refpt = (0 == sk_VertexID >> 2)" |
| 192 | "? float2(min(devbounds.x, devbounds.z), devbounds.y)" |
| 193 | ": float2(max(devbounds.x, devbounds.z), devbounds.w);"); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 194 | v->codeAppendf("refpt += N[0] * %f;", kAABloatRadius); // bloat for AA. |
| 195 | |
| 196 | // N[1] is the normal for the edge we are intersecting from the 45-degree bounding box, pointing |
| 197 | // out of the octagon. |
Chris Dalton | 5cd6700 | 2018-04-30 11:04:40 -0600 | [diff] [blame] | 198 | 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] | 199 | proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).name(), |
| 200 | proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).name()); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 201 | 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] | 202 | v->codeAppendf("refpt45 += N[1] * %f;", kAABloatRadius); // bloat for AA. |
| 203 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 204 | v->codeAppend ("float2 K = float2(dot(N[0], refpt), dot(N[1], refpt45));"); |
| 205 | v->codeAppendf("float2 octocoord = K * inverse(N);"); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 206 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 207 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "octocoord"); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 208 | |
| 209 | // Convert to atlas coordinates in order to do our texture lookup. |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 210 | v->codeAppendf("float2 atlascoord = octocoord + float2(%s);", |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame^] | 211 | proc.getInstanceAttrib(InstanceAttribs::kDevToAtlasOffset).name()); |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 212 | if (kTopLeft_GrSurfaceOrigin == proc.atlasProxy()->origin()) { |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 213 | v->codeAppendf("%s.xy = atlascoord * %s;", texcoord.vsOut(), atlasAdjust); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 214 | } else { |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 215 | SkASSERT(kBottomLeft_GrSurfaceOrigin == proc.atlasProxy()->origin()); |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 216 | 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] | 217 | texcoord.vsOut(), atlasAdjust, atlasAdjust); |
| 218 | } |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 219 | // The third texture coordinate is -.5 for even-odd paths and +.5 for winding ones. |
| 220 | // ("right < left" indicates even-odd fill type.) |
| 221 | v->codeAppendf("%s.z = sign(devbounds.z - devbounds.x) * .5;", texcoord.vsOut()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 222 | |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 223 | this->emitTransforms(v, varyingHandler, uniHandler, GrShaderVar("octocoord", kFloat2_GrSLType), |
| 224 | proc.localMatrix(), args.fFPCoordTransformHandler); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 225 | |
| 226 | // Fragment shader. |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 227 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 228 | |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 229 | // Look up coverage count in the atlas. |
| 230 | f->codeAppend ("half coverage = "); |
| 231 | f->appendTextureLookup(args.fTexSamplers[0], SkStringPrintf("%s.xy", texcoord.fsIn()).c_str(), |
| 232 | kFloat2_GrSLType); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 233 | f->codeAppend (".a;"); |
| 234 | |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 235 | // Scale coverage count by .5. Make it negative for even-odd paths and positive for winding |
| 236 | // ones. Clamp winding coverage counts at 1.0 (i.e. min(coverage/2, .5)). |
| 237 | f->codeAppendf("coverage = min(abs(coverage) * %s.z, .5);", texcoord.fsIn()); |
| 238 | |
| 239 | // For negative values, this finishes the even-odd sawtooth function. Since positive (winding) |
| 240 | // values were clamped at "coverage/2 = .5", this only undoes the previous multiply by .5. |
| 241 | f->codeAppend ("coverage = 1 - abs(fract(coverage) * 2 - 1);"); |
| 242 | |
| 243 | f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 244 | } |