Shader: Handle matrix cast properly

This commit is contained in:
Jérôme Leclercq
2022-01-23 19:58:04 +01:00
parent 249aebac05
commit 64efd81bf8
7 changed files with 305 additions and 11 deletions

View File

@@ -40,9 +40,10 @@ namespace Nz::ShaderAst
std::unordered_set<std::string> reservedIdentifiers;
std::unordered_map<std::size_t, ConstantValue> optionValues;
bool makeVariableNameUnique = false;
bool removeConstDeclaration = false;
bool reduceLoopsToWhile = false;
bool removeConstDeclaration = false;
bool removeCompoundAssignments = false;
bool removeMatrixCast = false;
bool removeOptionDeclaration = false;
bool removeScalarSwizzling = false;
bool splitMultipleBranches = false;

View File

@@ -54,6 +54,7 @@ namespace Nz::ShaderBuilder
struct Cast
{
inline std::unique_ptr<ShaderAst::CastExpression> operator()(ShaderAst::ExpressionType targetType, ShaderAst::ExpressionPtr expression) const;
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;
};
@@ -71,6 +72,7 @@ namespace Nz::ShaderBuilder
struct Constant
{
inline std::unique_ptr<ShaderAst::ConstantValueExpression> operator()(ShaderAst::ConstantValue value) const;
template<typename T> std::unique_ptr<ShaderAst::ConstantValueExpression> operator()(ShaderAst::ExpressionType type, T value) const;
};
struct DeclareConst

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Shader/ShaderBuilder.hpp>
#include <stdexcept>
#include <Nazara/Shader/Debug.hpp>
namespace Nz::ShaderBuilder
@@ -110,6 +111,15 @@ namespace Nz::ShaderBuilder
return callFunctionExpression;
}
inline std::unique_ptr<ShaderAst::CastExpression> Impl::Cast::operator()(ShaderAst::ExpressionType targetType, ShaderAst::ExpressionPtr expression) const
{
auto castNode = std::make_unique<ShaderAst::CastExpression>();
castNode->targetType = std::move(targetType);
castNode->expressions[0] = std::move(expression);
return castNode;
}
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>();
@@ -159,6 +169,22 @@ namespace Nz::ShaderBuilder
return constantNode;
}
template<typename T>
std::unique_ptr<ShaderAst::ConstantValueExpression> Impl::Constant::operator()(ShaderAst::ExpressionType type, T value) const
{
assert(IsPrimitiveType(type));
switch (std::get<ShaderAst::PrimitiveType>(type))
{
case ShaderAst::PrimitiveType::Boolean: return ShaderBuilder::Constant(value != T(0));
case ShaderAst::PrimitiveType::Float32: return ShaderBuilder::Constant(SafeCast<float>(value));
case ShaderAst::PrimitiveType::Int32: return ShaderBuilder::Constant(SafeCast<Int32>(value));
case ShaderAst::PrimitiveType::UInt32: return ShaderBuilder::Constant(SafeCast<UInt32>(value));
}
throw std::runtime_error("unexpected primitive type");
}
inline std::unique_ptr<ShaderAst::DeclareConstStatement> Impl::DeclareConst::operator()(std::string name, ShaderAst::ExpressionPtr initialValue) const
{
auto declareConstNode = std::make_unique<ShaderAst::DeclareConstStatement>();