Ask for XR_FB_composition_layer_depth_test and XR_KHR_composition_layer_depth
Enable depth testing between GUI and lobby Use reversed-Z, prefer floating point depth buffer
This commit is contained in:
@@ -86,12 +86,12 @@ target_compile_definitions(wivrn-imgui PUBLIC IMGUI_USE_WCHAR32)
|
||||
target_compile_definitions(wivrn-imgui PUBLIC IMGUI_ENABLE_FREETYPE)
|
||||
# target_compile_definitions(wivrn-imgui PUBLIC IMGUI_DISABLE_OBSOLETE_FUNCTIONS) # incompatible with implot
|
||||
# target_compile_definitions(wivrn-imgui PUBLIC IMGUI_ENABLE_FREETYPE_LUNASVG)
|
||||
target_compile_features(wivrn-imgui PRIVATE cxx_std_20)
|
||||
target_compile_features(wivrn-imgui PRIVATE cxx_std_23)
|
||||
target_link_libraries(wivrn-imgui FreetypeHarfbuzz)
|
||||
|
||||
target_link_libraries(wivrn wivrn-imgui)
|
||||
|
||||
target_compile_features(wivrn PRIVATE cxx_std_20)
|
||||
target_compile_features(wivrn PRIVATE cxx_std_23)
|
||||
target_compile_definitions(wivrn PRIVATE "XR_USE_TIMESPEC=1")
|
||||
|
||||
set_target_properties(wivrn PROPERTIES CXX_VISIBILITY_PRESET hidden)
|
||||
|
||||
@@ -756,6 +756,8 @@ void application::initialize()
|
||||
opt_extensions.push_back(XR_HTC_PASSTHROUGH_EXTENSION_NAME);
|
||||
opt_extensions.push_back(XR_FB_FACE_TRACKING2_EXTENSION_NAME);
|
||||
opt_extensions.push_back(XR_EXT_PALM_POSE_EXTENSION_NAME);
|
||||
opt_extensions.push_back(XR_KHR_COMPOSITION_LAYER_DEPTH_EXTENSION_NAME);
|
||||
opt_extensions.push_back(XR_FB_COMPOSITION_LAYER_DEPTH_TEST_EXTENSION_NAME);
|
||||
|
||||
for (const auto & i: interaction_profiles)
|
||||
{
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
#include "utils/sync_queue.h"
|
||||
#include "wivrn_packets.h"
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
#include <thread>
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
#include <locale>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <ranges>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <string_view>
|
||||
|
||||
@@ -617,7 +618,7 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
}
|
||||
}
|
||||
|
||||
for (auto [new_state, controller]: utils::zip(new_states, controllers))
|
||||
for (auto [new_state, controller]: std::views::zip(new_states, controllers))
|
||||
{
|
||||
if (new_state.hover_distance < 0.02 && controller.second.hover_distance >= 0.02)
|
||||
new_state.fingertip_touched = true;
|
||||
@@ -685,7 +686,7 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
}
|
||||
|
||||
focused_controller = new_focused_controller;
|
||||
for (auto && [controller, next_state]: utils::zip(controllers, new_states))
|
||||
for (auto && [controller, next_state]: std::views::zip(controllers, new_states))
|
||||
{
|
||||
controller.second = next_state;
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#include <fastgltf/types.hpp>
|
||||
#include <fastgltf/util.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <ranges>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
@@ -492,7 +493,7 @@ public:
|
||||
|
||||
std::vector<std::shared_ptr<scene_data::texture>> textures;
|
||||
textures.reserve(gltf.textures.size());
|
||||
for (auto && [srgb, gltf_texture]: utils::zip(srgb_array, gltf.textures))
|
||||
for (auto && [srgb, gltf_texture]: std::views::zip(srgb_array, gltf.textures))
|
||||
{
|
||||
auto & texture_ref = *textures.emplace_back(std::make_shared<scene_data::texture>());
|
||||
|
||||
|
||||
@@ -34,19 +34,21 @@
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <ranges>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <vk_mem_alloc.h>
|
||||
|
||||
extern const std::map<std::string, std::vector<uint32_t>> shaders;
|
||||
|
||||
static vk::Format find_usable_image_format(
|
||||
// TODO move in lobby?
|
||||
vk::Format scene_renderer::find_usable_image_format(
|
||||
vk::raii::PhysicalDevice physical_device,
|
||||
std::span<vk::Format> formats,
|
||||
std::span<const vk::Format> formats,
|
||||
vk::Extent3D min_extent,
|
||||
vk::ImageUsageFlags usage,
|
||||
vk::ImageType type = vk::ImageType::e2D,
|
||||
vk::ImageTiling tiling = vk::ImageTiling::eOptimal,
|
||||
vk::ImageCreateFlags flags = {})
|
||||
vk::ImageType type,
|
||||
vk::ImageTiling tiling,
|
||||
vk::ImageCreateFlags flags)
|
||||
{
|
||||
for (vk::Format format: formats)
|
||||
{
|
||||
@@ -176,27 +178,21 @@ scene_renderer::scene_renderer(
|
||||
vk::raii::CommandPool & cb_pool,
|
||||
vk::Extent2D output_size,
|
||||
vk::Format output_format,
|
||||
std::span<vk::Format> depth_formats,
|
||||
int frames_in_flight) :
|
||||
// std::span<vk::Format> depth_formats,
|
||||
vk::Format depth_format,
|
||||
int frames_in_flight,
|
||||
bool keep_depth_buffer) :
|
||||
physical_device(physical_device),
|
||||
device(device),
|
||||
physical_device_properties(physical_device.getProperties()),
|
||||
queue(queue),
|
||||
output_size(output_size),
|
||||
output_format(output_format),
|
||||
depth_format(
|
||||
find_usable_image_format(
|
||||
physical_device,
|
||||
depth_formats,
|
||||
{
|
||||
output_size.width,
|
||||
output_size.height,
|
||||
1,
|
||||
},
|
||||
vk::ImageUsageFlagBits::eDepthStencilAttachment)),
|
||||
depth_format(depth_format),
|
||||
layout_0(create_descriptor_set_layout(layout_bindings_0, vk::DescriptorSetLayoutCreateFlagBits::ePushDescriptorKHR)),
|
||||
layout_1(create_descriptor_set_layout(layout_bindings_1)),
|
||||
ds_pool_material(device, layout_1, layout_bindings_1, 100) // TODO tunable
|
||||
ds_pool_material(device, layout_1, layout_bindings_1, 100), // TODO tunable
|
||||
keep_depth_buffer(keep_depth_buffer)
|
||||
{
|
||||
// Create the default material
|
||||
default_material = create_default_material(cb_pool);
|
||||
@@ -211,7 +207,7 @@ scene_renderer::scene_renderer(
|
||||
.commandBufferCount = (uint32_t)frame_resources.size(),
|
||||
});
|
||||
|
||||
for (auto && [res, cb]: utils::zip(frame_resources, command_buffers))
|
||||
for (auto && [res, cb]: std::views::zip(frame_resources, command_buffers))
|
||||
{
|
||||
res.fence = device.createFence({.flags = vk::FenceCreateFlagBits::eSignaled});
|
||||
res.cb = std::move(cb);
|
||||
@@ -269,12 +265,11 @@ vk::raii::RenderPass scene_renderer::create_renderpass()
|
||||
.format = depth_format,
|
||||
.samples = MSAA_SAMPLES,
|
||||
.loadOp = vk::AttachmentLoadOp::eClear,
|
||||
.storeOp = vk::AttachmentStoreOp::eDontCare,
|
||||
.storeOp = keep_depth_buffer ? vk::AttachmentStoreOp::eStore : vk::AttachmentStoreOp::eDontCare,
|
||||
.stencilLoadOp = vk::AttachmentLoadOp::eClear,
|
||||
.stencilStoreOp = vk::AttachmentStoreOp::eDontCare,
|
||||
.initialLayout = vk::ImageLayout::eUndefined,
|
||||
.finalLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
|
||||
|
||||
},
|
||||
#ifdef MSAA_4x
|
||||
vk::AttachmentDescription{
|
||||
@@ -342,23 +337,24 @@ vk::raii::RenderPass scene_renderer::create_renderpass()
|
||||
return vk::raii::RenderPass(device, info);
|
||||
}
|
||||
|
||||
scene_renderer::output_image & scene_renderer::get_output_image_data(vk::Image output)
|
||||
scene_renderer::output_image & scene_renderer::get_output_image_data(vk::Image output_color, vk::Image output_depth)
|
||||
{
|
||||
auto it = output_images.find(output);
|
||||
std::pair images{output_color, output_depth};
|
||||
auto it = output_images.find(images);
|
||||
if (it != output_images.end())
|
||||
return it->second;
|
||||
|
||||
return output_images.emplace(output, create_output_image_data(output)).first->second;
|
||||
return output_images.emplace(images, create_output_image_data(output_color, output_depth)).first->second;
|
||||
}
|
||||
|
||||
scene_renderer::output_image scene_renderer::create_output_image_data(vk::Image output)
|
||||
scene_renderer::output_image scene_renderer::create_output_image_data(vk::Image output_color, vk::Image output_depth)
|
||||
{
|
||||
output_image out;
|
||||
|
||||
// TODO: use image view from xr::swapchain
|
||||
out.image_view = vk::raii::ImageView(
|
||||
device, vk::ImageViewCreateInfo{
|
||||
.image = output,
|
||||
.image = output_color,
|
||||
.viewType = vk::ImageViewType::e2D,
|
||||
.format = output_format,
|
||||
.components{},
|
||||
@@ -371,30 +367,33 @@ scene_renderer::output_image scene_renderer::create_output_image_data(vk::Image
|
||||
},
|
||||
});
|
||||
|
||||
out.depth_buffer = image_allocation{
|
||||
device,
|
||||
vk::ImageCreateInfo{
|
||||
.imageType = vk::ImageType::e2D,
|
||||
.format = depth_format,
|
||||
.extent = {
|
||||
.width = output_size.width,
|
||||
.height = output_size.height,
|
||||
.depth = 1,
|
||||
},
|
||||
.mipLevels = 1,
|
||||
.arrayLayers = 1,
|
||||
.samples = MSAA_SAMPLES,
|
||||
.tiling = vk::ImageTiling::eOptimal,
|
||||
.usage = vk::ImageUsageFlagBits::eDepthStencilAttachment | vk::ImageUsageFlagBits::eTransientAttachment},
|
||||
VmaAllocationCreateInfo{
|
||||
.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT, .usage = VMA_MEMORY_USAGE_AUTO,
|
||||
.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, // TODO: check
|
||||
}};
|
||||
// TODO: check requiredFlags
|
||||
if (not output_depth)
|
||||
out.depth_buffer = image_allocation{
|
||||
device,
|
||||
vk::ImageCreateInfo{
|
||||
.imageType = vk::ImageType::e2D,
|
||||
.format = depth_format,
|
||||
.extent = {
|
||||
.width = output_size.width,
|
||||
.height = output_size.height,
|
||||
.depth = 1,
|
||||
},
|
||||
.mipLevels = 1,
|
||||
.arrayLayers = 1,
|
||||
.samples = MSAA_SAMPLES,
|
||||
.tiling = vk::ImageTiling::eOptimal,
|
||||
.usage = vk::ImageUsageFlagBits::eDepthStencilAttachment | vk::ImageUsageFlagBits::eTransientAttachment},
|
||||
VmaAllocationCreateInfo{
|
||||
.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO,
|
||||
.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
||||
}};
|
||||
|
||||
out.depth_view = vk::raii::ImageView(
|
||||
device,
|
||||
vk::ImageViewCreateInfo{
|
||||
.image = out.depth_buffer,
|
||||
.image = output_depth ? output_depth : out.depth_buffer,
|
||||
.viewType = vk::ImageViewType::e2D,
|
||||
.format = depth_format,
|
||||
.components{},
|
||||
@@ -586,7 +585,7 @@ vk::raii::Pipeline scene_renderer::create_pipeline(const pipeline_info & info)
|
||||
.DepthStencilState = {vk::PipelineDepthStencilStateCreateInfo{
|
||||
.depthTestEnable = true,
|
||||
.depthWriteEnable = true,
|
||||
.depthCompareOp = vk::CompareOp::eLess,
|
||||
.depthCompareOp = vk::CompareOp::eGreater,
|
||||
.depthBoundsTestEnable = false,
|
||||
.minDepthBounds = 0.0f,
|
||||
.maxDepthBounds = 1.0f,
|
||||
@@ -777,7 +776,7 @@ void scene_renderer::render(scene_data & scene, const std::array<float, 4> & cle
|
||||
|
||||
std::array<vk::ClearValue, 2> clear_values{
|
||||
vk::ClearColorValue{clear_color},
|
||||
vk::ClearDepthStencilValue{1.0, 0},
|
||||
vk::ClearDepthStencilValue{0.0, 0},
|
||||
};
|
||||
|
||||
auto vertex_layout = scene_data::vertex::describe();
|
||||
@@ -814,7 +813,7 @@ void scene_renderer::render(scene_data & scene, const std::array<float, 4> & cle
|
||||
|
||||
for (const auto && [frame_index, frame]: utils::enumerate(frames))
|
||||
{
|
||||
scene_renderer::output_image & output = get_output_image_data(frame.destination);
|
||||
scene_renderer::output_image & output = get_output_image_data(frame.destination, frame.depth_buffer);
|
||||
glm::mat4 viewproj = frame.projection * frame.view;
|
||||
|
||||
vk::DeviceSize frame_ubo_offset = resources.uniform_buffer_offset;
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include <glm/vec2.hpp>
|
||||
#include <glm/vec3.hpp>
|
||||
#include <glm/vec4.hpp>
|
||||
#include <map>
|
||||
#include <span>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
@@ -82,12 +83,12 @@ class scene_renderer
|
||||
image_allocation depth_buffer;
|
||||
vk::raii::ImageView depth_view = nullptr;
|
||||
|
||||
image_allocation multisample_image;
|
||||
vk::raii::ImageView multisample_view = nullptr;
|
||||
// image_allocation multisample_image;
|
||||
// vk::raii::ImageView multisample_view = nullptr;
|
||||
};
|
||||
|
||||
// Initialization functions
|
||||
output_image create_output_image_data(vk::Image output);
|
||||
output_image create_output_image_data(vk::Image output_color, vk::Image output_depth);
|
||||
vk::raii::RenderPass create_renderpass();
|
||||
vk::raii::PipelineLayout create_pipeline_layout(std::span<vk::DescriptorSetLayout> layouts);
|
||||
vk::raii::Pipeline create_pipeline(const pipeline_info & info);
|
||||
@@ -97,10 +98,10 @@ class scene_renderer
|
||||
vk::raii::DescriptorSetLayout create_descriptor_set_layout(std::span<vk::DescriptorSetLayoutBinding> bindings, vk::DescriptorSetLayoutCreateFlags flags = {});
|
||||
|
||||
// Caches
|
||||
std::unordered_map<VkImage, output_image> output_images;
|
||||
std::/*unordered_*/ map<std::pair<VkImage, VkImage>, output_image> output_images; // TODO: use an unordered_map, create a hash for std::pair
|
||||
std::unordered_map<pipeline_info, vk::raii::Pipeline> pipelines;
|
||||
|
||||
output_image & get_output_image_data(vk::Image output);
|
||||
output_image & get_output_image_data(vk::Image output_color, vk::Image output_depth);
|
||||
vk::raii::Pipeline & get_pipeline(const pipeline_info & info);
|
||||
|
||||
vk::raii::DescriptorSetLayout layout_0; // Descriptor set 0: per-frame/view data (UBO) and per-instance data (UBO + SSBO)
|
||||
@@ -167,12 +168,22 @@ class scene_renderer
|
||||
int current_frame_index;
|
||||
vk::raii::QueryPool query_pool = nullptr;
|
||||
double gpu_time_s = 0;
|
||||
bool keep_depth_buffer = false;
|
||||
|
||||
per_frame_resources & current_frame();
|
||||
|
||||
void update_material_descriptor_set(scene_data::material & material);
|
||||
|
||||
public:
|
||||
static vk::Format find_usable_image_format(
|
||||
vk::raii::PhysicalDevice physical_device,
|
||||
std::span<const vk::Format> formats,
|
||||
vk::Extent3D min_extent,
|
||||
vk::ImageUsageFlags usage,
|
||||
vk::ImageType type = vk::ImageType::e2D,
|
||||
vk::ImageTiling tiling = vk::ImageTiling::eOptimal,
|
||||
vk::ImageCreateFlags flags = {});
|
||||
|
||||
scene_renderer(
|
||||
vk::raii::Device & device,
|
||||
vk::raii::PhysicalDevice physical_device,
|
||||
@@ -180,14 +191,16 @@ public:
|
||||
vk::raii::CommandPool & cb_pool,
|
||||
vk::Extent2D output_size,
|
||||
vk::Format output_format,
|
||||
std::span<vk::Format> depth_formats,
|
||||
int frames_in_flight = 2);
|
||||
vk::Format depth_format,
|
||||
int frames_in_flight = 2,
|
||||
bool keep_depth_buffer = false);
|
||||
|
||||
~scene_renderer();
|
||||
|
||||
struct frame_info
|
||||
{
|
||||
vk::Image destination;
|
||||
vk::Image depth_buffer;
|
||||
glm::mat4 projection;
|
||||
glm::mat4 view;
|
||||
};
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
#include "hand_model.h"
|
||||
#include "openxr/openxr.h"
|
||||
#include "utils/ranges.h"
|
||||
#include <ranges>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
hand_model::hand_model(const std::filesystem::path & gltf_path, scene_loader & loader, scene_data & scene)
|
||||
@@ -65,7 +65,7 @@ void hand_model::apply(const std::optional<std::array<xr::hand_tracker::joint, X
|
||||
{
|
||||
root_node->visible = true;
|
||||
|
||||
for (auto && [joint, loc]: utils::zip(joints, *joints_location))
|
||||
for (auto && [joint, loc]: std::views::zip(joints, *joints_location))
|
||||
{
|
||||
if (!joint)
|
||||
continue;
|
||||
|
||||
+185
-45
@@ -37,6 +37,7 @@
|
||||
#include <cstdint>
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
#include <magic_enum.hpp>
|
||||
#include <ranges>
|
||||
#include <simdjson.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <string>
|
||||
@@ -89,11 +90,16 @@ static std::string choose_webxr_profile()
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
static const std::array supported_formats = {
|
||||
static const std::array supported_color_formats = {
|
||||
vk::Format::eR8G8B8A8Srgb,
|
||||
vk::Format::eB8G8R8A8Srgb,
|
||||
};
|
||||
|
||||
static const std::array supported_depth_formats{
|
||||
vk::Format::eD32Sfloat,
|
||||
vk::Format::eX8D24UnormPack32,
|
||||
};
|
||||
|
||||
void scenes::lobby::move_gui(glm::vec3 head_position, glm::vec3 new_gui_position)
|
||||
{
|
||||
const float gui_pitch = -0.2;
|
||||
@@ -129,9 +135,10 @@ scenes::lobby::lobby()
|
||||
{
|
||||
spdlog::info(" {}", vk::to_string(format));
|
||||
}
|
||||
|
||||
for (auto format: session.get_swapchain_formats())
|
||||
{
|
||||
if (std::find(supported_formats.begin(), supported_formats.end(), format) != supported_formats.end())
|
||||
if (std::find(supported_color_formats.begin(), supported_color_formats.end(), format) != supported_color_formats.end())
|
||||
{
|
||||
swapchain_format = format;
|
||||
break;
|
||||
@@ -141,7 +148,31 @@ scenes::lobby::lobby()
|
||||
if (swapchain_format == vk::Format::eUndefined)
|
||||
throw std::runtime_error(_("No supported swapchain format"));
|
||||
|
||||
spdlog::info("Using format {}", vk::to_string(swapchain_format));
|
||||
auto views = system.view_configuration_views(viewconfig);
|
||||
stream_view = override_view(views[0], guess_model());
|
||||
uint32_t width = views[0].recommendedImageRectWidth;
|
||||
uint32_t height = views[0].recommendedImageRectHeight;
|
||||
|
||||
depth_format = scene_renderer::find_usable_image_format(
|
||||
physical_device,
|
||||
supported_depth_formats,
|
||||
{
|
||||
width,
|
||||
height,
|
||||
1,
|
||||
},
|
||||
vk::ImageUsageFlagBits::eDepthStencilAttachment);
|
||||
|
||||
spdlog::info("Using formats {} and {}", vk::to_string(swapchain_format), vk::to_string(depth_format));
|
||||
|
||||
composition_layer_depth_test_supported =
|
||||
instance.has_extension(XR_KHR_COMPOSITION_LAYER_DEPTH_EXTENSION_NAME) and
|
||||
instance.has_extension(XR_FB_COMPOSITION_LAYER_DEPTH_TEST_EXTENSION_NAME);
|
||||
|
||||
if (composition_layer_depth_test_supported)
|
||||
spdlog::info("Composition layer depth test supported");
|
||||
else
|
||||
spdlog::info("Composition layer depth test NOT supported");
|
||||
}
|
||||
|
||||
static std::string ip_address_to_string(const in_addr & addr)
|
||||
@@ -248,12 +279,14 @@ static glm::mat4 projection_matrix(XrFovf fov, float zn = 0.02)
|
||||
float t = tan(fov.angleUp);
|
||||
float b = tan(fov.angleDown);
|
||||
|
||||
// reversed Z projection, infinite far plane
|
||||
|
||||
// clang-format off
|
||||
return glm::mat4{
|
||||
{ 2/(r-l), 0, 0, 0 },
|
||||
{ 0, 2/(b-t), 0, 0 },
|
||||
{ (l+r)/(r-l), (t+b)/(b-t), -1, -1 },
|
||||
{ 0, 0, -2*zn, 0 }
|
||||
{ (l+r)/(r-l), (t+b)/(b-t), 0, -1 },
|
||||
{ 0, 0, zn, 0 }
|
||||
};
|
||||
// clang-format on
|
||||
}
|
||||
@@ -428,42 +461,81 @@ std::optional<glm::vec3> scenes::lobby::check_recenter_gui(glm::vec3 head_positi
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
static std::vector<XrCompositionLayerProjectionView> render_layer(std::vector<XrView> & views, std::vector<xr::swapchain> & swapchains, scene_renderer & renderer, scene_data & data, const std::array<float, 4> & clear_color)
|
||||
static std::pair<std::vector<XrCompositionLayerProjectionView>, std::vector<XrCompositionLayerDepthInfoKHR>> render_layer(
|
||||
std::vector<XrView> & views,
|
||||
std::vector<xr::swapchain> & color_swapchains,
|
||||
std::vector<xr::swapchain> & depth_swapchains,
|
||||
scene_renderer & renderer,
|
||||
scene_data & data,
|
||||
const std::array<float, 4> & clear_color)
|
||||
{
|
||||
std::vector<scene_renderer::frame_info> frames;
|
||||
frames.reserve(views.size());
|
||||
|
||||
std::vector<XrCompositionLayerProjectionView> layer_views;
|
||||
layer_views.reserve(views.size());
|
||||
std::vector<XrCompositionLayerProjectionView> proj_layer_views;
|
||||
std::vector<XrCompositionLayerDepthInfoKHR> depth_layer_views;
|
||||
proj_layer_views.reserve(views.size());
|
||||
depth_layer_views.reserve(views.size());
|
||||
|
||||
for (auto && [view, swapchain]: utils::zip(views, swapchains))
|
||||
for (auto && [view, color_swapchain, depth_swapchain]: std::views::zip(views, color_swapchains, depth_swapchains))
|
||||
{
|
||||
int image_index = swapchain.acquire();
|
||||
swapchain.wait();
|
||||
int color_image_index = color_swapchain.acquire();
|
||||
color_swapchain.wait();
|
||||
|
||||
int depth_image_index = depth_swapchain ? depth_swapchain.acquire() : 0;
|
||||
if (depth_swapchain)
|
||||
depth_swapchain.wait();
|
||||
|
||||
float zn = 0.02;
|
||||
|
||||
frames.push_back({
|
||||
.destination = swapchain.images()[image_index].image,
|
||||
.projection = projection_matrix(view.fov),
|
||||
.destination = color_swapchain.images()[color_image_index].image,
|
||||
.depth_buffer = depth_swapchain ? depth_swapchain.images()[depth_image_index].image : vk::Image{},
|
||||
.projection = projection_matrix(view.fov, zn),
|
||||
.view = view_matrix(view.pose),
|
||||
});
|
||||
|
||||
layer_views.push_back({
|
||||
proj_layer_views.push_back({
|
||||
.type = XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW,
|
||||
.pose = view.pose,
|
||||
.fov = view.fov,
|
||||
.subImage = {
|
||||
.swapchain = swapchain,
|
||||
.swapchain = color_swapchain,
|
||||
.imageRect = {
|
||||
.offset = {0, 0},
|
||||
.extent = swapchain.extent(),
|
||||
.extent = color_swapchain.extent(),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// TODO: check that the projection matrix is correct wrt https://registry.khronos.org/OpenXR/specs/1.1/html/xrspec.html#XR_KHR_composition_layer_depth
|
||||
depth_layer_views.push_back({
|
||||
.type = XR_TYPE_COMPOSITION_LAYER_DEPTH_INFO_KHR,
|
||||
.subImage = {
|
||||
.swapchain = depth_swapchain,
|
||||
.imageRect = {
|
||||
.offset = {0, 0},
|
||||
.extent = depth_swapchain.extent(),
|
||||
},
|
||||
},
|
||||
.minDepth = 0,
|
||||
.maxDepth = 1,
|
||||
.nearZ = std::numeric_limits<float>::infinity(),
|
||||
.farZ = zn,
|
||||
});
|
||||
}
|
||||
|
||||
renderer.render(data, clear_color, frames);
|
||||
|
||||
return layer_views;
|
||||
return {proj_layer_views, depth_layer_views};
|
||||
}
|
||||
|
||||
static std::vector<XrCompositionLayerProjectionView> render_layer(std::vector<XrView> & views, std::vector<xr::swapchain> & color_swapchains, scene_renderer & renderer, scene_data & data, const std::array<float, 4> & clear_color)
|
||||
{
|
||||
std::vector<xr::swapchain> depth_swapchains;
|
||||
depth_swapchains.resize(color_swapchains.size());
|
||||
|
||||
return std::get<0>(render_layer(views, color_swapchains, depth_swapchains, renderer, data, clear_color));
|
||||
}
|
||||
|
||||
namespace
|
||||
@@ -592,22 +664,57 @@ void scenes::lobby::render(const XrFrameState & frame_state)
|
||||
|
||||
assert(renderer);
|
||||
renderer->start_frame();
|
||||
|
||||
std::vector<XrCompositionLayerProjectionView> lobby_layer_views;
|
||||
if (not application::get_config().passthrough_enabled)
|
||||
lobby_layer_views = render_layer(views, swapchains_lobby, *renderer, *lobby_scene, {0, 0.25, 0.5, 1});
|
||||
std::vector<XrCompositionLayerDepthInfoKHR> depthinfo_layer_views;
|
||||
std::vector<XrCompositionLayerDepthTestFB> depthtest_layer_views;
|
||||
std::vector<XrCompositionLayerProjectionView> controllers_layer_views;
|
||||
|
||||
auto controllers_layer_views = render_layer(views, swapchains_controllers, *renderer, *controllers_scene, {0, 0, 0, 0});
|
||||
renderer->end_frame();
|
||||
|
||||
// After end_frame because the command buffers are submitted in end_frame
|
||||
if (not application::get_config().passthrough_enabled)
|
||||
if (composition_layer_depth_test_supported)
|
||||
{
|
||||
std::array<float, 4> clear_color;
|
||||
|
||||
lobby_handle->visible = not application::get_config().passthrough_enabled;
|
||||
|
||||
if (application::get_config().passthrough_enabled)
|
||||
clear_color = {0, 0, 0, 0};
|
||||
else
|
||||
clear_color = {0, 0.25, 0.5, 1};
|
||||
|
||||
std::tie(lobby_layer_views, depthinfo_layer_views) = render_layer(views, swapchains_lobby, swapchains_depth, *renderer, *lobby_scene, clear_color);
|
||||
|
||||
for (auto [color, depth]: std::views::zip(lobby_layer_views, depthinfo_layer_views))
|
||||
{
|
||||
color.next = &depth;
|
||||
}
|
||||
|
||||
renderer->end_frame();
|
||||
|
||||
// After end_frame because the command buffers are submitted in end_frame
|
||||
for (auto & swapchain: swapchains_lobby)
|
||||
swapchain.release();
|
||||
}
|
||||
|
||||
for (auto & swapchain: swapchains_controllers)
|
||||
swapchain.release();
|
||||
for (auto & swapchain: swapchains_depth)
|
||||
swapchain.release();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (not application::get_config().passthrough_enabled)
|
||||
lobby_layer_views = render_layer(views, swapchains_lobby, *renderer, *lobby_scene, {0, 0.25, 0.5, 1});
|
||||
|
||||
controllers_layer_views = render_layer(views, swapchains_controllers, *renderer, *controllers_scene, {0, 0, 0, 0});
|
||||
renderer->end_frame();
|
||||
|
||||
// After end_frame because the command buffers are submitted in end_frame
|
||||
if (not application::get_config().passthrough_enabled)
|
||||
{
|
||||
for (auto & swapchain: swapchains_lobby)
|
||||
swapchain.release();
|
||||
}
|
||||
|
||||
for (auto & swapchain: swapchains_controllers)
|
||||
swapchain.release();
|
||||
}
|
||||
|
||||
XrCompositionLayerProjection lobby_layer{
|
||||
.type = XR_TYPE_COMPOSITION_LAYER_PROJECTION,
|
||||
@@ -643,14 +750,39 @@ void scenes::lobby::render(const XrFrameState & frame_state)
|
||||
}},
|
||||
passthrough);
|
||||
}
|
||||
else
|
||||
|
||||
if (composition_layer_depth_test_supported)
|
||||
{
|
||||
layers_base.push_back(reinterpret_cast<XrCompositionLayerBaseHeader *>(&lobby_layer));
|
||||
}
|
||||
layers_base.push_back(reinterpret_cast<XrCompositionLayerBaseHeader *>(&imgui_layer));
|
||||
layers_base.push_back(reinterpret_cast<XrCompositionLayerBaseHeader *>(&controllers_layer));
|
||||
layers_base.push_back(reinterpret_cast<XrCompositionLayerBaseHeader *>(&imgui_layer));
|
||||
|
||||
session.end_frame(frame_state.predictedDisplayTime, layers_base, blend_mode);
|
||||
// Add XrCompositionLayerDepthTestFB to lobby_layer and imgui_layer
|
||||
// The sky in the lobby layer and the passthrough layer have the same depth, so the operation must be:
|
||||
// - LESS when passthrough is enabled (to avoid overwriting the passthrough)
|
||||
// - LESS_OR_EQUAL when passthrough is disabled (so that the sky is visible)
|
||||
XrCompositionLayerDepthTestFB layer_depth_test{
|
||||
.type = XR_TYPE_COMPOSITION_LAYER_DEPTH_TEST_FB,
|
||||
.next = nullptr,
|
||||
.depthMask = true,
|
||||
.compareOp = application::get_config().passthrough_enabled ? XR_COMPARE_OP_LESS_FB : XR_COMPARE_OP_LESS_OR_EQUAL_FB};
|
||||
|
||||
lobby_layer.next = &layer_depth_test;
|
||||
imgui_layer.next = &layer_depth_test;
|
||||
|
||||
session.end_frame(frame_state.predictedDisplayTime, layers_base, blend_mode);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (not application::get_config().passthrough_enabled)
|
||||
layers_base.push_back(reinterpret_cast<XrCompositionLayerBaseHeader *>(&lobby_layer));
|
||||
|
||||
layers_base.push_back(reinterpret_cast<XrCompositionLayerBaseHeader *>(&imgui_layer));
|
||||
|
||||
if (not composition_layer_depth_test_supported)
|
||||
layers_base.push_back(reinterpret_cast<XrCompositionLayerBaseHeader *>(&controllers_layer));
|
||||
|
||||
session.end_frame(frame_state.predictedDisplayTime, layers_base, blend_mode);
|
||||
}
|
||||
}
|
||||
|
||||
void scenes::lobby::on_focused()
|
||||
@@ -663,40 +795,47 @@ void scenes::lobby::on_focused()
|
||||
uint32_t height = views[0].recommendedImageRectHeight;
|
||||
|
||||
swapchains_lobby.reserve(views.size());
|
||||
swapchains_controllers.reserve(views.size());
|
||||
|
||||
if (composition_layer_depth_test_supported)
|
||||
swapchains_depth.reserve(views.size());
|
||||
else
|
||||
swapchains_controllers.reserve(views.size());
|
||||
|
||||
for ([[maybe_unused]] auto view: views)
|
||||
{
|
||||
assert(view.recommendedImageRectWidth == width);
|
||||
assert(view.recommendedImageRectHeight == height);
|
||||
|
||||
swapchains_lobby.emplace_back(session, device, swapchain_format, width, height);
|
||||
swapchains_controllers.emplace_back(session, device, swapchain_format, width, height);
|
||||
if (composition_layer_depth_test_supported)
|
||||
swapchains_depth.emplace_back(session, device, depth_format, width, height);
|
||||
else
|
||||
swapchains_controllers.emplace_back(session, device, swapchain_format, width, height);
|
||||
}
|
||||
|
||||
spdlog::info("Created lobby swapchains: {}x{}", width, height);
|
||||
|
||||
vk::Extent2D output_size{width, height};
|
||||
|
||||
std::array depth_formats{
|
||||
vk::Format::eX8D24UnormPack32,
|
||||
vk::Format::eD32Sfloat,
|
||||
};
|
||||
|
||||
renderer.emplace(device, physical_device, queue, commandpool, output_size, swapchain_format, depth_formats);
|
||||
renderer.emplace(device, physical_device, queue, commandpool, output_size, swapchain_format, depth_format, 2, composition_layer_depth_test_supported);
|
||||
|
||||
scene_loader loader(device, physical_device, queue, application::queue_family_index(), renderer->get_default_material());
|
||||
|
||||
lobby_scene.emplace();
|
||||
lobby_scene->import(loader("ground.gltf"));
|
||||
lobby_handle = lobby_scene->new_node();
|
||||
lobby_scene->import(loader("ground.gltf"), lobby_handle);
|
||||
|
||||
controllers_scene.emplace();
|
||||
input = input_profile("controllers/" + choose_webxr_profile() + "/profile.json", loader, *controllers_scene);
|
||||
if (not composition_layer_depth_test_supported)
|
||||
controllers_scene.emplace();
|
||||
|
||||
scene_data & controllers_scene_data = composition_layer_depth_test_supported ? *lobby_scene : *controllers_scene;
|
||||
input = input_profile("controllers/" + choose_webxr_profile() + "/profile.json", loader, controllers_scene_data);
|
||||
spdlog::info("Loaded input profile {}", input->id);
|
||||
|
||||
if (application::get_hand_tracking_supported())
|
||||
{
|
||||
left_hand.emplace("left-hand.glb", loader, *controllers_scene);
|
||||
right_hand.emplace("right-hand.glb", loader, *controllers_scene);
|
||||
left_hand.emplace("left-hand.glb", loader, controllers_scene_data);
|
||||
right_hand.emplace("right-hand.glb", loader, controllers_scene_data);
|
||||
}
|
||||
|
||||
recenter_left_action = get_action("recenter_left").first;
|
||||
@@ -789,6 +928,7 @@ void scenes::lobby::on_unfocused()
|
||||
renderer.reset();
|
||||
swapchains_lobby.clear();
|
||||
swapchains_controllers.clear();
|
||||
swapchains_depth.clear();
|
||||
swapchain_imgui = xr::swapchain();
|
||||
passthrough.emplace<std::monostate>();
|
||||
multicast.reset();
|
||||
|
||||
@@ -65,6 +65,8 @@ class lobby : public scene_impl<lobby>
|
||||
std::optional<input_profile> input;
|
||||
std::optional<hand_model> left_hand;
|
||||
std::optional<hand_model> right_hand;
|
||||
node_handle lobby_handle;
|
||||
bool composition_layer_depth_test_supported;
|
||||
|
||||
std::optional<imgui_context> imgui_ctx;
|
||||
std::array<XrAction, 2> haptic_output;
|
||||
@@ -75,8 +77,10 @@ class lobby : public scene_impl<lobby>
|
||||
|
||||
std::vector<xr::swapchain> swapchains_lobby;
|
||||
std::vector<xr::swapchain> swapchains_controllers;
|
||||
std::vector<xr::swapchain> swapchains_depth;
|
||||
xr::swapchain swapchain_imgui;
|
||||
vk::Format swapchain_format;
|
||||
vk::Format depth_format;
|
||||
xr::system::passthrough_type passthrough_supported;
|
||||
xr::passthrough passthrough;
|
||||
XrViewConfigurationView stream_view;
|
||||
|
||||
@@ -23,8 +23,8 @@
|
||||
#include "imgui_internal.h"
|
||||
#include "lobby.h"
|
||||
|
||||
#include "utils/ranges.h"
|
||||
#include <cwchar>
|
||||
#include <ranges>
|
||||
#include <unicode/uchar.h> // u_toupper
|
||||
|
||||
struct key
|
||||
@@ -364,7 +364,7 @@ void scenes::lobby::gui_keyboard(ImVec2 size)
|
||||
}
|
||||
|
||||
int id = 0;
|
||||
for (const auto & [row, width]: utils::zip(layout, keys_width))
|
||||
for (const auto & [row, width]: std::views::zip(layout, keys_width))
|
||||
{
|
||||
ImVec2 key_position = position;
|
||||
|
||||
|
||||
@@ -98,7 +98,7 @@ std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_ses
|
||||
|
||||
assert(views.size() == info.fov.size());
|
||||
|
||||
for (auto [i, j]: utils::zip(views, info.fov))
|
||||
for (auto [i, j]: std::views::zip(views, info.fov))
|
||||
{
|
||||
j = i.fov;
|
||||
}
|
||||
@@ -576,7 +576,7 @@ void scenes::stream::render(const XrFrameState & frame_state)
|
||||
blit_handles = common_frame(frame_state.predictedDisplayTime);
|
||||
|
||||
// Blit images from the decoders
|
||||
for (auto [i, blit_handle]: utils::zip(decoders, blit_handles))
|
||||
for (auto [i, blit_handle]: std::views::zip(decoders, blit_handles))
|
||||
{
|
||||
if (not blit_handle)
|
||||
continue;
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#include "utils/ranges.h"
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <ranges>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
namespace
|
||||
@@ -106,7 +107,7 @@ void scenes::stream::accumulate_metrics(XrTime predicted_display_time, const std
|
||||
min_encode_begin = std::min(min_encode_begin, bh->timing_info.encode_begin);
|
||||
}
|
||||
|
||||
for (auto && [metrics, bh]: utils::zip(decoder_metrics, blit_handles))
|
||||
for (auto && [metrics, bh]: std::views::zip(decoder_metrics, blit_handles))
|
||||
{
|
||||
if (metrics.size() != global_metrics.size())
|
||||
metrics.resize(global_metrics.size());
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
#include "application.h"
|
||||
#include "stream.h"
|
||||
#include "utils/ranges.h"
|
||||
#include <ranges>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <thread>
|
||||
|
||||
@@ -245,7 +245,7 @@ void scenes::stream::tracking()
|
||||
auto [flags, views] = session.locate_views(XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO, t0 + Δt, view_space);
|
||||
assert(views.size() == packet.views.size());
|
||||
|
||||
for (auto [i, j]: utils::zip(views, packet.views))
|
||||
for (auto [i, j]: std::views::zip(views, packet.views))
|
||||
{
|
||||
j.pose = i.pose;
|
||||
j.fov = i.fov;
|
||||
|
||||
@@ -116,60 +116,4 @@ enumerate_range<T> enumerate(T & t)
|
||||
return {t};
|
||||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
struct zip_range
|
||||
{
|
||||
struct iterator
|
||||
{
|
||||
using iterator1 = decltype(std::begin(std::declval<T1 &>()));
|
||||
using iterator2 = decltype(std::begin(std::declval<T2 &>()));
|
||||
|
||||
using value1 = decltype(*std::declval<iterator1>());
|
||||
using value2 = decltype(*std::declval<iterator2>());
|
||||
|
||||
iterator1 iter1;
|
||||
iterator2 iter2;
|
||||
|
||||
std::pair<value1 &, value2 &> operator*() const
|
||||
{
|
||||
return {*iter1, *iter2};
|
||||
}
|
||||
|
||||
iterator & operator++()
|
||||
{
|
||||
++iter1;
|
||||
++iter2;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(const iterator & other) const
|
||||
{
|
||||
return iter1 == other.iter1 || iter2 == other.iter2;
|
||||
}
|
||||
|
||||
bool operator!=(const iterator & other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
};
|
||||
|
||||
T1 & range1;
|
||||
T2 & range2;
|
||||
|
||||
iterator begin() const
|
||||
{
|
||||
return iterator{std::begin(range1), std::begin(range2)};
|
||||
}
|
||||
|
||||
iterator end() const
|
||||
{
|
||||
return iterator{std::end(range1), std::end(range2)};
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T1, typename T2>
|
||||
zip_range<T1, T2> zip(T1 & range1, T2 & range2)
|
||||
{
|
||||
return zip_range<T1, T2>{range1, range2};
|
||||
}
|
||||
} // namespace utils
|
||||
|
||||
@@ -51,6 +51,13 @@ public:
|
||||
{
|
||||
control.send(std::forward<T>(packet));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void serialize_control(serialization_packet & p, const T & data)
|
||||
{
|
||||
control.serialize<T>(p, data);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void send_stream(T && packet)
|
||||
{
|
||||
@@ -60,6 +67,12 @@ public:
|
||||
control.send(std::forward<T>(packet));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void serialize_stream(serialization_packet & p, const T & data)
|
||||
{
|
||||
stream.serialize<T>(p, data);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
int poll(T && visitor, std::chrono::milliseconds timeout)
|
||||
{
|
||||
|
||||
@@ -20,9 +20,9 @@
|
||||
#include "session.h"
|
||||
|
||||
#include "details/enumerate.h"
|
||||
#include "utils/ranges.h"
|
||||
#include "xr/instance.h"
|
||||
#include "xr/system.h"
|
||||
#include <ranges>
|
||||
#include <vulkan/vulkan.h>
|
||||
#include <openxr/openxr_platform.h>
|
||||
|
||||
@@ -308,7 +308,7 @@ void xr::session::sync_actions(std::span<XrActionSet> action_sets)
|
||||
{
|
||||
std::vector<XrActiveActionSet> active_action_sets(action_sets.size());
|
||||
|
||||
for (auto && [i, j]: utils::zip(active_action_sets, action_sets))
|
||||
for (auto && [i, j]: std::views::zip(active_action_sets, action_sets))
|
||||
{
|
||||
i.actionSet = j;
|
||||
i.subactionPath = XR_NULL_PATH;
|
||||
|
||||
+18
-1
@@ -25,10 +25,27 @@ xr::swapchain::swapchain(xr::session & s, vk::raii::Device & device, vk::Format
|
||||
{
|
||||
assert(sample_count == 1);
|
||||
|
||||
XrSwapchainUsageFlags usage_flags;
|
||||
|
||||
switch (format)
|
||||
{
|
||||
case vk::Format::eD16Unorm:
|
||||
case vk::Format::eD16UnormS8Uint:
|
||||
case vk::Format::eD24UnormS8Uint:
|
||||
case vk::Format::eX8D24UnormPack32:
|
||||
case vk::Format::eD32Sfloat:
|
||||
case vk::Format::eD32SfloatS8Uint:
|
||||
usage_flags = XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
|
||||
break;
|
||||
default:
|
||||
usage_flags = XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT;
|
||||
break;
|
||||
}
|
||||
|
||||
XrSwapchainCreateInfo create_info{
|
||||
.type = XR_TYPE_SWAPCHAIN_CREATE_INFO,
|
||||
.createFlags = 0,
|
||||
.usageFlags = XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT,
|
||||
.usageFlags = usage_flags,
|
||||
.format = static_cast<VkFormat>(format),
|
||||
.sampleCount = (uint32_t)sample_count,
|
||||
.width = (uint32_t)width,
|
||||
|
||||
Reference in New Issue
Block a user