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>
int main() 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(); glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); 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; _window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr); }
std::cout << extensionCount << " extensions supported\n"; void initVulkan()
{
createInstance();
}
glm::mat4 matrix; void mainLoop()
glm::vec4 vec; {
auto test = matrix * vec; while (!glfwWindowShouldClose(_window))
while (!glfwWindowShouldClose(window))
{ {
glfwPollEvents(); glfwPollEvents();
} }
}
glfwDestroyWindow(window);
void cleanup()
glfwTerminate(); {
vkDestroyInstance(_instance, nullptr);
return 0;
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()
{
HelloTriangleApplication app;
try
{
app.run();
}
catch(const std::exception& e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
} }