Skip to content
Snippets Groups Projects
BufferManager.cpp 15.60 KiB
/**
 * @author Tobias Frisch
 * @file vkcv/BufferManager.cpp
 */

#include "BufferManager.hpp"
#include "vkcv/Core.hpp"
#include <vkcv/Logger.hpp>

#include <limits>
#include <numeric>

namespace vkcv {

	bool BufferManager::init(Core &core) {
		if (!HandleManager<BufferEntry, BufferHandle>::init(core)) {
			return false;
		}
		
		const vma::Allocator &allocator = getCore().getContext().getAllocator();
		const auto& memoryProperties = allocator.getMemoryProperties();
		const auto& heaps = memoryProperties->memoryHeaps;
		
		std::vector<vk::MemoryPropertyFlags> heapMemoryFlags;
		heapMemoryFlags.resize(heaps.size());
		
		for (const auto& type : memoryProperties->memoryTypes) {
			if (type.heapIndex >= heaps.size()) {
				continue;
			}
			
			heapMemoryFlags[type.heapIndex] |= type.propertyFlags;
		}
		
		vk::DeviceSize maxDeviceHeapSize = 0;
		uint32_t deviceHeapIndex = 0;
		
		for (uint32_t i = 0; i < heaps.size(); i++) {
			if (!(heaps[i].flags & vk::MemoryHeapFlagBits::eDeviceLocal)) {
				continue;
			}
			
			if (!(heapMemoryFlags[i] & vk::MemoryPropertyFlagBits::eDeviceLocal)) {
				continue;
			}
			
			if (heaps[i].size < maxDeviceHeapSize) {
				continue;
			}
			
			maxDeviceHeapSize = heaps[i].size;
			deviceHeapIndex = i;
		}
		
		if (heapMemoryFlags[deviceHeapIndex] & vk::MemoryPropertyFlagBits::eHostVisible) {
			m_resizableBar = true;
		} else {
			m_resizableBar = false;
		}
		
		m_shaderDeviceAddress = getCore().getContext().getFeatureManager().checkFeatures<
		        vk::PhysicalDeviceBufferDeviceAddressFeatures
		>(
				vk::StructureType::ePhysicalDeviceBufferDeviceAddressFeatures,
				[](const vk::PhysicalDeviceBufferDeviceAddressFeatures &features) {
					return features.bufferDeviceAddress;
				}
		);

		m_stagingBuffer = createBuffer(