Vulkan Samples 阅读 -- Basics(五): Input Attachments Sub Passes Offscreen Renderings
生活随笔
收集整理的這篇文章主要介紹了
Vulkan Samples 阅读 -- Basics(五): Input Attachments Sub Passes Offscreen Renderings
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- Input Attachments
- prepare
- render
- 小結
- Sub Passes
- prepare
- render
- 小結
- Offscreen Renderings
- prepare
- render
- 小結
Input Attachments
prepare
- loadAssets: 加載模型
- prepareUniformBuffers
- updateUniformBuffers
- setupRenderPass: 重寫了父類的方法, 創建 Attachment
-
創建的數量是: swapChain.imageCount
-
每個 Attachment 都需要創建 color 和 depth 附件
- VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
- VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
-
初始化每個 Attachment 的參數
-
創建subpass: std::array<VkSubpassDescription,2> subpassDescriptions
- VkAttachmentReference
- 創建一個給 Attachment 填充顏色和深度的subpass
- 創建一個swap chain 填充顏色的 subpass
- 再創建兩個引用 VkAttachmentReference inputReferences
- 將 fragment shader 中的輸出寫入Attachment : InputAttachments
- swap chain 的subpass 中引入 InputAttachments, 填充顏色
-
創建 VkSubpassDependency
dependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL;dependencies[0].dstSubpass = 0;//源為管線dependencies[0].srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;//輸出到 color attachment 中dependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;//讀取方式dependencies[0].srcAccessMask = VK_ACCESS_MEMORY_READ_BIT;dependencies[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;dependencies[0].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;// This dependency transitions the input attachment from color attachment to shader readdependencies[1].srcSubpass = 0;dependencies[1].dstSubpass = 1;//源為 color attachmentdependencies[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;//輸出到 fragment shader 中dependencies[1].dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;dependencies[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;//讀取方式dependencies[1].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;dependencies[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;dependencies[2].srcSubpass = 0;dependencies[2].dstSubpass = VK_SUBPASS_EXTERNAL;//源為 color attachment dependencies[2].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;// 輸出到管線中dependencies[2].dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;dependencies[2].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;dependencies[2].dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;dependencies[2].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; -
vkCreateRenderPass
-
- setupDescriptors
- 創建3個pool
- VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
- VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
- VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
- Attachment write
- vkCreateDescriptorSetLayout
- vkCreatePipelineLayout
- vkAllocateDescriptorSets
- vkUpdateDescriptorSets
- Attachment read
- descriptorSetLayoutBinding: 三個
- VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: 顏色
- VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: 深度
- VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
- vkCreateDescriptorSetLayout
- vkCreatePipelineLayout
- vkAllocateDescriptorSets
- vkUpdateDescriptorSets
- descriptorSetLayoutBinding: 三個
- 創建3個pool
- preparePipelines: attachment 需創建兩個pipeline, 一個寫 Attachment, 一個讀 attachment
- VkGraphicsPipelineCreateInfo: 多出了為Attachment配置的代碼
- pipelineCI.renderPass = renderPass;
- Attachment write
- pipelineCI.subpass = 0;
- pipelineCI.layout = pipelineLayouts.attachmentWrite;
- vkCreateGraphicsPipelines
- Attachment read
- pipelineCI.subpass = 1
- pipelineCI.layout = pipelineLayouts.attachmentRead;
- vkCreateGraphicsPipelines
- VkGraphicsPipelineCreateInfo: 多出了為Attachment配置的代碼
- buildCommandBuffers: 分別為兩個 subpass 創建 CommandBuffer /*First sub passFills the attachments*/{vks::debugmarker::beginRegion(drawCmdBuffers[i], "Subpass 0: Writing attachments", glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.attachmentWrite);vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.attachmentWrite, 0, 1, &descriptorSets.attachmentWrite, 0, NULL);scene.draw(drawCmdBuffers[i]);vks::debugmarker::endRegion(drawCmdBuffers[i]);}/*Second sub passRender a full screen quad, reading from the previously written attachments via input attachments*/{vks::debugmarker::beginRegion(drawCmdBuffers[i], "Subpass 1: Reading attachments", glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));vkCmdNextSubpass(drawCmdBuffers[i], VK_SUBPASS_CONTENTS_INLINE);vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.attachmentRead);vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.attachmentRead, 0, 1, &descriptorSets.attachmentRead[i], 0, NULL);vkCmdDraw(drawCmdBuffers[i], 3, 1, 0, 0);vks::debugmarker::endRegion(drawCmdBuffers[i]);}
render
- draw
- updateUniformBuffers
小結
?? 本節主要講怎樣使用 Attachments 進行渲染. 但是我覺得這里的重點是怎樣使用subpass進行渲染.
Sub Passes
prepare
- loadAssets: 加載模型和紋理
- initLights: 設置光源
- prepareUniformBuffers
- updateUniformBufferDeferredMatrices
- updateUniformBufferDeferredLights
- setupFrameBuffer: 重寫父類方法
- 當屏幕大小發生遍布重寫設置Gbuffer: createGBufferAttachments
- writeDescriptorSets
- vkUpdateDescriptorSets
- 正向渲染
- writeDescriptorSets
- vkUpdateDescriptorSets
- vkCreateFramebuffer
- setupRenderPass: 重寫父類方法
- createGBufferAttachments: 創建Gbuffer
- createAttachment – position
- createAttachment – normal
- createAttachment – albedo
- VkAttachmentDescription: 有5個, 與上一節相似,主要設置,怎么寫入gbuffer,怎么從gbuffer讀數據
- vkCreateRenderPass
- createGBufferAttachments: 創建Gbuffer
- setupDescriptorSetLayout
- vkCreateDescriptorSetLayout
- vkCreatePipelineLayout
- preparePipelines: 中增加了 blendAttachmentStates
- colorBlendState.attachmentCount = static_cast<uint32_t>(blendAttachmentStates.size());
- colorBlendState.pAttachments = blendAttachmentStates.data();
- setupDescriptorPool
- 創建3個pool
- VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
- VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
- VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT
- 創建3個pool
- setupDescriptorSet
- vkAllocateDescriptorSets
- writeDescriptorSets
- prepareCompositionPass:
- Descriptor set layout
- descriptorSetLayoutBinding
- Position input attachment
- Normal input attachment
- Albedo input attachment
- Light positions
-vkCreateDescriptorSetLayout
- descriptorSetLayoutBinding
- Pipeline layout
- vkCreatePipelineLayout
- Descriptor sets
- vkAllocateDescriptorSets
- writeDescriptorSets
- writeDescriptorSet
- Position texture target
- Normals texture target
- Albedo texture target
- Fragment shader lights
-vkUpdateDescriptorSets
- writeDescriptorSet
- Pipeline
- pipelineCreateInfo
- vkCreateGraphicsPipelines
- Transparent (forward) pipeline
- vkCreateDescriptorSetLayout
- vkCreatePipelineLayout
- vkAllocateDescriptorSets
- vkUpdateDescriptorSets
- vkCreateGraphicsPipelines
- Descriptor set layout
- buildCommandBuffers: subpass有3個
- 第一個將場景數據寫入Gbuffer
- 第二個用gbuffer數據渲染到attachment中
- 第三個正向渲染
render
- draw
- updateUniformBufferDeferredMatrices
- updateUniformBufferDeferredLights
小結
?? 本節主要講延遲渲染的方法. 總體來說就是更詳細介紹了subpass的使用方法.
Offscreen Renderings
prepare
- loadAssets: 加載模型
- prepareOffscreen: 準備離屏渲染資源
- 準備的資源如下
- vkCreateImage(color)
- vkGetImageMemoryRequirements
- vkAllocateMemory
- vkBindImageMemory
- vkCreateImageView
- vkCreateSampler
- vkCreateImage(depth)
- vkGetImageMemoryRequirements
- vkAllocateMemory
- vkBindImageMemory
- vkCreateImageView
- attchmentDescriptions: 分別為color和depth
- subpassDescription: 附加Attachment引用
- vkCreateRenderPass
- vkCreateFramebuffer: 將渲染后的資源寫入 frame buffer
- prepareUniformBuffers
- updateUniformBuffers
- updateUniformBufferOffscreen: 離屏pass更新uniform
- setupDescriptorSetLayout
- vkCreateDescriptorSetLayout
- vkCreatePipelineLayout
- preparePipelines: 新建四個pipelines
- Render-target debug display
- Mirror
- Phong shading pipelines
- Offscreen
- setupDescriptorPool
- setupDescriptorSet: 為三個pipeline設置
- Mirror plane descriptor set (Mirror)
- Shaded descriptor sets (Phong shading pipelines)
- Offscreen
- buildCommandBuffers: subpass有2個
- First render pass: Offscreen rendering
- Scene rendering with applied radial blur
render
- draw
- updateUniformBuffers
- updateUniformBufferOffscreen
小結
?? 離屏渲染也就是將渲染結果寫入 attachment中 . 是 attachment 結合 subpass的一種實用方法.
總結
以上是生活随笔為你收集整理的Vulkan Samples 阅读 -- Basics(五): Input Attachments Sub Passes Offscreen Renderings的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 7.3 通用控件
- 下一篇: 软件工程导论复习知识点