Skip to content

Commit

Permalink
+ added support for submitting some scratch commands instantaneously,…
Browse files Browse the repository at this point in the history
… although is not yet used
  • Loading branch information
harrand committed Mar 4, 2023
1 parent 5466333 commit c06aca8
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 23 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ add_library(topaz STATIC
src/tz/gl/impl/vulkan/renderer.inl
src/tz/gl/impl/vulkan/renderer2.cpp
src/tz/gl/impl/vulkan/renderer2.hpp
src/tz/gl/impl/vulkan/renderer2.inl
# tz::gl (OpenGL Frontend)
src/tz/gl/impl/opengl/component.cpp
src/tz/gl/impl/opengl/component.hpp
Expand Down
42 changes: 41 additions & 1 deletion src/tz/gl/impl/vulkan/device2.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#if TZ_VULKAN
#include "tz/gl/impl/vulkan/device2.hpp"
#include "tz/gl/impl/vulkan/detail/semaphore.hpp"
#include "tz/gl/impl/vulkan/detail/fence.hpp"
#include "tz/gl/impl/vulkan/detail/tz_vulkan.hpp"
#include "tz/core/profile.hpp"
#include <algorithm>
Expand Down Expand Up @@ -199,7 +200,7 @@ namespace tz::gl

//--------------------------------------------------------------------------------------------------

device_command_pool::device_command_pool(const vk2::LogicalDevice& device):
device_command_pool::device_command_pool(vk2::LogicalDevice& device):
ldev(&device)
{
this->graphics_queue = this->ldev->get_hardware_queue
Expand Down Expand Up @@ -253,6 +254,28 @@ namespace tz::gl
this->fingerprint_alloc_types[fingerprint] = finfo;
}

void device_command_pool::vk_submit_and_run_commands_blocking(unsigned int fingerprint, std::size_t allocation_id, std::size_t buffer_id, const vk2::CommandBuffer& buffer)
{
vk2::Fence temp_fence
{{
.device = this->ldev
}};

#if TZ_DEBUG
std::size_t debug_real_alloc_length = this->fingerprint_allocation_history[fingerprint].size();
tz::assert(debug_real_alloc_length > buffer_id, "attempted to submit & run scratch command buffer id %zu at (fingerprint:allocid) %u:%zu. there are only %zu buffers in this allocation. please submit a bug report.", buffer_id, fingerprint, allocation_id, debug_real_alloc_length);
#endif // TZ_DEBUG

this->get_original_queue(this->fingerprint_alloc_types[fingerprint])->submit
(vk2::hardware::Queue::SubmitInfo{
.command_buffers = {&buffer},
.waits = {},
.signals = {},
.execution_complete_fence = &temp_fence
});
temp_fence.wait_until_signalled();
}

vk2::CommandPool::AllocationResult device_command_pool::impl_allocate_commands(const vk2::CommandPool::Allocation& alloc, unsigned int fingerprint, unsigned int attempt)
{
// allocate using the most recently created descriptor pool
Expand Down Expand Up @@ -300,6 +323,18 @@ namespace tz::gl
return this->graphics_commands;
}

vk2::hardware::Queue* device_command_pool::get_original_queue(const fingerprint_info_t& finfo)
{
if(finfo.compute)
{
return this->compute_queue;
}
if(finfo.requires_present)
{
return this->graphics_present_queue;
}
return this->graphics_queue;
}

//--------------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -428,6 +463,11 @@ namespace tz::gl
return this->ldev;
}

vk2::LogicalDevice& device_vulkan_base::vk_get_logical_device()
{
return this->ldev;
}

//--------------------------------------------------------------------------------------------------

