VulkanRenderer: Fix handling of shader modules
This commit is contained in:
parent
09df5f389e
commit
feffcfa6e5
|
|
@ -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<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<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags stages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states) override;
|
||||
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags stages, 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,53 @@
|
|||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Vulkan Renderer"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_VULKANRENDERER_VULKANSHADERSTAGE_HPP
|
||||
#define NAZARA_VULKANRENDERER_VULKANSHADERSTAGE_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Renderer/Enums.hpp>
|
||||
#include <Nazara/Renderer/ShaderModule.hpp>
|
||||
#include <Nazara/Shader/ShaderNodes.hpp>
|
||||
#include <Nazara/Shader/ShaderWriter.hpp>
|
||||
#include <Nazara/VulkanRenderer/Wrapper/ShaderModule.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_VULKANRENDERER_API VulkanShaderModule : public ShaderModule
|
||||
{
|
||||
public:
|
||||
struct Stage;
|
||||
|
||||
VulkanShaderModule() = default;
|
||||
VulkanShaderModule(const VulkanShaderModule&) = delete;
|
||||
VulkanShaderModule(VulkanShaderModule&&) = delete;
|
||||
~VulkanShaderModule() = default;
|
||||
|
||||
bool Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states);
|
||||
bool Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize);
|
||||
|
||||
inline const Vk::ShaderModule& GetHandle() const;
|
||||
inline const std::vector<Stage>& GetStages() const;
|
||||
|
||||
VulkanShaderModule& operator=(const VulkanShaderModule&) = delete;
|
||||
VulkanShaderModule& operator=(VulkanShaderModule&&) = delete;
|
||||
|
||||
struct Stage
|
||||
{
|
||||
ShaderStageType stage;
|
||||
std::string name;
|
||||
};
|
||||
|
||||
private:
|
||||
Vk::ShaderModule m_shaderModule;
|
||||
std::vector<Stage> m_stages;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/VulkanRenderer/VulkanShaderModule.inl>
|
||||
|
||||
#endif // NAZARA_VULKANRENDERER_VULKANSHADERSTAGE_HPP
|
||||
|
|
@ -2,19 +2,19 @@
|
|||
// This file is part of the "Nazara Engine - Vulkan Renderer"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/VulkanRenderer/VulkanShaderStage.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanShaderModule.hpp>
|
||||
#include <Nazara/VulkanRenderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline const Vk::ShaderModule& VulkanShaderStage::GetHandle() const
|
||||
inline const Vk::ShaderModule& VulkanShaderModule::GetHandle() const
|
||||
{
|
||||
return m_shaderModule;
|
||||
}
|
||||
|
||||
inline ShaderStageType VulkanShaderStage::GetStageType() const
|
||||
inline auto VulkanShaderModule::GetStages() const -> const std::vector<Stage>&
|
||||
{
|
||||
return m_stage;
|
||||
return m_stages;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,44 +0,0 @@
|
|||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Vulkan Renderer"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_VULKANRENDERER_VULKANSHADERSTAGE_HPP
|
||||
#define NAZARA_VULKANRENDERER_VULKANSHADERSTAGE_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Renderer/Enums.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 ShaderModule
|
||||
{
|
||||
public:
|
||||
VulkanShaderStage() = default;
|
||||
VulkanShaderStage(const VulkanShaderStage&) = delete;
|
||||
VulkanShaderStage(VulkanShaderStage&&) = delete;
|
||||
~VulkanShaderStage() = default;
|
||||
|
||||
bool Create(Vk::Device& device, const ShaderAst& shader, const ShaderWriter::States& states);
|
||||
bool Create(Vk::Device& device, ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize);
|
||||
|
||||
inline const Vk::ShaderModule& GetHandle() const;
|
||||
inline ShaderStageType GetStageType() const;
|
||||
|
||||
VulkanShaderStage& operator=(const VulkanShaderStage&) = delete;
|
||||
VulkanShaderStage& operator=(VulkanShaderStage&&) = delete;
|
||||
|
||||
private:
|
||||
Vk::ShaderModule m_shaderModule;
|
||||
ShaderStageType m_stage;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/VulkanRenderer/VulkanShaderStage.inl>
|
||||
|
||||
#endif // NAZARA_VULKANRENDERER_VULKANSHADERSTAGE_HPP
|
||||
|
|
@ -7,7 +7,7 @@
|
|||
#include <Nazara/VulkanRenderer/VulkanRenderPass.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanRenderPipeline.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanRenderPipelineLayout.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanShaderStage.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanShaderModule.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanSingleFramebuffer.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanTexture.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanTextureSampler.hpp>
|
||||
|
|
@ -46,25 +46,25 @@ namespace Nz
|
|||
{
|
||||
auto pipelineLayout = std::make_shared<VulkanRenderPipelineLayout>();
|
||||
if (!pipelineLayout->Create(*this, std::move(pipelineLayoutInfo)))
|
||||
return {};
|
||||
throw std::runtime_error("failed to instanciate vulkan render pipeline layout");
|
||||
|
||||
return pipelineLayout;
|
||||
}
|
||||
|
||||
std::shared_ptr<ShaderModule> VulkanDevice::InstantiateShaderModule(const ShaderAst& shaderAst, const ShaderWriter::States& states)
|
||||
std::shared_ptr<ShaderModule> VulkanDevice::InstantiateShaderModule(ShaderStageTypeFlags stages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states)
|
||||
{
|
||||
auto stage = std::make_shared<VulkanShaderStage>();
|
||||
if (!stage->Create(*this, shaderAst, states))
|
||||
return {};
|
||||
auto stage = std::make_shared<VulkanShaderModule>();
|
||||
if (!stage->Create(*this, stages, shaderAst, states))
|
||||
throw std::runtime_error("failed to instanciate vulkan shader module");
|
||||
|
||||
return stage;
|
||||
}
|
||||
|
||||
std::shared_ptr<ShaderModule> VulkanDevice::InstantiateShaderModule(ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize)
|
||||
std::shared_ptr<ShaderModule> VulkanDevice::InstantiateShaderModule(ShaderStageTypeFlags stages, ShaderLanguage lang, const void* source, std::size_t sourceSize)
|
||||
{
|
||||
auto stage = std::make_shared<VulkanShaderStage>();
|
||||
if (!stage->Create(*this, type, lang, source, sourceSize))
|
||||
return {};
|
||||
auto stage = std::make_shared<VulkanShaderModule>();
|
||||
if (!stage->Create(*this, stages, lang, source, sourceSize))
|
||||
throw std::runtime_error("failed to instanciate vulkan shader module");
|
||||
|
||||
return stage;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
#include <Nazara/Core/ErrorFlags.hpp>
|
||||
#include <Nazara/VulkanRenderer/Utils.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanRenderPipelineLayout.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanShaderStage.hpp>
|
||||
#include <Nazara/VulkanRenderer/VulkanShaderModule.hpp>
|
||||
#include <cassert>
|
||||
#include <Nazara/VulkanRenderer/Debug.hpp>
|
||||
|
||||
|
|
@ -166,13 +166,15 @@ namespace Nz
|
|||
|
||||
for (auto&& stagePtr : pipelineInfo.shaderModules)
|
||||
{
|
||||
Nz::VulkanShaderStage& vulkanStage = *static_cast<Nz::VulkanShaderStage*>(stagePtr.get());
|
||||
|
||||
VkPipelineShaderStageCreateInfo& createInfo = shaderStageCreateInfos.emplace_back();
|
||||
createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
||||
createInfo.module = vulkanStage.GetHandle();
|
||||
createInfo.pName = "main";
|
||||
createInfo.stage = ToVulkan(vulkanStage.GetStageType());
|
||||
Nz::VulkanShaderModule& vulkanModule = *static_cast<Nz::VulkanShaderModule*>(stagePtr.get());
|
||||
for (auto& stage : vulkanModule.GetStages())
|
||||
{
|
||||
VkPipelineShaderStageCreateInfo& createInfo = shaderStageCreateInfos.emplace_back();
|
||||
createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
||||
createInfo.module = vulkanModule.GetHandle();
|
||||
createInfo.pName = stage.name.data();
|
||||
createInfo.stage = ToVulkan(stage.stage);
|
||||
}
|
||||
}
|
||||
|
||||
return shaderStageCreateInfos;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,144 @@
|
|||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Vulkan Renderer"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/VulkanRenderer/VulkanShaderModule.hpp>
|
||||
#include <Nazara/Shader/ShaderAstSerializer.hpp>
|
||||
#include <Nazara/Shader/ShaderLangLexer.hpp>
|
||||
#include <Nazara/Shader/ShaderLangParser.hpp>
|
||||
#include <Nazara/Shader/SpirvDecoder.hpp>
|
||||
#include <Nazara/Shader/SpirvWriter.hpp>
|
||||
#include <Nazara/VulkanRenderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
namespace
|
||||
{
|
||||
struct SpirvEntryPointExtractor : SpirvDecoder
|
||||
{
|
||||
struct EntryPoint
|
||||
{
|
||||
SpirvExecutionModel executionModel;
|
||||
std::string name;
|
||||
};
|
||||
|
||||
std::vector<EntryPoint> entryPoints;
|
||||
|
||||
bool HandleOpcode(const SpirvInstruction& instruction, UInt32 wordCount) override
|
||||
{
|
||||
switch (instruction.op)
|
||||
{
|
||||
// All instructions that can appear before OpEntryPoint
|
||||
case SpirvOp::OpCapability:
|
||||
case SpirvOp::OpExtension:
|
||||
case SpirvOp::OpExtInstImport:
|
||||
case SpirvOp::OpMemoryModel:
|
||||
return true;
|
||||
|
||||
case SpirvOp::OpEntryPoint:
|
||||
{
|
||||
SpirvExecutionModel executionModel = static_cast<SpirvExecutionModel>(ReadWord());
|
||||
std::string name = ReadString();
|
||||
|
||||
entryPoints.push_back({
|
||||
executionModel,
|
||||
std::move(name)
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Return false for other instructions (which means OpEntryPoint will no longer appear from here)
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
bool VulkanShaderModule::Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderAst::StatementPtr& shaderAst, const ShaderWriter::States& states)
|
||||
{
|
||||
SpirvWriter::Environment env;
|
||||
|
||||
SpirvWriter writer;
|
||||
writer.SetEnv(env);
|
||||
|
||||
std::vector<UInt32> code = writer.Generate(shaderAst, states);
|
||||
return Create(device, shaderStages, ShaderLanguage::SpirV, code.data(), code.size() * sizeof(UInt32));
|
||||
}
|
||||
|
||||
bool VulkanShaderModule::Create(Vk::Device& device, ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const void* source, std::size_t sourceSize)
|
||||
{
|
||||
switch (lang)
|
||||
{
|
||||
case ShaderLanguage::GLSL:
|
||||
case ShaderLanguage::HLSL:
|
||||
case ShaderLanguage::MSL:
|
||||
break;
|
||||
|
||||
case ShaderLanguage::NazaraBinary:
|
||||
{
|
||||
auto shader = ShaderAst::UnserializeShader(source, sourceSize);
|
||||
return Create(device, shaderStages, shader, {});
|
||||
}
|
||||
|
||||
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);
|
||||
return Create(device, shaderStages, shaderAst, {});
|
||||
}
|
||||
|
||||
case ShaderLanguage::SpirV:
|
||||
{
|
||||
SpirvEntryPointExtractor extractor;
|
||||
extractor.Decode(reinterpret_cast<const Nz::UInt32*>(source), sourceSize);
|
||||
|
||||
ShaderStageTypeFlags remainingStages = shaderStages;
|
||||
for (auto& entryPoint : extractor.entryPoints)
|
||||
{
|
||||
ShaderStageType stageType;
|
||||
switch (entryPoint.executionModel)
|
||||
{
|
||||
case SpirvExecutionModel::Fragment:
|
||||
stageType = ShaderStageType::Fragment;
|
||||
break;
|
||||
|
||||
case SpirvExecutionModel::Vertex:
|
||||
stageType = ShaderStageType::Vertex;
|
||||
break;
|
||||
|
||||
default:
|
||||
continue; //< Ignore
|
||||
}
|
||||
|
||||
m_stages.push_back({
|
||||
stageType,
|
||||
std::move(entryPoint.name)
|
||||
});
|
||||
|
||||
remainingStages.Clear(stageType);
|
||||
}
|
||||
|
||||
if (remainingStages != 0)
|
||||
{
|
||||
NazaraError("Vulkan shader module does not handle all requested stage types");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!m_shaderModule.Create(device, reinterpret_cast<const Nz::UInt32*>(source), sourceSize))
|
||||
{
|
||||
NazaraError("failed to create shader module");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
NazaraError("this language is not supported");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,64 +0,0 @@
|
|||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Vulkan Renderer"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/VulkanRenderer/VulkanShaderStage.hpp>
|
||||
#include <Nazara/Shader/ShaderAst.hpp>
|
||||
#include <Nazara/Shader/ShaderAstSerializer.hpp>
|
||||
#include <Nazara/Shader/SpirvWriter.hpp>
|
||||
#include <Nazara/VulkanRenderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
bool VulkanShaderStage::Create(Vk::Device& device, const ShaderAst& shader, const ShaderWriter::States& states)
|
||||
{
|
||||
m_stage = shader.GetStage();
|
||||
|
||||
SpirvWriter::Environment env;
|
||||
|
||||
SpirvWriter writer;
|
||||
writer.SetEnv(env);
|
||||
|
||||
std::vector<UInt32> code = writer.Generate(shader, states);
|
||||
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)
|
||||
{
|
||||
m_stage = type;
|
||||
|
||||
switch (lang)
|
||||
{
|
||||
case ShaderLanguage::NazaraBinary:
|
||||
{
|
||||
auto shader = UnserializeShader(source, sourceSize);
|
||||
if (shader.GetStage() != type)
|
||||
throw std::runtime_error("incompatible shader stage");
|
||||
|
||||
if (!Create(device, shader, {}))
|
||||
return false;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case ShaderLanguage::SpirV:
|
||||
{
|
||||
if (!m_shaderModule.Create(device, reinterpret_cast<const Nz::UInt32*>(source), sourceSize))
|
||||
{
|
||||
NazaraError("Failed to create shader module");
|
||||
return false;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
NazaraError("this language is not supported");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue