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