Vlukan基础教程-02.创建实例
作者:Sun zi chao     发布时间:2022-03-12 09:40:00    阅读次数:109
本系列文章介绍Vlukan编程。Vlukan是一个跨平台的2D和3D绘图应用程序接口(API),是OpenGL的“继任者”。学习本教程的同学需要有GLFW以及C++的编程经验。有OpenGL或DirectX经验更好。

Vulkan程序的框架还是非常复杂的。 还没开始几何体绘制,框架的初始化就非常多步骤了。今天我们先来说下,Vulkan实例的创建。

VKInstance(实例):用于存储Vulkan程序相关状态的数据结构,用来区分不同的Vulkan应用程序或者同一应用程序内部不同的Vulkan上下文。

要想使用Vulkan的功能,我们的显卡就必须支持Vulkan的扩展。我们创建一个showExtensions函数来检索系统支持的Vulkan扩展。

检索系统所支持的VUlkan扩展

	void showExtensions(){
        uint32_t extensionCount = 0;
        vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);

        std::vector<VkExtensionProperties> extensions(extensionCount);

        vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data());

        std::cout << "系统支持Vulkan扩展:\n";

        for (const auto& extension : extensions) {
            std::cout << '\t' << extension.extensionName << '\n';
        }
    }
	

我们用GLFW创建Vulkan实例时也要用到一些基本的Vulkan扩展,具体操作如下:

Vulkan实例引入扩展

	//获取GLFW所需Vulkan的扩展
	uint32_t glfwExtensionCount = 0;
	const char** glfwExtensions;
	glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);

	std::cout << "GLFW所需Vulkan扩展:\n";
	int i;
	for(i=0;i<glfwExtensionCount;i++)
		std::cout<<glfwExtensions[i]<<std::endl;

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

	createInfo.enabledLayerCount = 0;

	//创建Vulkan实例
	if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
		throw std::runtime_error("failed to create instance!");
	}
	

完整程序

	#define GLFW_INCLUDE_VULKAN
	#include <GLFW/glfw3.h>

	#include <iostream>
	#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_01", nullptr, nullptr);
		}

		void initVulkan() {
			showExtensions();
			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;

			//获取GLFW所需Vulkan的扩展
			uint32_t glfwExtensionCount = 0;
			const char** glfwExtensions;
			glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);

			std::cout << "GLFW所需Vulkan扩展:\n";
			int i;
			for(i=0;i<glfwExtensionCount;i++)
				std::cout<<glfwExtensions[i]<<std::endl;

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

			createInfo.enabledLayerCount = 0;

			//创建Vulkan实例
			if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
				throw std::runtime_error("failed to create instance!");
			}
		}

		void showExtensions(){
			uint32_t extensionCount = 0;
			vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);

			std::vector<VkExtensionProperties> extensions(extensionCount);

			vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data());

			std::cout << "系统支持Vulkan扩展:\n";

			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;
	}

	




桂ICP备11003301号-1 公安备案号:45040302000027 Copyright @ 2021- 2022 By Sun zi chao

阅读统计: 1.93W 文章数量: 76 运行天数: 416天 返回cmnsoft