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