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