Shader: Add module statement

This commit is contained in:
Jérôme Leclercq 2022-03-01 19:36:18 +01:00
parent ad892dfb43
commit 99e07e6e1e
56 changed files with 418 additions and 123 deletions

View File

@ -1,3 +1,6 @@
[nzsl_version("1.0")]
module;
[layout(std140)]
struct ViewerData
{

View File

@ -1,3 +1,6 @@
[nzsl_version("1.0")]
module;
[layout(std140)]
struct ViewerData
{

View File

@ -1,3 +1,6 @@
[nzsl_version("1.0")]
module;
option HasDiffuseTexture: bool = false;
option HasAlphaTexture: bool = false;
option AlphaTest: bool = false;

View File

@ -1,3 +1,6 @@
[nzsl_version("1.0")]
module;
[layout(std140)]
struct BasicSettings
{

View File

@ -1,3 +1,6 @@
[nzsl_version("1.0")]
module;
external
{
[binding(0)] colorTexture: sampler2D[f32]

View File

@ -1,3 +1,6 @@
[nzsl_version("1.0")]
module;
[layout(std140)]
struct ViewerData
{

View File

@ -1,3 +1,6 @@
[nzsl_version("1.0")]
module;
[layout(std140)]
struct ViewerData
{

View File

@ -1,3 +1,6 @@
[nzsl_version("1.0")]
module;
[layout(std140)]
struct PointLight
{

View File

@ -1,3 +1,6 @@
[nzsl_version("1.0")]
module;
[layout(std140)]
struct ViewerData
{

View File

@ -1,3 +1,6 @@
[nzsl_version("1.0")]
module;
[layout(std140)]
struct ViewerData
{

View File

@ -11,6 +11,8 @@
NAZARA_REQUEST_DEDICATED_GPU()
const char shaderSource[] = R"(
[nzsl_version("1.0")]
module;
option red: bool = false;

View File

@ -12,7 +12,7 @@
#include <Nazara/Core/Bitset.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Renderer/RenderPipeline.hpp>
#include <Nazara/Shader/Ast/Nodes.hpp>
#include <Nazara/Shader/Ast/Module.hpp>
#include <unordered_map>
namespace Nz
@ -26,7 +26,7 @@ namespace Nz
struct Option;
using ConfigCallback = std::function<void(Config& config, const std::vector<RenderPipelineInfo::VertexBufferData>& vertexBuffers)>;
UberShader(ShaderStageTypeFlags shaderStages, const ShaderAst::StatementPtr& shaderAst);
UberShader(ShaderStageTypeFlags shaderStages, ShaderAst::ModulePtr shaderModule);
~UberShader() = default;
inline ShaderStageTypeFlags GetSupportedStages() const;
@ -63,7 +63,7 @@ namespace Nz
private:
std::unordered_map<Config, std::shared_ptr<ShaderModule>, ConfigHasher, ConfigEqual> m_combinations;
std::unordered_map<std::string, Option> m_optionIndexByName;
ShaderAst::StatementPtr m_shaderAst;
ShaderAst::ModulePtr m_shaderModule;
ConfigCallback m_configCallback;
ShaderStageTypeFlags m_shaderStages;
};

View File

@ -41,7 +41,7 @@ namespace Nz
std::shared_ptr<RenderPass> InstantiateRenderPass(std::vector<RenderPass::Attachment> attachments, std::vector<RenderPass::SubpassDescription> subpassDescriptions, std::vector<RenderPass::SubpassDependency> subpassDependencies) override;
std::shared_ptr<RenderPipeline> InstantiateRenderPipeline(RenderPipelineInfo pipelineInfo) override;
std::shared_ptr<RenderPipelineLayout> InstantiateRenderPipelineLayout(RenderPipelineLayoutInfo pipelineLayoutInfo) override;
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states) override;
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states) override;
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const ShaderWriter::States& states) override;
std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params) override;
std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) override;

View File

@ -14,7 +14,7 @@
#include <Nazara/Renderer/Enums.hpp>
#include <Nazara/Renderer/ShaderModule.hpp>
#include <Nazara/Shader/GlslWriter.hpp>
#include <Nazara/Shader/Ast/Nodes.hpp>
#include <Nazara/Shader/Ast/Module.hpp>
#include <vector>
namespace Nz
@ -22,7 +22,7 @@ namespace Nz
class NAZARA_OPENGLRENDERER_API OpenGLShaderModule : public ShaderModule
{
public:
OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states = {});
OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states = {});
OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const ShaderWriter::States& states = {});
OpenGLShaderModule(const OpenGLShaderModule&) = delete;
OpenGLShaderModule(OpenGLShaderModule&&) noexcept = default;
@ -34,7 +34,7 @@ namespace Nz
OpenGLShaderModule& operator=(OpenGLShaderModule&&) noexcept = default;
private:
void Create(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states);
void Create(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states);
static void CheckCompilationStatus(GL::Shader& shader);
@ -45,7 +45,7 @@ namespace Nz
struct ShaderStatement
{
std::shared_ptr<ShaderAst::Statement> ast;
ShaderAst::ModulePtr ast;
};
struct Shader

View File

@ -19,7 +19,7 @@
#include <Nazara/Renderer/Texture.hpp>
#include <Nazara/Renderer/TextureSampler.hpp>
#include <Nazara/Shader/ShaderWriter.hpp>
#include <Nazara/Shader/Ast/Nodes.hpp>
#include <Nazara/Shader/Ast/Module.hpp>
#include <Nazara/Utility/PixelFormat.hpp>
#include <memory>
#include <string>
@ -44,7 +44,7 @@ namespace Nz
virtual std::shared_ptr<RenderPass> InstantiateRenderPass(std::vector<RenderPass::Attachment> attachments, std::vector<RenderPass::SubpassDescription> subpassDescriptions, std::vector<RenderPass::SubpassDependency> subpassDependencies) = 0;
virtual std::shared_ptr<RenderPipeline> InstantiateRenderPipeline(RenderPipelineInfo pipelineInfo) = 0;
virtual std::shared_ptr<RenderPipelineLayout> InstantiateRenderPipelineLayout(RenderPipelineLayoutInfo pipelineLayoutInfo) = 0;
virtual std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states) = 0;
virtual std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states) = 0;
virtual std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const ShaderWriter::States& states) = 0;
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const std::filesystem::path& sourcePath, const ShaderWriter::States& states);
virtual std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params) = 0;

View File

