blob: de52726ceaa9813dd9acfb8c2a98bb72a38cc86d [file] [log] [blame]
Chris Dalton1a325d22017-07-14 15:17:41 -06001/*
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 Dalton383a2ef2018-01-08 17:21:41 -05008#include "GrCCPathProcessor.h"
Chris Dalton1a325d22017-07-14 15:17:41 -06009
Chris Daltond925f2d2018-05-07 19:19:06 -060010#include "GrGpuCommandBuffer.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060011#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.
20constexpr 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.
26static 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
38GR_DECLARE_STATIC_UNIQUE_KEY(gVertexBufferKey);
39
Chris Dalton383a2ef2018-01-08 17:21:41 -050040sk_sp<const GrBuffer> GrCCPathProcessor::FindVertexBuffer(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton5d2de082017-12-19 10:40:23 -070041 GR_DEFINE_STATIC_UNIQUE_KEY(gVertexBufferKey);
42 return onFlushRP->findOrMakeStaticBuffer(kVertex_GrBufferType, sizeof(kOctoEdgeNorms),
43 kOctoEdgeNorms, gVertexBufferKey);
44}
45
Chris Dalton27059d32018-01-23 14:06:50 -070046static constexpr uint16_t kRestartStrip = 0xffff;
47
48static constexpr uint16_t kOctoIndicesAsStrips[] = {
49 1, 0, 2, 4, 3, kRestartStrip, // First half.
50 5, 4, 6, 0, 7 // Second half.
51};
52
53static constexpr uint16_t kOctoIndicesAsTris[] = {
54 // First half.
55 1, 0, 2,
Chris Dalton1a325d22017-07-14 15:17:41 -060056 0, 4, 2,
Chris Dalton1a325d22017-07-14 15:17:41 -060057 2, 4, 3,
Chris Dalton27059d32018-01-23 14:06:50 -070058
59 // Second half.
60 5, 4, 6,
61 4, 0, 6,
Chris Dalton1a325d22017-07-14 15:17:41 -060062 6, 0, 7,
63};
64
65GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey);
66
Chris Dalton383a2ef2018-01-08 17:21:41 -050067sk_sp<const GrBuffer> GrCCPathProcessor::FindIndexBuffer(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton5d2de082017-12-19 10:40:23 -070068 GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
Chris Dalton27059d32018-01-23 14:06:50 -070069 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 Dalton5d2de082017-12-19 10:40:23 -070076}
77
Chris Dalton27059d32018-01-23 14:06:50 -070078GrCCPathProcessor::GrCCPathProcessor(GrResourceProvider* resourceProvider,
Chris Daltondaef06a2018-05-23 17:11:09 -060079 sk_sp<GrTextureProxy> atlas,
Chris Dalton1c548942018-05-22 13:09:48 -060080 const SkMatrix& viewMatrixIfUsingLocalCoords)
Chris Dalton383a2ef2018-01-08 17:21:41 -050081 : INHERITED(kGrCCPathProcessor_ClassID)
Chris Daltona3e92712017-12-04 11:45:51 -070082 , fAtlasAccess(std::move(atlas), GrSamplerState::Filter::kNearest,
83 GrSamplerState::WrapMode::kClamp, kFragment_GrShaderFlag) {
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040084 this->addInstanceAttrib("devbounds", kFloat4_GrVertexAttribType);
85 this->addInstanceAttrib("devbounds45", kFloat4_GrVertexAttribType);
Chris Daltona045eea2017-10-24 13:22:10 -060086 this->addInstanceAttrib("atlas_offset", kShort2_GrVertexAttribType);
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040087 this->addInstanceAttrib("color", kUByte4_norm_GrVertexAttribType);
Chris Dalton1a325d22017-07-14 15:17:41 -060088
89 SkASSERT(offsetof(Instance, fDevBounds) ==
90 this->getInstanceAttrib(InstanceAttribs::kDevBounds).fOffsetInRecord);
91 SkASSERT(offsetof(Instance, fDevBounds45) ==
92 this->getInstanceAttrib(InstanceAttribs::kDevBounds45).fOffsetInRecord);
Chris Dalton1a325d22017-07-14 15:17:41 -060093 SkASSERT(offsetof(Instance, fAtlasOffset) ==
94 this->getInstanceAttrib(InstanceAttribs::kAtlasOffset).fOffsetInRecord);
95 SkASSERT(offsetof(Instance, fColor) ==
96 this->getInstanceAttrib(InstanceAttribs::kColor).fOffsetInRecord);
97 SkASSERT(sizeof(Instance) == this->getInstanceStride());
98
Chris Dalton1c548942018-05-22 13:09:48 -060099 GR_STATIC_ASSERT(4 == kNumInstanceAttribs);
Chris Dalton1a325d22017-07-14 15:17:41 -0600100
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400101 this->addVertexAttrib("edge_norms", kFloat4_GrVertexAttribType);
Chris Dalton1a325d22017-07-14 15:17:41 -0600102
Chris Dalton1c548942018-05-22 13:09:48 -0600103 if (resourceProvider->caps()->usePrimitiveRestart()) {
104 this->setWillUsePrimitiveRestart();
105 }
106
Chris Dalton27059d32018-01-23 14:06:50 -0700107 fAtlasAccess.instantiate(resourceProvider);
Chris Dalton1a325d22017-07-14 15:17:41 -0600108 this->addTextureSampler(&fAtlasAccess);
Chris Dalton27059d32018-01-23 14:06:50 -0700109
Chris Dalton1c548942018-05-22 13:09:48 -0600110 if (!viewMatrixIfUsingLocalCoords.invert(&fLocalMatrix)) {
111 fLocalMatrix.setIdentity();
Chris Dalton27059d32018-01-23 14:06:50 -0700112 }
Chris Dalton1a325d22017-07-14 15:17:41 -0600113}
114
Chris Dalton1a325d22017-07-14 15:17:41 -0600115class GLSLPathProcessor : public GrGLSLGeometryProcessor {
116public:
117 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override;
118
119private:
120 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc,
121 FPCoordTransformIter&& transformIter) override {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500122 const GrCCPathProcessor& proc = primProc.cast<GrCCPathProcessor>();
Chris Dalton1a325d22017-07-14 15:17:41 -0600123 pdman.set2f(fAtlasAdjustUniform, 1.0f / proc.atlas()->width(),
124 1.0f / proc.atlas()->height());
Chris Dalton1c548942018-05-22 13:09:48 -0600125 this->setTransformDataHelper(proc.localMatrix(), pdman, &transformIter);
Chris Dalton1a325d22017-07-14 15:17:41 -0600126 }
127
128 GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform;
129
130 typedef GrGLSLGeometryProcessor INHERITED;
131};
132
Chris Dalton383a2ef2018-01-08 17:21:41 -0500133GrGLSLPrimitiveProcessor* GrCCPathProcessor::createGLSLInstance(const GrShaderCaps&) const {
Chris Dalton1a325d22017-07-14 15:17:41 -0600134 return new GLSLPathProcessor();
135}
136
Chris Daltond925f2d2018-05-07 19:19:06 -0600137void GrCCPathProcessor::drawPaths(GrOpFlushState* flushState, const GrPipeline& pipeline,
138 const GrBuffer* indexBuffer, const GrBuffer* vertexBuffer,
139 GrBuffer* instanceBuffer, int baseInstance, int endInstance,
140 const SkRect& bounds) const {
141 const GrCaps& caps = flushState->caps();
142 GrPrimitiveType primitiveType = caps.usePrimitiveRestart()
143 ? GrPrimitiveType::kTriangleStrip
144 : GrPrimitiveType::kTriangles;
145 int numIndicesPerInstance = caps.usePrimitiveRestart()
146 ? SK_ARRAY_COUNT(kOctoIndicesAsStrips)
147 : SK_ARRAY_COUNT(kOctoIndicesAsTris);
148 GrMesh mesh(primitiveType);
149 mesh.setIndexedInstanced(indexBuffer, numIndicesPerInstance, instanceBuffer,
150 endInstance - baseInstance, baseInstance);
151 mesh.setVertexData(vertexBuffer);
152
153 flushState->rtCommandBuffer()->draw(pipeline, *this, &mesh, nullptr, 1, bounds);
154}
155
Chris Dalton1a325d22017-07-14 15:17:41 -0600156void GLSLPathProcessor::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
Chris Dalton383a2ef2018-01-08 17:21:41 -0500157 using InstanceAttribs = GrCCPathProcessor::InstanceAttribs;
Chris Dalton7b046312018-02-02 11:06:30 -0700158 using Interpolation = GrGLSLVaryingHandler::Interpolation;
159
Chris Dalton383a2ef2018-01-08 17:21:41 -0500160 const GrCCPathProcessor& proc = args.fGP.cast<GrCCPathProcessor>();
Chris Dalton1a325d22017-07-14 15:17:41 -0600161 GrGLSLUniformHandler* uniHandler = args.fUniformHandler;
162 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
163
164 const char* atlasAdjust;
165 fAtlasAdjustUniform = uniHandler->addUniform(
166 kVertex_GrShaderFlag,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400167 kFloat2_GrSLType, "atlas_adjust", &atlasAdjust);
Chris Dalton1a325d22017-07-14 15:17:41 -0600168
169 varyingHandler->emitAttributes(proc);
170
Chris Daltondaef06a2018-05-23 17:11:09 -0600171 GrGLSLVarying texcoord(kFloat3_GrSLType);
Chris Dalton27372882017-12-08 13:34:21 -0700172 GrGLSLVarying color(kHalf4_GrSLType);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600173 varyingHandler->addVarying("texcoord", &texcoord);
Chris Dalton7b046312018-02-02 11:06:30 -0700174 varyingHandler->addPassThroughAttribute(&proc.getInstanceAttrib(InstanceAttribs::kColor),
175 args.fOutputColor, Interpolation::kCanBeFlat);
Chris Dalton1a325d22017-07-14 15:17:41 -0600176
Chris Daltond0b8d932017-12-21 16:48:52 -0700177 // The vertex shader bloats and intersects the devBounds and devBounds45 rectangles, in order to
178 // find an octagon that circumscribes the (bloated) path.
Chris Dalton1a325d22017-07-14 15:17:41 -0600179 GrGLSLVertexBuilder* v = args.fVertBuilder;
180
Chris Daltond0b8d932017-12-21 16:48:52 -0700181 // Each vertex is the intersection of one edge from devBounds and one from devBounds45.
182 // 'N' holds the normals to these edges as column vectors.
183 //
184 // NOTE: "float2x2(float4)" is valid and equivalent to "float2x2(float4.xy, float4.zw)",
185 // however Intel compilers crash when we use the former syntax in this shader.
186 v->codeAppendf("float2x2 N = float2x2(%s.xy, %s.zw);",
187 proc.getEdgeNormsAttrib().fName, proc.getEdgeNormsAttrib().fName);
Chris Dalton1a325d22017-07-14 15:17:41 -0600188
189 // N[0] is the normal for the edge we are intersecting from the regular bounding box, pointing
190 // out of the octagon.
Chris Daltondaef06a2018-05-23 17:11:09 -0600191 v->codeAppendf("float4 devbounds = %s;",
Chris Dalton1a325d22017-07-14 15:17:41 -0600192 proc.getInstanceAttrib(InstanceAttribs::kDevBounds).fName);
Chris Daltondaef06a2018-05-23 17:11:09 -0600193 v->codeAppend ("float2 refpt = (0 == sk_VertexID >> 2)"
194 "? float2(min(devbounds.x, devbounds.z), devbounds.y)"
195 ": float2(max(devbounds.x, devbounds.z), devbounds.w);");
Chris Dalton1a325d22017-07-14 15:17:41 -0600196 v->codeAppendf("refpt += N[0] * %f;", kAABloatRadius); // bloat for AA.
197
198 // N[1] is the normal for the edge we are intersecting from the 45-degree bounding box, pointing
199 // out of the octagon.
Chris Dalton5cd67002018-04-30 11:04:40 -0600200 v->codeAppendf("float2 refpt45 = (0 == ((sk_VertexID + 1) & (1 << 2))) ? %s.xy : %s.zw;",
Chris Dalton1a325d22017-07-14 15:17:41 -0600201 proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).fName,
202 proc.getInstanceAttrib(InstanceAttribs::kDevBounds45).fName);
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400203 v->codeAppendf("refpt45 *= float2x2(.5,.5,-.5,.5);"); // transform back to device space.
Chris Dalton1a325d22017-07-14 15:17:41 -0600204 v->codeAppendf("refpt45 += N[1] * %f;", kAABloatRadius); // bloat for AA.
205
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400206 v->codeAppend ("float2 K = float2(dot(N[0], refpt), dot(N[1], refpt45));");
207 v->codeAppendf("float2 octocoord = K * inverse(N);");
Chris Dalton1a325d22017-07-14 15:17:41 -0600208
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400209 gpArgs->fPositionVar.set(kFloat2_GrSLType, "octocoord");
Chris Dalton1a325d22017-07-14 15:17:41 -0600210
211 // Convert to atlas coordinates in order to do our texture lookup.
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400212 v->codeAppendf("float2 atlascoord = octocoord + float2(%s);",
Chris Dalton1a325d22017-07-14 15:17:41 -0600213 proc.getInstanceAttrib(InstanceAttribs::kAtlasOffset).fName);
Robert Phillipse44ef102017-07-21 15:37:19 -0400214 if (kTopLeft_GrSurfaceOrigin == proc.atlasProxy()->origin()) {
Chris Daltondaef06a2018-05-23 17:11:09 -0600215 v->codeAppendf("%s.xy = atlascoord * %s;", texcoord.vsOut(), atlasAdjust);
Chris Dalton1a325d22017-07-14 15:17:41 -0600216 } else {
Robert Phillipse44ef102017-07-21 15:37:19 -0400217 SkASSERT(kBottomLeft_GrSurfaceOrigin == proc.atlasProxy()->origin());
Chris Daltondaef06a2018-05-23 17:11:09 -0600218 v->codeAppendf("%s.xy = float2(atlascoord.x * %s.x, 1 - atlascoord.y * %s.y);",
Chris Dalton1a325d22017-07-14 15:17:41 -0600219 texcoord.vsOut(), atlasAdjust, atlasAdjust);
220 }
Chris Daltondaef06a2018-05-23 17:11:09 -0600221 // The third texture coordinate is -.5 for even-odd paths and +.5 for winding ones.
222 // ("right < left" indicates even-odd fill type.)
223 v->codeAppendf("%s.z = sign(devbounds.z - devbounds.x) * .5;", texcoord.vsOut());
Chris Dalton1a325d22017-07-14 15:17:41 -0600224
Chris Dalton1c548942018-05-22 13:09:48 -0600225 this->emitTransforms(v, varyingHandler, uniHandler, GrShaderVar("octocoord", kFloat2_GrSLType),
226 proc.localMatrix(), args.fFPCoordTransformHandler);
Chris Dalton1a325d22017-07-14 15:17:41 -0600227
228 // Fragment shader.
Chris Dalton60283612018-02-14 13:38:14 -0700229 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
Chris Dalton1a325d22017-07-14 15:17:41 -0600230
Chris Daltondaef06a2018-05-23 17:11:09 -0600231 // Look up coverage count in the atlas.
232 f->codeAppend ("half coverage = ");
233 f->appendTextureLookup(args.fTexSamplers[0], SkStringPrintf("%s.xy", texcoord.fsIn()).c_str(),
234 kFloat2_GrSLType);
Chris Dalton1a325d22017-07-14 15:17:41 -0600235 f->codeAppend (".a;");
236
Chris Daltondaef06a2018-05-23 17:11:09 -0600237 // Scale coverage count by .5. Make it negative for even-odd paths and positive for winding
238 // ones. Clamp winding coverage counts at 1.0 (i.e. min(coverage/2, .5)).
239 f->codeAppendf("coverage = min(abs(coverage) * %s.z, .5);", texcoord.fsIn());
240
241 // For negative values, this finishes the even-odd sawtooth function. Since positive (winding)
242 // values were clamped at "coverage/2 = .5", this only undoes the previous multiply by .5.
243 f->codeAppend ("coverage = 1 - abs(fract(coverage) * 2 - 1);");
244
245 f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage);
Chris Dalton1a325d22017-07-14 15:17:41 -0600246}