• 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                 uint32_t x,
37                 uint32_t y,
38                 uint32_t z,
39                 uint32_t mip_levels,
40                 uint32_t base_mip_level,
41                 uint32_t used_mip_levels,
42                 uint32_t samples);
43   ~TransferImage() override;
44 
45   Result Initialize(VkImageUsageFlags usage);
GetVkImageView()46   VkImageView GetVkImageView() const { return view_; }
47 
48   void ImageBarrier(CommandBuffer* command_buffer,
49                     VkImageLayout to_layout,
50                     VkPipelineStageFlags to_stage);
51 
52   /// Records a command on |command_buffer| to copy the buffer contents from the
53   /// host to the device.
54   void CopyToDevice(CommandBuffer* command_buffer) override;
55   /// Records a command on |command_buffer| to copy the buffer contents from the
56   /// device to the host.
57   void CopyToHost(CommandBuffer* command_buffer) override;
58 
59  private:
60   Result CreateVkImageView(VkImageAspectFlags aspect);
61   Result AllocateAndBindMemoryToVkImage(VkImage image,
62                                         VkDeviceMemory* memory,
63                                         VkMemoryPropertyFlags flags,
64                                         bool force_flags,
65                                         uint32_t* memory_type_index);
66   VkBufferImageCopy CreateBufferImageCopy(VkImageAspectFlags aspect,
67                                           uint32_t mip_level);
68 
69   VkImageViewType GetImageViewType() const;
70 
71   /// An extra `VkBuffer` is used to facilitate the transfer of data from the
72   /// host into the `VkImage` on the device.
73   VkBuffer host_accessible_buffer_ = VK_NULL_HANDLE;
74   VkDeviceMemory host_accessible_memory_ = VK_NULL_HANDLE;
75 
76   VkImageCreateInfo image_info_;
77   VkImageAspectFlags aspect_;
78 
79   VkImage image_ = VK_NULL_HANDLE;
80   VkImageView view_ = VK_NULL_HANDLE;
81   VkDeviceMemory memory_ = VK_NULL_HANDLE;
82 
83   VkImageLayout layout_ = VK_IMAGE_LAYOUT_UNDEFINED;
84   VkPipelineStageFlags stage_ = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
85 
86   uint32_t mip_levels_;
87   uint32_t base_mip_level_;
88   uint32_t used_mip_levels_;
89   uint32_t samples_;
90 };
91 
92 }  // namespace vulkan
93 }  // namespace amber
94 
95 #endif  // SRC_VULKAN_TRANSFER_IMAGE_H_
96