| 1 |
/* |
| 2 |
* Copyright 2024 Google LLC |
| 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/graphite/render/AnalyticBlurRenderStep.h" |
| 9 |
|
| 10 |
#include "include/core/SkM44.h" |
| 11 |
#include "include/core/SkSamplingOptions.h" |
| 12 |
#include "include/core/SkTileMode.h" |
| 13 |
#include "include/private/base/SkDebug.h" |
| 14 |
#include "src/base/SkEnumBitMask.h" |
| 15 |
#include "src/core/SkSLTypeShared.h" |
| 16 |
#include "src/gpu/BufferWriter.h" |
| 17 |
#include "src/gpu/graphite/Attribute.h" |
| 18 |
#include "src/gpu/graphite/ContextUtils.h" |
| 19 |
#include "src/gpu/graphite/DrawOrder.h" |
| 20 |
#include "src/gpu/graphite/DrawParams.h" |
| 21 |
#include "src/gpu/graphite/DrawTypes.h" |
| 22 |
#include "src/gpu/graphite/DrawWriter.h" |
| 23 |
#include "src/gpu/graphite/PipelineData.h" |
| 24 |
#include "src/gpu/graphite/geom/AnalyticBlurMask.h" |
| 25 |
#include "src/gpu/graphite/geom/Geometry.h" |
| 26 |
#include "src/gpu/graphite/geom/Rect.h" |
| 27 |
#include "src/gpu/graphite/geom/Transform.h" |
| 28 |
#include "src/gpu/graphite/render/CommonDepthStencilSettings.h" |
| 29 |
|
| 30 |
namespace skgpu::graphite { |
| 31 |
|
| 32 |
AnalyticBlurRenderStep::AnalyticBlurRenderStep(Layout layout) |
| 33 |
: RenderStep(layout, |
| 34 |
RenderStepID::kAnalyticBlur, |
| 35 |
Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage | |
| 36 |
Flags::kAppendVertices, |
| 37 |
/*uniforms=*/ |
| 38 |
{{"localToDevice", SkSLType::kFloat4x4}, |
| 39 |
{"deviceToScaledShape", SkSLType::kFloat3x3}, |
| 40 |
{"shapeData", SkSLType::kFloat4}, |
| 41 |
{"blurData", SkSLType::kHalf2}, |
| 42 |
{"shapeType", SkSLType::kInt}, |
| 43 |
{"depth", SkSLType::kFloat}}, |
| 44 |
PrimitiveType::kTriangles, |
| 45 |
kDirectDepthLessPass, |
| 46 |
/*staticAttrs=*/ {}, |
| 47 |
/*appendAttrs=*/ |
| 48 |
{{{"position", VertexAttribType::kFloat2, SkSLType::kFloat2}, |
| 49 |
{"ssboIndex", VertexAttribType::kUInt, SkSLType::kUInt}}}, |
| 50 |
/*varyings=*/ |
| 51 |
// scaledShapeCoords are the fragment coordinates in local shape space, where |
| 52 |
// the shape has been scaled to device space but not translated or rotated. |
| 53 |
{{{"scaledShapeCoords", SkSLType::kFloat2}}}) {} |
| 54 |
|
| 55 |
std::string AnalyticBlurRenderStep::vertexSkSL() const { |
| 56 |
return |
| 57 |
"float4 devPosition = localToDevice * float4(position, depth, 1.0);\n" |
| 58 |
"stepLocalCoords = position;\n" |
| 59 |
"scaledShapeCoords = (deviceToScaledShape * devPosition.xy1).xy;\n"; |
| 60 |
} |
| 61 |
|
| 62 |
std::string AnalyticBlurRenderStep::texturesAndSamplersSkSL( |
| 63 |
const ResourceBindingRequirements& bindingReqs, int* nextBindingIndex) const { |
| 64 |
return EmitSamplerLayout(bindingReqs, nextBindingIndex) + " sampler2D s;"; |
| 65 |
} |
| 66 |
|
| 67 |
const char* AnalyticBlurRenderStep::fragmentCoverageSkSL() const { |
| 68 |
return "outputCoverage = blur_coverage_fn(scaledShapeCoords, " |
| 69 |
"shapeData, " |
| 70 |
"blurData, " |
| 71 |
"shapeType, " |
| 72 |
"s);"; |
| 73 |
} |
| 74 |
|
| 75 |
void AnalyticBlurRenderStep::writeVertices(DrawWriter* writer, |
| 76 |
const DrawParams& params, |
| 77 |
uint32_t ssboIndex) const { |
| 78 |
const Rect& r = params.geometry().analyticBlurMask().drawBounds(); |
| 79 |
DrawWriter::Vertices verts{*writer}; |
| 80 |
verts.append(6) << skvx::float2(r.left(), r.top()) << ssboIndex |
| 81 |
<< skvx::float2(r.right(), r.top()) << ssboIndex |
| 82 |
<< skvx::float2(r.left(), r.bot()) << ssboIndex |
| 83 |
<< skvx::float2(r.right(), r.top()) << ssboIndex |
| 84 |
<< skvx::float2(r.right(), r.bot()) << ssboIndex |
| 85 |
<< skvx::float2(r.left(), r.bot()) << ssboIndex; |
| 86 |
} |
| 87 |
|
| 88 |
void AnalyticBlurRenderStep::writeUniformsAndTextures(const DrawParams& params, |
| 89 |
PipelineDataGatherer* gatherer) const { |
| 90 |
SkDEBUGCODE(gatherer->checkRewind()); |
| 91 |
SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());) |
| 92 |
|
| 93 |
gatherer->write(params.transform().matrix()); |
| 94 |
|
| 95 |
const AnalyticBlurMask& blur = params.geometry().analyticBlurMask(); |
| 96 |
gatherer->write(blur.deviceToScaledShape().asM33()); |
| 97 |
gatherer->write(blur.shapeData().asSkRect()); |
| 98 |
gatherer->writeHalf(blur.blurData()); |
| 99 |
gatherer->write(static_cast<int>(blur.shapeType())); |
| 100 |
gatherer->write(params.order().depthAsFloat()); |
| 101 |
|
| 102 |
SkSamplingOptions samplingOptions = blur.shapeType() == AnalyticBlurMask::ShapeType::kRect |
| 103 |
? SkFilterMode::kLinear |
| 104 |
: SkFilterMode::kNearest; |
| 105 |
gatherer->add(blur.refProxy(), {samplingOptions, SkTileMode::kClamp}); |
| 106 |
} |
| 107 |
|
| 108 |
} // namespace skgpu::graphite |