| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/Vulkan/VKShader.h" |
| 5 | |
| 6 | #include "Common/Align.h" |
| 7 | |
| 8 | #include "VideoBackends/Vulkan/ObjectCache.h" |
| 9 | #include "VideoBackends/Vulkan/ShaderCompiler.h" |
| 10 | #include "VideoBackends/Vulkan/VulkanContext.h" |
| 11 | |
| 12 | #include "VideoCommon/VideoConfig.h" |
| 13 | |
| 14 | namespace Vulkan |
| 15 | { |
| 16 | VKShader::VKShader(ShaderStage stage, std::vector<u32> spv, VkShaderModule mod, |
| 17 | std::string_view name) |
| 18 | : AbstractShader(stage), m_spv(std::move(spv)), m_module(mod), |
| 19 | m_compute_pipeline(VK_NULL_HANDLE), m_name(name) |
| 20 | { |
| 21 | if (!m_name.empty() && g_backend_info.bSupportsSettingObjectNames) |
| 22 | { |
| 23 | VkDebugUtilsObjectNameInfoEXT name_info = {}; |
| 24 | name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT; |
| 25 | name_info.objectType = VK_OBJECT_TYPE_SHADER_MODULE; |
| 26 | name_info.objectHandle = reinterpret_cast<uint64_t>(m_module); |
| 27 | name_info.pObjectName = m_name.data(); |
| 28 | vkSetDebugUtilsObjectNameEXT(g_vulkan_context->GetDevice(), &name_info); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | VKShader::VKShader(std::vector<u32> spv, VkPipeline compute_pipeline, std::string_view name) |
| 33 | : AbstractShader(ShaderStage::Compute), m_spv(std::move(spv)), m_module(VK_NULL_HANDLE), |
| 34 | m_compute_pipeline(compute_pipeline), m_name(name) |
| 35 | { |
| 36 | if (!m_name.empty() && g_backend_info.bSupportsSettingObjectNames) |
| 37 | { |
| 38 | VkDebugUtilsObjectNameInfoEXT name_info = {}; |
| 39 | name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT; |
| 40 | name_info.objectType = VK_OBJECT_TYPE_PIPELINE; |
| 41 | name_info.objectHandle = reinterpret_cast<uint64_t>(m_compute_pipeline); |
| 42 | name_info.pObjectName = m_name.data(); |
| 43 | vkSetDebugUtilsObjectNameEXT(g_vulkan_context->GetDevice(), &name_info); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | VKShader::~VKShader() |
| 48 | { |
| 49 | if (m_stage != ShaderStage::Compute) |
| 50 | vkDestroyShaderModule(g_vulkan_context->GetDevice(), m_module, nullptr); |
| 51 | else |
| 52 | vkDestroyPipeline(g_vulkan_context->GetDevice(), m_compute_pipeline, nullptr); |
| 53 | } |
| 54 | |
| 55 | AbstractShader::BinaryData VKShader::GetBinary() const |
| 56 | { |
| 57 | BinaryData ret(sizeof(u32) * m_spv.size()); |
| 58 | std::memcpy(ret.data(), m_spv.data(), sizeof(u32) * m_spv.size()); |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | static std::unique_ptr<VKShader> |
| 63 | CreateShaderObject(ShaderStage stage, ShaderCompiler::SPIRVCodeVector spv, std::string_view name) |
| 64 | { |
| 65 | VkShaderModuleCreateInfo info = {}; |
| 66 | info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; |
| 67 | info.codeSize = spv.size() * sizeof(u32); |
| 68 | info.pCode = spv.data(); |
| 69 | |
| 70 | VkShaderModule mod; |
| 71 | VkResult res = vkCreateShaderModule(g_vulkan_context->GetDevice(), &info, nullptr, &mod); |
| 72 | if (res != VK_SUCCESS) |
| 73 | { |
| 74 | LOG_VULKAN_ERROR(res, "vkCreateShaderModule failed: "); |
| 75 | return VK_NULL_HANDLE; |
| 76 | } |
| 77 | |
| 78 | // If it's a graphics shader, we defer pipeline creation. |
| 79 | if (stage != ShaderStage::Compute) |
| 80 | return std::make_unique<VKShader>(stage, std::move(spv), mod, name); |
| 81 | |
| 82 | // If it's a compute shader, we create the pipeline straight away. |
| 83 | const VkComputePipelineCreateInfo pipeline_info = { |
| 84 | VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, |
| 85 | nullptr, |
| 86 | 0, |
| 87 | {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, nullptr, 0, VK_SHADER_STAGE_COMPUTE_BIT, |
| 88 | mod, "main", nullptr}, |
| 89 | g_object_cache->GetPipelineLayout(PIPELINE_LAYOUT_COMPUTE), |
| 90 | VK_NULL_HANDLE, |
| 91 | -1}; |
| 92 | |
| 93 | VkPipeline pipeline; |
| 94 | res = vkCreateComputePipelines(g_vulkan_context->GetDevice(), g_object_cache->GetPipelineCache(), |
| 95 | 1, &pipeline_info, nullptr, &pipeline); |
| 96 | |
| 97 | // Shader module is no longer needed, now it is compiled to a pipeline. |
| 98 | vkDestroyShaderModule(g_vulkan_context->GetDevice(), mod, nullptr); |
| 99 | |
| 100 | if (res != VK_SUCCESS) |
| 101 | { |
| 102 | LOG_VULKAN_ERROR(res, "vkCreateComputePipelines failed: "); |
| 103 | return nullptr; |
| 104 | } |
| 105 | |
| 106 | return std::make_unique<VKShader>(std::move(spv), pipeline, name); |
| 107 | } |
| 108 | |
| 109 | std::unique_ptr<VKShader> VKShader::CreateFromSource(ShaderStage stage, std::string_view source, |
| 110 | VideoCommon::ShaderIncluder* shader_includer, |
| 111 | std::string_view name) |
| 112 | { |
| 113 | std::optional<ShaderCompiler::SPIRVCodeVector> spv; |
| 114 | switch (stage) |
| 115 | { |
| 116 | case ShaderStage::Vertex: |
| 117 | spv = ShaderCompiler::CompileVertexShader(source, shader_includer); |
| 118 | break; |
| 119 | case ShaderStage::Geometry: |
| 120 | spv = ShaderCompiler::CompileGeometryShader(source, shader_includer); |
| 121 | break; |
| 122 | case ShaderStage::Pixel: |
| 123 | spv = ShaderCompiler::CompileFragmentShader(source, shader_includer); |
| 124 | break; |
| 125 | case ShaderStage::Compute: |
| 126 | spv = ShaderCompiler::CompileComputeShader(source, shader_includer); |
| 127 | break; |
| 128 | default: |
| 129 | break; |
| 130 | } |
| 131 | |
| 132 | if (!spv) |
| 133 | return nullptr; |
| 134 | |
| 135 | return CreateShaderObject(stage, std::move(*spv), name); |
| 136 | } |
| 137 | |
| 138 | std::unique_ptr<VKShader> VKShader::CreateFromBinary(ShaderStage stage, const void* data, |
| 139 | size_t length, std::string_view name) |
| 140 | { |
| 141 | const size_t size_in_words = Common::AlignUp(length, sizeof(ShaderCompiler::SPIRVCodeType)) / |
| 142 | sizeof(ShaderCompiler::SPIRVCodeType); |
| 143 | ShaderCompiler::SPIRVCodeVector spv(size_in_words); |
| 144 | if (length > 0) |
| 145 | std::memcpy(spv.data(), data, length); |
| 146 | |
| 147 | return CreateShaderObject(stage, std::move(spv), name); |
| 148 | } |
| 149 | |
| 150 | } // namespace Vulkan |