Hash :
8e9dc1a6
Author :
Date :
2024-11-19T11:08:31
Validate anonymous struct names with namespace
Consider GLSL:
struct { vec4 e; } g;
struct sbbf { vec4 f; };
The struct name validation would fail if user chosen struct name would
clash with a symbol name that ANGLE internally gave to an anonymous
struct.
Fix by importing Name abstraction from MSL backend. A symbol name is
a pair (namespace, string).
Move operator<<(std::ostream &os, const ImmutableString &str)
to sh namespace because that is more natural for operator overloading
name resolution. MSVC works with this.
Bug: angleproject:379758201
Change-Id: Icc9b02aa8cb532e1d925e2fba4c45468f01b9144
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6035029
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Auto-Submit: Kimmo Kinnunen <kkinnunen@apple.com>
Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Commit-Queue: Kimmo Kinnunen <kkinnunen@apple.com>
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
//
// 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.
//
#ifndef COMPILER_TRANSLATOR_MSL_MODIFYSTRUCT_H_
#define COMPILER_TRANSLATOR_MSL_MODIFYSTRUCT_H_
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include "compiler/translator/Compiler.h"
#include "compiler/translator/Name.h"
#include "compiler/translator/msl/IdGen.h"
#include "compiler/translator/msl/Layout.h"
#include "compiler/translator/msl/ProgramPrelude.h"
#include "compiler/translator/msl/SymbolEnv.h"
namespace sh
{
enum class ConvertType
{
OriginalToModified,
ModifiedToOriginal,
};
// Configures how struct modification is performed.
class ModifyStructConfig
{
public:
struct Predicate
{
using Func = bool (*)(const TField &);
static bool False(const TField &) { return false; }
static bool True(const TField &) { return true; }
};
struct SaturateVector
{
// Valid return values are [0, 1, 2, 3, 4].
// If original dim >= return value, the field remains untouched.
using Func = uint8_t (*)(const TField &);
static uint8_t DontSaturate(const TField &) { return 0; }
static uint8_t FullySaturate(const TField &) { return 4; }
};
public:
ModifyStructConfig(ConvertType convertType, bool allowPacking, bool allowPadding)
: convertType(convertType), allowPacking(allowPacking), allowPadding(allowPadding)
{}
// Matrix field is split into multiple fields of row vectors.
Predicate::Func splitMatrixColumns = Predicate::False;
// Array fields are split into multiple fields of element type.
Predicate::Func inlineArray = Predicate::False;
// Struct fields have their subfields inlined directly.
Predicate::Func inlineStruct = Predicate::False;
// Struct fields are modified.
Predicate::Func recurseStruct = Predicate::False;
// Vector and scalar bool fields are promoted to uint32_t fields.
Predicate::Func promoteBoolToUint = Predicate::False;
// Creates a new structure where scalar or vector fields are saturated vectors.
// e.g. `float -> float4`.
// e.g. `float2 -> float4`.
SaturateVector::Func saturateScalarOrVector = SaturateVector::DontSaturate;
// Creates a new structure where scalar or vector array fields are saturated.
// e.g. `float[10] -> float4[10]`
// e.g. `float2[10] -> float4[10]`
SaturateVector::Func saturateScalarOrVectorArrays = SaturateVector::DontSaturate;
// Creates a new structure where matrix fields are row-saturated.
// e.g. `float2x2 -> float2x4`.
SaturateVector::Func saturateMatrixRows = SaturateVector::DontSaturate;
TLayoutBlockStorage initialBlockStorage = kDefaultLayoutBlockStorage;
TLayoutMatrixPacking initialMatrixPacking = kDefaultLayoutMatrixPacking;
ConvertType convertType;
bool allowPacking;
bool allowPadding;
AddressSpace externalAddressSpace;
};
struct ModifiedStructMachinery
{
const TStructure *modifiedStruct = nullptr;
TIntermFunctionDefinition *funcOriginalToModified = nullptr;
TIntermFunctionDefinition *funcModifiedToOriginal = nullptr;
TIntermFunctionDefinition *&getConverter(ConvertType convertType)
{
if (convertType == ConvertType::OriginalToModified)
{
return funcOriginalToModified;
}
else
{
return funcModifiedToOriginal;
}
}
};
// Indexed by topological order.
class ModifiedStructMachineries
{
public:
size_t size() const;
const ModifiedStructMachinery &at(size_t index) const;
const ModifiedStructMachinery *find(const TStructure &s) const;
void insert(const TStructure &s, const ModifiedStructMachinery &machinery);
private:
std::unordered_map<const TStructure *, ModifiedStructMachinery> originalToMachinery;
std::vector<const TStructure *> ordering;
};
// Returns true and `outMachinery` populated if modifications were performed.
// Returns false otherwise.
bool TryCreateModifiedStruct(TCompiler &compiler,
SymbolEnv &symbolEnv,
IdGen &idGen,
const ModifyStructConfig &config,
const TStructure &originalStruct,
const Name &modifiedStructName,
ModifiedStructMachineries &outMachineries,
const bool isUBO,
const bool allowPadding,
const bool useAttributeAliasing);
} // namespace sh
#endif // COMPILER_TRANSLATOR_MSL_MODIFYSTRUCT_H_