1 /*
2 * Copyright 2022 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/CoverBoundsRenderStep.h"
9
10 #include "include/core/SkM44.h"
11 #include "src/base/SkEnumBitMask.h"
12 #include "src/base/SkVx.h"
13 #include "src/core/SkSLTypeShared.h"
14 #include "src/gpu/BufferWriter.h"
15 #include "src/gpu/graphite/Attribute.h"
16 #include "src/gpu/graphite/DrawOrder.h"
17 #include "src/gpu/graphite/DrawParams.h"
18 #include "src/gpu/graphite/DrawTypes.h"
19 #include "src/gpu/graphite/DrawWriter.h"
20 #include "src/gpu/graphite/geom/Geometry.h"
21 #include "src/gpu/graphite/geom/Rect.h"
22 #include "src/gpu/graphite/geom/Shape.h"
23 #include "src/gpu/graphite/geom/Transform_graphite.h"
24
25 #include <string_view>
26
27 namespace skgpu::graphite {
28
CoverBoundsRenderStep(const char * tag,DepthStencilSettings dsSettings)29 CoverBoundsRenderStep::CoverBoundsRenderStep(const char* tag, DepthStencilSettings dsSettings)
30 : RenderStep("CoverBoundsRenderStep",
31 tag,
32 Flags::kPerformsShading,
33 /*uniforms=*/{},
34 PrimitiveType::kTriangleStrip,
35 dsSettings,
36 /*vertexAttrs=*/ {},
37 /*instanceAttrs=*/{{"bounds", VertexAttribType::kFloat4, SkSLType::kFloat4},
38 {"depth", VertexAttribType::kFloat, SkSLType::kFloat},
39 {"ssboIndices", VertexAttribType::kUInt2, SkSLType::kUInt2},
40 {"mat0", VertexAttribType::kFloat3, SkSLType::kFloat3},
41 {"mat1", VertexAttribType::kFloat3, SkSLType::kFloat3},
42 {"mat2", VertexAttribType::kFloat3, SkSLType::kFloat3}}) {}
43
~CoverBoundsRenderStep()44 CoverBoundsRenderStep::~CoverBoundsRenderStep() {}
45
vertexSkSL() const46 std::string CoverBoundsRenderStep::vertexSkSL() const {
47 // Returns the body of a vertex function, which must define a float4 devPosition variable and
48 // must write to an already-defined float2 stepLocalCoords variable.
49 return "float4 devPosition = cover_bounds_vertex_fn("
50 "float2(sk_VertexID / 2, sk_VertexID % 2), "
51 "bounds, depth, float3x3(mat0, mat1, mat2), "
52 "stepLocalCoords);\n";
53 }
54
writeVertices(DrawWriter * writer,const DrawParams & params,skvx::uint2 ssboIndices) const55 void CoverBoundsRenderStep::writeVertices(DrawWriter* writer,
56 const DrawParams& params,
57 skvx::uint2 ssboIndices) const {
58 // Each instance is 4 vertices, forming 2 triangles from a single triangle strip, so no indices
59 // are needed. sk_VertexID is used to place vertex positions, so no vertex buffer is needed.
60 DrawWriter::Instances instances{*writer, {}, {}, 4};
61
62 skvx::float4 bounds;
63 if (params.geometry().isShape() && params.geometry().shape().inverted()) {
64 // Normally all bounding boxes are sorted such that l<r and t<b. We upload an inverted
65 // rectangle [r,b,l,t] when it's an inverse fill to encode that the bounds are already in
66 // device space and then the VS will use the inverse of the transform to compute local
67 // coordinates.
68 bounds = skvx::shuffle</*R*/2, /*B*/3, /*L*/0, /*T*/1>(
69 skvx::cast<float>(skvx::int4::Load(¶ms.clip().scissor())));
70 } else {
71 bounds = params.geometry().bounds().ltrb();
72 }
73
74 // Since the local coords always have Z=0, we can discard the 3rd row and column of the matrix.
75 const SkM44& m = params.transform().matrix();
76 instances.append(1) << bounds << params.order().depthAsFloat() << ssboIndices
77 << m.rc(0,0) << m.rc(1,0) << m.rc(3,0)
78 << m.rc(0,1) << m.rc(1,1) << m.rc(3,1)
79 << m.rc(0,3) << m.rc(1,3) << m.rc(3,3);
80 }
81
writeUniformsAndTextures(const DrawParams &,PipelineDataGatherer *) const82 void CoverBoundsRenderStep::writeUniformsAndTextures(const DrawParams&,
83 PipelineDataGatherer*) const {
84 // All data is uploaded as instance attributes, so no uniforms are needed.
85 }
86
87 } // namespace skgpu::graphite
88