Shader/Modules: proof of concept

This commit is contained in:
Jérôme Leclercq
2022-03-05 04:07:18 +01:00
parent 7dab1d735f
commit 43ac86e85c
29 changed files with 667 additions and 192 deletions

View File

@@ -2,27 +2,44 @@
#include <Nazara/Platform.hpp> #include <Nazara/Platform.hpp>
#include <Nazara/Renderer.hpp> #include <Nazara/Renderer.hpp>
#include <Nazara/Shader.hpp> #include <Nazara/Shader.hpp>
#include <Nazara/Shader/SpirvConstantCache.hpp> #include <Nazara/Shader/Ast/SanitizeVisitor.hpp>
#include <Nazara/Shader/SpirvPrinter.hpp>
#include <Nazara/Utility.hpp> #include <Nazara/Utility.hpp>
#include <array> #include <array>
#include <iostream> #include <iostream>
NAZARA_REQUEST_DEDICATED_GPU() NAZARA_REQUEST_DEDICATED_GPU()
const char shaderSource[] = R"( const char moduleSource[] = R"(
[nzsl_version("1.0")] [nzsl_version("1.0")]
module; module;
option red: bool = false; fn dummy() {}
[layout(std140)]
struct Bar
{
}
struct Foo {}
[export]
[layout(std140)] [layout(std140)]
struct Data struct Data
{ {
projectionMatrix: mat4[f32], projectionMatrix: mat4[f32],
worldMatrix: mat4[f32], worldMatrix: mat4[f32],
viewMatrix: mat4[f32] viewMatrix: mat4[f32],
pilier: Bar
} }
)";
const char shaderSource[] = R"(
[nzsl_version("1.0")]
module;
import Test/module_test;
option red: bool = false;
[set(0)] [set(0)]
external external
@@ -105,10 +122,42 @@ int main()
return __LINE__; return __LINE__;
} }
Nz::ShaderAst::ModulePtr shaderModule = Nz::ShaderLang::Parse(std::string_view(shaderSource, sizeof(shaderSource)));
if (!shaderModule)
{
std::cout << "Failed to parse shader module" << std::endl;
return __LINE__;
}
Nz::ShaderAst::SanitizeVisitor::Options sanitizeOpt;
sanitizeOpt.moduleCallback = [](const std::vector<std::string>& modulePath) -> Nz::ShaderAst::ModulePtr
{
if (modulePath.size() != 2)
return {};
if (modulePath[0] != "Test")
return {};
if (modulePath[1] != "module_test")
return {};
return Nz::ShaderLang::Parse(std::string_view(moduleSource, sizeof(moduleSource)));
};
shaderModule = Nz::ShaderAst::Sanitize(*shaderModule, sanitizeOpt);
if (!shaderModule)
{
std::cout << "Failed to compile shader module" << std::endl;
return __LINE__;
}
Nz::LangWriter langWriter;
std::cout << langWriter.Generate(*shaderModule) << std::endl;
Nz::ShaderWriter::States states; Nz::ShaderWriter::States states;
states.optimize = true; states.optimize = true;
auto fragVertShader = device->InstantiateShaderModule(Nz::ShaderStageType::Fragment | Nz::ShaderStageType::Vertex, Nz::ShaderLanguage::NazaraShader, shaderSource, sizeof(shaderSource), states); auto fragVertShader = device->InstantiateShaderModule(Nz::ShaderStageType::Fragment | Nz::ShaderStageType::Vertex, *shaderModule, states);
if (!fragVertShader) if (!fragVertShader)
{ {
std::cout << "Failed to instantiate shader" << std::endl; std::cout << "Failed to instantiate shader" << std::endl;
@@ -116,7 +165,7 @@ int main()
} }
Nz::MeshParams meshParams; Nz::MeshParams meshParams;
meshParams.bufferFactory = GetRenderBufferFactory(device); meshParams.bufferFactory = Nz::GetRenderBufferFactory(device);
meshParams.center = true; meshParams.center = true;
meshParams.matrix = Nz::Matrix4f::Rotate(Nz::EulerAnglesf(0.f, -90.f, 0.f)) * Nz::Matrix4f::Scale(Nz::Vector3f(0.002f)); meshParams.matrix = Nz::Matrix4f::Rotate(Nz::EulerAnglesf(0.f, -90.f, 0.f)) * Nz::Matrix4f::Scale(Nz::Vector3f(0.002f));
meshParams.vertexDeclaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_Normal_UV); meshParams.vertexDeclaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_Normal_UV);

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<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<RenderPipeline> InstantiateRenderPipeline(RenderPipelineInfo pipelineInfo) override;
std::shared_ptr<RenderPipelineLayout> InstantiateRenderPipelineLayout(RenderPipelineLayoutInfo pipelineLayoutInfo) 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<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<Texture> InstantiateTexture(const TextureInfo& params) override;
std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) override; std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) override;

View File

@@ -22,7 +22,7 @@ namespace Nz
class NAZARA_OPENGLRENDERER_API OpenGLShaderModule : public ShaderModule class NAZARA_OPENGLRENDERER_API OpenGLShaderModule : public ShaderModule
{ {
public: 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(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize, const ShaderWriter::States& states = {});
OpenGLShaderModule(const OpenGLShaderModule&) = delete; OpenGLShaderModule(const OpenGLShaderModule&) = delete;
OpenGLShaderModule(OpenGLShaderModule&&) noexcept = default; OpenGLShaderModule(OpenGLShaderModule&&) noexcept = default;
@@ -34,7 +34,7 @@ namespace Nz
OpenGLShaderModule& operator=(OpenGLShaderModule&&) noexcept = default; OpenGLShaderModule& operator=(OpenGLShaderModule&&) noexcept = default;
private: 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); static void CheckCompilationStatus(GL::Shader& shader);

View File

@@ -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<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<RenderPipeline> InstantiateRenderPipeline(RenderPipelineInfo pipelineInfo) = 0;
virtual std::shared_ptr<RenderPipelineLayout> InstantiateRenderPipelineLayout(RenderPipelineLayoutInfo pipelineLayoutInfo) = 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; 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); 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; virtual std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params) = 0;

View 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

View 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>

View File

@@ -250,6 +250,7 @@ namespace Nz::ShaderAst
void Visit(AstStatementVisitor& visitor) override; void Visit(AstStatementVisitor& visitor) override;
std::optional<std::size_t> constIndex; std::optional<std::size_t> constIndex;
std::optional<bool> hidden;
std::string name; std::string name;
ExpressionValue<ExpressionType> type; ExpressionValue<ExpressionType> type;
ExpressionPtr expression; ExpressionPtr expression;
@@ -269,8 +270,9 @@ namespace Nz::ShaderAst
ExpressionValue<ExpressionType> type; ExpressionValue<ExpressionType> type;
}; };
ExpressionValue<UInt32> bindingSet;
std::vector<ExternalVar> externalVars; std::vector<ExternalVar> externalVars;
std::optional<bool> hidden;
ExpressionValue<UInt32> bindingSet;
}; };
struct NAZARA_SHADER_API DeclareFunctionStatement : Statement struct NAZARA_SHADER_API DeclareFunctionStatement : Statement
@@ -280,12 +282,13 @@ namespace Nz::ShaderAst
struct Parameter struct Parameter
{ {
std::string name;
ExpressionValue<ExpressionType> type; ExpressionValue<ExpressionType> type;
std::optional<std::size_t> varIndex;
std::string name;
}; };
std::optional<std::size_t> funcIndex; std::optional<std::size_t> funcIndex;
std::optional<std::size_t> varIndex; std::optional<bool> hidden;
std::string name; std::string name;
std::vector<Parameter> parameters; std::vector<Parameter> parameters;
std::vector<StatementPtr> statements; std::vector<StatementPtr> statements;
@@ -301,6 +304,7 @@ namespace Nz::ShaderAst
void Visit(AstStatementVisitor& visitor) override; void Visit(AstStatementVisitor& visitor) override;
std::optional<std::size_t> optIndex; std::optional<std::size_t> optIndex;
std::optional<bool> hidden;
std::string optName; std::string optName;
ExpressionPtr defaultValue; ExpressionPtr defaultValue;
ExpressionValue<ExpressionType> optType; ExpressionValue<ExpressionType> optType;
@@ -312,6 +316,7 @@ namespace Nz::ShaderAst
void Visit(AstStatementVisitor& visitor) override; void Visit(AstStatementVisitor& visitor) override;
std::optional<std::size_t> structIndex; std::optional<std::size_t> structIndex;
std::optional<bool> hidden;
ExpressionValue<bool> isExported; ExpressionValue<bool> isExported;
StructDescription description; StructDescription description;
}; };

