WIP: Vulkan: Workbench #107886

Closed
Jeroen Bakker wants to merge 88 commits from Jeroen-Bakker:vulkan-draw-manager-workbench into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
7 changed files with 77 additions and 10 deletions
Showing only changes of commit 747a714a92 - Show all commits

View File

@ -90,7 +90,7 @@ void VKBuffer::update(const void *data) const
const VKDevice &device = VKBackend::get().device_get();
VmaAllocator allocator = device.mem_allocator_get();
vmaFlushAllocation(allocator, allocation_, 0, VK_WHOLE_SIZE);
vmaFlushAllocation(allocator, allocation_, 0, size_in_bytes());
}
void VKBuffer::clear(VKContext &context, uint32_t clear_value)

View File

@ -99,7 +99,7 @@ void VKDescriptorSetTracker::bind(VKVertexBuffer &vertex_buffer,
{
Binding &binding = ensure_location(location);
binding.type = VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER;
binding.vk_buffer = vertex_buffer.vk_handle();
binding.vk_buffer_view = vertex_buffer.vk_buffer_view_get();
binding.buffer_size = vertex_buffer.size_alloc_get();
}
@ -127,6 +127,7 @@ void VKDescriptorSetTracker::update(VKContext &context)
BLI_assert(vk_descriptor_set != VK_NULL_HANDLE);
Vector<VkDescriptorBufferInfo> buffer_infos;
buffer_infos.reserve(16);
Vector<VkWriteDescriptorSet> descriptor_writes;
for (const Binding &binding : bindings_) {
@ -148,7 +149,27 @@ void VKDescriptorSetTracker::update(VKContext &context)
descriptor_writes.append(write_descriptor);
}
for (const Binding &binding : bindings_) {
if (!binding.is_texel_buffer()) {
continue;
}
VkDescriptorBufferInfo buffer_info = {};
buffer_info.buffer = binding.vk_buffer;
buffer_info.range = binding.buffer_size;
buffer_infos.append(buffer_info);
VkWriteDescriptorSet write_descriptor = {};
write_descriptor.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
write_descriptor.dstSet = vk_descriptor_set;
write_descriptor.dstBinding = binding.location;
write_descriptor.descriptorCount = 1;
write_descriptor.descriptorType = binding.type;
write_descriptor.pTexelBufferView = &binding.vk_buffer_view;
descriptor_writes.append(write_descriptor);
}
Vector<VkDescriptorImageInfo> image_infos;
image_infos.reserve(16);
for (const Binding &binding : bindings_) {
if (!binding.is_image()) {
continue;

View File

@ -120,6 +120,8 @@ class VKDescriptorSetTracker : protected VKResourceTracker<VKDescriptorSet> {
VkBuffer vk_buffer = VK_NULL_HANDLE;
VkDeviceSize buffer_size = 0;
VkBufferView vk_buffer_view = VK_NULL_HANDLE;
VKTexture *texture = nullptr;
VkSampler vk_sampler = VK_NULL_HANDLE;
@ -130,10 +132,12 @@ class VKDescriptorSetTracker : protected VKResourceTracker<VKDescriptorSet> {
bool is_buffer() const
{
return ELEM(type,
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
return ELEM(type, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
}
bool is_texel_buffer() const
{
return ELEM(type, VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
}
bool is_image() const

View File

@ -13,6 +13,11 @@
namespace blender::gpu {
VKUniformBuffer::~VKUniformBuffer()
{
unbind();
}
void VKUniformBuffer::update(const void *data)
{
if (!buffer_.is_allocated()) {
@ -67,8 +72,10 @@ void VKUniformBuffer::bind_as_ssbo(int slot)
void VKUniformBuffer::unbind()
{
VKContext &context = *VKContext::get();
context.state_manager_get().uniform_buffer_unbind(this);
VKContext *context = VKContext::get();
if (context) {
context->state_manager_get().uniform_buffer_unbind(this);
}
}
} // namespace blender::gpu

View File

@ -20,6 +20,7 @@ class VKUniformBuffer : public UniformBuf, NonCopyable {
public:
VKUniformBuffer(int size, const char *name) : UniformBuf(size, name) {}
~VKUniformBuffer();
void update(const void *data) override;
void clear_to_zero() override;

View File

@ -8,6 +8,7 @@
#include "MEM_guardedalloc.h"
#include "vk_data_conversion.hh"
#include "vk_memory.hh"
#include "vk_shader.hh"
#include "vk_shader_interface.hh"
#include "vk_state_manager.hh"
@ -48,6 +49,21 @@ void VKVertexBuffer::bind(uint binding)
{
upload_data();
if (vk_buffer_view_ == VK_NULL_HANDLE) {
VkBufferViewCreateInfo buffer_view_info = {};
eGPUTextureFormat texture_format = to_texture_format(&format);
buffer_view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
buffer_view_info.buffer = buffer_.vk_handle();
buffer_view_info.format = to_vk_format(texture_format);
buffer_view_info.range = buffer_.size_in_bytes();
VK_ALLOCATION_CALLBACKS;
const VKDevice &device = VKBackend::get().device_get();
vkCreateBufferView(
device.device_get(), &buffer_view_info, vk_allocation_callbacks, &vk_buffer_view_);
}
VKContext &context = *VKContext::get();
VKShader *shader = static_cast<VKShader *>(context.shader);
const VKShaderInterface &shader_interface = shader->interface_get();
@ -104,6 +120,13 @@ void VKVertexBuffer::release_data()
context.state_manager_get().texel_buffer_unbind(this);
}
if (vk_buffer_view_ != VK_NULL_HANDLE) {
VK_ALLOCATION_CALLBACKS;
const VKDevice &device = VKBackend::get().device_get();
vkDestroyBufferView(device.device_get(), vk_buffer_view_, vk_allocation_callbacks);
vk_buffer_view_ = VK_NULL_HANDLE;
}
MEM_SAFE_FREE(data);
}
@ -128,7 +151,9 @@ void VKVertexBuffer::upload_data()
if (!buffer_.is_allocated()) {
allocate();
}
if (!ELEM(usage_, GPU_USAGE_STATIC, GPU_USAGE_STREAM, GPU_USAGE_DYNAMIC)) {
return;
}
if (flag & GPU_VERTBUF_DATA_DIRTY) {
void *data_to_upload = data;
if (conversion_needed(format)) {
@ -158,7 +183,8 @@ void VKVertexBuffer::allocate()
usage_,
static_cast<VkBufferUsageFlagBits>(VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT));
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT |
VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT));
debug::object_label(buffer_.vk_handle(), "VertexBuffer");
}

View File

@ -17,6 +17,8 @@ class VKTexture;
class VKVertexBuffer : public VertBuf {
VKBuffer buffer_;
bool should_unbind_ = false;
/** When a vertex buffer is used as a UNIFORM_TEXEL_BUFFER the buffer requires a buffer view. */
VkBufferView vk_buffer_view_ = VK_NULL_HANDLE;
public:
~VKVertexBuffer();
@ -35,6 +37,12 @@ class VKVertexBuffer : public VertBuf {
return buffer_.vk_handle();
}
VkBufferView vk_buffer_view_get() const
{
BLI_assert(vk_buffer_view_ != VK_NULL_HANDLE);
return vk_buffer_view_;
}
protected:
void acquire_data() override;
void resize_data() override;