Hash :
b23bf47c
Author :
Date :
2023-01-25T18:50:40
Reland "Metal: rewrite default uniforms and uniform blocks" Instead of rewriting uniforms in shaders to match std140 layout, re-pack incoming uniform blocks' std140 packed variables to match Metal's layout. This change intorduces a new BlockLayoutEncoder for Metal types The block encoder handles packing typically larger GL types (bools) into smaller types, and adding support for more compressed matrix types. Since we no longer need to do shader-time packing and unpacking of data from std140 padded structs, complicated shader transformations have been removed. This patch greatly reduces register pressure, especially when working with shaders with arrays of previously expanded types. (Vec3's) Reland: Fix an issue where the default uniform block's final size was not aligned to the default uniform block's alignment requirements, causing crashes with the debug layer enabled. Bug: angleproject:7137 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3733524 Commit-Queue: Kyle Piddington <kpiddington@apple.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Kenneth Russell <kbr@chromium.org> Change-Id: I89d3b817675486fde73b91b0be0f4c25986d4ba5 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4209867
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
//
// Copyright 2023 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// renderermtl_utils:
// Helper methods pertaining to the Metal back-end.
//
#include "libANGLE/renderer/metal/renderermtl_utils.h"
#include "libANGLE/renderer/renderer_utils.h"
namespace rx
{
namespace
{
template <int cols, int rows, bool IsColumnMajor>
inline int GetFlattenedIndex(int col, int row)
{
if (IsColumnMajor)
{
return col * rows + row;
}
else
{
return row * cols + col;
}
}
template <typename T,
bool IsSrcColumnMajor,
int colsSrc,
int rowsSrc,
bool IsDstColumnMajor,
int colsDst,
int rowsDst>
void ExpandMatrix(T *target, const GLfloat *value)
{
static_assert(colsSrc <= colsDst && rowsSrc <= rowsDst, "Can only expand!");
constexpr int kDstFlatSize = colsDst * rowsDst;
T staging[kDstFlatSize] = {0};
for (int r = 0; r < rowsSrc; r++)
{
for (int c = 0; c < colsSrc; c++)
{
int srcIndex = GetFlattenedIndex<colsSrc, rowsSrc, IsSrcColumnMajor>(c, r);
int dstIndex = GetFlattenedIndex<colsDst, rowsDst, IsDstColumnMajor>(c, r);
staging[dstIndex] = static_cast<T>(value[srcIndex]);
}
}
memcpy(target, staging, kDstFlatSize * sizeof(T));
}
template <bool IsSrcColumMajor,
int colsSrc,
int rowsSrc,
bool IsDstColumnMajor,
int colsDst,
int rowsDst>
void SetFloatUniformMatrix(unsigned int arrayElementOffset,
unsigned int elementCount,
GLsizei countIn,
const GLfloat *value,
uint8_t *targetData)
{
unsigned int count =
std::min(elementCount - arrayElementOffset, static_cast<unsigned int>(countIn));
const unsigned int targetMatrixStride = colsDst * rowsDst;
GLfloat *target = reinterpret_cast<GLfloat *>(
targetData + arrayElementOffset * sizeof(GLfloat) * targetMatrixStride);
for (unsigned int i = 0; i < count; i++)
{
ExpandMatrix<GLfloat, IsSrcColumMajor, colsSrc, rowsSrc, IsDstColumnMajor, colsDst,
rowsDst>(target, value);
target += targetMatrixStride;
value += colsSrc * rowsSrc;
}
}
void SetFloatUniformMatrixFast(unsigned int arrayElementOffset,
unsigned int elementCount,
GLsizei countIn,
size_t matrixSize,
const GLfloat *value,
uint8_t *targetData)
{
const unsigned int count =
std::min(elementCount - arrayElementOffset, static_cast<unsigned int>(countIn));
const uint8_t *valueData = reinterpret_cast<const uint8_t *>(value);
targetData = targetData + arrayElementOffset * matrixSize;
memcpy(targetData, valueData, matrixSize * count);
}
} // anonymous namespace
namespace mtl
{
#define ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(api, cols, rows) \
template void SetFloatUniformMatrix##api<cols, rows>::Run( \
unsigned int, unsigned int, GLsizei, GLboolean, const GLfloat *, uint8_t *)
ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(Metal, 2, 2);
ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(Metal, 3, 3);
ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(Metal, 2, 3);
ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(Metal, 3, 2);
ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(Metal, 2, 4);
ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(Metal, 3, 4);
ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC(Metal, 4, 3);
#undef ANGLE_INSTANTIATE_SET_UNIFORM_MATRIX_FUNC
#define ANGLE_SPECIALIZATION_COLS_SET_UNIFORM_MATRIX_FUNC(api, cols, rows) \
template void SetFloatUniformMatrix##api<cols, rows>::Run( \
unsigned int, unsigned int, GLsizei, GLboolean, const GLfloat *, uint8_t *)
template <int cols>
struct SetFloatUniformMatrixMetal<cols, 4>
{
static void Run(unsigned int arrayElementOffset,
unsigned int elementCount,
GLsizei countIn,
GLboolean transpose,
const GLfloat *value,
uint8_t *targetData);
};
template <int cols>
struct SetFloatUniformMatrixMetal<cols, 2>
{
static void Run(unsigned int arrayElementOffset,
unsigned int elementCount,
GLsizei countIn,
GLboolean transpose,
const GLfloat *value,
uint8_t *targetData);
};
ANGLE_SPECIALIZATION_COLS_SET_UNIFORM_MATRIX_FUNC(Metal, 2, 4);
ANGLE_SPECIALIZATION_COLS_SET_UNIFORM_MATRIX_FUNC(Metal, 3, 4);
ANGLE_SPECIALIZATION_COLS_SET_UNIFORM_MATRIX_FUNC(Metal, 4, 4);
ANGLE_SPECIALIZATION_COLS_SET_UNIFORM_MATRIX_FUNC(Metal, 2, 2);
ANGLE_SPECIALIZATION_COLS_SET_UNIFORM_MATRIX_FUNC(Metal, 3, 2);
ANGLE_SPECIALIZATION_COLS_SET_UNIFORM_MATRIX_FUNC(Metal, 4, 2);
#undef ANGLE_SPECIALIZATION_COLS_SET_UNIFORM_MATRIX_FUNC
template <int cols>
void SetFloatUniformMatrixMetal<cols, 4>::Run(unsigned int arrayElementOffset,
unsigned int elementCount,
GLsizei countIn,
GLboolean transpose,
const GLfloat *value,
uint8_t *targetData)
{
const bool isSrcColumnMajor = !transpose;
if (isSrcColumnMajor)
{
// Both src and dst matrixs have the same layout,
// a single memcpy updates all the matrices
constexpr size_t srcMatrixSize = sizeof(GLfloat) * cols * 4;
SetFloatUniformMatrixFast(arrayElementOffset, elementCount, countIn, srcMatrixSize, value,
targetData);
}
else
{
// fallback to general cases
SetFloatUniformMatrix<false, cols, 4, true, cols, 4>(arrayElementOffset, elementCount,
countIn, value, targetData);
}
}
template <int cols>
void SetFloatUniformMatrixMetal<cols, 2>::Run(unsigned int arrayElementOffset,
unsigned int elementCount,
GLsizei countIn,
GLboolean transpose,
const GLfloat *value,
uint8_t *targetData)
{
const bool isSrcColumnMajor = !transpose;
if (isSrcColumnMajor)
{
// Both src and dst matrixs have the same layout,
// a single memcpy updates all the matrices
constexpr size_t srcMatrixSize = sizeof(GLfloat) * cols * 2;
SetFloatUniformMatrixFast(arrayElementOffset, elementCount, countIn, srcMatrixSize, value,
targetData);
}
else
{
// fallback to general cases
SetFloatUniformMatrix<false, cols, 2, true, cols, 2>(arrayElementOffset, elementCount,
countIn, value, targetData);
}
}
template <int cols, int rows>
void SetFloatUniformMatrixMetal<cols, rows>::Run(unsigned int arrayElementOffset,
unsigned int elementCount,
GLsizei countIn,
GLboolean transpose,
const GLfloat *value,
uint8_t *targetData)
{
const bool isSrcColumnMajor = !transpose;
constexpr size_t numRows = rows == 3 ? 4 : rows;
// Metal expects matrix uniforms to be column-major, and each column is padded to 4 rows.
if (isSrcColumnMajor)
{
SetFloatUniformMatrix<true, cols, rows, true, cols, numRows>(
arrayElementOffset, elementCount, countIn, value, targetData);
}
else
{
SetFloatUniformMatrix<false, cols, rows, true, cols, numRows>(
arrayElementOffset, elementCount, countIn, value, targetData);
}
}
template void GetMatrixUniformMetal<GLint>(GLenum, GLint *, const GLint *, bool);
template void GetMatrixUniformMetal<GLuint>(GLenum, GLuint *, const GLuint *, bool);
void GetMatrixUniformMetal(GLenum type, GLfloat *dataOut, const GLfloat *source, bool transpose)
{
int columns = gl::VariableColumnCount(type);
int rows = gl::VariableRowCount(type);
int rowsPerCol = rows == 3 ? 4 : rows;
for (GLint col = 0; col < columns; ++col)
{
for (GLint row = 0; row < rows; ++row)
{
GLfloat *outptr = dataOut + ((col * rows) + row);
const GLfloat *inptr =
transpose ? source + ((row * columns) + col) : source + ((col * rowsPerCol) + row);
*outptr = *inptr;
}
}
}
template <typename NonFloatT>
void GetMatrixUniformMetal(GLenum type, NonFloatT *dataOut, const NonFloatT *source, bool transpose)
{
UNREACHABLE();
}
} // namespace mtl
} // namespace rx