This commit is contained in:
lukas 2020-05-02 22:21:51 +02:00
commit df4ebe6078
2 changed files with 180 additions and 0 deletions

52
CMakeLists.txt Normal file
View File

@ -0,0 +1,52 @@
# @author Lukas Heiligenbrunner
# main CMake file
#
# Build lib dependencies:
## libcurl (with sources)
## libconfig (with sources)
#
# documenation build needs doxygen to be installed.
cmake_minimum_required(VERSION 3.16)
project(testvulkan)
set(CMAKE_CXX_STANDARD 20)
SET(CMAKE_BUILD_TYPE Release) # manually SET build type (Release / Debug)
SET(LIB_METHOD STATIC) #SHARED / STATIC
SET(PROJECT_VERSION 0.1)
option(BUILD_DOC "Build documentation" ON)
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# config libs
message(STATUS "Config of Libraries")
# libcurl
find_package(Vulkan REQUIRED)
if (Vulkan_FOUND)
message(STATUS "Found Vulkan version: ${Vulkan_VERSION_STRING}")
message(STATUS "Using Vulkan include dir(s): ${Vulkan_INCLUDE_DIRS}")
message(STATUS "Using Vulkan lib(s): ${Vulkan_LIBRARIES}")
else ()
message(FATAL_ERROR "Could not find CURL")
endif ()
include_directories(${Vulkan_INCLUDE_DIR} inc)
find_package(glfw3 REQUIRED)
if (glfw3_FOUND)
message(STATUS "Found Vulkan version: ${glfw3_VERSION_STRING}")
message(STATUS "Using Vulkan include dir(s): ${glfw3_INCLUDE_DIRS}")
message(STATUS "Using Vulkan lib(s): ${glfw3_LIBRARIES}")
else ()
message(FATAL_ERROR "Could not find CURL")
endif ()
include_directories(${glfw3_INCLUDE_DIR} inc)
add_executable(testvulkan main.cpp)
target_link_libraries(testvulkan Vulkan::Vulkan glfw)

128
main.cpp Normal file
View File

