212 lines
No EOL
5.5 KiB
C++
212 lines
No EOL
5.5 KiB
C++
#define GLFW_INCLUDE_VULKAN
|
|
|
|
#include <cstring>
|
|
#include "GLFW/glfw3.h"
|
|
|
|
#include "iostream"
|
|
#include "stdexcept"
|
|
#include "cstdlib"
|
|
#include "vector"
|
|
|
|
const std::vector<const char*> validationLayers = {
|
|
#ifdef NDEBUG
|
|
#else
|
|
"VK_LAYER_KHRONOS_validation"
|
|
#endif
|
|
};
|
|
|
|
class Application {
|
|
public:
|
|
void run() {
|
|
initWindow();
|
|
initVulkan();
|
|
mainLoop();
|
|
cleanup();
|
|
}
|
|
|
|
private:
|
|
void initWindow() {
|
|
glfwInit();
|
|
|
|
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
|
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
|
|
mWindow = glfwCreateWindow(800, 600, "App", nullptr, nullptr);
|
|
}
|
|
|
|
void initVulkan() {
|
|
createInstance();
|
|
pickPhysicalDevice();
|
|
findPhysicalDeviceQueueFamilies();
|
|
createLogicalDevice();
|
|
getQueues();
|
|
}
|
|
|
|
void mainLoop() {
|
|
while(!glfwWindowShouldClose(mWindow)) {
|
|
glfwPollEvents();
|
|
}
|
|
}
|
|
|
|
void cleanup() {
|
|
vkDestroyDevice(mDevice, nullptr);
|
|
vkDestroyInstance(mInstance, nullptr);
|
|
glfwDestroyWindow(mWindow);
|
|
glfwTerminate();
|
|
}
|
|
|
|
|
|
void createInstance() {
|
|
if (!checkValidationLayerSupport()) {
|
|
throw std::runtime_error("no required validation layers present");
|
|
}
|
|
|
|
uint32_t glfwExtensionCount = 0;
|
|
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
|
|
|
|
VkApplicationInfo appInfo {
|
|
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
|
|
.pApplicationName = "App",
|
|
.applicationVersion = VK_MAKE_VERSION(0, 0, 0),
|
|
.pEngineName = "no",
|
|
.engineVersion = VK_MAKE_VERSION(0, 0, 0),
|
|
.apiVersion = VK_API_VERSION_1_0,
|
|
};
|
|
|
|
VkInstanceCreateInfo createInfo {
|
|
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
|
|
.pApplicationInfo = &appInfo,
|
|
.enabledLayerCount = (uint32_t) validationLayers.size(),
|
|
.ppEnabledLayerNames = validationLayers.data(),
|
|
.enabledExtensionCount = glfwExtensionCount,
|
|
.ppEnabledExtensionNames = glfwExtensions,
|
|
};
|
|
|
|
if (vkCreateInstance(&createInfo, nullptr, &mInstance) != VK_SUCCESS) {
|
|
throw std::runtime_error("failed to create instance!");
|
|
}
|
|
}
|
|
|
|
static bool checkValidationLayerSupport() {
|
|
uint32_t layerCount;
|
|
vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
|
|
|
|
std::vector<VkLayerProperties> availableLayers(layerCount);
|
|
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
|
|
|
|
for (const auto& requestedLayer : validationLayers) {
|
|
bool presents = false;
|
|
for (auto &layer: availableLayers) {
|
|
if (strcmp(requestedLayer, layer.layerName) == 0) {
|
|
presents = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!presents) return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void pickPhysicalDevice() {
|
|
uint32_t deviceCount = 0;
|
|
vkEnumeratePhysicalDevices(mInstance, &deviceCount, nullptr);
|
|
|
|
if (deviceCount == 0) {
|
|
throw std::runtime_error("no gpu with vulkan support");
|
|
}
|
|
|
|
std::vector<VkPhysicalDevice> devices(deviceCount);
|
|
vkEnumeratePhysicalDevices(mInstance, &deviceCount, devices.data());
|
|
|
|
for (const auto& device : devices) {
|
|
if (isDeviceSuitable(device)) {
|
|
mPhysicalDevice = device;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!mPhysicalDevice) throw std::runtime_error("no suitable gpu");
|
|
}
|
|
|
|
static bool isDeviceSuitable(VkPhysicalDevice device) {
|
|
VkPhysicalDeviceProperties properties;
|
|
VkPhysicalDeviceFeatures features;
|
|
|
|
vkGetPhysicalDeviceProperties(device, &properties);
|
|
vkGetPhysicalDeviceFeatures(device, &features);
|
|
|
|
// if (properties.deviceType != VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) return false;
|
|
if (!features.geometryShader) return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
void findPhysicalDeviceQueueFamilies() {
|
|
uint32_t queuesCount = 0;
|
|
vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queuesCount, nullptr);
|
|
|
|
std::vector<VkQueueFamilyProperties> queueFamilyProperties(queuesCount);
|
|
vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queuesCount, queueFamilyProperties.data());
|
|
|
|
int index = 0;
|
|
for (const auto& familyProperty : queueFamilyProperties) {
|
|
if (familyProperty.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
|
|
mGraphicsQueueFamilyIndex = index;
|
|
}
|
|
index++;
|
|
}
|
|
|
|
if (index == -1) throw std::runtime_error("nu require queue families found");
|
|
}
|
|
|
|
void createLogicalDevice() {
|
|
float queuePriority = 1.f;
|
|
|
|
VkDeviceQueueCreateInfo queueCreateInfos {
|
|
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
|
|
.queueFamilyIndex = mGraphicsQueueFamilyIndex,
|
|
.queueCount = 1,
|
|
.pQueuePriorities = &queuePriority,
|
|
};
|
|
|
|
VkPhysicalDeviceFeatures features {};
|
|
|
|
VkDeviceCreateInfo deviceCreateInfo {
|
|
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
|
|
.queueCreateInfoCount = 1,
|
|
.pQueueCreateInfos = &queueCreateInfos,
|
|
.pEnabledFeatures = &features,
|
|
};
|
|
|
|
vkCreateDevice(mPhysicalDevice, &deviceCreateInfo, nullptr, &mDevice);
|
|
|
|
if (mDevice == VK_NULL_HANDLE) throw std::runtime_error("failed to create vulkan logical device");
|
|
}
|
|
|
|
void getQueues() {
|
|
vkGetDeviceQueue(mDevice, mGraphicsQueueFamilyIndex, 0, &mGraphicsQueue);
|
|
}
|
|
|
|
private:
|
|
GLFWwindow* mWindow = nullptr;
|
|
|
|
VkInstance mInstance = VK_NULL_HANDLE;
|
|
VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
|
|
uint32_t mGraphicsQueueFamilyIndex = -1;
|
|
VkDevice mDevice = VK_NULL_HANDLE;
|
|
VkQueue mGraphicsQueue = VK_NULL_HANDLE;
|
|
};
|
|
|
|
int main() {
|
|
Application app;
|
|
|
|
try {
|
|
app.run();
|
|
} catch (const std::exception& e) {
|
|
std::cerr << e.what() << std::endl;
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
} |