View File

@@ -13,6 +13,7 @@
#include <Nazara/Shader/Ast/AstCloner.hpp> #include <Nazara/Shader/Ast/AstCloner.hpp>
#include <Nazara/Shader/Ast/AstTypes.hpp> #include <Nazara/Shader/Ast/AstTypes.hpp>
#include <Nazara/Shader/Ast/Module.hpp> #include <Nazara/Shader/Ast/Module.hpp>
#include <functional>
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#include <vector> #include <vector>
@@ -41,6 +42,7 @@ namespace Nz::ShaderAst
struct Options struct Options
{ {
std::function<ModulePtr(const std::vector<std::string>& /*modulePath*/)> moduleCallback;
std::unordered_set<std::string> reservedIdentifiers; std::unordered_set<std::string> reservedIdentifiers;
std::unordered_map<std::size_t, ConstantValue> optionValues; std::unordered_map<std::size_t, ConstantValue> optionValues;
bool makeVariableNameUnique = false; bool makeVariableNameUnique = false;
@@ -112,13 +114,13 @@ namespace Nz::ShaderAst
void PropagateFunctionFlags(std::size_t funcIndex, FunctionFlags flags, Bitset<>& seen); void PropagateFunctionFlags(std::size_t funcIndex, FunctionFlags flags, Bitset<>& seen);
void RegisterBuiltin(); void RegisterBuiltin();
std::size_t RegisterConstant(std::string name, ConstantValue value, 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, 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, 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, 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, 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, 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, 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(); void ResolveFunctions();
const ExpressionPtr& ResolveCondExpression(ConditionalExpression& node); const ExpressionPtr& ResolveCondExpression(ConditionalExpression& node);

View File

@@ -32,8 +32,8 @@ namespace Nz
GlslWriter(GlslWriter&&) = delete; GlslWriter(GlslWriter&&) = delete;
~GlslWriter() = default; ~GlslWriter() = default;
inline std::string Generate(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, 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); void SetEnv(Environment environment);
@@ -48,7 +48,7 @@ namespace Nz
}; };
static const char* GetFlipYUniformName(); 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: private:
void Append(const ShaderAst::ArrayType& type); void Append(const ShaderAst::ArrayType& type);

View File

@@ -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); return Generate(std::nullopt, shader, bindingMapping, states);
} }

View File

@@ -29,7 +29,7 @@ namespace Nz
LangWriter(LangWriter&&) = delete; LangWriter(LangWriter&&) = delete;
~LangWriter() = default; ~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); void SetEnv(Environment environment);

View File

@@ -36,7 +36,7 @@ namespace Nz
SpirvWriter(SpirvWriter&&) = delete; SpirvWriter(SpirvWriter&&) = delete;
~SpirvWriter() = default; ~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); 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<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<RenderPipeline> InstantiateRenderPipeline(RenderPipelineInfo pipelineInfo) override;
std::shared_ptr<RenderPipelineLayout> InstantiateRenderPipelineLayout(RenderPipelineLayoutInfo pipelineLayoutInfo) 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<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<Texture> InstantiateTexture(const TextureInfo& params) override;
std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) override; std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) override;

View File

@@ -27,7 +27,7 @@ namespace Nz
VulkanShaderModule(VulkanShaderModule&&) = delete; VulkanShaderModule(VulkanShaderModule&&) = delete;
~VulkanShaderModule() = default; ~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); 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; inline const Vk::ShaderModule& GetHandle() const;

View File

@@ -144,7 +144,7 @@ namespace Nz
return std::make_shared<OpenGLRenderPipelineLayout>(std::move(pipelineLayoutInfo)); return std::make_shared<OpenGLRenderPipelineLayout>(std::move(pipelineLayoutInfo));
} }
std::shared_ptr<ShaderModule> OpenGLDevice::InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states) std::shared_ptr<ShaderModule> OpenGLDevice::InstantiateShaderModule(ShaderStageTypeFlags shaderStages, const ShaderAst::Module& shaderModule, const ShaderWriter::States& states)
{ {
return std::make_shared<OpenGLShaderModule>(*this, shaderStages, shaderModule, states); return std::make_shared<OpenGLShaderModule>(*this, shaderStages, shaderModule, states);
} }

View File

