mirror of
https://github.com/PancakeTAS/lsfg-vk.git
synced 2025-10-30 07:01:10 +00:00
less copies for buffer init
This commit is contained in:
parent
f724229ae4
commit
80f27ad188
3 changed files with 13 additions and 15 deletions
|
|
@ -5,7 +5,6 @@
|
|||
|
||||
#include <vulkan/vulkan_core.h>
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
namespace Vulkan::Core {
|
||||
|
|
@ -21,14 +20,16 @@ namespace Vulkan::Core {
|
|||
/// Create the buffer.
|
||||
///
|
||||
/// @param device Vulkan device
|
||||
/// @param size Size of the buffer in bytes.
|
||||
/// @param data Initial data for the buffer.
|
||||
/// @param data Initial data for the buffer, also specifies the size of the buffer.
|
||||
/// @param usage Usage flags for the buffer
|
||||
///
|
||||
/// @throws ls::vulkan_error if object creation fails.
|
||||
///
|
||||
Buffer(const Device& device, size_t size, std::vector<uint8_t> data,
|
||||
VkBufferUsageFlags usage);
|
||||
template<typename T>
|
||||
Buffer(const Device& device, const T& data, VkBufferUsageFlags usage)
|
||||
: size(sizeof(T)) {
|
||||
construct(device, reinterpret_cast<const void*>(&data), usage);
|
||||
}
|
||||
|
||||
/// Get the Vulkan handle.
|
||||
[[nodiscard]] auto handle() const { return *this->buffer; }
|
||||
|
|
@ -42,6 +43,8 @@ namespace Vulkan::Core {
|
|||
Buffer& operator=(Buffer&&) noexcept = default;
|
||||
~Buffer() = default;
|
||||
private:
|
||||
void construct(const Device& device, const void* data, VkBufferUsageFlags usage);
|
||||
|
||||
std::shared_ptr<VkBuffer> buffer;
|
||||
std::shared_ptr<VkDeviceMemory> memory;
|
||||
|
||||
|
|
|
|||
|
|
@ -6,12 +6,11 @@
|
|||
|
||||
using namespace Vulkan::Core;
|
||||
|
||||
Buffer::Buffer(const Device& device, size_t size, std::vector<uint8_t> data,
|
||||
VkBufferUsageFlags usage) : size(size) {
|
||||
void Buffer::construct(const Device& device, const void* data, VkBufferUsageFlags usage) {
|
||||
// create buffer
|
||||
const VkBufferCreateInfo desc{
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
||||
.size = size,
|
||||
.size = this->size,
|
||||
.usage = usage,
|
||||
.sharingMode = VK_SHARING_MODE_EXCLUSIVE
|
||||
};
|
||||
|
|
@ -59,10 +58,10 @@ Buffer::Buffer(const Device& device, size_t size, std::vector<uint8_t> data,
|
|||
|
||||
// upload data to buffer
|
||||
uint8_t* buf{};
|
||||
res = vkMapMemory(device.handle(), memoryHandle, 0, size, 0, reinterpret_cast<void**>(&buf));
|
||||
res = vkMapMemory(device.handle(), memoryHandle, 0, this->size, 0, reinterpret_cast<void**>(&buf));
|
||||
if (res != VK_SUCCESS || buf == nullptr)
|
||||
throw ls::vulkan_error(res, "Failed to map memory for Vulkan buffer");
|
||||
std::copy_n(data.data(), std::min(size, data.size()), buf);
|
||||
std::copy_n(reinterpret_cast<const uint8_t*>(data), this->size, buf);
|
||||
vkUnmapMemory(device.handle(), memoryHandle);
|
||||
|
||||
// store buffer and memory in shared ptr
|
||||
|
|
|
|||
|
|
@ -62,11 +62,7 @@ int main() {
|
|||
VK_IMAGE_ASPECT_COLOR_BIT
|
||||
));
|
||||
|
||||
const auto* dataBuffer = reinterpret_cast<const uint8_t*>(&data);
|
||||
std::vector<uint8_t> dataVec(sizeof(DataBuffer));
|
||||
std::copy_n(dataBuffer, sizeof(DataBuffer), dataVec.data());
|
||||
const Core::Buffer buffer(device, dataVec.size(), dataVec,
|
||||
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT);
|
||||
const Core::Buffer buffer(device, data, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
|
||||
|
||||
std::vector<Core::Image> outputImages;
|
||||
outputImages.reserve(7);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue