1 // Copyright 2018 The Amber Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef SRC_VULKAN_FRAME_BUFFER_H_ 16 #define SRC_VULKAN_FRAME_BUFFER_H_ 17 18 #include <memory> 19 #include <vector> 20 21 #include "src/pipeline.h" 22 #include "src/vulkan/transfer_image.h" 23 24 namespace amber { 25 namespace vulkan { 26 27 class CommandBuffer; 28 class Device; 29 30 /// Wrapper around a Vulkan FrameBuffer object. 31 class FrameBuffer { 32 public: 33 FrameBuffer( 34 Device* device, 35 const std::vector<const amber::Pipeline::BufferInfo*>& color_attachments, 36 amber::Pipeline::BufferInfo depth_stencil_attachment, 37 const std::vector<const amber::Pipeline::BufferInfo*>& resolve_targets, 38 uint32_t width, 39 uint32_t height); 40 ~FrameBuffer(); 41 42 Result Initialize(VkRenderPass render_pass); 43 44 void ChangeFrameToDrawLayout(CommandBuffer* command); 45 void ChangeFrameToProbeLayout(CommandBuffer* command); 46 void ChangeFrameToWriteLayout(CommandBuffer* command); 47 GetVkFrameBuffer()48 VkFramebuffer GetVkFrameBuffer() const { return frame_; } GetColorBufferPtr(size_t idx)49 const void* GetColorBufferPtr(size_t idx) const { 50 return color_images_[idx]->HostAccessibleMemoryPtr(); 51 } 52 53 // Only record the command for copying the image that backs this 54 // framebuffer to the host accessible buffer. The actual submission 55 // of the command must be done later. 56 void TransferImagesToHost(CommandBuffer* command); 57 void TransferImagesToDevice(CommandBuffer* command); 58 59 void CopyImagesToBuffers(); 60 void CopyBuffersToImages(); 61 GetWidth()62 uint32_t GetWidth() const { return width_; } GetHeight()63 uint32_t GetHeight() const { return height_; } 64 65 private: 66 void ChangeFrameLayout(CommandBuffer* command, 67 VkImageLayout color_layout, 68 VkPipelineStageFlags color_stage, 69 VkImageLayout depth_layout, 70 VkPipelineStageFlags depth_stage); 71 72 Device* device_ = nullptr; 73 std::vector<const amber::Pipeline::BufferInfo*> color_attachments_; 74 std::vector<const amber::Pipeline::BufferInfo*> resolve_targets_; 75 amber::Pipeline::BufferInfo depth_stencil_attachment_; 76 VkFramebuffer frame_ = VK_NULL_HANDLE; 77 std::vector<std::unique_ptr<TransferImage>> color_images_; 78 std::vector<std::unique_ptr<TransferImage>> resolve_images_; 79 std::unique_ptr<TransferImage> depth_stencil_image_; 80 uint32_t width_ = 0; 81 uint32_t height_ = 0; 82 uint32_t depth_ = 1; 83 }; 84 85 } // namespace vulkan 86 } // namespace amber 87 88 #endif // SRC_VULKAN_FRAME_BUFFER_H_ 89