| 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/Renderer.h" |
| 9 |
|
| 10 |
#include "src/gpu/graphite/DrawParams.h" |
| 11 |
#include "src/gpu/graphite/UniformManager.h" |
| 12 |
|
| 13 |
namespace skgpu::graphite { |
| 14 |
|
| 15 |
RenderStep::RenderStep(Layout layout, |
| 16 |
RenderStepID renderStepID, |
| 17 |
SkEnumBitMask<Flags> flags, |
| 18 |
std::initializer_list<Uniform> uniforms, |
| 19 |
PrimitiveType primitiveType, |
| 20 |
DepthStencilSettings depthStencilSettings, |
| 21 |
SkSpan<const Attribute> staticAttrs, |
| 22 |
SkSpan<const Attribute> appendAttrs, |
| 23 |
SkSpan<const Varying> varyings) |
| 24 |
: fRenderStepID(renderStepID) |
| 25 |
, fFlags(flags) |
| 26 |
, fPrimitiveType(primitiveType) |
| 27 |
, fDepthStencilSettings(depthStencilSettings) |
| 28 |
, fUniforms(uniforms) |
| 29 |
, fStaticAttrs(staticAttrs.begin(), staticAttrs.end()) |
| 30 |
, fAppendAttrs(appendAttrs.begin(), appendAttrs.end()) |
| 31 |
, fVaryings(varyings.begin(), varyings.end()) |
| 32 |
, fUniformAlignment(0) |
| 33 |
, fStaticDataStride(0) |
| 34 |
, fAppendDataStride(0) { |
| 35 |
for (auto v : this->staticAttributes()) { |
| 36 |
fStaticDataStride += v.sizeAlign4(); |
| 37 |
} |
| 38 |
for (auto i : this->appendAttributes()) { |
| 39 |
fAppendDataStride += i.sizeAlign4(); |
| 40 |
} |
| 41 |
|
| 42 |
UniformOffsetCalculator calculator = UniformOffsetCalculator::ForTopLevel(layout); |
| 43 |
for (const auto& u : fUniforms) { |
| 44 |
calculator.advanceOffset(u.type(), u.count()); |
| 45 |
} |
| 46 |
fUniformAlignment = calculator.requiredAlignment(); |
| 47 |
} |
| 48 |
|
| 49 |
std::optional<SkIRect> RenderStep::getScissor(const DrawParams& params, |
| 50 |
SkIRect currentScissor, |
| 51 |
SkIRect deviceBounds) const { |
| 52 |
if (currentScissor == params.scissor()) { |
| 53 |
return {}; // Trivially no change in scissor state is required |
| 54 |
} |
| 55 |
|
| 56 |
Rect drawBounds = params.drawBounds(); |
| 57 |
if (params.geometry().isShape() && params.geometry().shape().inverted()) { |
| 58 |
// For inverse filled shapes, the scissor is able to be handled in unique ways. |
| 59 |
if (fFlags & Flags::kInverseFillsScissor) { |
| 60 |
// In this case, the RenderStep geometrically respects the scissor so as long as the |
| 61 |
// current scissor doesn't interfere, we don't need a state change. |
| 62 |
if (currentScissor.contains(params.scissor())) { |
| 63 |
return {}; |
| 64 |
} else { |
| 65 |
// This draw doesn't need a scissor at all, so return the device bounds. It is |
| 66 |
// expected that this will generally lead to fewer scissor state changes (for |
| 67 |
// instance when applying the cover steps for a lot of inverse-filled intersect |
| 68 |
// clip depth-only draws). However, it could lead to a redundant scissor change if |
| 69 |
// the next draw would have used this draw's original scissor. |
| 70 |
return deviceBounds; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
if (fFlags & Flags::kIgnoreInverseFill) { |
| 75 |
// In this case params.drawBounds() fills the scissor from the inverse fill rule, |
| 76 |
// but we want to apply the scissor as if it were a regular fill. |
| 77 |
drawBounds = params.transformedShapeBounds(); // this ignores fill rule |
| 78 |
drawBounds.intersect(params.scissor()); |
| 79 |
} // Else leave drawBounds filling the original scissor |
| 80 |
|
| 81 |
// Fall through to regular scissor state checking with the possibly-updated bounds |
| 82 |
} |
| 83 |
|
| 84 |
// Draws that are unaffected by a clip stack will have a scissor matching the device's bounds. |
| 85 |
// If their transformed shape bounds clipped to the current scissor are no different than their |
| 86 |
// draw bounds (clipped to the original scissor), then no state change is required. |
| 87 |
Rect currentClippedBounds = params.transformedShapeBounds(); |
| 88 |
currentClippedBounds.intersect(currentScissor); |
| 89 |
if (currentClippedBounds == drawBounds) { |
| 90 |
return {}; |
| 91 |
} |
| 92 |
|
| 93 |
if (drawBounds == params.transformedShapeBounds()) { |
| 94 |
// We need to change the scissor, but the registered scissor is a no-op so |
| 95 |
// use the device bounds as a canonical scissor. |
| 96 |
return deviceBounds; |
| 97 |
} |
| 98 |
|
| 99 |
return params.scissor(); |
| 100 |
} |
| 101 |
|
| 102 |
Coverage RenderStep::GetCoverage(SkEnumBitMask<Flags> flags) { |
| 103 |
return !(flags & Flags::kEmitsCoverage) ? Coverage::kNone |
| 104 |
: (flags & Flags::kLCDCoverage) ? Coverage::kLCD |
| 105 |
: Coverage::kSingleChannel; |
| 106 |
} |
| 107 |
|
| 108 |
const char* RenderStep::RenderStepName(RenderStepID id) { |
| 109 |
#define CASE1(BaseName) case RenderStepID::k##BaseName: return #BaseName "RenderStep"; |
| 110 |
#define CASE2(BaseName, VariantName) \ |
| 111 |
case RenderStepID::k##BaseName##_##VariantName: return #BaseName "RenderStep[" #VariantName "]"; |
| 112 |
|
| 113 |
switch (id) { |
| 114 |
SKGPU_RENDERSTEP_TYPES(CASE1, CASE2) |
| 115 |
} |
| 116 |
#undef CASE1 |
| 117 |
#undef CASE2 |
| 118 |
|
| 119 |
SkUNREACHABLE; |
| 120 |
} |
| 121 |
|
| 122 |
bool RenderStep::IsValidRenderStepID(uint32_t renderStepID) { |
| 123 |
return renderStepID > (int) RenderStep::RenderStepID::kInvalid && |
| 124 |
renderStepID < RenderStep::kNumRenderSteps; |
| 125 |
} |
| 126 |
|
| 127 |
} // namespace skgpu::graphite |