clang-format
This commit is contained in:
@@ -18,13 +18,13 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "application.h"
|
||||
#include "utils/alignment.h"
|
||||
#include "vk/allocation.h"
|
||||
#include <fastgltf/tools.hpp>
|
||||
#include <fastgltf/types.hpp>
|
||||
#include <vector>
|
||||
#include <vulkan/vulkan.hpp>
|
||||
#include <fastgltf/types.hpp>
|
||||
#include <fastgltf/tools.hpp>
|
||||
#include "application.h"
|
||||
#include "vk/allocation.h"
|
||||
#include "utils/alignment.h"
|
||||
|
||||
class gpu_buffer
|
||||
{
|
||||
@@ -125,8 +125,7 @@ public:
|
||||
VmaAllocationCreateInfo{
|
||||
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO},
|
||||
"gpu_buffer::copy_to_gpu"
|
||||
};
|
||||
"gpu_buffer::copy_to_gpu"};
|
||||
|
||||
memcpy(gpu_buffer.map(), bytes.data(), bytes.size());
|
||||
gpu_buffer.unmap();
|
||||
|
||||
@@ -18,18 +18,18 @@
|
||||
|
||||
#include "growable_descriptor_pool.h"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
|
||||
struct descriptor_set
|
||||
{
|
||||
growable_descriptor_pool& growable_pool;
|
||||
growable_descriptor_pool & growable_pool;
|
||||
vk::DescriptorPool pool;
|
||||
vk::raii::DescriptorSet ds;
|
||||
|
||||
~descriptor_set()
|
||||
{
|
||||
for(auto& i: growable_pool.pools)
|
||||
for (auto & i: growable_pool.pools)
|
||||
{
|
||||
if (*i.descriptor_pool == pool)
|
||||
{
|
||||
@@ -40,25 +40,31 @@ struct descriptor_set
|
||||
}
|
||||
};
|
||||
|
||||
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)
|
||||
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)
|
||||
throw std::invalid_argument("descriptorsets_per_pool");
|
||||
|
||||
for(vk::DescriptorSetLayoutBinding binding: bindings)
|
||||
for (vk::DescriptorSetLayoutBinding binding: bindings)
|
||||
{
|
||||
if (auto it = std::find_if(size.begin(), size.end(), [&binding](vk::DescriptorPoolSize& x){return x.type == binding.descriptorType;}); it != size.end())
|
||||
{
|
||||
it->descriptorCount += binding.descriptorCount * descriptorsets_per_pool;
|
||||
}
|
||||
else if (binding.descriptorCount > 0)
|
||||
{
|
||||
size.push_back(vk::DescriptorPoolSize{
|
||||
.type = binding.descriptorType,
|
||||
.descriptorCount = binding.descriptorCount * descriptorsets_per_pool,
|
||||
});
|
||||
}
|
||||
if (auto it = std::find_if(size.begin(), size.end(), [&binding](vk::DescriptorPoolSize & x) { return x.type == binding.descriptorType; }); it != size.end())
|
||||
{
|
||||
it->descriptorCount += binding.descriptorCount * descriptorsets_per_pool;
|
||||
}
|
||||
else if (binding.descriptorCount > 0)
|
||||
{
|
||||
size.push_back(vk::DescriptorPoolSize{
|
||||
.type = binding.descriptorType,
|
||||
.descriptorCount = binding.descriptorCount * descriptorsets_per_pool,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
layout_ = *layout;
|
||||
@@ -91,8 +97,8 @@ std::shared_ptr<vk::raii::DescriptorSet> growable_descriptor_pool::allocate()
|
||||
pool_info.setPoolSizes(size);
|
||||
|
||||
pools.push_back(pool{
|
||||
.free_count = descriptorsets_per_pool - 1,
|
||||
.descriptor_pool = vk::raii::DescriptorPool(device, pool_info),
|
||||
.free_count = descriptorsets_per_pool - 1,
|
||||
.descriptor_pool = vk::raii::DescriptorPool(device, pool_info),
|
||||
});
|
||||
|
||||
alloc_info.descriptorPool = *pools.back().descriptor_pool;
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <span>
|
||||
#include <vector>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
|
||||
class growable_descriptor_pool
|
||||
@@ -28,8 +28,8 @@ class growable_descriptor_pool
|
||||
|
||||
struct pool
|
||||
{
|
||||
int free_count;
|
||||
vk::raii::DescriptorPool descriptor_pool;
|
||||
int free_count;
|
||||
vk::raii::DescriptorPool descriptor_pool;
|
||||
};
|
||||
|
||||
vk::raii::Device & device;
|
||||
@@ -40,7 +40,7 @@ class growable_descriptor_pool
|
||||
std::vector<pool> pools;
|
||||
|
||||
public:
|
||||
growable_descriptor_pool(vk::raii::Device & device, vk::raii::DescriptorSetLayout& layout, 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();
|
||||
};
|
||||
|
||||
+180
-144
@@ -20,13 +20,12 @@
|
||||
#include "image_loader.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <ktxvulkan.h>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <stdexcept>
|
||||
#include <variant>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
#include <ktxvulkan.h>
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#define STBI_ONLY_JPEG
|
||||
@@ -42,28 +41,28 @@ using stbi_ptr = std::unique_ptr<void, decltype([](void * pixels) { stbi_image_f
|
||||
|
||||
namespace
|
||||
{
|
||||
struct image_resources
|
||||
{
|
||||
image_allocation allocation;
|
||||
vk::Image image;
|
||||
vk::raii::ImageView image_view = nullptr;
|
||||
};
|
||||
struct image_resources
|
||||
{
|
||||
image_allocation allocation;
|
||||
vk::Image image;
|
||||
vk::raii::ImageView image_view = nullptr;
|
||||
};
|
||||
|
||||
struct ktx_image_resources
|
||||
{
|
||||
ktxVulkanTexture ktx_texture;
|
||||
VkDevice device;
|
||||
vk::Image image;
|
||||
vk::raii::ImageView image_view = nullptr;
|
||||
struct ktx_image_resources
|
||||
{
|
||||
ktxVulkanTexture ktx_texture;
|
||||
VkDevice device;
|
||||
vk::Image image;
|
||||
vk::raii::ImageView image_view = nullptr;
|
||||
|
||||
ktx_image_resources() = default;
|
||||
ktx_image_resources(const ktx_image_resources&) = delete;
|
||||
ktx_image_resources& operator=(const ktx_image_resources&) = delete;
|
||||
~ktx_image_resources()
|
||||
{
|
||||
ktxVulkanTexture_Destruct(&ktx_texture, device, nullptr);
|
||||
}
|
||||
};
|
||||
ktx_image_resources() = default;
|
||||
ktx_image_resources(const ktx_image_resources &) = delete;
|
||||
ktx_image_resources & operator=(const ktx_image_resources &) = delete;
|
||||
~ktx_image_resources()
|
||||
{
|
||||
ktxVulkanTexture_Destruct(&ktx_texture, device, nullptr);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
constexpr vk::Format formats[4] = {vk::Format::eUndefined, vk::Format::eUndefined, vk::Format::eUndefined, vk::Format::eUndefined};
|
||||
@@ -156,10 +155,10 @@ int bytes_per_pixel(vk::Format format)
|
||||
}
|
||||
} // namespace
|
||||
|
||||
image_loader::image_loader(vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, vk::raii::Queue& queue, vk::raii::CommandPool & cb_pool) :
|
||||
device(device),
|
||||
queue(queue),
|
||||
cb_pool(cb_pool)
|
||||
image_loader::image_loader(vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, vk::raii::Queue & queue, vk::raii::CommandPool & cb_pool) :
|
||||
device(device),
|
||||
queue(queue),
|
||||
cb_pool(cb_pool)
|
||||
{
|
||||
vdi = ktxVulkanDeviceInfo_Create(*physical_device, *device, *queue, *cb_pool, nullptr);
|
||||
}
|
||||
@@ -173,10 +172,10 @@ image_loader::~image_loader()
|
||||
void image_loader::do_load_raw(const void * pixels, vk::Extent3D extent, vk::Format format)
|
||||
{
|
||||
auto cb = std::move(device.allocateCommandBuffers({
|
||||
.commandPool = *cb_pool,
|
||||
.level = vk::CommandBufferLevel::ePrimary,
|
||||
.commandBufferCount = 1,
|
||||
})[0]);
|
||||
.commandPool = *cb_pool,
|
||||
.level = vk::CommandBufferLevel::ePrimary,
|
||||
.commandBufferCount = 1,
|
||||
})[0]);
|
||||
|
||||
cb.begin(vk::CommandBufferBeginInfo{.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit});
|
||||
|
||||
@@ -211,7 +210,7 @@ void image_loader::do_load_raw(const void * pixels, vk::Extent3D extent, vk::For
|
||||
// Allocate image
|
||||
auto r = std::make_shared<image_resources>();
|
||||
r->allocation = image_allocation{
|
||||
device,
|
||||
device,
|
||||
vk::ImageCreateInfo{
|
||||
.imageType = vk::ImageType::e2D,
|
||||
.format = format,
|
||||
@@ -225,40 +224,51 @@ void image_loader::do_load_raw(const void * pixels, vk::Extent3D extent, vk::For
|
||||
VmaAllocationCreateInfo{
|
||||
.flags = 0,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE,
|
||||
}, "image_loader::do_load"};
|
||||
},
|
||||
"image_loader::do_load"};
|
||||
|
||||
r->image = (vk::Image)r->allocation;
|
||||
|
||||
// Transition all mipmap levels layout to eTransferDstOptimal
|
||||
cb.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eTransfer, vk::DependencyFlags{}, {}, {}, vk::ImageMemoryBarrier{
|
||||
.srcAccessMask = vk::AccessFlagBits::eNone,
|
||||
.dstAccessMask = vk::AccessFlagBits::eTransferWrite,
|
||||
.oldLayout = vk::ImageLayout::eUndefined,
|
||||
.newLayout = vk::ImageLayout::eTransferDstOptimal,
|
||||
.image = r->image,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = num_mipmaps,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
});
|
||||
cb.pipelineBarrier(
|
||||
vk::PipelineStageFlagBits::eTransfer,
|
||||
vk::PipelineStageFlagBits::eTransfer,
|
||||
vk::DependencyFlags{},
|
||||
{},
|
||||
{},
|
||||
vk::ImageMemoryBarrier{
|
||||
.srcAccessMask = vk::AccessFlagBits::eNone,
|
||||
.dstAccessMask = vk::AccessFlagBits::eTransferWrite,
|
||||
.oldLayout = vk::ImageLayout::eUndefined,
|
||||
.newLayout = vk::ImageLayout::eTransferDstOptimal,
|
||||
.image = r->image,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = num_mipmaps,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
});
|
||||
|
||||
// Copy image data
|
||||
cb.copyBufferToImage(staging_buffer, r->image, vk::ImageLayout::eTransferDstOptimal, vk::BufferImageCopy{
|
||||
.bufferOffset = 0,
|
||||
.bufferRowLength = 0,
|
||||
.bufferImageHeight = 0,
|
||||
.imageSubresource = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.mipLevel = 0,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
.imageOffset = {0, 0, 0},
|
||||
.imageExtent = extent,
|
||||
});
|
||||
cb.copyBufferToImage(
|
||||
staging_buffer,
|
||||
r->image,
|
||||
vk::ImageLayout::eTransferDstOptimal,
|
||||
vk::BufferImageCopy{
|
||||
.bufferOffset = 0,
|
||||
.bufferRowLength = 0,
|
||||
.bufferImageHeight = 0,
|
||||
.imageSubresource = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.mipLevel = 0,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
.imageOffset = {0, 0, 0},
|
||||
.imageExtent = extent,
|
||||
});
|
||||
|
||||
// Create mipmaps
|
||||
int width = extent.width;
|
||||
@@ -269,71 +279,94 @@ void image_loader::do_load_raw(const void * pixels, vk::Extent3D extent, vk::For
|
||||
int next_height = height > 1 ? height / 2 : 1;
|
||||
|
||||
// Transition source image layout to eTransferSrcOptimal
|
||||
cb.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eFragmentShader, vk::DependencyFlags{}, {}, {}, vk::ImageMemoryBarrier{
|
||||
.srcAccessMask = vk::AccessFlagBits::eTransferWrite,
|
||||
.dstAccessMask = vk::AccessFlagBits::eShaderRead,
|
||||
.oldLayout = vk::ImageLayout::eTransferDstOptimal,
|
||||
.newLayout = vk::ImageLayout::eTransferSrcOptimal,
|
||||
.image = r->image,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = level - 1,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
});
|
||||
cb.pipelineBarrier(
|
||||
vk::PipelineStageFlagBits::eTransfer,
|
||||
vk::PipelineStageFlagBits::eFragmentShader,
|
||||
vk::DependencyFlags{},
|
||||
{},
|
||||
{},
|
||||
vk::ImageMemoryBarrier{
|
||||
.srcAccessMask = vk::AccessFlagBits::eTransferWrite,
|
||||
.dstAccessMask = vk::AccessFlagBits::eShaderRead,
|
||||
.oldLayout = vk::ImageLayout::eTransferDstOptimal,
|
||||
.newLayout = vk::ImageLayout::eTransferSrcOptimal,
|
||||
.image = r->image,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = level - 1,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
});
|
||||
|
||||
// Blit level n-1 to level n
|
||||
cb.blitImage(r->image, vk::ImageLayout::eTransferSrcOptimal, r->image, vk::ImageLayout::eTransferDstOptimal, vk::ImageBlit{
|
||||
.srcSubresource = {.aspectMask = vk::ImageAspectFlagBits::eColor, .mipLevel = level - 1, .baseArrayLayer = 0, .layerCount = 1},
|
||||
.srcOffsets = std::array{
|
||||
vk::Offset3D{0, 0, 0},
|
||||
vk::Offset3D{width, height, 1},
|
||||
},
|
||||
.dstSubresource = {.aspectMask = vk::ImageAspectFlagBits::eColor, .mipLevel = level, .baseArrayLayer = 0, .layerCount = 1},
|
||||
.dstOffsets = std::array{
|
||||
vk::Offset3D{0, 0, 0},
|
||||
vk::Offset3D{next_width, next_height, 1},
|
||||
},
|
||||
},
|
||||
vk::Filter::eLinear);
|
||||
cb.blitImage(
|
||||
r->image,
|
||||
vk::ImageLayout::eTransferSrcOptimal,
|
||||
r->image,
|
||||
vk::ImageLayout::eTransferDstOptimal,
|
||||
vk::ImageBlit{
|
||||
.srcSubresource = {.aspectMask = vk::ImageAspectFlagBits::eColor, .mipLevel = level - 1, .baseArrayLayer = 0, .layerCount = 1},
|
||||
.srcOffsets = std::array{
|
||||
vk::Offset3D{0, 0, 0},
|
||||
vk::Offset3D{width, height, 1},
|
||||
},
|
||||
.dstSubresource = {.aspectMask = vk::ImageAspectFlagBits::eColor, .mipLevel = level, .baseArrayLayer = 0, .layerCount = 1},
|
||||
.dstOffsets = std::array{
|
||||
vk::Offset3D{0, 0, 0},
|
||||
vk::Offset3D{next_width, next_height, 1},
|
||||
},
|
||||
},
|
||||
vk::Filter::eLinear);
|
||||
|
||||
// Transition source image layout to eShaderReadOnlyOptimal
|
||||
cb.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eFragmentShader, vk::DependencyFlags{}, {}, {}, vk::ImageMemoryBarrier{
|
||||
.srcAccessMask = vk::AccessFlagBits::eTransferRead,
|
||||
.dstAccessMask = vk::AccessFlagBits::eShaderRead,
|
||||
.oldLayout = vk::ImageLayout::eTransferSrcOptimal,
|
||||
.newLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
|
||||
.image = r->image,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = level - 1,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
});
|
||||
cb.pipelineBarrier(
|
||||
vk::PipelineStageFlagBits::eTransfer,
|
||||
vk::PipelineStageFlagBits::eFragmentShader,
|
||||
vk::DependencyFlags{},
|
||||
{},
|
||||
{},
|
||||
vk::ImageMemoryBarrier{
|
||||
.srcAccessMask = vk::AccessFlagBits::eTransferRead,
|
||||
.dstAccessMask = vk::AccessFlagBits::eShaderRead,
|
||||
.oldLayout = vk::ImageLayout::eTransferSrcOptimal,
|
||||
.newLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
|
||||
.image = r->image,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = level - 1,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
});
|
||||
|
||||
width = next_width;
|
||||
height = next_height;
|
||||
}
|
||||
|
||||
// Transition the last level to eShaderReadOnlyOptimal
|
||||
cb.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eFragmentShader, vk::DependencyFlags{}, {}, {}, vk::ImageMemoryBarrier{
|
||||
.srcAccessMask = vk::AccessFlagBits::eTransferWrite,
|
||||
.dstAccessMask = vk::AccessFlagBits::eShaderRead,
|
||||
.oldLayout = vk::ImageLayout::eTransferDstOptimal,
|
||||
.newLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
|
||||
.image = r->image,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = num_mipmaps - 1,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
});
|
||||
cb.pipelineBarrier(
|
||||
vk::PipelineStageFlagBits::eTransfer,
|
||||
vk::PipelineStageFlagBits::eFragmentShader,
|
||||
vk::DependencyFlags{},
|
||||
{},
|
||||
{},
|
||||
vk::ImageMemoryBarrier{
|
||||
.srcAccessMask = vk::AccessFlagBits::eTransferWrite,
|
||||
.dstAccessMask = vk::AccessFlagBits::eShaderRead,
|
||||
.oldLayout = vk::ImageLayout::eTransferDstOptimal,
|
||||
.newLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
|
||||
.image = r->image,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = num_mipmaps - 1,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
});
|
||||
|
||||
cb.end();
|
||||
vk::SubmitInfo info;
|
||||
@@ -343,28 +376,31 @@ void image_loader::do_load_raw(const void * pixels, vk::Extent3D extent, vk::For
|
||||
if (auto result = device.waitForFences(*fence, true, 1'000'000'000); result != vk::Result::eSuccess)
|
||||
throw std::runtime_error("vkWaitForfences: " + vk::to_string(result));
|
||||
|
||||
r->image_view = vk::raii::ImageView{device, vk::ImageViewCreateInfo{
|
||||
.image = r->image,
|
||||
.viewType = image_view_type,
|
||||
.format = format,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = num_mipmaps,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
}};
|
||||
r->image_view = vk::raii::ImageView{
|
||||
device,
|
||||
vk::ImageViewCreateInfo{
|
||||
.image = r->image,
|
||||
.viewType = image_view_type,
|
||||
.format = format,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = num_mipmaps,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
image_view = std::shared_ptr<vk::raii::ImageView>(r, &r->image_view);
|
||||
}
|
||||
|
||||
void image_loader::do_load_ktx(std::span<const std::byte> bytes)
|
||||
{
|
||||
ktxTexture* texture;
|
||||
ktxTexture * texture;
|
||||
ktxVulkanTexture vk_texture;
|
||||
|
||||
ktxResult err = ktxTexture_CreateFromMemory(reinterpret_cast<const uint8_t*>(bytes.data()), bytes.size(), /*KTX_TEXTURE_CREATE_CHECK_GLTF_BASISU_BIT*/0, &texture);
|
||||
ktxResult err = ktxTexture_CreateFromMemory(reinterpret_cast<const uint8_t *>(bytes.data()), bytes.size(), /*KTX_TEXTURE_CREATE_CHECK_GLTF_BASISU_BIT*/ 0, &texture);
|
||||
|
||||
if (err != KTX_SUCCESS)
|
||||
{
|
||||
@@ -377,10 +413,7 @@ void image_loader::do_load_ktx(std::span<const std::byte> bytes)
|
||||
// TODO
|
||||
}
|
||||
|
||||
err = ktxTexture_VkUploadEx(texture, vdi, &vk_texture,
|
||||
VK_IMAGE_TILING_OPTIMAL,
|
||||
VK_IMAGE_USAGE_SAMPLED_BIT,
|
||||
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
||||
err = ktxTexture_VkUploadEx(texture, vdi, &vk_texture, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
||||
if (err != KTX_SUCCESS)
|
||||
{
|
||||
// TODO try uncompressing the texture
|
||||
@@ -401,18 +434,21 @@ void image_loader::do_load_ktx(std::span<const std::byte> bytes)
|
||||
r->ktx_texture = vk_texture;
|
||||
r->device = *device;
|
||||
r->image = vk::Image(vk_texture.image);
|
||||
r->image_view = vk::raii::ImageView{device, vk::ImageViewCreateInfo{
|
||||
.image = r->image,
|
||||
.viewType = image_view_type,
|
||||
.format = format,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = num_mipmaps,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
}};
|
||||
r->image_view = vk::raii::ImageView{
|
||||
device,
|
||||
vk::ImageViewCreateInfo{
|
||||
.image = r->image,
|
||||
.viewType = image_view_type,
|
||||
.format = format,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = num_mipmaps,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
image_view = std::shared_ptr<vk::raii::ImageView>(r, &r->image_view);
|
||||
}
|
||||
|
||||
@@ -20,15 +20,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "vk/allocation.h"
|
||||
#include "vk/fwd.h"
|
||||
#include <cstddef>
|
||||
#include <glm/vec2.hpp>
|
||||
#include <glm/vec3.hpp>
|
||||
#include <glm/vec4.hpp>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
#include <tuple>
|
||||
#include <vulkan/vulkan.hpp>
|
||||
#include "vk/fwd.h"
|
||||
|
||||
struct ktxVulkanDeviceInfo;
|
||||
|
||||
@@ -51,20 +50,19 @@ struct image_loader
|
||||
// Load raw pixel data
|
||||
void load(const void * pixels, size_t size, vk::Extent3D extent, vk::Format format);
|
||||
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
void load(std::span<T> pixels, vk::Extent3D extent, vk::Format format)
|
||||
{
|
||||
load(pixels.data(), pixels.size() * sizeof(T), extent, format);
|
||||
}
|
||||
|
||||
template<typename T, size_t N>
|
||||
template <typename T, size_t N>
|
||||
void load(const std::array<T, N> & pixels, vk::Extent3D extent, vk::Format format)
|
||||
{
|
||||
load(pixels.data(), pixels.size() * sizeof(T), extent, format);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
void load(const std::vector<T> & pixels, vk::Extent3D extent, vk::Format format)
|
||||
{
|
||||
load(pixels.data(), pixels.size() * sizeof(T), extent, format);
|
||||
|
||||
+210
-212
@@ -21,21 +21,21 @@
|
||||
|
||||
#include "application.h"
|
||||
#include "asset.h"
|
||||
#include "image_loader.h"
|
||||
#include "openxr/openxr.h"
|
||||
#include "utils/ranges.h"
|
||||
#include "vulkan/vulkan_enums.hpp"
|
||||
#include "vulkan/vulkan_handles.hpp"
|
||||
#include "vulkan/vulkan_to_string.hpp"
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <imgui.h>
|
||||
#include <imgui_internal.h>
|
||||
#include <backends/imgui_impl_vulkan.h>
|
||||
#include <glm/gtc/matrix_access.hpp>
|
||||
#include <algorithm>
|
||||
#include <backends/imgui_impl_vulkan.h>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <glm/gtc/matrix_access.hpp>
|
||||
#include <imgui.h>
|
||||
#include <imgui_internal.h>
|
||||
#include <optional>
|
||||
#include "image_loader.h"
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include "IconsFontAwesome6.h"
|
||||
|
||||
@@ -51,47 +51,48 @@
|
||||
* struct ImGui_ImplVulkanH_Window
|
||||
*/
|
||||
|
||||
static vk::raii::RenderPass create_renderpass(vk::raii::Device& device, vk::Format format, bool clear)
|
||||
static vk::raii::RenderPass create_renderpass(vk::raii::Device & device, vk::Format format, bool clear)
|
||||
{
|
||||
vk::AttachmentDescription attachment{
|
||||
.format = format,
|
||||
.samples = vk::SampleCountFlagBits::e1,
|
||||
.loadOp = clear ? vk::AttachmentLoadOp::eClear : vk::AttachmentLoadOp::eDontCare,
|
||||
.storeOp = vk::AttachmentStoreOp::eStore,
|
||||
.stencilLoadOp = vk::AttachmentLoadOp::eDontCare,
|
||||
.stencilStoreOp = vk::AttachmentStoreOp::eDontCare,
|
||||
.initialLayout = vk::ImageLayout::eUndefined,
|
||||
.finalLayout = vk::ImageLayout::eColorAttachmentOptimal,
|
||||
vk::AttachmentDescription attachment{
|
||||
.format = format,
|
||||
.samples = vk::SampleCountFlagBits::e1,
|
||||
.loadOp = clear ? vk::AttachmentLoadOp::eClear : vk::AttachmentLoadOp::eDontCare,
|
||||
.storeOp = vk::AttachmentStoreOp::eStore,
|
||||
.stencilLoadOp = vk::AttachmentLoadOp::eDontCare,
|
||||
.stencilStoreOp = vk::AttachmentStoreOp::eDontCare,
|
||||
.initialLayout = vk::ImageLayout::eUndefined,
|
||||
.finalLayout = vk::ImageLayout::eColorAttachmentOptimal,
|
||||
};
|
||||
|
||||
vk::AttachmentReference color_attachment{
|
||||
.attachment = 0,
|
||||
.layout = vk::ImageLayout::eColorAttachmentOptimal,
|
||||
.attachment = 0,
|
||||
.layout = vk::ImageLayout::eColorAttachmentOptimal,
|
||||
};
|
||||
|
||||
vk::SubpassDescription subpass = {
|
||||
.pipelineBindPoint = vk::PipelineBindPoint::eGraphics,
|
||||
.colorAttachmentCount = 1,
|
||||
.pColorAttachments = &color_attachment,
|
||||
.pipelineBindPoint = vk::PipelineBindPoint::eGraphics,
|
||||
.colorAttachmentCount = 1,
|
||||
.pColorAttachments = &color_attachment,
|
||||
};
|
||||
|
||||
vk::SubpassDependency dependency = {
|
||||
.srcSubpass = VK_SUBPASS_EXTERNAL,
|
||||
.dstSubpass = 0,
|
||||
.srcStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput,
|
||||
.dstStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput,
|
||||
.srcAccessMask = {},
|
||||
.dstAccessMask = vk::AccessFlagBits::eColorAttachmentWrite,
|
||||
.srcSubpass = VK_SUBPASS_EXTERNAL,
|
||||
.dstSubpass = 0,
|
||||
.srcStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput,
|
||||
.dstStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput,
|
||||
.srcAccessMask = {},
|
||||
.dstAccessMask = vk::AccessFlagBits::eColorAttachmentWrite,
|
||||
};
|
||||
|
||||
return vk::raii::RenderPass(device, vk::RenderPassCreateInfo{
|
||||
.attachmentCount = 1,
|
||||
.pAttachments = &attachment,
|
||||
.subpassCount = 1,
|
||||
.pSubpasses = &subpass,
|
||||
.dependencyCount = 1,
|
||||
.pDependencies = &dependency,
|
||||
});
|
||||
return vk::raii::RenderPass(
|
||||
device, vk::RenderPassCreateInfo{
|
||||
.attachmentCount = 1,
|
||||
.pAttachments = &attachment,
|
||||
.subpassCount = 1,
|
||||
.pSubpasses = &subpass,
|
||||
.dependencyCount = 1,
|
||||
.pDependencies = &dependency,
|
||||
});
|
||||
}
|
||||
|
||||
static void check_vk_result(VkResult result)
|
||||
@@ -103,7 +104,7 @@ static void check_vk_result(VkResult result)
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::pair<ImVec2, float>> imgui_context::ray_plane_intersection(const imgui_context::controller_state& in)
|
||||
std::optional<std::pair<ImVec2, float>> imgui_context::ray_plane_intersection(const imgui_context::controller_state & in)
|
||||
{
|
||||
if (!in.active)
|
||||
return {};
|
||||
@@ -131,133 +132,129 @@ std::optional<std::pair<ImVec2, float>> imgui_context::ray_plane_intersection(co
|
||||
coord = coord / scale_;
|
||||
|
||||
if (fabs(coord.x) <= 0.5 && fabs(coord.y) <= 0.5)
|
||||
return std::make_pair(ImVec2
|
||||
(
|
||||
(0.5 + coord.x) * size.width,
|
||||
(0.5 - coord.y) * size.height
|
||||
),
|
||||
-lambda);
|
||||
return std::make_pair(ImVec2(
|
||||
(0.5 + coord.x) * size.width,
|
||||
(0.5 - coord.y) * size.height),
|
||||
-lambda);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
imgui_context::imgui_frame& imgui_context::get_frame(vk::Image destination)
|
||||
imgui_context::imgui_frame & imgui_context::get_frame(vk::Image destination)
|
||||
{
|
||||
for(auto& i: frames)
|
||||
for (auto & i: frames)
|
||||
{
|
||||
if (i.destination == destination)
|
||||
return i;
|
||||
}
|
||||
|
||||
auto& frame = frames.emplace_back();
|
||||
auto & frame = frames.emplace_back();
|
||||
|
||||
frame.destination = destination;
|
||||
|
||||
// Only 1 mipmap level for the framebuffer view
|
||||
frame.image_view_framebuffer = vk::raii::ImageView(device, vk::ImageViewCreateInfo{
|
||||
.image = destination,
|
||||
.viewType = vk::ImageViewType::e2D,
|
||||
.format = format,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
}
|
||||
});
|
||||
|
||||
frame.framebuffer = vk::raii::Framebuffer(device, vk::FramebufferCreateInfo{
|
||||
.renderPass = *renderpass,
|
||||
.attachmentCount = 1,
|
||||
.pAttachments = &*frame.image_view_framebuffer,
|
||||
.width = size.width,
|
||||
.height = size.height,
|
||||
.layers = 1,
|
||||
});
|
||||
frame.image_view_framebuffer = vk::raii::ImageView(
|
||||
device,
|
||||
vk::ImageViewCreateInfo{
|
||||
.image = destination,
|
||||
.viewType = vk::ImageViewType::e2D,
|
||||
.format = format,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
});
|
||||
|
||||
frame.framebuffer = vk::raii::Framebuffer(
|
||||
device,
|
||||
vk::FramebufferCreateInfo{
|
||||
.renderPass = *renderpass,
|
||||
.attachmentCount = 1,
|
||||
.pAttachments = &*frame.image_view_framebuffer,
|
||||
.width = size.width,
|
||||
.height = size.height,
|
||||
.layers = 1,
|
||||
});
|
||||
|
||||
return frame;
|
||||
}
|
||||
|
||||
static const std::array pool_sizes =
|
||||
{
|
||||
vk::DescriptorPoolSize{
|
||||
.type = vk::DescriptorType::eCombinedImageSampler,
|
||||
.descriptorCount = 100,
|
||||
}
|
||||
};
|
||||
{
|
||||
vk::DescriptorPoolSize{
|
||||
.type = vk::DescriptorType::eCombinedImageSampler,
|
||||
.descriptorCount = 100,
|
||||
}};
|
||||
|
||||
static const vk::DescriptorSetLayoutBinding layout_bindings{
|
||||
.binding = 0,
|
||||
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
|
||||
.descriptorCount = 1,
|
||||
.stageFlags = vk::ShaderStageFlagBits::eFragment
|
||||
};
|
||||
.binding = 0,
|
||||
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
|
||||
.descriptorCount = 1,
|
||||
.stageFlags = vk::ShaderStageFlagBits::eFragment};
|
||||
|
||||
imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii::Device& device, uint32_t queue_family_index, vk::raii::Queue& queue, XrSpace world, std::span<controller> controllers_, xr::swapchain& swapchain, glm::vec2 size) :
|
||||
physical_device(physical_device),
|
||||
device(device),
|
||||
queue_family_index(queue_family_index),
|
||||
queue(queue),
|
||||
descriptor_pool(device, vk::DescriptorPoolCreateInfo
|
||||
{
|
||||
.flags = vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet,
|
||||
.maxSets = pool_sizes[0].descriptorCount,
|
||||
.poolSizeCount = pool_sizes.size(),
|
||||
.pPoolSizes = pool_sizes.data(),
|
||||
}),
|
||||
ds_layout(device, vk::DescriptorSetLayoutCreateInfo
|
||||
{
|
||||
.bindingCount = 1,
|
||||
.pBindings = &layout_bindings
|
||||
}),
|
||||
renderpass(create_renderpass(device, swapchain.format(), true)),
|
||||
command_pool(device, vk::CommandPoolCreateInfo{
|
||||
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer | vk::CommandPoolCreateFlagBits::eTransient,
|
||||
.queueFamilyIndex = queue_family_index,
|
||||
}),
|
||||
command_buffers(swapchain.images().size()),
|
||||
size(swapchain.extent().width, swapchain.extent().height),
|
||||
format(swapchain.format()),
|
||||
scale_(size.x, size.y),
|
||||
swapchain(swapchain),
|
||||
context(ImGui::CreateContext()),
|
||||
plot_context(ImPlot::CreateContext()),
|
||||
io((ImGui::SetCurrentContext(context), ImGui::GetIO())),
|
||||
world(world)
|
||||
imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii::Device & device, uint32_t queue_family_index, vk::raii::Queue & queue, XrSpace world, std::span<controller> controllers_, xr::swapchain & swapchain, glm::vec2 size) :
|
||||
physical_device(physical_device),
|
||||
device(device),
|
||||
queue_family_index(queue_family_index),
|
||||
queue(queue),
|
||||
descriptor_pool(device,
|
||||
vk::DescriptorPoolCreateInfo{
|
||||
.flags = vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet,
|
||||
.maxSets = pool_sizes[0].descriptorCount,
|
||||
.poolSizeCount = pool_sizes.size(),
|
||||
.pPoolSizes = pool_sizes.data(),
|
||||
}),
|
||||
ds_layout(device, vk::DescriptorSetLayoutCreateInfo{.bindingCount = 1, .pBindings = &layout_bindings}),
|
||||
renderpass(create_renderpass(device, swapchain.format(), true)),
|
||||
command_pool(device,
|
||||
vk::CommandPoolCreateInfo{
|
||||
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer | vk::CommandPoolCreateFlagBits::eTransient,
|
||||
.queueFamilyIndex = queue_family_index,
|
||||
}),
|
||||
command_buffers(swapchain.images().size()),
|
||||
size(swapchain.extent().width, swapchain.extent().height),
|
||||
format(swapchain.format()),
|
||||
scale_(size.x, size.y),
|
||||
swapchain(swapchain),
|
||||
context(ImGui::CreateContext()),
|
||||
plot_context(ImPlot::CreateContext()),
|
||||
io((ImGui::SetCurrentContext(context), ImGui::GetIO())),
|
||||
world(world)
|
||||
{
|
||||
controllers.reserve(controllers_.size());
|
||||
for(const auto& i:controllers_)
|
||||
for (const auto & i: controllers_)
|
||||
controllers.emplace_back(i, controller_state{});
|
||||
|
||||
io.IniFilename = nullptr;
|
||||
|
||||
for(command_buffer& cb: command_buffers)
|
||||
for (command_buffer & cb: command_buffers)
|
||||
{
|
||||
cb.command_buffer = std::move(device.allocateCommandBuffers({
|
||||
.commandPool = *command_pool,
|
||||
.commandBufferCount = 1,
|
||||
.commandPool = *command_pool,
|
||||
.commandBufferCount = 1,
|
||||
})[0]);
|
||||
|
||||
cb.fence = device.createFence(vk::FenceCreateInfo{.flags = vk::FenceCreateFlagBits::eSignaled});
|
||||
}
|
||||
|
||||
ImGui_ImplVulkan_InitInfo init_info = {
|
||||
.Instance = *application::get_vulkan_instance(),
|
||||
.PhysicalDevice = *application::get_physical_device(),
|
||||
.Device = *application::get_device(),
|
||||
.QueueFamily = application::queue_family_index(),
|
||||
.Queue = *application::get_queue(),
|
||||
.PipelineCache = *application::get_pipeline_cache(),
|
||||
.DescriptorPool = *descriptor_pool,
|
||||
.Subpass = 0,
|
||||
.MinImageCount = 2,
|
||||
.ImageCount = (uint32_t)swapchain.images().size(), // used to cycle between VkBuffers in ImGui_ImplVulkan_RenderDrawData
|
||||
.MSAASamples = VK_SAMPLE_COUNT_1_BIT,
|
||||
.Allocator = nullptr,
|
||||
.CheckVkResultFn = check_vk_result,
|
||||
.Instance = *application::get_vulkan_instance(),
|
||||
.PhysicalDevice = *application::get_physical_device(),
|
||||
.Device = *application::get_device(),
|
||||
.QueueFamily = application::queue_family_index(),
|
||||
.Queue = *application::get_queue(),
|
||||
.PipelineCache = *application::get_pipeline_cache(),
|
||||
.DescriptorPool = *descriptor_pool,
|
||||
.Subpass = 0,
|
||||
.MinImageCount = 2,
|
||||
.ImageCount = (uint32_t)swapchain.images().size(), // used to cycle between VkBuffers in ImGui_ImplVulkan_RenderDrawData
|
||||
.MSAASamples = VK_SAMPLE_COUNT_1_BIT,
|
||||
.Allocator = nullptr,
|
||||
.CheckVkResultFn = check_vk_result,
|
||||
};
|
||||
|
||||
ImGui::SetCurrentContext(context);
|
||||
@@ -271,29 +268,27 @@ imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii:
|
||||
{
|
||||
ImFontConfig config;
|
||||
config.FontDataOwnedByAtlas = false;
|
||||
io.Fonts->AddFontFromMemoryTTF(const_cast<std::byte*>(roboto.data()), roboto.size(), 30, &config);
|
||||
io.Fonts->AddFontFromMemoryTTF(const_cast<std::byte *>(roboto.data()), roboto.size(), 30, &config);
|
||||
|
||||
config.MergeMode = true;
|
||||
config.GlyphMinAdvanceX = 40; // Use if you want to make the icon monospaced
|
||||
static const ImWchar icon_ranges[] = { ICON_MIN_FA, ICON_MAX_FA, 0 };
|
||||
io.Fonts->AddFontFromMemoryTTF(const_cast<std::byte*>(font_awesome_regular.data()), font_awesome_regular.size(), 30, &config, icon_ranges);
|
||||
io.Fonts->AddFontFromMemoryTTF(const_cast<std::byte*>(font_awesome_solid.data()), font_awesome_solid.size(), 30, &config, icon_ranges);
|
||||
static const ImWchar icon_ranges[] = {ICON_MIN_FA, ICON_MAX_FA, 0};
|
||||
io.Fonts->AddFontFromMemoryTTF(const_cast<std::byte *>(font_awesome_regular.data()), font_awesome_regular.size(), 30, &config, icon_ranges);
|
||||
io.Fonts->AddFontFromMemoryTTF(const_cast<std::byte *>(font_awesome_solid.data()), font_awesome_solid.size(), 30, &config, icon_ranges);
|
||||
}
|
||||
|
||||
{
|
||||
ImFontConfig config;
|
||||
config.FontDataOwnedByAtlas = false;
|
||||
large_font = io.Fonts->AddFontFromMemoryTTF(const_cast<std::byte*>(roboto.data()), roboto.size(), 75, &config);
|
||||
|
||||
large_font = io.Fonts->AddFontFromMemoryTTF(const_cast<std::byte *>(roboto.data()), roboto.size(), 75, &config);
|
||||
}
|
||||
|
||||
|
||||
// Setup Dear ImGui style
|
||||
ImGui::StyleColorsDark();
|
||||
// ImGui::StyleColorsClassic();
|
||||
//ImGui::StyleColorsLight();
|
||||
// ImGui::StyleColorsLight();
|
||||
|
||||
ImGuiStyle& style = ImGui::GetStyle();
|
||||
ImGuiStyle & style = ImGui::GetStyle();
|
||||
|
||||
style.WindowBorderSize = 0;
|
||||
}
|
||||
@@ -313,22 +308,21 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
|
||||
std::vector<controller_state> new_states;
|
||||
|
||||
for(auto&& [index, controller]: utils::enumerate(controllers))
|
||||
for (auto && [index, controller]: utils::enumerate(controllers))
|
||||
{
|
||||
auto& [ctrl, state] = controller;
|
||||
auto & [ctrl, state] = controller;
|
||||
|
||||
controller_state& new_state = new_states.emplace_back();
|
||||
controller_state & new_state = new_states.emplace_back();
|
||||
|
||||
if (auto joints = ctrl.hand.locate(world, display_time))
|
||||
{
|
||||
XrHandJointLocationEXT& index_tip = (*joints)[XR_HAND_JOINT_INDEX_TIP_EXT].first;
|
||||
XrHandJointLocationEXT & index_tip = (*joints)[XR_HAND_JOINT_INDEX_TIP_EXT].first;
|
||||
if (index_tip.locationFlags & XR_SPACE_LOCATION_POSITION_TRACKED_BIT)
|
||||
{
|
||||
new_state.aim_position = {
|
||||
index_tip.pose.position.x,
|
||||
index_tip.pose.position.y,
|
||||
index_tip.pose.position.z
|
||||
};
|
||||
index_tip.pose.position.x,
|
||||
index_tip.pose.position.y,
|
||||
index_tip.pose.position.z};
|
||||
|
||||
new_state.aim_orientation = orientation_;
|
||||
|
||||
@@ -340,7 +334,7 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
new_state.hover_distance = std::abs(position_distance->second);
|
||||
|
||||
if (std::abs(position_distance->second) < 0.1f)
|
||||
new_state.fingertip_hovered = true;
|
||||
new_state.fingertip_hovered = true;
|
||||
else
|
||||
new_state.active = false;
|
||||
|
||||
@@ -363,7 +357,7 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
|
||||
if (ctrl.squeeze)
|
||||
{
|
||||
auto squeeze = application::read_action_float(ctrl.squeeze).value_or(std::pair{0,0});
|
||||
auto squeeze = application::read_action_float(ctrl.squeeze).value_or(std::pair{0, 0});
|
||||
new_state.squeeze_value = squeeze.second;
|
||||
|
||||
// TODO tunable
|
||||
@@ -375,7 +369,7 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
|
||||
if (ctrl.trigger)
|
||||
{
|
||||
auto trigger = application::read_action_float(ctrl.trigger).value_or(std::pair{0,0});
|
||||
auto trigger = application::read_action_float(ctrl.trigger).value_or(std::pair{0, 0});
|
||||
new_state.trigger_value = trigger.second;
|
||||
|
||||
// TODO tunable
|
||||
@@ -388,20 +382,20 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
if (ctrl.scroll)
|
||||
{
|
||||
if (auto act = application::read_action_vec2(ctrl.scroll); act)
|
||||
new_state.scroll_value = { -act->second.x * scroll_scale, act->second.y * scroll_scale };
|
||||
new_state.scroll_value = {-act->second.x * scroll_scale, act->second.y * scroll_scale};
|
||||
else
|
||||
new_state.scroll_value = {0, 0};
|
||||
}
|
||||
}
|
||||
|
||||
for(auto [new_state, controller]: utils::zip(new_states, controllers))
|
||||
for (auto [new_state, controller]: utils::zip(new_states, controllers))
|
||||
{
|
||||
if (new_state.hover_distance < 0.02 && controller.second.hover_distance >= 0.02)
|
||||
new_state.fingertip_touched = true;
|
||||
}
|
||||
|
||||
float closest_hover_distance = 1e10;
|
||||
for(auto&& [index, new_state]: utils::enumerate(new_states))
|
||||
for (auto && [index, new_state]: utils::enumerate(new_states))
|
||||
{
|
||||
if (new_state.hover_distance < closest_hover_distance && new_state.fingertip_hovered)
|
||||
{
|
||||
@@ -434,7 +428,7 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
|
||||
bool last_trigger = controllers[new_focused_controller].second.trigger_clicked || controllers[new_focused_controller].second.fingertip_touched;
|
||||
button_pressed = new_states[new_focused_controller].trigger_clicked ||
|
||||
(new_states[new_focused_controller].fingertip_touched && !controllers[new_focused_controller].second.fingertip_touched);
|
||||
(new_states[new_focused_controller].fingertip_touched && !controllers[new_focused_controller].second.fingertip_touched);
|
||||
|
||||
if (position_distance)
|
||||
{
|
||||
@@ -462,7 +456,7 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
}
|
||||
|
||||
focused_controller = new_focused_controller;
|
||||
for(auto&& [controller, next_state]: utils::zip(controllers, new_states))
|
||||
for (auto && [controller, next_state]: utils::zip(controllers, new_states))
|
||||
{
|
||||
controller.second = next_state;
|
||||
}
|
||||
@@ -477,18 +471,16 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
|
||||
ImGui::NewFrame();
|
||||
|
||||
ImDrawList* draw_list = ImGui::GetForegroundDrawList();
|
||||
ImDrawList * draw_list = ImGui::GetForegroundDrawList();
|
||||
|
||||
if (position_distance)
|
||||
{
|
||||
float distance_to_border = std::min({
|
||||
position_distance->first.x,
|
||||
size.width - position_distance->first.x,
|
||||
position_distance->first.y,
|
||||
size.height - position_distance->first.y
|
||||
});
|
||||
float distance_to_border = std::min({position_distance->first.x,
|
||||
size.width - position_distance->first.x,
|
||||
position_distance->first.y,
|
||||
size.height - position_distance->first.y});
|
||||
|
||||
float radius = 10; //std::clamp<float>(distance_to_border / 4, 0, 10);
|
||||
float radius = 10; // std::clamp<float>(distance_to_border / 4, 0, 10);
|
||||
float alpha = std::clamp<float>((distance_to_border - 10) / 50, 0, 0.8);
|
||||
|
||||
ImU32 color_pressed = ImGui::GetColorU32(ImVec4(0, 0.2, 1, alpha));
|
||||
@@ -497,7 +489,7 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
bool pressed = button_pressed || new_states[new_focused_controller].fingertip_touched;
|
||||
|
||||
draw_list->AddCircleFilled(position_distance->first, radius, pressed ? color_pressed : color_unpressed);
|
||||
draw_list->AddCircle(position_distance->first, radius*1.2, ImGui::GetColorU32(ImVec4(0, 0, 0, alpha)), 0, radius * 0.4);
|
||||
draw_list->AddCircle(position_distance->first, radius * 1.2, ImGui::GetColorU32(ImVec4(0, 0, 0, alpha)), 0, radius * 0.4);
|
||||
}
|
||||
|
||||
image_index = swapchain.acquire();
|
||||
@@ -515,9 +507,9 @@ XrCompositionLayerQuad imgui_context::end_frame()
|
||||
|
||||
current_command_buffer = (current_command_buffer + 1) % command_buffers.size();
|
||||
|
||||
auto& f = get_frame(destination);
|
||||
auto& cb = get_command_buffer().command_buffer;
|
||||
auto& fence = get_command_buffer().fence;
|
||||
auto & f = get_frame(destination);
|
||||
auto & cb = get_command_buffer().command_buffer;
|
||||
auto & fence = get_command_buffer().fence;
|
||||
|
||||
if (auto result = device.waitForFences(*fence, true, 1'000'000'000); result != vk::Result::eSuccess)
|
||||
throw std::runtime_error("vkWaitForfences: " + vk::to_string(result));
|
||||
@@ -528,14 +520,14 @@ XrCompositionLayerQuad imgui_context::end_frame()
|
||||
vk::ClearValue clear{vk::ClearColorValue(0, 0, 0, 0)};
|
||||
|
||||
cb.beginRenderPass(vk::RenderPassBeginInfo{
|
||||
.renderPass = *renderpass,
|
||||
.framebuffer = *f.framebuffer,
|
||||
.renderArea = {
|
||||
.extent = size,
|
||||
},
|
||||
.clearValueCount = 1,
|
||||
.pClearValues = &clear
|
||||
}, vk::SubpassContents::eInline);
|
||||
.renderPass = *renderpass,
|
||||
.framebuffer = *f.framebuffer,
|
||||
.renderArea = {
|
||||
.extent = size,
|
||||
},
|
||||
.clearValueCount = 1,
|
||||
.pClearValues = &clear},
|
||||
vk::SubpassContents::eInline);
|
||||
|
||||
ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), *cb);
|
||||
|
||||
@@ -544,25 +536,26 @@ XrCompositionLayerQuad imgui_context::end_frame()
|
||||
cb.end();
|
||||
|
||||
queue.submit(vk::SubmitInfo{
|
||||
.commandBufferCount = 1,
|
||||
.pCommandBuffers = &*cb,
|
||||
}, *fence);
|
||||
.commandBufferCount = 1,
|
||||
.pCommandBuffers = &*cb,
|
||||
},
|
||||
*fence);
|
||||
|
||||
swapchain.release();
|
||||
|
||||
return XrCompositionLayerQuad{
|
||||
.type = XR_TYPE_COMPOSITION_LAYER_QUAD,
|
||||
.layerFlags = XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT,
|
||||
.space = world,
|
||||
.eyeVisibility = XrEyeVisibility::XR_EYE_VISIBILITY_BOTH,
|
||||
.subImage = {
|
||||
.swapchain = swapchain,
|
||||
.imageRect = {
|
||||
.offset = {0, 0},
|
||||
.extent = {(int32_t)size.width, (int32_t)size.height}},
|
||||
},
|
||||
.pose = pose(),
|
||||
.size = scale(),
|
||||
.type = XR_TYPE_COMPOSITION_LAYER_QUAD,
|
||||
.layerFlags = XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT,
|
||||
.space = world,
|
||||
.eyeVisibility = XrEyeVisibility::XR_EYE_VISIBILITY_BOTH,
|
||||
.subImage = {
|
||||
.swapchain = swapchain,
|
||||
.imageRect = {
|
||||
.offset = {0, 0},
|
||||
.extent = {(int32_t)size.width, (int32_t)size.height}},
|
||||
},
|
||||
.pose = pose(),
|
||||
.size = scale(),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -574,7 +567,7 @@ imgui_context::~imgui_context()
|
||||
std::vector<vk::Fence> fences;
|
||||
|
||||
// Release the command buffers without freing them, they will be destroyed with the command pool
|
||||
for(auto& f: command_buffers)
|
||||
for (auto & f: command_buffers)
|
||||
{
|
||||
f.command_buffer.release();
|
||||
fences.push_back(*f.fence);
|
||||
@@ -589,54 +582,59 @@ imgui_context::~imgui_context()
|
||||
ImGui::DestroyContext(context);
|
||||
}
|
||||
|
||||
|
||||
ImTextureID imgui_context::load_texture(const std::string& filename, vk::raii::Sampler&& sampler)
|
||||
ImTextureID imgui_context::load_texture(const std::string & filename, vk::raii::Sampler && sampler)
|
||||
{
|
||||
bool srgb = true;
|
||||
image_loader loader(physical_device, device, queue, command_pool);
|
||||
loader.load(asset{filename}, srgb);
|
||||
|
||||
vk::raii::DescriptorSet ds = std::move(device.allocateDescriptorSets({
|
||||
.descriptorPool = *descriptor_pool,
|
||||
.descriptorSetCount = 1,
|
||||
.pSetLayouts = &*ds_layout
|
||||
.descriptorPool = *descriptor_pool,
|
||||
.descriptorSetCount = 1,
|
||||
.pSetLayouts = &*ds_layout,
|
||||
})[0]);
|
||||
|
||||
vk::DescriptorImageInfo image_info{
|
||||
.sampler = *sampler,
|
||||
.imageView = **loader.image_view,
|
||||
.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
|
||||
.sampler = *sampler,
|
||||
.imageView = **loader.image_view,
|
||||
.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
|
||||
};
|
||||
|
||||
vk::WriteDescriptorSet ds_write{
|
||||
.dstSet = *ds,
|
||||
.descriptorCount = 1,
|
||||
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
|
||||
.pImageInfo = &image_info
|
||||
};
|
||||
.dstSet = *ds,
|
||||
.descriptorCount = 1,
|
||||
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
|
||||
.pImageInfo = &image_info};
|
||||
|
||||
device.updateDescriptorSets(ds_write, nullptr);
|
||||
|
||||
ImTextureID id = *ds;
|
||||
|
||||
textures.emplace(id, texture_data{
|
||||
.sampler = std::move(sampler),
|
||||
// .image = std::move(loader.image),
|
||||
.image_view = std::move(loader.image_view),
|
||||
.descriptor_set = std::move(ds),
|
||||
});
|
||||
textures.emplace(
|
||||
id,
|
||||
texture_data{
|
||||
.sampler = std::move(sampler),
|
||||
// .image = std::move(loader.image),
|
||||
.image_view = std::move(loader.image_view),
|
||||
.descriptor_set = std::move(ds),
|
||||
});
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
ImTextureID imgui_context::load_texture(const std::string& filename)
|
||||
ImTextureID imgui_context::load_texture(const std::string & filename)
|
||||
{
|
||||
return load_texture(filename, vk::raii::Sampler(device, vk::SamplerCreateInfo{
|
||||
.magFilter = vk::Filter::eLinear,
|
||||
.minFilter = vk::Filter::eLinear,
|
||||
.mipmapMode = vk::SamplerMipmapMode::eLinear,
|
||||
.borderColor = vk::BorderColor::eFloatTransparentBlack,
|
||||
}));
|
||||
return load_texture(
|
||||
filename,
|
||||
vk::raii::Sampler{
|
||||
device,
|
||||
vk::SamplerCreateInfo{
|
||||
.magFilter = vk::Filter::eLinear,
|
||||
.minFilter = vk::Filter::eLinear,
|
||||
.mipmapMode = vk::SamplerMipmapMode::eLinear,
|
||||
.borderColor = vk::BorderColor::eFloatTransparentBlack,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
void imgui_context::free_texture(ImTextureID texture)
|
||||
|
||||
+45
-33
@@ -18,19 +18,19 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "xr/swapchain.h"
|
||||
#include "xr/hand_tracker.h"
|
||||
#include <imgui.h>
|
||||
#include <implot.h>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
#include "xr/swapchain.h"
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
#include <openxr/openxr.h>
|
||||
#include <imgui.h>
|
||||
#include <implot.h>
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
#include <span>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
#include <openxr/openxr.h>
|
||||
|
||||
class imgui_context
|
||||
{
|
||||
@@ -63,7 +63,7 @@ public:
|
||||
XrAction scroll; // XR_ACTION_TYPE_VECTOR2F_INPUT
|
||||
// TODO: thresholds?
|
||||
|
||||
xr::hand_tracker& hand;
|
||||
xr::hand_tracker & hand;
|
||||
};
|
||||
|
||||
struct controller_state
|
||||
@@ -87,9 +87,9 @@ public:
|
||||
|
||||
private:
|
||||
vk::raii::PhysicalDevice physical_device;
|
||||
vk::raii::Device& device;
|
||||
vk::raii::Device & device;
|
||||
uint32_t queue_family_index;
|
||||
vk::raii::Queue& queue;
|
||||
vk::raii::Queue & queue;
|
||||
|
||||
vk::raii::Pipeline pipeline = nullptr;
|
||||
vk::raii::DescriptorPool descriptor_pool;
|
||||
@@ -100,29 +100,29 @@ private:
|
||||
std::unordered_map<ImTextureID, texture_data> textures;
|
||||
|
||||
std::vector<imgui_frame> frames;
|
||||
imgui_frame& get_frame(vk::Image destination);
|
||||
imgui_frame & get_frame(vk::Image destination);
|
||||
|
||||
std::vector<command_buffer> command_buffers;
|
||||
size_t current_command_buffer = 0;
|
||||
command_buffer& get_command_buffer()
|
||||
command_buffer & get_command_buffer()
|
||||
{
|
||||
return command_buffers[current_command_buffer];
|
||||
}
|
||||
|
||||
vk::Extent2D size;
|
||||
vk::Format format;
|
||||
vk::ClearValue clear_value = {vk::ClearColorValue{0,0,0,0}};
|
||||
vk::ClearValue clear_value = {vk::ClearColorValue{0, 0, 0, 0}};
|
||||
|
||||
glm::vec3 position_ = {0, 1, -1.5};
|
||||
glm::quat orientation_ = {1, 0, 0, 0};
|
||||
glm::vec2 scale_;
|
||||
|
||||
xr::swapchain& swapchain;
|
||||
xr::swapchain & swapchain;
|
||||
int image_index;
|
||||
|
||||
ImGuiContext * context;
|
||||
ImPlotContext * plot_context;
|
||||
ImGuiIO& io;
|
||||
ImGuiIO & io;
|
||||
|
||||
std::vector<std::pair<controller, controller_state>> controllers;
|
||||
XrSpace world;
|
||||
@@ -131,10 +131,19 @@ private:
|
||||
|
||||
bool button_pressed = false;
|
||||
|
||||
std::optional<std::pair<ImVec2, float>> ray_plane_intersection(const imgui_context::controller_state& in);
|
||||
std::optional<std::pair<ImVec2, float>> ray_plane_intersection(const imgui_context::controller_state & in);
|
||||
|
||||
public:
|
||||
imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii::Device& device, uint32_t queue_family_index, vk::raii::Queue& queue, XrSpace world, std::span<controller> controllers, xr::swapchain& swapchain, glm::vec2 size);
|
||||
imgui_context(
|
||||
vk::raii::PhysicalDevice physical_device,
|
||||
vk::raii::Device & device,
|
||||
uint32_t queue_family_index,
|
||||
vk::raii::Queue & queue,
|
||||
XrSpace world,
|
||||
std::span<controller> controllers,
|
||||
xr::swapchain & swapchain,
|
||||
glm::vec2 size);
|
||||
|
||||
~imgui_context();
|
||||
|
||||
void set_position(glm::vec3 position, glm::quat orientation)
|
||||
@@ -146,26 +155,26 @@ public:
|
||||
XrPosef pose() const
|
||||
{
|
||||
return XrPosef{
|
||||
.orientation = {
|
||||
.x = orientation_.x,
|
||||
.y = orientation_.y,
|
||||
.z = orientation_.z,
|
||||
.w = orientation_.w,
|
||||
},
|
||||
.position = {
|
||||
.x = position_.x,
|
||||
.y = position_.y,
|
||||
.z = position_.z,
|
||||
}
|
||||
.orientation = {
|
||||
.x = orientation_.x,
|
||||
.y = orientation_.y,
|
||||
.z = orientation_.z,
|
||||
.w = orientation_.w,
|
||||
},
|
||||
.position = {
|
||||
.x = position_.x,
|
||||
.y = position_.y,
|
||||
.z = position_.z,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
glm::vec3& position()
|
||||
glm::vec3 & position()
|
||||
{
|
||||
return position_;
|
||||
}
|
||||
|
||||
glm::quat& orientation()
|
||||
glm::quat & orientation()
|
||||
{
|
||||
return orientation_;
|
||||
}
|
||||
@@ -182,7 +191,10 @@ public:
|
||||
|
||||
XrExtent2Df scale() const
|
||||
{
|
||||
return { scale_.x, scale_.y };
|
||||
return {
|
||||
scale_.x,
|
||||
scale_.y,
|
||||
};
|
||||
}
|
||||
|
||||
void new_frame(XrTime display_time);
|
||||
@@ -194,8 +206,8 @@ public:
|
||||
return focused_controller;
|
||||
}
|
||||
|
||||
ImTextureID load_texture(const std::string& filename, vk::raii::Sampler&& sampler);
|
||||
ImTextureID load_texture(const std::string& filename);
|
||||
ImTextureID load_texture(const std::string & filename, vk::raii::Sampler && sampler);
|
||||
ImTextureID load_texture(const std::string & filename);
|
||||
void free_texture(ImTextureID);
|
||||
void set_current();
|
||||
};
|
||||
|
||||
@@ -140,8 +140,6 @@ fastgltf::Asset load_gltf_asset(fastgltf::GltfDataBuffer & buffer, const std::fi
|
||||
fastgltf::Options::DontRequireValidAssetMember |
|
||||
fastgltf::Options::AllowDouble |
|
||||
fastgltf::Options::LoadGLBBuffers |
|
||||
// fastgltf::Options::LoadExternalBuffers |
|
||||
// fastgltf::Options::LoadExternalImages |
|
||||
fastgltf::Options::DecomposeNodeMatrices;
|
||||
|
||||
fastgltf::Expected<fastgltf::Asset> expected_asset{fastgltf::Error::None};
|
||||
@@ -322,8 +320,8 @@ constexpr bool starts_with(std::span<const std::byte> data, std::span<const uint
|
||||
|
||||
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};
|
||||
const uint8_t jpeg_magic[] = {0xFF, 0xD8, 0xFF};
|
||||
const uint8_t png_magic[] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
|
||||
const uint8_t ktx1_magic[] = {0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A};
|
||||
const uint8_t ktx2_magic[] = {0xAB, 0x4B, 0x54, 0x58, 0x20, 0x32, 0x30, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A};
|
||||
|
||||
@@ -342,7 +340,7 @@ fastgltf::MimeType guess_mime_type(std::span<const std::byte> image_data)
|
||||
std::shared_ptr<vk::raii::ImageView> do_load_image(
|
||||
vk::raii::PhysicalDevice & physical_device,
|
||||
vk::raii::Device & device,
|
||||
vk::raii::Queue & queue,
|
||||
vk::raii::Queue & queue,
|
||||
vk::raii::CommandPool & cb_pool,
|
||||
std::span<const std::byte> image_data,
|
||||
bool srgb)
|
||||
@@ -352,7 +350,6 @@ std::shared_ptr<vk::raii::ImageView> do_load_image(
|
||||
case fastgltf::MimeType::JPEG:
|
||||
case fastgltf::MimeType::PNG:
|
||||
case fastgltf::MimeType::KTX2: {
|
||||
|
||||
try
|
||||
{
|
||||
image_loader loader(physical_device, device, queue, cb_pool);
|
||||
@@ -361,7 +358,7 @@ std::shared_ptr<vk::raii::ImageView> do_load_image(
|
||||
spdlog::debug("Loaded image {}x{}, format {}, {} mipmaps", loader.extent.width, loader.extent.height, vk::to_string(loader.format), loader.num_mipmaps);
|
||||
return loader.image_view;
|
||||
}
|
||||
catch(std::exception& e)
|
||||
catch (std::exception & e)
|
||||
{
|
||||
spdlog::info("Cannot load image: {}", e.what());
|
||||
return {};
|
||||
@@ -372,7 +369,6 @@ std::shared_ptr<vk::raii::ImageView> do_load_image(
|
||||
spdlog::error("Unsupported image MIME type");
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class loader_context
|
||||
@@ -406,7 +402,7 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
fastgltf::span<const std::byte> load(const fastgltf::URI& uri)
|
||||
fastgltf::span<const std::byte> load(const fastgltf::URI & uri)
|
||||
{
|
||||
auto path = base_directory.empty() ? std::filesystem::path(uri.path()) : base_directory / std::filesystem::path(uri.path());
|
||||
std::span<const std::byte> bytes;
|
||||
@@ -416,12 +412,12 @@ public:
|
||||
return fastgltf::span<const std::byte>(bytes.data(), bytes.size());
|
||||
}
|
||||
|
||||
fastgltf::span<const std::byte> load(const fastgltf::sources::URI& uri)
|
||||
fastgltf::span<const std::byte> load(const fastgltf::sources::URI & uri)
|
||||
{
|
||||
return load(uri.uri);
|
||||
}
|
||||
|
||||
template<typename T, std::size_t Extent>
|
||||
template <typename T, std::size_t Extent>
|
||||
fastgltf::span<T, fastgltf::dynamic_extent> subspan(fastgltf::span<T, Extent> span, size_t offset, size_t count = fastgltf::dynamic_extent)
|
||||
{
|
||||
assert(offset < span.size());
|
||||
@@ -449,7 +445,7 @@ public:
|
||||
|
||||
fastgltf::Buffer & buffer = gltf.buffers.at(view.bufferIndex);
|
||||
|
||||
auto buffer2 = visit_source(buffer.data);
|
||||
auto buffer2 = visit_source(buffer.data);
|
||||
|
||||
return {subspan(buffer2.bytes, view.byteOffset, view.byteLength), buffer_view.mimeType};
|
||||
},
|
||||
@@ -457,7 +453,7 @@ public:
|
||||
if (!uri.uri.isLocalPath())
|
||||
throw std::runtime_error("Non local paths are not supported"); // TODO ?
|
||||
|
||||
fastgltf::span<const std::byte> data = load(uri);
|
||||
fastgltf::span<const std::byte> data = load(uri);
|
||||
|
||||
// Don't use the MIME type from fastgltf, it's not initialized for URIs
|
||||
return {subspan(data, uri.fileByteOffset), fastgltf::MimeType::None};
|
||||
@@ -504,7 +500,6 @@ public:
|
||||
srgb_array.at(gltf_material.emissiveTexture->textureIndex) = true;
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::shared_ptr<scene_data::texture>> textures;
|
||||
textures.reserve(gltf.textures.size());
|
||||
for (auto && [srgb, gltf_texture]: utils::zip(srgb_array, gltf.textures))
|
||||
@@ -683,8 +678,8 @@ public:
|
||||
primitive_ref.vertex_offset = staging_buffer.add_vertices(vertices);
|
||||
primitive_ref.vertex_count = vertices.size();
|
||||
|
||||
primitive_ref.cull_mode = vk::CullModeFlagBits::eBack; // TBC
|
||||
primitive_ref.front_face = vk::FrontFace::eCounterClockwise; // TBC
|
||||
primitive_ref.cull_mode = vk::CullModeFlagBits::eBack; // TBC
|
||||
primitive_ref.front_face = vk::FrontFace::eCounterClockwise; // TBC
|
||||
primitive_ref.topology = convert(gltf_primitive.type);
|
||||
|
||||
if (gltf_primitive.materialIndex)
|
||||
@@ -698,7 +693,7 @@ public:
|
||||
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 });
|
||||
unsorted_objects.resize(gltf.nodes.size(), {.parent_id = scene_data::node::root_id});
|
||||
|
||||
for (const auto & [index, gltf_node]: utils::enumerate(gltf.nodes))
|
||||
{
|
||||
@@ -707,18 +702,18 @@ public:
|
||||
|
||||
if (gltf_node.skinIndex)
|
||||
{
|
||||
auto& skin = gltf.skins.at(*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))
|
||||
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) {
|
||||
fastgltf::iterateAccessorWithIndex<glm::mat4>(gltf, accessor, [&](const glm::mat4 & value, std::size_t idx) {
|
||||
unsorted_objects[index].joints.at(idx).second = value;
|
||||
});
|
||||
}
|
||||
@@ -780,16 +775,16 @@ public:
|
||||
}
|
||||
|
||||
// renumber joint indices
|
||||
for(auto& node: sorted_nodes)
|
||||
for (auto & node: sorted_nodes)
|
||||
{
|
||||
for(auto& joint: node.joints)
|
||||
for (auto & joint: node.joints)
|
||||
{
|
||||
joint.first = new_index[joint.first];
|
||||
}
|
||||
}
|
||||
|
||||
assert(sorted_nodes.size() == unsorted_nodes.size());
|
||||
for(size_t i = 0; i < sorted_nodes.size(); i++)
|
||||
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);
|
||||
}
|
||||
@@ -812,7 +807,7 @@ scene_data scene_loader::operator()(const std::filesystem::path & gltf_path)
|
||||
|
||||
asset asset_file(gltf_path);
|
||||
fastgltf::GltfDataBuffer data_buffer;
|
||||
data_buffer.copyBytes(reinterpret_cast<const uint8_t*>(asset_file.data()), asset_file.size());
|
||||
data_buffer.copyBytes(reinterpret_cast<const uint8_t *>(asset_file.data()), asset_file.size());
|
||||
|
||||
fastgltf::Asset asset = load_gltf_asset(data_buffer, gltf_path.parent_path());
|
||||
loader_context ctx(gltf_path.parent_path(), asset, physical_device, device, queue, cb_pool);
|
||||
@@ -868,7 +863,7 @@ scene_data & scene_data::import(scene_data && other, node_handle parent)
|
||||
if (node.mesh_id)
|
||||
*node.mesh_id += mesh_offset;
|
||||
|
||||
for(auto& joint: node.joints)
|
||||
for (auto & joint: node.joints)
|
||||
{
|
||||
joint.first += nodes_offset;
|
||||
}
|
||||
@@ -892,7 +887,6 @@ scene_data & scene_data::import(scene_data && other, node_handle parent)
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
scene_data & scene_data::import(scene_data && other)
|
||||
{
|
||||
return import(std::move(other), {});
|
||||
@@ -903,19 +897,18 @@ node_handle scene_data::new_node()
|
||||
size_t id = scene_nodes.size();
|
||||
|
||||
scene_nodes.push_back(node{
|
||||
.parent_id = node::root_id,
|
||||
.position = {0, 0, 0},
|
||||
.orientation = {1, 0, 0, 0},
|
||||
.scale = {1, 1, 1},
|
||||
.visible = true
|
||||
});
|
||||
.parent_id = node::root_id,
|
||||
.position = {0, 0, 0},
|
||||
.orientation = {1, 0, 0, 0},
|
||||
.scale = {1, 1, 1},
|
||||
.visible = true});
|
||||
|
||||
return {id, this};
|
||||
}
|
||||
|
||||
node_handle scene_data::find_node(std::string_view name)
|
||||
{
|
||||
for(auto&& [index, node]: utils::enumerate(scene_nodes))
|
||||
for (auto && [index, node]: utils::enumerate(scene_nodes))
|
||||
{
|
||||
if (node.name == name)
|
||||
{
|
||||
@@ -936,7 +929,7 @@ node_handle scene_data::find_node(node_handle root, std::string_view name)
|
||||
|
||||
flag[root.id] = true;
|
||||
|
||||
for(size_t index = root.id; index < scene_nodes.size(); index++)
|
||||
for (size_t index = root.id; index < scene_nodes.size(); index++)
|
||||
{
|
||||
size_t parent = scene_nodes[index].parent_id;
|
||||
|
||||
@@ -958,9 +951,9 @@ node_handle scene_data::find_node(node_handle root, std::string_view name)
|
||||
|
||||
std::shared_ptr<scene_data::material> scene_data::find_material(std::string_view name)
|
||||
{
|
||||
for(auto& mesh:meshes)
|
||||
for (auto & mesh: meshes)
|
||||
{
|
||||
for(auto& primitive: mesh.primitives)
|
||||
for (auto & primitive: mesh.primitives)
|
||||
{
|
||||
if (primitive.material_ && primitive.material_->name == name)
|
||||
return primitive.material_;
|
||||
|
||||
+16
-16
@@ -28,14 +28,13 @@
|
||||
#include <glm/vec2.hpp>
|
||||
#include <glm/vec3.hpp>
|
||||
#include <glm/vec4.hpp>
|
||||
#include <ktxvulkan.h>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
#include <vk/allocation.h>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
#include <ktxvulkan.h>
|
||||
|
||||
struct sampler_info
|
||||
{
|
||||
@@ -193,8 +192,8 @@ struct scene_data
|
||||
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);
|
||||
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);
|
||||
@@ -214,11 +213,11 @@ class scene_loader
|
||||
|
||||
public:
|
||||
scene_loader(vk::raii::Device & device, vk::raii::PhysicalDevice physical_device, vk::raii::Queue & queue, uint32_t queue_family_index, std::shared_ptr<scene_data::material> default_material) :
|
||||
device(device),
|
||||
physical_device(physical_device),
|
||||
queue(queue),
|
||||
queue_family_index(queue_family_index),
|
||||
default_material(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);
|
||||
@@ -232,32 +231,33 @@ class node_handle
|
||||
|
||||
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;
|
||||
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*()
|
||||
scene_data::node & operator*()
|
||||
{
|
||||
assert(scene != nullptr);
|
||||
assert(id < scene->scene_nodes.size());
|
||||
return scene->scene_nodes[id];
|
||||
}
|
||||
|
||||
const scene_data::node& operator*() const
|
||||
const scene_data::node & operator*() const
|
||||
{
|
||||
assert(scene != nullptr);
|
||||
assert(id < scene->scene_nodes.size());
|
||||
return scene->scene_nodes[id];
|
||||
}
|
||||
|
||||
scene_data::node* operator->()
|
||||
scene_data::node * operator->()
|
||||
{
|
||||
assert(scene != nullptr);
|
||||
assert(id < scene->scene_nodes.size());
|
||||
return &scene->scene_nodes[id];
|
||||
}
|
||||
|
||||
const scene_data::node* operator->() const
|
||||
const scene_data::node * operator->() const
|
||||
{
|
||||
assert(scene != nullptr);
|
||||
assert(id < scene->scene_nodes.size());
|
||||
|
||||
+364
-333
@@ -23,19 +23,18 @@
|
||||
#include "render/image_loader.h"
|
||||
#include "render/scene_data.h"
|
||||
#include "utils/alignment.h"
|
||||
#include "utils/fmt_glm.h"
|
||||
#include "utils/ranges.h"
|
||||
#include "vk/allocation.h"
|
||||
#include "vk/pipeline.h"
|
||||
#include "vk/shader.h"
|
||||
#include <boost/pfr/core.hpp>
|
||||
#include <chrono>
|
||||
#include <glm/gtc/matrix_access.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
#include <glm/gtc/matrix_access.hpp>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include "utils/fmt_glm.h"
|
||||
#include <vk_mem_alloc.h>
|
||||
|
||||
extern const std::map<std::string, std::vector<uint32_t>> shaders;
|
||||
@@ -113,16 +112,13 @@ std::shared_ptr<scene_data::material> scene_renderer::create_default_material(vk
|
||||
default_material->normal_texture = create_default_texture(cb_pool, {128, 128, 255, 255});
|
||||
|
||||
default_material->buffer = std::make_shared<buffer_allocation>(
|
||||
device,
|
||||
vk::BufferCreateInfo{
|
||||
.size = sizeof(default_material->staging),
|
||||
.usage = vk::BufferUsageFlagBits::eUniformBuffer
|
||||
},
|
||||
VmaAllocationCreateInfo{
|
||||
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO
|
||||
}
|
||||
);
|
||||
device,
|
||||
vk::BufferCreateInfo{
|
||||
.size = sizeof(default_material->staging),
|
||||
.usage = vk::BufferUsageFlagBits::eUniformBuffer},
|
||||
VmaAllocationCreateInfo{
|
||||
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO});
|
||||
memcpy(default_material->buffer->map(), &default_material->staging, sizeof(default_material->staging));
|
||||
default_material->buffer->unmap();
|
||||
|
||||
@@ -137,47 +133,67 @@ static vk::FrontFace reverse(vk::FrontFace face)
|
||||
return vk::FrontFace::eCounterClockwise;
|
||||
}
|
||||
|
||||
static std::array layout_bindings_0
|
||||
{
|
||||
vk::DescriptorSetLayoutBinding{
|
||||
.binding = 0,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
.descriptorCount = 1,
|
||||
.stageFlags = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment},
|
||||
vk::DescriptorSetLayoutBinding{
|
||||
.binding = 1,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
.descriptorCount = 1,
|
||||
.stageFlags = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment},
|
||||
vk::DescriptorSetLayoutBinding{
|
||||
.binding = 2,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
.descriptorCount = 1,
|
||||
.stageFlags = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment}
|
||||
static std::array layout_bindings_0{
|
||||
vk::DescriptorSetLayoutBinding{
|
||||
.binding = 0,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
.descriptorCount = 1,
|
||||
.stageFlags = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment,
|
||||
},
|
||||
vk::DescriptorSetLayoutBinding{
|
||||
.binding = 1,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
.descriptorCount = 1,
|
||||
.stageFlags = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment,
|
||||
},
|
||||
vk::DescriptorSetLayoutBinding{
|
||||
.binding = 2,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
.descriptorCount = 1,
|
||||
.stageFlags = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment,
|
||||
},
|
||||
};
|
||||
|
||||
static std::array layout_bindings_1
|
||||
{
|
||||
vk::DescriptorSetLayoutBinding{
|
||||
.binding = 0,
|
||||
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
|
||||
.descriptorCount = 5,
|
||||
.stageFlags = vk::ShaderStageFlagBits::eFragment},
|
||||
vk::DescriptorSetLayoutBinding{
|
||||
.binding = 5,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
.descriptorCount = 1,
|
||||
.stageFlags = vk::ShaderStageFlagBits::eFragment}
|
||||
static std::array layout_bindings_1{
|
||||
vk::DescriptorSetLayoutBinding{
|
||||
.binding = 0,
|
||||
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
|
||||
.descriptorCount = 5,
|
||||
.stageFlags = vk::ShaderStageFlagBits::eFragment,
|
||||
},
|
||||
vk::DescriptorSetLayoutBinding{
|
||||
.binding = 5,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
.descriptorCount = 1,
|
||||
.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) :
|
||||
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),
|
||||
device(device),
|
||||
physical_device_properties(physical_device.getProperties()),
|
||||
queue(queue),
|
||||
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)),
|
||||
depth_format(
|
||||
find_usable_image_format(
|
||||
physical_device,
|
||||
depth_formats,
|
||||
{
|
||||
output_size.width,
|
||||
output_size.height,
|
||||
1,
|
||||
},
|
||||
vk::ImageUsageFlagBits::eDepthStencilAttachment)),
|
||||
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
|
||||
@@ -188,11 +204,12 @@ scene_renderer::scene_renderer(vk::raii::Device & device, vk::raii::PhysicalDevi
|
||||
// Create Vulkan resources
|
||||
frame_resources.resize(frames_in_flight);
|
||||
|
||||
std::vector<vk::raii::CommandBuffer> command_buffers = device.allocateCommandBuffers({
|
||||
.commandPool = *cb_pool,
|
||||
.level = vk::CommandBufferLevel::ePrimary,
|
||||
.commandBufferCount = (uint32_t)frame_resources.size(),
|
||||
});
|
||||
std::vector<vk::raii::CommandBuffer> command_buffers = device.allocateCommandBuffers(
|
||||
{
|
||||
.commandPool = *cb_pool,
|
||||
.level = vk::CommandBufferLevel::ePrimary,
|
||||
.commandBufferCount = (uint32_t)frame_resources.size(),
|
||||
});
|
||||
|
||||
for (auto && [res, cb]: utils::zip(frame_resources, command_buffers))
|
||||
{
|
||||
@@ -220,7 +237,7 @@ scene_renderer::~scene_renderer()
|
||||
void scene_renderer::wait_idle()
|
||||
{
|
||||
std::vector<vk::Fence> fences(frame_resources.size());
|
||||
for(auto&& [i, f]: utils::enumerate(frame_resources))
|
||||
for (auto && [i, f]: utils::enumerate(frame_resources))
|
||||
fences[i] = *f.fence;
|
||||
|
||||
if (auto result = device.waitForFences(fences, true, 1'000'000'000); result != vk::Result::eSuccess)
|
||||
@@ -294,10 +311,10 @@ vk::raii::RenderPass scene_renderer::create_renderpass()
|
||||
|
||||
vk::SubpassDescription subpasses{
|
||||
.pipelineBindPoint = vk::PipelineBindPoint::eGraphics,
|
||||
.colorAttachmentCount = 1,
|
||||
.pColorAttachments = &color_attachment,
|
||||
.colorAttachmentCount = 1,
|
||||
.pColorAttachments = &color_attachment,
|
||||
#ifdef MSAA_4x
|
||||
.pResolveAttachments = &resolve_attachment,
|
||||
.pResolveAttachments = &resolve_attachment,
|
||||
#endif
|
||||
.pDepthStencilAttachment = &depth_attachment,
|
||||
};
|
||||
@@ -339,73 +356,92 @@ scene_renderer::output_image scene_renderer::create_output_image_data(vk::Image
|
||||
output_image out;
|
||||
|
||||
// TODO: use image view from xr::swapchain
|
||||
out.image_view = vk::raii::ImageView(device, vk::ImageViewCreateInfo{
|
||||
.image = output,
|
||||
.viewType = vk::ImageViewType::e2D,
|
||||
.format = output_format,
|
||||
.components{},
|
||||
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
|
||||
});
|
||||
out.image_view = vk::raii::ImageView(
|
||||
device, vk::ImageViewCreateInfo{
|
||||
.image = output,
|
||||
.viewType = vk::ImageViewType::e2D,
|
||||
.format = output_format,
|
||||
.components{},
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
});
|
||||
|
||||
out.depth_buffer = image_allocation{device,
|
||||
vk::ImageCreateInfo{
|
||||
.imageType = vk::ImageType::e2D,
|
||||
.format = depth_format,
|
||||
.extent = {
|
||||
.width = output_size.width,
|
||||
.height = output_size.height,
|
||||
.depth = 1,
|
||||
},
|
||||
.mipLevels = 1,
|
||||
.arrayLayers = 1,
|
||||
.samples = MSAA_SAMPLES,
|
||||
.tiling = vk::ImageTiling::eOptimal,
|
||||
.usage = vk::ImageUsageFlagBits::eDepthStencilAttachment | vk::ImageUsageFlagBits::eTransientAttachment
|
||||
out.depth_buffer = image_allocation{
|
||||
device,
|
||||
vk::ImageCreateInfo{
|
||||
.imageType = vk::ImageType::e2D,
|
||||
.format = depth_format,
|
||||
.extent = {
|
||||
.width = output_size.width,
|
||||
.height = output_size.height,
|
||||
.depth = 1,
|
||||
},
|
||||
.mipLevels = 1,
|
||||
.arrayLayers = 1,
|
||||
.samples = MSAA_SAMPLES,
|
||||
.tiling = vk::ImageTiling::eOptimal,
|
||||
.usage = vk::ImageUsageFlagBits::eDepthStencilAttachment | vk::ImageUsageFlagBits::eTransientAttachment},
|
||||
VmaAllocationCreateInfo{
|
||||
.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT, .usage = VMA_MEMORY_USAGE_AUTO,
|
||||
.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, // TODO: check
|
||||
}};
|
||||
|
||||
},
|
||||
VmaAllocationCreateInfo{
|
||||
.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO,
|
||||
.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, // TODO: check
|
||||
}};
|
||||
|
||||
out.depth_view = vk::raii::ImageView(device, vk::ImageViewCreateInfo{
|
||||
.image = out.depth_buffer,
|
||||
.viewType = vk::ImageViewType::e2D,
|
||||
.format = depth_format,
|
||||
.components{},
|
||||
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eDepth, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
|
||||
});
|
||||
out.depth_view = vk::raii::ImageView(
|
||||
device,
|
||||
vk::ImageViewCreateInfo{
|
||||
.image = out.depth_buffer,
|
||||
.viewType = vk::ImageViewType::e2D,
|
||||
.format = depth_format,
|
||||
.components{},
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eDepth,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
});
|
||||
|
||||
#ifdef MSAA_4x
|
||||
out.multisample_image = image_allocation{vk::ImageCreateInfo{
|
||||
.imageType = vk::ImageType::e2D,
|
||||
.format = output_format,
|
||||
.extent = {
|
||||
.width = output_size.width,
|
||||
.height = output_size.height,
|
||||
.depth = 1,
|
||||
},
|
||||
.mipLevels = 1,
|
||||
.arrayLayers = 1,
|
||||
.samples = MSAA_SAMPLES,
|
||||
.tiling = vk::ImageTiling::eOptimal,
|
||||
.usage = vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eTransientAttachment
|
||||
.imageType = vk::ImageType::e2D,
|
||||
.format = output_format,
|
||||
.extent = {
|
||||
.width = output_size.width,
|
||||
.height = output_size.height,
|
||||
.depth = 1,
|
||||
},
|
||||
.mipLevels = 1,
|
||||
.arrayLayers = 1,
|
||||
.samples = MSAA_SAMPLES,
|
||||
.tiling = vk::ImageTiling::eOptimal,
|
||||
.usage = vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eTransientAttachment,
|
||||
},
|
||||
VmaAllocationCreateInfo{
|
||||
.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO,
|
||||
.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, // TODO: check
|
||||
.preferredFlags = VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT,
|
||||
}};
|
||||
|
||||
},
|
||||
VmaAllocationCreateInfo{
|
||||
.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO,
|
||||
.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, // TODO: check
|
||||
.preferredFlags = VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT,
|
||||
}};
|
||||
out.multisample_view = vk::raii::ImageView(device, vk::ImageViewCreateInfo{
|
||||
.image = out.multisample_image,
|
||||
.viewType = vk::ImageViewType::e2D,
|
||||
.format = output_format,
|
||||
.components{},
|
||||
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
|
||||
});
|
||||
.image = out.multisample_image,
|
||||
.viewType = vk::ImageViewType::e2D,
|
||||
.format = output_format,
|
||||
.components{},
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
});
|
||||
#endif
|
||||
|
||||
vk::FramebufferCreateInfo fb_info{
|
||||
@@ -417,7 +453,7 @@ scene_renderer::output_image scene_renderer::create_output_image_data(vk::Image
|
||||
|
||||
#ifdef MSAA_4x
|
||||
std::array attachments{
|
||||
vk::ImageView{*out.multisample_view},
|
||||
vk::ImageView{*out.multisample_view},
|
||||
vk::ImageView{*out.depth_view},
|
||||
vk::ImageView{*out.image_view},
|
||||
};
|
||||
@@ -444,7 +480,7 @@ vk::raii::Pipeline & scene_renderer::get_pipeline(const pipeline_info & info)
|
||||
|
||||
vk::raii::DescriptorSetLayout scene_renderer::create_descriptor_set_layout(std::span<vk::DescriptorSetLayoutBinding> bindings, vk::DescriptorSetLayoutCreateFlags flags)
|
||||
{
|
||||
vk::DescriptorSetLayoutCreateInfo dsl_info{.flags = flags};
|
||||
vk::DescriptorSetLayoutCreateInfo dsl_info{.flags = flags};
|
||||
dsl_info.setBindings(bindings);
|
||||
return vk::raii::DescriptorSetLayout{device, dsl_info};
|
||||
}
|
||||
@@ -467,116 +503,109 @@ vk::raii::Pipeline scene_renderer::create_pipeline(const pipeline_info & info)
|
||||
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, 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::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,
|
||||
.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,
|
||||
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,
|
||||
},
|
||||
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,
|
||||
}};
|
||||
},
|
||||
},
|
||||
.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)
|
||||
@@ -585,15 +614,17 @@ vk::Sampler scene_renderer::get_sampler(const sampler_info & info)
|
||||
if (it != samplers.end())
|
||||
return **it->second;
|
||||
|
||||
auto out = std::make_shared<vk::raii::Sampler>(device, vk::SamplerCreateInfo{
|
||||
.magFilter = info.mag_filter,
|
||||
.minFilter = info.min_filter,
|
||||
.mipmapMode = info.min_filter_mipmap,
|
||||
.addressModeU = info.wrapS,
|
||||
.addressModeV = info.wrapT,
|
||||
// .anisotropyEnable = true,
|
||||
// .maxAnisotropy = 4,
|
||||
});
|
||||
auto out = std::make_shared<vk::raii::Sampler>(
|
||||
device,
|
||||
vk::SamplerCreateInfo{
|
||||
.magFilter = info.mag_filter,
|
||||
.minFilter = info.min_filter,
|
||||
.mipmapMode = info.min_filter_mipmap,
|
||||
.addressModeU = info.wrapS,
|
||||
.addressModeV = info.wrapT,
|
||||
// .anisotropyEnable = true,
|
||||
// .maxAnisotropy = 4,
|
||||
});
|
||||
|
||||
samplers.emplace(info, out);
|
||||
return **out;
|
||||
@@ -603,7 +634,7 @@ void scene_renderer::start_frame()
|
||||
{
|
||||
current_frame_index = (current_frame_index + 1) % frame_resources.size();
|
||||
|
||||
auto& f = current_frame();
|
||||
auto & f = current_frame();
|
||||
if (auto result = device.waitForFences(*f.fence, true, 1'000'000'000); result != vk::Result::eSuccess)
|
||||
throw std::runtime_error("vkWaitForfences: " + vk::to_string(result));
|
||||
device.resetFences(*f.fence);
|
||||
@@ -611,11 +642,11 @@ void scene_renderer::start_frame()
|
||||
if (f.query_pool_filled)
|
||||
{
|
||||
auto [res, timestamps] = query_pool.getResults<uint64_t>(
|
||||
current_frame_index * 2,
|
||||
2,
|
||||
2 * sizeof(uint64_t),
|
||||
sizeof(uint64_t),
|
||||
vk::QueryResultFlagBits::e64 | vk::QueryResultFlagBits::eWait);
|
||||
current_frame_index * 2,
|
||||
2,
|
||||
2 * sizeof(uint64_t),
|
||||
sizeof(uint64_t),
|
||||
vk::QueryResultFlagBits::e64 | vk::QueryResultFlagBits::eWait);
|
||||
|
||||
if (res == vk::Result::eSuccess)
|
||||
{
|
||||
@@ -634,29 +665,31 @@ void scene_renderer::start_frame()
|
||||
{
|
||||
// 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)"
|
||||
};
|
||||
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)"};
|
||||
}
|
||||
}
|
||||
|
||||
void scene_renderer::end_frame()
|
||||
{
|
||||
auto& f = current_frame();
|
||||
auto & f = current_frame();
|
||||
|
||||
f.cb.writeTimestamp(vk::PipelineStageFlagBits::eTopOfPipe, *query_pool, current_frame_index * 2 + 1);
|
||||
f.cb.end();
|
||||
|
||||
queue.submit(vk::SubmitInfo{
|
||||
.commandBufferCount = 1,
|
||||
.pCommandBuffers = &*f.cb,
|
||||
}, *f.fence);
|
||||
.commandBufferCount = 1,
|
||||
.pCommandBuffers = &*f.cb,
|
||||
},
|
||||
*f.fence);
|
||||
f.query_pool_filled = true;
|
||||
}
|
||||
|
||||
@@ -665,50 +698,50 @@ scene_renderer::per_frame_resources & scene_renderer::current_frame()
|
||||
return frame_resources[current_frame_index];
|
||||
}
|
||||
|
||||
void scene_renderer::update_material_descriptor_set(scene_data::material& material)
|
||||
void scene_renderer::update_material_descriptor_set(scene_data::material & material)
|
||||
{
|
||||
if (!material.ds || !material.ds.unique())
|
||||
material.ds = ds_pool_material.allocate();
|
||||
|
||||
vk::DescriptorSet ds = **material.ds;
|
||||
|
||||
auto f = [&](std::shared_ptr<scene_data::texture>& texture){
|
||||
auto f = [&](std::shared_ptr<scene_data::texture> & texture) {
|
||||
return vk::DescriptorImageInfo{
|
||||
.sampler = get_sampler(texture->sampler),
|
||||
.imageView = **texture->image_view,
|
||||
.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
|
||||
.sampler = get_sampler(texture->sampler),
|
||||
.imageView = **texture->image_view,
|
||||
.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal,
|
||||
};
|
||||
};
|
||||
|
||||
std::array write_ds_image{
|
||||
f(material.base_color_texture),
|
||||
f(material.metallic_roughness_texture),
|
||||
f(material.occlusion_texture),
|
||||
f(material.emissive_texture),
|
||||
f(material.normal_texture),
|
||||
f(material.base_color_texture),
|
||||
f(material.metallic_roughness_texture),
|
||||
f(material.occlusion_texture),
|
||||
f(material.emissive_texture),
|
||||
f(material.normal_texture),
|
||||
};
|
||||
|
||||
vk::DescriptorBufferInfo write_ds_buffer{
|
||||
.buffer = *material.buffer,
|
||||
.offset = material.offset,
|
||||
.range = sizeof(scene_data::material::gpu_data)
|
||||
.buffer = *material.buffer,
|
||||
.offset = material.offset,
|
||||
.range = sizeof(scene_data::material::gpu_data),
|
||||
};
|
||||
|
||||
std::array write_ds{
|
||||
vk::WriteDescriptorSet{
|
||||
.dstSet = ds,
|
||||
.dstBinding = 0,
|
||||
.descriptorCount = (uint32_t)write_ds_image.size(),
|
||||
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
|
||||
.pImageInfo = write_ds_image.data()
|
||||
},
|
||||
vk::WriteDescriptorSet{
|
||||
.dstSet = ds,
|
||||
.dstBinding = (uint32_t)write_ds_image.size(),
|
||||
.descriptorCount = 1,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
.pBufferInfo = &write_ds_buffer
|
||||
},
|
||||
vk::WriteDescriptorSet{
|
||||
.dstSet = ds,
|
||||
.dstBinding = 0,
|
||||
.descriptorCount = (uint32_t)write_ds_image.size(),
|
||||
.descriptorType = vk::DescriptorType::eCombinedImageSampler,
|
||||
.pImageInfo = write_ds_image.data(),
|
||||
},
|
||||
vk::WriteDescriptorSet{
|
||||
.dstSet = ds,
|
||||
.dstBinding = (uint32_t)write_ds_image.size(),
|
||||
.descriptorCount = 1,
|
||||
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
||||
.pBufferInfo = &write_ds_buffer,
|
||||
},
|
||||
};
|
||||
|
||||
device.updateDescriptorSets(write_ds, {});
|
||||
@@ -731,7 +764,7 @@ void scene_renderer::update_material_descriptor_set(scene_data::material& materi
|
||||
// }
|
||||
// }
|
||||
|
||||
void scene_renderer::render(scene_data & scene, const std::array<float, 4>& clear_color, std::span<frame_info> frames)
|
||||
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();
|
||||
|
||||
@@ -753,7 +786,7 @@ void scene_renderer::render(scene_data & scene, const std::array<float, 4>& clea
|
||||
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_nodes))
|
||||
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;
|
||||
@@ -785,7 +818,7 @@ void scene_renderer::render(scene_data & scene, const std::array<float, 4>& clea
|
||||
glm::mat4 viewproj = frame.projection * frame.view;
|
||||
|
||||
vk::DeviceSize frame_ubo_offset = resources.uniform_buffer_offset;
|
||||
frame_gpu_data& frame_ubo = *reinterpret_cast<frame_gpu_data*>(ubo + 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
|
||||
@@ -794,26 +827,27 @@ void scene_renderer::render(scene_data & scene, const std::array<float, 4>& clea
|
||||
// frame_ubo.ambient_color = glm::vec4(0.2,0.2,0.2,0); // TODO
|
||||
// frame_ubo.light_color = glm::vec4(0.8,0.8,0.8,0); // TODO
|
||||
|
||||
frame_ubo.ambient_color = glm::vec4(0,0,0,0); // TODO
|
||||
frame_ubo.light_color = glm::vec4(0.8,0.8,0.8,0); // TODO
|
||||
frame_ubo.ambient_color = glm::vec4(0, 0, 0, 0); // TODO
|
||||
frame_ubo.light_color = glm::vec4(0.8, 0.8, 0.8, 0); // TODO
|
||||
|
||||
frame_ubo.light_position = glm::vec4(1,1,1,0); // TODO
|
||||
frame_ubo.light_position = glm::vec4(1, 1, 1, 0); // TODO
|
||||
frame_ubo.proj = frame.projection;
|
||||
frame_ubo.view = frame.view;
|
||||
|
||||
cb.beginRenderPass(vk::RenderPassBeginInfo{
|
||||
.renderPass = *renderpass,
|
||||
.framebuffer = *output.framebuffer,
|
||||
.renderArea = {
|
||||
.offset = {0, 0},
|
||||
.extent = output_size,
|
||||
},
|
||||
.clearValueCount = clear_values.size(),
|
||||
.pClearValues = clear_values.data(),
|
||||
},
|
||||
vk::SubpassContents::eInline);
|
||||
cb.beginRenderPass(
|
||||
vk::RenderPassBeginInfo{
|
||||
.renderPass = *renderpass,
|
||||
.framebuffer = *output.framebuffer,
|
||||
.renderArea = {
|
||||
.offset = {0, 0},
|
||||
.extent = output_size,
|
||||
},
|
||||
.clearValueCount = clear_values.size(),
|
||||
.pClearValues = clear_values.data(),
|
||||
},
|
||||
vk::SubpassContents::eInline);
|
||||
|
||||
for(const auto& [index, node]: utils::enumerate(scene.scene_nodes))
|
||||
for (const auto & [index, node]: utils::enumerate(scene.scene_nodes))
|
||||
{
|
||||
if (!node.mesh_id)
|
||||
continue;
|
||||
@@ -821,11 +855,11 @@ void scene_renderer::render(scene_data & scene, const std::array<float, 4>& clea
|
||||
if (!visible[index])
|
||||
continue;
|
||||
|
||||
scene_data::mesh& mesh = scene.meshes.at(*node.mesh_id);
|
||||
glm::mat4& transform = transform_to_root[index];
|
||||
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);
|
||||
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;
|
||||
@@ -836,7 +870,7 @@ void scene_renderer::render(scene_data & scene, const std::array<float, 4>& clea
|
||||
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))
|
||||
for (auto && [idx, joint]: utils::enumerate(node.joints))
|
||||
{
|
||||
joint_matrices[idx] = glm::inverse(transform) * transform_to_root[joint.first] * joint.second;
|
||||
}
|
||||
@@ -846,7 +880,7 @@ void scene_renderer::render(scene_data & scene, const std::array<float, 4>& clea
|
||||
object_ubo.modelview = frame.view * transform;
|
||||
object_ubo.modelviewproj = viewproj * transform;
|
||||
|
||||
for(scene_data::primitive& primitive: mesh.primitives)
|
||||
for (scene_data::primitive & primitive: mesh.primitives)
|
||||
{
|
||||
// Get the material
|
||||
std::shared_ptr<scene_data::material> material = primitive.material_ ? primitive.material_ : default_material;
|
||||
@@ -856,14 +890,14 @@ void scene_renderer::render(scene_data & scene, const std::array<float, 4>& clea
|
||||
|
||||
// Get the pipeline
|
||||
pipeline_info info{
|
||||
.shader_name = material->shader_name,
|
||||
.cull_mode = primitive.cull_mode,
|
||||
.front_face = primitive.front_face,
|
||||
.topology = primitive.topology,
|
||||
.blend_enable = material->blend_enable,
|
||||
.shader_name = material->shader_name,
|
||||
.cull_mode = primitive.cull_mode,
|
||||
.front_face = primitive.front_face,
|
||||
.topology = primitive.topology,
|
||||
.blend_enable = material->blend_enable,
|
||||
|
||||
.nb_texcoords = 2, // TODO
|
||||
.skinning = !node.joints.empty()
|
||||
.nb_texcoords = 2, // TODO
|
||||
.skinning = !node.joints.empty(),
|
||||
};
|
||||
|
||||
if (material->double_sided)
|
||||
@@ -880,41 +914,39 @@ void scene_renderer::render(scene_data & scene, const std::array<float, 4>& clea
|
||||
cb.bindVertexBuffers(0, (vk::Buffer)*mesh.buffer, primitive.vertex_offset);
|
||||
|
||||
vk::DescriptorBufferInfo buffer_info_1{
|
||||
.buffer = resources.uniform_buffer,
|
||||
.offset = frame_ubo_offset,
|
||||
.range = sizeof(frame_gpu_data)
|
||||
.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)
|
||||
};
|
||||
.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
|
||||
};
|
||||
.buffer = resources.uniform_buffer,
|
||||
.offset = joints_ubo_offset,
|
||||
.range = sizeof(glm::mat4) * 32};
|
||||
|
||||
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
|
||||
},
|
||||
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.pushDescriptorSetKHR(vk::PipelineBindPoint::eGraphics, *pipeline_layout, 0, descriptors);
|
||||
@@ -929,7 +961,6 @@ void scene_renderer::render(scene_data & scene, const std::array<float, 4>& clea
|
||||
|
||||
resources.resources.push_back(material->ds);
|
||||
}
|
||||
|
||||
}
|
||||
cb.endRenderPass();
|
||||
}
|
||||
|
||||
@@ -18,10 +18,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vulkan/vulkan.hpp>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include <glm/vec2.hpp>
|
||||
@@ -30,6 +27,8 @@
|
||||
#include <span>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <vulkan/vulkan.hpp>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
|
||||
#include "vk/allocation.h"
|
||||
|
||||
@@ -158,10 +157,10 @@ class scene_renderer
|
||||
std::vector<std::shared_ptr<void>> resources;
|
||||
bool query_pool_filled = false;
|
||||
|
||||
// Buffer for per-view and per-instance data
|
||||
// device local, host visible, host coherent
|
||||
// Buffer for per-view and per-instance data
|
||||
// device local, host visible, host coherent
|
||||
size_t uniform_buffer_offset;
|
||||
buffer_allocation uniform_buffer;
|
||||
buffer_allocation uniform_buffer;
|
||||
};
|
||||
|
||||
std::vector<per_frame_resources> frame_resources;
|
||||
@@ -169,12 +168,20 @@ class scene_renderer
|
||||
vk::raii::QueryPool query_pool = nullptr;
|
||||
double gpu_time_s = 0;
|
||||
|
||||
per_frame_resources& current_frame();
|
||||
per_frame_resources & current_frame();
|
||||
|
||||
void update_material_descriptor_set(scene_data::material& material);
|
||||
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<vk::Format> depth_formats, int frames_in_flight = 2);
|
||||
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 = 2);
|
||||
|
||||
~scene_renderer();
|
||||
|
||||
@@ -186,7 +193,7 @@ public:
|
||||
};
|
||||
|
||||
void start_frame();
|
||||
void render(scene_data & scene, const std::array<float, 4>& clear_color, std::span<frame_info> info);
|
||||
void render(scene_data & scene, const std::array<float, 4> & clear_color, std::span<frame_info> info);
|
||||
void end_frame();
|
||||
|
||||
double get_gpu_time() const
|
||||
|
||||
Reference in New Issue
Block a user