Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010 Google Inc. |
| 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 GrVertexWriter_DEFINED |
| 9 | #define GrVertexWriter_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/private/SkTemplates.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrColor.h" |
Michael Ludwig | fd4f4df | 2019-05-29 09:51:09 -0400 | [diff] [blame] | 13 | #include "src/gpu/geometry/GrQuad.h" |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 14 | #include <type_traits> |
| 15 | |
| 16 | /** |
| 17 | * Helper for writing vertex data to a buffer. Usage: |
| 18 | * GrVertexWriter vertices{target->makeVertexSpace(...)}; |
| 19 | * vertices.write(A0, B0, C0, ...); |
| 20 | * vertices.write(A1, B1, C1, ...); |
| 21 | * |
| 22 | * Supports any number of arguments. Each argument must be POD (plain old data), or an array |
| 23 | * thereof. |
| 24 | */ |
| 25 | struct GrVertexWriter { |
Brian Osman | 4486d98 | 2018-11-15 15:56:04 -0500 | [diff] [blame] | 26 | void* fPtr; |
| 27 | |
Chris Dalton | a43d583 | 2021-02-07 21:10:32 -0700 | [diff] [blame^] | 28 | GrVertexWriter() = default; |
| 29 | GrVertexWriter(void* ptr) : fPtr(ptr) {} |
| 30 | GrVertexWriter(const GrVertexWriter&) = delete; |
| 31 | GrVertexWriter(GrVertexWriter&& that) { *this = std::move(that); } |
| 32 | |
| 33 | GrVertexWriter& operator=(const GrVertexWriter&) = delete; |
| 34 | GrVertexWriter& operator=(GrVertexWriter&& that) { |
| 35 | fPtr = that.fPtr; |
| 36 | that.fPtr = nullptr; |
| 37 | return *this; |
| 38 | } |
| 39 | |
| 40 | bool operator==(const GrVertexWriter& that) { |
| 41 | return fPtr == that.fPtr; |
| 42 | } |
| 43 | |
| 44 | bool isValid() const { return fPtr != nullptr; } |
| 45 | |
| 46 | GrVertexWriter makeOffset(size_t offsetInBytes) const { |
| 47 | return {SkTAddOffset<void>(fPtr, offsetInBytes)}; |
| 48 | } |
| 49 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 50 | template <typename T> |
| 51 | class Conditional { |
| 52 | public: |
| 53 | explicit Conditional(bool condition, const T& value) |
| 54 | : fCondition(condition), fValue(value) {} |
| 55 | private: |
| 56 | friend struct GrVertexWriter; |
| 57 | |
| 58 | bool fCondition; |
| 59 | T fValue; |
| 60 | }; |
| 61 | |
| 62 | template <typename T> |
| 63 | static Conditional<T> If(bool condition, const T& value) { |
| 64 | return Conditional<T>(condition, value); |
| 65 | } |
| 66 | |
Brian Osman | d2fa2eb | 2018-12-26 16:48:40 -0500 | [diff] [blame] | 67 | template <typename T> |
| 68 | struct Skip {}; |
| 69 | |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 70 | template <typename T, typename... Args> |
| 71 | void write(const T& val, const Args&... remainder) { |
| 72 | static_assert(std::is_pod<T>::value, ""); |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 73 | // This assert is barely related to what we're trying to check - that our vertex data |
| 74 | // matches our attribute layouts, where each attribute is aligned to four bytes. If this |
| 75 | // becomes a problem, just remove it. |
| 76 | static_assert(alignof(T) <= 4, ""); |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 77 | memcpy(fPtr, &val, sizeof(T)); |
| 78 | fPtr = SkTAddOffset<void>(fPtr, sizeof(T)); |
| 79 | this->write(remainder...); |
| 80 | } |
| 81 | |
| 82 | template <typename T, size_t N, typename... Args> |
| 83 | void write(const T(&val)[N], const Args&... remainder) { |
| 84 | static_assert(std::is_pod<T>::value, ""); |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 85 | static_assert(alignof(T) <= 4, ""); |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 86 | memcpy(fPtr, val, N * sizeof(T)); |
| 87 | fPtr = SkTAddOffset<void>(fPtr, N * sizeof(T)); |
| 88 | this->write(remainder...); |
| 89 | } |
| 90 | |
Brian Osman | 5c8a6b3 | 2018-11-19 11:56:57 -0500 | [diff] [blame] | 91 | template <typename... Args> |
| 92 | void write(const GrVertexColor& color, const Args&... remainder) { |
| 93 | this->write(color.fColor[0]); |
| 94 | if (color.fWideColor) { |
| 95 | this->write(color.fColor[1]); |
Brian Osman | 2715bf5 | 2019-12-06 14:38:47 -0500 | [diff] [blame] | 96 | this->write(color.fColor[2]); |
| 97 | this->write(color.fColor[3]); |
Brian Osman | 5c8a6b3 | 2018-11-19 11:56:57 -0500 | [diff] [blame] | 98 | } |
| 99 | this->write(remainder...); |
| 100 | } |
| 101 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 102 | template <typename T, typename... Args> |
| 103 | void write(const Conditional<T>& val, const Args&... remainder) { |
| 104 | if (val.fCondition) { |
| 105 | this->write(val.fValue); |
| 106 | } |
| 107 | this->write(remainder...); |
| 108 | } |
| 109 | |
Brian Osman | d2fa2eb | 2018-12-26 16:48:40 -0500 | [diff] [blame] | 110 | template <typename T, typename... Args> |
| 111 | void write(const Skip<T>& val, const Args&... remainder) { |
| 112 | fPtr = SkTAddOffset<void>(fPtr, sizeof(T)); |
| 113 | this->write(remainder...); |
| 114 | } |
| 115 | |
Michael Ludwig | f995c05 | 2018-11-26 15:24:29 -0500 | [diff] [blame] | 116 | template <typename... Args> |
| 117 | void write(const Sk4f& vector, const Args&... remainder) { |
| 118 | float buffer[4]; |
| 119 | vector.store(buffer); |
| 120 | this->write<float, 4>(buffer); |
| 121 | this->write(remainder...); |
| 122 | } |
| 123 | |
Chris Dalton | a43d583 | 2021-02-07 21:10:32 -0700 | [diff] [blame^] | 124 | template <typename T> |
| 125 | void writeArray(const T* array, int count) { |
| 126 | static_assert(std::is_pod<T>::value, ""); |
| 127 | static_assert(alignof(T) <= 4, ""); |
| 128 | memcpy(fPtr, array, count * sizeof(T)); |
| 129 | fPtr = SkTAddOffset<void>(fPtr, count * sizeof(T)); |
| 130 | } |
| 131 | |
| 132 | template <typename T> |
| 133 | void fill(const T& val, int repeatCount) { |
| 134 | for (int i = 0; i < repeatCount; ++i) { |
| 135 | this->write(val); |
| 136 | } |
| 137 | } |
| 138 | |
Brian Osman | ffd11f4a | 2020-03-30 09:57:53 -0400 | [diff] [blame] | 139 | void writeRaw(const void* data, size_t size) { |
| 140 | memcpy(fPtr, data, size); |
| 141 | fPtr = SkTAddOffset<void>(fPtr, size); |
| 142 | } |
| 143 | |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 144 | void write() {} |
| 145 | |
Brian Osman | 4486d98 | 2018-11-15 15:56:04 -0500 | [diff] [blame] | 146 | /** |
| 147 | * Specialized utility for writing a four-vertices, with some data being replicated at each |
| 148 | * vertex, and other data being the appropriate 2-components from an SkRect to construct a |
| 149 | * triangle strip. |
| 150 | * |
| 151 | * writeQuad(A, B, C, ...) is similar to write(A, B, C, ...), except that: |
| 152 | * |
| 153 | * - Four sets of data will be written |
| 154 | * - For any arguments of type TriStrip, a unique SkPoint will be written at each vertex, |
| 155 | * in this order: left-top, left-bottom, right-top, right-bottom. |
| 156 | */ |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 157 | template <typename T> |
| 158 | struct TriStrip { T l, t, r, b; }; |
| 159 | |
| 160 | static TriStrip<float> TriStripFromRect(const SkRect& r) { |
| 161 | return { r.fLeft, r.fTop, r.fRight, r.fBottom }; |
| 162 | } |
Brian Osman | 4486d98 | 2018-11-15 15:56:04 -0500 | [diff] [blame] | 163 | |
Robert Phillips | 6d3bc29 | 2020-04-06 10:29:28 -0400 | [diff] [blame] | 164 | static TriStrip<uint16_t> TriStripFromUVs(const std::array<uint16_t, 4>& rect) { |
| 165 | return { rect[0], rect[1], rect[2], rect[3] }; |
| 166 | } |
| 167 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 168 | template <typename T> |
| 169 | struct TriFan { T l, t, r, b; }; |
| 170 | |
| 171 | static TriFan<float> TriFanFromRect(const SkRect& r) { |
| 172 | return { r.fLeft, r.fTop, r.fRight, r.fBottom }; |
| 173 | } |
| 174 | |
Brian Osman | 4486d98 | 2018-11-15 15:56:04 -0500 | [diff] [blame] | 175 | template <typename... Args> |
| 176 | void writeQuad(const Args&... remainder) { |
| 177 | this->writeQuadVert<0>(remainder...); |
| 178 | this->writeQuadVert<1>(remainder...); |
| 179 | this->writeQuadVert<2>(remainder...); |
| 180 | this->writeQuadVert<3>(remainder...); |
| 181 | } |
| 182 | |
| 183 | private: |
| 184 | template <int corner, typename T, typename... Args> |
| 185 | void writeQuadVert(const T& val, const Args&... remainder) { |
| 186 | this->writeQuadValue<corner>(val); |
| 187 | this->writeQuadVert<corner>(remainder...); |
| 188 | } |
| 189 | |
| 190 | template <int corner> |
| 191 | void writeQuadVert() {} |
| 192 | |
| 193 | template <int corner, typename T> |
| 194 | void writeQuadValue(const T& val) { |
| 195 | this->write(val); |
| 196 | } |
| 197 | |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 198 | template <int corner, typename T> |
| 199 | void writeQuadValue(const TriStrip<T>& r) { |
Brian Osman | 4486d98 | 2018-11-15 15:56:04 -0500 | [diff] [blame] | 200 | switch (corner) { |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 201 | case 0: this->write(r.l, r.t); break; |
| 202 | case 1: this->write(r.l, r.b); break; |
| 203 | case 2: this->write(r.r, r.t); break; |
| 204 | case 3: this->write(r.r, r.b); break; |
Brian Osman | 4486d98 | 2018-11-15 15:56:04 -0500 | [diff] [blame] | 205 | } |
| 206 | } |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 207 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 208 | template <int corner, typename T> |
| 209 | void writeQuadValue(const TriFan<T>& r) { |
| 210 | switch (corner) { |
| 211 | case 0: this->write(r.l, r.t); break; |
| 212 | case 1: this->write(r.l, r.b); break; |
| 213 | case 2: this->write(r.r, r.b); break; |
| 214 | case 3: this->write(r.r, r.t); break; |
| 215 | } |
| 216 | } |
| 217 | |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 218 | template <int corner> |
| 219 | void writeQuadValue(const GrQuad& q) { |
| 220 | this->write(q.point(corner)); |
| 221 | } |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | #endif |