RAIIfy vulkan

This commit is contained in:
Guillaume Meunier
2024-01-07 11:45:07 +01:00
parent 54fe51b21f
commit cf14f9bd8b
44 changed files with 2711 additions and 2793 deletions
+210 -176
View File
@@ -21,209 +21,248 @@
#include "render/text_rasterizer.h"
#include "stream.h"
#include "../common/version.h"
#include "application.h"
#include "vk/pipeline_layout.h"
#include "vk/shader.h"
#include "vk/pipeline.h"
#include <cstdint>
#include <glm/gtc/quaternion.hpp>
#include <string>
#include <tiny_gltf.h>
#include <spdlog/spdlog.h>
#include <vulkan/vulkan_enums.hpp>
#include <vulkan/vulkan_raii.hpp>
static const std::string discover_service = "_wivrn._tcp.local.";
scenes::lobby::~lobby() {}
scenes::lobby::lobby() :
status_string_rasterizer(device, physical_device, commandpool, queue), renderer(device, physical_device, queue)
status_string_rasterizer(device, physical_device, commandpool, queue)//, renderer(device, physical_device, queue)
{
// renderer.load_gltf("Lobby.gltf");
uint32_t width = swapchains[0].width();
uint32_t height = swapchains[0].height();
// Create renderpass
VkAttachmentReference color_ref{.attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL};
vk::RenderPassCreateInfo renderpass_info;
vk::renderpass::info renderpass_info{.attachments = {VkAttachmentDescription{
.format = swapchains[0].format(),
.samples = VK_SAMPLE_COUNT_1_BIT,
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
}},
.subpasses = {VkSubpassDescription{
.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
.colorAttachmentCount = 1,
.pColorAttachments = &color_ref,
}},
.dependencies = {}};
vk::AttachmentDescription attachment_desc{
.format = swapchains[0].format(),
.samples = vk::SampleCountFlagBits::e1,
.loadOp = vk::AttachmentLoadOp::eClear,
.storeOp = vk::AttachmentStoreOp::eStore,
.initialLayout = vk::ImageLayout::eUndefined,
.finalLayout = vk::ImageLayout::eColorAttachmentOptimal,
};
renderpass_info.setAttachments(attachment_desc);
renderpass = vk::renderpass(device, renderpass_info);
vk::SubpassDescription subpass_desc{
.pipelineBindPoint = vk::PipelineBindPoint::eGraphics,
};
vk::AttachmentReference color_ref{
.attachment = 0,
.layout = vk::ImageLayout::eColorAttachmentOptimal,
};
subpass_desc.setColorAttachments(color_ref);
renderpass_info.setSubpasses(subpass_desc);
VkSamplerCreateInfo sampler_info{
.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
.magFilter = VK_FILTER_LINEAR,
.minFilter = VK_FILTER_LINEAR,
.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER,
.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER,
.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER,
.mipLodBias = 0.0f,
.anisotropyEnable = VK_FALSE,
.maxAnisotropy = 1,
.compareEnable = VK_FALSE,
.compareOp = VK_COMPARE_OP_NEVER,
.minLod = 0.0f,
.maxLod = 0.0f,
.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK,
.unnormalizedCoordinates = VK_FALSE,
renderpass = vk::raii::RenderPass(device, renderpass_info);
vk::SamplerCreateInfo sampler_info{
.magFilter = vk::Filter::eLinear,
.minFilter = vk::Filter::eLinear,
.mipmapMode = vk::SamplerMipmapMode::eNearest,
.addressModeU = vk::SamplerAddressMode::eClampToBorder,
.addressModeV = vk::SamplerAddressMode::eClampToBorder,
.addressModeW = vk::SamplerAddressMode::eClampToBorder,
.mipLodBias = 0.0f,
.anisotropyEnable = VK_FALSE,
.maxAnisotropy = 1,
.compareEnable = VK_FALSE,
.compareOp = vk::CompareOp::eNever,
.minLod = 0.0f,
.maxLod = 0.0f,
.borderColor = vk::BorderColor::eFloatOpaqueBlack,
.unnormalizedCoordinates = VK_FALSE,
};
CHECK_VK(vkCreateSampler(device, &sampler_info, nullptr, &status_string_sampler));
status_string_sampler = vk::raii::Sampler(device, sampler_info);
// Create VkDescriptorSetLayout
VkDescriptorSetLayoutBinding layout_binding{
.binding = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
// Create descriptor set layout
vk::DescriptorSetLayoutBinding layout_binding{
.binding = 0,
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eFragment,
};
VkDescriptorSetLayoutCreateInfo layout_info{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.bindingCount = 1,
.pBindings = &layout_binding,
vk::DescriptorSetLayoutCreateInfo layout_info;
layout_info.setBindings(layout_binding);
status_string_image_descriptor_set_layout = vk::raii::DescriptorSetLayout(device, layout_info);
vk::DescriptorPoolSize status_string_image_descriptor_set_pool_size{
.type = vk::DescriptorType::eCombinedImageSampler,
.descriptorCount = 1,
};
CHECK_VK(
vkCreateDescriptorSetLayout(device, &layout_info, nullptr, &status_string_image_descriptor_set_layout));
VkDescriptorPoolSize status_string_image_descriptor_set_pool_size{
.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = 1,
vk::DescriptorPoolCreateInfo pool_info{
.maxSets = 1,
};
pool_info.setPoolSizes(status_string_image_descriptor_set_pool_size);
VkDescriptorPoolCreateInfo pool_info{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.flags = 0,
.maxSets = 1,
.poolSizeCount = 1,
.pPoolSizes = &status_string_image_descriptor_set_pool_size,
};
CHECK_VK(vkCreateDescriptorPool(device, &pool_info, nullptr, &status_string_descriptor_pool));
status_string_descriptor_pool = vk::raii::DescriptorPool(device, pool_info);
VkDescriptorSetAllocateInfo ds_info{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.descriptorPool = status_string_descriptor_pool,
.descriptorSetCount = 1,
.pSetLayouts = &status_string_image_descriptor_set_layout,
vk::DescriptorSetAllocateInfo ds_info{
.descriptorPool = *status_string_descriptor_pool,
.descriptorSetCount = 1,
};
CHECK_VK(vkAllocateDescriptorSets(device, &ds_info, &status_string_image_descriptor_set));
ds_info.setSetLayouts(*status_string_image_descriptor_set_layout);
status_string_image_descriptor_set = device.allocateDescriptorSets(ds_info)[0].release();
vk::PipelineLayoutCreateInfo pipeline_layout_info;
pipeline_layout_info.setSetLayouts(*status_string_image_descriptor_set_layout);
vk::PushConstantRange push_constant_range{
.stageFlags = vk::ShaderStageFlagBits::eVertex,
.offset = 0,
.size = sizeof(glm::mat4),
};
pipeline_layout_info.setPushConstantRanges(push_constant_range);
layout = vk::raii::PipelineLayout(device, pipeline_layout_info);
// Create graphics pipeline
vk::shader vertex_shader(device, "text.vert");
vk::shader fragment_shader(device, "text.frag");
vk::raii::ShaderModule vertex_shader = load_shader(device, "text.vert");
vk::raii::ShaderModule fragment_shader = load_shader(device, "text.frag");
vk::pipeline::graphics_info pipeline_info;
vk::pipeline_builder pipeline_info
{
.flags = {},
.Stages = {{
.stage = vk::ShaderStageFlagBits::eVertex,
.module = *vertex_shader,
.pName = "main",
},{
.stage = vk::ShaderStageFlagBits::eFragment,
.module = *fragment_shader,
.pName = "main",
}},
.VertexInputState = {.flags = {}},
.VertexBindingDescriptions = {},
.VertexAttributeDescriptions = {},
.InputAssemblyState = {{
.topology = vk::PrimitiveTopology::eTriangleList,
}},
.ViewportState = {.flags = {}},
.Viewports = {{
.x = 0,
.y = 0,
.width = (float)width,
.height = (float)height,
.minDepth = 0,
.maxDepth = 1,
}},
.Scissors = {{
.offset = { .x = 0, .y = 0 },
.extent= { .width = width, .height = height },
}},
.RasterizationState = {{
.polygonMode = vk::PolygonMode::eFill,
.lineWidth = 1,
}},
.MultisampleState = {{
.rasterizationSamples = vk::SampleCountFlagBits::e1,
}},
.ColorBlendState = {.flags = {}},
.ColorBlendAttachments = {{
.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB
}},
.layout = *layout,
.renderPass = *renderpass,
.subpass = 0,
};
pipeline_info.shader_stages.resize(2);
pipeline_info.shader_stages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
pipeline_info.shader_stages[0].module = vertex_shader;
pipeline_info.shader_stages[0].pName = "main";
pipeline_info.shader_stages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
pipeline_info.shader_stages[1].module = fragment_shader;
pipeline_info.shader_stages[1].pName = "main";
pipeline_info.InputAssemblyState.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
pipeline_info.viewports.push_back(VkViewport{0, 0, (float)width, (float)height, 0.0f, 1.0f});
pipeline_info.scissors.push_back(VkRect2D{{0, 0}, {width, height}});
pipeline_info.RasterizationState.polygonMode = VK_POLYGON_MODE_FILL;
pipeline_info.RasterizationState.lineWidth = 1;
pipeline_info.MultisampleState.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
VkPipelineColorBlendAttachmentState pcbas{};
pcbas.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT;
pipeline_info.ColorBlendState.attachmentCount = 1;
pipeline_info.ColorBlendState.pAttachments = &pcbas;
pipeline_info.renderPass = renderpass;
pipeline_info.subpass = 0;
layout = vk::pipeline_layout(
device, {.descriptor_set_layouts = {status_string_image_descriptor_set_layout}, .push_constant_ranges = {VkPushConstantRange{.stageFlags = VK_SHADER_STAGE_VERTEX_BIT, .offset = 0, .size = sizeof(glm::mat4)}}});
pipeline = vk::pipeline(device, pipeline_info, layout);
pipeline = vk::raii::Pipeline(device, nullptr, pipeline_info);
images_data.resize(swapchains.size());
for (size_t i = 0; i < swapchains.size(); i++)
{
auto & images = swapchains[i].images();
images_data[i].resize(images.size());
images_data[i].reserve(images.size());
for (size_t j = 0; j < images.size(); j++)
{
VkFramebufferCreateInfo fb_create_info{};
fb_create_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
fb_create_info.renderPass = renderpass;
fb_create_info.attachmentCount = 1;
fb_create_info.pAttachments = &images[j].view;
fb_create_info.width = swapchains[i].width();
fb_create_info.height = swapchains[i].height();
fb_create_info.layers = 1;
CHECK_VK(vkCreateFramebuffer(device, &fb_create_info, nullptr, &images_data[i][j].framebuffer));
vk::FramebufferCreateInfo fb_create_info{
.renderPass = *renderpass,
.attachmentCount = 1,
.pAttachments = &*images[j].view,
.width = swapchains[i].width(),
.height = swapchains[i].height(),
.layers = 1,
};
images_data[i][j].render_finished = create_semaphore();
images_data[i].emplace_back(image_data{
.framebuffer = vk::raii::Framebuffer(device, fb_create_info),
.render_finished = create_semaphore()
});
}
}
command_buffer = commandpool.allocate_command_buffer();
vk::CommandBufferAllocateInfo alloc_info;
alloc_info.commandBufferCount = 1;
alloc_info.level = vk::CommandBufferLevel::ePrimary;
alloc_info.commandPool = *commandpool;
command_buffer = std::move(device.allocateCommandBuffers(alloc_info)[0]);
application::set_debug_reports_name((VkCommandBuffer)*command_buffer, "lobby command buffer");
fence = create_fence(false);
}
void scenes::lobby::rasterize_status_string()
{
if (status_string_image_view)
vkDestroyImageView(device, status_string_image_view, nullptr);
status_string_rasterized_text = status_string_rasterizer.render(status_string);
assert(status_string_rasterized_text.image != VK_NULL_HANDLE);
assert(status_string_rasterized_text.memory != VK_NULL_HANDLE);
VkImageViewCreateInfo iv_info{.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.image = status_string_rasterized_text.image,
.viewType = VK_IMAGE_VIEW_TYPE_2D,
.format = status_string_rasterized_text.format,
.components = {VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY},
.subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1}};
CHECK_VK(vkCreateImageView(device, &iv_info, nullptr, &status_string_image_view));
VkDescriptorImageInfo image_info{
.sampler = status_string_sampler,
.imageView = status_string_image_view,
.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
vk::ImageViewCreateInfo iv_info{
.image = (VkImage)status_string_rasterized_text.image,
.viewType = vk::ImageViewType::e2D,
.format = status_string_rasterized_text.format,
.components = {
.r = vk::ComponentSwizzle::eIdentity,
.g = vk::ComponentSwizzle::eIdentity,
.b = vk::ComponentSwizzle::eIdentity,
.a = vk::ComponentSwizzle::eIdentity,
},
.subresourceRange = {
.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
},
};
VkWriteDescriptorSet descriptor_write{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = status_string_image_descriptor_set,
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.pImageInfo = &image_info,
status_string_image_view = vk::raii::ImageView(device, iv_info);
vk::DescriptorImageInfo image_info{
.sampler = *status_string_sampler,
.imageView = *status_string_image_view,
.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
};
vkUpdateDescriptorSets(device, 1, &descriptor_write, 0, nullptr);
vk::WriteDescriptorSet descriptor_write{
.dstSet = status_string_image_descriptor_set,
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
.pImageInfo = &image_info,
};
device.updateDescriptorSets(descriptor_write, {});
last_status_string = status_string;
}
@@ -315,11 +354,9 @@ void scenes::lobby::render()
session.begin_frame();
CHECK_VK(vkResetCommandBuffer(command_buffer, 0));
command_buffer.reset();
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
CHECK_VK(vkBeginCommandBuffer(command_buffer, &beginInfo));
command_buffer.begin(vk::CommandBufferBeginInfo{});
auto [flags, views] = session.locate_views(viewconfig, framestate.predictedDisplayTime, world_space);
assert(views.size() == swapchains.size());
@@ -343,14 +380,12 @@ void scenes::lobby::render()
layer_view[swapchain_index].subImage.imageRect.extent.height = swapchains[swapchain_index].height();
}
CHECK_VK(vkEndCommandBuffer(command_buffer));
command_buffer.end();
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &command_buffer;
vk::SubmitInfo submit_info;
submit_info.setCommandBuffers(*command_buffer);
CHECK_VK(vkQueueSubmit(queue, 1, &submitInfo, fence));
queue.submit(submit_info, *fence);
XrCompositionLayerProjection layer{
.type = XR_TYPE_COMPOSITION_LAYER_PROJECTION,
@@ -365,8 +400,8 @@ void scenes::lobby::render()
session.end_frame(framestate.predictedDisplayTime, layers_base);
CHECK_VK(vkWaitForFences(device, 1, &fence, VK_TRUE, UINT64_MAX));
CHECK_VK(vkResetFences(device, 1, &fence));
device.waitForFences(*fence, VK_TRUE, UINT64_MAX);
device.resetFences(*fence);
}
void scenes::lobby::render_view(XrViewStateFlags flags, XrTime display_time, XrView & view, int swapchain_index, int image_index)
@@ -374,22 +409,22 @@ void scenes::lobby::render_view(XrViewStateFlags flags, XrTime display_time, XrV
xr::swapchain & swapchain = swapchains[swapchain_index];
image_data & data = images_data[swapchain_index][image_index];
VkClearValue clearColor = {{{0.0f, 0.0f, 0.0f, 1.0f}}};
vk::ClearValue clear_color;
clear_color.color = {0.0f, 0.0f, 0.0f, 1.0f};
VkRenderPassBeginInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = renderpass;
renderPassInfo.framebuffer = data.framebuffer;
renderPassInfo.renderArea.offset = {0, 0};
renderPassInfo.renderArea.extent = {swapchain.width(), swapchain.height()};
renderPassInfo.clearValueCount = 1;
renderPassInfo.pClearValues = &clearColor;
vk::RenderPassBeginInfo renderpass_info{
.renderPass = *renderpass,
.framebuffer = *data.framebuffer,
.renderArea = {
.offset = {0, 0},
.extent = {swapchain.width(), swapchain.height()},
}
};
renderpass_info.setClearValues(clear_color);
vkCmdBeginRenderPass(command_buffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, layout, 0, 1, &status_string_image_descriptor_set, 0, nullptr);
command_buffer.beginRenderPass(renderpass_info, vk::SubpassContents::eInline);
command_buffer.bindPipeline(vk::PipelineBindPoint::eGraphics, *pipeline);
command_buffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, *layout, 0, status_string_image_descriptor_set, {});
float zn = 0.1;
float r = tan(view.fov.angleRight);
@@ -420,11 +455,10 @@ void scenes::lobby::render_view(XrViewStateFlags flags, XrTime display_time, XrV
{-0.5 * aspect_ratio, -0.5, -10, 1}};
glm::mat4 mvp = proj * glm::inverse(view_matrix) * model_matrix;
vkCmdPushConstants(command_buffer, layout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::mat4), &mvp);
vkCmdDraw(command_buffer, 6, 1, 0, 0);
vkCmdEndRenderPass(command_buffer);
command_buffer.pushConstants<glm::mat4>(*layout, vk::ShaderStageFlagBits::eVertex, 0, mvp);
command_buffer.draw(6, 1, 0, 0);
command_buffer.endRenderPass();
}
void scenes::lobby::on_focused()
+17 -19
View File
@@ -20,9 +20,7 @@
#pragma once
#include "scene.h"
#include "vk/buffer.h"
#include "vk/image.h"
#include "vk/renderpass.h"
#include <vulkan/vulkan_raii.hpp>
#include "wivrn_discover.h"
#include <tiny_gltf.h>
@@ -39,25 +37,25 @@ class lobby : public scene
{
std::string status_string;
std::string last_status_string;
VkSampler status_string_sampler{};
vk::raii::Sampler status_string_sampler = nullptr;
text status_string_rasterized_text;
VkImageView status_string_image_view{};
VkDescriptorPool status_string_descriptor_pool{};
VkDescriptorSetLayout status_string_image_descriptor_set_layout{};
VkDescriptorSet status_string_image_descriptor_set{};
vk::raii::ImageView status_string_image_view = nullptr;
vk::raii::DescriptorPool status_string_descriptor_pool = nullptr;
vk::raii::DescriptorSetLayout status_string_image_descriptor_set_layout = nullptr;
vk::DescriptorSet status_string_image_descriptor_set;
text_rasterizer status_string_rasterizer;
vk::renderpass renderpass;
vk::pipeline_layout layout;
vk::pipeline pipeline;
vk::raii::RenderPass renderpass = nullptr;
vk::raii::PipelineLayout layout = nullptr;
vk::raii::Pipeline pipeline = nullptr;
VkFence fence{};
VkCommandBuffer command_buffer{};
vk::raii::Fence fence = nullptr;
vk::raii::CommandBuffer command_buffer = nullptr;
struct image_data
{
VkFramebuffer framebuffer{};
VkSemaphore render_finished{};
vk::raii::Framebuffer framebuffer;
vk::raii::Semaphore render_finished;
};
std::vector<std::vector<image_data>> images_data;
@@ -65,15 +63,15 @@ class lobby : public scene
std::optional<wivrn_discover> discover;
tinygltf::Model model;
std::vector<vk::device_memory> model_memory;
std::vector<vk::buffer> model_buffers;
std::vector<vk::image> model_images;
std::vector<vk::DeviceMemory> model_memory;
std::vector<vk::Buffer> model_buffers;
std::vector<vk::Image> model_images;
void load_model(const std::string & filename);
std::shared_ptr<stream> next_scene;
scene_renderer renderer;
// scene_renderer renderer;
void rasterize_status_string();
+96 -115
View File
@@ -17,6 +17,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <vulkan/vulkan.hpp>
#include <vulkan/vulkan_enums.hpp>
#include <vulkan/vulkan_raii.hpp>
#define GLM_FORCE_RADIANS
#include "stream.h"
@@ -30,7 +33,6 @@
#include "utils/ranges.h"
#include "utils/sync_queue.h"
#include "vk/device_memory.h"
#include "vk/image.h"
#include "wivrn_packets.h"
#include "xr/details/enumerate.h"
#include <algorithm>
@@ -111,7 +113,10 @@ std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_ses
self->video_thread = std::thread(&stream::video, self.get());
pthread_setname_np(self->video_thread.native_handle(), "video_thread");
self->command_buffer = self->commandpool.allocate_command_buffer();
self->command_buffer = std::move(self->device.allocateCommandBuffers({
.commandPool = *self->commandpool,
.level = vk::CommandBufferLevel::ePrimary,
.commandBufferCount = 1})[0]);
self->fence = self->create_fence(false);
// Look up the XrActions for haptics
@@ -258,38 +263,33 @@ void scenes::stream::render()
image_indices[swapchain_index] = image_index;
}
CHECK_VK(vkResetCommandBuffer(command_buffer, 0));
command_buffer.reset();
VkCommandBufferBeginInfo begin_info{
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
};
CHECK_VK(vkBeginCommandBuffer(command_buffer, &begin_info));
vk::CommandBufferBeginInfo begin_info;
begin_info.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit;
command_buffer.begin(begin_info);
// Transition the layout of the decoder framebuffer to the one the decoders expect
std::vector<VkImageMemoryBarrier> image_barriers;
std::vector<vk::ImageMemoryBarrier> image_barriers;
for (size_t i = 0; i < decoder_output.size(); i++)
{
image_barriers.push_back(VkImageMemoryBarrier{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.srcAccessMask = 0,
.dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.newLayout = shard_accumulator::framebuffer_expected_layout,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = decoder_output[i].image,
.subresourceRange = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
},
});
}
vk::ImageMemoryBarrier barrier;
barrier.srcAccessMask = vk::AccessFlagBits::eNone;
barrier.dstAccessMask = vk::AccessFlagBits::eShaderRead;
barrier.oldLayout = vk::ImageLayout::eUndefined;
barrier.newLayout = shard_accumulator::framebuffer_expected_layout;
barrier.srcQueueFamilyIndex = vk::QueueFamilyIgnored;
barrier.dstQueueFamilyIndex = vk::QueueFamilyIgnored;
barrier.image = (vk::Image)decoder_output[i].image;
barrier.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
barrier.subresourceRange.baseMipLevel = 0;
barrier.subresourceRange.levelCount = 1;
barrier.subresourceRange.baseArrayLayer = 0;
barrier.subresourceRange.layerCount = 1;
vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, nullptr, 0, nullptr, image_barriers.size(), image_barriers.data());
image_barriers.push_back(barrier);
}
command_buffer.pipelineBarrier(vk::PipelineStageFlagBits::eAllCommands, vk::PipelineStageFlagBits::eAllCommands, vk::DependencyFlags{}, {}, {}, image_barriers);
// Keep a reference to the resources needed to blit the images until vkWaitForFences
std::vector<std::shared_ptr<shard_accumulator::blit_handle>> current_blit_handles;
@@ -330,40 +330,35 @@ void scenes::stream::render()
image_barriers.clear();
for (size_t i = 0; i < decoder_output.size(); i++)
{
image_barriers.push_back(VkImageMemoryBarrier{
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.srcAccessMask = 0,
.dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
.oldLayout = shard_accumulator::framebuffer_expected_layout,
.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = decoder_output[i].image,
.subresourceRange = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
},
});
vk::ImageMemoryBarrier barrier;
barrier.srcAccessMask = vk::AccessFlagBits::eNone;
barrier.dstAccessMask = vk::AccessFlagBits::eShaderRead;
barrier.oldLayout = shard_accumulator::framebuffer_expected_layout;
barrier.newLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
barrier.srcQueueFamilyIndex = vk::QueueFamilyIgnored;
barrier.dstQueueFamilyIndex = vk::QueueFamilyIgnored;
barrier.image = (vk::Image)decoder_output[i].image;
barrier.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
barrier.subresourceRange.baseMipLevel = 0;
barrier.subresourceRange.levelCount = 1;
barrier.subresourceRange.baseArrayLayer = 0;
barrier.subresourceRange.layerCount = 1;
image_barriers.push_back(barrier);
}
vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, image_barriers.size(), image_barriers.data());
command_buffer.pipelineBarrier(vk::PipelineStageFlagBits::eColorAttachmentOutput, vk::PipelineStageFlagBits::eFragmentShader, vk::DependencyFlags{}, {}, {}, image_barriers);
// Reproject the image to the real pose
for (size_t view = 0; view < view_count; view++)
{
size_t destination_index = view * swapchains[0].images().size() + image_indices[view];
reprojector.reproject(command_buffer, view, destination_index, pose[view].orientation, fov[view], views[view].pose.orientation, views[view].fov);
reprojector->reproject(command_buffer, view, destination_index, pose[view].orientation, fov[view], views[view].pose.orientation, views[view].fov);
}
CHECK_VK(vkEndCommandBuffer(command_buffer));
VkSubmitInfo submit_info{
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
.commandBufferCount = 1,
.pCommandBuffers = &command_buffer,
};
CHECK_VK(vkQueueSubmit(queue, 1, &submit_info, fence));
command_buffer.end();
vk::SubmitInfo submit_info;
submit_info.setCommandBuffers(*command_buffer);
queue.submit(submit_info, *fence);
std::vector<XrCompositionLayerBaseHeader *> layers_base;
std::vector<XrCompositionLayerProjectionView> layer_view;
@@ -405,8 +400,8 @@ void scenes::stream::render()
layers_base.push_back(reinterpret_cast<XrCompositionLayerBaseHeader *>(&layer));
session.end_frame(/*timestamp*/ framestate.predictedDisplayTime, layers_base);
CHECK_VK(vkWaitForFences(device, 1, &fence, VK_TRUE, UINT64_MAX));
CHECK_VK(vkResetFences(device, 1, &fence));
device.waitForFences(*fence, VK_TRUE, UINT64_MAX);
device.resetFences(*fence);
// We don't need those after vkWaitForFences
current_blit_handles.clear();
@@ -426,22 +421,6 @@ void scenes::stream::cleanup()
// Assumes decoder_mutex is locked
ready_ = false;
decoders.clear();
for (auto & output: decoder_output)
{
if (output.image)
vkDestroyImage(device, output.image, nullptr);
if (output.image_view)
vkDestroyImageView(device, output.image_view, nullptr);
if (output.memory)
vkFreeMemory(device, output.memory, nullptr);
output.image = VK_NULL_HANDLE;
output.image_view = VK_NULL_HANDLE;
output.memory = VK_NULL_HANDLE;
}
}
void scenes::stream::setup(const to_headset::video_stream_description & description)
@@ -463,44 +442,44 @@ void scenes::stream::setup(const to_headset::video_stream_description & descript
VkExtent3D decoder_out_size{width, height, 1};
for (size_t i = 0; i < view_count; i++)
{
decoder_output[i].format = VK_FORMAT_A8B8G8R8_SRGB_PACK32;
decoder_output[i].size = {
.width = width,
.height = height};
decoder_output[i].format = vk::Format::eA8B8G8R8SrgbPack32;
decoder_output[i].size.width = width;
decoder_output[i].size.height = height;
VkImageCreateInfo image_info{
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
.flags = 0,
.imageType = VK_IMAGE_TYPE_2D,
.format = VK_FORMAT_A8B8G8R8_SRGB_PACK32,
.extent = decoder_out_size,
.mipLevels = 1,
.arrayLayers = 1,
.samples = VK_SAMPLE_COUNT_1_BIT,
.tiling = VK_IMAGE_TILING_OPTIMAL,
.usage = VK_IMAGE_USAGE_SAMPLED_BIT | shard_accumulator::framebuffer_usage,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
};
vk::ImageCreateInfo image_info;
image_info.flags = vk::ImageCreateFlags{};
image_info.imageType = vk::ImageType::e2D;
image_info.format = vk::Format::eA8B8G8R8SrgbPack32;
image_info.extent = decoder_out_size;
image_info.mipLevels = 1;
image_info.arrayLayers = 1;
image_info.samples = vk::SampleCountFlagBits::e1;
image_info.tiling = vk::ImageTiling::eOptimal;
image_info.usage = vk::ImageUsageFlagBits::eSampled | shard_accumulator::framebuffer_usage;
image_info.sharingMode = vk::SharingMode::eExclusive;
image_info.initialLayout = vk::ImageLayout::eUndefined;
decoder_output[i].image = vk::image{device, image_info}.release();
VmaAllocationCreateInfo alloc_info{};
alloc_info.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
decoder_output[i].memory = vk::device_memory{device, physical_device, decoder_output[i].image, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT}.release();
decoder_output[i].image = image_allocation{image_info, alloc_info};
VkImageViewCreateInfo image_view_info{
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.image = decoder_output[i].image,
.viewType = VK_IMAGE_VIEW_TYPE_2D,
.format = VK_FORMAT_A8B8G8R8_SRGB_PACK32,
.components = {}, // IDENTITY by default
.subresourceRange = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1}};
vk::ImageViewCreateInfo image_view_info;
CHECK_VK(vkCreateImageView(device, &image_view_info, nullptr, &decoder_output[i].image_view));
image_view_info.image = (VkImage)decoder_output[i].image;
image_view_info.viewType = vk::ImageViewType::e2D;
image_view_info.format = vk::Format::eA8B8G8R8SrgbPack32;
image_view_info.components.r = vk::ComponentSwizzle::eIdentity;
image_view_info.components.g = vk::ComponentSwizzle::eIdentity;
image_view_info.components.b = vk::ComponentSwizzle::eIdentity;
image_view_info.components.a = vk::ComponentSwizzle::eIdentity;
image_view_info.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
image_view_info.subresourceRange.baseMipLevel = 0;
image_view_info.subresourceRange.levelCount = 1;
image_view_info.subresourceRange.baseArrayLayer = 0;
image_view_info.subresourceRange.layerCount = 1;
decoder_output[i].image_view = vk::raii::ImageView(device, image_view_info);
}
std::vector<shard_accumulator::blit_target> blit_targets;
@@ -508,10 +487,12 @@ void scenes::stream::setup(const to_headset::video_stream_description & descript
for (size_t i = 0; i < view_count; i++)
{
blit_targets[i].image = decoder_output[i].image;
blit_targets[i].image_view = decoder_output[i].image_view;
blit_targets[i].extent = {width, height};
blit_targets[i].offset = {static_cast<int>(width * i), 0};
blit_targets[i].image = (VkImage)decoder_output[i].image;
blit_targets[i].image_view = *decoder_output[i].image_view;
blit_targets[i].extent.width = width;
blit_targets[i].extent.height = height;
blit_targets[i].offset.x = width * i;
blit_targets[i].offset.y = 0;
}
for (const auto & [stream_index, item]: utils::enumerate(description.items))
@@ -520,27 +501,27 @@ void scenes::stream::setup(const to_headset::video_stream_description & descript
accumulator_images dec;
dec.decoder = std::make_unique<shard_accumulator>(device, physical_device, item, description.fps, shared_from_this(), stream_index);
dec.decoder->set_blit_targets(blit_targets, VK_FORMAT_A8B8G8R8_SRGB_PACK32);
dec.decoder->set_blit_targets(blit_targets, vk::Format::eA8B8G8R8SrgbPack32);
decoders.push_back(std::move(dec));
}
spdlog::info("Initializing reprojector");
VkExtent2D extent = {swapchains[0].width(), swapchains[0].height()};
std::vector<VkImage> swapchain_images;
vk::Extent2D extent = {swapchains[0].width(), swapchains[0].height()};
std::vector<vk::Image> swapchain_images;
for (auto & swapchain: swapchains)
{
for (auto & image: swapchain.images())
swapchain_images.push_back(image.image);
}
std::vector<VkImage> images;
std::vector<vk::Image> images;
for (renderpass_output & i: decoder_output)
{
images.push_back(i.image);
images.push_back((vk::Image)i.image);
}
reprojector.init(device, physical_device, images, swapchain_images, extent, swapchains[0].format(), description);
reprojector.emplace(device, physical_device, images, swapchain_images, extent, swapchains[0].format(), description);
}
void scenes::stream::video()
+8 -9
View File
@@ -51,11 +51,10 @@ class stream : public scene, public std::enable_shared_from_this<stream>
struct renderpass_output
{
VkExtent2D size;
VkFormat format;
VkImage image;
VkDeviceMemory memory;
VkImageView image_view;
vk::Extent2D size;
vk::Format format;
image_allocation image;
vk::raii::ImageView image_view = nullptr;
};
std::unique_ptr<wivrn_session> network_session;
@@ -71,10 +70,10 @@ class stream : public scene, public std::enable_shared_from_this<stream>
std::array<renderpass_output, view_count> decoder_output{};
stream_reprojection reprojector;
std::optional<stream_reprojection> reprojector;
VkFence fence;
VkCommandBuffer command_buffer;
vk::raii::Fence fence = nullptr;
vk::raii::CommandBuffer command_buffer = nullptr;
std::array<std::pair<XrAction, XrPath>, 2> haptics_actions;
std::vector<std::tuple<device_id, XrAction, XrActionType>> input_actions;
@@ -103,7 +102,7 @@ public:
void operator()(to_headset::video_stream_description &&);
void operator()(audio_data&&);
VkFormat swapchain_format()
vk::Format swapchain_format()
{
return swapchains[0].format();
}
+1
View File
@@ -18,6 +18,7 @@
*/
#include "stream.h"
#include "application.h"
void scenes::stream::read_actions()
{
+1
View File
@@ -18,6 +18,7 @@
*/
#include "stream.h"
#include <spdlog/spdlog.h>
void scenes::stream::process_packets()
{
+266 -266
View File
@@ -18,12 +18,16 @@
*/
#include "stream_reprojection.h"
#include "spdlog/spdlog.h"
#include "utils/check.h"
#include "vk/allocation.h"
#include "vk/shader.h"
#include "vk/pipeline.h"
#include <spdlog/spdlog.h>
#include <array>
#include <glm/glm.hpp>
#include <glm/gtx/quaternion.hpp>
#include <vk_mem_alloc.h>
#include <vulkan/vulkan_core.h>
#include <vulkan/vulkan_raii.hpp>
struct stream_reprojection::uniform
{
@@ -36,311 +40,312 @@ struct stream_reprojection::uniform
alignas(8) glm::vec2 xc;
};
stream_reprojection::~stream_reprojection()
stream_reprojection::stream_reprojection(vk::raii::Device& device, vk::raii::PhysicalDevice& physical_device, std::vector<vk::Image> input_images_, std::vector<vk::Image> output_images_, vk::Extent2D extent, vk::Format format, const xrt::drivers::wivrn::to_headset::video_stream_description & description) :
input_images(std::move(input_images_)),
output_images(std::move(output_images_)),
extent(extent)
{
cleanup();
}
void stream_reprojection::cleanup()
{
if (device)
{
if (descriptor_pool)
vkDestroyDescriptorPool(device, descriptor_pool, nullptr);
if (descriptor_set_layout)
vkDestroyDescriptorSetLayout(device, descriptor_set_layout, nullptr);
for (VkFramebuffer framebuffer: framebuffers)
vkDestroyFramebuffer(device, framebuffer, nullptr);
for (VkImageView image_view: output_image_views)
vkDestroyImageView(device, image_view, nullptr);
for (VkImageView image_view: input_image_views)
vkDestroyImageView(device, image_view, nullptr);
if (sampler)
vkDestroySampler(device, sampler, nullptr);
descriptor_pool = VK_NULL_HANDLE;
descriptor_set_layout = VK_NULL_HANDLE;
sampler = VK_NULL_HANDLE;
framebuffers.clear();
output_image_views.clear();
input_image_views.clear();
ubo.clear();
}
}
void stream_reprojection::init(VkDevice device, VkPhysicalDevice physical_device, std::vector<VkImage> input_images_, std::vector<VkImage> output_images_, VkExtent2D extent, VkFormat format, const xrt::drivers::wivrn::to_headset::video_stream_description & description)
{
cleanup();
this->device = device;
this->physical_device = physical_device,
this->input_images = std::move(input_images_);
this->output_images = std::move(output_images_);
this->extent = extent;
this->format = format;
foveation_parameters = description.foveation;
VkPhysicalDeviceProperties properties;
vkGetPhysicalDeviceProperties(physical_device, &properties);
vk::PhysicalDeviceProperties properties = physical_device.getProperties();
VkSamplerCreateInfo sampler_info{
.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
.magFilter = VK_FILTER_LINEAR,
.minFilter = VK_FILTER_LINEAR,
.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER,
.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER,
.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER,
.mipLodBias = 0.0f,
.anisotropyEnable = VK_FALSE,
.maxAnisotropy = 1,
.compareEnable = VK_FALSE,
.compareOp = VK_COMPARE_OP_NEVER,
.minLod = 0.0f,
.maxLod = 0.0f,
.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK,
.unnormalizedCoordinates = VK_FALSE,
vk::SamplerCreateInfo sampler_info{
.magFilter = vk::Filter::eLinear,
.minFilter = vk::Filter::eLinear,
.mipmapMode = vk::SamplerMipmapMode::eNearest,
.addressModeU = vk::SamplerAddressMode::eClampToBorder,
.addressModeV = vk::SamplerAddressMode::eClampToBorder,
.addressModeW = vk::SamplerAddressMode::eClampToBorder,
.mipLodBias = 0.0f,
.anisotropyEnable = VK_FALSE,
.maxAnisotropy = 1,
.compareEnable = VK_FALSE,
.compareOp = vk::CompareOp::eNever,
.minLod = 0.0f,
.maxLod = 0.0f,
.borderColor = vk::BorderColor::eFloatOpaqueBlack,
.unnormalizedCoordinates = VK_FALSE,
};
CHECK_VK(vkCreateSampler(device, &sampler_info, nullptr, &sampler));
sampler = vk::raii::Sampler(device, sampler_info);
size_t uniform_size = sizeof(uniform) + properties.limits.minUniformBufferOffsetAlignment - 1;
uniform_size = uniform_size - uniform_size % properties.limits.minUniformBufferOffsetAlignment;
VkBufferCreateInfo create_info{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = uniform_size * input_images.size(),
.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
vk::BufferCreateInfo create_info{
.size = uniform_size * input_images.size(),
.usage = vk::BufferUsageFlagBits::eUniformBuffer,
.sharingMode = vk::SharingMode::eExclusive,
};
buffer = vk::buffer(device, create_info);
memory = vk::device_memory(device, physical_device, buffer, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
memory.map_memory();
VmaAllocationCreateInfo alloc_info{
.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
};
buffer = buffer_allocation(create_info, alloc_info);
void * data = buffer.map();
for (size_t i = 0; i < input_images.size(); i++)
ubo.push_back(reinterpret_cast<uniform *>(reinterpret_cast<uintptr_t>(memory.data()) + i * uniform_size));
ubo.push_back(reinterpret_cast<uniform *>(reinterpret_cast<uintptr_t>(data) + i * uniform_size));
// Create VkDescriptorSetLayout
std::array<VkDescriptorSetLayoutBinding, 2> layout_binding{
VkDescriptorSetLayoutBinding{
.binding = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
std::array layout_binding{
vk::DescriptorSetLayoutBinding{
.binding = 0,
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eFragment,
},
vk::DescriptorSetLayoutBinding{
.binding = 1,
.descriptorType = vk::DescriptorType::eUniformBuffer,
.descriptorCount = 1,
.stageFlags = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment,
}
};
vk::DescriptorSetLayoutCreateInfo layout_info;
layout_info.setBindings(layout_binding);
descriptor_set_layout = vk::raii::DescriptorSetLayout(device, layout_info);
std::array pool_size{
vk::DescriptorPoolSize{
.type = vk::DescriptorType::eCombinedImageSampler,
.descriptorCount = (uint32_t)input_images.size(),
},
{
.binding = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
}};
VkDescriptorSetLayoutCreateInfo layout_info{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.bindingCount = layout_binding.size(),
.pBindings = layout_binding.data(),
vk::DescriptorPoolSize{
.type = vk::DescriptorType::eUniformBuffer,
.descriptorCount = (uint32_t)input_images.size(),
}
};
CHECK_VK(vkCreateDescriptorSetLayout(device, &layout_info, nullptr, &descriptor_set_layout));
vk::DescriptorPoolCreateInfo pool_info;
pool_info.flags = vk::DescriptorPoolCreateFlags{};
pool_info.maxSets = input_images.size();
pool_info.setPoolSizes(pool_size);
std::array<VkDescriptorPoolSize, 2> pool_size{VkDescriptorPoolSize{
.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = (uint32_t)input_images.size(),
},
VkDescriptorPoolSize{
.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = (uint32_t)input_images.size(),
}};
VkDescriptorPoolCreateInfo pool_info{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.flags = 0,
.maxSets = (uint32_t)input_images.size(),
.poolSizeCount = pool_size.size(),
.pPoolSizes = pool_size.data(),
};
CHECK_VK(vkCreateDescriptorPool(device, &pool_info, nullptr, &descriptor_pool));
descriptor_pool = vk::raii::DescriptorPool(device, pool_info);
// Create image views and descriptor sets
input_image_views.reserve(input_images.size());
descriptor_sets.reserve(input_images.size());
VkDeviceSize offset = 0;
for (VkImage image: input_images)
for (vk::Image image: input_images)
{
VkImageViewCreateInfo iv_info{
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.image = image,
.viewType = VK_IMAGE_VIEW_TYPE_2D,
.format = VK_FORMAT_A8B8G8R8_SRGB_PACK32,
.components = {VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY},
.subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1}};
VkImageView image_view;
CHECK_VK(vkCreateImageView(device, &iv_info, nullptr, &image_view));
input_image_views.push_back(image_view);
VkDescriptorSetAllocateInfo ds_info{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.descriptorPool = descriptor_pool,
.descriptorSetCount = 1,
.pSetLayouts = &descriptor_set_layout,
};
VkDescriptorSet ds;
CHECK_VK(vkAllocateDescriptorSets(device, &ds_info, &ds));
descriptor_sets.push_back(ds);
VkDescriptorImageInfo image_info{
.sampler = sampler,
.imageView = image_view,
.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
vk::ImageViewCreateInfo iv_info{
.image = image,
.viewType = vk::ImageViewType::e2D,
.format = vk::Format::eA8B8G8R8SrgbPack32,
.components = {
.r = vk::ComponentSwizzle::eIdentity,
.g = vk::ComponentSwizzle::eIdentity,
.b = vk::ComponentSwizzle::eIdentity,
.a = vk::ComponentSwizzle::eIdentity,
},
.subresourceRange = {
.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
}
};
VkDescriptorBufferInfo buffer_info{
.buffer = buffer, .offset = offset, .range = sizeof(uniform)};
input_image_views.emplace_back(device, iv_info);
vk::DescriptorSetAllocateInfo ds_info{
.descriptorPool = *descriptor_pool,
.descriptorSetCount = 1,
.pSetLayouts = &*descriptor_set_layout,
};
descriptor_sets.push_back(device.allocateDescriptorSets(ds_info)[0].release());
vk::DescriptorImageInfo image_info{
.sampler = *sampler,
.imageView = *input_image_views.back(),
.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
};
vk::DescriptorBufferInfo buffer_info{
.buffer = (vk::Buffer)buffer,
.offset = offset,
.range = sizeof(uniform),
};
offset += uniform_size;
std::array<VkWriteDescriptorSet, 2> write{
VkWriteDescriptorSet{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = ds,
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.pImageInfo = &image_info,
},
VkWriteDescriptorSet{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = ds,
.dstBinding = 1,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.pBufferInfo = &buffer_info},
std::array write{
vk::WriteDescriptorSet{
.dstSet = descriptor_sets.back(),
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
.pImageInfo = &image_info,
},
vk::WriteDescriptorSet{
.dstSet = descriptor_sets.back(),
.dstBinding = 1,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = vk::DescriptorType::eUniformBuffer,
.pBufferInfo = &buffer_info,
}
};
vkUpdateDescriptorSets(device, write.size(), write.data(), 0, nullptr);
device.updateDescriptorSets(write, {});
}
// Create renderpass
VkAttachmentReference color_ref{.attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL};
vk::AttachmentReference color_ref{
.attachment = 0,
.layout = vk::ImageLayout::eColorAttachmentOptimal,
};
vk::renderpass::info renderpass_info{.attachments = {VkAttachmentDescription{
.format = format,
.samples = VK_SAMPLE_COUNT_1_BIT,
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
}},
.subpasses = {VkSubpassDescription{
.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
.colorAttachmentCount = 1,
.pColorAttachments = &color_ref,
}},
.dependencies = {}};
vk::AttachmentDescription attachment{
.format = format,
.samples = vk::SampleCountFlagBits::e1,
.loadOp = vk::AttachmentLoadOp::eClear,
.storeOp = vk::AttachmentStoreOp::eStore,
.initialLayout = vk::ImageLayout::eColorAttachmentOptimal,
.finalLayout = vk::ImageLayout::eColorAttachmentOptimal,
};
vk::SubpassDescription subpass{
.pipelineBindPoint = vk::PipelineBindPoint::eGraphics,
};
subpass.setColorAttachments(color_ref);
vk::RenderPassCreateInfo renderpass_info;
renderpass_info.setAttachments(attachment);
renderpass_info.setSubpasses(subpass);
renderpass = vk::raii::RenderPass(device, renderpass_info);
renderpass = vk::renderpass(device, renderpass_info);
// Create graphics pipeline
vk::shader vertex_shader(device, "reprojection.vert");
vk::shader fragment_shader(device, "reprojection.frag");
vk::raii::ShaderModule vertex_shader = load_shader(device, "reprojection.vert");
vk::raii::ShaderModule fragment_shader = load_shader(device, "reprojection.frag");
VkPipelineColorBlendAttachmentState pcbas{.colorWriteMask = VK_COLOR_COMPONENT_R_BIT |
VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT};
layout = vk::pipeline_layout(device, {.descriptor_set_layouts = {descriptor_set_layout}});
vk::PipelineLayoutCreateInfo pipeline_layout_info;
pipeline_layout_info.setSetLayouts(*descriptor_set_layout);
layout = vk::raii::PipelineLayout(device, pipeline_layout_info);
VkBool32 use_foveation[] = {
foveation_parameters[0].x.scale < 1,
foveation_parameters[0].y.scale < 1
};
std::vector<VkSpecializationMapEntry> specialization_constants_desc = {
{.constantID = 0, .offset = 0, .size = sizeof(VkBool32)},
{.constantID = 1, .offset = sizeof(VkBool32), .size = sizeof(VkBool32)}
std::array specialization_constants_desc{
vk::SpecializationMapEntry{
.constantID = 0,
.offset = 0,
.size = sizeof(VkBool32),
},
vk::SpecializationMapEntry{
.constantID = 1,
.offset = sizeof(VkBool32),
.size = sizeof(VkBool32),
}
};
VkSpecializationInfo specialization_info{
.mapEntryCount = (uint32_t)specialization_constants_desc.size(),
.pMapEntries = specialization_constants_desc.data(),
.dataSize = sizeof(use_foveation),
.pData = use_foveation};
vk::SpecializationInfo specialization_info;
vk::pipeline::graphics_info pipeline_info{
.shader_stages =
{{.stage = VK_SHADER_STAGE_VERTEX_BIT, .module = vertex_shader, .pName = "main"},
{.stage = VK_SHADER_STAGE_FRAGMENT_BIT, .module = fragment_shader, .pName = "main", .pSpecializationInfo = &specialization_info}},
.vertex_input_bindings = {},
.vertex_input_attributes = {},
.InputAssemblyState = {.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP},
.viewports = {VkViewport{.x = 0,
.y = 0,
.width = (float)extent.width,
.height = (float)extent.height,
.minDepth = 0,
.maxDepth = 1}},
.scissors = {VkRect2D{
.offset = {0, 0},
.extent = extent,
}},
.RasterizationState = {.polygonMode = VK_POLYGON_MODE_FILL, .lineWidth = 1},
.MultisampleState = {.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT},
.ColorBlendState = {.attachmentCount = 1, .pAttachments = &pcbas},
.dynamic_states = {},
.renderPass = renderpass,
.subpass = 0,
specialization_info.setMapEntries(specialization_constants_desc);
specialization_info.setData<VkBool32>(use_foveation);
vk::pipeline_builder pipeline_info
{
.flags = {},
.Stages = {{
.stage = vk::ShaderStageFlagBits::eVertex,
.module = *vertex_shader,
.pName = "main",
},{
.stage = vk::ShaderStageFlagBits::eFragment,
.module = *fragment_shader,
.pName = "main",
.pSpecializationInfo = &specialization_info,
}},
.VertexInputState = {.flags = {}},
.VertexBindingDescriptions = {},
.VertexAttributeDescriptions = {},
.InputAssemblyState = {{
.topology = vk::PrimitiveTopology::eTriangleStrip,
}},
.ViewportState = {.flags = {}},
.Viewports = {{
.x = 0,
.y = 0,
.width = (float)extent.width,
.height = (float)extent.height,
.minDepth = 0,
.maxDepth = 1,
}},
.Scissors = {{
.offset = { .x = 0, .y = 0 },
.extent= extent,
}},
.RasterizationState = {{
.polygonMode = vk::PolygonMode::eFill,
.lineWidth = 1,
}},
.MultisampleState = {{
.rasterizationSamples = vk::SampleCountFlagBits::e1,
}},
.ColorBlendState = {.flags = {}},
.ColorBlendAttachments = {{
.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB
}},
.layout = *layout,
.renderPass = *renderpass,
.subpass = 0,
};
pipeline = vk::pipeline(device, pipeline_info, layout);
pipeline = vk::raii::Pipeline(device, nullptr, pipeline_info);
// Create image views and framebuffers
output_image_views.reserve(output_images.size());
framebuffers.reserve(output_images.size());
for (VkImage image: output_images)
for (vk::Image image: output_images)
{
VkImageViewCreateInfo iv_info{
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.image = image,
.viewType = VK_IMAGE_VIEW_TYPE_2D,
.format = format,
.components = {VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY},
.subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1}};
VkImageView image_view;
CHECK_VK(vkCreateImageView(device, &iv_info, nullptr, &image_view));
output_image_views.push_back(image_view);
VkFramebufferCreateInfo fb_create_info{
.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
.renderPass = renderpass,
.attachmentCount = 1,
.pAttachments = &image_view,
.width = extent.width,
.height = extent.height,
.layers = 1,
vk::ImageViewCreateInfo iv_info{
.image = image,
.viewType = vk::ImageViewType::e2D,
.format = format,
.components = {
.r = vk::ComponentSwizzle::eIdentity,
.g = vk::ComponentSwizzle::eIdentity,
.b = vk::ComponentSwizzle::eIdentity,
.a = vk::ComponentSwizzle::eIdentity,
},
.subresourceRange = {
.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
},
};
VkFramebuffer framebuffer;
CHECK_VK(vkCreateFramebuffer(device, &fb_create_info, nullptr, &framebuffer));
framebuffers.push_back(framebuffer);
output_image_views.emplace_back(device, iv_info);
vk::FramebufferCreateInfo fb_create_info{
.renderPass = *renderpass,
.width = extent.width,
.height = extent.height,
.layers = 1,
};
fb_create_info.setAttachments(*output_image_views.back());
framebuffers.emplace_back(device, fb_create_info);
}
}
void stream_reprojection::reproject(VkCommandBuffer command_buffer, int source, int destination, XrQuaternionf source_pose, XrFovf source_fov, XrQuaternionf dest_pose, XrFovf dest_fov)
void stream_reprojection::reproject(vk::raii::CommandBuffer& command_buffer, int source, int destination, XrQuaternionf source_pose, XrFovf source_fov, XrQuaternionf dest_pose, XrFovf dest_fov)
{
if (source < 0 || source >= (int)input_images.size())
throw std::runtime_error("Invalid source image index");
@@ -399,26 +404,21 @@ void stream_reprojection::reproject(VkCommandBuffer command_buffer, int source,
ubo[source]->lambda.y = foveation_parameters[source].y.a / foveation_parameters[source].y.scale;
ubo[source]->xc.y = foveation_parameters[source].y.center;
VkClearValue clear_color{};
vk::ClearValue clear_color;
VkRenderPassBeginInfo begin_info{
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
.renderPass = renderpass,
.framebuffer = framebuffers[destination],
.renderArea =
{
.offset = {0, 0},
.extent = extent,
},
.clearValueCount = 1,
.pClearValues = &clear_color,
vk::RenderPassBeginInfo begin_info{
.renderPass = *renderpass,
.framebuffer = *framebuffers[destination],
.renderArea = {
.offset = {0, 0},
.extent = extent,
},
};
begin_info.setClearValues(clear_color);
vkCmdBeginRenderPass(command_buffer, &begin_info, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, layout, 0, 1, &descriptor_sets[source], 0, nullptr);
vkCmdDraw(command_buffer, 3, 1, 0, 0);
vkCmdEndRenderPass(command_buffer);
command_buffer.beginRenderPass(begin_info, vk::SubpassContents::eInline);
command_buffer.bindPipeline(vk::PipelineBindPoint::eGraphics, *pipeline);
command_buffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, *layout, 0, descriptor_sets[source], {});
command_buffer.draw(3, 1, 0, 0);
command_buffer.endRenderPass();
}
+23 -37
View File
@@ -19,59 +19,45 @@
#pragma once
#include "vk/buffer.h"
#include "vk/device_memory.h"
#include "vk/pipeline.h"
#include "vk/pipeline_layout.h"
#include "vk/renderpass.h"
#include "vk/shader.h"
#include "vk/allocation.h"
#include "wivrn_packets.h"
#include <vulkan/vulkan_core.h>
#include <openxr/openxr.h>
#include <vulkan/vulkan_raii.hpp>
class stream_reprojection
{
struct uniform;
VkDevice device;
VkPhysicalDevice physical_device;
// Uniform buffer
vk::buffer buffer;
vk::device_memory memory;
std::vector<uniform *> ubo{};
// Source images
VkSampler sampler{};
std::vector<VkImage> input_images;
std::vector<VkImageView> input_image_views;
std::vector<VkDescriptorSet> descriptor_sets;
// Destination images
std::vector<VkImage> output_images;
std::vector<VkImageView> output_image_views;
std::vector<VkFramebuffer> framebuffers;
VkExtent2D extent{};
VkFormat format{};
buffer_allocation buffer;
std::vector<uniform *> ubo;
// Graphic pipeline
VkDescriptorSetLayout descriptor_set_layout{};
VkDescriptorPool descriptor_pool{};
vk::pipeline_layout layout;
vk::pipeline pipeline;
vk::renderpass renderpass;
vk::raii::DescriptorSetLayout descriptor_set_layout = nullptr;
vk::raii::DescriptorPool descriptor_pool = nullptr;
vk::raii::PipelineLayout layout = nullptr;
vk::raii::RenderPass renderpass = nullptr;
vk::raii::Pipeline pipeline = nullptr;
// Source images
vk::raii::Sampler sampler = nullptr;
std::vector<vk::Image> input_images;
std::vector<vk::raii::ImageView> input_image_views;
std::vector<vk::DescriptorSet> descriptor_sets;
// Destination images
std::vector<vk::Image> output_images;
std::vector<vk::raii::ImageView> output_image_views;
std::vector<vk::raii::Framebuffer> framebuffers;
vk::Extent2D extent;
// Foveation
std::array<xrt::drivers::wivrn::to_headset::video_stream_description::foveation_parameter, 2> foveation_parameters;
void cleanup();
public:
stream_reprojection() = default;
stream_reprojection(vk::raii::Device& device, vk::raii::PhysicalDevice& physical_device, std::vector<vk::Image> input_images, std::vector<vk::Image> output_images, vk::Extent2D extent, vk::Format format, const xrt::drivers::wivrn::to_headset::video_stream_description & description);
stream_reprojection(const stream_reprojection &) = delete;
void init(VkDevice device, VkPhysicalDevice physical_device, std::vector<VkImage> input_images, std::vector<VkImage> output_images, VkExtent2D extent, VkFormat format, const xrt::drivers::wivrn::to_headset::video_stream_description & description);
~stream_reprojection();
void reproject(VkCommandBuffer command_buffer, int source, int destination, XrQuaternionf source_pose, XrFovf source_fov, XrQuaternionf destination_pose, XrFovf destination_fov);
void reproject(vk::raii::CommandBuffer& command_buffer, int source, int destination, XrQuaternionf source_pose, XrFovf source_fov, XrQuaternionf destination_pose, XrFovf destination_fov);
};
+1
View File
@@ -19,6 +19,7 @@
#include "stream.h"
#include "utils/ranges.h"
#include "application.h"
#include <thread>
static from_headset::tracking::pose locate_space(device_id device, XrSpace space, XrSpace reference, XrTime time)