Show hand models, refactor the scene renderer
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -415,6 +415,8 @@ void application::initialize_vulkan()
|
||||
}
|
||||
#endif
|
||||
|
||||
device_extensions.push_back(VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
|
||||
|
||||
#ifdef __ANDROID__
|
||||
device_extensions.push_back(VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME);
|
||||
device_extensions.push_back(VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME);
|
||||
|
||||
@@ -27,7 +27,6 @@
|
||||
#include "utils/singleton.h"
|
||||
#include "vk/vk_allocator.h"
|
||||
#include "xr/xr.h"
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
|
||||
@@ -40,7 +40,7 @@ struct descriptor_set
|
||||
}
|
||||
};
|
||||
|
||||
growable_descriptor_pool::growable_descriptor_pool(vk::raii::Device & device, std::span<vk::DescriptorSetLayoutBinding> bindings, int descriptorsets_per_pool) :
|
||||
growable_descriptor_pool::growable_descriptor_pool(vk::raii::Device & device, vk::raii::DescriptorSetLayout& layout, std::span<vk::DescriptorSetLayoutBinding> bindings, int descriptorsets_per_pool) :
|
||||
device(device), layout_(nullptr), descriptorsets_per_pool(descriptorsets_per_pool)
|
||||
{
|
||||
if (descriptorsets_per_pool <= 0)
|
||||
@@ -61,16 +61,14 @@ growable_descriptor_pool::growable_descriptor_pool(vk::raii::Device & device, st
|
||||
}
|
||||
}
|
||||
|
||||
vk::DescriptorSetLayoutCreateInfo dsl_info{};
|
||||
dsl_info.setBindings(bindings);
|
||||
layout_ = vk::raii::DescriptorSetLayout{device, dsl_info};
|
||||
layout_ = *layout;
|
||||
}
|
||||
|
||||
std::shared_ptr<vk::raii::DescriptorSet> growable_descriptor_pool::allocate()
|
||||
{
|
||||
vk::DescriptorSetAllocateInfo alloc_info{
|
||||
.descriptorSetCount = 1,
|
||||
.pSetLayouts = &*layout_,
|
||||
.pSetLayouts = &layout_,
|
||||
};
|
||||
|
||||
for (pool & i: pools)
|
||||
|
||||
@@ -33,18 +33,14 @@ class growable_descriptor_pool
|
||||
};
|
||||
|
||||
vk::raii::Device & device;
|
||||
vk::raii::DescriptorSetLayout layout_;
|
||||
vk::DescriptorSetLayout layout_;
|
||||
|
||||
int descriptorsets_per_pool;
|
||||
std::vector<vk::DescriptorPoolSize> size;
|
||||
std::vector<pool> pools;
|
||||
|
||||
public:
|
||||
growable_descriptor_pool(vk::raii::Device & device, std::span<vk::DescriptorSetLayoutBinding> bindings, int descriptorsets_per_pool = 100);
|
||||
growable_descriptor_pool(vk::raii::Device & device, vk::raii::DescriptorSetLayout& layout, std::span<vk::DescriptorSetLayoutBinding> bindings, int descriptorsets_per_pool = 100);
|
||||
|
||||
std::shared_ptr<vk::raii::DescriptorSet> allocate();
|
||||
vk::raii::DescriptorSetLayout& layout()
|
||||
{
|
||||
return layout_;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -24,14 +24,12 @@
|
||||
#include "utils/fmt_glm.h"
|
||||
#include "utils/ranges.h"
|
||||
#include <boost/pfr/core.hpp>
|
||||
#include <concepts>
|
||||
#include <fastgltf/base64.hpp>
|
||||
#include <fastgltf/glm_element_traits.hpp>
|
||||
#include <fastgltf/parser.hpp>
|
||||
#include <fastgltf/tools.hpp>
|
||||
#include <fastgltf/types.hpp>
|
||||
#include <fastgltf/util.hpp>
|
||||
#include <fstream>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <stdexcept>
|
||||
@@ -40,7 +38,6 @@
|
||||
#include <vk_mem_alloc.h>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
|
||||
#include "application.h"
|
||||
#include "asset.h"
|
||||
|
||||
template <class T>
|
||||
@@ -323,7 +320,7 @@ constexpr bool starts_with(std::span<const std::byte> data, std::span<const uint
|
||||
return data.size() >= prefix.size() && !memcmp(data.data(), prefix.data(), prefix.size());
|
||||
}
|
||||
|
||||
static fastgltf::MimeType guess_mime_type(std::span<const std::byte> image_data)
|
||||
fastgltf::MimeType guess_mime_type(std::span<const std::byte> image_data)
|
||||
{
|
||||
const uint8_t png_magic[] = {0xFF, 0xD8, 0xFF};
|
||||
const uint8_t jpeg_magic[] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
|
||||
@@ -342,7 +339,7 @@ static fastgltf::MimeType guess_mime_type(std::span<const std::byte> image_data)
|
||||
return fastgltf::MimeType::None;
|
||||
}
|
||||
|
||||
static std::shared_ptr<vk::raii::ImageView> do_load_image(
|
||||
std::shared_ptr<vk::raii::ImageView> do_load_image(
|
||||
vk::raii::PhysicalDevice & physical_device,
|
||||
vk::raii::Device & device,
|
||||
vk::raii::Queue & queue,
|
||||
@@ -698,7 +695,7 @@ public:
|
||||
return meshes;
|
||||
}
|
||||
|
||||
std::vector<scene_data::node> load_all_objects()
|
||||
std::vector<scene_data::node> load_all_nodes()
|
||||
{
|
||||
std::vector<scene_data::node> unsorted_objects;
|
||||
unsorted_objects.resize(gltf.nodes.size(), {.parent_id = scene_data::node::root_id });
|
||||
@@ -708,6 +705,25 @@ public:
|
||||
if (gltf_node.meshIndex)
|
||||
unsorted_objects[index].mesh_id = *gltf_node.meshIndex;
|
||||
|
||||
if (gltf_node.skinIndex)
|
||||
{
|
||||
auto& skin = gltf.skins.at(*gltf_node.skinIndex);
|
||||
|
||||
unsorted_objects[index].joints.resize(skin.joints.size());
|
||||
|
||||
for(auto [i, joint_index]: utils::enumerate(skin.joints))
|
||||
unsorted_objects[index].joints.at(i).first = joint_index;
|
||||
|
||||
if (skin.inverseBindMatrices)
|
||||
{
|
||||
const fastgltf::Accessor & accessor = gltf.accessors.at(*skin.inverseBindMatrices);
|
||||
|
||||
fastgltf::iterateAccessorWithIndex<glm::mat4>(gltf, accessor, [&](const glm::mat4& value, std::size_t idx) {
|
||||
unsorted_objects[index].joints.at(idx).second = value;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t child: gltf_node.children)
|
||||
{
|
||||
unsorted_objects[child].parent_id = index;
|
||||
@@ -725,37 +741,37 @@ public:
|
||||
return unsorted_objects;
|
||||
}
|
||||
|
||||
std::vector<scene_data::node> topological_sort(const std::vector<scene_data::node> & unsorted_objects)
|
||||
std::vector<scene_data::node> topological_sort(const std::vector<scene_data::node> & unsorted_nodes)
|
||||
{
|
||||
std::vector<scene_data::node> sorted_objects;
|
||||
std::vector<scene_data::node> sorted_nodes;
|
||||
std::vector<size_t> new_index;
|
||||
std::vector<bool> already_sorted;
|
||||
|
||||
sorted_objects.reserve(unsorted_objects.size());
|
||||
already_sorted.resize(unsorted_objects.size(), false);
|
||||
new_index.resize(unsorted_objects.size(), scene_data::node::root_id);
|
||||
sorted_nodes.reserve(unsorted_nodes.size());
|
||||
already_sorted.resize(unsorted_nodes.size(), false);
|
||||
new_index.resize(unsorted_nodes.size(), scene_data::node::root_id);
|
||||
|
||||
[[maybe_unused]] bool loop_detected = true;
|
||||
|
||||
while (sorted_objects.size() < unsorted_objects.size())
|
||||
while (sorted_nodes.size() < unsorted_nodes.size())
|
||||
{
|
||||
for (size_t i = 0; i < unsorted_objects.size(); i++)
|
||||
for (size_t i = 0; i < unsorted_nodes.size(); i++)
|
||||
{
|
||||
if (already_sorted[i])
|
||||
continue;
|
||||
|
||||
if (unsorted_objects[i].parent_id == scene_data::node::root_id)
|
||||
if (unsorted_nodes[i].parent_id == scene_data::node::root_id)
|
||||
{
|
||||
sorted_objects.push_back(unsorted_objects[i]);
|
||||
sorted_nodes.push_back(unsorted_nodes[i]);
|
||||
already_sorted[i] = true;
|
||||
new_index[i] = sorted_objects.size() - 1;
|
||||
new_index[i] = sorted_nodes.size() - 1;
|
||||
loop_detected = false;
|
||||
}
|
||||
else if (already_sorted[unsorted_objects[i].parent_id])
|
||||
else if (already_sorted[unsorted_nodes[i].parent_id])
|
||||
{
|
||||
sorted_objects.emplace_back(unsorted_objects[i]).parent_id = new_index[unsorted_objects[i].parent_id];
|
||||
sorted_nodes.emplace_back(unsorted_nodes[i]).parent_id = new_index[unsorted_nodes[i].parent_id];
|
||||
already_sorted[i] = true;
|
||||
new_index[i] = sorted_objects.size() - 1;
|
||||
new_index[i] = sorted_nodes.size() - 1;
|
||||
loop_detected = false;
|
||||
}
|
||||
}
|
||||
@@ -763,13 +779,22 @@ public:
|
||||
assert(!loop_detected);
|
||||
}
|
||||
|
||||
assert(sorted_objects.size() == unsorted_objects.size());
|
||||
for(size_t i = 0; i < sorted_objects.size(); i++)
|
||||
// renumber joint indices
|
||||
for(auto& node: sorted_nodes)
|
||||
{
|
||||
assert(sorted_objects[i].parent_id == scene_data::node::root_id || sorted_objects[i].parent_id < i);
|
||||
for(auto& joint: node.joints)
|
||||
{
|
||||
joint.first = new_index[joint.first];
|
||||
}
|
||||
}
|
||||
|
||||
return sorted_objects;
|
||||
assert(sorted_nodes.size() == unsorted_nodes.size());
|
||||
for(size_t i = 0; i < sorted_nodes.size(); i++)
|
||||
{
|
||||
assert(sorted_nodes[i].parent_id == scene_data::node::root_id || sorted_nodes[i].parent_id < i);
|
||||
}
|
||||
|
||||
return sorted_nodes;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -811,7 +836,7 @@ scene_data scene_loader::operator()(const std::filesystem::path & gltf_path)
|
||||
// Load all meshes
|
||||
data.meshes = ctx.load_all_meshes(materials, staging_buffer);
|
||||
|
||||
data.scene_objects = ctx.topological_sort(ctx.load_all_objects());
|
||||
data.scene_nodes = ctx.topological_sort(ctx.load_all_nodes());
|
||||
|
||||
// Copy the staging buffer to the GPU
|
||||
spdlog::debug("Uploading scene data ({} bytes) to GPU memory", staging_buffer.size());
|
||||
@@ -831,33 +856,38 @@ scene_data & scene_data::import(scene_data && other, node_handle parent)
|
||||
assert(parent.scene == this || parent.id == node::root_id);
|
||||
|
||||
size_t mesh_offset = meshes.size();
|
||||
size_t scene_objects_offset = scene_objects.size();
|
||||
size_t nodes_offset = scene_nodes.size();
|
||||
|
||||
for (auto & i: other.meshes)
|
||||
meshes.push_back(std::move(i));
|
||||
for (auto & mesh: other.meshes)
|
||||
meshes.push_back(std::move(mesh));
|
||||
|
||||
for (auto i: other.scene_objects)
|
||||
for (auto node: other.scene_nodes)
|
||||
{
|
||||
assert(!i.mesh_id || *i.mesh_id < other.meshes.size());
|
||||
assert(!node.mesh_id || *node.mesh_id < other.meshes.size());
|
||||
|
||||
if (i.mesh_id)
|
||||
*i.mesh_id += mesh_offset;
|
||||
if (node.mesh_id)
|
||||
*node.mesh_id += mesh_offset;
|
||||
|
||||
if (i.parent_id == node::root_id)
|
||||
for(auto& joint: node.joints)
|
||||
{
|
||||
i.parent_id = parent.id;
|
||||
joint.first += nodes_offset;
|
||||
}
|
||||
|
||||
if (node.parent_id == node::root_id)
|
||||
{
|
||||
node.parent_id = parent.id;
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(i.parent_id < other.scene_objects.size());
|
||||
i.parent_id += scene_objects_offset;
|
||||
assert(node.parent_id < other.scene_nodes.size());
|
||||
node.parent_id += nodes_offset;
|
||||
}
|
||||
|
||||
scene_objects.push_back(i);
|
||||
scene_nodes.push_back(node);
|
||||
}
|
||||
|
||||
other.meshes.clear();
|
||||
other.scene_objects.clear();
|
||||
other.scene_nodes.clear();
|
||||
|
||||
return *this;
|
||||
}
|
||||
@@ -870,9 +900,9 @@ scene_data & scene_data::import(scene_data && other)
|
||||
|
||||
node_handle scene_data::new_node()
|
||||
{
|
||||
size_t id = scene_objects.size();
|
||||
size_t id = scene_nodes.size();
|
||||
|
||||
scene_objects.push_back(node{
|
||||
scene_nodes.push_back(node{
|
||||
.parent_id = node::root_id,
|
||||
.position = {0, 0, 0},
|
||||
.orientation = {1, 0, 0, 0},
|
||||
@@ -885,7 +915,7 @@ node_handle scene_data::new_node()
|
||||
|
||||
node_handle scene_data::find_node(std::string_view name)
|
||||
{
|
||||
for(auto&& [index, node]: utils::enumerate(scene_objects))
|
||||
for(auto&& [index, node]: utils::enumerate(scene_nodes))
|
||||
{
|
||||
if (node.name == name)
|
||||
{
|
||||
@@ -899,16 +929,16 @@ node_handle scene_data::find_node(std::string_view name)
|
||||
|
||||
node_handle scene_data::find_node(node_handle root, std::string_view name)
|
||||
{
|
||||
assert(root.id < scene_objects.size());
|
||||
assert(root.id < scene_nodes.size());
|
||||
assert(root.scene == this);
|
||||
|
||||
std::vector<bool> flag(scene_objects.size(), false);
|
||||
std::vector<bool> flag(scene_nodes.size(), false);
|
||||
|
||||
flag[root.id] = true;
|
||||
|
||||
for(size_t index = root.id; index < scene_objects.size(); index++)
|
||||
for(size_t index = root.id; index < scene_nodes.size(); index++)
|
||||
{
|
||||
size_t parent = scene_objects[index].parent_id;
|
||||
size_t parent = scene_nodes[index].parent_id;
|
||||
|
||||
if (parent == node::root_id)
|
||||
continue;
|
||||
@@ -916,7 +946,7 @@ node_handle scene_data::find_node(node_handle root, std::string_view name)
|
||||
if (!flag[parent])
|
||||
continue;
|
||||
|
||||
if (scene_objects[index].name == name)
|
||||
if (scene_nodes[index].name == name)
|
||||
return {index, this};
|
||||
|
||||
flag[index] = true;
|
||||
|
||||
+14
-14
@@ -178,14 +178,14 @@ struct scene_data
|
||||
|
||||
uint32_t layer_mask = 1;
|
||||
|
||||
// std::vector<glm::mat4> bones_transform; // TODO: bones
|
||||
std::vector<std::pair<size_t, glm::mat4>> joints; // Node index, inverse bind matrix of each joint
|
||||
};
|
||||
|
||||
// TODO: lights
|
||||
// TODO: skybox
|
||||
|
||||
std::vector<scene_data::mesh> meshes;
|
||||
std::vector<scene_data::node> scene_objects;
|
||||
std::vector<scene_data::node> scene_nodes;
|
||||
|
||||
scene_data() = default;
|
||||
scene_data(const scene_data &) = delete;
|
||||
@@ -239,45 +239,45 @@ public:
|
||||
scene_data::node& operator*()
|
||||
{
|
||||
assert(scene != nullptr);
|
||||
assert(id < scene->scene_objects.size());
|
||||
return scene->scene_objects[id];
|
||||
assert(id < scene->scene_nodes.size());
|
||||
return scene->scene_nodes[id];
|
||||
}
|
||||
|
||||
const scene_data::node& operator*() const
|
||||
{
|
||||
assert(scene != nullptr);
|
||||
assert(id < scene->scene_objects.size());
|
||||
return scene->scene_objects[id];
|
||||
assert(id < scene->scene_nodes.size());
|
||||
return scene->scene_nodes[id];
|
||||
}
|
||||
|
||||
scene_data::node* operator->()
|
||||
{
|
||||
assert(scene != nullptr);
|
||||
assert(id < scene->scene_objects.size());
|
||||
return &scene->scene_objects[id];
|
||||
assert(id < scene->scene_nodes.size());
|
||||
return &scene->scene_nodes[id];
|
||||
}
|
||||
|
||||
const scene_data::node* operator->() const
|
||||
{
|
||||
assert(scene != nullptr);
|
||||
assert(id < scene->scene_objects.size());
|
||||
return &scene->scene_objects[id];
|
||||
assert(id < scene->scene_nodes.size());
|
||||
return &scene->scene_nodes[id];
|
||||
}
|
||||
|
||||
node_handle parent()
|
||||
{
|
||||
assert(scene != nullptr);
|
||||
assert(id < scene->scene_objects.size());
|
||||
assert(id < scene->scene_nodes.size());
|
||||
|
||||
return {scene->scene_objects[id].parent_id, scene};
|
||||
return {scene->scene_nodes[id].parent_id, scene};
|
||||
}
|
||||
|
||||
const node_handle parent() const
|
||||
{
|
||||
assert(scene != nullptr);
|
||||
assert(id < scene->scene_objects.size());
|
||||
assert(id < scene->scene_nodes.size());
|
||||
|
||||
return {scene->scene_objects[id].parent_id, scene};
|
||||
return {scene->scene_nodes[id].parent_id, scene};
|
||||
}
|
||||
|
||||
operator bool() const
|
||||
|
||||
+240
-181
@@ -32,7 +32,6 @@
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
#include <glm/gtc/matrix_access.hpp>
|
||||
#include <initializer_list>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <spdlog/spdlog.h>
|
||||
@@ -130,7 +129,7 @@ std::shared_ptr<scene_data::material> scene_renderer::create_default_material(vk
|
||||
return default_material;
|
||||
}
|
||||
|
||||
vk::FrontFace reverse(vk::FrontFace face)
|
||||
static vk::FrontFace reverse(vk::FrontFace face)
|
||||
{
|
||||
if (face == vk::FrontFace::eCounterClockwise)
|
||||
return vk::FrontFace::eClockwise;
|
||||
@@ -138,7 +137,8 @@ vk::FrontFace reverse(vk::FrontFace face)
|
||||
return vk::FrontFace::eCounterClockwise;
|
||||
}
|
||||
|
||||
std::array layout_bindings_0{
|
||||
static std::array layout_bindings_0
|
||||
{
|
||||
vk::DescriptorSetLayoutBinding{
|
||||
.binding = 0,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
@@ -146,11 +146,18 @@ std::array layout_bindings_0{
|
||||
.stageFlags = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment},
|
||||
vk::DescriptorSetLayoutBinding{
|
||||
.binding = 1,
|
||||
.descriptorType = vk::DescriptorType::eUniformBufferDynamic,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
.descriptorCount = 1,
|
||||
.stageFlags = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment}};
|
||||
.stageFlags = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment},
|
||||
vk::DescriptorSetLayoutBinding{
|
||||
.binding = 2,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
.descriptorCount = 1,
|
||||
.stageFlags = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment}
|
||||
};
|
||||
|
||||
std::array layout_bindings_1{
|
||||
static std::array layout_bindings_1
|
||||
{
|
||||
vk::DescriptorSetLayoutBinding{
|
||||
.binding = 0,
|
||||
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
|
||||
@@ -160,7 +167,8 @@ std::array layout_bindings_1{
|
||||
.binding = 5,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
.descriptorCount = 1,
|
||||
.stageFlags = vk::ShaderStageFlagBits::eFragment}};
|
||||
.stageFlags = vk::ShaderStageFlagBits::eFragment}
|
||||
};
|
||||
|
||||
scene_renderer::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<vk::Format> depth_formats, int frames_in_flight) :
|
||||
physical_device(physical_device),
|
||||
@@ -170,8 +178,9 @@ scene_renderer::scene_renderer(vk::raii::Device & device, vk::raii::PhysicalDevi
|
||||
output_size(output_size),
|
||||
output_format(output_format),
|
||||
depth_format(find_usable_image_format(physical_device, depth_formats, {output_size.width, output_size.height, 1}, vk::ImageUsageFlagBits::eDepthStencilAttachment)),
|
||||
ds_pool_frame(device, layout_bindings_0, frames_in_flight),
|
||||
ds_pool_material(device, layout_bindings_1, 100) // TODO tunable
|
||||
layout_0(create_descriptor_set_layout(layout_bindings_0, vk::DescriptorSetLayoutCreateFlagBits::ePushDescriptorKHR)),
|
||||
layout_1(create_descriptor_set_layout(layout_bindings_1)),
|
||||
ds_pool_material(device, layout_1, layout_bindings_1, 100) // TODO tunable
|
||||
{
|
||||
// Create the default material
|
||||
default_material = create_default_material(cb_pool);
|
||||
@@ -199,9 +208,8 @@ scene_renderer::scene_renderer(vk::raii::Device & device, vk::raii::PhysicalDevi
|
||||
|
||||
renderpass = create_renderpass();
|
||||
|
||||
std::array layouts{*ds_pool_frame.layout(), *ds_pool_material.layout()};
|
||||
std::array layouts{*layout_0, *layout_1};
|
||||
pipeline_layout = create_pipeline_layout(layouts);
|
||||
|
||||
}
|
||||
|
||||
scene_renderer::~scene_renderer()
|
||||
@@ -434,6 +442,13 @@ vk::raii::Pipeline & scene_renderer::get_pipeline(const pipeline_info & info)
|
||||
return pipelines.emplace(info, create_pipeline(info)).first->second;
|
||||
}
|
||||
|
||||
vk::raii::DescriptorSetLayout scene_renderer::create_descriptor_set_layout(std::span<vk::DescriptorSetLayoutBinding> bindings, vk::DescriptorSetLayoutCreateFlags flags)
|
||||
{
|
||||
vk::DescriptorSetLayoutCreateInfo dsl_info{.flags = flags};
|
||||
dsl_info.setBindings(bindings);
|
||||
return vk::raii::DescriptorSetLayout{device, dsl_info};
|
||||
}
|
||||
|
||||
vk::raii::PipelineLayout scene_renderer::create_pipeline_layout(std::span<vk::DescriptorSetLayout> layouts)
|
||||
{
|
||||
vk::PipelineLayoutCreateInfo pipeline_layout_info;
|
||||
@@ -451,61 +466,117 @@ 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");
|
||||
|
||||
return vk::raii::Pipeline{device, application::get_pipeline_cache(), vk::pipeline_builder{
|
||||
.Stages{
|
||||
vk::PipelineShaderStageCreateInfo{.stage = vk::ShaderStageFlagBits::eVertex, .module = *vertex_shader, .pName = "main"},
|
||||
vk::PipelineShaderStageCreateInfo{.stage = vk::ShaderStageFlagBits::eFragment, .module = *fragment_shader, .pName = "main"},
|
||||
},
|
||||
.VertexBindingDescriptions = {vertex_description.binding},
|
||||
.VertexAttributeDescriptions = vertex_description.attributes,
|
||||
.InputAssemblyState = {{
|
||||
.topology = info.topology,
|
||||
.primitiveRestartEnable = false,
|
||||
}},
|
||||
.Viewports = {vk::Viewport{
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.width = (float)output_size.width,
|
||||
.height = (float)output_size.height,
|
||||
.minDepth = 0,
|
||||
.maxDepth = 1,
|
||||
}},
|
||||
.Scissors = {vk::Rect2D{
|
||||
.offset = {0, 0},
|
||||
.extent = output_size,
|
||||
}},
|
||||
.RasterizationState = {vk::PipelineRasterizationStateCreateInfo{
|
||||
.polygonMode = vk::PolygonMode::eFill,
|
||||
.cullMode = info.cull_mode,
|
||||
.frontFace = info.front_face,
|
||||
.lineWidth = 1.0,
|
||||
}},
|
||||
.MultisampleState = {vk::PipelineMultisampleStateCreateInfo{
|
||||
.rasterizationSamples = MSAA_SAMPLES,
|
||||
}},
|
||||
.DepthStencilState = {vk::PipelineDepthStencilStateCreateInfo{
|
||||
.depthTestEnable = true,
|
||||
.depthWriteEnable = true,
|
||||
.depthCompareOp = vk::CompareOp::eLess,
|
||||
.depthBoundsTestEnable = false,
|
||||
.minDepthBounds = 0.0f,
|
||||
.maxDepthBounds = 1.0f,
|
||||
}},
|
||||
.ColorBlendState = {vk::PipelineColorBlendStateCreateInfo{}},
|
||||
.ColorBlendAttachments = {vk::PipelineColorBlendAttachmentState{
|
||||
.blendEnable = info.blend_enable,
|
||||
.srcColorBlendFactor = vk::BlendFactor::eSrcAlpha,
|
||||
.dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha,
|
||||
.colorBlendOp = vk::BlendOp::eAdd,
|
||||
.srcAlphaBlendFactor = vk::BlendFactor::eSrcAlpha,
|
||||
.dstAlphaBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha,
|
||||
.alphaBlendOp = vk::BlendOp::eAdd,
|
||||
.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA}},
|
||||
.DynamicState = {},
|
||||
.layout = *pipeline_layout,
|
||||
.renderPass = *renderpass,
|
||||
.subpass = 0,
|
||||
}};
|
||||
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, nb_clipping),
|
||||
.size = sizeof(int32_t),
|
||||
},
|
||||
vk::SpecializationMapEntry
|
||||
{
|
||||
.constantID = 2,
|
||||
.offset = offsetof(pipeline_info, dithering),
|
||||
.size = sizeof(VkBool32),
|
||||
},
|
||||
vk::SpecializationMapEntry
|
||||
{
|
||||
.constantID = 3,
|
||||
.offset = offsetof(pipeline_info, alpha_cutout),
|
||||
.size = sizeof(VkBool32),
|
||||
},
|
||||
vk::SpecializationMapEntry
|
||||
{
|
||||
.constantID = 4,
|
||||
.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,
|
||||
};
|
||||
|
||||
return vk::raii::Pipeline{
|
||||
device,
|
||||
application::get_pipeline_cache(),
|
||||
vk::pipeline_builder{
|
||||
.Stages{
|
||||
vk::PipelineShaderStageCreateInfo{
|
||||
.stage = vk::ShaderStageFlagBits::eVertex,
|
||||
.module = *vertex_shader,
|
||||
.pName = "main",
|
||||
.pSpecializationInfo = &specialization,
|
||||
|
||||
},
|
||||
vk::PipelineShaderStageCreateInfo{
|
||||
.stage = vk::ShaderStageFlagBits::eFragment,
|
||||
.module = *fragment_shader,
|
||||
.pName = "main",
|
||||
.pSpecializationInfo = &specialization,
|
||||
|
||||
},
|
||||
},
|
||||
.VertexBindingDescriptions = {vertex_description.binding},
|
||||
.VertexAttributeDescriptions = vertex_description.attributes,
|
||||
.InputAssemblyState = {{
|
||||
.topology = info.topology,
|
||||
.primitiveRestartEnable = false,
|
||||
}},
|
||||
.Viewports = {vk::Viewport{
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.width = (float)output_size.width,
|
||||
.height = (float)output_size.height,
|
||||
.minDepth = 0,
|
||||
.maxDepth = 1,
|
||||
}},
|
||||
.Scissors = {vk::Rect2D{
|
||||
.offset = {0, 0},
|
||||
.extent = output_size,
|
||||
}},
|
||||
.RasterizationState = {vk::PipelineRasterizationStateCreateInfo{
|
||||
.polygonMode = vk::PolygonMode::eFill,
|
||||
.cullMode = info.cull_mode,
|
||||
.frontFace = info.front_face,
|
||||
.lineWidth = 1.0,
|
||||
}},
|
||||
.MultisampleState = {vk::PipelineMultisampleStateCreateInfo{
|
||||
.rasterizationSamples = MSAA_SAMPLES,
|
||||
}},
|
||||
.DepthStencilState = {vk::PipelineDepthStencilStateCreateInfo{
|
||||
.depthTestEnable = true,
|
||||
.depthWriteEnable = true,
|
||||
.depthCompareOp = vk::CompareOp::eLess,
|
||||
.depthBoundsTestEnable = false,
|
||||
.minDepthBounds = 0.0f,
|
||||
.maxDepthBounds = 1.0f,
|
||||
}},
|
||||
.ColorBlendState = {vk::PipelineColorBlendStateCreateInfo{}},
|
||||
.ColorBlendAttachments = {vk::PipelineColorBlendAttachmentState{
|
||||
.blendEnable = info.blend_enable,
|
||||
.srcColorBlendFactor = vk::BlendFactor::eSrcAlpha,
|
||||
.dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha,
|
||||
.colorBlendOp = vk::BlendOp::eAdd,
|
||||
.srcAlphaBlendFactor = vk::BlendFactor::eSrcAlpha,
|
||||
.dstAlphaBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha,
|
||||
.alphaBlendOp = vk::BlendOp::eAdd,
|
||||
.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA
|
||||
}},
|
||||
.DynamicState = {},
|
||||
.layout = *pipeline_layout,
|
||||
.renderPass = *renderpass,
|
||||
.subpass = 0,
|
||||
}};
|
||||
}
|
||||
|
||||
vk::Sampler scene_renderer::get_sampler(const sampler_info & info)
|
||||
@@ -557,18 +628,21 @@ void scene_renderer::start_frame()
|
||||
f.cb.resetQueryPool(*query_pool, current_frame_index * 2, 2);
|
||||
f.cb.writeTimestamp(vk::PipelineStageFlagBits::eTopOfPipe, *query_pool, current_frame_index * 2);
|
||||
|
||||
// TODO: replace with vector<byte> and write to GPU in end_frame()
|
||||
f.staging_buffer_offset = 0;
|
||||
if (!f.staging_buffer)
|
||||
f.uniform_buffer_offset = 0;
|
||||
|
||||
if (!f.uniform_buffer)
|
||||
{
|
||||
f.staging_buffer = buffer_allocation{device,
|
||||
vk::BufferCreateInfo{
|
||||
.size = 1048576,
|
||||
.usage = vk::BufferUsageFlagBits::eUniformBuffer},
|
||||
VmaAllocationCreateInfo{
|
||||
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE},
|
||||
"scene_renderer::render (UBO staging)"};
|
||||
// TODO multiple UBOs if it does not fit
|
||||
f.uniform_buffer = buffer_allocation{
|
||||
device,
|
||||
vk::BufferCreateInfo{
|
||||
.size = 1048576,
|
||||
.usage = vk::BufferUsageFlagBits::eUniformBuffer},
|
||||
VmaAllocationCreateInfo{
|
||||
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE},
|
||||
"scene_renderer::render (UBO)"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -640,82 +714,46 @@ void scene_renderer::update_material_descriptor_set(scene_data::material& materi
|
||||
device.updateDescriptorSets(write_ds, {});
|
||||
}
|
||||
|
||||
static void print_scene_hierarchy(const scene_data& scene, std::span<glm::mat4> model_matrices, size_t root = scene_data::node::root_id, int level = 0)
|
||||
{
|
||||
for(const auto&& [index, node]: utils::enumerate(scene.scene_objects))
|
||||
{
|
||||
if (node.parent_id != root)
|
||||
continue;
|
||||
|
||||
// glm::mat4 M = model_matrices[index];
|
||||
// glm::vec4 pos = glm::column(M, 3);
|
||||
// spdlog::info("{:{}} {} pos={}, rot={}, pos to root={}", "", level * 2, node.name, node.translation, node.rotation, glm::vec3(pos));
|
||||
|
||||
spdlog::info("{:{}} {}", "", level * 2, node.name);
|
||||
|
||||
print_scene_hierarchy(scene, model_matrices, index, level + 1);
|
||||
}
|
||||
}
|
||||
// static void print_scene_hierarchy(const scene_data& scene, std::span<glm::mat4> model_matrices, size_t root = scene_data::node::root_id, int level = 0)
|
||||
// {
|
||||
// for(const auto&& [index, node]: utils::enumerate(scene.scene_nodes))
|
||||
// {
|
||||
// if (node.parent_id != root)
|
||||
// continue;
|
||||
//
|
||||
// // glm::mat4 M = model_matrices[index];
|
||||
// // glm::vec4 pos = glm::column(M, 3);
|
||||
// // spdlog::info("{:{}} {} pos={}, rot={}, pos to root={}", "", level * 2, node.name, node.translation, node.rotation, glm::vec3(pos));
|
||||
//
|
||||
// spdlog::info("{:{}} {}", "", level * 2, node.name);
|
||||
//
|
||||
// print_scene_hierarchy(scene, model_matrices, index, level + 1);
|
||||
// }
|
||||
// }
|
||||
|
||||
void scene_renderer::render(scene_data & scene, const std::array<float, 4>& clear_color, std::span<frame_info> frames)
|
||||
{
|
||||
per_frame_resources & resources = current_frame();
|
||||
|
||||
size_t ubo_alignment = physical_device_properties.limits.minUniformBufferOffsetAlignment;
|
||||
|
||||
size_t ubo_frame_size = utils::align_up(ubo_alignment, sizeof(frame_gpu_data));
|
||||
size_t ubo_instance_size = utils::align_up(ubo_alignment, sizeof(instance_gpu_data));
|
||||
|
||||
size_t nb_frames = frames.size();
|
||||
//size_t nb_objects = scene.scene_objects.size();
|
||||
size_t nb_objects = std::count_if(scene.scene_objects.begin(), scene.scene_objects.end(), [](const scene_data::node& object){
|
||||
return object.mesh_id;
|
||||
});
|
||||
|
||||
// Enough room for one frame_gpu_data per view and one instance_gpu_data per instance and per view
|
||||
// size_t ubo_size = nb_frames * (ubo_frame_size + nb_objects * ubo_instance_size);
|
||||
|
||||
// Compute the position of each ubo, taking in account the minimum alignment
|
||||
std::vector<size_t> ubo_frame_offset(nb_frames);
|
||||
for(size_t i = 0; i < nb_frames; i++)
|
||||
{
|
||||
ubo_frame_offset[i] = resources.staging_buffer_offset;
|
||||
resources.staging_buffer_offset += ubo_frame_size;
|
||||
}
|
||||
|
||||
std::vector<size_t> ubo_object_offset(nb_frames * nb_objects);
|
||||
for(size_t i = 0; i < nb_frames; i++)
|
||||
{
|
||||
for(size_t j = 0; j < nb_objects; j++)
|
||||
{
|
||||
ubo_object_offset[i * nb_objects + j] = resources.staging_buffer_offset;
|
||||
resources.staging_buffer_offset += ubo_instance_size;
|
||||
}
|
||||
}
|
||||
assert(resources.staging_buffer_offset <= resources.staging_buffer.info().size);
|
||||
|
||||
auto current_ubo_frame_offset = ubo_frame_offset.begin();
|
||||
auto current_ubo_object_offset = ubo_object_offset.begin();
|
||||
size_t buffer_alignment = std::max<size_t>(sizeof(glm::mat4), physical_device_properties.limits.minUniformBufferOffsetAlignment);
|
||||
// size_t buffer_alignment = std::max<size_t>(sizeof(glm::mat4), physical_device_properties.limits.minStorageBufferOffsetAlignment);
|
||||
|
||||
vk::raii::CommandBuffer & cb = resources.cb;
|
||||
|
||||
uintptr_t ubo = reinterpret_cast<uintptr_t>(resources.staging_buffer.map());
|
||||
uint8_t * ubo = resources.uniform_buffer.data();
|
||||
|
||||
std::array<vk::ClearValue, 2> clear_values{
|
||||
vk::ClearColorValue{clear_color},
|
||||
vk::ClearDepthStencilValue{1.0, 0},
|
||||
};
|
||||
|
||||
// cb.copyBuffer(resources.staging_buffer, resources.uniform_buffer, {});
|
||||
|
||||
auto vertex_layout = scene_data::vertex::describe();
|
||||
|
||||
std::vector<glm::mat4> transform_to_root(scene.scene_objects.size());
|
||||
std::vector<bool> reverse_side(scene.scene_objects.size());
|
||||
std::vector<bool> visible(scene.scene_objects.size());
|
||||
std::vector<glm::mat4> transform_to_root(scene.scene_nodes.size());
|
||||
std::vector<bool> reverse_side(scene.scene_nodes.size());
|
||||
std::vector<bool> visible(scene.scene_nodes.size());
|
||||
|
||||
|
||||
for(const auto& [index, object]: utils::enumerate(scene.scene_objects))
|
||||
for(const auto& [index, object]: utils::enumerate(scene.scene_nodes))
|
||||
{
|
||||
glm::mat4 transform_to_parent = glm::translate(glm::mat4(1), object.position) * (glm::mat4)object.orientation * glm::scale(glm::mat4(1), object.scale);
|
||||
float det = object.scale.x * object.scale.y * object.scale.z;
|
||||
@@ -746,7 +784,9 @@ void scene_renderer::render(scene_data & scene, const std::array<float, 4>& clea
|
||||
scene_renderer::output_image & output = get_output_image_data(frame.destination);
|
||||
glm::mat4 viewproj = frame.projection * frame.view;
|
||||
|
||||
frame_gpu_data& frame_ubo = *reinterpret_cast<frame_gpu_data*>(ubo + *current_ubo_frame_offset);
|
||||
vk::DeviceSize frame_ubo_offset = resources.uniform_buffer_offset;
|
||||
frame_gpu_data& frame_ubo = *reinterpret_cast<frame_gpu_data*>(ubo + resources.uniform_buffer_offset);
|
||||
resources.uniform_buffer_offset += utils::align_up(buffer_alignment, sizeof(frame_gpu_data));
|
||||
|
||||
// frame_ubo.ambient_color = glm::vec4(0.5,0.5,0.5,0); // TODO
|
||||
// frame_ubo.light_color = glm::vec4(0.5,0.5,0.5,0); // TODO
|
||||
@@ -773,45 +813,32 @@ void scene_renderer::render(scene_data & scene, const std::array<float, 4>& clea
|
||||
},
|
||||
vk::SubpassContents::eInline);
|
||||
|
||||
auto ds = ds_pool_frame.allocate();
|
||||
vk::DescriptorBufferInfo buffer_info_1{
|
||||
.buffer = resources.staging_buffer,
|
||||
.offset = (vk::DeviceSize)*current_ubo_frame_offset,
|
||||
.range = sizeof(frame_gpu_data)
|
||||
};;
|
||||
vk::DescriptorBufferInfo buffer_info_2{
|
||||
.buffer = resources.staging_buffer,
|
||||
.offset = 0,
|
||||
.range = sizeof(instance_gpu_data)
|
||||
};
|
||||
device.updateDescriptorSets(std::array{
|
||||
vk::WriteDescriptorSet{
|
||||
.dstSet = **ds,
|
||||
.dstBinding = 0,
|
||||
.descriptorCount = 1,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
.pBufferInfo = &buffer_info_1,
|
||||
},
|
||||
vk::WriteDescriptorSet{
|
||||
.dstSet = **ds,
|
||||
.dstBinding = 1,
|
||||
.descriptorCount = 1,
|
||||
.descriptorType = vk::DescriptorType::eUniformBufferDynamic,
|
||||
.pBufferInfo = &buffer_info_2,
|
||||
},
|
||||
}, {});
|
||||
resources.resources.push_back(ds);
|
||||
|
||||
for(const auto& [index, object]: utils::enumerate(scene.scene_objects))
|
||||
for(const auto& [index, node]: utils::enumerate(scene.scene_nodes))
|
||||
{
|
||||
if (!object.mesh_id)
|
||||
if (!node.mesh_id)
|
||||
continue;
|
||||
|
||||
scene_data::mesh& mesh = scene.meshes.at(*object.mesh_id);
|
||||
|
||||
instance_gpu_data& object_ubo = *reinterpret_cast<instance_gpu_data*>(ubo + *current_ubo_object_offset);
|
||||
|
||||
scene_data::mesh& mesh = scene.meshes.at(*node.mesh_id);
|
||||
glm::mat4& transform = transform_to_root[index];
|
||||
|
||||
vk::DeviceSize instance_ubo_offset = resources.uniform_buffer_offset;
|
||||
instance_gpu_data& object_ubo = *reinterpret_cast<instance_gpu_data*>(ubo + resources.uniform_buffer_offset);
|
||||
resources.uniform_buffer_offset += utils::align_up(buffer_alignment, sizeof(frame_gpu_data));
|
||||
|
||||
vk::DeviceSize joints_ubo_offset = 0;
|
||||
if (!node.joints.empty())
|
||||
{
|
||||
joints_ubo_offset = resources.uniform_buffer_offset;
|
||||
glm::mat4 * joint_matrices = reinterpret_cast<glm::mat4 *>(ubo + resources.uniform_buffer_offset);
|
||||
resources.uniform_buffer_offset += utils::align_up(buffer_alignment, sizeof(glm::mat4) * 32);
|
||||
assert(node.joints.size() <= 32);
|
||||
|
||||
for(auto&& [idx, joint]: utils::enumerate(node.joints))
|
||||
{
|
||||
joint_matrices[idx] = glm::inverse(transform) * transform_to_root[joint.first] * joint.second;
|
||||
}
|
||||
}
|
||||
|
||||
object_ubo.model = transform;
|
||||
object_ubo.modelview = frame.view * transform;
|
||||
object_ubo.modelviewproj = viewproj * transform;
|
||||
@@ -831,6 +858,9 @@ void scene_renderer::render(scene_data & scene, const std::array<float, 4>& clea
|
||||
.front_face = primitive.front_face,
|
||||
.topology = primitive.topology,
|
||||
.blend_enable = material->blend_enable,
|
||||
|
||||
.nb_texcoords = 2, // TODO
|
||||
.skinning = !node.joints.empty()
|
||||
};
|
||||
|
||||
if (material->double_sided)
|
||||
@@ -846,16 +876,48 @@ void scene_renderer::render(scene_data & scene, const std::array<float, 4>& clea
|
||||
|
||||
cb.bindVertexBuffers(0, (vk::Buffer)*mesh.buffer, primitive.vertex_offset);
|
||||
|
||||
std::array<vk::DescriptorSet, 2> descriptor_sets{
|
||||
**ds, // Set 0: per view and per object instance UBOs
|
||||
**material->ds // Set 1: material
|
||||
vk::DescriptorBufferInfo buffer_info_1{
|
||||
.buffer = resources.uniform_buffer,
|
||||
.offset = frame_ubo_offset,
|
||||
.range = sizeof(frame_gpu_data)
|
||||
|
||||
};
|
||||
vk::DescriptorBufferInfo buffer_info_2{
|
||||
.buffer = resources.uniform_buffer,
|
||||
.offset = instance_ubo_offset,
|
||||
.range = sizeof(instance_gpu_data)
|
||||
};
|
||||
vk::DescriptorBufferInfo buffer_info_3{
|
||||
.buffer = resources.uniform_buffer,
|
||||
.offset = joints_ubo_offset,
|
||||
.range = sizeof(glm::mat4) * 32
|
||||
};
|
||||
|
||||
std::array<uint32_t, 1> dynamic_offsets{
|
||||
(uint32_t)*current_ubo_object_offset // Offset of the object data
|
||||
std::array descriptors{
|
||||
vk::WriteDescriptorSet{
|
||||
.dstBinding = 0,
|
||||
.descriptorCount = 1,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
.pBufferInfo = &buffer_info_1
|
||||
},
|
||||
vk::WriteDescriptorSet{
|
||||
.dstBinding = 1,
|
||||
.descriptorCount = 1,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
.pBufferInfo = &buffer_info_2
|
||||
},
|
||||
vk::WriteDescriptorSet{
|
||||
.dstBinding = 2,
|
||||
.descriptorCount = 1,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
.pBufferInfo = &buffer_info_3
|
||||
},
|
||||
};
|
||||
|
||||
cb.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, *pipeline_layout, 0, descriptor_sets, dynamic_offsets);
|
||||
cb.pushDescriptorSetKHR(vk::PipelineBindPoint::eGraphics, *pipeline_layout, 0, descriptors);
|
||||
|
||||
// Set 1: material
|
||||
cb.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, *pipeline_layout, 1, **material->ds, {});
|
||||
|
||||
if (primitive.indexed)
|
||||
cb.drawIndexed(primitive.index_count, 1, 0, 0, 0);
|
||||
@@ -865,10 +927,7 @@ void scene_renderer::render(scene_data & scene, const std::array<float, 4>& clea
|
||||
resources.resources.push_back(material->ds);
|
||||
}
|
||||
|
||||
current_ubo_object_offset++;
|
||||
}
|
||||
cb.endRenderPass();
|
||||
|
||||
current_ubo_frame_offset++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,13 @@ struct pipeline_info
|
||||
vk::PrimitiveTopology topology = vk::PrimitiveTopology::eTriangleList;
|
||||
bool blend_enable = false;
|
||||
|
||||
// Specialization constants data
|
||||
int32_t nb_texcoords = 2;
|
||||
int32_t nb_clipping = 1;
|
||||
VkBool32 dithering = true;
|
||||
VkBool32 alpha_cutout = false;
|
||||
VkBool32 skinning = false;
|
||||
|
||||
bool operator==(const pipeline_info & other) const noexcept = default;
|
||||
};
|
||||
|
||||
@@ -57,8 +64,6 @@ struct hash<pipeline_info> : utils::magic_hash<pipeline_info>
|
||||
|
||||
class scene_renderer
|
||||
{
|
||||
public:
|
||||
// vk::raii::Instance& instance;
|
||||
vk::raii::PhysicalDevice physical_device;
|
||||
vk::raii::Device & device;
|
||||
vk::PhysicalDeviceProperties physical_device_properties;
|
||||
@@ -90,6 +95,7 @@ public:
|
||||
|
||||
std::shared_ptr<scene_data::texture> create_default_texture(vk::raii::CommandPool & cb_pool, std::vector<uint8_t> pixel);
|
||||
std::shared_ptr<scene_data::material> create_default_material(vk::raii::CommandPool & cb_pool);
|
||||
vk::raii::DescriptorSetLayout create_descriptor_set_layout(std::span<vk::DescriptorSetLayoutBinding> bindings, vk::DescriptorSetLayoutCreateFlags flags = {});
|
||||
|
||||
// Caches
|
||||
std::unordered_map<VkImage, output_image> output_images;
|
||||
@@ -98,8 +104,8 @@ public:
|
||||
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;
|
||||
vk::raii::DescriptorSetLayout layout_0; // Descriptor set 0: per-frame/view data (UBO) and per-instance data (UBO + SSBO)
|
||||
vk::raii::DescriptorSetLayout layout_1; // Descriptor set 1: per-material data (5 combined image samplers and 1 uniform buffer)
|
||||
|
||||
// Descriptor set 1: per-material data (5 combined image samplers and 1 uniform buffer)
|
||||
growable_descriptor_pool ds_pool_material;
|
||||
@@ -152,13 +158,10 @@ public:
|
||||
std::vector<std::shared_ptr<void>> resources;
|
||||
bool query_pool_filled = false;
|
||||
|
||||
// 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;
|
||||
// Buffer for per-view and per-instance data
|
||||
// device local, host visible, host coherent
|
||||
size_t uniform_buffer_offset;
|
||||
buffer_allocation uniform_buffer;
|
||||
};
|
||||
|
||||
std::vector<per_frame_resources> frame_resources;
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "hand_model.h"
|
||||
#include "openxr/openxr.h"
|
||||
#include "utils/ranges.h"
|
||||
|
||||
hand_model::hand_model(xr::hand_tracker & hand, const std::filesystem::path & gltf_path, scene_loader & loader, scene_data & scene) :
|
||||
hand(hand)
|
||||
{
|
||||
node_handle root_node = scene.new_node();
|
||||
root_node->name = gltf_path.stem();
|
||||
|
||||
scene.import(loader(gltf_path), root_node);
|
||||
|
||||
joints.resize(XR_HAND_JOINT_COUNT_EXT);
|
||||
// clang-format off
|
||||
joints[XR_HAND_JOINT_PALM_EXT] = node_handle();
|
||||
joints[XR_HAND_JOINT_WRIST_EXT] = scene.find_node(root_node, "wrist");
|
||||
joints[XR_HAND_JOINT_THUMB_METACARPAL_EXT] = scene.find_node(root_node, "thumb-metacarpal");
|
||||
joints[XR_HAND_JOINT_THUMB_PROXIMAL_EXT] = scene.find_node(root_node, "thumb-phalanx-proximal");
|
||||
joints[XR_HAND_JOINT_THUMB_DISTAL_EXT] = scene.find_node(root_node, "thumb-phalanx-distal");
|
||||
joints[XR_HAND_JOINT_THUMB_TIP_EXT] = scene.find_node(root_node, "thumb-tip");
|
||||
joints[XR_HAND_JOINT_INDEX_METACARPAL_EXT] = scene.find_node(root_node, "index-finger-metacarpal");
|
||||
joints[XR_HAND_JOINT_INDEX_PROXIMAL_EXT] = scene.find_node(root_node, "index-finger-phalanx-proximal");
|
||||
joints[XR_HAND_JOINT_INDEX_INTERMEDIATE_EXT] = scene.find_node(root_node, "index-finger-phalanx-intermediate");
|
||||
joints[XR_HAND_JOINT_INDEX_DISTAL_EXT] = scene.find_node(root_node, "index-finger-phalanx-distal");
|
||||
joints[XR_HAND_JOINT_INDEX_TIP_EXT] = scene.find_node(root_node, "index-finger-tip");
|
||||
joints[XR_HAND_JOINT_MIDDLE_METACARPAL_EXT] = scene.find_node(root_node, "middle-finger-metacarpal");
|
||||
joints[XR_HAND_JOINT_MIDDLE_PROXIMAL_EXT] = scene.find_node(root_node, "middle-finger-phalanx-proximal");
|
||||
joints[XR_HAND_JOINT_MIDDLE_INTERMEDIATE_EXT] = scene.find_node(root_node, "middle-finger-phalanx-intermediate");
|
||||
joints[XR_HAND_JOINT_MIDDLE_DISTAL_EXT] = scene.find_node(root_node, "middle-finger-phalanx-distal");
|
||||
joints[XR_HAND_JOINT_MIDDLE_TIP_EXT] = scene.find_node(root_node, "middle-finger-tip");
|
||||
joints[XR_HAND_JOINT_RING_METACARPAL_EXT] = scene.find_node(root_node, "ring-finger-metacarpal");
|
||||
joints[XR_HAND_JOINT_RING_PROXIMAL_EXT] = scene.find_node(root_node, "ring-finger-phalanx-proximal");
|
||||
joints[XR_HAND_JOINT_RING_INTERMEDIATE_EXT] = scene.find_node(root_node, "ring-finger-phalanx-intermediate");
|
||||
joints[XR_HAND_JOINT_RING_DISTAL_EXT] = scene.find_node(root_node, "ring-finger-phalanx-distal");
|
||||
joints[XR_HAND_JOINT_RING_TIP_EXT] = scene.find_node(root_node, "ring-finger-tip");
|
||||
joints[XR_HAND_JOINT_LITTLE_METACARPAL_EXT] = scene.find_node(root_node, "pinky-finger-metacarpal");
|
||||
joints[XR_HAND_JOINT_LITTLE_PROXIMAL_EXT] = scene.find_node(root_node, "pinky-finger-phalanx-proximal");
|
||||
joints[XR_HAND_JOINT_LITTLE_INTERMEDIATE_EXT] = scene.find_node(root_node, "pinky-finger-phalanx-intermediate");
|
||||
joints[XR_HAND_JOINT_LITTLE_DISTAL_EXT] = scene.find_node(root_node, "pinky-finger-phalanx-distal");
|
||||
joints[XR_HAND_JOINT_LITTLE_TIP_EXT] = scene.find_node(root_node, "pinky-finger-tip");
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
void hand_model::apply(XrSpace world_space, XrTime predicted_display_time)
|
||||
{
|
||||
std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT> joints_location = hand.locate(world_space, predicted_display_time);
|
||||
|
||||
for(auto&& [joint, loc]: utils::zip(joints, joints_location))
|
||||
{
|
||||
if (!joint)
|
||||
continue;
|
||||
|
||||
joint->position = {
|
||||
loc.first.pose.position.x,
|
||||
loc.first.pose.position.y,
|
||||
loc.first.pose.position.z
|
||||
};
|
||||
|
||||
joint->orientation = {
|
||||
loc.first.pose.orientation.w,
|
||||
loc.first.pose.orientation.x,
|
||||
loc.first.pose.orientation.y,
|
||||
loc.first.pose.orientation.z
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "xr/hand_tracker.h"
|
||||
#include <render/scene_data.h>
|
||||
#include <openxr/openxr.h>
|
||||
|
||||
struct hand_model
|
||||
{
|
||||
xr::hand_tracker& hand;
|
||||
|
||||
std::vector<node_handle> joints;
|
||||
|
||||
hand_model(xr::hand_tracker & hand, const std::filesystem::path & gltf_path, scene_loader& loader, scene_data& scene);
|
||||
|
||||
void apply(XrSpace world_space, XrTime predicted_display_time);
|
||||
};
|
||||
@@ -20,14 +20,12 @@
|
||||
|
||||
#include "application.h"
|
||||
#include "asset.h"
|
||||
#include <cstddef>
|
||||
#include <magic_enum.hpp>
|
||||
#include <simdjson.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
|
||||
namespace
|
||||
|
||||
@@ -586,6 +586,12 @@ void scenes::lobby::render(XrTime predicted_display_time, bool should_render)
|
||||
|
||||
input->apply(world_space, predicted_display_time);
|
||||
|
||||
if (left_hand)
|
||||
left_hand->apply(world_space, predicted_display_time);
|
||||
|
||||
if (right_hand)
|
||||
right_hand->apply(world_space, predicted_display_time);
|
||||
|
||||
XrCompositionLayerQuad imgui_layer = draw_gui(predicted_display_time);
|
||||
|
||||
assert(renderer);
|
||||
@@ -680,6 +686,12 @@ void scenes::lobby::on_focused()
|
||||
input = input_profile("controllers/" + choose_webxr_profile() + "/profile.json", loader, *controllers_scene);
|
||||
spdlog::info("Loaded input profile {}", input->id);
|
||||
|
||||
if (application::get_hand_tracking_supported())
|
||||
{
|
||||
left_hand.emplace(application::get_left_hand(), "left-hand.glb", loader, *controllers_scene);
|
||||
right_hand.emplace(application::get_right_hand(), "right-hand.glb", loader, *controllers_scene);
|
||||
}
|
||||
|
||||
std::array imgui_inputs{
|
||||
imgui_context::controller{
|
||||
.aim = get_action_space("left_aim"),
|
||||
@@ -732,6 +744,11 @@ void scenes::lobby::on_unfocused()
|
||||
imgui_ctx.reset();
|
||||
lobby_scene.reset(); // Must be reset before the renderer so that the descriptor sets are freed before their pools
|
||||
controllers_scene.reset();
|
||||
|
||||
input.reset();
|
||||
left_hand.reset();
|
||||
right_hand.reset();
|
||||
|
||||
renderer.reset();
|
||||
swapchains_lobby.clear();
|
||||
swapchains_controllers.clear();
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "render/scene_data.h"
|
||||
#include "scene.h"
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
#include "scenes/hand_model.h"
|
||||
#include "wivrn_discover.h"
|
||||
|
||||
#include "xr/passthrough.h"
|
||||
@@ -77,6 +78,8 @@ class lobby : public scene_impl<lobby>
|
||||
std::optional<scene_data> lobby_scene;
|
||||
std::optional<scene_data> controllers_scene;
|
||||
std::optional<input_profile> input;
|
||||
std::optional<hand_model> left_hand;
|
||||
std::optional<hand_model> right_hand;
|
||||
|
||||
std::optional<imgui_context> imgui_ctx;
|
||||
std::array<XrAction, 2> haptic_output;
|
||||
|
||||
+29
-10
@@ -20,16 +20,16 @@
|
||||
#version 450
|
||||
|
||||
layout (constant_id = 0) const int nb_texcoords = 2;
|
||||
layout (constant_id = 1) const int nb_joints = 1;
|
||||
layout (constant_id = 2) const int nb_clipping = 1;
|
||||
layout (constant_id = 3) const bool dithering = true;
|
||||
layout (constant_id = 4) const bool alpha_cutout = false;
|
||||
layout (constant_id = 1) const int nb_clipping = 1;
|
||||
layout (constant_id = 2) const bool dithering = true;
|
||||
layout (constant_id = 3) const bool alpha_cutout = false;
|
||||
layout (constant_id = 4) const bool skinning = false;
|
||||
|
||||
const float fog_min_dist = 20.0;
|
||||
const float fog_max_dist = 35.0;
|
||||
const vec4 fog_color = vec4(0.0, 0.25, 0.5, 1.0);
|
||||
|
||||
layout(set = 0, binding = 0) uniform scene_ubo
|
||||
layout(set = 0, binding = 0) uniform scene_ssbo
|
||||
{
|
||||
mat4 view;
|
||||
mat4 proj;
|
||||
@@ -39,13 +39,18 @@ layout(set = 0, binding = 0) uniform scene_ubo
|
||||
// vec4 clipping_plane[8];
|
||||
} scene;
|
||||
|
||||
layout(set = 0, binding = 1) uniform mesh_ubo
|
||||
layout(set = 0, binding = 1) uniform mesh_ssbo
|
||||
{
|
||||
mat4 model;
|
||||
mat4 modelview;
|
||||
mat4 modelviewproj;
|
||||
} mesh;
|
||||
|
||||
layout(set = 0, binding = 2) uniform joints_ssbo
|
||||
{
|
||||
mat4 joint_matrices[32];
|
||||
} joints;
|
||||
|
||||
#ifdef FRAG_SHADER
|
||||
layout(set = 1, binding = 0) uniform sampler2D base_color;
|
||||
layout(set = 1, binding = 1) uniform sampler2D metallic_roughness;
|
||||
@@ -76,8 +81,8 @@ layout(location = 1) in vec3 in_normal;
|
||||
layout(location = 2) in vec3 in_tangent;
|
||||
layout(location = 3) in vec2 in_texcoord[2];
|
||||
layout(location = 5) in vec4 in_color;
|
||||
layout(location = 6) in vec4 in_joints[nb_joints];
|
||||
layout(location = 7) in vec4 in_weights[nb_joints];
|
||||
layout(location = 6) in vec4 in_joints;
|
||||
layout(location = 7) in vec4 in_weights;
|
||||
#endif
|
||||
|
||||
// Vertex-to-fragment
|
||||
@@ -105,12 +110,26 @@ layout(location = 0) out vec4 out_color;
|
||||
|
||||
void main()
|
||||
{
|
||||
normal = vec3(mesh.modelview * vec4(in_normal, 0.0));
|
||||
|
||||
for(int i = 0; i < nb_texcoords; i++)
|
||||
texcoord[i] = in_texcoord[i];
|
||||
|
||||
gl_Position = mesh.modelviewproj * vec4(in_position, 1.0);
|
||||
if (skinning)
|
||||
{
|
||||
mat4 skinMatrix =
|
||||
in_weights.x * joints.joint_matrices[int(in_joints.x)] +
|
||||
in_weights.y * joints.joint_matrices[int(in_joints.y)] +
|
||||
in_weights.z * joints.joint_matrices[int(in_joints.z)] +
|
||||
in_weights.w * joints.joint_matrices[int(in_joints.w)];
|
||||
|
||||
normal = vec3(mesh.modelview * skinMatrix * vec4(in_normal, 0.0));
|
||||
gl_Position = mesh.modelviewproj * skinMatrix * vec4(in_position, 1.0);
|
||||
}
|
||||
else
|
||||
{
|
||||
normal = vec3(mesh.modelview * vec4(in_normal, 0.0));
|
||||
gl_Position = mesh.modelviewproj * vec4(in_position, 1.0);
|
||||
}
|
||||
frag_pos = mesh.modelview * vec4(in_position, 1.0);
|
||||
light_pos = scene.view * scene.light_position;
|
||||
|
||||
|
||||
@@ -48,6 +48,11 @@ std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT> xr::hand_tracker::l
|
||||
|
||||
CHECK_XR(xrLocateHandJointsEXT(id, &info, &locations));
|
||||
|
||||
if (!locations.isActive)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT> joints;
|
||||
for(int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user