DEV Community

lim73011968
lim73011968

Posted on

Part1 Vulkan: Drawing Triangle 1

This is not a guide, just me writing down my notes
This fallows the Vulkan Tutorial link

Some details I learned along the way

1.Base Code section

  • This is more related with Cpp than Vulkan but I'm just gonna write it as a reminder to myself. Members in cpp or oop means the components in the class which are (filed/variable) and (method/function)

  • When objects are created it uses vkCreateXXX or allocate using vkAllocateXXX. However when these objects are no longer needed, these are destroyed using vkDestroyXXX and vkFreeXXX

  • pAllocator. This is an optional parameter that allows you to specify callbacks for a custom memory allocator. We will ignore this parameter in the tutorial and always pass nullptr as argument.(cause we are beginners)

2.Instance section

  • Instance is the connection between "Your Application" and "The Vulkan Library". Also it initializes the Vulkan Library

  • VkApplicationInfo --> this just specify application information. This code is not really required but it provides some useful info to dirver.

//not really required but still provide the info needed for the dirver.
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;
}
Enter fullscreen mode Exit fullscreen mode
  • VkInstanceCreateInfo --> this is not optional and tells the Vulkan driver which global extensions and validation layers we want to use. You don't have to know what global extension and validation layers are because they will be mentioned in next chapter.
//this creates the info full detail is in the end of the code.
//for now don't think to much about this.

VkInstanceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo;
Enter fullscreen mode Exit fullscreen mode

-vkCreateInstance --> now the important part! we are going to store the "createInfo" inside the "vkInstance instance variable". Not complicated!!

//What is "nullptr" inside the second parameter???
//that is the callback which we will not use cause... I mentioned this before
VkResult result = vkCreateInstance(&createInfo, nullptr, &instance);
Enter fullscreen mode Exit fullscreen mode
  • vkEnumerateInstanceExtensionProperties --> this just retrieves the supported extension before actually creating an instance(not really required but cool to have/checks which support is missing)
    uint32_t extensionCount = 0;
    //you can just think of this like an array
    //where inside the extensions vector the information about supported extensions will be stored annnnd 
    //it could be accesed by using index named extension count
    std::vector<VkExtensionProperties> extensions(extensionCount);
    //this like mentioned above the actual supported extensions details will bbe stored in extensions vector
    //------explaination of the code-------
    //vkEnumerateInstanceExtensionProperties(the callback which we will not be using, the address of "number of extensions", the actual vector data);
    vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data());

    //now actually printing
    std::cout << "available extensions:\n";
    //this is not realted with vulkan but still gonna write it down
    //(const auto& extension : extensions)
    // ":" --> bring the first value in extensions vecotr and put it inside extension variable
    // auto --> let computer choose what data type will the extension be
    // & point to the actual value of extension variable do not make a copy of extension varaible
    for (const auto& extension : extensions) {
        std::cout << '\t' << extension.extensionName << '\n';
    }
Enter fullscreen mode Exit fullscreen mode

The Full Code

#include <vector>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
//the vulkan header has been replaced becuase the GLFW will call it automatically
#include <iostream>
#include <cstdlib>

//this is a constant definition
//uint32_t is 32bit integer
const uint32_t WIDTH = 800;
const uint32_t HEIGHT = 600;

class HelloTriangleApplication {
public:
    void run() {
    //add this method initWindow() to run
    initWindow();
        initVulkan();
        mainLoop();
        cleanup();
    }

private:
    //write a private member that creates the actual window
    GLFWwindow* window;
    //a private member that hold the instance
    VkInstance instance;

    void initWindow(){
    //the very first call is glfwInit();
    //why?? you may ask because we have to tell the glfw it is not an OpenGL and should tell glfw to NOT create OpenGL Context
    glfwInit();
    //not sure what these 2 does
    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
    //this as you know creates an actual window
    //detail of this line
    //window = glfwCreateWindow(the width, the height, the title, which monitor to open the window, this is only relevent to OpenGL);
    window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
    }

    void initVulkan() {
    //a self made function that creates an instance
    createInstance();
    }

    void mainLoop() {
    //run the application untill the window is closed or an error occurs
        while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();
    }
    }
    //we need to clean up resources
    void cleanup() {
    //destroyes the instance
    vkDestroyInstance(instance, nullptr );
    //this destroyes the window
    glfwDestroyWindow(window);

    glfwTerminate();
    }

    void createInstance() {
    //VkApplicationInfo-Structure specifiying application information
    //this whole code is kind of optional but it provieds some useful information to the driver
    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;

    //the code below is not optional and tells the Vulkan driver which global extensions and validation layers we want to use. 
    //You don't have to know what global extension and validation layers are 
    //because they will be mentioned in next chapter

    VkInstanceCreateInfo createInfo{};
    createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    createInfo.pApplicationInfo = &appInfo;

    //---------EXTENSION PART---------

    //how many extension do we need??
    uint32_t glfwExtensionCount = 0;
    //a storage for extension names
    const char** glfwExtensions;

    //this basically fills in two variable we mentioned
    //1. glfwExtensions
    //2. glfwExtensionCount
    //Asks GLFW: "How many extensions do we need??? and which one??"
    glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);

    //we put the "count" and the "name tag" inside the VkInstanceCreateInfo createInfo we mentioned earlier
    createInfo.enabledExtensionCount = glfwExtensionCount;
    createInfo.ppEnabledExtensionNames = glfwExtensions;

    //we are not touching this for now
    createInfo.enabledLayerCount = 0;

    //this is like giving information "Which stuff I am going to use to make Vulkan Project!!!"  
    //1.the first parameter is the pointer to the createInfo. See the code we have been putting information about "Extensions" in the createInfo.
    //2.the second paramter is the callback which we don't have to know cause it will always stay nullptr.
    //3.remember the "Vkinstance instance;" we mentioned earlier outside private and public??? we are going to put the created instance inside that variable.
    VkResult result = vkCreateInstance(&createInfo, nullptr, &instance);

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

    uint32_t extensionCount = 0;
    //you can just think of this like an array
    //where inside the extensions vector the information about supported extensions will be stored annnnd 
    //it could be accesed by using index named extension count
    std::vector<VkExtensionProperties> extensions(extensionCount);
    //this like mentioned above the actual supported extensions details will bbe stored in extensions vector
    //------explaination of the code-------
    //vkEnumerateInstanceExtensionProperties(the callback which we will not be using, the address of "number of extensions", the actual vector data);
    vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data());

    //now actually printing
    std::cout << "available extensions:\n";
    //this is not realted with vulkan but still gonna write it down
    //(const auto& extension : extensions)
    // ":" --> bring the first value in extensions vecotr and put it inside extension variable
    // auto --> let computer choose what data type will the extension be
    // & point to the actual value of extension variable do not make a copy of extension varaible
    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;
}

Enter fullscreen mode Exit fullscreen mode

3.Validation layers
1.Validation layers --> Because Vulkan is such a verbose API, error checking is difficult to handle. So we use something called validation layers so we can check errors efficiently. There are two layers we will see: instance and device specific

  • instance layers would only check calls related to global Vulkan objects like instances
  • device specific layers would only check calls related to a specific GPU.

still making....

Top comments (0)