创建vulkan instance

本文介绍了如何创建Vulkan实例,并探讨了Vulkan使用上的不便及其潜在的优势。参考了一个详细的教程,教程链接见内。
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>

#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
#include <glm/mat4x4.hpp>
#include<vulkan/vulkan.h>

#include <iostream>


class XFutureWorld
{
public:
	XFutureWorld():
		window(NULL)
	{
		
	}
	~XFutureWorld()
	{
		
	}
private:
	const int WIDTH = 800;
	const int HEIGHT = 600;
	GLFWwindow* window;

	VkInstance instance;
public:
	void run()
	{
		initWindow();
		initVulkan();
		mainLoop();
		cleanUp();
	}

private:

	void initWindow()
	{
		//调用glfw 初始化
		glfwInit();
		glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
		glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
		//穿件glfw 窗口
	    window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan window", nullptr, nullptr);
		if(window==NULL)
		{
			std::cout << "窗口创建失败";
		}
	}

	void initVulkan()
	{
		createInstance();
	}

	void mainLoop()
	{
		while (!glfwWindowShouldClose(window)) {
			glfwPollEvents();
		}
	}

	void cleanUp()
	{
		vkDestroyInstance(instance, nullptr);
		glfwDestroyWindow(window);
		glfwTerminate();
	}

	//创建instance 
	void createInstance()
	{
		//为该数据结构赋予自定义应用程序的信息。这些数据从技术角度是可选择的,
		//但是它可以为驱动程序提供一些有用的信息来优化程序特殊的使用情景,比如驱动程序使用一些图形引擎的特殊行为。
		//可选的
		VkApplicationInfo appInfo = {};
		appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
		appInfo.pNext = nullptr;
		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;

		//Vulkan中的大量信息通过结构体而不是函数参数传递,我们将填充一个结构体以提供足够的信息创建instance。
		//必须存在,它需要告知Vulkan驱动程序我们需要使用哪些全局的 extensions 和 validation layers
		VkInstanceCreateInfo createInfo = {};
		createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
		createInfo.pApplicationInfo = &appInfo;
		
		//全局扩展?
		unsigned int glfwExtensionCount = 0;
		const char** glfwExtensions;

		//GLFW有一个方便的内置函数,返回它有关的扩展信息
		glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);

		createInfo.enabledExtensionCount = glfwExtensionCount;
		createInfo.ppEnabledExtensionNames = glfwExtensions;
		createInfo.enabledLayerCount = 0;

	    //VkResult result = vkCreateInstance(&createInfo, nullptr, &instance);

		if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
			throw std::runtime_error("failed to create instance!");
		}

	}
};

int main() {

	XFutureWorld xFutureWorld;

	try
	{
		xFutureWorld.run();
	}
	catch (const std::runtime_error& e)
	{
		std::cerr << e.what() << std::endl;
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;






	


	//uint32_t extensionCount = 0;
	//vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);

	//std::cout << extensionCount << " extensions supported" << std::endl;

	//glm::mat4 matrix;
	//glm::vec4 vec;
	//auto test = matrix * vec;

}

代码注释应该很清楚,参考的教程中 到第11章才能看到东西,感觉vulkan的使用上还是不是很便利,希望带来的各种提升能够真的弥补这种不便利性。

参考教程地址:

https://www.cnblogs.com/heitao/p/6903297.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值