Initial creation classes in manager
This commit is contained in:
parent
7e675cd532
commit
45ba5a5b5e
3 changed files with 147 additions and 72 deletions
|
|
@ -3,6 +3,7 @@
|
|||
## Principles
|
||||
|
||||
* Non-vulkan naming convention to disambiguate Vulkan vs Kompute components
|
||||
* BYOV: It would play nicely with existing applications with a bring-your-own-Vulkan design
|
||||
* TODO
|
||||
|
||||
## Getting Started
|
||||
|
|
|
|||
200
src/Manager.cpp
200
src/Manager.cpp
|
|
@ -34,79 +34,143 @@ debugMessageCallback(VkDebugReportFlagsEXT flags,
|
|||
|
||||
Manager::Manager()
|
||||
{
|
||||
vk::ApplicationInfo applicationInfo;
|
||||
applicationInfo.pApplicationName = "Vulkan compute";
|
||||
applicationInfo.pEngineName = "VulkanCompute";
|
||||
applicationInfo.apiVersion = VK_API_VERSION_1_2;
|
||||
|
||||
std::vector<const char*> applicationExtensions;
|
||||
applicationExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
|
||||
|
||||
vk::InstanceCreateInfo computeInstanceCreateInfo;
|
||||
computeInstanceCreateInfo.pApplicationInfo = &applicationInfo;
|
||||
if (!applicationExtensions.empty()) {
|
||||
computeInstanceCreateInfo.enabledExtensionCount =
|
||||
(uint32_t)applicationExtensions.size();
|
||||
computeInstanceCreateInfo.ppEnabledExtensionNames =
|
||||
applicationExtensions.data();
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
// We'll identify the layers that are supported
|
||||
std::vector<const char*> validLayerNames;
|
||||
std::vector<const char*> desiredLayerNames = {
|
||||
"VK_LAYER_LUNARG_assistant_layer",
|
||||
"VK_LAYER_LUNARG_standard_validation"
|
||||
};
|
||||
// Identify the valid layer names based on the desiredLayerNames
|
||||
{
|
||||
std::set<std::string> uniqueLayerNames;
|
||||
std::vector<vk::LayerProperties> availableLayerProperties =
|
||||
vk::enumerateInstanceLayerProperties();
|
||||
for (vk::LayerProperties layerProperties :
|
||||
availableLayerProperties) {
|
||||
std::string layerName(layerProperties.layerName);
|
||||
uniqueLayerNames.insert(layerName);
|
||||
}
|
||||
for (const char* desiredLayerName : desiredLayerNames) {
|
||||
if (uniqueLayerNames.count(desiredLayerName) != 0) {
|
||||
validLayerNames.push_back(desiredLayerName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (validLayerNames.size() > 0) {
|
||||
computeInstanceCreateInfo.enabledLayerCount =
|
||||
(uint32_t)validLayerNames.size();
|
||||
computeInstanceCreateInfo.ppEnabledLayerNames =
|
||||
validLayerNames.data();
|
||||
}
|
||||
#endif
|
||||
|
||||
this->mInstance = vk::createInstance(computeInstanceCreateInfo);
|
||||
|
||||
#if DEBUG
|
||||
if (validLayerNames.size() > 0) {
|
||||
vk::DebugReportFlagsEXT debugFlags =
|
||||
vk::DebugReportFlagBitsEXT::eError |
|
||||
vk::DebugReportFlagBitsEXT::eWarning;
|
||||
vk::DebugReportCallbackCreateInfoEXT debugCreateInfo = {};
|
||||
debugCreateInfo.pfnCallback =
|
||||
(PFN_vkDebugReportCallbackEXT)debugMessageCallback;
|
||||
debugCreateInfo.flags = debugFlags;
|
||||
|
||||
this->mDebugDispatcher.init(this->mInstance,
|
||||
&vkGetInstanceProcAddr);
|
||||
this->mDebugReportCallback =
|
||||
this->mInstance.createDebugReportCallbackEXT(
|
||||
debugCreateInfo, nullptr, this->mDebugDispatcher);
|
||||
}
|
||||
#endif
|
||||
this->createInstance();
|
||||
this->createDevice();
|
||||
}
|
||||
|
||||
Manager::~Manager()
|
||||
{
|
||||
}
|
||||
|
||||
void Manager::createInstance() {
|
||||
|
||||
this->mFreeInstance = true;
|
||||
|
||||
vk::ApplicationInfo applicationInfo;
|
||||
applicationInfo.pApplicationName = "Vulkan compute";
|
||||
applicationInfo.pEngineName = "VulkanCompute";
|
||||
applicationInfo.apiVersion = VK_API_VERSION_1_2;
|
||||
|
||||
std::vector<const char*> applicationExtensions;
|
||||
applicationExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
|
||||
|
||||
vk::InstanceCreateInfo computeInstanceCreateInfo;
|
||||
computeInstanceCreateInfo.pApplicationInfo = &applicationInfo;
|
||||
if (!applicationExtensions.empty()) {
|
||||
computeInstanceCreateInfo.enabledExtensionCount =
|
||||
(uint32_t)applicationExtensions.size();
|
||||
computeInstanceCreateInfo.ppEnabledExtensionNames =
|
||||
applicationExtensions.data();
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
// We'll identify the layers that are supported
|
||||
std::vector<const char*> validLayerNames;
|
||||
std::vector<const char*> desiredLayerNames = {
|
||||
"VK_LAYER_LUNARG_assistant_layer",
|
||||
"VK_LAYER_LUNARG_standard_validation"
|
||||
};
|
||||
// Identify the valid layer names based on the desiredLayerNames
|
||||
{
|
||||
std::set<std::string> uniqueLayerNames;
|
||||
std::vector<vk::LayerProperties> availableLayerProperties =
|
||||
vk::enumerateInstanceLayerProperties();
|
||||
for (vk::LayerProperties layerProperties :
|
||||
availableLayerProperties) {
|
||||
std::string layerName(layerProperties.layerName);
|
||||
uniqueLayerNames.insert(layerName);
|
||||
}
|
||||
for (const char* desiredLayerName : desiredLayerNames) {
|
||||
if (uniqueLayerNames.count(desiredLayerName) != 0) {
|
||||
validLayerNames.push_back(desiredLayerName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (validLayerNames.size() > 0) {
|
||||
computeInstanceCreateInfo.enabledLayerCount =
|
||||
(uint32_t)validLayerNames.size();
|
||||
computeInstanceCreateInfo.ppEnabledLayerNames =
|
||||
validLayerNames.data();
|
||||
}
|
||||
#endif
|
||||
|
||||
vk::Instance instance = vk::createInstance(computeInstanceCreateInfo, nullptr, this->mInstance);
|
||||
|
||||
#if DEBUG
|
||||
if (validLayerNames.size() > 0) {
|
||||
vk::DebugReportFlagsEXT debugFlags =
|
||||
vk::DebugReportFlagBitsEXT::eError |
|
||||
vk::DebugReportFlagBitsEXT::eWarning;
|
||||
vk::DebugReportCallbackCreateInfoEXT debugCreateInfo = {};
|
||||
debugCreateInfo.pfnCallback =
|
||||
(PFN_vkDebugReportCallbackEXT)debugMessageCallback;
|
||||
debugCreateInfo.flags = debugFlags;
|
||||
|
||||
this->mDebugDispatcher.init(*this->mInstance,
|
||||
&vkGetInstanceProcAddr);
|
||||
this->mDebugReportCallback =
|
||||
this->mInstance->createDebugReportCallbackEXT(
|
||||
debugCreateInfo, nullptr, this->mDebugDispatcher);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void Manager::createDevice() {
|
||||
|
||||
if (this->mInstance == nullptr) {
|
||||
throw std::runtime_error("Manager instance is null");
|
||||
}
|
||||
|
||||
this->mFreeDevice = true;
|
||||
this->mFreeComputeQueue = true;
|
||||
|
||||
std::vector<vk::PhysicalDevice> physicalDevices =
|
||||
this->mInstance->enumeratePhysicalDevices();
|
||||
|
||||
vk::PhysicalDevice physicalDevice = physicalDevices[0];
|
||||
|
||||
vk::PhysicalDeviceProperties physicalDeviceProperties =
|
||||
physicalDevice.getProperties();
|
||||
|
||||
spdlog::info("Using device {}", physicalDeviceProperties.deviceName);
|
||||
|
||||
// Find compute queue
|
||||
std::vector<vk::QueueFamilyProperties> allQueueFamilyProperties =
|
||||
physicalDevice.getQueueFamilyProperties();
|
||||
|
||||
this->mComputeQueueFamilyIndex = -1;
|
||||
for (uint32_t i = 0; i < allQueueFamilyProperties.size(); i++) {
|
||||
vk::QueueFamilyProperties queueFamilyProperties =
|
||||
allQueueFamilyProperties[i];
|
||||
|
||||
if (queueFamilyProperties.queueFlags &
|
||||
vk::QueueFlagBits::eCompute) {
|
||||
this->mComputeQueueFamilyIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (this->mComputeQueueFamilyIndex < 0) {
|
||||
spdlog::critical("Compute queue is not supported");
|
||||
}
|
||||
|
||||
const float defaultQueuePriority(0.0f);
|
||||
const uint32_t defaultQueueCount(1);
|
||||
vk::DeviceQueueCreateInfo deviceQueueCreateInfo(
|
||||
vk::DeviceQueueCreateFlags(),
|
||||
this->mComputeQueueFamilyIndex,
|
||||
defaultQueueCount,
|
||||
&defaultQueuePriority);
|
||||
|
||||
vk::DeviceCreateInfo deviceCreateInfo(
|
||||
vk::DeviceCreateFlags(),
|
||||
1, // Number of deviceQueueCreateInfo
|
||||
&deviceQueueCreateInfo);
|
||||
|
||||
physicalDevice.createDevice(deviceCreateInfo, nullptr, this->mDevice);
|
||||
|
||||
this->mDevice->getQueue(this->mComputeQueueFamilyIndex, 0, this->mComputeQueue);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,16 +14,26 @@ public:
|
|||
|
||||
virtual ~Manager();
|
||||
|
||||
private:
|
||||
vk::Instance mInstance;
|
||||
// Templated Commands
|
||||
|
||||
// Create functions
|
||||
vk::InstanceCreateInfo createInstanceInfo();
|
||||
private:
|
||||
vk::Instance* mInstance = nullptr;
|
||||
bool mFreeInstance = false;
|
||||
vk::Device* mDevice = nullptr;
|
||||
bool mFreeDevice = false;
|
||||
uint32_t mComputeQueueFamilyIndex = -1;
|
||||
vk::Queue* mComputeQueue = nullptr;
|
||||
bool mFreeComputeQueue = false;
|
||||
|
||||
#if DEBUG
|
||||
vk::DebugReportCallbackEXT mDebugReportCallback;
|
||||
vk::DispatchLoaderDynamic mDebugDispatcher;
|
||||
#endif
|
||||
|
||||
// Create functions
|
||||
void createInstance();
|
||||
void createDevice();
|
||||
|
||||
};
|
||||
|
||||
} // End namespace kp
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue