Shader/Modules: proof of concept
This commit is contained in:
@@ -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::Module& shaderModule, const ShaderWriter::States& states) override;
|
||||
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, const 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;
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace Nz
|
||||
class NAZARA_OPENGLRENDERER_API OpenGLShaderModule : public ShaderModule
|
||||
{
|
||||
public:
|
||||
OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states = {});
|
||||
OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, const 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::Module& shaderModule, const ShaderWriter::States& states);
|
||||
void Create(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, const ShaderAst::Module& shaderModule, const ShaderWriter::States& states);
|
||||
|
||||
static void CheckCompilationStatus(GL::Shader& shader);
|
||||
|
||||
|
||||
@@ -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::Module& shaderModule, const ShaderWriter::States& states) = 0;
|
||||
virtual std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, const 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;
|
||||
|
||||
62
include/Nazara/Shader/Ast/IndexRemapperVisitor.hpp
Normal file
62
include/Nazara/Shader/Ast/IndexRemapperVisitor.hpp
Normal file
@@ -0,0 +1,62 @@
|
||||
// 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_INDEXREMAPPER_HPP
|
||||
#define NAZARA_SHADER_AST_INDEXREMAPPER_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Shader/Config.hpp>
|
||||
#include <Nazara/Shader/Ast/AstCloner.hpp>
|
||||
#include <functional>
|
||||
|
||||
namespace Nz::ShaderAst
|
||||
{
|
||||
class NAZARA_SHADER_API IndexRemapperVisitor : public AstCloner
|
||||
{
|
||||
public:
|
||||
struct Callbacks;
|
||||
|
||||
IndexRemapperVisitor() = default;
|
||||
IndexRemapperVisitor(const IndexRemapperVisitor&) = delete;
|
||||
IndexRemapperVisitor(IndexRemapperVisitor&&) = delete;
|
||||
~IndexRemapperVisitor() = default;
|
||||
|
||||
StatementPtr Clone(Statement& statement, const Callbacks& callbacks);
|
||||
|
||||
IndexRemapperVisitor& operator=(const IndexRemapperVisitor&) = delete;
|
||||
IndexRemapperVisitor& operator=(IndexRemapperVisitor&&) = delete;
|
||||
|
||||
struct Callbacks
|
||||
{
|
||||
std::function<std::size_t(std::size_t previousIndex)> constIndexGenerator;
|
||||
std::function<std::size_t(std::size_t previousIndex)> funcIndexGenerator;
|
||||
std::function<std::size_t(std::size_t previousIndex) > structIndexGenerator;
|
||||
//std::function<std::size_t()> typeIndexGenerator;
|
||||
std::function<std::size_t(std::size_t previousIndex)> varIndexGenerator;
|
||||
};
|
||||
|
||||
private:
|
||||
StatementPtr Clone(DeclareConstStatement& node) override;
|
||||
StatementPtr Clone(DeclareExternalStatement& node) override;
|
||||
StatementPtr Clone(DeclareFunctionStatement& node) override;
|
||||
StatementPtr Clone(DeclareStructStatement& node) override;
|
||||
StatementPtr Clone(DeclareVariableStatement& node) override;
|
||||
|
||||
ExpressionPtr Clone(CallFunctionExpression& node) override;
|
||||
ExpressionPtr Clone(VariableExpression& node) override;
|
||||
|
||||
void HandleType(ExpressionValue<ExpressionType>& exprType);
|
||||
|
||||
struct Context;
|
||||
Context* m_context;
|
||||
};
|
||||
|
||||
inline StatementPtr RemapIndices(Statement& statement, const IndexRemapperVisitor::Callbacks& callbacks);
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/Ast/IndexRemapperVisitor.inl>
|
||||
|
||||
#endif // NAZARA_SHADER_AST_INDEXREMAPPER_HPP
|
||||
17
include/Nazara/Shader/Ast/IndexRemapperVisitor.inl
Normal file
17
include/Nazara/Shader/Ast/IndexRemapperVisitor.inl
Normal file
@@ -0,0 +1,17 @@
|
||||
// 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/Ast/IndexRemapperVisitor.hpp>
|
||||
#include <Nazara/Shader/Debug.hpp>
|
||||
|
||||
namespace Nz::ShaderAst
|
||||
{
|
||||
StatementPtr RemapIndices(Statement& statement, const IndexRemapperVisitor::Callbacks& callbacks)
|
||||
{
|
||||
IndexRemapperVisitor visitor;
|
||||
return visitor.Clone(statement, callbacks);
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/DebugOff.hpp>
|
||||
@@ -250,6 +250,7 @@ namespace Nz::ShaderAst
|
||||
void Visit(AstStatementVisitor& visitor) override;
|
||||
|
||||
std::optional<std::size_t> constIndex;
|
||||
std::optional<bool> hidden;
|
||||
std::string name;
|
||||
ExpressionValue<ExpressionType> type;
|
||||
ExpressionPtr expression;
|
||||
@@ -269,8 +270,9 @@ namespace Nz::ShaderAst
|
||||
ExpressionValue<ExpressionType> type;
|
||||
};
|
||||
|
||||
ExpressionValue<UInt32> bindingSet;
|
||||
std::vector<ExternalVar> externalVars;
|
||||
std::optional<bool> hidden;
|
||||
ExpressionValue<UInt32> bindingSet;
|
||||
};
|
||||
|
||||
struct NAZARA_SHADER_API DeclareFunctionStatement : Statement
|
||||
@@ -280,12 +282,13 @@ namespace Nz::ShaderAst
|
||||
|
||||
struct Parameter
|
||||
{
|
||||
std::string name;
|
||||
ExpressionValue<ExpressionType> type;
|
||||
std::optional<std::size_t> varIndex;
|
||||
std::string name;
|
||||
};
|
||||
|
||||
std::optional<std::size_t> funcIndex;
|
||||
std::optional<std::size_t> varIndex;
|
||||
std::optional<bool> hidden;
|
||||
std::string name;
|
||||
std::vector<Parameter> parameters;
|
||||
std::vector<StatementPtr> statements;
|
||||
@@ -301,6 +304,7 @@ namespace Nz::ShaderAst
|
||||
void Visit(AstStatementVisitor& visitor) override;
|
||||
|
||||
std::optional<std::size_t> optIndex;
|
||||
std::optional<bool> hidden;
|
||||
std::string optName;
|
||||
ExpressionPtr defaultValue;
|
||||
ExpressionValue<ExpressionType> optType;
|
||||
@@ -312,6 +316,7 @@ namespace Nz::ShaderAst
|
||||
void Visit(AstStatementVisitor& visitor) override;
|
||||
|
||||
std::optional<std::size_t> structIndex;
|
||||
std::optional<bool> hidden;
|
||||
ExpressionValue<bool> isExported;
|
||||
StructDescription description;
|
||||
};
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <Nazara/Shader/Ast/AstCloner.hpp>
|
||||
#include <Nazara/Shader/Ast/AstTypes.hpp>
|
||||
#include <Nazara/Shader/Ast/Module.hpp>
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
@@ -41,6 +42,7 @@ namespace Nz::ShaderAst
|
||||
|
||||
struct Options
|
||||
{
|
||||
std::function<ModulePtr(const std::vector<std::string>& /*modulePath*/)> moduleCallback;
|
||||
std::unordered_set<std::string> reservedIdentifiers;
|
||||
std::unordered_map<std::size_t, ConstantValue> optionValues;
|
||||
bool makeVariableNameUnique = false;
|
||||
@@ -112,13 +114,13 @@ namespace Nz::ShaderAst
|
||||
void PropagateFunctionFlags(std::size_t funcIndex, FunctionFlags flags, Bitset<>& seen);
|
||||
|
||||
void RegisterBuiltin();
|
||||
std::size_t RegisterConstant(std::string name, ConstantValue value, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterFunction(std::string name, FunctionData funcData, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterIntrinsic(std::string name, IntrinsicType type, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterStruct(std::string name, StructDescription* description, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterType(std::string name, ExpressionType expressionType, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterType(std::string name, PartialType partialType, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterVariable(std::string name, ExpressionType type, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterConstant(std::string name, ConstantValue value, bool hidden = false, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterFunction(std::string name, FunctionData funcData, bool hidden = false, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterIntrinsic(std::string name, IntrinsicType type, bool hidden = false, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterStruct(std::string name, StructDescription* description, bool hidden = false, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterType(std::string name, ExpressionType expressionType, bool hidden = false, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterType(std::string name, PartialType partialType, bool hidden = false, std::optional<std::size_t> index = {});
|
||||
std::size_t RegisterVariable(std::string name, ExpressionType type, bool hidden = false, std::optional<std::size_t> index = {});
|
||||
|
||||
void ResolveFunctions();
|
||||
const ExpressionPtr& ResolveCondExpression(ConditionalExpression& node);
|
||||
|
||||
@@ -32,8 +32,8 @@ namespace Nz
|
||||
GlslWriter(GlslWriter&&) = delete;
|
||||
~GlslWriter() = default;
|
||||
|
||||
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 = {});
|
||||
inline std::string Generate(const ShaderAst::Module& module, const BindingMapping& bindingMapping = {}, const States& states = {});
|
||||
std::string Generate(std::optional<ShaderStageType> shaderStage, const ShaderAst::Module& module, const BindingMapping& bindingMapping = {}, const States& states = {});
|
||||
|
||||
void SetEnv(Environment environment);
|
||||
|
||||
@@ -48,7 +48,7 @@ namespace Nz
|
||||
};
|
||||
|
||||
static const char* GetFlipYUniformName();
|
||||
static ShaderAst::ModulePtr Sanitize(ShaderAst::Module& module, std::unordered_map<std::size_t, ShaderAst::ConstantValue> optionValues, std::string* error = nullptr);
|
||||
static ShaderAst::ModulePtr Sanitize(const ShaderAst::Module& module, std::unordered_map<std::size_t, ShaderAst::ConstantValue> optionValues, std::string* error = nullptr);
|
||||
|
||||
private:
|
||||
void Append(const ShaderAst::ArrayType& type);
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace Nz
|
||||
{
|
||||
}
|
||||
|
||||
inline std::string GlslWriter::Generate(ShaderAst::Module& shader, const BindingMapping& bindingMapping, const States& states)
|
||||
inline std::string GlslWriter::Generate(const ShaderAst::Module& shader, const BindingMapping& bindingMapping, const States& states)
|
||||
{
|
||||
return Generate(std::nullopt, shader, bindingMapping, states);
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace Nz
|
||||
LangWriter(LangWriter&&) = delete;
|
||||
~LangWriter() = default;
|
||||
|
||||
std::string Generate(ShaderAst::Module& module, const States& conditions = {});
|
||||
std::string Generate(const ShaderAst::Module& module, const States& conditions = {});
|
||||
|
||||
void SetEnv(Environment environment);
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ namespace Nz
|
||||
SpirvWriter(SpirvWriter&&) = delete;
|
||||
~SpirvWriter() = default;
|
||||
|
||||
std::vector<UInt32> Generate(ShaderAst::Module& module, const States& states = {});
|
||||
std::vector<UInt32> Generate(const ShaderAst::Module& module, const States& states = {});
|
||||
|
||||
void SetEnv(Environment environment);
|
||||
|
||||
|
||||
@@ -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::Module& shaderModule, const ShaderWriter::States& states) override;
|
||||
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags stages, const 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;
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace Nz
|
||||
VulkanShaderModule(VulkanShaderModule&&) = delete;
|
||||
~VulkanShaderModule() = default;
|
||||
|
||||
bool Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states);
|
||||
bool Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, const 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;
|
||||
|
||||
Reference in New Issue
Block a user