mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2024-11-15 22:09:19 -07:00
61a656570e
"Cache data" can be used to assist a driver with creating pipelines by using previously-compiled shader ISA.
132 lines
4.1 KiB
C++
132 lines
4.1 KiB
C++
// Copyright 2017 Dolphin Emulator Project
|
|
// Licensed under GPLv2+
|
|
// Refer to the license.txt file included.
|
|
|
|
#include "Common/Align.h"
|
|
#include "Common/Assert.h"
|
|
|
|
#include "VideoBackends/Vulkan/ObjectCache.h"
|
|
#include "VideoBackends/Vulkan/ShaderCompiler.h"
|
|
#include "VideoBackends/Vulkan/VKShader.h"
|
|
#include "VideoBackends/Vulkan/VulkanContext.h"
|
|
|
|
namespace Vulkan
|
|
{
|
|
VKShader::VKShader(ShaderStage stage, std::vector<u32> spv, VkShaderModule mod)
|
|
: AbstractShader(stage), m_spv(std::move(spv)), m_module(mod),
|
|
m_compute_pipeline(VK_NULL_HANDLE)
|
|
{
|
|
}
|
|
|
|
VKShader::VKShader(std::vector<u32> spv, VkPipeline compute_pipeline)
|
|
: AbstractShader(ShaderStage::Compute), m_spv(std::move(spv)), m_module(VK_NULL_HANDLE),
|
|
m_compute_pipeline(compute_pipeline)
|
|
{
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
static std::unique_ptr<VKShader> CreateShaderObject(ShaderStage stage,
|
|
ShaderCompiler::SPIRVCodeVector spv)
|
|
{
|
|
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;
|
|
}
|
|
|
|
// If it's a graphics shader, we defer pipeline creation.
|
|
if (stage != ShaderStage::Compute)
|
|
return std::make_unique<VKShader>(stage, std::move(spv), mod);
|
|
|
|
// If it's a compute shader, we create the pipeline straight away.
|
|
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)
|
|
{
|
|
LOG_VULKAN_ERROR(res, "vkCreateComputePipelines failed: ");
|
|
return nullptr;
|
|
}
|
|
|
|
return std::make_unique<VKShader>(std::move(spv), pipeline);
|
|
}
|
|
|
|
std::unique_ptr<VKShader> VKShader::CreateFromSource(ShaderStage stage, const char* source,
|
|
size_t length)
|
|
{
|
|
ShaderCompiler::SPIRVCodeVector spv;
|
|
bool result;
|
|
switch (stage)
|
|
{
|
|
case ShaderStage::Vertex:
|
|
result = ShaderCompiler::CompileVertexShader(&spv, source, length);
|
|
break;
|
|
case ShaderStage::Geometry:
|
|
result = ShaderCompiler::CompileGeometryShader(&spv, source, length);
|
|
break;
|
|
case ShaderStage::Pixel:
|
|
result = ShaderCompiler::CompileFragmentShader(&spv, source, length);
|
|
break;
|
|
case ShaderStage::Compute:
|
|
result = ShaderCompiler::CompileComputeShader(&spv, source, length);
|
|
break;
|
|
default:
|
|
result = false;
|
|
break;
|
|
}
|
|
|
|
if (!result)
|
|
return nullptr;
|
|
|
|
return CreateShaderObject(stage, std::move(spv));
|
|
}
|
|
|
|
std::unique_ptr<VKShader> VKShader::CreateFromBinary(ShaderStage stage, const void* data,
|
|
size_t length)
|
|
{
|
|
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);
|
|
|
|
return CreateShaderObject(stage, std::move(spv));
|
|
}
|
|
|
|
} // namespace Vulkan
|