Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1 | // Copyright 2018 The Android Open Source Project |
| 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 expresso or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | #include "VkCommonOperations.h" |
| 15 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 16 | #include <GLES2/gl2.h> |
| 17 | #include <GLES2/gl2ext.h> |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 18 | #include <GLES3/gl3.h> |
Kaiyi Li | eac86a7 | 2021-02-04 08:09:41 -0800 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <string.h> |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 21 | #include <vulkan/vk_enum_string_helper.h> |
| 22 | |
| 23 | #include <iomanip> |
| 24 | #include <ostream> |
| 25 | #include <sstream> |
Kaiyi Li | eac86a7 | 2021-02-04 08:09:41 -0800 | [diff] [blame] | 26 | #include <unordered_set> |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 27 | |
Jason Macnak | 843b84b | 2023-05-19 14:19:25 -0700 | [diff] [blame] | 28 | #include "VkDecoderGlobalState.h" |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 29 | #include "VkFormatUtils.h" |
Kaiyi Li | eac86a7 | 2021-02-04 08:09:41 -0800 | [diff] [blame] | 30 | #include "VulkanDispatch.h" |
Jason Macnak | 63805b1 | 2023-02-22 18:44:05 +0000 | [diff] [blame] | 31 | #include "aemu/base/Optional.h" |
Jason Macnak | 63805b1 | 2023-02-22 18:44:05 +0000 | [diff] [blame] | 32 | #include "aemu/base/Tracing.h" |
Jason Macnak | 4c45edb | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 33 | #include "aemu/base/containers/Lookup.h" |
| 34 | #include "aemu/base/containers/StaticMap.h" |
| 35 | #include "aemu/base/synchronization/Lock.h" |
| 36 | #include "aemu/base/system/System.h" |
Kaiyi Li | eac86a7 | 2021-02-04 08:09:41 -0800 | [diff] [blame] | 37 | #include "common/goldfish_vk_dispatch.h" |
Doug Horn | 0c2ea5a | 2021-10-29 17:30:16 -0700 | [diff] [blame] | 38 | #include "host-common/GfxstreamFatalError.h" |
Joshua Duong | c3edd0c | 2022-12-13 08:07:55 -0800 | [diff] [blame] | 39 | #include "host-common/emugl_vm_operations.h" |
Kaiyi Li | eac86a7 | 2021-02-04 08:09:41 -0800 | [diff] [blame] | 40 | #include "host-common/vm_operations.h" |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 41 | |
| 42 | #ifdef _WIN32 |
| 43 | #include <windows.h> |
| 44 | #else |
| 45 | #include <fcntl.h> |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 46 | #include <unistd.h> |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 47 | #endif |
| 48 | |
| 49 | #ifdef __APPLE__ |
| 50 | #include <CoreFoundation/CoreFoundation.h> |
| 51 | #endif |
| 52 | |
Jason Macnak | ed0c9e6 | 2023-03-30 15:58:24 -0700 | [diff] [blame] | 53 | namespace gfxstream { |
| 54 | namespace vk { |
| 55 | namespace { |
| 56 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 57 | #define VK_COMMON_ERROR(fmt, ...) \ |
| 58 | fprintf(stderr, "%s:%d " fmt "\n", __func__, __LINE__, ##__VA_ARGS__); |
| 59 | #define VK_COMMON_LOG(fmt, ...) \ |
| 60 | fprintf(stdout, "%s:%d " fmt "\n", __func__, __LINE__, ##__VA_ARGS__); |
| 61 | #define VK_COMMON_VERBOSE(fmt, ...) \ |
| 62 | if (android::base::isVerboseLogging()) \ |
| 63 | fprintf(stderr, "%s:%d " fmt "\n", __func__, __LINE__, ##__VA_ARGS__); |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 64 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 65 | using android::base::AutoLock; |
Jason Macnak | ed0c9e6 | 2023-03-30 15:58:24 -0700 | [diff] [blame] | 66 | using android::base::kNullopt; |
Kaiyi Li | 07583e2 | 2022-06-23 18:11:18 -0700 | [diff] [blame] | 67 | using android::base::ManagedDescriptor; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 68 | using android::base::Optional; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 69 | using android::base::StaticLock; |
| 70 | using android::base::StaticMap; |
Kaiyi Li | 4935d31 | 2022-01-12 16:57:24 -0800 | [diff] [blame] | 71 | using emugl::ABORT_REASON_OTHER; |
| 72 | using emugl::FatalError; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 73 | |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 74 | constexpr size_t kPageBits = 12; |
| 75 | constexpr size_t kPageSize = 1u << kPageBits; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 76 | |
Greg Schlomoff | bf53b10 | 2023-04-14 12:08:15 -0700 | [diff] [blame] | 77 | static int kMaxDebugMarkerAnnotations = 10; |
| 78 | |
| 79 | static std::optional<std::string> sMemoryLogPath = std::nullopt; |
| 80 | |
| 81 | const char* string_AstcEmulationMode(AstcEmulationMode mode) { |
| 82 | switch (mode) { |
| 83 | case AstcEmulationMode::Disabled: |
| 84 | return "Disabled"; |
| 85 | case AstcEmulationMode::Cpu: |
| 86 | return "Cpu"; |
| 87 | case AstcEmulationMode::Gpu: |
| 88 | return "Gpu"; |
| 89 | } |
| 90 | return "Unknown"; |
| 91 | } |
| 92 | |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 93 | } // namespace |
| 94 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 95 | static StaticMap<VkDevice, uint32_t> sKnownStagingTypeIndices; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 96 | |
| 97 | static android::base::StaticLock sVkEmulationLock; |
| 98 | |
Igor Chernyshev | ed39a8d | 2023-12-21 11:03:24 -0800 | [diff] [blame] | 99 | static bool updateColorBufferFromBytesLocked(uint32_t colorBufferHandle, uint32_t x, uint32_t y, |
| 100 | uint32_t w, uint32_t h, const void* pixels, |
| 101 | size_t inputPixelsSize); |
| 102 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 103 | #if !defined(__QNX__) |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 104 | VK_EXT_MEMORY_HANDLE dupExternalMemory(VK_EXT_MEMORY_HANDLE h) { |
| 105 | #ifdef _WIN32 |
| 106 | auto myProcessHandle = GetCurrentProcess(); |
| 107 | VK_EXT_MEMORY_HANDLE res; |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 108 | DuplicateHandle(myProcessHandle, h, // source process and handle |
| 109 | myProcessHandle, &res, // target process and pointer to handle |
| 110 | 0 /* desired access (ignored) */, true /* inherit */, |
| 111 | DUPLICATE_SAME_ACCESS /* same access option */); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 112 | return res; |
| 113 | #else |
| 114 | return dup(h); |
| 115 | #endif |
| 116 | } |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 117 | #endif |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 118 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 119 | bool getStagingMemoryTypeIndex(VulkanDispatch* vk, VkDevice device, |
| 120 | const VkPhysicalDeviceMemoryProperties* memProps, |
| 121 | uint32_t* typeIndex) { |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 122 | auto res = sKnownStagingTypeIndices.get(device); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 123 | |
| 124 | if (res) { |
| 125 | *typeIndex = *res; |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | VkBufferCreateInfo testCreateInfo = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 130 | VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, |
| 131 | 0, |
| 132 | 0, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 133 | 4096, |
| 134 | // To be a staging buffer, it must support being |
| 135 | // both a transfer src and dst. |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 136 | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 137 | // TODO: See if buffers over shared queues need to be |
| 138 | // considered separately |
| 139 | VK_SHARING_MODE_EXCLUSIVE, |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 140 | 0, |
| 141 | nullptr, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | VkBuffer testBuffer; |
| 145 | VkResult testBufferCreateRes = |
| 146 | vk->vkCreateBuffer(device, &testCreateInfo, nullptr, &testBuffer); |
| 147 | |
| 148 | if (testBufferCreateRes != VK_SUCCESS) { |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 149 | VK_COMMON_ERROR( |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 150 | "Could not create test buffer " |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 151 | "for staging buffer query. VkResult: 0x%llx", |
Lingfeng Yang | 1e4f28a | 2020-11-09 14:56:56 -0800 | [diff] [blame] | 152 | (unsigned long long)testBufferCreateRes); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 153 | return false; |
| 154 | } |
| 155 | |
| 156 | VkMemoryRequirements memReqs; |
| 157 | vk->vkGetBufferMemoryRequirements(device, testBuffer, &memReqs); |
| 158 | |
| 159 | // To be a staging buffer, we need to allow CPU read/write access. |
| 160 | // Thus, we need the memory type index both to be host visible |
| 161 | // and to be supported in the memory requirements of the buffer. |
| 162 | bool foundSuitableStagingMemoryType = false; |
| 163 | uint32_t stagingMemoryTypeIndex = 0; |
| 164 | |
Idan Raiter | 8fd0a0b | 2022-12-05 07:11:45 -0800 | [diff] [blame] | 165 | for (uint32_t i = 0; i < memProps->memoryTypeCount; ++i) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 166 | const auto& typeInfo = memProps->memoryTypes[i]; |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 167 | bool hostVisible = typeInfo.propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; |
| 168 | bool hostCached = typeInfo.propertyFlags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 169 | bool allowedInBuffer = (1 << i) & memReqs.memoryTypeBits; |
| 170 | if (hostVisible && hostCached && allowedInBuffer) { |
| 171 | foundSuitableStagingMemoryType = true; |
| 172 | stagingMemoryTypeIndex = i; |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
Idan Raiter | 8fd0a0b | 2022-12-05 07:11:45 -0800 | [diff] [blame] | 177 | // If the previous loop failed, try to accept a type that is not HOST_CACHED. |
| 178 | if (!foundSuitableStagingMemoryType) { |
| 179 | for (uint32_t i = 0; i < memProps->memoryTypeCount; ++i) { |
| 180 | const auto& typeInfo = memProps->memoryTypes[i]; |
| 181 | bool hostVisible = typeInfo.propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; |
| 182 | bool allowedInBuffer = (1 << i) & memReqs.memoryTypeBits; |
| 183 | if (hostVisible && allowedInBuffer) { |
| 184 | VK_COMMON_ERROR("Warning: using non-cached HOST_VISIBLE type for staging memory"); |
| 185 | foundSuitableStagingMemoryType = true; |
| 186 | stagingMemoryTypeIndex = i; |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 192 | vk->vkDestroyBuffer(device, testBuffer, nullptr); |
| 193 | |
| 194 | if (!foundSuitableStagingMemoryType) { |
| 195 | std::stringstream ss; |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 196 | ss << "Could not find suitable memory type index " |
| 197 | << "for staging buffer. Memory type bits: " << std::hex << memReqs.memoryTypeBits << "\n" |
| 198 | << "Available host visible memory type indices:" |
| 199 | << "\n"; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 200 | for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; ++i) { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 201 | if (memProps->memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 202 | ss << "Host visible memory type index: %u" << i << "\n"; |
| 203 | } |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 204 | if (memProps->memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 205 | ss << "Host cached memory type index: %u" << i << "\n"; |
| 206 | } |
| 207 | } |
| 208 | |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 209 | VK_COMMON_ERROR("Error: %s", ss.str().c_str()); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 210 | |
| 211 | return false; |
| 212 | } |
| 213 | |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 214 | sKnownStagingTypeIndices.set(device, stagingMemoryTypeIndex); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 215 | *typeIndex = stagingMemoryTypeIndex; |
| 216 | |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | static VkEmulation* sVkEmulation = nullptr; |
| 221 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 222 | static bool extensionsSupported(const std::vector<VkExtensionProperties>& currentProps, |
| 223 | const std::vector<const char*>& wantedExtNames) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 224 | std::vector<bool> foundExts(wantedExtNames.size(), false); |
| 225 | |
| 226 | for (uint32_t i = 0; i < currentProps.size(); ++i) { |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 227 | VK_COMMON_VERBOSE("has extension: %s", currentProps[i].extensionName); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 228 | for (size_t j = 0; j < wantedExtNames.size(); ++j) { |
| 229 | if (!strcmp(wantedExtNames[j], currentProps[i].extensionName)) { |
| 230 | foundExts[j] = true; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | for (size_t i = 0; i < wantedExtNames.size(); ++i) { |
| 236 | bool found = foundExts[i]; |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 237 | // LOG(VERBOSE) << "needed extension: " << wantedExtNames[i] |
| 238 | // << " found: " << found; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 239 | if (!found) { |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 240 | // LOG(VERBOSE) << wantedExtNames[i] << " not found, bailing."; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | // For a given ImageSupportInfo, populates usageWithExternalHandles and |
| 249 | // requiresDedicatedAllocation. memoryTypeBits are populated later once the |
| 250 | // device is created, beacuse that needs a test image to be created. |
| 251 | // If we don't support external memory, it's assumed dedicated allocations are |
| 252 | // not needed. |
| 253 | // Precondition: sVkEmulation instance has been created and ext memory caps known. |
| 254 | // Returns false if the query failed. |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 255 | static bool getImageFormatExternalMemorySupportInfo(VulkanDispatch* vk, VkPhysicalDevice physdev, |
| 256 | VkEmulation::ImageSupportInfo* info) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 257 | // Currently there is nothing special we need to do about |
| 258 | // VkFormatProperties2, so just use the normal version |
| 259 | // and put it in the format2 struct. |
| 260 | VkFormatProperties outFormatProps; |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 261 | vk->vkGetPhysicalDeviceFormatProperties(physdev, info->format, &outFormatProps); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 262 | |
| 263 | info->formatProps2 = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 264 | VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2, |
| 265 | 0, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 266 | outFormatProps, |
| 267 | }; |
| 268 | |
| 269 | if (!sVkEmulation->instanceSupportsExternalMemoryCapabilities) { |
| 270 | info->supportsExternalMemory = false; |
| 271 | info->requiresDedicatedAllocation = false; |
| 272 | |
| 273 | VkImageFormatProperties outImageFormatProps; |
| 274 | VkResult res = vk->vkGetPhysicalDeviceImageFormatProperties( |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 275 | physdev, info->format, info->type, info->tiling, info->usageFlags, info->createFlags, |
| 276 | &outImageFormatProps); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 277 | |
| 278 | if (res != VK_SUCCESS) { |
| 279 | if (res == VK_ERROR_FORMAT_NOT_SUPPORTED) { |
| 280 | info->supported = false; |
| 281 | return true; |
| 282 | } else { |
| 283 | fprintf(stderr, |
| 284 | "%s: vkGetPhysicalDeviceImageFormatProperties query " |
| 285 | "failed with %d " |
| 286 | "for format 0x%x type 0x%x usage 0x%x flags 0x%x\n", |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 287 | __func__, res, info->format, info->type, info->usageFlags, |
| 288 | info->createFlags); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 289 | return false; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | info->supported = true; |
| 294 | |
| 295 | info->imageFormatProps2 = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 296 | VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2, |
| 297 | 0, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 298 | outImageFormatProps, |
| 299 | }; |
| 300 | |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 301 | // LOG(VERBOSE) << "Supported (not externally): " |
| 302 | // << string_VkFormat(info->format) << " " |
| 303 | // << string_VkImageType(info->type) << " " |
| 304 | // << string_VkImageTiling(info->tiling) << " " |
| 305 | // << string_VkImageUsageFlagBits( |
| 306 | // (VkImageUsageFlagBits)info->usageFlags); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 307 | |
| 308 | return true; |
| 309 | } |
| 310 | |
| 311 | VkPhysicalDeviceExternalImageFormatInfo extInfo = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 312 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO, |
| 313 | 0, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 314 | VK_EXT_MEMORY_HANDLE_TYPE_BIT, |
| 315 | }; |
| 316 | |
| 317 | VkPhysicalDeviceImageFormatInfo2 formatInfo2 = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 318 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2, |
| 319 | &extInfo, |
| 320 | info->format, |
| 321 | info->type, |
| 322 | info->tiling, |
| 323 | info->usageFlags, |
| 324 | info->createFlags, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 325 | }; |
| 326 | |
| 327 | VkExternalImageFormatProperties outExternalProps = { |
| 328 | VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES, |
| 329 | 0, |
| 330 | { |
| 331 | (VkExternalMemoryFeatureFlags)0, |
| 332 | (VkExternalMemoryHandleTypeFlags)0, |
| 333 | (VkExternalMemoryHandleTypeFlags)0, |
| 334 | }, |
| 335 | }; |
| 336 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 337 | VkImageFormatProperties2 outProps2 = {VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2, |
| 338 | &outExternalProps, |
| 339 | { |
| 340 | {0, 0, 0}, |
| 341 | 0, |
| 342 | 0, |
| 343 | 1, |
| 344 | 0, |
| 345 | }}; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 346 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 347 | VkResult res = sVkEmulation->getImageFormatProperties2Func(physdev, &formatInfo2, &outProps2); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 348 | |
| 349 | if (res != VK_SUCCESS) { |
| 350 | if (res == VK_ERROR_FORMAT_NOT_SUPPORTED) { |
| 351 | info->supported = false; |
| 352 | return true; |
| 353 | } else { |
| 354 | fprintf(stderr, |
| 355 | "%s: vkGetPhysicalDeviceImageFormatProperties2KHR query " |
| 356 | "failed " |
| 357 | "for format 0x%x type 0x%x usage 0x%x flags 0x%x\n", |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 358 | __func__, info->format, info->type, info->usageFlags, info->createFlags); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 359 | return false; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | info->supported = true; |
| 364 | |
| 365 | VkExternalMemoryFeatureFlags featureFlags = |
| 366 | outExternalProps.externalMemoryProperties.externalMemoryFeatures; |
| 367 | |
| 368 | VkExternalMemoryHandleTypeFlags exportImportedFlags = |
| 369 | outExternalProps.externalMemoryProperties.exportFromImportedHandleTypes; |
| 370 | |
| 371 | // Don't really care about export form imported handle types yet |
| 372 | (void)exportImportedFlags; |
| 373 | |
| 374 | VkExternalMemoryHandleTypeFlags compatibleHandleTypes = |
| 375 | outExternalProps.externalMemoryProperties.compatibleHandleTypes; |
| 376 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 377 | info->supportsExternalMemory = (VK_EXT_MEMORY_HANDLE_TYPE_BIT & compatibleHandleTypes) && |
| 378 | (VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT & featureFlags) && |
| 379 | (VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT & featureFlags); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 380 | |
| 381 | info->requiresDedicatedAllocation = |
| 382 | (VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT & featureFlags); |
| 383 | |
| 384 | info->imageFormatProps2 = outProps2; |
| 385 | info->extFormatProps = outExternalProps; |
| 386 | info->imageFormatProps2.pNext = &info->extFormatProps; |
| 387 | |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 388 | // LOG(VERBOSE) << "Supported: " |
| 389 | // << string_VkFormat(info->format) << " " |
| 390 | // << string_VkImageType(info->type) << " " |
| 391 | // << string_VkImageTiling(info->tiling) << " " |
| 392 | // << string_VkImageUsageFlagBits( |
| 393 | // (VkImageUsageFlagBits)info->usageFlags) |
| 394 | // << " " |
| 395 | // << "supportsExternalMemory? " << info->supportsExternalMemory |
| 396 | // << " " |
| 397 | // << "requiresDedicated? " << info->requiresDedicatedAllocation; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 398 | |
| 399 | return true; |
| 400 | } |
| 401 | |
Michael Yu | a269bfe | 2022-03-21 17:50:13 -0700 | [diff] [blame] | 402 | // Vulkan driverVersions are bit-shift packs of their dotted versions |
| 403 | // For example, nvidia driverversion 1934229504 unpacks to 461.40 |
| 404 | // note: while this is equivalent to VkPhysicalDeviceDriverProperties.driverInfo on NVIDIA, |
| 405 | // on intel that value is simply "Intel driver". |
| 406 | static std::string decodeDriverVersion(uint32_t vendorId, uint32_t driverVersion) { |
| 407 | std::stringstream result; |
| 408 | switch (vendorId) { |
| 409 | case 0x10DE: { |
| 410 | // Nvidia. E.g. driverVersion = 1934229504(0x734a0000) maps to 461.40 |
| 411 | uint32_t major = driverVersion >> 22; |
| 412 | uint32_t minor = (driverVersion >> 14) & 0xff; |
| 413 | uint32_t build = (driverVersion >> 6) & 0xff; |
| 414 | uint32_t revision = driverVersion & 0x3f; |
| 415 | result << major << '.' << minor << '.' << build << '.' << revision; |
| 416 | break; |
| 417 | } |
| 418 | case 0x8086: { |
| 419 | // Intel. E.g. driverVersion = 1647866(0x1924fa) maps to 100.9466 (27.20.100.9466) |
| 420 | uint32_t high = driverVersion >> 14; |
| 421 | uint32_t low = driverVersion & 0x3fff; |
| 422 | result << high << '.' << low; |
| 423 | break; |
| 424 | } |
| 425 | case 0x002: // amd |
| 426 | default: { |
| 427 | uint32_t major = VK_VERSION_MAJOR(driverVersion); |
| 428 | uint32_t minor = VK_VERSION_MINOR(driverVersion); |
| 429 | uint32_t patch = VK_VERSION_PATCH(driverVersion); |
| 430 | result << major << "." << minor << "." << patch; |
| 431 | break; |
| 432 | } |
| 433 | } |
| 434 | return result.str(); |
| 435 | } |
| 436 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 437 | static std::vector<VkEmulation::ImageSupportInfo> getBasicImageSupportList() { |
Kaiyi Li | 71874cc | 2022-08-22 08:40:46 -0700 | [diff] [blame] | 438 | struct ImageFeatureCombo { |
| 439 | VkFormat format; |
| 440 | VkImageCreateFlags createFlags = 0; |
| 441 | }; |
| 442 | // Set the mutable flag for RGB UNORM formats so that the created image can also be sampled in |
| 443 | // the sRGB Colorspace. See |
| 444 | // https://chromium-review.googlesource.com/c/chromiumos/platform/minigbm/+/3827672/comments/77db9cb3_60663a6a |
| 445 | // for details. |
| 446 | std::vector<ImageFeatureCombo> combos = { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 447 | // Cover all the gralloc formats |
Kaiyi Li | 71874cc | 2022-08-22 08:40:46 -0700 | [diff] [blame] | 448 | {VK_FORMAT_R8G8B8A8_UNORM, |
| 449 | VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT | VK_IMAGE_CREATE_EXTENDED_USAGE_BIT}, |
| 450 | {VK_FORMAT_R8G8B8_UNORM, |
| 451 | VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT | VK_IMAGE_CREATE_EXTENDED_USAGE_BIT}, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 452 | |
Kaiyi Li | 71874cc | 2022-08-22 08:40:46 -0700 | [diff] [blame] | 453 | {VK_FORMAT_R5G6B5_UNORM_PACK16}, |
Kaiyi Li | 2459623 | 2022-08-11 15:17:43 -0700 | [diff] [blame] | 454 | |
Kaiyi Li | 71874cc | 2022-08-22 08:40:46 -0700 | [diff] [blame] | 455 | {VK_FORMAT_R16G16B16A16_SFLOAT}, |
| 456 | {VK_FORMAT_R16G16B16_SFLOAT}, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 457 | |
Kaiyi Li | 71874cc | 2022-08-22 08:40:46 -0700 | [diff] [blame] | 458 | {VK_FORMAT_B8G8R8A8_UNORM, |
| 459 | VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT | VK_IMAGE_CREATE_EXTENDED_USAGE_BIT}, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 460 | |
Kaiyi Li | 71874cc | 2022-08-22 08:40:46 -0700 | [diff] [blame] | 461 | {VK_FORMAT_R8_UNORM, |
| 462 | VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT | VK_IMAGE_CREATE_EXTENDED_USAGE_BIT}, |
Jason Macnak | 98d95fa | 2023-01-09 11:13:45 -0800 | [diff] [blame] | 463 | {VK_FORMAT_R16_UNORM, |
| 464 | VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT | VK_IMAGE_CREATE_EXTENDED_USAGE_BIT}, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 465 | |
Kaiyi Li | 71874cc | 2022-08-22 08:40:46 -0700 | [diff] [blame] | 466 | {VK_FORMAT_A2R10G10B10_UINT_PACK32}, |
| 467 | {VK_FORMAT_A2R10G10B10_UNORM_PACK32}, |
| 468 | {VK_FORMAT_A2B10G10R10_UNORM_PACK32}, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 469 | |
| 470 | // Compressed texture formats |
Kaiyi Li | 71874cc | 2022-08-22 08:40:46 -0700 | [diff] [blame] | 471 | {VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK}, |
| 472 | {VK_FORMAT_ASTC_4x4_UNORM_BLOCK}, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 473 | |
| 474 | // TODO: YUV formats used in Android |
| 475 | // Fails on Mac |
Kaiyi Li | 71874cc | 2022-08-22 08:40:46 -0700 | [diff] [blame] | 476 | {VK_FORMAT_G8_B8R8_2PLANE_420_UNORM}, |
| 477 | {VK_FORMAT_G8_B8R8_2PLANE_422_UNORM}, |
| 478 | {VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM}, |
| 479 | {VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM}, |
| 480 | {VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16}, |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 481 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 482 | }; |
| 483 | |
| 484 | std::vector<VkImageType> types = { |
| 485 | VK_IMAGE_TYPE_2D, |
| 486 | }; |
| 487 | |
| 488 | std::vector<VkImageTiling> tilings = { |
| 489 | VK_IMAGE_TILING_LINEAR, |
| 490 | VK_IMAGE_TILING_OPTIMAL, |
| 491 | }; |
| 492 | |
| 493 | std::vector<VkImageUsageFlags> usageFlags = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 494 | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT, |
| 495 | VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 496 | VK_IMAGE_USAGE_TRANSFER_DST_BIT, |
| 497 | }; |
| 498 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 499 | std::vector<VkEmulation::ImageSupportInfo> res; |
| 500 | |
Kaiyi Li | 71874cc | 2022-08-22 08:40:46 -0700 | [diff] [blame] | 501 | // Currently: 17 format + create flags combo, 2 tilings, 5 usage flags -> 170 cases to check. |
| 502 | for (auto combo : combos) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 503 | for (auto t : types) { |
| 504 | for (auto ti : tilings) { |
| 505 | for (auto u : usageFlags) { |
Kaiyi Li | 71874cc | 2022-08-22 08:40:46 -0700 | [diff] [blame] | 506 | VkEmulation::ImageSupportInfo info; |
| 507 | info.format = combo.format; |
| 508 | info.type = t; |
| 509 | info.tiling = ti; |
| 510 | info.usageFlags = u; |
| 511 | info.createFlags = combo.createFlags; |
| 512 | res.push_back(info); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | return res; |
| 519 | } |
| 520 | |
Aaron Ruby | 4846dba | 2024-01-08 13:24:42 -0500 | [diff] [blame] | 521 | VkEmulation* createGlobalVkEmulation(VulkanDispatch* vk, bool useVulkanNativeSwapchain) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 522 | // Downstream branches can provide abort logic or otherwise use result without a new macro |
| 523 | #define VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR(res, ...) \ |
| 524 | do { \ |
| 525 | (void)res; /* no-op of unused param*/ \ |
| 526 | ERR(__VA_ARGS__); \ |
| 527 | return nullptr; \ |
Kaiyi Li | 0268399 | 2022-02-07 11:09:50 -0800 | [diff] [blame] | 528 | } while (0) |
| 529 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 530 | AutoLock lock(sVkEmulationLock); |
| 531 | |
| 532 | if (sVkEmulation) return sVkEmulation; |
| 533 | |
Jason Macnak | ed0c9e6 | 2023-03-30 15:58:24 -0700 | [diff] [blame] | 534 | if (!vkDispatchValid(vk)) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 535 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR(ABORT_REASON_OTHER, "Dispatch is invalid."); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | sVkEmulation = new VkEmulation; |
| 539 | |
| 540 | sVkEmulation->gvk = vk; |
| 541 | auto gvk = vk; |
| 542 | |
| 543 | std::vector<const char*> externalMemoryInstanceExtNames = { |
Greg Schlomoff | cd180b0 | 2022-05-12 16:50:56 -0700 | [diff] [blame] | 544 | VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME, |
| 545 | VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 546 | }; |
| 547 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 548 | std::vector<const char*> externalMemoryDeviceExtNames = { |
Greg Schlomoff | cd180b0 | 2022-05-12 16:50:56 -0700 | [diff] [blame] | 549 | VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME, |
| 550 | VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME, |
| 551 | VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 552 | #ifdef _WIN32 |
Greg Schlomoff | cd180b0 | 2022-05-12 16:50:56 -0700 | [diff] [blame] | 553 | VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME, |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 554 | #elif defined(__QNX__) |
| 555 | VK_QNX_EXTERNAL_MEMORY_SCREEN_BUFFER_EXTENSION_NAME, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 556 | #else |
Greg Schlomoff | cd180b0 | 2022-05-12 16:50:56 -0700 | [diff] [blame] | 557 | VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 558 | #endif |
| 559 | }; |
| 560 | |
Gurchetan Singh | f08e59a | 2022-08-25 16:50:36 -0700 | [diff] [blame] | 561 | std::vector<const char*> externalSemaphoreInstanceExtNames = { |
| 562 | VK_KHR_EXTERNAL_SEMAPHORE_CAPABILITIES_EXTENSION_NAME, |
| 563 | }; |
| 564 | |
Yahan Zhou | afa9c8b | 2023-10-16 16:30:28 -0700 | [diff] [blame] | 565 | std::vector<const char*> surfaceInstanceExtNames = { |
| 566 | VK_KHR_SURFACE_EXTENSION_NAME, |
| 567 | }; |
| 568 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 569 | uint32_t extCount = 0; |
| 570 | gvk->vkEnumerateInstanceExtensionProperties(nullptr, &extCount, nullptr); |
Michael Yu | a269bfe | 2022-03-21 17:50:13 -0700 | [diff] [blame] | 571 | std::vector<VkExtensionProperties>& exts = sVkEmulation->instanceExtensions; |
| 572 | exts.resize(extCount); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 573 | gvk->vkEnumerateInstanceExtensionProperties(nullptr, &extCount, exts.data()); |
| 574 | |
| 575 | bool externalMemoryCapabilitiesSupported = |
| 576 | extensionsSupported(exts, externalMemoryInstanceExtNames); |
Gurchetan Singh | f08e59a | 2022-08-25 16:50:36 -0700 | [diff] [blame] | 577 | bool externalSemaphoreCapabilitiesSupported = |
| 578 | extensionsSupported(exts, externalSemaphoreInstanceExtNames); |
Yahan Zhou | afa9c8b | 2023-10-16 16:30:28 -0700 | [diff] [blame] | 579 | bool surfaceSupported = |
| 580 | extensionsSupported(exts, surfaceInstanceExtNames); |
Jason Macnak | c02837a | 2023-07-28 14:53:28 -0700 | [diff] [blame] | 581 | #if defined(__APPLE__) && defined(VK_MVK_moltenvk) |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 582 | bool moltenVKSupported = |
| 583 | (vk->vkGetMTLTextureMVK != nullptr) && (vk->vkSetMTLTextureMVK != nullptr); |
Kaiyi Li | a9bf367 | 2022-09-08 18:47:43 -0700 | [diff] [blame] | 584 | #endif |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 585 | |
| 586 | VkInstanceCreateInfo instCi = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 587 | VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, 0, 0, nullptr, 0, nullptr, 0, nullptr, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 588 | }; |
| 589 | |
Kaiyi Li | eac86a7 | 2021-02-04 08:09:41 -0800 | [diff] [blame] | 590 | std::unordered_set<const char*> enabledExtensions; |
| 591 | |
Jason Macnak | e704548 | 2023-04-11 11:24:19 -0700 | [diff] [blame] | 592 | const bool debugUtilsSupported = extensionsSupported(exts, {VK_EXT_DEBUG_UTILS_EXTENSION_NAME}); |
| 593 | const bool debugUtilsRequested = false; // TODO: enable via a feature or env var? |
| 594 | const bool debugUtilsAvailableAndRequested = debugUtilsSupported && debugUtilsRequested; |
| 595 | if (debugUtilsAvailableAndRequested) { |
| 596 | enabledExtensions.emplace(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); |
| 597 | } |
| 598 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 599 | if (externalMemoryCapabilitiesSupported) { |
Kaiyi Li | eac86a7 | 2021-02-04 08:09:41 -0800 | [diff] [blame] | 600 | for (auto extension : externalMemoryInstanceExtNames) { |
| 601 | enabledExtensions.emplace(extension); |
| 602 | } |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 603 | } |
| 604 | |
Jason Macnak | c02837a | 2023-07-28 14:53:28 -0700 | [diff] [blame] | 605 | #if defined(__APPLE__) && defined(VK_MVK_moltenvk) |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 606 | if (moltenVKSupported) { |
| 607 | // We don't need both moltenVK and external memory. Disable |
| 608 | // external memory if moltenVK is supported. |
| 609 | externalMemoryCapabilitiesSupported = false; |
Kaiyi Li | eac86a7 | 2021-02-04 08:09:41 -0800 | [diff] [blame] | 610 | enabledExtensions.clear(); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 611 | } |
Kaiyi Li | a9bf367 | 2022-09-08 18:47:43 -0700 | [diff] [blame] | 612 | #endif |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 613 | |
Aaron Ruby | 4846dba | 2024-01-08 13:24:42 -0500 | [diff] [blame] | 614 | if (useVulkanNativeSwapchain) { |
| 615 | for (auto extension : SwapChainStateVk::getRequiredInstanceExtensions()) { |
| 616 | enabledExtensions.emplace(extension); |
| 617 | } |
Kaiyi Li | eac86a7 | 2021-02-04 08:09:41 -0800 | [diff] [blame] | 618 | } |
Gurchetan Singh | e26be87 | 2023-05-09 10:18:44 -0700 | [diff] [blame] | 619 | |
Jason Macnak | c02837a | 2023-07-28 14:53:28 -0700 | [diff] [blame] | 620 | #if defined(__APPLE__) && defined(VK_MVK_moltenvk) |
Gurchetan Singh | e26be87 | 2023-05-09 10:18:44 -0700 | [diff] [blame] | 621 | if (moltenVKSupported) { |
| 622 | enabledExtensions.emplace(VK_MVK_MOLTENVK_EXTENSION_NAME); |
| 623 | } |
| 624 | #endif |
| 625 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 626 | std::vector<const char*> enabledExtensions_(enabledExtensions.begin(), enabledExtensions.end()); |
| 627 | instCi.enabledExtensionCount = static_cast<uint32_t>(enabledExtensions_.size()); |
Kaiyi Li | eac86a7 | 2021-02-04 08:09:41 -0800 | [diff] [blame] | 628 | instCi.ppEnabledExtensionNames = enabledExtensions_.data(); |
| 629 | |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 630 | VkApplicationInfo appInfo = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 631 | VK_STRUCTURE_TYPE_APPLICATION_INFO, 0, "AEMU", 1, "AEMU", 1, VK_MAKE_VERSION(1, 0, 0), |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 632 | }; |
| 633 | |
| 634 | instCi.pApplicationInfo = &appInfo; |
| 635 | |
| 636 | // Can we know instance version early? |
| 637 | if (gvk->vkEnumerateInstanceVersion) { |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 638 | // LOG(VERBOSE) << "global loader has vkEnumerateInstanceVersion."; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 639 | uint32_t instanceVersion; |
| 640 | VkResult res = gvk->vkEnumerateInstanceVersion(&instanceVersion); |
| 641 | if (VK_SUCCESS == res) { |
| 642 | if (instanceVersion >= VK_MAKE_VERSION(1, 1, 0)) { |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 643 | // LOG(VERBOSE) << "global loader has vkEnumerateInstanceVersion returning >= 1.1."; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 644 | appInfo.apiVersion = VK_MAKE_VERSION(1, 1, 0); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 649 | // LOG(VERBOSE) << "Creating instance, asking for version " |
| 650 | // << VK_VERSION_MAJOR(appInfo.apiVersion) << "." |
| 651 | // << VK_VERSION_MINOR(appInfo.apiVersion) << "." |
| 652 | // << VK_VERSION_PATCH(appInfo.apiVersion) << " ..."; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 653 | |
| 654 | VkResult res = gvk->vkCreateInstance(&instCi, nullptr, &sVkEmulation->instance); |
| 655 | |
| 656 | if (res != VK_SUCCESS) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 657 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR(res, "Failed to create Vulkan instance. Error %s.", |
| 658 | string_VkResult(res)); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 659 | } |
| 660 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 661 | // Create instance level dispatch. |
| 662 | sVkEmulation->ivk = new VulkanDispatch; |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 663 | init_vulkan_dispatch_from_instance(vk, sVkEmulation->instance, sVkEmulation->ivk); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 664 | |
| 665 | auto ivk = sVkEmulation->ivk; |
| 666 | |
| 667 | if (!vulkan_dispatch_check_instance_VK_VERSION_1_0(ivk)) { |
| 668 | fprintf(stderr, "%s: Warning: Vulkan 1.0 APIs missing from instance\n", __func__); |
| 669 | } |
| 670 | |
| 671 | if (ivk->vkEnumerateInstanceVersion) { |
| 672 | uint32_t instanceVersion; |
| 673 | VkResult enumInstanceRes = ivk->vkEnumerateInstanceVersion(&instanceVersion); |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 674 | if ((VK_SUCCESS == enumInstanceRes) && instanceVersion >= VK_MAKE_VERSION(1, 1, 0)) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 675 | if (!vulkan_dispatch_check_instance_VK_VERSION_1_1(ivk)) { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 676 | fprintf(stderr, "%s: Warning: Vulkan 1.1 APIs missing from instance (1st try)\n", |
| 677 | __func__); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
| 681 | if (appInfo.apiVersion < VK_MAKE_VERSION(1, 1, 0) && |
| 682 | instanceVersion >= VK_MAKE_VERSION(1, 1, 0)) { |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 683 | // LOG(VERBOSE) << "Found out that we can create a higher version instance."; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 684 | appInfo.apiVersion = VK_MAKE_VERSION(1, 1, 0); |
| 685 | |
| 686 | gvk->vkDestroyInstance(sVkEmulation->instance, nullptr); |
| 687 | |
| 688 | VkResult res = gvk->vkCreateInstance(&instCi, nullptr, &sVkEmulation->instance); |
| 689 | |
| 690 | if (res != VK_SUCCESS) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 691 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR( |
| 692 | res, "Failed to create Vulkan 1.1 instance. Error %s.", string_VkResult(res)); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 695 | init_vulkan_dispatch_from_instance(vk, sVkEmulation->instance, sVkEmulation->ivk); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 696 | |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 697 | // LOG(VERBOSE) << "Created Vulkan 1.1 instance on second try."; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 698 | |
| 699 | if (!vulkan_dispatch_check_instance_VK_VERSION_1_1(ivk)) { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 700 | fprintf(stderr, "%s: Warning: Vulkan 1.1 APIs missing from instance (2nd try)\n", |
| 701 | __func__); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | } |
| 705 | |
Michael Yu | a269bfe | 2022-03-21 17:50:13 -0700 | [diff] [blame] | 706 | sVkEmulation->vulkanInstanceVersion = appInfo.apiVersion; |
| 707 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 708 | sVkEmulation->instanceSupportsExternalMemoryCapabilities = externalMemoryCapabilitiesSupported; |
Greg Schlomoff | 83e57ac | 2022-11-07 12:50:35 -0800 | [diff] [blame] | 709 | sVkEmulation->instanceSupportsExternalSemaphoreCapabilities = |
| 710 | externalSemaphoreCapabilitiesSupported; |
Yahan Zhou | afa9c8b | 2023-10-16 16:30:28 -0700 | [diff] [blame] | 711 | sVkEmulation->instanceSupportsSurface = surfaceSupported; |
Jason Macnak | c02837a | 2023-07-28 14:53:28 -0700 | [diff] [blame] | 712 | #if defined(__APPLE__) && defined(VK_MVK_moltenvk) |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 713 | sVkEmulation->instanceSupportsMoltenVK = moltenVKSupported; |
Kaiyi Li | a9bf367 | 2022-09-08 18:47:43 -0700 | [diff] [blame] | 714 | #endif |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 715 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 716 | if (sVkEmulation->instanceSupportsExternalMemoryCapabilities) { |
Kaiyi Li | 31edce6 | 2022-02-02 20:11:05 -0800 | [diff] [blame] | 717 | sVkEmulation->getImageFormatProperties2Func = vk_util::getVkInstanceProcAddrWithFallback< |
| 718 | vk_util::vk_fn_info::GetPhysicalDeviceImageFormatProperties2>( |
| 719 | {ivk->vkGetInstanceProcAddr, vk->vkGetInstanceProcAddr}, sVkEmulation->instance); |
| 720 | sVkEmulation->getPhysicalDeviceProperties2Func = vk_util::getVkInstanceProcAddrWithFallback< |
| 721 | vk_util::vk_fn_info::GetPhysicalDeviceProperties2>( |
| 722 | {ivk->vkGetInstanceProcAddr, vk->vkGetInstanceProcAddr}, sVkEmulation->instance); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 723 | } |
Kaiyi Li | d5ce082 | 2021-11-20 12:48:27 -0800 | [diff] [blame] | 724 | sVkEmulation->getPhysicalDeviceFeatures2Func = |
| 725 | vk_util::getVkInstanceProcAddrWithFallback<vk_util::vk_fn_info::GetPhysicalDeviceFeatures2>( |
| 726 | {ivk->vkGetInstanceProcAddr, vk->vkGetInstanceProcAddr}, sVkEmulation->instance); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 727 | |
Jason Macnak | c02837a | 2023-07-28 14:53:28 -0700 | [diff] [blame] | 728 | #if defined(__APPLE__) && defined(VK_MVK_moltenvk) |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 729 | if (sVkEmulation->instanceSupportsMoltenVK) { |
Doug Horn | efb1796 | 2021-01-07 16:17:34 -0800 | [diff] [blame] | 730 | sVkEmulation->setMTLTextureFunc = reinterpret_cast<PFN_vkSetMTLTextureMVK>( |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 731 | vk->vkGetInstanceProcAddr(sVkEmulation->instance, "vkSetMTLTextureMVK")); |
Doug Horn | efb1796 | 2021-01-07 16:17:34 -0800 | [diff] [blame] | 732 | |
| 733 | if (!sVkEmulation->setMTLTextureFunc) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 734 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR(ABORT_REASON_OTHER, |
| 735 | "Cannot find vkSetMTLTextureMVK."); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 736 | } |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 737 | sVkEmulation->getMTLTextureFunc = reinterpret_cast<PFN_vkGetMTLTextureMVK>( |
| 738 | vk->vkGetInstanceProcAddr(sVkEmulation->instance, "vkGetMTLTextureMVK")); |
Doug Horn | efb1796 | 2021-01-07 16:17:34 -0800 | [diff] [blame] | 739 | if (!sVkEmulation->getMTLTextureFunc) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 740 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR(ABORT_REASON_OTHER, |
| 741 | "Cannot find vkGetMTLTextureMVK."); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 742 | } |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 743 | // LOG(VERBOSE) << "Instance supports VK_MVK_moltenvk."; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 744 | } |
Kaiyi Li | a9bf367 | 2022-09-08 18:47:43 -0700 | [diff] [blame] | 745 | #endif |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 746 | |
| 747 | uint32_t physdevCount = 0; |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 748 | ivk->vkEnumeratePhysicalDevices(sVkEmulation->instance, &physdevCount, nullptr); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 749 | std::vector<VkPhysicalDevice> physdevs(physdevCount); |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 750 | ivk->vkEnumeratePhysicalDevices(sVkEmulation->instance, &physdevCount, physdevs.data()); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 751 | |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 752 | // LOG(VERBOSE) << "Found " << physdevCount << " Vulkan physical devices."; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 753 | |
| 754 | if (physdevCount == 0) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 755 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR(ABORT_REASON_OTHER, "No physical devices available."); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | std::vector<VkEmulation::DeviceSupportInfo> deviceInfos(physdevCount); |
| 759 | |
| 760 | for (int i = 0; i < physdevCount; ++i) { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 761 | ivk->vkGetPhysicalDeviceProperties(physdevs[i], &deviceInfos[i].physdevProps); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 762 | |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 763 | // LOG(VERBOSE) << "Considering Vulkan physical device " << i << ": " |
| 764 | // << deviceInfos[i].physdevProps.deviceName; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 765 | |
| 766 | // It's easier to figure out the staging buffer along with |
| 767 | // external memories if we have the memory properties on hand. |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 768 | ivk->vkGetPhysicalDeviceMemoryProperties(physdevs[i], &deviceInfos[i].memProps); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 769 | |
| 770 | uint32_t deviceExtensionCount = 0; |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 771 | ivk->vkEnumerateDeviceExtensionProperties(physdevs[i], nullptr, &deviceExtensionCount, |
| 772 | nullptr); |
Michael Yu | a269bfe | 2022-03-21 17:50:13 -0700 | [diff] [blame] | 773 | std::vector<VkExtensionProperties>& deviceExts = deviceInfos[i].extensions; |
| 774 | deviceExts.resize(deviceExtensionCount); |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 775 | ivk->vkEnumerateDeviceExtensionProperties(physdevs[i], nullptr, &deviceExtensionCount, |
| 776 | deviceExts.data()); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 777 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 778 | deviceInfos[i].supportsExternalMemoryImport = false; |
| 779 | deviceInfos[i].supportsExternalMemoryExport = false; |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 780 | deviceInfos[i].glInteropSupported = 0; // set later |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 781 | |
| 782 | if (sVkEmulation->instanceSupportsExternalMemoryCapabilities) { |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 783 | deviceInfos[i].supportsExternalMemoryExport = |
| 784 | deviceInfos[i].supportsExternalMemoryImport = |
| 785 | extensionsSupported(deviceExts, externalMemoryDeviceExtNames); |
| 786 | #if defined(__QNX__) |
| 787 | // External memory export not supported on QNX |
| 788 | deviceInfos[i].supportsExternalMemoryExport = false; |
| 789 | #endif |
Lingfeng Yang | 273d6be | 2021-06-08 19:26:52 -0700 | [diff] [blame] | 790 | deviceInfos[i].supportsIdProperties = |
| 791 | sVkEmulation->getPhysicalDeviceProperties2Func != nullptr; |
Michael Yu | a269bfe | 2022-03-21 17:50:13 -0700 | [diff] [blame] | 792 | deviceInfos[i].supportsDriverProperties = |
| 793 | extensionsSupported(deviceExts, {VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME}) || |
| 794 | (deviceInfos[i].physdevProps.apiVersion >= VK_API_VERSION_1_2); |
| 795 | |
Lingfeng Yang | 273d6be | 2021-06-08 19:26:52 -0700 | [diff] [blame] | 796 | if (!sVkEmulation->getPhysicalDeviceProperties2Func) { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 797 | fprintf(stderr, |
| 798 | "%s: warning: device claims to support ID properties " |
| 799 | "but vkGetPhysicalDeviceProperties2 could not be found\n", |
| 800 | __func__); |
Lingfeng Yang | 273d6be | 2021-06-08 19:26:52 -0700 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | |
Michael Yu | a269bfe | 2022-03-21 17:50:13 -0700 | [diff] [blame] | 804 | if (sVkEmulation->getPhysicalDeviceProperties2Func) { |
| 805 | VkPhysicalDeviceProperties2 deviceProps = { |
| 806 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR, |
Lingfeng Yang | 273d6be | 2021-06-08 19:26:52 -0700 | [diff] [blame] | 807 | }; |
Michael Yu | a269bfe | 2022-03-21 17:50:13 -0700 | [diff] [blame] | 808 | VkPhysicalDeviceIDProperties idProps = { |
| 809 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES_KHR, |
Lingfeng Yang | 273d6be | 2021-06-08 19:26:52 -0700 | [diff] [blame] | 810 | }; |
Michael Yu | a269bfe | 2022-03-21 17:50:13 -0700 | [diff] [blame] | 811 | VkPhysicalDeviceDriverPropertiesKHR driverProps = { |
| 812 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR, |
| 813 | }; |
| 814 | |
| 815 | auto devicePropsChain = vk_make_chain_iterator(&deviceProps); |
| 816 | |
| 817 | if (deviceInfos[i].supportsIdProperties) { |
| 818 | vk_append_struct(&devicePropsChain, &idProps); |
| 819 | } |
| 820 | |
| 821 | if (deviceInfos[i].supportsDriverProperties) { |
| 822 | vk_append_struct(&devicePropsChain, &driverProps); |
| 823 | } |
| 824 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 825 | sVkEmulation->getPhysicalDeviceProperties2Func(physdevs[i], &deviceProps); |
Michael Yu | a269bfe | 2022-03-21 17:50:13 -0700 | [diff] [blame] | 826 | |
| 827 | deviceInfos[i].idProps = vk_make_orphan_copy(idProps); |
| 828 | |
| 829 | std::stringstream driverVendorBuilder; |
| 830 | driverVendorBuilder << "Vendor " << std::hex << std::setfill('0') << std::showbase |
| 831 | << deviceInfos[i].physdevProps.vendorID; |
| 832 | |
| 833 | std::string decodedDriverVersion = decodeDriverVersion( |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 834 | deviceInfos[i].physdevProps.vendorID, deviceInfos[i].physdevProps.driverVersion); |
Michael Yu | a269bfe | 2022-03-21 17:50:13 -0700 | [diff] [blame] | 835 | |
| 836 | std::stringstream driverVersionBuilder; |
| 837 | driverVersionBuilder << "Driver Version " << std::hex << std::setfill('0') |
| 838 | << std::showbase << deviceInfos[i].physdevProps.driverVersion |
| 839 | << " Decoded As " << decodedDriverVersion; |
| 840 | |
| 841 | std::string driverVendor = driverVendorBuilder.str(); |
| 842 | std::string driverVersion = driverVersionBuilder.str(); |
| 843 | if (deviceInfos[i].supportsDriverProperties && driverProps.driverID) { |
| 844 | driverVendor = std::string{driverProps.driverName} + " (" + driverVendor + ")"; |
| 845 | driverVersion = std::string{driverProps.driverInfo} + " (" + |
| 846 | string_VkDriverId(driverProps.driverID) + " " + driverVersion + ")"; |
| 847 | } |
| 848 | |
| 849 | deviceInfos[i].driverVendor = driverVendor; |
| 850 | deviceInfos[i].driverVersion = driverVersion; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 851 | } |
| 852 | |
Kaiyi Li | d5ce082 | 2021-11-20 12:48:27 -0800 | [diff] [blame] | 853 | deviceInfos[i].hasSamplerYcbcrConversionExtension = |
| 854 | extensionsSupported(deviceExts, {VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME}); |
| 855 | if (sVkEmulation->getPhysicalDeviceFeatures2Func) { |
| 856 | VkPhysicalDeviceFeatures2 features2 = { |
| 857 | .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, |
| 858 | }; |
| 859 | auto features2Chain = vk_make_chain_iterator(&features2); |
| 860 | VkPhysicalDeviceSamplerYcbcrConversionFeatures samplerYcbcrConversionFeatures = { |
| 861 | .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES, |
| 862 | }; |
| 863 | vk_append_struct(&features2Chain, &samplerYcbcrConversionFeatures); |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 864 | #if defined(__QNX__) |
| 865 | VkPhysicalDeviceExternalMemoryScreenBufferFeaturesQNX extMemScreenBufferFeatures = { |
| 866 | .sType = |
| 867 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_SCREEN_BUFFER_FEATURES_QNX, |
| 868 | }; |
| 869 | vk_append_struct(&features2Chain, &extMemScreenBufferFeatures); |
| 870 | #endif |
Kaiyi Li | d5ce082 | 2021-11-20 12:48:27 -0800 | [diff] [blame] | 871 | sVkEmulation->getPhysicalDeviceFeatures2Func(physdevs[i], &features2); |
| 872 | |
| 873 | deviceInfos[i].supportsSamplerYcbcrConversion = |
| 874 | samplerYcbcrConversionFeatures.samplerYcbcrConversion == VK_TRUE; |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 875 | #if defined(__QNX__) |
| 876 | deviceInfos[i].supportsExternalMemoryImport = |
| 877 | extMemScreenBufferFeatures.screenBufferImport == VK_TRUE; |
| 878 | } else { |
| 879 | deviceInfos[i].supportsExternalMemoryImport = false; |
| 880 | #endif |
Kaiyi Li | d5ce082 | 2021-11-20 12:48:27 -0800 | [diff] [blame] | 881 | } |
| 882 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 883 | uint32_t queueFamilyCount = 0; |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 884 | ivk->vkGetPhysicalDeviceQueueFamilyProperties(physdevs[i], &queueFamilyCount, nullptr); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 885 | std::vector<VkQueueFamilyProperties> queueFamilyProps(queueFamilyCount); |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 886 | ivk->vkGetPhysicalDeviceQueueFamilyProperties(physdevs[i], &queueFamilyCount, |
| 887 | queueFamilyProps.data()); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 888 | |
| 889 | for (uint32_t j = 0; j < queueFamilyCount; ++j) { |
| 890 | auto count = queueFamilyProps[j].queueCount; |
| 891 | auto flags = queueFamilyProps[j].queueFlags; |
| 892 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 893 | bool hasGraphicsQueueFamily = (count > 0 && (flags & VK_QUEUE_GRAPHICS_BIT)); |
| 894 | bool hasComputeQueueFamily = (count > 0 && (flags & VK_QUEUE_COMPUTE_BIT)); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 895 | |
| 896 | deviceInfos[i].hasGraphicsQueueFamily = |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 897 | deviceInfos[i].hasGraphicsQueueFamily || hasGraphicsQueueFamily; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 898 | |
| 899 | deviceInfos[i].hasComputeQueueFamily = |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 900 | deviceInfos[i].hasComputeQueueFamily || hasComputeQueueFamily; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 901 | |
| 902 | if (hasGraphicsQueueFamily) { |
| 903 | deviceInfos[i].graphicsQueueFamilyIndices.push_back(j); |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 904 | // LOG(VERBOSE) << "Graphics queue family index: " << j; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | if (hasComputeQueueFamily) { |
| 908 | deviceInfos[i].computeQueueFamilyIndices.push_back(j); |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 909 | // LOG(VERBOSE) << "Compute queue family index: " << j; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | // Of all the devices enumerated, find the best one. Try to find a device |
| 915 | // with graphics queue as the highest priority, then ext memory, then |
| 916 | // compute. |
| 917 | |
| 918 | // Graphics queue is highest priority since without that, we really |
| 919 | // shouldn't be using the driver. Although, one could make a case for doing |
| 920 | // some sorts of things if only a compute queue is available (such as for |
| 921 | // AI), that's not really the priority yet. |
| 922 | |
| 923 | // As for external memory, we really should not be running on any driver |
| 924 | // without external memory support, but we might be able to pull it off, and |
| 925 | // single Vulkan apps might work via CPU transfer of the rendered frames. |
| 926 | |
| 927 | // Compute support is treated as icing on the cake and not relied upon yet |
| 928 | // for anything critical to emulation. However, we might potentially use it |
| 929 | // to perform image format conversion on GPUs where that's not natively |
| 930 | // supported. |
| 931 | |
| 932 | // Another implicit choice is to select only one Vulkan device. This makes |
| 933 | // things simple for now, but we could consider utilizing multiple devices |
| 934 | // in use cases that make sense, if/when they come up. |
| 935 | |
| 936 | std::vector<uint32_t> deviceScores(physdevCount, 0); |
| 937 | |
| 938 | for (uint32_t i = 0; i < physdevCount; ++i) { |
| 939 | uint32_t deviceScore = 0; |
Lingfeng Yang | 273d6be | 2021-06-08 19:26:52 -0700 | [diff] [blame] | 940 | if (deviceInfos[i].hasGraphicsQueueFamily) deviceScore += 10000; |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 941 | if (deviceInfos[i].supportsExternalMemoryImport || |
| 942 | deviceInfos[i].supportsExternalMemoryExport) |
| 943 | deviceScore += 1000; |
Lingfeng Yang | 273d6be | 2021-06-08 19:26:52 -0700 | [diff] [blame] | 944 | if (deviceInfos[i].physdevProps.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU || |
| 945 | deviceInfos[i].physdevProps.deviceType == VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU) { |
| 946 | deviceScore += 100; |
| 947 | } |
| 948 | if (deviceInfos[i].physdevProps.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU) { |
| 949 | deviceScore += 50; |
| 950 | } |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 951 | deviceScores[i] = deviceScore; |
| 952 | } |
| 953 | |
| 954 | uint32_t maxScoringIndex = 0; |
| 955 | uint32_t maxScore = 0; |
| 956 | |
Lingfeng Yang | 273d6be | 2021-06-08 19:26:52 -0700 | [diff] [blame] | 957 | // If we don't support physical device ID properties, |
| 958 | // just pick the first physical device. |
| 959 | if (!sVkEmulation->instanceSupportsExternalMemoryCapabilities) { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 960 | fprintf(stderr, |
| 961 | "%s: warning: instance doesn't support " |
| 962 | "external memory capabilities, picking first physical device\n", |
| 963 | __func__); |
Lingfeng Yang | 273d6be | 2021-06-08 19:26:52 -0700 | [diff] [blame] | 964 | maxScoringIndex = 0; |
| 965 | } else { |
| 966 | for (uint32_t i = 0; i < physdevCount; ++i) { |
| 967 | if (deviceScores[i] > maxScore) { |
| 968 | maxScoringIndex = i; |
| 969 | maxScore = deviceScores[i]; |
| 970 | } |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | |
| 974 | sVkEmulation->physdev = physdevs[maxScoringIndex]; |
Gurchetan Singh | 424f067 | 2022-08-26 11:10:12 -0700 | [diff] [blame] | 975 | sVkEmulation->physicalDeviceIndex = maxScoringIndex; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 976 | sVkEmulation->deviceInfo = deviceInfos[maxScoringIndex]; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 977 | // Postcondition: sVkEmulation has valid device support info |
| 978 | |
| 979 | // Ask about image format support here. |
| 980 | // TODO: May have to first ask when selecting physical devices |
| 981 | // (e.g., choose between Intel or NVIDIA GPU for certain image format |
| 982 | // support) |
| 983 | sVkEmulation->imageSupportInfo = getBasicImageSupportList(); |
| 984 | for (size_t i = 0; i < sVkEmulation->imageSupportInfo.size(); ++i) { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 985 | getImageFormatExternalMemorySupportInfo(ivk, sVkEmulation->physdev, |
| 986 | &sVkEmulation->imageSupportInfo[i]); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | if (!sVkEmulation->deviceInfo.hasGraphicsQueueFamily) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 990 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR(ABORT_REASON_OTHER, |
| 991 | "No Vulkan devices with graphics queues found."); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | auto deviceVersion = sVkEmulation->deviceInfo.physdevProps.apiVersion; |
Doug Horn | a25b511 | 2021-06-03 10:59:00 -0700 | [diff] [blame] | 995 | VK_COMMON_LOG("Selecting Vulkan device: %s", sVkEmulation->deviceInfo.physdevProps.deviceName); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 996 | |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 997 | // LOG(VERBOSE) << "Version: " |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 998 | // << VK_VERSION_MAJOR(deviceVersion) << "." << VK_VERSION_MINOR(deviceVersion) << |
| 999 | // "." << VK_VERSION_PATCH(deviceVersion); |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 1000 | // LOG(VERBOSE) << "Has graphics queue? " |
| 1001 | // << sVkEmulation->deviceInfo.hasGraphicsQueueFamily; |
| 1002 | // LOG(VERBOSE) << "Has external memory support? " |
| 1003 | // << sVkEmulation->deviceInfo.supportsExternalMemory; |
| 1004 | // LOG(VERBOSE) << "Has compute queue? " |
| 1005 | // << sVkEmulation->deviceInfo.hasComputeQueueFamily; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1006 | |
| 1007 | float priority = 1.0f; |
| 1008 | VkDeviceQueueCreateInfo dqCi = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1009 | VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, |
| 1010 | 0, |
| 1011 | 0, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1012 | sVkEmulation->deviceInfo.graphicsQueueFamilyIndices[0], |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1013 | 1, |
| 1014 | &priority, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1015 | }; |
| 1016 | |
Kaiyi Li | eac86a7 | 2021-02-04 08:09:41 -0800 | [diff] [blame] | 1017 | std::unordered_set<const char*> selectedDeviceExtensionNames_; |
| 1018 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1019 | if (sVkEmulation->deviceInfo.supportsExternalMemoryImport || |
| 1020 | sVkEmulation->deviceInfo.supportsExternalMemoryExport) { |
Kaiyi Li | eac86a7 | 2021-02-04 08:09:41 -0800 | [diff] [blame] | 1021 | for (auto extension : externalMemoryDeviceExtNames) { |
| 1022 | selectedDeviceExtensionNames_.emplace(extension); |
| 1023 | } |
| 1024 | } |
Aaron Ruby | 4846dba | 2024-01-08 13:24:42 -0500 | [diff] [blame] | 1025 | if (useVulkanNativeSwapchain) { |
| 1026 | for (auto extension : SwapChainStateVk::getRequiredDeviceExtensions()) { |
| 1027 | selectedDeviceExtensionNames_.emplace(extension); |
| 1028 | } |
Kaiyi Li | eac86a7 | 2021-02-04 08:09:41 -0800 | [diff] [blame] | 1029 | } |
Kaiyi Li | d5ce082 | 2021-11-20 12:48:27 -0800 | [diff] [blame] | 1030 | if (sVkEmulation->deviceInfo.hasSamplerYcbcrConversionExtension) { |
| 1031 | selectedDeviceExtensionNames_.emplace(VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME); |
| 1032 | } |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1033 | std::vector<const char*> selectedDeviceExtensionNames(selectedDeviceExtensionNames_.begin(), |
| 1034 | selectedDeviceExtensionNames_.end()); |
Kaiyi Li | eac86a7 | 2021-02-04 08:09:41 -0800 | [diff] [blame] | 1035 | |
| 1036 | VkDeviceCreateInfo dCi = {}; |
| 1037 | dCi.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; |
Kaiyi Li | eac86a7 | 2021-02-04 08:09:41 -0800 | [diff] [blame] | 1038 | dCi.queueCreateInfoCount = 1; |
| 1039 | dCi.pQueueCreateInfos = &dqCi; |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1040 | dCi.enabledExtensionCount = static_cast<uint32_t>(selectedDeviceExtensionNames.size()); |
Kaiyi Li | eac86a7 | 2021-02-04 08:09:41 -0800 | [diff] [blame] | 1041 | dCi.ppEnabledExtensionNames = selectedDeviceExtensionNames.data(); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1042 | |
Kaiyi Li | d5ce082 | 2021-11-20 12:48:27 -0800 | [diff] [blame] | 1043 | // Setting up VkDeviceCreateInfo::pNext |
| 1044 | auto deviceCiChain = vk_make_chain_iterator(&dCi); |
| 1045 | |
| 1046 | VkPhysicalDeviceFeatures2 features = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2}; |
| 1047 | vk_append_struct(&deviceCiChain, &features); |
| 1048 | |
| 1049 | std::unique_ptr<VkPhysicalDeviceSamplerYcbcrConversionFeatures> samplerYcbcrConversionFeatures = |
| 1050 | nullptr; |
| 1051 | if (sVkEmulation->deviceInfo.supportsSamplerYcbcrConversion) { |
| 1052 | samplerYcbcrConversionFeatures = |
| 1053 | std::make_unique<VkPhysicalDeviceSamplerYcbcrConversionFeatures>( |
| 1054 | VkPhysicalDeviceSamplerYcbcrConversionFeatures{ |
| 1055 | .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES, |
| 1056 | .samplerYcbcrConversion = VK_TRUE, |
| 1057 | }); |
| 1058 | vk_append_struct(&deviceCiChain, samplerYcbcrConversionFeatures.get()); |
| 1059 | } |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1060 | #if defined(__QNX__) |
| 1061 | std::unique_ptr<VkPhysicalDeviceExternalMemoryScreenBufferFeaturesQNX> |
| 1062 | extMemScreenBufferFeaturesQNX = nullptr; |
| 1063 | if (sVkEmulation->deviceInfo.supportsExternalMemoryImport) { |
| 1064 | extMemScreenBufferFeaturesQNX = std::make_unique< |
| 1065 | VkPhysicalDeviceExternalMemoryScreenBufferFeaturesQNX>( |
| 1066 | VkPhysicalDeviceExternalMemoryScreenBufferFeaturesQNX{ |
| 1067 | .sType = |
| 1068 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_SCREEN_BUFFER_FEATURES_QNX, |
| 1069 | .screenBufferImport = VK_TRUE, |
| 1070 | }); |
| 1071 | vk_append_struct(&deviceCiChain, extMemScreenBufferFeaturesQNX.get()); |
| 1072 | } |
| 1073 | #endif |
Kaiyi Li | d5ce082 | 2021-11-20 12:48:27 -0800 | [diff] [blame] | 1074 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1075 | ivk->vkCreateDevice(sVkEmulation->physdev, &dCi, nullptr, &sVkEmulation->device); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1076 | |
| 1077 | if (res != VK_SUCCESS) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 1078 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR(res, "Failed to create Vulkan device. Error %s.", |
| 1079 | string_VkResult(res)); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | // device created; populate dispatch table |
| 1083 | sVkEmulation->dvk = new VulkanDispatch; |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1084 | init_vulkan_dispatch_from_device(ivk, sVkEmulation->device, sVkEmulation->dvk); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1085 | |
| 1086 | auto dvk = sVkEmulation->dvk; |
| 1087 | |
| 1088 | // Check if the dispatch table has everything 1.1 related |
| 1089 | if (!vulkan_dispatch_check_device_VK_VERSION_1_0(dvk)) { |
| 1090 | fprintf(stderr, "%s: Warning: Vulkan 1.0 APIs missing from device.\n", __func__); |
| 1091 | } |
| 1092 | if (deviceVersion >= VK_MAKE_VERSION(1, 1, 0)) { |
| 1093 | if (!vulkan_dispatch_check_device_VK_VERSION_1_1(dvk)) { |
| 1094 | fprintf(stderr, "%s: Warning: Vulkan 1.1 APIs missing from device\n", __func__); |
| 1095 | } |
| 1096 | } |
| 1097 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1098 | if (sVkEmulation->deviceInfo.supportsExternalMemoryImport) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1099 | sVkEmulation->deviceInfo.getImageMemoryRequirements2Func = |
| 1100 | reinterpret_cast<PFN_vkGetImageMemoryRequirements2KHR>( |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1101 | dvk->vkGetDeviceProcAddr(sVkEmulation->device, "vkGetImageMemoryRequirements2KHR")); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1102 | if (!sVkEmulation->deviceInfo.getImageMemoryRequirements2Func) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 1103 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR(ABORT_REASON_OTHER, |
| 1104 | "Cannot find vkGetImageMemoryRequirements2KHR."); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1105 | } |
| 1106 | sVkEmulation->deviceInfo.getBufferMemoryRequirements2Func = |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1107 | reinterpret_cast<PFN_vkGetBufferMemoryRequirements2KHR>(dvk->vkGetDeviceProcAddr( |
| 1108 | sVkEmulation->device, "vkGetBufferMemoryRequirements2KHR")); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1109 | if (!sVkEmulation->deviceInfo.getBufferMemoryRequirements2Func) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 1110 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR(ABORT_REASON_OTHER, |
| 1111 | "Cannot find vkGetBufferMemoryRequirements2KHR"); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1112 | } |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1113 | } |
| 1114 | if (sVkEmulation->deviceInfo.supportsExternalMemoryExport) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1115 | #ifdef _WIN32 |
| 1116 | sVkEmulation->deviceInfo.getMemoryHandleFunc = |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1117 | reinterpret_cast<PFN_vkGetMemoryWin32HandleKHR>( |
| 1118 | dvk->vkGetDeviceProcAddr(sVkEmulation->device, "vkGetMemoryWin32HandleKHR")); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1119 | #else |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1120 | sVkEmulation->deviceInfo.getMemoryHandleFunc = reinterpret_cast<PFN_vkGetMemoryFdKHR>( |
| 1121 | dvk->vkGetDeviceProcAddr(sVkEmulation->device, "vkGetMemoryFdKHR")); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1122 | #endif |
| 1123 | if (!sVkEmulation->deviceInfo.getMemoryHandleFunc) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 1124 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR(ABORT_REASON_OTHER, |
| 1125 | "Cannot find vkGetMemory(Fd|Win32Handle)KHR"); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 1129 | // LOG(VERBOSE) << "Vulkan logical device created and extension functions obtained.\n"; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1130 | |
Kaiyi Li | 263e9f4 | 2021-08-20 07:33:05 -0700 | [diff] [blame] | 1131 | sVkEmulation->queueLock = std::make_shared<android::base::Lock>(); |
| 1132 | { |
| 1133 | android::base::AutoLock lock(*sVkEmulation->queueLock); |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1134 | dvk->vkGetDeviceQueue(sVkEmulation->device, |
| 1135 | sVkEmulation->deviceInfo.graphicsQueueFamilyIndices[0], 0, |
| 1136 | &sVkEmulation->queue); |
Kaiyi Li | 263e9f4 | 2021-08-20 07:33:05 -0700 | [diff] [blame] | 1137 | } |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1138 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1139 | sVkEmulation->queueFamilyIndex = sVkEmulation->deviceInfo.graphicsQueueFamilyIndices[0]; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1140 | |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 1141 | // LOG(VERBOSE) << "Vulkan device queue obtained."; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1142 | |
| 1143 | VkCommandPoolCreateInfo poolCi = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1144 | VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, |
| 1145 | 0, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1146 | VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, |
| 1147 | sVkEmulation->queueFamilyIndex, |
| 1148 | }; |
| 1149 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1150 | VkResult poolCreateRes = dvk->vkCreateCommandPool(sVkEmulation->device, &poolCi, nullptr, |
| 1151 | &sVkEmulation->commandPool); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1152 | |
| 1153 | if (poolCreateRes != VK_SUCCESS) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 1154 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR(poolCreateRes, |
| 1155 | "Failed to create command pool. Error: %s.", |
| 1156 | string_VkResult(poolCreateRes)); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | VkCommandBufferAllocateInfo cbAi = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1160 | VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, |
| 1161 | 0, |
| 1162 | sVkEmulation->commandPool, |
| 1163 | VK_COMMAND_BUFFER_LEVEL_PRIMARY, |
| 1164 | 1, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1165 | }; |
| 1166 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1167 | VkResult cbAllocRes = |
| 1168 | dvk->vkAllocateCommandBuffers(sVkEmulation->device, &cbAi, &sVkEmulation->commandBuffer); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1169 | |
| 1170 | if (cbAllocRes != VK_SUCCESS) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 1171 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR(cbAllocRes, |
| 1172 | "Failed to allocate command buffer. Error: %s.", |
| 1173 | string_VkResult(cbAllocRes)); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | VkFenceCreateInfo fenceCi = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1177 | VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, |
| 1178 | 0, |
| 1179 | 0, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1180 | }; |
| 1181 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1182 | VkResult fenceCreateRes = dvk->vkCreateFence(sVkEmulation->device, &fenceCi, nullptr, |
| 1183 | &sVkEmulation->commandBufferFence); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1184 | |
| 1185 | if (fenceCreateRes != VK_SUCCESS) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 1186 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR( |
| 1187 | fenceCreateRes, "Failed to create fence for command buffer. Error: %s.", |
| 1188 | string_VkResult(fenceCreateRes)); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | // At this point, the global emulation state's logical device can alloc |
| 1192 | // memory and send commands. However, it can't really do much yet to |
| 1193 | // communicate the results without the staging buffer. Set that up here. |
| 1194 | // Note that the staging buffer is meant to use external memory, with a |
| 1195 | // non-external-memory fallback. |
| 1196 | |
| 1197 | VkBufferCreateInfo bufCi = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1198 | VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, |
| 1199 | 0, |
| 1200 | 0, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1201 | sVkEmulation->staging.size, |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1202 | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1203 | VK_SHARING_MODE_EXCLUSIVE, |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1204 | 0, |
| 1205 | nullptr, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1206 | }; |
| 1207 | |
| 1208 | VkResult bufCreateRes = |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1209 | dvk->vkCreateBuffer(sVkEmulation->device, &bufCi, nullptr, &sVkEmulation->staging.buffer); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1210 | |
| 1211 | if (bufCreateRes != VK_SUCCESS) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 1212 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR(bufCreateRes, |
| 1213 | "Failed to create staging buffer index. Error: %s.", |
| 1214 | string_VkResult(bufCreateRes)); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | VkMemoryRequirements memReqs; |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1218 | dvk->vkGetBufferMemoryRequirements(sVkEmulation->device, sVkEmulation->staging.buffer, |
| 1219 | &memReqs); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1220 | |
| 1221 | sVkEmulation->staging.memory.size = memReqs.size; |
| 1222 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1223 | bool gotStagingTypeIndex = |
| 1224 | getStagingMemoryTypeIndex(dvk, sVkEmulation->device, &sVkEmulation->deviceInfo.memProps, |
| 1225 | &sVkEmulation->staging.memory.typeIndex); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1226 | |
| 1227 | if (!gotStagingTypeIndex) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 1228 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR(ABORT_REASON_OTHER, |
| 1229 | "Failed to determine staging memory type index."); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1230 | } |
| 1231 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1232 | if (!((1 << sVkEmulation->staging.memory.typeIndex) & memReqs.memoryTypeBits)) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 1233 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR( |
| 1234 | ABORT_REASON_OTHER, |
Kaiyi Li | 0268399 | 2022-02-07 11:09:50 -0800 | [diff] [blame] | 1235 | "Failed: Inconsistent determination of memory type index for staging buffer"); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1236 | } |
| 1237 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1238 | if (!allocExternalMemory(dvk, &sVkEmulation->staging.memory, false /* not external */, |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1239 | kNullopt /* deviceAlignment */)) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 1240 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR(ABORT_REASON_OTHER, |
| 1241 | "Failed to allocate memory for staging buffer."); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | VkResult stagingBufferBindRes = dvk->vkBindBufferMemory( |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1245 | sVkEmulation->device, sVkEmulation->staging.buffer, sVkEmulation->staging.memory.memory, 0); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1246 | |
| 1247 | if (stagingBufferBindRes != VK_SUCCESS) { |
William Ho | 6c844be | 2022-05-09 13:27:46 -0700 | [diff] [blame] | 1248 | VK_EMU_INIT_RETURN_OR_ABORT_ON_ERROR(stagingBufferBindRes, |
| 1249 | "Failed to bind memory for staging buffer. Error %s.", |
| 1250 | string_VkResult(stagingBufferBindRes)); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1251 | } |
| 1252 | |
Jason Macnak | e704548 | 2023-04-11 11:24:19 -0700 | [diff] [blame] | 1253 | sVkEmulation->debugUtilsAvailableAndRequested = debugUtilsAvailableAndRequested; |
| 1254 | if (sVkEmulation->debugUtilsAvailableAndRequested) { |
| 1255 | sVkEmulation->debugUtilsHelper = |
| 1256 | DebugUtilsHelper::withUtilsEnabled(sVkEmulation->device, sVkEmulation->ivk); |
| 1257 | } |
| 1258 | |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 1259 | // LOG(VERBOSE) << "Vulkan global emulation state successfully initialized."; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1260 | sVkEmulation->live = true; |
| 1261 | |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 1262 | sVkEmulation->transferQueueCommandBufferPool.resize(0); |
| 1263 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1264 | return sVkEmulation; |
| 1265 | } |
| 1266 | |
Jason Macnak | 12c1c39 | 2023-03-14 15:15:38 -0700 | [diff] [blame] | 1267 | std::optional<uint32_t> findRepresentativeColorBufferMemoryTypeIndexLocked(); |
| 1268 | |
Kaiyi Li | e4159ae | 2021-11-20 06:58:15 -0800 | [diff] [blame] | 1269 | void initVkEmulationFeatures(std::unique_ptr<VkEmulationFeatures> features) { |
Kaiyi Li | 4ddffd9 | 2022-02-15 09:34:55 -0800 | [diff] [blame] | 1270 | if (!sVkEmulation || !sVkEmulation->live) { |
| 1271 | ERR("VkEmulation is either not initialized or destroyed."); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1272 | return; |
| 1273 | } |
| 1274 | |
Kaiyi Li | 4ddffd9 | 2022-02-15 09:34:55 -0800 | [diff] [blame] | 1275 | AutoLock lock(sVkEmulationLock); |
| 1276 | INFO("Initializing VkEmulation features:"); |
Kaiyi Li | e4159ae | 2021-11-20 06:58:15 -0800 | [diff] [blame] | 1277 | INFO(" glInteropSupported: %s", features->glInteropSupported ? "true" : "false"); |
| 1278 | INFO(" useDeferredCommands: %s", features->deferredCommands ? "true" : "false"); |
Kaiyi Li | 4ddffd9 | 2022-02-15 09:34:55 -0800 | [diff] [blame] | 1279 | INFO(" createResourceWithRequirements: %s", |
Kaiyi Li | e4159ae | 2021-11-20 06:58:15 -0800 | [diff] [blame] | 1280 | features->createResourceWithRequirements ? "true" : "false"); |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 1281 | INFO(" useVulkanComposition: %s", features->useVulkanComposition ? "true" : "false"); |
Kaiyi Li | e4159ae | 2021-11-20 06:58:15 -0800 | [diff] [blame] | 1282 | INFO(" useVulkanNativeSwapchain: %s", features->useVulkanNativeSwapchain ? "true" : "false"); |
| 1283 | INFO(" enable guestRenderDoc: %s", features->guestRenderDoc ? "true" : "false"); |
Greg Schlomoff | 4dbcbd6 | 2022-11-16 15:36:56 -0800 | [diff] [blame] | 1284 | INFO(" ASTC LDR emulation mode: %d", features->astcLdrEmulationMode); |
Kaiyi Li | 696aac2 | 2022-08-09 17:59:02 -0700 | [diff] [blame] | 1285 | INFO(" enable ETC2 emulation: %s", features->enableEtc2Emulation ? "true" : "false"); |
Kaiyi Li | fca9166 | 2022-08-08 09:24:51 -0700 | [diff] [blame] | 1286 | INFO(" enable Ycbcr emulation: %s", features->enableYcbcrEmulation ? "true" : "false"); |
Jason Macnak | cd59838 | 2022-07-21 11:06:06 -0700 | [diff] [blame] | 1287 | INFO(" guestUsesAngle: %s", features->guestUsesAngle ? "true" : "false"); |
Jason Macnak | cc53a20 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1288 | INFO(" useDedicatedAllocations: %s", features->useDedicatedAllocations ? "true" : "false"); |
Kaiyi Li | e4159ae | 2021-11-20 06:58:15 -0800 | [diff] [blame] | 1289 | sVkEmulation->deviceInfo.glInteropSupported = features->glInteropSupported; |
| 1290 | sVkEmulation->useDeferredCommands = features->deferredCommands; |
| 1291 | sVkEmulation->useCreateResourcesWithRequirements = features->createResourceWithRequirements; |
| 1292 | sVkEmulation->guestRenderDoc = std::move(features->guestRenderDoc); |
Greg Schlomoff | 4dbcbd6 | 2022-11-16 15:36:56 -0800 | [diff] [blame] | 1293 | sVkEmulation->astcLdrEmulationMode = features->astcLdrEmulationMode; |
Kaiyi Li | 696aac2 | 2022-08-09 17:59:02 -0700 | [diff] [blame] | 1294 | sVkEmulation->enableEtc2Emulation = features->enableEtc2Emulation; |
Kaiyi Li | fca9166 | 2022-08-08 09:24:51 -0700 | [diff] [blame] | 1295 | sVkEmulation->enableYcbcrEmulation = features->enableYcbcrEmulation; |
Jason Macnak | cd59838 | 2022-07-21 11:06:06 -0700 | [diff] [blame] | 1296 | sVkEmulation->guestUsesAngle = features->guestUsesAngle; |
Jason Macnak | cc53a20 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1297 | sVkEmulation->useDedicatedAllocations = features->useDedicatedAllocations; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1298 | |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 1299 | if (features->useVulkanComposition) { |
| 1300 | if (sVkEmulation->compositorVk) { |
| 1301 | ERR("Reset VkEmulation::compositorVk."); |
| 1302 | } |
Jason Macnak | 5c76bdb | 2024-01-30 09:07:34 -0800 | [diff] [blame] | 1303 | sVkEmulation->compositorVk = |
| 1304 | CompositorVk::create(*sVkEmulation->ivk, sVkEmulation->device, sVkEmulation->physdev, |
| 1305 | sVkEmulation->queue, sVkEmulation->queueLock, |
| 1306 | sVkEmulation->queueFamilyIndex, 3, sVkEmulation->debugUtilsHelper); |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
Kaiyi Li | e4159ae | 2021-11-20 06:58:15 -0800 | [diff] [blame] | 1309 | if (features->useVulkanNativeSwapchain) { |
Kaiyi Li | 4ddffd9 | 2022-02-15 09:34:55 -0800 | [diff] [blame] | 1310 | if (sVkEmulation->displayVk) { |
| 1311 | ERR("Reset VkEmulation::displayVk."); |
| 1312 | } |
| 1313 | sVkEmulation->displayVk = std::make_unique<DisplayVk>( |
| 1314 | *sVkEmulation->ivk, sVkEmulation->physdev, sVkEmulation->queueFamilyIndex, |
| 1315 | sVkEmulation->queueFamilyIndex, sVkEmulation->device, sVkEmulation->queue, |
| 1316 | sVkEmulation->queueLock, sVkEmulation->queue, sVkEmulation->queueLock); |
| 1317 | } |
Jason Macnak | 12c1c39 | 2023-03-14 15:15:38 -0700 | [diff] [blame] | 1318 | |
| 1319 | sVkEmulation->representativeColorBufferMemoryTypeIndex = |
| 1320 | findRepresentativeColorBufferMemoryTypeIndexLocked(); |
| 1321 | if (sVkEmulation->representativeColorBufferMemoryTypeIndex) { |
| 1322 | VK_COMMON_VERBOSE("Emulated ColorBuffer memory type is based on memory type index %d.", |
| 1323 | *sVkEmulation->representativeColorBufferMemoryTypeIndex); |
| 1324 | } else { |
| 1325 | GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER)) |
| 1326 | << "Failed to find memory type for ColorBuffers."; |
| 1327 | } |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | VkEmulation* getGlobalVkEmulation() { |
| 1331 | if (sVkEmulation && !sVkEmulation->live) return nullptr; |
| 1332 | return sVkEmulation; |
| 1333 | } |
| 1334 | |
| 1335 | void teardownGlobalVkEmulation() { |
| 1336 | if (!sVkEmulation) return; |
| 1337 | |
| 1338 | // Don't try to tear down something that did not set up completely; too risky |
| 1339 | if (!sVkEmulation->live) return; |
| 1340 | |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 1341 | sVkEmulation->compositorVk.reset(); |
Kaiyi Li | 4ddffd9 | 2022-02-15 09:34:55 -0800 | [diff] [blame] | 1342 | sVkEmulation->displayVk.reset(); |
| 1343 | |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1344 | freeExternalMemoryLocked(sVkEmulation->dvk, &sVkEmulation->staging.memory); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1345 | |
Greg Schlomoff | 83e57ac | 2022-11-07 12:50:35 -0800 | [diff] [blame] | 1346 | sVkEmulation->dvk->vkDestroyBuffer(sVkEmulation->device, sVkEmulation->staging.buffer, nullptr); |
| 1347 | |
| 1348 | sVkEmulation->dvk->vkDestroyFence(sVkEmulation->device, sVkEmulation->commandBufferFence, |
| 1349 | nullptr); |
| 1350 | |
| 1351 | sVkEmulation->dvk->vkFreeCommandBuffers(sVkEmulation->device, sVkEmulation->commandPool, 1, |
| 1352 | &sVkEmulation->commandBuffer); |
| 1353 | |
| 1354 | sVkEmulation->dvk->vkDestroyCommandPool(sVkEmulation->device, sVkEmulation->commandPool, |
| 1355 | nullptr); |
| 1356 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1357 | sVkEmulation->ivk->vkDestroyDevice(sVkEmulation->device, nullptr); |
| 1358 | sVkEmulation->gvk->vkDestroyInstance(sVkEmulation->instance, nullptr); |
Lingfeng Yang | cf627ed | 2020-11-03 16:37:46 -0800 | [diff] [blame] | 1359 | |
Jason Macnak | 843b84b | 2023-05-19 14:19:25 -0700 | [diff] [blame] | 1360 | VkDecoderGlobalState::reset(); |
| 1361 | |
Lingfeng Yang | cf627ed | 2020-11-03 16:37:46 -0800 | [diff] [blame] | 1362 | sVkEmulation->live = false; |
| 1363 | delete sVkEmulation; |
| 1364 | sVkEmulation = nullptr; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1365 | } |
| 1366 | |
Jason Macnak | 32281f7 | 2022-10-12 13:00:36 -0700 | [diff] [blame] | 1367 | std::unique_ptr<gfxstream::DisplaySurface> createDisplaySurface(FBNativeWindowType window, |
Greg Schlomoff | 83e57ac | 2022-11-07 12:50:35 -0800 | [diff] [blame] | 1368 | uint32_t width, uint32_t height) { |
Jason Macnak | 32281f7 | 2022-10-12 13:00:36 -0700 | [diff] [blame] | 1369 | if (!sVkEmulation || !sVkEmulation->live) { |
| 1370 | return nullptr; |
| 1371 | } |
| 1372 | |
Greg Schlomoff | 83e57ac | 2022-11-07 12:50:35 -0800 | [diff] [blame] | 1373 | auto surfaceVk = DisplaySurfaceVk::create(*sVkEmulation->ivk, sVkEmulation->instance, window); |
Jason Macnak | 32281f7 | 2022-10-12 13:00:36 -0700 | [diff] [blame] | 1374 | if (!surfaceVk) { |
| 1375 | VK_COMMON_ERROR("Failed to create DisplaySurfaceVk."); |
| 1376 | return nullptr; |
| 1377 | } |
| 1378 | |
| 1379 | return std::make_unique<gfxstream::DisplaySurface>(width, height, std::move(surfaceVk)); |
| 1380 | } |
| 1381 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1382 | // Precondition: sVkEmulation has valid device support info |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1383 | bool allocExternalMemory(VulkanDispatch* vk, VkEmulation::ExternalMemoryInfo* info, |
Jason Macnak | cc53a20 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1384 | bool actuallyExternal, Optional<uint64_t> deviceAlignment, |
| 1385 | Optional<VkBuffer> bufferForDedicatedAllocation, |
| 1386 | Optional<VkImage> imageForDedicatedAllocation) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1387 | VkExportMemoryAllocateInfo exportAi = { |
Jason Macnak | cc53a20 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1388 | .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO, |
| 1389 | .pNext = nullptr, |
| 1390 | .handleTypes = VK_EXT_MEMORY_HANDLE_TYPE_BIT, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1391 | }; |
| 1392 | |
Jason Macnak | cc53a20 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1393 | VkMemoryDedicatedAllocateInfo dedicatedAllocInfo = { |
| 1394 | .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO, |
| 1395 | .pNext = nullptr, |
| 1396 | .image = VK_NULL_HANDLE, |
| 1397 | .buffer = VK_NULL_HANDLE, |
| 1398 | }; |
| 1399 | |
| 1400 | VkMemoryAllocateInfo allocInfo = { |
| 1401 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, |
| 1402 | .pNext = nullptr, |
| 1403 | .allocationSize = info->size, |
| 1404 | .memoryTypeIndex = info->typeIndex, |
| 1405 | }; |
| 1406 | |
| 1407 | auto allocInfoChain = vk_make_chain_iterator(&allocInfo); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1408 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1409 | if (sVkEmulation->deviceInfo.supportsExternalMemoryExport && actuallyExternal) { |
Jason Macnak | cc53a20 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1410 | vk_append_struct(&allocInfoChain, &exportAi); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1411 | } |
| 1412 | |
Jason Macnak | cc53a20 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1413 | if (bufferForDedicatedAllocation.hasValue() || imageForDedicatedAllocation.hasValue()) { |
| 1414 | info->dedicatedAllocation = true; |
| 1415 | if (bufferForDedicatedAllocation.hasValue()) { |
| 1416 | dedicatedAllocInfo.buffer = *bufferForDedicatedAllocation; |
| 1417 | } |
| 1418 | if (imageForDedicatedAllocation.hasValue()) { |
| 1419 | dedicatedAllocInfo.image = *imageForDedicatedAllocation; |
| 1420 | } |
| 1421 | vk_append_struct(&allocInfoChain, &dedicatedAllocInfo); |
| 1422 | } |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1423 | |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1424 | bool memoryAllocated = false; |
| 1425 | std::vector<VkDeviceMemory> allocationAttempts; |
| 1426 | constexpr size_t kMaxAllocationAttempts = 20u; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1427 | |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1428 | while (!memoryAllocated) { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1429 | VkResult allocRes = |
| 1430 | vk->vkAllocateMemory(sVkEmulation->device, &allocInfo, nullptr, &info->memory); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1431 | |
| 1432 | if (allocRes != VK_SUCCESS) { |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 1433 | // LOG(VERBOSE) << "allocExternalMemory: failed in vkAllocateMemory: " |
| 1434 | // << allocRes; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1435 | break; |
| 1436 | } |
| 1437 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1438 | if (sVkEmulation->deviceInfo.memProps.memoryTypes[info->typeIndex].propertyFlags & |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1439 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { |
Jason Macnak | cc53a20 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1440 | VkResult mapRes = vk->vkMapMemory(sVkEmulation->device, info->memory, 0, info->size, 0, |
| 1441 | &info->mappedPtr); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1442 | if (mapRes != VK_SUCCESS) { |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 1443 | // LOG(VERBOSE) << "allocExternalMemory: failed in vkMapMemory: " |
| 1444 | // << mapRes; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1445 | break; |
| 1446 | } |
| 1447 | } |
| 1448 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1449 | uint64_t mappedPtrPageOffset = reinterpret_cast<uint64_t>(info->mappedPtr) % kPageSize; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1450 | |
| 1451 | if ( // don't care about alignment (e.g. device-local memory) |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1452 | !deviceAlignment.hasValue() || |
| 1453 | // If device has an alignment requirement larger than current |
| 1454 | // host pointer alignment (i.e. the lowest 1 bit of mappedPtr), |
| 1455 | // the only possible way to make mappedPtr valid is to ensure |
| 1456 | // that it is already aligned to page. |
| 1457 | mappedPtrPageOffset == 0u || |
| 1458 | // If device has an alignment requirement smaller or equals to |
| 1459 | // current host pointer alignment, clients can set a offset |
| 1460 | // |kPageSize - mappedPtrPageOffset| in vkBindImageMemory to |
| 1461 | // make it aligned to page and compatible with device |
| 1462 | // requirements. |
| 1463 | (kPageSize - mappedPtrPageOffset) % deviceAlignment.value() == 0) { |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1464 | // allocation success. |
| 1465 | memoryAllocated = true; |
| 1466 | } else { |
| 1467 | allocationAttempts.push_back(info->memory); |
| 1468 | |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 1469 | // LOG(VERBOSE) << "allocExternalMemory: attempt #" |
| 1470 | // << allocationAttempts.size() |
| 1471 | // << " failed; deviceAlignment: " |
| 1472 | // << deviceAlignment.valueOr(0) |
| 1473 | // << " mappedPtrPageOffset: " << mappedPtrPageOffset; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1474 | |
| 1475 | if (allocationAttempts.size() >= kMaxAllocationAttempts) { |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 1476 | // LOG(VERBOSE) << "allocExternalMemory: unable to allocate" |
| 1477 | // << " memory with CPU mapped ptr aligned to page"; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1478 | break; |
| 1479 | } |
| 1480 | } |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1481 | } |
| 1482 | |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1483 | // clean up previous failed attempts |
| 1484 | for (const auto& mem : allocationAttempts) { |
| 1485 | vk->vkFreeMemory(sVkEmulation->device, mem, nullptr /* allocator */); |
| 1486 | } |
| 1487 | if (!memoryAllocated) { |
| 1488 | return false; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1489 | } |
| 1490 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1491 | if (!sVkEmulation->deviceInfo.supportsExternalMemoryExport || !actuallyExternal) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1492 | return true; |
| 1493 | } |
| 1494 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1495 | VkResult exportRes = VK_SUCCESS; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1496 | #ifdef _WIN32 |
| 1497 | VkMemoryGetWin32HandleInfoKHR getWin32HandleInfo = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1498 | VK_STRUCTURE_TYPE_MEMORY_GET_WIN32_HANDLE_INFO_KHR, |
| 1499 | 0, |
| 1500 | info->memory, |
| 1501 | VK_EXT_MEMORY_HANDLE_TYPE_BIT, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1502 | }; |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1503 | exportRes = sVkEmulation->deviceInfo.getMemoryHandleFunc( |
| 1504 | sVkEmulation->device, &getWin32HandleInfo, &info->externalHandle); |
| 1505 | #elif !defined(__QNX__) |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1506 | VkMemoryGetFdInfoKHR getFdInfo = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1507 | VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR, |
| 1508 | 0, |
| 1509 | info->memory, |
| 1510 | VK_EXT_MEMORY_HANDLE_TYPE_BIT, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1511 | }; |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1512 | exportRes = sVkEmulation->deviceInfo.getMemoryHandleFunc(sVkEmulation->device, &getFdInfo, |
| 1513 | &info->externalHandle); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1514 | #endif |
| 1515 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1516 | if (exportRes != VK_SUCCESS || VK_EXT_MEMORY_HANDLE_INVALID == info->externalHandle) { |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 1517 | // LOG(VERBOSE) << "allocExternalMemory: Failed to get external memory " |
| 1518 | // "native handle: " |
| 1519 | // << exportRes; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1520 | return false; |
| 1521 | } |
| 1522 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1523 | return true; |
| 1524 | } |
| 1525 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1526 | void freeExternalMemoryLocked(VulkanDispatch* vk, VkEmulation::ExternalMemoryInfo* info) { |
| 1527 | if (!info->memory) return; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1528 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1529 | if (sVkEmulation->deviceInfo.memProps.memoryTypes[info->typeIndex].propertyFlags & |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1530 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1531 | if (sVkEmulation->occupiedGpas.find(info->gpa) != sVkEmulation->occupiedGpas.end()) { |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1532 | sVkEmulation->occupiedGpas.erase(info->gpa); |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1533 | get_emugl_vm_operations().unmapUserBackedRam(info->gpa, info->sizeToPage); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1534 | info->gpa = 0u; |
| 1535 | } |
| 1536 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1537 | vk->vkUnmapMemory(sVkEmulation->device, info->memory); |
| 1538 | info->mappedPtr = nullptr; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1539 | info->pageAlignedHva = nullptr; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | vk->vkFreeMemory(sVkEmulation->device, info->memory, nullptr); |
| 1543 | |
| 1544 | info->memory = VK_NULL_HANDLE; |
| 1545 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1546 | if (info->externalHandle != VK_EXT_MEMORY_HANDLE_INVALID) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1547 | #ifdef _WIN32 |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1548 | CloseHandle(info->externalHandle); |
| 1549 | #elif !defined(__QNX__) |
| 1550 | close(info->externalHandle); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1551 | #endif |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1552 | info->externalHandle = VK_EXT_MEMORY_HANDLE_INVALID; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1553 | } |
| 1554 | } |
| 1555 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1556 | bool importExternalMemory(VulkanDispatch* vk, VkDevice targetDevice, |
| 1557 | const VkEmulation::ExternalMemoryInfo* info, VkDeviceMemory* out) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1558 | #ifdef _WIN32 |
| 1559 | VkImportMemoryWin32HandleInfoKHR importInfo = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1560 | VK_STRUCTURE_TYPE_IMPORT_MEMORY_WIN32_HANDLE_INFO_KHR, |
| 1561 | 0, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1562 | VK_EXT_MEMORY_HANDLE_TYPE_BIT, |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1563 | info->externalHandle, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1564 | 0, |
| 1565 | }; |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1566 | #elif defined(__QNX__) |
| 1567 | VkImportScreenBufferInfoQNX importInfo = { |
| 1568 | VK_STRUCTURE_TYPE_IMPORT_SCREEN_BUFFER_INFO_QNX, |
| 1569 | info->externalHandle, |
| 1570 | }; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1571 | #else |
| 1572 | VkImportMemoryFdInfoKHR importInfo = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1573 | VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR, |
| 1574 | 0, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1575 | VK_EXT_MEMORY_HANDLE_TYPE_BIT, |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1576 | dupExternalMemory(info->externalHandle), |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1577 | }; |
| 1578 | #endif |
| 1579 | VkMemoryAllocateInfo allocInfo = { |
| 1580 | VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, |
| 1581 | &importInfo, |
Jason Macnak | cc53a20 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1582 | info->size, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1583 | info->typeIndex, |
| 1584 | }; |
| 1585 | |
| 1586 | VkResult res = vk->vkAllocateMemory(targetDevice, &allocInfo, nullptr, out); |
| 1587 | |
| 1588 | if (res != VK_SUCCESS) { |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 1589 | // LOG(ERROR) << "importExternalMemory: Failed with " << res; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1590 | return false; |
| 1591 | } |
| 1592 | |
| 1593 | return true; |
| 1594 | } |
| 1595 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1596 | bool importExternalMemoryDedicatedImage(VulkanDispatch* vk, VkDevice targetDevice, |
| 1597 | const VkEmulation::ExternalMemoryInfo* info, VkImage image, |
| 1598 | VkDeviceMemory* out) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1599 | VkMemoryDedicatedAllocateInfo dedicatedInfo = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1600 | VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO, |
| 1601 | 0, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1602 | image, |
| 1603 | VK_NULL_HANDLE, |
| 1604 | }; |
| 1605 | |
| 1606 | #ifdef _WIN32 |
| 1607 | VkImportMemoryWin32HandleInfoKHR importInfo = { |
| 1608 | VK_STRUCTURE_TYPE_IMPORT_MEMORY_WIN32_HANDLE_INFO_KHR, |
| 1609 | &dedicatedInfo, |
| 1610 | VK_EXT_MEMORY_HANDLE_TYPE_BIT, |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1611 | info->externalHandle, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1612 | 0, |
| 1613 | }; |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1614 | #elif defined(__QNX__) |
| 1615 | VkImportScreenBufferInfoQNX importInfo = { |
| 1616 | VK_STRUCTURE_TYPE_IMPORT_SCREEN_BUFFER_INFO_QNX, |
| 1617 | &dedicatedInfo, |
| 1618 | info->externalHandle, |
| 1619 | }; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1620 | #else |
| 1621 | VkImportMemoryFdInfoKHR importInfo = { |
| 1622 | VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR, |
| 1623 | &dedicatedInfo, |
| 1624 | VK_EXT_MEMORY_HANDLE_TYPE_BIT, |
Lars Harrison | 70fe8f8 | 2024-02-20 16:13:53 -0800 | [diff] [blame] | 1625 | dupExternalMemory(info->externalHandle), |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1626 | }; |
| 1627 | #endif |
| 1628 | VkMemoryAllocateInfo allocInfo = { |
| 1629 | VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, |
| 1630 | &importInfo, |
| 1631 | info->size, |
| 1632 | info->typeIndex, |
| 1633 | }; |
| 1634 | |
| 1635 | VkResult res = vk->vkAllocateMemory(targetDevice, &allocInfo, nullptr, out); |
| 1636 | |
| 1637 | if (res != VK_SUCCESS) { |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 1638 | // LOG(ERROR) << "importExternalMemoryDedicatedImage: Failed with " << res; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1639 | return false; |
| 1640 | } |
| 1641 | |
| 1642 | return true; |
| 1643 | } |
| 1644 | |
Jason Macnak | 4c45edb | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1645 | // From ANGLE "src/common/angleutils.h" |
| 1646 | #define GL_BGR10_A2_ANGLEX 0x6AF9 |
| 1647 | |
Jason Macnak | 5fb2271 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1648 | static VkFormat glFormat2VkFormat(GLint internalFormat) { |
| 1649 | switch (internalFormat) { |
Jason Macnak | 6402a1b | 2022-06-02 15:29:05 -0700 | [diff] [blame] | 1650 | case GL_R8: |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1651 | case GL_LUMINANCE: |
| 1652 | return VK_FORMAT_R8_UNORM; |
| 1653 | case GL_RGB: |
| 1654 | case GL_RGB8: |
Yahan Zhou | 1f77da8 | 2023-07-25 15:41:10 -0700 | [diff] [blame] | 1655 | // b/281550953 |
| 1656 | // RGB8 is not supported on many vulkan drivers. |
| 1657 | // Try RGBA8 instead. |
Igor Chernyshev | ed39a8d | 2023-12-21 11:03:24 -0800 | [diff] [blame] | 1658 | // Note: copyImageData() performs channel conversion for this case. |
Yahan Zhou | 1f77da8 | 2023-07-25 15:41:10 -0700 | [diff] [blame] | 1659 | return VK_FORMAT_R8G8B8A8_UNORM; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1660 | case GL_RGB565: |
| 1661 | return VK_FORMAT_R5G6B5_UNORM_PACK16; |
| 1662 | case GL_RGB16F: |
| 1663 | return VK_FORMAT_R16G16B16_SFLOAT; |
| 1664 | case GL_RGBA: |
| 1665 | case GL_RGBA8: |
| 1666 | return VK_FORMAT_R8G8B8A8_UNORM; |
| 1667 | case GL_RGB5_A1_OES: |
| 1668 | return VK_FORMAT_A1R5G5B5_UNORM_PACK16; |
| 1669 | case GL_RGBA4_OES: |
| 1670 | return VK_FORMAT_R4G4B4A4_UNORM_PACK16; |
| 1671 | case GL_RGB10_A2: |
| 1672 | case GL_UNSIGNED_INT_10_10_10_2_OES: |
| 1673 | return VK_FORMAT_A2R10G10B10_UNORM_PACK32; |
Yilong Li | 7179d97 | 2021-04-09 09:30:39 -0700 | [diff] [blame] | 1674 | case GL_BGR10_A2_ANGLEX: |
| 1675 | return VK_FORMAT_A2B10G10R10_UNORM_PACK32; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1676 | case GL_RGBA16F: |
| 1677 | return VK_FORMAT_R16G16B16A16_SFLOAT; |
| 1678 | case GL_BGRA_EXT: |
| 1679 | case GL_BGRA8_EXT: |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1680 | return VK_FORMAT_B8G8R8A8_UNORM; |
Kaiyi Li | 2209e38 | 2022-08-30 12:47:19 -0700 | [diff] [blame] | 1681 | case GL_R16_EXT: |
| 1682 | return VK_FORMAT_R16_UNORM; |
| 1683 | case GL_RG8_EXT: |
| 1684 | return VK_FORMAT_R8G8_UNORM; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1685 | default: |
Jason Macnak | 6402a1b | 2022-06-02 15:29:05 -0700 | [diff] [blame] | 1686 | VK_COMMON_ERROR("Unhandled format %d, falling back to VK_FORMAT_R8G8B8A8_UNORM", |
Jason Macnak | 5fb2271 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1687 | internalFormat); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1688 | return VK_FORMAT_R8G8B8A8_UNORM; |
| 1689 | } |
| 1690 | }; |
| 1691 | |
Jason Macnak | 9e9c68e | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1692 | static bool isFormatVulkanCompatible(GLenum internalFormat) { |
| 1693 | VkFormat vkFormat = glFormat2VkFormat(internalFormat); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1694 | |
| 1695 | for (const auto& supportInfo : sVkEmulation->imageSupportInfo) { |
| 1696 | if (supportInfo.format == vkFormat && supportInfo.supported) { |
| 1697 | return true; |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | return false; |
| 1702 | } |
| 1703 | |
Jason Macnak | 5fb2271 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1704 | bool isColorBufferExportedToGl(uint32_t colorBufferHandle, bool* exported) { |
| 1705 | if (!sVkEmulation || !sVkEmulation->live) { |
| 1706 | GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER)) << "Vulkan emulation not available."; |
| 1707 | } |
| 1708 | |
| 1709 | AutoLock lock(sVkEmulationLock); |
| 1710 | |
| 1711 | auto info = android::base::find(sVkEmulation->colorBuffers, colorBufferHandle); |
| 1712 | if (!info) { |
| 1713 | return false; |
| 1714 | } |
| 1715 | |
| 1716 | *exported = info->glExported; |
| 1717 | return true; |
| 1718 | } |
| 1719 | |
Jason Macnak | 12c1c39 | 2023-03-14 15:15:38 -0700 | [diff] [blame] | 1720 | bool getColorBufferAllocationInfoLocked(uint32_t colorBufferHandle, VkDeviceSize* outSize, |
| 1721 | uint32_t* outMemoryTypeIndex, |
| 1722 | bool* outMemoryIsDedicatedAlloc, void** outMappedPtr) { |
Jason Macnak | 5fb2271 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1723 | auto info = android::base::find(sVkEmulation->colorBuffers, colorBufferHandle); |
| 1724 | if (!info) { |
| 1725 | return false; |
| 1726 | } |
| 1727 | |
| 1728 | if (outSize) { |
| 1729 | *outSize = info->memory.size; |
| 1730 | } |
| 1731 | |
| 1732 | if (outMemoryTypeIndex) { |
| 1733 | *outMemoryTypeIndex = info->memory.typeIndex; |
| 1734 | } |
| 1735 | |
Jason Macnak | cc53a20 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1736 | if (outMemoryIsDedicatedAlloc) { |
| 1737 | *outMemoryIsDedicatedAlloc = info->memory.dedicatedAllocation; |
| 1738 | } |
| 1739 | |
Jason Macnak | 5fb2271 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1740 | if (outMappedPtr) { |
| 1741 | *outMappedPtr = info->memory.mappedPtr; |
| 1742 | } |
| 1743 | |
| 1744 | return true; |
| 1745 | } |
| 1746 | |
Jason Macnak | 12c1c39 | 2023-03-14 15:15:38 -0700 | [diff] [blame] | 1747 | bool getColorBufferAllocationInfo(uint32_t colorBufferHandle, VkDeviceSize* outSize, |
| 1748 | uint32_t* outMemoryTypeIndex, bool* outMemoryIsDedicatedAlloc, |
| 1749 | void** outMappedPtr) { |
| 1750 | if (!sVkEmulation || !sVkEmulation->live) { |
| 1751 | GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER)) << "Vulkan emulation not available."; |
| 1752 | } |
| 1753 | |
| 1754 | AutoLock lock(sVkEmulationLock); |
| 1755 | return getColorBufferAllocationInfoLocked(colorBufferHandle, outSize, outMemoryTypeIndex, |
| 1756 | outMemoryIsDedicatedAlloc, outMappedPtr); |
| 1757 | } |
| 1758 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1759 | static uint32_t lastGoodTypeIndex(uint32_t indices) { |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1760 | for (int32_t i = 31; i >= 0; --i) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1761 | if (indices & (1 << i)) { |
| 1762 | return i; |
| 1763 | } |
| 1764 | } |
| 1765 | return 0; |
| 1766 | } |
| 1767 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1768 | static uint32_t lastGoodTypeIndexWithMemoryProperties(uint32_t indices, |
| 1769 | VkMemoryPropertyFlags memoryProperty) { |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1770 | for (int32_t i = 31; i >= 0; --i) { |
| 1771 | if ((indices & (1u << i)) && |
| 1772 | (!memoryProperty || |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1773 | (sVkEmulation->deviceInfo.memProps.memoryTypes[i].propertyFlags & memoryProperty))) { |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1774 | return i; |
| 1775 | } |
| 1776 | } |
| 1777 | return 0; |
| 1778 | } |
| 1779 | |
Kaiyi Li | cd9e14d | 2021-12-09 22:08:51 +0000 | [diff] [blame] | 1780 | // pNext, sharingMode, queueFamilyIndexCount, pQueueFamilyIndices, and initialLayout won't be |
| 1781 | // filled. |
| 1782 | static std::unique_ptr<VkImageCreateInfo> generateColorBufferVkImageCreateInfo_locked( |
| 1783 | VkFormat format, uint32_t width, uint32_t height, VkImageTiling tiling) { |
Kaiyi Li | 71874cc | 2022-08-22 08:40:46 -0700 | [diff] [blame] | 1784 | const VkEmulation::ImageSupportInfo* maybeImageSupportInfo = nullptr; |
Kaiyi Li | cd9e14d | 2021-12-09 22:08:51 +0000 | [diff] [blame] | 1785 | for (const auto& supportInfo : sVkEmulation->imageSupportInfo) { |
| 1786 | if (supportInfo.format == format && supportInfo.supported) { |
Kaiyi Li | 71874cc | 2022-08-22 08:40:46 -0700 | [diff] [blame] | 1787 | maybeImageSupportInfo = &supportInfo; |
Kaiyi Li | cd9e14d | 2021-12-09 22:08:51 +0000 | [diff] [blame] | 1788 | break; |
| 1789 | } |
| 1790 | } |
Kaiyi Li | 71874cc | 2022-08-22 08:40:46 -0700 | [diff] [blame] | 1791 | if (!maybeImageSupportInfo) { |
Kaiyi Li | cd9e14d | 2021-12-09 22:08:51 +0000 | [diff] [blame] | 1792 | ERR("Format %s is not supported.", string_VkFormat(format)); |
| 1793 | return nullptr; |
| 1794 | } |
Kaiyi Li | 71874cc | 2022-08-22 08:40:46 -0700 | [diff] [blame] | 1795 | const VkEmulation::ImageSupportInfo& imageSupportInfo = *maybeImageSupportInfo; |
| 1796 | const VkFormatProperties& formatProperties = imageSupportInfo.formatProps2.formatProperties; |
Kaiyi Li | cd9e14d | 2021-12-09 22:08:51 +0000 | [diff] [blame] | 1797 | |
| 1798 | constexpr std::pair<VkFormatFeatureFlags, VkImageUsageFlags> formatUsagePairs[] = { |
Jason Macnak | b9055d7 | 2022-04-06 10:40:14 -0700 | [diff] [blame] | 1799 | {VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT, |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1800 | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT}, |
| 1801 | {VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT, VK_IMAGE_USAGE_SAMPLED_BIT}, |
| 1802 | {VK_FORMAT_FEATURE_TRANSFER_SRC_BIT, VK_IMAGE_USAGE_TRANSFER_SRC_BIT}, |
| 1803 | {VK_FORMAT_FEATURE_TRANSFER_DST_BIT, VK_IMAGE_USAGE_TRANSFER_DST_BIT}, |
| 1804 | {VK_FORMAT_FEATURE_BLIT_SRC_BIT, VK_IMAGE_USAGE_TRANSFER_SRC_BIT}, |
Kaiyi Li | cd9e14d | 2021-12-09 22:08:51 +0000 | [diff] [blame] | 1805 | }; |
| 1806 | VkFormatFeatureFlags tilingFeatures = (tiling == VK_IMAGE_TILING_OPTIMAL) |
| 1807 | ? formatProperties.optimalTilingFeatures |
| 1808 | : formatProperties.linearTilingFeatures; |
| 1809 | |
| 1810 | VkImageUsageFlags usage = 0; |
| 1811 | for (const auto& formatUsage : formatUsagePairs) { |
| 1812 | usage |= (tilingFeatures & formatUsage.first) ? formatUsage.second : 0u; |
| 1813 | } |
| 1814 | |
| 1815 | return std::make_unique<VkImageCreateInfo>(VkImageCreateInfo{ |
| 1816 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 1817 | // The caller is responsible to fill pNext. |
| 1818 | .pNext = nullptr, |
Kaiyi Li | 71874cc | 2022-08-22 08:40:46 -0700 | [diff] [blame] | 1819 | .flags = imageSupportInfo.createFlags, |
Kaiyi Li | cd9e14d | 2021-12-09 22:08:51 +0000 | [diff] [blame] | 1820 | .imageType = VK_IMAGE_TYPE_2D, |
| 1821 | .format = format, |
| 1822 | .extent = |
| 1823 | { |
| 1824 | .width = width, |
| 1825 | .height = height, |
| 1826 | .depth = 1, |
| 1827 | }, |
| 1828 | .mipLevels = 1, |
| 1829 | .arrayLayers = 1, |
| 1830 | .samples = VK_SAMPLE_COUNT_1_BIT, |
| 1831 | .tiling = tiling, |
| 1832 | .usage = usage, |
| 1833 | // The caller is responsible to fill sharingMode. |
| 1834 | .sharingMode = VK_SHARING_MODE_MAX_ENUM, |
| 1835 | // The caller is responsible to fill queueFamilyIndexCount. |
| 1836 | .queueFamilyIndexCount = 0, |
| 1837 | // The caller is responsible to fill pQueueFamilyIndices. |
| 1838 | .pQueueFamilyIndices = nullptr, |
| 1839 | // The caller is responsible to fill initialLayout. |
| 1840 | .initialLayout = VK_IMAGE_LAYOUT_MAX_ENUM, |
| 1841 | }); |
| 1842 | } |
| 1843 | |
| 1844 | std::unique_ptr<VkImageCreateInfo> generateColorBufferVkImageCreateInfo(VkFormat format, |
| 1845 | uint32_t width, |
| 1846 | uint32_t height, |
| 1847 | VkImageTiling tiling) { |
| 1848 | if (!sVkEmulation || !sVkEmulation->live) { |
| 1849 | GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER)) << "Host Vulkan device lost"; |
| 1850 | } |
| 1851 | AutoLock lock(sVkEmulationLock); |
| 1852 | return generateColorBufferVkImageCreateInfo_locked(format, width, height, tiling); |
| 1853 | } |
| 1854 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1855 | static bool updateExternalMemoryInfo(VK_EXT_MEMORY_HANDLE extMemHandle, |
| 1856 | const VkMemoryRequirements* pMemReqs, |
| 1857 | VkEmulation::ExternalMemoryInfo* pInfo) { |
| 1858 | // Set externalHandle on the output info |
| 1859 | pInfo->externalHandle = extMemHandle; |
| 1860 | |
| 1861 | #if defined(__QNX__) |
| 1862 | VkScreenBufferPropertiesQNX screenBufferProps = { |
| 1863 | VK_STRUCTURE_TYPE_SCREEN_BUFFER_PROPERTIES_QNX, |
| 1864 | 0, |
| 1865 | }; |
| 1866 | auto vk = sVkEmulation->dvk; |
| 1867 | VkResult queryRes = |
| 1868 | vk->vkGetScreenBufferPropertiesQNX(sVkEmulation->device, extMemHandle, &screenBufferProps); |
| 1869 | if (VK_SUCCESS != queryRes) { |
| 1870 | VK_COMMON_ERROR("Failed to get QNX Screen Buffer properties, VK error: %d", queryRes); |
| 1871 | return false; |
| 1872 | } |
| 1873 | if (!((1 << pInfo->typeIndex) & screenBufferProps.memoryTypeBits)) { |
| 1874 | VK_COMMON_ERROR("QNX Screen buffer can not be imported to memory (typeIndex=%d): %d", |
| 1875 | pInfo->typeIndex); |
| 1876 | return false; |
| 1877 | } |
| 1878 | if (screenBufferProps.allocationSize < pMemReqs->size) { |
| 1879 | VK_COMMON_ERROR( |
| 1880 | "QNX Screen buffer allocationSize (0x%lx) is not large enough for ColorBuffer image " |
| 1881 | "size requirements (0x%lx)", |
| 1882 | screenBufferProps.allocationSize, pMemReqs->size); |
| 1883 | return false; |
| 1884 | } |
| 1885 | // Use the actual allocationSize for VkDeviceMemory object creation |
| 1886 | pInfo->size = screenBufferProps.allocationSize; |
| 1887 | #endif |
| 1888 | |
| 1889 | return true; |
| 1890 | } |
| 1891 | |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1892 | // TODO(liyl): Currently we can only specify required memoryProperty |
| 1893 | // for a color buffer. |
| 1894 | // |
| 1895 | // Ideally we would like to specify a memory type index directly from |
| 1896 | // localAllocInfo.memoryTypeIndex when allocating color buffers in |
| 1897 | // vkAllocateMemory(). But this type index mechanism breaks "Modify the |
| 1898 | // allocation size and type index to suit the resulting image memory |
| 1899 | // size." which seems to be needed to keep the Android/Fuchsia guest |
| 1900 | // memory type index consistent across guest allocations, and without |
| 1901 | // which those guests might end up import allocating from a color buffer |
| 1902 | // with mismatched type indices. |
| 1903 | // |
| 1904 | // We should make it so the guest can only allocate external images/ |
| 1905 | // buffers of one type index for image and one type index for buffer |
| 1906 | // to begin with, via filtering from the host. |
Jason Macnak | 12c1c39 | 2023-03-14 15:15:38 -0700 | [diff] [blame] | 1907 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1908 | bool initializeVkColorBufferLocked( |
| 1909 | uint32_t colorBufferHandle, VK_EXT_MEMORY_HANDLE extMemHandle = VK_EXT_MEMORY_HANDLE_INVALID) { |
| 1910 | auto infoPtr = android::base::find(sVkEmulation->colorBuffers, colorBufferHandle); |
| 1911 | // Not initialized |
| 1912 | if (!infoPtr) { |
Jason Macnak | 5cd7f79 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1913 | return false; |
| 1914 | } |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1915 | // Already initialized Vulkan memory and other related Vulkan objects |
| 1916 | if (infoPtr->initialized) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1917 | return true; |
| 1918 | } |
| 1919 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1920 | if (!isFormatVulkanCompatible(infoPtr->internalFormat)) { |
| 1921 | VK_COMMON_VERBOSE("Failed to create Vk ColorBuffer: format:%d not compatible.", |
| 1922 | infoPtr->internalFormat); |
| 1923 | return false; |
| 1924 | } |
| 1925 | |
| 1926 | if ((VK_EXT_MEMORY_HANDLE_INVALID != extMemHandle) && |
| 1927 | (!sVkEmulation->deviceInfo.supportsExternalMemoryImport)) { |
| 1928 | VK_COMMON_ERROR( |
| 1929 | "Failed to initialize Vk ColorBuffer -- extMemHandle provided, but device does " |
| 1930 | "not support externalMemoryImport"); |
| 1931 | return false; |
| 1932 | } |
| 1933 | |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1934 | VkFormat vkFormat; |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1935 | bool glCompatible = (infoPtr->frameworkFormat == FRAMEWORK_FORMAT_GL_COMPATIBLE); |
| 1936 | switch (infoPtr->frameworkFormat) { |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1937 | case FrameworkFormat::FRAMEWORK_FORMAT_GL_COMPATIBLE: |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1938 | vkFormat = glFormat2VkFormat(infoPtr->internalFormat); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1939 | break; |
| 1940 | case FrameworkFormat::FRAMEWORK_FORMAT_NV12: |
| 1941 | vkFormat = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM; |
| 1942 | break; |
Jason Macnak | edf2858 | 2022-06-02 17:46:37 -0700 | [diff] [blame] | 1943 | case FrameworkFormat::FRAMEWORK_FORMAT_P010: |
| 1944 | vkFormat = VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16; |
| 1945 | break; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1946 | case FrameworkFormat::FRAMEWORK_FORMAT_YV12: |
| 1947 | case FrameworkFormat::FRAMEWORK_FORMAT_YUV_420_888: |
| 1948 | vkFormat = VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM; |
| 1949 | break; |
| 1950 | default: |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1951 | VK_COMMON_ERROR("WARNING: unhandled framework format %d\n", infoPtr->frameworkFormat); |
| 1952 | vkFormat = glFormat2VkFormat(infoPtr->internalFormat); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1953 | break; |
| 1954 | } |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1955 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1956 | VkImageTiling tiling = (infoPtr->memoryProperty & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) |
Kaiyi Li | cd9e14d | 2021-12-09 22:08:51 +0000 | [diff] [blame] | 1957 | ? VK_IMAGE_TILING_LINEAR |
| 1958 | : VK_IMAGE_TILING_OPTIMAL; |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1959 | std::unique_ptr<VkImageCreateInfo> imageCi = generateColorBufferVkImageCreateInfo_locked( |
| 1960 | vkFormat, infoPtr->width, infoPtr->height, tiling); |
Kaiyi Li | cd9e14d | 2021-12-09 22:08:51 +0000 | [diff] [blame] | 1961 | // pNext will be filled later. |
JP Cottin | 3a11fc3 | 2023-03-22 21:30:22 +0000 | [diff] [blame] | 1962 | if (imageCi == nullptr) { |
| 1963 | // it can happen if the format is not supported |
| 1964 | return false; |
| 1965 | } |
Kaiyi Li | cd9e14d | 2021-12-09 22:08:51 +0000 | [diff] [blame] | 1966 | imageCi->sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 1967 | imageCi->queueFamilyIndexCount = 0; |
| 1968 | imageCi->pQueueFamilyIndices = nullptr; |
| 1969 | imageCi->initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 1970 | |
Aaron Ruby | 7f5aaab | 2023-12-05 17:21:34 -0500 | [diff] [blame] | 1971 | auto imageCiChain = vk_make_chain_iterator(imageCi.get()); |
| 1972 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1973 | // Create the image. If external memory is supported, make it external. |
| 1974 | VkExternalMemoryImageCreateInfo extImageCi = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 1975 | VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO, |
| 1976 | 0, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1977 | VK_EXT_MEMORY_HANDLE_TYPE_BIT, |
| 1978 | }; |
| 1979 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 1980 | if (sVkEmulation->deviceInfo.supportsExternalMemoryImport || |
| 1981 | sVkEmulation->deviceInfo.supportsExternalMemoryExport) { |
Aaron Ruby | 7f5aaab | 2023-12-05 17:21:34 -0500 | [diff] [blame] | 1982 | vk_append_struct(&imageCiChain, &extImageCi); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1983 | } |
| 1984 | |
Aaron Ruby | 7f5aaab | 2023-12-05 17:21:34 -0500 | [diff] [blame] | 1985 | #if defined(__QNX__) |
| 1986 | VkExternalFormatQNX externalFormatQnx = { |
| 1987 | .sType = VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_QNX, |
| 1988 | .pNext = NULL, |
| 1989 | .externalFormat = 0, /* Do not override screen format */ |
| 1990 | }; |
| 1991 | if (VK_EXT_MEMORY_HANDLE_INVALID != extMemHandle) { |
| 1992 | vk_append_struct(&imageCiChain, &externalFormatQnx); |
| 1993 | /* Maintain linear tiling on QNX for downstream display controller interaction */ |
| 1994 | imageCi->tiling = VK_IMAGE_TILING_LINEAR; |
| 1995 | } |
| 1996 | #endif |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 1997 | |
Jason Macnak | 5fb2271 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 1998 | auto vk = sVkEmulation->dvk; |
| 1999 | |
Kaiyi Li | cd9e14d | 2021-12-09 22:08:51 +0000 | [diff] [blame] | 2000 | VkResult createRes = |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2001 | vk->vkCreateImage(sVkEmulation->device, imageCi.get(), nullptr, &infoPtr->image); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2002 | if (createRes != VK_SUCCESS) { |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 2003 | // LOG(VERBOSE) << "Failed to create Vulkan image for ColorBuffer " |
| 2004 | // << colorBufferHandle; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2005 | return false; |
| 2006 | } |
| 2007 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2008 | bool useDedicated = |
| 2009 | sVkEmulation->useDedicatedAllocations || (VK_EXT_MEMORY_HANDLE_INVALID != extMemHandle); |
Andrew Woloszyn | f7135ef | 2023-02-23 16:20:35 -0500 | [diff] [blame] | 2010 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2011 | infoPtr->imageCreateInfoShallow = vk_make_orphan_copy(*imageCi); |
| 2012 | infoPtr->currentLayout = infoPtr->imageCreateInfoShallow.initialLayout; |
| 2013 | infoPtr->currentQueueFamilyIndex = sVkEmulation->queueFamilyIndex; |
Jason Macnak | b112ade | 2022-05-06 13:42:07 -0700 | [diff] [blame] | 2014 | |
Jason Macnak | 04b1904 | 2023-02-28 15:47:05 -0800 | [diff] [blame] | 2015 | if (!useDedicated && vk->vkGetImageMemoryRequirements2KHR) { |
Andrew Woloszyn | f7135ef | 2023-02-23 16:20:35 -0500 | [diff] [blame] | 2016 | VkMemoryDedicatedRequirements dedicated_reqs{ |
| 2017 | VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS, nullptr}; |
| 2018 | VkMemoryRequirements2 reqs{VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2, &dedicated_reqs}; |
| 2019 | |
| 2020 | VkImageMemoryRequirementsInfo2 info{VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2, |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2021 | nullptr, infoPtr->image}; |
Andrew Woloszyn | f7135ef | 2023-02-23 16:20:35 -0500 | [diff] [blame] | 2022 | vk->vkGetImageMemoryRequirements2KHR(sVkEmulation->device, &info, &reqs); |
Jason Macnak | 04b1904 | 2023-02-28 15:47:05 -0800 | [diff] [blame] | 2023 | useDedicated = dedicated_reqs.requiresDedicatedAllocation; |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2024 | infoPtr->memReqs = reqs.memoryRequirements; |
Andrew Woloszyn | f7135ef | 2023-02-23 16:20:35 -0500 | [diff] [blame] | 2025 | } else { |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2026 | vk->vkGetImageMemoryRequirements(sVkEmulation->device, infoPtr->image, &infoPtr->memReqs); |
Andrew Woloszyn | f7135ef | 2023-02-23 16:20:35 -0500 | [diff] [blame] | 2027 | } |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2028 | |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2029 | // Currently we only care about two memory properties: DEVICE_LOCAL |
| 2030 | // and HOST_VISIBLE; other memory properties specified in |
| 2031 | // rcSetColorBufferVulkanMode2() call will be ignored for now. |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2032 | infoPtr->memoryProperty = infoPtr->memoryProperty & (VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | |
| 2033 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2034 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2035 | infoPtr->memory.size = infoPtr->memReqs.size; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2036 | |
| 2037 | // Determine memory type. |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2038 | if (infoPtr->memoryProperty) { |
| 2039 | infoPtr->memory.typeIndex = lastGoodTypeIndexWithMemoryProperties( |
| 2040 | infoPtr->memReqs.memoryTypeBits, infoPtr->memoryProperty); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2041 | } else { |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2042 | infoPtr->memory.typeIndex = lastGoodTypeIndex(infoPtr->memReqs.memoryTypeBits); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2043 | } |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2044 | |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 2045 | // LOG(VERBOSE) << "ColorBuffer " << colorBufferHandle |
| 2046 | // << ", allocation size and type index: " << res.memory.size |
| 2047 | // << ", " << res.memory.typeIndex |
| 2048 | // << ", allocated memory property: " |
| 2049 | // << sVkEmulation->deviceInfo.memProps |
| 2050 | // .memoryTypes[res.memory.typeIndex] |
| 2051 | // .propertyFlags |
| 2052 | // << ", requested memory property: " << memoryProperty; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2053 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2054 | Optional<VkImage> dedicatedImage = useDedicated ? Optional<VkImage>(infoPtr->image) : kNullopt; |
| 2055 | if (VK_EXT_MEMORY_HANDLE_INVALID != extMemHandle) { |
| 2056 | if (!updateExternalMemoryInfo(extMemHandle, &infoPtr->memReqs, &infoPtr->memory)) { |
| 2057 | VK_COMMON_ERROR( |
| 2058 | "Failed to update external memory info for ColorBuffer: %d\n", |
| 2059 | colorBufferHandle); |
| 2060 | return false; |
| 2061 | } |
| 2062 | if (!importExternalMemoryDedicatedImage(vk, sVkEmulation->device, &infoPtr->memory, |
| 2063 | *dedicatedImage, &infoPtr->memory.memory)) { |
| 2064 | VK_COMMON_ERROR( |
| 2065 | "Failed to import external memory with dedicated Image for colorBuffer: %d\n", |
| 2066 | colorBufferHandle); |
| 2067 | return false; |
| 2068 | } |
| 2069 | } else { |
| 2070 | bool isHostVisible = infoPtr->memoryProperty & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; |
| 2071 | Optional<uint64_t> deviceAlignment = |
| 2072 | isHostVisible ? Optional<uint64_t>(infoPtr->memReqs.alignment) : kNullopt; |
| 2073 | bool allocRes = allocExternalMemory(vk, &infoPtr->memory, true /*actuallyExternal*/, |
| 2074 | deviceAlignment, kNullopt, dedicatedImage); |
| 2075 | if (!allocRes) { |
| 2076 | // LOG(VERBOSE) << "Failed to allocate ColorBuffer with Vulkan backing."; |
| 2077 | return false; |
| 2078 | } |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2079 | } |
| 2080 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2081 | infoPtr->memory.pageOffset = reinterpret_cast<uint64_t>(infoPtr->memory.mappedPtr) % kPageSize; |
| 2082 | infoPtr->memory.bindOffset = |
| 2083 | infoPtr->memory.pageOffset ? kPageSize - infoPtr->memory.pageOffset : 0u; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2084 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2085 | VkResult bindImageMemoryRes = vk->vkBindImageMemory( |
| 2086 | sVkEmulation->device, infoPtr->image, infoPtr->memory.memory, infoPtr->memory.bindOffset); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2087 | |
| 2088 | if (bindImageMemoryRes != VK_SUCCESS) { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2089 | fprintf(stderr, "%s: Failed to bind image memory. %d\n", __func__, bindImageMemoryRes); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2090 | return false; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2091 | } |
| 2092 | |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 2093 | const VkImageViewCreateInfo imageViewCi = { |
| 2094 | .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
| 2095 | .pNext = nullptr, |
| 2096 | .flags = 0, |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2097 | .image = infoPtr->image, |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 2098 | .viewType = VK_IMAGE_VIEW_TYPE_2D, |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2099 | .format = infoPtr->imageCreateInfoShallow.format, |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 2100 | .components = |
| 2101 | { |
| 2102 | .r = VK_COMPONENT_SWIZZLE_IDENTITY, |
| 2103 | .g = VK_COMPONENT_SWIZZLE_IDENTITY, |
| 2104 | .b = VK_COMPONENT_SWIZZLE_IDENTITY, |
| 2105 | .a = VK_COMPONENT_SWIZZLE_IDENTITY, |
| 2106 | }, |
| 2107 | .subresourceRange = |
| 2108 | { |
| 2109 | .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, |
| 2110 | .baseMipLevel = 0, |
| 2111 | .levelCount = 1, |
| 2112 | .baseArrayLayer = 0, |
| 2113 | .layerCount = 1, |
| 2114 | }, |
| 2115 | }; |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2116 | createRes = |
| 2117 | vk->vkCreateImageView(sVkEmulation->device, &imageViewCi, nullptr, &infoPtr->imageView); |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 2118 | if (createRes != VK_SUCCESS) { |
| 2119 | // LOG(VERBOSE) << "Failed to create Vulkan image for ColorBuffer " |
| 2120 | // << colorBufferHandle; |
| 2121 | return false; |
| 2122 | } |
| 2123 | |
Kaiyi Li | a9bf367 | 2022-09-08 18:47:43 -0700 | [diff] [blame] | 2124 | #if defined(VK_MVK_moltenvk) && defined(__APPLE__) |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2125 | if (sVkEmulation->instanceSupportsMoltenVK) { |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2126 | sVkEmulation->getMTLTextureFunc(infoPtr->image, &infoPtr->mtlTexture); |
| 2127 | if (!infoPtr->mtlTexture) { |
Doug Horn | efb1796 | 2021-01-07 16:17:34 -0800 | [diff] [blame] | 2128 | fprintf(stderr, "%s: Failed to get MTLTexture.\n", __func__); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2129 | } |
Doug Horn | efb1796 | 2021-01-07 16:17:34 -0800 | [diff] [blame] | 2130 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2131 | CFRetain(infoPtr->mtlTexture); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2132 | } |
Kaiyi Li | a9bf367 | 2022-09-08 18:47:43 -0700 | [diff] [blame] | 2133 | #endif |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2134 | |
Jason Macnak | 5c76bdb | 2024-01-30 09:07:34 -0800 | [diff] [blame] | 2135 | sVkEmulation->debugUtilsHelper.addDebugLabel(infoPtr->image, "ColorBuffer:%d", |
| 2136 | colorBufferHandle); |
| 2137 | sVkEmulation->debugUtilsHelper.addDebugLabel(infoPtr->imageView, "ColorBuffer:%d", |
| 2138 | colorBufferHandle); |
| 2139 | sVkEmulation->debugUtilsHelper.addDebugLabel(infoPtr->memory.memory, "ColorBuffer:%d", |
| 2140 | colorBufferHandle); |
| 2141 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2142 | infoPtr->initialized = true; |
| 2143 | |
| 2144 | return true; |
| 2145 | } |
| 2146 | |
Igor Chernyshev | ed39a8d | 2023-12-21 11:03:24 -0800 | [diff] [blame] | 2147 | static bool createVkColorBufferLocked(uint32_t width, uint32_t height, GLenum internalFormat, |
| 2148 | FrameworkFormat frameworkFormat, uint32_t colorBufferHandle, |
| 2149 | bool vulkanOnly, uint32_t memoryProperty) { |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2150 | auto infoPtr = android::base::find(sVkEmulation->colorBuffers, colorBufferHandle); |
| 2151 | // Already initialized |
| 2152 | if (infoPtr) { |
| 2153 | return true; |
| 2154 | } |
| 2155 | |
| 2156 | VkEmulation::ColorBufferInfo res; |
| 2157 | |
| 2158 | res.handle = colorBufferHandle; |
| 2159 | res.width = width; |
| 2160 | res.height = height; |
| 2161 | res.memoryProperty = memoryProperty; |
| 2162 | res.internalFormat = internalFormat; |
| 2163 | res.frameworkFormat = frameworkFormat; |
| 2164 | res.frameworkStride = 0; |
| 2165 | |
Kaiyi Li | 2cb8a1f | 2022-08-30 15:55:15 -0700 | [diff] [blame] | 2166 | if (vulkanOnly) { |
| 2167 | res.vulkanMode = VkEmulation::VulkanMode::VulkanOnly; |
| 2168 | } |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2169 | |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 2170 | sVkEmulation->colorBuffers[colorBufferHandle] = res; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2171 | return true; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2172 | } |
| 2173 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2174 | bool createVkColorBuffer(uint32_t width, uint32_t height, GLenum internalFormat, |
| 2175 | FrameworkFormat frameworkFormat, uint32_t colorBufferHandle, |
| 2176 | bool vulkanOnly, uint32_t memoryProperty) { |
Jason Macnak | 12c1c39 | 2023-03-14 15:15:38 -0700 | [diff] [blame] | 2177 | if (!sVkEmulation || !sVkEmulation->live) { |
| 2178 | GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER)) << "VkEmulation not available."; |
| 2179 | } |
| 2180 | |
| 2181 | AutoLock lock(sVkEmulationLock); |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2182 | if (!createVkColorBufferLocked(width, height, internalFormat, frameworkFormat, |
| 2183 | colorBufferHandle, vulkanOnly, memoryProperty)) { |
| 2184 | return false; |
| 2185 | } |
| 2186 | |
| 2187 | const auto& deviceInfo = sVkEmulation->deviceInfo; |
| 2188 | if (!deviceInfo.supportsExternalMemoryExport && deviceInfo.supportsExternalMemoryImport) { |
| 2189 | /* Returns, deferring initialization of the Vulkan components themselves. |
| 2190 | * Platforms that support import but not export of external memory must |
| 2191 | * use importExtMemoryHandleToVkColorBuffer(). Otherwise, the colorBuffer |
| 2192 | * memory can not be externalized. |
| 2193 | */ |
| 2194 | return true; |
| 2195 | } |
| 2196 | |
| 2197 | return initializeVkColorBufferLocked(colorBufferHandle); |
Jason Macnak | 12c1c39 | 2023-03-14 15:15:38 -0700 | [diff] [blame] | 2198 | } |
| 2199 | |
Jason Macnak | 9e9c68e | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2200 | std::optional<VkColorBufferMemoryExport> exportColorBufferMemory(uint32_t colorBufferHandle) { |
| 2201 | if (!sVkEmulation || !sVkEmulation->live) { |
| 2202 | return std::nullopt; |
| 2203 | } |
| 2204 | |
| 2205 | AutoLock lock(sVkEmulationLock); |
| 2206 | |
| 2207 | const auto& deviceInfo = sVkEmulation->deviceInfo; |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2208 | if ((!(deviceInfo.supportsExternalMemoryExport || !deviceInfo.supportsExternalMemoryImport)) || |
| 2209 | (!deviceInfo.glInteropSupported)) { |
Jason Macnak | 9e9c68e | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2210 | return std::nullopt; |
| 2211 | } |
| 2212 | |
| 2213 | auto info = android::base::find(sVkEmulation->colorBuffers, colorBufferHandle); |
| 2214 | if (!info) { |
| 2215 | return std::nullopt; |
| 2216 | } |
| 2217 | |
| 2218 | if (info->frameworkFormat != FRAMEWORK_FORMAT_GL_COMPATIBLE) { |
| 2219 | return std::nullopt; |
| 2220 | } |
| 2221 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2222 | #if !defined(__QNX__) |
| 2223 | ManagedDescriptor descriptor(dupExternalMemory(info->memory.externalHandle)); |
Jason Macnak | 9e9c68e | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2224 | |
| 2225 | info->glExported = true; |
| 2226 | |
| 2227 | return VkColorBufferMemoryExport{ |
| 2228 | .descriptor = std::move(descriptor), |
| 2229 | .size = info->memory.size, |
| 2230 | .linearTiling = info->imageCreateInfoShallow.tiling == VK_IMAGE_TILING_LINEAR, |
Jason Macnak | cc53a20 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2231 | .dedicatedAllocation = info->memory.dedicatedAllocation, |
Jason Macnak | 9e9c68e | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2232 | }; |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2233 | #else |
| 2234 | return std::nullopt; |
| 2235 | #endif |
Jason Macnak | 9e9c68e | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
Jason Macnak | 12c1c39 | 2023-03-14 15:15:38 -0700 | [diff] [blame] | 2238 | bool teardownVkColorBufferLocked(uint32_t colorBufferHandle) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2239 | if (!sVkEmulation || !sVkEmulation->live) return false; |
| 2240 | |
| 2241 | auto vk = sVkEmulation->dvk; |
| 2242 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2243 | auto infoPtr = android::base::find(sVkEmulation->colorBuffers, colorBufferHandle); |
| 2244 | |
| 2245 | if (!infoPtr) return false; |
| 2246 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2247 | if (infoPtr->initialized) { |
| 2248 | auto& info = *infoPtr; |
| 2249 | { |
| 2250 | android::base::AutoLock lock(*sVkEmulation->queueLock); |
| 2251 | VK_CHECK(vk->vkQueueWaitIdle(sVkEmulation->queue)); |
| 2252 | } |
| 2253 | vk->vkDestroyImageView(sVkEmulation->device, info.imageView, nullptr); |
| 2254 | vk->vkDestroyImage(sVkEmulation->device, info.image, nullptr); |
| 2255 | freeExternalMemoryLocked(vk, &info.memory); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2256 | |
| 2257 | #ifdef __APPLE__ |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2258 | if (info.mtlTexture) { |
| 2259 | CFRelease(info.mtlTexture); |
| 2260 | } |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2261 | #endif |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2262 | } |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2263 | |
| 2264 | sVkEmulation->colorBuffers.erase(colorBufferHandle); |
| 2265 | |
| 2266 | return true; |
| 2267 | } |
| 2268 | |
Jason Macnak | 12c1c39 | 2023-03-14 15:15:38 -0700 | [diff] [blame] | 2269 | bool teardownVkColorBuffer(uint32_t colorBufferHandle) { |
| 2270 | if (!sVkEmulation || !sVkEmulation->live) return false; |
| 2271 | |
| 2272 | AutoLock lock(sVkEmulationLock); |
| 2273 | return teardownVkColorBufferLocked(colorBufferHandle); |
| 2274 | } |
| 2275 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2276 | bool importExtMemoryHandleToVkColorBuffer(uint32_t colorBufferHandle, uint32_t type, |
| 2277 | VK_EXT_MEMORY_HANDLE extMemHandle) { |
| 2278 | if (!sVkEmulation || !sVkEmulation->live) { |
| 2279 | GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER)) << "VkEmulation not available."; |
| 2280 | } |
| 2281 | if (VK_EXT_MEMORY_HANDLE_INVALID == extMemHandle) { |
| 2282 | return false; |
| 2283 | } |
| 2284 | |
| 2285 | AutoLock lock(sVkEmulationLock); |
| 2286 | // Initialize the colorBuffer with the external memory handle |
| 2287 | // Note that this will fail if the colorBuffer memory was previously initialized. |
| 2288 | return initializeVkColorBufferLocked(colorBufferHandle, extMemHandle); |
| 2289 | } |
| 2290 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2291 | VkEmulation::ColorBufferInfo getColorBufferInfo(uint32_t colorBufferHandle) { |
| 2292 | VkEmulation::ColorBufferInfo res; |
| 2293 | |
| 2294 | AutoLock lock(sVkEmulationLock); |
| 2295 | |
| 2296 | auto infoPtr = android::base::find(sVkEmulation->colorBuffers, colorBufferHandle); |
| 2297 | |
| 2298 | if (!infoPtr) return res; |
| 2299 | |
| 2300 | res = *infoPtr; |
| 2301 | return res; |
| 2302 | } |
| 2303 | |
Jason Macnak | 0436300 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2304 | bool colorBufferNeedsUpdateBetweenGlAndVk(const VkEmulation::ColorBufferInfo& colorBufferInfo) { |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2305 | // GL is not used. |
| 2306 | if (colorBufferInfo.vulkanMode == VkEmulation::VulkanMode::VulkanOnly) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2307 | return false; |
| 2308 | } |
| 2309 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2310 | // YUV formats require extra conversions. |
| 2311 | if (colorBufferInfo.frameworkFormat != FrameworkFormat::FRAMEWORK_FORMAT_GL_COMPATIBLE) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2312 | return true; |
| 2313 | } |
| 2314 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2315 | // GL and VK are sharing the same underlying memory. |
| 2316 | if (colorBufferInfo.glExported) { |
| 2317 | return false; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2318 | } |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2319 | |
| 2320 | return true; |
| 2321 | } |
| 2322 | |
Jason Macnak | 0436300 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2323 | bool colorBufferNeedsUpdateBetweenGlAndVk(uint32_t colorBufferHandle) { |
| 2324 | if (!sVkEmulation || !sVkEmulation->live) { |
| 2325 | return false; |
| 2326 | } |
| 2327 | |
| 2328 | AutoLock lock(sVkEmulationLock); |
| 2329 | |
| 2330 | auto colorBufferInfo = android::base::find(sVkEmulation->colorBuffers, colorBufferHandle); |
| 2331 | if (!colorBufferInfo) { |
| 2332 | return false; |
| 2333 | } |
| 2334 | |
| 2335 | return colorBufferNeedsUpdateBetweenGlAndVk(*colorBufferInfo); |
| 2336 | } |
| 2337 | |
| 2338 | bool readColorBufferToBytes(uint32_t colorBufferHandle, std::vector<uint8_t>* bytes) { |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2339 | if (!sVkEmulation || !sVkEmulation->live) { |
Yahan Zhou | 8168045 | 2022-07-28 10:46:13 -0700 | [diff] [blame] | 2340 | VK_COMMON_VERBOSE("VkEmulation not available."); |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2341 | return false; |
| 2342 | } |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2343 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2344 | AutoLock lock(sVkEmulationLock); |
| 2345 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2346 | auto colorBufferInfo = android::base::find(sVkEmulation->colorBuffers, colorBufferHandle); |
| 2347 | if (!colorBufferInfo) { |
Yahan Zhou | 8168045 | 2022-07-28 10:46:13 -0700 | [diff] [blame] | 2348 | VK_COMMON_VERBOSE("Failed to read from ColorBuffer:%d, not found.", colorBufferHandle); |
Jason Macnak | 0436300 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2349 | bytes->clear(); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2350 | return false; |
| 2351 | } |
| 2352 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2353 | VkDeviceSize bytesNeeded = 0; |
| 2354 | bool result = getFormatTransferInfo(colorBufferInfo->imageCreateInfoShallow.format, |
| 2355 | colorBufferInfo->imageCreateInfoShallow.extent.width, |
| 2356 | colorBufferInfo->imageCreateInfoShallow.extent.height, |
| 2357 | &bytesNeeded, nullptr); |
| 2358 | if (!result) { |
| 2359 | VK_COMMON_ERROR("Failed to read from ColorBuffer:%d, failed to get read size.", |
| 2360 | colorBufferHandle); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2361 | return false; |
| 2362 | } |
| 2363 | |
Jason Macnak | 0436300 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2364 | bytes->resize(bytesNeeded); |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2365 | |
Gurchetan Singh | cff9995 | 2022-10-14 13:02:03 -0700 | [diff] [blame] | 2366 | result = readColorBufferToBytesLocked( |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2367 | colorBufferHandle, 0, 0, colorBufferInfo->imageCreateInfoShallow.extent.width, |
Jason Macnak | 0436300 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2368 | colorBufferInfo->imageCreateInfoShallow.extent.height, bytes->data()); |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2369 | if (!result) { |
| 2370 | VK_COMMON_ERROR("Failed to read from ColorBuffer:%d, failed to get read size.", |
| 2371 | colorBufferHandle); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2372 | return false; |
| 2373 | } |
| 2374 | |
Jason Macnak | 0436300 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2375 | return true; |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2376 | } |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2377 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2378 | bool readColorBufferToBytes(uint32_t colorBufferHandle, uint32_t x, uint32_t y, uint32_t w, |
| 2379 | uint32_t h, void* outPixels) { |
| 2380 | if (!sVkEmulation || !sVkEmulation->live) { |
| 2381 | VK_COMMON_ERROR("VkEmulation not available."); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2382 | return false; |
| 2383 | } |
| 2384 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2385 | AutoLock lock(sVkEmulationLock); |
| 2386 | return readColorBufferToBytesLocked(colorBufferHandle, x, y, w, h, outPixels); |
| 2387 | } |
| 2388 | |
| 2389 | bool readColorBufferToBytesLocked(uint32_t colorBufferHandle, uint32_t x, uint32_t y, uint32_t w, |
| 2390 | uint32_t h, void* outPixels) { |
| 2391 | if (!sVkEmulation || !sVkEmulation->live) { |
| 2392 | VK_COMMON_ERROR("VkEmulation not available."); |
| 2393 | return false; |
| 2394 | } |
| 2395 | |
| 2396 | auto vk = sVkEmulation->dvk; |
| 2397 | |
| 2398 | auto colorBufferInfo = android::base::find(sVkEmulation->colorBuffers, colorBufferHandle); |
| 2399 | if (!colorBufferInfo) { |
| 2400 | VK_COMMON_ERROR("Failed to read from ColorBuffer:%d, not found.", colorBufferHandle); |
| 2401 | return false; |
| 2402 | } |
| 2403 | |
| 2404 | if (!colorBufferInfo->image) { |
| 2405 | VK_COMMON_ERROR("Failed to read from ColorBuffer:%d, no VkImage.", colorBufferHandle); |
| 2406 | return false; |
| 2407 | } |
| 2408 | |
| 2409 | if (x != 0 || y != 0 || w != colorBufferInfo->imageCreateInfoShallow.extent.width || |
| 2410 | h != colorBufferInfo->imageCreateInfoShallow.extent.height) { |
| 2411 | VK_COMMON_ERROR("Failed to read from ColorBuffer:%d, unhandled subrect.", |
| 2412 | colorBufferHandle); |
| 2413 | return false; |
| 2414 | } |
| 2415 | |
Yahan Zhou | d7970e4 | 2022-08-05 10:51:24 -0700 | [diff] [blame] | 2416 | VkDeviceSize bufferCopySize = 0; |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2417 | std::vector<VkBufferImageCopy> bufferImageCopies; |
| 2418 | if (!getFormatTransferInfo(colorBufferInfo->imageCreateInfoShallow.format, |
| 2419 | colorBufferInfo->imageCreateInfoShallow.extent.width, |
| 2420 | colorBufferInfo->imageCreateInfoShallow.extent.height, |
| 2421 | &bufferCopySize, &bufferImageCopies)) { |
| 2422 | VK_COMMON_ERROR("Failed to read ColorBuffer:%d, unable to get transfer info.", |
| 2423 | colorBufferHandle); |
| 2424 | return false; |
| 2425 | } |
| 2426 | |
| 2427 | // Avoid transitioning from VK_IMAGE_LAYOUT_UNDEFINED. Unfortunetly, Android does not |
| 2428 | // yet have a mechanism for sharing the expected VkImageLayout. However, the Vulkan |
| 2429 | // spec's image layout transition sections says "If the old layout is |
| 2430 | // VK_IMAGE_LAYOUT_UNDEFINED, the contents of that range may be discarded." Some |
| 2431 | // Vulkan drivers have been observed to actually perform the discard which leads to |
| 2432 | // ColorBuffer-s being unintentionally cleared. See go/ahb-vkimagelayout for a more |
| 2433 | // thorough write up. |
| 2434 | if (colorBufferInfo->currentLayout == VK_IMAGE_LAYOUT_UNDEFINED) { |
| 2435 | colorBufferInfo->currentLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; |
| 2436 | } |
| 2437 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2438 | // Record our synchronization commands. |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2439 | const VkCommandBufferBeginInfo beginInfo = { |
| 2440 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
| 2441 | .pNext = nullptr, |
| 2442 | .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2443 | }; |
| 2444 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2445 | VkCommandBuffer commandBuffer = sVkEmulation->commandBuffer; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2446 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2447 | VK_CHECK(vk->vkBeginCommandBuffer(commandBuffer, &beginInfo)); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2448 | |
Jason Macnak | 5c76bdb | 2024-01-30 09:07:34 -0800 | [diff] [blame] | 2449 | sVkEmulation->debugUtilsHelper.cmdBeginDebugLabel( |
| 2450 | commandBuffer, "readColorBufferToBytes(ColorBuffer:%d)", colorBufferHandle); |
| 2451 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2452 | const VkImageMemoryBarrier toTransferSrcImageBarrier = { |
| 2453 | .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, |
| 2454 | .pNext = nullptr, |
| 2455 | .srcAccessMask = 0, |
| 2456 | .dstAccessMask = VK_ACCESS_HOST_READ_BIT, |
| 2457 | .oldLayout = colorBufferInfo->currentLayout, |
| 2458 | .newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 2459 | .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, |
| 2460 | .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, |
| 2461 | .image = colorBufferInfo->image, |
| 2462 | .subresourceRange = |
| 2463 | { |
| 2464 | .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, |
| 2465 | .baseMipLevel = 0, |
| 2466 | .levelCount = 1, |
| 2467 | .baseArrayLayer = 0, |
| 2468 | .layerCount = 1, |
| 2469 | }, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2470 | }; |
| 2471 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2472 | vk->vkCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2473 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, nullptr, 0, nullptr, 1, |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2474 | &toTransferSrcImageBarrier); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2475 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2476 | colorBufferInfo->currentLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; |
Doug Horn | 0c2ea5a | 2021-10-29 17:30:16 -0700 | [diff] [blame] | 2477 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2478 | vk->vkCmdCopyImageToBuffer(commandBuffer, colorBufferInfo->image, |
| 2479 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, sVkEmulation->staging.buffer, |
| 2480 | bufferImageCopies.size(), bufferImageCopies.data()); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2481 | |
Jason Macnak | 5c76bdb | 2024-01-30 09:07:34 -0800 | [diff] [blame] | 2482 | sVkEmulation->debugUtilsHelper.cmdEndDebugLabel(commandBuffer); |
| 2483 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2484 | VK_CHECK(vk->vkEndCommandBuffer(commandBuffer)); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2485 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2486 | const VkSubmitInfo submitInfo = { |
| 2487 | .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, |
| 2488 | .pNext = nullptr, |
| 2489 | .waitSemaphoreCount = 0, |
| 2490 | .pWaitSemaphores = nullptr, |
| 2491 | .pWaitDstStageMask = nullptr, |
| 2492 | .commandBufferCount = 1, |
| 2493 | .pCommandBuffers = &commandBuffer, |
| 2494 | .signalSemaphoreCount = 0, |
| 2495 | .pSignalSemaphores = nullptr, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2496 | }; |
| 2497 | |
Kaiyi Li | 263e9f4 | 2021-08-20 07:33:05 -0700 | [diff] [blame] | 2498 | { |
| 2499 | android::base::AutoLock lock(*sVkEmulation->queueLock); |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2500 | VK_CHECK(vk->vkQueueSubmit(sVkEmulation->queue, 1, &submitInfo, |
| 2501 | sVkEmulation->commandBufferFence)); |
Kaiyi Li | 263e9f4 | 2021-08-20 07:33:05 -0700 | [diff] [blame] | 2502 | } |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2503 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2504 | static constexpr uint64_t ANB_MAX_WAIT_NS = 5ULL * 1000ULL * 1000ULL * 1000ULL; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2505 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2506 | VK_CHECK(vk->vkWaitForFences(sVkEmulation->device, 1, &sVkEmulation->commandBufferFence, |
| 2507 | VK_TRUE, ANB_MAX_WAIT_NS)); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2508 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2509 | VK_CHECK(vk->vkResetFences(sVkEmulation->device, 1, &sVkEmulation->commandBufferFence)); |
| 2510 | |
| 2511 | const VkMappedMemoryRange toInvalidate = { |
| 2512 | .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE, |
| 2513 | .pNext = nullptr, |
| 2514 | .memory = sVkEmulation->staging.memory.memory, |
| 2515 | .offset = 0, |
| 2516 | .size = VK_WHOLE_SIZE, |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2517 | }; |
| 2518 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2519 | VK_CHECK(vk->vkInvalidateMappedMemoryRanges(sVkEmulation->device, 1, &toInvalidate)); |
| 2520 | |
| 2521 | const auto* stagingBufferPtr = sVkEmulation->staging.memory.mappedPtr; |
| 2522 | std::memcpy(outPixels, stagingBufferPtr, bufferCopySize); |
| 2523 | |
| 2524 | return true; |
| 2525 | } |
| 2526 | |
Jason Macnak | 0436300 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2527 | bool updateColorBufferFromBytes(uint32_t colorBufferHandle, const std::vector<uint8_t>& bytes) { |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2528 | if (!sVkEmulation || !sVkEmulation->live) { |
Yahan Zhou | 8168045 | 2022-07-28 10:46:13 -0700 | [diff] [blame] | 2529 | VK_COMMON_VERBOSE("VkEmulation not available."); |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2530 | return false; |
| 2531 | } |
| 2532 | |
| 2533 | AutoLock lock(sVkEmulationLock); |
| 2534 | |
| 2535 | auto colorBufferInfo = android::base::find(sVkEmulation->colorBuffers, colorBufferHandle); |
| 2536 | if (!colorBufferInfo) { |
Yahan Zhou | 8168045 | 2022-07-28 10:46:13 -0700 | [diff] [blame] | 2537 | VK_COMMON_VERBOSE("Failed to update ColorBuffer:%d, not found.", colorBufferHandle); |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2538 | return false; |
| 2539 | } |
| 2540 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2541 | return updateColorBufferFromBytesLocked( |
| 2542 | colorBufferHandle, 0, 0, colorBufferInfo->imageCreateInfoShallow.extent.width, |
Igor Chernyshev | ed39a8d | 2023-12-21 11:03:24 -0800 | [diff] [blame] | 2543 | colorBufferInfo->imageCreateInfoShallow.extent.height, bytes.data(), bytes.size()); |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2544 | } |
| 2545 | |
| 2546 | bool updateColorBufferFromBytes(uint32_t colorBufferHandle, uint32_t x, uint32_t y, uint32_t w, |
| 2547 | uint32_t h, const void* pixels) { |
| 2548 | if (!sVkEmulation || !sVkEmulation->live) { |
| 2549 | VK_COMMON_ERROR("VkEmulation not available."); |
| 2550 | return false; |
| 2551 | } |
| 2552 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2553 | AutoLock lock(sVkEmulationLock); |
Igor Chernyshev | ed39a8d | 2023-12-21 11:03:24 -0800 | [diff] [blame] | 2554 | return updateColorBufferFromBytesLocked(colorBufferHandle, x, y, w, h, pixels, 0); |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2555 | } |
| 2556 | |
Igor Chernyshev | ed39a8d | 2023-12-21 11:03:24 -0800 | [diff] [blame] | 2557 | static void convertRgbToRgbaPixels(void* dst, const void* src, uint32_t w, uint32_t h) { |
| 2558 | const size_t pixelCount = w * h; |
| 2559 | const uint8_t* srcBytes = reinterpret_cast<const uint8_t*>(src); |
| 2560 | uint32_t* dstPixels = reinterpret_cast<uint32_t*>(dst); |
| 2561 | for (size_t i = 0; i < pixelCount; ++i) { |
| 2562 | const uint8_t r = *(srcBytes++); |
| 2563 | const uint8_t g = *(srcBytes++); |
| 2564 | const uint8_t b = *(srcBytes++); |
| 2565 | *(dstPixels++) = 0xff000000 | (b << 16) | (g << 8) | r; |
| 2566 | } |
| 2567 | } |
| 2568 | |
| 2569 | static bool updateColorBufferFromBytesLocked(uint32_t colorBufferHandle, uint32_t x, uint32_t y, |
| 2570 | uint32_t w, uint32_t h, const void* pixels, |
| 2571 | size_t inputPixelsSize) { |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2572 | if (!sVkEmulation || !sVkEmulation->live) { |
| 2573 | VK_COMMON_ERROR("VkEmulation not available."); |
| 2574 | return false; |
| 2575 | } |
| 2576 | |
| 2577 | auto vk = sVkEmulation->dvk; |
| 2578 | |
| 2579 | auto colorBufferInfo = android::base::find(sVkEmulation->colorBuffers, colorBufferHandle); |
| 2580 | if (!colorBufferInfo) { |
| 2581 | VK_COMMON_ERROR("Failed to update ColorBuffer:%d, not found.", colorBufferHandle); |
| 2582 | return false; |
| 2583 | } |
| 2584 | |
| 2585 | if (!colorBufferInfo->image) { |
| 2586 | VK_COMMON_ERROR("Failed to update ColorBuffer:%d, no VkImage.", colorBufferHandle); |
| 2587 | return false; |
| 2588 | } |
| 2589 | |
| 2590 | if (x != 0 || y != 0 || w != colorBufferInfo->imageCreateInfoShallow.extent.width || |
| 2591 | h != colorBufferInfo->imageCreateInfoShallow.extent.height) { |
| 2592 | VK_COMMON_ERROR("Failed to update ColorBuffer:%d, unhandled subrect.", colorBufferHandle); |
| 2593 | return false; |
| 2594 | } |
| 2595 | |
Igor Chernyshev | ed39a8d | 2023-12-21 11:03:24 -0800 | [diff] [blame] | 2596 | VkDeviceSize dstBufferSize = 0; |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2597 | std::vector<VkBufferImageCopy> bufferImageCopies; |
| 2598 | if (!getFormatTransferInfo(colorBufferInfo->imageCreateInfoShallow.format, |
| 2599 | colorBufferInfo->imageCreateInfoShallow.extent.width, |
| 2600 | colorBufferInfo->imageCreateInfoShallow.extent.height, |
Igor Chernyshev | ed39a8d | 2023-12-21 11:03:24 -0800 | [diff] [blame] | 2601 | &dstBufferSize, &bufferImageCopies)) { |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2602 | VK_COMMON_ERROR("Failed to update ColorBuffer:%d, unable to get transfer info.", |
| 2603 | colorBufferHandle); |
| 2604 | return false; |
| 2605 | } |
| 2606 | |
| 2607 | const VkDeviceSize stagingBufferSize = sVkEmulation->staging.size; |
Igor Chernyshev | ed39a8d | 2023-12-21 11:03:24 -0800 | [diff] [blame] | 2608 | if (dstBufferSize > stagingBufferSize) { |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2609 | VK_COMMON_ERROR("Failed to update ColorBuffer:%d, transfer size %" PRIu64 |
| 2610 | " too large for staging buffer size:%" PRIu64 ".", |
Igor Chernyshev | ed39a8d | 2023-12-21 11:03:24 -0800 | [diff] [blame] | 2611 | colorBufferHandle, dstBufferSize, stagingBufferSize); |
| 2612 | return false; |
| 2613 | } |
| 2614 | |
| 2615 | bool isThreeByteRgb = (colorBufferInfo->internalFormat == GL_RGB || |
| 2616 | colorBufferInfo->internalFormat == GL_RGB8); |
| 2617 | size_t expectedInputSize = (isThreeByteRgb ? dstBufferSize / 4 * 3 : dstBufferSize); |
| 2618 | |
| 2619 | if (inputPixelsSize != 0 && inputPixelsSize != expectedInputSize) { |
| 2620 | VK_COMMON_ERROR( |
| 2621 | "Unexpected contents size when trying to update ColorBuffer:%d, " |
| 2622 | "provided:%zu expected:%zu", |
| 2623 | colorBufferHandle, inputPixelsSize, expectedInputSize); |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2624 | return false; |
| 2625 | } |
| 2626 | |
| 2627 | auto* stagingBufferPtr = sVkEmulation->staging.memory.mappedPtr; |
Igor Chernyshev | ed39a8d | 2023-12-21 11:03:24 -0800 | [diff] [blame] | 2628 | |
| 2629 | if (isThreeByteRgb) { |
| 2630 | // Convert RGB to RGBA, since only for these types glFormat2VkFormat() makes |
| 2631 | // an incompatible choice of 4-byte backing VK_FORMAT_R8G8B8A8_UNORM. |
| 2632 | // b/281550953 |
| 2633 | convertRgbToRgbaPixels(stagingBufferPtr, pixels, w, h); |
| 2634 | } else { |
| 2635 | std::memcpy(stagingBufferPtr, pixels, dstBufferSize); |
| 2636 | } |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2637 | |
Lars Harrison | 7098977 | 2024-02-03 01:53:24 +0000 | [diff] [blame] | 2638 | // NOTE: Host vulkan state might not know the correct layout of the |
| 2639 | // destination image, as guest grallocs are designed to be used by either |
| 2640 | // GL or Vulkan. Consequently, we typically avoid image transitions from |
| 2641 | // VK_IMAGE_LAYOUT_UNDEFINED as Vulkan spec allows the contents to be |
| 2642 | // discarded (and some drivers have been observed doing it). You can |
| 2643 | // check go/ahb-vkimagelayout for more information. But since this |
| 2644 | // function does not allow subrects (see above), it will write the |
| 2645 | // provided contents onto the entirety of the target buffer, meaning this |
| 2646 | // risk of discarding data should not impact anything. |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2647 | |
| 2648 | // Record our synchronization commands. |
| 2649 | const VkCommandBufferBeginInfo beginInfo = { |
| 2650 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
| 2651 | .pNext = nullptr, |
| 2652 | .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, |
| 2653 | }; |
| 2654 | |
| 2655 | VkCommandBuffer commandBuffer = sVkEmulation->commandBuffer; |
| 2656 | |
| 2657 | VK_CHECK(vk->vkBeginCommandBuffer(commandBuffer, &beginInfo)); |
| 2658 | |
Jason Macnak | 5c76bdb | 2024-01-30 09:07:34 -0800 | [diff] [blame] | 2659 | sVkEmulation->debugUtilsHelper.cmdBeginDebugLabel( |
| 2660 | commandBuffer, "updateColorBufferFromBytes(ColorBuffer:%d)", colorBufferHandle); |
| 2661 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2662 | const VkImageMemoryBarrier toTransferDstImageBarrier = { |
| 2663 | .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, |
| 2664 | .pNext = nullptr, |
| 2665 | .srcAccessMask = 0, |
| 2666 | .dstAccessMask = VK_ACCESS_HOST_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT, |
| 2667 | .oldLayout = colorBufferInfo->currentLayout, |
| 2668 | .newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 2669 | .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, |
| 2670 | .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, |
| 2671 | .image = colorBufferInfo->image, |
| 2672 | .subresourceRange = |
| 2673 | { |
| 2674 | .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, |
| 2675 | .baseMipLevel = 0, |
| 2676 | .levelCount = 1, |
| 2677 | .baseArrayLayer = 0, |
| 2678 | .layerCount = 1, |
| 2679 | }, |
| 2680 | }; |
| 2681 | |
| 2682 | vk->vkCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
| 2683 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, nullptr, 0, nullptr, 1, |
| 2684 | &toTransferDstImageBarrier); |
| 2685 | |
| 2686 | colorBufferInfo->currentLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 2687 | |
| 2688 | // Copy to staging buffer |
| 2689 | vk->vkCmdCopyBufferToImage(commandBuffer, sVkEmulation->staging.buffer, colorBufferInfo->image, |
| 2690 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, bufferImageCopies.size(), |
| 2691 | bufferImageCopies.data()); |
| 2692 | |
Jason Macnak | 5c76bdb | 2024-01-30 09:07:34 -0800 | [diff] [blame] | 2693 | sVkEmulation->debugUtilsHelper.cmdEndDebugLabel(commandBuffer); |
| 2694 | |
Jason Macnak | 7b94cd2 | 2022-04-19 14:58:35 -0700 | [diff] [blame] | 2695 | VK_CHECK(vk->vkEndCommandBuffer(commandBuffer)); |
| 2696 | |
| 2697 | const VkSubmitInfo submitInfo = { |
| 2698 | .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, |
| 2699 | .pNext = nullptr, |
| 2700 | .waitSemaphoreCount = 0, |
| 2701 | .pWaitSemaphores = nullptr, |
| 2702 | .pWaitDstStageMask = nullptr, |
| 2703 | .commandBufferCount = 1, |
| 2704 | .pCommandBuffers = &commandBuffer, |
| 2705 | .signalSemaphoreCount = 0, |
| 2706 | .pSignalSemaphores = nullptr, |
| 2707 | }; |
| 2708 | |
| 2709 | { |
| 2710 | android::base::AutoLock lock(*sVkEmulation->queueLock); |
| 2711 | VK_CHECK(vk->vkQueueSubmit(sVkEmulation->queue, 1, &submitInfo, |
| 2712 | sVkEmulation->commandBufferFence)); |
| 2713 | } |
| 2714 | |
| 2715 | static constexpr uint64_t ANB_MAX_WAIT_NS = 5ULL * 1000ULL * 1000ULL * 1000ULL; |
| 2716 | |
| 2717 | VK_CHECK(vk->vkWaitForFences(sVkEmulation->device, 1, &sVkEmulation->commandBufferFence, |
| 2718 | VK_TRUE, ANB_MAX_WAIT_NS)); |
| 2719 | |
| 2720 | VK_CHECK(vk->vkResetFences(sVkEmulation->device, 1, &sVkEmulation->commandBufferFence)); |
| 2721 | |
| 2722 | const VkMappedMemoryRange toInvalidate = { |
| 2723 | .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE, |
| 2724 | .pNext = nullptr, |
| 2725 | .memory = sVkEmulation->staging.memory.memory, |
| 2726 | .offset = 0, |
| 2727 | .size = VK_WHOLE_SIZE, |
| 2728 | }; |
| 2729 | VK_CHECK(vk->vkInvalidateMappedMemoryRanges(sVkEmulation->device, 1, &toInvalidate)); |
| 2730 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2731 | return true; |
| 2732 | } |
| 2733 | |
| 2734 | VK_EXT_MEMORY_HANDLE getColorBufferExtMemoryHandle(uint32_t colorBuffer) { |
| 2735 | if (!sVkEmulation || !sVkEmulation->live) return VK_EXT_MEMORY_HANDLE_INVALID; |
| 2736 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2737 | AutoLock lock(sVkEmulationLock); |
| 2738 | |
| 2739 | auto infoPtr = android::base::find(sVkEmulation->colorBuffers, colorBuffer); |
| 2740 | |
| 2741 | if (!infoPtr) { |
| 2742 | // Color buffer not found; this is usually OK. |
| 2743 | return VK_EXT_MEMORY_HANDLE_INVALID; |
| 2744 | } |
| 2745 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2746 | return infoPtr->memory.externalHandle; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2747 | } |
| 2748 | |
| 2749 | bool setColorBufferVulkanMode(uint32_t colorBuffer, uint32_t vulkanMode) { |
| 2750 | if (!sVkEmulation || !sVkEmulation->live) return VK_EXT_MEMORY_HANDLE_INVALID; |
| 2751 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2752 | AutoLock lock(sVkEmulationLock); |
| 2753 | |
| 2754 | auto infoPtr = android::base::find(sVkEmulation->colorBuffers, colorBuffer); |
| 2755 | |
| 2756 | if (!infoPtr) { |
| 2757 | return false; |
| 2758 | } |
| 2759 | |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2760 | infoPtr->vulkanMode = static_cast<VkEmulation::VulkanMode>(vulkanMode); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2761 | |
| 2762 | return true; |
| 2763 | } |
| 2764 | |
Doug Horn | efb1796 | 2021-01-07 16:17:34 -0800 | [diff] [blame] | 2765 | MTLTextureRef getColorBufferMTLTexture(uint32_t colorBuffer) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2766 | if (!sVkEmulation || !sVkEmulation->live) return nullptr; |
| 2767 | |
| 2768 | AutoLock lock(sVkEmulationLock); |
| 2769 | |
| 2770 | auto infoPtr = android::base::find(sVkEmulation->colorBuffers, colorBuffer); |
| 2771 | |
| 2772 | if (!infoPtr) { |
| 2773 | // Color buffer not found; this is usually OK. |
| 2774 | return nullptr; |
| 2775 | } |
| 2776 | |
| 2777 | #ifdef __APPLE__ |
Doug Horn | efb1796 | 2021-01-07 16:17:34 -0800 | [diff] [blame] | 2778 | CFRetain(infoPtr->mtlTexture); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2779 | #endif |
Doug Horn | efb1796 | 2021-01-07 16:17:34 -0800 | [diff] [blame] | 2780 | return infoPtr->mtlTexture; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 2781 | } |
| 2782 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2783 | int32_t mapGpaToBufferHandle(uint32_t bufferHandle, uint64_t gpa, uint64_t size) { |
| 2784 | if (!sVkEmulation || !sVkEmulation->live) return VK_ERROR_DEVICE_LOST; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2785 | |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2786 | AutoLock lock(sVkEmulationLock); |
| 2787 | |
| 2788 | VkEmulation::ExternalMemoryInfo* memoryInfoPtr = nullptr; |
| 2789 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2790 | auto colorBufferInfoPtr = android::base::find(sVkEmulation->colorBuffers, bufferHandle); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2791 | if (colorBufferInfoPtr) { |
| 2792 | memoryInfoPtr = &colorBufferInfoPtr->memory; |
| 2793 | } |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2794 | auto bufferInfoPtr = android::base::find(sVkEmulation->buffers, bufferHandle); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2795 | if (bufferInfoPtr) { |
| 2796 | memoryInfoPtr = &bufferInfoPtr->memory; |
| 2797 | } |
| 2798 | |
| 2799 | if (!memoryInfoPtr) { |
| 2800 | return VK_ERROR_INVALID_EXTERNAL_HANDLE; |
| 2801 | } |
| 2802 | |
| 2803 | // memory should be already mapped to host. |
| 2804 | if (!memoryInfoPtr->mappedPtr) { |
| 2805 | return VK_ERROR_MEMORY_MAP_FAILED; |
| 2806 | } |
| 2807 | |
| 2808 | memoryInfoPtr->gpa = gpa; |
| 2809 | memoryInfoPtr->pageAlignedHva = |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2810 | reinterpret_cast<uint8_t*>(memoryInfoPtr->mappedPtr) + memoryInfoPtr->bindOffset; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2811 | |
| 2812 | size_t rawSize = memoryInfoPtr->size + memoryInfoPtr->pageOffset; |
| 2813 | if (size && size < rawSize) { |
| 2814 | rawSize = size; |
| 2815 | } |
| 2816 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2817 | memoryInfoPtr->sizeToPage = ((rawSize + kPageSize - 1) >> kPageBits) << kPageBits; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2818 | |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 2819 | // LOG(VERBOSE) << "mapGpaToColorBuffer: hva = " << memoryInfoPtr->mappedPtr |
| 2820 | // << ", pageAlignedHva = " << memoryInfoPtr->pageAlignedHva |
| 2821 | // << " -> [ " << memoryInfoPtr->gpa << ", " |
| 2822 | // << memoryInfoPtr->gpa + memoryInfoPtr->sizeToPage << " ]"; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2823 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2824 | if (sVkEmulation->occupiedGpas.find(gpa) != sVkEmulation->occupiedGpas.end()) { |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 2825 | // emugl::emugl_crash_reporter("FATAL: already mapped gpa 0x%lx! ", gpa); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2826 | return VK_ERROR_MEMORY_MAP_FAILED; |
| 2827 | } |
| 2828 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2829 | get_emugl_vm_operations().mapUserBackedRam(gpa, memoryInfoPtr->pageAlignedHva, |
| 2830 | memoryInfoPtr->sizeToPage); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2831 | |
| 2832 | sVkEmulation->occupiedGpas.insert(gpa); |
| 2833 | |
| 2834 | return memoryInfoPtr->pageOffset; |
| 2835 | } |
| 2836 | |
Jason Macnak | 5fb2271 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2837 | bool getBufferAllocationInfo(uint32_t bufferHandle, VkDeviceSize* outSize, |
Jason Macnak | cc53a20 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2838 | uint32_t* outMemoryTypeIndex, bool* outMemoryIsDedicatedAlloc) { |
Jason Macnak | 5fb2271 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2839 | if (!sVkEmulation || !sVkEmulation->live) { |
| 2840 | GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER)) << "Vulkan emulation not available."; |
| 2841 | } |
| 2842 | |
| 2843 | AutoLock lock(sVkEmulationLock); |
| 2844 | |
| 2845 | auto info = android::base::find(sVkEmulation->buffers, bufferHandle); |
| 2846 | if (!info) { |
| 2847 | return false; |
| 2848 | } |
| 2849 | |
| 2850 | if (outSize) { |
| 2851 | *outSize = info->memory.size; |
| 2852 | } |
| 2853 | |
| 2854 | if (outMemoryTypeIndex) { |
| 2855 | *outMemoryTypeIndex = info->memory.typeIndex; |
| 2856 | } |
| 2857 | |
Jason Macnak | cc53a20 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2858 | if (outMemoryIsDedicatedAlloc) { |
| 2859 | *outMemoryIsDedicatedAlloc = info->memory.dedicatedAllocation; |
| 2860 | } |
| 2861 | |
Jason Macnak | 5fb2271 | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2862 | return true; |
| 2863 | } |
| 2864 | |
Jason Macnak | 4c45edb | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2865 | bool setupVkBuffer(uint64_t size, uint32_t bufferHandle, bool vulkanOnly, uint32_t memoryProperty) { |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2866 | if (vulkanOnly == false) { |
Jason Macnak | 4c45edb | 2023-02-23 00:40:01 +0000 | [diff] [blame] | 2867 | VK_COMMON_ERROR("Data buffers should be vulkanOnly. Setup failed."); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2868 | return false; |
| 2869 | } |
| 2870 | |
| 2871 | auto vk = sVkEmulation->dvk; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2872 | |
| 2873 | AutoLock lock(sVkEmulationLock); |
| 2874 | |
| 2875 | auto infoPtr = android::base::find(sVkEmulation->buffers, bufferHandle); |
| 2876 | |
| 2877 | // Already setup |
| 2878 | if (infoPtr) { |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2879 | return true; |
| 2880 | } |
| 2881 | |
| 2882 | VkEmulation::BufferInfo res; |
| 2883 | |
| 2884 | res.handle = bufferHandle; |
| 2885 | |
| 2886 | res.size = size; |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2887 | res.usageFlags = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | |
| 2888 | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT | |
| 2889 | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2890 | res.createFlags = 0; |
| 2891 | |
| 2892 | res.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 2893 | |
| 2894 | // Create the image. If external memory is supported, make it external. |
| 2895 | VkExternalMemoryBufferCreateInfo extBufferCi = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2896 | VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO, |
| 2897 | 0, |
| 2898 | VK_EXT_MEMORY_HANDLE_TYPE_BIT, |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2899 | }; |
| 2900 | |
| 2901 | VkExternalMemoryBufferCreateInfo* extBufferCiPtr = nullptr; |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 2902 | if (sVkEmulation->deviceInfo.supportsExternalMemoryImport || |
| 2903 | sVkEmulation->deviceInfo.supportsExternalMemoryExport) { |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2904 | extBufferCiPtr = &extBufferCi; |
| 2905 | } |
| 2906 | |
| 2907 | VkBufferCreateInfo bufferCi = { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2908 | VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 2909 | extBufferCiPtr, |
| 2910 | res.createFlags, |
| 2911 | res.size, |
| 2912 | res.usageFlags, |
| 2913 | res.sharingMode, |
| 2914 | /* queueFamilyIndexCount */ 0, |
| 2915 | /* pQueueFamilyIndices */ nullptr, |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2916 | }; |
| 2917 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2918 | VkResult createRes = vk->vkCreateBuffer(sVkEmulation->device, &bufferCi, nullptr, &res.buffer); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2919 | |
| 2920 | if (createRes != VK_SUCCESS) { |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 2921 | // LOG(VERBOSE) << "Failed to create Vulkan Buffer for Buffer " |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2922 | // << bufferHandle; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2923 | return false; |
| 2924 | } |
Jason Macnak | 04b1904 | 2023-02-28 15:47:05 -0800 | [diff] [blame] | 2925 | bool useDedicated = false; |
Andrew Woloszyn | f7135ef | 2023-02-23 16:20:35 -0500 | [diff] [blame] | 2926 | if (vk->vkGetBufferMemoryRequirements2KHR) { |
| 2927 | VkMemoryDedicatedRequirements dedicated_reqs{ |
| 2928 | VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS, nullptr}; |
| 2929 | VkMemoryRequirements2 reqs{VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2, &dedicated_reqs}; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2930 | |
Andrew Woloszyn | f7135ef | 2023-02-23 16:20:35 -0500 | [diff] [blame] | 2931 | VkBufferMemoryRequirementsInfo2 info{VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2, |
| 2932 | nullptr, res.buffer}; |
| 2933 | vk->vkGetBufferMemoryRequirements2KHR(sVkEmulation->device, &info, &reqs); |
Jason Macnak | 04b1904 | 2023-02-28 15:47:05 -0800 | [diff] [blame] | 2934 | useDedicated = dedicated_reqs.requiresDedicatedAllocation; |
Andrew Woloszyn | f7135ef | 2023-02-23 16:20:35 -0500 | [diff] [blame] | 2935 | res.memReqs = reqs.memoryRequirements; |
| 2936 | } else { |
| 2937 | vk->vkGetBufferMemoryRequirements(sVkEmulation->device, res.buffer, &res.memReqs); |
| 2938 | } |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2939 | |
| 2940 | // Currently we only care about two memory properties: DEVICE_LOCAL |
| 2941 | // and HOST_VISIBLE; other memory properties specified in |
| 2942 | // rcSetColorBufferVulkanMode2() call will be ignored for now. |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2943 | memoryProperty = memoryProperty & |
| 2944 | (VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2945 | |
| 2946 | res.memory.size = res.memReqs.size; |
| 2947 | |
| 2948 | // Determine memory type. |
| 2949 | if (memoryProperty) { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2950 | res.memory.typeIndex = |
| 2951 | lastGoodTypeIndexWithMemoryProperties(res.memReqs.memoryTypeBits, memoryProperty); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2952 | } else { |
| 2953 | res.memory.typeIndex = lastGoodTypeIndex(res.memReqs.memoryTypeBits); |
| 2954 | } |
| 2955 | |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 2956 | // LOG(VERBOSE) << "Buffer " << bufferHandle |
| 2957 | // << "allocation size and type index: " << res.memory.size |
| 2958 | // << ", " << res.memory.typeIndex |
| 2959 | // << ", allocated memory property: " |
| 2960 | // << sVkEmulation->deviceInfo.memProps |
| 2961 | // .memoryTypes[res.memory.typeIndex] |
| 2962 | // .propertyFlags |
| 2963 | // << ", requested memory property: " << memoryProperty; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2964 | |
| 2965 | bool isHostVisible = memoryProperty & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; |
| 2966 | Optional<uint64_t> deviceAlignment = |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2967 | isHostVisible ? Optional<uint64_t>(res.memReqs.alignment) : kNullopt; |
Jason Macnak | 04b1904 | 2023-02-28 15:47:05 -0800 | [diff] [blame] | 2968 | Optional<VkBuffer> dedicated_buffer = useDedicated ? Optional<VkBuffer>(res.buffer) : kNullopt; |
Andrew Woloszyn | f7135ef | 2023-02-23 16:20:35 -0500 | [diff] [blame] | 2969 | bool allocRes = allocExternalMemory(vk, &res.memory, true /* actuallyExternal */, |
| 2970 | deviceAlignment, dedicated_buffer); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2971 | |
| 2972 | if (!allocRes) { |
Lingfeng Yang | c02cb03 | 2020-10-26 14:21:25 -0700 | [diff] [blame] | 2973 | // LOG(VERBOSE) << "Failed to allocate ColorBuffer with Vulkan backing."; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2974 | } |
| 2975 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2976 | res.memory.pageOffset = reinterpret_cast<uint64_t>(res.memory.mappedPtr) % kPageSize; |
| 2977 | res.memory.bindOffset = res.memory.pageOffset ? kPageSize - res.memory.pageOffset : 0u; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2978 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2979 | VkResult bindBufferMemoryRes = |
| 2980 | vk->vkBindBufferMemory(sVkEmulation->device, res.buffer, res.memory.memory, 0); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2981 | |
| 2982 | if (bindBufferMemoryRes != VK_SUCCESS) { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2983 | fprintf(stderr, "%s: Failed to bind buffer memory. %d\n", __func__, bindBufferMemoryRes); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2984 | return bindBufferMemoryRes; |
| 2985 | } |
| 2986 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2987 | bool isHostVisibleMemory = memoryProperty & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2988 | |
| 2989 | if (isHostVisibleMemory) { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2990 | VkResult mapMemoryRes = vk->vkMapMemory(sVkEmulation->device, res.memory.memory, 0, |
| 2991 | res.memory.size, {}, &res.memory.mappedPtr); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2992 | |
| 2993 | if (mapMemoryRes != VK_SUCCESS) { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 2994 | fprintf(stderr, "%s: Failed to map image memory. %d\n", __func__, mapMemoryRes); |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 2995 | return false; |
| 2996 | } |
| 2997 | } |
| 2998 | |
| 2999 | res.glExported = false; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 3000 | |
| 3001 | sVkEmulation->buffers[bufferHandle] = res; |
Jason Macnak | 5c76bdb | 2024-01-30 09:07:34 -0800 | [diff] [blame] | 3002 | |
| 3003 | sVkEmulation->debugUtilsHelper.addDebugLabel(res.buffer, "Buffer:%d", bufferHandle); |
| 3004 | sVkEmulation->debugUtilsHelper.addDebugLabel(res.memory.memory, "Buffer:%d", bufferHandle); |
| 3005 | |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 3006 | return allocRes; |
| 3007 | } |
| 3008 | |
| 3009 | bool teardownVkBuffer(uint32_t bufferHandle) { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 3010 | if (!sVkEmulation || !sVkEmulation->live) return false; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 3011 | |
| 3012 | auto vk = sVkEmulation->dvk; |
| 3013 | AutoLock lock(sVkEmulationLock); |
| 3014 | |
| 3015 | auto infoPtr = android::base::find(sVkEmulation->buffers, bufferHandle); |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 3016 | if (!infoPtr) return false; |
Kaiyi Li | 263e9f4 | 2021-08-20 07:33:05 -0700 | [diff] [blame] | 3017 | { |
| 3018 | android::base::AutoLock lock(*sVkEmulation->queueLock); |
| 3019 | VK_CHECK(vk->vkQueueWaitIdle(sVkEmulation->queue)); |
| 3020 | } |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 3021 | auto& info = *infoPtr; |
| 3022 | |
| 3023 | vk->vkDestroyBuffer(sVkEmulation->device, info.buffer, nullptr); |
| 3024 | freeExternalMemoryLocked(vk, &info.memory); |
| 3025 | sVkEmulation->buffers.erase(bufferHandle); |
| 3026 | |
| 3027 | return true; |
| 3028 | } |
| 3029 | |
| 3030 | VK_EXT_MEMORY_HANDLE getBufferExtMemoryHandle(uint32_t bufferHandle) { |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 3031 | if (!sVkEmulation || !sVkEmulation->live) return VK_EXT_MEMORY_HANDLE_INVALID; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 3032 | |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 3033 | AutoLock lock(sVkEmulationLock); |
| 3034 | |
| 3035 | auto infoPtr = android::base::find(sVkEmulation->buffers, bufferHandle); |
| 3036 | if (!infoPtr) { |
| 3037 | // Color buffer not found; this is usually OK. |
| 3038 | return VK_EXT_MEMORY_HANDLE_INVALID; |
| 3039 | } |
| 3040 | |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 3041 | return infoPtr->memory.externalHandle; |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 3042 | } |
| 3043 | |
Jason Macnak | 6402a1b | 2022-06-02 15:29:05 -0700 | [diff] [blame] | 3044 | bool readBufferToBytes(uint32_t bufferHandle, uint64_t offset, uint64_t size, void* outBytes) { |
| 3045 | if (!sVkEmulation || !sVkEmulation->live) { |
| 3046 | VK_COMMON_ERROR("VkEmulation not available."); |
| 3047 | return false; |
| 3048 | } |
| 3049 | |
| 3050 | auto vk = sVkEmulation->dvk; |
| 3051 | |
| 3052 | AutoLock lock(sVkEmulationLock); |
| 3053 | |
| 3054 | auto bufferInfo = android::base::find(sVkEmulation->buffers, bufferHandle); |
| 3055 | if (!bufferInfo) { |
| 3056 | VK_COMMON_ERROR("Failed to read from Buffer:%d, not found.", bufferHandle); |
| 3057 | return false; |
| 3058 | } |
| 3059 | |
| 3060 | const auto& stagingBufferInfo = sVkEmulation->staging; |
| 3061 | if (size > stagingBufferInfo.size) { |
| 3062 | VK_COMMON_ERROR("Failed to read from Buffer:%d, staging buffer too small.", bufferHandle); |
| 3063 | return false; |
| 3064 | } |
| 3065 | |
| 3066 | const VkCommandBufferBeginInfo beginInfo = { |
| 3067 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
| 3068 | .pNext = nullptr, |
| 3069 | .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, |
| 3070 | }; |
| 3071 | |
| 3072 | VkCommandBuffer commandBuffer = sVkEmulation->commandBuffer; |
| 3073 | |
| 3074 | VK_CHECK(vk->vkBeginCommandBuffer(commandBuffer, &beginInfo)); |
| 3075 | |
Jason Macnak | 5c76bdb | 2024-01-30 09:07:34 -0800 | [diff] [blame] | 3076 | sVkEmulation->debugUtilsHelper.cmdBeginDebugLabel(commandBuffer, "readBufferToBytes(Buffer:%d)", |
| 3077 | bufferHandle); |
| 3078 | |
Jason Macnak | 6402a1b | 2022-06-02 15:29:05 -0700 | [diff] [blame] | 3079 | const VkBufferCopy bufferCopy = { |
| 3080 | .srcOffset = offset, |
| 3081 | .dstOffset = 0, |
| 3082 | .size = size, |
| 3083 | }; |
| 3084 | vk->vkCmdCopyBuffer(commandBuffer, bufferInfo->buffer, stagingBufferInfo.buffer, 1, |
| 3085 | &bufferCopy); |
| 3086 | |
Jason Macnak | 5c76bdb | 2024-01-30 09:07:34 -0800 | [diff] [blame] | 3087 | sVkEmulation->debugUtilsHelper.cmdEndDebugLabel(commandBuffer); |
| 3088 | |
Jason Macnak | 6402a1b | 2022-06-02 15:29:05 -0700 | [diff] [blame] | 3089 | VK_CHECK(vk->vkEndCommandBuffer(commandBuffer)); |
| 3090 | |
| 3091 | const VkSubmitInfo submitInfo = { |
| 3092 | .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, |
| 3093 | .pNext = nullptr, |
| 3094 | .waitSemaphoreCount = 0, |
| 3095 | .pWaitSemaphores = nullptr, |
| 3096 | .pWaitDstStageMask = nullptr, |
| 3097 | .commandBufferCount = 1, |
| 3098 | .pCommandBuffers = &commandBuffer, |
| 3099 | .signalSemaphoreCount = 0, |
| 3100 | .pSignalSemaphores = nullptr, |
| 3101 | }; |
| 3102 | |
| 3103 | { |
| 3104 | android::base::AutoLock lock(*sVkEmulation->queueLock); |
| 3105 | VK_CHECK(vk->vkQueueSubmit(sVkEmulation->queue, 1, &submitInfo, |
| 3106 | sVkEmulation->commandBufferFence)); |
| 3107 | } |
| 3108 | |
| 3109 | static constexpr uint64_t ANB_MAX_WAIT_NS = 5ULL * 1000ULL * 1000ULL * 1000ULL; |
| 3110 | |
| 3111 | VK_CHECK(vk->vkWaitForFences(sVkEmulation->device, 1, &sVkEmulation->commandBufferFence, |
| 3112 | VK_TRUE, ANB_MAX_WAIT_NS)); |
| 3113 | |
| 3114 | VK_CHECK(vk->vkResetFences(sVkEmulation->device, 1, &sVkEmulation->commandBufferFence)); |
| 3115 | |
| 3116 | const VkMappedMemoryRange toInvalidate = { |
| 3117 | .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE, |
| 3118 | .pNext = nullptr, |
| 3119 | .memory = stagingBufferInfo.memory.memory, |
| 3120 | .offset = 0, |
| 3121 | .size = size, |
| 3122 | }; |
| 3123 | |
| 3124 | VK_CHECK(vk->vkInvalidateMappedMemoryRanges(sVkEmulation->device, 1, &toInvalidate)); |
| 3125 | |
| 3126 | const void* srcPtr = reinterpret_cast<const void*>( |
| 3127 | reinterpret_cast<const char*>(stagingBufferInfo.memory.mappedPtr)); |
| 3128 | void* dstPtr = outBytes; |
| 3129 | void* dstPtrOffset = reinterpret_cast<void*>(reinterpret_cast<char*>(dstPtr) + offset); |
| 3130 | std::memcpy(dstPtrOffset, srcPtr, size); |
| 3131 | |
| 3132 | return true; |
| 3133 | } |
| 3134 | |
Jason Macnak | a296e68 | 2022-10-27 16:21:13 -0700 | [diff] [blame] | 3135 | bool updateBufferFromBytes(uint32_t bufferHandle, uint64_t offset, uint64_t size, |
| 3136 | const void* bytes) { |
Jason Macnak | 6402a1b | 2022-06-02 15:29:05 -0700 | [diff] [blame] | 3137 | if (!sVkEmulation || !sVkEmulation->live) { |
| 3138 | VK_COMMON_ERROR("VkEmulation not available."); |
| 3139 | return false; |
| 3140 | } |
| 3141 | |
| 3142 | auto vk = sVkEmulation->dvk; |
| 3143 | |
| 3144 | AutoLock lock(sVkEmulationLock); |
| 3145 | |
| 3146 | auto bufferInfo = android::base::find(sVkEmulation->buffers, bufferHandle); |
| 3147 | if (!bufferInfo) { |
| 3148 | VK_COMMON_ERROR("Failed to update Buffer:%d, not found.", bufferHandle); |
| 3149 | return false; |
| 3150 | } |
| 3151 | |
| 3152 | const auto& stagingBufferInfo = sVkEmulation->staging; |
| 3153 | if (size > stagingBufferInfo.size) { |
| 3154 | VK_COMMON_ERROR("Failed to update Buffer:%d, staging buffer too small.", bufferHandle); |
| 3155 | return false; |
| 3156 | } |
| 3157 | |
| 3158 | const void* srcPtr = bytes; |
| 3159 | const void* srcPtrOffset = |
| 3160 | reinterpret_cast<const void*>(reinterpret_cast<const char*>(srcPtr) + offset); |
| 3161 | void* dstPtr = stagingBufferInfo.memory.mappedPtr; |
| 3162 | std::memcpy(dstPtr, srcPtrOffset, size); |
| 3163 | |
| 3164 | const VkMappedMemoryRange toFlush = { |
| 3165 | .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE, |
| 3166 | .pNext = nullptr, |
| 3167 | .memory = stagingBufferInfo.memory.memory, |
| 3168 | .offset = 0, |
| 3169 | .size = size, |
| 3170 | }; |
| 3171 | VK_CHECK(vk->vkFlushMappedMemoryRanges(sVkEmulation->device, 1, &toFlush)); |
| 3172 | |
| 3173 | const VkCommandBufferBeginInfo beginInfo = { |
| 3174 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
| 3175 | .pNext = nullptr, |
| 3176 | .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, |
| 3177 | }; |
| 3178 | |
| 3179 | VkCommandBuffer commandBuffer = sVkEmulation->commandBuffer; |
| 3180 | |
| 3181 | VK_CHECK(vk->vkBeginCommandBuffer(commandBuffer, &beginInfo)); |
| 3182 | |
Jason Macnak | 5c76bdb | 2024-01-30 09:07:34 -0800 | [diff] [blame] | 3183 | sVkEmulation->debugUtilsHelper.cmdBeginDebugLabel( |
| 3184 | commandBuffer, "updateBufferFromBytes(Buffer:%d)", bufferHandle); |
| 3185 | |
Jason Macnak | 6402a1b | 2022-06-02 15:29:05 -0700 | [diff] [blame] | 3186 | const VkBufferCopy bufferCopy = { |
| 3187 | .srcOffset = 0, |
| 3188 | .dstOffset = offset, |
| 3189 | .size = size, |
| 3190 | }; |
| 3191 | vk->vkCmdCopyBuffer(commandBuffer, stagingBufferInfo.buffer, bufferInfo->buffer, 1, |
| 3192 | &bufferCopy); |
| 3193 | |
Jason Macnak | 5c76bdb | 2024-01-30 09:07:34 -0800 | [diff] [blame] | 3194 | sVkEmulation->debugUtilsHelper.cmdEndDebugLabel(commandBuffer); |
| 3195 | |
Jason Macnak | 6402a1b | 2022-06-02 15:29:05 -0700 | [diff] [blame] | 3196 | VK_CHECK(vk->vkEndCommandBuffer(commandBuffer)); |
| 3197 | |
| 3198 | const VkSubmitInfo submitInfo = { |
| 3199 | .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, |
| 3200 | .pNext = nullptr, |
| 3201 | .waitSemaphoreCount = 0, |
| 3202 | .pWaitSemaphores = nullptr, |
| 3203 | .pWaitDstStageMask = nullptr, |
| 3204 | .commandBufferCount = 1, |
| 3205 | .pCommandBuffers = &commandBuffer, |
| 3206 | .signalSemaphoreCount = 0, |
| 3207 | .pSignalSemaphores = nullptr, |
| 3208 | }; |
| 3209 | |
| 3210 | { |
| 3211 | android::base::AutoLock lock(*sVkEmulation->queueLock); |
| 3212 | VK_CHECK(vk->vkQueueSubmit(sVkEmulation->queue, 1, &submitInfo, |
| 3213 | sVkEmulation->commandBufferFence)); |
| 3214 | } |
| 3215 | |
| 3216 | static constexpr uint64_t ANB_MAX_WAIT_NS = 5ULL * 1000ULL * 1000ULL * 1000ULL; |
| 3217 | |
| 3218 | VK_CHECK(vk->vkWaitForFences(sVkEmulation->device, 1, &sVkEmulation->commandBufferFence, |
| 3219 | VK_TRUE, ANB_MAX_WAIT_NS)); |
| 3220 | |
| 3221 | VK_CHECK(vk->vkResetFences(sVkEmulation->device, 1, &sVkEmulation->commandBufferFence)); |
| 3222 | |
| 3223 | return true; |
| 3224 | } |
| 3225 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 3226 | VkExternalMemoryHandleTypeFlags transformExternalMemoryHandleTypeFlags_tohost( |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 3227 | VkExternalMemoryHandleTypeFlags bits) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 3228 | VkExternalMemoryHandleTypeFlags res = bits; |
| 3229 | |
| 3230 | // Transform Android/Fuchsia/Linux bits to host bits. |
| 3231 | if (bits & VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT) { |
| 3232 | res &= ~VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT; |
| 3233 | } |
| 3234 | |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 3235 | #ifdef _WIN32 |
| 3236 | res &= ~VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT; |
| 3237 | res &= ~VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT; |
| 3238 | #endif |
| 3239 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 3240 | if (bits & VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID) { |
| 3241 | res &= ~VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID; |
| 3242 | res |= VK_EXT_MEMORY_HANDLE_TYPE_BIT; |
| 3243 | } |
| 3244 | |
Lingfeng Yang | cbcfca4 | 2022-01-27 12:18:57 -0800 | [diff] [blame] | 3245 | if (bits & VK_EXTERNAL_MEMORY_HANDLE_TYPE_ZIRCON_VMO_BIT_FUCHSIA) { |
| 3246 | res &= ~VK_EXTERNAL_MEMORY_HANDLE_TYPE_ZIRCON_VMO_BIT_FUCHSIA; |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 3247 | res |= VK_EXT_MEMORY_HANDLE_TYPE_BIT; |
| 3248 | } |
Yilong Li | a7a442a | 2021-03-24 17:17:25 -0700 | [diff] [blame] | 3249 | |
| 3250 | if (bits & VK_EXTERNAL_MEMORY_HANDLE_TYPE_ZIRCON_VMO_BIT_FUCHSIA) { |
| 3251 | res &= ~VK_EXTERNAL_MEMORY_HANDLE_TYPE_ZIRCON_VMO_BIT_FUCHSIA; |
| 3252 | res |= VK_EXT_MEMORY_HANDLE_TYPE_BIT; |
| 3253 | } |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 3254 | |
| 3255 | #if defined(__QNX__) |
| 3256 | // QNX only: Replace DMA_BUF_BIT_EXT with SCREEN_BUFFER_BIT_QNX for host calls |
| 3257 | if (bits & VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT) { |
| 3258 | res &= ~VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT; |
| 3259 | res |= VK_EXT_MEMORY_HANDLE_TYPE_BIT; |
| 3260 | } |
| 3261 | #endif |
| 3262 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 3263 | return res; |
| 3264 | } |
| 3265 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 3266 | VkExternalMemoryHandleTypeFlags transformExternalMemoryHandleTypeFlags_fromhost( |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 3267 | VkExternalMemoryHandleTypeFlags hostBits, |
| 3268 | VkExternalMemoryHandleTypeFlags wantedGuestHandleType) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 3269 | VkExternalMemoryHandleTypeFlags res = hostBits; |
| 3270 | |
| 3271 | if (res & VK_EXT_MEMORY_HANDLE_TYPE_BIT) { |
| 3272 | res &= ~VK_EXT_MEMORY_HANDLE_TYPE_BIT; |
| 3273 | res |= wantedGuestHandleType; |
| 3274 | } |
| 3275 | |
Lingfeng Yang | 7690689 | 2020-10-26 11:10:46 -0700 | [diff] [blame] | 3276 | #ifdef _WIN32 |
| 3277 | res &= ~VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT; |
| 3278 | res &= ~VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT; |
| 3279 | #endif |
| 3280 | |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 3281 | return res; |
| 3282 | } |
| 3283 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 3284 | VkExternalMemoryProperties transformExternalMemoryProperties_tohost( |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 3285 | VkExternalMemoryProperties props) { |
| 3286 | VkExternalMemoryProperties res = props; |
| 3287 | res.exportFromImportedHandleTypes = |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 3288 | transformExternalMemoryHandleTypeFlags_tohost(props.exportFromImportedHandleTypes); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 3289 | res.compatibleHandleTypes = |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 3290 | transformExternalMemoryHandleTypeFlags_tohost(props.compatibleHandleTypes); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 3291 | return res; |
| 3292 | } |
| 3293 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 3294 | VkExternalMemoryProperties transformExternalMemoryProperties_fromhost( |
| 3295 | VkExternalMemoryProperties props, VkExternalMemoryHandleTypeFlags wantedGuestHandleType) { |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 3296 | VkExternalMemoryProperties res = props; |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 3297 | res.exportFromImportedHandleTypes = transformExternalMemoryHandleTypeFlags_fromhost( |
| 3298 | props.exportFromImportedHandleTypes, wantedGuestHandleType); |
| 3299 | res.compatibleHandleTypes = transformExternalMemoryHandleTypeFlags_fromhost( |
| 3300 | props.compatibleHandleTypes, wantedGuestHandleType); |
Lingfeng Yang | ea978ce | 2019-12-13 13:42:23 -0800 | [diff] [blame] | 3301 | return res; |
| 3302 | } |
| 3303 | |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 3304 | void setColorBufferCurrentLayout(uint32_t colorBufferHandle, VkImageLayout layout) { |
| 3305 | AutoLock lock(sVkEmulationLock); |
| 3306 | |
| 3307 | auto infoPtr = android::base::find(sVkEmulation->colorBuffers, colorBufferHandle); |
| 3308 | if (!infoPtr) { |
| 3309 | VK_COMMON_ERROR("Invalid ColorBuffer handle %d.", static_cast<int>(colorBufferHandle)); |
| 3310 | return; |
| 3311 | } |
| 3312 | infoPtr->currentLayout = layout; |
| 3313 | } |
| 3314 | |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3315 | // Allocate a ready to use VkCommandBuffer for queue transfer. The caller needs |
| 3316 | // to signal the returned VkFence when the VkCommandBuffer completes. |
| 3317 | static std::tuple<VkCommandBuffer, VkFence> allocateQueueTransferCommandBuffer_locked() { |
| 3318 | auto vk = sVkEmulation->dvk; |
| 3319 | // Check if a command buffer in the pool is ready to use. If the associated |
| 3320 | // VkFence is ready, vkGetFenceStatus will return VK_SUCCESS, and the |
| 3321 | // associated command buffer should be ready to use, so we return that |
| 3322 | // command buffer with the associated VkFence. If the associated VkFence is |
| 3323 | // not ready, vkGetFenceStatus will return VK_NOT_READY, we will continue to |
| 3324 | // search and test the next command buffer. If the VkFence is in an error |
| 3325 | // state, vkGetFenceStatus will return with other VkResult variants, we will |
| 3326 | // abort. |
| 3327 | for (auto& [commandBuffer, fence] : sVkEmulation->transferQueueCommandBufferPool) { |
| 3328 | auto res = vk->vkGetFenceStatus(sVkEmulation->device, fence); |
| 3329 | if (res == VK_SUCCESS) { |
| 3330 | VK_CHECK(vk->vkResetFences(sVkEmulation->device, 1, &fence)); |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 3331 | VK_CHECK(vk->vkResetCommandBuffer(commandBuffer, |
| 3332 | VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT)); |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3333 | return std::make_tuple(commandBuffer, fence); |
| 3334 | } |
| 3335 | if (res == VK_NOT_READY) { |
| 3336 | continue; |
| 3337 | } |
Doug Horn | e675112 | 2022-03-31 14:19:44 -0700 | [diff] [blame] | 3338 | // We either have a device lost, or an invalid fence state. For the device lost case, |
| 3339 | // VK_CHECK will ensure we capture the relevant streams. |
| 3340 | VK_CHECK(res); |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3341 | } |
| 3342 | VkCommandBuffer commandBuffer; |
| 3343 | VkCommandBufferAllocateInfo allocateInfo = { |
| 3344 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, |
| 3345 | .pNext = nullptr, |
| 3346 | .commandPool = sVkEmulation->commandPool, |
| 3347 | .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, |
| 3348 | .commandBufferCount = 1, |
| 3349 | }; |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 3350 | VK_CHECK(vk->vkAllocateCommandBuffers(sVkEmulation->device, &allocateInfo, &commandBuffer)); |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3351 | VkFence fence; |
| 3352 | VkFenceCreateInfo fenceCi = { |
| 3353 | .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, |
| 3354 | .pNext = nullptr, |
| 3355 | .flags = 0, |
| 3356 | }; |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 3357 | VK_CHECK(vk->vkCreateFence(sVkEmulation->device, &fenceCi, nullptr, &fence)); |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3358 | |
| 3359 | sVkEmulation->transferQueueCommandBufferPool.emplace_back(commandBuffer, fence); |
| 3360 | |
| 3361 | VK_COMMON_VERBOSE( |
| 3362 | "Create a new command buffer for queue transfer for a total of %d " |
| 3363 | "transfer command buffers", |
| 3364 | static_cast<int>(sVkEmulation->transferQueueCommandBufferPool.size())); |
| 3365 | |
| 3366 | return std::make_tuple(commandBuffer, fence); |
| 3367 | } |
| 3368 | |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 3369 | const VkImageLayout kGuestUseDefaultImageLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3370 | |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 3371 | void releaseColorBufferForGuestUse(uint32_t colorBufferHandle) { |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3372 | if (!sVkEmulation || !sVkEmulation->live) { |
Doug Horn | 0c2ea5a | 2021-10-29 17:30:16 -0700 | [diff] [blame] | 3373 | GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER)) << "Host Vulkan device lost"; |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3374 | } |
| 3375 | |
| 3376 | AutoLock lock(sVkEmulationLock); |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3377 | |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 3378 | auto infoPtr = android::base::find(sVkEmulation->colorBuffers, colorBufferHandle); |
| 3379 | if (!infoPtr) { |
| 3380 | VK_COMMON_ERROR("Failed to find ColorBuffer handle %d.", |
| 3381 | static_cast<int>(colorBufferHandle)); |
| 3382 | return; |
| 3383 | } |
| 3384 | |
| 3385 | std::optional<VkImageMemoryBarrier> layoutTransitionBarrier; |
| 3386 | if (infoPtr->currentLayout != kGuestUseDefaultImageLayout) { |
| 3387 | layoutTransitionBarrier = VkImageMemoryBarrier{ |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3388 | .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, |
| 3389 | .pNext = nullptr, |
| 3390 | .srcAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT, |
| 3391 | .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT, |
| 3392 | .oldLayout = infoPtr->currentLayout, |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 3393 | .newLayout = kGuestUseDefaultImageLayout, |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3394 | .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, |
| 3395 | .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, |
| 3396 | .image = infoPtr->image, |
| 3397 | .subresourceRange = |
| 3398 | { |
| 3399 | .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, |
| 3400 | .baseMipLevel = 0, |
| 3401 | .levelCount = 1, |
| 3402 | .baseArrayLayer = 0, |
| 3403 | .layerCount = 1, |
| 3404 | }, |
| 3405 | }; |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 3406 | infoPtr->currentLayout = kGuestUseDefaultImageLayout; |
| 3407 | } |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3408 | |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 3409 | std::optional<VkImageMemoryBarrier> queueTransferBarrier; |
| 3410 | if (infoPtr->currentQueueFamilyIndex != VK_QUEUE_FAMILY_EXTERNAL) { |
| 3411 | queueTransferBarrier = VkImageMemoryBarrier{ |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3412 | .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, |
| 3413 | .pNext = nullptr, |
| 3414 | .srcAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT, |
| 3415 | .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT, |
| 3416 | .oldLayout = infoPtr->currentLayout, |
| 3417 | .newLayout = infoPtr->currentLayout, |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 3418 | .srcQueueFamilyIndex = infoPtr->currentQueueFamilyIndex, |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3419 | .dstQueueFamilyIndex = VK_QUEUE_FAMILY_EXTERNAL, |
| 3420 | .image = infoPtr->image, |
| 3421 | .subresourceRange = |
| 3422 | { |
| 3423 | .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, |
| 3424 | .baseMipLevel = 0, |
| 3425 | .levelCount = 1, |
| 3426 | .baseArrayLayer = 0, |
| 3427 | .layerCount = 1, |
| 3428 | }, |
| 3429 | }; |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 3430 | infoPtr->currentQueueFamilyIndex = VK_QUEUE_FAMILY_EXTERNAL; |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3431 | } |
| 3432 | |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 3433 | if (!layoutTransitionBarrier && !queueTransferBarrier) { |
| 3434 | return; |
| 3435 | } |
| 3436 | |
| 3437 | auto vk = sVkEmulation->dvk; |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3438 | auto [commandBuffer, fence] = allocateQueueTransferCommandBuffer_locked(); |
| 3439 | |
| 3440 | VK_CHECK(vk->vkResetCommandBuffer(commandBuffer, 0)); |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 3441 | |
| 3442 | const VkCommandBufferBeginInfo beginInfo = { |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3443 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
| 3444 | .pNext = nullptr, |
| 3445 | .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, |
| 3446 | .pInheritanceInfo = nullptr, |
| 3447 | }; |
| 3448 | VK_CHECK(vk->vkBeginCommandBuffer(commandBuffer, &beginInfo)); |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 3449 | |
Jason Macnak | 5c76bdb | 2024-01-30 09:07:34 -0800 | [diff] [blame] | 3450 | sVkEmulation->debugUtilsHelper.cmdBeginDebugLabel( |
| 3451 | commandBuffer, "releaseColorBufferForGuestUse(ColorBuffer:%d)", colorBufferHandle); |
| 3452 | |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 3453 | if (layoutTransitionBarrier) { |
| 3454 | vk->vkCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
| 3455 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, nullptr, 0, nullptr, 1, |
| 3456 | &layoutTransitionBarrier.value()); |
| 3457 | } |
| 3458 | if (queueTransferBarrier) { |
| 3459 | vk->vkCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
| 3460 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, nullptr, 0, nullptr, 1, |
| 3461 | &queueTransferBarrier.value()); |
| 3462 | } |
| 3463 | |
Jason Macnak | 5c76bdb | 2024-01-30 09:07:34 -0800 | [diff] [blame] | 3464 | sVkEmulation->debugUtilsHelper.cmdEndDebugLabel(commandBuffer); |
| 3465 | |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3466 | VK_CHECK(vk->vkEndCommandBuffer(commandBuffer)); |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 3467 | |
| 3468 | const VkSubmitInfo submitInfo = { |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3469 | .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, |
| 3470 | .pNext = nullptr, |
| 3471 | .waitSemaphoreCount = 0, |
| 3472 | .pWaitSemaphores = nullptr, |
| 3473 | .pWaitDstStageMask = nullptr, |
| 3474 | .commandBufferCount = 1, |
| 3475 | .pCommandBuffers = &commandBuffer, |
| 3476 | .signalSemaphoreCount = 0, |
| 3477 | .pSignalSemaphores = nullptr, |
| 3478 | }; |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3479 | { |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3480 | android::base::AutoLock lock(*sVkEmulation->queueLock); |
| 3481 | VK_CHECK(vk->vkQueueSubmit(sVkEmulation->queue, 1, &submitInfo, fence)); |
| 3482 | } |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3483 | |
Greg Schlomoff | e111d0d | 2022-05-19 14:40:32 -0700 | [diff] [blame] | 3484 | static constexpr uint64_t ANB_MAX_WAIT_NS = 5ULL * 1000ULL * 1000ULL * 1000ULL; |
| 3485 | VK_CHECK(vk->vkWaitForFences(sVkEmulation->device, 1, &fence, VK_TRUE, ANB_MAX_WAIT_NS)); |
Kaiyi Li | 5509d44 | 2021-10-18 15:13:46 -0700 | [diff] [blame] | 3486 | } |
| 3487 | |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 3488 | std::unique_ptr<BorrowedImageInfoVk> borrowColorBufferForComposition(uint32_t colorBufferHandle, |
| 3489 | bool colorBufferIsTarget) { |
Kaiyi Li | cfd9733 | 2021-10-18 16:01:41 -0700 | [diff] [blame] | 3490 | AutoLock lock(sVkEmulationLock); |
| 3491 | |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 3492 | auto colorBufferInfo = android::base::find(sVkEmulation->colorBuffers, colorBufferHandle); |
| 3493 | if (!colorBufferInfo) { |
Kaiyi Li | cfd9733 | 2021-10-18 16:01:41 -0700 | [diff] [blame] | 3494 | VK_COMMON_ERROR("Invalid ColorBuffer handle %d.", static_cast<int>(colorBufferHandle)); |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 3495 | return nullptr; |
Kaiyi Li | cfd9733 | 2021-10-18 16:01:41 -0700 | [diff] [blame] | 3496 | } |
Jason Macnak | 741afe7 | 2022-04-14 08:19:52 -0700 | [diff] [blame] | 3497 | |
| 3498 | auto compositorInfo = std::make_unique<BorrowedImageInfoVk>(); |
| 3499 | compositorInfo->id = colorBufferInfo->handle; |
| 3500 | compositorInfo->width = colorBufferInfo->imageCreateInfoShallow.extent.width; |
| 3501 | compositorInfo->height = colorBufferInfo->imageCreateInfoShallow.extent.height; |
| 3502 | compositorInfo->image = colorBufferInfo->image; |
| 3503 | compositorInfo->imageView = colorBufferInfo->imageView; |
| 3504 | compositorInfo->imageCreateInfo = colorBufferInfo->imageCreateInfoShallow; |
| 3505 | compositorInfo->preBorrowLayout = colorBufferInfo->currentLayout; |
| 3506 | compositorInfo->preBorrowQueueFamilyIndex = colorBufferInfo->currentQueueFamilyIndex; |
| 3507 | if (colorBufferIsTarget && sVkEmulation->displayVk) { |
| 3508 | // Instruct the compositor to perform the layout transition after use so |
| 3509 | // that it is ready to be blitted to the display. |
| 3510 | compositorInfo->postBorrowQueueFamilyIndex = sVkEmulation->queueFamilyIndex; |
| 3511 | compositorInfo->postBorrowLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; |
| 3512 | } else { |
| 3513 | // Instruct the compositor to perform the queue transfer release after use |
| 3514 | // so that the color buffer can be acquired by the guest. |
| 3515 | compositorInfo->postBorrowQueueFamilyIndex = VK_QUEUE_FAMILY_EXTERNAL; |
| 3516 | compositorInfo->postBorrowLayout = colorBufferInfo->currentLayout; |
| 3517 | |
| 3518 | if (compositorInfo->postBorrowLayout == VK_IMAGE_LAYOUT_UNDEFINED) { |
| 3519 | compositorInfo->postBorrowLayout = kGuestUseDefaultImageLayout; |
| 3520 | } |
| 3521 | } |
| 3522 | |
| 3523 | colorBufferInfo->currentLayout = compositorInfo->postBorrowLayout; |
| 3524 | colorBufferInfo->currentQueueFamilyIndex = compositorInfo->postBorrowQueueFamilyIndex; |
| 3525 | |
| 3526 | return compositorInfo; |
| 3527 | } |
| 3528 | |
| 3529 | std::unique_ptr<BorrowedImageInfoVk> borrowColorBufferForDisplay(uint32_t colorBufferHandle) { |
| 3530 | AutoLock lock(sVkEmulationLock); |
| 3531 | |
| 3532 | auto colorBufferInfo = android::base::find(sVkEmulation->colorBuffers, colorBufferHandle); |
| 3533 | if (!colorBufferInfo) { |
| 3534 | VK_COMMON_ERROR("Invalid ColorBuffer handle %d.", static_cast<int>(colorBufferHandle)); |
| 3535 | return nullptr; |
| 3536 | } |
| 3537 | |
| 3538 | auto compositorInfo = std::make_unique<BorrowedImageInfoVk>(); |
| 3539 | compositorInfo->id = colorBufferInfo->handle; |
| 3540 | compositorInfo->width = colorBufferInfo->imageCreateInfoShallow.extent.width; |
| 3541 | compositorInfo->height = colorBufferInfo->imageCreateInfoShallow.extent.height; |
| 3542 | compositorInfo->image = colorBufferInfo->image; |
| 3543 | compositorInfo->imageView = colorBufferInfo->imageView; |
| 3544 | compositorInfo->imageCreateInfo = colorBufferInfo->imageCreateInfoShallow; |
| 3545 | compositorInfo->preBorrowLayout = colorBufferInfo->currentLayout; |
| 3546 | compositorInfo->preBorrowQueueFamilyIndex = sVkEmulation->queueFamilyIndex; |
| 3547 | |
| 3548 | // Instruct the display to perform the queue transfer release after use so |
| 3549 | // that the color buffer can be acquired by the guest. |
| 3550 | compositorInfo->postBorrowQueueFamilyIndex = VK_QUEUE_FAMILY_EXTERNAL; |
| 3551 | compositorInfo->postBorrowLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; |
| 3552 | |
| 3553 | colorBufferInfo->currentLayout = compositorInfo->postBorrowLayout; |
| 3554 | colorBufferInfo->currentQueueFamilyIndex = compositorInfo->postBorrowQueueFamilyIndex; |
| 3555 | |
| 3556 | return compositorInfo; |
Kaiyi Li | cfd9733 | 2021-10-18 16:01:41 -0700 | [diff] [blame] | 3557 | } |
| 3558 | |
Jason Macnak | 12c1c39 | 2023-03-14 15:15:38 -0700 | [diff] [blame] | 3559 | std::optional<uint32_t> findRepresentativeColorBufferMemoryTypeIndexLocked() { |
| 3560 | constexpr const uint32_t kArbitraryWidth = 64; |
| 3561 | constexpr const uint32_t kArbitraryHeight = 64; |
| 3562 | constexpr const uint32_t kArbitraryHandle = std::numeric_limits<uint32_t>::max(); |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 3563 | if (!createVkColorBufferLocked(kArbitraryWidth, kArbitraryHeight, GL_RGBA8, |
| 3564 | FrameworkFormat::FRAMEWORK_FORMAT_GL_COMPATIBLE, |
| 3565 | kArbitraryHandle, true, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) { |
Jason Macnak | 12c1c39 | 2023-03-14 15:15:38 -0700 | [diff] [blame] | 3566 | ERR("Failed to setup memory type index test ColorBuffer."); |
| 3567 | return std::nullopt; |
| 3568 | } |
Aaron Ruby | 4fc86ed | 2023-11-17 16:20:22 -0500 | [diff] [blame] | 3569 | if (!initializeVkColorBufferLocked(kArbitraryHandle)) { |
| 3570 | ERR("Failed to initialize memory type index test ColorBuffer."); |
| 3571 | return std::nullopt; |
| 3572 | } |
Jason Macnak | 12c1c39 | 2023-03-14 15:15:38 -0700 | [diff] [blame] | 3573 | |
| 3574 | uint32_t memoryTypeIndex = 0; |
| 3575 | if (!getColorBufferAllocationInfoLocked(kArbitraryHandle, nullptr, &memoryTypeIndex, nullptr, |
| 3576 | nullptr)) { |
| 3577 | ERR("Failed to lookup memory type index test ColorBuffer."); |
| 3578 | return std::nullopt; |
| 3579 | } |
| 3580 | |
| 3581 | if (!teardownVkColorBufferLocked(kArbitraryHandle)) { |
| 3582 | ERR("Failed to clean up memory type index test ColorBuffer."); |
| 3583 | return std::nullopt; |
| 3584 | } |
| 3585 | |
| 3586 | return memoryTypeIndex; |
| 3587 | } |
| 3588 | |
Jason Macnak | ed0c9e6 | 2023-03-30 15:58:24 -0700 | [diff] [blame] | 3589 | } // namespace vk |
| 3590 | } // namespace gfxstream |