| 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 |
#ifndef skgpu_tessellate_FixedCountBufferUtils_DEFINED |
| 9 |
#define skgpu_tessellate_FixedCountBufferUtils_DEFINED |
| 10 |
|
| 11 |
#include "src/gpu/tessellate/LinearTolerances.h" |
| 12 |
#include "src/gpu/tessellate/Tessellation.h" |
| 13 |
|
| 14 |
#include <algorithm> |
| 15 |
#include <cstddef> |
| 16 |
#include <cstdint> |
| 17 |
#include <limits> |
| 18 |
|
| 19 |
namespace skgpu { struct VertexWriter; } |
| 20 |
|
| 21 |
namespace skgpu::tess { |
| 22 |
|
| 23 |
/** |
| 24 |
* Fixed-count tessellation operates in three modes, two for filling paths, and one for stroking. |
| 25 |
* These modes may have additional sub-variations, but in terms of vertex buffer management, these |
| 26 |
* three categories are sufficient: |
| 27 |
* |
| 28 |
* - FixedCountCurves: for filling paths where just the curves are tessellated. Additional measures |
| 29 |
* to fill space between the inner control points of the paths are needed. |
| 30 |
* - FixedCountWedges: for filling paths by tessellating the curves and adding an additional inline |
| 31 |
* triangle with a shared vertex that all verbs connect to. Works with PatchAttribs::kFanPoint. |
| 32 |
* - FixedCountStrokes: for stroking a path. Likely paired with PatchAttribs::kJoinControlPoint and |
| 33 |
* PatchAttribs::kStrokeParams. |
| 34 |
* |
| 35 |
* The three types defined below for these three modes provide utility functions for heuristics to |
| 36 |
* choose pre-allocation size when accumulating instance attributes with a PatchWriter, and |
| 37 |
* functions for creating static/GPU-private vertex and index buffers that are used as the template |
| 38 |
* for instanced rendering. |
| 39 |
*/ |
| 40 |
class FixedCountCurves { |
| 41 |
FixedCountCurves() = delete; |
| 42 |
public: |
| 43 |
// A heuristic function for reserving instance attribute space before using a PatchWriter. |
| 44 |
static constexpr int PreallocCount(int totalCombinedPathVerbCnt) { |
| 45 |
// Over-allocate enough curves for 1 in 4 to chop. Every chop introduces 2 new patches: |
| 46 |
// another curve patch and a triangle patch that glues the two chops together, |
| 47 |
// i.e. + 2 * ((count + 3) / 4) == (count + 3) / 2 |
| 48 |
constexpr int kMaxVerbCount = std::numeric_limits<int>::max() >> 2; |
| 49 |
totalCombinedPathVerbCnt = std::min(kMaxVerbCount, totalCombinedPathVerbCnt); |
| 50 |
return totalCombinedPathVerbCnt + (totalCombinedPathVerbCnt + 3) / 2; |
| 51 |
} |
| 52 |
|
| 53 |
// Convert the accumulated worst-case tolerances into an index count passed into an instanced, |
| 54 |
// indexed draw function that uses FixedCountCurves static vertex and index buffers. |
| 55 |
static int VertexCount(const LinearTolerances& tolerances) { |
| 56 |
// We should already chopped curves to make sure none needed a higher resolveLevel than |
| 57 |
// kMaxResolveLevel. |
| 58 |
int resolveLevel = std::min(tolerances.requiredResolveLevel(), kMaxResolveLevel); |
| 59 |
return NumCurveTrianglesAtResolveLevel(resolveLevel) * 3; |
| 60 |
} |
| 61 |
|
| 62 |
static constexpr size_t VertexBufferVertexCount() { |
| 63 |
return kMaxParametricSegments + 1; |
| 64 |
} |
| 65 |
|
| 66 |
static constexpr size_t VertexBufferStride() { |
| 67 |
return 2 * sizeof(float); |
| 68 |
} |
| 69 |
|
| 70 |
// Return the number of bytes to allocate for a buffer filled via WriteVertexBuffer, assuming |
| 71 |
// the shader and curve instances do require more than kMaxParametricSegments segments. |
| 72 |
static constexpr size_t VertexBufferSize() { |
| 73 |
return VertexBufferVertexCount() * VertexBufferStride(); |
| 74 |
} |
| 75 |
|
| 76 |
// As above but for the corresponding index buffer, written via WriteIndexBuffer. |
| 77 |
static constexpr size_t IndexBufferSize() { |
| 78 |
return NumCurveTrianglesAtResolveLevel(kMaxResolveLevel) * 3 * sizeof(uint16_t); |
| 79 |
} |
| 80 |
|
| 81 |
static void WriteVertexBuffer(VertexWriter, size_t bufferSize); |
| 82 |
|
| 83 |
static void WriteIndexBuffer(VertexWriter, size_t bufferSize); |
| 84 |
}; |
| 85 |
|
| 86 |
class FixedCountWedges { |
| 87 |
FixedCountWedges() = delete; |
| 88 |
public: |
| 89 |
// These functions provide equivalent functionality to the matching ones in FixedCountCurves, |
| 90 |
// but are intended for use with a shader and PatchWriter that has enabled the kFanPoint attrib. |
| 91 |
|
| 92 |
static constexpr int PreallocCount(int totalCombinedPathVerbCnt) { |
| 93 |
// Over-allocate enough wedges for 1 in 4 to chop, i.e., ceil(maxWedges * 5/4) |
| 94 |
constexpr int kMaxVerbCount = std::numeric_limits<int>::max() >> 3; |
| 95 |
totalCombinedPathVerbCnt = std::min(kMaxVerbCount, totalCombinedPathVerbCnt); |
| 96 |
return (totalCombinedPathVerbCnt * 5 + 3) / 4; |
| 97 |
} |
| 98 |
|
| 99 |
static int VertexCount(const LinearTolerances& tolerances) { |
| 100 |
// Emit 3 vertices per curve triangle, plus 3 more for the wedge fan triangle. |
| 101 |
int resolveLevel = std::min(tolerances.requiredResolveLevel(), kMaxResolveLevel); |
| 102 |
return (NumCurveTrianglesAtResolveLevel(resolveLevel) + 1) * 3; |
| 103 |
} |
| 104 |
|
| 105 |
static constexpr size_t VertexBufferVertexCount() { |
| 106 |
return (kMaxParametricSegments + 1) + 1/*fan vertex*/; |
| 107 |
} |
| 108 |
|
| 109 |
static constexpr size_t VertexBufferStride() { |
| 110 |
return 2 * sizeof(float); |
| 111 |
} |
| 112 |
|
| 113 |
static constexpr size_t VertexBufferSize() { |
| 114 |
return VertexBufferVertexCount() * VertexBufferStride(); |
| 115 |
} |
| 116 |
|
| 117 |
static constexpr size_t IndexBufferSize() { |
| 118 |
return (NumCurveTrianglesAtResolveLevel(kMaxResolveLevel) + 1/*fan triangle*/) * |
| 119 |
3 * sizeof(uint16_t); |
| 120 |
} |
| 121 |
|
| 122 |
static void WriteVertexBuffer(VertexWriter, size_t bufferSize); |
| 123 |
|
| 124 |
static void WriteIndexBuffer(VertexWriter, size_t bufferSize); |
| 125 |
}; |
| 126 |
|
| 127 |
class FixedCountStrokes { |
| 128 |
FixedCountStrokes() = delete; |
| 129 |
public: |
| 130 |
// These functions provide equivalent functionality to the matching ones in FixedCountCurves, |
| 131 |
// but are intended for a shader that that strokes a path instead of filling, where vertices |
| 132 |
// are associated with joins, caps, radial segments, or parametric segments. |
| 133 |
// |
| 134 |
// NOTE: The fixed-count stroke buffer is only needed when vertex IDs are not available as an |
| 135 |
// SkSL built-in. And unlike the curve and wedge variants, stroke drawing never relies on an |
| 136 |
// index buffer so those functions are not provided. |
| 137 |
|
| 138 |
// Don't draw more vertices than can be indexed by a signed short. We just have to draw the line |
| 139 |
// somewhere and this seems reasonable enough. (There are two vertices per edge, so 2^14 edges |
| 140 |
// make 2^15 vertices.) |
| 141 |
static constexpr int kMaxEdges = (1 << 14) - 1; |
| 142 |
static constexpr int kMaxEdgesNoVertexIDs = 1024; |
| 143 |
|
| 144 |
static constexpr int PreallocCount(int totalCombinedPathVerbCnt) { |
| 145 |
// Over-allocate enough patches for each stroke to chop once, and for 8 extra caps. Since |
| 146 |
// we have to chop at inflections, points of 180 degree rotation, and anywhere a stroke |
| 147 |
// requires too many parametric segments, many strokes will end up getting choppped. |
| 148 |
constexpr int kMaxVerbCount = std::numeric_limits<int>::max() >> 2; |
| 149 |
totalCombinedPathVerbCnt = std::min(kMaxVerbCount, totalCombinedPathVerbCnt); |
| 150 |
return (totalCombinedPathVerbCnt * 2) + 8/* caps */; |
| 151 |
} |
| 152 |
|
| 153 |
// Does not account for falling back to kMaxEdgesNoVertexIDs |
| 154 |
static int VertexCount(const LinearTolerances& tolerances) { |
| 155 |
return std::min(tolerances.requiredStrokeEdges(), kMaxEdges) * 2; |
| 156 |
} |
| 157 |
|
| 158 |
static constexpr size_t VertexBufferSize() { |
| 159 |
// Each vertex is a single float (explicit id) and each edge is composed of two vertices. |
| 160 |
return 2 * kMaxEdgesNoVertexIDs * sizeof(float); |
| 161 |
} |
| 162 |
|
| 163 |
// Initializes the fallback vertex buffer that should be bound when sk_VertexID is not supported |
| 164 |
static void WriteVertexBuffer(VertexWriter, size_t bufferSize); |
| 165 |
}; |
| 166 |
|
| 167 |
} // namespace skgpu::tess |
| 168 |
|
| 169 |
#endif // skgpu_tessellate_FixedCountBufferUtils |