Hash :
9b63700b
Author :
Date :
2023-07-11T10:25:40
Translator: Reorganize files This change makes the translator files more organized by: - Grouping files that are specific to a certain output under their own directory, - Moving transformations under tree_ops - Removing Direct from metal translation now that that SPIR-V Cross path is removed Bug: angleproject:6394 Change-Id: Iaf5bb8d5604b84748dece029821b1f77b2813967 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4678780 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org>
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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
//
// Copyright 2020 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.
//
#include <algorithm>
#include <cctype>
#include <cstring>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include "compiler/translator/Symbol.h"
#include "compiler/translator/msl/Layout.h"
using namespace sh;
////////////////////////////////////////////////////////////////////////////////
enum class Language
{
Metal,
GLSL,
};
static size_t RoundUpToMultipleOf(size_t x, size_t multiple)
{
const size_t rem = x % multiple;
return rem == 0 ? x : x + (multiple - rem);
}
void Layout::requireAlignment(size_t align, bool pad)
{
alignOf = std::max(alignOf, align);
if (pad)
{
sizeOf = RoundUpToMultipleOf(sizeOf, align);
}
}
bool Layout::operator==(const Layout &other) const
{
return sizeOf == other.sizeOf && alignOf == other.alignOf;
}
void Layout::operator+=(const Layout &other)
{
requireAlignment(other.alignOf, true);
sizeOf += other.sizeOf;
}
void Layout::operator*=(size_t scale)
{
sizeOf *= scale;
}
Layout Layout::operator+(const Layout &other) const
{
auto self = *this;
self += other;
return self;
}
Layout Layout::operator*(size_t scale) const
{
auto self = *this;
self *= scale;
return self;
}
static Layout ScalarLayoutOf(const TType &type, Language language)
{
const TBasicType basicType = type.getBasicType();
switch (basicType)
{
case TBasicType::EbtBool:
return {1, 1};
case TBasicType::EbtInt:
case TBasicType::EbtUInt:
case TBasicType::EbtFloat:
return {4, 4};
default:
if (IsSampler(basicType))
{
switch (language)
{
case Language::Metal:
return {8, 8};
case Language::GLSL:
return {4, 4};
}
}
UNIMPLEMENTED();
return Layout::Invalid();
}
}
static const size_t innerScalesPacked[] = {0, 1, 2, 3, 4};
static const size_t innerScalesUnpacked[] = {0, 1, 2, 4, 4};
Layout sh::MetalLayoutOf(const TType &type, MetalLayoutOfConfig config)
{
ASSERT(type.getNominalSize() <= 4);
ASSERT(type.getSecondarySize() <= 4);
const TLayoutBlockStorage storage = type.getLayoutQualifier().blockStorage;
const bool isPacked = !config.disablePacking && (storage == TLayoutBlockStorage::EbsPacked ||
storage == TLayoutBlockStorage::EbsShared);
if (type.isArray() && !config.maskArray)
{
config.maskArray = true;
const Layout layout = MetalLayoutOf(type, config);
config.maskArray = false;
const size_t vol = type.getArraySizeProduct();
return layout * vol;
}
if (const TStructure *structure = type.getStruct())
{
ASSERT(type.getNominalSize() == 1);
ASSERT(type.getSecondarySize() == 1);
auto config2 = config;
config2.maskArray = false;
auto layout = Layout::Identity();
const TFieldList &fields = structure->fields();
for (const TField *field : fields)
{
layout += MetalLayoutOf(*field->type(), config2);
}
if (config.assumeStructsAreTailPadded)
{
size_t pad =
(kDefaultStructAlignmentSize - layout.sizeOf) % kDefaultStructAlignmentSize;
layout.sizeOf += pad;
}
layout.sizeOf = RoundUpToMultipleOf(layout.sizeOf, layout.alignOf);
return layout;
}
if (config.treatSamplersAsTextureEnv && IsSampler(type.getBasicType()))
{
return {16, 8}; // pointer{8,8} and metal::sampler{8,8}
}
const Layout scalarLayout = ScalarLayoutOf(type, Language::Metal);
if (type.isRank0())
{
return scalarLayout;
}
else if (type.isVector())
{
if (isPacked)
{
const size_t innerScale = innerScalesPacked[type.getNominalSize()];
auto layout = Layout{scalarLayout.sizeOf * innerScale, scalarLayout.alignOf};
return layout;
}
else
{
const size_t innerScale = innerScalesUnpacked[type.getNominalSize()];
auto layout = Layout::Both(scalarLayout.sizeOf * innerScale);
return layout;
}
}
else
{
ASSERT(type.isMatrix());
ASSERT(type.getBasicType() == TBasicType::EbtFloat);
// typeCxR <=> typeR[C]
const size_t innerScale = innerScalesUnpacked[type.getRows()];
const size_t outerScale = static_cast<size_t>(type.getCols());
const size_t n = scalarLayout.sizeOf * innerScale;
return {n * outerScale, n};
}
}
TLayoutBlockStorage sh::Overlay(TLayoutBlockStorage oldStorage, const TType &type)
{
const TLayoutBlockStorage newStorage = type.getLayoutQualifier().blockStorage;
switch (newStorage)
{
case TLayoutBlockStorage::EbsUnspecified:
return oldStorage == TLayoutBlockStorage::EbsUnspecified ? kDefaultLayoutBlockStorage
: oldStorage;
default:
return newStorage;
}
}
TLayoutMatrixPacking sh::Overlay(TLayoutMatrixPacking oldPacking, const TType &type)
{
const TLayoutMatrixPacking newPacking = type.getLayoutQualifier().matrixPacking;
switch (newPacking)
{
case TLayoutMatrixPacking::EmpUnspecified:
return oldPacking == TLayoutMatrixPacking::EmpUnspecified ? kDefaultLayoutMatrixPacking
: oldPacking;
default:
return newPacking;
}
}
bool sh::CanBePacked(TLayoutBlockStorage storage)
{
switch (storage)
{
case TLayoutBlockStorage::EbsPacked:
case TLayoutBlockStorage::EbsShared:
return true;
case TLayoutBlockStorage::EbsStd140:
case TLayoutBlockStorage::EbsStd430:
return false;
case TLayoutBlockStorage::EbsUnspecified:
UNREACHABLE();
return false;
}
}
bool sh::CanBePacked(TLayoutQualifier layoutQualifier)
{
return CanBePacked(layoutQualifier.blockStorage);
}
bool sh::CanBePacked(const TType &type)
{
if (!type.isVector())
{
return false;
}
return CanBePacked(type.getLayoutQualifier());
}
void sh::SetBlockStorage(TType &type, TLayoutBlockStorage storage)
{
auto qual = type.getLayoutQualifier();
qual.blockStorage = storage;
type.setLayoutQualifier(qual);
}
static Layout CommonGlslStructLayoutOf(TField const *const *begin,
TField const *const *end,
const TLayoutBlockStorage storage,
const TLayoutMatrixPacking matrixPacking,
const bool maskArray,
const size_t baseAlignment)
{
const bool isPacked =
storage == TLayoutBlockStorage::EbsPacked || storage == TLayoutBlockStorage::EbsShared;
auto layout = Layout::Identity();
for (auto iter = begin; iter != end; ++iter)
{
layout += GlslLayoutOf(*(*iter)->type(), storage, matrixPacking, false);
}
if (!isPacked) // XXX: Correct?
{
layout.sizeOf = RoundUpToMultipleOf(layout.sizeOf, layout.alignOf);
}
layout.requireAlignment(baseAlignment, true);
return layout;
}
static Layout CommonGlslLayoutOf(const TType &type,
const TLayoutBlockStorage storage,
const TLayoutMatrixPacking matrixPacking,
const bool maskArray,
const size_t baseAlignment)
{
ASSERT(storage != TLayoutBlockStorage::EbsUnspecified);
const bool isPacked =
storage == TLayoutBlockStorage::EbsPacked || storage == TLayoutBlockStorage::EbsShared;
if (type.isArray() && !type.isMatrix() && !maskArray)
{
auto layout = GlslLayoutOf(type, storage, matrixPacking, true);
layout *= type.getArraySizeProduct();
layout.requireAlignment(baseAlignment, true);
return layout;
}
if (const TStructure *structure = type.getStruct())
{
ASSERT(type.getNominalSize() == 1);
ASSERT(type.getSecondarySize() == 1);
const TFieldList &fields = structure->fields();
return CommonGlslStructLayoutOf(fields.data(), fields.data() + fields.size(), storage,
matrixPacking, maskArray, baseAlignment);
}
const auto scalarLayout = ScalarLayoutOf(type, Language::GLSL);
if (type.isRank0())
{
return scalarLayout;
}
else if (type.isVector())
{
if (isPacked)
{
const size_t sizeScale = innerScalesPacked[type.getNominalSize()];
const size_t alignScale = innerScalesUnpacked[type.getNominalSize()];
auto layout =
Layout{scalarLayout.sizeOf * sizeScale, scalarLayout.alignOf * alignScale};
return layout;
}
else
{
const size_t innerScale = innerScalesUnpacked[type.getNominalSize()];
auto layout = Layout::Both(scalarLayout.sizeOf * innerScale);
return layout;
}
}
else
{
ASSERT(type.isMatrix());
size_t innerDim;
size_t outerDim;
switch (matrixPacking)
{
case TLayoutMatrixPacking::EmpColumnMajor:
innerDim = static_cast<size_t>(type.getRows());
outerDim = static_cast<size_t>(type.getCols());
break;
case TLayoutMatrixPacking::EmpRowMajor:
innerDim = static_cast<size_t>(type.getCols());
outerDim = static_cast<size_t>(type.getRows());
break;
case TLayoutMatrixPacking::EmpUnspecified:
UNREACHABLE();
innerDim = 0;
outerDim = 0;
}
outerDim *= type.getArraySizeProduct();
const size_t innerScale = innerScalesUnpacked[innerDim];
const size_t n = innerScale * scalarLayout.sizeOf;
Layout layout = {outerDim * n, n};
layout.requireAlignment(baseAlignment, true);
return layout;
}
}
Layout sh::GlslLayoutOf(const TType &type,
TLayoutBlockStorage storage,
TLayoutMatrixPacking matrixPacking,
bool maskArray)
{
ASSERT(type.getNominalSize() <= 4);
ASSERT(type.getSecondarySize() <= 4);
storage = Overlay(storage, type);
matrixPacking = Overlay(matrixPacking, type);
switch (storage)
{
case TLayoutBlockStorage::EbsPacked:
return CommonGlslLayoutOf(type, storage, matrixPacking, maskArray, 1);
case TLayoutBlockStorage::EbsShared:
return CommonGlslLayoutOf(type, storage, matrixPacking, maskArray, 16);
case TLayoutBlockStorage::EbsStd140:
return CommonGlslLayoutOf(type, storage, matrixPacking, maskArray, 16);
case TLayoutBlockStorage::EbsStd430:
return CommonGlslLayoutOf(type, storage, matrixPacking, maskArray, 1);
case TLayoutBlockStorage::EbsUnspecified:
UNREACHABLE();
return Layout::Invalid();
}
}
[[nodiscard]] Layout sh::GlslStructLayoutOf(TField const *const *begin,
TField const *const *end,
TLayoutBlockStorage storage,
TLayoutMatrixPacking matrixPacking,
bool maskArray)
{
ASSERT(storage != TLayoutBlockStorage::EbsUnspecified);
ASSERT(matrixPacking != TLayoutMatrixPacking::EmpUnspecified);
switch (storage)
{
case TLayoutBlockStorage::EbsPacked:
return CommonGlslStructLayoutOf(begin, end, storage, matrixPacking, maskArray, 1);
case TLayoutBlockStorage::EbsShared:
return CommonGlslStructLayoutOf(begin, end, storage, matrixPacking, maskArray, 16);
case TLayoutBlockStorage::EbsStd140:
return CommonGlslStructLayoutOf(begin, end, storage, matrixPacking, maskArray, 16);
case TLayoutBlockStorage::EbsStd430:
return CommonGlslStructLayoutOf(begin, end, storage, matrixPacking, maskArray, 1);
case TLayoutBlockStorage::EbsUnspecified:
UNREACHABLE();
return Layout::Invalid();
}
}