From 261557e936a5fe2299c47faa750b78250c8b455c Mon Sep 17 00:00:00 2001 From: Jeroen Bakker Date: Mon, 13 Mar 2023 19:16:51 +0100 Subject: [PATCH 1/2] Vulkan: Add initial VkPixelBuffer. VK Pixel buffer is used by external render engines to allocate buffers using the same GPU backend that Blender runs in. In a later stage we should test what exact binding flags are needed. I expect that it should be able to use as a transfer buffer to copy the pixels over to a texture using transfer commands. --- source/blender/gpu/vulkan/vk_buffer.cc | 6 ++++++ source/blender/gpu/vulkan/vk_buffer.hh | 7 +++++++ source/blender/gpu/vulkan/vk_pixel_buffer.cc | 12 ++++++++++-- source/blender/gpu/vulkan/vk_pixel_buffer.hh | 4 ++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/source/blender/gpu/vulkan/vk_buffer.cc b/source/blender/gpu/vulkan/vk_buffer.cc index 134190c11d0..dbb91905d0d 100644 --- a/source/blender/gpu/vulkan/vk_buffer.cc +++ b/source/blender/gpu/vulkan/vk_buffer.cc @@ -90,6 +90,12 @@ void VKBuffer::read(void *data) const memcpy(data, mapped_memory_, size_in_bytes_); } +void *VKBuffer::mapped_memory_get() const +{ + BLI_assert_msg(is_mapped(), "Cannot read a non-mapped buffer."); + return mapped_memory_; +} + bool VKBuffer::is_mapped() const { return mapped_memory_ != nullptr; diff --git a/source/blender/gpu/vulkan/vk_buffer.hh b/source/blender/gpu/vulkan/vk_buffer.hh index 2cbd7f1205b..917103114a5 100644 --- a/source/blender/gpu/vulkan/vk_buffer.hh +++ b/source/blender/gpu/vulkan/vk_buffer.hh @@ -49,6 +49,13 @@ class VKBuffer { return vk_buffer_; } + /** + * Get the reference to the mapped memory. + * + * Can only be called when the buffer is (still) mapped. + */ + void *mapped_memory_get() const; + private: /** Check if this buffer is mapped. */ bool is_mapped() const; diff --git a/source/blender/gpu/vulkan/vk_pixel_buffer.cc b/source/blender/gpu/vulkan/vk_pixel_buffer.cc index 609c6ec68a8..f673d444f27 100644 --- a/source/blender/gpu/vulkan/vk_pixel_buffer.cc +++ b/source/blender/gpu/vulkan/vk_pixel_buffer.cc @@ -11,20 +11,28 @@ namespace blender::gpu { VKPixelBuffer::VKPixelBuffer(int64_t size) : PixelBuffer(size) { + VKContext &context = *VKContext::get(); + buffer_.create(context, + size, + GPU_USAGE_STATIC, + static_cast(VK_BUFFER_USAGE_TRANSFER_SRC_BIT | + VK_BUFFER_USAGE_TRANSFER_DST_BIT)); } void *VKPixelBuffer::map() { - return nullptr; + /* Vulkan buffers are always mapped between allocation and freeing. */ + return buffer_.mapped_memory_get(); } void VKPixelBuffer::unmap() { + /* Vulkan buffers are always mapped between allocation and freeing. */ } int64_t VKPixelBuffer::get_native_handle() { - return -1; + return int64_t(buffer_.vk_handle()); } uint VKPixelBuffer::get_size() diff --git a/source/blender/gpu/vulkan/vk_pixel_buffer.hh b/source/blender/gpu/vulkan/vk_pixel_buffer.hh index 7ea1529fd46..c5430444052 100644 --- a/source/blender/gpu/vulkan/vk_pixel_buffer.hh +++ b/source/blender/gpu/vulkan/vk_pixel_buffer.hh @@ -9,9 +9,13 @@ #include "gpu_texture_private.hh" +#include "vk_buffer.hh" + namespace blender::gpu { class VKPixelBuffer : public PixelBuffer { + VKBuffer buffer_; + public: VKPixelBuffer(int64_t size); void *map() override; -- 2.30.2 From ebae6659ad04e082df0f2ed40f90925b54dc8680 Mon Sep 17 00:00:00 2001 From: Jeroen Bakker Date: Mon, 13 Mar 2023 19:21:34 +0100 Subject: [PATCH 2/2] Improve assert message. --- source/blender/gpu/vulkan/vk_buffer.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/blender/gpu/vulkan/vk_buffer.cc b/source/blender/gpu/vulkan/vk_buffer.cc index dbb91905d0d..a5afcde7162 100644 --- a/source/blender/gpu/vulkan/vk_buffer.cc +++ b/source/blender/gpu/vulkan/vk_buffer.cc @@ -92,7 +92,7 @@ void VKBuffer::read(void *data) const void *VKBuffer::mapped_memory_get() const { - BLI_assert_msg(is_mapped(), "Cannot read a non-mapped buffer."); + BLI_assert_msg(is_mapped(), "Cannot access a non-mapped buffer."); return mapped_memory_; } -- 2.30.2