• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Amber Authors.
2 // Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SRC_VULKAN_TRANSFER_BUFFER_H_
17 #define SRC_VULKAN_TRANSFER_BUFFER_H_
18 
19 #include <vector>
20 
21 #include "amber/result.h"
22 #include "amber/vulkan_header.h"
23 #include "src/format.h"
24 #include "src/vulkan/resource.h"
25 
26 namespace amber {
27 namespace vulkan {
28 
29 class CommandBuffer;
30 class Device;
31 
32 /// Wrapper around a Vulkan VkBuffer object.
33 class TransferBuffer : public Resource {
34  public:
35   TransferBuffer(Device* device, uint32_t size_in_bytes, Format* format);
36   ~TransferBuffer() override;
37 
AsTransferBuffer()38   TransferBuffer* AsTransferBuffer() override { return this; }
AddUsageFlags(VkBufferUsageFlags flags)39   Result AddUsageFlags(VkBufferUsageFlags flags) {
40     if (buffer_ != VK_NULL_HANDLE) {
41       return Result(
42           "Vulkan: TransferBuffer::AddUsageFlags Usage flags can't be changed "
43           "after initializing the buffer.");
44     }
45     usage_flags_ |= flags;
46     return {};
47   }
48   Result Initialize() override;
GetVkBufferView()49   const VkBufferView* GetVkBufferView() const { return &view_; }
50 
GetVkBuffer()51   VkBuffer GetVkBuffer() const { return buffer_; }
52   VkDeviceAddress getBufferDeviceAddress();
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   VkBufferUsageFlags usage_flags_ = 0;
63   VkBuffer buffer_ = VK_NULL_HANDLE;
64   VkDeviceMemory memory_ = VK_NULL_HANDLE;
65   VkBufferView view_ = VK_NULL_HANDLE;
66   VkFormat format_ = VK_FORMAT_UNDEFINED;
67 };
68 
69 }  // namespace vulkan
70 }  // namespace amber
71 
72 #endif  // SRC_VULKAN_TRANSFER_BUFFER_H_
73