@ -11,7 +11,7 @@
#include <Nazara/Core/ByteArray.hpp>
#include <Nazara/Core/ByteStream.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/Ast/Nodes.hpp>
#include <Nazara/Shader/Ast/Module.hpp>
namespace Nz::ShaderAst
{
@ -94,9 +94,11 @@ namespace Nz::ShaderAst
inline ShaderAstSerializer(ByteStream& stream);
~ShaderAstSerializer() = default;
void Serialize(StatementPtr& shader);
void Serialize(Module& shader);
private:
using AstSerializerBase::Serialize;
bool IsWriting() const override;
void Node(ExpressionPtr& node) override;
void Node(StatementPtr& node) override;
@ -125,9 +127,11 @@ namespace Nz::ShaderAst
ShaderAstUnserializer(ByteStream& stream);
~ShaderAstUnserializer() = default;
StatementPtr Unserialize();
ModulePtr Unserialize();
private:
using AstSerializerBase::Serialize;
bool IsWriting() const override;
void Node(ExpressionPtr& node) override;
void Node(StatementPtr& node) override;
@ -150,9 +154,9 @@ namespace Nz::ShaderAst
ByteStream& m_stream;
};
NAZARA_SHADER_API ByteArray SerializeShader(StatementPtr& shader);
inline StatementPtr UnserializeShader(const void* data, std::size_t size);
NAZARA_SHADER_API StatementPtr UnserializeShader(ByteStream& stream);
NAZARA_SHADER_API ByteArray SerializeShader(Module& shader);
inline ModulePtr UnserializeShader(const void* data, std::size_t size);
NAZARA_SHADER_API ModulePtr UnserializeShader(ByteStream& stream);
}
#include <Nazara/Shader/Ast/AstSerializer.inl>

View File

@ -173,7 +173,7 @@ namespace Nz::ShaderAst
{
}
inline StatementPtr UnserializeShader(const void* data, std::size_t size)
inline ModulePtr UnserializeShader(const void* data, std::size_t size)
{
ByteStream byteStream(data, size);
return UnserializeShader(byteStream);

View File

@ -31,7 +31,8 @@ namespace Nz::ShaderAst
Vector4f,
Vector2i32,
Vector3i32,
Vector4i32
Vector4i32,
std::string
>;
using ConstantValue = TypeListInstantiate<ConstantTypes, std::variant>;

View File

@ -35,6 +35,7 @@ namespace Nz
Entry, //< Entry point (function only) - has argument type
Layout, //< Struct layout (struct only) - has argument style
Location, //< Location (struct member only) - has argument index
LangVersion, //< NZSL version - has argument version string
Set, //< Binding set (external var only) - has argument index
Unroll, //< Unroll (for/for each only) - has argument mode
};
@ -136,6 +137,7 @@ namespace Nz
Float32, //< f32
Int32, //< i32
UInt32, //< ui32
String //< str
};
enum class UnaryType

View File

@ -0,0 +1,28 @@
// 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_AST_MODULE_HPP
#define NAZARA_SHADER_AST_MODULE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/Ast/Nodes.hpp>
#include <memory>
namespace Nz::ShaderAst
{
struct Module;
using ModulePtr = std::shared_ptr<Module>;
struct Module
{
MultiStatementPtr rootNode;
UInt32 shaderLangVersion;
};
}
#endif // NAZARA_SHADER_AST_MODULE_HPP

View File

@ -12,6 +12,7 @@
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/Ast/AstCloner.hpp>
#include <Nazara/Shader/Ast/AstTypes.hpp>
#include <Nazara/Shader/Ast/Module.hpp>
#include <unordered_map>
#include <unordered_set>
#include <vector>
@ -30,8 +31,8 @@ namespace Nz::ShaderAst
SanitizeVisitor(SanitizeVisitor&&) = delete;
~SanitizeVisitor() = default;
inline StatementPtr Sanitize(Statement& statement, std::string* error = nullptr);
StatementPtr Sanitize(Statement& statement, const Options& options, std::string* error = nullptr);
inline ModulePtr Sanitize(Module& module, std::string* error = nullptr);
ModulePtr Sanitize(Module& module, const Options& options, std::string* error = nullptr);
SanitizeVisitor& operator=(const SanitizeVisitor&) = delete;
SanitizeVisitor& operator=(SanitizeVisitor&&) = delete;
@ -177,8 +178,8 @@ namespace Nz::ShaderAst
Context* m_context;
};
inline StatementPtr Sanitize(Statement& ast, std::string* error = nullptr);
inline StatementPtr Sanitize(Statement& ast, const SanitizeVisitor::Options& options, std::string* error = nullptr);
inline ModulePtr Sanitize(Module& module, std::string* error = nullptr);
inline ModulePtr Sanitize(Module& module, const SanitizeVisitor::Options& options, std::string* error = nullptr);
}
#include <Nazara/Shader/Ast/SanitizeVisitor.inl>

View File

@ -7,21 +7,21 @@
namespace Nz::ShaderAst
{
inline StatementPtr SanitizeVisitor::Sanitize(Statement& statement, std::string* error)
inline ModulePtr SanitizeVisitor::Sanitize(Module& module, std::string* error)
{
return Sanitize(statement, {}, error);
return Sanitize(module, {}, error);
}
inline StatementPtr Sanitize(Statement& ast, std::string* error)
inline ModulePtr Sanitize(Module& module, std::string* error)
{
SanitizeVisitor sanitizer;
return sanitizer.Sanitize(ast, error);
return sanitizer.Sanitize(module, error);
}
inline StatementPtr Sanitize(Statement& ast, const SanitizeVisitor::Options& options, std::string* error)
inline ModulePtr Sanitize(Module& module, const SanitizeVisitor::Options& options, std::string* error)
{
SanitizeVisitor sanitizer;
return sanitizer.Sanitize(ast, options, error);
return sanitizer.Sanitize(module, options, error);
}
}

View File

@ -12,6 +12,7 @@
#include <Nazara/Shader/ShaderWriter.hpp>
#include <Nazara/Shader/Ast/AstExpressionVisitorExcept.hpp>
#include <Nazara/Shader/Ast/AstStatementVisitorExcept.hpp>
#include <Nazara/Shader/Ast/Module.hpp>
#include <set>
#include <sstream>
#include <string>
@ -31,8 +32,8 @@ namespace Nz
GlslWriter(GlslWriter&&) = delete;
~GlslWriter() = default;
inline std::string Generate(ShaderAst::Statement& shader, const BindingMapping& bindingMapping = {}, const States& states = {});
std::string Generate(std::optional<ShaderStageType> shaderStage, ShaderAst::Statement& shader, const BindingMapping& bindingMapping = {}, const States& states = {});
inline std::string Generate(ShaderAst::Module& module, const BindingMapping& bindingMapping = {}, const States& states = {});
std::string Generate(std::optional<ShaderStageType> shaderStage, ShaderAst::Module& module, const BindingMapping& bindingMapping = {}, const States& states = {});
void SetEnv(Environment environment);
@ -47,7 +48,7 @@ namespace Nz
};
static const char* GetFlipYUniformName();
static ShaderAst::StatementPtr Sanitize(ShaderAst::Statement& ast, std::unordered_map<std::size_t, ShaderAst::ConstantValue> optionValues, std::string* error = nullptr);
static ShaderAst::ModulePtr Sanitize(ShaderAst::Module& module, std::unordered_map<std::size_t, ShaderAst::ConstantValue> optionValues, std::string* error = nullptr);
private:
void Append(const ShaderAst::ArrayType& type);

