first vulkan + glfw window

This commit is contained in:
Mahdi Dibaiee 2023-01-19 10:36:26 +00:00
parent eead703163
commit 56fb7bf113
4 changed files with 112 additions and 19 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
main
# Prerequisites
*.d

View File

@ -6,5 +6,5 @@ Install VulkanSDK: https://vulkan.lunarg.com/
brew install glfw glm
export VULKAN_SDK=$HOME/path/to/your/VulkanSDK/1.3.236.0
make
make run
```

BIN
main

Binary file not shown.

128
main.cpp
View File

@ -1,35 +1,127 @@
#include <vulkan/vulkan.h>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <stdio.h>
#include <iostream>
#include <stdexcept>
#include <cstdlib>
int main() {
class HelloTriangleApplication {
public:
void run() {
initWindow();
initVulkan();
mainLoop();
cleanup();
}
private:
GLFWwindow* window;
VkInstance instance;
const uint32_t WIDTH = 800;
const uint32_t HEIGHT = 600;
void initWindow() {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
GLFWwindow* window = glfwCreateWindow(800, 600, "Vulkan window", nullptr, nullptr);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
uint32_t extensionCount = 0;
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
}
std::cout << extensionCount << " extensions supported\n";
void createInstance() {
VkApplicationInfo appInfo{};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "Hello Triangle";
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.pEngineName = "No Engine";
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.apiVersion = VK_API_VERSION_1_0;
glm::mat4 matrix;
glm::vec4 vec;
auto test = matrix * vec;
VkInstanceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo;
while(!glfwWindowShouldClose(window)) {
glfwPollEvents();
std::vector<const char*> requiredExtensions;
uint32_t glfwExtensionCount = 0;
const char** glfwExtensions;
// Enable glfw extensions
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
for (uint32_t i = 0; i < glfwExtensionCount; i++) {
requiredExtensions.emplace_back(glfwExtensions[i]);
}
// Enable Portability Enumeration extension (required for macOS)
requiredExtensions.emplace_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);
createInfo.flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
createInfo.enabledExtensionCount = (uint32_t) requiredExtensions.size();
createInfo.ppEnabledExtensionNames = requiredExtensions.data();
createInfo.enabledLayerCount = 0;
VkResult result = vkCreateInstance(&createInfo, nullptr, &instance);
if (result != VK_SUCCESS) {
std::cout << "error creating instance: " << result << std::endl;
throw std::runtime_error("failed to create instance!");
}
std::cout << "Created instance" << std::endl;
// Check for available extensions
uint32_t extensionCount = 0;
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
std::vector<VkExtensionProperties> extensions(extensionCount);
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data());
std::cout << "available extensions:" << std::endl;
for (const auto& extension : extensions) {
std::cout << '\t' << extension.extensionName;
for (const char* required : requiredExtensions) {
if (strcmp(required, extension.extensionName) == 0) {
std::cout << " *";
}
}
std::cout << std::endl;
}
}
void initVulkan() {
createInstance();
}
void mainLoop() {
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
}
void cleanup() {
vkDestroyInstance(instance, nullptr);
glfwDestroyWindow(window);
glfwTerminate();
}
};
return 0;
int main() {
HelloTriangleApplication app;
try {
app.run();
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}