/* * WiVRn VR streaming * Copyright (C) 2024 Guillaume Meunier * * 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 #include #include #include #include #include #include #include #include #include "vk/allocation.h" #include "render/growable_descriptor_pool.h" #include "render/scene_data.h" struct pipeline_info { std::string shader_name; vk::CullModeFlags cull_mode = vk::CullModeFlagBits::eNone; vk::FrontFace front_face = vk::FrontFace::eClockwise; vk::PrimitiveTopology topology = vk::PrimitiveTopology::eTriangleList; bool blend_enable = false; bool operator==(const pipeline_info & other) const noexcept = default; }; namespace std { template <> struct hash : utils::magic_hash {}; } // namespace std class scene_renderer { public: // vk::raii::Instance& instance; vk::raii::PhysicalDevice physical_device; vk::raii::Device & device; vk::PhysicalDeviceProperties physical_device_properties; vk::raii::Queue & queue; // Scene format const vk::Extent2D output_size; const vk::Format output_format; const vk::Format depth_format; // Destination images struct output_image { vk::raii::Framebuffer framebuffer = nullptr; vk::raii::ImageView image_view = nullptr; image_allocation depth_buffer; vk::raii::ImageView depth_view = nullptr; image_allocation multisample_image; vk::raii::ImageView multisample_view = nullptr; }; // Initialization functions output_image create_output_image_data(vk::Image output); vk::raii::RenderPass create_renderpass(); vk::raii::PipelineLayout create_pipeline_layout(std::span layouts); vk::raii::Pipeline create_pipeline(const pipeline_info & info); std::shared_ptr create_default_texture(vk::raii::CommandPool & cb_pool, std::vector pixel); std::shared_ptr create_default_material(vk::raii::CommandPool & cb_pool); // Caches std::unordered_map output_images; std::unordered_map pipelines; output_image & get_output_image_data(vk::Image output); vk::raii::Pipeline & get_pipeline(const pipeline_info & info); // Descriptor set 0: per-frame/view data (uniform buffer) and per-instance data (dynamic uniform buffer) growable_descriptor_pool ds_pool_frame; // Descriptor set 1: per-material data (5 combined image samplers and 1 uniform buffer) growable_descriptor_pool ds_pool_material; vk::raii::PipelineLayout pipeline_layout = nullptr; // Texture samplers std::unordered_map> samplers; vk::Sampler get_sampler(const sampler_info & info); // Render pass vk::raii::RenderPass renderpass = nullptr; // Default material and textures // This material has 1x1 textures with the following values: // base_color (1.0, 1.0, 1.0, 1.0) // metallic_roughness (1.0, 1.0) // occlusion (1.0) // emissive (0.0, 0.0, 0.0) // normal (0.5, 0.5, 1.0) // And: // base_color_factor (1.0, 1.0, 1.0, 1.0) // base_emissive_factor (0.0, 0.0, 0.0) // metallic_factor 1.0 // roughness_factor 1.0 // occlusion_strength 0.0 // normal_scale 0.0 std::shared_ptr default_material; struct frame_gpu_data { glm::mat4 view; glm::mat4 proj; glm::vec4 light_position; glm::vec4 ambient_color; glm::vec4 light_color; }; struct instance_gpu_data { glm::mat4 model; glm::mat4 modelview; glm::mat4 modelviewproj; }; struct per_frame_resources { vk::raii::Fence fence = nullptr; vk::raii::CommandBuffer cb = nullptr; std::vector> resources; // Uniform buffer for per-view and per-instance data // host visible, host coherent size_t staging_buffer_offset; buffer_allocation staging_buffer; // device local // buffer_allocation uniform_buffer; }; std::vector frame_resources; int current_frame_index; per_frame_resources& current_frame(); void update_material_descriptor_set(scene_data::material& material); public: scene_renderer(vk::raii::Device & device, vk::raii::PhysicalDevice physical_device, vk::raii::Queue & queue, vk::raii::CommandPool & cb_pool, vk::Extent2D output_size, vk::Format output_format, std::span depth_formats, int frames_in_flight = 2); ~scene_renderer(); struct frame_info { vk::Image destination; glm::mat4 projection; glm::mat4 view; }; void start_frame(); void render(scene_data & scene, const std::array& clear_color, std::span info); void end_frame(); std::shared_ptr get_default_material() { return default_material; } void wait_idle(); };