| 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/VulkanResourceProvider.h" |
| 9 |
|
| 10 |
#include "include/core/SkSpan.h" |
| 11 |
#include "include/gpu/MutableTextureState.h" |
| 12 |
#include "include/gpu/graphite/BackendTexture.h" |
| 13 |
#include "include/gpu/graphite/vk/VulkanGraphiteTypes.h" |
| 14 |
#include "include/gpu/vk/VulkanMutableTextureState.h" |
| 15 |
#include "src/gpu/graphite/Buffer.h" |
| 16 |
#include "src/gpu/graphite/ComputePipeline.h" |
| 17 |
#include "src/gpu/graphite/GraphicsPipeline.h" |
| 18 |
#include "src/gpu/graphite/RenderPassDesc.h" |
| 19 |
#include "src/gpu/graphite/Sampler.h" |
| 20 |
#include "src/gpu/graphite/Texture.h" |
| 21 |
#include "src/gpu/graphite/TextureInfoPriv.h" |
| 22 |
#include "src/gpu/graphite/vk/VulkanBuffer.h" |
| 23 |
#include "src/gpu/graphite/vk/VulkanCommandBuffer.h" |
| 24 |
#include "src/gpu/graphite/vk/VulkanDescriptorPool.h" |
| 25 |
#include "src/gpu/graphite/vk/VulkanDescriptorSet.h" |
| 26 |
#include "src/gpu/graphite/vk/VulkanFramebuffer.h" |
| 27 |
#include "src/gpu/graphite/vk/VulkanGraphicsPipeline.h" |
| 28 |
#include "src/gpu/graphite/vk/VulkanGraphiteUtils.h" |
| 29 |
#include "src/gpu/graphite/vk/VulkanRenderPass.h" |
| 30 |
#include "src/gpu/graphite/vk/VulkanSampler.h" |
| 31 |
#include "src/gpu/graphite/vk/VulkanSharedContext.h" |
| 32 |
#include "src/gpu/graphite/vk/VulkanTexture.h" |
| 33 |
#include "src/gpu/graphite/vk/VulkanYcbcrConversion.h" |
| 34 |
#include "src/gpu/vk/VulkanMemory.h" |
| 35 |
#include "src/sksl/SkSLCompiler.h" |
| 36 |
|
| 37 |
#ifdef SK_BUILD_FOR_ANDROID |
| 38 |
#include "src/gpu/vk/VulkanUtilsPriv.h" |
| 39 |
#include <android/hardware_buffer.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
namespace skgpu::graphite { |
| 43 |
|
| 44 |
constexpr int kMaxNumberOfCachedBufferDescSets = 1024; |
| 45 |
|
| 46 |
namespace { |
| 47 |
|
| 48 |
// Create a mock pipeline layout that has a compatible input attachment descriptor set layout and |
| 49 |
// push constant parameters with all other real pipeline layouts. This allows us to perform |
| 50 |
// once-per-renderpass operations even before a real pipeline is bound by the command buffer. |
| 51 |
VkPipelineLayout create_mock_layout(const VulkanSharedContext* sharedContext) { |
| 52 |
SkASSERT(sharedContext); |
| 53 |
VkPushConstantRange pushConstantRange; |
| 54 |
pushConstantRange.offset = 0; |
| 55 |
pushConstantRange.size = VulkanResourceProvider::kIntrinsicConstantSize; |
| 56 |
pushConstantRange.stageFlags = VulkanResourceProvider::kIntrinsicConstantStageFlags; |
| 57 |
|
| 58 |
skia_private::STArray<1, DescriptorData> inputDesc { |
| 59 |
VulkanGraphicsPipeline::kInputAttachmentDescriptor}; |
| 60 |
VkDescriptorSetLayout setLayout; |
| 61 |
DescriptorDataToVkDescSetLayout(sharedContext, inputDesc, &setLayout); |
| 62 |
|
| 63 |
VkPipelineLayoutCreateInfo layoutCreateInfo = {}; |
| 64 |
layoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; |
| 65 |
layoutCreateInfo.setLayoutCount = 1; |
| 66 |
layoutCreateInfo.pSetLayouts = &setLayout; |
| 67 |
layoutCreateInfo.pushConstantRangeCount = 1; |
| 68 |
layoutCreateInfo.pPushConstantRanges = &pushConstantRange; |
| 69 |
|
| 70 |
VkResult result; |
| 71 |
VkPipelineLayout pipelineLayout; |
| 72 |
VULKAN_CALL_RESULT(sharedContext, |
| 73 |
result, |
| 74 |
CreatePipelineLayout(sharedContext->device(), |
| 75 |
&layoutCreateInfo, |
| 76 |
/*const VkAllocationCallbacks*=*/nullptr, |
| 77 |
&pipelineLayout)); |
| 78 |
|
| 79 |
// Once the pipeline layout is created, we can clean up the VkDescriptorSetLayout. |
| 80 |
VULKAN_CALL(sharedContext->interface(), |
| 81 |
DestroyDescriptorSetLayout(sharedContext->device(), setLayout, nullptr)); |
| 82 |
|
| 83 |
return pipelineLayout; |
| 84 |
} |
| 85 |
|
| 86 |
} // anonymous namespace |
| 87 |
|
| 88 |
VulkanResourceProvider::VulkanResourceProvider(SharedContext* sharedContext, |
| 89 |
SingleOwner* singleOwner, |
| 90 |
uint32_t recorderID, |
| 91 |
size_t resourceBudget) |
| 92 |
: ResourceProvider(sharedContext, singleOwner, recorderID, resourceBudget) |
| 93 |
, fMockPipelineLayout( |
| 94 |
create_mock_layout(static_cast<const VulkanSharedContext*>(sharedContext))) |
| 95 |
, fUniformBufferDescSetCache(kMaxNumberOfCachedBufferDescSets) {} |
| 96 |
|
| 97 |
VulkanResourceProvider::~VulkanResourceProvider() { |
| 98 |
if (fMockPipelineLayout) { |
| 99 |
VULKAN_CALL(this->vulkanSharedContext()->interface(), |
| 100 |
DestroyPipelineLayout(this->vulkanSharedContext()->device(), |
| 101 |
fMockPipelineLayout, |
| 102 |
nullptr)); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
const VulkanSharedContext* VulkanResourceProvider::vulkanSharedContext() const { |
| 107 |
return static_cast<const VulkanSharedContext*>(fSharedContext); |
| 108 |
} |
| 109 |
|
| 110 |
VulkanSharedContext* VulkanResourceProvider::nonConstVulkanSharedContext() { |
| 111 |
return static_cast<VulkanSharedContext*>(fSharedContext); |
| 112 |
} |
| 113 |
|
| 114 |
sk_sp<Texture> VulkanResourceProvider::onCreateWrappedTexture(const BackendTexture& texture) { |
| 115 |
sk_sp<VulkanYcbcrConversion> ycbcrConversion; |
| 116 |
const auto& vkInfo = TextureInfoPriv::Get<VulkanTextureInfo>(texture.info()); |
| 117 |
if (vkInfo.fYcbcrConversionInfo.isValid()) { |
| 118 |
ycbcrConversion = this->findOrCreateCompatibleYcbcrConversion(vkInfo.fYcbcrConversionInfo); |
| 119 |
if (!ycbcrConversion) { |
| 120 |
return nullptr; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return VulkanTexture::MakeWrapped(this->vulkanSharedContext(), |
| 125 |
texture.dimensions(), |
| 126 |
texture.info(), |
| 127 |
BackendTextures::GetMutableState(texture), |
| 128 |
BackendTextures::GetVkImage(texture), |
| 129 |
/*alloc=*/{} /*Skia does not own wrapped texture memory*/, |
| 130 |
std::move(ycbcrConversion)); |
| 131 |
} |
| 132 |
|
| 133 |
sk_sp<ComputePipeline> VulkanResourceProvider::createComputePipeline(const ComputePipelineDesc&) { |
| 134 |
return nullptr; |
| 135 |
} |
| 136 |
|
| 137 |
sk_sp<Texture> VulkanResourceProvider::createTexture(SkISize size, |
| 138 |
const TextureInfo& info) { |
| 139 |
sk_sp<VulkanYcbcrConversion> ycbcrConversion; |
| 140 |
const auto& vkInfo = TextureInfoPriv::Get<VulkanTextureInfo>(info); |
| 141 |
if (vkInfo.fYcbcrConversionInfo.isValid()) { |
| 142 |
ycbcrConversion = this->findOrCreateCompatibleYcbcrConversion(vkInfo.fYcbcrConversionInfo); |
| 143 |
if (!ycbcrConversion) { |
| 144 |
return nullptr; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
return VulkanTexture::Make(this->vulkanSharedContext(), |
| 149 |
size, |
| 150 |
info, |
| 151 |
std::move(ycbcrConversion)); |
| 152 |
} |
| 153 |
|
| 154 |
sk_sp<Buffer> VulkanResourceProvider::createBuffer(size_t size, |
| 155 |
BufferType type, |
| 156 |
AccessPattern accessPattern) { |
| 157 |
return VulkanBuffer::Make(this->vulkanSharedContext(), size, type, accessPattern); |
| 158 |
} |
| 159 |
|
| 160 |
sk_sp<Sampler> VulkanResourceProvider::createSampler(const SamplerDesc& samplerDesc) { |
| 161 |
sk_sp<VulkanYcbcrConversion> ycbcrConversion = nullptr; |
| 162 |
|
| 163 |
// Non-zero conversion information means the sampler utilizes a ycbcr conversion. |
| 164 |
const bool usesYcbcrConversion = samplerDesc.isImmutable(); |
| 165 |
if (usesYcbcrConversion) { |
| 166 |
VulkanYcbcrConversionInfo ycbcrInfo = VulkanYcbcrConversion::FromImmutableSamplerInfo( |
| 167 |
samplerDesc.immutableSamplerInfo()); |
| 168 |
ycbcrConversion = this->findOrCreateCompatibleYcbcrConversion(ycbcrInfo); |
| 169 |
if (!ycbcrConversion) { |
| 170 |
return nullptr; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return VulkanSampler::Make(this->vulkanSharedContext(), |
| 175 |
samplerDesc, |
| 176 |
std::move(ycbcrConversion)); |
| 177 |
} |
| 178 |
|
| 179 |
BackendTexture VulkanResourceProvider::onCreateBackendTexture(SkISize dimensions, |
| 180 |
const TextureInfo& info) { |
| 181 |
const auto& vkTexInfo = TextureInfoPriv::Get<VulkanTextureInfo>(info); |
| 182 |
VulkanTexture::CreatedImageInfo createdTextureInfo; |
| 183 |
if (!VulkanTexture::MakeVkImage(this->vulkanSharedContext(), dimensions, info, |
| 184 |
&createdTextureInfo)) { |
| 185 |
return {}; |
| 186 |
} |
| 187 |
return BackendTextures::MakeVulkan( |
| 188 |
dimensions, |
| 189 |
vkTexInfo, |
| 190 |
skgpu::MutableTextureStates::GetVkImageLayout(createdTextureInfo.fMutableState.get()), |
| 191 |
skgpu::MutableTextureStates::GetVkQueueFamilyIndex( |
| 192 |
createdTextureInfo.fMutableState.get()), |
| 193 |
createdTextureInfo.fImage, |
| 194 |
createdTextureInfo.fMemoryAlloc); |
| 195 |
} |
| 196 |
|
| 197 |
namespace { |
| 198 |
GraphiteResourceKey build_desc_set_key(const SkSpan<DescriptorData>& requestedDescriptors) { |
| 199 |
static const ResourceType kType = GraphiteResourceKey::GenerateResourceType(); |
| 200 |
|
| 201 |
// The number of int32s needed for a key can depend on whether we use immutable samplers or not. |
| 202 |
// So, accumulte key data while passing through to check for that quantity and simply copy |
| 203 |
// into builder afterwards. |
| 204 |
skia_private::TArray<uint32_t> keyData (requestedDescriptors.size() + 1); |
| 205 |
|
| 206 |
keyData.push_back(requestedDescriptors.size()); |
| 207 |
for (const DescriptorData& desc : requestedDescriptors) { |
| 208 |
keyData.push_back(static_cast<uint8_t>(desc.fType) << 24 | |
| 209 |
desc.fBindingIndex << 16 | |
| 210 |
static_cast<uint16_t>(desc.fCount)); |
| 211 |
if (desc.fImmutableSampler) { |
| 212 |
const VulkanSampler* sampler = |
| 213 |
static_cast<const VulkanSampler*>(desc.fImmutableSampler); |
| 214 |
SkASSERT(sampler); |
| 215 |
keyData.push_back_n(sampler->samplerDesc().asSpan().size(), |
| 216 |
sampler->samplerDesc().asSpan().data()); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
GraphiteResourceKey key; |
| 221 |
GraphiteResourceKey::Builder builder(&key, kType, SkTo<uint16_t>(keyData.size())); |
| 222 |
|
| 223 |
for (int i = 0; i < keyData.size(); i++) { |
| 224 |
builder[i] = keyData[i]; |
| 225 |
} |
| 226 |
|
| 227 |
builder.finish(); |
| 228 |
return key; |
| 229 |
} |
| 230 |
|
| 231 |
sk_sp<VulkanDescriptorSet> add_new_desc_set_to_cache(const VulkanSharedContext* context, |
| 232 |
const sk_sp<VulkanDescriptorPool>& pool, |
| 233 |
const GraphiteResourceKey& descSetKey, |
| 234 |
ResourceCache* resourceCache) { |
| 235 |
sk_sp<VulkanDescriptorSet> descSet = VulkanDescriptorSet::Make(context, pool); |
| 236 |
if (!descSet) { |
| 237 |
return nullptr; |
| 238 |
} |
| 239 |
resourceCache->insertResource(descSet.get(), descSetKey, Budgeted::kYes, Shareable::kNo); |
| 240 |
|
| 241 |
return descSet; |
| 242 |
} |
| 243 |
} // anonymous namespace |
| 244 |
|
| 245 |
sk_sp<VulkanDescriptorSet> VulkanResourceProvider::findOrCreateDescriptorSet( |
| 246 |
SkSpan<DescriptorData> requestedDescriptors) { |
| 247 |
if (requestedDescriptors.empty()) { |
| 248 |
return nullptr; |
| 249 |
} |
| 250 |
|
| 251 |
// Search for available descriptor sets by assembling a key based upon the set's structure. |
| 252 |
GraphiteResourceKey key = build_desc_set_key(requestedDescriptors); |
| 253 |
if (auto descSet = fResourceCache->findAndRefResource( |
| 254 |
key, skgpu::Budgeted::kYes, Shareable::kNo)) { |
| 255 |
// A non-null resource pointer indicates we have found an available descriptor set. |
| 256 |
return sk_sp<VulkanDescriptorSet>(static_cast<VulkanDescriptorSet*>(descSet)); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
// If we did not find an existing avilable desc set, allocate sets with the appropriate layout |
| 261 |
// and add them to the cache. |
| 262 |
VkDescriptorSetLayout layout; |
| 263 |
const VulkanSharedContext* context = this->vulkanSharedContext(); |
| 264 |
DescriptorDataToVkDescSetLayout(context, requestedDescriptors, &layout); |
| 265 |
if (!layout) { |
| 266 |
return nullptr; |
| 267 |
} |
| 268 |
auto pool = VulkanDescriptorPool::Make(context, requestedDescriptors, layout); |
| 269 |
if (!pool) { |
| 270 |
VULKAN_CALL(context->interface(), DestroyDescriptorSetLayout(context->device(), |
| 271 |
layout, |
| 272 |
nullptr)); |
| 273 |
return nullptr; |
| 274 |
} |
| 275 |
|
| 276 |
// Start with allocating one descriptor set. If one cannot be successfully created, then we can |
| 277 |
// return early before attempting to allocate more. Storing a ptr to the first set also |
| 278 |
// allows us to return that later without having to perform a find operation on the cache once |
| 279 |
// all the sets are added. |
| 280 |
auto firstDescSet = |
| 281 |
add_new_desc_set_to_cache(context, pool, key, fResourceCache.get()); |
| 282 |
if (!firstDescSet) { |
| 283 |
return nullptr; |
| 284 |
} |
| 285 |
|
| 286 |
// Continue to allocate & cache the maximum number of sets so they can be easily accessed as |
| 287 |
// they're needed. |
| 288 |
for (int i = 1; i < VulkanDescriptorPool::kMaxNumSets ; i++) { |
| 289 |
auto descSet = |
| 290 |
add_new_desc_set_to_cache(context, pool, key, fResourceCache.get()); |
| 291 |
if (!descSet) { |
| 292 |
SKGPU_LOG_W("Descriptor set allocation %d of %d was unsuccessful; no more sets will be" |
| 293 |
"allocated from this pool.", i, VulkanDescriptorPool::kMaxNumSets); |
| 294 |
break; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
return firstDescSet; |
| 299 |
} |
| 300 |
|
| 301 |
namespace { |
| 302 |
|
| 303 |
VulkanResourceProvider::UniformBindGroupKey make_ubo_bind_group_key( |
| 304 |
SkSpan<DescriptorData> requestedDescriptors, |
| 305 |
SkSpan<BindBufferInfo> bindUniformBufferInfo) { |
| 306 |
VulkanResourceProvider::UniformBindGroupKey uniqueKey; |
| 307 |
{ |
| 308 |
// Each entry in the bind group needs 2 uint32_t in the key: |
| 309 |
// - buffer's unique ID: 32 bits. |
| 310 |
// - buffer's binding size: 32 bits. |
| 311 |
// We need total of 4 entries in the uniform buffer bind group. |
| 312 |
// Unused entries will be assigned zero values. |
| 313 |
VulkanResourceProvider::UniformBindGroupKey::Builder builder(&uniqueKey); |
| 314 |
|
| 315 |
for (uint32_t i = 0; i < VulkanGraphicsPipeline::kNumUniformBuffers; ++i) { |
| 316 |
builder[2 * i] = 0; |
| 317 |
builder[2 * i + 1] = 0; |
| 318 |
} |
| 319 |
|
| 320 |
for (uint32_t i = 0; i < requestedDescriptors.size(); ++i) { |
| 321 |
int descriptorBindingIndex = requestedDescriptors[i].fBindingIndex; |
| 322 |
SkASSERT(SkTo<unsigned long>(descriptorBindingIndex) < bindUniformBufferInfo.size()); |
| 323 |
SkASSERT(SkTo<unsigned long>(descriptorBindingIndex) < |
| 324 |
VulkanGraphicsPipeline::kNumUniformBuffers); |
| 325 |
const auto& bindInfo = bindUniformBufferInfo[descriptorBindingIndex]; |
| 326 |
const VulkanBuffer* boundBuffer = static_cast<const VulkanBuffer*>(bindInfo.fBuffer); |
| 327 |
SkASSERT(boundBuffer); |
| 328 |
builder[2 * descriptorBindingIndex] = boundBuffer->uniqueID().asUInt(); |
| 329 |
builder[2 * descriptorBindingIndex + 1] = bindInfo.fSize; |
| 330 |
} |
| 331 |
|
| 332 |
builder.finish(); |
| 333 |
} |
| 334 |
|
| 335 |
return uniqueKey; |
| 336 |
} |
| 337 |
|
| 338 |
void update_uniform_descriptor_set(SkSpan<DescriptorData> requestedDescriptors, |
| 339 |
SkSpan<BindBufferInfo> bindUniformBufferInfo, |
| 340 |
VkDescriptorSet descSet, |
| 341 |
const VulkanSharedContext* sharedContext) { |
| 342 |
for (size_t i = 0; i < requestedDescriptors.size(); i++) { |
| 343 |
int descriptorBindingIndex = requestedDescriptors[i].fBindingIndex; |
| 344 |
SkASSERT(SkTo<unsigned long>(descriptorBindingIndex) < bindUniformBufferInfo.size()); |
| 345 |
const auto& bindInfo = bindUniformBufferInfo[descriptorBindingIndex]; |
| 346 |
if (bindInfo.fBuffer) { |
| 347 |
#if defined(SK_DEBUG) |
| 348 |
static uint64_t maxBufferRange = |
| 349 |
sharedContext->caps()->storageBufferSupport() |
| 350 |
? sharedContext->vulkanCaps().maxStorageBufferRange() |
| 351 |
: sharedContext->vulkanCaps().maxUniformBufferRange(); |
| 352 |
SkASSERT(bindInfo.fSize <= maxBufferRange); |
| 353 |
#endif |
| 354 |
VkDescriptorBufferInfo bufferInfo = {}; |
| 355 |
auto vulkanBuffer = static_cast<const VulkanBuffer*>(bindInfo.fBuffer); |
| 356 |
bufferInfo.buffer = vulkanBuffer->vkBuffer(); |
| 357 |
bufferInfo.offset = 0; // We always use dynamic ubos so we set the base offset to 0 |
| 358 |
bufferInfo.range = bindInfo.fSize; |
| 359 |
|
| 360 |
VkWriteDescriptorSet writeInfo = {}; |
| 361 |
writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 362 |
writeInfo.dstSet = descSet; |
| 363 |
writeInfo.dstBinding = descriptorBindingIndex; |
| 364 |
writeInfo.dstArrayElement = 0; |
| 365 |
writeInfo.descriptorCount = requestedDescriptors[i].fCount; |
| 366 |
writeInfo.descriptorType = DsTypeEnumToVkDs(requestedDescriptors[i].fType); |
| 367 |
writeInfo.pBufferInfo = &bufferInfo; |
| 368 |
|
| 369 |
// TODO(b/293925059): Migrate to updating all the uniform descriptors with one driver |
| 370 |
// call. Calling UpdateDescriptorSets once to encapsulate updates to all uniform |
| 371 |
// descriptors would be ideal, but that led to issues with draws where all the UBOs |
| 372 |
// within that set would unexpectedly be assigned the same offset. Updating them one at |
| 373 |
// a time within this loop works in the meantime but is suboptimal. |
| 374 |
VULKAN_CALL(sharedContext->interface(), |
| 375 |
UpdateDescriptorSets(sharedContext->device(), |
| 376 |
/*descriptorWriteCount=*/1, |
| 377 |
&writeInfo, |
| 378 |
/*descriptorCopyCount=*/0, |
| 379 |
/*pDescriptorCopies=*/nullptr)); |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
} // anonymous namespace |
| 385 |
|
| 386 |
sk_sp<VulkanDescriptorSet> VulkanResourceProvider::findOrCreateUniformBuffersDescriptorSet( |
| 387 |
SkSpan<DescriptorData> requestedDescriptors, |
| 388 |
SkSpan<BindBufferInfo> bindUniformBufferInfo) { |
| 389 |
SkASSERT(requestedDescriptors.size() <= VulkanGraphicsPipeline::kNumUniformBuffers); |
| 390 |
|
| 391 |
auto key = make_ubo_bind_group_key(requestedDescriptors, bindUniformBufferInfo); |
| 392 |
auto* existingDescSet = fUniformBufferDescSetCache.find(key); |
| 393 |
if (existingDescSet) { |
| 394 |
return *existingDescSet; |
| 395 |
} |
| 396 |
sk_sp<VulkanDescriptorSet> newDS = this->findOrCreateDescriptorSet(requestedDescriptors); |
| 397 |
if (!newDS) { |
| 398 |
return nullptr; |
| 399 |
} |
| 400 |
|
| 401 |
update_uniform_descriptor_set(requestedDescriptors, |
| 402 |
bindUniformBufferInfo, |
| 403 |
*newDS->descriptorSet(), |
| 404 |
this->vulkanSharedContext()); |
| 405 |
return *fUniformBufferDescSetCache.insert(key, newDS); |
| 406 |
} |
| 407 |
|
| 408 |
sk_sp<VulkanRenderPass> VulkanResourceProvider::findOrCreateRenderPass( |
| 409 |
const RenderPassDesc& originalRenderPassDesc, bool compatibleOnly) { |
| 410 |
static constexpr Budgeted kBudgeted = Budgeted::kYes; |
| 411 |
static constexpr Shareable kShareable = Shareable::kYes; |
| 412 |
static const ResourceType kType = GraphiteResourceKey::GenerateResourceType(); |
| 413 |
|
| 414 |
const RenderPassDesc renderPassDesc = |
| 415 |
compatibleOnly ? MakePipelineCompatibleRenderPass(originalRenderPassDesc) |
| 416 |
: originalRenderPassDesc; |
| 417 |
|
| 418 |
GraphiteResourceKey key; |
| 419 |
{ |
| 420 |
GraphiteResourceKey::Builder builder(&key, kType, 1); |
| 421 |
builder[0] = VulkanRenderPass::GetRenderPassKey(renderPassDesc, |
| 422 |
/*compatibleForPipelineKey=*/false); |
| 423 |
} |
| 424 |
if (Resource* resource = fResourceCache->findAndRefResource(key, kBudgeted, kShareable)) { |
| 425 |
return sk_sp<VulkanRenderPass>(static_cast<VulkanRenderPass*>(resource)); |
| 426 |
} |
| 427 |
|
| 428 |
sk_sp<VulkanRenderPass> renderPass = |
| 429 |
VulkanRenderPass::Make(this->vulkanSharedContext(), renderPassDesc); |
| 430 |
if (!renderPass) { |
| 431 |
return nullptr; |
| 432 |
} |
| 433 |
|
| 434 |
fResourceCache->insertResource(renderPass.get(), key, kBudgeted, kShareable); |
| 435 |
|
| 436 |
return renderPass; |
| 437 |
} |
| 438 |
|
| 439 |
namespace { |
| 440 |
|
| 441 |
void gather_attachment_views(skia_private::TArray<VkImageView>& attachmentViews, |
| 442 |
VulkanTexture* colorTexture, |
| 443 |
VulkanTexture* resolveTexture, |
| 444 |
VulkanTexture* depthStencilTexture) { |
| 445 |
if (colorTexture) { |
| 446 |
VkImageView& colorAttachmentView = attachmentViews.push_back(); |
| 447 |
colorAttachmentView = |
| 448 |
colorTexture->getImageView(VulkanImageView::Usage::kAttachment)->imageView(); |
| 449 |
|
| 450 |
if (resolveTexture) { |
| 451 |
VkImageView& resolveView = attachmentViews.push_back(); |
| 452 |
resolveView = |
| 453 |
resolveTexture->getImageView(VulkanImageView::Usage::kAttachment)->imageView(); |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
if (depthStencilTexture) { |
| 458 |
VkImageView& stencilView = attachmentViews.push_back(); |
| 459 |
stencilView = |
| 460 |
depthStencilTexture->getImageView(VulkanImageView::Usage::kAttachment)->imageView(); |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
} // anonymous namespace |
| 465 |
|
| 466 |
sk_sp<VulkanFramebuffer> VulkanResourceProvider::findOrCreateFramebuffer( |
| 467 |
const VulkanSharedContext* context, |
| 468 |
VulkanTexture* colorTexture, |
| 469 |
VulkanTexture* resolveTexture, |
| 470 |
VulkanTexture* depthStencilTexture, |
| 471 |
const RenderPassDesc& renderPassDesc, |
| 472 |
const VulkanRenderPass& renderPass, |
| 473 |
const int width, |
| 474 |
const int height) { |
| 475 |
|
| 476 |
VulkanTexture* mainTexture = nullptr; |
| 477 |
if (colorTexture) { |
| 478 |
mainTexture = resolveTexture ? resolveTexture : colorTexture; |
| 479 |
} else { |
| 480 |
SkASSERT(depthStencilTexture); |
| 481 |
mainTexture = depthStencilTexture; |
| 482 |
} |
| 483 |
SkASSERT(mainTexture); |
| 484 |
VulkanTexture* msaaTexture = resolveTexture ? colorTexture : nullptr; |
| 485 |
|
| 486 |
// First check for a cached frame buffer. |
| 487 |
sk_sp<VulkanFramebuffer> fb = mainTexture->getCachedFramebuffer(renderPassDesc, |
| 488 |
msaaTexture, |
| 489 |
depthStencilTexture); |
| 490 |
if (fb) { |
| 491 |
return fb; |
| 492 |
} |
| 493 |
|
| 494 |
// Gather attachment views neeeded for frame buffer creation. |
| 495 |
skia_private::TArray<VkImageView> attachmentViews; |
| 496 |
gather_attachment_views(attachmentViews, colorTexture, resolveTexture, depthStencilTexture); |
| 497 |
|
| 498 |
VkFramebufferCreateInfo framebufferInfo = {}; |
| 499 |
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; |
| 500 |
framebufferInfo.renderPass = renderPass.renderPass(); |
| 501 |
framebufferInfo.attachmentCount = attachmentViews.size(); |
| 502 |
framebufferInfo.pAttachments = attachmentViews.begin(); |
| 503 |
framebufferInfo.width = width; |
| 504 |
framebufferInfo.height = height; |
| 505 |
framebufferInfo.layers = 1; |
| 506 |
fb = VulkanFramebuffer::Make(context, |
| 507 |
framebufferInfo, |
| 508 |
renderPassDesc, |
| 509 |
sk_ref_sp(msaaTexture), |
| 510 |
sk_ref_sp(depthStencilTexture)); |
| 511 |
mainTexture->addCachedFramebuffer(fb); |
| 512 |
return fb; |
| 513 |
} |
| 514 |
|
| 515 |
void VulkanResourceProvider::onDeleteBackendTexture(const BackendTexture& texture) { |
| 516 |
SkASSERT(texture.isValid()); |
| 517 |
SkASSERT(texture.backend() == BackendApi::kVulkan); |
| 518 |
|
| 519 |
VULKAN_CALL(this->vulkanSharedContext()->interface(), |
| 520 |
DestroyImage(this->vulkanSharedContext()->device(), |
| 521 |
BackendTextures::GetVkImage(texture), |
| 522 |
/*VkAllocationCallbacks=*/nullptr)); |
| 523 |
|
| 524 |
VulkanAlloc alloc = BackendTextures::GetMemoryAlloc(texture); |
| 525 |
// Free the image memory used for the BackendTexture's VkImage. |
| 526 |
// |
| 527 |
// How we do this is dependent upon on how the image was allocated (via the memory allocator or |
| 528 |
// with a direct call to the Vulkan driver) . If the VulkanAlloc's fBackendMemory is != 0, then |
| 529 |
// that means the allocator was used. Otherwise, a direct driver call was used and we should |
| 530 |
// free the VkDeviceMemory (fMemory). |
| 531 |
if (alloc.fBackendMemory) { |
| 532 |
skgpu::VulkanMemory::FreeImageMemory(this->vulkanSharedContext()->memoryAllocator(), alloc); |
| 533 |
} else { |
| 534 |
SkASSERT(alloc.fMemory != VK_NULL_HANDLE); |
| 535 |
VULKAN_CALL(this->vulkanSharedContext()->interface(), |
| 536 |
FreeMemory(this->vulkanSharedContext()->device(), alloc.fMemory, nullptr)); |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
sk_sp<VulkanYcbcrConversion> VulkanResourceProvider::findOrCreateCompatibleYcbcrConversion( |
| 541 |
const VulkanYcbcrConversionInfo& ycbcrInfo) const { |
| 542 |
static constexpr Budgeted kBudgeted = Budgeted::kYes; |
| 543 |
static constexpr Shareable kShareable = Shareable::kYes; |
| 544 |
if (!ycbcrInfo.isValid()) { |
| 545 |
return nullptr; |
| 546 |
} |
| 547 |
|
| 548 |
GraphiteResourceKey key; |
| 549 |
{ |
| 550 |
static const ResourceType kType = GraphiteResourceKey::GenerateResourceType(); |
| 551 |
static constexpr uint16_t kKeySize = 3; |
| 552 |
|
| 553 |
GraphiteResourceKey::Builder builder(&key, kType, kKeySize); |
| 554 |
ImmutableSamplerInfo packedInfo = VulkanYcbcrConversion::ToImmutableSamplerInfo(ycbcrInfo); |
| 555 |
builder[0] = packedInfo.fNonFormatYcbcrConversionInfo; |
| 556 |
builder[1] = (uint32_t) packedInfo.fFormat; |
| 557 |
builder[2] = (uint32_t) (packedInfo.fFormat >> 32); |
| 558 |
} |
| 559 |
|
| 560 |
if (Resource* resource = fResourceCache->findAndRefResource(key, kBudgeted, kShareable)) { |
| 561 |
return sk_sp(static_cast<VulkanYcbcrConversion*>(resource)); |
| 562 |
} |
| 563 |
|
| 564 |
auto ycbcrConversion = VulkanYcbcrConversion::Make(this->vulkanSharedContext(), ycbcrInfo); |
| 565 |
if (!ycbcrConversion) { |
| 566 |
return nullptr; |
| 567 |
} |
| 568 |
|
| 569 |
fResourceCache->insertResource(ycbcrConversion.get(), key, kBudgeted, kShareable); |
| 570 |
return ycbcrConversion; |
| 571 |
} |
| 572 |
|
| 573 |
sk_sp<VulkanGraphicsPipeline> VulkanResourceProvider::findOrCreateLoadMSAAPipeline( |
| 574 |
const RenderPassDesc& renderPassDesc) { |
| 575 |
if (renderPassDesc.fColorResolveAttachment.fFormat == TextureFormat::kUnsupported || |
| 576 |
renderPassDesc.fColorAttachment.fFormat == TextureFormat::kUnsupported) { |
| 577 |
SKGPU_LOG_E("Loading MSAA from resolve texture requires valid color & resolve attachment"); |
| 578 |
return nullptr; |
| 579 |
} |
| 580 |
|
| 581 |
// Check to see if we already have a suitable pipeline that we can use. |
| 582 |
const uint32_t compatibleRenderPassHash = |
| 583 |
VulkanRenderPass::GetRenderPassKey(renderPassDesc, /*compatibleForPipelineKey=*/true); |
| 584 |
for (int i = 0; i < fLoadMSAAPipelines.size(); i++) { |
| 585 |
if (compatibleRenderPassHash == fLoadMSAAPipelines.at(i).first) { |
| 586 |
return fLoadMSAAPipelines.at(i).second; |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
if (!fLoadMSAAProgram) { |
| 591 |
// Lazily create the modules and pipeline layout the first time we need to load MSAA |
| 592 |
fLoadMSAAProgram = |
| 593 |
VulkanGraphicsPipeline::CreateLoadMSAAProgram(this->vulkanSharedContext()); |
| 594 |
if (!fLoadMSAAProgram) { |
| 595 |
SKGPU_LOG_E("Failed to initialize MSAA load pipeline creation structure(s)"); |
| 596 |
return nullptr; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
sk_sp<VulkanGraphicsPipeline> pipeline = VulkanGraphicsPipeline::MakeLoadMSAAPipeline( |
| 601 |
this->nonConstVulkanSharedContext(), *fLoadMSAAProgram, renderPassDesc); |
| 602 |
if (!pipeline) { |
| 603 |
SKGPU_LOG_E("Failed to create MSAA load pipeline"); |
| 604 |
return nullptr; |
| 605 |
} |
| 606 |
|
| 607 |
fLoadMSAAPipelines.push_back(std::make_pair(compatibleRenderPassHash, pipeline)); |
| 608 |
return pipeline; |
| 609 |
} |
| 610 |
|
| 611 |
VulkanThreadSafeResourceProvider::VulkanThreadSafeResourceProvider( |
| 612 |
std::unique_ptr<ResourceProvider> resourceProvider) |
| 613 |
: ThreadSafeResourceProvider(std::move(resourceProvider)) {} |
| 614 |
|
| 615 |
sk_sp<VulkanRenderPass> VulkanThreadSafeResourceProvider::findOrCreateRenderPass( |
| 616 |
const RenderPassDesc& renderPassDesc, |
| 617 |
bool compatibleOnly) { |
| 618 |
SkAutoSpinlock lock{fSpinLock}; |
| 619 |
|
| 620 |
VulkanResourceProvider* vkResourceProvider = |
| 621 |
static_cast<VulkanResourceProvider*>(fWrappedProvider.get()); |
| 622 |
|
| 623 |
sk_sp<VulkanRenderPass> renderPass = |
| 624 |
vkResourceProvider->findOrCreateRenderPass(renderPassDesc, compatibleOnly); |
| 625 |
SkAssertResult(renderPass->gpuMemorySize() == 0); |
| 626 |
|
| 627 |
return renderPass; |
| 628 |
} |
| 629 |
|
| 630 |
#ifdef SK_BUILD_FOR_ANDROID |
| 631 |
|
| 632 |
BackendTexture VulkanResourceProvider::onCreateBackendTexture(AHardwareBuffer* hardwareBuffer, |
| 633 |
bool isRenderable, |
| 634 |
bool isProtectedContent, |
| 635 |
SkISize dimensions, |
| 636 |
bool fromAndroidWindow) const { |
| 637 |
|
| 638 |
const VulkanSharedContext* vkContext = this->vulkanSharedContext(); |
| 639 |
VkDevice device = vkContext->device(); |
| 640 |
const VulkanCaps& vkCaps = vkContext->vulkanCaps(); |
| 641 |
|
| 642 |
VkAndroidHardwareBufferFormatPropertiesANDROID hwbFormatProps; |
| 643 |
VkAndroidHardwareBufferPropertiesANDROID hwbProps; |
| 644 |
if (!skgpu::GetAHardwareBufferProperties( |
| 645 |
&hwbFormatProps, &hwbProps, vkContext->interface(), hardwareBuffer, device)) { |
| 646 |
return {}; |
| 647 |
} |
| 648 |
|
| 649 |
// Import as external if the AHardwareBuffer has an undefined format or if graphite does not |
| 650 |
// support the provided VkFormat. |
| 651 |
bool importAsExternalFormat = hwbFormatProps.format == VK_FORMAT_UNDEFINED || |
| 652 |
!vkCaps.isFormatSupported(hwbFormatProps.format); |
| 653 |
#if defined(SK_DEBUG) |
| 654 |
if (importAsExternalFormat && hwbFormatProps.format != VK_FORMAT_UNDEFINED) { |
| 655 |
SKGPU_LOG_D("Ignoring AHardwareBuffer VkFormat(%d) because it is not supported by graphite." |
| 656 |
" Falling back to importing as external format.\n", hwbFormatProps.format); |
| 657 |
} |
| 658 |
#endif |
| 659 |
|
| 660 |
// Start to assemble VulkanTextureInfo which is needed later on to create the VkImage but can |
| 661 |
// sooner help us query VulkanCaps for certain format feature support. |
| 662 |
// |
| 663 |
// Note that the optimal tiling is always the right choice, including for external formats |
| 664 |
// (which always require optimal) and AHBs (where the real layout is determined by AHB usage |
| 665 |
// flags, and optimal tiling would automatically mean linear if it has to). |
| 666 |
VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL; |
| 667 |
VkImageCreateFlags imgCreateflags = isProtectedContent ? VK_IMAGE_CREATE_PROTECTED_BIT : 0; |
| 668 |
VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT; |
| 669 |
// When importing as an external format the image usage can only be VK_IMAGE_USAGE_SAMPLED_BIT. |
| 670 |
if (!importAsExternalFormat) { |
| 671 |
usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 672 |
if (isRenderable) { |
| 673 |
// Renderable attachments can be used as input attachments if we are loading from MSAA. |
| 674 |
usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT; |
| 675 |
// Opt into MSAA-render-to-SS to match getDefaultSampled/AttachmentInfo in VulkanCaps |
| 676 |
if (vkCaps.msaaRenderToSingleSampledSupport()) { |
| 677 |
imgCreateflags |= VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT; |
| 678 |
} |
| 679 |
} |
| 680 |
// For non-renderable Graphite-created textures, VulkanCaps also attempts to use |
| 681 |
// VK_IMAGE_USAGE_HOST_TRANSFER_BIT, but if we are wrapping an AHardwareBuffer, its contents |
| 682 |
// are presumably managed by the client and we aren't expecting to have to write directly |
| 683 |
// to it from CPU memory. |
| 684 |
} |
| 685 |
VulkanTextureInfo vkTexInfo { |
| 686 |
VK_SAMPLE_COUNT_1_BIT, |
| 687 |
Mipmapped::kNo, |
| 688 |
imgCreateflags, |
| 689 |
importAsExternalFormat ? VK_FORMAT_UNDEFINED : hwbFormatProps.format, |
| 690 |
tiling, |
| 691 |
usageFlags, |
| 692 |
VK_SHARING_MODE_EXCLUSIVE, |
| 693 |
VK_IMAGE_ASPECT_COLOR_BIT, |
| 694 |
VulkanYcbcrConversionInfo() }; |
| 695 |
|
| 696 |
if (isRenderable && (importAsExternalFormat || !vkCaps.isRenderable(vkTexInfo))) { |
| 697 |
SKGPU_LOG_W("Renderable texture requested from an AHardwareBuffer which uses a VkFormat " |
| 698 |
"that Skia cannot render to (VkFormat: %d).\n", hwbFormatProps.format); |
| 699 |
return {}; |
| 700 |
} |
| 701 |
|
| 702 |
if (!importAsExternalFormat && (!vkCaps.isTransferSrc(vkTexInfo) || |
| 703 |
!vkCaps.isTransferDst(vkTexInfo) || |
| 704 |
!vkCaps.isTexturable(vkTexInfo))) { |
| 705 |
if (isRenderable) { |
| 706 |
SKGPU_LOG_W("VkFormat %d does not support the necessary format features. Because a " |
| 707 |
"renderable texture was requested, we cannot fall back to importing with " |
| 708 |
"an external format.\n", hwbFormatProps.format); |
| 709 |
return {}; |
| 710 |
} |
| 711 |
// If the VkFormat does not support the features we need, then import as an external format. |
| 712 |
importAsExternalFormat = true; |
| 713 |
// If we use VkExternalFormatANDROID with an externalFormat != 0, then format must = |
| 714 |
// VK_FORMAT_UNDEFINED. |
| 715 |
vkTexInfo.fFormat = VK_FORMAT_UNDEFINED; |
| 716 |
vkTexInfo.fImageUsageFlags = VK_IMAGE_USAGE_SAMPLED_BIT; |
| 717 |
} |
| 718 |
|
| 719 |
VulkanYcbcrConversionInfo ycbcrInfo; |
| 720 |
VkExternalFormatANDROID externalFormat; |
| 721 |
externalFormat.sType = VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID; |
| 722 |
externalFormat.pNext = nullptr; |
| 723 |
externalFormat.externalFormat = 0; // If this is zero it is as if we aren't using this struct. |
| 724 |
if (importAsExternalFormat || skgpu::VkFormatNeedsYcbcrSampler(hwbFormatProps.format)) { |
| 725 |
GetYcbcrConversionInfoFromFormatProps(&ycbcrInfo, hwbFormatProps); |
| 726 |
if (!ycbcrInfo.isValid()) { |
| 727 |
SKGPU_LOG_W("Failed to create valid YCbCr conversion information from hardware buffer" |
| 728 |
"format properties.\n"); |
| 729 |
return {}; |
| 730 |
} |
| 731 |
vkTexInfo.fYcbcrConversionInfo = ycbcrInfo; |
| 732 |
externalFormat.externalFormat = hwbFormatProps.externalFormat; |
| 733 |
} |
| 734 |
const VkExternalMemoryImageCreateInfo externalMemoryImageInfo{ |
| 735 |
VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO, // sType |
| 736 |
&externalFormat, // pNext |
| 737 |
VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID, // handleTypes |
| 738 |
}; |
| 739 |
|
| 740 |
SkASSERT(!(vkTexInfo.fFlags & VK_IMAGE_CREATE_PROTECTED_BIT) || |
| 741 |
fSharedContext->isProtected() == Protected::kYes); |
| 742 |
|
| 743 |
const VkImageCreateInfo imageCreateInfo = { |
| 744 |
VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType |
| 745 |
&externalMemoryImageInfo, // pNext |
| 746 |
vkTexInfo.fFlags, // VkImageCreateFlags |
| 747 |
VK_IMAGE_TYPE_2D, // VkImageType |
| 748 |
vkTexInfo.fFormat, // VkFormat |
| 749 |
{ (uint32_t)dimensions.fWidth, (uint32_t)dimensions.fHeight, 1 }, // VkExtent3D |
| 750 |
1, // mipLevels |
| 751 |
1, // arrayLayers |
| 752 |
VK_SAMPLE_COUNT_1_BIT, // samples |
| 753 |
vkTexInfo.fImageTiling, // VkImageTiling |
| 754 |
vkTexInfo.fImageUsageFlags, // VkImageUsageFlags |
| 755 |
vkTexInfo.fSharingMode, // VkSharingMode |
| 756 |
0, // queueFamilyCount |
| 757 |
nullptr, // pQueueFamilyIndices |
| 758 |
VK_IMAGE_LAYOUT_UNDEFINED, // initialLayout |
| 759 |
}; |
| 760 |
|
| 761 |
VkResult result; |
| 762 |
VkImage image; |
| 763 |
VULKAN_CALL_RESULT(vkContext, result, CreateImage(device, &imageCreateInfo, nullptr, &image)); |
| 764 |
if (result != VK_SUCCESS) { |
| 765 |
return {}; |
| 766 |
} |
| 767 |
|
| 768 |
const VkPhysicalDeviceMemoryProperties2& phyDevMemProps = |
| 769 |
vkContext->vulkanCaps().physicalDeviceMemoryProperties2(); |
| 770 |
VulkanAlloc alloc; |
| 771 |
if (!AllocateAndBindImageMemory(&alloc, image, phyDevMemProps, hwbProps, hardwareBuffer, |
| 772 |
vkContext->interface(), device)) { |
| 773 |
VULKAN_CALL(vkContext->interface(), DestroyImage(device, image, nullptr)); |
| 774 |
return {}; |
| 775 |
} |
| 776 |
|
| 777 |
return BackendTextures::MakeVulkan(dimensions, |
| 778 |
vkTexInfo, |
| 779 |
VK_IMAGE_LAYOUT_UNDEFINED, |
| 780 |
VK_QUEUE_FAMILY_FOREIGN_EXT, |
| 781 |
image, |
| 782 |
alloc); |
| 783 |
} |
| 784 |
|
| 785 |
#endif // SK_BUILD_FOR_ANDROID |
| 786 |
|
| 787 |
} // namespace skgpu::graphite |