Put the GUI and the controllers in other composition layers
This commit is contained in:
+77
-217
@@ -27,8 +27,8 @@
|
||||
#include "vulkan/vulkan_handles.hpp"
|
||||
#include "vulkan/vulkan_to_string.hpp"
|
||||
#include <spdlog/spdlog.h>
|
||||
#include "vk/allocation.h"
|
||||
#include <imgui.h>
|
||||
#include <imgui_internal.h>
|
||||
#include <backends/imgui_impl_vulkan.h>
|
||||
#include <glm/gtc/matrix_access.hpp>
|
||||
|
||||
@@ -54,7 +54,7 @@ static vk::raii::RenderPass create_renderpass(vk::raii::Device& device, vk::Form
|
||||
.stencilLoadOp = vk::AttachmentLoadOp::eDontCare,
|
||||
.stencilStoreOp = vk::AttachmentStoreOp::eDontCare,
|
||||
.initialLayout = vk::ImageLayout::eUndefined,
|
||||
.finalLayout = vk::ImageLayout::eTransferSrcOptimal,
|
||||
.finalLayout = vk::ImageLayout::eColorAttachmentOptimal,
|
||||
};
|
||||
|
||||
vk::AttachmentReference color_attachment{
|
||||
@@ -96,17 +96,17 @@ static void check_vk_result(VkResult result)
|
||||
}
|
||||
}
|
||||
|
||||
static std::optional<ImVec2> ray_plane_intersection(const imgui_context::imgui_viewport& vp, const imgui_context::controller_state& in)
|
||||
std::optional<ImVec2> imgui_context::ray_plane_intersection(const imgui_context::controller_state& in)
|
||||
{
|
||||
if (!in.active)
|
||||
return {};
|
||||
|
||||
auto M = glm::transpose(glm::mat3_cast(vp.orientation)); // world-to-plane transform
|
||||
auto M = glm::transpose(glm::mat3_cast(orientation_)); // world-to-plane transform
|
||||
|
||||
glm::vec3 controller_direction = glm::column(glm::mat3_cast(in.aim_orientation), 2);
|
||||
|
||||
// Compute all vectors in the reference frame of the GUI plane
|
||||
glm::vec3 ray_start = M * (in.aim_position - vp.position);
|
||||
glm::vec3 ray_start = M * (in.aim_position - position_);
|
||||
glm::vec3 ray_dir = M * controller_direction;
|
||||
|
||||
if (ray_dir.z > 0.0001f)
|
||||
@@ -123,93 +123,72 @@ static std::optional<ImVec2> ray_plane_intersection(const imgui_context::imgui_v
|
||||
coord.y = ray_start.y + lambda * ray_dir.y;
|
||||
|
||||
// Convert from mesh coordinates to imgui coordinates
|
||||
coord = coord / vp.scale;
|
||||
coord = coord / scale;
|
||||
|
||||
if (fabs(coord.x) <= 0.5 && fabs(coord.y) <= 0.5)
|
||||
return ImVec2(
|
||||
(0.5 + coord.x) * vp.size.width,
|
||||
(0.5 - coord.y) * vp.size.height);
|
||||
(0.5 + coord.x) * size.width,
|
||||
(0.5 - coord.y) * size.height);
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
imgui_context::imgui_viewport::imgui_viewport(vk::raii::Device& device, vk::raii::CommandPool& command_pool, vk::RenderPass renderpass, vk::Extent2D size, vk::Format format) :
|
||||
device(device),
|
||||
size(size)
|
||||
imgui_context::imgui_frame& imgui_context::get_frame(vk::Image destination)
|
||||
{
|
||||
num_mipmaps = std::floor(std::log2(std::max(size.width, size.height))) + 1;
|
||||
|
||||
for(auto& frame:frames)
|
||||
for(auto& i: frames)
|
||||
{
|
||||
frame.image = image_allocation(
|
||||
vk::ImageCreateInfo{
|
||||
.imageType = vk::ImageType::e2D,
|
||||
.format = format,
|
||||
.extent = {
|
||||
size.width,
|
||||
size.height,
|
||||
1
|
||||
},
|
||||
.mipLevels = num_mipmaps,
|
||||
.arrayLayers = 1,
|
||||
.samples = vk::SampleCountFlagBits::e1,
|
||||
.tiling = vk::ImageTiling::eOptimal,
|
||||
.usage = vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eTransferSrc | vk::ImageUsageFlagBits::eTransferDst,
|
||||
.initialLayout = vk::ImageLayout::eUndefined,
|
||||
},
|
||||
VmaAllocationCreateInfo{
|
||||
.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE
|
||||
}
|
||||
);
|
||||
|
||||
// Only 1 mipmap level for the framebuffer view
|
||||
frame.image_view_framebuffer = vk::raii::ImageView(device, vk::ImageViewCreateInfo{
|
||||
.image = (vk::Image)frame.image,
|
||||
.viewType = vk::ImageViewType::e2D,
|
||||
.format = format,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
}
|
||||
});
|
||||
|
||||
frame.image_view_texture = vk::raii::ImageView(device, vk::ImageViewCreateInfo{
|
||||
.image = (vk::Image)frame.image,
|
||||
.viewType = vk::ImageViewType::e2D,
|
||||
.format = format,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = num_mipmaps,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
}
|
||||
});
|
||||
|
||||
frame.framebuffer = vk::raii::Framebuffer(device, vk::FramebufferCreateInfo{
|
||||
.renderPass = renderpass,
|
||||
.attachmentCount = 1,
|
||||
.pAttachments = &*frame.image_view_framebuffer,
|
||||
.width = size.width,
|
||||
.height = size.height,
|
||||
.layers = 1,
|
||||
});
|
||||
|
||||
frame.command_buffer = std::move(device.allocateCommandBuffers({
|
||||
.commandPool = *command_pool,
|
||||
.commandBufferCount = 1,
|
||||
})[0]);
|
||||
|
||||
frame.fence = device.createFence(vk::FenceCreateInfo{.flags = vk::FenceCreateFlagBits::eSignaled});
|
||||
if (i.destination == destination)
|
||||
return i;
|
||||
}
|
||||
|
||||
auto& frame = frames.emplace_back();
|
||||
|
||||
frame.destination = destination;
|
||||
|
||||
// Only 1 mipmap level for the framebuffer view
|
||||
frame.image_view_framebuffer = vk::raii::ImageView(device, vk::ImageViewCreateInfo{
|
||||
.image = destination,
|
||||
.viewType = vk::ImageViewType::e2D,
|
||||
.format = format,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
}
|
||||
});
|
||||
|
||||
frame.framebuffer = vk::raii::Framebuffer(device, vk::FramebufferCreateInfo{
|
||||
.renderPass = *renderpass,
|
||||
.attachmentCount = 1,
|
||||
.pAttachments = &*frame.image_view_framebuffer,
|
||||
.width = size.width,
|
||||
.height = size.height,
|
||||
.layers = 1,
|
||||
});
|
||||
|
||||
frame.command_buffer = std::move(device.allocateCommandBuffers({
|
||||
.commandPool = *command_pool,
|
||||
.commandBufferCount = 1,
|
||||
})[0]);
|
||||
|
||||
frame.fence = device.createFence(vk::FenceCreateInfo{.flags = vk::FenceCreateFlagBits::eSignaled});
|
||||
|
||||
return frame;
|
||||
}
|
||||
|
||||
imgui_context::imgui_context(vk::raii::Device& device, uint32_t queue_family_index, vk::raii::Queue& queue, XrSpace world, std::span<controller> controllers_, float resolution, glm::vec2 scale) :
|
||||
static const std::array pool_sizes =
|
||||
{
|
||||
vk::DescriptorPoolSize{
|
||||
.type = vk::DescriptorType::eCombinedImageSampler,
|
||||
.descriptorCount = 1,
|
||||
}
|
||||
};
|
||||
|
||||
imgui_context::imgui_context(vk::raii::Device& device, uint32_t queue_family_index, vk::raii::Queue& queue, XrSpace world, std::span<controller> controllers_, vk::Extent2D size, float resolution, vk::Format format) :
|
||||
device(device),
|
||||
queue_family_index(queue_family_index),
|
||||
queue(queue),
|
||||
@@ -220,11 +199,14 @@ imgui_context::imgui_context(vk::raii::Device& device, uint32_t queue_family_ind
|
||||
.poolSizeCount = pool_sizes.size(),
|
||||
.pPoolSizes = pool_sizes.data(),
|
||||
}),
|
||||
renderpass(create_renderpass(device, vk::Format::eR8G8B8A8Unorm, true)),
|
||||
renderpass(create_renderpass(device, format, true)),
|
||||
command_pool(device, vk::CommandPoolCreateInfo{
|
||||
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer | vk::CommandPoolCreateFlagBits::eTransient,
|
||||
.queueFamilyIndex = queue_family_index,
|
||||
}),
|
||||
size(size),
|
||||
format(format),
|
||||
scale(size.width / resolution, size.height / resolution),
|
||||
context(ImGui::CreateContext()),
|
||||
io(ImGui::GetIO()),
|
||||
world(world)
|
||||
@@ -233,14 +215,6 @@ imgui_context::imgui_context(vk::raii::Device& device, uint32_t queue_family_ind
|
||||
for(const auto& i:controllers_)
|
||||
controllers.emplace_back(i, controller_state{});
|
||||
|
||||
vk::Extent2D extent{
|
||||
(uint32_t)(resolution * scale.x),
|
||||
(uint32_t)(resolution * scale.y)
|
||||
};
|
||||
|
||||
viewport = std::make_shared<imgui_viewport>(device, command_pool, *renderpass, extent, vk::Format::eR8G8B8A8Unorm);
|
||||
viewport->scale = scale;
|
||||
|
||||
io.IniFilename = nullptr;
|
||||
|
||||
ImGui_ImplVulkan_InitInfo init_info = {
|
||||
@@ -252,8 +226,9 @@ imgui_context::imgui_context(vk::raii::Device& device, uint32_t queue_family_ind
|
||||
.PipelineCache = *application::get_pipeline_cache(),
|
||||
.DescriptorPool = *descriptor_pool,
|
||||
.Subpass = 0,
|
||||
.MinImageCount = imgui_viewport::frames_in_flight,
|
||||
.ImageCount = imgui_viewport::frames_in_flight,
|
||||
// image count is not actually used because we don't use a WSI swapchain
|
||||
.MinImageCount = 2, // imgui_viewport::frames_in_flight,
|
||||
.ImageCount = 2, //imgui_viewport::frames_in_flight,
|
||||
.MSAASamples = VK_SAMPLE_COUNT_1_BIT,
|
||||
.Allocator = nullptr,
|
||||
.CheckVkResultFn = check_vk_result,
|
||||
@@ -272,7 +247,7 @@ imgui_context::imgui_context(vk::raii::Device& device, uint32_t queue_family_ind
|
||||
|
||||
// Setup Dear ImGui style
|
||||
ImGui::StyleColorsDark();
|
||||
//ImGui::StyleColorsClassic();
|
||||
// ImGui::StyleColorsClassic();
|
||||
//ImGui::StyleColorsLight();
|
||||
|
||||
ImGuiStyle& style = ImGui::GetStyle();
|
||||
@@ -289,15 +264,8 @@ imgui_context::imgui_context(vk::raii::Device& device, uint32_t queue_family_ind
|
||||
|
||||
style.Colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0,0,0,0.8);
|
||||
|
||||
|
||||
// TODO: scroll to drag https://github.com/ocornut/imgui/issues/3379
|
||||
}
|
||||
void imgui_context::set_position(glm::vec3 position, glm::quat orientation)
|
||||
{
|
||||
auto& vp = *viewport;
|
||||
vp.position = position;
|
||||
vp.orientation = orientation;
|
||||
}
|
||||
|
||||
void imgui_context::new_frame(XrTime display_time)
|
||||
{
|
||||
@@ -382,7 +350,7 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
|
||||
if (new_focused_controller != (size_t)-1)
|
||||
{
|
||||
position = ray_plane_intersection(*viewport, new_states[new_focused_controller]);
|
||||
position = ray_plane_intersection(new_states[new_focused_controller]);
|
||||
trigger = new_states[new_focused_controller].trigger_value;
|
||||
auto scroll = new_states[new_focused_controller].scroll_value;
|
||||
|
||||
@@ -423,9 +391,10 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
}
|
||||
|
||||
// Start the Dear ImGui frame
|
||||
ImGui::SetCurrentContext(context);
|
||||
ImGui_ImplVulkan_NewFrame();
|
||||
|
||||
io.DisplaySize = ImVec2(viewport->size.width, viewport->size.height);
|
||||
io.DisplaySize = ImVec2(size.width, size.height);
|
||||
io.DisplayFramebufferScale = ImVec2(1, 1);
|
||||
|
||||
// See ImGui_ImplSDL2_ProcessEvent
|
||||
@@ -444,18 +413,16 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<vk::raii::ImageView> imgui_context::render()
|
||||
void imgui_context::render(vk::Image destination)
|
||||
{
|
||||
ImGui::Render();
|
||||
ImDrawData* draw_data = ImGui::GetDrawData();
|
||||
const bool is_minimized = (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f);
|
||||
|
||||
if (is_minimized)
|
||||
return {};
|
||||
return;
|
||||
|
||||
auto &vp = *viewport;
|
||||
vp.frameindex = (vp.frameindex + 1) % imgui_viewport::frames_in_flight;
|
||||
auto& f = vp.frames[vp.frameindex];
|
||||
auto& f = get_frame(destination);
|
||||
auto& cb = f.command_buffer;
|
||||
|
||||
device.waitForFences(*f.fence, true, 1'000'000'000); // TODO check timeout
|
||||
@@ -469,7 +436,7 @@ std::shared_ptr<vk::raii::ImageView> imgui_context::render()
|
||||
.renderPass = *renderpass,
|
||||
.framebuffer = *f.framebuffer,
|
||||
.renderArea = {
|
||||
.extent = vp.size,
|
||||
.extent = size,
|
||||
},
|
||||
.clearValueCount = 1,
|
||||
.pClearValues = &clear
|
||||
@@ -479,113 +446,6 @@ std::shared_ptr<vk::raii::ImageView> imgui_context::render()
|
||||
|
||||
cb.endRenderPass();
|
||||
|
||||
// TODO: create mipmaps
|
||||
// Create mipmaps
|
||||
int width = vp.size.width;
|
||||
int height = vp.size.height;
|
||||
vk::ImageLayout prev_layout = vk::ImageLayout::eTransferSrcOptimal;
|
||||
for(uint32_t level = 1; level < vp.num_mipmaps; level++)
|
||||
{
|
||||
int next_width = width > 1 ? width / 2 : 1;
|
||||
int next_height = height > 1 ? height / 2 : 1;
|
||||
|
||||
// Transition source image layout to eTransferSrcOptimal and destination image layout to eTransferDstOptimal
|
||||
cb.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eFragmentShader, vk::DependencyFlags{}, {}, {},
|
||||
{
|
||||
vk::ImageMemoryBarrier{
|
||||
.srcAccessMask = vk::AccessFlagBits::eTransferWrite,
|
||||
.dstAccessMask = vk::AccessFlagBits::eShaderRead,
|
||||
.oldLayout = prev_layout,
|
||||
.newLayout = vk::ImageLayout::eTransferSrcOptimal,
|
||||
.image = f.image,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = level - 1,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
},
|
||||
vk::ImageMemoryBarrier{
|
||||
.srcAccessMask = vk::AccessFlagBits::eTransferWrite, // TODO
|
||||
.dstAccessMask = vk::AccessFlagBits::eShaderRead, // TODO
|
||||
.oldLayout = vk::ImageLayout::eUndefined,
|
||||
.newLayout = vk::ImageLayout::eTransferDstOptimal,
|
||||
.image = f.image,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = level,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
},
|
||||
|
||||
});
|
||||
prev_layout = vk::ImageLayout::eTransferDstOptimal;
|
||||
|
||||
// Blit level n-1 to level n
|
||||
cb.blitImage(f.image, vk::ImageLayout::eTransferSrcOptimal, f.image, vk::ImageLayout::eTransferDstOptimal,
|
||||
vk::ImageBlit{
|
||||
.srcSubresource = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.mipLevel = level - 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1},
|
||||
.srcOffsets = std::array{
|
||||
vk::Offset3D{0, 0, 0},
|
||||
vk::Offset3D{width, height, 1},
|
||||
},
|
||||
.dstSubresource = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.mipLevel = level,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1
|
||||
},
|
||||
.dstOffsets = std::array{
|
||||
vk::Offset3D{0, 0, 0},
|
||||
vk::Offset3D{next_width, next_height, 1},
|
||||
},
|
||||
},
|
||||
vk::Filter::eLinear);
|
||||
|
||||
|
||||
// Transition source image layout to eShaderReadOnlyOptimal
|
||||
cb.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eFragmentShader, vk::DependencyFlags{}, {}, {}, vk::ImageMemoryBarrier{
|
||||
.srcAccessMask = vk::AccessFlagBits::eTransferRead,
|
||||
.dstAccessMask = vk::AccessFlagBits::eShaderRead,
|
||||
.oldLayout = vk::ImageLayout::eTransferSrcOptimal,
|
||||
.newLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
|
||||
.image = f.image,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = level - 1,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
});
|
||||
|
||||
width = next_width;
|
||||
height = next_height;
|
||||
}
|
||||
|
||||
// Transition the last level to eShaderReadOnlyOptimal
|
||||
cb.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eFragmentShader, vk::DependencyFlags{}, {}, {}, vk::ImageMemoryBarrier{
|
||||
.srcAccessMask = vk::AccessFlagBits::eTransferWrite,
|
||||
.dstAccessMask = vk::AccessFlagBits::eShaderRead,
|
||||
.oldLayout = prev_layout,
|
||||
.newLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
|
||||
.image = f.image,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = vp.num_mipmaps - 1,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
});
|
||||
|
||||
cb.end();
|
||||
|
||||
queue.submit(vk::SubmitInfo{
|
||||
@@ -594,19 +454,19 @@ std::shared_ptr<vk::raii::ImageView> imgui_context::render()
|
||||
}, *f.fence);
|
||||
|
||||
// Use the aliasing constructor
|
||||
return std::shared_ptr<vk::raii::ImageView>{viewport, &vp.frames[vp.frameindex].image_view_texture};
|
||||
// return std::shared_ptr<vk::raii::ImageView>{viewport, &vp.frames[vp.frameindex].image_view_texture};
|
||||
}
|
||||
|
||||
imgui_context::~imgui_context()
|
||||
{
|
||||
// Release the command buffers, they will be destroyed with the command pool
|
||||
for(auto& f: viewport->frames)
|
||||
for(auto& f: frames)
|
||||
f.command_buffer.release();
|
||||
|
||||
// Wait for fences here and not in imgui_viewport::~imgui_viewport so that the command buffers have finished when ImGui_ImplVulkan_Shutdown is called
|
||||
std::array<vk::Fence, imgui_viewport::frames_in_flight> fences;
|
||||
for(auto&& [i, f]: utils::enumerate(viewport->frames))
|
||||
fences[i] = *f.fence;
|
||||
std::vector<vk::Fence> fences;
|
||||
for(auto& f: frames)
|
||||
fences.push_back(*f.fence);
|
||||
|
||||
device.waitForFences(fences, true, 1'000'000'000);
|
||||
|
||||
|
||||
+62
-41
@@ -18,48 +18,27 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "render/scene_data.h"
|
||||
#include <imgui.h>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
#include <openxr/openxr.h>
|
||||
#include "vk/allocation.h"
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <vector>
|
||||
|
||||
class imgui_context
|
||||
{
|
||||
public:
|
||||
struct imgui_frame
|
||||
{
|
||||
image_allocation image;
|
||||
vk::Image destination;
|
||||
vk::raii::ImageView image_view_framebuffer = nullptr;
|
||||
vk::raii::ImageView image_view_texture = nullptr;
|
||||
vk::raii::Framebuffer framebuffer = nullptr;
|
||||
vk::raii::CommandBuffer command_buffer = nullptr;
|
||||
vk::raii::Fence fence = nullptr;
|
||||
};
|
||||
|
||||
struct imgui_viewport
|
||||
{
|
||||
static constexpr int frames_in_flight = 2;
|
||||
uint32_t num_mipmaps;
|
||||
|
||||
std::array<imgui_frame, frames_in_flight> frames;
|
||||
int frameindex = 0;
|
||||
|
||||
vk::raii::Device& device;
|
||||
|
||||
vk::Extent2D size;
|
||||
vk::ClearValue clear_value = {vk::ClearColorValue{0,0,0,0}};
|
||||
|
||||
// TODO: sync with 3d scene
|
||||
glm::vec3 position = {0, 1, -1.5};
|
||||
glm::quat orientation = {1, 0, 0, 0};
|
||||
glm::vec2 scale = {1, 1};
|
||||
|
||||
imgui_viewport(vk::raii::Device& device, vk::raii::CommandPool& command_pool, vk::RenderPass renderpass, vk::Extent2D size, vk::Format format);
|
||||
};
|
||||
|
||||
struct controller
|
||||
{
|
||||
XrSpace aim;
|
||||
@@ -86,14 +65,6 @@ public:
|
||||
};
|
||||
|
||||
private:
|
||||
static inline const std::array pool_sizes =
|
||||
{
|
||||
vk::DescriptorPoolSize{
|
||||
.type = vk::DescriptorType::eCombinedImageSampler,
|
||||
.descriptorCount = 1,
|
||||
}
|
||||
};
|
||||
|
||||
vk::raii::Device& device;
|
||||
uint32_t queue_family_index;
|
||||
vk::raii::Queue& queue;
|
||||
@@ -103,14 +74,20 @@ private:
|
||||
vk::raii::RenderPass renderpass;
|
||||
vk::raii::CommandPool command_pool;
|
||||
|
||||
// shared_ptr because it needs to be kept alive until the output textures are not used anymore
|
||||
std::shared_ptr<imgui_viewport> viewport;
|
||||
std::vector<imgui_frame> frames;
|
||||
imgui_frame& get_frame(vk::Image destination);
|
||||
|
||||
vk::Extent2D size;
|
||||
vk::Format format;
|
||||
vk::ClearValue clear_value = {vk::ClearColorValue{0,0,0,0}};
|
||||
|
||||
glm::vec3 position_ = {0, 1, -1.5};
|
||||
glm::quat orientation_ = {1, 0, 0, 0};
|
||||
glm::vec2 scale = {1, 1};
|
||||
|
||||
ImGuiContext * context;
|
||||
ImGuiIO& io;
|
||||
|
||||
// std::string ini_filename;
|
||||
|
||||
std::vector<std::pair<controller, controller_state>> controllers;
|
||||
XrSpace world;
|
||||
size_t focused_controller = (size_t)-1;
|
||||
@@ -118,14 +95,58 @@ private:
|
||||
|
||||
bool button_pressed = false;
|
||||
|
||||
std::optional<ImVec2> ray_plane_intersection(const imgui_context::controller_state& in);
|
||||
|
||||
public:
|
||||
imgui_context(vk::raii::Device& device, uint32_t queue_family_index,
|
||||
vk::raii::Queue& queue, XrSpace world, std::span<controller> controllers, float resolution, glm::vec2 scale);
|
||||
imgui_context(vk::raii::Device& device, uint32_t queue_family_index, vk::raii::Queue& queue, XrSpace world, std::span<controller> controllers, vk::Extent2D size, float resolution, vk::Format format);
|
||||
~imgui_context();
|
||||
|
||||
void set_position(glm::vec3 position, glm::quat orientation);
|
||||
void set_position(glm::vec3 position, glm::quat orientation)
|
||||
{
|
||||
position_ = position;
|
||||
orientation_ = orientation;
|
||||
}
|
||||
|
||||
XrPosef pose()
|
||||
{
|
||||
return XrPosef{
|
||||
.orientation = {
|
||||
.x = orientation_.x,
|
||||
.y = orientation_.y,
|
||||
.z = orientation_.z,
|
||||
.w = orientation_.w,
|
||||
},
|
||||
.position = {
|
||||
.x = position_.x,
|
||||
.y = position_.y,
|
||||
.z = position_.z,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
glm::vec3& position()
|
||||
{
|
||||
return position_;
|
||||
}
|
||||
|
||||
glm::quat& orientation()
|
||||
{
|
||||
return orientation_;
|
||||
}
|
||||
|
||||
glm::vec3 position() const
|
||||
{
|
||||
return position_;
|
||||
}
|
||||
|
||||
glm::quat orientation() const
|
||||
{
|
||||
return orientation_;
|
||||
}
|
||||
|
||||
void new_frame(XrTime display_time);
|
||||
std::shared_ptr<vk::raii::ImageView> render();
|
||||
|
||||
void render(vk::Image destination);
|
||||
|
||||
ImFont * large_font;
|
||||
size_t get_focused_controller() const
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include "vk/pipeline.h"
|
||||
#include "vk/shader.h"
|
||||
#include <boost/pfr/core.hpp>
|
||||
#include <chrono>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
#include <glm/gtc/matrix_access.hpp>
|
||||
@@ -191,6 +192,11 @@ scene_renderer::scene_renderer(vk::raii::Device & device, vk::raii::PhysicalDevi
|
||||
{
|
||||
res.fence = device.createFence({.flags = vk::FenceCreateFlagBits::eSignaled});
|
||||
res.cb = std::move(cb);
|
||||
|
||||
res.query_pool = vk::raii::QueryPool(device, vk::QueryPoolCreateInfo{
|
||||
.queryType = vk::QueryType::eTimestamp,
|
||||
.queryCount = 2,
|
||||
});
|
||||
}
|
||||
|
||||
renderpass = create_renderpass();
|
||||
@@ -245,6 +251,13 @@ void scene_renderer::wait_idle()
|
||||
device.waitForFences(fences, true, 1'000'000'000); // TODO check for timeout
|
||||
}
|
||||
|
||||
// #define MSAA_4x
|
||||
|
||||
#ifdef MSAA_4x
|
||||
#define MSAA_SAMPLES vk::SampleCountFlagBits::e4
|
||||
#else
|
||||
#define MSAA_SAMPLES vk::SampleCountFlagBits::e1
|
||||
#endif
|
||||
|
||||
vk::raii::RenderPass scene_renderer::create_renderpass()
|
||||
{
|
||||
@@ -253,7 +266,7 @@ vk::raii::RenderPass scene_renderer::create_renderpass()
|
||||
std::array attachments = {
|
||||
vk::AttachmentDescription{
|
||||
.format = output_format,
|
||||
.samples = vk::SampleCountFlagBits::e4,
|
||||
.samples = MSAA_SAMPLES,
|
||||
.loadOp = vk::AttachmentLoadOp::eClear,
|
||||
.storeOp = vk::AttachmentStoreOp::eStore,
|
||||
.initialLayout = vk::ImageLayout::eUndefined,
|
||||
@@ -261,7 +274,7 @@ vk::raii::RenderPass scene_renderer::create_renderpass()
|
||||
},
|
||||
vk::AttachmentDescription{
|
||||
.format = depth_format,
|
||||
.samples = vk::SampleCountFlagBits::e4,
|
||||
.samples = MSAA_SAMPLES,
|
||||
.loadOp = vk::AttachmentLoadOp::eClear,
|
||||
.storeOp = vk::AttachmentStoreOp::eDontCare,
|
||||
.stencilLoadOp = vk::AttachmentLoadOp::eClear,
|
||||
@@ -270,6 +283,7 @@ vk::raii::RenderPass scene_renderer::create_renderpass()
|
||||
.finalLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
|
||||
|
||||
},
|
||||
#ifdef MSAA_4x
|
||||
vk::AttachmentDescription{
|
||||
.format = output_format,
|
||||
.samples = vk::SampleCountFlagBits::e1,
|
||||
@@ -280,7 +294,9 @@ vk::raii::RenderPass scene_renderer::create_renderpass()
|
||||
.initialLayout = vk::ImageLayout::eUndefined,
|
||||
.finalLayout = vk::ImageLayout::eColorAttachmentOptimal,
|
||||
|
||||
}};
|
||||
}
|
||||
#endif
|
||||
};
|
||||
info.setAttachments(attachments);
|
||||
|
||||
vk::AttachmentReference color_attachment{
|
||||
@@ -293,16 +309,20 @@ vk::raii::RenderPass scene_renderer::create_renderpass()
|
||||
.layout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
|
||||
};
|
||||
|
||||
#ifdef MSAA_4x
|
||||
vk::AttachmentReference resolve_attachment{
|
||||
.attachment = 2,
|
||||
.layout = vk::ImageLayout::eColorAttachmentOptimal,
|
||||
};
|
||||
#endif
|
||||
|
||||
vk::SubpassDescription subpasses{
|
||||
.pipelineBindPoint = vk::PipelineBindPoint::eGraphics,
|
||||
.colorAttachmentCount = 1,
|
||||
.pColorAttachments = &color_attachment,
|
||||
#ifdef MSAA_4x
|
||||
.pResolveAttachments = &resolve_attachment,
|
||||
#endif
|
||||
.pDepthStencilAttachment = &depth_attachment,
|
||||
};
|
||||
info.setSubpasses(subpasses);
|
||||
@@ -361,7 +381,7 @@ scene_renderer::output_image scene_renderer::create_output_image_data(vk::Image
|
||||
},
|
||||
.mipLevels = 1,
|
||||
.arrayLayers = 1,
|
||||
.samples = vk::SampleCountFlagBits::e4,
|
||||
.samples = MSAA_SAMPLES,
|
||||
.tiling = vk::ImageTiling::eOptimal,
|
||||
.usage = vk::ImageUsageFlagBits::eDepthStencilAttachment | vk::ImageUsageFlagBits::eTransientAttachment
|
||||
|
||||
@@ -380,6 +400,7 @@ scene_renderer::output_image scene_renderer::create_output_image_data(vk::Image
|
||||
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eDepth, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
|
||||
});
|
||||
|
||||
#ifdef MSAA_4x
|
||||
out.multisample_image = image_allocation{vk::ImageCreateInfo{
|
||||
.imageType = vk::ImageType::e2D,
|
||||
.format = output_format,
|
||||
@@ -390,7 +411,7 @@ scene_renderer::output_image scene_renderer::create_output_image_data(vk::Image
|
||||
},
|
||||
.mipLevels = 1,
|
||||
.arrayLayers = 1,
|
||||
.samples = vk::SampleCountFlagBits::e4,
|
||||
.samples = MSAA_SAMPLES,
|
||||
.tiling = vk::ImageTiling::eOptimal,
|
||||
.usage = vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eTransientAttachment
|
||||
|
||||
@@ -408,6 +429,7 @@ scene_renderer::output_image scene_renderer::create_output_image_data(vk::Image
|
||||
.components{},
|
||||
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
|
||||
});
|
||||
#endif
|
||||
|
||||
vk::FramebufferCreateInfo fb_info{
|
||||
.renderPass = *renderpass,
|
||||
@@ -416,11 +438,18 @@ scene_renderer::output_image scene_renderer::create_output_image_data(vk::Image
|
||||
.layers = 1,
|
||||
};
|
||||
|
||||
#ifdef MSAA_4x
|
||||
std::array attachments{
|
||||
vk::ImageView{*out.multisample_view},
|
||||
vk::ImageView{*out.depth_view},
|
||||
vk::ImageView{*out.image_view},
|
||||
};
|
||||
#else
|
||||
std::array attachments{
|
||||
vk::ImageView{*out.image_view},
|
||||
vk::ImageView{*out.depth_view},
|
||||
};
|
||||
#endif
|
||||
fb_info.setAttachments(attachments);
|
||||
out.framebuffer = vk::raii::Framebuffer(device, fb_info);
|
||||
|
||||
@@ -483,7 +512,7 @@ vk::raii::Pipeline scene_renderer::create_pipeline(const pipeline_info & info)
|
||||
.lineWidth = 1.0,
|
||||
}},
|
||||
.MultisampleState = {vk::PipelineMultisampleStateCreateInfo{
|
||||
.rasterizationSamples = vk::SampleCountFlagBits::e4,
|
||||
.rasterizationSamples = MSAA_SAMPLES,
|
||||
}},
|
||||
.DepthStencilState = {vk::PipelineDepthStencilStateCreateInfo{
|
||||
.depthTestEnable = true,
|
||||
@@ -502,7 +531,7 @@ vk::raii::Pipeline scene_renderer::create_pipeline(const pipeline_info & info)
|
||||
.srcAlphaBlendFactor = vk::BlendFactor::eSrcAlpha,
|
||||
.dstAlphaBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha,
|
||||
.alphaBlendOp = vk::BlendOp::eAdd,
|
||||
.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB}},
|
||||
.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA}},
|
||||
.DynamicState = {},
|
||||
.layout = *pipeline_layout,
|
||||
.renderPass = *renderpass,
|
||||
@@ -530,17 +559,60 @@ vk::Sampler scene_renderer::get_sampler(const sampler_info & info)
|
||||
return **out;
|
||||
}
|
||||
|
||||
scene_renderer::per_frame_resources & scene_renderer::start_frame()
|
||||
void scene_renderer::start_frame()
|
||||
{
|
||||
current_frame = (current_frame + 1) % frame_resources.size();
|
||||
current_frame_index = (current_frame_index + 1) % frame_resources.size();
|
||||
|
||||
device.waitForFences(*frame_resources[current_frame].fence, true, 1'000'000'000);
|
||||
device.resetFences(*frame_resources[current_frame].fence);
|
||||
auto& f = current_frame();
|
||||
f.cpu_time_start = std::chrono::steady_clock::now();
|
||||
|
||||
frame_resources[current_frame].resources.clear();
|
||||
frame_resources[current_frame].cb.begin({.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit});
|
||||
device.waitForFences(*f.fence, true, 1'000'000'000);
|
||||
device.resetFences(*f.fence);
|
||||
|
||||
return frame_resources[current_frame];
|
||||
f.resources.clear();
|
||||
f.cb.begin({.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit});
|
||||
|
||||
f.cb.resetQueryPool(*f.query_pool, 0, 2);
|
||||
f.cb.writeTimestamp(vk::PipelineStageFlagBits::eTopOfPipe, *f.query_pool, 0);
|
||||
|
||||
// TODO: replace with vector<byte> and write to GPU in end_frame()
|
||||
f.staging_buffer_offset = 0;
|
||||
if (!f.staging_buffer)
|
||||
{
|
||||
f.staging_buffer = buffer_allocation{vk::BufferCreateInfo{
|
||||
.size = 1048576,
|
||||
.usage = vk::BufferUsageFlagBits::eUniformBuffer},
|
||||
VmaAllocationCreateInfo{
|
||||
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE},
|
||||
"scene_renderer::render (UBO staging)"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
void scene_renderer::end_frame()
|
||||
{
|
||||
auto& f = current_frame();
|
||||
|
||||
f.cb.writeTimestamp(vk::PipelineStageFlagBits::eBottomOfPipe, *f.query_pool, 1);
|
||||
f.cb.end();
|
||||
|
||||
queue.submit(vk::SubmitInfo{
|
||||
.commandBufferCount = 1,
|
||||
.pCommandBuffers = &*f.cb,
|
||||
}, *f.fence);
|
||||
|
||||
auto cpu_time_stop = std::chrono::steady_clock::now();
|
||||
spdlog::info("scene_renderer::render: CPU time: {} us", std::chrono::duration_cast<std::chrono::microseconds>(cpu_time_stop - f.cpu_time_start).count());
|
||||
|
||||
auto [res, timestamps] = f.query_pool.getResults<uint64_t>(0, 2, 2 * sizeof(uint64_t), sizeof(uint64_t), vk::QueryResultFlagBits::e64 | vk::QueryResultFlagBits::eWait);
|
||||
if (res == vk::Result::eSuccess)
|
||||
spdlog::info("scene_renderer::render: GPU time: {} us", (int)((timestamps[1] - timestamps[0]) * physical_device_properties.limits.timestampPeriod * 1.0e-3));
|
||||
}
|
||||
|
||||
scene_renderer::per_frame_resources & scene_renderer::current_frame()
|
||||
{
|
||||
return frame_resources[current_frame_index];
|
||||
}
|
||||
|
||||
void scene_renderer::update_material_descriptor_set(scene_data::material& material)
|
||||
@@ -609,8 +681,10 @@ static void print_scene_hierarchy(const scene_data& scene, std::span<glm::mat4>
|
||||
}
|
||||
}
|
||||
|
||||
void scene_renderer::render(scene_data & scene, std::span<frame_info> frames)
|
||||
void scene_renderer::render(scene_data & scene, const std::array<float, 4>& clear_color, std::span<frame_info> frames)
|
||||
{
|
||||
per_frame_resources & resources = current_frame();
|
||||
|
||||
size_t ubo_alignment = physical_device_properties.limits.minUniformBufferOffsetAlignment;
|
||||
|
||||
size_t ubo_frame_size = utils::align_up(ubo_alignment, sizeof(frame_gpu_data));
|
||||
@@ -623,59 +697,36 @@ void scene_renderer::render(scene_data & scene, std::span<frame_info> frames)
|
||||
});
|
||||
|
||||
// Enough room for one frame_gpu_data per view and one instance_gpu_data per instance and per view
|
||||
size_t ubo_size = nb_frames * (ubo_frame_size + nb_objects * ubo_instance_size);
|
||||
// size_t ubo_size = nb_frames * (ubo_frame_size + nb_objects * ubo_instance_size);
|
||||
|
||||
// Compute the position of each ubo, taking in account the minimum alignment
|
||||
ptrdiff_t offset = 0;
|
||||
std::vector<ptrdiff_t> ubo_frame_offset(nb_frames);
|
||||
std::vector<size_t> ubo_frame_offset(nb_frames);
|
||||
for(size_t i = 0; i < nb_frames; i++)
|
||||
{
|
||||
ubo_frame_offset[i] = offset;
|
||||
offset += ubo_frame_size;
|
||||
ubo_frame_offset[i] = resources.staging_buffer_offset;
|
||||
resources.staging_buffer_offset += ubo_frame_size;
|
||||
}
|
||||
|
||||
std::vector<ptrdiff_t> ubo_object_offset(nb_frames * nb_objects);
|
||||
std::vector<size_t> ubo_object_offset(nb_frames * nb_objects);
|
||||
for(size_t i = 0; i < nb_frames; i++)
|
||||
{
|
||||
for(size_t j = 0; j < nb_objects; j++)
|
||||
{
|
||||
ubo_object_offset[i * nb_objects + j] = offset;
|
||||
offset += ubo_frame_size;
|
||||
ubo_object_offset[i * nb_objects + j] = resources.staging_buffer_offset;
|
||||
resources.staging_buffer_offset += ubo_instance_size;
|
||||
}
|
||||
}
|
||||
assert(resources.staging_buffer_offset <= resources.staging_buffer.info().size);
|
||||
|
||||
auto current_ubo_frame_offset = ubo_frame_offset.begin();
|
||||
auto current_ubo_object_offset = ubo_object_offset.begin();
|
||||
|
||||
per_frame_resources & resources = start_frame();
|
||||
vk::raii::CommandBuffer & cb = resources.cb;
|
||||
|
||||
if (!resources.staging_buffer || resources.staging_buffer.info().size < ubo_size)
|
||||
{
|
||||
resources.staging_buffer = buffer_allocation{vk::BufferCreateInfo{
|
||||
.size = ubo_size,
|
||||
.usage = vk::BufferUsageFlagBits::eUniformBuffer},
|
||||
VmaAllocationCreateInfo{
|
||||
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE},
|
||||
"scene_renderer::render (UBO staging)"
|
||||
};
|
||||
}
|
||||
|
||||
// if (!resources.uniform_buffer || resources.uniform_buffer.info().size < ubo_size)
|
||||
// {
|
||||
// resources.staging_buffer = buffer_allocation{vk::BufferCreateInfo{
|
||||
// .size = ubo_size,
|
||||
// .usage = vk::BufferUsageFlagBits::eUniformBuffer},
|
||||
// VmaAllocationCreateInfo{
|
||||
// .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
|
||||
// .usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE}};
|
||||
// }
|
||||
|
||||
uintptr_t ubo = reinterpret_cast<uintptr_t>(resources.staging_buffer.map());
|
||||
|
||||
std::array<vk::ClearValue, 2> clear_values{
|
||||
vk::ClearColorValue(0.0f, 0.0f, 0.0f, 0.0f),
|
||||
vk::ClearColorValue{clear_color},
|
||||
vk::ClearDepthStencilValue{1.0, 0},
|
||||
};
|
||||
|
||||
@@ -844,12 +895,4 @@ void scene_renderer::render(scene_data & scene, std::span<frame_info> frames)
|
||||
|
||||
current_ubo_frame_offset++;
|
||||
}
|
||||
|
||||
cb.end();
|
||||
|
||||
queue.submit(vk::SubmitInfo{
|
||||
.commandBufferCount = 1,
|
||||
.pCommandBuffers = &*cb,
|
||||
}, *frame_resources[current_frame].fence);
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include <vulkan/vulkan.hpp>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include <glm/vec2.hpp>
|
||||
@@ -150,20 +151,24 @@ public:
|
||||
{
|
||||
vk::raii::Fence fence = nullptr;
|
||||
vk::raii::CommandBuffer cb = nullptr;
|
||||
vk::raii::QueryPool query_pool = nullptr;
|
||||
std::vector<std::shared_ptr<void>> resources;
|
||||
|
||||
// Uniform buffer for per-view and per-instance data
|
||||
// host visible, host coherent
|
||||
size_t staging_buffer_offset;
|
||||
buffer_allocation staging_buffer;
|
||||
|
||||
// device local
|
||||
// buffer_allocation uniform_buffer;
|
||||
|
||||
std::chrono::steady_clock::time_point cpu_time_start;
|
||||
};
|
||||
|
||||
std::vector<per_frame_resources> frame_resources;
|
||||
int current_frame;
|
||||
int current_frame_index;
|
||||
|
||||
per_frame_resources& start_frame();
|
||||
per_frame_resources& current_frame();
|
||||
|
||||
void update_material_descriptor_set(scene_data::material& material);
|
||||
|
||||
@@ -179,7 +184,9 @@ public:
|
||||
glm::mat4 view;
|
||||
};
|
||||
|
||||
void render(scene_data & scene, std::span<frame_info> info);
|
||||
void start_frame();
|
||||
void render(scene_data & scene, const std::array<float, 4>& clear_color, std::span<frame_info> info);
|
||||
void end_frame();
|
||||
|
||||
std::shared_ptr<scene_data::material> get_default_material()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user