1. 程式人生 > >CSharpGL(57)[譯]Vulkan清空螢幕

CSharpGL(57)[譯]Vulkan清空螢幕

CSharpGL(57)[譯]Vulkan清空螢幕

本文是對(http://ogldev.atspace.co.uk/www/tutorial51/tutorial51.html)的翻譯,作為學習Vulkan的一次嘗試。

不翻譯的話,每次都在看第一句,那就學不完了。

Background 背景

+BIT祝威+悄悄在此留下版了個權的資訊說:

Welcome back. I hope that you've been able to complete the previous tutorial successfully and you are now ready to continue. In this tutorial we will do a very basic operation that usually starts a new frame - clear the screen. In OpenGL this can be done very easily with just the glClear() command but as you can already assume - it's a totally different ball game with Vulkan. This tutorial will introduce us to three new and improtant Vulkan entities - swap chain, images and the command buffers.

歡迎回來。我希望你已經完成了上一篇教程,現在準備好繼續了。本教程中我們將做一個非常基本的操作——在開啟新的一幀時首先做的——清空螢幕。在OpenGL中,這可以直接簡單地用glClear()命令完成,但是你可能已經想到了——這在Vulkan中是完全不同的事。本教程將介紹給大家3個新的重要的Vulkan實體——交換鏈,Image和命令快取。

Let's look at a very simple OpenGL render loop which just clears the screen:

+BIT祝威+悄悄在此留下版了個權的資訊說:

我們來看看OpenGL的一個簡單的渲染迴圈——僅僅清空螢幕:

1 void RenderLoop()
2 {
3     glClear(GL_COLOR_BUFFER_BIT);
4     glutSwapBuffers();   // Or in GLFW: glfwSwapBuffers(pWindow);
5 }

 

What we have here is a GL command to clear the color buffer followed by a GLUT or GLFW call that swaps the front buffer which is currently being displayed with the back buffer (which is really the buffer that glClear targeted). These two seemingly innocent functions hide a ton of back stage activity by the OpenGL driver. What Vulkan does is to provide us with a standard interface to the low level operations that used to be the sole domain of the OpenGL driver. Now we have to take control and manage these back stage activities ourselves.

這段程式碼裡,我們用一個GL命令清空顏色快取,然後用一個GLUT或GLFW呼叫來交換front快取(當前在顯示的)與back快取(glClear清空的)。這兩個看起來弱弱的函式隱藏了OpenGL驅動的成噸的後臺活動。Vulkan提供給我們的,是一個對低層操作的介面,以前這是屬於OpenGL驅動的獨有領域。現在你必須自己來管理控制這些後臺活動了。

+BIT祝威+悄悄在此留下版了個權的資訊說:

So now let's think what really happens in the driver when it executes that render loop. In most graphics drivers there is a concept of a command buffer. The command buffer is a memory buffer that the driver fills with GPU instructions. The driver translates the GL commands into GPU instructions. It submits the command buffer to the GPU and there is usually some form of a queue of all the command buffers that are currently in flight. The GPU picks up the command buffers one by one and executes their contents. The command buffers contain instructions, pointers to resources, state changes and everything else that the GPU needs in order to execute the the OpenGL commands correctly. Each command buffer can potentially contain multiple OpenGL commands (and it usually does because it is more efficient). It is up to the driver to decide how to batch the OpenGL commands into the command buffers. The GPU informs the driver whenever a command buffer is completed and the driver can stall the application to prevent it from getting too much ahead of the GPU (e.g. the GPU renders frame N while the application is already at frame N+10).

所以現在我們來想想,驅動在執行那個渲染迴圈時到底發生了什麼。大多數圖形卡里都有一個“命令快取”的概念。命令快取,是驅動填入了GPU指令的一塊記憶體。驅動將GL命令翻譯成GPU指令。驅動將命令快取提交給GPU,正在執行的所有的命令快取通常形成某種形式的佇列。GPU一個一個地拿起命令快取,執行它們的內容。命令快取包換指令、對資源的指標、狀態變化和任何執行OpenGL命令所需的東西。每個命令快取都可能包含多個OpenGL命令(通常是這樣,因為更高效)。驅動決定如何將OpenGL命令批發為命令快取。命令快取完成後,GPU就通知驅動,驅動就暫停應用程式,以避免它領先GPU太多(例如GPU渲染幀N,同時應用程式已經在幀N+10了)。

This model works pretty well. Why do we need to change it? Well, making the driver in charge of command buffer management prevents us from some important potential performance optimizations that only we can make. For example, consider the Mesh class that we developed in previous tutorials when we studied the Assimp library. Rendering a mesh meant that in each frame we had to submit the same group of draw commands when the only real change was a few matrices that controlled the transformation. For each draw command the driver had to do considerable amount of work which is a waste of time in each frame. What if we could create a command buffer for this mesh class ahead of time and just submit it in each frame (while changing the matrices somehow)? That's the whole idea behind Vulkan. For the OpenGL driver the frame is just a series of GL commands and the driver doesn't understand what exactly the application is doing. It doesn't even know that these commands will be repeated in the next frame. Only the app designer understands what's going on and can create command buffers in a way that will match the structure of the application.

這個模型工作得相當好。為什麼我們要改變它?好吧,讓驅動來負責管理命令快取,可以讓我們失去只有我們能做到的重要的效能優化機會。例如,考慮我們在之前的教程中學習Assimp庫時開發的網格類。渲染一個網格,意味著對每個陣我們必須相同的渲染命令,而真正有所改變的僅僅是用於控制方位變換的矩陣。對每個渲染命令,驅動必須做可觀的工作量,這在每一幀裡都造成了浪費。如果我們能為這個網格類建立一個命令快取,每一幀裡提交它(同時想辦法修改矩陣)該多好?這就是Vulkan的核心思想。對OpenGL驅動來說,一幀只是一系列GL命令,驅動不理解應用程式到底在做什麼。它甚至不知道這些命令會在下一幀重複。只有app設計者理解在發生什麼,只有他能建立命令快取,且使之與應用程式的結構相適應。

Another area where OpenGL never excelled in is multi threading. Submitting draw commands in different threads is possible but complex. The problem is that OpenGL was not designed with multi threading in mind. Therefore, in most cases a graphics app has one rendering thread and uses multi-threading for the rest of the logic. Vulkan addresses multi threading by allowing you to build command buffers concurrently and introduces the concept of queues and semaphores to handle concurrency at the GPU level.

OpenGL不擅長的另一個領域就是多執行緒。在不同的執行緒裡提供渲染命令是可能的,但是很複雜。問題在於OpenGL在設計之初就沒有考慮要支援多執行緒。因此,大多數時候一個圖形app有1個渲染執行緒和多個邏輯執行緒。Vulkan支援多執行緒:它允許你併發地構建命令快取,引入了queue和semaphore的概念來處理GPU層次的併發問題。

Let's get back to that render loop. By now you can imagine that what we are going to do is create a command buffer and add the clear instruction to it. What about swap buffers? We have been using GLUT/GLFW so we never gave much thought about it. GLUT/GLFW are not part of OpenGL. They are libraries built on top of windowing APIs such as GLX (Linux), WGL (Windows), EGL (Android) and CGL (Mac). They make it easy to build OS independent OpenGL programs. If you use the underlying APIs directly you will have to create an OpenGL context and window surface which are in general corresponding to the instance and surface we created in the previous tutorial. The underlying APIs provide functions such as glXSwapBuffers() and eglSwapBuffers() in order to swap the front and back buffers that are hidden under the cover of the surface. They don't provide you much control beyond that.

我們回到那個渲染迴圈。現在你可以想象我們計劃做的,就是建立一個命令快取,給它加入清空指令。那麼swap快取呢?我們一直在用GLUT/GLFW,所以沒有好好考慮過它。GLUT/GLFW不是OpenGL的一部分。它們是基於視窗API(例如Linux的GLX、Windows的WGL、Android的EGL和Mac的CGL)的庫。它們讓構建作業系統無關的OpenGl程式簡單了。如果你直接使用底層API,你將不得不建立OpenGL上下文和視窗surface,它們一般都是與我們在之前教程中建立的instance和surface對應的。底層API提供glXSwapBuffers()和eglSwapBuffers()這樣的函式,用於交換front和back快取。除此之外它們沒有提供給你更多的控制權。

Vulkan goes a step further by introducing the concepts of swap chain, images and presentation engine. The Vulkan spec describes the swap chain as an abstraction of an array of presentable images that are associated with the surface. The images are what is actually being displayed on the screen and only one can be displayed at a time. When an image is displayed the application is free to prepare the remaining images and queue them for presentation. The total number of images can also be controlled by the application.

+BIT祝威+悄悄在此留下版了個權的資訊說:

Vulkan走得更遠,它引入了交換鏈、image和表現引擎的概念。Vulkan說明書將交換鏈描述為關聯到surface的可顯示image的陣列的抽象。這些image實際上就是顯示到螢幕上的東西,同一時間只能有1個顯示出來。當一個image被顯示時,應用程式可以自由準備其他image,並將其列隊待用。Image總數也可以被應用程式控制。

The presentation engine represents the display on the platform. It is responsible for picking up images from the queue, presenting them on the screen and notifying the application when an image can be reused.

表現引擎代表在平臺上的顯示。他負責從佇列裡拿起image,提交給螢幕,當image可以被重用時通知應用程式。

Now that we understand these concepts let's review what we need add to the previous tutorial in order to make it clear the screen. Here's the one time initialization steps:

既然我們理解了這些概念,我們來評審一下需要加入上一個教程的東西,以便清空螢幕。下面是一次性初始化的步驟:

  1. Get the command buffer queue from the logical device. Remember that the device create info included an array of VkDeviceQueueCreateInfo structures with the number of queues from each family to create. For simplicity we are using just one queue from graphics family. So this queue was already created in the previous tutorial. We just need to get its address.
    從logical device得到命令快取。記住,device建立資訊中包含一個VkDeviceQueueCreateInfo結構體和queue編號。簡單來說,我們只用圖形family的一個queue。這個queue是已經在上一個教程中建立好了的。我們只需找到它的地址。
  2. Create the swap chain and get the handles to its images.
    建立交換鏈,得到它的image的控制代碼。
  3. Create a command buffer and add the clear instruction to it.
    建立命令快取,新增清空指令。

And here's what we need to do in the render loop:

這裡是我們需要在渲染迴圈中做的:

  1. Acquire the next image from the swap chain.
    從交換鏈中請求下一個image。
  2. Submit the command buffer.
    提交命令快取。
  3. Submit a request to present the image.
    提交“顯示image”的請求。

Now let's review the code to accomplish this.

現在我們來評審一下相關程式碼。

Source walkthru 原始碼瀏覽

All the logic that needs to be developed for this tutorial will go into the following class:

+BIT祝威+悄悄在此留下版了個權的資訊說:

本教程中所有需要開發的邏輯都在下述類中:

 1 class OgldevVulkanApp
 2 {
 3 public:
 4 
 5     OgldevVulkanApp(const char* pAppName);
 6     
 7     ~OgldevVulkanApp();
 8     
 9     void Init();    
10     
11     void Run();
12     
13 private:
14 
15     void CreateSwapChain();
16     void CreateCommandBuffer();
17     void RecordCommandBuffers();
18     void RenderScene();
19 
20     std::string m_appName;
21     VulkanWindowControl* m_pWindowControl;
22     OgldevVulkanCore m_core;    
23     std::vector<VkImage> m_images;
24     VkSwapchainKHR m_swapChainKHR;
25     VkQueue m_queue;
26     std::vector<VkCommandBuffer> m_cmdBufs;
27     VkCommandPool m_cmdBufPool;
28 };

 

What we have here are a couple of public functions (Init() and Run()) that will be called from main() later on and several private member functions that are based on the steps that were described in the previous section. In addition, there are a few private member variables. The VulkanWindowControl and OgldevVulkanCore which were part of the main() function in the previous tutorial were moved here. We also have a vector of images, swap chain object, command queue, vector of command buffers and a command buffer pool. Now let's look at the Init() function:

+BIT祝威+悄悄在此留下版了個權的資訊說:

上述程式碼中是2個public函式(Init()和Run()),它們將被main()函式呼叫;還有幾個private函式,是我們在上一篇教程中涉及的。另外,還有幾個private成員變數。上一篇教程中的VulkanWindowControl和OgldevVulkanCore在這裡被移除了。我們還有image陣列、交換鏈物件、命令佇列、命令快取陣列和命令快取池。現在我們來看看Init()函式:

 1 void OgldevVulkanApp::Init()
 2 {
 3 #ifdef WIN32
 4     m_pWindowControl = new Win32Control(m_appName.c_str());
 5 #else            
 6     m_pWindowControl = new XCBControl();
 7 #endif    
 8     m_pWindowControl->Init(WINDOW_WIDTH, WINDOW_HEIGHT);
 9 
10     m_core.Init(m_pWindowControl);
11         
12     vkGetDeviceQueue(m_core.GetDevice(), m_core.GetQueueFamily(), 0, &m_queue);
13 
14     CreateSwapChain();
15     CreateCommandBuffer();
16     RecordCommandBuffers();
17 }

 

This function starts in a similar fashion to the previous tutorial by creating and initializing the window control and Vulkan core objects. After that we call the private members to create the swap chain and command buffer and to record the clear instruction into the command buffer. Note the call to vkGetDeviceQueue(). This Vulkan function fetches the handle of a VkQueue object from the device. The first three parameters are the device, the index of the queue family and the index of the queue in that queue family (zero in our case because there is only one queue). The driver returns the result in the last parameter. The two getter functions here were added in this tutorial to the Vulkan core object.

類似上一篇教程中的方式,這個函式開始時建立和初始化視窗控制元件和Vulkan核心物件。之後,我們呼叫privaite成員,建立交換鏈和命令快取,將清空指令寫入命令快取。注意對vkGetDeviceQueue()的呼叫。這個Vulkan函式從device提取VkQueue物件的控制代碼。前3個引數分別是device、queue family的索引和queue在queue family中的索引(本例中為0,因為只有1個queue)。驅動返回的結果儲存到最後的引數裡。本教程還加入了兩個對Vulkan核心物件的getter函式。

Let's review the creation of the swap chain step by step:

我們來一步步地評審建立交換鏈的過程:

1 void OgldevVulkanApp::CreateSwapChain()
2 {          
3     const VkSurfaceCapabilitiesKHR& SurfaceCaps = m_core.GetSurfaceCaps();
4          
5     assert(SurfaceCaps.currentExtent.width != -1);

 

The first thing we need to do is to fetch the surface capabilities from the Vulkan core object. Remember that in the previous tutorial we populated a physical device database in the Vulkan core object with info about all the physical devices in the system. Some of that info was not generic but specific to the combination of the physical device and the surface that was created earlier. An example is the VkSurfaceCapabilitiesKHR vector which contains a VkSurfaceCapabilitiesKHR structure for each physical device. The function GetSurfaceCaps() indexes into that vector using the physical device index (which was selected in the previous tutorial). The VkSurfaceCapabilitiesKHR structure contains a lot of info on the surface. The currentExtent member describes the current size of the surface. Its type is a VkExtent2D which contains a width and height. Theoretically, the current extent should contain the dimensions that we have set when creating the surface and I have found that to be true on both Linux and Windows. In several examples (including the one in the Khronos SDK) I saw some logic which checks whether the width of the current extent is -1 and if so overwrites that with desired dimensions. I found that logic to be redundant so I just placed the assert you see above.

+BIT祝威+悄悄在此留下版了個權的資訊說:

我們要做的第一件事是從Vulkan核心物件獲取surface的capabilities。回憶上一篇教程中我們在Vulkan核心物件中填入了一個physical device資料庫,其中含有系統上所有的physical device資訊。有些資訊不是通用的,而是針對之前建立的physical device和surface的組合的。一個粒子是VkSurfaceCapabilitiesKHR陣列,其包含對每個physical device的VkSurfaceCapabilitiesKHR結構體。函式用physical device索引(在上一篇教程中選擇的)使用這個陣列。VkSurfaceCapabilitiesKHR結構體包含surface的很多資訊。其中的currentExtent成員描述了surface當前的大小。它的型別是VkExtent2D,其包含寬度和高度。理論上,當建立surface時,當前範圍應該包含我們設定的維度。我發現這在Linux和Windows上都是真的。在幾個例子中(包括Khronos SDK中的例子)我看到一些邏輯是用於檢查當前範圍的寬度是否是-1.如果是,就用需要的維度覆蓋那個寬度。我發現那個邏輯是多餘的,所以我就用上述程式碼中的assert替換了它。

1     uint NumImages = 2;
2 
3     assert(NumImages >= SurfaceCaps.minImageCount);
4     assert(NumImages <= SurfaceCaps.maxImageCount);

 

Next we set the number of images that we will create in the swap chain to 2. This mimics the behavior of double buffering in OpenGL. I added assertions to make sure that this number is within the valid range of the platform. I assume that you won't hit these assertions but if you do you can try with one image only.

接下來,我們將交換鏈中的image數量設定為2。這模仿了OpenGL中的雙快取。我加入了assert來確保這個數值是在平臺的有效要求內的。我假設你不會觸發這些assert,但是如果你碰到了,你可以試試只有1個image。

1     VkSwapchainCreateInfoKHR SwapChainCreateInfo = {};
2     
3     SwapChainCreateInfo.sType            = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
4     SwapChainCreateInfo.surface          = m_core.GetSurface();
5     SwapChainCreateInfo.minImageCount    = NumImages;

 

The function that creates the swap chain takes most of its parameters from the VkSwapchainCreateInfoKHR structure. The first three parameters are obvious - the structure type, the surface handle and the number of images. Once created the swap chain is permanently attached to the same surface.

建立交換鏈的函式的大部分引數來自VkSwapchainCreateInfoKHR結構體。前3個引數很明顯——結構體型別,surface控制代碼和image數量。一旦建立後,交換鏈就永遠附著到同一surface上了。

1     SwapChainCreateInfo.imageFormat      = m_core.GetSurfaceFormat().format;
2     SwapChainCreateInfo.imageColorSpace  = m_core.GetSurfaceFormat().colorSpace;

 

Next comes the image format and color space. The image format was discussed in the previous tutorial. It describes the layout of data in image memory. It contains stuff such as channels (red, green and/or blue) and format (float, normalized int, etc). The color space describes the way the values are matched to colors. For example, this can be linear or sRGB. We will take both from the physical device database.

+BIT祝威+悄悄在此留下版了個權的資訊說:

接下來是image格式和顏色空間。上一篇教程討論過顏色格式了。它描述資料在image記憶體中的佈局方式。它包含通道(RGB)、格式(float,標準化int,等)等內容。顏色空間描述值對映到顏色的方式。例如,可以是線性的或sRGB的。我們將從physical device資料庫使用這兩種。

1     SwapChainCreateInfo.imageExtent      = SurfaceCaps.currentExtent;

 

We can create the swap chain with a different size than the surface. For now, just grab the current extent from the surface capabilities structure.

我們可以建立大小與surface不同的交換鏈。目前,就用surface的capabilities結構體的當前範圍好了。

1     SwapChainCreateInfo.imageUsage       = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;

 

We need to tell the driver how we are going to use this swap chain. We do that by specifying a combination of bit masks and there are 8 usage bits in total. For example, the swap chain can be used as a source or destination of a transfer (buffer copy) operation, as a depth stencil attachment, etc. We just want a standard color buffer so we use the bit above.

我們需要告訴驅動,我們將如何使用交換鏈。我們通過標識一個最多8位的掩碼來實現。例如,交換鏈可以被用於轉移(快取複製)的源或目的,用於模板附件,等。我們只想要一個標準的顏色快取,所以用上述位掩碼。

1     SwapChainCreateInfo.preTransform     = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;

 

The pre transform field was designed for hand held devices that can change their orientation (cellular phones and tablets). It specifies how the orientation must be changed before presentation (90 degrees, 180 degrees, etc). It is more relevant to Android so we just tell the driver not to do any orientation change.

欄位preTransform用於可改變朝向的手持裝置(行動電話和平板電腦)。它標明在顯示前應該如何改變朝向(90度,180度,等)。這和Android關係比較大,所以我們告訴驅動不修改朝向。

1     SwapChainCreateInfo.imageArrayLayers = 1;

 

imageArrayLayers is intended for stereoscopic applications where rendering takes place from more than one location and then combined before presentations. An example is VR where you want to render the scene from each eye separately. We are not going to do that today so just specify 1.

欄位imageArrayLayers用於立體應用程式,其渲染髮生在不止一處,然後聯合起來再顯示。

1     SwapChainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;

 

Swap chain images can be shared by queues of different families. We will use exclusive access by the queue family we have selected previously.

+BIT祝威+悄悄在此留下版了個權的資訊說:

交換鏈的image可以被不同family的queue共享。我們將??(譯者注:看不懂)

1     SwapChainCreateInfo.presentMode      = VK_PRESENT_MODE_FIFO_KHR;

 

In the previous tutorial we briefly touched on the presentation engine which is the part of the platform involved in actually taking the swap chain image and putting it on the screen. This engine also exists in OpenGL where it is quite limited in comparison to Vulkan. In OpenGL you can select between single and double buffering. Double buffering avoids tearing by switching the buffers only on VSync and you have some control on the number of VSync in a second. That's it. Vulkan, however, provides you with no less than four different modes of operation that allow a higher level of flexibility and performance. We will be conservative here and use the FIFO mode which is the most similar to OpenGL double buffering.

上一篇教程中我們稍微提及了表現引擎,它參與了接收交換鏈image並將其放到螢幕上的過程。這個引擎也在OpenGL中存在,但是與Vulkan相比,存在感很低。在OpenGL中你可以在單快取和雙快取中選擇。雙快取避免了切換快取時的撕裂,你還可以控制垂直同步的速度。僅此而已。但是,Vulkan提供至少4種操作模式,允許更高的擴充套件性和效能。我們就保守點,用FIFO模式,這是最接近OpenGL雙快取的模式。

1     SwapChainCreateInfo.clipped          = true;

 

The clipped field indicates whether the driver can discard parts of the image that are outside of the visible surface. There are some obscure cases where this is interesting but not in our case.

欄位clipped表面驅動十分能忽略image位於可見surface的外部的部分。有時候這會有稀裡糊塗的問題,但是我們的例子裡沒有。

1     SwapChainCreateInfo.compositeAlpha   = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;

 

compositeAlpha controls the manner in which the image is combined with other surfaces. This is only relevant on some of the operating systems so we don't use it.

欄位控制image聯合其他surface的方式。這隻在某些作業系統的才有用,我們不用管它。

1     VkResult res = vkCreateSwapchainKHR(m_core.GetDevice(), &SwapChainCreateInfo, NULL, &m_swapChainKHR);
2     CHECK_VULKAN_ERROR("vkCreateSwapchainKHR error %d\n", res);    

 

Finally, we can create the swap chain and get its handle.

+BIT祝威+悄悄在此留下版了個權的資訊說:

最後,我們建立交換鏈,得到它的控制代碼。

1     uint NumSwapChainImages = 0;
2     res = vkGetSwapchainImagesKHR(m_core.GetDevice(), m_swapChainKHR, &NumSwapChainImages, NULL);
3     CHECK_VULKAN_ERROR("vkGetSwapchainImagesKHR error %d\n", res);

 

When we created the swap chain we specified the minimum number of images it should contain. In the above call we fetch the actual number of images that were created.

建立交換鏈後,我們標明瞭它應該包含的image的最小數量。上述程式碼中我們獲取了實際建立的image數量。

1     m_images.resize(NumSwapChainImages);
2     m_cmdBufs.resize(NumSwapChainImages);
3     
4     res = vkGetSwapchainImagesKHR(m_core.GetDevice(), m_swapChainKHR, &NumSwapChainImages, &(m_images[0]));
5     CHECK_VULKAN_ERROR("vkGetSwapchainImagesKHR error %d\n", res);
6 }

 

We have to get the handles of all the swap chain images so we resize the image handle vector accordingly. We also resize the command buffer vector because we will record a dedicated command buffer for each image in the swap chain.

我們必須得到所有交換鏈image的控制代碼,所以我們調整控制代碼陣列的大小。我們還要調整命令快取陣列的大小,因為我們將為交換鏈的每個image記錄一個命令快取。

The following function creates the command buffers:

下述函式建立了命令快取:

1 void OgldevVulkanApp::CreateCommandBuffer()
2 {
3     VkCommandPoolCreateInfo cmdPoolCreateInfo = {};
4     cmdPoolCreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
5     cmdPoolCreateInfo.queueFamilyIndex = m_core.GetQueueFamily();
6     
7     VkResult res = vkCreateCommandPool(m_core.GetDevice(), &cmdPoolCreateInfo, NULL, &m_cmdBufPool);    
8     CHECK_VULKAN_ERROR("vkCreateCommandPool error %d\n", res);

 

Command buffer are not created directly. Instead, they must be allocated from pools. As expected, the motivation is performance. By making command buffers part of a pool, better memory management and reuse can be implemented. It is imported to note that the pools are not thread safe. This means that any action on the pool or its command buffers must be explicitly synchronized by the application. So if you want multiple threads to create command buffers in parallel you can either do this synchronization or simply create a different pool for each thread.

命令快取不是直接建立的。相反,它們必須從池裡分配。可以想見,動機是效能。讓命令快取稱為池的一部分,可以實現更好的記憶體管理和複用。重要的一點是,池不是執行緒安全的。這意味著對池或它的命令快取的操作必須是明確的同步執行。所以如果你想在多執行緒並行地建立命令快取,要麼同步執行,要麼為不同的執行緒各建立一個執行緒。

The function vkCreateCommandPool() creates the pool. It takes a VkCommandPoolCreateInfo structure parameter whose most important member is the queue family index. All commands allocated from this pool must be submitted to queues from this queue family.

函式vkCreateCommandPool()建立這個池。它接收VkCommandPoolCreateInfo結構體作為引數,其最重要的成員是quue family索引。由此池申請的所有命令都必須提交到這個queue family。

1     VkCommandBufferAllocateInfo cmdBufAllocInfo = {};
2     cmdBufAllocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
3     cmdBufAllocInfo.commandPool = m_cmdBufPool;
4     cmdBufAllocInfo.commandBufferCount = m_images.size();
5     cmdBufAllocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
6         
7     res = vkAllocateCommandBuffers(m_core.GetDevice(), &cmdBufAllocInfo, &m_cmdBufs[0]);            
8     CHECK_VULKAN_ERROR("vkAllocateCommandBuffers error %d\n", res);
9 }

 

We are now ready to create the command buffers. In the VkCommandBufferAllocateInfo structure we specify the pool we have just created and the number of command buffers (we need a dedicated command buffer per image in the swap chain). We also specify whether this is a primary or secondary command buffer. Primary command buffers are the common vehicle for submitting commands to the GPU but they cannot reference each other. This means that you can have two very similar command buffers but you still need to record everything into each one. You cannot share the common stuff between them. This is where secondary command buffers come in. They cannot be directly submitted to the queues but they can be referenced by primary command buffers which solves the problem of sharing. At this point we only need primary command buffers.

我們現在可以建立命令快取了。在VkCommandBufferAllocateInfo結構體中我們標明瞭我們剛剛建立的池和命令快取的數量(對交換鏈中的每個image,我們需要一個專用的命令快取)。一級命令快取是提交命令到GPU的輪子,但是它們不能相互引用。這意味著你可能有兩個很相似的命令快取,但是你還是需要在每個裡記錄所有的資訊。你不能在兩者之間共享任何東西。這就是二級命令快取出場的時候了。它們不能被直接提交到queue,但是可以被一級命令快取引用,這解決了共享的問題。目前我們只需要一級命令快取。

Now let's record the clear instruction into our new command buffers.

現在我們將清空指令寫入我們新的命令快取中。

1 void OgldevVulkanApp::RecordCommandBuffers() 
2 {
3     VkCommandBufferBeginInfo beginInfo = {};
4     beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
5     beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;

 

Recording of command buffers must be done inside a region of the code explictly marked by a vkBeginCommandBuffer() and vkEndCommandBuffer(). In the VkCommandBufferBeginInfo structure we have a field named 'flags' where we tell the driver that the command buffers will be resubmitted to the queue over and over again. There are other usage models but for now we don't need them.

+BIT祝威+悄悄在此留下版了個權的資訊說:

記錄命令快取必須在程式碼vkBeginCommandBuffer()和vkEndCommandBuffer()之間進行。在結構體中我們有一個欄位'flags',它告訴驅動命令快取將會被反覆提交到queue。還有其他使用模式,不過暫時我們不需要。

1     VkClearColorValue clearColor = { 164.0f/256.0f, 30.0f/256.0f, 34.0f/256.0f, 0.0f };
2     VkClearValue clearValue = {};
3     clearValue.color = clearColor;

 

We have to specify our clear color using the two structures above. The first one is a union of four float/int/uint which allows different ways to do that. The second structure is a union of a VkClearColorValue structure and a VkClearDepthStencilValue structure. This scheme is used in parts of the API that can take either of the two structures. We go with the color case. Since I'm very creative today I used the RGB values from the color of the Vulkan logo ;-) 

我們必須用上述2個結構體宣告自己的清空顏色。第一個是4個float/int/uint的聯合體,支援多種使用方式。第二個是VkClearColorValue結構體和VkClearDepthStencilValue結構體的聯合體。這個方案普遍運用於能接收兩種結構體的API。我們用顏色功能。由於我今天創造力十足,我用的RGB值來自Vulkan的logo顏色。嘿嘿。

Note that each color channel goes from 0 (darkest) to 1 (brightest) and that this endless spectrum of real numbers is divided to 256 discrete segments which is why I divide by 256.

注意,每個顏色通道都是從0(最暗)到1(最亮),這個無限的實數光譜被分為256個離散的片段。因此我這裡除了256。

1     VkImageSubresourceRange imageRange = {};
2     imageRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
3     imageRange.levelCount = 1;
4     imageRange.layerCount = 1;

 

We need to specify the range of images that we want to clear. In future tutorials we will study more complex schemes where there will be multiple mipmap levels, layers, etc. For now we just want the basics so we specify one mip map level and one layer. The aspectMask field tells the driver whether to clear the color, depth or stenctil (or a combination of them). We are only interested in the color aspect of the images.

我們要標明需要清空的image。在未來的教程中,我們將要就更復雜的方案,到時候會有多mipmap level,層,等。目前我們只想做基本工作,所以我們標識1個mipmap level和1個層即可。欄位aspectMask告訴驅動是否要清空顏色、深度或模版(或其聯合體)。我們只對image的顏色方面感興趣。

 1     for (uint i = 0 ; i < m_cmdBufs.size() ; i++) {             
 2         VkResult res = vkBeginCommandBuffer(m_cmdBufs[i], &beginInfo);
 3         CHECK_VULKAN_ERROR("vkBeginCommandBuffer error %d\n", res);
 4 
 5         vkCmdClearColorImage(m_cmdBufs[i], m_images[i], VK_IMAGE_LAYOUT_GENERAL, &clearColor, 1, &imageRange);                
 6 
 7         res = vkEndCommandBuffer(m_cmdBufs[i]);
 8         CHECK_VULKAN_ERROR("vkEndCommandBuffer error %d\n", res);
 9     }
10 }

 

We are now ready to record the command buffers. As mentioned earlier, the commands that do the actual recording must be inside a block marked by calls that begin and end a command buffer. For that we specify the command buffer to record to and the beginInfo structure which we already prepared. Since we have an array of command buffers (one buffer per swap chain image) the entire thing is enclosed inside a for loop. vkCmdClearColorImage() records the clear instruction into the command buffer. As parameters it takes the command buffer to record, the target image, the layout of the image in memory, the clear color, the number of VkImageSubresourceRange structures to use and a pointer to an array of these structures (only one in our case).

我們可以開始記錄命令快取了。之前提到過,記錄這些命令的操作必須位於開始和結束命令快取的函式呼叫之間。為此,我們標明需要記錄的命令快取和備好的beginInfo結構體。由於我們有命令快取的陣列(每個交換鏈image對應一個快取),整件事被包在一個for迴圈裡。函式vkCmdClearColorImage()記錄清空指令到命令快取中。它接收命令快取、目標image、image在記憶體中的佈局、清空色、VkImageSubresourceRange結構體的數量和指向這些結構體陣列的指標為引數,

We prepared everything we need and we can now code our main render function. In standard OpenGL this usually means specifying a list of GL commands to draw stuff followed by a swap buffers call (be it GLUT, GLFW or any other windowing API). For the driver it means a tedious repetition of command buffer recording and submission where changes from one frame to the next are relatively small (changes in shader matrices, etc). But in Vulkan all our command buffers are already recorded! We just need to queue them to the GPU. Since we have to be more verbose in Vulkan we also need to manage how we acquire and image for rendering and how to tell the presentation image to display it.

我們準備好了所需的一切, 現在可以編寫主渲染函數了。在標準OpenGL中這通常意味著標明很多GL命令,以渲染些什麼,之後再交換快取(用GLUT、GLFW或任何其他視窗API)。對於區域,這意味著一個冗長乏味的重複命令快取記錄和提交操作,兩幀之間的變化其實很小(shader矩陣的改變,等)。但是在Vulkan中我們所有的命令快取都已經記錄好了!我們只需將它們排隊送到GPU。我們在Vulkan中不得不做很多冗長的工作,還需要管理如何請求要渲染的image,如何告訴表現image去顯示。

1 void OgldevVulkanApp::RenderScene()
2 {
3     uint ImageIndex = 0;
4     
5     VkResult res = vkAcquireNextImageKHR(m_core.GetDevice(), m_swapChainKHR, UINT64_MAX, NULL, NULL, &ImageIndex);
6     CHECK_VULKAN_ERROR("vkAcquireNextImageKHR error %d\n", res);

 

The first thing we need to do is to acquire an image from the presentation engine which is available for rendering. We can acquire more than one image (e.g. if we plan to render two or more frames ahead) in an advanced scenario but for now one image will be enough. The API call above takes the device and swap chain as the first two parameters, respectively. The third parameter is the amount of time we're prepared to wait until that function returns. Often, the presentation engine cannot provide an image immediately because it needs to wait for an image to be released or some internal OS or GPU event (e.g. the VSync signal of the display). If we specify zero we make this a non blocking call which means that if an image is available we get it immediately and if not the function returns with an error. Any value above zero and below the maximum value of an unsigned 64bit integer will cause a timeout of that number of nanoseconds. The value of UINT64_MAX will cause the function to return only when an image becomes available (or some internal error occured). This seems like the safest course of action for us here. The next two parameters are pointers to a semaphore and a fence, respectively. Vulkan was designed with a lot of asynchronous operation in mind. This means that you can define inter-dependencies between queues on the GPU, between the CPU and GPU, etc. This allows you to submit work to the image even if it is not really ready to be rendered to (which is a bit counter intuitive to what vkAcquireNextImageKHR is supposed to do but can still happen). These semaphore and fence are synchornization primitives that must be waited upon before the actual rendering to the image can begin. A semaphore syncs between stuff on the GPU and the fence between the host CPU and the GPU. As you can see, I've specified NULL in both cases which might be unsafe and theoretically is not supposed to work yet it does. This may be because of the simplicity of our application. It allowed me to postpone all the synchronization business to a later date. Please let me know if you encounter problems because of this. The last parameter to the function is the index of the image that became available.

+BIT祝威+悄悄在此留下版了個權的資訊說:

我們需要做的第一件事,是從表現引擎中獲取一個可用於渲染的image。在高階場景中,我們可以獲取不止一個(例如,如果我們計劃提前渲染2個或多個幀),但目前1個image就足夠了。上述API呼叫接收device和交換鏈為前2個引數。第3個引數是我們準備等待函式返回的時間。常常地,表現引擎不能立即提供image,因為他需要等待image被釋放或某些作業系統內部或GPU事件(例如顯示的垂直同步訊號)。如果我們寫0,我們就讓它成為了一個非阻塞呼叫,也就是說,如果有image可用,我們會立即得到它,如果沒有,函式就返回一個error。任何大於0小於uint64的整數都會引發超時(納秒)。UINT64_MAX的值會讓函式只在有可用image(或者發生內部錯誤)時才返回。這看起來像是最安全的選擇。後2個引數是訊號和fence指標。Vulkan被設計為很多非同步操作。這意味著你可以在GPU上的queue之間、在CPU和GPU之間定義相互依賴關係。這執行你提交工作到image,即使它還沒有準備好被渲染(這違反直覺,vkAcquireNextImageKHR原本不該這樣,但是仍舊是可能發生的)。這些訊號和fence是同步的基石,實際渲染到image開始前,必須等它們。一個訊號同步GPU上的東西,fence用於宿主CPU和GPU之間。如你所見,我用NULL填入引數,這可能不安全,理論上行不通,但實際上還是工作了。

1     VkSubmitInfo submitInfo = {};
2     submitInfo.sType                = VK_STRUCTURE_TYPE_SUBMIT_INFO;
3     submitInfo.commandBufferCount   = 1;
4     submitInfo.pCommandBuffers      = &m_cmdBufs[ImageIndex];
5     
6     res = vkQueueSubmit(m_queue, 1, &submitInfo, NULL);    
7     CHECK_VULKAN_ERROR("vkQueueSubmit error %d\n", res);

 

Now that we have an image, let's submit the work to the queue. The vkQueueSubmit() function takes the handle of a queue, the number of VkSubmitInfo structures and a pointer to the corresponding array. The last parameter is a fence which we will conviniently ignore for now. The VkSubmitInfo actually contains 8 members in addition to the standard sType, but we are going to use only 2 (so just imagine how much complexity is still down there). We specify that we have one command buffer and we provide its address (the one that corresponds to the acquired image). The Vulkan spec notes that submission of work can have a high overhead and encourages us to pack as many command buffers as we possibly can into that API to minimize that overhead. In this simple example we don't have an opportunity to do that but we should keep that in mind as our application becomes more complex in the future.

現在我們有了image,我們把工作提交到queue吧。函式vkQueueSubmit()接收queue的控制代碼,結構體的數量和對於陣列的指標。最後一個引數是fence,目前我們忽略它。除了sType外,VkSubmitInfo實際上還有8個成員,但是我們計劃只用2個(所以想象下後面還會有多少複雜的東西吧)。我們標明我們還有1個命令快取,提供它的地址(對應到獲取到的image的那個)。Vulkan說明書提到,提交工作的開銷比較大,鼓勵我們儘可能打包最多的命令快取到API,以最小化開銷。在這個簡單的例子中,我們沒有機會這麼做,但是我們應該記住這一點,因為應用程式會變得越來越複雜。

1     VkPresentInfoKHR presentInfo = {};
2     presentInfo.sType              = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
3     presentInfo.swapchainCount     = 1;
4     presentInfo.pSwapchains        = &m_swapChainKHR;
5     presentInfo.pImageIndices      = &ImageIndex;
6     
7     res = vkQueuePresentKHR(m_queue, &presentInfo);    
8     CHECK_VULKAN_ERROR("vkQueuePresentKHR error %d\n" , res);
9 }
+BIT祝威+悄悄在此留下版了個權的資訊說:

 

Once the previous API call has returned we know that the command buffer is on its way to the GPU queue but we have no idea when exactly it is going to be executed, and frankly, we don't really care. Command buffers in a queue are guaranteed to be processed in the order of submission and since we submit a present command after the clear command into the same queue we know that the image will be cleared before it is presented. So the vkQueuePresent() call is basically a marker that ends the frame and tells the presentation engine to display it. This function takes two parameters - a queue which has presentation capabilities (we took care of that when initializing the device and queue) and a pointer to a VkPresentInfoKHR structure. This structure contains, among other stuff, two arrays of equal sizes. A swap chain array and an image index array. This means that you can queue a present command to multiple swap chains where each swap chain is connected to a different window. Every swap chain in the array has a corresponding image index which specifies which image will be presented. The swapchainCount member says how many swap chains and images we are going present.

一旦之前的API返回了,我們就知道命令快取前往GPU的queue了,但是我們不知道具體何時它才會被執行,坦白說,我們也不在乎。一個queue裡的命令快取被保證會按提交的順序執行,由於我們在清空命令之後向同一queue提交顯示命令,我們知道image會先清空後顯示。所以呼叫vkQueuePresent()函式基本上就是標記幀結束,告訴表現引擎去顯示。這個函式接收2個引數——有表現能力的queue(初始化device和queue的時候我們處理好了它)和VkPresentInfoKHR結構體的指標。除了其他東西,這個結構體還包含兩個大小相同的陣列——一個交換鏈陣列和一個image索引陣列。這意味著你可以將一個顯示命令排到多個交換鏈的queue上,每個交換鏈都可以連線到不同的視窗。陣列中的每個交換鏈有個對應的image索引,標明哪個image要被顯示。成員swapchainCount告訴我們我們要顯示多少交換鏈和image。

1 void OgldevVulkanApp::Run()
2 {    
3     while (true) {        
4         RenderScene();
5     }
6 }

 

Our main render function is very simple. We loop endlessly and call the function that we have just reviewed.

+BIT祝威+悄悄在此留下版了個權的資訊說:

我們的主渲染函式很簡單。我們無限迴圈,呼叫剛剛評審過的函式即可。

 1 int main(int argc, char** argv)
 2 {
 3     OgldevVulkanApp app("Tutorial 51");
 4     
 5     app.Init();
 6     
 7     app.Run();
 8     
 9     return 0;
10 }

 

The main function is also very simple. We declare an OgldevVulkanApp object, initialize and run it.

+BIT祝威+悄悄在此留下版了個權的資訊說:

主函式main還是很簡單。我們宣告OgldevVulkanApp物件,初始化和執行它。

That's it for today. I hope that your window is clear. Next time we will draw a triangle.

今天就到這裡吧。我行為你的視窗被清空了。下次我們將畫一個三角形。

&n