Synchronize all uses of the command queue

This commit is contained in:
Guillaume Meunier
2025-08-04 15:49:42 +02:00
committed by Patrick Nicolas
parent 55def29a10
commit 2327eacbeb
15 changed files with 89 additions and 50 deletions
+17 -9
View File
@@ -20,12 +20,14 @@
#include "image_loader.h"
#include "wivrn_config.h"
#include <cstdint>
#if WIVRN_USE_LIBKTX
#include <ktxvulkan.h>
#endif
#include <chrono>
#include <cstdint>
#include <memory>
#include <span>
#include <spdlog/fmt/chrono.h>
#include <spdlog/spdlog.h>
#include <stdexcept>
#include <vulkan/vulkan_raii.hpp>
@@ -160,21 +162,20 @@ int bytes_per_pixel(vk::Format format)
}
} // namespace
image_loader::image_loader(vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, vk::raii::Queue & queue, vk::raii::CommandPool & cb_pool) :
image_loader::image_loader(vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, thread_safe<vk::raii::Queue> & queue, vk::raii::CommandPool & cb_pool) :
device(device),
queue(queue),
cb_pool(cb_pool)
{
#if WIVRN_USE_LIBKTX
vdi = ktxVulkanDeviceInfo_Create(*physical_device, *device, *queue, *cb_pool, nullptr);
ktxVulkanDeviceInfo_Construct(&vdi, *physical_device, *device, *queue.get_unsafe(), *cb_pool, nullptr);
#endif
}
image_loader::~image_loader()
{
#if WIVRN_USE_LIBKTX
if (vdi)
ktxVulkanDeviceInfo_Destroy(vdi);
ktxVulkanDeviceInfo_Destruct(&vdi);
#endif
}
@@ -381,7 +382,7 @@ void image_loader::do_load_raw(const void * pixels, vk::Extent3D extent, vk::For
vk::SubmitInfo info;
info.setCommandBuffers(*cb);
auto fence = device.createFence(vk::FenceCreateInfo{});
queue.submit(info, *fence);
queue.lock()->submit(info, *fence);
if (auto result = device.waitForFences(*fence, true, 1'000'000'000); result != vk::Result::eSuccess)
throw std::runtime_error("vkWaitForfences: " + vk::to_string(result));
@@ -414,21 +415,28 @@ void image_loader::do_load_ktx(std::span<const std::byte> bytes)
if (err != KTX_SUCCESS)
{
spdlog::info("ktxTexture_CreateFromMemory: error {}", (int)err);
spdlog::warn("ktxTexture_CreateFromMemory: error {}", (int)err);
throw std::runtime_error("ktxTexture_CreateFromMemory");
}
if (ktxTexture_NeedsTranscoding(texture))
{
// TODO
spdlog::warn("ktxTexture_NeedsTranscoding");
throw std::runtime_error("ktxTexture_NeedsTranscoding");
}
{
auto _ = queue.lock(); // ktxTexture_VkUploadEx uses the queue internally (in ktxTexture_VkUploadEx_WithSuballocator)
// TODO: patch vkloader.c to accept a mutex
err = ktxTexture_VkUploadEx(texture, &vdi, &vk_texture, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
}
err = ktxTexture_VkUploadEx(texture, vdi, &vk_texture, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
if (err != KTX_SUCCESS)
{
// TODO try uncompressing the texture
ktxTexture_Destroy(texture);
spdlog::info("ktxTexture_CreateFromMemory: error {}", (int)err);
spdlog::warn("ktxTexture_CreateFromMemory: error {}", (int)err);
throw std::runtime_error("ktxTexture_VkUploadEx");
}
+8 -6
View File
@@ -19,6 +19,7 @@
#pragma once
#include "utils/thread_safe.h"
#include "vk/allocation.h"
#include "vk/fwd.h"
#include "wivrn_config.h"
@@ -31,7 +32,7 @@
#include <vulkan/vulkan.hpp>
#if WIVRN_USE_LIBKTX
struct ktxVulkanDeviceInfo;
#include <ktxvulkan.h>
#endif
struct image_loader
@@ -45,7 +46,10 @@ struct image_loader
uint32_t num_mipmaps;
image_loader(vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, vk::raii::Queue & queue, vk::raii::CommandPool & cb_pool);
image_loader(vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, thread_safe<vk::raii::Queue> & queue, vk::raii::CommandPool & cb_pool);
image_loader(const image_loader &) = delete;
image_loader & operator=(const image_loader &) = delete;
~image_loader();
// Load a PNG/JPEG/KTX2 file
void load(std::span<const std::byte> bytes, bool srgb);
@@ -71,15 +75,13 @@ struct image_loader
load(pixels.data(), pixels.size() * sizeof(T), extent, format);
}
~image_loader();
private:
#if WIVRN_USE_LIBKTX
ktxVulkanDeviceInfo * vdi = nullptr;
ktxVulkanDeviceInfo vdi;
#endif
vk::raii::Device & device;
vk::raii::Queue & queue;
thread_safe<vk::raii::Queue> & queue;
vk::raii::CommandPool & cb_pool;
buffer_allocation staging_buffer;
+19 -8
View File
@@ -297,7 +297,14 @@ static const std::array layout_bindings = {
.stageFlags = vk::ShaderStageFlagBits::eFragment,
}};
imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, uint32_t queue_family_index, vk::raii::Queue & queue, std::span<controller> controllers_, xr::swapchain & swapchain, std::vector<viewport> layers) :
imgui_context::imgui_context(
vk::raii::PhysicalDevice physical_device,
vk::raii::Device & device,
uint32_t queue_family_index,
thread_safe<vk::raii::Queue> & queue,
std::span<controller> controllers_,
xr::swapchain & swapchain,
std::vector<viewport> layers) :
physical_device(physical_device),
device(device),
queue_family_index(queue_family_index),
@@ -341,7 +348,7 @@ imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii:
.PhysicalDevice = *application::get_physical_device(),
.Device = *application::get_device(),
.QueueFamily = application::queue_family_index(),
.Queue = *application::get_queue(),
.Queue = *queue.get_unsafe(),
.RenderPass = *renderpass,
.MinImageCount = 2,
.ImageCount = (uint32_t)swapchain.images().size(), // used to cycle between VkBuffers in ImGui_ImplVulkan_RenderDrawData
@@ -901,17 +908,21 @@ std::vector<std::pair<int, XrCompositionLayerQuad>> imgui_context::end_frame()
.pClearValues = &clear},
vk::SubpassContents::eInline);
ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), *cb);
{
auto _ = queue.lock(); // ImGui_ImplVulkan_RenderDrawData uses the queue internally (in ImGui_ImplVulkan_UpdateTexture)
// TODO: patch imgui_impl_vulkan to accept a mutex
ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), *cb);
}
cb.endRenderPass();
cb.end();
queue.submit(vk::SubmitInfo{
.commandBufferCount = 1,
.pCommandBuffers = &*cb,
},
*fence);
queue.lock()->submit(vk::SubmitInfo{
.commandBufferCount = 1,
.pCommandBuffers = &*cb,
},
*fence);
swapchain.release();
+3 -2
View File
@@ -19,6 +19,7 @@
#pragma once
#include "render/growable_descriptor_pool.h"
#include "utils/thread_safe.h"
#include "wivrn_config.h"
#include "xr/hand_tracker.h"
#include "xr/space.h"
@@ -119,7 +120,7 @@ private:
vk::raii::PhysicalDevice physical_device;
vk::raii::Device & device;
uint32_t queue_family_index;
vk::raii::Queue & queue;
thread_safe<vk::raii::Queue> & queue;
vk::raii::Pipeline pipeline = nullptr;
vk::raii::DescriptorSetLayout ds_layout;
@@ -178,7 +179,7 @@ public:
vk::raii::PhysicalDevice physical_device,
vk::raii::Device & device,
uint32_t queue_family_index,
vk::raii::Queue & queue,
thread_safe<vk::raii::Queue> & queue,
std::span<controller> controllers,
xr::swapchain & swapchain,
std::vector<viewport> layers);
+3 -3
View File
@@ -260,7 +260,7 @@ fastgltf::MimeType guess_mime_type(std::span<const std::byte> image_data)
std::shared_ptr<vk::raii::ImageView> do_load_image(
vk::raii::PhysicalDevice & physical_device,
vk::raii::Device & device,
vk::raii::Queue & queue,
thread_safe<vk::raii::Queue> & queue,
vk::raii::CommandPool & cb_pool,
std::span<const std::byte> image_data,
bool srgb)
@@ -316,7 +316,7 @@ class loader_context
fastgltf::Asset & gltf;
vk::raii::PhysicalDevice physical_device;
vk::raii::Device & device;
vk::raii::Queue & queue;
thread_safe<vk::raii::Queue> & queue;
vk::raii::CommandPool & cb_pool;
std::vector<asset> loaded_assets;
@@ -330,7 +330,7 @@ public:
fastgltf::Asset & gltf,
vk::raii::PhysicalDevice physical_device,
vk::raii::Device & device,
vk::raii::Queue & queue,
thread_safe<vk::raii::Queue> & queue,
vk::raii::CommandPool & cb_pool) :
base_directory(base_directory),
gltf(gltf),
+3 -2
View File
@@ -22,17 +22,18 @@
#include <vulkan/vulkan_raii.hpp>
#include "scene_components.h"
#include "utils/thread_safe.h"
class scene_loader
{
vk::raii::Device & device;
vk::raii::PhysicalDevice physical_device;
vk::raii::Queue & queue;
thread_safe<vk::raii::Queue> & queue;
uint32_t queue_family_index;
std::shared_ptr<renderer::material> default_material;
public:
scene_loader(vk::raii::Device & device, vk::raii::PhysicalDevice physical_device, vk::raii::Queue & queue, uint32_t queue_family_index, std::shared_ptr<renderer::material> default_material) :
scene_loader(vk::raii::Device & device, vk::raii::PhysicalDevice physical_device, thread_safe<vk::raii::Queue> & queue, uint32_t queue_family_index, std::shared_ptr<renderer::material> default_material) :
device(device),
physical_device(physical_device),
queue(queue),
+6 -6
View File
@@ -202,7 +202,7 @@ static std::array layout_bindings_1{
scene_renderer::scene_renderer(
vk::raii::Device & device,
vk::raii::PhysicalDevice physical_device,
vk::raii::Queue & queue,
thread_safe<vk::raii::Queue> & queue,
vk::raii::CommandPool & cb_pool,
int frames_in_flight) :
physical_device(physical_device),
@@ -706,11 +706,11 @@ void scene_renderer::end_frame()
f.cb.writeTimestamp(vk::PipelineStageFlagBits::eTopOfPipe, *query_pool, current_frame_index * 2 + 1);
f.cb.end();
queue.submit(vk::SubmitInfo{
.commandBufferCount = 1,
.pCommandBuffers = &*f.cb,
},
*f.fence);
queue.lock()->submit(vk::SubmitInfo{
.commandBufferCount = 1,
.pCommandBuffers = &*f.cb,
},
*f.fence);
f.query_pool_filled = true;
}
+3 -2
View File
@@ -34,6 +34,7 @@
#include "render/growable_descriptor_pool.h"
#include "render/scene_components.h"
#include "utils/thread_safe.h"
struct renderpass_info
{
@@ -103,7 +104,7 @@ class scene_renderer
vk::raii::PhysicalDevice physical_device;
vk::raii::Device & device;
vk::PhysicalDeviceProperties physical_device_properties;
vk::raii::Queue & queue;
thread_safe<vk::raii::Queue> & queue;
// Destination images
struct output_image
@@ -217,7 +218,7 @@ public:
scene_renderer(
vk::raii::Device & device,
vk::raii::PhysicalDevice physical_device,
vk::raii::Queue & queue,
thread_safe<vk::raii::Queue> & queue,
vk::raii::CommandPool & cb_pool,
int frames_in_flight = 2);