@@ -13,7 +13,7 @@
namespace Nz namespace Nz
{ {
OpenGLShaderModule::OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states) : OpenGLShaderModule::OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, const ShaderAst::Module& shaderModule, const ShaderWriter::States& states) :
m_device(device) m_device(device)
{ {
NazaraAssert(shaderStages != 0, "at least one shader stage must be specified"); NazaraAssert(shaderStages != 0, "at least one shader stage must be specified");
@@ -137,7 +137,7 @@ namespace Nz
return stageFlags; return stageFlags;
} }
void OpenGLShaderModule::Create(OpenGLDevice& /*device*/, ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states) void OpenGLShaderModule::Create(OpenGLDevice& /*device*/, ShaderStageTypeFlags shaderStages, const ShaderAst::Module& shaderModule, const ShaderWriter::States& states)
{ {
m_states = states; m_states = states;
m_states.sanitized = true; //< Shader is always sanitized (because of keywords) m_states.sanitized = true; //< Shader is always sanitized (because of keywords)

View File

@@ -81,6 +81,7 @@ namespace Nz::ShaderAst
{ {
auto clone = std::make_unique<DeclareConstStatement>(); auto clone = std::make_unique<DeclareConstStatement>();
clone->constIndex = node.constIndex; clone->constIndex = node.constIndex;
clone->hidden = node.hidden;
clone->name = node.name; clone->name = node.name;
clone->type = Clone(node.type); clone->type = Clone(node.type);
clone->expression = CloneExpression(node.expression); clone->expression = CloneExpression(node.expression);
@@ -92,6 +93,7 @@ namespace Nz::ShaderAst
{ {
auto clone = std::make_unique<DeclareExternalStatement>(); auto clone = std::make_unique<DeclareExternalStatement>();
clone->bindingSet = Clone(node.bindingSet); clone->bindingSet = Clone(node.bindingSet);
clone->hidden = node.hidden;
clone->externalVars.reserve(node.externalVars.size()); clone->externalVars.reserve(node.externalVars.size());
for (const auto& var : node.externalVars) for (const auto& var : node.externalVars)
@@ -114,9 +116,9 @@ namespace Nz::ShaderAst
clone->earlyFragmentTests = Clone(node.earlyFragmentTests); clone->earlyFragmentTests = Clone(node.earlyFragmentTests);
clone->entryStage = Clone(node.entryStage); clone->entryStage = Clone(node.entryStage);
clone->funcIndex = node.funcIndex; clone->funcIndex = node.funcIndex;
clone->hidden = node.hidden;
clone->name = node.name; clone->name = node.name;
clone->returnType = Clone(node.returnType); clone->returnType = Clone(node.returnType);
clone->varIndex = node.varIndex;
clone->parameters.reserve(node.parameters.size()); clone->parameters.reserve(node.parameters.size());
for (auto& parameter : node.parameters) for (auto& parameter : node.parameters)
@@ -124,6 +126,7 @@ namespace Nz::ShaderAst
auto& cloneParam = clone->parameters.emplace_back(); auto& cloneParam = clone->parameters.emplace_back();
cloneParam.name = parameter.name; cloneParam.name = parameter.name;
cloneParam.type = Clone(parameter.type); cloneParam.type = Clone(parameter.type);
cloneParam.varIndex = parameter.varIndex;
} }
clone->statements.reserve(node.statements.size()); clone->statements.reserve(node.statements.size());
@@ -137,6 +140,7 @@ namespace Nz::ShaderAst
{ {
auto clone = std::make_unique<DeclareOptionStatement>(); auto clone = std::make_unique<DeclareOptionStatement>();
clone->defaultValue = CloneExpression(node.defaultValue); clone->defaultValue = CloneExpression(node.defaultValue);
clone->hidden = node.hidden;
clone->optIndex = node.optIndex; clone->optIndex = node.optIndex;
clone->optName = node.optName; clone->optName = node.optName;
clone->optType = Clone(node.optType); clone->optType = Clone(node.optType);
@@ -147,8 +151,9 @@ namespace Nz::ShaderAst
StatementPtr AstCloner::Clone(DeclareStructStatement& node) StatementPtr AstCloner::Clone(DeclareStructStatement& node)
{ {
auto clone = std::make_unique<DeclareStructStatement>(); auto clone = std::make_unique<DeclareStructStatement>();
clone->structIndex = node.structIndex; clone->hidden = node.hidden;
clone->isExported = Clone(node.isExported); clone->isExported = Clone(node.isExported);
clone->structIndex = node.structIndex;
clone->description.layout = Clone(node.description.layout); clone->description.layout = Clone(node.description.layout);
clone->description.name = node.description.name; clone->description.name = node.description.name;
@@ -170,10 +175,10 @@ namespace Nz::ShaderAst
StatementPtr AstCloner::Clone(DeclareVariableStatement& node) StatementPtr AstCloner::Clone(DeclareVariableStatement& node)
{ {
auto clone = std::make_unique<DeclareVariableStatement>(); auto clone = std::make_unique<DeclareVariableStatement>();
clone->initialExpression = CloneExpression(node.initialExpression);
clone->varIndex = node.varIndex; clone->varIndex = node.varIndex;
clone->varName = node.varName; clone->varName = node.varName;
clone->varType = Clone(node.varType); clone->varType = Clone(node.varType);
clone->initialExpression = CloneExpression(node.initialExpression);
return clone; return clone;
} }

View File

@@ -195,6 +195,7 @@ namespace Nz::ShaderAst
void AstSerializerBase::Serialize(DeclareExternalStatement& node) void AstSerializerBase::Serialize(DeclareExternalStatement& node)
{ {
OptVal(node.hidden);
ExprValue(node.bindingSet); ExprValue(node.bindingSet);
Container(node.externalVars); Container(node.externalVars);
@@ -211,6 +212,7 @@ namespace Nz::ShaderAst
void AstSerializerBase::Serialize(DeclareConstStatement& node) void AstSerializerBase::Serialize(DeclareConstStatement& node)
{ {
OptVal(node.constIndex); OptVal(node.constIndex);
OptVal(node.hidden);
Value(node.name); Value(node.name);
ExprValue(node.type); ExprValue(node.type);
Node(node.expression); Node(node.expression);
@@ -224,13 +226,14 @@ namespace Nz::ShaderAst
ExprValue(node.earlyFragmentTests); ExprValue(node.earlyFragmentTests);
ExprValue(node.entryStage); ExprValue(node.entryStage);
OptVal(node.funcIndex); OptVal(node.funcIndex);
OptVal(node.varIndex); OptVal(node.hidden);
Container(node.parameters); Container(node.parameters);
for (auto& parameter : node.parameters) for (auto& parameter : node.parameters)
{ {
Value(parameter.name); Value(parameter.name);
ExprValue(parameter.type); ExprValue(parameter.type);
OptVal(parameter.varIndex);
} }
Container(node.statements); Container(node.statements);
@@ -241,6 +244,7 @@ namespace Nz::ShaderAst
void AstSerializerBase::Serialize(DeclareOptionStatement& node) void AstSerializerBase::Serialize(DeclareOptionStatement& node)
{ {
OptVal(node.optIndex); OptVal(node.optIndex);
OptVal(node.hidden);
Value(node.optName); Value(node.optName);
ExprValue(node.optType); ExprValue(node.optType);
Node(node.defaultValue); Node(node.defaultValue);
@@ -249,6 +253,7 @@ namespace Nz::ShaderAst
void AstSerializerBase::Serialize(DeclareStructStatement& node) void AstSerializerBase::Serialize(DeclareStructStatement& node)
{ {
OptVal(node.structIndex); OptVal(node.structIndex);
OptVal(node.hidden);
ExprValue(node.isExported); ExprValue(node.isExported);
Value(node.description.name); Value(node.description.name);

View File

@@ -85,13 +85,13 @@ namespace Nz::ShaderAst
// Register struct used in parameters or return type // Register struct used in parameters or return type
if (!node.parameters.empty()) if (!node.parameters.empty())
{ {
assert(node.varIndex);
std::size_t parameterVarIndex = *node.varIndex;
for (auto& parameter : node.parameters) for (auto& parameter : node.parameters)
{ {
assert(parameter.varIndex);
// Since parameters must always be defined, their type isn't a dependency of parameter variables // Since parameters must always be defined, their type isn't a dependency of parameter variables
assert(m_variableUsages.find(parameterVarIndex) == m_variableUsages.end()); assert(m_variableUsages.find(*parameter.varIndex) == m_variableUsages.end());
m_variableUsages.emplace(parameterVarIndex, UsageSet{}); m_variableUsages.emplace(*parameter.varIndex, UsageSet{});
const auto& exprType = parameter.type.GetResultingValue(); const auto& exprType = parameter.type.GetResultingValue();
if (IsStructType(exprType)) if (IsStructType(exprType))
@@ -99,8 +99,6 @@ namespace Nz::ShaderAst
std::size_t structIndex = std::get<ShaderAst::StructType>(exprType).structIndex; std::size_t structIndex = std::get<ShaderAst::StructType>(exprType).structIndex;
usageSet.usedStructs.UnboundedSet(structIndex); usageSet.usedStructs.UnboundedSet(structIndex);
} }
++parameterVarIndex;
} }
} }

View File

@@ -0,0 +1,177 @@
// 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/Core/Algorithm.hpp>
#include <unordered_map>
#include <Nazara/Shader/Debug.hpp>
namespace Nz::ShaderAst
{
namespace
{
template<typename T> const T& Retrieve(const std::unordered_map<std::size_t, T>& map, std::size_t id)
{
auto it = map.find(id);
assert(it != map.end());
return it->second;
}
template<typename T, typename U> void UniqueInsert(std::unordered_map<T, U>& map, T key, U value)
{
assert(map.find(key) == map.end());
map.emplace(std::move(key), std::move(value));
}
template<typename T, typename U>
std::unique_ptr<T> static_unique_pointer_cast(std::unique_ptr<U>&& ptr)
{
return std::unique_ptr<T>(SafeCast<T*>(ptr.release()));
}
}
struct IndexRemapperVisitor::Context
{
const IndexRemapperVisitor::Callbacks* callbacks;
std::unordered_map<std::size_t, std::size_t> newConstIndices;
std::unordered_map<std::size_t, std::size_t> newFuncIndices;
std::unordered_map<std::size_t, std::size_t> newStructIndices;
std::unordered_map<std::size_t, std::size_t> newVarIndices;
};
StatementPtr IndexRemapperVisitor::Clone(Statement& statement, const Callbacks& callbacks)
{
assert(callbacks.constIndexGenerator);
assert(callbacks.funcIndexGenerator);
assert(callbacks.structIndexGenerator);
//assert(callbacks.typeIndexGenerator);
assert(callbacks.varIndexGenerator);
Context context;
context.callbacks = &callbacks;
m_context = &context;
return AstCloner::Clone(statement);
}
StatementPtr IndexRemapperVisitor::Clone(DeclareConstStatement& node)
{
DeclareConstStatementPtr clone = static_unique_pointer_cast<DeclareConstStatement>(AstCloner::Clone(node));
assert(clone->constIndex);
std::size_t newConstIndex = m_context->callbacks->constIndexGenerator(*clone->constIndex);
UniqueInsert(m_context->newConstIndices, *clone->constIndex, newConstIndex);
clone->constIndex = newConstIndex;
return clone;
}
StatementPtr IndexRemapperVisitor::Clone(DeclareExternalStatement& node)
{
DeclareExternalStatementPtr clone = static_unique_pointer_cast<DeclareExternalStatement>(AstCloner::Clone(node));
for (auto& extVar : clone->externalVars)
{
assert(extVar.varIndex);
std::size_t newVarIndex = m_context->callbacks->varIndexGenerator(*extVar.varIndex);
UniqueInsert(m_context->newVarIndices, *extVar.varIndex, newVarIndex);
extVar.varIndex = newVarIndex;
}
return clone;
}
StatementPtr IndexRemapperVisitor::Clone(DeclareFunctionStatement& node)
{
DeclareFunctionStatementPtr clone = static_unique_pointer_cast<DeclareFunctionStatement>(AstCloner::Clone(node));
assert(clone->funcIndex);
std::size_t newFuncIndex = m_context->callbacks->funcIndexGenerator(*clone->funcIndex);
UniqueInsert(m_context->newFuncIndices, *clone->funcIndex, newFuncIndex);
clone->funcIndex = newFuncIndex;
if (!clone->parameters.empty())
{
for (auto& parameter : node.parameters)
{
assert(parameter.varIndex);
parameter.varIndex = Retrieve(m_context->newVarIndices, *parameter.varIndex);
HandleType(parameter.type);
}
}
if (node.returnType.HasValue())
HandleType(node.returnType);
return clone;
}
StatementPtr IndexRemapperVisitor::Clone(DeclareStructStatement& node)
{
DeclareStructStatementPtr clone = static_unique_pointer_cast<DeclareStructStatement>(AstCloner::Clone(node));
assert(clone->structIndex);
std::size_t newStructIndex = m_context->callbacks->structIndexGenerator(*clone->structIndex);
UniqueInsert(m_context->newStructIndices, *clone->structIndex, newStructIndex);
clone->structIndex = newStructIndex;
for (auto& structMember : clone->description.members)
HandleType(structMember.type);
return clone;
}
StatementPtr IndexRemapperVisitor::Clone(DeclareVariableStatement& node)
{
DeclareVariableStatementPtr clone = static_unique_pointer_cast<DeclareVariableStatement>(AstCloner::Clone(node));
assert(clone->varIndex);
std::size_t newVarIndex = m_context->callbacks->varIndexGenerator(*clone->varIndex);
UniqueInsert(m_context->newConstIndices, *clone->varIndex, newVarIndex);
clone->varIndex = newVarIndex;
HandleType(node.varType);
return clone;
}
ExpressionPtr IndexRemapperVisitor::Clone(CallFunctionExpression& node)
{
CallFunctionExpressionPtr clone = static_unique_pointer_cast<CallFunctionExpression>(AstCloner::Clone(node));
const auto& targetFuncType = GetExpressionType(*node.targetFunction);
if (std::holds_alternative<FunctionType>(targetFuncType))
{
const auto& funcType = std::get<FunctionType>(targetFuncType);
FunctionType newFunc;
newFunc.funcIndex = Retrieve(m_context->newFuncIndices, funcType.funcIndex);
clone->cachedExpressionType = ExpressionType{ newFunc }; //< FIXME We should add FunctionExpression like VariableExpression to handle this
}
return clone;
}
ExpressionPtr IndexRemapperVisitor::Clone(VariableExpression& node)
{
VariableExpressionPtr clone = static_unique_pointer_cast<VariableExpression>(AstCloner::Clone(node));
assert(clone->variableId);
clone->variableId = Retrieve(m_context->newVarIndices, clone->variableId);
return clone;
}
void IndexRemapperVisitor::HandleType(ExpressionValue<ExpressionType>& exprType)
{
const auto& resultingType = exprType.GetResultingValue();
if (IsStructType(resultingType))
{
std::size_t newStructIndex = Retrieve(m_context->newStructIndices, std::get<StructType>(resultingType).structIndex);
exprType = ExpressionType{ StructType{ newStructIndex } };
}
}
}

View File

@@ -9,9 +9,14 @@
#include <Nazara/Core/StackVector.hpp> #include <Nazara/Core/StackVector.hpp>
#include <Nazara/Shader/ShaderBuilder.hpp> #include <Nazara/Shader/ShaderBuilder.hpp>
#include <Nazara/Shader/Ast/AstConstantPropagationVisitor.hpp> #include <Nazara/Shader/Ast/AstConstantPropagationVisitor.hpp>
#include <Nazara/Shader/Ast/AstExportVisitor.hpp>
#include <Nazara/Shader/Ast/AstRecursiveVisitor.hpp> #include <Nazara/Shader/Ast/AstRecursiveVisitor.hpp>
#include <Nazara/Shader/Ast/AstUtils.hpp> #include <Nazara/Shader/Ast/AstUtils.hpp>
#include <Nazara/Shader/Ast/DependencyCheckerVisitor.hpp>
#include <Nazara/Shader/Ast/EliminateUnusedPassVisitor.hpp>
#include <Nazara/Shader/Ast/IndexRemapperVisitor.hpp>
#include <numeric> #include <numeric>
#include <sstream>
#include <stdexcept> #include <stdexcept>
#include <unordered_set> #include <unordered_set>
#include <Nazara/Shader/Debug.hpp> #include <Nazara/Shader/Debug.hpp>
@@ -46,6 +51,7 @@ namespace Nz::ShaderAst
struct IdentifierData struct IdentifierData
{ {
Bitset<UInt64> availableIndices; Bitset<UInt64> availableIndices;
Bitset<UInt64> preregisteredIndices;
std::unordered_map<std::size_t, T> values; std::unordered_map<std::size_t, T> values;
template<typename U> template<typename U>
@@ -53,18 +59,21 @@ namespace Nz::ShaderAst
{ {
std::size_t dataIndex; std::size_t dataIndex;
if (index.has_value()) if (index.has_value())
dataIndex = *index;
else
{ {
dataIndex = availableIndices.FindFirst(); dataIndex = *index;
if (dataIndex == availableIndices.npos)
dataIndex = availableIndices.GetSize();
}
if (dataIndex >= availableIndices.GetSize()) if (dataIndex >= availableIndices.GetSize())
availableIndices.Resize(dataIndex + 1, true); availableIndices.Resize(dataIndex + 1, true);
else if (!availableIndices.Test(dataIndex)) else if (!availableIndices.Test(dataIndex))
throw AstError{ "index " + std::to_string(dataIndex) + " is already used" }; {
if (preregisteredIndices.UnboundedTest(dataIndex))
preregisteredIndices.Reset(dataIndex);
else
throw AstError{ "index " + std::to_string(dataIndex) + " is already used" };
}
}
else
dataIndex = RegisterNewIndex();
assert(values.find(dataIndex) == values.end()); assert(values.find(dataIndex) == values.end());
@@ -73,6 +82,23 @@ namespace Nz::ShaderAst
return dataIndex; return dataIndex;
} }
std::size_t RegisterNewIndex(bool preregister = false)
{
std::size_t index = availableIndices.FindFirst();
if (index == availableIndices.npos)
{
index = availableIndices.GetSize();
availableIndices.Resize(index + 1, true);
}
availableIndices.Set(index, false);
if (preregister)
preregisteredIndices.UnboundedSet(index);
return index;
}
T& Retrieve(std::size_t index) T& Retrieve(std::size_t index)
{ {
auto it = values.find(index); auto it = values.find(index);
@@ -94,11 +120,13 @@ namespace Nz::ShaderAst
std::size_t previousSize; std::size_t previousSize;
}; };
std::size_t nextOptionIndex = 0;
Options options;
std::array<DeclareFunctionStatement*, ShaderStageTypeCount> entryFunctions = {}; std::array<DeclareFunctionStatement*, ShaderStageTypeCount> entryFunctions = {};
std::optional<DependencyCheckerVisitor::UsageSet> importUsage;
std::size_t nextOptionIndex = 0;
std::vector<Identifier> identifiersInScope; std::vector<Identifier> identifiersInScope;
std::vector<PendingFunction> pendingFunctions; std::vector<PendingFunction> pendingFunctions;
std::vector<Scope> scopes;
std::vector<StatementPtr>* currentStatementList = nullptr;
std::unordered_set<std::string> declaredExternalVar; std::unordered_set<std::string> declaredExternalVar;
std::unordered_set<UInt64> usedBindingIndexes; std::unordered_set<UInt64> usedBindingIndexes;
IdentifierData<ConstantValue> constantValues; IdentifierData<ConstantValue> constantValues;
@@ -107,9 +135,8 @@ namespace Nz::ShaderAst
IdentifierData<StructDescription*> structs; IdentifierData<StructDescription*> structs;
IdentifierData<std::variant<ExpressionType, PartialType>> types; IdentifierData<std::variant<ExpressionType, PartialType>> types;
IdentifierData<ExpressionType> variableTypes; IdentifierData<ExpressionType> variableTypes;
std::vector<Scope> scopes; Options options;
CurrentFunctionData* currentFunction = nullptr; CurrentFunctionData* currentFunction = nullptr;
std::vector<StatementPtr>* currentStatementList = nullptr;
}; };
ModulePtr SanitizeVisitor::Sanitize(const Module& module, const Options& options, std::string* error) ModulePtr SanitizeVisitor::Sanitize(const Module& module, const Options& options, std::string* error)
@@ -769,7 +796,10 @@ namespace Nz::ShaderAst
clone->type = expressionType; clone->type = expressionType;
clone->constIndex = RegisterConstant(clone->name, value, clone->constIndex); if (m_context->importUsage.has_value())
clone->hidden = true;
clone->constIndex = RegisterConstant(clone->name, value, clone->hidden.value_or(false), clone->constIndex);
if (m_context->options.removeConstDeclaration) if (m_context->options.removeConstDeclaration)
return ShaderBuilder::NoOp(); return ShaderBuilder::NoOp();
@@ -783,6 +813,13 @@ namespace Nz::ShaderAst
auto clone = static_unique_pointer_cast<DeclareExternalStatement>(AstCloner::Clone(node)); auto clone = static_unique_pointer_cast<DeclareExternalStatement>(AstCloner::Clone(node));
if (m_context->importUsage.has_value())
{
// Since unused variables have been removed when importing a module, every variable should be used
assert(!clone->externalVars.empty());
clone->hidden = !m_context->importUsage->usedVariables.UnboundedTest(*clone->externalVars.front().varIndex);
}
UInt32 defaultBlockSet = 0; UInt32 defaultBlockSet = 0;
if (clone->bindingSet.HasValue()) if (clone->bindingSet.HasValue())
defaultBlockSet = ComputeExprValue(clone->bindingSet); defaultBlockSet = ComputeExprValue(clone->bindingSet);
@@ -823,7 +860,7 @@ namespace Nz::ShaderAst
throw AstError{ "external variable " + extVar.name + " is of wrong type: only uniform and sampler are allowed in external blocks" }; throw AstError{ "external variable " + extVar.name + " is of wrong type: only uniform and sampler are allowed in external blocks" };
extVar.type = std::move(resolvedType); extVar.type = std::move(resolvedType);
extVar.varIndex = RegisterVariable(extVar.name, std::move(varType), extVar.varIndex); extVar.varIndex = RegisterVariable(extVar.name, std::move(varType), clone->hidden.value_or(false), extVar.varIndex);
SanitizeIdentifier(extVar.name); SanitizeIdentifier(extVar.name);
} }
@@ -881,6 +918,12 @@ namespace Nz::ShaderAst
} }
} }
if (m_context->importUsage.has_value())
{
assert(clone->funcIndex);
clone->hidden = !m_context->importUsage->usedStructs.UnboundedTest(*clone->funcIndex);
}
// Function content is resolved in a second pass // Function content is resolved in a second pass
auto& pendingFunc = m_context->pendingFunctions.emplace_back(); auto& pendingFunc = m_context->pendingFunctions.emplace_back();
pendingFunc.cloneNode = clone.get(); pendingFunc.cloneNode = clone.get();
@@ -895,7 +938,7 @@ namespace Nz::ShaderAst
FunctionData funcData; FunctionData funcData;
funcData.node = clone.get(); //< update function node funcData.node = clone.get(); //< update function node
std::size_t funcIndex = RegisterFunction(clone->name, std::move(funcData), node.funcIndex); std::size_t funcIndex = RegisterFunction(clone->name, std::move(funcData), clone->hidden.value_or(false), node.funcIndex);
clone->funcIndex = funcIndex; clone->funcIndex = funcIndex;
SanitizeIdentifier(clone->name); SanitizeIdentifier(clone->name);
@@ -919,10 +962,13 @@ namespace Nz::ShaderAst
std::size_t optionIndex = m_context->nextOptionIndex++; std::size_t optionIndex = m_context->nextOptionIndex++;
if (m_context->importUsage.has_value())
clone->hidden = true;
if (auto optionValueIt = m_context->options.optionValues.find(optionIndex); optionValueIt != m_context->options.optionValues.end()) if (auto optionValueIt = m_context->options.optionValues.find(optionIndex); optionValueIt != m_context->options.optionValues.end())
clone->optIndex = RegisterConstant(clone->optName, optionValueIt->second, clone->optIndex); clone->optIndex = RegisterConstant(clone->optName, optionValueIt->second, clone->hidden.value_or(false), clone->optIndex);
else if (clone->defaultValue) else if (clone->defaultValue)
clone->optIndex = RegisterConstant(clone->optName, ComputeConstantValue(*clone->defaultValue), clone->optIndex); clone->optIndex = RegisterConstant(clone->optName, ComputeConstantValue(*clone->defaultValue), clone->hidden.value_or(false), clone->optIndex);
else else
throw AstError{ "missing option " + clone->optName + " value (has no default value)" }; throw AstError{ "missing option " + clone->optName + " value (has no default value)" };
@@ -942,6 +988,12 @@ namespace Nz::ShaderAst
if (clone->isExported.HasValue()) if (clone->isExported.HasValue())
clone->isExported = ComputeExprValue(clone->isExported); clone->isExported = ComputeExprValue(clone->isExported);
if (m_context->importUsage.has_value())
{
assert(clone->structIndex);
clone->hidden = !m_context->importUsage->usedStructs.UnboundedTest(*clone->structIndex);
}
std::unordered_set<std::string> declaredMembers; std::unordered_set<std::string> declaredMembers;
for (auto& member : clone->description.members) for (auto& member : clone->description.members)
{ {
@@ -980,7 +1032,7 @@ namespace Nz::ShaderAst
member.type = std::move(resolvedType); member.type = std::move(resolvedType);
} }
clone->structIndex = RegisterStruct(clone->description.name, &clone->description, clone->structIndex); clone->structIndex = RegisterStruct(clone->description.name, &clone->description, clone->hidden.value_or(false), clone->structIndex);
SanitizeIdentifier(clone->description.name); SanitizeIdentifier(clone->description.name);
@@ -1310,7 +1362,102 @@ namespace Nz::ShaderAst
StatementPtr SanitizeVisitor::Clone(ImportStatement& node) StatementPtr SanitizeVisitor::Clone(ImportStatement& node)
{ {
// Nested import is handled separately
assert(!m_context->importUsage);
if (!m_context->options.moduleCallback)
return static_unique_pointer_cast<ImportStatement>(AstCloner::Clone(node)); return static_unique_pointer_cast<ImportStatement>(AstCloner::Clone(node));
auto ModulePathAsString = [&]() -> std::string
{
std::ostringstream ss;
bool first = true;
for (const std::string& part : node.modulePath)
{
if (!first)
ss << "/";
ss << part;
first = false;
}
return ss.str();
};
ModulePtr targetModule = m_context->options.moduleCallback(node.modulePath);
if (!targetModule)
throw AstError{ "module " + ModulePathAsString() + " not found" };
std::string error;
ModulePtr sanitizedModule = ShaderAst::Sanitize(*targetModule, m_context->options, &error);
if (!sanitizedModule)
throw AstError{ "module " + ModulePathAsString() + " compilation failed: " + error };
// Extract exported nodes and their dependencies
DependencyCheckerVisitor::Config depConfig;
depConfig.usedShaderStages.Clear();
DependencyCheckerVisitor moduleDependencies;
moduleDependencies.Process(*sanitizedModule->rootNode, depConfig);
DependencyCheckerVisitor::UsageSet exportedSet;
AstExportVisitor::Callbacks callbacks;
callbacks.onExportedStruct = [&](DeclareStructStatement& node)
{
assert(node.structIndex);
moduleDependencies.MarkStructAsUsed(*node.structIndex);
exportedSet.usedStructs.UnboundedSet(*node.structIndex);
};
AstExportVisitor exportVisitor;
exportVisitor.Visit(*sanitizedModule->rootNode, callbacks);
moduleDependencies.Resolve();
auto statementPtr = EliminateUnusedPass(*sanitizedModule->rootNode, moduleDependencies.GetUsage());
DependencyCheckerVisitor::UsageSet remappedExportedSet;
IndexRemapperVisitor::Callbacks remapCallbacks;
remapCallbacks.constIndexGenerator = [this](std::size_t previousIndex) { return m_context->constantValues.RegisterNewIndex(true); };
remapCallbacks.funcIndexGenerator = [&](std::size_t previousIndex)
{
std::size_t newIndex = m_context->functions.RegisterNewIndex(true);
if (exportedSet.usedFunctions.Test(previousIndex))
remappedExportedSet.usedFunctions.UnboundedSet(newIndex);
return newIndex;
};
remapCallbacks.structIndexGenerator = [&](std::size_t previousIndex)
{
std::size_t newIndex = m_context->structs.RegisterNewIndex(true);
if (exportedSet.usedStructs.Test(previousIndex))
remappedExportedSet.usedStructs.UnboundedSet(newIndex);
return newIndex;
};
remapCallbacks.varIndexGenerator = [&](std::size_t previousIndex)
{
std::size_t newIndex = m_context->variableTypes.RegisterNewIndex(true);
if (exportedSet.usedVariables.Test(previousIndex))
remappedExportedSet.usedVariables.UnboundedSet(newIndex);
return newIndex;
};
statementPtr = RemapIndices(*statementPtr, remapCallbacks);
// Register exported variables (FIXME: This shouldn't be necessary and could be handled by the IndexRemapperVisitor)
m_context->importUsage = remappedExportedSet;
CallOnExit restoreImportOnExit([&] { m_context->importUsage.reset(); });
return AstCloner::Clone(*statementPtr);
} }
StatementPtr SanitizeVisitor::Clone(MultiStatement& node) StatementPtr SanitizeVisitor::Clone(MultiStatement& node)
@@ -1694,131 +1841,151 @@ namespace Nz::ShaderAst
RegisterIntrinsic("reflect", IntrinsicType::Reflect); RegisterIntrinsic("reflect", IntrinsicType::Reflect);
} }
std::size_t SanitizeVisitor::RegisterConstant(std::string name, ConstantValue value, std::optional<std::size_t> index) std::size_t SanitizeVisitor::RegisterConstant(std::string name, ConstantValue value, bool hidden, std::optional<std::size_t> index)
{ {
if (FindIdentifier(name))
throw AstError{ name + " is already used" };
std::size_t constantIndex = m_context->constantValues.Register(std::move(value), index); std::size_t constantIndex = m_context->constantValues.Register(std::move(value), index);
if (!hidden)
{
if (FindIdentifier(name))
throw AstError{ name + " is already used" };
m_context->identifiersInScope.push_back({ m_context->identifiersInScope.push_back({
std::move(name), std::move(name),
constantIndex, constantIndex,
Identifier::Type::Constant Identifier::Type::Constant
}); });
}
return constantIndex; return constantIndex;
} }
std::size_t SanitizeVisitor::RegisterFunction(std::string name, FunctionData funcData, std::optional<std::size_t> index) std::size_t SanitizeVisitor::RegisterFunction(std::string name, FunctionData funcData, bool hidden, std::optional<std::size_t> index)
{ {
if (auto* identifier = FindIdentifier(name))
{
bool duplicate = true;
// Functions cannot be declared twice, except for entry ones if their stages are different
if (funcData.node->entryStage.HasValue() && identifier->type == Identifier::Type::Function)
{
auto& otherFunction = m_context->functions.Retrieve(identifier->index);
if (funcData.node->entryStage.GetResultingValue() != otherFunction.node->entryStage.GetResultingValue())
duplicate = false;
}
if (duplicate)
throw AstError{ funcData.node->name + " is already used" };
}
std::size_t functionIndex = m_context->functions.Register(std::move(funcData), index); std::size_t functionIndex = m_context->functions.Register(std::move(funcData), index);
m_context->identifiersInScope.push_back({ if (!hidden)
std::move(name), {
functionIndex, if (auto* identifier = FindIdentifier(name))
Identifier::Type::Function {
}); bool duplicate = true;
// Functions cannot be declared twice, except for entry ones if their stages are different
if (funcData.node->entryStage.HasValue() && identifier->type == Identifier::Type::Function)
{
auto& otherFunction = m_context->functions.Retrieve(identifier->index);
if (funcData.node->entryStage.GetResultingValue() != otherFunction.node->entryStage.GetResultingValue())
duplicate = false;
}
if (duplicate)
throw AstError{ funcData.node->name + " is already used" };
}
m_context->identifiersInScope.push_back({
std::move(name),
functionIndex,
Identifier::Type::Function
});
}
return functionIndex; return functionIndex;
} }
std::size_t SanitizeVisitor::RegisterIntrinsic(std::string name, IntrinsicType type, std::optional<std::size_t> index) std::size_t SanitizeVisitor::RegisterIntrinsic(std::string name, IntrinsicType type, bool hidden, std::optional<std::size_t> index)
{ {
if (FindIdentifier(name))
throw AstError{ name + " is already used" };
std::size_t intrinsicIndex = m_context->intrinsics.Register(std::move(type), index); std::size_t intrinsicIndex = m_context->intrinsics.Register(std::move(type), index);
m_context->identifiersInScope.push_back({ if (!hidden)
std::move(name), {
intrinsicIndex, if (FindIdentifier(name))
Identifier::Type::Intrinsic throw AstError{ name + " is already used" };
});
m_context->identifiersInScope.push_back({
std::move(name),
intrinsicIndex,
Identifier::Type::Intrinsic
});
}
return intrinsicIndex; return intrinsicIndex;
} }
std::size_t SanitizeVisitor::RegisterStruct(std::string name, StructDescription* description, std::optional<std::size_t> index) std::size_t SanitizeVisitor::RegisterStruct(std::string name, StructDescription* description, bool hidden, std::optional<std::size_t> index)
{ {
if (FindIdentifier(name))
throw AstError{ name + " is already used" };
std::size_t structIndex = m_context->structs.Register(description, index); std::size_t structIndex = m_context->structs.Register(description, index);
m_context->identifiersInScope.push_back({ if (!hidden)
std::move(name), {
structIndex, if (FindIdentifier(name))
Identifier::Type::Struct throw AstError{ name + " is already used" };
});
m_context->identifiersInScope.push_back({
std::move(name),
structIndex,
Identifier::Type::Struct
});
}
return structIndex; return structIndex;
} }
std::size_t SanitizeVisitor::RegisterType(std::string name, ExpressionType expressionType, std::optional<std::size_t> index) std::size_t SanitizeVisitor::RegisterType(std::string name, ExpressionType expressionType, bool hidden, std::optional<std::size_t> index)
{ {
if (FindIdentifier(name))
throw AstError{ name + " is already used" };
std::size_t typeIndex = m_context->types.Register(std::move(expressionType), index); std::size_t typeIndex = m_context->types.Register(std::move(expressionType), index);
m_context->identifiersInScope.push_back({ if (!hidden)
std::move(name),
typeIndex,
Identifier::Type::Type
});
return typeIndex;
}
std::size_t SanitizeVisitor::RegisterType(std::string name, PartialType partialType, std::optional<std::size_t> index)
{
if (FindIdentifier(name))
throw AstError{ name + " is already used" };
std::size_t typeIndex = m_context->types.Register(std::move(partialType), index);
m_context->identifiersInScope.push_back({
std::move(name),
typeIndex,
Identifier::Type::Type
});
return typeIndex;
}
std::size_t SanitizeVisitor::RegisterVariable(std::string name, ExpressionType type, std::optional<std::size_t> index)
{
if (auto* identifier = FindIdentifier(name))
{ {
// Allow variable shadowing if (FindIdentifier(name))
if (identifier->type != Identifier::Type::Variable)
throw AstError{ name + " is already used" }; throw AstError{ name + " is already used" };
m_context->identifiersInScope.push_back({
std::move(name),
typeIndex,
Identifier::Type::Type
});
} }
return typeIndex;
}
std::size_t SanitizeVisitor::RegisterType(std::string name, PartialType partialType, bool hidden, std::optional<std::size_t> index)
{
std::size_t typeIndex = m_context->types.Register(std::move(partialType), index);
if (!hidden)
{
if (FindIdentifier(name))
throw AstError{ name + " is already used" };
m_context->identifiersInScope.push_back({
std::move(name),
typeIndex,
Identifier::Type::Type
});
}
return typeIndex;
}
std::size_t SanitizeVisitor::RegisterVariable(std::string name, ExpressionType type, bool hidden, std::optional<std::size_t> index)
{
std::size_t varIndex = m_context->variableTypes.Register(std::move(type), index); std::size_t varIndex = m_context->variableTypes.Register(std::move(type), index);
m_context->identifiersInScope.push_back({ if (!hidden)
std::move(name), {
varIndex, if (auto* identifier = FindIdentifier(name))
Identifier::Type::Variable {
}); // Allow variable shadowing
if (identifier->type != Identifier::Type::Variable)
throw AstError{ name + " is already used" };
}
m_context->identifiersInScope.push_back({
std::move(name),
varIndex,
Identifier::Type::Variable
});
}
return varIndex; return varIndex;
} }
@@ -1830,16 +1997,10 @@ namespace Nz::ShaderAst
{ {
PushScope(); PushScope();
std::optional<std::size_t> varIndex = pendingFunc.cloneNode->varIndex;
for (auto& parameter : pendingFunc.cloneNode->parameters) for (auto& parameter : pendingFunc.cloneNode->parameters)
{ {
std::size_t index = RegisterVariable(parameter.name, parameter.type.GetResultingValue(), varIndex); parameter.varIndex = RegisterVariable(parameter.name, parameter.type.GetResultingValue(), false, parameter.varIndex);
if (!pendingFunc.cloneNode->varIndex)
pendingFunc.cloneNode->varIndex = index; //< First parameter variable index is node variable index
SanitizeIdentifier(parameter.name); SanitizeIdentifier(parameter.name);
if (varIndex)
(*varIndex)++;
} }
Context::CurrentFunctionData tempFuncData; Context::CurrentFunctionData tempFuncData;
@@ -2304,7 +2465,7 @@ namespace Nz::ShaderAst
TypeMustMatch(resolvedType, GetExpressionType(*node.initialExpression)); TypeMustMatch(resolvedType, GetExpressionType(*node.initialExpression));
} }
node.varIndex = RegisterVariable(node.varName, resolvedType, node.varIndex); node.varIndex = RegisterVariable(node.varName, resolvedType, false, node.varIndex);
node.varType = std::move(resolvedType); node.varType = std::move(resolvedType);
if (m_context->options.makeVariableNameUnique) if (m_context->options.makeVariableNameUnique)

View File

@@ -152,7 +152,7 @@ namespace Nz
unsigned int indentLevel = 0; unsigned int indentLevel = 0;
}; };
std::string GlslWriter::Generate(std::optional<ShaderStageType> shaderStage, ShaderAst::Module& module, const BindingMapping& bindingMapping, const States& states) std::string GlslWriter::Generate(std::optional<ShaderStageType> shaderStage, const ShaderAst::Module& module, const BindingMapping& bindingMapping, const States& states)
{ {
State state(bindingMapping); State state(bindingMapping);
state.optionValues = states.optionValues; state.optionValues = states.optionValues;
@@ -210,7 +210,7 @@ namespace Nz
return s_flipYUniformName; return s_flipYUniformName;
} }
ShaderAst::ModulePtr GlslWriter::Sanitize(ShaderAst::Module& module, std::unordered_map<std::size_t, ShaderAst::ConstantValue> optionValues, std::string* error) ShaderAst::ModulePtr GlslWriter::Sanitize(const ShaderAst::Module& module, std::unordered_map<std::size_t, ShaderAst::ConstantValue> optionValues, std::string* error)
{ {
// Always sanitize for reserved identifiers // Always sanitize for reserved identifiers
ShaderAst::SanitizeVisitor::Options options; ShaderAst::SanitizeVisitor::Options options;
@@ -604,12 +604,11 @@ namespace Nz
{ {
if (!m_currentState->inputFields.empty()) if (!m_currentState->inputFields.empty())
{ {
assert(node.varIndex);
assert(!node.parameters.empty()); assert(!node.parameters.empty());
auto& parameter = node.parameters.front(); auto& parameter = node.parameters.front();
const std::string& varName = parameter.name; const std::string& varName = parameter.name;
RegisterVariable(*node.varIndex, varName); RegisterVariable(*parameter.varIndex, varName);
assert(IsStructType(parameter.type.GetResultingValue())); assert(IsStructType(parameter.type.GetResultingValue()));
std::size_t structIndex = std::get<ShaderAst::StructType>(parameter.type.GetResultingValue()).structIndex; std::size_t structIndex = std::get<ShaderAst::StructType>(parameter.type.GetResultingValue()).structIndex;
@@ -1123,13 +1122,10 @@ namespace Nz
if (node.entryStage.HasValue()) if (node.entryStage.HasValue())
return HandleEntryPoint(node); return HandleEntryPoint(node);
std::optional<std::size_t> varIndexOpt = node.varIndex;
for (const auto& parameter : node.parameters) for (const auto& parameter : node.parameters)
{ {
assert(varIndexOpt); assert(parameter.varIndex);
std::size_t& varIndex = *varIndexOpt; RegisterVariable(*parameter.varIndex, parameter.name);
RegisterVariable(varIndex++, parameter.name);
} }
AppendFunctionDeclaration(node); AppendFunctionDeclaration(node);

View File

@@ -102,7 +102,7 @@ namespace Nz
unsigned int indentLevel = 0; unsigned int indentLevel = 0;
}; };
std::string LangWriter::Generate(ShaderAst::Module& module, const States& /*states*/) std::string LangWriter::Generate(const ShaderAst::Module& module, const States& /*states*/)
{ {
State state; State state;
m_currentState = &state; m_currentState = &state;
@@ -820,22 +820,21 @@ namespace Nz
{ {
NazaraAssert(m_currentState, "This function should only be called while processing an AST"); NazaraAssert(m_currentState, "This function should only be called while processing an AST");
std::optional<std::size_t> varIndexOpt = node.varIndex;
AppendAttributes(true, EntryAttribute{ node.entryStage }, EarlyFragmentTestsAttribute{ node.earlyFragmentTests }, DepthWriteAttribute{ node.depthWrite }); AppendAttributes(true, EntryAttribute{ node.entryStage }, EarlyFragmentTestsAttribute{ node.earlyFragmentTests }, DepthWriteAttribute{ node.depthWrite });
Append("fn ", node.name, "("); Append("fn ", node.name, "(");
for (std::size_t i = 0; i < node.parameters.size(); ++i) for (std::size_t i = 0; i < node.parameters.size(); ++i)
{ {
const auto& parameter = node.parameters[i];
if (i != 0) if (i != 0)
Append(", "); Append(", ");
Append(node.parameters[i].name); Append(parameter.name);
Append(": "); Append(": ");
Append(node.parameters[i].type); Append(parameter.type);
assert(varIndexOpt); assert(parameter.varIndex);
std::size_t& varIndex = *varIndexOpt; RegisterVariable(*parameter.varIndex, parameter.name);
RegisterVariable(varIndex++, node.parameters[i].name);
} }
Append(")"); Append(")");
if (node.returnType.HasValue()) if (node.returnType.HasValue())

View File

@@ -640,13 +640,15 @@ namespace Nz::ShaderLang
ShaderAst::DeclareFunctionStatement::Parameter Parser::ParseFunctionParameter() ShaderAst::DeclareFunctionStatement::Parameter Parser::ParseFunctionParameter()
{ {
std::string parameterName = ParseIdentifierAsName(); ShaderAst::DeclareFunctionStatement::Parameter parameter;
parameter.name = ParseIdentifierAsName();
Expect(Advance(), TokenType::Colon); Expect(Advance(), TokenType::Colon);
ShaderAst::ExpressionPtr parameterType = ParseType(); parameter.type = ParseType();
return { parameterName, std::move(parameterType) }; return parameter;
} }
ShaderAst::StatementPtr Parser::ParseImportStatement() ShaderAst::StatementPtr Parser::ParseImportStatement()

View File

@@ -624,13 +624,13 @@ namespace Nz
if (!func.parameters.empty()) if (!func.parameters.empty())
{ {
std::size_t varIndex = *node.varIndex; assert(node.parameters.size() == func.parameters.size());
for (const auto& param : func.parameters) for (std::size_t i = 0; i < func.parameters.size(); ++i)
{ {
UInt32 paramResultId = m_writer.AllocateResultId(); UInt32 paramResultId = m_writer.AllocateResultId();
m_instructions.Append(SpirvOp::OpFunctionParameter, param.pointerTypeId, paramResultId); m_instructions.Append(SpirvOp::OpFunctionParameter, func.parameters[i].pointerTypeId, paramResultId);
RegisterVariable(varIndex++, param.typeId, paramResultId, SpirvStorageClass::Function); RegisterVariable(*node.parameters[i].varIndex, func.parameters[i].typeId, paramResultId, SpirvStorageClass::Function);
} }
} }
@@ -655,8 +655,6 @@ namespace Nz
{ {
auto& inputStruct = *entryPointData.inputStruct; auto& inputStruct = *entryPointData.inputStruct;
std::size_t varIndex = *node.varIndex;
UInt32 paramId = m_writer.AllocateResultId(); UInt32 paramId = m_writer.AllocateResultId();
m_currentBlock->Append(SpirvOp::OpVariable, inputStruct.pointerId, paramId, SpirvStorageClass::Function); m_currentBlock->Append(SpirvOp::OpVariable, inputStruct.pointerId, paramId, SpirvStorageClass::Function);
@@ -667,7 +665,7 @@ namespace Nz
m_currentBlock->Append(SpirvOp::OpCopyMemory, resultId, input.varId); m_currentBlock->Append(SpirvOp::OpCopyMemory, resultId, input.varId);
} }
RegisterVariable(varIndex, inputStruct.typeId, paramId, SpirvStorageClass::Function); RegisterVariable(*node.parameters.front().varIndex, inputStruct.typeId, paramId, SpirvStorageClass::Function);
} }
} }

View File

@@ -496,7 +496,7 @@ namespace Nz
{ {
} }
std::vector<UInt32> SpirvWriter::Generate(ShaderAst::Module& module, const States& states) std::vector<UInt32> SpirvWriter::Generate(const ShaderAst::Module& module, const States& states)
{ {
ShaderAst::ModulePtr sanitizedModule; ShaderAst::ModulePtr sanitizedModule;
ShaderAst::Statement* targetAst; ShaderAst::Statement* targetAst;

View File

@@ -61,7 +61,7 @@ namespace Nz
return pipelineLayout; return pipelineLayout;
} }
std::shared_ptr<ShaderModule> VulkanDevice::InstantiateShaderModule(ShaderStageTypeFlags stages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states) std::shared_ptr<ShaderModule> VulkanDevice::InstantiateShaderModule(ShaderStageTypeFlags stages, const ShaderAst::Module& shaderModule, const ShaderWriter::States& states)
{ {
auto stage = std::make_shared<VulkanShaderModule>(); auto stage = std::make_shared<VulkanShaderModule>();
if (!stage->Create(*this, stages, shaderModule, states)) if (!stage->Create(*this, stages, shaderModule, states))

View File

@@ -57,7 +57,7 @@ namespace Nz
}; };
} }
bool VulkanShaderModule::Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderAst::Module& shaderModule, const ShaderWriter::States& states) bool VulkanShaderModule::Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, const ShaderAst::Module& shaderModule, const ShaderWriter::States& states)
{ {
SpirvWriter::Environment env; SpirvWriter::Environment env;

View File

@@ -819,10 +819,9 @@ std::unique_ptr<Nz::ShaderAst::DeclareFunctionStatement> ShaderGraph::ToFunction
std::vector<Nz::ShaderAst::DeclareFunctionStatement::Parameter> parameters; std::vector<Nz::ShaderAst::DeclareFunctionStatement::Parameter> parameters;
if (!m_inputs.empty()) if (!m_inputs.empty())
{ {
parameters.push_back({ auto& parameter = parameters.emplace_back();
"input", parameter.name = "input";
Nz::ShaderAst::ExpressionPtr{ Nz::ShaderBuilder::Identifier("InputData") } parameter.type = Nz::ShaderAst::ExpressionPtr{ Nz::ShaderBuilder::Identifier("InputData") };
});
} }
Nz::ShaderAst::ExpressionPtr returnType; Nz::ShaderAst::ExpressionPtr returnType;