Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 "src/gpu/tessellate/GrDrawAtlasPathOp.h" |
| 9 | |
| 10 | #include "src/gpu/GrOpFlushState.h" |
| 11 | #include "src/gpu/GrOpsRenderPass.h" |
| 12 | #include "src/gpu/GrProgramInfo.h" |
Robert Phillips | 1a82a4e | 2021-07-01 10:27:44 -0400 | [diff] [blame^] | 13 | #include "src/gpu/GrResourceProvider.h" |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 14 | #include "src/gpu/GrVertexWriter.h" |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 15 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 16 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 17 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 18 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
| 19 | |
| 20 | namespace { |
| 21 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 22 | class DrawAtlasPathShader : public GrGeometryProcessor { |
| 23 | public: |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 24 | DrawAtlasPathShader(const GrTextureProxy* atlasProxy, GrSwizzle swizzle, bool isInverseFill, |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 25 | bool usesLocalCoords, const GrShaderCaps& shaderCaps) |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 26 | : GrGeometryProcessor(kDrawAtlasPathShader_ClassID) |
| 27 | , fAtlasAccess(GrSamplerState::Filter::kNearest, atlasProxy->backendFormat(), swizzle) |
| 28 | , fAtlasDimensions(atlasProxy->backingStoreDimensions()) |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 29 | , fIsInverseFill(isInverseFill) |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 30 | , fUsesLocalCoords(usesLocalCoords) { |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 31 | fAttribs.emplace_back("dev_xywh", kInt4_GrVertexAttribType, kInt4_GrSLType); |
| 32 | fAttribs.emplace_back("atlas_xy", kInt2_GrVertexAttribType, kInt2_GrSLType); |
| 33 | fAttribs.emplace_back("color", kFloat4_GrVertexAttribType, kHalf4_GrSLType); |
| 34 | if (fIsInverseFill) { |
| 35 | fAttribs.emplace_back("drawbounds", kFloat4_GrVertexAttribType, kFloat4_GrSLType); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 36 | } |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 37 | if (fUsesLocalCoords) { |
| 38 | fAttribs.emplace_back("viewmatrix_scaleskew", kFloat4_GrVertexAttribType, |
| 39 | kFloat4_GrSLType); |
| 40 | fAttribs.emplace_back("viewmatrix_trans", kFloat2_GrVertexAttribType, kFloat2_GrSLType); |
| 41 | } |
| 42 | this->setInstanceAttributes(fAttribs.data(), fAttribs.count()); |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 43 | if (!shaderCaps.vertexIDSupport()) { |
| 44 | constexpr static Attribute kUnitCoordAttrib("unit_coord", kFloat2_GrVertexAttribType, |
| 45 | kFloat2_GrSLType); |
| 46 | this->setVertexAttributes(&kUnitCoordAttrib, 1); |
| 47 | } |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 48 | this->setTextureSamplerCnt(1); |
| 49 | } |
| 50 | |
| 51 | private: |
| 52 | const char* name() const override { return "DrawAtlasPathShader"; } |
| 53 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
| 54 | b->add32(fUsesLocalCoords); |
| 55 | } |
| 56 | const TextureSampler& onTextureSampler(int) const override { return fAtlasAccess; } |
Robert Phillips | f10535f | 2021-03-23 09:30:45 -0400 | [diff] [blame] | 57 | GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps&) const override; |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 58 | |
| 59 | const TextureSampler fAtlasAccess; |
| 60 | const SkISize fAtlasDimensions; |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 61 | const bool fIsInverseFill; |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 62 | const bool fUsesLocalCoords; |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 63 | SkSTArray<6, GrGeometryProcessor::Attribute> fAttribs; |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 64 | |
| 65 | class Impl; |
| 66 | }; |
| 67 | |
| 68 | class DrawAtlasPathShader::Impl : public GrGLSLGeometryProcessor { |
| 69 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 70 | const auto& shader = args.fGeomProc.cast<DrawAtlasPathShader>(); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 71 | args.fVaryingHandler->emitAttributes(shader); |
| 72 | |
| 73 | GrGLSLVarying atlasCoord(kFloat2_GrSLType); |
| 74 | args.fVaryingHandler->addVarying("atlascoord", &atlasCoord); |
| 75 | |
| 76 | GrGLSLVarying color(kHalf4_GrSLType); |
John Stiles | 4d7ac49 | 2021-03-09 20:16:43 -0500 | [diff] [blame] | 77 | args.fFragBuilder->codeAppendf("half4 %s;", args.fOutputColor); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 78 | args.fVaryingHandler->addPassThroughAttribute( |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 79 | shader.fAttribs[2], args.fOutputColor, |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 80 | GrGLSLVaryingHandler::Interpolation::kCanBeFlat); |
| 81 | |
| 82 | const char* atlasAdjust; |
| 83 | fAtlasAdjustUniform = args.fUniformHandler->addUniform( |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 84 | nullptr, kVertex_GrShaderFlag, kFloat2_GrSLType, "atlas_adjust", &atlasAdjust); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 85 | |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 86 | if (args.fShaderCaps->vertexIDSupport()) { |
| 87 | // If we don't have sk_VertexID support then "unit_coord" already came in as a vertex |
| 88 | // attrib. |
| 89 | args.fVertBuilder->codeAppendf(R"( |
| 90 | float2 unit_coord = float2(sk_VertexID & 1, sk_VertexID >> 1);)"); |
| 91 | } |
| 92 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 93 | args.fVertBuilder->codeAppendf(R"( |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 94 | float2 devtopleft = float2(dev_xywh.xy);)"); |
| 95 | |
| 96 | if (shader.fIsInverseFill) { |
| 97 | args.fVertBuilder->codeAppendf(R"( |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 98 | float2 devcoord = mix(drawbounds.xy, drawbounds.zw, unit_coord);)"); |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 99 | } else { |
| 100 | args.fVertBuilder->codeAppendf(R"( |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 101 | float2 devcoord = abs(float2(dev_xywh.zw)) * unit_coord + devtopleft;)"); |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | args.fVertBuilder->codeAppendf(R"( |
| 105 | float2 atlascoord = devcoord - devtopleft; |
| 106 | bool transposed = dev_xywh.w < 0; // Negative height means the path is transposed. |
| 107 | if (transposed) { |
| 108 | atlascoord = atlascoord.yx; |
| 109 | } |
| 110 | atlascoord += float2(atlas_xy); |
| 111 | %s = atlascoord * %s;)", |
| 112 | atlasCoord.vsOut(), atlasAdjust); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 113 | |
| 114 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "devcoord"); |
| 115 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 116 | if (shader.fUsesLocalCoords) { |
| 117 | args.fVertBuilder->codeAppendf(R"( |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 118 | float2x2 M = float2x2(viewmatrix_scaleskew); |
| 119 | float2 localcoord = inverse(M) * (devcoord - viewmatrix_trans);)"); |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 120 | gpArgs->fLocalCoordVar.set(kFloat2_GrSLType, "localcoord"); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 121 | } |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 122 | |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 123 | if (shader.fIsInverseFill) { |
| 124 | GrGLSLVarying atlasBounds(kFloat4_GrSLType); |
| 125 | args.fVaryingHandler->addVarying("atlasbounds", &atlasBounds, |
| 126 | GrGLSLVaryingHandler::Interpolation::kCanBeFlat); |
| 127 | args.fVertBuilder->codeAppendf(R"( |
| 128 | int2 atlas_wh = (transposed) ? abs(dev_xywh.wz) : dev_xywh.zw; |
| 129 | %s = float4(atlas_xy, atlas_xy + atlas_wh) * %s.xyxy;)", atlasBounds.vsOut(), |
| 130 | atlasAdjust); |
| 131 | |
| 132 | args.fFragBuilder->codeAppendf(R"( |
| 133 | half coverage = 0; |
| 134 | float2 atlascoord = %s; |
| 135 | float4 atlasbounds = %s; |
| 136 | if (all(greaterThan(atlascoord, atlasbounds.xy)) && |
| 137 | all(lessThan(atlascoord, atlasbounds.zw))) { |
| 138 | coverage = )", atlasCoord.fsIn(), atlasBounds.fsIn()); |
| 139 | args.fFragBuilder->appendTextureLookup(args.fTexSamplers[0], "atlascoord"); |
| 140 | args.fFragBuilder->codeAppendf(R"(.a; |
| 141 | } |
| 142 | half4 %s = half4(1 - coverage);)", args.fOutputCoverage); |
| 143 | } else { |
| 144 | args.fFragBuilder->codeAppendf("half4 %s = ", args.fOutputCoverage); |
| 145 | args.fFragBuilder->appendTextureLookup(args.fTexSamplers[0], atlasCoord.fsIn()); |
| 146 | args.fFragBuilder->codeAppendf(".aaaa;"); |
| 147 | } |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Brian Osman | 609f159 | 2020-07-01 15:14:39 -0400 | [diff] [blame] | 150 | void setData(const GrGLSLProgramDataManager& pdman, |
Brian Salomon | 5a32828 | 2021-04-14 10:32:25 -0400 | [diff] [blame] | 151 | const GrShaderCaps&, |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 152 | const GrGeometryProcessor& geomProc) override { |
| 153 | const SkISize& dimensions = geomProc.cast<DrawAtlasPathShader>().fAtlasDimensions; |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 154 | pdman.set2f(fAtlasAdjustUniform, 1.f / dimensions.width(), 1.f / dimensions.height()); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform; |
| 158 | }; |
| 159 | |
Robert Phillips | f10535f | 2021-03-23 09:30:45 -0400 | [diff] [blame] | 160 | GrGLSLGeometryProcessor* DrawAtlasPathShader::createGLSLInstance(const GrShaderCaps&) const { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 161 | return new Impl(); |
| 162 | } |
| 163 | |
| 164 | } // namespace |
| 165 | |
| 166 | GrProcessorSet::Analysis GrDrawAtlasPathOp::finalize(const GrCaps& caps, const GrAppliedClip* clip, |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 167 | GrClampType clampType) { |
| 168 | const GrProcessorSet::Analysis& analysis = fProcessors.finalize( |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 169 | fHeadInstance.fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip, |
| 170 | &GrUserStencilSettings::kUnused, caps, clampType, &fHeadInstance.fColor); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 171 | fUsesLocalCoords = analysis.usesLocalCoords(); |
| 172 | return analysis; |
| 173 | } |
| 174 | |
| 175 | GrOp::CombineResult GrDrawAtlasPathOp::onCombineIfPossible( |
Herb Derby | e25c300 | 2020-10-27 15:57:27 -0400 | [diff] [blame] | 176 | GrOp* op, SkArenaAlloc* alloc, const GrCaps&) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 177 | auto* that = op->cast<GrDrawAtlasPathOp>(); |
| 178 | SkASSERT(fAtlasProxy == that->fAtlasProxy); |
| 179 | SkASSERT(fEnableHWAA == that->fEnableHWAA); |
| 180 | |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 181 | if (fIsInverseFill != that->fIsInverseFill || fProcessors != that->fProcessors) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 182 | return CombineResult::kCannotCombine; |
| 183 | } |
| 184 | |
| 185 | SkASSERT(fUsesLocalCoords == that->fUsesLocalCoords); |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 186 | auto* copy = alloc->make<Instance>(that->fHeadInstance); |
| 187 | *fTailInstance = copy; |
| 188 | fTailInstance = (!copy->fNext) ? ©->fNext : that->fTailInstance; |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 189 | fInstanceCount += that->fInstanceCount; |
| 190 | return CombineResult::kMerged; |
| 191 | } |
| 192 | |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 193 | void GrDrawAtlasPathOp::onPrePrepare(GrRecordingContext*, const GrSurfaceProxyView& writeView, |
| 194 | GrAppliedClip*, const GrDstProxyView&, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 195 | GrXferBarrierFlags renderPassXferBarriers, |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 196 | GrLoadOp colorLoadOp) { |
| 197 | SK_ABORT("DDL support not implemented for GrDrawAtlasPathOp."); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 200 | GR_DECLARE_STATIC_UNIQUE_KEY(gUnitQuadBufferKey); |
| 201 | |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 202 | void GrDrawAtlasPathOp::onPrepare(GrOpFlushState* state) { |
| 203 | SkArenaAlloc* arena = state->allocator(); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 204 | |
| 205 | GrPipeline::InitArgs initArgs; |
| 206 | if (fEnableHWAA) { |
| 207 | initArgs.fInputFlags |= GrPipeline::InputFlags::kHWAntialias; |
| 208 | } |
| 209 | initArgs.fCaps = &state->caps(); |
| 210 | initArgs.fDstProxyView = state->drawOpArgs().dstProxyView(); |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 211 | initArgs.fWriteSwizzle = state->drawOpArgs().writeView().swizzle(); |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 212 | auto pipeline = arena->make<GrPipeline>(initArgs, std::move(fProcessors), |
| 213 | state->detachAppliedClip()); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 214 | GrSwizzle swizzle = state->caps().getReadSwizzle(fAtlasProxy->backendFormat(), |
| 215 | GrColorType::kAlpha_8); |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 216 | auto shader = arena->make<DrawAtlasPathShader>(fAtlasProxy.get(), swizzle, fIsInverseFill, |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 217 | fUsesLocalCoords, *state->caps().shaderCaps()); |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 218 | fProgram = arena->make<GrProgramInfo>(state->writeView(), pipeline, |
| 219 | &GrUserStencilSettings::kUnused, shader, |
| 220 | GrPrimitiveType::kTriangleStrip, 0, |
| 221 | state->renderPassBarriers(), state->colorLoadOp()); |
Chris Dalton | 012f849 | 2020-03-05 11:49:15 -0700 | [diff] [blame] | 222 | |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 223 | if (GrVertexWriter instanceWriter = state->makeVertexSpace( |
| 224 | shader->instanceStride(), fInstanceCount, &fInstanceBuffer, &fBaseInstance)) { |
| 225 | for (const Instance* instance = &fHeadInstance; instance; instance = instance->fNext) { |
| 226 | instanceWriter.write( |
| 227 | instance->fDevXYWH, |
| 228 | instance->fAtlasXY, |
| 229 | instance->fColor, |
| 230 | GrVertexWriter::If(fIsInverseFill, instance->fDrawBoundsIfInverseFilled), |
| 231 | GrVertexWriter::If(fUsesLocalCoords, instance->fViewMatrixIfUsingLocalCoords)); |
| 232 | } |
| 233 | } |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 234 | |
| 235 | if (!state->caps().shaderCaps()->vertexIDSupport()) { |
| 236 | constexpr static SkPoint kUnitQuad[4] = {{0,0}, {0,1}, {1,0}, {1,1}}; |
| 237 | |
| 238 | GR_DEFINE_STATIC_UNIQUE_KEY(gUnitQuadBufferKey); |
| 239 | |
| 240 | fVertexBufferIfNoIDSupport = state->resourceProvider()->findOrMakeStaticBuffer( |
| 241 | GrGpuBufferType::kVertex, sizeof(kUnitQuad), kUnitQuad, gUnitQuadBufferKey); |
| 242 | } |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 243 | } |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 244 | |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 245 | void GrDrawAtlasPathOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) { |
| 246 | SkASSERT(fAtlasProxy->isInstantiated()); |
| 247 | state->bindPipelineAndScissorClip(*fProgram, this->bounds()); |
| 248 | state->bindTextures(fProgram->geomProc(), *fAtlasProxy, fProgram->pipeline()); |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 249 | state->bindBuffers(nullptr, std::move(fInstanceBuffer), fVertexBufferIfNoIDSupport); |
Chris Dalton | aa0e45c | 2020-03-16 10:05:11 -0600 | [diff] [blame] | 250 | state->drawInstanced(fInstanceCount, fBaseInstance, 4, 0); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 251 | } |