Renderer: Replace ShaderStage by ShaderModule (a module can handle multiple stages)
This commit is contained in:
parent
c1d1838336
commit
e4aabf309e
|
|
@ -10,7 +10,7 @@
|
|||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Graphics/Enums.hpp>
|
||||
#include <Nazara/Renderer/RenderPipelineLayout.hpp>
|
||||
#include <Nazara/Renderer/ShaderStage.hpp>
|
||||
#include <Nazara/Renderer/ShaderModule.hpp>
|
||||
#include <Nazara/Utility/Enums.hpp>
|
||||
#include <array>
|
||||
#include <limits>
|
||||
|
|
|
|||
|
|
@ -10,26 +10,27 @@
|
|||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Core/Bitset.hpp>
|
||||
#include <Nazara/Graphics/Config.hpp>
|
||||
#include <Nazara/Shader/ShaderAst.hpp>
|
||||
#include <Nazara/Shader/ShaderNodes.hpp>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class ShaderStage;
|
||||
class ShaderModule;
|
||||
|
||||
class NAZARA_GRAPHICS_API UberShader
|
||||
{
|
||||
public:
|
||||
UberShader(ShaderAst shaderAst);
|
||||
UberShader(ShaderStageType shaderStage, ShaderAst::StatementPtr shaderAst);
|
||||
~UberShader() = default;
|
||||
|
||||
UInt64 GetConditionFlagByName(const std::string_view& condition) const;
|
||||
|
||||
const std::shared_ptr<ShaderStage>& Get(UInt64 combination);
|
||||
const std::shared_ptr<ShaderModule>& Get(UInt64 combination);
|
||||
|
||||
private:
|
||||
std::unordered_map<UInt64 /*combination*/, std::shared_ptr<ShaderStage>> m_combinations;
|
||||
ShaderAst m_shaderAst;
|
||||
std::unordered_map<UInt64 /*combination*/, std::shared_ptr<ShaderModule>> m_combinations;
|
||||
ShaderAst::StatementPtr m_shaderAst;
|
||||
ShaderStageType m_shaderStage;
|
||||
UInt64 m_combinationMask;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@
|
|||
#include <Nazara/OpenGLRenderer/OpenGLRenderPipelineLayout.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLRenderWindow.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLShaderBinding.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLShaderStage.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLShaderModule.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLTexture.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLTextureSampler.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLUploadPool.hpp>
|
||||
|
|
|
|||
|
|
@ -38,8 +38,8 @@ 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<ShaderStage> InstantiateShaderStage(const ShaderAst& shaderAst, const ShaderWriter::States& states) override;
|
||||
std::shared_ptr<ShaderStage> InstantiateShaderStage(ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize) override;
|
||||
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states) override;
|
||||
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize) override;
|
||||
std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params) override;
|
||||
std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) override;
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - OpenGL Renderer"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_OPENGLRENDERER_OPENGLSHADERMODULE_HPP
|
||||
#define NAZARA_OPENGLRENDERER_OPENGLSHADERMODULE_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Renderer/Enums.hpp>
|
||||
#include <Nazara/Renderer/ShaderModule.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Wrapper/Context.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Wrapper/Shader.hpp>
|
||||
#include <Nazara/Shader/ShaderWriter.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_OPENGLRENDERER_API OpenGLShaderModule : public ShaderModule
|
||||
{
|
||||
public:
|
||||
OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states);
|
||||
OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize);
|
||||
OpenGLShaderModule(const OpenGLShaderModule&) = delete;
|
||||
OpenGLShaderModule(OpenGLShaderModule&&) noexcept = default;
|
||||
~OpenGLShaderModule() = default;
|
||||
|
||||
inline const std::vector<GL::Shader>& GetShaders() const;
|
||||
|
||||
OpenGLShaderModule& operator=(const OpenGLShaderModule&) = delete;
|
||||
OpenGLShaderModule& operator=(OpenGLShaderModule&&) noexcept = default;
|
||||
|
||||
private:
|
||||
void Create(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states);
|
||||
|
||||
static void CheckCompilationStatus(GL::Shader& shader);
|
||||
|
||||
std::vector<GL::Shader> m_shaders;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/OpenGLRenderer/OpenGLShaderModule.inl>
|
||||
|
||||
#endif // NAZARA_OPENGLRENDERER_OPENGLSHADERSTAGE_HPP
|
||||
|
|
@ -2,14 +2,14 @@
|
|||
// This file is part of the "Nazara Engine - OpenGL Renderer"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/OpenGLRenderer/OpenGLShaderStage.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLShaderModule.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline const GL::Shader& OpenGLShaderStage::GetShader() const
|
||||
inline const std::vector<GL::Shader>& OpenGLShaderModule::GetShaders() const
|
||||
{
|
||||
return m_shader;
|
||||
return m_shaders;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - OpenGL Renderer"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_OPENGLRENDERER_OPENGLSHADERSTAGE_HPP
|
||||
#define NAZARA_OPENGLRENDERER_OPENGLSHADERSTAGE_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Renderer/Enums.hpp>
|
||||
#include <Nazara/Renderer/ShaderStage.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Wrapper/Context.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Wrapper/Shader.hpp>
|
||||
#include <Nazara/Shader/ShaderWriter.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class ShaderAst;
|
||||
|
||||
class NAZARA_OPENGLRENDERER_API OpenGLShaderStage : public ShaderStage
|
||||
{
|
||||
public:
|
||||
OpenGLShaderStage(OpenGLDevice& device, const ShaderAst& shaderAst, const ShaderWriter::States& states);
|
||||
OpenGLShaderStage(OpenGLDevice& device, ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize);
|
||||
OpenGLShaderStage(const OpenGLShaderStage&) = delete;
|
||||
OpenGLShaderStage(OpenGLShaderStage&&) noexcept = default;
|
||||
~OpenGLShaderStage() = default;
|
||||
|
||||
inline const GL::Shader& GetShader() const;
|
||||
|
||||
OpenGLShaderStage& operator=(const OpenGLShaderStage&) = delete;
|
||||
OpenGLShaderStage& operator=(OpenGLShaderStage&&) noexcept = default;
|
||||
|
||||
private:
|
||||
void CheckCompilationStatus();
|
||||
void Create(OpenGLDevice& device, const ShaderAst& shaderAst, const ShaderWriter::States& states);
|
||||
|
||||
GL::Shader m_shader;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/OpenGLRenderer/OpenGLShaderStage.inl>
|
||||
|
||||
#endif // NAZARA_OPENGLRENDERER_OPENGLSHADERSTAGE_HPP
|
||||
|
|
@ -53,7 +53,7 @@
|
|||
#include <Nazara/Renderer/RenderWindowImpl.hpp>
|
||||
#include <Nazara/Renderer/RenderWindowParameters.hpp>
|
||||
#include <Nazara/Renderer/ShaderBinding.hpp>
|
||||
#include <Nazara/Renderer/ShaderStage.hpp>
|
||||
#include <Nazara/Renderer/ShaderModule.hpp>
|
||||
#include <Nazara/Renderer/Texture.hpp>
|
||||
#include <Nazara/Renderer/TextureSampler.hpp>
|
||||
#include <Nazara/Renderer/UploadPool.hpp>
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@
|
|||
namespace Nz
|
||||
{
|
||||
class CommandPool;
|
||||
class ShaderStage;
|
||||
class ShaderModule;
|
||||
|
||||
class NAZARA_RENDERER_API RenderDevice
|
||||
{
|
||||
|
|
@ -39,9 +39,9 @@ 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<ShaderStage> InstantiateShaderStage(const ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states) = 0;
|
||||
virtual std::shared_ptr<ShaderStage> InstantiateShaderStage(ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize) = 0;
|
||||
std::shared_ptr<ShaderStage> InstantiateShaderStage(ShaderStageType type, ShaderLanguage lang, const std::filesystem::path& sourcePath);
|
||||
virtual std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states) = 0;
|
||||
virtual std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize) = 0;
|
||||
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const std::filesystem::path& sourcePath);
|
||||
virtual std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params) = 0;
|
||||
virtual std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) = 0;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ namespace Nz
|
|||
};
|
||||
|
||||
std::shared_ptr<RenderPipelineLayout> pipelineLayout;
|
||||
std::vector<std::shared_ptr<ShaderStage>> shaderStages;
|
||||
std::vector<std::shared_ptr<ShaderModule>> shaderModules;
|
||||
std::vector<VertexBufferData> vertexBuffers;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@
|
|||
|
||||
namespace Nz
|
||||
{
|
||||
class ShaderStage;
|
||||
class ShaderModule;
|
||||
|
||||
struct RenderStates
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_RENDERER_SHADERSTAGE_HPP
|
||||
#define NAZARA_RENDERER_SHADERSTAGE_HPP
|
||||
#ifndef NAZARA_RENDERER_SHADERMODULE_HPP
|
||||
#define NAZARA_RENDERER_SHADERMODULE_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Renderer/Config.hpp>
|
||||
|
|
@ -13,11 +13,11 @@
|
|||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_RENDERER_API ShaderStage
|
||||
class NAZARA_RENDERER_API ShaderModule
|
||||
{
|
||||
public:
|
||||
ShaderStage() = default;
|
||||
virtual ~ShaderStage();
|
||||
ShaderModule() = default;
|
||||
virtual ~ShaderModule();
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -29,8 +29,8 @@ 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<ShaderStage> InstantiateShaderStage(const ShaderAst& shaderAst, const ShaderWriter::States& states) override;
|
||||
std::shared_ptr<ShaderStage> InstantiateShaderStage(ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize) override;
|
||||
std::shared_ptr<ShaderModule> InstantiateShaderModule(const ShaderAst& shaderAst, const ShaderWriter::States& states) override;
|
||||
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize) override;
|
||||
std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params) override;
|
||||
std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) override;
|
||||
|
||||
|
|
|
|||
|
|
@ -9,14 +9,14 @@
|
|||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Renderer/Enums.hpp>
|
||||
#include <Nazara/Renderer/ShaderStage.hpp>
|
||||
#include <Nazara/Renderer/ShaderModule.hpp>
|
||||
#include <Nazara/Shader/ShaderWriter.hpp>
|
||||
#include <Nazara/VulkanRenderer/Wrapper/ShaderModule.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_VULKANRENDERER_API VulkanShaderStage : public ShaderStage
|
||||
class NAZARA_VULKANRENDERER_API VulkanShaderStage : public ShaderModule
|
||||
{
|
||||
public:
|
||||
VulkanShaderStage() = default;
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ namespace Nz
|
|||
for (const auto& shaderEntry : m_pipelineInfo.shaders)
|
||||
{
|
||||
if (shaderEntry.uberShader)
|
||||
renderPipelineInfo.shaderStages.push_back(shaderEntry.uberShader->Get(shaderEntry.enabledConditions));
|
||||
renderPipelineInfo.shaderModules.push_back(shaderEntry.uberShader->Get(shaderEntry.enabledConditions));
|
||||
}
|
||||
|
||||
renderPipelineInfo.vertexBuffers = vertexBuffers;
|
||||
|
|
|
|||
|
|
@ -11,8 +11,9 @@
|
|||
|
||||
namespace Nz
|
||||
{
|
||||
UberShader::UberShader(ShaderAst::StatementPtr shaderAst) :
|
||||
m_shaderAst(std::move(shaderAst))
|
||||
UberShader::UberShader(ShaderStageType shaderStage, ShaderAst::StatementPtr shaderAst) :
|
||||
m_shaderAst(std::move(shaderAst)),
|
||||
m_shaderStage(shaderStage)
|
||||
{
|
||||
//std::size_t conditionCount = m_shaderAst.GetConditionCount();
|
||||
std::size_t conditionCount = 0;
|
||||
|
|
@ -34,7 +35,7 @@ namespace Nz
|
|||
return 0;
|
||||
}
|
||||
|
||||
const std::shared_ptr<ShaderStage>& UberShader::Get(UInt64 combination)
|
||||
const std::shared_ptr<ShaderModule>& UberShader::Get(UInt64 combination)
|
||||
{
|
||||
combination &= m_combinationMask;
|
||||
|
||||
|
|
@ -44,7 +45,7 @@ namespace Nz
|
|||
ShaderWriter::States states;
|
||||
states.enabledConditions = combination;
|
||||
|
||||
std::shared_ptr<ShaderStage> stage = Graphics::Instance()->GetRenderDevice().InstantiateShaderStage(m_shaderAst, std::move(states));
|
||||
std::shared_ptr<ShaderModule> stage = Graphics::Instance()->GetRenderDevice().InstantiateShaderModule(m_shaderStage, m_shaderAst, std::move(states));
|
||||
|
||||
it = m_combinations.emplace(combination, std::move(stage)).first;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
#include <Nazara/OpenGLRenderer/OpenGLRenderPass.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLRenderPipeline.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLRenderPipelineLayout.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLShaderStage.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLShaderModule.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLTexture.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLTextureSampler.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Wrapper/Loader.hpp>
|
||||
|
|
@ -80,14 +80,14 @@ namespace Nz
|
|||
return std::make_shared<OpenGLRenderPipelineLayout>(std::move(pipelineLayoutInfo));
|
||||
}
|
||||
|
||||
std::shared_ptr<ShaderStage> OpenGLDevice::InstantiateShaderStage(const ShaderAst& shaderAst, const ShaderWriter::States& states)
|
||||
std::shared_ptr<ShaderModule> OpenGLDevice::InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states)
|
||||
{
|
||||
return std::make_shared<OpenGLShaderStage>(*this, shaderAst, states);
|
||||
return std::make_shared<OpenGLShaderModule>(*this, shaderStages, shaderAst, states);
|
||||
}
|
||||
|
||||
std::shared_ptr<ShaderStage> OpenGLDevice::InstantiateShaderStage(ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize)
|
||||
std::shared_ptr<ShaderModule> OpenGLDevice::InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize)
|
||||
{
|
||||
return std::make_shared<OpenGLShaderStage>(*this, type, lang, source, sourceSize);
|
||||
return std::make_shared<OpenGLShaderModule>(*this, shaderStages, lang, source, sourceSize);
|
||||
}
|
||||
|
||||
std::shared_ptr<Texture> OpenGLDevice::InstantiateTexture(const TextureInfo& params)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
#include <Nazara/Core/ErrorFlags.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Utils.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLRenderPipelineLayout.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLShaderStage.hpp>
|
||||
#include <Nazara/OpenGLRenderer/OpenGLShaderModule.hpp>
|
||||
#include <Nazara/Shader/GlslWriter.hpp>
|
||||
#include <cassert>
|
||||
#include <stdexcept>
|
||||
|
|
@ -21,10 +21,11 @@ namespace Nz
|
|||
if (!m_program.Create(device))
|
||||
throw std::runtime_error("failed to create program");
|
||||
|
||||
for (const auto& shaderStagePtr : m_pipelineInfo.shaderStages)
|
||||
for (const auto& shaderModulePtr : m_pipelineInfo.shaderModules)
|
||||
{
|
||||
OpenGLShaderStage& shaderStage = static_cast<OpenGLShaderStage&>(*shaderStagePtr);
|
||||
m_program.AttachShader(shaderStage.GetShader().GetObjectId());
|
||||
OpenGLShaderModule& shaderModule = static_cast<OpenGLShaderModule&>(*shaderModulePtr);
|
||||
for (const GL::Shader& shader : shaderModule.GetShaders())
|
||||
m_program.AttachShader(shader.GetObjectId());
|
||||
}
|
||||
|
||||
m_program.Link();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,136 @@
|
|||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - OpenGL Renderer"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/OpenGLRenderer/OpenGLShaderModule.hpp>
|
||||
#include <Nazara/Core/MemoryView.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Utils.hpp>
|
||||
#include <Nazara/Shader/GlslWriter.hpp>
|
||||
#include <Nazara/Shader/ShaderAstSerializer.hpp>
|
||||
#include <Nazara/Shader/ShaderLangLexer.hpp>
|
||||
#include <Nazara/Shader/ShaderLangParser.hpp>
|
||||
#include <stdexcept>
|
||||
#include <Nazara/OpenGLRenderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
OpenGLShaderModule::OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states)
|
||||
{
|
||||
NazaraAssert(shaderStages != 0, "at least one shader stage must be specified");
|
||||
Create(device, shaderStages, shaderAst, states);
|
||||
}
|
||||
|
||||
OpenGLShaderModule::OpenGLShaderModule(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize)
|
||||
{
|
||||
NazaraAssert(shaderStages != 0, "at least one shader stage must be specified");
|
||||
|
||||
switch (lang)
|
||||
{
|
||||
case ShaderLanguage::GLSL:
|
||||
{
|
||||
for (std::size_t i = 0; i < ShaderStageTypeCount; ++i)
|
||||
{
|
||||
ShaderStageType shaderStage = static_cast<ShaderStageType>(i);
|
||||
if (shaderStages.Test(shaderStage))
|
||||
{
|
||||
NazaraAssert(shaderStages == shaderStage, "when supplying GLSL, only one shader stage type can be specified");
|
||||
|
||||
GL::Shader shader;
|
||||
if (!shader.Create(device, ToOpenGL(shaderStage)))
|
||||
throw std::runtime_error("failed to create shader"); //< TODO: Handle error message
|
||||
|
||||
shader.SetSource(reinterpret_cast<const char*>(source), GLint(sourceSize));
|
||||
shader.Compile();
|
||||
CheckCompilationStatus(shader);
|
||||
|
||||
m_shaders.emplace_back(std::move(shader));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case ShaderLanguage::NazaraBinary:
|
||||
{
|
||||
auto shader = ShaderAst::UnserializeShader(source, sourceSize);
|
||||
Create(device, shaderStages, shader, {});
|
||||
break;
|
||||
}
|
||||
|
||||
case ShaderLanguage::NazaraShader:
|
||||
{
|
||||
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);
|
||||
Create(device, shaderStages, shaderAst, {});
|
||||
break;
|
||||
}
|
||||
|
||||
case ShaderLanguage::SpirV:
|
||||
{
|
||||
throw std::runtime_error("TODO");
|
||||
|
||||
// TODO: Parse SpirV to extract entry points?
|
||||
|
||||
/*if (!device.GetReferenceContext().IsExtensionSupported(GL::Extension::SpirV))
|
||||
throw std::runtime_error("SpirV is not supported by this OpenGL implementation");
|
||||
|
||||
m_shader.SetBinarySource(GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, source, GLsizei(sourceSize));
|
||||
m_shader.SpecializeShader("main", 0U, nullptr, nullptr);*/
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
throw std::runtime_error("Unsupported shader language");
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLShaderModule::CheckCompilationStatus(GL::Shader& shader)
|
||||
{
|
||||
std::string errorLog;
|
||||
if (!shader.GetCompilationStatus(&errorLog))
|
||||
throw std::runtime_error("Failed to compile shader: " + errorLog);
|
||||
}
|
||||
|
||||
void OpenGLShaderModule::Create(OpenGLDevice& device, ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states)
|
||||
{
|
||||
const auto& context = device.GetReferenceContext();
|
||||
const auto& contextParams = context.GetParams();
|
||||
|
||||
GlslWriter::Environment env;
|
||||
env.glES = (contextParams.type == GL::ContextType::OpenGL_ES);
|
||||
env.glMajorVersion = contextParams.glMajorVersion;
|
||||
env.glMinorVersion = contextParams.glMinorVersion;
|
||||
env.extCallback = [&](const std::string_view& ext)
|
||||
{
|
||||
return context.IsExtensionSupported(std::string(ext));
|
||||
};
|
||||
env.flipYPosition = true;
|
||||
|
||||
GlslWriter writer;
|
||||
writer.SetEnv(env);
|
||||
|
||||
for (std::size_t i = 0; i < ShaderStageTypeCount; ++i)
|
||||
{
|
||||
ShaderStageType shaderStage = static_cast<ShaderStageType>(i);
|
||||
if (shaderStages.Test(shaderStage))
|
||||
{
|
||||
GL::Shader shader;
|
||||
|
||||
if (!shader.Create(device, ToOpenGL(shaderStage)))
|
||||
throw std::runtime_error("failed to create shader"); //< TODO: Handle error message
|
||||
|
||||
std::string code = writer.Generate(shaderStage, shaderAst, states);
|
||||
|
||||
shader.SetSource(code.data(), code.size());
|
||||
shader.Compile();
|
||||
|
||||
CheckCompilationStatus(shader);
|
||||
|
||||
m_shaders.emplace_back(std::move(shader));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,94 +0,0 @@
|
|||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - OpenGL Renderer"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/OpenGLRenderer/OpenGLShaderStage.hpp>
|
||||
#include <Nazara/Core/MemoryView.hpp>
|
||||
#include <Nazara/OpenGLRenderer/Utils.hpp>
|
||||
#include <Nazara/Shader/GlslWriter.hpp>
|
||||
#include <Nazara/Shader/ShaderAst.hpp>
|
||||
#include <Nazara/Shader/ShaderAstSerializer.hpp>
|
||||
#include <stdexcept>
|
||||
#include <Nazara/OpenGLRenderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
OpenGLShaderStage::OpenGLShaderStage(OpenGLDevice& device, const ShaderAst& shaderAst, const ShaderWriter::States& states)
|
||||
{
|
||||
if (!m_shader.Create(device, ToOpenGL(shaderAst.GetStage())))
|
||||
throw std::runtime_error("failed to create shader"); //< TODO: Handle error message
|
||||
|
||||
Create(device, shaderAst, states);
|
||||
CheckCompilationStatus();
|
||||
}
|
||||
|
||||
OpenGLShaderStage::OpenGLShaderStage(OpenGLDevice& device, ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize)
|
||||
{
|
||||
if (!m_shader.Create(device, ToOpenGL(type)))
|
||||
throw std::runtime_error("failed to create shader"); //< TODO: Handle error message
|
||||
|
||||
switch (lang)
|
||||
{
|
||||
case ShaderLanguage::GLSL:
|
||||
m_shader.SetSource(reinterpret_cast<const char*>(source), GLint(sourceSize));
|
||||
m_shader.Compile();
|
||||
break;
|
||||
|
||||
case ShaderLanguage::NazaraBinary:
|
||||
{
|
||||
auto shader = UnserializeShader(source, sourceSize);
|
||||
if (shader.GetStage() != type)
|
||||
throw std::runtime_error("incompatible shader stage");
|
||||
|
||||
Create(device, shader, {});
|
||||
break;
|
||||
}
|
||||
|
||||
case ShaderLanguage::SpirV:
|
||||
{
|
||||
if (!device.GetReferenceContext().IsExtensionSupported(GL::Extension::SpirV))
|
||||
throw std::runtime_error("SpirV is not supported by this OpenGL implementation");
|
||||
|
||||
m_shader.SetBinarySource(GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, source, GLsizei(sourceSize));
|
||||
m_shader.SpecializeShader("main", 0U, nullptr, nullptr);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
throw std::runtime_error("Unsupported shader language");
|
||||
}
|
||||
|
||||
CheckCompilationStatus();
|
||||
}
|
||||
|
||||
void OpenGLShaderStage::CheckCompilationStatus()
|
||||
{
|
||||
std::string errorLog;
|
||||
if (!m_shader.GetCompilationStatus(&errorLog))
|
||||
throw std::runtime_error("Failed to compile shader: " + errorLog);
|
||||
}
|
||||
|
||||
void OpenGLShaderStage::Create(OpenGLDevice& device, const ShaderAst& shaderAst, const ShaderWriter::States& states)
|
||||
{
|
||||
const auto& context = device.GetReferenceContext();
|
||||
const auto& contextParams = context.GetParams();
|
||||
|
||||
GlslWriter::Environment env;
|
||||
env.glES = (contextParams.type == GL::ContextType::OpenGL_ES);
|
||||
env.glMajorVersion = contextParams.glMajorVersion;
|
||||
env.glMinorVersion = contextParams.glMinorVersion;
|
||||
env.extCallback = [&](const std::string_view& ext)
|
||||
{
|
||||
return context.IsExtensionSupported(std::string(ext));
|
||||
};
|
||||
env.flipYPosition = true;
|
||||
|
||||
GlslWriter writer;
|
||||
writer.SetEnv(env);
|
||||
|
||||
std::string code = writer.Generate(shaderAst, states);
|
||||
|
||||
m_shader.SetSource(code.data(), code.size());
|
||||
m_shader.Compile();
|
||||
}
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@ namespace Nz
|
|||
{
|
||||
RenderDevice::~RenderDevice() = default;
|
||||
|
||||
std::shared_ptr<ShaderStage> RenderDevice::InstantiateShaderStage(ShaderStageType type, ShaderLanguage lang, const std::filesystem::path& sourcePath)
|
||||
std::shared_ptr<ShaderModule> RenderDevice::InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const std::filesystem::path& sourcePath)
|
||||
{
|
||||
File file(sourcePath);
|
||||
if (!file.Open(OpenMode_ReadOnly | OpenMode_Text))
|
||||
|
|
@ -29,6 +29,6 @@ namespace Nz
|
|||
return {};
|
||||
}
|
||||
|
||||
return InstantiateShaderStage(type, lang, source.data(), source.size());
|
||||
return InstantiateShaderModule(shaderStages, lang, source.data(), source.size());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,10 +2,10 @@
|
|||
// This file is part of the "Nazara Engine - Renderer module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Renderer/ShaderStage.hpp>
|
||||
#include <Nazara/Renderer/ShaderModule.hpp>
|
||||
#include <Nazara/Renderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
ShaderStage::~ShaderStage() = default;
|
||||
ShaderModule::~ShaderModule() = default;
|
||||
}
|
||||
|
|
@ -51,7 +51,7 @@ namespace Nz
|
|||
return pipelineLayout;
|
||||
}
|
||||
|
||||
std::shared_ptr<ShaderStage> VulkanDevice::InstantiateShaderStage(const ShaderAst& shaderAst, const ShaderWriter::States& states)
|
||||
std::shared_ptr<ShaderModule> VulkanDevice::InstantiateShaderModule(const ShaderAst& shaderAst, const ShaderWriter::States& states)
|
||||
{
|
||||
auto stage = std::make_shared<VulkanShaderStage>();
|
||||
if (!stage->Create(*this, shaderAst, states))
|
||||
|
|
@ -60,7 +60,7 @@ namespace Nz
|
|||
return stage;
|
||||
}
|
||||
|
||||
std::shared_ptr<ShaderStage> VulkanDevice::InstantiateShaderStage(ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize)
|
||||
std::shared_ptr<ShaderModule> VulkanDevice::InstantiateShaderModule(ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize)
|
||||
{
|
||||
auto stage = std::make_shared<VulkanShaderStage>();
|
||||
if (!stage->Create(*this, type, lang, source, sourceSize))
|
||||
|
|
|
|||
|
|
@ -164,7 +164,7 @@ namespace Nz
|
|||
{
|
||||
std::vector<VkPipelineShaderStageCreateInfo> shaderStageCreateInfos;
|
||||
|
||||
for (auto&& stagePtr : pipelineInfo.shaderStages)
|
||||
for (auto&& stagePtr : pipelineInfo.shaderModules)
|
||||
{
|
||||
Nz::VulkanShaderStage& vulkanStage = *static_cast<Nz::VulkanShaderStage*>(stagePtr.get());
|
||||
|
||||
|
|
|
|||
|
|
@ -20,14 +20,7 @@ namespace Nz
|
|||
writer.SetEnv(env);
|
||||
|
||||
std::vector<UInt32> code = writer.Generate(shader, states);
|
||||
|
||||
if (!m_shaderModule.Create(device, code.data(), code.size() * sizeof(UInt32)))
|
||||
{
|
||||
NazaraError("Failed to create shader module");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return Create(device, m_stage, ShaderLanguage::SpirV, code.data(), code.size() * sizeof(UInt32));
|
||||
}
|
||||
|
||||
bool VulkanShaderStage::Create(Vk::Device& device, ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize)
|
||||
|
|
|
|||
Loading…
Reference in New Issue