Shader: Add attribute parsing

This commit is contained in:
Jérôme Leclercq 2021-03-11 23:20:06 +01:00
parent 8135f22b2f
commit da81a5b871
4 changed files with 110 additions and 13 deletions

View File

@ -16,6 +16,12 @@ namespace Nz::ShaderAst
Simple //< = Simple //< =
}; };
enum class AttributeType
{
Entry, //< Entry point (function only) - has argument type
Layout //< Struct layout (struct only) - has argument style
};
enum class BasicType enum class BasicType
{ {
Boolean, //< bool Boolean, //< bool

View File

@ -25,6 +25,12 @@ namespace Nz::ShaderLang
public: public:
using exception::exception; using exception::exception;
}; };
class UnknownAttribute : public std::exception
{
public:
using exception::exception;
};
class UnknownType : public std::exception class UnknownType : public std::exception
{ {
@ -49,10 +55,13 @@ namespace Nz::ShaderLang
private: private:
// Flow control // Flow control
const Token& Advance(); const Token& Advance();
void Consume(std::size_t count = 1);
const Token& Expect(const Token& token, TokenType type); const Token& Expect(const Token& token, TokenType type);
const Token& Expect(TokenType type); const Token& Expect(TokenType type);
const Token& Peek(std::size_t advance = 0); const Token& Peek(std::size_t advance = 0);
std::vector<ShaderAst::Attribute> ParseAttributes();
// Statements // Statements
std::vector<ShaderAst::StatementPtr> ParseFunctionBody(); std::vector<ShaderAst::StatementPtr> ParseFunctionBody();
ShaderAst::StatementPtr ParseFunctionDeclaration(); ShaderAst::StatementPtr ParseFunctionDeclaration();
@ -70,6 +79,7 @@ namespace Nz::ShaderLang
ShaderAst::ExpressionPtr ParseParenthesisExpression(); ShaderAst::ExpressionPtr ParseParenthesisExpression();
ShaderAst::ExpressionPtr ParsePrimaryExpression(); ShaderAst::ExpressionPtr ParsePrimaryExpression();
ShaderAst::AttributeType ParseIdentifierAsAttributeType();
const std::string& ParseIdentifierAsName(); const std::string& ParseIdentifierAsName();
ShaderAst::ShaderExpressionType ParseIdentifierAsType(); ShaderAst::ShaderExpressionType ParseIdentifierAsType();
@ -77,6 +87,7 @@ namespace Nz::ShaderLang
struct Context struct Context
{ {
std::vector<ShaderAst::Attribute> pendingAttributes;
std::unique_ptr<ShaderAst::MultiStatement> root; std::unique_ptr<ShaderAst::MultiStatement> root;
std::size_t tokenCount; std::size_t tokenCount;
std::size_t tokenIndex = 0; std::size_t tokenIndex = 0;

View File

@ -12,9 +12,9 @@
#include <Nazara/Math/Vector3.hpp> #include <Nazara/Math/Vector3.hpp>
#include <Nazara/Math/Vector4.hpp> #include <Nazara/Math/Vector4.hpp>
#include <Nazara/Shader/Config.hpp> #include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/ShaderAstTypes.hpp>
#include <Nazara/Shader/ShaderConstantValue.hpp> #include <Nazara/Shader/ShaderConstantValue.hpp>
#include <Nazara/Shader/ShaderEnums.hpp> #include <Nazara/Shader/ShaderEnums.hpp>
#include <Nazara/Shader/ShaderAstTypes.hpp>
#include <array> #include <array>
#include <memory> #include <memory>
#include <optional> #include <optional>
@ -25,6 +25,12 @@ namespace Nz::ShaderAst
class AstExpressionVisitor; class AstExpressionVisitor;
class AstStatementVisitor; class AstStatementVisitor;
struct Attribute
{
AttributeType type;
std::string args;
};
struct NAZARA_SHADER_API Node struct NAZARA_SHADER_API Node
{ {
Node() = default; Node() = default;

View File

@ -33,6 +33,11 @@ namespace Nz::ShaderLang
{ "vec3u32", ShaderAst::BasicType::UInt3 }, { "vec3u32", ShaderAst::BasicType::UInt3 },
{ "vec4u32", ShaderAst::BasicType::UInt4 }, { "vec4u32", ShaderAst::BasicType::UInt4 },
}; };
std::unordered_map<std::string, ShaderAst::AttributeType> identifierToAttributeType = {
{ "entry", ShaderAst::AttributeType::Entry },
{ "layout", ShaderAst::AttributeType::Layout },
};
} }
ShaderAst::StatementPtr Parser::Parse(const std::vector<Token>& tokens) ShaderAst::StatementPtr Parser::Parse(const std::vector<Token>& tokens)
@ -51,6 +56,10 @@ namespace Nz::ShaderLang
const Token& nextToken = Peek(); const Token& nextToken = Peek();
switch (nextToken.type) switch (nextToken.type)
{ {
case TokenType::OpenAttribute:
context.pendingAttributes = ParseAttributes();
break;
case TokenType::FunctionDeclaration: case TokenType::FunctionDeclaration:
context.root->statements.push_back(ParseFunctionDeclaration()); context.root->statements.push_back(ParseFunctionDeclaration());
break; break;
@ -75,6 +84,12 @@ namespace Nz::ShaderLang
return token; return token;
} }
void Parser::Consume(std::size_t count)
{
assert(m_context->tokenIndex + count < m_context->tokenCount);
m_context->tokenIndex += count;
}
const Token& Parser::Expect(const Token& token, TokenType type) const Token& Parser::Expect(const Token& token, TokenType type)
{ {
if (token.type != type) if (token.type != type)
@ -97,6 +112,58 @@ namespace Nz::ShaderLang
return m_context->tokens[m_context->tokenIndex + advance]; return m_context->tokens[m_context->tokenIndex + advance];
} }
std::vector<ShaderAst::Attribute> Parser::ParseAttributes()
{
std::vector<ShaderAst::Attribute> attributes;
Expect(Advance(), TokenType::OpenAttribute);
bool expectComma = false;
for (;;)
{
const Token& t = Peek();
if (t.type == TokenType::ClosingAttribute)
{
// Parse [[attribute1]] [[attribute2]] the same as [[attribute1, attribute2]]
if (Peek(1).type == TokenType::OpenAttribute)
{
Consume(2);
expectComma = false;
continue;
}
break;
}
if (expectComma)
Expect(Advance(), TokenType::Comma);
ShaderAst::AttributeType attributeType = ParseIdentifierAsAttributeType();
std::string arg;
if (Peek().type == TokenType::OpenParenthesis)
{
Consume();
if (Peek().type == TokenType::Identifier)
arg = std::get<std::string>(Advance().data);
Expect(Advance(), TokenType::ClosingParenthesis);
}
expectComma = true;
attributes.push_back({
attributeType,
std::move(arg)
});
}
Expect(Advance(), TokenType::ClosingAttribute);
return attributes;
}
std::vector<ShaderAst::StatementPtr> Parser::ParseFunctionBody() std::vector<ShaderAst::StatementPtr> Parser::ParseFunctionBody()
{ {
return ParseStatementList(); return ParseStatementList();
@ -120,10 +187,7 @@ namespace Nz::ShaderLang
break; break;
if (!firstParameter) if (!firstParameter)
{ Expect(Advance(), TokenType::Comma);
Expect(t, TokenType::Comma);
Advance();
}
parameters.push_back(ParseFunctionParameter()); parameters.push_back(ParseFunctionParameter());
firstParameter = false; firstParameter = false;
@ -134,8 +198,7 @@ namespace Nz::ShaderLang
ShaderAst::ShaderExpressionType returnType = ShaderAst::BasicType::Void; ShaderAst::ShaderExpressionType returnType = ShaderAst::BasicType::Void;
if (Peek().type == TokenType::FunctionReturn) if (Peek().type == TokenType::FunctionReturn)
{ {
Advance(); //< Consume -> Consume();
returnType = ParseIdentifierAsType(); returnType = ParseIdentifierAsType();
} }
@ -218,7 +281,7 @@ namespace Nz::ShaderLang
ShaderAst::ExpressionPtr expression; ShaderAst::ExpressionPtr expression;
if (Peek().type == TokenType::Assign) if (Peek().type == TokenType::Assign)
{ {
Advance(); Consume();
expression = ParseExpression(); expression = ParseExpression();
} }
@ -235,7 +298,7 @@ namespace Nz::ShaderLang
if (tokenPrecedence < exprPrecedence) if (tokenPrecedence < exprPrecedence)
return lhs; return lhs;
Advance(); Consume();
ShaderAst::ExpressionPtr rhs = ParsePrimaryExpression(); ShaderAst::ExpressionPtr rhs = ParsePrimaryExpression();
const Token& nextOp = Peek(); const Token& nextOp = Peek();
@ -255,7 +318,6 @@ namespace Nz::ShaderLang
default: throw UnexpectedToken{}; default: throw UnexpectedToken{};
} }
} }
lhs = ShaderBuilder::Binary(binaryType, std::move(lhs), std::move(rhs)); lhs = ShaderBuilder::Binary(binaryType, std::move(lhs), std::move(rhs));
} }
@ -295,15 +357,15 @@ namespace Nz::ShaderLang
switch (token.type) switch (token.type)
{ {
case TokenType::BoolFalse: case TokenType::BoolFalse:
Advance(); Consume();
return ShaderBuilder::Constant(false); return ShaderBuilder::Constant(false);
case TokenType::BoolTrue: case TokenType::BoolTrue:
Advance(); Consume();
return ShaderBuilder::Constant(true); return ShaderBuilder::Constant(true);
case TokenType::FloatingPointValue: case TokenType::FloatingPointValue:
Advance(); Consume();
return ShaderBuilder::Constant(float(std::get<double>(token.data))); //< FIXME return ShaderBuilder::Constant(float(std::get<double>(token.data))); //< FIXME
case TokenType::Identifier: case TokenType::Identifier:
@ -320,6 +382,18 @@ namespace Nz::ShaderLang
} }
} }
ShaderAst::AttributeType Parser::ParseIdentifierAsAttributeType()
{
const Token& identifierToken = Expect(Advance(), TokenType::Identifier);
const std::string& identifier = std::get<std::string>(identifierToken.data);
auto it = identifierToAttributeType.find(identifier);
if (it == identifierToAttributeType.end())
throw UnknownAttribute{};
return it->second;
}
const std::string& Parser::ParseIdentifierAsName() const std::string& Parser::ParseIdentifierAsName()
{ {
const Token& identifierToken = Expect(Advance(), TokenType::Identifier); const Token& identifierToken = Expect(Advance(), TokenType::Identifier);