2017-09-08 09:42:56 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-09-08 09:42:56 +00:00
|
|
|
|
2018-02-24 15:09:32 +00:00
|
|
|
#include "Common/Align.h"
|
2017-09-08 09:42:56 +00:00
|
|
|
#include "Common/Assert.h"
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
#include "VideoBackends/Vulkan/ObjectCache.h"
|
2017-09-08 09:42:56 +00:00
|
|
|
#include "VideoBackends/Vulkan/ShaderCompiler.h"
|
|
|
|
#include "VideoBackends/Vulkan/VKShader.h"
|
|
|
|
#include "VideoBackends/Vulkan/VulkanContext.h"
|
|
|
|
|
|
|
|
namespace Vulkan
|
|
|
|
{
|
2021-08-28 05:30:05 +00:00
|
|
|
VKShader::VKShader(ShaderStage stage, std::vector<u32> spv, VkShaderModule mod,
|
|
|
|
std::string_view name)
|
2017-09-08 09:42:56 +00:00
|
|
|
: AbstractShader(stage), m_spv(std::move(spv)), m_module(mod),
|
2021-08-28 05:30:05 +00:00
|
|
|
m_compute_pipeline(VK_NULL_HANDLE), m_name(name)
|
2017-09-08 09:42:56 +00:00
|
|
|
{
|
2021-08-28 05:30:05 +00:00
|
|
|
if (!m_name.empty())
|
|
|
|
{
|
|
|
|
VkDebugUtilsObjectNameInfoEXT name_info = {};
|
|
|
|
name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
|
|
|
|
name_info.objectType = VK_OBJECT_TYPE_SHADER_MODULE;
|
|
|
|
name_info.objectHandle = reinterpret_cast<uint64_t>(m_module);
|
|
|
|
name_info.pObjectName = m_name.data();
|
|
|
|
vkSetDebugUtilsObjectNameEXT(g_vulkan_context->GetDevice(), &name_info);
|
|
|
|
}
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-28 05:30:05 +00:00
|
|
|
VKShader::VKShader(std::vector<u32> spv, VkPipeline compute_pipeline, std::string_view name)
|
2017-09-08 09:42:56 +00:00
|
|
|
: AbstractShader(ShaderStage::Compute), m_spv(std::move(spv)), m_module(VK_NULL_HANDLE),
|
2021-08-28 05:30:05 +00:00
|
|
|
m_compute_pipeline(compute_pipeline), m_name(name)
|
2017-09-08 09:42:56 +00:00
|
|
|
{
|
2021-08-28 05:30:05 +00:00
|
|
|
if (!m_name.empty())
|
|
|
|
{
|
|
|
|
VkDebugUtilsObjectNameInfoEXT name_info = {};
|
|
|
|
name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
|
|
|
|
name_info.objectType = VK_OBJECT_TYPE_PIPELINE;
|
|
|
|
name_info.objectHandle = reinterpret_cast<uint64_t>(m_compute_pipeline);
|
|
|
|
name_info.pObjectName = m_name.data();
|
|
|
|
vkSetDebugUtilsObjectNameEXT(g_vulkan_context->GetDevice(), &name_info);
|
|
|
|
}
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VKShader::~VKShader()
|
|
|
|
{
|
|
|
|
if (m_stage != ShaderStage::Compute)
|
|
|
|
vkDestroyShaderModule(g_vulkan_context->GetDevice(), m_module, nullptr);
|
|
|
|
else
|
|
|
|
vkDestroyPipeline(g_vulkan_context->GetDevice(), m_compute_pipeline, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractShader::BinaryData VKShader::GetBinary() const
|
|
|
|
{
|
|
|
|
BinaryData ret(sizeof(u32) * m_spv.size());
|
|
|
|
std::memcpy(ret.data(), m_spv.data(), sizeof(u32) * m_spv.size());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-08-28 05:30:05 +00:00
|
|
|
static std::unique_ptr<VKShader>
|
|
|
|
CreateShaderObject(ShaderStage stage, ShaderCompiler::SPIRVCodeVector spv, std::string_view name)
|
2017-09-08 09:42:56 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
VkShaderModuleCreateInfo info = {};
|
|
|
|
info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
|
|
|
info.codeSize = spv.size() * sizeof(u32);
|
|
|
|
info.pCode = spv.data();
|
|
|
|
|
|
|
|
VkShaderModule mod;
|
|
|
|
VkResult res = vkCreateShaderModule(g_vulkan_context->GetDevice(), &info, nullptr, &mod);
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_VULKAN_ERROR(res, "vkCreateShaderModule failed: ");
|
|
|
|
return VK_NULL_HANDLE;
|
|
|
|
}
|
2017-09-08 09:42:56 +00:00
|
|
|
|
|
|
|
// If it's a graphics shader, we defer pipeline creation.
|
|
|
|
if (stage != ShaderStage::Compute)
|
2021-08-28 05:30:05 +00:00
|
|
|
return std::make_unique<VKShader>(stage, std::move(spv), mod, name);
|
2017-09-08 09:42:56 +00:00
|
|
|
|
|
|
|
// If it's a compute shader, we create the pipeline straight away.
|
2019-02-15 01:59:50 +00:00
|
|
|
const VkComputePipelineCreateInfo pipeline_info = {
|
|
|
|
VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
|
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
{VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, nullptr, 0, VK_SHADER_STAGE_COMPUTE_BIT,
|
|
|
|
mod, "main", nullptr},
|
|
|
|
g_object_cache->GetPipelineLayout(PIPELINE_LAYOUT_COMPUTE),
|
|
|
|
VK_NULL_HANDLE,
|
|
|
|
-1};
|
|
|
|
|
|
|
|
VkPipeline pipeline;
|
|
|
|
res = vkCreateComputePipelines(g_vulkan_context->GetDevice(), g_object_cache->GetPipelineCache(),
|
|
|
|
1, &pipeline_info, nullptr, &pipeline);
|
|
|
|
|
|
|
|
// Shader module is no longer needed, now it is compiled to a pipeline.
|
|
|
|
vkDestroyShaderModule(g_vulkan_context->GetDevice(), mod, nullptr);
|
|
|
|
|
|
|
|
if (res != VK_SUCCESS)
|
2017-09-08 09:42:56 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
LOG_VULKAN_ERROR(res, "vkCreateComputePipelines failed: ");
|
2017-09-08 09:42:56 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-08-28 05:30:05 +00:00
|
|
|
return std::make_unique<VKShader>(std::move(spv), pipeline, name);
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-28 05:30:05 +00:00
|
|
|
std::unique_ptr<VKShader> VKShader::CreateFromSource(ShaderStage stage, std::string_view source,
|
|
|
|
std::string_view name)
|
2017-09-08 09:42:56 +00:00
|
|
|
{
|
2019-05-30 07:52:05 +00:00
|
|
|
std::optional<ShaderCompiler::SPIRVCodeVector> spv;
|
2017-09-08 09:42:56 +00:00
|
|
|
switch (stage)
|
|
|
|
{
|
|
|
|
case ShaderStage::Vertex:
|
2019-05-30 07:52:05 +00:00
|
|
|
spv = ShaderCompiler::CompileVertexShader(source);
|
2017-09-08 09:42:56 +00:00
|
|
|
break;
|
|
|
|
case ShaderStage::Geometry:
|
2019-05-30 07:52:05 +00:00
|
|
|
spv = ShaderCompiler::CompileGeometryShader(source);
|
2017-09-08 09:42:56 +00:00
|
|
|
break;
|
|
|
|
case ShaderStage::Pixel:
|
2019-05-30 07:52:05 +00:00
|
|
|
spv = ShaderCompiler::CompileFragmentShader(source);
|
2017-09-08 09:42:56 +00:00
|
|
|
break;
|
|
|
|
case ShaderStage::Compute:
|
2019-05-30 07:52:05 +00:00
|
|
|
spv = ShaderCompiler::CompileComputeShader(source);
|
2017-09-08 09:42:56 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-05-30 07:52:05 +00:00
|
|
|
if (!spv)
|
2017-09-08 09:42:56 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2021-08-28 05:30:05 +00:00
|
|
|
return CreateShaderObject(stage, std::move(*spv), name);
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<VKShader> VKShader::CreateFromBinary(ShaderStage stage, const void* data,
|
2021-08-28 05:30:05 +00:00
|
|
|
size_t length, std::string_view name)
|
2017-09-08 09:42:56 +00:00
|
|
|
{
|
2018-02-24 15:09:32 +00:00
|
|
|
const size_t size_in_words = Common::AlignUp(length, sizeof(ShaderCompiler::SPIRVCodeType)) /
|
|
|
|
sizeof(ShaderCompiler::SPIRVCodeType);
|
|
|
|
ShaderCompiler::SPIRVCodeVector spv(size_in_words);
|
|
|
|
if (length > 0)
|
|
|
|
std::memcpy(spv.data(), data, length);
|
2017-09-08 09:42:56 +00:00
|
|
|
|
2021-08-28 05:30:05 +00:00
|
|
|
return CreateShaderObject(stage, std::move(spv), name);
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Vulkan
|