View File

@ -12,7 +12,7 @@ namespace Nz
{
}
inline std::string GlslWriter::Generate(ShaderAst::Statement& shader, const BindingMapping& bindingMapping, const States& states)
inline std::string GlslWriter::Generate(ShaderAst::Module& shader, const BindingMapping& bindingMapping, const States& states)
{
return Generate(std::nullopt, shader, bindingMapping, states);
}

View File

@ -12,6 +12,7 @@
#include <Nazara/Shader/ShaderWriter.hpp>
#include <Nazara/Shader/Ast/AstExpressionVisitorExcept.hpp>
#include <Nazara/Shader/Ast/AstStatementVisitorExcept.hpp>
#include <Nazara/Shader/Ast/Module.hpp>
#include <set>
#include <sstream>
#include <string>
@ -28,7 +29,7 @@ namespace Nz
LangWriter(LangWriter&&) = delete;
~LangWriter() = default;
std::string Generate(ShaderAst::Statement& shader, const States& conditions = {});
std::string Generate(ShaderAst::Module& module, const States& conditions = {});
void SetEnv(Environment environment);
@ -44,6 +45,7 @@ namespace Nz
struct EntryAttribute;
struct LayoutAttribute;
struct LocationAttribute;
struct NzslAttribute;
struct SetAttribute;
struct UnrollAttribute;
@ -74,6 +76,7 @@ namespace Nz
void AppendAttribute(EntryAttribute entry);
void AppendAttribute(LayoutAttribute layout);
void AppendAttribute(LocationAttribute location);
void AppendAttribute(NzslAttribute nzslVersion);
void AppendAttribute(SetAttribute set);
void AppendAttribute(UnrollAttribute unroll);
void AppendCommentSection(const std::string& section);

View File

@ -41,6 +41,16 @@ namespace Nz::ShaderLang
using exception::exception;
};
class UnfinishedString : public std::exception
{
using exception::exception;
};
class UnrecognizedChar : public std::exception
{
using exception::exception;
};
class UnrecognizedToken : public std::exception
{
using exception::exception;

View File

@ -10,7 +10,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/ShaderLangLexer.hpp>
#include <Nazara/Shader/Ast/Nodes.hpp>
#include <Nazara/Shader/Ast/Module.hpp>
#include <filesystem>
#include <optional>
@ -34,6 +34,12 @@ namespace Nz::ShaderLang
using runtime_error::runtime_error;
};
class DuplicateModule : public std::runtime_error
{
public:
using runtime_error::runtime_error;
};
class ReservedKeyword : public std::exception
{
public:
@ -64,7 +70,7 @@ namespace Nz::ShaderLang
inline Parser();
~Parser() = default;
ShaderAst::StatementPtr Parse(const std::vector<Token>& tokens);
ShaderAst::ModulePtr Parse(const std::vector<Token>& tokens);
private:
// Flow control
@ -87,6 +93,7 @@ namespace Nz::ShaderLang
std::vector<ShaderAst::StatementPtr> ParseFunctionBody();
ShaderAst::StatementPtr ParseFunctionDeclaration(std::vector<ShaderAst::ExprValue> attributes = {});
ShaderAst::DeclareFunctionStatement::Parameter ParseFunctionParameter();
void ParseModuleStatement(std::vector<ShaderAst::ExprValue> attributes);
ShaderAst::StatementPtr ParseOptionDeclaration();
ShaderAst::StatementPtr ParseReturnStatement();
ShaderAst::StatementPtr ParseSingleStatement();
@ -106,6 +113,7 @@ namespace Nz::ShaderLang
std::vector<ShaderAst::ExpressionPtr> ParseParameters();
ShaderAst::ExpressionPtr ParseParenthesisExpression();
ShaderAst::ExpressionPtr ParsePrimaryExpression();
ShaderAst::ExpressionPtr ParseStringExpression();
ShaderAst::ExpressionPtr ParseVariableAssignation();
ShaderAst::AttributeType ParseIdentifierAsAttributeType();
@ -118,16 +126,16 @@ namespace Nz::ShaderLang
{
std::size_t tokenCount;
std::size_t tokenIndex = 0;
std::unique_ptr<ShaderAst::MultiStatement> root;
ShaderAst::ModulePtr module;
const Token* tokens;
};
Context* m_context;
};
inline ShaderAst::StatementPtr Parse(const std::string_view& source);
inline ShaderAst::StatementPtr Parse(const std::vector<Token>& tokens);
NAZARA_SHADER_API ShaderAst::StatementPtr ParseFromFile(const std::filesystem::path& sourcePath);
inline ShaderAst::ModulePtr Parse(const std::string_view& source);
inline ShaderAst::ModulePtr Parse(const std::vector<Token>& tokens);
NAZARA_SHADER_API ShaderAst::ModulePtr ParseFromFile(const std::filesystem::path& sourcePath);
}
#include <Nazara/Shader/ShaderLangParser.inl>

View File

@ -12,12 +12,12 @@ namespace Nz::ShaderLang
{
}
inline ShaderAst::StatementPtr Parse(const std::string_view& source)
inline ShaderAst::ModulePtr Parse(const std::string_view& source)
{
return Parse(Tokenize(source));
}
inline ShaderAst::StatementPtr Parse(const std::vector<Token>& tokens)
inline ShaderAst::ModulePtr Parse(const std::vector<Token>& tokens)
{
Parser parser;
return parser.Parse(tokens);

View File

@ -51,6 +51,7 @@ NAZARA_SHADERLANG_TOKEN(Multiply)
NAZARA_SHADERLANG_TOKEN(MultiplyAssign)
NAZARA_SHADERLANG_TOKEN(Minus)
NAZARA_SHADERLANG_TOKEN(MinusAssign)
NAZARA_SHADERLANG_TOKEN(Module)
NAZARA_SHADERLANG_TOKEN(Not)
NAZARA_SHADERLANG_TOKEN(NotEqual)
NAZARA_SHADERLANG_TOKEN(Plus)
@ -61,6 +62,7 @@ NAZARA_SHADERLANG_TOKEN(OpenParenthesis)
NAZARA_SHADERLANG_TOKEN(Option)
NAZARA_SHADERLANG_TOKEN(Return)
NAZARA_SHADERLANG_TOKEN(Semicolon)
NAZARA_SHADERLANG_TOKEN(StringValue)
NAZARA_SHADERLANG_TOKEN(Struct)
NAZARA_SHADERLANG_TOKEN(While)

View File

@ -12,7 +12,7 @@
#include <Nazara/Shader/ShaderWriter.hpp>
#include <Nazara/Shader/SpirvConstantCache.hpp>
#include <Nazara/Shader/Ast/ConstantValue.hpp>
#include <Nazara/Shader/Ast/Nodes.hpp>
#include <Nazara/Shader/Ast/Module.hpp>
#include <string>
#include <string_view>
#include <unordered_map>
@ -36,7 +36,7 @@ namespace Nz
SpirvWriter(SpirvWriter&&) = delete;
~SpirvWriter() = default;
std::vector<UInt32> Generate(ShaderAst::Statement& shader, const States& states = {});
std::vector<UInt32> Generate(ShaderAst::Module& module, const States& states = {});
void SetEnv(Environment environment);

View File

@ -32,7 +32,7 @@ namespace Nz
std::shared_ptr<RenderPass> InstantiateRenderPass(std::vector<RenderPass::Attachment> attachments, std::vector<RenderPass::SubpassDescription> subpassDescriptions, std::vector<RenderPass::SubpassDependency> subpassDependencies) override;
std::shared_ptr<RenderPipeline> InstantiateRenderPipeline(RenderPipelineInfo pipelineInfo) override;
std::shared_ptr<RenderPipelineLayout> InstantiateRenderPipelineLayout(RenderPipelineLayoutInfo pipelineLayoutInfo) override;
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags stages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states) override;
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags stages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states) override;
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags stages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const ShaderWriter::States& states) override;
std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params) override;
std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) override;

