[譯]Vulkan教程(16)圖形管道基礎之總結

[譯]Vulkan教程(16)圖形管道基礎之總結html

Conclusion 總結

We can now combine all of the structures and objects from the previous chapters to create the graphics pipeline! Here's the types of objects we have now, as a quick recap:編程

  • Shader stages: the shader modules that define the functionality of the programmable stages of the graphics pipeline
  • Fixed-function state: all of the structures that define the fixed-function stages of the pipeline, like input assembly, rasterizer, viewport and color blending
  • Pipeline layout: the uniform and push values referenced by the shader that can be updated at draw time
  • Render pass: the attachments referenced by the pipeline stages and their usage

咱們如今能夠將以前章節的全部結構體和對象組合起來to建立圖形管道了!下面是咱們有的對象的類型,快速回顧一下:數組

  • Shader階段:shader模塊that定義可編程階段的功能of圖形管道。
  • 固定功能狀態:全部的結構體that定義固定功能階段of管道,例如輸入組裝、光柵化、視口和顏色混合。
  • 管道佈局:uniform和push值that被shader引用that能夠在繪製時更新。
  • Render pass:附件that被管道階段引用和附件的用法。

All of these combined fully define the functionality of the graphics pipeline, so we can now begin filling in the VkGraphicsPipelineCreateInfo structure at the end of the createGraphicsPipeline function. But before the calls to vkDestroyShaderModule because these are still to be used during the creation.緩存

全部這些組合起來定義了圖形管道的功能,因此咱們能夠開始填入VkGraphicsPipelineCreateInfo 結構體at createGraphicsPipeline 函數結尾。可是要在調用vkDestroyShaderModule 以前,由於這些在建立過程當中仍是在被用到。less

VkGraphicsPipelineCreateInfo pipelineInfo = {}; pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; pipelineInfo.stageCount = 2; pipelineInfo.pStages = shaderStages;

 

We start by referencing the array of VkPipelineShaderStageCreateInfo structs.ide

開始時,咱們引用VkPipelineShaderStageCreateInfo 結構體的數組。函數

pipelineInfo.pVertexInputState = &vertexInputInfo; pipelineInfo.pInputAssemblyState = &inputAssembly; pipelineInfo.pViewportState = &viewportState; pipelineInfo.pRasterizationState = &rasterizer; pipelineInfo.pMultisampleState = &multisampling; pipelineInfo.pDepthStencilState = nullptr; // Optional
pipelineInfo.pColorBlendState = &colorBlending; pipelineInfo.pDynamicState = nullptr; // Optional

 

Then we reference all of the structures describing the fixed-function stage.佈局

而後咱們引用描述固定功能階段的全部結構體。ui

pipelineInfo.layout = pipelineLayout;

 

After that comes the pipeline layout, which is a Vulkan handle rather than a struct pointer.this

而後是管道佈局,which是一個Vulkan句柄,而不是結構體指針。

pipelineInfo.renderPass = renderPass; pipelineInfo.subpass = 0;

 

And finally we have the reference to the render pass and the index of the sub pass where this graphics pipeline will be used. It is also possible to use other render passes with this pipeline instead of this specific instance, but they have to be compatible with renderPass. The requirements for compatibility are described here, but we won't be using that feature in this tutorial.

最後,咱們引用render pass和subpass的索引where圖形管道會用到。有可能這個管道還使用另外一個render pass,而不是這個,但它們必須與renderPass兼容。對於兼容要求在here,但本教程中咱們不會用到這個特性。

pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; // Optional
pipelineInfo.basePipelineIndex = -1; // Optional

 

There are actually two more parameters: basePipelineHandle and basePipelineIndex. Vulkan allows you to create a new graphics pipeline by deriving from an existing pipeline. The idea of pipeline derivatives is that it is less expensive to set up pipelines when they have much functionality in common with an existing pipeline and switching between pipelines from the same parent can also be done quicker. You can either specify the handle of an existing pipeline with basePipelineHandle or reference another pipeline that is about to be created by index with basePipelineIndex. Right now there is only a single pipeline, so we'll simply specify a null handle and an invalid index. These values are only used if the VK_PIPELINE_CREATE_DERIVATIVE_BIT flag is also specified in the flags field of VkGraphicsPipelineCreateInfo.

其實還有2個參數:basePipelineHandle 和basePipelineIndex。Vulkan容許你建立新圖形管道by繼承一個已有的管道。這個管道繼承的思想是,它比較省資源when它們不少功能相同with一個現有的管道,and在同一父管道下的管道之間切換也會更快。你能夠要麼用basePipelineHandle 指定一個現有管道的句柄,要麼引用另外一個管道that即將被建立by basePipelineIndex索引。如今只有一個管道,因此咱們簡單地指定一個null句柄和一個無效的索引便可。這些值只會在VK_PIPELINE_CREATE_DERIVATIVE_BIT 標誌也在VkGraphicsPipelineCreateInfoflags 字段指定時用到。

Now prepare for the final step by creating a class member to hold the VkPipeline object:

如今準備好最後的步驟by建立一個類成員to記錄VkPipeline 對象。

VkPipeline graphicsPipeline;

 

And finally create the graphics pipeline:

最後建立圖形管道:

if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) { throw std::runtime_error("failed to create graphics pipeline!"); }

 

The vkCreateGraphicsPipelines function actually has more parameters than the usual object creation functions in Vulkan. It is designed to take multiple VkGraphicsPipelineCreateInfo objects and create multiple VkPipelineobjects in a single call.

vkCreateGraphicsPipelines 函數實際上有更多的參數that一般的對象建立函數in Vulkan。它被設計to接收多個VkGraphicsPipelineCreateInfo 對象and建立多個VkPipelineobjects對象in一個調用。

The second parameter, for which we've passed the VK_NULL_HANDLE argument, references an optional VkPipelineCache object. A pipeline cache can be used to store and reuse data relevant to pipeline creation across multiple calls to vkCreateGraphicsPipelines and even across program executions if the cache is stored to a file. This makes it possible to significantly speed up pipeline creation at a later time. We'll get into this in the pipeline cache chapter.

第二個參數,咱們傳入了VK_NULL_HANDLE ,其引用一個可選的VkPipelineCache 對象。一個管道緩存能夠被用於保存和複用數據that與管道建立相關-在屢次調用vkCreateGraphicsPipelines 中,甚至在屢次程序執行中if緩存被保存到了文件中。這樣就又可能to顯著地加速管道建立at之後。咱們將在後續章節詳談管道緩存。

The graphics pipeline is required for all common drawing operations, so it should also only be destroyed at the end of the program:

圖形管道是必要的for全部經常使用繪製操做,因此它只應該在程序結尾時被銷燬:

void cleanup() { vkDestroyPipeline(device, graphicsPipeline, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr); ... }

 

Now run your program to confirm that all this hard work has resulted in a successful pipeline creation! We are already getting quite close to seeing something pop up on the screen. In the next couple of chapters we'll set up the actual framebuffers from the swap chain images and prepare the drawing commands.

如今運行你的程序to確認that全部這些艱苦努力成功地建立了管道對象!咱們已經很接近to看到屏幕上顯示點東西了。接下來的幾章咱們將設置幀緩存from交換鏈image,並準備繪製命令。

C++ code / Vertex shader / Fragment shader

 

相關文章
相關標籤/搜索