Files
test2/source/blender/gpu/vulkan/vk_shader_module.cc
Jeroen Bakker e6b3cc8983 Vulkan: Device command builder
This PR implements a new the threading model for building render graphs
based on tests performed last month. For out workload multithreaded
command building will block in the driver or device. So better to use a
single thread for command building.

Details of the internal working is documented at https://developer.blender.org/docs/features/gpu/vulkan/render_graph/

- When a context is activated on a thread the context asks for a
  render graph it can use by calling `VKDevice::render_graph_new`.
- Parts of the GPU backend that requires GPU commands will add a
  specific render graph node to the render graph. The nodes also
  contains a reference to all resources it needs including the
  access it needs and the image layout.
- When the context is flushed the render graph is submitted to the
  device by calling `VKDevice::render_graph_submit`.
- The device puts the render graph in `VKDevice::submission_pool`.
- There is a single background thread that gets the next render
  graph to send to the GPU (`VKDevice::submission_runner`).
  - Reorder the commands of the render graph to comply with Vulkan
    specific command order rules and reducing possible bottlenecks.
    (`VKScheduler`)
  - Generate the required barriers `VKCommandBuilder::groups_extract_barriers`.
    This is a separate step to reduce resource locking giving other
    threads access to the resource states when they are building
    the render graph nodes.
  - GPU commands and pipeline barriers are recorded to a VkCommandBuffer.
    (`VKCommandBuilder::record_commands`)
  - When completed the command buffer can be submitted to the device
    queue. `vkQueueSubmit`
  - Render graphs that have been submitted can be reused by a next
    thread. This is done by pushing the render graph to the
    `VKDevice::unused_render_graphs` queue.

Pull Request: https://projects.blender.org/blender/blender/pulls/132681
2025-01-27 08:55:23 +01:00

64 lines
1.7 KiB
C++

/* SPDX-FileCopyrightText: 2024 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup gpu
*/
#include "vk_shader_module.hh"
#include "vk_backend.hh"
#include "vk_shader.hh"
#include <iomanip>
#include <sstream>
namespace blender::gpu {
VKShaderModule::~VKShaderModule()
{
VKDiscardPool &discard_pool = VKDiscardPool::discard_pool_get();
if (vk_shader_module != VK_NULL_HANDLE) {
discard_pool.discard_shader_module(vk_shader_module);
vk_shader_module = VK_NULL_HANDLE;
}
}
void VKShaderModule::finalize(StringRefNull name)
{
BLI_assert(vk_shader_module == VK_NULL_HANDLE);
if (compilation_result.GetCompilationStatus() != shaderc_compilation_status_success &&
spirv_binary.is_empty())
{
return;
}
VkShaderModuleCreateInfo create_info = {};
create_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
if (!spirv_binary.is_empty()) {
create_info.codeSize = spirv_binary.size() * sizeof(uint32_t);
create_info.pCode = spirv_binary.data();
}
else {
create_info.codeSize = (compilation_result.end() - compilation_result.begin()) *
sizeof(uint32_t);
create_info.pCode = compilation_result.begin();
}
const VKDevice &device = VKBackend::get().device;
vkCreateShaderModule(device.vk_handle(), &create_info, nullptr, &vk_shader_module);
debug::object_label(vk_shader_module, name.c_str());
}
void VKShaderModule::build_sources_hash()
{
DefaultHash<std::string> hasher;
BLI_assert(!combined_sources.empty());
uint64_t hash = hasher(combined_sources);
std::stringstream ss;
ss << std::setfill('0') << std::setw(sizeof(uint64_t) * 2) << std::hex << hash;
sources_hash = ss.str();
BLI_assert(!sources_hash.empty());
}
} // namespace blender::gpu