From 633f7feeb1dda12c0fc0ad79dea238f99fcf0722 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Mon, 1 Sep 2025 23:30:41 +0200 Subject: [PATCH] Skip blit when possible --- client/scenes/blitter.cpp | 359 ++++++++++++++++++ client/scenes/blitter.h | 72 ++++ client/scenes/stream.cpp | 309 +-------------- client/scenes/stream.h | 20 +- ...reprojection.cpp => stream_defoveator.cpp} | 345 ++++++++--------- ...eam_reprojection.h => stream_defoveator.h} | 35 +- client/shaders/reprojection.glsl.inc | 44 ++- client/shaders/stream.glsl | 30 +- 8 files changed, 678 insertions(+), 536 deletions(-) create mode 100644 client/scenes/blitter.cpp create mode 100644 client/scenes/blitter.h rename client/scenes/{stream_reprojection.cpp => stream_defoveator.cpp} (69%) rename client/scenes/{stream_reprojection.h => stream_defoveator.h} (75%) diff --git a/client/scenes/blitter.cpp b/client/scenes/blitter.cpp new file mode 100644 index 00000000..fd952a80 --- /dev/null +++ b/client/scenes/blitter.cpp @@ -0,0 +1,359 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "blitter.h" + +#include "application.h" +#include "utils/ranges.h" +#include "vk/pipeline.h" +#include "vk/shader.h" +#include "vk/specialization_constants.h" +#include +#include +#include + +namespace wivrn +{ + +static const uint32_t num_views = 2; + +blitter::blitter(vk::raii::Device & device, size_t view) : + device(device), view(view) +{ +} + +void blitter::reset(const to_headset::video_stream_description & desc) +{ + using channels_t = to_headset::video_stream_description::channels_t; + this->desc = desc; + + current = {}; + target = image_allocation(); + image_view = nullptr; + rp = nullptr; + fb = nullptr; + sampler = nullptr; + pipelines.clear(); + passthrough_rgb = passthrough_a = -1; + + auto w = desc.width / num_views; + for (auto [i, item]: utils::enumerate(desc.items)) + { + if (item.offset_x <= view * w and + item.offset_y == 0 and + item.offset_x + item.width * item.subsampling >= (view + 1) * w and + item.height * item.subsampling >= desc.height) + { + spdlog::info("Stream {} is eligible for blit-less path on view {}", i, view); + switch (item.channels) + { + case channels_t::colour: + passthrough_rgb = i; + break; + case channels_t::alpha: + passthrough_a = i; + break; + } + const bool alpha = item.channels == to_headset::video_stream_description::channels_t::alpha; + auto & r = alpha ? current.rect_a : current.rect_rgb; + r = vk::Rect2D{ + .offset = { + .x = int32_t(view * w) - item.offset_x, + .y = 0, + }, + .extent = { + .width = uint32_t(item.width * item.subsampling), + .height = uint32_t(item.height * item.subsampling), + }}; + } + } + + if (passthrough_rgb != -1 and passthrough_a != -1) + return; + + target = image_allocation( + device, + vk::ImageCreateInfo{ + .imageType = vk::ImageType::e2D, + .format = vk::Format::eB8G8R8A8Srgb, + .extent = { + .width = w, + .height = desc.height, + .depth = 1, + }, + .mipLevels = 1, + .arrayLayers = 1, + .usage = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eColorAttachment, + }, + { + .usage = VMA_MEMORY_USAGE_AUTO, + }, + std::format("blit image {}", view)); + + image_view = device.createImageView(vk::ImageViewCreateInfo{ + .image = vk::Image(target), + .viewType = vk::ImageViewType::e2D, + .format = target.info().format, + .subresourceRange = { + .aspectMask = vk::ImageAspectFlagBits::eColor, + .levelCount = 1, + .layerCount = 1, + }}); + + // render pass + { + vk::AttachmentReference color_ref{ + .attachment = 0, + .layout = vk::ImageLayout::eColorAttachmentOptimal, + }; + + vk::SubpassDescription subpass{ + .pipelineBindPoint = vk::PipelineBindPoint::eGraphics, + .colorAttachmentCount = 1, + .pColorAttachments = &color_ref, + }; + + vk::AttachmentDescription color_desc{ + .format = target.info().format, + .loadOp = vk::AttachmentLoadOp::eDontCare, + .finalLayout = vk::ImageLayout::eShaderReadOnlyOptimal, + }; + + rp = device.createRenderPass(vk::RenderPassCreateInfo{ + .attachmentCount = 1, + .pAttachments = &color_desc, + .subpassCount = 1, + .pSubpasses = &subpass, + }); + } + + fb = device.createFramebuffer( + vk::FramebufferCreateInfo{ + .renderPass = *rp, + .attachmentCount = 1, + .pAttachments = &*image_view, + .width = w, + .height = desc.height, + .layers = 1, + }); + + sampler = device.createSampler(vk::SamplerCreateInfo{ + .magFilter = vk::Filter::eLinear, + .minFilter = vk::Filter::eLinear, + .addressModeU = vk::SamplerAddressMode::eClampToBorder, + .addressModeV = vk::SamplerAddressMode::eClampToBorder, + .addressModeW = vk::SamplerAddressMode::eClampToBorder, + .borderColor = vk::BorderColor::eFloatOpaqueBlack, + }); + + pipelines.resize(desc.items.size()); + for (auto [i, item]: utils::enumerate(desc.items)) + { + pipelines[i].used = item.offset_x < (view + 1) * w and + item.offset_x + item.width * item.subsampling > view * w; + } + + current.rgb = *image_view; + current.sampler_rgb = *sampler; + current.rect_rgb = vk::Rect2D{.extent = {w, desc.height}}; + current.layout_rgb = vk::ImageLayout::eShaderReadOnlyOptimal; +} + +void blitter::begin(vk::raii::CommandBuffer & cmd) +{ + assert(not desc.items.empty()); + if (*fb == VK_NULL_HANDLE) + { + current.rgb = nullptr; + current.a = nullptr; + current.sampler_rgb = nullptr; + current.sampler_a = nullptr; + return; + } + + current.rgb = *image_view; + current.sampler_rgb = *sampler; + + cmd.beginRenderPass( + { + .renderPass = *rp, + .framebuffer = *fb, + .renderArea = { + .offset = {0, 0}, + .extent = {target.info().extent.width, target.info().extent.height}, + }, + }, + vk::SubpassContents::eInline); +} + +void blitter::push_image(vk::raii::CommandBuffer & cmd, uint8_t stream, vk::Sampler sampler, vk::ImageView image, vk::ImageLayout layout) +{ + if (stream == passthrough_rgb) + { + current.rgb = image; + current.sampler_rgb = sampler; + current.layout_rgb = layout; + return; + } + if (stream == passthrough_a) + { + current.a = image; + current.sampler_a = sampler; + current.layout_a = layout; + return; + } + if (pipelines.empty()) + return; + + assert(stream < pipelines.size()); + auto & p = pipelines[stream]; + if (not p.used) + return; + + const auto & extent = target.info().extent; + const auto & description = desc.items[stream]; + if (not *p.pipeline) + { + const bool alpha = description.channels == to_headset::video_stream_description::channels_t::alpha; + vk::DescriptorSetLayoutBinding sampler_layout_binding{ + .binding = 0, + .descriptorType = vk::DescriptorType::eCombinedImageSampler, + .descriptorCount = 1, + .stageFlags = vk::ShaderStageFlagBits::eFragment, + .pImmutableSamplers = &sampler, + }; + + p.set_layout = device.createDescriptorSetLayout(vk::DescriptorSetLayoutCreateInfo{ + .flags = vk::DescriptorSetLayoutCreateFlagBits::ePushDescriptorKHR, + .bindingCount = 1, + .pBindings = &sampler_layout_binding, + }); + + auto vert_constants = make_specialization_constants( + float(description.width) / description.video_width, + float(description.height) / description.video_height); + + auto frag_constants = make_specialization_constants( + VkBool32(alpha)); + + // Create graphics pipeline + vk::raii::ShaderModule vertex_shader = load_shader(device, "stream.vert"); + vk::raii::ShaderModule fragment_shader = load_shader(device, "stream.frag"); + + p.layout = device.createPipelineLayout(vk::PipelineLayoutCreateInfo{ + .setLayoutCount = 1, + .pSetLayouts = &*p.set_layout, + }); + + vk::pipeline_builder pipeline_info{ + .Stages = {{ + .stage = vk::ShaderStageFlagBits::eVertex, + .module = *vertex_shader, + .pName = "main", + .pSpecializationInfo = vert_constants, + }, + { + .stage = vk::ShaderStageFlagBits::eFragment, + .module = *fragment_shader, + .pName = "main", + .pSpecializationInfo = frag_constants, + }}, + .VertexBindingDescriptions = {}, + .VertexAttributeDescriptions = {}, + .InputAssemblyState = {{ + .topology = vk::PrimitiveTopology::eTriangleStrip, + }}, + // With vk::DynamicState::eViewport, vk::DynamicState::eScissor the number of viewports + // and scissors is still used, put a vector with one element + .Viewports = {{}}, + .Scissors = {{}}, + .RasterizationState = {{ + .polygonMode = vk::PolygonMode::eFill, + .lineWidth = 1, + }}, + .MultisampleState = {{ + .rasterizationSamples = vk::SampleCountFlagBits::e1, + }}, + .ColorBlendAttachments = { + {.colorWriteMask = alpha ? vk::ColorComponentFlagBits::eA + : vk::ColorComponentFlagBits::eA | vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB}}, + .DynamicStates = {vk::DynamicState::eViewport, vk::DynamicState::eScissor}, + .layout = *p.layout, + .renderPass = *rp, + .subpass = 0, + }; + + p.pipeline = device.createGraphicsPipeline(application::get_pipeline_cache(), pipeline_info); + spdlog::info("Created blit pipeline for stream {}, view {}", stream, view); + } + + cmd.bindPipeline(vk::PipelineBindPoint::eGraphics, *p.pipeline); + + vk::DescriptorImageInfo image_info{ + .imageView = image, + .imageLayout = layout, + }; + cmd.pushDescriptorSetKHR( + vk::PipelineBindPoint::eGraphics, + *p.layout, + 0, + vk::WriteDescriptorSet{ + .dstBinding = 0, + .descriptorCount = 1, + .descriptorType = vk::DescriptorType::eCombinedImageSampler, + .pImageInfo = &image_info, + }); + + int x0 = description.offset_x - view * extent.width; + int y0 = description.offset_y; + int x1 = x0 + description.width * description.subsampling; + int y1 = y0 + description.height * description.subsampling; + + cmd.setViewport( + 0, + vk::Viewport{ + .x = (float)x0, + .y = (float)y0, + .width = float(description.width * description.subsampling), + .height = float(description.height * description.subsampling), + .minDepth = 0, + .maxDepth = 1, + }); + + x0 = std::clamp(x0, 0, extent.width); + x1 = std::clamp(x1, 0, extent.width); + y0 = std::clamp(y0, 0, extent.height); + y1 = std::clamp(y1, 0, extent.height); + + cmd.setScissor( + 0, + vk::Rect2D{ + .offset = {.x = x0, .y = y0}, + .extent = {.width = uint32_t(x1 - x0), .height = uint32_t(y1 - y0)}, + }); + cmd.draw(3, 1, 0, 0); +} + +blitter::output blitter::end(vk::raii::CommandBuffer & cmd) +{ + if (*fb) + cmd.endRenderPass(); + return current; +} + +} // namespace wivrn diff --git a/client/scenes/blitter.h b/client/scenes/blitter.h new file mode 100644 index 00000000..c1a037e8 --- /dev/null +++ b/client/scenes/blitter.h @@ -0,0 +1,72 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include "vk/allocation.h" +#include "wivrn_packets.h" +#include + +namespace wivrn +{ +class blitter +{ +public: + struct output + { + vk::ImageView rgb; + vk::Sampler sampler_rgb; + vk::Rect2D rect_rgb; + vk::ImageLayout layout_rgb; + vk::ImageView a; + vk::Sampler sampler_a; + vk::Rect2D rect_a; + vk::ImageLayout layout_a; + }; + +private: + vk::raii::Device & device; + size_t view; + to_headset::video_stream_description desc; + // indices to simply passthrough, if -1 image needs to be blitted + int passthrough_rgb = -1; + int passthrough_a = -1; + image_allocation target; + vk::raii::ImageView image_view = nullptr; + vk::raii::RenderPass rp = nullptr; + vk::raii::Framebuffer fb = nullptr; + vk::raii::Sampler sampler = nullptr; + struct pipeline_t + { + vk::raii::DescriptorSetLayout set_layout = nullptr; + vk::raii::PipelineLayout layout = nullptr; + vk::raii::Pipeline pipeline = nullptr; + bool used = false; // true if we need this image for blitting + }; + std::vector pipelines; + output current; + +public: + blitter(vk::raii::Device & device, size_t view); + + void reset(const to_headset::video_stream_description & desc); + void begin(vk::raii::CommandBuffer &); + void push_image(vk::raii::CommandBuffer & buf, uint8_t stream, vk::Sampler sampler, vk::ImageView image, vk::ImageLayout); + output end(vk::raii::CommandBuffer &); +}; +} // namespace wivrn diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index a289dd01..aba0b167 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -38,9 +38,6 @@ #include "utils/contains.h" #include "utils/named_thread.h" #include "utils/ranges.h" -#include "vk/pipeline.h" -#include "vk/shader.h" -#include "vk/specialization_constants.h" #include "wivrn_packets.h" #include #include @@ -155,7 +152,8 @@ static const std::array supported_depth_formats{ }; scenes::stream::stream() : - scene_impl(supported_color_formats, supported_depth_formats) + scene_impl(supported_color_formats, supported_depth_formats), + blitters{blitter(device, 0), blitter(device, 1)} { } @@ -783,103 +781,6 @@ void scenes::stream::render(const XrFrameState & frame_state) } assert(swapchain); - for (auto & i: decoders) - { - if (auto sampler = i.decoder->sampler(); sampler and not *i.blit_pipeline) - { - // Create blit pipeline - // Create VkDescriptorSetLayout with an immutable sampler - vk::DescriptorSetLayoutBinding sampler_layout_binding{ - .binding = 0, - .descriptorType = vk::DescriptorType::eCombinedImageSampler, - .descriptorCount = 1, - .stageFlags = vk::ShaderStageFlagBits::eFragment, - .pImmutableSamplers = &sampler, - }; - - vk::DescriptorSetLayoutCreateInfo layout_info{ - .bindingCount = 1, - .pBindings = &sampler_layout_binding, - }; - - i.descriptor_set_layout = vk::raii::DescriptorSetLayout(device, layout_info); - i.descriptor_set = device.allocateDescriptorSets( - vk::DescriptorSetAllocateInfo{ - .descriptorPool = *blit_descriptor_pool, - .descriptorSetCount = 1, - .pSetLayouts = &*i.descriptor_set_layout, - })[0] - .release(); - - const auto & description = i.decoder->desc(); - vk::Extent2D image_size = i.decoder->image_size(); - spdlog::info("useful size: {}x{} with buffer {}x{}", - description.width, - description.height, - image_size.width, - image_size.height); - - auto vert_constants = make_specialization_constants( - float(description.width) / image_size.width, - float(description.height) / image_size.height); - - auto frag_constants = make_specialization_constants( - VkBool32(need_srgb_conversion(guess_model())), - VkBool32(i.alpha())); - - // Create graphics pipeline - vk::raii::ShaderModule vertex_shader = load_shader(device, "stream.vert"); - vk::raii::ShaderModule fragment_shader = load_shader(device, "stream.frag"); - - vk::PipelineLayoutCreateInfo pipeline_layout_info{ - .setLayoutCount = 1, - .pSetLayouts = &*i.descriptor_set_layout, - }; - - i.blit_pipeline_layout = vk::raii::PipelineLayout(device, pipeline_layout_info); - - vk::pipeline_builder pipeline_info{ - .Stages = {{ - .stage = vk::ShaderStageFlagBits::eVertex, - .module = *vertex_shader, - .pName = "main", - .pSpecializationInfo = vert_constants, - }, - { - .stage = vk::ShaderStageFlagBits::eFragment, - .module = *fragment_shader, - .pName = "main", - .pSpecializationInfo = frag_constants, - }}, - .VertexBindingDescriptions = {}, - .VertexAttributeDescriptions = {}, - .InputAssemblyState = {{ - .topology = vk::PrimitiveTopology::eTriangleStrip, - }}, - // With vk::DynamicState::eViewport, vk::DynamicState::eScissor the number of viewports - // and scissors is still used, put a vector with one element - .Viewports = {{}}, - .Scissors = {{}}, - .RasterizationState = {{ - .polygonMode = vk::PolygonMode::eFill, - .lineWidth = 1, - }}, - .MultisampleState = {{ - .rasterizationSamples = vk::SampleCountFlagBits::e1, - }}, - .ColorBlendAttachments = { - {.colorWriteMask = i.alpha() ? vk::ColorComponentFlagBits::eA - : vk::ColorComponentFlagBits::eA | vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB}}, - .DynamicStates = {vk::DynamicState::eViewport, vk::DynamicState::eScissor}, - .layout = *i.blit_pipeline_layout, - .renderPass = *blit_render_pass, - .subpass = 0, - }; - - i.blit_pipeline = vk::raii::Pipeline(device, application::get_pipeline_cache(), pipeline_info); - } - } - if (device.waitForFences(*fence, VK_TRUE, UINT64_MAX) == vk::Result::eTimeout) throw std::runtime_error("Vulkan fence timeout"); @@ -931,10 +832,9 @@ void scenes::stream::render(const XrFrameState & frame_state) std::array foveation{}; bool use_alpha = false; - // Blit images from the decoders - for (auto [i, blit_handle]: std::views::zip(decoders, current_blit_handles)) + for (auto & blit_handle: current_blit_handles) { - if (not blit_handle or not *i.blit_pipeline) + if (not blit_handle) continue; blit_handle->feedback.blitted = instance.now(); @@ -948,27 +848,12 @@ void scenes::stream::render(const XrFrameState & frame_state) foveation = blit_handle->view_info.foveation; use_alpha = blit_handle->view_info.alpha; - vk::DescriptorImageInfo image_info{ - .imageView = *blit_handle->image_view, - .imageLayout = vk::ImageLayout::eGeneral, - }; - - vk::WriteDescriptorSet descriptor_write{ - .dstSet = i.descriptor_set, - .dstBinding = 0, - .dstArrayElement = 0, - .descriptorCount = 1, - .descriptorType = vk::DescriptorType::eCombinedImageSampler, - .pImageInfo = &image_info, - }; - - device.updateDescriptorSets(descriptor_write, {}); - if (*blit_handle->current_layout != vk::ImageLayout::eGeneral) + if (*blit_handle->current_layout == vk::ImageLayout::eUndefined) { vk::ImageMemoryBarrier barrier{ .srcAccessMask = vk::AccessFlagBits::eNone, .dstAccessMask = vk::AccessFlagBits::eMemoryRead, - .oldLayout = *blit_handle->current_layout, + .oldLayout = vk::ImageLayout::eUndefined, .newLayout = vk::ImageLayout::eGeneral, .image = blit_handle->image, .subresourceRange = { @@ -983,72 +868,18 @@ void scenes::stream::render(const XrFrameState & frame_state) } } - uint16_t x_offset = 0; - for (auto & out: decoder_output) + // Blit images from the decoders + std::array blitted; + for (auto [i, b]: utils::enumerate(blitters)) { - vk::Extent2D decoder_out_size{ - .width = decoder_out_image.info().extent.width, - .height = decoder_out_image.info().extent.height, - }; - command_buffer.beginRenderPass( - { - .renderPass = *blit_render_pass, - .framebuffer = *out.frame_buffer, - .renderArea = { - .offset = {0, 0}, - .extent = decoder_out_size, - }, - .clearValueCount = 0, - }, - vk::SubpassContents::eInline); - - for (const auto & decoder: decoders) + b.begin(command_buffer); + for (auto [j, blit_handle]: utils::enumerate(current_blit_handles)) { - if (not *decoder.blit_pipeline) + if (not blit_handle) continue; - if (decoder.alpha() and not use_alpha) - continue; - - command_buffer.bindPipeline(vk::PipelineBindPoint::eGraphics, *decoder.blit_pipeline); - - const auto & description = decoder.decoder->desc(); - int x0 = description.offset_x - x_offset; - int y0 = description.offset_y; - int x1 = x0 + description.width * description.subsampling; - int y1 = y0 + description.height * description.subsampling; - - vk::Viewport viewport{ - .x = (float)x0, - .y = (float)y0, - .width = float(description.width * description.subsampling), - .height = float(description.height * description.subsampling), - .minDepth = 0, - .maxDepth = 1, - }; - - x0 = std::clamp(x0, 0, decoder_out_size.width); - x1 = std::clamp(x1, 0, decoder_out_size.width); - y0 = std::clamp(y0, 0, decoder_out_size.height); - y1 = std::clamp(y1, 0, decoder_out_size.height); - - vk::Rect2D scissor{ - .offset = {.x = x0, .y = y0}, - .extent = {.width = (uint32_t)(x1 - x0), .height = (uint32_t)(y1 - y0)}, - }; - - command_buffer.setViewport(0, viewport); - command_buffer.setScissor(0, scissor); - - command_buffer.bindDescriptorSets( - vk::PipelineBindPoint::eGraphics, - *decoder.blit_pipeline_layout, - 0, - decoder.descriptor_set, - nullptr); - command_buffer.draw(3, 1, 0, 0); + b.push_image(command_buffer, j, decoders[j].decoder->sampler(), *blit_handle->image_view, *blit_handle->current_layout); } - command_buffer.endRenderPass(); - x_offset += decoder_out_size.width; + blitted[i] = b.end(command_buffer); } command_buffer.writeTimestamp(vk::PipelineStageFlagBits::eBottomOfPipe, *query_pool, 1); @@ -1059,7 +890,7 @@ void scenes::stream::render(const XrFrameState & frame_state) int32_t max_height = 0; for (size_t i = 0; i < view_count; ++i) { - extents[i] = reprojector->defoveated_size(foveation[i]); + extents[i] = defoveator->defoveated_size(foveation[i]); max_width = std::max(max_width, extents[i].width); max_height = std::max(max_height, extents[i].height); } @@ -1089,7 +920,7 @@ void scenes::stream::render(const XrFrameState & frame_state) // defoveate the image int image_index = swapchain.acquire(); swapchain.wait(); - reprojector->reproject(command_buffer, foveation, image_index); + defoveator->defoveate(command_buffer, foveation, blitted, image_index); command_buffer.writeTimestamp(vk::PipelineStageFlagBits::eBottomOfPipe, *query_pool, 2); @@ -1266,109 +1097,8 @@ void scenes::stream::setup(const to_headset::video_stream_description & descript } video_stream_description = description; - - const uint32_t video_width = description.width / view_count; - const uint32_t video_height = description.height; - - // Create renderpass - { - vk::AttachmentDescription color_desc{ - .format = vk::Format::eA8B8G8R8SrgbPack32, - .samples = vk::SampleCountFlagBits::e1, - .loadOp = vk::AttachmentLoadOp::eDontCare, - .storeOp = vk::AttachmentStoreOp::eStore, - .initialLayout = vk::ImageLayout::eUndefined, - .finalLayout = vk::ImageLayout::eShaderReadOnlyOptimal, - }; - - vk::AttachmentReference color_ref{ - .attachment = 0, - .layout = vk::ImageLayout::eColorAttachmentOptimal, - }; - - vk::SubpassDescription subpass{ - .pipelineBindPoint = vk::PipelineBindPoint::eGraphics, - }; - subpass.setColorAttachments(color_ref); - - vk::RenderPassCreateInfo renderpass_info{ - .flags = {}, - }; - renderpass_info.setAttachments(color_desc); - renderpass_info.setSubpasses(subpass); - - blit_render_pass = vk::raii::RenderPass(device, renderpass_info); - } - - // Create outputs for the decoders - { - vk::ImageCreateInfo image_info{ - .flags = vk::ImageCreateFlags{}, - .imageType = vk::ImageType::e2D, - .format = vk::Format::eA8B8G8R8SrgbPack32, - .extent = {video_width, video_height, 1}, - .mipLevels = 1, - .arrayLayers = view_count, - .samples = vk::SampleCountFlagBits::e1, - .tiling = vk::ImageTiling::eOptimal, - .usage = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eColorAttachment, - .sharingMode = vk::SharingMode::eExclusive, - .initialLayout = vk::ImageLayout::eUndefined, - }; - - VmaAllocationCreateInfo alloc_info{ - .requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, - }; - - decoder_out_image = image_allocation{device, image_info, alloc_info}; - - vk::ImageViewCreateInfo image_view_info{ - .image = vk::Image{decoder_out_image}, - .viewType = vk::ImageViewType::e2D, - .format = image_info.format, - .components = {}, - .subresourceRange = { - .aspectMask = vk::ImageAspectFlagBits::eColor, - .baseMipLevel = 0, - .levelCount = 1, - .baseArrayLayer = 0, - .layerCount = 1, - }, - }; - - for (uint32_t view = 0; view < view_count; ++view) - { - auto & output = decoder_output[view]; - - image_view_info.subresourceRange.baseArrayLayer = view; - output.image_view = vk::raii::ImageView(device, image_view_info); - - output.frame_buffer = vk::raii::Framebuffer( - device, - vk::FramebufferCreateInfo{ - .renderPass = *blit_render_pass, - .attachmentCount = 1, - .pAttachments = &*output.image_view, - .width = image_info.extent.width, - .height = image_info.extent.height, - .layers = 1, - }); - } - } - - { - vk::DescriptorPoolSize pool_size{ - .type = vk::DescriptorType::eCombinedImageSampler, - .descriptorCount = uint32_t(2 * description.items.size()), - }; - blit_descriptor_pool = vk::raii::DescriptorPool( - device, - vk::DescriptorPoolCreateInfo{ - .maxSets = uint32_t(2 * description.items.size()), - .poolSizeCount = 1, - .pPoolSizes = &pool_size, - }); - } + for (auto & b: blitters) + b.reset(description); for (const auto & [stream_index, item]: utils::enumerate(description.items)) { @@ -1413,10 +1143,9 @@ void scenes::stream::setup_reprojection_swapchain(uint32_t swapchain_width, uint for (auto & image: swapchain.images()) swapchain_images.push_back(image.image); - reprojector.emplace( + defoveator.emplace( device, physical_device, - decoder_out_image, swapchain_images, extent, swapchain.format()); diff --git a/client/scenes/stream.h b/client/scenes/stream.h index 146b6752..64cafea0 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -23,8 +23,9 @@ #include "decoder/shard_accumulator.h" #include "render/imgui_impl.h" #include "scene.h" +#include "scenes/blitter.h" #include "scenes/input_profile.h" -#include "stream_reprojection.h" +#include "stream_defoveator.h" #include "utils/thread_safe.h" #include "wifi_lock.h" #include "wivrn_client.h" @@ -63,10 +64,6 @@ private: struct accumulator_images { std::unique_ptr decoder; - vk::raii::DescriptorSetLayout descriptor_set_layout = nullptr; - vk::DescriptorSet descriptor_set = nullptr; - vk::raii::PipelineLayout blit_pipeline_layout = nullptr; - vk::raii::Pipeline blit_pipeline = nullptr; // latest frames, rolling buffer std::array, image_buffer_size> latest_frames; @@ -97,19 +94,10 @@ private: std::shared_mutex decoder_mutex; std::optional video_stream_description; std::vector decoders; // Locked by decoder_mutex - vk::raii::DescriptorPool blit_descriptor_pool = nullptr; - vk::raii::RenderPass blit_render_pass = nullptr; - image_allocation decoder_out_image; + std::array blitters; - struct renderpass_output - { - vk::raii::ImageView image_view = nullptr; - vk::raii::Framebuffer frame_buffer = nullptr; - }; - std::array decoder_output; - - std::optional reprojector; + std::optional defoveator; vk::raii::Fence fence = nullptr; vk::raii::CommandBuffer command_buffer = nullptr; diff --git a/client/scenes/stream_reprojection.cpp b/client/scenes/stream_defoveator.cpp similarity index 69% rename from client/scenes/stream_reprojection.cpp rename to client/scenes/stream_defoveator.cpp index 662ad97a..e22700c3 100644 --- a/client/scenes/stream_reprojection.cpp +++ b/client/scenes/stream_defoveator.cpp @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -#include "stream_reprojection.h" +#include "stream_defoveator.h" #include "application.h" #include "utils/contains.h" #include "utils/ranges.h" @@ -32,7 +32,7 @@ #include #include -struct stream_reprojection::vertex +struct stream_defoveator::vertex { // output image position alignas(8) glm::vec2 position; @@ -41,7 +41,13 @@ struct stream_reprojection::vertex int32_t shading_rate; }; -void stream_reprojection::ensure_vertices(size_t num_vertices) +struct vert_pc +{ + glm::ivec4 rgb_rect; + glm::ivec4 a_rect; +}; + +void stream_defoveator::ensure_vertices(size_t num_vertices) { vk::BufferCreateInfo create_info{ .size = num_vertices * sizeof(vertex) * view_count, @@ -60,159 +66,58 @@ void stream_reprojection::ensure_vertices(size_t num_vertices) vertices_size = num_vertices * sizeof(vertex); } -stream_reprojection::vertex * stream_reprojection::get_vertices(size_t view) +stream_defoveator::vertex * stream_defoveator::get_vertices(size_t view) { assert(buffer); return reinterpret_cast(reinterpret_cast(buffer.map()) + view * vertices_size); } -stream_reprojection::stream_reprojection( - vk::raii::Device & device, - vk::raii::PhysicalDevice & physical_device, - image_allocation & input_image, - std::vector output_images_, - vk::Extent2D output_extent, - vk::Format format) : - view_count(input_image.info().arrayLayers), - device(device), - input_extent{ - .width = input_image.info().extent.width, - .height = input_image.info().extent.height, - }, - output_images(std::move(output_images_)), - output_extent(output_extent) +stream_defoveator::pipeline_t & stream_defoveator::ensure_pipeline(size_t view, vk::Sampler rgb, vk::Sampler a) { - 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, - }; + auto & target = a ? pipeline_a[view] : pipeline_rgb[view]; + if (*target.pipeline) + return target; - if (utils::contains(application::get_vk_device_extensions(), VK_IMG_FILTER_CUBIC_EXTENSION_NAME)) - sampler_info.magFilter = vk::Filter::eCubicIMG; - - sampler = vk::raii::Sampler(device, sampler_info); + std::array samplers{rgb, a}; + int32_t alpha = a ? 1 : 0; // Create VkDescriptorSetLayout - vk::DescriptorSetLayoutBinding layout_binding{ - .binding = 0, - .descriptorType = vk::DescriptorType::eCombinedImageSampler, - .descriptorCount = 1, - .stageFlags = 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)view_count, + std::array layout_binding{ + vk::DescriptorSetLayoutBinding{ + .binding = 0, + .descriptorType = vk::DescriptorType::eCombinedImageSampler, + .descriptorCount = uint32_t(alpha + 1), + .stageFlags = vk::ShaderStageFlagBits::eFragment, + .pImmutableSamplers = samplers.data(), }, }; - vk::DescriptorPoolCreateInfo pool_info{ - .maxSets = view_count, - .poolSizeCount = pool_size.size(), - .pPoolSizes = pool_size.data(), + target.descriptor_set_layout = device.createDescriptorSetLayout(vk::DescriptorSetLayoutCreateInfo{ + .bindingCount = layout_binding.size(), + .pBindings = layout_binding.data(), + }); + + target.ds = device.allocateDescriptorSets(vk::DescriptorSetAllocateInfo{ + .descriptorPool = *ds_pool, + .descriptorSetCount = 1, + .pSetLayouts = &*target.descriptor_set_layout, + })[0] + .release(); + + // pipeline layout + vk::PushConstantRange pc_range{ + .stageFlags = vk::ShaderStageFlagBits::eVertex, + .size = sizeof(vert_pc), }; - descriptor_pool = vk::raii::DescriptorPool(device, pool_info); - - // Create image views and descriptor sets - for (uint32_t view = 0; view < view_count; ++view) - { - vk::ImageViewCreateInfo iv_info{ - .image = input_image, - .viewType = vk::ImageViewType::e2D, - .format = input_image.info().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 = view, - .layerCount = 1, - }, - }; - - vk::ImageView input_image_view = *input_image_views.emplace_back(device, iv_info); - - vk::DescriptorSetAllocateInfo ds_info{ - .descriptorPool = *descriptor_pool, - .descriptorSetCount = 1, - .pSetLayouts = &*descriptor_set_layout, - }; - - auto descriptor_set = descriptor_sets.emplace_back(device.allocateDescriptorSets(ds_info)[0].release()); - - vk::DescriptorImageInfo image_info{ - .sampler = *sampler, - .imageView = input_image_view, - .imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal, - }; - - device.updateDescriptorSets( - vk::WriteDescriptorSet{ - .dstSet = descriptor_set, - .dstBinding = 0, - .dstArrayElement = 0, - .descriptorCount = 1, - .descriptorType = vk::DescriptorType::eCombinedImageSampler, - .pImageInfo = &image_info, - }, - {}); - } - - // Create renderpass - vk::AttachmentReference color_ref{ - .attachment = 0, - .layout = vk::ImageLayout::eColorAttachmentOptimal, + vk::PipelineLayoutCreateInfo pipeline_layout_info{ + .setLayoutCount = 1, + .pSetLayouts = &*target.descriptor_set_layout, + .pushConstantRangeCount = 1, + .pPushConstantRanges = &pc_range, }; - vk::AttachmentDescription attachment{ - .format = format, - .samples = vk::SampleCountFlagBits::e1, - .loadOp = vk::AttachmentLoadOp::eDontCare, - .storeOp = vk::AttachmentStoreOp::eStore, - .initialLayout = vk::ImageLayout::eColorAttachmentOptimal, - .finalLayout = vk::ImageLayout::eColorAttachmentOptimal, - }; - - vk::SubpassDescription subpass{ - .pipelineBindPoint = vk::PipelineBindPoint::eGraphics, - }; - subpass.setColorAttachments(color_ref); - - vk::StructureChain renderpass_info{ - vk::RenderPassCreateInfo{ - .attachmentCount = 1, - .pAttachments = &attachment, - .subpassCount = 1, - .pSubpasses = &subpass, - }, - }; - - renderpass = vk::raii::RenderPass(device, renderpass_info.get()); + target.layout = vk::raii::PipelineLayout(device, pipeline_layout_info); const auto & vk_device_extensions = application::get_vk_device_extensions(); bool fragment_shading_rate = false; @@ -231,6 +136,8 @@ stream_reprojection::stream_reprojection( // Fragment shader +#if 0 +FIXME: SGSR const configuration::sgsr_settings & sgsr = application::get_config().sgsr; auto sgsr_specialization = make_specialization_constants( VkBool32(sgsr.use_edge_direction), @@ -238,12 +145,12 @@ stream_reprojection::stream_reprojection( float(sgsr.edge_sharpness)); vk::raii::ShaderModule fragment_shader = load_shader(device, sgsr.enabled ? "reprojection_sgsr.frag" : "reprojection.frag"); - - // Create graphics pipeline - vk::PipelineLayoutCreateInfo pipeline_layout_info; - pipeline_layout_info.setSetLayouts(*descriptor_set_layout); - - layout = vk::raii::PipelineLayout(device, pipeline_layout_info); +#else + auto specialization = make_specialization_constants( + int32_t(alpha), + VkBool32(need_srgb_conversion(guess_model()))); + vk::raii::ShaderModule fragment_shader = load_shader(device, "reprojection.frag"); +#endif vk::pipeline_builder pipeline_info_builder{ .flags = {}, @@ -257,7 +164,7 @@ stream_reprojection::stream_reprojection( .stage = vk::ShaderStageFlagBits::eFragment, .module = *fragment_shader, .pName = "main", - .pSpecializationInfo = sgsr.enabled ? sgsr_specialization.info() : nullptr, + .pSpecializationInfo = specialization, }, }, .VertexBindingDescriptions = { @@ -313,7 +220,7 @@ stream_reprojection::stream_reprojection( .ColorBlendAttachments = {{ .colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA, }}, - .layout = *layout, + .layout = *target.layout, .renderPass = *renderpass, .subpass = 0, }; @@ -360,7 +267,61 @@ stream_reprojection::stream_reprojection( } } - pipeline = vk::raii::Pipeline(device, application::get_pipeline_cache(), pipeline_info); + target.pipeline = device.createGraphicsPipeline(application::get_pipeline_cache(), pipeline_info); + return target; +} + +stream_defoveator::stream_defoveator( + vk::raii::Device & device, + vk::raii::PhysicalDevice & physical_device, + std::vector output_images_, + vk::Extent2D output_extent, + vk::Format format) : + device(device), + physical_device(physical_device), + output_images(std::move(output_images_)), + output_extent(output_extent) +{ + // Create renderpass + vk::AttachmentDescription attachment{ + .format = format, + .samples = vk::SampleCountFlagBits::e1, + .loadOp = vk::AttachmentLoadOp::eDontCare, + .storeOp = vk::AttachmentStoreOp::eStore, + .finalLayout = vk::ImageLayout::eColorAttachmentOptimal, + }; + + vk::AttachmentReference color_ref{ + .attachment = 0, + .layout = vk::ImageLayout::eColorAttachmentOptimal, + }; + vk::SubpassDescription subpass{ + .pipelineBindPoint = vk::PipelineBindPoint::eGraphics, + .colorAttachmentCount = 1, + .pColorAttachments = &color_ref, + }; + + vk::StructureChain renderpass_info{ + vk::RenderPassCreateInfo{ + .attachmentCount = 1, + .pAttachments = &attachment, + .subpassCount = 1, + .pSubpasses = &subpass, + }, + }; + + renderpass = vk::raii::RenderPass(device, renderpass_info.get()); + + vk::DescriptorPoolSize pool_size{ + .type = vk::DescriptorType::eCombinedImageSampler, + .descriptorCount = view_count * 2, + }; + + ds_pool = device.createDescriptorPool(vk::DescriptorPoolCreateInfo{ + .maxSets = view_count * 2, + .poolSizeCount = 1, + .pPoolSizes = &pool_size, + }); // Create image views and framebuffers output_image_views.reserve(output_images.size() * view_count); @@ -413,7 +374,7 @@ static size_t required_vertices(const wivrn::to_headset::foveation_parameter & p return (2 * (p.x.size() + 1) + 1) * p.y.size(); } -int32_t stream_reprojection::shading_rate(int pixels_x, int pixels_y) +int32_t stream_defoveator::shading_rate(int pixels_x, int pixels_y) { int x = 0; if (pixels_x >= 2) @@ -428,9 +389,10 @@ int32_t stream_reprojection::shading_rate(int pixels_x, int pixels_y) return fragment_sizes[x][y]; } -void stream_reprojection::reproject(vk::raii::CommandBuffer & command_buffer, - const std::array & foveation, - int destination) +void stream_defoveator::defoveate(vk::raii::CommandBuffer & command_buffer, + const std::array & foveation, + std::span inputs, + int destination) { if (destination < 0 || destination >= (int)output_images.size()) throw std::runtime_error("Invalid destination image index"); @@ -447,8 +409,6 @@ void stream_reprojection::reproject(vk::raii::CommandBuffer & command_buffer, const int n_ratio_x = (px.size() - 1) / 2; glm::vec2 in(0), out(0); // pixel coordinates - glm::vec2 in_pixel_size(1. / input_extent.width, - 1. / input_extent.height); glm::vec2 out_pixel_size(2. / output_extent.width, 2. / output_extent.height); for (auto [iy, n_out_y]: utils::enumerate_range(py)) @@ -462,12 +422,12 @@ void stream_reprojection::reproject(vk::raii::CommandBuffer & command_buffer, const int ratio_x = std::abs(n_ratio_x - int(ix)) + 1; *vertices++ = { .position = out * out_pixel_size - glm::vec2(1), - .uv = in * in_pixel_size, + .uv = in, .shading_rate = shading_rate(ratio_x, ratio_y), }; *vertices++ = { .position = (out + glm::vec2(0, n_out_y * ratio_y)) * out_pixel_size - glm::vec2(1), - .uv = (in + glm::vec2(0, n_out_y)) * in_pixel_size, + .uv = (in + glm::vec2(0, n_out_y)), .shading_rate = shading_rate(ratio_x, ratio_y), }; in.x += n_out_x; @@ -475,40 +435,21 @@ void stream_reprojection::reproject(vk::raii::CommandBuffer & command_buffer, } *vertices++ = { .position = out * out_pixel_size - glm::vec2(1), - .uv = in * in_pixel_size, + .uv = in, }; in.y += n_out_y; out.y += n_out_y * ratio_y; *vertices++ = { .position = out * out_pixel_size - glm::vec2(1), - .uv = in * in_pixel_size, + .uv = in, }; *vertices++ = { .position = out * out_pixel_size - glm::vec2(1), - .uv = in * in_pixel_size, + .uv = in, }; } } - command_buffer.pipelineBarrier( - vk::PipelineStageFlagBits::eTopOfPipe, - vk::PipelineStageFlagBits::eColorAttachmentOutput, - {}, - {}, - {}, - vk::ImageMemoryBarrier{ - .srcAccessMask = {}, - .dstAccessMask = vk::AccessFlagBits::eColorAttachmentWrite, - .oldLayout = vk::ImageLayout::eUndefined, - .newLayout = vk::ImageLayout::eColorAttachmentOptimal, - .image = output_images[destination], - .subresourceRange = { - .aspectMask = vk::ImageAspectFlagBits::eColor, - .levelCount = 1, - .layerCount = view_count, - }, - }); - for (size_t view = 0; view < view_count; ++view) { vk::RenderPassBeginInfo begin_info{ @@ -520,9 +461,49 @@ void stream_reprojection::reproject(vk::raii::CommandBuffer & command_buffer, }, }; + const auto & input = inputs[view]; + auto & pipeline = ensure_pipeline(view, input.sampler_rgb, input.sampler_a); + + std::array image_info{ + vk::DescriptorImageInfo{ + .sampler = input.sampler_rgb, + .imageView = input.rgb, + .imageLayout = input.layout_rgb, + }, + vk::DescriptorImageInfo{ + .sampler = input.sampler_a, + .imageView = input.a, + .imageLayout = input.layout_a, + }, + }; + + std::array descriptor_writes{ + vk::WriteDescriptorSet{ + .dstSet = pipeline.ds, + .dstBinding = 0, + .descriptorCount = input.sampler_a ? 2u : 1u, + .descriptorType = vk::DescriptorType::eCombinedImageSampler, + .pImageInfo = image_info.data(), + }, + }; + + vert_pc pc{ + .rgb_rect = glm::ivec4(input.rect_rgb.offset.x, + input.rect_rgb.offset.y, + input.rect_rgb.extent.width, + input.rect_rgb.extent.height), + .a_rect = glm::ivec4(input.rect_a.offset.x, + input.rect_a.offset.y, + input.rect_a.extent.width, + input.rect_a.extent.height), + }; + + device.updateDescriptorSets(descriptor_writes, {}); + 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[view], {}); + command_buffer.bindPipeline(vk::PipelineBindPoint::eGraphics, *pipeline.pipeline); + command_buffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, *pipeline.layout, 0, pipeline.ds, {}); + command_buffer.pushConstants(*pipeline.layout, vk::ShaderStageFlagBits::eVertex, 0, pc); command_buffer.bindVertexBuffers(0, vk::Buffer(buffer), vertices_size * view); command_buffer.draw(required_vertices(foveation[view]), 1, 0, 0); command_buffer.endRenderPass(); @@ -542,7 +523,7 @@ static uint16_t count_pixels(const std::vector & param) return res; } -XrExtent2Di stream_reprojection::defoveated_size(const wivrn::to_headset::foveation_parameter & view) const +XrExtent2Di stream_defoveator::defoveated_size(const wivrn::to_headset::foveation_parameter & view) const { return { count_pixels(view.x), diff --git a/client/scenes/stream_reprojection.h b/client/scenes/stream_defoveator.h similarity index 75% rename from client/scenes/stream_reprojection.h rename to client/scenes/stream_defoveator.h index de4cc829..e14affc1 100644 --- a/client/scenes/stream_reprojection.h +++ b/client/scenes/stream_defoveator.h @@ -19,27 +19,35 @@ #pragma once +#include "blitter.h" #include "vk/allocation.h" #include "wivrn_packets.h" #include #include -class stream_reprojection +class stream_defoveator { struct vertex; - const uint32_t view_count; + static const uint32_t view_count = 2; // Vertex buffer buffer_allocation buffer; size_t vertices_size = 0; vk::raii::Device & device; + vk::raii::PhysicalDevice & physical_device; // Graphic pipeline - 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; + vk::raii::DescriptorPool ds_pool = nullptr; + struct pipeline_t + { + vk::raii::DescriptorSetLayout descriptor_set_layout = nullptr; + vk::DescriptorSet ds; + vk::raii::PipelineLayout layout = nullptr; + vk::raii::Pipeline pipeline = nullptr; + }; + pipeline_t pipeline_rgb[view_count]; + pipeline_t pipeline_a[view_count]; // Allowed sizes for variable shading rate // indices are for x, y @@ -48,12 +56,6 @@ class stream_reprojection // 2 is 4 pixels or more int32_t fragment_sizes[3][3] = {}; - // Source image - vk::raii::Sampler sampler = nullptr; - std::vector input_image_views; - std::vector descriptor_sets; - vk::Extent2D input_extent; - // Destination images std::vector output_images; std::vector output_image_views; @@ -64,21 +66,22 @@ class stream_reprojection vertex * get_vertices(size_t view); int32_t shading_rate(int pixels_x, int pixels_y); + pipeline_t & ensure_pipeline(size_t view, vk::Sampler rgb, vk::Sampler a); public: - stream_reprojection( + stream_defoveator( vk::raii::Device & device, vk::raii::PhysicalDevice & physical_device, - image_allocation & input_image, std::vector output_images, vk::Extent2D output_extent, vk::Format format); - stream_reprojection(const stream_reprojection &) = delete; + stream_defoveator(const stream_defoveator &) = delete; - void reproject( + void defoveate( vk::raii::CommandBuffer & command_buffer, const std::array & foveation, + std::span inputs, int destination); XrExtent2Di defoveated_size(const wivrn::to_headset::foveation_parameter &) const; diff --git a/client/shaders/reprojection.glsl.inc b/client/shaders/reprojection.glsl.inc index b82b7b4f..3c99775f 100644 --- a/client/shaders/reprojection.glsl.inc +++ b/client/shaders/reprojection.glsl.inc @@ -28,12 +28,19 @@ layout (location = 0) in vec2 vPosition; layout (location = 1) in vec2 vUV; layout (location = 2) in int vShadingRate; -layout(location = 0) out vec2 outUV; +layout(location = 0) out vec4 outUV; + +layout(push_constant) uniform pc +{ + ivec4 rgb_rect; + ivec4 a_rect; +}; void main() { gl_Position = vec4(vPosition, 0.0, 1.0); - outUV = vUV; + outUV.xy = (vUV + rgb_rect.xy) / rgb_rect.zw; + outUV.zw = (vUV + a_rect.xy) / a_rect.zw; #if VSR gl_PrimitiveShadingRateEXT = vShadingRate; #endif @@ -42,15 +49,42 @@ void main() #ifdef FRAG_SHADER -layout(set = 0, binding = 0) uniform sampler2D texSampler; +layout(constant_id = 0) const int alpha = 1; +layout(constant_id = 1) const bool do_srgb = false; -layout(location = 0) in vec2 inUV; +layout(set = 0, binding = 0) uniform sampler2D rgb[alpha + 1]; + +layout(location = 0) in vec4 inUV; layout(location = 0) out vec4 outColor; +float sRGB_to_linear(float x) +{ + if (x <= 0.04045) + return x / 12.92; + return pow((x + 0.055) / 1.055, 2.4); +} + +vec4 sRGB_to_linear_rgba(vec4 x) +{ + return vec4( + sRGB_to_linear(x.r), + sRGB_to_linear(x.g), + sRGB_to_linear(x.b), + x.a); +} + void main() { - outColor = texture(texSampler, inUV).rgba; + if (alpha == 1) + outColor = vec4(texture(rgb[0], inUV.xy).rgb, texture(rgb[1], inUV.zw).r); + else + outColor = texture(rgb[0], inUV.xy).rgba; + + if (do_srgb) + { + outColor = sRGB_to_linear_rgba(outColor); + } } #endif diff --git a/client/shaders/stream.glsl b/client/shaders/stream.glsl index 84b4ca78..c07b0953 100644 --- a/client/shaders/stream.glsl +++ b/client/shaders/stream.glsl @@ -45,40 +45,16 @@ void main() #ifdef FRAG_SHADER layout(binding = 0) uniform sampler2D texSampler; -layout(constant_id = 0) const bool do_srgb = true; -layout(constant_id = 1) const bool is_alpha = false; +layout(constant_id = 0) const bool is_alpha = false; layout(location = 0) in vec2 inUV; layout(location = 0) out vec4 outColor; -float sRGB_to_linear(float x) -{ - if (x <= 0.04045) - return x / 12.92; - return pow((x + 0.055) / 1.055, 2.4); -} - -vec4 sRGB_to_linear_rgba(vec4 x) -{ - return vec4( - sRGB_to_linear(x.r), - sRGB_to_linear(x.g), - sRGB_to_linear(x.b), - x.r); -} - void main() { - if (do_srgb) - { - outColor = sRGB_to_linear_rgba(texture(texSampler, inUV)); - } - else - { - outColor = texture(texSampler, inUV); - outColor.a = outColor.r; - } + outColor = texture(texSampler, inUV); + outColor.a = outColor.r; if (!is_alpha) { outColor.a = 1;