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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ccpr/GrCCPathProcessor.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/GrOnFlushResourceProvider.h" |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrOpsRenderPass.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 12 | #include "src/gpu/GrTexture.h" |
Chris Dalton | f91b755 | 2019-04-29 16:21:18 -0600 | [diff] [blame] | 13 | #include "src/gpu/GrTexturePriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/ccpr/GrCCPerFlushResources.h" |
| 15 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 16 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 17 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
| 18 | #include "src/gpu/glsl/GrGLSLVarying.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 19 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 20 | // Paths are drawn as octagons. Each point on the octagon is the intersection of two lines: one edge |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 21 | // from the path's bounding box and one edge from its 45-degree bounding box. The selectors |
| 22 | // below indicate one corner from the bounding box, paired with a corner from the 45-degree bounding |
| 23 | // box. The octagon vertex is the point that lies between these two corners, found by intersecting |
| 24 | // their edges. |
| 25 | static constexpr float kOctoEdgeNorms[8*4] = { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 26 | // bbox // bbox45 |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 27 | 0,0, 0,0, |
| 28 | 0,0, 1,0, |
| 29 | 1,0, 1,0, |
| 30 | 1,0, 1,1, |
| 31 | 1,1, 1,1, |
| 32 | 1,1, 0,1, |
| 33 | 0,1, 0,1, |
| 34 | 0,1, 0,0, |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | GR_DECLARE_STATIC_UNIQUE_KEY(gVertexBufferKey); |
| 38 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 39 | sk_sp<const GrGpuBuffer> GrCCPathProcessor::FindVertexBuffer(GrOnFlushResourceProvider* onFlushRP) { |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 40 | GR_DEFINE_STATIC_UNIQUE_KEY(gVertexBufferKey); |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 41 | return onFlushRP->findOrMakeStaticBuffer(GrGpuBufferType::kVertex, sizeof(kOctoEdgeNorms), |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 42 | kOctoEdgeNorms, gVertexBufferKey); |
| 43 | } |
| 44 | |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 45 | static constexpr uint16_t kRestartStrip = 0xffff; |
| 46 | |
| 47 | static constexpr uint16_t kOctoIndicesAsStrips[] = { |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 48 | 3, 4, 2, 0, 1, kRestartStrip, // First half. |
| 49 | 7, 0, 6, 4, 5 // Second half. |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static constexpr uint16_t kOctoIndicesAsTris[] = { |
| 53 | // First half. |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 54 | 3, 4, 2, |
| 55 | 4, 0, 2, |
| 56 | 2, 0, 1, |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 57 | |
| 58 | // Second half. |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 59 | 7, 0, 6, |
| 60 | 0, 4, 6, |
| 61 | 6, 4, 5, |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
| 65 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 66 | constexpr GrPrimitiveProcessor::Attribute GrCCPathProcessor::kInstanceAttribs[]; |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 67 | constexpr GrPrimitiveProcessor::Attribute GrCCPathProcessor::kCornersAttrib; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 68 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 69 | sk_sp<const GrGpuBuffer> GrCCPathProcessor::FindIndexBuffer(GrOnFlushResourceProvider* onFlushRP) { |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 70 | GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 71 | if (onFlushRP->caps()->usePrimitiveRestart()) { |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 72 | return onFlushRP->findOrMakeStaticBuffer(GrGpuBufferType::kIndex, |
| 73 | sizeof(kOctoIndicesAsStrips), kOctoIndicesAsStrips, |
| 74 | gIndexBufferKey); |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 75 | } else { |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 76 | return onFlushRP->findOrMakeStaticBuffer(GrGpuBufferType::kIndex, |
| 77 | sizeof(kOctoIndicesAsTris), kOctoIndicesAsTris, |
| 78 | gIndexBufferKey); |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 79 | } |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Chris Dalton | 6a5317a | 2019-07-12 09:55:52 -0600 | [diff] [blame] | 82 | GrCCPathProcessor::GrCCPathProcessor(CoverageMode coverageMode, const GrTexture* atlasTexture, |
| 83 | const GrSwizzle& swizzle, GrSurfaceOrigin atlasOrigin, |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 84 | const SkMatrix& viewMatrixIfUsingLocalCoords) |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 85 | : INHERITED(kGrCCPathProcessor_ClassID) |
Chris Dalton | 6a5317a | 2019-07-12 09:55:52 -0600 | [diff] [blame] | 86 | , fCoverageMode(coverageMode) |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 87 | , fAtlasAccess(GrSamplerState::Filter::kNearest, atlasTexture->backendFormat(), swizzle) |
Brian Salomon | 2638f3d | 2019-10-22 12:20:37 -0400 | [diff] [blame] | 88 | , fAtlasDimensions(atlasTexture->dimensions()) |
Chris Dalton | f91b755 | 2019-04-29 16:21:18 -0600 | [diff] [blame] | 89 | , fAtlasOrigin(atlasOrigin) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 90 | // TODO: Can we just assert that atlas has GrCCAtlas::kTextureOrigin and remove fAtlasOrigin? |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 91 | this->setInstanceAttributes(kInstanceAttribs, SK_ARRAY_COUNT(kInstanceAttribs)); |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 92 | SkASSERT(this->instanceStride() == sizeof(Instance)); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 93 | |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 94 | this->setVertexAttributes(&kCornersAttrib, 1); |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 95 | this->setTextureSamplerCnt(1); |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 96 | |
Chris Dalton | 1c54894 | 2018-05-22 13:09:48 -0600 | [diff] [blame] | 97 | if (!viewMatrixIfUsingLocalCoords.invert(&fLocalMatrix)) { |
| 98 | fLocalMatrix.setIdentity(); |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 99 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 100 | } |
| 101 | |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 102 | class GrCCPathProcessor::Impl : public GrGLSLGeometryProcessor { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 103 | public: |
| 104 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override; |
| 105 | |
| 106 | private: |
| 107 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc, |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 108 | const CoordTransformRange& transformRange) override { |
Chris Dalton | f91b755 | 2019-04-29 16:21:18 -0600 | [diff] [blame] | 109 | const auto& proc = primProc.cast<GrCCPathProcessor>(); |
Brian Salomon | 2638f3d | 2019-10-22 12:20:37 -0400 | [diff] [blame] | 110 | pdman.set2f(fAtlasAdjustUniform, |
| 111 | 1.0f / proc.fAtlasDimensions.fWidth, |
| 112 | 1.0f / proc.fAtlasDimensions.fHeight); |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 113 | this->setTransformDataHelper(proc.fLocalMatrix, pdman, transformRange); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform; |
| 117 | |
| 118 | typedef GrGLSLGeometryProcessor INHERITED; |
| 119 | }; |
| 120 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 121 | GrGLSLPrimitiveProcessor* GrCCPathProcessor::createGLSLInstance(const GrShaderCaps&) const { |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 122 | return new Impl(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 123 | } |
| 124 | |
Chris Dalton | d925f2d | 2018-05-07 19:19:06 -0600 | [diff] [blame] | 125 | void GrCCPathProcessor::drawPaths(GrOpFlushState* flushState, const GrPipeline& pipeline, |
Chris Dalton | 39ca973 | 2020-03-10 10:34:17 -0600 | [diff] [blame] | 126 | const GrSurfaceProxy& atlasProxy, |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 127 | const GrCCPerFlushResources& resources, int baseInstance, |
| 128 | int endInstance, const SkRect& bounds) const { |
Chris Dalton | d925f2d | 2018-05-07 19:19:06 -0600 | [diff] [blame] | 129 | const GrCaps& caps = flushState->caps(); |
| 130 | GrPrimitiveType primitiveType = caps.usePrimitiveRestart() |
| 131 | ? GrPrimitiveType::kTriangleStrip |
| 132 | : GrPrimitiveType::kTriangles; |
| 133 | int numIndicesPerInstance = caps.usePrimitiveRestart() |
| 134 | ? SK_ARRAY_COUNT(kOctoIndicesAsStrips) |
| 135 | : SK_ARRAY_COUNT(kOctoIndicesAsTris); |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 136 | auto enablePrimitiveRestart = GrPrimitiveRestart(flushState->caps().usePrimitiveRestart()); |
| 137 | |
Chris Dalton | 39ca973 | 2020-03-10 10:34:17 -0600 | [diff] [blame] | 138 | GrRenderTargetProxy* rtProxy = flushState->proxy(); |
| 139 | GrProgramInfo programInfo(rtProxy->numSamples(), rtProxy->numStencilSamples(), |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 140 | rtProxy->backendFormat(), flushState->writeView()->origin(), |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 141 | &pipeline, this, primitiveType); |
Chris Dalton | d925f2d | 2018-05-07 19:19:06 -0600 | [diff] [blame] | 142 | |
Chris Dalton | aa0e45c | 2020-03-16 10:05:11 -0600 | [diff] [blame] | 143 | flushState->bindPipelineAndScissorClip(programInfo, bounds); |
| 144 | flushState->bindTextures(*this, atlasProxy, pipeline); |
| 145 | flushState->bindBuffers(resources.indexBuffer(), resources.instanceBuffer(), |
Chris Dalton | 39ca973 | 2020-03-10 10:34:17 -0600 | [diff] [blame] | 146 | resources.vertexBuffer(), enablePrimitiveRestart); |
Chris Dalton | aa0e45c | 2020-03-16 10:05:11 -0600 | [diff] [blame] | 147 | flushState->drawIndexedInstanced(numIndicesPerInstance, 0, endInstance - baseInstance, |
Chris Dalton | 39ca973 | 2020-03-10 10:34:17 -0600 | [diff] [blame] | 148 | baseInstance, 0); |
Chris Dalton | d925f2d | 2018-05-07 19:19:06 -0600 | [diff] [blame] | 149 | } |
| 150 | |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 151 | void GrCCPathProcessor::Impl::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { |
Chris Dalton | 7b04631 | 2018-02-02 11:06:30 -0700 | [diff] [blame] | 152 | using Interpolation = GrGLSLVaryingHandler::Interpolation; |
| 153 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 154 | const GrCCPathProcessor& proc = args.fGP.cast<GrCCPathProcessor>(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 155 | GrGLSLUniformHandler* uniHandler = args.fUniformHandler; |
| 156 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
Chris Dalton | 6a5317a | 2019-07-12 09:55:52 -0600 | [diff] [blame] | 157 | bool isCoverageCount = (CoverageMode::kCoverageCount == proc.fCoverageMode); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 158 | |
| 159 | const char* atlasAdjust; |
| 160 | fAtlasAdjustUniform = uniHandler->addUniform( |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 161 | kVertex_GrShaderFlag, kFloat2_GrSLType, "atlas_adjust", &atlasAdjust); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 162 | |
| 163 | varyingHandler->emitAttributes(proc); |
| 164 | |
Chris Dalton | 6a5317a | 2019-07-12 09:55:52 -0600 | [diff] [blame] | 165 | GrGLSLVarying texcoord((isCoverageCount) ? kFloat3_GrSLType : kFloat2_GrSLType); |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 166 | varyingHandler->addVarying("texcoord", &texcoord); |
Chris Dalton | 6a5317a | 2019-07-12 09:55:52 -0600 | [diff] [blame] | 167 | |
| 168 | GrGLSLVarying color(kHalf4_GrSLType); |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 169 | varyingHandler->addPassThroughAttribute( |
| 170 | kInstanceAttribs[kColorAttribIdx], args.fOutputColor, Interpolation::kCanBeFlat); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 171 | |
Chris Dalton | d0b8d93 | 2017-12-21 16:48:52 -0700 | [diff] [blame] | 172 | // The vertex shader bloats and intersects the devBounds and devBounds45 rectangles, in order to |
| 173 | // find an octagon that circumscribes the (bloated) path. |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 174 | GrGLSLVertexBuilder* v = args.fVertBuilder; |
| 175 | |
Chris Dalton | 5b5403e | 2019-06-05 11:54:39 -0600 | [diff] [blame] | 176 | // Are we clockwise? (Positive wind => nonzero fill rule.) |
| 177 | // Or counter-clockwise? (negative wind => even/odd fill rule.) |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 178 | v->codeAppendf("float wind = sign(devbounds.z - devbounds.x);"); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 179 | |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 180 | // Find our reference corner from the device-space bounding box. |
| 181 | v->codeAppendf("float2 refpt = mix(devbounds.xy, devbounds.zw, corners.xy);"); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 182 | |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 183 | // Find our reference corner from the 45-degree bounding box. |
| 184 | v->codeAppendf("float2 refpt45 = mix(devbounds45.xy, devbounds45.zw, corners.zw);"); |
| 185 | // Transform back to device space. |
| 186 | v->codeAppendf("refpt45 *= float2x2(+1, +1, -wind, +wind) * .5;"); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 187 | |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 188 | // Find the normals to each edge, then intersect them to find our octagon vertex. |
| 189 | v->codeAppendf("float2x2 N = float2x2(" |
| 190 | "corners.z + corners.w - 1, corners.w - corners.z, " |
| 191 | "corners.xy*2 - 1);"); |
| 192 | v->codeAppendf("N = float2x2(wind, 0, 0, 1) * N;"); |
| 193 | v->codeAppendf("float2 K = float2(dot(N[0], refpt), dot(N[1], refpt45));"); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 194 | v->codeAppendf("float2 octocoord = K * inverse(N);"); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 195 | |
Chris Dalton | 51e8b7e | 2018-10-09 21:18:45 -0600 | [diff] [blame] | 196 | // Round the octagon out to ensure we rasterize every pixel the path might touch. (Positive |
| 197 | // bloatdir means we should take the "ceil" and negative means to take the "floor".) |
| 198 | // |
| 199 | // NOTE: If we were just drawing a rect, ceil/floor would be enough. But since there are also |
| 200 | // diagonals in the octagon that cross through pixel centers, we need to outset by another |
| 201 | // quarter px to ensure those pixels get rasterized. |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 202 | v->codeAppendf("float2 bloatdir = (0 != N[0].x) " |
| 203 | "? float2(N[0].x, N[1].y)" |
| 204 | ": float2(N[1].x, N[0].y);"); |
| 205 | v->codeAppendf("octocoord = (ceil(octocoord * bloatdir - 1e-4) + 0.25) * bloatdir;"); |
Chris Dalton | 6a5317a | 2019-07-12 09:55:52 -0600 | [diff] [blame] | 206 | v->codeAppendf("float2 atlascoord = octocoord + float2(dev_to_atlas_offset);"); |
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. |
Chris Dalton | f91b755 | 2019-04-29 16:21:18 -0600 | [diff] [blame] | 209 | if (kTopLeft_GrSurfaceOrigin == proc.fAtlasOrigin) { |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 210 | v->codeAppendf("%s.xy = atlascoord * %s;", texcoord.vsOut(), atlasAdjust); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 211 | } else { |
Chris Dalton | f91b755 | 2019-04-29 16:21:18 -0600 | [diff] [blame] | 212 | SkASSERT(kBottomLeft_GrSurfaceOrigin == proc.fAtlasOrigin); |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 213 | 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] | 214 | texcoord.vsOut(), atlasAdjust, atlasAdjust); |
| 215 | } |
Chris Dalton | 6a5317a | 2019-07-12 09:55:52 -0600 | [diff] [blame] | 216 | if (isCoverageCount) { |
| 217 | v->codeAppendf("%s.z = wind * .5;", texcoord.vsOut()); |
| 218 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 219 | |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 220 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "octocoord"); |
Chris Dalton | f91b755 | 2019-04-29 16:21:18 -0600 | [diff] [blame] | 221 | this->emitTransforms(v, varyingHandler, uniHandler, gpArgs->fPositionVar, proc.fLocalMatrix, |
Chris Dalton | 377b18b | 2019-04-18 13:33:50 -0600 | [diff] [blame] | 222 | args.fFPCoordTransformHandler); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 223 | |
| 224 | // Fragment shader. |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 225 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 226 | |
Chris Dalton | 6a5317a | 2019-07-12 09:55:52 -0600 | [diff] [blame] | 227 | // Look up coverage in the atlas. |
| 228 | f->codeAppendf("half coverage = "); |
Brian Salomon | d19cd76 | 2020-01-06 13:16:31 -0500 | [diff] [blame] | 229 | f->appendTextureLookup(args.fTexSamplers[0], SkStringPrintf("%s.xy", texcoord.fsIn()).c_str()); |
Chris Dalton | 6a5317a | 2019-07-12 09:55:52 -0600 | [diff] [blame] | 230 | f->codeAppendf(".a;"); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 231 | |
Chris Dalton | 6a5317a | 2019-07-12 09:55:52 -0600 | [diff] [blame] | 232 | if (isCoverageCount) { |
| 233 | f->codeAppendf("coverage = abs(coverage);"); |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 234 | |
Chris Dalton | 6a5317a | 2019-07-12 09:55:52 -0600 | [diff] [blame] | 235 | // Scale coverage count by .5. Make it negative for even-odd paths and positive for |
| 236 | // winding ones. Clamp winding coverage counts at 1.0 (i.e. min(coverage/2, .5)). |
| 237 | f->codeAppendf("coverage = min(abs(coverage) * half(%s.z), .5);", texcoord.fsIn()); |
| 238 | |
| 239 | // For negative values, this finishes the even-odd sawtooth function. Since positive |
| 240 | // (winding) values were clamped at "coverage/2 = .5", this only undoes the previous |
| 241 | // multiply by .5. |
| 242 | f->codeAppend ("coverage = 1 - abs(fract(coverage) * 2 - 1);"); |
| 243 | } |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 244 | |
| 245 | f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 246 | } |