/* * WiVRn VR streaming * Copyright (C) 2022 Guillaume Meunier * Copyright (C) 2022 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 "utils/magic_hash.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include struct sampler_info { vk::Filter mag_filter = vk::Filter::eLinear; vk::Filter min_filter = vk::Filter::eLinear; vk::SamplerMipmapMode min_filter_mipmap = vk::SamplerMipmapMode::eLinear; vk::SamplerAddressMode wrapS = vk::SamplerAddressMode::eRepeat; vk::SamplerAddressMode wrapT = vk::SamplerAddressMode::eRepeat; bool operator==(const sampler_info & other) const noexcept = default; }; namespace std { template <> struct hash : utils::magic_hash {}; } // namespace std class node_handle; struct scene_data { struct texture { std::shared_ptr image_view; sampler_info sampler; }; struct material { struct gpu_data { glm::vec4 base_color_factor = glm::vec4(1, 1, 1, 1); glm::vec4 base_emissive_factor = glm::vec4(0, 0, 0, 0); float metallic_factor = 1; float roughness_factor = 1; float occlusion_strength = 0; float normal_scale = 0; uint32_t base_color_texcoord = 0; uint32_t metallic_roughness_texcoord = 0; uint32_t occlusion_texcoord = 0; uint32_t emissive_texcoord = 0; uint32_t normal_texcoord = 0; // TODO: add fastgltf::TextureTransform? }; std::shared_ptr base_color_texture; std::shared_ptr metallic_roughness_texture; std::shared_ptr occlusion_texture; std::shared_ptr emissive_texture; std::shared_ptr normal_texture; // Disable back face culling with this material bool double_sided = true; // TODO transparency // fastgltf::AlphaMode alphaMode; // float alpha_cutoff; gpu_data staging; // Resources used by this material std::shared_ptr buffer; size_t offset; // The descriptor set is managed by the scene renderer, it is updated whenever ds_dirty is true // Bindings 0-4: textures // Binding 5: uniform buffer std::shared_ptr ds; // Set to true to update the descriptor set at the next frame bool ds_dirty; std::string name; std::string shader_name = "lit"; bool blend_enable = false; }; struct vertex { glm::vec3 position; glm::vec3 normal; glm::vec3 tangent; std::array texcoord; glm::vec4 color; std::array joints; std::array weights; struct description { vk::VertexInputBindingDescription binding; std::vector attributes; std::vector attribute_names; vk::PipelineVertexInputStateCreateFlags flags{}; }; static description describe(); }; struct primitive { bool indexed; uint32_t index_count; uint32_t vertex_count; vk::IndexType index_type; vk::DeviceSize index_offset; vk::DeviceSize vertex_offset; // See also material::double_sided vk::CullModeFlagBits cull_mode = vk::CullModeFlagBits::eNone; vk::FrontFace front_face = vk::FrontFace::eClockwise; vk::PrimitiveTopology topology = vk::PrimitiveTopology::eTriangleList; std::shared_ptr material_; }; struct mesh { std::vector primitives; std::shared_ptr buffer; }; struct node { static constexpr size_t root_id = size_t(-1); size_t parent_id; std::optional mesh_id; glm::vec3 position; glm::quat orientation; glm::vec3 scale; std::string name; bool visible; uint32_t layer_mask = 1; std::vector> joints; // Node index, inverse bind matrix of each joint }; // TODO: lights // TODO: skybox std::vector meshes; std::vector scene_nodes; scene_data() = default; scene_data(const scene_data &) = delete; scene_data(scene_data &&) = default; scene_data & operator=(const scene_data &) = delete; scene_data & operator=(scene_data &&) = default; scene_data & import(scene_data && other, node_handle parent); scene_data & import(scene_data && other); node_handle new_node(); node_handle find_node(std::string_view name); node_handle find_node(node_handle parent, std::string_view name); std::vector find_children(node_handle parent); std::shared_ptr find_material(std::string_view name); }; class scene_loader { vk::raii::Device & device; vk::raii::PhysicalDevice physical_device; vk::raii::Queue & queue; uint32_t queue_family_index; std::shared_ptr default_material; public: scene_loader(vk::raii::Device & device, vk::raii::PhysicalDevice physical_device, vk::raii::Queue & queue, uint32_t queue_family_index, std::shared_ptr default_material) : device(device), physical_device(physical_device), queue(queue), queue_family_index(queue_family_index), default_material(default_material) {} scene_data operator()(const std::filesystem::path & gltf_path); }; class node_handle { friend struct scene_data; size_t id = scene_data::node::root_id; scene_data * scene = nullptr; public: node_handle() = default; node_handle(size_t id, scene_data * scene) : id(id), scene(scene) {} node_handle(const node_handle &) = default; node_handle & operator=(const node_handle &) = default; scene_data::node & operator*() { assert(scene != nullptr); assert(id < scene->scene_nodes.size()); return scene->scene_nodes[id]; } const scene_data::node & operator*() const { assert(scene != nullptr); assert(id < scene->scene_nodes.size()); return scene->scene_nodes[id]; } scene_data::node * operator->() { assert(scene != nullptr); assert(id < scene->scene_nodes.size()); return &scene->scene_nodes[id]; } const scene_data::node * operator->() const { assert(scene != nullptr); assert(id < scene->scene_nodes.size()); return &scene->scene_nodes[id]; } node_handle parent() { assert(scene != nullptr); assert(id < scene->scene_nodes.size()); return {scene->scene_nodes[id].parent_id, scene}; } const node_handle parent() const { assert(scene != nullptr); assert(id < scene->scene_nodes.size()); return {scene->scene_nodes[id].parent_id, scene}; } operator bool() const { return id != scene_data::node::root_id; } };