| 1 |
/* |
| 2 |
* Copyright 2022 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/gpu/graphite/vk/VulkanSharedContext.h" |
| 9 |
|
| 10 |
#include "include/gpu/GpuTypes.h" |
| 11 |
#include "include/gpu/graphite/ContextOptions.h" |
| 12 |
#include "include/gpu/graphite/PersistentPipelineStorage.h" |
| 13 |
#include "include/gpu/vk/VulkanBackendContext.h" |
| 14 |
#include "include/gpu/vk/VulkanExtensions.h" |
| 15 |
#include "include/private/base/SkMutex.h" |
| 16 |
#include "src/gpu/GpuTypesPriv.h" |
| 17 |
#include "src/gpu/graphite/Log.h" |
| 18 |
#include "src/gpu/graphite/ResourceTypes.h" |
| 19 |
#include "src/gpu/graphite/vk/VulkanBuffer.h" |
| 20 |
#include "src/gpu/graphite/vk/VulkanCaps.h" |
| 21 |
#include "src/gpu/graphite/vk/VulkanResourceProvider.h" |
| 22 |
#include "src/gpu/vk/VulkanInterface.h" |
| 23 |
#include "src/gpu/vk/VulkanUtilsPriv.h" |
| 24 |
|
| 25 |
namespace skgpu::graphite { |
| 26 |
|
| 27 |
sk_sp<SharedContext> VulkanSharedContext::Make(const VulkanBackendContext& context, |
| 28 |
const ContextOptions& options) { |
| 29 |
if (context.fInstance == VK_NULL_HANDLE || |
| 30 |
context.fPhysicalDevice == VK_NULL_HANDLE || |
| 31 |
context.fDevice == VK_NULL_HANDLE || |
| 32 |
context.fQueue == VK_NULL_HANDLE) { |
| 33 |
SKGPU_LOG_E("Failed to create VulkanSharedContext because either fInstance," |
| 34 |
"fPhysicalDevice, fDevice, or fQueue in the VulkanBackendContext is" |
| 35 |
"VK_NULL_HANDLE."); |
| 36 |
return nullptr; |
| 37 |
} |
| 38 |
if (!context.fGetProc) { |
| 39 |
SKGPU_LOG_E("Failed to create VulkanSharedContext because there is no valid VulkanGetProc" |
| 40 |
"on the VulkanBackendContext"); |
| 41 |
return nullptr; |
| 42 |
} |
| 43 |
// If no extensions are provided, make sure we don't have a null dereference downstream. |
| 44 |
skgpu::VulkanExtensions noExtensions; |
| 45 |
const skgpu::VulkanExtensions* extensions = &noExtensions; |
| 46 |
if (context.fVkExtensions) { |
| 47 |
extensions = context.fVkExtensions; |
| 48 |
} |
| 49 |
|
| 50 |
uint32_t physDevVersion = 0; |
| 51 |
sk_sp<const skgpu::VulkanInterface> interface = |
| 52 |
skgpu::MakeInterface(context, extensions, &physDevVersion, nullptr); |
| 53 |
if (!interface) { |
| 54 |
SKGPU_LOG_E("Failed to create VulkanInterface."); |
| 55 |
return nullptr; |
| 56 |
} |
| 57 |
|
| 58 |
VkPhysicalDeviceFeatures2 features; |
| 59 |
const VkPhysicalDeviceFeatures2* featuresPtr; |
| 60 |
// If fDeviceFeatures2 is not null, then we ignore fDeviceFeatures. If both are null, we assume |
| 61 |
// no features are enabled. |
| 62 |
if (!context.fDeviceFeatures2 && context.fDeviceFeatures) { |
| 63 |
features.pNext = nullptr; |
| 64 |
features.features = *context.fDeviceFeatures; |
| 65 |
featuresPtr = &features; |
| 66 |
} else { |
| 67 |
featuresPtr = context.fDeviceFeatures2; |
| 68 |
} |
| 69 |
|
| 70 |
std::unique_ptr<const VulkanCaps> caps(new VulkanCaps(options, |
| 71 |
interface.get(), |
| 72 |
context.fPhysicalDevice, |
| 73 |
physDevVersion, |
| 74 |
featuresPtr, |
| 75 |
extensions, |
| 76 |
context.fProtectedContext)); |
| 77 |
|
| 78 |
sk_sp<skgpu::VulkanMemoryAllocator> memoryAllocator = context.fMemoryAllocator; |
| 79 |
if (!memoryAllocator) { |
| 80 |
SKGPU_LOG_E("No supplied vulkan memory allocator and unable to create one internally."); |
| 81 |
return nullptr; |
| 82 |
} |
| 83 |
|
| 84 |
return sk_sp<SharedContext>(new VulkanSharedContext(context, |
| 85 |
std::move(interface), |
| 86 |
std::move(memoryAllocator), |
| 87 |
std::move(caps), |
| 88 |
options.fExecutor, |
| 89 |
options.fPersistentPipelineStorage, |
| 90 |
options.fUserDefinedKnownRuntimeEffects)); |
| 91 |
} |
| 92 |
|
| 93 |
VulkanSharedContext::VulkanSharedContext( |
| 94 |
const VulkanBackendContext& backendContext, |
| 95 |
sk_sp<const skgpu::VulkanInterface> interface, |
| 96 |
sk_sp<skgpu::VulkanMemoryAllocator> memoryAllocator, |
| 97 |
std::unique_ptr<const VulkanCaps> caps, |
| 98 |
SkExecutor* executor, |
| 99 |
PersistentPipelineStorage* persistentPipelineStorage, |
| 100 |
SkSpan<sk_sp<SkRuntimeEffect>> userDefinedKnownRuntimeEffects) |
| 101 |
: SharedContext(std::move(caps), |
| 102 |
BackendApi::kVulkan, |
| 103 |
executor, |
| 104 |
userDefinedKnownRuntimeEffects) |
| 105 |
, fInterface(std::move(interface)) |
| 106 |
, fMemoryAllocator(std::move(memoryAllocator)) |
| 107 |
, fPhysDevice(backendContext.fPhysicalDevice) |
| 108 |
, fDevice(backendContext.fDevice) |
| 109 |
, fQueueIndex(backendContext.fGraphicsQueueIndex) |
| 110 |
, fDeviceLostContext(backendContext.fDeviceLostContext) |
| 111 |
, fDeviceLostProc(backendContext.fDeviceLostProc) { |
| 112 |
fPipelineCache = this->createPipelineCache(backendContext.fPhysicalDevice, |
| 113 |
persistentPipelineStorage); |
| 114 |
fThreadSafeResourceProvider = std::make_unique<VulkanThreadSafeResourceProvider>( |
| 115 |
this->makeResourceProvider(&fSingleOwner, |
| 116 |
SK_InvalidGenID, |
| 117 |
kThreadedSafeResourceBudget)); |
| 118 |
} |
| 119 |
|
| 120 |
VulkanSharedContext::~VulkanSharedContext() { |
| 121 |
if (fPipelineCache != VK_NULL_HANDLE) { |
| 122 |
VULKAN_CALL(this->interface(), |
| 123 |
DestroyPipelineCache(this->device(), |
| 124 |
fPipelineCache, |
| 125 |
nullptr)); |
| 126 |
fPipelineCache = VK_NULL_HANDLE; |
| 127 |
} |
| 128 |
fThreadSafeResourceProvider.reset(); |
| 129 |
|
| 130 |
// need to clear out resources before the allocator is removed |
| 131 |
this->globalCache()->deleteResources(); |
| 132 |
} |
| 133 |
|
| 134 |
VkPipelineCache VulkanSharedContext::createPipelineCache( |
| 135 |
VkPhysicalDevice physDev, |
| 136 |
PersistentPipelineStorage* persistentPipelineStorage) { |
| 137 |
VkPipelineCacheCreateInfo createInfo = {}; |
| 138 |
createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
| 139 |
createInfo.initialDataSize = 0; |
| 140 |
createInfo.pInitialData = nullptr; |
| 141 |
|
| 142 |
sk_sp<SkData> cachedData; // here to keep the cached data valid past CreatePipelineCache |
| 143 |
if (persistentPipelineStorage) { |
| 144 |
cachedData = persistentPipelineStorage->load(); |
| 145 |
|
| 146 |
// For version one of the header, the total header size is 16 bytes plus |
| 147 |
// VK_UUID_SIZE bytes. See Section 9.6 (Pipeline Cache) in the vulkan spec to see |
| 148 |
// the breakdown of these bytes. |
| 149 |
static const int kV1HeaderSize = 4*sizeof(uint32_t) + VK_UUID_SIZE; |
| 150 |
if (cachedData && cachedData->size() >= kV1HeaderSize) { |
| 151 |
const uint32_t* cacheHeader = (const uint32_t*)cachedData->data(); |
| 152 |
if (cacheHeader[1] == VK_PIPELINE_CACHE_HEADER_VERSION_ONE) { |
| 153 |
SkASSERT(cacheHeader[0] == kV1HeaderSize); |
| 154 |
|
| 155 |
VkPhysicalDeviceProperties2 props; |
| 156 |
props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2; |
| 157 |
props.pNext = nullptr; |
| 158 |
|
| 159 |
VULKAN_CALL(fInterface.get(), GetPhysicalDeviceProperties2(physDev, &props)); |
| 160 |
|
| 161 |
const VkPhysicalDeviceProperties& physDevProps = props.properties; |
| 162 |
|
| 163 |
const uint8_t* supportedPipelineCacheUUID = physDevProps.pipelineCacheUUID; |
| 164 |
if (cacheHeader[2] == physDevProps.vendorID && |
| 165 |
cacheHeader[3] == physDevProps.deviceID && |
| 166 |
memcmp(&cacheHeader[4], supportedPipelineCacheUUID, VK_UUID_SIZE) == 0) { |
| 167 |
createInfo.initialDataSize = cachedData->size(); |
| 168 |
createInfo.pInitialData = cachedData->data(); |
| 169 |
fLastKnownPersistentPipelineStorageSize = createInfo.initialDataSize; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
VkResult result; |
| 176 |
VkPipelineCache pipelineCache = VK_NULL_HANDLE; |
| 177 |
VULKAN_CALL_RESULT(this, |
| 178 |
result, |
| 179 |
CreatePipelineCache(this->device(), |
| 180 |
&createInfo, |
| 181 |
nullptr, |
| 182 |
&pipelineCache)); |
| 183 |
if (VK_SUCCESS != result) { |
| 184 |
SKGPU_LOG_W("CreatePipelineCache failed"); |
| 185 |
return VK_NULL_HANDLE; |
| 186 |
} |
| 187 |
|
| 188 |
return pipelineCache; |
| 189 |
} |
| 190 |
|
| 191 |
VulkanThreadSafeResourceProvider* VulkanSharedContext::threadSafeResourceProvider() const { |
| 192 |
return static_cast<VulkanThreadSafeResourceProvider*>(fThreadSafeResourceProvider.get()); |
| 193 |
} |
| 194 |
|
| 195 |
void VulkanSharedContext::syncPipelineData(PersistentPipelineStorage* persistentPipelineStorage, |
| 196 |
size_t maxSize) { |
| 197 |
SkASSERT(persistentPipelineStorage); |
| 198 |
|
| 199 |
if (fPipelineCache == VK_NULL_HANDLE || !fHasNewVkPipelineCacheData) { |
| 200 |
return; // ill-formed SharedContext or no new pipelines |
| 201 |
} |
| 202 |
|
| 203 |
size_t origDataSize = 0; |
| 204 |
VkResult result; |
| 205 |
VULKAN_CALL_RESULT( |
| 206 |
this, |
| 207 |
result, |
| 208 |
GetPipelineCacheData(this->device(), fPipelineCache, &origDataSize, nullptr)); |
| 209 |
if (result != VK_SUCCESS) { |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
if (!this->vulkanCaps().supportsPipelineCreationCacheControl()) { |
| 214 |
// Since we don't have cache control 'fHasNewVkPipelineCacheData' can be wildly |
| 215 |
// inaccurate. Attempt to compensate by comparing the uncapped data sizes. |
| 216 |
if (fLastKnownPersistentPipelineStorageSize == origDataSize) { |
| 217 |
return; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
size_t cappedDataSize = std::min(origDataSize, maxSize); |
| 222 |
|
| 223 |
std::unique_ptr<uint8_t[]> data(new uint8_t[cappedDataSize]); |
| 224 |
|
| 225 |
VULKAN_CALL_RESULT_NOCHECK( |
| 226 |
this->interface(), |
| 227 |
result, |
| 228 |
GetPipelineCacheData(this->device(), fPipelineCache, &cappedDataSize, (void*)data.get())); |
| 229 |
this->checkVkResult(result); |
| 230 |
if ((result != VK_SUCCESS) && (result != VK_INCOMPLETE)) { |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
fLastKnownPersistentPipelineStorageSize = origDataSize; |
| 235 |
fHasNewVkPipelineCacheData = false; |
| 236 |
persistentPipelineStorage->store(*SkData::MakeWithoutCopy(data.get(), cappedDataSize)); |
| 237 |
} |
| 238 |
|
| 239 |
std::unique_ptr<ResourceProvider> VulkanSharedContext::makeResourceProvider( |
| 240 |
SingleOwner* singleOwner, |
| 241 |
uint32_t recorderID, |
| 242 |
size_t resourceBudget) { |
| 243 |
return std::unique_ptr<ResourceProvider>( |
| 244 |
new VulkanResourceProvider(this, |
| 245 |
singleOwner, |
| 246 |
recorderID, |
| 247 |
resourceBudget)); |
| 248 |
} |
| 249 |
|
| 250 |
sk_sp<GraphicsPipeline> VulkanSharedContext::createGraphicsPipeline( |
| 251 |
const RuntimeEffectDictionary* runtimeDict, |
| 252 |
const UniqueKey& pipelineKey, |
| 253 |
const GraphicsPipelineDesc& pipelineDesc, |
| 254 |
const RenderPassDesc& renderPassDesc, |
| 255 |
SkEnumBitMask<PipelineCreationFlags> pipelineCreationFlags, |
| 256 |
uint32_t compilationID) { |
| 257 |
return VulkanGraphicsPipeline::Make(this, |
| 258 |
runtimeDict, |
| 259 |
pipelineKey, |
| 260 |
pipelineDesc, |
| 261 |
renderPassDesc, |
| 262 |
pipelineCreationFlags, |
| 263 |
compilationID); |
| 264 |
} |
| 265 |
|
| 266 |
bool VulkanSharedContext::checkVkResult(VkResult result) const { |
| 267 |
switch (result) { |
| 268 |
case VK_SUCCESS: |
| 269 |
return true; |
| 270 |
case VK_ERROR_DEVICE_LOST: |
| 271 |
{ |
| 272 |
SkAutoMutexExclusive lock(fDeviceIsLostMutex); |
| 273 |
if (fDeviceIsLost) { |
| 274 |
return false; |
| 275 |
} |
| 276 |
fDeviceIsLost = true; |
| 277 |
// Fall through to InvokeDeviceLostCallback (on first VK_ERROR_DEVICE_LOST) only afer |
| 278 |
// releasing fDeviceIsLostMutex, otherwise clients might cause deadlock by checking |
| 279 |
// isDeviceLost() from the callback. |
| 280 |
} |
| 281 |
skgpu::InvokeDeviceLostCallback(interface(), |
| 282 |
device(), |
| 283 |
fDeviceLostContext, |
| 284 |
fDeviceLostProc, |
| 285 |
vulkanCaps().supportsDeviceFaultInfo()); |
| 286 |
return false; |
| 287 |
case VK_ERROR_OUT_OF_DEVICE_MEMORY: |
| 288 |
case VK_ERROR_OUT_OF_HOST_MEMORY: |
| 289 |
// TODO: determine how we'll track this in a thread-safe manner |
| 290 |
//this->setOOMed(); |
| 291 |
return false; |
| 292 |
default: |
| 293 |
return false; |
| 294 |
} |
| 295 |
} |
| 296 |
} // namespace skgpu::graphite |