vulkan-project/vulkan_util.cpp
2022-10-14 20:58:20 +02:00

127 lines
4.7 KiB
C++

#include "vulkan_util.hpp"
#include <iostream>
#include <vulkan/vulkan_core.h>
namespace gz::vk {
//
// CONVENIENCE
//
PhysicalDeviceFeatures::PhysicalDeviceFeatures() {
f.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
f11.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
f12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
f13.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES;
f.pNext = &f11;
f11.pNext = &f12;
f12.pNext = &f13;
}
bool PhysicalDeviceFeatures::requiredFeaturesAvailable(const PhysicalDeviceFeatures& requiredFeatures) const {
// iterate over all the features in the structs
// VkPhysicalDeviceFeatures2
size_t featureCount = sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
// pointer to first feature
const VkBool32* pFeature = &f.features.robustBufferAccess;
const VkBool32* pRequiredFeature = &requiredFeatures.f.features.robustBufferAccess;
for (size_t i = 0; i < featureCount; i++) {
if (*(pRequiredFeature + i) == VK_TRUE and *(pFeature + i) == VK_FALSE) {
/* std::cout << "core feature not supported: i = " << i << "\n"; */
return false;
}
}
// VkPhysicalDeviceVulkan11Features
// pointer to first feature: after sType and pNext
featureCount = (sizeof(VkPhysicalDeviceVulkan11Features) - sizeof(VkStructureType) - sizeof(void*)) / sizeof(VkBool32);
pFeature = &f11.storageBuffer16BitAccess;
pRequiredFeature = &requiredFeatures.f11.storageBuffer16BitAccess;
for (size_t i = 0; i < featureCount; i++) {
if (*(pRequiredFeature + i) == VK_TRUE and *(pFeature + i) == VK_FALSE) {
/* std::cout << "vulkan11 feature not supported: i = " << i << "\n"; */
return false;
}
}
// VkPhysicalDeviceVulkan12Features
// pointer to first feature: after sType and pNext
featureCount = (sizeof(VkPhysicalDeviceVulkan12Features) - sizeof(VkStructureType) - sizeof(void*)) / sizeof(VkBool32);
pFeature = &f12.samplerMirrorClampToEdge;
pRequiredFeature = &requiredFeatures.f12.samplerMirrorClampToEdge;
for (size_t i = 0; i < featureCount; i++) {
if (*(pRequiredFeature + i) == VK_TRUE and *(pFeature + i) == VK_FALSE) {
/* std::cout << "vulkan12 feature not supported: i = " << i << "\n"; */
return false;
}
}
// VkPhysicalDeviceVulkan13Features
// pointer to first feature: after sType and pNext
featureCount = (sizeof(VkPhysicalDeviceVulkan13Features) - sizeof(VkStructureType) - sizeof(void*)) / sizeof(VkBool32);
pFeature = &f13.robustImageAccess;
pRequiredFeature = &requiredFeatures.f13.robustImageAccess;
for (size_t i = 0; i < featureCount; i++) {
if (*(pRequiredFeature + i) == VK_TRUE and *(pFeature + i) == VK_FALSE) {
/* std::cout << "vulkan13 feature not supported: i = " << i << "\n"; */
return false;
}
}
return true;
}
void PipelineContainer::erase(const PipelineT& key, VkDevice& device, const VkAllocationCallbacks* pAllocator) {
vkDestroyPipeline(device, pipelines[key].pipeline, pAllocator);
vkDestroyPipelineLayout(device, pipelines[key].layout, pAllocator);
pipelines.erase(pipelines.find(key));
}
PipelineContainer::iterator PipelineContainer::erase(const PipelineContainer::iterator& it, VkDevice& device, const VkAllocationCallbacks* pAllocator) {
vkDestroyPipeline(device, it->second.pipeline, pAllocator);
vkDestroyPipelineLayout(device, it->second.layout, pAllocator);
return pipelines.erase(it);
}
void PipelineContainer::destroy(VkDevice& device, const VkAllocationCallbacks* pAllocator) {
auto it = pipelines.begin(); while (it != pipelines.end()) {
it = erase(it, device);
}
}
VkDependencyInfo getDepInfo(const VkImageMemoryBarrier2& barrier) {
VkDependencyInfo depI{};
depI.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO;
depI.imageMemoryBarrierCount = 1;
depI.pImageMemoryBarriers = &barrier;
return depI;
}
//
//
//
ObjectUsingVulkan::ObjectUsingVulkan(std::string&& name, std::vector<void*>&& ptrsToHandles, std::vector<void*>&& vectorWithPtrsToHandles) {
objectName = std::move(name);
for (auto it = vectorWithPtrsToHandles.begin(); it != vectorWithPtrsToHandles.end(); it++) {
vectorWithPtrToHandle.emplace_back(reinterpret_cast<std::vector<void*>*>(*it));
}
ptrToHandle = std::move(ptrsToHandles);
updateHandles();
}
void ObjectUsingVulkan::updateHandles() {
handles.clear();
for (auto it = ptrToHandle.begin(); it != ptrToHandle.end(); it++) {
handles.insert(reinterpret_cast<uint64_t>(reinterpret_cast<void*>(*it)));
}
for (auto it = vectorWithPtrToHandle.begin(); it != vectorWithPtrToHandle.end(); it++) {
for (auto it2 = (*it)->begin(); it2 != (*it)->end(); it2++) {
handles.insert(reinterpret_cast<uint64_t>(reinterpret_cast<void*>(*it2)));
}
}
}
} // namespace gz::vk