From 57e590a80ee172b36213a5f4624ed904429bd07a Mon Sep 17 00:00:00 2001
From: Tobias Frisch <tfrisch@uni-koblenz.de>
Date: Sun, 30 May 2021 14:20:18 +0200
Subject: [PATCH] [#52] Buffer allow filling via vector

Signed-off-by: Tobias Frisch <tfrisch@uni-koblenz.de>
---
 include/vkcv/Buffer.hpp          |  8 +++++++-
 include/vkcv/BufferManager.hpp   |  2 +-
 projects/first_mesh/src/main.cpp | 20 ++++++++++++++------
 src/vkcv/BufferManager.cpp       |  6 +++---
 4 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/include/vkcv/Buffer.hpp b/include/vkcv/Buffer.hpp
index 5a95471b..d327067c 100644
--- a/include/vkcv/Buffer.hpp
+++ b/include/vkcv/Buffer.hpp
@@ -7,6 +7,8 @@
 #include "Handles.hpp"
 #include "BufferManager.hpp"
 
+#include <vector>
+
 namespace vkcv {
 
 	template<typename T>
@@ -36,10 +38,14 @@ namespace vkcv {
 			return m_count * sizeof(T);
 		}
 		
-		void fill(T* data, size_t count = 0, size_t offset = 0) {
+		void fill(const T* data, size_t count = 0, size_t offset = 0) {
 			 m_manager->fillBuffer(m_handle, data, count * sizeof(T), offset * sizeof(T));
 		}
 		
+		void fill(const std::vector<T>& vector, size_t offset = 0) {
+			fill( static_cast<const T*>(vector.data()), static_cast<size_t>(vector.size()), offset);
+		}
+		
 		[[nodiscard]]
 		T* map(size_t offset = 0, size_t count = 0) {
 			return reinterpret_cast<T*>(m_manager->mapBuffer(m_handle, offset * sizeof(T), count * sizeof(T)));
diff --git a/include/vkcv/BufferManager.hpp b/include/vkcv/BufferManager.hpp
index 4b12f47b..322873b3 100644
--- a/include/vkcv/BufferManager.hpp
+++ b/include/vkcv/BufferManager.hpp
@@ -103,7 +103,7 @@ namespace vkcv
 		 * @param size Size of data in bytes
 		 * @param offset Offset to fill in data in bytes
 		 */
-		void fillBuffer(const BufferHandle& handle, void* data, size_t size, size_t offset);
+		void fillBuffer(const BufferHandle& handle, const void* data, size_t size, size_t offset);
 		
 		/**
 		 * Maps memory to a buffer represented by a given
diff --git a/projects/first_mesh/src/main.cpp b/projects/first_mesh/src/main.cpp
index fe53ab11..102e6f38 100644
--- a/projects/first_mesh/src/main.cpp
+++ b/projects/first_mesh/src/main.cpp
@@ -45,13 +45,21 @@ int main(int argc, const char** argv) {
 	}
 
 	assert(mesh.vertexGroups.size() > 0);
-	const size_t vertexBufferSize = mesh.vertexGroups[0].vertexBuffer.data.size();
-	auto vertexBuffer = core.createBuffer<uint8_t>(vkcv::BufferType::VERTEX, vertexBufferSize, vkcv::BufferMemoryType::DEVICE_LOCAL);
-	vertexBuffer.fill(mesh.vertexGroups[0].vertexBuffer.data.data(), vertexBufferSize);
+	auto vertexBuffer = core.createBuffer<uint8_t>(
+			vkcv::BufferType::VERTEX,
+			mesh.vertexGroups[0].vertexBuffer.data.size(),
+			vkcv::BufferMemoryType::DEVICE_LOCAL
+	);
+	
+	vertexBuffer.fill(mesh.vertexGroups[0].vertexBuffer.data);
 
-	const size_t indexBufferSize = mesh.vertexGroups[0].indexBuffer.data.size();
-	auto indexBuffer = core.createBuffer<uint8_t>(vkcv::BufferType::INDEX, indexBufferSize, vkcv::BufferMemoryType::DEVICE_LOCAL);
-	indexBuffer.fill(mesh.vertexGroups[0].indexBuffer.data.data(), indexBufferSize);
+	auto indexBuffer = core.createBuffer<uint8_t>(
+			vkcv::BufferType::INDEX,
+			mesh.vertexGroups[0].indexBuffer.data.size(),
+			vkcv::BufferMemoryType::DEVICE_LOCAL
+	);
+	
+	indexBuffer.fill(mesh.vertexGroups[0].indexBuffer.data);
 
 	// an example attachment for passes that output to the window
 	const vkcv::AttachmentDescription present_color_attachment(
diff --git a/src/vkcv/BufferManager.cpp b/src/vkcv/BufferManager.cpp
index 740e8999..ab34a3df 100644
--- a/src/vkcv/BufferManager.cpp
+++ b/src/vkcv/BufferManager.cpp
@@ -123,7 +123,7 @@ namespace vkcv {
 	}
 	
 	struct StagingStepInfo {
-		void* data;
+		const void* data;
 		size_t size;
 		size_t offset;
 		
@@ -152,7 +152,7 @@ namespace vkcv {
 		const vk::Device& device = core->getContext().getDevice();
 		
 		void* mapped = device.mapMemory(info.stagingMemory, 0, mapped_size);
-		memcpy(mapped, reinterpret_cast<char*>(info.data) + info.stagingPosition, mapped_size);
+		memcpy(mapped, reinterpret_cast<const char*>(info.data) + info.stagingPosition, mapped_size);
 		device.unmapMemory(info.stagingMemory);
 		
 		SubmitInfo submitInfo;
@@ -218,7 +218,7 @@ namespace vkcv {
 		return buffer.m_memory;
 	}
 	
-	void BufferManager::fillBuffer(const BufferHandle& handle, void *data, size_t size, size_t offset) {
+	void BufferManager::fillBuffer(const BufferHandle& handle, const void *data, size_t size, size_t offset) {
 		const uint64_t id = handle.getId();
 		
 		if (size == 0) {
-- 
GitLab