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_BUFFER_H_
16 #define SRC_VULKAN_TRANSFER_BUFFER_H_
17 
18 #include <vector>
19 
20 #include "amber/result.h"
21 #include "amber/vulkan_header.h"
22 #include "src/format.h"
23 #include "src/vulkan/resource.h"
24 
25 namespace amber {
26 namespace vulkan {
27 
28 class CommandBuffer;
29 class Device;
30 
31 /// Wrapper around a Vulkan VkBuffer object.
32 class TransferBuffer : public Resource {
33  public:
34   TransferBuffer(Device* device, uint32_t size_in_bytes, Format* format);
35   ~TransferBuffer() override;
36 
AsTransferBuffer()37   TransferBuffer* AsTransferBuffer() override { return this; }
AddUsageFlags(VkBufferUsageFlags flags)38   Result AddUsageFlags(VkBufferUsageFlags flags) {
39     if (buffer_ != VK_NULL_HANDLE) {
40       return Result(
41           "Vulkan: TransferBuffer::AddUsageFlags Usage flags can't be changed "
42           "after initializing the buffer.");
43     }
44     usage_flags_ |= flags;
45     return {};
46   }
47   Result Initialize() override;
GetVkBufferView()48   const VkBufferView* GetVkBufferView() const { return &view_; }
49 
GetVkBuffer()50   VkBuffer GetVkBuffer() const { return buffer_; }
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   VkBufferUsageFlags usage_flags_ = 0;
61   VkBuffer buffer_ = VK_NULL_HANDLE;
62   VkDeviceMemory memory_ = VK_NULL_HANDLE;
63   VkBufferView view_ = VK_NULL_HANDLE;
64   VkFormat format_ = VK_FORMAT_UNDEFINED;
65 };
66 
67 }  // namespace vulkan
68 }  // namespace amber
69 
70 #endif  // SRC_VULKAN_TRANSFER_BUFFER_H_
71