| 1 |
/* |
| 2 |
* Copyright 2021 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 |
#ifndef skgpu_graphite_DrawOrder_DEFINED |
| 9 |
#define skgpu_graphite_DrawOrder_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkTypes.h" |
| 12 |
|
| 13 |
namespace skgpu::graphite { |
| 14 |
|
| 15 |
// Helper to encapsulate an unsigned number and enforce that it can only be used to create a |
| 16 |
// monotonic sequence. The template argument 'Sequence' is used to define different sequences |
| 17 |
// enforced by the compiler. For simplicity, and current needs within Graphite, it's assumed the |
| 18 |
// entire sequence can be indexed by uint16_t. |
| 19 |
template<typename Sequence> |
| 20 |
class MonotonicValue { |
| 21 |
public: |
| 22 |
static constexpr MonotonicValue First() { return 0; } |
| 23 |
static constexpr MonotonicValue Last() { return 0xffff; } |
| 24 |
|
| 25 |
MonotonicValue() = default; |
| 26 |
MonotonicValue(const MonotonicValue& o) = default; |
| 27 |
|
| 28 |
MonotonicValue& operator=(const MonotonicValue& o) = default; |
| 29 |
|
| 30 |
bool operator< (MonotonicValue o) const { return fIndex < o.fIndex; } |
| 31 |
bool operator<=(MonotonicValue o) const { return fIndex <= o.fIndex; } |
| 32 |
|
| 33 |
bool operator> (MonotonicValue o) const { return fIndex > o.fIndex; } |
| 34 |
bool operator>=(MonotonicValue o) const { return fIndex >= o.fIndex; } |
| 35 |
|
| 36 |
bool operator==(MonotonicValue o) const { return fIndex == o.fIndex; } |
| 37 |
bool operator!=(MonotonicValue o) const { return fIndex != o.fIndex; } |
| 38 |
|
| 39 |
uint16_t bits() const { return fIndex; } |
| 40 |
|
| 41 |
// Get the next value in the sequence after this one |
| 42 |
MonotonicValue next() const { return fIndex + 1; } |
| 43 |
|
| 44 |
private: |
| 45 |
friend class DrawOrder; // For depth/stencil co-opting |
| 46 |
|
| 47 |
constexpr MonotonicValue(uint16_t index) : fIndex(index) {} |
| 48 |
|
| 49 |
uint16_t fIndex; |
| 50 |
}; |
| 51 |
|
| 52 |
/** |
| 53 |
* CompressedPaintersOrder is an ordinal number that allows draw commands to be re-ordered so long |
| 54 |
* as when they are executed, the read/writes to the color|depth attachments respect the original |
| 55 |
* painter's order. Logical draws with the same CompressedPaintersOrder can be assumed to be |
| 56 |
* executed in any order, however that may have been determined (e.g. BoundsManager or relying on |
| 57 |
* a depth test during rasterization). |
| 58 |
*/ |
| 59 |
struct CompressedPaintersOrderSequence {}; |
| 60 |
using CompressedPaintersOrder = MonotonicValue<CompressedPaintersOrderSequence>; |
| 61 |
|
| 62 |
/** |
| 63 |
* Each DisjointStencilIndex specifies an implicit set of non-overlapping draws. Assuming that two |
| 64 |
* draws have the same CompressedPaintersOrder and the same DisjointStencilIndex, their substeps |
| 65 |
* for multi-pass rendering (stencil-then-cover, etc.) can be intermingled with each other and |
| 66 |
* produce the same results as if each draw's substeps were executed in order before moving on to |
| 67 |
* the next draw's. |
| 68 |
* |
| 69 |
* Ordering within a set can be entirely arbitrary (i.e. all stencil steps can go before all cover |
| 70 |
* steps). Ordering between sets is also arbitrary since all draws share the same |
| 71 |
* CompressedPaintersOrder, so long as one set is entirely drawn before the next. |
| 72 |
* |
| 73 |
* Two draws that have different CompressedPaintersOrders but the same DisjointStencilIndex are |
| 74 |
* unrelated, they may or may not overlap. The painters order scopes the disjoint sets. |
| 75 |
*/ |
| 76 |
struct DisjointStencilIndexSequence {}; |
| 77 |
using DisjointStencilIndex = MonotonicValue<DisjointStencilIndexSequence>; |
| 78 |
|
| 79 |
/** |
| 80 |
* Every draw has an associated depth value. The value is constant across the entire draw and is |
| 81 |
* not related to any varying Z coordinate induced by a 4x4 transform. The painter's depth is stored |
| 82 |
* in the depth attachment and the GREATER depth test is used to reject or accept pixels/samples |
| 83 |
* relative to what has already been rendered into the depth attachment. This allows draws that do |
| 84 |
* not depend on the previous color to be radically re-ordered relative to their original painter's |
| 85 |
* order while producing correct results. |
| 86 |
*/ |
| 87 |
struct PaintersDepthSequence {}; |
| 88 |
using PaintersDepth = MonotonicValue<PaintersDepthSequence>; |
| 89 |
|
| 90 |
/** |
| 91 |
* DrawOrder aggregates the three separate sequences that Graphite uses to re-order draws and their |
| 92 |
* substeps as much as possible while preserving the painter's order semantics of the Skia API. |
| 93 |
* |
| 94 |
* To build the full DrawOrder for a draw, start with its assigned PaintersDepth (i.e. the original |
| 95 |
* painter's order of the draw call). From there, the DrawOrder can be updated to reflect |
| 96 |
* dependencies on previous draws, either from depth-only clip draws or because the draw is |
| 97 |
* transparent and must blend with the previous color values. Lastly, once the |
| 98 |
* CompressedPaintersOrder is finalized, the DrawOrder can be updated to reflect whether or not |
| 99 |
* the draw will involve the stencil buffer--and if so, specify the disjoint stencil set it |
| 100 |
* belongs to. |
| 101 |
* |
| 102 |
* The original and effective order that draws are executed in is defined by the PaintersDepth. |
| 103 |
* However, the actual execution order is defined by first the CompressedPaintersOrder and then |
| 104 |
* the DisjointStencilIndex. This means that draws with much higher depths can be executed earlier |
| 105 |
* if painter's order compression allows for it. |
| 106 |
*/ |
| 107 |
class DrawOrder { |
| 108 |
public: |
| 109 |
// The first PaintersDepth is reserved for clearing the depth attachment; any draw using this |
| 110 |
// depth will always fail the depth test. |
| 111 |
inline static constexpr PaintersDepth kClearDepth = PaintersDepth::First(); |
| 112 |
// The first CompressedPaintersOrder is reserved to indicate there is no previous draw that |
| 113 |
// must come before a draw. |
| 114 |
inline static constexpr |
| 115 |
CompressedPaintersOrder kNoIntersection = CompressedPaintersOrder::First(); |
| 116 |
// The last DisjointStencilIndex is reserved to indicate an unassigned stencil set. |
| 117 |
// This automatically orders stencil-less B2F draws after draws that use the stencil and after |
| 118 |
// draws that are sorted F2B (packed into stencil index with reverseDepthAsStencil()). |
| 119 |
inline static constexpr DisjointStencilIndex kUnassigned = DisjointStencilIndex::Last(); |
| 120 |
|
| 121 |
explicit DrawOrder(PaintersDepth originalOrder) |
| 122 |
: fPaintOrder(kNoIntersection) |
| 123 |
, fStencilIndex(kUnassigned) |
| 124 |
, fDepth(originalOrder) {} |
| 125 |
|
| 126 |
DrawOrder(PaintersDepth originalOrder, CompressedPaintersOrder compressedOrder) |
| 127 |
: fPaintOrder(compressedOrder) |
| 128 |
, fStencilIndex(kUnassigned) |
| 129 |
, fDepth(originalOrder) {} |
| 130 |
|
| 131 |
CompressedPaintersOrder paintOrder() const { return fPaintOrder; } |
| 132 |
DisjointStencilIndex stencilIndex() const { return fStencilIndex; } |
| 133 |
PaintersDepth depth() const { return fDepth; } |
| 134 |
|
| 135 |
// While the PaintersDepth is a monotonically increasing value, the depth buffer prefers to |
| 136 |
// use LESS and LEQUAL comparisons starting with a clear value of 1.f, so we normalize and flip |
| 137 |
// the floating point value to count down from 1.0. |
| 138 |
float depthAsFloat() const { |
| 139 |
return 1.f - fDepth.bits() / (float) PaintersDepth::Last().bits(); |
| 140 |
} |
| 141 |
|
| 142 |
// Coopt the stencil index to encode the draw's actual painter's depth in decreasing order, |
| 143 |
// for use enforcing F2B order (since the compressed painter's order handles B2F). |
| 144 |
DrawOrder& reverseDepthAsStencil() { |
| 145 |
SkASSERT(fStencilIndex == kUnassigned); // can't have a real stencil index |
| 146 |
fStencilIndex = DisjointStencilIndex::Last().bits() - fDepth.bits(); |
| 147 |
return *this; |
| 148 |
} |
| 149 |
|
| 150 |
DrawOrder& dependsOnPaintersOrder(CompressedPaintersOrder prevDraw) { |
| 151 |
// A draw must be ordered after all previous draws that it depends on |
| 152 |
CompressedPaintersOrder next = prevDraw.next(); |
| 153 |
if (fPaintOrder < next) { |
| 154 |
fPaintOrder = next; |
| 155 |
} |
| 156 |
return *this; |
| 157 |
} |
| 158 |
|
| 159 |
DrawOrder& dependsOnStencil(DisjointStencilIndex disjointSet) { |
| 160 |
// Stencil usage should only be set once |
| 161 |
SkASSERT(fStencilIndex == kUnassigned); |
| 162 |
fStencilIndex = disjointSet; |
| 163 |
return *this; |
| 164 |
} |
| 165 |
|
| 166 |
private: |
| 167 |
CompressedPaintersOrder fPaintOrder; |
| 168 |
DisjointStencilIndex fStencilIndex; |
| 169 |
PaintersDepth fDepth; |
| 170 |
}; |
| 171 |
|
| 172 |
} // namespace skgpu::graphite |
| 173 |
|
| 174 |
#endif // skgpu_graphite_DrawOrder_DEFINED |