Add unity build support

This commit is contained in:
Jérôme Leclercq
2022-03-15 08:26:57 +01:00
parent 0a4fd8f56d
commit 6bd9f1a9e4
109 changed files with 964 additions and 680 deletions

View File

@@ -10,14 +10,8 @@
namespace Nz::ShaderAst
{
namespace
namespace NAZARA_ANONYMOUS_NAMESPACE
{
template<typename T, typename U>
std::unique_ptr<T> static_unique_pointer_cast(std::unique_ptr<U>&& ptr)
{
return std::unique_ptr<T>(static_cast<T*>(ptr.release()));
}
template <typename T>
struct is_complete_helper
{
@@ -1138,6 +1132,8 @@ namespace Nz::ShaderAst
template<BinaryType Type>
ExpressionPtr AstConstantPropagationVisitor::PropagateBinaryConstant(const ConstantValueExpression& lhs, const ConstantValueExpression& rhs)
{
NAZARA_USE_ANONYMOUS_NAMESPACE
std::unique_ptr<ConstantValueExpression> optimized;
std::visit([&](auto&& arg1)
{
@@ -1167,6 +1163,8 @@ namespace Nz::ShaderAst
template<typename TargetType>
ExpressionPtr AstConstantPropagationVisitor::PropagateSingleValueCast(const ConstantValueExpression& operand)
{
NAZARA_USE_ANONYMOUS_NAMESPACE
std::unique_ptr<ConstantValueExpression> optimized;
std::visit([&](auto&& arg)
@@ -1184,6 +1182,8 @@ namespace Nz::ShaderAst
template<std::size_t TargetComponentCount>
ExpressionPtr AstConstantPropagationVisitor::PropagateConstantSwizzle(const std::array<UInt32, 4>& components, const ConstantValueExpression& operand)
{
NAZARA_USE_ANONYMOUS_NAMESPACE
std::unique_ptr<ConstantValueExpression> optimized;
std::visit([&](auto&& arg)
{
@@ -1204,6 +1204,8 @@ namespace Nz::ShaderAst
template<UnaryType Type>
ExpressionPtr AstConstantPropagationVisitor::PropagateUnaryConstant(const ConstantValueExpression& operand)
{
NAZARA_USE_ANONYMOUS_NAMESPACE
std::unique_ptr<ConstantValueExpression> optimized;
std::visit([&](auto&& arg)
{
@@ -1227,6 +1229,8 @@ namespace Nz::ShaderAst
template<typename TargetType>
ExpressionPtr AstConstantPropagationVisitor::PropagateVec2Cast(TargetType v1, TargetType v2)
{
NAZARA_USE_ANONYMOUS_NAMESPACE
std::unique_ptr<ConstantValueExpression> optimized;
using CCType = CastConstant<Vector2<TargetType>, TargetType, TargetType>;
@@ -1240,6 +1244,8 @@ namespace Nz::ShaderAst
template<typename TargetType>
ExpressionPtr AstConstantPropagationVisitor::PropagateVec3Cast(TargetType v1, TargetType v2, TargetType v3)
{
NAZARA_USE_ANONYMOUS_NAMESPACE
std::unique_ptr<ConstantValueExpression> optimized;
using CCType = CastConstant<Vector3<TargetType>, TargetType, TargetType, TargetType>;
@@ -1253,6 +1259,8 @@ namespace Nz::ShaderAst
template<typename TargetType>
ExpressionPtr AstConstantPropagationVisitor::PropagateVec4Cast(TargetType v1, TargetType v2, TargetType v3, TargetType v4)
{
NAZARA_USE_ANONYMOUS_NAMESPACE
std::unique_ptr<ConstantValueExpression> optimized;
using CCType = CastConstant<Vector4<TargetType>, TargetType, TargetType, TargetType, TargetType>;

View File

@@ -12,8 +12,8 @@ namespace Nz::ShaderAst
{
namespace
{
constexpr UInt32 s_magicNumber = 0x4E534852;
constexpr UInt32 s_currentVersion = 1;
constexpr UInt32 s_shaderAstMagicNumber = 0x4E534852;
constexpr UInt32 s_shaderAstCurrentVersion = 1;
class ShaderSerializerVisitor : public AstExpressionVisitor, public AstStatementVisitor
{
@@ -375,7 +375,7 @@ namespace Nz::ShaderAst
void ShaderAstSerializer::Serialize(ModulePtr& module)
{
m_stream << s_magicNumber << s_currentVersion;
m_stream << s_shaderAstMagicNumber << s_shaderAstCurrentVersion;
SerializeModule(module);
@@ -588,11 +588,11 @@ namespace Nz::ShaderAst
UInt32 magicNumber;
UInt32 version;
m_stream >> magicNumber;
if (magicNumber != s_magicNumber)
if (magicNumber != s_shaderAstMagicNumber)
throw std::runtime_error("invalid shader file");
m_stream >> version;
if (version > s_currentVersion)
if (version > s_shaderAstCurrentVersion)
throw std::runtime_error("unsupported version");
ModulePtr module;

View File

@@ -7,23 +7,6 @@
namespace Nz::ShaderAst
{
namespace
{
template<typename T> T& Retrieve(std::unordered_map<std::size_t, T>& map, std::size_t id)
{
auto it = map.find(id);
assert(it != map.end());
return it->second;
}
template<typename T> const T& Retrieve(const std::unordered_map<std::size_t, T>& map, std::size_t id)
{
auto it = map.find(id);
assert(it != map.end());
return it->second;
}
}
void DependencyCheckerVisitor::Process(Statement& statement, const Config& config)
{
m_config = config;

View File

@@ -25,19 +25,10 @@
namespace Nz::ShaderAst
{
namespace
struct SanitizeVisitor::AstError
{
struct AstError
{
std::string errMsg;
};
template<typename T, typename U>
std::unique_ptr<T> static_unique_pointer_cast(std::unique_ptr<U>&& ptr)
{
return std::unique_ptr<T>(SafeCast<T*>(ptr.release()));
}
}
std::string errMsg;
};
struct SanitizeVisitor::CurrentFunctionData
{
@@ -446,7 +437,7 @@ namespace Nz::ShaderAst
for (auto& index : node.indices)
MandatoryExpr(index);
auto clone = static_unique_pointer_cast<AccessIndexExpression>(AstCloner::Clone(node));
auto clone = StaticUniquePointerCast<AccessIndexExpression>(AstCloner::Clone(node));
Validate(*clone);
// TODO: Handle AccessIndex on structs with m_context->options.useIdentifierAccessesForStructs
@@ -478,7 +469,7 @@ namespace Nz::ShaderAst
MandatoryExpr(node.left);
MandatoryExpr(node.right);
auto clone = static_unique_pointer_cast<AssignExpression>(AstCloner::Clone(node));
auto clone = StaticUniquePointerCast<AssignExpression>(AstCloner::Clone(node));
Validate(*clone);
return clone;
@@ -486,7 +477,7 @@ namespace Nz::ShaderAst
ExpressionPtr SanitizeVisitor::Clone(BinaryExpression& node)
{
auto clone = static_unique_pointer_cast<BinaryExpression>(AstCloner::Clone(node));
auto clone = StaticUniquePointerCast<BinaryExpression>(AstCloner::Clone(node));
Validate(*clone);
return clone;
@@ -590,7 +581,7 @@ namespace Nz::ShaderAst
ExpressionPtr SanitizeVisitor::Clone(CastExpression& node)
{
auto clone = static_unique_pointer_cast<CastExpression>(AstCloner::Clone(node));
auto clone = StaticUniquePointerCast<CastExpression>(AstCloner::Clone(node));
Validate(*clone);
const ExpressionType& targetType = clone->targetType.GetResultingValue();
@@ -690,7 +681,7 @@ namespace Nz::ShaderAst
if (std::holds_alternative<NoValue>(node.value))
throw std::runtime_error("expected a value");
auto clone = static_unique_pointer_cast<ConstantValueExpression>(AstCloner::Clone(node));
auto clone = StaticUniquePointerCast<ConstantValueExpression>(AstCloner::Clone(node));
clone->cachedExpressionType = GetExpressionType(clone->value);
return clone;
@@ -772,7 +763,7 @@ namespace Nz::ShaderAst
ExpressionPtr SanitizeVisitor::Clone(UnaryExpression& node)
{
auto clone = static_unique_pointer_cast<UnaryExpression>(AstCloner::Clone(node));
auto clone = StaticUniquePointerCast<UnaryExpression>(AstCloner::Clone(node));
Validate(*clone);
return clone;
@@ -780,7 +771,7 @@ namespace Nz::ShaderAst
ExpressionPtr SanitizeVisitor::Clone(VariableValueExpression& node)
{
auto clone = static_unique_pointer_cast<VariableValueExpression>(AstCloner::Clone(node));
auto clone = StaticUniquePointerCast<VariableValueExpression>(AstCloner::Clone(node));
Validate(*clone);
return clone;
@@ -887,7 +878,7 @@ namespace Nz::ShaderAst
StatementPtr SanitizeVisitor::Clone(DeclareConstStatement& node)
{
auto clone = static_unique_pointer_cast<DeclareConstStatement>(AstCloner::Clone(node));
auto clone = StaticUniquePointerCast<DeclareConstStatement>(AstCloner::Clone(node));
if (!clone->expression)
throw AstError{ "const variables must have an expression" };
@@ -917,7 +908,7 @@ namespace Nz::ShaderAst
{
assert(m_context);
auto clone = static_unique_pointer_cast<DeclareExternalStatement>(AstCloner::Clone(node));
auto clone = StaticUniquePointerCast<DeclareExternalStatement>(AstCloner::Clone(node));
UInt32 defaultBlockSet = 0;
if (clone->bindingSet.HasValue())
@@ -1051,7 +1042,7 @@ namespace Nz::ShaderAst
if (m_context->currentFunction)
throw AstError{ "options must be declared outside of functions" };
auto clone = static_unique_pointer_cast<DeclareOptionStatement>(AstCloner::Clone(node));
auto clone = StaticUniquePointerCast<DeclareOptionStatement>(AstCloner::Clone(node));
if (clone->optName.empty())
throw AstError{ "empty option name" };
@@ -1083,7 +1074,7 @@ namespace Nz::ShaderAst
if (m_context->currentFunction)
throw AstError{ "structs must be declared outside of functions" };
auto clone = static_unique_pointer_cast<DeclareStructStatement>(AstCloner::Clone(node));
auto clone = StaticUniquePointerCast<DeclareStructStatement>(AstCloner::Clone(node));
if (clone->isExported.HasValue())
clone->isExported = ComputeExprValue(clone->isExported);
@@ -1140,7 +1131,7 @@ namespace Nz::ShaderAst
if (!m_context->currentFunction)
throw AstError{ "global variables outside of external blocks are forbidden" };
auto clone = static_unique_pointer_cast<DeclareVariableStatement>(AstCloner::Clone(node));
auto clone = StaticUniquePointerCast<DeclareVariableStatement>(AstCloner::Clone(node));
Validate(*clone);
return clone;
@@ -1615,7 +1606,7 @@ namespace Nz::ShaderAst
MandatoryExpr(node.condition);
MandatoryStatement(node.body);
auto clone = static_unique_pointer_cast<WhileStatement>(AstCloner::Clone(node));
auto clone = StaticUniquePointerCast<WhileStatement>(AstCloner::Clone(node));
Validate(*clone);
ExpressionValue<LoopUnroll> unrollValue;
@@ -1902,7 +1893,7 @@ namespace Nz::ShaderAst
};
// Run optimizer on constant value to hopefully retrieve a single constant value
return static_unique_pointer_cast<T>(ShaderAst::PropagateConstants(node, optimizerOptions));
return StaticUniquePointerCast<T>(ShaderAst::PropagateConstants(node, optimizerOptions));
}
void SanitizeVisitor::PreregisterIndices(const Module& module)