From dc04f7edccce4cce1dba5d0385154b3277c45172 Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sat, 16 Aug 2025 10:28:45 +0200 Subject: [PATCH] Add utility to write specialization constants --- client/render/scene_renderer.cpp | 40 +++---------- client/scenes/stream.cpp | 49 +++------------- client/scenes/stream_reprojection.cpp | 54 +++-------------- common/vk/specialization_constants.h | 84 +++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 117 deletions(-) create mode 100644 common/vk/specialization_constants.h diff --git a/client/render/scene_renderer.cpp b/client/render/scene_renderer.cpp index d944c559..da28ec8f 100644 --- a/client/render/scene_renderer.cpp +++ b/client/render/scene_renderer.cpp @@ -28,6 +28,7 @@ #include "vk/allocation.h" #include "vk/pipeline.h" #include "vk/shader.h" +#include "vk/specialization_constants.h" #include #include #include @@ -535,34 +536,11 @@ vk::raii::Pipeline scene_renderer::create_pipeline(const pipeline_info & info) auto vertex_shader = load_shader(device, info.shader_name + ".vert"); auto fragment_shader = load_shader(device, info.shader_name + ".frag"); - std::array specialization_constants_desc{ - vk::SpecializationMapEntry{ - .constantID = 0, - .offset = offsetof(pipeline_info, nb_texcoords), - .size = sizeof(int32_t), - }, - vk::SpecializationMapEntry{ - .constantID = 1, - .offset = offsetof(pipeline_info, dithering), - .size = sizeof(VkBool32), - }, - vk::SpecializationMapEntry{ - .constantID = 2, - .offset = offsetof(pipeline_info, alpha_cutout), - .size = sizeof(VkBool32), - }, - vk::SpecializationMapEntry{ - .constantID = 3, - .offset = offsetof(pipeline_info, skinning), - .size = sizeof(VkBool32), - }}; - - vk::SpecializationInfo specialization{ - .mapEntryCount = specialization_constants_desc.size(), - .pMapEntries = specialization_constants_desc.data(), - .dataSize = sizeof(pipeline_info), - .pData = &info, - }; + auto specialization = make_specialization_constants( + int32_t(info.nb_texcoords), + VkBool32(info.dithering), + VkBool32(info.alpha_cutout), + VkBool32(info.skinning)); return vk::raii::Pipeline{ device, @@ -573,14 +551,14 @@ vk::raii::Pipeline scene_renderer::create_pipeline(const pipeline_info & info) .stage = vk::ShaderStageFlagBits::eVertex, .module = *vertex_shader, .pName = "main", - .pSpecializationInfo = &specialization, + .pSpecializationInfo = specialization, }, vk::PipelineShaderStageCreateInfo{ .stage = vk::ShaderStageFlagBits::eFragment, .module = *fragment_shader, .pName = "main", - .pSpecializationInfo = &specialization, + .pSpecializationInfo = specialization, }, }, @@ -797,7 +775,7 @@ void scene_renderer::update_material_descriptor_set(renderer::material & materia device.updateDescriptorSets(write_ds, {}); } -static void print_scene_hierarchy(const entt::registry & scene, entt::entity root = entt::null, int level = 0) +[[maybe_unused]] static void print_scene_hierarchy(const entt::registry & scene, entt::entity root = entt::null, int level = 0) { if (level == 0) spdlog::info("Node hierarchy:"); diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index dccc3291..50dca0a8 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -42,6 +42,7 @@ #include "utils/ranges.h" #include "vk/pipeline.h" #include "vk/shader.h" +#include "vk/specialization_constants.h" #include "wivrn_packets.h" #include #include @@ -804,51 +805,19 @@ void scenes::stream::render(const XrFrameState & frame_state) const auto & description = i.decoder->desc(); vk::Extent2D image_size = i.decoder->image_size(); - std::array useful_size{ - float(description.width) / image_size.width, - float(description.height) / image_size.height, - }; spdlog::info("useful size: {}x{} with buffer {}x{}", description.width, description.height, image_size.width, image_size.height); - std::array specialization_constants_desc{ - vk::SpecializationMapEntry{ - .constantID = 0, - .offset = 0, - .size = sizeof(float), - }, - vk::SpecializationMapEntry{ - .constantID = 1, - .offset = sizeof(float), - .size = sizeof(float), - }}; + auto vert_constants = make_specialization_constants( + float(description.width) / image_size.width, + float(description.height) / image_size.height); - vk::SpecializationInfo vert_specialization_info; - vert_specialization_info.setMapEntries(specialization_constants_desc); - vert_specialization_info.setData(useful_size); - - std::array constants = { - need_srgb_conversion(guess_model()), - i.alpha(), - }; - std::array frag_specialization_constant_desc = { - vk::SpecializationMapEntry{ - .constantID = 0, - .offset = 0, - .size = sizeof(constants[0]), - }, - vk::SpecializationMapEntry{ - .constantID = 1, - .offset = sizeof(constants[0]), - .size = sizeof(constants[1]), - }, - }; - vk::SpecializationInfo frag_specialization_info; - frag_specialization_info.setMapEntries(frag_specialization_constant_desc); - frag_specialization_info.setData(constants); + 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"); @@ -866,13 +835,13 @@ void scenes::stream::render(const XrFrameState & frame_state) .stage = vk::ShaderStageFlagBits::eVertex, .module = *vertex_shader, .pName = "main", - .pSpecializationInfo = &vert_specialization_info, + .pSpecializationInfo = vert_constants, }, { .stage = vk::ShaderStageFlagBits::eFragment, .module = *fragment_shader, .pName = "main", - .pSpecializationInfo = &frag_specialization_info, + .pSpecializationInfo = frag_constants, }}, .VertexBindingDescriptions = {}, .VertexAttributeDescriptions = {}, diff --git a/client/scenes/stream_reprojection.cpp b/client/scenes/stream_reprojection.cpp index 91443138..6528ea13 100644 --- a/client/scenes/stream_reprojection.cpp +++ b/client/scenes/stream_reprojection.cpp @@ -24,6 +24,7 @@ #include "vk/allocation.h" #include "vk/pipeline.h" #include "vk/shader.h" +#include "vk/specialization_constants.h" #include #include #include @@ -39,13 +40,6 @@ struct stream_reprojection::vertex alignas(8) glm::vec2 uv; }; -struct SgsrSpecializationConstants -{ - VkBool32 use_edge_direction; - float edge_threshold; - float edge_sharpness; -}; - void stream_reprojection::ensure_vertices(size_t num_vertices) { vk::BufferCreateInfo create_info{ @@ -223,46 +217,14 @@ stream_reprojection::stream_reprojection( vk::raii::ShaderModule vertex_shader = load_shader(device, "reprojection.vert"); // Fragment shader - std::string fragment_shader_name = "reprojection.frag"; - vk::SpecializationInfo fragment_specialization_info; - SgsrSpecializationConstants fragment_specialization_constants; - std::vector fragment_specialization_constants_desc; - const configuration::sgsr_settings sgsr = application::get_config().sgsr; - if (sgsr.enabled) - { - fragment_shader_name = "reprojection_sgsr.frag"; + const configuration::sgsr_settings & sgsr = application::get_config().sgsr; + auto sgsr_specialization = make_specialization_constants( + VkBool32(sgsr.use_edge_direction), + float(sgsr.edge_threshold / 255.f), + float(sgsr.edge_sharpness)); - fragment_specialization_constants = { - .use_edge_direction = sgsr.use_edge_direction, - .edge_threshold = float(sgsr.edge_threshold / 255.0), - .edge_sharpness = sgsr.edge_sharpness, - }; - - fragment_specialization_constants_desc = { - vk::SpecializationMapEntry{ - .constantID = 0, - .offset = offsetof(SgsrSpecializationConstants, use_edge_direction), - .size = sizeof(fragment_specialization_constants.use_edge_direction), - }, - vk::SpecializationMapEntry{ - .constantID = 1, - .offset = offsetof(SgsrSpecializationConstants, edge_threshold), - .size = sizeof(fragment_specialization_constants.edge_threshold), - }, - vk::SpecializationMapEntry{ - .constantID = 2, - .offset = offsetof(SgsrSpecializationConstants, edge_sharpness), - .size = sizeof(fragment_specialization_constants.edge_sharpness), - }, - }; - - fragment_specialization_info.setMapEntries(fragment_specialization_constants_desc); - fragment_specialization_info.setDataSize(sizeof(fragment_specialization_constants)); - fragment_specialization_info.setPData(&fragment_specialization_constants); - } - - vk::raii::ShaderModule fragment_shader = load_shader(device, fragment_shader_name); + vk::raii::ShaderModule fragment_shader = load_shader(device, sgsr.enabled ? "reprojection_sgsr.frag" : "reprojection.frag"); // Create graphics pipeline vk::PipelineLayoutCreateInfo pipeline_layout_info; @@ -282,7 +244,7 @@ stream_reprojection::stream_reprojection( .stage = vk::ShaderStageFlagBits::eFragment, .module = *fragment_shader, .pName = "main", - .pSpecializationInfo = &fragment_specialization_info, + .pSpecializationInfo = sgsr.enabled ? sgsr_specialization.info() : nullptr, }, }, .VertexBindingDescriptions = { diff --git a/common/vk/specialization_constants.h b/common/vk/specialization_constants.h new file mode 100644 index 00000000..a2d513ab --- /dev/null +++ b/common/vk/specialization_constants.h @@ -0,0 +1,84 @@ +/* + * 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 +#include +#include +#include + +template +class specialization_constants +{ + std::tuple values; + std::array entries; + vk::SpecializationInfo info_; + + specialization_constants(specialization_constants &&) = delete; + specialization_constants(const specialization_constants &) = delete; + specialization_constants & operator=(const specialization_constants &) = delete; + specialization_constants & operator=(specialization_constants &&) = delete; + + template + void fill_entries() + { + entries[i] = vk::SpecializationMapEntry{ + .constantID = i, + .offset = uint32_t(uintptr_t(std::addressof(std::get(values))) - uintptr_t(std::addressof(values))), + .size = sizeof(std::get(values)), + }; + if constexpr (i > 0) + fill_entries(); + } + +public: + specialization_constants(Args &&... args) : + values(std::forward(args)...), + info_{ + .mapEntryCount = uint32_t(entries.size()), + .pMapEntries = entries.data(), + .dataSize = sizeof(values), + .pData = &values, + } + { + fill_entries(); + } + + operator vk::SpecializationInfo *() + { + return &info_; + } + + vk::SpecializationInfo * info() + { + return &info_; + } + + template + auto & data() + { + return std::get(values); + } +}; + +template +specialization_constants make_specialization_constants(Args &&... args) +{ + return specialization_constants(std::forward(args)...); +}