View File

@ -11,7 +11,7 @@
#include <Nazara/Renderer/Enums.hpp>
#include <Nazara/Renderer/ShaderModule.hpp>
#include <Nazara/Shader/ShaderWriter.hpp>
#include <Nazara/Shader/Ast/Nodes.hpp>
#include <Nazara/Shader/Ast/Module.hpp>
#include <Nazara/VulkanRenderer/Wrapper/ShaderModule.hpp>
#include <vector>
@ -27,7 +27,7 @@ namespace Nz
VulkanShaderModule(VulkanShaderModule&&) = delete;
~VulkanShaderModule() = default;
bool Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states);
bool Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states);
bool Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const ShaderWriter::States& states);
inline const Vk::ShaderModule& GetHandle() const;

View File

@ -260,8 +260,8 @@ namespace Nz
std::vector<std::shared_ptr<UberShader>> BasicMaterial::BuildShaders()
{
ShaderAst::StatementPtr shaderAst = ShaderLang::Parse(std::string_view(reinterpret_cast<const char*>(r_shader), sizeof(r_shader)));
auto shader = std::make_shared<UberShader>(ShaderStageType::Fragment | ShaderStageType::Vertex, shaderAst);
ShaderAst::ModulePtr shaderModule = ShaderLang::Parse(std::string_view(reinterpret_cast<const char*>(r_shader), sizeof(r_shader)));
auto shader = std::make_shared<UberShader>(ShaderStageType::Fragment | ShaderStageType::Vertex, std::move(shaderModule));
return { std::move(shader) };
}

View File

@ -18,8 +18,8 @@ namespace Nz
std::vector<std::shared_ptr<UberShader>> DepthMaterial::BuildShaders()
{
ShaderAst::StatementPtr shaderAst = ShaderLang::Parse(std::string_view(reinterpret_cast<const char*>(r_shader), sizeof(r_shader)));
auto shader = std::make_shared<UberShader>(ShaderStageType::Fragment | ShaderStageType::Vertex, shaderAst);
ShaderAst::ModulePtr shaderModule = ShaderLang::Parse(std::string_view(reinterpret_cast<const char*>(r_shader), sizeof(r_shader)));
auto shader = std::make_shared<UberShader>(ShaderStageType::Fragment | ShaderStageType::Vertex, std::move(shaderModule));
return { std::move(shader) };
}

View File

@ -668,7 +668,7 @@ namespace Nz
void FrameGraph::BuildPhysicalPasses()
{
const std::shared_ptr<RenderDevice>& renderDevice = Graphics::Instance()->GetRenderDevice();
const RenderPassCache& renderPassCache = Graphics::Instance()->GetRenderPassCache();
std::vector<TextureLayout> textureLayouts(m_pending.textures.size(), TextureLayout::Undefined);
@ -683,7 +683,7 @@ namespace Nz
std::vector<RenderPass::SubpassDescription> subpassesDesc;
std::vector<RenderPass::SubpassDependency> subpassesDeps;
auto RegisterColorInputRead = [&](const FramePass::Input& input, PhysicalPassData::Subpass& subpass)
auto RegisterColorInputRead = [&](const FramePass::Input& input)
{
std::size_t textureId = Retrieve(m_pending.attachmentToTextures, input.attachmentId);
@ -766,7 +766,7 @@ namespace Nz
for (const auto& input : subpassInputs)
{
if (input.doesRead)
RegisterColorInputRead(input, subpass);
RegisterColorInputRead(input);
}
for (const auto& output : subpassOutputs)
@ -887,8 +887,7 @@ namespace Nz
BuildPhysicalPassDependencies(colorAttachmentCount, depthStencilAttachmentIndex.has_value(), renderPassAttachments, subpassesDesc, subpassesDeps);
m_pending.renderPasses.push_back(Graphics::Instance()->GetRenderPassCache().Get(renderPassAttachments, subpassesDesc, subpassesDeps));
//m_pending.renderPasses.push_back(renderDevice->InstantiateRenderPass(std::move(renderPassAttachments), std::move(subpassesDesc), std::move(subpassesDeps)));
m_pending.renderPasses.push_back(renderPassCache.Get(renderPassAttachments, subpassesDesc, subpassesDeps));
physicalPassIndex++;
}

View File

@ -347,7 +347,7 @@ namespace Nz
std::vector<std::shared_ptr<UberShader>> PhongLightingMaterial::BuildShaders()
{
ShaderAst::StatementPtr shaderAst;
ShaderAst::ModulePtr shaderModule;
#ifdef NAZARA_DEBUG
std::filesystem::path shaderPath = "../../src/Nazara/Graphics/Resources/Shaders/phong_material.nzsl";
@ -355,7 +355,7 @@ namespace Nz
{
try
{
shaderAst = ShaderLang::ParseFromFile(shaderPath);
shaderModule = ShaderLang::ParseFromFile(shaderPath);
}
catch (const std::exception& e)
{
@ -364,10 +364,10 @@ namespace Nz
}
#endif
if (!shaderAst)
shaderAst = ShaderLang::Parse(std::string_view(reinterpret_cast<const char*>(r_shader), sizeof(r_shader)));
if (!shaderModule)
shaderModule = ShaderLang::Parse(std::string_view(reinterpret_cast<const char*>(r_shader), sizeof(r_shader)));
auto shader = std::make_shared<UberShader>(ShaderStageType::Fragment | ShaderStageType::Vertex, shaderAst);
auto shader = std::make_shared<UberShader>(ShaderStageType::Fragment | ShaderStageType::Vertex, std::move(shaderModule));
return { std::move(shader) };
}

View File

@ -1,3 +1,6 @@
[nzsl_version("1.0")]
module;
option HasDiffuseTexture: bool = false;
option HasAlphaTexture: bool = false;
option AlphaTest: bool = false;

View File

@ -1,3 +1,6 @@
[nzsl_version("1.0")]
module;
external
{
[binding(0)] texture: sampler2D[f32]

View File

@ -1,3 +1,6 @@
[nzsl_version("1.0")]
module;
option HasDiffuseTexture: bool = false;
option HasAlphaTexture: bool = false;
option AlphaTest: bool = false;

View File

@ -1,3 +1,6 @@
[nzsl_version("1.0")]
module;
// Basic material options
option HasDiffuseTexture: bool = false;
option HasAlphaTexture: bool = false;

View File

@ -13,13 +13,14 @@
namespace Nz
{
UberShader::UberShader(ShaderStageTypeFlags shaderStages, const ShaderAst::StatementPtr& shaderAst) :
UberShader::UberShader(ShaderStageTypeFlags shaderStages, ShaderAst::ModulePtr shaderModule) :
m_shaderModule(std::move(shaderModule)),
m_shaderStages(shaderStages)
{
NazaraAssert(m_shaderStages != 0, "there must be at least one shader stage");
NazaraAssert(m_shaderModule, "invalid shader module");
//TODO: Try to partially sanitize shader?
m_shaderAst = ShaderAst::Clone(*shaderAst);
std::size_t optionCount = 0;
@ -33,6 +34,8 @@ namespace Nz
callbacks.onOptionDeclaration = [&](const std::string& optionName, const ShaderAst::ExpressionValue<ShaderAst::ExpressionType>& optionType)
{
//TODO: Check optionType
m_optionIndexByName[optionName] = Option{
optionCount
};
@ -41,7 +44,7 @@ namespace Nz
};
ShaderAst::AstReflect reflect;
reflect.Reflect(*m_shaderAst, callbacks);
reflect.Reflect(*m_shaderModule->rootNode, callbacks);
if ((m_shaderStages & supportedStageType) != m_shaderStages)
throw std::runtime_error("shader doesn't support all required shader stages");
@ -63,7 +66,7 @@ namespace Nz
states.optionValues[i] = config.optionValues[i];
}
std::shared_ptr<ShaderModule> stage = Graphics::Instance()->GetRenderDevice()->InstantiateShaderModule(m_shaderStages, *m_shaderAst, std::move(states));
std::shared_ptr<ShaderModule> stage = Graphics::Instance()->GetRenderDevice()->InstantiateShaderModule(m_shaderStages, *m_shaderModule, std::move(states));
it = m_combinations.emplace(config, std::move(stage)).first;
}

View File

@ -144,9 +144,9 @@ namespace Nz
return std::make_shared<OpenGLRenderPipelineLayout>(std::move(pipelineLayoutInfo));
}
std::shared_ptr<ShaderModule> OpenGLDevice::InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states)
std::shared_ptr<ShaderModule> OpenGLDevice::InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states)
{
return std::make_shared<OpenGLShaderModule>(*this, shaderStages, shaderAst, states);
return std::make_shared<OpenGLShaderModule>(*this, shaderStages, shaderModule, states);
}
std::shared_ptr<ShaderModule> OpenGLDevice::InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const ShaderWriter::States& states)

