Shader: Add constant cast optimization

This commit is contained in:
Jérôme Leclercq
2021-05-18 17:25:37 +02:00
parent e716b44aa3
commit a002d5c210
11 changed files with 342 additions and 62 deletions

View File

@@ -31,13 +31,18 @@ namespace Nz::ShaderAst
protected:
ExpressionPtr Clone(BinaryExpression& node) override;
ExpressionPtr Clone(CastExpression& node) override;
ExpressionPtr Clone(ConditionalExpression& node) override;
ExpressionPtr Clone(UnaryExpression& node) override;
StatementPtr Clone(BranchStatement& node) override;
StatementPtr Clone(ConditionalStatement& node) override;
template<BinaryType Type> ExpressionPtr PropagateConstant(std::unique_ptr<ConstantExpression>&& lhs, std::unique_ptr<ConstantExpression>&& rhs);
template<UnaryType Type> ExpressionPtr PropagateConstant(std::unique_ptr<ConstantExpression>&& operand);
template<BinaryType Type> ExpressionPtr PropagateBinaryConstant(std::unique_ptr<ConstantExpression>&& lhs, std::unique_ptr<ConstantExpression>&& rhs);
template<typename TargetType> ExpressionPtr PropagateSingleValueCast(std::unique_ptr<ConstantExpression>&& operand);
template<UnaryType Type> ExpressionPtr PropagateUnaryConstant(std::unique_ptr<ConstantExpression>&& operand);
template<typename TargetType> ExpressionPtr PropagateVec2Cast(TargetType v1, TargetType v2);
template<typename TargetType> ExpressionPtr PropagateVec3Cast(TargetType v1, TargetType v2, TargetType v3);
template<typename TargetType> ExpressionPtr PropagateVec4Cast(TargetType v1, TargetType v2, TargetType v3, TargetType v4);
private:
std::optional<UInt64> m_enabledOptions;

View File

@@ -9,6 +9,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/Ast/Enums.hpp>
#include <variant>
namespace Nz::ShaderAst
{

View File

@@ -11,6 +11,8 @@
#include <Nazara/Math/Vector2.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Math/Vector4.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/Ast/ExpressionType.hpp>
#include <variant>
namespace Nz::ShaderAst
@@ -27,6 +29,8 @@ namespace Nz::ShaderAst
Vector3i32,
Vector4i32
>;
NAZARA_SHADER_API ExpressionType GetExpressionType(const ConstantValue& constant);
}
#endif

View File

@@ -9,8 +9,8 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Utility/Enums.hpp>
#include <Nazara/Shader/Ast/Enums.hpp>
#include <Nazara/Shader/Ast/Attribute.hpp>
#include <Nazara/Shader/Ast/Enums.hpp>
#include <optional>
#include <string>
#include <variant>

View File

@@ -126,8 +126,6 @@ namespace Nz::ShaderAst
NodeType GetType() const override;
void Visit(AstExpressionVisitor& visitor) override;
ExpressionType GetExpressionType() const;
ShaderAst::ConstantValue value;
};

View File

@@ -9,6 +9,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/Ast/Nodes.hpp>
#include <array>
#include <memory>
#include <optional>
@@ -39,6 +40,7 @@ namespace Nz::ShaderBuilder
struct Cast
{
inline std::unique_ptr<ShaderAst::CastExpression> operator()(ShaderAst::ExpressionType targetType, std::array<ShaderAst::ExpressionPtr, 4> expressions) const;
inline std::unique_ptr<ShaderAst::CastExpression> operator()(ShaderAst::ExpressionType targetType, std::vector<ShaderAst::ExpressionPtr> expressions) const;
};

View File

@@ -58,6 +58,15 @@ namespace Nz::ShaderBuilder
return branchNode;
}
inline std::unique_ptr<ShaderAst::CastExpression> Impl::Cast::operator()(ShaderAst::ExpressionType targetType, std::array<ShaderAst::ExpressionPtr, 4> expressions) const
{
auto castNode = std::make_unique<ShaderAst::CastExpression>();
castNode->expressions = std::move(expressions);
castNode->targetType = std::move(targetType);
return castNode;
}
inline std::unique_ptr<ShaderAst::CastExpression> Impl::Cast::operator()(ShaderAst::ExpressionType targetType, std::vector<ShaderAst::ExpressionPtr> expressions) const
{
auto castNode = std::make_unique<ShaderAst::CastExpression>();