| 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 |
#include "src/gpu/graphite/UniformManager.h" |
| 9 |
|
| 10 |
#include "src/gpu/graphite/PipelineData.h" |
| 11 |
|
| 12 |
// ensure that these types are the sizes the uniform data is expecting |
| 13 |
static_assert(sizeof(int32_t) == 4); |
| 14 |
static_assert(sizeof(float) == 4); |
| 15 |
static_assert(sizeof(SkHalf) == 2); |
| 16 |
|
| 17 |
namespace skgpu::graphite { |
| 18 |
|
| 19 |
int UniformOffsetCalculator::advanceOffset(SkSLType type, int count) { |
| 20 |
SkASSERT(SkSLTypeCanBeUniformValue(type)); |
| 21 |
|
| 22 |
int dimension = SkSLTypeMatrixSize(type); |
| 23 |
if (dimension > 0) { |
| 24 |
// All SkSL matrices are square and can be interpreted as an array of column vectors |
| 25 |
count = std::max(count, 1) * dimension; |
| 26 |
} else { |
| 27 |
dimension = SkSLTypeVecLength(type); |
| 28 |
} |
| 29 |
SkASSERT(1 <= dimension && dimension <= 4); |
| 30 |
|
| 31 |
// Bump dimension up to 4 if the array or vec3 consumes 4 primitives per element |
| 32 |
// NOTE: This affects the size, alignment already rounds up to a power of 2 automatically. |
| 33 |
const bool isArray = count > Uniform::kNonArray; |
| 34 |
const bool forceAlign16 = isArray && LayoutRules::AlignArraysTo16(fLayout); |
| 35 |
if (forceAlign16 || (dimension == 3 && (isArray || LayoutRules::PadVec3Size(fLayout)))) { |
| 36 |
dimension = 4; |
| 37 |
} |
| 38 |
|
| 39 |
const int primitiveSize = LayoutRules::UseFullPrecision(fLayout) || |
| 40 |
SkSLTypeIsFullPrecisionNumericType(type) || |
| 41 |
forceAlign16 ? 4 : 2; |
| 42 |
const int align = SkNextPow2(dimension) * primitiveSize; |
| 43 |
const int alignedOffset = SkAlignTo(fOffset, align); |
| 44 |
fOffset = alignedOffset + dimension * primitiveSize * std::max(count, 1); |
| 45 |
fReqAlignment = std::max(fReqAlignment, align); |
| 46 |
|
| 47 |
return alignedOffset; |
| 48 |
} |
| 49 |
|
| 50 |
int UniformOffsetCalculator::advanceStruct(const UniformOffsetCalculator& substruct, int count) { |
| 51 |
SkASSERT(substruct.fLayout == fLayout); // Invalid if the layout rules used aren't consistent |
| 52 |
|
| 53 |
// If array element strides are forced to 16-byte alignment, structs must also have their |
| 54 |
// base alignment rounded up to 16-byte alignment, which should have been accounted for in |
| 55 |
// 'substruct's constructor. |
| 56 |
const int baseAlignment = substruct.requiredAlignment(); |
| 57 |
SkASSERT(!LayoutRules::AlignArraysTo16(fLayout) || SkIsAlign16(baseAlignment)); |
| 58 |
|
| 59 |
// Per layout rule #9, the struct size must be padded to its base alignment |
| 60 |
// (see https://registry.khronos.org/OpenGL/specs/gl/glspec45.core.pdf#page=159). |
| 61 |
const int alignedSize = SkAlignTo(substruct.size(), baseAlignment); |
| 62 |
|
| 63 |
const int alignedOffset = SkAlignTo(fOffset, baseAlignment); |
| 64 |
fOffset = alignedOffset + alignedSize * std::max(count, 1); |
| 65 |
fReqAlignment = std::max(fReqAlignment, baseAlignment); |
| 66 |
|
| 67 |
return alignedOffset; |
| 68 |
} |
| 69 |
|
| 70 |
////////////////////////////////////////////////////////////////////////////// |
| 71 |
|
| 72 |
void UniformManager::resetWithNewLayout(Layout layout) { |
| 73 |
fStorage.clear(); |
| 74 |
fLayout = layout; |
| 75 |
fReqAlignment = 1; |
| 76 |
fEndPaintAlignment = 1; |
| 77 |
fEndPaintOffset = 0; |
| 78 |
fNonShadingOffset = 0; |
| 79 |
fStructBaseAlignment = 0; |
| 80 |
fWrotePaintColor = false; |
| 81 |
|
| 82 |
#ifdef SK_DEBUG |
| 83 |
fOffsetCalculator = UniformOffsetCalculator::ForTopLevel(layout); |
| 84 |
fMarkedOffsetCalculator = fOffsetCalculator; |
| 85 |
fSubstructCalculator = {}; |
| 86 |
fExpectedUniforms = {}; |
| 87 |
fExpectedUniformIndex = 0; |
| 88 |
#endif |
| 89 |
} |
| 90 |
|
| 91 |
void UniformManager::rewindToMark() { |
| 92 |
// Prepare the storage paramters such that: |
| 93 |
// 1) If the renderstep is shading, the size and alignment can grow directly from the state at |
| 94 |
// at the end of the paint uniforms. |
| 95 |
// 2) If the renderstep is non-shading, the storage can be aligned to the renderstep's |
| 96 |
// uniform alignment requirements. |
| 97 |
fStorage.resize(fEndPaintOffset); |
| 98 |
fReqAlignment = fEndPaintAlignment; |
| 99 |
fNonShadingOffset = 0; |
| 100 |
SkDEBUGCODE(fOffsetCalculator = fMarkedOffsetCalculator); |
| 101 |
|
| 102 |
// If we're rewinding, we shouldn't be using substructs. |
| 103 |
SkASSERT(fSubstructStartingOffset == -1); |
| 104 |
// Any struct should be closed. |
| 105 |
SkASSERT(fStructBaseAlignment == 0); |
| 106 |
} |
| 107 |
|
| 108 |
static std::pair<SkSLType, int> adjust_for_matrix_type(SkSLType type, int count) { |
| 109 |
// All Layouts flatten matrices and arrays of matrices into arrays of columns, so update |
| 110 |
// 'type' to be the column type and either multiply 'count' by the number of columns for |
| 111 |
// arrays of matrices, or set to exactly the number of columns for a "non-array" matrix. |
| 112 |
switch(type) { |
| 113 |
case SkSLType::kFloat2x2: return {SkSLType::kFloat2, 2*std::max(1, count)}; |
| 114 |
case SkSLType::kFloat3x3: return {SkSLType::kFloat3, 3*std::max(1, count)}; |
| 115 |
case SkSLType::kFloat4x4: return {SkSLType::kFloat4, 4*std::max(1, count)}; |
| 116 |
|
| 117 |
case SkSLType::kHalf2x2: return {SkSLType::kHalf2, 2*std::max(1, count)}; |
| 118 |
case SkSLType::kHalf3x3: return {SkSLType::kHalf3, 3*std::max(1, count)}; |
| 119 |
case SkSLType::kHalf4x4: return {SkSLType::kHalf4, 4*std::max(1, count)}; |
| 120 |
|
| 121 |
// Otherwise leave type and count alone. |
| 122 |
default: return {type, count}; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
void UniformManager::write(const Uniform& u, const void* data) { |
| 127 |
SkASSERT(SkSLTypeCanBeUniformValue(u.type())); |
| 128 |
SkASSERT(!u.isPaintColor()); // Must go through writePaintColor() |
| 129 |
|
| 130 |
auto [type, count] = adjust_for_matrix_type(u.type(), u.count()); |
| 131 |
SkASSERT(SkSLTypeMatrixSize(type) < 0); // Matrix types should have been flattened |
| 132 |
|
| 133 |
const bool fullPrecision = LayoutRules::UseFullPrecision(fLayout) || !IsHalfVector(type); |
| 134 |
|
| 135 |
#define GEN_WRITE_TABLE(M, Half, Align16) \ |
| 136 |
switch(SkSLTypeVecLength(type)) { \ |
| 137 |
case 1: M(1, Half, Align16); break; \ |
| 138 |
case 2: M(2, Half, Align16); break; \ |
| 139 |
case 3: M(3, Half, Align16); break; \ |
| 140 |
case 4: M(4, Half, Align16); break; } |
| 141 |
#define FOR_NON_ARRAY(N, Half, Align16) this->write<N, Half>(data, type) |
| 142 |
#define FOR_ARRAY(N, Half, Align16) this->writeArray<N, Half, Align16>(data, count, type) |
| 143 |
|
| 144 |
if (count == Uniform::kNonArray) { |
| 145 |
if (fullPrecision) { |
| 146 |
GEN_WRITE_TABLE(FOR_NON_ARRAY, /*Half=*/false, /*ignored*/) |
| 147 |
} else { |
| 148 |
GEN_WRITE_TABLE(FOR_NON_ARRAY, /*Half=*/true, /*ignored*/) |
| 149 |
} |
| 150 |
} else { |
| 151 |
if (LayoutRules::AlignArraysTo16(fLayout)) { |
| 152 |
if (fullPrecision) { |
| 153 |
GEN_WRITE_TABLE(FOR_ARRAY, /*Half=*/false, /*Align16=*/true) |
| 154 |
} else { |
| 155 |
GEN_WRITE_TABLE(FOR_ARRAY, /*Half=*/true, /*Align16=*/true) |
| 156 |
} |
| 157 |
} else { |
| 158 |
if (fullPrecision) { |
| 159 |
GEN_WRITE_TABLE(FOR_ARRAY, /*Half=*/false, /*Align16=*/false) |
| 160 |
} else { |
| 161 |
GEN_WRITE_TABLE(FOR_ARRAY, /*Half=*/true, /*Align16=*/false) |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
#undef FOR_ARRAY |
| 167 |
#undef FOR_NON_ARRAY |
| 168 |
#undef GEN_WRITE_TABLE |
| 169 |
} |
| 170 |
|
| 171 |
#if defined(SK_DEBUG) |
| 172 |
|
| 173 |
void UniformManager::checkBeginStruct(int baseAlignment) { |
| 174 |
// Wrote a struct field before the struct was started |
| 175 |
SkASSERT(fExpectedUniformIndex == 0); |
| 176 |
|
| 177 |
// Not expecting to start a struct (layout must be valid) |
| 178 |
SkASSERT(fSubstructCalculator.layout() != Layout::kInvalid); |
| 179 |
|
| 180 |
// Somehow already started a substruct (base alignment should be <= 0 initially) |
| 181 |
SkASSERT(fStructBaseAlignment <= 0); |
| 182 |
|
| 183 |
// Empty substructs are not allowed |
| 184 |
SkASSERT(!fExpectedUniforms.empty()); |
| 185 |
|
| 186 |
// Assume the expected uniforms describe the whole substruct |
| 187 |
auto structCalculator = UniformOffsetCalculator::ForStruct(fLayout); |
| 188 |
for (const Uniform& f : fExpectedUniforms) { |
| 189 |
structCalculator.advanceOffset(f.type(), f.count()); |
| 190 |
} |
| 191 |
|
| 192 |
// Calculated alignment must match the passed base alignment |
| 193 |
SkASSERT(baseAlignment == structCalculator.requiredAlignment()); |
| 194 |
|
| 195 |
fSubstructStartingOffset = fOffsetCalculator.advanceStruct(structCalculator); |
| 196 |
} |
| 197 |
|
| 198 |
void UniformManager::checkEndStruct() { |
| 199 |
// Didn't write all the expected fields before ending the struct |
| 200 |
SkASSERT(fExpectedUniformIndex == (int)fExpectedUniforms.size()); |
| 201 |
|
| 202 |
// Not expecting a struct (layout must be valid) |
| 203 |
SkASSERT(fSubstructCalculator.layout() != Layout::kInvalid); |
| 204 |
|
| 205 |
// Missing a beginStruct() (base alignment must be > 0 if we are in a struct) |
| 206 |
SkASSERT(fStructBaseAlignment > 0); |
| 207 |
|
| 208 |
// `fStructCalculator` should now have been advanced equivalently to the substruct calculator |
| 209 |
// used in checkBeginStruct() to calculate the expected starting offset. |
| 210 |
const int structSize = SkAlignTo(fSubstructCalculator.size(), |
| 211 |
fSubstructCalculator.requiredAlignment()); |
| 212 |
|
| 213 |
// Somehow didn't end on the correct boundary |
| 214 |
SkASSERT(fStorage.size() == fSubstructStartingOffset + structSize); |
| 215 |
|
| 216 |
// UniformManager's alignment got out of sync with expected alignment |
| 217 |
SkASSERT(fReqAlignment == fOffsetCalculator.requiredAlignment()); |
| 218 |
SkASSERT(fReqAlignment >= fSubstructCalculator.requiredAlignment()); |
| 219 |
|
| 220 |
// Reset the substruct calculator to mark that the struct has been completed |
| 221 |
fSubstructCalculator = {}; |
| 222 |
} |
| 223 |
|
| 224 |
void UniformManager::checkExpected(const void* dst, SkSLType type, int count) { |
| 225 |
// A write() outside of a UniformExpectationsVisitor or too many uniforms written for what |
| 226 |
// is expected. |
| 227 |
SkASSERT(fExpectedUniformIndex < SkTo<int>(fExpectedUniforms.size())); |
| 228 |
|
| 229 |
if (fSubstructCalculator.layout() != Layout::kInvalid) { |
| 230 |
// A write() that should be inside a struct, but missing a call to beginStruct() |
| 231 |
SkASSERT(fStructBaseAlignment > 0); |
| 232 |
} else { |
| 233 |
// A substruct was started when it shouldn't have been. |
| 234 |
SkASSERT(fStructBaseAlignment <= 0); |
| 235 |
} |
| 236 |
|
| 237 |
const Uniform& expected = fExpectedUniforms[fExpectedUniformIndex++]; |
| 238 |
// Not all types are supported as uniforms or supported by UniformManager |
| 239 |
SkASSERT(SkSLTypeCanBeUniformValue(expected.type())); |
| 240 |
|
| 241 |
auto [expectedType, expectedCount] = adjust_for_matrix_type(expected.type(), expected.count()); |
| 242 |
SkASSERT(expectedType == type && expectedCount == count); |
| 243 |
|
| 244 |
if (dst) { |
| 245 |
// If we have 'dst', it's the aligned starting offset of the uniform being checked, so |
| 246 |
// subtracting the address of the first byte in fStorage gives us the offset. |
| 247 |
int offset = static_cast<int>(reinterpret_cast<intptr_t>(dst) - |
| 248 |
reinterpret_cast<intptr_t>(fStorage.data()) - |
| 249 |
fNonShadingOffset); |
| 250 |
|
| 251 |
if (fSubstructCalculator.layout() == Layout::kInvalid) { |
| 252 |
// Pass original expected type and count to the offset calculator for validation. |
| 253 |
SkASSERT(offset == fOffsetCalculator.advanceOffset(expected.type(), expected.count())); |
| 254 |
SkASSERT(fReqAlignment == fOffsetCalculator.requiredAlignment()); |
| 255 |
|
| 256 |
// And if it is the paint color uniform, we should not have already written it. |
| 257 |
SkASSERT(!(fWrotePaintColor && expected.isPaintColor())); |
| 258 |
} else { |
| 259 |
int relOffset = fSubstructCalculator.advanceOffset(expected.type(), expected.count()); |
| 260 |
SkASSERT(offset == fSubstructStartingOffset + relOffset); |
| 261 |
|
| 262 |
// The overall required alignment might already be higher from prior fields, but should |
| 263 |
// be at least what's required by the substruct. |
| 264 |
SkASSERT(fReqAlignment >= fSubstructCalculator.requiredAlignment()); |
| 265 |
|
| 266 |
// And it should not be a paint color uniform within a substruct. |
| 267 |
SkASSERT(!expected.isPaintColor()); |
| 268 |
} |
| 269 |
} else { |
| 270 |
// If 'dst' is null, it's an already-visited paint color uniform, so it's not being written |
| 271 |
// and not changing the offset, and should not be part of a substruct. |
| 272 |
SkASSERT(fWrotePaintColor); |
| 273 |
SkASSERT(fSubstructCalculator.layout() == Layout::kInvalid); |
| 274 |
SkASSERT(expected.isPaintColor()); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
bool UniformManager::isReset() const { |
| 279 |
return fStorage.empty(); |
| 280 |
} |
| 281 |
|
| 282 |
void UniformManager::setExpectedUniforms(SkSpan<const Uniform> expected, bool isSubstruct) { |
| 283 |
fExpectedUniforms = expected; |
| 284 |
fExpectedUniformIndex = 0; |
| 285 |
|
| 286 |
if (isSubstruct) { |
| 287 |
// Start collecting the subsequent uniforms with a 0-based offset to determine their |
| 288 |
// relative layout and required base alignment of the entire struct. |
| 289 |
fSubstructCalculator = UniformOffsetCalculator::ForStruct(fLayout); |
| 290 |
} else { |
| 291 |
// Expected uniforms will advance fOffsetCalculator directly |
| 292 |
SkASSERT(fSubstructCalculator.layout() == Layout::kInvalid); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
void UniformManager::doneWithExpectedUniforms() { |
| 297 |
SkASSERT(fExpectedUniformIndex == static_cast<int>(fExpectedUniforms.size())); |
| 298 |
// Any expected substruct should have been ended and validated inside endStruct(); if this fails |
| 299 |
// it means there is a missing endStruct(). |
| 300 |
SkASSERT(fSubstructCalculator.layout() == Layout::kInvalid); |
| 301 |
fExpectedUniforms = {}; |
| 302 |
} |
| 303 |
|
| 304 |
#endif // SK_DEBUG |
| 305 |
|
| 306 |
} // namespace skgpu::graphite |