create vulkan instance

This commit is contained in:
ktyl 2023-03-20 21:06:56 +00:00
parent bd26428d91
commit 214cc42c93
1 changed files with 101 additions and 25 deletions

View File

@ -1,37 +1,113 @@
#define GLFW_INCLUDE_VULKAN #define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPth_ZERO_TO_ONE
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <iostream> #include <iostream>
#include <stdexcept>
#include <cstdlib>
#include <vector>
const uint32_t WIDTH = 800;
const uint32_t HEIGHT = 600;
class HelloTriangleApplication
{
public:
void run()
{
initWindow();
initVulkan();
mainLoop();
cleanup();
}
private:
GLFWwindow* _window;
VkInstance _instance;
void initWindow()
{
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
_window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
}
void initVulkan()
{
createInstance();
}
void mainLoop()
{
while (!glfwWindowShouldClose(_window))
{
glfwPollEvents();
}
}
void cleanup()
{
vkDestroyInstance(_instance, nullptr);
glfwDestroyWindow(_window);
glfwTerminate();
}
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;
VkInstanceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo;
uint32_t glfwExtensionCount = 0;
const char** glfwExtensions;
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
createInfo.enabledExtensionCount = glfwExtensionCount;
createInfo.ppEnabledExtensionNames = glfwExtensions;
createInfo.enabledLayerCount = 0;
if (vkCreateInstance(&createInfo, nullptr, &_instance) != VK_SUCCESS)
{
throw std::runtime_error("failed to create instance!");
}
uint32_t extensionCount = 0;
std::vector<VkExtensionProperties> extensions(extensionCount);
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data());
std::cout << "available extensions: " << extensionCount << std::endl;
for (const auto& extension : extensions)
{
std::cout << '\t' << extension.extensionName << '\n';
}
}
};
int main() int main()
{ {
glfwInit(); HelloTriangleApplication app;
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); try
GLFWwindow* window = glfwCreateWindow(800, 600, "Vulkan window", nullptr, nullptr);
uint32_t extensionCount = 0;
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
std::cout << extensionCount << " extensions supported\n";
glm::mat4 matrix;
glm::vec4 vec;
auto test = matrix * vec;
while (!glfwWindowShouldClose(window))
{ {
glfwPollEvents(); app.run();
}
catch(const std::exception& e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
} }
glfwDestroyWindow(window); return EXIT_SUCCESS;
glfwTerminate();
return 0;
} }