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_RESOURCE_H_ 17 #define SRC_VULKAN_RESOURCE_H_ 18 19 #include <map> 20 #include <memory> 21 #include <vector> 22 23 #include "amber/result.h" 24 #include "amber/value.h" 25 #include "amber/vulkan_header.h" 26 27 namespace amber { 28 29 class BLAS; 30 class TLAS; 31 class SBT; 32 33 namespace vulkan { 34 35 class CommandBuffer; 36 class Device; 37 class TransferBuffer; 38 class TransferImage; 39 class BLAS; 40 class TLAS; 41 class SBT; 42 43 typedef std::map<amber::BLAS*, std::unique_ptr<amber::vulkan::BLAS>> BlasesMap; 44 typedef std::map<amber::TLAS*, std::unique_ptr<amber::vulkan::TLAS>> TlasesMap; 45 typedef std::map<amber::SBT*, amber::vulkan::SBT*> SbtsMap; 46 47 // Class for Vulkan resources. Its children are Vulkan Buffer and Vulkan Image. 48 class Resource { 49 public: 50 virtual ~Resource(); 51 52 /// Records a command on |command_buffer| to copy the buffer contents from the 53 /// host to the device. 54 virtual void CopyToDevice(CommandBuffer* command_buffer) = 0; 55 /// Records a command on |command_buffer| to copy the buffer contents from the 56 /// device to the host. 57 virtual void CopyToHost(CommandBuffer* command_buffer) = 0; 58 HostAccessibleMemoryPtr()59 void* HostAccessibleMemoryPtr() const { return memory_ptr_; } 60 GetSizeInBytes()61 uint32_t GetSizeInBytes() const { return size_in_bytes_; } 62 void UpdateMemoryWithRawData(const std::vector<uint8_t>& raw_data); 63 IsReadOnly()64 bool IsReadOnly() const { return is_read_only_; } SetReadOnly(bool read_only)65 void SetReadOnly(bool read_only) { is_read_only_ = read_only; } 66 virtual Result Initialize() = 0; AsTransferBuffer()67 virtual TransferBuffer* AsTransferBuffer() { return nullptr; } AsTransferImage()68 virtual TransferImage* AsTransferImage() { return nullptr; } AddAllocateFlags(VkMemoryAllocateFlags memory_allocate_flags)69 virtual void AddAllocateFlags(VkMemoryAllocateFlags memory_allocate_flags) { 70 memory_allocate_flags_ |= memory_allocate_flags; 71 } GetMemoryPropertiesFlags()72 VkMemoryPropertyFlags GetMemoryPropertiesFlags() { 73 return memory_properties_flags_; 74 } SetMemoryPropertiesFlags(VkMemoryPropertyFlags flags)75 void SetMemoryPropertiesFlags(VkMemoryPropertyFlags flags) { 76 memory_properties_flags_ = flags; 77 } 78 79 protected: 80 Resource(Device* device, uint32_t size); 81 Result CreateVkBuffer(VkBuffer* buffer, VkBufferUsageFlags usage); 82 83 Result AllocateAndBindMemoryToVkBuffer(VkBuffer buffer, 84 VkDeviceMemory* memory, 85 VkMemoryPropertyFlags flags, 86 bool force_flags, 87 uint32_t* memory_type_index); 88 89 Result MapMemory(VkDeviceMemory memory); 90 void UnMapMemory(VkDeviceMemory memory); SetMemoryPtr(void * ptr)91 void SetMemoryPtr(void* ptr) { memory_ptr_ = ptr; } 92 93 /// Records a memory barrier on |command_buffer|, to ensure prior writes to 94 /// this buffer have completed and are available to subsequent commands. 95 void MemoryBarrier(CommandBuffer* command_buffer); 96 97 /// Returns a memory index for the given Vulkan device, for a memory type 98 /// which has the given |flags| set. If no memory is found with the given 99 /// |flags| set then the first non-zero memory index is returned. If 100 /// |require_flags_found| is true then if no memory is found with the given 101 /// |flags| then the maximum uint32_t value is returned instead of the 102 /// first non-zero memory index. 103 uint32_t ChooseMemory(uint32_t memory_type_bits, 104 VkMemoryPropertyFlags flags, 105 bool require_flags_found); 106 Result AllocateMemory(VkDeviceMemory* memory, 107 VkDeviceSize size, 108 uint32_t memory_type_index); 109 110 Device* device_ = nullptr; 111 112 private: 113 uint32_t size_in_bytes_ = 0; 114 void* memory_ptr_ = nullptr; 115 bool is_read_only_ = false; 116 VkMemoryAllocateFlags memory_allocate_flags_ = 0u; 117 VkMemoryPropertyFlags memory_properties_flags_ = 118 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | 119 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; 120 }; 121 122 } // namespace vulkan 123 } // namespace amber 124 125 #endif // SRC_VULKAN_RESOURCE_H_ 126