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 | |
| 10 | #include "GrOnFlushResourceProvider.h" |
| 11 | #include "GrTexture.h" |
| 12 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 13 | #include "glsl/GrGLSLGeometryProcessor.h" |
| 14 | #include "glsl/GrGLSLProgramBuilder.h" |
| 15 | #include "glsl/GrGLSLVarying.h" |
| 16 | |
| 17 | // Slightly undershoot an AA bloat radius of 0.5 so vertices that fall on integer boundaries don't |
| 18 | // accidentally reach into neighboring path masks within the atlas. |
| 19 | constexpr float kAABloatRadius = 0.491111f; |
| 20 | |
| 21 | // Paths are drawn as octagons. Each point on the octagon is the intersection of two lines: one edge |
| 22 | // from the path's bounding box and one edge from its 45-degree bounding box. The below inputs |
| 23 | // define a vertex by the two edges that need to be intersected. Normals point out of the octagon, |
| 24 | // and the bounding boxes are sent in as instance attribs. |
| 25 | static constexpr float kOctoEdgeNorms[8 * 4] = { |
| 26 | // bbox // bbox45 |
| 27 | -1, 0, -1,+1, |
| 28 | -1, 0, -1,-1, |
| 29 | 0,-1, -1,-1, |
| 30 | 0,-1, +1,-1, |
| 31 | +1, 0, +1,-1, |
| 32 | +1, 0, +1,+1, |
| 33 | 0,+1, +1,+1, |
| 34 | 0,+1, -1,+1, |
| 35 | }; |
| 36 | |
| 37 | GR_DECLARE_STATIC_UNIQUE_KEY(gVertexBufferKey); |
| 38 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 39 | sk_sp<const GrBuffer> GrCCPathProcessor::FindVertexBuffer(GrOnFlushResourceProvider* onFlushRP) { |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 40 | GR_DEFINE_STATIC_UNIQUE_KEY(gVertexBufferKey); |
| 41 | return onFlushRP->findOrMakeStaticBuffer(kVertex_GrBufferType, sizeof(kOctoEdgeNorms), |
| 42 | kOctoEdgeNorms, gVertexBufferKey); |
| 43 | } |
| 44 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 45 | // Index buffer for the octagon defined above. |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 46 | static uint16_t kOctoIndices[GrCCPathProcessor::kPerInstanceIndexCount] = { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 47 | 0, 4, 2, |
| 48 | 0, 6, 4, |
| 49 | 0, 2, 1, |
| 50 | 2, 4, 3, |
| 51 | 4, 6, 5, |
| 52 | 6, 0, 7, |
| 53 | }; |
| 54 | |
| 55 | GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
| 56 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 57 | sk_sp<const GrBuffer> GrCCPathProcessor::FindIndexBuffer(GrOnFlushResourceProvider* onFlushRP) { |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 58 | GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
| 59 | return onFlushRP->findOrMakeStaticBuffer(kIndex_GrBufferType, sizeof(kOctoIndices), |
| 60 | kOctoIndices, gIndexBufferKey); |
| 61 | } |
| 62 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 63 | GrCCPathProcessor::GrCCPathProcessor(GrResourceProvider* rp, sk_sp<GrTextureProxy> atlas, |
| 64 | SkPath::FillType fillType, const GrShaderCaps& shaderCaps) |
| 65 | : INHERITED(kGrCCPathProcessor_ClassID) |
Chris Dalton | a3e9271 | 2017-12-04 11:45:51 -0700 | [diff] [blame] | 66 | , fFillType(fillType) |
| 67 | , fAtlasAccess(std::move(atlas), GrSamplerState::Filter::kNearest, |
| 68 | GrSamplerState::WrapMode::kClamp, kFragment_GrShaderFlag) { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 69 | this->addInstanceAttrib("devbounds", kFloat4_GrVertexAttribType); |
| 70 | this->addInstanceAttrib("devbounds45", kFloat4_GrVertexAttribType); |
| 71 | this->addInstanceAttrib("view_matrix", kFloat4_GrVertexAttribType); |
| 72 | this->addInstanceAttrib("view_translate", kFloat2_GrVertexAttribType); |
Chris Dalton | a045eea | 2017-10-24 13:22:10 -0600 | [diff] [blame] | 73 | this->addInstanceAttrib("atlas_offset", kShort2_GrVertexAttribType); |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 74 | this->addInstanceAttrib("color", kUByte4_norm_GrVertexAttribType); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 75 | |
| 76 | SkASSERT(offsetof(Instance, fDevBounds) == |
| 77 | this->getInstanceAttrib(InstanceAttribs::kDevBounds).fOffsetInRecord); |
| 78 | SkASSERT(offsetof(Instance, fDevBounds45) == |
| 79 | this->getInstanceAttrib(InstanceAttribs::kDevBounds45).fOffsetInRecord); |
| 80 | SkASSERT(offsetof(Instance, fViewMatrix) == |
| 81 | this->getInstanceAttrib(InstanceAttribs::kViewMatrix).fOffsetInRecord); |
| 82 | SkASSERT(offsetof(Instance, fViewTranslate) == |
| 83 | this->getInstanceAttrib(InstanceAttribs::kViewTranslate).fOffsetInRecord); |
| 84 | SkASSERT(offsetof(Instance, fAtlasOffset) == |
| 85 | this->getInstanceAttrib(InstanceAttribs::kAtlasOffset).fOffsetInRecord); |
| 86 | SkASSERT(offsetof(Instance, fColor) == |
| 87 | this->getInstanceAttrib(InstanceAttribs::kColor).fOffsetInRecord); |
| 88 | SkASSERT(sizeof(Instance) == this->getInstanceStride()); |
| 89 | |
| 90 | GR_STATIC_ASSERT(6 == kNumInstanceAttribs); |
| 91 | |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 92 | this->addVertexAttrib("edge_norms", kFloat4_GrVertexAttribType); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 93 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 94 | fAtlasAccess.instantiate(rp); |
| 95 | this->addTextureSampler(&fAtlasAccess); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 96 | } |
| 97 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 98 | void GrCCPathProcessor::getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const { |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 99 | b->add32((fFillType << 16) | this->atlasProxy()->origin()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | class GLSLPathProcessor : public GrGLSLGeometryProcessor { |
| 103 | public: |
| 104 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override; |
| 105 | |
| 106 | private: |
| 107 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc, |
| 108 | FPCoordTransformIter&& transformIter) override { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 109 | const GrCCPathProcessor& proc = primProc.cast<GrCCPathProcessor>(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 110 | pdman.set2f(fAtlasAdjustUniform, 1.0f / proc.atlas()->width(), |
| 111 | 1.0f / proc.atlas()->height()); |
| 112 | this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter); |
| 113 | } |
| 114 | |
| 115 | GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform; |
| 116 | |
| 117 | typedef GrGLSLGeometryProcessor INHERITED; |
| 118 | }; |
| 119 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 120 | GrGLSLPrimitiveProcessor* GrCCPathProcessor::createGLSLInstance(const GrShaderCaps&) const { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 121 | return new GLSLPathProcessor(); |
| 122 | } |
| 123 | |
| 124 | void GLSLPathProcessor::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 125 | using InstanceAttribs = GrCCPathProcessor::InstanceAttribs; |
| 126 | const GrCCPathProcessor& proc = args.fGP.cast<GrCCPathProcessor>(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 127 | GrGLSLUniformHandler* uniHandler = args.fUniformHandler; |
| 128 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 129 | |
| 130 | const char* atlasAdjust; |
| 131 | fAtlasAdjustUniform = uniHandler->addUniform( |
| 132 | kVertex_GrShaderFlag, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 133 | kFloat2_GrSLType, "atlas_adjust", &atlasAdjust); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 134 | |
| 135 | varyingHandler->emitAttributes(proc); |
| 136 | |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 137 | GrGLSLVarying texcoord(kFloat2_GrSLType); |
| 138 | GrGLSLVarying color(kHalf4_GrSLType); |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 139 | varyingHandler->addVarying("texcoord", &texcoord); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 140 | varyingHandler->addFlatPassThroughAttribute(&proc.getInstanceAttrib(InstanceAttribs::kColor), |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 141 | args.fOutputColor); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 142 | |
Chris Dalton | d0b8d93 | 2017-12-21 16:48:52 -0700 | [diff] [blame] | 143 | // The vertex shader bloats and intersects the devBounds and devBounds45 rectangles, in order to |
| 144 | // find an octagon that circumscribes the (bloated) path. |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 145 | GrGLSLVertexBuilder* v = args.fVertBuilder; |
| 146 | |
Chris Dalton | d0b8d93 | 2017-12-21 16:48:52 -0700 | [diff] [blame] | 147 | // Each vertex is the intersection of one edge from devBounds and one from devBounds45. |
| 148 | // 'N' holds the normals to these edges as column vectors. |
| 149 | // |
| 150 | // NOTE: "float2x2(float4)" is valid and equivalent to "float2x2(float4.xy, float4.zw)", |
| 151 | // however Intel compilers crash when we use the former syntax in this shader. |
| 152 | v->codeAppendf("float2x2 N = float2x2(%s.xy, %s.zw);", |
| 153 | proc.getEdgeNormsAttrib().fName, proc.getEdgeNormsAttrib().fName); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 154 | |
| 155 | // N[0] is the normal for the edge we are intersecting from the regular bounding box, pointing |
| 156 | // out of the octagon. |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 157 | v->codeAppendf("float2 refpt = (min(N[0].x, N[0].y) < 0) ? %s.xy : %s.zw;", |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 158 | proc.getInstanceAttrib(InstanceAttribs::kDevBounds).fName, |
| 159 | proc.getInstanceAttrib(InstanceAttribs::kDevBounds).fName); |
| 160 | v->codeAppendf("refpt += N[0] * %f;", kAABloatRadius); // bloat for AA. |
| 161 | |
| 162 | // N[1] is the normal for the edge we are intersecting from the 45-degree bounding box, pointing |
| 163 | // out of the octagon. |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 164 | v->codeAppendf("float2 refpt45 = (N[1].x < 0) ? %s.xy : %s.zw;", |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 165 | proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).fName, |
| 166 | proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).fName); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 167 | 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] | 168 | v->codeAppendf("refpt45 += N[1] * %f;", kAABloatRadius); // bloat for AA. |
| 169 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 170 | v->codeAppend ("float2 K = float2(dot(N[0], refpt), dot(N[1], refpt45));"); |
| 171 | v->codeAppendf("float2 octocoord = K * inverse(N);"); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 172 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 173 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "octocoord"); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 174 | |
| 175 | // Convert to atlas coordinates in order to do our texture lookup. |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 176 | v->codeAppendf("float2 atlascoord = octocoord + float2(%s);", |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 177 | proc.getInstanceAttrib(InstanceAttribs::kAtlasOffset).fName); |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 178 | if (kTopLeft_GrSurfaceOrigin == proc.atlasProxy()->origin()) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 179 | v->codeAppendf("%s = atlascoord * %s;", texcoord.vsOut(), atlasAdjust); |
| 180 | } else { |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 181 | SkASSERT(kBottomLeft_GrSurfaceOrigin == proc.atlasProxy()->origin()); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 182 | v->codeAppendf("%s = float2(atlascoord.x * %s.x, 1 - atlascoord.y * %s.y);", |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 183 | texcoord.vsOut(), atlasAdjust, atlasAdjust); |
| 184 | } |
| 185 | |
Chris Dalton | d0b8d93 | 2017-12-21 16:48:52 -0700 | [diff] [blame] | 186 | // Convert to path/local cordinates. |
| 187 | v->codeAppendf("float2x2 viewmatrix = float2x2(%s.xy, %s.zw);", // float2x2(float4) busts Intel. |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 188 | proc.getInstanceAttrib(InstanceAttribs::kViewMatrix).fName, |
Chris Dalton | d0b8d93 | 2017-12-21 16:48:52 -0700 | [diff] [blame] | 189 | proc.getInstanceAttrib(InstanceAttribs::kViewMatrix).fName); |
| 190 | v->codeAppendf("float2 pathcoord = inverse(viewmatrix) * (octocoord - %s);", |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 191 | proc.getInstanceAttrib(InstanceAttribs::kViewTranslate).fName); |
| 192 | |
Brian Salomon | 04460cc | 2017-12-06 14:47:42 -0500 | [diff] [blame] | 193 | this->emitTransforms(v, varyingHandler, uniHandler, GrShaderVar("pathcoord", kFloat2_GrSLType), |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 194 | args.fFPCoordTransformHandler); |
| 195 | |
| 196 | // Fragment shader. |
| 197 | GrGLSLPPFragmentBuilder* f = args.fFragBuilder; |
| 198 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 199 | f->codeAppend ("half coverage_count = "); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 200 | f->appendTextureLookup(args.fTexSamplers[0], texcoord.fsIn(), kFloat2_GrSLType); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 201 | f->codeAppend (".a;"); |
| 202 | |
| 203 | if (SkPath::kWinding_FillType == proc.fillType()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 204 | f->codeAppendf("%s = half4(min(abs(coverage_count), 1));", args.fOutputCoverage); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 205 | } else { |
| 206 | SkASSERT(SkPath::kEvenOdd_FillType == proc.fillType()); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 207 | f->codeAppend ("half t = mod(abs(coverage_count), 2);"); |
| 208 | f->codeAppendf("%s = half4(1 - abs(t - 1));", args.fOutputCoverage); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 209 | } |
| 210 | } |