• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TRANSFER_IMAGE_H_
16 #define SRC_VULKAN_TRANSFER_IMAGE_H_
17 
18 #include "amber/result.h"
19 #include "amber/vulkan_header.h"
20 #include "src/format.h"
21 #include "src/vulkan/resource.h"
22 
23 namespace amber {
24 namespace vulkan {
25 
26 class CommandBuffer;
27 class Device;
28 
29 /// Wrapper around a Vulkan VkImage.
30 class TransferImage : public Resource {
31  public:
32   TransferImage(Device* device,
33                 const Format& format,
34                 VkImageAspectFlags aspect,
35                 VkImageType image_type,
36                 VkImageUsageFlags image_usage_flags,
37                 uint32_t x,
38                 uint32_t y,
39                 uint32_t z,
40                 uint32_t mip_levels,
41                 uint32_t base_mip_level,
42                 uint32_t used_mip_levels,
43                 uint32_t samples);
44   ~TransferImage() override;
45 
AsTransferImage()46   TransferImage* AsTransferImage() override { return this; }
47   Result Initialize() override;
GetVkImageView()48   VkImageView GetVkImageView() const { return view_; }
49 
50   void ImageBarrier(CommandBuffer* command_buffer,
51                     VkImageLayout to_layout,
52                     VkPipelineStageFlags to_stage);
53 
54   /// Records a command on |command_buffer| to copy the buffer contents from the
55   /// host to the device.
56   void CopyToDevice(CommandBuffer* command_buffer) override;
57   /// Records a command on |command_buffer| to copy the buffer contents from the
58   /// device to the host.
59   void CopyToHost(CommandBuffer* command_buffer) override;
60 
61  private:
62   Result CreateVkImageView(VkImageAspectFlags aspect);
63   Result AllocateAndBindMemoryToVkImage(VkImage image,
64                                         VkDeviceMemory* memory,
65                                         VkMemoryPropertyFlags flags,
66                                         bool force_flags,
67                                         uint32_t* memory_type_index);
68   VkBufferImageCopy CreateBufferImageCopy(VkImageAspectFlags aspect,
69                                           uint32_t mip_level);
70 
71   VkImageViewType GetImageViewType() const;
72 
73   /// An extra `VkBuffer` is used to facilitate the transfer of data from the
74   /// host into the `VkImage` on the device.
75   VkBuffer host_accessible_buffer_ = VK_NULL_HANDLE;
76   VkDeviceMemory host_accessible_memory_ = VK_NULL_HANDLE;
77 
78   VkImageCreateInfo image_info_;
79   VkImageAspectFlags aspect_;
80 
81   VkImage image_ = VK_NULL_HANDLE;
82   VkImageView view_ = VK_NULL_HANDLE;
83   VkDeviceMemory memory_ = VK_NULL_HANDLE;
84 
85   VkImageLayout layout_ = VK_IMAGE_LAYOUT_UNDEFINED;
86   VkPipelineStageFlags stage_ = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
87 
88   uint32_t mip_levels_;
89   uint32_t base_mip_level_;
90   uint32_t used_mip_levels_;
91   uint32_t samples_;
92 };
93 
94 }  // namespace vulkan
95 }  // namespace amber
96 
97 #endif  // SRC_VULKAN_TRANSFER_IMAGE_H_
98