Shader: First working version on both Vulkan & OpenGL (ES)

This commit is contained in:
Jérôme Leclercq
2021-04-12 15:38:20 +02:00
parent f93a5bbdc1
commit ea99c6a19e
42 changed files with 1803 additions and 1053 deletions

View File

@@ -0,0 +1,32 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Shader generator"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_SHADER_CONSTANTVALUE_HPP
#define NAZARA_SHADER_CONSTANTVALUE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Math/Vector2.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Math/Vector4.hpp>
#include <variant>
namespace Nz::ShaderAst
{
using ConstantValue = std::variant<
bool,
float,
Int32,
UInt32,
Vector2f,
Vector3f,
Vector4f,
Vector2i32,
Vector3i32,
Vector4i32
>;
}
#endif

View File

@@ -50,9 +50,17 @@ namespace Nz::ShaderAst
inline bool operator!=(const SamplerType& rhs) const;
};
struct StructType
{
std::size_t structIndex;
inline bool operator==(const StructType& rhs) const;
inline bool operator!=(const StructType& rhs) const;
};
struct UniformType
{
IdentifierType containedType;
std::variant<IdentifierType, StructType> containedType;
inline bool operator==(const UniformType& rhs) const;
inline bool operator!=(const UniformType& rhs) const;
@@ -67,7 +75,7 @@ namespace Nz::ShaderAst
inline bool operator!=(const VectorType& rhs) const;
};
using ExpressionType = std::variant<NoType, IdentifierType, PrimitiveType, MatrixType, SamplerType, UniformType, VectorType>;
using ExpressionType = std::variant<NoType, IdentifierType, PrimitiveType, MatrixType, SamplerType, StructType, UniformType, VectorType>;
struct StructDescription
{

View File

@@ -51,6 +51,17 @@ namespace Nz::ShaderAst
return !operator==(rhs);
}
inline bool StructType::operator==(const StructType& rhs) const
{
return structIndex == rhs.structIndex;
}
inline bool StructType::operator!=(const StructType& rhs) const
{
return !operator==(rhs);
}
inline bool UniformType::operator==(const UniformType& rhs) const
{
return containedType == rhs.containedType;
@@ -61,6 +72,7 @@ namespace Nz::ShaderAst
return !operator==(rhs);
}
inline bool VectorType::operator==(const VectorType& rhs) const
{
return componentCount == rhs.componentCount && type == rhs.type;

View File

@@ -0,0 +1,90 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Shader generator"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_SHADERAST_TRANSFORMVISITOR_HPP
#define NAZARA_SHADERAST_TRANSFORMVISITOR_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/ShaderAstCloner.hpp>
#include <vector>
namespace Nz::ShaderAst
{
class NAZARA_SHADER_API TransformVisitor : AstCloner
{
public:
inline TransformVisitor();
TransformVisitor(const TransformVisitor&) = delete;
TransformVisitor(TransformVisitor&&) = delete;
~TransformVisitor() = default;
StatementPtr Transform(StatementPtr& statement);
TransformVisitor& operator=(const TransformVisitor&) = delete;
TransformVisitor& operator=(TransformVisitor&&) = delete;
private:
struct Identifier;
ExpressionPtr Clone(AccessMemberIdentifierExpression& node) override;
ExpressionPtr Clone(CastExpression& node) override;
ExpressionPtr Clone(IdentifierExpression& node) override;
ExpressionPtr CloneExpression(ExpressionPtr& expr) override;
inline const Identifier* FindIdentifier(const std::string_view& identifierName) const;
void PushScope();
void PopScope();
inline std::size_t RegisterFunction(std::string name);
inline std::size_t RegisterStruct(std::string name, StructDescription description);
inline std::size_t RegisterVariable(std::string name);
ExpressionType ResolveType(const ExpressionType& exprType);
using AstCloner::Visit;
void Visit(BranchStatement& node) override;
void Visit(ConditionalStatement& node) override;
void Visit(DeclareExternalStatement& node) override;
void Visit(DeclareFunctionStatement& node) override;
void Visit(DeclareStructStatement& node) override;
void Visit(DeclareVariableStatement& node) override;
void Visit(MultiStatement& node) override;
struct Alias
{
std::variant<ExpressionType> value;
};
struct Struct
{
std::size_t structIndex;
};
struct Variable
{
std::size_t varIndex;
};
struct Identifier
{
std::string name;
std::variant<Alias, Struct, Variable> value;
};
private:
std::size_t m_nextFuncIndex;
std::size_t m_nextVarIndex;
std::vector<Identifier> m_identifiersInScope;
std::vector<StructDescription> m_structs;
std::vector<std::size_t> m_scopeSizes;
};
}
#include <Nazara/Shader/Ast/TransformVisitor.inl>
#endif

View File

@@ -0,0 +1,62 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - Shader generator"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Shader/Ast/TransformVisitor.hpp>
#include <Nazara/Shader/Debug.hpp>
namespace Nz::ShaderAst
{
inline TransformVisitor::TransformVisitor() :
m_nextFuncIndex(0),
m_nextVarIndex(0)
{
}
inline auto TransformVisitor::FindIdentifier(const std::string_view& identifierName) const -> const Identifier*
{
auto it = std::find_if(m_identifiersInScope.rbegin(), m_identifiersInScope.rend(), [&](const Identifier& identifier) { return identifier.name == identifierName; });
if (it == m_identifiersInScope.rend())
return nullptr;
return &*it;
}
inline std::size_t TransformVisitor::RegisterFunction(std::string name)
{
std::size_t funcIndex = m_nextFuncIndex++;
return funcIndex;
}
inline std::size_t TransformVisitor::RegisterStruct(std::string name, StructDescription description)
{
std::size_t structIndex = m_structs.size();
m_structs.emplace_back(std::move(description));
m_identifiersInScope.push_back({
std::move(name),
Struct {
structIndex
}
});
return structIndex;
}
inline std::size_t TransformVisitor::RegisterVariable(std::string name)
{
std::size_t varIndex = m_nextVarIndex++;
m_identifiersInScope.push_back({
std::move(name),
Variable {
varIndex
}
});
return varIndex;
}
}
#include <Nazara/Shader/DebugOff.hpp>