Shader: Add SourceLocation members
TODO: Fill from Parser and use them for error throwing in SanitizeVisitor
This commit is contained in:
parent
b8bf19f8cd
commit
960ab64d98
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Shader/Config.hpp>
|
||||
#include <Nazara/Shader/ShaderLangSourceLocation.hpp>
|
||||
#include <Nazara/Shader/Ast/Attribute.hpp>
|
||||
#include <Nazara/Shader/Ast/Module.hpp>
|
||||
#include <vector>
|
||||
|
|
@ -23,12 +24,15 @@ namespace Nz::ShaderAst
|
|||
|
||||
template<typename T> bool Compare(const T& lhs, const T& rhs);
|
||||
template<typename T, std::size_t S> bool Compare(const std::array<T, S>& lhs, const std::array<T, S>& rhs);
|
||||
template<typename T> bool Compare(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs);
|
||||
template<typename T> bool Compare(const std::vector<T>& lhs, const std::vector<T>& rhs);
|
||||
template<typename T> bool Compare(const std::unique_ptr<T>& lhs, const std::unique_ptr<T>& rhs);
|
||||
template<typename T> bool Compare(const ExpressionValue<T>& lhs, const ExpressionValue<T>& rhs);
|
||||
inline bool Compare(const AccessIdentifierExpression::Identifier& lhs, const AccessIdentifierExpression::Identifier& rhs);
|
||||
inline bool Compare(const BranchStatement::ConditionalStatement& lhs, const BranchStatement::ConditionalStatement& rhs);
|
||||
inline bool Compare(const DeclareExternalStatement::ExternalVar& lhs, const DeclareExternalStatement::ExternalVar& rhs);
|
||||
inline bool Compare(const DeclareFunctionStatement::Parameter& lhs, const DeclareFunctionStatement::Parameter& rhs);
|
||||
inline bool Compare(const ShaderLang::SourceLocation& lhs, const ShaderLang::SourceLocation& rhs);
|
||||
inline bool Compare(const StructDescription& lhs, const StructDescription& rhs);
|
||||
inline bool Compare(const StructDescription::StructMember& lhs, const StructDescription::StructMember& rhs);
|
||||
|
||||
|
|
|
|||
|
|
@ -13,6 +13,9 @@ namespace Nz::ShaderAst
|
|||
if (lhs.GetType() != rhs.GetType())
|
||||
return false;
|
||||
|
||||
if (!Compare(lhs.sourceLocation, rhs.sourceLocation))
|
||||
return false;
|
||||
|
||||
switch (lhs.GetType())
|
||||
{
|
||||
case NodeType::None: break;
|
||||
|
|
@ -70,6 +73,9 @@ namespace Nz::ShaderAst
|
|||
if (lhs.GetType() != rhs.GetType())
|
||||
return false;
|
||||
|
||||
if (!Compare(lhs.sourceLocation, rhs.sourceLocation))
|
||||
return false;
|
||||
|
||||
switch (lhs.GetType())
|
||||
{
|
||||
case NodeType::None: break;
|
||||
|
|
@ -101,6 +107,17 @@ namespace Nz::ShaderAst
|
|||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Compare(const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs)
|
||||
{
|
||||
if (lhs == nullptr)
|
||||
return rhs == nullptr;
|
||||
else if (rhs == nullptr)
|
||||
return false;
|
||||
|
||||
return Compare(*lhs, *rhs);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Compare(const std::vector<T>& lhs, const std::vector<T>& rhs)
|
||||
{
|
||||
|
|
@ -153,6 +170,17 @@ namespace Nz::ShaderAst
|
|||
return true;
|
||||
}
|
||||
|
||||
bool Compare(const AccessIdentifierExpression::Identifier& lhs, const AccessIdentifierExpression::Identifier& rhs)
|
||||
{
|
||||
if (!Compare(lhs.identifier, rhs.identifier))
|
||||
return false;
|
||||
|
||||
if (!Compare(lhs.sourceLocation, rhs.sourceLocation))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool Compare(const BranchStatement::ConditionalStatement& lhs, const BranchStatement::ConditionalStatement& rhs)
|
||||
{
|
||||
if (!Compare(lhs.condition, rhs.condition))
|
||||
|
|
@ -178,6 +206,9 @@ namespace Nz::ShaderAst
|
|||
if (!Compare(lhs.type, rhs.type))
|
||||
return false;
|
||||
|
||||
if (!Compare(lhs.sourceLocation, rhs.sourceLocation))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -189,6 +220,29 @@ namespace Nz::ShaderAst
|
|||
if (!Compare(lhs.type, rhs.type))
|
||||
return false;
|
||||
|
||||
if (!Compare(lhs.sourceLocation, rhs.sourceLocation))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Compare(const ShaderLang::SourceLocation& lhs, const ShaderLang::SourceLocation& rhs)
|
||||
{
|
||||
if (!Compare(lhs.endColumn, rhs.endColumn))
|
||||
return false;
|
||||
|
||||
if (!Compare(lhs.endLine, rhs.endLine))
|
||||
return false;
|
||||
|
||||
if (!Compare(lhs.startColumn, rhs.startColumn))
|
||||
return false;
|
||||
|
||||
if (!Compare(lhs.startLine, rhs.startLine))
|
||||
return false;
|
||||
|
||||
if (!Compare(lhs.file, rhs.file))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -223,6 +277,9 @@ namespace Nz::ShaderAst
|
|||
if (!Compare(lhs.type, rhs.type))
|
||||
return false;
|
||||
|
||||
if (!Compare(lhs.sourceLocation, rhs.sourceLocation))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
#include <Nazara/Core/ByteArray.hpp>
|
||||
#include <Nazara/Core/ByteStream.hpp>
|
||||
#include <Nazara/Shader/Config.hpp>
|
||||
#include <Nazara/Shader/ShaderLangSourceLocation.hpp>
|
||||
#include <Nazara/Shader/Ast/Module.hpp>
|
||||
|
||||
namespace Nz::ShaderAst
|
||||
|
|
@ -43,7 +44,6 @@ namespace Nz::ShaderAst
|
|||
void Serialize(TypeExpression& node);
|
||||
void Serialize(VariableValueExpression& node);
|
||||
void Serialize(UnaryExpression& node);
|
||||
void SerializeExpressionCommon(Expression& expr);
|
||||
|
||||
void Serialize(BranchStatement& node);
|
||||
void Serialize(ConditionalStatement& node);
|
||||
|
|
@ -65,6 +65,9 @@ namespace Nz::ShaderAst
|
|||
void Serialize(ScopedStatement& node);
|
||||
void Serialize(WhileStatement& node);
|
||||
|
||||
void SerializeExpressionCommon(Expression& expr);
|
||||
void SerializeNodeCommon(ShaderAst::Node& node);
|
||||
|
||||
protected:
|
||||
template<typename T> void Container(T& container);
|
||||
template<typename T> void Enum(T& enumVal);
|
||||
|
|
@ -75,11 +78,14 @@ namespace Nz::ShaderAst
|
|||
|
||||
virtual bool IsWriting() const = 0;
|
||||
|
||||
virtual void SerializeModule(ModulePtr& module) = 0;
|
||||
|
||||
virtual void Node(ExpressionPtr& node) = 0;
|
||||
virtual void Node(StatementPtr& node) = 0;
|
||||
|
||||
virtual void SerializeModule(ModulePtr& module) = 0;
|
||||
virtual void SharedString(std::shared_ptr<const std::string>& val) = 0;
|
||||
|
||||
inline void SourceLoc(ShaderLang::SourceLocation& sourceLoc);
|
||||
|
||||
virtual void Type(ExpressionType& type) = 0;
|
||||
|
||||
virtual void Value(bool& val) = 0;
|
||||
|
|
@ -110,11 +116,11 @@ namespace Nz::ShaderAst
|
|||
private:
|
||||
using AstSerializerBase::Serialize;
|
||||
|
||||
void SerializeModule(ModulePtr& module) override;
|
||||
|
||||
bool IsWriting() const override;
|
||||
void Node(ExpressionPtr& node) override;
|
||||
void Node(StatementPtr& node) override;
|
||||
void SerializeModule(ModulePtr& module) override;
|
||||
void SharedString(std::shared_ptr<const std::string>& val) override;
|
||||
void Type(ExpressionType& type) override;
|
||||
void Value(bool& val) override;
|
||||
void Value(float& val) override;
|
||||
|
|
@ -131,6 +137,7 @@ namespace Nz::ShaderAst
|
|||
void Value(UInt32& val) override;
|
||||
void Value(UInt64& val) override;
|
||||
|
||||
std::unordered_map<std::string, UInt32> m_stringIndices;
|
||||
ByteStream& m_stream;
|
||||
};
|
||||
|
||||
|
|
@ -145,11 +152,11 @@ namespace Nz::ShaderAst
|
|||
private:
|
||||
using AstSerializerBase::Serialize;
|
||||
|
||||
void SerializeModule(ModulePtr& module) override;
|
||||
|
||||
bool IsWriting() const override;
|
||||
void Node(ExpressionPtr& node) override;
|
||||
void Node(StatementPtr& node) override;
|
||||
void SerializeModule(ModulePtr& module) override;
|
||||
void SharedString(std::shared_ptr<const std::string>& val) override;
|
||||
void Type(ExpressionType& type) override;
|
||||
void Value(bool& val) override;
|
||||
void Value(float& val) override;
|
||||
|
|
@ -166,6 +173,7 @@ namespace Nz::ShaderAst
|
|||
void Value(UInt32& val) override;
|
||||
void Value(UInt64& val) override;
|
||||
|
||||
std::vector<std::shared_ptr<const std::string>> m_strings;
|
||||
ByteStream& m_stream;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -166,6 +166,15 @@ namespace Nz::ShaderAst
|
|||
}
|
||||
}
|
||||
|
||||
inline void AstSerializerBase::SourceLoc(ShaderLang::SourceLocation& sourceLoc)
|
||||
{
|
||||
SharedString(sourceLoc.file);
|
||||
Value(sourceLoc.endColumn);
|
||||
Value(sourceLoc.endLine);
|
||||
Value(sourceLoc.startColumn);
|
||||
Value(sourceLoc.startLine);
|
||||
}
|
||||
|
||||
inline void AstSerializerBase::SizeT(std::size_t& val)
|
||||
{
|
||||
bool isWriting = IsWriting();
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
#define NAZARA_SHADER_AST_EXPRESSIONTYPE_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Shader/ShaderLangSourceLocation.hpp>
|
||||
#include <Nazara/Shader/Ast/Attribute.hpp>
|
||||
#include <Nazara/Shader/Ast/Enums.hpp>
|
||||
#include <Nazara/Utility/Enums.hpp>
|
||||
|
|
@ -166,6 +167,7 @@ namespace Nz::ShaderAst
|
|||
ExpressionValue<UInt32> locationIndex;
|
||||
ExpressionValue<ExpressionType> type;
|
||||
std::string name;
|
||||
ShaderLang::SourceLocation sourceLocation;
|
||||
};
|
||||
|
||||
ExpressionValue<StructLayout> layout;
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@
|
|||
#include <Nazara/Math/Vector3.hpp>
|
||||
#include <Nazara/Math/Vector4.hpp>
|
||||
#include <Nazara/Shader/Config.hpp>
|
||||
#include <Nazara/Shader/ShaderLangSourceLocation.hpp>
|
||||
#include <Nazara/Shader/Ast/Attribute.hpp>
|
||||
#include <Nazara/Shader/Ast/ConstantValue.hpp>
|
||||
#include <Nazara/Shader/Ast/Enums.hpp>
|
||||
|
|
@ -41,6 +42,8 @@ namespace Nz::ShaderAst
|
|||
|
||||
Node& operator=(const Node&) = delete;
|
||||
Node& operator=(Node&&) noexcept = default;
|
||||
|
||||
ShaderLang::SourceLocation sourceLocation;
|
||||
};
|
||||
|
||||
// Expressions
|
||||
|
|
@ -69,7 +72,13 @@ namespace Nz::ShaderAst
|
|||
NodeType GetType() const override;
|
||||
void Visit(AstExpressionVisitor& visitor) override;
|
||||
|
||||
std::vector<std::string> identifiers;
|
||||
struct Identifier
|
||||
{
|
||||
std::string identifier;
|
||||
ShaderLang::SourceLocation sourceLocation;
|
||||
};
|
||||
|
||||
std::vector<Identifier> identifiers;
|
||||
ExpressionPtr expr;
|
||||
};
|
||||
|
||||
|
|
@ -317,6 +326,7 @@ namespace Nz::ShaderAst
|
|||
ExpressionValue<UInt32> bindingIndex;
|
||||
ExpressionValue<UInt32> bindingSet;
|
||||
ExpressionValue<ExpressionType> type;
|
||||
ShaderLang::SourceLocation sourceLocation;
|
||||
};
|
||||
|
||||
std::vector<ExternalVar> externalVars;
|
||||
|
|
@ -330,9 +340,10 @@ namespace Nz::ShaderAst
|
|||
|
||||
struct Parameter
|
||||
{
|
||||
ExpressionValue<ExpressionType> type;
|
||||
std::optional<std::size_t> varIndex;
|
||||
std::string name;
|
||||
ExpressionValue<ExpressionType> type;
|
||||
ShaderLang::SourceLocation sourceLocation;
|
||||
};
|
||||
|
||||
std::optional<std::size_t> funcIndex;
|
||||
|
|
|
|||
|
|
@ -12,7 +12,12 @@ namespace Nz::ShaderBuilder
|
|||
{
|
||||
auto accessMemberNode = std::make_unique<ShaderAst::AccessIdentifierExpression>();
|
||||
accessMemberNode->expr = std::move(expr);
|
||||
accessMemberNode->identifiers = std::move(memberIdentifiers);
|
||||
accessMemberNode->identifiers.reserve(memberIdentifiers.size());
|
||||
for (std::string& identifier : memberIdentifiers)
|
||||
{
|
||||
auto& identifierEntry = accessMemberNode->identifiers.emplace_back();
|
||||
identifierEntry.identifier = std::move(identifier);
|
||||
}
|
||||
|
||||
return accessMemberNode;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@
|
|||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Shader/Config.hpp>
|
||||
#include <Nazara/Shader/ShaderLangParser.hpp>
|
||||
#include <Nazara/Shader/ShaderLangSourceLocation.hpp>
|
||||
#include <Nazara/Shader/Ast/Enums.hpp>
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
|
|
@ -18,22 +19,6 @@
|
|||
|
||||
namespace Nz::ShaderLang
|
||||
{
|
||||
struct SourceLocation
|
||||
{
|
||||
inline SourceLocation();
|
||||
inline SourceLocation(unsigned int line, unsigned int column, std::shared_ptr<const std::string> file);
|
||||
inline SourceLocation(unsigned int line, unsigned int startColumn, unsigned int endColumn, std::shared_ptr<const std::string> file);
|
||||
inline SourceLocation(unsigned int startLine, unsigned int endLine, unsigned int startColumn, unsigned int endColumn, std::shared_ptr<const std::string> file);
|
||||
|
||||
inline bool IsValid() const;
|
||||
|
||||
std::shared_ptr<const std::string> file; //< Since the same file will be used for every node, prevent holding X time the same path
|
||||
unsigned int endColumn;
|
||||
unsigned int endLine;
|
||||
unsigned int startColumn;
|
||||
unsigned int startLine;
|
||||
};
|
||||
|
||||
enum class ErrorCategory
|
||||
{
|
||||
Ast,
|
||||
|
|
|
|||
|
|
@ -7,46 +7,6 @@
|
|||
|
||||
namespace Nz::ShaderLang
|
||||
{
|
||||
inline SourceLocation::SourceLocation() :
|
||||
endColumn(0),
|
||||
endLine(0),
|
||||
startColumn(0),
|
||||
startLine(0)
|
||||
{
|
||||
}
|
||||
|
||||
inline SourceLocation::SourceLocation(unsigned int Line, unsigned int Column, std::shared_ptr<const std::string> File) :
|
||||
file(std::move(File)),
|
||||
endColumn(Column),
|
||||
endLine(Line),
|
||||
startColumn(Column),
|
||||
startLine(Line)
|
||||
{
|
||||
}
|
||||
|
||||
inline SourceLocation::SourceLocation(unsigned int Line, unsigned int StartColumn, unsigned int EndColumn, std::shared_ptr<const std::string> File) :
|
||||
file(std::move(File)),
|
||||
endColumn(EndColumn),
|
||||
endLine(Line),
|
||||
startColumn(StartColumn),
|
||||
startLine(Line)
|
||||
{
|
||||
}
|
||||
|
||||
inline SourceLocation::SourceLocation(unsigned int StartLine, unsigned int EndLine, unsigned int StartColumn, unsigned int EndColumn, std::shared_ptr<const std::string> File) :
|
||||
file(std::move(File)),
|
||||
endColumn(EndColumn),
|
||||
endLine(EndLine),
|
||||
startColumn(StartColumn),
|
||||
startLine(StartLine)
|
||||
{
|
||||
}
|
||||
|
||||
inline bool SourceLocation::IsValid() const
|
||||
{
|
||||
return startLine != 0 && endLine != 0 && endColumn != 0 && startColumn != 0;
|
||||
}
|
||||
|
||||
inline Error::Error(SourceLocation sourceLocation, ErrorCategory errorCategory, unsigned int errorType) noexcept :
|
||||
m_errorCategory(errorCategory),
|
||||
m_sourceLocation(std::move(sourceLocation)),
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Shader module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_SHADER_SHADERLANGSOURCELOCATION_HPP
|
||||
#define NAZARA_SHADER_SHADERLANGSOURCELOCATION_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Shader/Config.hpp>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace Nz::ShaderLang
|
||||
{
|
||||
struct SourceLocation
|
||||
{
|
||||
inline SourceLocation();
|
||||
inline SourceLocation(unsigned int line, unsigned int column, std::shared_ptr<const std::string> file);
|
||||
inline SourceLocation(unsigned int line, unsigned int startColumn, unsigned int endColumn, std::shared_ptr<const std::string> file);
|
||||
inline SourceLocation(unsigned int startLine, unsigned int endLine, unsigned int startColumn, unsigned int endColumn, std::shared_ptr<const std::string> file);
|
||||
|
||||
inline bool IsValid() const;
|
||||
|
||||
std::shared_ptr<const std::string> file; //< Since the same file will be used for every node, prevent holding X time the same path
|
||||
UInt32 endColumn;
|
||||
UInt32 endLine;
|
||||
UInt32 startColumn;
|
||||
UInt32 startLine;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/ShaderLangSourceLocation.inl>
|
||||
|
||||
#endif // NAZARA_SHADER_SHADERLANGSOURCELOCATION_HPP
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Shader module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Shader/ShaderLangSourceLocation.hpp>
|
||||
#include <Nazara/Shader/Debug.hpp>
|
||||
|
||||
namespace Nz::ShaderLang
|
||||
{
|
||||
inline SourceLocation::SourceLocation() :
|
||||
endColumn(0),
|
||||
endLine(0),
|
||||
startColumn(0),
|
||||
startLine(0)
|
||||
{
|
||||
}
|
||||
|
||||
inline SourceLocation::SourceLocation(unsigned int Line, unsigned int Column, std::shared_ptr<const std::string> File) :
|
||||
file(std::move(File)),
|
||||
endColumn(Column),
|
||||
endLine(Line),
|
||||
startColumn(Column),
|
||||
startLine(Line)
|
||||
{
|
||||
}
|
||||
|
||||
inline SourceLocation::SourceLocation(unsigned int Line, unsigned int StartColumn, unsigned int EndColumn, std::shared_ptr<const std::string> File) :
|
||||
file(std::move(File)),
|
||||
endColumn(EndColumn),
|
||||
endLine(Line),
|
||||
startColumn(StartColumn),
|
||||
startLine(Line)
|
||||
{
|
||||
}
|
||||
|
||||
inline SourceLocation::SourceLocation(unsigned int StartLine, unsigned int EndLine, unsigned int StartColumn, unsigned int EndColumn, std::shared_ptr<const std::string> File) :
|
||||
file(std::move(File)),
|
||||
endColumn(EndColumn),
|
||||
endLine(EndLine),
|
||||
startColumn(StartColumn),
|
||||
startLine(StartLine)
|
||||
{
|
||||
}
|
||||
|
||||
inline bool SourceLocation::IsValid() const
|
||||
{
|
||||
return startLine != 0 && endLine != 0 && endColumn != 0 && startColumn != 0;
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/DebugOff.hpp>
|
||||
|
|
@ -26,12 +26,14 @@ namespace Nz::ShaderAst
|
|||
#define NAZARA_SHADERAST_EXPRESSION(Node) void Visit(Node& node) override \
|
||||
{ \
|
||||
m_serializer.Serialize(node); \
|
||||
m_serializer.SerializeNodeCommon(node); \
|
||||
m_serializer.SerializeExpressionCommon(node); \
|
||||
}
|
||||
|
||||
#define NAZARA_SHADERAST_STATEMENT(Node) void Visit(Node& node) override \
|
||||
{ \
|
||||
m_serializer.Serialize(node); \
|
||||
m_serializer.SerializeNodeCommon(node); \
|
||||
}
|
||||
#include <Nazara/Shader/Ast/AstNodeList.hpp>
|
||||
|
||||
|
|
@ -45,8 +47,11 @@ namespace Nz::ShaderAst
|
|||
Node(node.expr);
|
||||
|
||||
Container(node.identifiers);
|
||||
for (std::string& identifier : node.identifiers)
|
||||
Value(identifier);
|
||||
for (auto& identifier : node.identifiers)
|
||||
{
|
||||
Value(identifier.identifier);
|
||||
SourceLoc(identifier.sourceLocation);
|
||||
}
|
||||
}
|
||||
|
||||
void AstSerializerBase::Serialize(AccessIndexExpression& node)
|
||||
|
|
@ -204,11 +209,6 @@ namespace Nz::ShaderAst
|
|||
Node(node.expression);
|
||||
}
|
||||
|
||||
void AstSerializerBase::SerializeExpressionCommon(Expression& expr)
|
||||
{
|
||||
OptType(expr.cachedExpressionType);
|
||||
}
|
||||
|
||||
void AstSerializerBase::Serialize(BranchStatement& node)
|
||||
{
|
||||
Container(node.condStatements);
|
||||
|
|
@ -235,6 +235,14 @@ namespace Nz::ShaderAst
|
|||
Node(node.expression);
|
||||
}
|
||||
|
||||
void AstSerializerBase::Serialize(DeclareConstStatement& node)
|
||||
{
|
||||
OptVal(node.constIndex);
|
||||
Value(node.name);
|
||||
ExprValue(node.type);
|
||||
Node(node.expression);
|
||||
}
|
||||
|
||||
void AstSerializerBase::Serialize(DeclareExternalStatement& node)
|
||||
{
|
||||
ExprValue(node.bindingSet);
|
||||
|
|
@ -247,17 +255,10 @@ namespace Nz::ShaderAst
|
|||
ExprValue(extVar.type);
|
||||
ExprValue(extVar.bindingIndex);
|
||||
ExprValue(extVar.bindingSet);
|
||||
SourceLoc(extVar.sourceLocation);
|
||||
}
|
||||
}
|
||||
|
||||
void AstSerializerBase::Serialize(DeclareConstStatement& node)
|
||||
{
|
||||
OptVal(node.constIndex);
|
||||
Value(node.name);
|
||||
ExprValue(node.type);
|
||||
Node(node.expression);
|
||||
}
|
||||
|
||||
void AstSerializerBase::Serialize(DeclareFunctionStatement& node)
|
||||
{
|
||||
Value(node.name);
|
||||
|
|
@ -274,6 +275,7 @@ namespace Nz::ShaderAst
|
|||
Value(parameter.name);
|
||||
ExprValue(parameter.type);
|
||||
OptVal(parameter.varIndex);
|
||||
SourceLoc(parameter.sourceLocation);
|
||||
}
|
||||
|
||||
Container(node.statements);
|
||||
|
|
@ -305,6 +307,7 @@ namespace Nz::ShaderAst
|
|||
ExprValue(member.builtin);
|
||||
ExprValue(member.cond);
|
||||
ExprValue(member.locationIndex);
|
||||
SourceLoc(member.sourceLocation);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -378,6 +381,16 @@ namespace Nz::ShaderAst
|
|||
Node(node.body);
|
||||
}
|
||||
|
||||
void AstSerializerBase::SerializeExpressionCommon(Expression& expr)
|
||||
{
|
||||
OptType(expr.cachedExpressionType);
|
||||
}
|
||||
|
||||
void AstSerializerBase::SerializeNodeCommon(ShaderAst::Node& node)
|
||||
{
|
||||
SourceLoc(node.sourceLocation);
|
||||
}
|
||||
|
||||
void ShaderAstSerializer::Serialize(ModulePtr& module)
|
||||
{
|
||||
m_stream << s_shaderAstMagicNumber << s_shaderAstCurrentVersion;
|
||||
|
|
@ -387,23 +400,6 @@ namespace Nz::ShaderAst
|
|||
m_stream.FlushBits();
|
||||
}
|
||||
|
||||
void ShaderAstSerializer::SerializeModule(ModulePtr& module)
|
||||
{
|
||||
m_stream << module->metadata->moduleName;
|
||||
m_stream << module->metadata->moduleId;
|
||||
m_stream << module->metadata->shaderLangVersion;
|
||||
|
||||
Container(module->importedModules);
|
||||
for (auto& importedModule : module->importedModules)
|
||||
{
|
||||
Value(importedModule.identifier);
|
||||
SerializeModule(importedModule.module);
|
||||
}
|
||||
|
||||
ShaderSerializerVisitor visitor(*this);
|
||||
module->rootNode->Visit(visitor);
|
||||
}
|
||||
|
||||
bool ShaderAstSerializer::IsWriting() const
|
||||
{
|
||||
return true;
|
||||
|
|
@ -432,6 +428,44 @@ namespace Nz::ShaderAst
|
|||
node->Visit(visitor);
|
||||
}
|
||||
}
|
||||
|
||||
void ShaderAstSerializer::SerializeModule(ModulePtr& module)
|
||||
{
|
||||
m_stream << module->metadata->moduleName;
|
||||
m_stream << module->metadata->moduleId;
|
||||
m_stream << module->metadata->shaderLangVersion;
|
||||
|
||||
Container(module->importedModules);
|
||||
for (auto& importedModule : module->importedModules)
|
||||
{
|
||||
Value(importedModule.identifier);
|
||||
SerializeModule(importedModule.module);
|
||||
}
|
||||
|
||||
ShaderSerializerVisitor visitor(*this);
|
||||
module->rootNode->Visit(visitor);
|
||||
}
|
||||
|
||||
void ShaderAstSerializer::SharedString(std::shared_ptr<const std::string>& val)
|
||||
{
|
||||
bool hasValue = (val != nullptr);
|
||||
Value(hasValue);
|
||||
|
||||
if (hasValue)
|
||||
{
|
||||
auto it = m_stringIndices.find(*val);
|
||||
bool newString = (it == m_stringIndices.end());
|
||||
Value(newString);
|
||||
|
||||
if (newString)
|
||||
{
|
||||
Value(const_cast<std::string&>(*val)); //< won't be used for writing
|
||||
m_stringIndices.emplace(*val, m_stringIndices.size());
|
||||
}
|
||||
else
|
||||
Value(it->second); //< string index
|
||||
}
|
||||
}
|
||||
|
||||
void ShaderAstSerializer::Type(ExpressionType& type)
|
||||
{
|
||||
|
|
@ -606,29 +640,6 @@ namespace Nz::ShaderAst
|
|||
return module;
|
||||
}
|
||||
|
||||
void ShaderAstUnserializer::SerializeModule(ModulePtr& module)
|
||||
{
|
||||
std::shared_ptr<Module::Metadata> metadata = std::make_shared<Module::Metadata>();
|
||||
m_stream >> metadata->moduleName;
|
||||
m_stream >> metadata->moduleId;
|
||||
m_stream >> metadata->shaderLangVersion;
|
||||
|
||||
std::vector<Module::ImportedModule> importedModules;
|
||||
Container(importedModules);
|
||||
for (auto& importedModule : importedModules)
|
||||
{
|
||||
Value(const_cast<std::string&>(importedModule.identifier)); //< not used for writing
|
||||
SerializeModule(importedModule.module);
|
||||
}
|
||||
|
||||
MultiStatementPtr rootNode = std::make_unique<MultiStatement>();
|
||||
|
||||
ShaderSerializerVisitor visitor(*this);
|
||||
rootNode->Visit(visitor);
|
||||
|
||||
module = std::make_shared<Module>(std::move(metadata), std::move(rootNode), std::move(importedModules));
|
||||
}
|
||||
|
||||
bool ShaderAstUnserializer::IsWriting() const
|
||||
{
|
||||
return false;
|
||||
|
|
@ -686,6 +697,58 @@ namespace Nz::ShaderAst
|
|||
}
|
||||
}
|
||||
|
||||
void ShaderAstUnserializer::SerializeModule(ModulePtr& module)
|
||||
{
|
||||
std::shared_ptr<Module::Metadata> metadata = std::make_shared<Module::Metadata>();
|
||||
m_stream >> metadata->moduleName;
|
||||
m_stream >> metadata->moduleId;
|
||||
m_stream >> metadata->shaderLangVersion;
|
||||
|
||||
std::vector<Module::ImportedModule> importedModules;
|
||||
Container(importedModules);
|
||||
for (auto& importedModule : importedModules)
|
||||
{
|
||||
Value(const_cast<std::string&>(importedModule.identifier)); //< not used for writing
|
||||
SerializeModule(importedModule.module);
|
||||
}
|
||||
|
||||
MultiStatementPtr rootNode = std::make_unique<MultiStatement>();
|
||||
|
||||
ShaderSerializerVisitor visitor(*this);
|
||||
rootNode->Visit(visitor);
|
||||
|
||||
module = std::make_shared<Module>(std::move(metadata), std::move(rootNode), std::move(importedModules));
|
||||
}
|
||||
|
||||
void ShaderAstUnserializer::SharedString(std::shared_ptr<const std::string>& val)
|
||||
{
|
||||
bool hasValue;
|
||||
Value(hasValue);
|
||||
|
||||
if (hasValue)
|
||||
{
|
||||
bool newString;
|
||||
Value(newString);
|
||||
|
||||
if (newString)
|
||||
{
|
||||
std::string newStr;
|
||||
Value(newStr);
|
||||
|
||||
val = std::make_shared<const std::string>(std::move(newStr));
|
||||
m_strings.emplace_back(val);
|
||||
}
|
||||
else
|
||||
{
|
||||
UInt32 strIndex;
|
||||
Value(strIndex);
|
||||
|
||||
assert(strIndex < m_strings.size());
|
||||
val = m_strings[strIndex];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ShaderAstUnserializer::Type(ExpressionType& type)
|
||||
{
|
||||
UInt8 typeIndex;
|
||||
|
|
|
|||
|
|
@ -306,16 +306,16 @@ namespace Nz::ShaderAst
|
|||
std::size_t moduleIndex = m_context->moduleIndices.Retrieve(identifierData->index);
|
||||
|
||||
const auto& env = *m_context->modules[moduleIndex].environment;
|
||||
identifierData = FindIdentifier(env, node.identifiers.front());
|
||||
identifierData = FindIdentifier(env, node.identifiers.front().identifier);
|
||||
if (identifierData)
|
||||
return HandleIdentifier(identifierData);
|
||||
}
|
||||
}
|
||||
|
||||
ExpressionPtr indexedExpr = CloneExpression(node.expr);
|
||||
for (const std::string& identifier : node.identifiers)
|
||||
for (const auto& identifierEntry : node.identifiers)
|
||||
{
|
||||
if (identifier.empty())
|
||||
if (identifierEntry.identifier.empty())
|
||||
throw AstError{ "empty identifier" };
|
||||
|
||||
const ExpressionType* exprType = GetExpressionType(*indexedExpr);
|
||||
|
|
@ -326,12 +326,12 @@ namespace Nz::ShaderAst
|
|||
// TODO: Add proper support for methods
|
||||
if (IsSamplerType(resolvedType))
|
||||
{
|
||||
if (identifier == "Sample")
|
||||
if (identifierEntry.identifier == "Sample")
|
||||
{
|
||||
// TODO: Add a MethodExpression?
|
||||
auto identifierExpr = std::make_unique<AccessIdentifierExpression>();
|
||||
identifierExpr->expr = std::move(indexedExpr);
|
||||
identifierExpr->identifiers.push_back(identifier);
|
||||
identifierExpr->identifiers.emplace_back().identifier = identifierEntry.identifier;
|
||||
|
||||
MethodType methodType;
|
||||
methodType.methodIndex = 0; //< FIXME
|
||||
|
|
@ -342,7 +342,7 @@ namespace Nz::ShaderAst
|
|||
indexedExpr = std::move(identifierExpr);
|
||||
}
|
||||
else
|
||||
throw AstError{ "type has no method " + identifier };
|
||||
throw AstError{ "type has no method " + identifierEntry.identifier };
|
||||
}
|
||||
else if (IsStructType(resolvedType))
|
||||
{
|
||||
|
|
@ -367,7 +367,7 @@ namespace Nz::ShaderAst
|
|||
continue;
|
||||
}
|
||||
|
||||
if (field.name == identifier)
|
||||
if (field.name == identifierEntry.identifier)
|
||||
{
|
||||
fieldPtr = &field;
|
||||
break;
|
||||
|
|
@ -377,7 +377,7 @@ namespace Nz::ShaderAst
|
|||
}
|
||||
|
||||
if (!fieldPtr)
|
||||
throw AstError{ "unknown field " + identifier };
|
||||
throw AstError{ "unknown field " + identifierEntry.identifier };
|
||||
|
||||
if (m_context->options.useIdentifierAccessesForStructs)
|
||||
{
|
||||
|
|
@ -394,8 +394,9 @@ namespace Nz::ShaderAst
|
|||
else
|
||||
accessIdentifierPtr = static_cast<AccessIdentifierExpression*>(indexedExpr.get());
|
||||
|
||||
accessIdentifierPtr->identifiers.push_back(fieldPtr->name);
|
||||
accessIdentifierPtr->cachedExpressionType = ResolveTypeExpr(fieldPtr->type);
|
||||
|
||||
accessIdentifierPtr->identifiers.emplace_back().identifier = fieldPtr->name;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -411,7 +412,7 @@ namespace Nz::ShaderAst
|
|||
else if (IsPrimitiveType(resolvedType) || IsVectorType(resolvedType))
|
||||
{
|
||||
// Swizzle expression
|
||||
std::size_t swizzleComponentCount = identifier.size();
|
||||
std::size_t swizzleComponentCount = identifierEntry.identifier.size();
|
||||
if (swizzleComponentCount > 4)
|
||||
throw AstError{ "cannot swizzle more than four elements" };
|
||||
|
||||
|
|
@ -419,7 +420,7 @@ namespace Nz::ShaderAst
|
|||
{
|
||||
for (std::size_t j = 0; j < swizzleComponentCount; ++j)
|
||||
{
|
||||
if (ToSwizzleIndex(identifier[j]) != 0)
|
||||
if (ToSwizzleIndex(identifierEntry.identifier[j]) != 0)
|
||||
throw AstError{ "invalid swizzle" };
|
||||
//throw ShaderLang::CompilerInvalidSwizzleError{};
|
||||
}
|
||||
|
|
@ -452,7 +453,7 @@ namespace Nz::ShaderAst
|
|||
|
||||
swizzle->componentCount = swizzleComponentCount;
|
||||
for (std::size_t j = 0; j < swizzleComponentCount; ++j)
|
||||
swizzle->components[j] = ToSwizzleIndex(identifier[j]);
|
||||
swizzle->components[j] = ToSwizzleIndex(identifierEntry.identifier[j]);
|
||||
|
||||
Validate(*swizzle);
|
||||
|
||||
|
|
|
|||
|
|
@ -833,8 +833,8 @@ namespace Nz
|
|||
assert(exprType);
|
||||
assert(IsStructType(*exprType));
|
||||
|
||||
for (const std::string& identifier : node.identifiers)
|
||||
Append(".", identifier);
|
||||
for (const auto& identifierEntry : node.identifiers)
|
||||
Append(".", identifierEntry.identifier);
|
||||
}
|
||||
|
||||
void GlslWriter::Visit(ShaderAst::AccessIndexExpression& node)
|
||||
|
|
|
|||
|
|
@ -736,8 +736,8 @@ namespace Nz
|
|||
{
|
||||
Visit(node.expr, true);
|
||||
|
||||
for (const std::string& identifier : node.identifiers)
|
||||
Append(".", identifier);
|
||||
for (const auto& identifierEntry : node.identifiers)
|
||||
Append(".", identifierEntry.identifier);
|
||||
}
|
||||
|
||||
void LangWriter::Visit(ShaderAst::AccessIndexExpression& node)
|
||||
|
|
|
|||
Loading…
Reference in New Issue