1 // Copyright 2019 The ANGLE Project Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // MemoryObjectVk.cpp: Defines the class interface for MemoryObjectVk, implementing
6 // MemoryObjectImpl.
7
8 #include "libANGLE/renderer/vulkan/MemoryObjectVk.h"
9
10 #include "common/debug.h"
11 #include "common/vulkan/vk_headers.h"
12 #include "libANGLE/Context.h"
13 #include "libANGLE/renderer/vulkan/ContextVk.h"
14 #include "libANGLE/renderer/vulkan/RendererVk.h"
15 #include "vulkan/vulkan_fuchsia_ext.h"
16
17 #if !defined(ANGLE_PLATFORM_WINDOWS)
18 # include <unistd.h>
19 #else
20 # include <io.h>
21 #endif
22
23 #if defined(ANGLE_PLATFORM_FUCHSIA)
24 # include <zircon/status.h>
25 # include <zircon/syscalls.h>
26 #endif
27
28 namespace rx
29 {
30
31 namespace
32 {
33
34 #if defined(ANGLE_PLATFORM_WINDOWS)
close(int fd)35 int close(int fd)
36 {
37 return _close(fd);
38 }
39 #endif
40
CloseZirconVmo(zx_handle_t handle)41 void CloseZirconVmo(zx_handle_t handle)
42 {
43 #if defined(ANGLE_PLATFORM_FUCHSIA)
44 zx_handle_close(handle);
45 #else
46 UNREACHABLE();
47 #endif
48 }
49
DuplicateZirconVmo(ContextVk * contextVk,zx_handle_t handle,zx_handle_t * duplicate)50 angle::Result DuplicateZirconVmo(ContextVk *contextVk, zx_handle_t handle, zx_handle_t *duplicate)
51 {
52 #if defined(ANGLE_PLATFORM_FUCHSIA)
53 zx_status_t status = zx_handle_duplicate(handle, ZX_RIGHT_SAME_RIGHTS, duplicate);
54 ANGLE_VK_CHECK(contextVk, status == ZX_OK, VK_ERROR_INVALID_EXTERNAL_HANDLE);
55 return angle::Result::Continue;
56 #else
57 UNREACHABLE();
58 return angle::Result::Stop;
59 #endif
60 }
61
ToVulkanHandleType(gl::HandleType handleType)62 VkExternalMemoryHandleTypeFlagBits ToVulkanHandleType(gl::HandleType handleType)
63 {
64 switch (handleType)
65 {
66 case gl::HandleType::OpaqueFd:
67 return VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT;
68 case gl::HandleType::ZirconVmo:
69 return VK_EXTERNAL_MEMORY_HANDLE_TYPE_ZIRCON_VMO_BIT_FUCHSIA;
70 default:
71 // Not a memory handle type.
72 UNREACHABLE();
73 return VK_EXTERNAL_MEMORY_HANDLE_TYPE_FLAG_BITS_MAX_ENUM;
74 }
75 }
76
77 } // namespace
78
MemoryObjectVk()79 MemoryObjectVk::MemoryObjectVk() {}
80
81 MemoryObjectVk::~MemoryObjectVk() = default;
82
onDestroy(const gl::Context * context)83 void MemoryObjectVk::onDestroy(const gl::Context *context)
84 {
85 if (mFd != kInvalidFd)
86 {
87 close(mFd);
88 mFd = kInvalidFd;
89 }
90
91 if (mZirconHandle != ZX_HANDLE_INVALID)
92 {
93 CloseZirconVmo(mZirconHandle);
94 mZirconHandle = ZX_HANDLE_INVALID;
95 }
96 }
97
setDedicatedMemory(const gl::Context * context,bool dedicatedMemory)98 angle::Result MemoryObjectVk::setDedicatedMemory(const gl::Context *context, bool dedicatedMemory)
99 {
100 mDedicatedMemory = dedicatedMemory;
101 return angle::Result::Continue;
102 }
103
importFd(gl::Context * context,GLuint64 size,gl::HandleType handleType,GLint fd)104 angle::Result MemoryObjectVk::importFd(gl::Context *context,
105 GLuint64 size,
106 gl::HandleType handleType,
107 GLint fd)
108 {
109 ContextVk *contextVk = vk::GetImpl(context);
110
111 switch (handleType)
112 {
113 case gl::HandleType::OpaqueFd:
114 return importOpaqueFd(contextVk, size, fd);
115
116 default:
117 UNREACHABLE();
118 return angle::Result::Stop;
119 }
120 }
121
importZirconHandle(gl::Context * context,GLuint64 size,gl::HandleType handleType,GLuint handle)122 angle::Result MemoryObjectVk::importZirconHandle(gl::Context *context,
123 GLuint64 size,
124 gl::HandleType handleType,
125 GLuint handle)
126 {
127 ContextVk *contextVk = vk::GetImpl(context);
128
129 switch (handleType)
130 {
131 case gl::HandleType::ZirconVmo:
132 return importZirconVmo(contextVk, size, handle);
133
134 default:
135 UNREACHABLE();
136 return angle::Result::Stop;
137 }
138 }
139
importOpaqueFd(ContextVk * contextVk,GLuint64 size,GLint fd)140 angle::Result MemoryObjectVk::importOpaqueFd(ContextVk *contextVk, GLuint64 size, GLint fd)
141 {
142 ASSERT(mHandleType == gl::HandleType::InvalidEnum);
143 ASSERT(mFd == kInvalidFd);
144 ASSERT(fd != kInvalidFd);
145 mHandleType = gl::HandleType::OpaqueFd;
146 mFd = fd;
147 mSize = size;
148 return angle::Result::Continue;
149 }
150
importZirconVmo(ContextVk * contextVk,GLuint64 size,GLuint handle)151 angle::Result MemoryObjectVk::importZirconVmo(ContextVk *contextVk, GLuint64 size, GLuint handle)
152 {
153 ASSERT(mHandleType == gl::HandleType::InvalidEnum);
154 ASSERT(mZirconHandle == ZX_HANDLE_INVALID);
155 ASSERT(handle != ZX_HANDLE_INVALID);
156 mHandleType = gl::HandleType::ZirconVmo;
157 mZirconHandle = handle;
158 mSize = size;
159 return angle::Result::Continue;
160 }
161
createImage(ContextVk * contextVk,gl::TextureType type,size_t levels,GLenum internalFormat,const gl::Extents & size,GLuint64 offset,vk::ImageHelper * image,GLbitfield createFlags,GLbitfield usageFlags)162 angle::Result MemoryObjectVk::createImage(ContextVk *contextVk,
163 gl::TextureType type,
164 size_t levels,
165 GLenum internalFormat,
166 const gl::Extents &size,
167 GLuint64 offset,
168 vk::ImageHelper *image,
169 GLbitfield createFlags,
170 GLbitfield usageFlags)
171 {
172 RendererVk *renderer = contextVk->getRenderer();
173
174 const vk::Format &vkFormat = renderer->getFormat(internalFormat);
175
176 // EXT_external_objects issue 13 says that all supported usage flags must be specified.
177 // However, ANGLE_external_objects_flags allows these flags to be masked. Note that the GL enum
178 // values constituting the bits of |usageFlags| are identical to their corresponding Vulkan
179 // value.
180 const VkImageUsageFlags imageUsageFlags =
181 vk::GetMaximalImageUsageFlags(renderer, vkFormat.actualImageFormatID) & usageFlags;
182
183 VkExternalMemoryImageCreateInfo externalMemoryImageCreateInfo = {};
184 externalMemoryImageCreateInfo.sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO;
185 externalMemoryImageCreateInfo.handleTypes = ToVulkanHandleType(mHandleType);
186
187 VkExtent3D vkExtents;
188 uint32_t layerCount;
189 gl_vk::GetExtentsAndLayerCount(type, size, &vkExtents, &layerCount);
190
191 // Initialize VkImage with initial layout of VK_IMAGE_LAYOUT_UNDEFINED.
192 //
193 // Binding a VkImage with an initial layout of VK_IMAGE_LAYOUT_UNDEFINED to external memory
194 // whose content has already been defined does not make the content undefined (see 11.7.1.
195 // External Resource Sharing).
196 //
197 // If the content is already defined, the ownership rules imply that the first operation on the
198 // texture must be a call to glWaitSemaphoreEXT that grants ownership of the image and informs
199 // us of the true layout.
200 //
201 // If the content is not already defined, the first operation may not be a glWaitSemaphore, but
202 // in this case undefined layout is appropriate.
203 //
204 // ANGLE_external_objects_flags allows create flags to be specified by the application instead
205 // of getting defaulted to zero. Note that the GL enum values constituting the bits of
206 // |createFlags| are identical to their corresponding Vulkan value.
207 ANGLE_TRY(image->initExternal(contextVk, type, vkExtents, vkFormat, 1, imageUsageFlags,
208 createFlags, vk::ImageLayout::Undefined,
209 &externalMemoryImageCreateInfo, gl::LevelIndex(0),
210 static_cast<uint32_t>(levels), layerCount,
211 contextVk->isRobustResourceInitEnabled(), nullptr, false));
212
213 VkMemoryRequirements externalMemoryRequirements;
214 image->getImage().getMemoryRequirements(renderer->getDevice(), &externalMemoryRequirements);
215
216 void *importMemoryInfo = nullptr;
217 VkMemoryDedicatedAllocateInfo memoryDedicatedAllocateInfo = {};
218 if (mDedicatedMemory)
219 {
220 memoryDedicatedAllocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR;
221 memoryDedicatedAllocateInfo.image = image->getImage().getHandle();
222 importMemoryInfo = &memoryDedicatedAllocateInfo;
223 }
224
225 VkImportMemoryFdInfoKHR importMemoryFdInfo = {};
226 VkImportMemoryZirconHandleInfoFUCHSIA importMemoryZirconHandleInfo = {};
227 switch (mHandleType)
228 {
229 case gl::HandleType::OpaqueFd:
230 ASSERT(mFd != kInvalidFd);
231 importMemoryFdInfo.sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR;
232 importMemoryFdInfo.pNext = importMemoryInfo;
233 importMemoryFdInfo.handleType = ToVulkanHandleType(mHandleType);
234 importMemoryFdInfo.fd = dup(mFd);
235 importMemoryInfo = &importMemoryFdInfo;
236 break;
237 case gl::HandleType::ZirconVmo:
238 ASSERT(mZirconHandle != ZX_HANDLE_INVALID);
239 importMemoryZirconHandleInfo.sType =
240 VK_STRUCTURE_TYPE_IMPORT_MEMORY_ZIRCON_HANDLE_INFO_FUCHSIA;
241 importMemoryZirconHandleInfo.pNext = importMemoryInfo;
242 importMemoryZirconHandleInfo.handleType = ToVulkanHandleType(mHandleType);
243 ANGLE_TRY(
244 DuplicateZirconVmo(contextVk, mZirconHandle, &importMemoryZirconHandleInfo.handle));
245 importMemoryInfo = &importMemoryZirconHandleInfo;
246 break;
247 default:
248 UNREACHABLE();
249 }
250
251 // TODO(jmadill, spang): Memory sub-allocation. http://anglebug.com/2162
252 ASSERT(offset == 0);
253 ASSERT(externalMemoryRequirements.size == mSize);
254
255 VkMemoryPropertyFlags flags = 0;
256 ANGLE_TRY(image->initExternalMemory(contextVk, renderer->getMemoryProperties(),
257 externalMemoryRequirements, nullptr, importMemoryInfo,
258 renderer->getQueueFamilyIndex(), flags));
259
260 return angle::Result::Continue;
261 }
262
263 } // namespace rx
264