| 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/VulkanTexture.h" |
| 9 |
|
| 10 |
#include "include/gpu/MutableTextureState.h" |
| 11 |
#include "include/gpu/graphite/vk/VulkanGraphiteTypes.h" |
| 12 |
#include "include/gpu/vk/VulkanMutableTextureState.h" |
| 13 |
#include "src/core/SkCompressedDataUtils.h" |
| 14 |
#include "src/core/SkMipmap.h" |
| 15 |
#include "src/gpu/DataUtils.h" |
| 16 |
#include "src/gpu/graphite/Log.h" |
| 17 |
#include "src/gpu/graphite/Sampler.h" |
| 18 |
#include "src/gpu/graphite/task/UploadTask.h" |
| 19 |
#include "src/gpu/graphite/vk/VulkanCaps.h" |
| 20 |
#include "src/gpu/graphite/vk/VulkanCommandBuffer.h" |
| 21 |
#include "src/gpu/graphite/vk/VulkanDescriptorSet.h" |
| 22 |
#include "src/gpu/graphite/vk/VulkanFramebuffer.h" |
| 23 |
#include "src/gpu/graphite/vk/VulkanGraphiteUtils.h" |
| 24 |
#include "src/gpu/graphite/vk/VulkanResourceProvider.h" |
| 25 |
#include "src/gpu/graphite/vk/VulkanSharedContext.h" |
| 26 |
#include "src/gpu/vk/VulkanMemory.h" |
| 27 |
#include "src/gpu/vk/VulkanMutableTextureStatePriv.h" |
| 28 |
|
| 29 |
using namespace skia_private; |
| 30 |
|
| 31 |
namespace skgpu::graphite { |
| 32 |
|
| 33 |
bool VulkanTexture::MakeVkImage(const VulkanSharedContext* sharedContext, |
| 34 |
SkISize dimensions, |
| 35 |
const TextureInfo& info, |
| 36 |
CreatedImageInfo* outInfo) { |
| 37 |
SkASSERT(outInfo); |
| 38 |
const VulkanCaps& caps = sharedContext->vulkanCaps(); |
| 39 |
|
| 40 |
if (dimensions.isEmpty()) { |
| 41 |
SKGPU_LOG_E("Tried to create VkImage with empty dimensions."); |
| 42 |
return false; |
| 43 |
} |
| 44 |
if (dimensions.width() > caps.maxTextureSize() || |
| 45 |
dimensions.height() > caps.maxTextureSize()) { |
| 46 |
SKGPU_LOG_E("Tried to create VkImage with too large a size."); |
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
if ((info.isProtected() == Protected::kYes) != caps.protectedSupport()) { |
| 51 |
SKGPU_LOG_E("Tried to create %s VkImage in %s Context.", |
| 52 |
info.isProtected() == Protected::kYes ? "protected" : "unprotected", |
| 53 |
caps.protectedSupport() ? "protected" : "unprotected"); |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
const auto& vkInfo = TextureInfoPriv::Get<VulkanTextureInfo>(info); |
| 58 |
|
| 59 |
bool isLinear = vkInfo.fImageTiling == VK_IMAGE_TILING_LINEAR; |
| 60 |
VkImageLayout initialLayout = isLinear ? VK_IMAGE_LAYOUT_PREINITIALIZED |
| 61 |
: VK_IMAGE_LAYOUT_UNDEFINED; |
| 62 |
|
| 63 |
// Create Image |
| 64 |
VkSampleCountFlagBits vkSamples = SampleCountToVkSampleCount(info.sampleCount()); |
| 65 |
|
| 66 |
SkASSERT(!isLinear || vkSamples == VK_SAMPLE_COUNT_1_BIT); |
| 67 |
SkASSERT(info.isProtected() == Protected::kNo || |
| 68 |
(caps.protectedSupport() && SkToBool(VK_IMAGE_CREATE_PROTECTED_BIT & vkInfo.fFlags))); |
| 69 |
|
| 70 |
uint32_t numMipLevels = 1; |
| 71 |
if (vkInfo.fMipmapped == Mipmapped::kYes) { |
| 72 |
numMipLevels = SkMipmap::ComputeLevelCount(dimensions) + 1; |
| 73 |
} |
| 74 |
|
| 75 |
uint32_t width = static_cast<uint32_t>(dimensions.fWidth); |
| 76 |
uint32_t height = static_cast<uint32_t>(dimensions.fHeight); |
| 77 |
|
| 78 |
const VkImageCreateInfo imageCreateInfo = { |
| 79 |
VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType |
| 80 |
nullptr, // pNext |
| 81 |
vkInfo.fFlags, // VkImageCreateFlags |
| 82 |
VK_IMAGE_TYPE_2D, // VkImageType |
| 83 |
vkInfo.fFormat, // VkFormat |
| 84 |
{ width, height, 1 }, // VkExtent3D |
| 85 |
numMipLevels, // mipLevels |
| 86 |
1, // arrayLayers |
| 87 |
vkSamples, // samples |
| 88 |
vkInfo.fImageTiling, // VkImageTiling |
| 89 |
vkInfo.fImageUsageFlags, // VkImageUsageFlags |
| 90 |
vkInfo.fSharingMode, // VkSharingMode |
| 91 |
0, // queueFamilyCount |
| 92 |
nullptr, // pQueueFamilyIndices |
| 93 |
initialLayout // initialLayout |
| 94 |
}; |
| 95 |
|
| 96 |
auto device = sharedContext->device(); |
| 97 |
|
| 98 |
VkImage image = VK_NULL_HANDLE; |
| 99 |
VkResult result; |
| 100 |
VULKAN_CALL_RESULT( |
| 101 |
sharedContext, result, CreateImage(device, &imageCreateInfo, nullptr, &image)); |
| 102 |
if (result != VK_SUCCESS) { |
| 103 |
SKGPU_LOG_E("Failed call to vkCreateImage with error: %d", result); |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
auto allocator = sharedContext->memoryAllocator(); |
| 108 |
bool forceDedicatedMemory = caps.shouldAlwaysUseDedicatedImageMemory(); |
| 109 |
bool useLazyAllocation = |
| 110 |
SkToBool(vkInfo.fImageUsageFlags & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT); |
| 111 |
|
| 112 |
auto checkResult = [sharedContext](VkResult result) { |
| 113 |
return sharedContext->checkVkResult(result); |
| 114 |
}; |
| 115 |
if (!skgpu::VulkanMemory::AllocImageMemory(allocator, |
| 116 |
image, |
| 117 |
info.isProtected(), |
| 118 |
forceDedicatedMemory, |
| 119 |
useLazyAllocation, |
| 120 |
checkResult, |
| 121 |
&outInfo->fMemoryAlloc)) { |
| 122 |
// If lazy memory allocation fails, fallback to attempting to use a regular allocation. |
| 123 |
if (useLazyAllocation && |
| 124 |
skgpu::VulkanMemory::AllocImageMemory(allocator, |
| 125 |
image, |
| 126 |
info.isProtected(), |
| 127 |
forceDedicatedMemory, |
| 128 |
/*useLazyAllocation=*/false, |
| 129 |
checkResult, |
| 130 |
&outInfo->fMemoryAlloc)) { |
| 131 |
SKGPU_LOG_W("Could not allocate lazy image memory; using non-lazy instead."); |
| 132 |
useLazyAllocation = false; |
| 133 |
} else { |
| 134 |
const char* protectednessStr = |
| 135 |
info.isProtected() == Protected::kYes ? "protected" : "unprotected"; |
| 136 |
const char* memoryTypeStr = forceDedicatedMemory ? "dedicated" : "shared"; |
| 137 |
SKGPU_LOG_E("Failed to allocate %s %s image memory.", protectednessStr, memoryTypeStr); |
| 138 |
|
| 139 |
VULKAN_CALL(sharedContext->interface(), DestroyImage(device, image, nullptr)); |
| 140 |
return false; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
VULKAN_CALL_RESULT( |
| 145 |
sharedContext, |
| 146 |
result, |
| 147 |
BindImageMemory( |
| 148 |
device, image, outInfo->fMemoryAlloc.fMemory, outInfo->fMemoryAlloc.fOffset)); |
| 149 |
if (result != VK_SUCCESS) { |
| 150 |
skgpu::VulkanMemory::FreeImageMemory(allocator, outInfo->fMemoryAlloc); |
| 151 |
VULKAN_CALL(sharedContext->interface(), DestroyImage(device, image, nullptr)); |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
outInfo->fImage = image; |
| 156 |
outInfo->fMutableState = sk_make_sp<MutableTextureState>( |
| 157 |
skgpu::MutableTextureStates::MakeVulkan(initialLayout, VK_QUEUE_FAMILY_IGNORED)); |
| 158 |
return true; |
| 159 |
} |
| 160 |
|
| 161 |
sk_sp<Texture> VulkanTexture::Make(const VulkanSharedContext* sharedContext, |
| 162 |
SkISize dimensions, |
| 163 |
const TextureInfo& info, |
| 164 |
sk_sp<VulkanYcbcrConversion> ycbcrConversion) { |
| 165 |
CreatedImageInfo imageInfo; |
| 166 |
if (!MakeVkImage(sharedContext, dimensions, info, &imageInfo)) { |
| 167 |
return nullptr; |
| 168 |
} |
| 169 |
|
| 170 |
return sk_sp<Texture>(new VulkanTexture(sharedContext, |
| 171 |
dimensions, |
| 172 |
info, |
| 173 |
std::move(imageInfo.fMutableState), |
| 174 |
imageInfo.fImage, |
| 175 |
imageInfo.fMemoryAlloc, |
| 176 |
Ownership::kOwned, |
| 177 |
std::move(ycbcrConversion))); |
| 178 |
} |
| 179 |
|
| 180 |
sk_sp<Texture> VulkanTexture::MakeWrapped(const VulkanSharedContext* sharedContext, |
| 181 |
SkISize dimensions, |
| 182 |
const TextureInfo& info, |
| 183 |
sk_sp<MutableTextureState> mutableState, |
| 184 |
VkImage image, |
| 185 |
const VulkanAlloc& alloc, |
| 186 |
sk_sp<VulkanYcbcrConversion> ycbcrConversion) { |
| 187 |
return sk_sp<Texture>(new VulkanTexture(sharedContext, |
| 188 |
dimensions, |
| 189 |
info, |
| 190 |
std::move(mutableState), |
| 191 |
image, |
| 192 |
alloc, |
| 193 |
Ownership::kWrapped, |
| 194 |
std::move(ycbcrConversion))); |
| 195 |
} |
| 196 |
|
| 197 |
VulkanTexture::~VulkanTexture() {} |
| 198 |
|
| 199 |
void VulkanTexture::setImageLayoutAndQueueIndex(VulkanCommandBuffer* cmdBuffer, |
| 200 |
VkImageLayout newLayout, |
| 201 |
VkAccessFlags dstAccessMask, |
| 202 |
VkPipelineStageFlags dstStageMask, |
| 203 |
uint32_t newQueueFamilyIndex) const { |
| 204 |
|
| 205 |
SkASSERT(newLayout == this->currentLayout() || |
| 206 |
(VK_IMAGE_LAYOUT_UNDEFINED != newLayout && |
| 207 |
VK_IMAGE_LAYOUT_PREINITIALIZED != newLayout)); |
| 208 |
VkImageLayout currentLayout = this->currentLayout(); |
| 209 |
uint32_t currentQueueIndex = this->currentQueueFamilyIndex(); |
| 210 |
|
| 211 |
const auto& textureInfo = this->vulkanTextureInfo(); |
| 212 |
auto sharedContext = static_cast<const VulkanSharedContext*>(this->sharedContext()); |
| 213 |
|
| 214 |
// Enable the following block on new devices to test that their lazy images stay at 0 memory use |
| 215 |
#if 0 |
| 216 |
auto device = sharedContext->device(); |
| 217 |
if (fAlloc.fFlags & skgpu::VulkanAlloc::kLazilyAllocated_Flag) { |
| 218 |
VkDeviceSize size; |
| 219 |
VULKAN_CALL(sharedContext->interface(), |
| 220 |
GetDeviceMemoryCommitment(device, fAlloc.fMemory, &size)); |
| 221 |
SkDebugf("Lazy Image. This: %p, image: %d, size: %d\n", this, fImage, size); |
| 222 |
} |
| 223 |
#endif |
| 224 |
#ifdef SK_DEBUG |
| 225 |
if (textureInfo.fSharingMode == VK_SHARING_MODE_CONCURRENT) { |
| 226 |
if (newQueueFamilyIndex == VK_QUEUE_FAMILY_IGNORED) { |
| 227 |
SkASSERT(currentQueueIndex == VK_QUEUE_FAMILY_IGNORED || |
| 228 |
currentQueueIndex == VK_QUEUE_FAMILY_EXTERNAL || |
| 229 |
currentQueueIndex == VK_QUEUE_FAMILY_FOREIGN_EXT); |
| 230 |
} else { |
| 231 |
SkASSERT(newQueueFamilyIndex == VK_QUEUE_FAMILY_EXTERNAL || |
| 232 |
newQueueFamilyIndex == VK_QUEUE_FAMILY_FOREIGN_EXT); |
| 233 |
SkASSERT(currentQueueIndex == VK_QUEUE_FAMILY_IGNORED); |
| 234 |
} |
| 235 |
} else { |
| 236 |
SkASSERT(textureInfo.fSharingMode == VK_SHARING_MODE_EXCLUSIVE); |
| 237 |
if (newQueueFamilyIndex == VK_QUEUE_FAMILY_IGNORED || |
| 238 |
currentQueueIndex == sharedContext->queueIndex()) { |
| 239 |
SkASSERT(currentQueueIndex == VK_QUEUE_FAMILY_IGNORED || |
| 240 |
currentQueueIndex == VK_QUEUE_FAMILY_EXTERNAL || |
| 241 |
currentQueueIndex == VK_QUEUE_FAMILY_FOREIGN_EXT || |
| 242 |
currentQueueIndex == sharedContext->queueIndex()); |
| 243 |
} else if (newQueueFamilyIndex == VK_QUEUE_FAMILY_EXTERNAL || |
| 244 |
newQueueFamilyIndex == VK_QUEUE_FAMILY_FOREIGN_EXT) { |
| 245 |
SkASSERT(currentQueueIndex == VK_QUEUE_FAMILY_IGNORED || |
| 246 |
currentQueueIndex == sharedContext->queueIndex()); |
| 247 |
} |
| 248 |
} |
| 249 |
#endif |
| 250 |
|
| 251 |
if (textureInfo.fSharingMode == VK_SHARING_MODE_EXCLUSIVE) { |
| 252 |
if (newQueueFamilyIndex == VK_QUEUE_FAMILY_IGNORED) { |
| 253 |
newQueueFamilyIndex = sharedContext->queueIndex(); |
| 254 |
} |
| 255 |
if (currentQueueIndex == VK_QUEUE_FAMILY_IGNORED) { |
| 256 |
currentQueueIndex = sharedContext->queueIndex(); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
// If the old and new layout are the same and the layout is a read only layout, there is no need |
| 261 |
// to put in a barrier unless we also need to switch queues. |
| 262 |
if (newLayout == currentLayout && currentQueueIndex == newQueueFamilyIndex && |
| 263 |
(VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL == currentLayout || |
| 264 |
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == currentLayout || |
| 265 |
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == currentLayout)) { |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
VkAccessFlags srcAccessMask = VulkanTexture::LayoutToSrcAccessMask(currentLayout); |
| 270 |
VkPipelineStageFlags srcStageMask = VulkanTexture::LayoutToPipelineSrcStageFlags(currentLayout); |
| 271 |
|
| 272 |
VkImageAspectFlags aspectFlags = |
| 273 |
GetVkImageAspectFlags(TextureInfoPriv::ViewFormat(this->textureInfo())); |
| 274 |
uint32_t numMipLevels = 1; |
| 275 |
SkISize dimensions = this->dimensions(); |
| 276 |
if (this->mipmapped() == Mipmapped::kYes) { |
| 277 |
numMipLevels = SkMipmap::ComputeLevelCount(dimensions) + 1; |
| 278 |
} |
| 279 |
VkImageMemoryBarrier imageMemoryBarrier = { |
| 280 |
VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType |
| 281 |
nullptr, // pNext |
| 282 |
srcAccessMask, // srcAccessMask |
| 283 |
dstAccessMask, // dstAccessMask |
| 284 |
currentLayout, // oldLayout |
| 285 |
newLayout, // newLayout |
| 286 |
currentQueueIndex, // srcQueueFamilyIndex |
| 287 |
newQueueFamilyIndex, // dstQueueFamilyIndex |
| 288 |
fImage, // image |
| 289 |
{ aspectFlags, 0, numMipLevels, 0, 1 } // subresourceRange |
| 290 |
}; |
| 291 |
SkASSERT(srcAccessMask == imageMemoryBarrier.srcAccessMask); |
| 292 |
cmdBuffer->addImageMemoryBarrier(this, srcStageMask, dstStageMask, /*byRegion=*/false, |
| 293 |
&imageMemoryBarrier); |
| 294 |
|
| 295 |
skgpu::MutableTextureStates::SetVkImageLayout(this->mutableState(), newLayout); |
| 296 |
skgpu::MutableTextureStates::SetVkQueueFamilyIndex(this->mutableState(), newQueueFamilyIndex); |
| 297 |
} |
| 298 |
|
| 299 |
namespace { |
| 300 |
|
| 301 |
bool uses_lazy_memory(const VulkanAlloc& alloc) { |
| 302 |
return alloc.fFlags & VulkanAlloc::Flag::kLazilyAllocated_Flag; |
| 303 |
} |
| 304 |
|
| 305 |
#ifdef SK_DEBUG |
| 306 |
bool has_transient_usage(const TextureInfo& info) { |
| 307 |
const auto& vkInfo = TextureInfoPriv::Get<VulkanTextureInfo>(info); |
| 308 |
return vkInfo.fImageUsageFlags & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT; |
| 309 |
} |
| 310 |
#endif |
| 311 |
|
| 312 |
} // anonymous |
| 313 |
|
| 314 |
VulkanTexture::VulkanTexture(const VulkanSharedContext* sharedContext, |
| 315 |
SkISize dimensions, |
| 316 |
const TextureInfo& info, |
| 317 |
sk_sp<MutableTextureState> mutableState, |
| 318 |
VkImage image, |
| 319 |
const VulkanAlloc& alloc, |
| 320 |
Ownership ownership, |
| 321 |
sk_sp<VulkanYcbcrConversion> ycbcrConversion) |
| 322 |
: Texture(sharedContext, |
| 323 |
dimensions, |
| 324 |
info, |
| 325 |
uses_lazy_memory(alloc), |
| 326 |
std::move(mutableState), |
| 327 |
ownership) |
| 328 |
, fImage(image) |
| 329 |
, fMemoryAlloc(alloc) |
| 330 |
, fYcbcrConversion(std::move(ycbcrConversion)) { |
| 331 |
SkASSERT(!uses_lazy_memory(fMemoryAlloc) || has_transient_usage(info)); |
| 332 |
} |
| 333 |
|
| 334 |
void VulkanTexture::freeGpuData() { |
| 335 |
// Need to delete any ImageViews first |
| 336 |
fImageViews.clear(); |
| 337 |
|
| 338 |
// If the texture is wrapped we don't own this data |
| 339 |
if (this->ownership() != Ownership::kWrapped) { |
| 340 |
auto sharedContext = static_cast<const VulkanSharedContext*>(this->sharedContext()); |
| 341 |
VULKAN_CALL(sharedContext->interface(), |
| 342 |
DestroyImage(sharedContext->device(), fImage, nullptr)); |
| 343 |
skgpu::VulkanMemory::FreeImageMemory(sharedContext->memoryAllocator(), fMemoryAlloc); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
void VulkanTexture::updateImageLayout(VkImageLayout newLayout) { |
| 348 |
skgpu::MutableTextureStates::SetVkImageLayout(this->mutableState(), newLayout); |
| 349 |
} |
| 350 |
|
| 351 |
VkImageLayout VulkanTexture::currentLayout() const { |
| 352 |
return skgpu::MutableTextureStates::GetVkImageLayout(this->mutableState()); |
| 353 |
} |
| 354 |
|
| 355 |
uint32_t VulkanTexture::currentQueueFamilyIndex() const { |
| 356 |
return skgpu::MutableTextureStates::GetVkQueueFamilyIndex(this->mutableState()); |
| 357 |
} |
| 358 |
|
| 359 |
VkPipelineStageFlags VulkanTexture::LayoutToPipelineSrcStageFlags(const VkImageLayout layout) { |
| 360 |
if (VK_IMAGE_LAYOUT_GENERAL == layout) { |
| 361 |
return VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; |
| 362 |
} else if (VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == layout || |
| 363 |
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL == layout) { |
| 364 |
return VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 365 |
} else if (VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL == layout) { |
| 366 |
return VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 367 |
} else if (VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL == layout || |
| 368 |
VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL == layout) { |
| 369 |
return VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT; |
| 370 |
} else if (VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == layout) { |
| 371 |
return VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; |
| 372 |
} else if (VK_IMAGE_LAYOUT_PREINITIALIZED == layout) { |
| 373 |
return VK_PIPELINE_STAGE_HOST_BIT; |
| 374 |
} else if (VK_IMAGE_LAYOUT_PRESENT_SRC_KHR == layout) { |
| 375 |
return VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 376 |
} |
| 377 |
|
| 378 |
SkASSERT(VK_IMAGE_LAYOUT_UNDEFINED == layout); |
| 379 |
return VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 380 |
} |
| 381 |
|
| 382 |
VkAccessFlags VulkanTexture::LayoutToSrcAccessMask(const VkImageLayout layout) { |
| 383 |
// Currently we assume we will never being doing any explict shader writes (this doesn't include |
| 384 |
// color attachment or depth/stencil writes). So we will ignore the |
| 385 |
// VK_MEMORY_OUTPUT_SHADER_WRITE_BIT. |
| 386 |
|
| 387 |
// We can only directly access the host memory if we are in preinitialized or general layout, |
| 388 |
// and the image is linear. However, device access to images written by the host happens after |
| 389 |
// vkQueueSubmit, which implicitly makes host writes _visible_ to the device, i.e. |
| 390 |
// VK_ACCESS_HOST_WRITE_BIT is unnecessary. Host data is made _available_ to the device via |
| 391 |
// vkFlushMappedMemoryRanges. |
| 392 |
VkAccessFlags flags = 0; |
| 393 |
if (VK_IMAGE_LAYOUT_GENERAL == layout) { |
| 394 |
flags = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | |
| 395 |
VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | |
| 396 |
VK_ACCESS_TRANSFER_WRITE_BIT; |
| 397 |
} else if (VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL == layout) { |
| 398 |
flags = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 399 |
} else if (VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL == layout) { |
| 400 |
flags = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; |
| 401 |
} else if (VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL == layout) { |
| 402 |
flags = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 403 |
} else if (VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == layout || |
| 404 |
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == layout || |
| 405 |
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR == layout || |
| 406 |
VK_IMAGE_LAYOUT_PREINITIALIZED == layout) { |
| 407 |
// There are no writes that need to be made available |
| 408 |
flags = 0; |
| 409 |
} |
| 410 |
return flags; |
| 411 |
} |
| 412 |
|
| 413 |
const VulkanImageView* VulkanTexture::getImageView(VulkanImageView::Usage usage) const { |
| 414 |
for (int i = 0; i < fImageViews.size(); ++i) { |
| 415 |
if (fImageViews[i]->usage() == usage) { |
| 416 |
return fImageViews[i].get(); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
auto sharedContext = static_cast<const VulkanSharedContext*>(this->sharedContext()); |
| 421 |
const auto& vkTexInfo = this->vulkanTextureInfo(); |
| 422 |
int miplevels = vkTexInfo.fMipmapped == Mipmapped::kYes |
| 423 |
? SkMipmap::ComputeLevelCount(this->dimensions()) + 1 |
| 424 |
: 1; |
| 425 |
auto imageView = VulkanImageView::Make(sharedContext, |
| 426 |
fImage, |
| 427 |
vkTexInfo.fFormat, |
| 428 |
usage, |
| 429 |
miplevels, |
| 430 |
fYcbcrConversion); |
| 431 |
return fImageViews.push_back(std::move(imageView)).get(); |
| 432 |
} |
| 433 |
|
| 434 |
bool VulkanTexture::supportsInputAttachmentUsage() const { |
| 435 |
return (this->vulkanTextureInfo().fImageUsageFlags & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT); |
| 436 |
} |
| 437 |
|
| 438 |
size_t VulkanTexture::onUpdateGpuMemorySize() { |
| 439 |
if (!uses_lazy_memory(fMemoryAlloc)) { |
| 440 |
// We don't expect non-transient textures to change their size over time. |
| 441 |
return this->gpuMemorySize(); |
| 442 |
} |
| 443 |
|
| 444 |
auto sharedContext = static_cast<const VulkanSharedContext*>(this->sharedContext()); |
| 445 |
VkDeviceSize committedMemory; |
| 446 |
VULKAN_CALL(sharedContext->interface(), |
| 447 |
GetDeviceMemoryCommitment(sharedContext->device(), |
| 448 |
fMemoryAlloc.fMemory, |
| 449 |
&committedMemory)); |
| 450 |
return committedMemory; |
| 451 |
} |
| 452 |
|
| 453 |
sk_sp<VulkanDescriptorSet> VulkanTexture::getCachedSingleTextureDescriptorSet( |
| 454 |
const Sampler* sampler) const { |
| 455 |
SkASSERT(sampler); |
| 456 |
for (auto& cachedSet : fCachedSingleTextureDescSets) { |
| 457 |
if (cachedSet.first->uniqueID() == sampler->uniqueID()) { |
| 458 |
return cachedSet.second; |
| 459 |
} |
| 460 |
} |
| 461 |
return nullptr; |
| 462 |
} |
| 463 |
|
| 464 |
void VulkanTexture::addCachedSingleTextureDescriptorSet(sk_sp<VulkanDescriptorSet> set, |
| 465 |
sk_sp<const Sampler> sampler) const { |
| 466 |
SkASSERT(set); |
| 467 |
SkASSERT(sampler); |
| 468 |
fCachedSingleTextureDescSets.push_back(std::make_pair(std::move(sampler), std::move(set))); |
| 469 |
} |
| 470 |
|
| 471 |
sk_sp<VulkanFramebuffer> VulkanTexture::getCachedFramebuffer( |
| 472 |
const RenderPassDesc& renderPassDesc, |
| 473 |
const VulkanTexture* msaaTexture, |
| 474 |
const VulkanTexture* depthStencilTexture) const { |
| 475 |
for (auto& cachedFB : fCachedFramebuffers) { |
| 476 |
if (cachedFB->compatible(renderPassDesc, msaaTexture, depthStencilTexture)) { |
| 477 |
return cachedFB; |
| 478 |
} |
| 479 |
} |
| 480 |
return nullptr; |
| 481 |
} |
| 482 |
|
| 483 |
void VulkanTexture::addCachedFramebuffer(sk_sp<VulkanFramebuffer> fb) { |
| 484 |
SkASSERT(fb); |
| 485 |
fCachedFramebuffers.push_back(std::move(fb)); |
| 486 |
} |
| 487 |
|
| 488 |
bool VulkanTexture::canUploadOnHost(const UploadSource& source) const { |
| 489 |
// Can't use host-image-copy if the usage flag is not set. |
| 490 |
if ((this->vulkanTextureInfo().fImageUsageFlags & VK_IMAGE_USAGE_HOST_TRANSFER_BIT) == 0) { |
| 491 |
return false; |
| 492 |
} |
| 493 |
|
| 494 |
// Can't use host-image-copy if the image is busy on the GPU. |
| 495 |
if (this->isTextureBusyOnGPU()) { |
| 496 |
return false; |
| 497 |
} |
| 498 |
|
| 499 |
if (source.isRGB888Format()) { |
| 500 |
// Need to transform RGBX8 to RGBA8 in a temp memory anyway, might as well use the buffer |
| 501 |
// upload path for faster temp memory -> image copy by the GPU. |
| 502 |
return false; |
| 503 |
} |
| 504 |
|
| 505 |
// For now, only use host-image-copy if the image has never been used. If needed in the future, |
| 506 |
// we could inspect the VkPhysicalDeviceHostImageCopyProperties::pCopySrcLayouts array to know |
| 507 |
// which layouts the image can be to be used with HIC. However, a better solution could be to |
| 508 |
// recreate the VkImage even if the existing one is busy on the GPU, since this function |
| 509 |
// entirely overwrites the texture anyway. |
| 510 |
if (this->currentLayout() != VK_IMAGE_LAYOUT_UNDEFINED) { |
| 511 |
return false; |
| 512 |
} |
| 513 |
|
| 514 |
return true; |
| 515 |
} |
| 516 |
|
| 517 |
bool VulkanTexture::uploadDataOnHost(const UploadSource& source, const SkIRect& dstRect) { |
| 518 |
auto sharedContext = static_cast<const VulkanSharedContext*>(this->sharedContext()); |
| 519 |
SkSpan<const MipLevel> levels = source.levels(); |
| 520 |
const unsigned int mipLevelCount = levels.size(); |
| 521 |
|
| 522 |
const TextureInfo& textureInfo = this->textureInfo(); |
| 523 |
const TextureFormat format = TextureInfoPriv::ViewFormat(textureInfo); |
| 524 |
const VkImageAspectFlags aspectFlags = GetVkImageAspectFlags(format); |
| 525 |
|
| 526 |
SkASSERT(this->currentLayout() == VK_IMAGE_LAYOUT_UNDEFINED); |
| 527 |
|
| 528 |
VkHostImageLayoutTransitionInfo transition = {}; |
| 529 |
transition.sType = VK_STRUCTURE_TYPE_HOST_IMAGE_LAYOUT_TRANSITION_INFO; |
| 530 |
transition.image = fImage; |
| 531 |
transition.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 532 |
transition.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 533 |
transition.subresourceRange.aspectMask = aspectFlags; |
| 534 |
transition.subresourceRange.levelCount = mipLevelCount; |
| 535 |
transition.subresourceRange.layerCount = 1; |
| 536 |
|
| 537 |
if (VULKAN_CALL(sharedContext->interface(), |
| 538 |
TransitionImageLayout(sharedContext->device(), 1, &transition)) != VK_SUCCESS) { |
| 539 |
return false; |
| 540 |
} |
| 541 |
this->updateImageLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); |
| 542 |
|
| 543 |
TArray<VkMemoryToImageCopy> copyRegions(mipLevelCount); |
| 544 |
|
| 545 |
// The assumption is either that we have no mipmaps, or that our rect is the entire texture |
| 546 |
SkASSERT(mipLevelCount == 1 || dstRect == SkIRect::MakeSize(this->dimensions())); |
| 547 |
|
| 548 |
// Copy data mip by mip. |
| 549 |
const int32_t offsetX = dstRect.x(); |
| 550 |
const int32_t offsetY = dstRect.y(); |
| 551 |
int32_t currentWidth = dstRect.width(); |
| 552 |
int32_t currentHeight = dstRect.height(); |
| 553 |
|
| 554 |
for (unsigned int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
| 555 |
// Upload data for compressed formats are fully packed. If this changes, the division by |
| 556 |
// bytes-per-pixel should be adjusted for compressed formats. |
| 557 |
SkASSERT(source.compression() == SkTextureCompressionType::kNone || |
| 558 |
levels[currentMipLevel].fRowBytes == 0); |
| 559 |
|
| 560 |
VkMemoryToImageCopy copyRegion = {}; |
| 561 |
copyRegion.sType = VK_STRUCTURE_TYPE_MEMORY_TO_IMAGE_COPY; |
| 562 |
copyRegion.pHostPointer = levels[currentMipLevel].fPixels; |
| 563 |
copyRegion.memoryRowLength = levels[currentMipLevel].fRowBytes / source.bytesPerPixel(); |
| 564 |
copyRegion.memoryImageHeight = 0; // Tightly packed |
| 565 |
copyRegion.imageSubresource.aspectMask = aspectFlags; |
| 566 |
copyRegion.imageSubresource.mipLevel = currentMipLevel; |
| 567 |
copyRegion.imageSubresource.layerCount = 1; |
| 568 |
copyRegion.imageOffset.x = offsetX; |
| 569 |
copyRegion.imageOffset.y = offsetY; |
| 570 |
copyRegion.imageExtent.width = currentWidth; |
| 571 |
copyRegion.imageExtent.height = currentHeight; |
| 572 |
copyRegion.imageExtent.depth = 1; |
| 573 |
|
| 574 |
copyRegions.push_back(copyRegion); |
| 575 |
|
| 576 |
// Calculate the extent for the next mip. The offset does not need modification, since it's |
| 577 |
// zero if mipLevelCount > 1, asserted before the loop. |
| 578 |
currentWidth = std::max(1, currentWidth / 2); |
| 579 |
currentHeight = std::max(1, currentHeight / 2); |
| 580 |
} |
| 581 |
|
| 582 |
VkCopyMemoryToImageInfo copyInfo = {}; |
| 583 |
copyInfo.sType = VK_STRUCTURE_TYPE_COPY_MEMORY_TO_IMAGE_INFO; |
| 584 |
copyInfo.dstImage = fImage; |
| 585 |
copyInfo.dstImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 586 |
copyInfo.regionCount = mipLevelCount; |
| 587 |
copyInfo.pRegions = copyRegions.data(); |
| 588 |
|
| 589 |
const VkResult result = VULKAN_CALL(sharedContext->interface(), |
| 590 |
CopyMemoryToImage(sharedContext->device(), ©Info)); |
| 591 |
return result == VK_SUCCESS; |
| 592 |
} |
| 593 |
|
| 594 |
} // namespace skgpu::graphite |