device_vulkan2::device_vulkan2():
Expand Down
13 changes: 8 additions & 5 deletions src/tz/gl/impl/vulkan/device2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ namespace tz::gl
bool compute = false;
bool requires_present = true;
};
device_command_pool(const vk2::LogicalDevice& device);
device_command_pool(vk2::LogicalDevice& device);
vk2::CommandPool::AllocationResult vk_allocate_commands(const vk2::CommandPool::Allocation& alloc, unsigned int fingerprint);
void vk_free_commands(unsigned int fingerprint, std::size_t allocation_id, std::span<vk2::CommandBuffer> command_buffers);
void vk_command_pool_touch(unsigned int fingerprint, fingerprint_info_t finfo);
void vk_submit_and_run_commands_blocking(unsigned int fingerprint, std::size_t allocation_id, std::size_t buffer_id, const vk2::CommandBuffer& buffer);
private:
struct allocation_history
{
Expand All @@ -90,10 +91,11 @@ namespace tz::gl
};
vk2::CommandPool::AllocationResult impl_allocate_commands(const vk2::CommandPool::Allocation& alloc, unsigned int fingerprint, unsigned int attempt);
vk2::CommandPool& get_fitting_pool(const fingerprint_info_t& finfo);
const vk2::LogicalDevice* ldev;
const vk2::hardware::Queue* graphics_queue = nullptr;
const vk2::hardware::Queue* graphics_present_queue = nullptr;
const vk2::hardware::Queue* compute_queue = nullptr;
vk2::hardware::Queue* get_original_queue(const fingerprint_info_t& finfo);
vk2::LogicalDevice* ldev;
vk2::hardware::Queue* graphics_queue = nullptr;
vk2::hardware::Queue* graphics_present_queue = nullptr;
vk2::hardware::Queue* compute_queue = nullptr;
vk2::CommandPool graphics_commands = vk2::CommandPool::null();
vk2::CommandPool graphics_present_commands = vk2::CommandPool::null();
vk2::CommandPool compute_commands = vk2::CommandPool::null();
Expand All @@ -106,6 +108,7 @@ namespace tz::gl
public:
device_vulkan_base();
const vk2::LogicalDevice& vk_get_logical_device() const;
vk2::LogicalDevice& vk_get_logical_device();
protected:
vk2::LogicalDevice ldev;
};
Expand Down
13 changes: 13 additions & 0 deletions src/tz/gl/impl/vulkan/renderer2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,19 @@ namespace tz::gl
this->free_commands(command_type::both);
}

void renderer_command_processor::do_scratch_work(std::function<void(vk2::CommandBufferRecording&)> record_commands)
{
vk2::CommandBuffer& buf = this->scratch_command_buffer();
// firstly record the commands requested.
{
vk2::CommandBufferRecording rec = buf.record();
record_commands(rec);
}
// then, execute them.
constexpr std::size_t scratch_id = 1;
tz::gl::get_device2().vk_submit_and_run_commands_blocking(renderer_vulkan_base::uid, scratch_id, 0, buf);
}

constexpr std::size_t cmdbuf_work_alloc_id = 0;
constexpr std::size_t cmdbuf_scratch_alloc_id = 1;

Expand Down
5 changes: 2 additions & 3 deletions src/tz/gl/impl/vulkan/renderer2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#define TZ_GL_IMPL_VULKAN_RENDERER2_HPP
#include "tz/gl/api/renderer.hpp"
#include "tz/core/memory/maybe_owned_ptr.hpp"
#include "tz/core/types.hpp"
#include "tz/gl/impl/vulkan/component.hpp"
#include "tz/gl/impl/common/renderer.hpp"
#include <functional>

#include "tz/gl/impl/vulkan/detail/descriptors.hpp"
#include "tz/gl/impl/vulkan/detail/command.hpp"
Expand Down Expand Up @@ -78,7 +78,7 @@ namespace tz::gl
renderer_command_processor(const tz::gl::renderer_info& info);
~renderer_command_processor();
renderer_command_processor() = default;
void do_scratch_work(tz::action<vk2::CommandBufferRecording&> auto record_commands);
void do_scratch_work(std::function<void(vk2::CommandBufferRecording&)> record_commands);

enum class command_type
{
Expand Down Expand Up @@ -127,5 +127,4 @@ namespace tz::gl
static_assert(tz::gl::renderer_type<renderer_vulkan2>);
}

#include "tz/gl/impl/vulkan/renderer2.inl"
#endif // TZ_GL_IMPL_VULKAN_RENDERER2_HPP
13 changes: 0 additions & 13 deletions src/tz/gl/impl/vulkan/renderer2.inl

This file was deleted.

0 comments on commit c06aca8

Please sign in to comment.