blob: 7243f8b3797dc21c52120532210639281e33ce61 [file] [log] [blame]
dan sinclair70d430c2018-09-28 16:12:38 -04001// 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
20#include "amber/result.h"
21#include "vulkan/vulkan.h"
22
23namespace amber {
24namespace vulkan {
25
26class Resource {
27 public:
28 virtual ~Resource();
29
30 virtual VkDeviceMemory GetHostAccessMemory() const {
31 return host_accessible_memory_;
32 }
33
Jaebaek Seoc244ec82018-12-01 23:47:57 -050034 virtual Result CopyToHost(VkCommandBuffer command) = 0;
35
dan sinclair70d430c2018-09-28 16:12:38 -040036 virtual void Shutdown();
37
38 void* HostAccessibleMemoryPtr() const { return memory_ptr_; }
39
Jaebaek Seoc244ec82018-12-01 23:47:57 -050040 size_t GetSizeInBytes() const { return size_in_bytes_; }
41
dan sinclair70d430c2018-09-28 16:12:38 -040042 protected:
43 Resource(VkDevice device,
44 size_t size,
45 const VkPhysicalDeviceMemoryProperties& properties);
46 Result Initialize();
47 Result CreateVkBuffer(VkBuffer* buffer, VkBufferUsageFlags usage);
48
49 VkDevice GetDevice() const { return device_; }
50 VkBuffer GetHostAccessibleBuffer() const { return host_accessible_buffer_; }
51
dan sinclair70d430c2018-09-28 16:12:38 -040052 struct AllocateResult {
53 Result r;
54 uint32_t memory_type_index;
55 };
56
57 AllocateResult AllocateAndBindMemoryToVkBuffer(VkBuffer buffer,
58 VkDeviceMemory* memory,
59 VkMemoryPropertyFlags flags,
60 bool force_flags);
61 AllocateResult AllocateAndBindMemoryToVkImage(VkImage image,
62 VkDeviceMemory* memory,
63 VkMemoryPropertyFlags flags,
64 bool force_flags);
65
Jaebaek Seoc244ec82018-12-01 23:47:57 -050066 bool IsMemoryHostAccessible(uint32_t memory_type_index) {
dan sinclair70d430c2018-09-28 16:12:38 -040067 return (physical_memory_properties_.memoryTypes[memory_type_index]
68 .propertyFlags &
69 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) ==
70 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
71 }
72
Jaebaek Seoc244ec82018-12-01 23:47:57 -050073 bool IsMemoryHostCoherent(uint32_t memory_type_index) {
74 return (physical_memory_properties_.memoryTypes[memory_type_index]
75 .propertyFlags &
76 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) ==
77 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
78 }
79
dan sinclair70d430c2018-09-28 16:12:38 -040080 Result MapMemory(VkDeviceMemory memory);
81 void UnMapMemory(VkDeviceMemory memory);
82
Jaebaek Seo540f5372018-11-15 11:10:03 -050083 // Make all memory operations before calling this method effective i.e.,
84 // prevent hazards caused by out-of-order execution.
85 void MemoryBarrier(VkCommandBuffer command);
86
dan sinclair70d430c2018-09-28 16:12:38 -040087 private:
88 uint32_t ChooseMemory(uint32_t memory_type_bits,
89 VkMemoryPropertyFlags flags,
90 bool force_flags);
91 Result AllocateMemory(VkDeviceMemory* memory,
92 VkDeviceSize size,
93 uint32_t memory_type_index);
94
95 Result BindMemoryToVkBuffer(VkBuffer buffer, VkDeviceMemory memory);
96 const VkMemoryRequirements GetVkBufferMemoryRequirements(
97 VkBuffer buffer) const;
98
99 Result BindMemoryToVkImage(VkImage image, VkDeviceMemory memory);
100 const VkMemoryRequirements GetVkImageMemoryRequirements(VkImage image) const;
101
102 VkDevice device_ = VK_NULL_HANDLE;
Jaebaek Seoc244ec82018-12-01 23:47:57 -0500103 size_t size_in_bytes_ = 0;
dan sinclair70d430c2018-09-28 16:12:38 -0400104 VkPhysicalDeviceMemoryProperties physical_memory_properties_;
105
106 VkBuffer host_accessible_buffer_ = VK_NULL_HANDLE;
107 VkDeviceMemory host_accessible_memory_ = VK_NULL_HANDLE;
108 void* memory_ptr_ = nullptr;
109};
110
111} // namespace vulkan
112} // namespace amber
113
114#endif // SRC_VULKAN_RESOURCE_H_