Vulkan Tutorial 02 Writing Vulkan application Framework Prototypes

Source: Internet
Author: User

Operating system: Windows8.1

Graphics: Nivida gtx965m

Development tools: Visual Studio 2017

General structure

In the previous section, we created a properly configured, operational Vulkan application and tested it using the test code. In this section we start from scratch and build a prototype of a GLFW-based Vulkan application Prototyping framework using the following code.

#include <vulkan/vulkan.h>#include<iostream>#include<stdexcept>#include<functional>classhellotriangleapplication { Public:    voidrun () {Initvulkan ();        Mainloop ();    Cleanup (); }Private:    voidInitvulkan () {}voidMainloop () {}voidCleanup () {}};intMain () {Hellotriangleapplication app; Try{app.run (); } Catch(Conststd::runtime_error&e) {Std::cerr<< e.what () <<Std::endl; returnexit_failure; }    returnexit_success;}

First add the Vulkan header file from the Lunarg SDK, which provides the functions, structs, and enumerations you need to purchase your Vulkan application. We include the stdexcept and iostream header files for throwing exception information, and the functional header file is used for the Resource Management Section to support lambda expressions.

The program is encapsulated into a class that stores Vulkan private member objects and adds basic functions to initialize them. The first call starts with the Initvulkan function. When everything is ready, we go into the main loop and start rendering the frame. We will join the mainloop function containing the loop loop call, which is not stopped until the GLFW form management. When the form closes and mainloop returns, we need to free any resources we have already applied for, which is defined in the cleanup function.

During the run of the program, if any serious error occurs, we will throw a std::runtime_error and indicate the exception description, which will be captured by the main function and printed with hints. Soon you will encounter an example of throwing an error about the Vulkan application that does not support a necessary extension feature.

Basically, in each subsequent section, a new Vulkan function call is added from the Initvulkan function, and the added function generates Vulkan objects and is saved as a private member of the class, remember in cleanup Cleanup and release of resources in the

Resource Management

We know that every memory allocated through malloc needs free memory resources after it is used, and every Vulkan object we create needs to be explicitly destroyed when it is not in use. In C + +, you can use <memory> to complete auto resource management, but in this section, choose to explicitly write all of the memory allocation and release operations, mainly because the Vulkan design concept is to clear each step, Understand the life cycle of each object and avoid the exceptions that might be caused by unknown code.

Of course, after this section, we can implement auto resource management by overloading std::shared_ptr . For more general Vulkan procedures, it is recommended to follow the RAII principle to maintain the management of resources.

The Vulkan object can be created directly using the vkcreatexxx system function, or it can be assigned by an object with functions such as vkallocatexxx . Make sure that each object calls vkdestroyxxx and vkfreexxx to destroy and release the corresponding resources when they are not in use. The parameters of these functions are usually different for different types of objects, but they share one parameter: Pallocator. This is an optional parameter, Vulkan allows us to customize the memory allocator. We will ignore this parameter in this tutorial and always take nullptr as the parameter.

Integrating GLFW

If we develop programs that do not require screen-based display, then pure Vulkan itself can be perfectly supported for development. But if we create some exciting visualizations, we need to introduce the form system GLFW and replace #include <vulkan/vulkan.h> accordingly.

#define Glfw_include_vulkan<GLFW/glfw3.h>

Vulkan related support has been provided in the new version of GLFW, with detailed usage recommendations in the official information.

With the replacement, GLFW will be used to support Vulkan, and the Vulkan header file is loaded automatically. Add a initwindow function call in the run function and make sure that it is called first before other function calls. We will use this function to complete the GLFW form initialization work.

void run () {    initwindow ();    Initvulkan ();    Mainloop ();    Cleanup ();} Private :     void Initwindow () {    }

The first call in Initwindow is glfwinit (), which initializes the GLFW library. Because the initial GLFW is to create a context for OpenGL, here we need to tell it not to invoke OpenGL-related initialization operations.

Glfwwindowhint (Glfw_client_api, GLFW_NO_API);

Pay particular attention to the window size setting, which we'll call later, and now use another window hint to just use it.

Glfwwindowhint (glfw_resizable, Glfw_false);

Now all that is left is to create the actual form. Add a glfwwindow* form where private class members store their references and initialize the form:

window = Glfwcreatewindow ("Vulkan", nullptr, nullptr);

The first three parameters define the width, height, and title of the form. The fourth parameter allows you to create a listener to open the form, and the last parameter is related to OpenGL, we choose Nullptr.

Use constants instead of hard-coded widths and heights, as we will refer to that number multiple times in subsequent content. Add the following lines above the hellotriangleapplication class definition:

Const int  - ; Const int ;

and replace the form to create the code statement as:

" Vulkan ", nullptr, nullptr);

You should now have a initwindow function that looks like this:

void Initwindow () {    glfwinit ();    Glfwwindowhint (Glfw_client_api, GLFW_NO_API);    Glfwwindowhint (glfw_resizable, glfw_false);     " Vulkan " , nullptr, nullptr);}

Keep the program running until an error occurs or the form closes, we need to add an event loop to the Mainloop function as follows:

void Mainloop () {    while ( !  Glfwwindowshouldclose (window)) {        glfwpollevents ();    }}

This code should be easy to read. It loops and checks the GLFW event until the X button is pressed, or the form is closed. The loop structure will call the render function later.

Once the form is closed, we need to clean up the resource through the cleanup function and end the GLFW itself.

void Cleanup () {    Glfwdestroywindow (window);    Glfwterminate ();}

To run the program, we should see a white form named Vulkan until the form is closed to terminate the application.

OK, by now we have completed a skeleton prototype of the Vulkan program, and in the next section we will create the first Vulkan Object!

Vulkan Tutorial 02 Writing Vulkan application Framework Prototypes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.