View File

@ -47,8 +47,12 @@ namespace Nz
{
if (!stageFlags.Test(stage))
{
ShaderAst::StatementPtr dummyAst = ShaderBuilder::DeclareFunction(stage, "main", {}, {});
OpenGLShaderModule shaderModule(device, stage, *dummyAst);
ShaderAst::Module dummyModule;
dummyModule.rootNode = ShaderBuilder::MultiStatement();
dummyModule.rootNode->statements.push_back(ShaderBuilder::DeclareFunction(stage, "main", {}, {}));
dummyModule.shaderLangVersion = 100;
OpenGLShaderModule shaderModule(device, stage, dummyModule);
stageFlags |= shaderModule.Attach(m_program, pipelineLayout.GetBindingMapping());
}
};

View File

@ -13,11 +13,11 @@
namespace Nz
{
OpenGLShaderModule::OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states) :
OpenGLShaderModule::OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states) :
m_device(device)
{
NazaraAssert(shaderStages != 0, "at least one shader stage must be specified");
Create(device, shaderStages, shaderAst, states);
Create(device, shaderStages, shaderModule, states);
}
OpenGLShaderModule::OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const ShaderWriter::States& states) :
@ -58,7 +58,7 @@ namespace Nz
std::vector<Nz::ShaderLang::Token> tokens = Nz::ShaderLang::Tokenize(std::string_view(static_cast<const char*>(source), sourceSize));
Nz::ShaderLang::Parser parser;
Nz::ShaderAst::StatementPtr shaderAst = parser.Parse(tokens);
Nz::ShaderAst::ModulePtr shaderAst = parser.Parse(tokens);
Create(device, shaderStages, *shaderAst, states);
break;
}
@ -137,11 +137,11 @@ namespace Nz
return stageFlags;
}
void OpenGLShaderModule::Create(OpenGLDevice& /*device*/, ShaderStageTypeFlags shaderStages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states)
void OpenGLShaderModule::Create(OpenGLDevice& /*device*/, ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states)
{
m_states = states;
m_states.sanitized = true; //< Shader is always sanitized (because of keywords)
std::shared_ptr<ShaderAst::Statement> sanitized = GlslWriter::Sanitize(shaderAst, states.optionValues);
ShaderAst::ModulePtr sanitized = GlslWriter::Sanitize(shaderModule, states.optionValues);
for (std::size_t i = 0; i < ShaderStageTypeCount; ++i)
{

View File

@ -834,6 +834,7 @@ namespace Nz::ShaderAst
case PrimitiveType::Float32: optimized = PropagateSingleValueCast<float>(constantExpr); break;
case PrimitiveType::Int32: optimized = PropagateSingleValueCast<Int32>(constantExpr); break;
case PrimitiveType::UInt32: optimized = PropagateSingleValueCast<UInt32>(constantExpr); break;
case PrimitiveType::String: break;
}
}
}
@ -866,7 +867,7 @@ namespace Nz::ShaderAst
if constexpr (std::is_same_v<T, NoValue>)
throw std::runtime_error("invalid type (value expected)");
else if constexpr (std::is_same_v<T, bool> || std::is_same_v<T, float> || std::is_same_v<T, Int32> || std::is_same_v<T, UInt32>)
else if constexpr (std::is_same_v<T, bool> || std::is_same_v<T, float> || std::is_same_v<T, Int32> || std::is_same_v<T, UInt32> || std::is_same_v<T, std::string>)
constantValues.push_back(arg);
else if constexpr (std::is_same_v<T, Vector2f> || std::is_same_v<T, Vector2i32>)
{

View File

@ -5,6 +5,7 @@
#include <Nazara/Shader/Ast/AstSerializer.hpp>
#include <Nazara/Shader/Ast/AstExpressionVisitor.hpp>
#include <Nazara/Shader/Ast/AstStatementVisitor.hpp>
#include <Nazara/Shader/ShaderBuilder.hpp>
#include <Nazara/Shader/Debug.hpp>
namespace Nz::ShaderAst
@ -120,7 +121,7 @@ namespace Nz::ShaderAst
Value(value);
};
static_assert(std::variant_size_v<decltype(node.value)> == 11);
static_assert(std::variant_size_v<decltype(node.value)> == 12);
switch (typeIndex)
{
case 0: break;
@ -134,6 +135,7 @@ namespace Nz::ShaderAst
case 8: SerializeValue(Vector2i32()); break;
case 9: SerializeValue(Vector3i32()); break;
case 10: SerializeValue(Vector4i32()); break;
case 11: SerializeValue(std::string()); break;
default: throw std::runtime_error("unexpected data type");
}
}
@ -327,11 +329,12 @@ namespace Nz::ShaderAst
Node(node.body);
}
void ShaderAstSerializer::Serialize(StatementPtr& shader)
void ShaderAstSerializer::Serialize(Module& module)
{
m_stream << s_magicNumber << s_currentVersion;
Node(shader);
m_stream << module.shaderLangVersion;
Serialize(*module.rootNode);
m_stream.FlushBits();
}
@ -514,7 +517,7 @@ namespace Nz::ShaderAst
m_stream << val;
}
StatementPtr ShaderAstUnserializer::Unserialize()
ModulePtr ShaderAstUnserializer::Unserialize()
{
UInt32 magicNumber;
UInt32 version;
@ -526,13 +529,15 @@ namespace Nz::ShaderAst
if (version > s_currentVersion)
throw std::runtime_error("unsupported version");
StatementPtr node;
ModulePtr module = std::make_shared<Module>();
Node(node);
if (!node)
throw std::runtime_error("functions can only have statements");
m_stream >> module->shaderLangVersion;
return node;
module->rootNode = ShaderBuilder::MultiStatement();
ShaderSerializerVisitor visitor(*this);
module->rootNode->Visit(visitor);
return module;
}
bool ShaderAstUnserializer::IsWriting() const
@ -827,18 +832,18 @@ namespace Nz::ShaderAst
}
ByteArray SerializeShader(StatementPtr& shader)
ByteArray SerializeShader(Module& module)
{
ByteArray byteArray;
ByteStream stream(&byteArray, OpenModeFlags(OpenMode::WriteOnly));
ShaderAstSerializer serializer(stream);
serializer.Serialize(shader);
serializer.Serialize(module);
return byteArray;
}
StatementPtr UnserializeShader(ByteStream& stream)
ModulePtr UnserializeShader(ByteStream& stream)
{
ShaderAstUnserializer unserializer(stream);
return unserializer.Unserialize();

View File

@ -24,6 +24,8 @@ namespace Nz::ShaderAst
return PrimitiveType::Int32;
else if constexpr (std::is_same_v<T, UInt32>)
return PrimitiveType::UInt32;
else if constexpr (std::is_same_v<T, std::string>)
return PrimitiveType::String;
else if constexpr (std::is_same_v<T, Vector2f>)
return VectorType{ 2, PrimitiveType::Float32 };
else if constexpr (std::is_same_v<T, Vector3f>)

View File

@ -112,9 +112,10 @@ namespace Nz::ShaderAst
std::vector<StatementPtr>* currentStatementList = nullptr;
};
StatementPtr SanitizeVisitor::Sanitize(Statement& statement, const Options& options, std::string* error)
ModulePtr SanitizeVisitor::Sanitize(Module& module, const Options& options, std::string* error)
{
StatementPtr clone;
ModulePtr clone = std::make_shared<Module>();
clone->shaderLangVersion = module.shaderLangVersion;
Context currentContext;
currentContext.options = options;
@ -129,7 +130,7 @@ namespace Nz::ShaderAst
// First pass, evaluate everything except function code
try
{
clone = AstCloner::Clone(statement);
clone->rootNode = static_unique_pointer_cast<MultiStatement>(AstCloner::Clone(*module.rootNode));
}
catch (const AstError& err)
{

View File

@ -152,7 +152,7 @@ namespace Nz
unsigned int indentLevel = 0;
};
std::string GlslWriter::Generate(std::optional<ShaderStageType> shaderStage, ShaderAst::Statement& shader, const BindingMapping& bindingMapping, const States& states)
std::string GlslWriter::Generate(std::optional<ShaderStageType> shaderStage, ShaderAst::Module& module, const BindingMapping& bindingMapping, const States& states)
{
State state(bindingMapping);
state.optionValues = states.optionValues;
@ -164,15 +164,15 @@ namespace Nz
m_currentState = nullptr;
});
ShaderAst::StatementPtr sanitizedAst;
ShaderAst::ModulePtr sanitizedModule;
ShaderAst::Statement* targetAst;
if (!states.sanitized)
{
sanitizedAst = Sanitize(shader, states.optionValues);
targetAst = sanitizedAst.get();
sanitizedModule = Sanitize(module, states.optionValues);
targetAst = sanitizedModule->rootNode.get();
}
else
targetAst = &shader;
targetAst = module.rootNode.get();
ShaderAst::StatementPtr optimizedAst;
@ -210,7 +210,7 @@ namespace Nz
return s_flipYUniformName;
}
ShaderAst::StatementPtr GlslWriter::Sanitize(ShaderAst::Statement& ast, std::unordered_map<std::size_t, ShaderAst::ConstantValue> optionValues, std::string* error)
ShaderAst::ModulePtr GlslWriter::Sanitize(ShaderAst::Module& module, std::unordered_map<std::size_t, ShaderAst::ConstantValue> optionValues, std::string* error)
{
// Always sanitize for reserved identifiers
ShaderAst::SanitizeVisitor::Options options;
@ -228,7 +228,7 @@ namespace Nz
"cross", "dot", "exp", "length", "max", "min", "pow", "texture"
};
return ShaderAst::Sanitize(ast, options, error);
return ShaderAst::Sanitize(module, options, error);
}
void GlslWriter::Append(const ShaderAst::ArrayType& /*type*/)
@ -856,6 +856,8 @@ namespace Nz
if constexpr (std::is_same_v<T, ShaderAst::NoValue>)
throw std::runtime_error("invalid type (value expected)");
else if constexpr (std::is_same_v<T, std::string>)
throw std::runtime_error("unexpected string litteral");
else if constexpr (std::is_same_v<T, bool>)
Append((arg) ? "true" : "false");
else if constexpr (std::is_same_v<T, float> || std::is_same_v<T, Int32>)