@ -0,0 +1,128 @@
#include <iostream>
#include <vector>
#include "vulkan/vulkan.h"
int main() {
VkApplicationInfo appinfo; // general app info
appinfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appinfo.pNext = nullptr;
appinfo.pApplicationName = "Test Vulkan";
appinfo.applicationVersion = VK_MAKE_VERSION(0, 1, 0);
appinfo.pEngineName = "volukanEngine";
appinfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
appinfo.apiVersion = VK_API_VERSION_1_1;
uint32_t amountOfLayers = 0;
vkEnumerateInstanceLayerProperties(&amountOfLayers, NULL);
VkLayerProperties *layers = new VkLayerProperties[amountOfLayers];
vkEnumerateInstanceLayerProperties(&amountOfLayers, layers);
std::cout << "amount of instance layers: " << amountOfLayers << std::endl;
for (int i = 0; i < amountOfLayers; i++) {
std::cout << "Name: " << layers[i].layerName << std::endl;
std::cout << "Spec Version: " << layers[i].specVersion << std::endl;
std::cout << "Description: " << layers[i].description << std::endl << std::endl;
}
const std::vector<const char *> validationLayers = {
"VK_LAYER_NV_optimus"
};
VkInstanceCreateInfo instanceInfo;
instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instanceInfo.pNext = nullptr;
instanceInfo.flags = 0;
instanceInfo.pApplicationInfo = &appinfo;
instanceInfo.enabledLayerCount = validationLayers.size();
instanceInfo.ppEnabledLayerNames = validationLayers.data();
instanceInfo.enabledExtensionCount = 0;
instanceInfo.ppEnabledExtensionNames = nullptr;
VkInstance instance;
// create vulkan instance
if (vkCreateInstance(&instanceInfo, nullptr, &instance) != VK_SUCCESS) {
std::cout << "error on creating vkinstance" << std::endl;
}
std::cout << "detecting gpus." << std::endl;
uint32_t physicaldevicesnumber = 0;
if (vkEnumeratePhysicalDevices(instance, &physicaldevicesnumber, nullptr) != VK_SUCCESS) {
std::cout << "error getting graphics number" << std::endl;
}
std::cout << "found " << physicaldevicesnumber << " GPUs" << std::endl;
std::vector<VkPhysicalDevice> physicalDevices;
physicalDevices.resize(physicaldevicesnumber);
// VkPhysicalDevice *physicalDevice = new VkPhysicalDevice[physicaldevicesnumber];
if (vkEnumeratePhysicalDevices(instance, &physicaldevicesnumber, physicalDevices.data()) != VK_SUCCESS) {
std::cout << "error getting graphics cards" << std::endl;
}
VkPhysicalDeviceProperties properties;
for (int i = 0; i < physicaldevicesnumber; i++) {
vkGetPhysicalDeviceProperties(physicalDevices.at(i), &properties);
std::cout << "Device name: " << properties.deviceName << std::endl;
std::cout << "API Version: " << VK_VERSION_MAJOR(properties.apiVersion) << "."
<< VK_VERSION_MINOR(properties.apiVersion) << "." << VK_VERSION_PATCH(properties.apiVersion)
<< std::endl;
std::cout << "Driver Version: " << VK_VERSION_MAJOR(properties.driverVersion) << "."
<< VK_VERSION_MINOR(properties.driverVersion) << "." << VK_VERSION_PATCH(properties.driverVersion)
<< std::endl;
std::cout << std::endl;
}
uint32_t amountOfExtensions = 0;
vkEnumerateInstanceExtensionProperties(NULL, &amountOfExtensions, NULL);
VkExtensionProperties *extensions = new VkExtensionProperties[amountOfExtensions];
vkEnumerateInstanceExtensionProperties(NULL, &amountOfExtensions, extensions);
std::cout << "found " << amountOfExtensions << " extensions" << std::endl;
for (int i = 0; i < amountOfExtensions; i++) {
std::cout << "Name: " << extensions[i].extensionName << std::endl;
}
VkDeviceQueueCreateInfo deviceQueueCreateInfo;
deviceQueueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
deviceQueueCreateInfo.pNext = nullptr;
deviceQueueCreateInfo.flags = 0;
deviceQueueCreateInfo.queueFamilyIndex = 0; // todo choose best queue family index
deviceQueueCreateInfo.queueCount = 4; // todo check if 4 supported
float queuePrios[] = {1.0f, 1.0f, 1.0f, 1.0f};
deviceQueueCreateInfo.pQueuePriorities = queuePrios;
VkPhysicalDeviceFeatures usedFeatures = {};
// set features here
VkDeviceCreateInfo deviceCreateInfo;
deviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
deviceCreateInfo.pNext = nullptr;
deviceCreateInfo.flags = 0;
deviceCreateInfo.queueCreateInfoCount = 1;
deviceCreateInfo.pQueueCreateInfos = &deviceQueueCreateInfo;
deviceCreateInfo.enabledLayerCount = 0;
deviceCreateInfo.ppEnabledLayerNames = nullptr;
deviceCreateInfo.enabledExtensionCount = 0;
deviceCreateInfo.ppEnabledExtensionNames = nullptr;
deviceCreateInfo.pEnabledFeatures = &usedFeatures;
std::cout << "creating logical device" << std::endl;
VkDevice device;
if (vkCreateDevice(physicalDevices.at(0), &deviceCreateInfo, NULL, &device) !=
VK_SUCCESS) { // todo select right physicaldevice
std::cout << "create logical device failed." << std::endl;
}
// stop jobs
vkDeviceWaitIdle(device); // waits until any work has finished
vkDestroyDevice(device, nullptr);
vkDestroyInstance(instance, nullptr);
delete[] layers;
delete[] extensions;
return 0;
}