7fb1f060ff
This patch adds initial support for compute shaders to the vulkan backend. As the development is oriented to the test- cases we have the implementation is limited to what is used there. It has been validated that with this patch that the following test cases are running as expected - `GPUVulkanTest.gpu_shader_compute_vbo` - `GPUVulkanTest.gpu_shader_compute_ibo` - `GPUVulkanTest.gpu_shader_compute_ssbo` - `GPUVulkanTest.gpu_storage_buffer_create_update_read` - `GPUVulkanTest.gpu_shader_compute_2d` This patch includes: - Allocating VkBuffer on device. - Uploading data from CPU to VkBuffer. - Binding VkBuffer as SSBO to a compute shader. - Execute compute shader and altering VkBuffer. - Download the VkBuffer to CPU ram. - Validate that it worked. - Use device only vertex buffer as SSBO - Use device only index buffer as SSBO - Use device only image buffers GHOST API has been changed as the original design was created before we even had support for compute shaders in blender. The function `GHOST_getVulkanBackbuffer` has been separated to retrieve the command buffer without a backbuffer (`GHOST_getVulkanCommandBuffer`). In order to do correct command buffer processing we needed access to the queue owned by GHOST. This is returned as part of the `GHOST_getVulkanHandles` function. Open topics (not considered part of this patch) - Memory barriers & command buffer encoding - Indirect compute dispatching - Rest of the test cases - Data conversions when requested data format is different than on device. - GPUVulkanTest.gpu_shader_compute_1d is supported on AMD devices. NVIDIA doesn't seem to support 1d textures. Pull-request: #104518
145 lines
4.3 KiB
C++
145 lines
4.3 KiB
C++
/* SPDX-License-Identifier: GPL-2.0-or-later
|
|
* Copyright 2023 Blender Foundation. All rights reserved. */
|
|
|
|
/** \file
|
|
* \ingroup gpu
|
|
*/
|
|
|
|
#include "vk_command_buffer.hh"
|
|
#include "vk_buffer.hh"
|
|
#include "vk_context.hh"
|
|
#include "vk_memory.hh"
|
|
#include "vk_texture.hh"
|
|
|
|
#include "BLI_assert.h"
|
|
|
|
namespace blender::gpu {
|
|
|
|
VKCommandBuffer::~VKCommandBuffer()
|
|
{
|
|
if (vk_device_ != VK_NULL_HANDLE) {
|
|
VK_ALLOCATION_CALLBACKS;
|
|
vkDestroyFence(vk_device_, vk_fence_, vk_allocation_callbacks);
|
|
vk_fence_ = VK_NULL_HANDLE;
|
|
}
|
|
}
|
|
|
|
void VKCommandBuffer::init(const VkDevice vk_device,
|
|
const VkQueue vk_queue,
|
|
VkCommandBuffer vk_command_buffer)
|
|
{
|
|
vk_device_ = vk_device;
|
|
vk_queue_ = vk_queue;
|
|
vk_command_buffer_ = vk_command_buffer;
|
|
|
|
if (vk_fence_ == VK_NULL_HANDLE) {
|
|
VK_ALLOCATION_CALLBACKS;
|
|
VkFenceCreateInfo fenceInfo{};
|
|
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
|
fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
|
vkCreateFence(vk_device_, &fenceInfo, vk_allocation_callbacks, &vk_fence_);
|
|
}
|
|
}
|
|
|
|
void VKCommandBuffer::begin_recording()
|
|
{
|
|
vkWaitForFences(vk_device_, 1, &vk_fence_, VK_TRUE, UINT64_MAX);
|
|
vkResetFences(vk_device_, 1, &vk_fence_);
|
|
vkResetCommandBuffer(vk_command_buffer_, 0);
|
|
|
|
VkCommandBufferBeginInfo begin_info = {};
|
|
begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
|
vkBeginCommandBuffer(vk_command_buffer_, &begin_info);
|
|
}
|
|
|
|
void VKCommandBuffer::end_recording()
|
|
{
|
|
vkEndCommandBuffer(vk_command_buffer_);
|
|
}
|
|
|
|
void VKCommandBuffer::bind(const VKPipeline &pipeline, VkPipelineBindPoint bind_point)
|
|
{
|
|
vkCmdBindPipeline(vk_command_buffer_, bind_point, pipeline.vk_handle());
|
|
}
|
|
void VKCommandBuffer::bind(const VKDescriptorSet &descriptor_set,
|
|
const VkPipelineLayout vk_pipeline_layout,
|
|
VkPipelineBindPoint bind_point)
|
|
{
|
|
VkDescriptorSet vk_descriptor_set = descriptor_set.vk_handle();
|
|
vkCmdBindDescriptorSets(
|
|
vk_command_buffer_, bind_point, vk_pipeline_layout, 0, 1, &vk_descriptor_set, 0, 0);
|
|
}
|
|
|
|
void VKCommandBuffer::copy(VKBuffer &dst_buffer,
|
|
VKTexture &src_texture,
|
|
Span<VkBufferImageCopy> regions)
|
|
{
|
|
vkCmdCopyImageToBuffer(vk_command_buffer_,
|
|
src_texture.vk_image_handle(),
|
|
VK_IMAGE_LAYOUT_GENERAL,
|
|
dst_buffer.vk_handle(),
|
|
regions.size(),
|
|
regions.data());
|
|
}
|
|
|
|
void VKCommandBuffer::pipeline_barrier(VkPipelineStageFlags source_stages,
|
|
VkPipelineStageFlags destination_stages)
|
|
{
|
|
vkCmdPipelineBarrier(vk_command_buffer_,
|
|
source_stages,
|
|
destination_stages,
|
|
0,
|
|
0,
|
|
nullptr,
|
|
0,
|
|
nullptr,
|
|
0,
|
|
nullptr);
|
|
}
|
|
|
|
void VKCommandBuffer::pipeline_barrier(Span<VkImageMemoryBarrier> image_memory_barriers)
|
|
{
|
|
vkCmdPipelineBarrier(vk_command_buffer_,
|
|
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
|
|
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
|
|
VK_DEPENDENCY_BY_REGION_BIT,
|
|
0,
|
|
nullptr,
|
|
0,
|
|
nullptr,
|
|
image_memory_barriers.size(),
|
|
image_memory_barriers.data());
|
|
}
|
|
|
|
void VKCommandBuffer::dispatch(int groups_x_len, int groups_y_len, int groups_z_len)
|
|
{
|
|
vkCmdDispatch(vk_command_buffer_, groups_x_len, groups_y_len, groups_z_len);
|
|
}
|
|
|
|
void VKCommandBuffer::submit()
|
|
{
|
|
end_recording();
|
|
encode_recorded_commands();
|
|
submit_encoded_commands();
|
|
begin_recording();
|
|
}
|
|
|
|
void VKCommandBuffer::encode_recorded_commands()
|
|
{
|
|
/* Intentionally not implemented. For the graphics pipeline we want to extract the
|
|
* resources and its usages so we can encode multiple commands in the same command buffer with
|
|
* the correct synchorinzations. */
|
|
}
|
|
|
|
void VKCommandBuffer::submit_encoded_commands()
|
|
{
|
|
VkSubmitInfo submit_info = {};
|
|
submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
submit_info.commandBufferCount = 1;
|
|
submit_info.pCommandBuffers = &vk_command_buffer_;
|
|
|
|
vkQueueSubmit(vk_queue_, 1, &submit_info, vk_fence_);
|
|
}
|
|
|
|
} // namespace blender::gpu
|