View File

@ -76,6 +76,13 @@ namespace Nz
inline bool HasValue() const { return locationIndex.HasValue(); }
};
struct LangWriter::NzslAttribute
{
const ShaderAst::ExpressionValue<UInt32>& version;
inline bool HasValue() const { return version.HasValue(); }
};
struct LangWriter::SetAttribute
{
const ShaderAst::ExpressionValue<UInt32>& setIndex;
@ -101,7 +108,7 @@ namespace Nz
unsigned int indentLevel = 0;
};
std::string LangWriter::Generate(ShaderAst::Statement& shader, const States& /*states*/)
std::string LangWriter::Generate(ShaderAst::Module& module, const States& /*states*/)
{
State state;
m_currentState = &state;
@ -110,11 +117,11 @@ namespace Nz
m_currentState = nullptr;
});
ShaderAst::StatementPtr sanitizedAst = ShaderAst::Sanitize(shader);
ShaderAst::ModulePtr sanitizedModule = ShaderAst::Sanitize(module);
AppendHeader();
sanitizedAst->Visit(*this);
sanitizedModule->rootNode->Visit(*this);
return state.stream.str();
}
@ -453,6 +460,10 @@ namespace Nz
Append(")");
}
void LangWriter::AppendAttribute(NzslAttribute nzslVersion)
{
}
void LangWriter::AppendAttribute(SetAttribute set)
{
if (!set.HasValue())
@ -766,6 +777,8 @@ namespace Nz
Append((arg) ? "true" : "false");
else if constexpr (std::is_same_v<T, float> || std::is_same_v<T, Int32> || std::is_same_v<T, UInt32>)
Append(std::to_string(arg));
else if constexpr (std::is_same_v<T, std::string>)
Append('"', arg, '"'); //< TODO: Escape string
else if constexpr (std::is_same_v<T, Vector2f>)
Append("vec2[f32](" + std::to_string(arg.x) + ", " + std::to_string(arg.y) + ")");
else if constexpr (std::is_same_v<T, Vector2i32>)

View File

@ -51,6 +51,7 @@ namespace Nz::ShaderLang
{ "if", TokenType::If },
{ "in", TokenType::In },
{ "let", TokenType::Let },
{ "module", TokenType::Module },
{ "option", TokenType::Option },
{ "return", TokenType::Return },
{ "struct", TokenType::Struct },
@ -65,7 +66,7 @@ namespace Nz::ShaderLang
if (currentPos + advance < str.size() && str[currentPos + advance] != '\0')
return str[currentPos + advance];
else
return char(-1);
return '\0';
};
auto IsAlphaNum = [&](const char c)
@ -85,7 +86,7 @@ namespace Nz::ShaderLang
token.column = static_cast<unsigned int>(currentPos - lastLineFeed);
token.line = lineNumber;
if (c == -1)
if (c == '\0')
{
token.type = TokenType::EndOfStream;
tokens.push_back(std::move(token));
@ -385,6 +386,55 @@ namespace Nz::ShaderLang
case '[': tokenType = TokenType::OpenSquareBracket; break;
case ']': tokenType = TokenType::ClosingSquareBracket; break;
case '"':
{
// string litteral
currentPos++;
std::string litteral;
char current;
while ((current = Peek(0)) != '"')
{
char character;
switch (current)
{
case '\0':
case '\n':
case '\r':
throw UnfinishedString{};
case '\\':
{
currentPos++;
char next = Peek();
switch (next)
{
case 'n': character = '\n'; break;
case 'r': character = '\r'; break;
case 't': character = '\t'; break;
case '"': character = '"'; break;
case '\\': character = '\\'; break;
default:
throw UnrecognizedChar{};
}
break;
}
default:
character = current;
break;
}
litteral.push_back(character);
currentPos++;
}
tokenType = TokenType::StringValue;
token.data = std::move(litteral);
break;
}
default:
{
if (IsAlphaNum(c))

View File

@ -7,6 +7,7 @@
#include <Nazara/Core/File.hpp>
#include <Nazara/Shader/ShaderBuilder.hpp>
#include <cassert>
#include <regex>
#include <Nazara/Shader/Debug.hpp>
namespace Nz::ShaderLang
@ -29,6 +30,7 @@ namespace Nz::ShaderLang
{ "entry", ShaderAst::AttributeType::Entry },
{ "layout", ShaderAst::AttributeType::Layout },
{ "location", ShaderAst::AttributeType::Location },
{ "nzsl_version", ShaderAst::AttributeType::LangVersion },
{ "set", ShaderAst::AttributeType::Set },
{ "unroll", ShaderAst::AttributeType::Unroll },
};
@ -106,18 +108,24 @@ namespace Nz::ShaderLang
}
}
ShaderAst::StatementPtr Parser::Parse(const std::vector<Token>& tokens)
ShaderAst::ModulePtr Parser::Parse(const std::vector<Token>& tokens)
{
Context context;
context.tokenCount = tokens.size();
context.tokens = tokens.data();
context.root = std::make_unique<ShaderAst::MultiStatement>();
m_context = &context;
std::vector<ShaderAst::ExprValue> attributes;
auto EnsureModule = [this]() -> ShaderAst::Module&
{
if (!m_context->module)
throw UnexpectedToken{ "unexpected token before module declaration" };
return *m_context->module;
};
bool reachedEndOfStream = false;
while (!reachedEndOfStream)
{
@ -125,11 +133,14 @@ namespace Nz::ShaderLang
switch (nextToken.type)
{
case TokenType::Const:
{
if (!attributes.empty())
throw UnexpectedToken{};
context.root->statements.push_back(ParseConstStatement());
const auto& module = EnsureModule();
module.rootNode->statements.push_back(ParseConstStatement());
break;
}
case TokenType::EndOfStream:
if (!attributes.empty())
@ -139,38 +150,58 @@ namespace Nz::ShaderLang
break;
case TokenType::External:
context.root->statements.push_back(ParseExternalBlock(std::move(attributes)));
{
const auto& module = EnsureModule();
module.rootNode->statements.push_back(ParseExternalBlock(std::move(attributes)));
attributes.clear();
break;
}
case TokenType::OpenSquareBracket:
assert(attributes.empty());
attributes = ParseAttributes();
break;
case TokenType::Module:
if (attributes.empty())
throw UnexpectedToken{};
ParseModuleStatement(std::move(attributes));
attributes.clear();
break;
case TokenType::Option:
{
if (!attributes.empty())
throw UnexpectedToken{};
context.root->statements.push_back(ParseOptionDeclaration());
const auto& module = EnsureModule();
module.rootNode->statements.push_back(ParseOptionDeclaration());
break;
}
case TokenType::FunctionDeclaration:
context.root->statements.push_back(ParseFunctionDeclaration(std::move(attributes)));
{
const auto& module = EnsureModule();
module.rootNode->statements.push_back(ParseFunctionDeclaration(std::move(attributes)));
attributes.clear();
break;
}
case TokenType::Struct:
context.root->statements.push_back(ParseStructDeclaration(std::move(attributes)));
{
const auto& module = EnsureModule();
module.rootNode->statements.push_back(ParseStructDeclaration(std::move(attributes)));
attributes.clear();
break;
}
default:
throw UnexpectedToken{};
}
}
return std::move(context.root);
return std::move(context.module);
}
const Token& Parser::Advance()
@ -604,6 +635,74 @@ namespace Nz::ShaderLang
return { parameterName, std::move(parameterType) };
}
void Parser::ParseModuleStatement(std::vector<ShaderAst::ExprValue> attributes)
{
Expect(Advance(), TokenType::Module);
if (m_context->module)
throw DuplicateModule{ "you must set one module statement per file" };
std::optional<UInt32> moduleVersion;
for (auto&& [attributeType, arg] : attributes)
{
switch (attributeType)
{
case ShaderAst::AttributeType::LangVersion:
{
// Version parsing
if (moduleVersion.has_value())
throw AttributeError{ "attribute " + std::string("nzsl_version") + " must be present once" };
if (!arg)
throw AttributeError{ "attribute " + std::string("nzsl_version") + " requires a parameter"};
const ShaderAst::ExpressionPtr& expr = *arg;
if (expr->GetType() != ShaderAst::NodeType::ConstantValueExpression)
throw AttributeError{ "attribute " + std::string("nzsl_version") + " expect a single string parameter" };
auto& constantValue = SafeCast<ShaderAst::ConstantValueExpression&>(*expr);
if (ShaderAst::GetExpressionType(constantValue.value) != ShaderAst::ExpressionType{ ShaderAst::PrimitiveType::String })
throw AttributeError{ "attribute " + std::string("nzsl_version") + " expect a single string parameter" };
const std::string& versionStr = std::get<std::string>(constantValue.value);
std::regex versionRegex(R"(^(\d+)(\.(\d+)(\.(\d+))?)?$)", std::regex::ECMAScript);
std::smatch versionMatch;
if (!std::regex_match(versionStr, versionMatch, versionRegex))
throw AttributeError("invalid version for attribute nzsl");
assert(versionMatch.size() == 6);
std::uint32_t version = 0;
version += std::stoi(versionMatch[1]) * 100;
if (versionMatch.length(3) > 0)
version += std::stoi(versionMatch[3]) * 10;
if (versionMatch.length(5) > 0)
version += std::stoi(versionMatch[5]) * 1;
moduleVersion = version;
break;
}
default:
throw AttributeError{ "unhandled attribute for module" };
}
}
if (!moduleVersion.has_value())
throw AttributeError{ "missing module version" };
m_context->module = std::make_shared<ShaderAst::Module>();
m_context->module->rootNode = ShaderBuilder::MultiStatement();
m_context->module->shaderLangVersion = *moduleVersion;
Expect(Advance(), TokenType::Semicolon);
}
ShaderAst::StatementPtr Parser::ParseOptionDeclaration()
{
Expect(Advance(), TokenType::Option);
@ -1132,11 +1231,20 @@ namespace Nz::ShaderLang
case TokenType::OpenParenthesis:
return ParseParenthesisExpression();
case TokenType::StringValue:
return ParseStringExpression();
default:
throw UnexpectedToken{};
}
}
ShaderAst::ExpressionPtr Parser::ParseStringExpression()
{
const Token& litteralToken = Expect(Advance(), TokenType::StringValue);
return ShaderBuilder::Constant(std::get<std::string>(litteralToken.data));
}
ShaderAst::AttributeType Parser::ParseIdentifierAsAttributeType()
{
const Token& identifierToken = Expect(Advance(), TokenType::Identifier);
@ -1192,7 +1300,7 @@ namespace Nz::ShaderLang
}
}
ShaderAst::StatementPtr ParseFromFile(const std::filesystem::path& sourcePath)
ShaderAst::ModulePtr ParseFromFile(const std::filesystem::path& sourcePath)
{
File file(sourcePath);
if (!file.Open(OpenMode::ReadOnly | OpenMode::Text))

View File

@ -465,6 +465,8 @@ namespace Nz
if constexpr (std::is_same_v<T, ShaderAst::NoValue>)
throw std::runtime_error("invalid type (value expected)");
else if constexpr (std::is_same_v<T, std::string>)
throw std::runtime_error("unexpected string litteral");
else if constexpr (std::is_same_v<T, bool>)
return ConstantBool{ arg };
else if constexpr (std::is_same_v<T, float> || std::is_same_v<T, Int32> || std::is_same_v<T, UInt32>)

View File

@ -496,11 +496,10 @@ namespace Nz
{
}
std::vector<UInt32> SpirvWriter::Generate(ShaderAst::Statement& shader, const States& states)
std::vector<UInt32> SpirvWriter::Generate(ShaderAst::Module& module, const States& states)
{
ShaderAst::Statement* targetAst = &shader;
ShaderAst::StatementPtr sanitizedAst;
ShaderAst::ModulePtr sanitizedModule;
ShaderAst::Statement* targetAst;
if (!states.sanitized)
{
ShaderAst::SanitizeVisitor::Options options;
@ -512,9 +511,11 @@ namespace Nz
options.splitMultipleBranches = true;
options.useIdentifierAccessesForStructs = false;
sanitizedAst = ShaderAst::Sanitize(shader, options);
targetAst = sanitizedAst.get();
sanitizedModule = ShaderAst::Sanitize(module, options);
targetAst = sanitizedModule->rootNode.get();
}
else
targetAst = module.rootNode.get();
ShaderAst::StatementPtr optimizedAst;
if (states.optimize)

View File

@ -61,10 +61,10 @@ namespace Nz
return pipelineLayout;
}
std::shared_ptr<ShaderModule> VulkanDevice::InstantiateShaderModule(ShaderStageTypeFlags stages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states)
std::shared_ptr<ShaderModule> VulkanDevice::InstantiateShaderModule(ShaderStageTypeFlags stages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states)
{
auto stage = std::make_shared<VulkanShaderModule>();
if (!stage->Create(*this, stages, shaderAst, states))
if (!stage->Create(*this, stages, shaderModule, states))
throw std::runtime_error("failed to instantiate vulkan shader module");
return stage;

View File

@ -57,14 +57,14 @@ namespace Nz
};
}
bool VulkanShaderModule::Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderAst::Statement& shaderAst, const ShaderWriter::States& states)
bool VulkanShaderModule::Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states)
{
SpirvWriter::Environment env;
SpirvWriter writer;
writer.SetEnv(env);
std::vector<UInt32> code = writer.Generate(shaderAst, states);
std::vector<UInt32> code = writer.Generate(shaderModule, states);
return Create(device, shaderStages, ShaderLanguage::SpirV, code.data(), code.size() * sizeof(UInt32), {});
}
@ -88,8 +88,8 @@ namespace Nz
std::vector<ShaderLang::Token> tokens = ShaderLang::Tokenize(std::string_view(static_cast<const char*>(source), sourceSize));
ShaderLang::Parser parser;
ShaderAst::StatementPtr shaderAst = parser.Parse(tokens);
return Create(device, shaderStages, *shaderAst, states);
ShaderAst::ModulePtr shaderModule = parser.Parse(tokens);
return Create(device, shaderStages, *shaderModule, states);
}
case ShaderLanguage::SpirV: