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 | |
| 8 | #include "GrCCPRPathProcessor.h" |
| 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 | |
| 39 | // Index buffer for the octagon defined above. |
| 40 | static uint16_t kOctoIndices[GrCCPRPathProcessor::kPerInstanceIndexCount] = { |
| 41 | 0, 4, 2, |
| 42 | 0, 6, 4, |
| 43 | 0, 2, 1, |
| 44 | 2, 4, 3, |
| 45 | 4, 6, 5, |
| 46 | 6, 0, 7, |
| 47 | }; |
| 48 | |
| 49 | GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
| 50 | |
| 51 | GrCCPRPathProcessor::GrCCPRPathProcessor(GrResourceProvider* rp, sk_sp<GrTextureProxy> atlas, |
| 52 | SkPath::FillType fillType, const GrShaderCaps& shaderCaps) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 53 | : INHERITED(kGrCCPRPathProcessor_ClassID) |
| 54 | , fFillType(fillType) { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 55 | this->addInstanceAttrib("devbounds", kFloat4_GrVertexAttribType); |
| 56 | this->addInstanceAttrib("devbounds45", kFloat4_GrVertexAttribType); |
| 57 | this->addInstanceAttrib("view_matrix", kFloat4_GrVertexAttribType); |
| 58 | this->addInstanceAttrib("view_translate", kFloat2_GrVertexAttribType); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 59 | // FIXME: this could be a vector of two shorts if it were supported by Ganesh. |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 60 | // Note: this should be doable now with kUShort2_GrVertexAttribType |
| 61 | this->addInstanceAttrib("atlas_offset", kInt2_GrVertexAttribType); |
| 62 | this->addInstanceAttrib("color", kUByte4_norm_GrVertexAttribType); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 63 | |
| 64 | SkASSERT(offsetof(Instance, fDevBounds) == |
| 65 | this->getInstanceAttrib(InstanceAttribs::kDevBounds).fOffsetInRecord); |
| 66 | SkASSERT(offsetof(Instance, fDevBounds45) == |
| 67 | this->getInstanceAttrib(InstanceAttribs::kDevBounds45).fOffsetInRecord); |
| 68 | SkASSERT(offsetof(Instance, fViewMatrix) == |
| 69 | this->getInstanceAttrib(InstanceAttribs::kViewMatrix).fOffsetInRecord); |
| 70 | SkASSERT(offsetof(Instance, fViewTranslate) == |
| 71 | this->getInstanceAttrib(InstanceAttribs::kViewTranslate).fOffsetInRecord); |
| 72 | SkASSERT(offsetof(Instance, fAtlasOffset) == |
| 73 | this->getInstanceAttrib(InstanceAttribs::kAtlasOffset).fOffsetInRecord); |
| 74 | SkASSERT(offsetof(Instance, fColor) == |
| 75 | this->getInstanceAttrib(InstanceAttribs::kColor).fOffsetInRecord); |
| 76 | SkASSERT(sizeof(Instance) == this->getInstanceStride()); |
| 77 | |
| 78 | GR_STATIC_ASSERT(6 == kNumInstanceAttribs); |
| 79 | |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 80 | this->addVertexAttrib("edge_norms", kFloat4_GrVertexAttribType); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 81 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 82 | fAtlasAccess.reset(std::move(atlas), GrSamplerState::Filter::kNearest, |
| 83 | GrSamplerState::WrapMode::kClamp, kFragment_GrShaderFlag); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 84 | fAtlasAccess.instantiate(rp); |
| 85 | this->addTextureSampler(&fAtlasAccess); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void GrCCPRPathProcessor::getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const { |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 89 | b->add32((fFillType << 16) | this->atlasProxy()->origin()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | class GLSLPathProcessor : public GrGLSLGeometryProcessor { |
| 93 | public: |
| 94 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override; |
| 95 | |
| 96 | private: |
| 97 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc, |
| 98 | FPCoordTransformIter&& transformIter) override { |
| 99 | const GrCCPRPathProcessor& proc = primProc.cast<GrCCPRPathProcessor>(); |
| 100 | pdman.set2f(fAtlasAdjustUniform, 1.0f / proc.atlas()->width(), |
| 101 | 1.0f / proc.atlas()->height()); |
| 102 | this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter); |
| 103 | } |
| 104 | |
| 105 | GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform; |
| 106 | |
| 107 | typedef GrGLSLGeometryProcessor INHERITED; |
| 108 | }; |
| 109 | |
| 110 | GrGLSLPrimitiveProcessor* GrCCPRPathProcessor::createGLSLInstance(const GrShaderCaps&) const { |
| 111 | return new GLSLPathProcessor(); |
| 112 | } |
| 113 | |
| 114 | void GLSLPathProcessor::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) { |
| 115 | using InstanceAttribs = GrCCPRPathProcessor::InstanceAttribs; |
| 116 | const GrCCPRPathProcessor& proc = args.fGP.cast<GrCCPRPathProcessor>(); |
| 117 | GrGLSLUniformHandler* uniHandler = args.fUniformHandler; |
| 118 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 119 | |
| 120 | const char* atlasAdjust; |
| 121 | fAtlasAdjustUniform = uniHandler->addUniform( |
| 122 | kVertex_GrShaderFlag, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 123 | kFloat2_GrSLType, "atlas_adjust", &atlasAdjust); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 124 | |
| 125 | varyingHandler->emitAttributes(proc); |
| 126 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 127 | GrGLSLVertToFrag texcoord(kFloat2_GrSLType); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 128 | GrGLSLVertToFrag color(kHalf4_GrSLType); |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 129 | varyingHandler->addVarying("texcoord", &texcoord); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 130 | varyingHandler->addFlatPassThroughAttribute(&proc.getInstanceAttrib(InstanceAttribs::kColor), |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 131 | args.fOutputColor); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 132 | |
| 133 | // Vertex shader. |
| 134 | GrGLSLVertexBuilder* v = args.fVertBuilder; |
| 135 | |
| 136 | // Find the intersections of (bloated) devBounds and devBounds45 in order to come up with an |
| 137 | // octagon that circumscribes the (bloated) path. A vertex is the intersection of two lines: |
| 138 | // one edge from the path's bounding box and one edge from its 45-degree bounding box. |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 139 | v->codeAppendf("float2x2 N = float2x2(%s);", proc.getEdgeNormsAttrib().fName); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 140 | |
| 141 | // N[0] is the normal for the edge we are intersecting from the regular bounding box, pointing |
| 142 | // out of the octagon. |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 143 | 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] | 144 | proc.getInstanceAttrib(InstanceAttribs::kDevBounds).fName, |
| 145 | proc.getInstanceAttrib(InstanceAttribs::kDevBounds).fName); |
| 146 | v->codeAppendf("refpt += N[0] * %f;", kAABloatRadius); // bloat for AA. |
| 147 | |
| 148 | // N[1] is the normal for the edge we are intersecting from the 45-degree bounding box, pointing |
| 149 | // out of the octagon. |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 150 | v->codeAppendf("float2 refpt45 = (N[1].x < 0) ? %s.xy : %s.zw;", |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 151 | proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).fName, |
| 152 | proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).fName); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 153 | 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] | 154 | v->codeAppendf("refpt45 += N[1] * %f;", kAABloatRadius); // bloat for AA. |
| 155 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 156 | v->codeAppend ("float2 K = float2(dot(N[0], refpt), dot(N[1], refpt45));"); |
| 157 | v->codeAppendf("float2 octocoord = K * inverse(N);"); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 158 | |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 159 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "octocoord"); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 160 | |
| 161 | // Convert to atlas coordinates in order to do our texture lookup. |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 162 | v->codeAppendf("float2 atlascoord = octocoord + float2(%s);", |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 163 | proc.getInstanceAttrib(InstanceAttribs::kAtlasOffset).fName); |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 164 | if (kTopLeft_GrSurfaceOrigin == proc.atlasProxy()->origin()) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 165 | v->codeAppendf("%s = atlascoord * %s;", texcoord.vsOut(), atlasAdjust); |
| 166 | } else { |
Robert Phillips | e44ef10 | 2017-07-21 15:37:19 -0400 | [diff] [blame] | 167 | SkASSERT(kBottomLeft_GrSurfaceOrigin == proc.atlasProxy()->origin()); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 168 | 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] | 169 | texcoord.vsOut(), atlasAdjust, atlasAdjust); |
| 170 | } |
| 171 | |
| 172 | // Convert to (local) path cordinates. |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 173 | v->codeAppendf("float2 pathcoord = inverse(float2x2(%s)) * (octocoord - %s);", |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 174 | proc.getInstanceAttrib(InstanceAttribs::kViewMatrix).fName, |
| 175 | proc.getInstanceAttrib(InstanceAttribs::kViewTranslate).fName); |
| 176 | |
| 177 | this->emitTransforms(v, varyingHandler, uniHandler, gpArgs->fPositionVar, "pathcoord", |
| 178 | args.fFPCoordTransformHandler); |
| 179 | |
| 180 | // Fragment shader. |
| 181 | GrGLSLPPFragmentBuilder* f = args.fFragBuilder; |
| 182 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 183 | f->codeAppend ("half coverage_count = "); |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 184 | f->appendTextureLookup(args.fTexSamplers[0], texcoord.fsIn(), kFloat2_GrSLType); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 185 | f->codeAppend (".a;"); |
| 186 | |
| 187 | if (SkPath::kWinding_FillType == proc.fillType()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 188 | f->codeAppendf("%s = half4(min(abs(coverage_count), 1));", args.fOutputCoverage); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 189 | } else { |
| 190 | SkASSERT(SkPath::kEvenOdd_FillType == proc.fillType()); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 191 | f->codeAppend ("half t = mod(abs(coverage_count), 2);"); |
| 192 | f->codeAppendf("%s = half4(1 - abs(t - 1));", args.fOutputCoverage); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | sk_sp<GrBuffer> GrCCPRPathProcessor::FindOrMakeIndexBuffer(GrOnFlushResourceProvider* onFlushRP) { |
| 197 | GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
| 198 | return onFlushRP->findOrMakeStaticBuffer(gIndexBufferKey, kIndex_GrBufferType, |
| 199 | sizeof(kOctoIndices), kOctoIndices); |
| 200 | } |
| 201 | |
| 202 | sk_sp<GrBuffer> GrCCPRPathProcessor::FindOrMakeVertexBuffer(GrOnFlushResourceProvider* onFlushRP) { |
| 203 | GR_DEFINE_STATIC_UNIQUE_KEY(gVertexBufferKey); |
| 204 | return onFlushRP->findOrMakeStaticBuffer(gVertexBufferKey, kVertex_GrBufferType, |
| 205 | sizeof(kOctoEdgeNorms), kOctoEdgeNorms); |
| 206 | } |