/* * WiVRn VR streaming * Copyright (C) 2024 Guillaume Meunier * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #pragma once #include "application.h" #include "utils/alignment.h" #include "vk/allocation.h" #include #include #include #include class gpu_buffer { std::vector bytes; vk::PhysicalDeviceLimits limits; fastgltf::Asset & asset; vk::BufferUsageFlags usage; size_t add(size_t alignment, const void * data_to_add, size_t size) { size_t offset = utils::align_up(alignment, bytes.size()); bytes.resize(offset + size); memcpy(&bytes[offset], data_to_add, size); return offset; } template requires requires(T t) { { t.data() } -> std::convertible_to; { t.size() } -> std::convertible_to; } size_t add(size_t alignment, const T & data_to_add) { return add(alignment, data_to_add.data(), data_to_add.size() * sizeof(data_to_add.data()[0])); } public: gpu_buffer(vk::PhysicalDeviceProperties properties, fastgltf::Asset & asset) : limits(properties.limits), asset(asset), usage(0) {} template size_t add_uniform(const T & data_to_add) { usage |= vk::BufferUsageFlagBits::eUniformBuffer; size_t alignment = std::max(limits.minUniformBufferOffsetAlignment, alignof(T)); return add(alignment, &data_to_add, sizeof(data_to_add)); } template size_t add_vertices(const T & data_to_add) { usage |= vk::BufferUsageFlagBits::eVertexBuffer; size_t alignment = std::max(4, alignof(T)); return add(alignment, data_to_add); } size_t add_indices(const fastgltf::Accessor & accessor) { usage |= vk::BufferUsageFlagBits::eIndexBuffer; switch (accessor.componentType) { case fastgltf::ComponentType::Byte: case fastgltf::ComponentType::UnsignedByte: { std::vector indices(accessor.count); using index_t = std::remove_cvref_t; fastgltf::copyFromAccessor(asset, accessor, indices.data()); return add(4, indices); } case fastgltf::ComponentType::Short: case fastgltf::ComponentType::UnsignedShort: { std::vector indices(accessor.count); using index_t = std::remove_cvref_t; fastgltf::copyFromAccessor(asset, accessor, indices.data()); return add(4, indices); } case fastgltf::ComponentType::Int: case fastgltf::ComponentType::UnsignedInt: { std::vector indices(accessor.count); using index_t = std::remove_cvref_t; fastgltf::copyFromAccessor(asset, accessor, indices.data()); return add(4, indices); } default: throw std::runtime_error("Invalid index type"); } } buffer_allocation copy_to_gpu() { buffer_allocation gpu_buffer{ application::get_device(), vk::BufferCreateInfo{ .size = bytes.size(), .usage = usage}, VmaAllocationCreateInfo{ .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT, .usage = VMA_MEMORY_USAGE_AUTO}, "gpu_buffer::copy_to_gpu"}; memcpy(gpu_buffer.map(), bytes.data(), bytes.size()); gpu_buffer.unmap(); return gpu_buffer; } size_t size() const { return bytes.size(); } };