| 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/VulkanCommandBuffer.h" |
| 9 |
|
| 10 |
#include "include/gpu/MutableTextureState.h" |
| 11 |
#include "include/gpu/graphite/BackendSemaphore.h" |
| 12 |
#include "include/gpu/graphite/vk/VulkanGraphiteTypes.h" |
| 13 |
#include "include/gpu/vk/VulkanMutableTextureState.h" |
| 14 |
#include "include/private/base/SkTArray.h" |
| 15 |
#include "src/gpu/DataUtils.h" |
| 16 |
#include "src/gpu/graphite/ContextUtils.h" |
| 17 |
#include "src/gpu/graphite/DescriptorData.h" |
| 18 |
#include "src/gpu/graphite/Log.h" |
| 19 |
#include "src/gpu/graphite/PipelineData.h" |
| 20 |
#include "src/gpu/graphite/RenderPassDesc.h" |
| 21 |
#include "src/gpu/graphite/Surface_Graphite.h" |
| 22 |
#include "src/gpu/graphite/TextureProxy.h" |
| 23 |
#include "src/gpu/graphite/UniformManager.h" |
| 24 |
#include "src/gpu/graphite/vk/VulkanBuffer.h" |
| 25 |
#include "src/gpu/graphite/vk/VulkanCaps.h" |
| 26 |
#include "src/gpu/graphite/vk/VulkanDescriptorSet.h" |
| 27 |
#include "src/gpu/graphite/vk/VulkanFramebuffer.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/vk/VulkanUtilsPriv.h" |
| 34 |
|
| 35 |
using namespace skia_private; |
| 36 |
|
| 37 |
namespace skgpu::graphite { |
| 38 |
|
| 39 |
class VulkanDescriptorSet; |
| 40 |
|
| 41 |
std::unique_ptr<VulkanCommandBuffer> VulkanCommandBuffer::Make( |
| 42 |
const VulkanSharedContext* sharedContext, |
| 43 |
VulkanResourceProvider* resourceProvider, |
| 44 |
Protected isProtected) { |
| 45 |
// Create VkCommandPool |
| 46 |
VkCommandPoolCreateFlags cmdPoolCreateFlags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT; |
| 47 |
if (isProtected == Protected::kYes) { |
| 48 |
cmdPoolCreateFlags |= VK_COMMAND_POOL_CREATE_PROTECTED_BIT; |
| 49 |
} |
| 50 |
|
| 51 |
const VkCommandPoolCreateInfo cmdPoolInfo = { |
| 52 |
VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // sType |
| 53 |
nullptr, // pNext |
| 54 |
cmdPoolCreateFlags, // CmdPoolCreateFlags |
| 55 |
sharedContext->queueIndex(), // queueFamilyIndex |
| 56 |
}; |
| 57 |
VkResult result; |
| 58 |
VkCommandPool pool; |
| 59 |
VULKAN_CALL_RESULT(sharedContext, |
| 60 |
result, |
| 61 |
CreateCommandPool(sharedContext->device(), &cmdPoolInfo, nullptr, &pool)); |
| 62 |
if (result != VK_SUCCESS) { |
| 63 |
return nullptr; |
| 64 |
} |
| 65 |
|
| 66 |
const VkCommandBufferAllocateInfo cmdInfo = { |
| 67 |
VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType |
| 68 |
nullptr, // pNext |
| 69 |
pool, // commandPool |
| 70 |
VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level |
| 71 |
1 // bufferCount |
| 72 |
}; |
| 73 |
|
| 74 |
VkCommandBuffer primaryCmdBuffer; |
| 75 |
VULKAN_CALL_RESULT( |
| 76 |
sharedContext, |
| 77 |
result, |
| 78 |
AllocateCommandBuffers(sharedContext->device(), &cmdInfo, &primaryCmdBuffer)); |
| 79 |
if (result != VK_SUCCESS) { |
| 80 |
VULKAN_CALL(sharedContext->interface(), |
| 81 |
DestroyCommandPool(sharedContext->device(), pool, nullptr)); |
| 82 |
return nullptr; |
| 83 |
} |
| 84 |
|
| 85 |
return std::unique_ptr<VulkanCommandBuffer>(new VulkanCommandBuffer(pool, |
| 86 |
primaryCmdBuffer, |
| 87 |
sharedContext, |
| 88 |
resourceProvider, |
| 89 |
isProtected)); |
| 90 |
} |
| 91 |
|
| 92 |
VulkanCommandBuffer::VulkanCommandBuffer(VkCommandPool pool, |
| 93 |
VkCommandBuffer primaryCommandBuffer, |
| 94 |
const VulkanSharedContext* sharedContext, |
| 95 |
VulkanResourceProvider* resourceProvider, |
| 96 |
Protected isProtected) |
| 97 |
: CommandBuffer(isProtected) |
| 98 |
, fPool(pool) |
| 99 |
, fPrimaryCommandBuffer(primaryCommandBuffer) |
| 100 |
, fSharedContext(sharedContext) |
| 101 |
, fResourceProvider(resourceProvider) { |
| 102 |
// When making a new command buffer, we automatically begin the command buffer |
| 103 |
this->begin(); |
| 104 |
} |
| 105 |
|
| 106 |
VulkanCommandBuffer::~VulkanCommandBuffer() { |
| 107 |
if (fActive) { |
| 108 |
// Need to end command buffer before deleting it |
| 109 |
VULKAN_CALL(fSharedContext->interface(), EndCommandBuffer(fPrimaryCommandBuffer)); |
| 110 |
fActive = false; |
| 111 |
} |
| 112 |
|
| 113 |
if (VK_NULL_HANDLE != fSubmitFence) { |
| 114 |
VULKAN_CALL(fSharedContext->interface(), |
| 115 |
DestroyFence(fSharedContext->device(), fSubmitFence, nullptr)); |
| 116 |
} |
| 117 |
// This should delete any command buffers as well. |
| 118 |
VULKAN_CALL(fSharedContext->interface(), |
| 119 |
DestroyCommandPool(fSharedContext->device(), fPool, nullptr)); |
| 120 |
} |
| 121 |
|
| 122 |
void VulkanCommandBuffer::onResetCommandBuffer() { |
| 123 |
SkASSERT(!fActive); |
| 124 |
VULKAN_CALL_ERRCHECK(fSharedContext, ResetCommandPool(fSharedContext->device(), fPool, 0)); |
| 125 |
fActiveGraphicsPipeline = nullptr; |
| 126 |
fBindUniformBuffers = true; |
| 127 |
fBoundIndirectBuffer = VK_NULL_HANDLE; |
| 128 |
fBoundIndirectBufferOffset = 0; |
| 129 |
fTargetTexture = nullptr; |
| 130 |
fTextureSamplerDescSetToBind = VK_NULL_HANDLE; |
| 131 |
fNumTextureSamplers = 0; |
| 132 |
fUniformBuffersToBind.fill({}); |
| 133 |
for (int i = 0; i < 4; ++i) { |
| 134 |
fCachedBlendConstant[i] = -1.0; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
bool VulkanCommandBuffer::setNewCommandBufferResources() { |
| 139 |
this->begin(); |
| 140 |
return true; |
| 141 |
} |
| 142 |
|
| 143 |
void VulkanCommandBuffer::begin() { |
| 144 |
SkASSERT(!fActive); |
| 145 |
VkCommandBufferBeginInfo cmdBufferBeginInfo = {}; |
| 146 |
cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 147 |
cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 148 |
|
| 149 |
VULKAN_CALL_ERRCHECK(fSharedContext, |
| 150 |
BeginCommandBuffer(fPrimaryCommandBuffer, &cmdBufferBeginInfo)); |
| 151 |
fActive = true; |
| 152 |
|
| 153 |
// Set all the dynamic state that Graphite never changes once at the beginning of the command |
| 154 |
// buffer. The following state are constants in Graphite: |
| 155 |
// |
| 156 |
// * lineWidth |
| 157 |
// * depthBiasEnable, depthBiasConstantFactor, depthBiasClamp, depthBiasSlopeFactor |
| 158 |
// * min/maxDepthBounds, depthBoundsTestEnable |
| 159 |
// * primitiveRestartEnable |
| 160 |
// * cullMode |
| 161 |
// * frontFace |
| 162 |
// * rasterizerDiscardEnable |
| 163 |
|
| 164 |
if (fSharedContext->caps()->useBasicDynamicState()) { |
| 165 |
VULKAN_CALL(fSharedContext->interface(), |
| 166 |
CmdSetLineWidth(fPrimaryCommandBuffer, |
| 167 |
/*lineWidth=*/1.0)); |
| 168 |
VULKAN_CALL(fSharedContext->interface(), |
| 169 |
CmdSetDepthBias(fPrimaryCommandBuffer, |
| 170 |
/*depthBiasConstantFactor=*/0.0f, |
| 171 |
/*depthBiasClamp=*/0.0f, |
| 172 |
/*depthBiasSlopeFactor=*/0.0f)); |
| 173 |
VULKAN_CALL(fSharedContext->interface(), |
| 174 |
CmdSetDepthBounds(fPrimaryCommandBuffer, |
| 175 |
/*minDepthBounds=*/0.0f, |
| 176 |
/*maxDepthBounds=*/1.0f)); |
| 177 |
VULKAN_CALL(fSharedContext->interface(), |
| 178 |
CmdSetDepthBoundsTestEnable(fPrimaryCommandBuffer, |
| 179 |
/*depthBoundsTestEnable=*/VK_FALSE)); |
| 180 |
VULKAN_CALL(fSharedContext->interface(), |
| 181 |
CmdSetDepthBiasEnable(fPrimaryCommandBuffer, |
| 182 |
/*depthBiasEnable=*/VK_FALSE)); |
| 183 |
VULKAN_CALL(fSharedContext->interface(), |
| 184 |
CmdSetPrimitiveRestartEnable(fPrimaryCommandBuffer, |
| 185 |
/*primitiveRestartEnable=*/VK_FALSE)); |
| 186 |
VULKAN_CALL(fSharedContext->interface(), |
| 187 |
CmdSetCullMode(fPrimaryCommandBuffer, |
| 188 |
/*cullMode=*/VK_CULL_MODE_NONE)); |
| 189 |
VULKAN_CALL(fSharedContext->interface(), |
| 190 |
CmdSetFrontFace(fPrimaryCommandBuffer, |
| 191 |
/*frontFace=*/VK_FRONT_FACE_COUNTER_CLOCKWISE)); |
| 192 |
VULKAN_CALL(fSharedContext->interface(), |
| 193 |
CmdSetRasterizerDiscardEnable(fPrimaryCommandBuffer, |
| 194 |
/*rasterizerDiscardEnable=*/VK_FALSE)); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
void VulkanCommandBuffer::end() { |
| 199 |
SkASSERT(fActive); |
| 200 |
SkASSERT(!fActiveRenderPass); |
| 201 |
|
| 202 |
this->submitPipelineBarriers(); |
| 203 |
|
| 204 |
VULKAN_CALL_ERRCHECK(fSharedContext, EndCommandBuffer(fPrimaryCommandBuffer)); |
| 205 |
|
| 206 |
fActive = false; |
| 207 |
} |
| 208 |
|
| 209 |
void VulkanCommandBuffer::addWaitSemaphores(size_t numWaitSemaphores, |
| 210 |
const BackendSemaphore* waitSemaphores) { |
| 211 |
if (!waitSemaphores) { |
| 212 |
SkASSERT(numWaitSemaphores == 0); |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
for (size_t i = 0; i < numWaitSemaphores; ++i) { |
| 217 |
auto& semaphore = waitSemaphores[i]; |
| 218 |
if (semaphore.isValid() && semaphore.backend() == BackendApi::kVulkan) { |
| 219 |
fWaitSemaphores.push_back(BackendSemaphores::GetVkSemaphore(semaphore)); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
void VulkanCommandBuffer::addSignalSemaphores(size_t numSignalSemaphores, |
| 225 |
const BackendSemaphore* signalSemaphores) { |
| 226 |
if (!signalSemaphores) { |
| 227 |
SkASSERT(numSignalSemaphores == 0); |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
for (size_t i = 0; i < numSignalSemaphores; ++i) { |
| 232 |
auto& semaphore = signalSemaphores[i]; |
| 233 |
if (semaphore.isValid() && semaphore.backend() == BackendApi::kVulkan) { |
| 234 |
fSignalSemaphores.push_back(BackendSemaphores::GetVkSemaphore(semaphore)); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
void VulkanCommandBuffer::prepareSurfaceForStateUpdate(SkSurface* targetSurface, |
| 240 |
const MutableTextureState* newState) { |
| 241 |
TextureProxy* textureProxy = static_cast<Surface*>(targetSurface)->backingTextureProxy(); |
| 242 |
VulkanTexture* texture = static_cast<VulkanTexture*>(textureProxy->texture()); |
| 243 |
|
| 244 |
// Even though internally we use this helper for getting src access flags and stages they |
| 245 |
// can also be used for general dst flags since we don't know exactly what the client |
| 246 |
// plans on using the image for. |
| 247 |
VkImageLayout newLayout = skgpu::MutableTextureStates::GetVkImageLayout(newState); |
| 248 |
if (newLayout == VK_IMAGE_LAYOUT_UNDEFINED) { |
| 249 |
newLayout = texture->currentLayout(); |
| 250 |
} |
| 251 |
VkPipelineStageFlags dstStage = VulkanTexture::LayoutToPipelineSrcStageFlags(newLayout); |
| 252 |
VkAccessFlags dstAccess = VulkanTexture::LayoutToSrcAccessMask(newLayout); |
| 253 |
|
| 254 |
uint32_t currentQueueFamilyIndex = texture->currentQueueFamilyIndex(); |
| 255 |
uint32_t newQueueFamilyIndex = skgpu::MutableTextureStates::GetVkQueueFamilyIndex(newState); |
| 256 |
auto isSpecialQueue = [](uint32_t queueFamilyIndex) { |
| 257 |
return queueFamilyIndex == VK_QUEUE_FAMILY_EXTERNAL || |
| 258 |
queueFamilyIndex == VK_QUEUE_FAMILY_FOREIGN_EXT; |
| 259 |
}; |
| 260 |
if (isSpecialQueue(currentQueueFamilyIndex) && isSpecialQueue(newQueueFamilyIndex)) { |
| 261 |
// It is illegal to have both the new and old queue be special queue families (i.e. external |
| 262 |
// or foreign). |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
this->trackCommandBufferResource(sk_ref_sp(texture)); |
| 267 |
|
| 268 |
texture->setImageLayoutAndQueueIndex(this, |
| 269 |
newLayout, |
| 270 |
dstAccess, |
| 271 |
dstStage, |
| 272 |
newQueueFamilyIndex); |
| 273 |
} |
| 274 |
|
| 275 |
// Requests a sampler. Dynamic samplers live in the global cache, requiring no tracking, but |
| 276 |
// immutable samplers are created on the current graphics pipeline, and may outlive it, requiring |
| 277 |
// further tracking. |
| 278 |
const Sampler* VulkanCommandBuffer::getSampler( |
| 279 |
const DrawPassCommands::BindTexturesAndSamplers* command, int32_t index) { |
| 280 |
auto desc = command->fSamplers[index]; |
| 281 |
if (desc.isImmutable()) { |
| 282 |
const VulkanSampler* immutableSampler = fActiveGraphicsPipeline->immutableSampler(index); |
| 283 |
if (immutableSampler) { |
| 284 |
this->trackCommandBufferResource(sk_ref_sp<Sampler>(immutableSampler)); |
| 285 |
} |
| 286 |
return immutableSampler; |
| 287 |
} else { |
| 288 |
return fSharedContext->globalCache()->getDynamicSampler(desc); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
static VkResult submit_to_queue(const VulkanSharedContext* sharedContext, |
| 293 |
VkQueue queue, |
| 294 |
VkFence fence, |
| 295 |
uint32_t waitCount, |
| 296 |
const VkSemaphore* waitSemaphores, |
| 297 |
const VkPipelineStageFlags* waitStages, |
| 298 |
uint32_t commandBufferCount, |
| 299 |
const VkCommandBuffer* commandBuffers, |
| 300 |
uint32_t signalCount, |
| 301 |
const VkSemaphore* signalSemaphores, |
| 302 |
Protected protectedContext, |
| 303 |
MarkFrameBoundary markFrameBoundary, |
| 304 |
uint64_t frameID) { |
| 305 |
VkSubmitInfo submitInfo = {}; |
| 306 |
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 307 |
|
| 308 |
VkProtectedSubmitInfo protectedSubmitInfo = {}; |
| 309 |
if (protectedContext == Protected::kYes) { |
| 310 |
protectedSubmitInfo.sType = VK_STRUCTURE_TYPE_PROTECTED_SUBMIT_INFO; |
| 311 |
protectedSubmitInfo.protectedSubmit = VK_TRUE; |
| 312 |
|
| 313 |
AddToPNextChain(&submitInfo, &protectedSubmitInfo); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
VkFrameBoundaryEXT frameBoundary; |
| 318 |
if (markFrameBoundary == MarkFrameBoundary::kYes && |
| 319 |
sharedContext->vulkanCaps().supportsFrameBoundary()) { |
| 320 |
memset(&frameBoundary, 0, sizeof(VkFrameBoundaryEXT)); |
| 321 |
frameBoundary.sType = VK_STRUCTURE_TYPE_FRAME_BOUNDARY_EXT; |
| 322 |
frameBoundary.flags = VK_FRAME_BOUNDARY_FRAME_END_BIT_EXT; |
| 323 |
frameBoundary.frameID = frameID; |
| 324 |
|
| 325 |
AddToPNextChain(&submitInfo, &frameBoundary); |
| 326 |
} |
| 327 |
|
| 328 |
submitInfo.waitSemaphoreCount = waitCount; |
| 329 |
submitInfo.pWaitSemaphores = waitSemaphores; |
| 330 |
submitInfo.pWaitDstStageMask = waitStages; |
| 331 |
submitInfo.commandBufferCount = commandBufferCount; |
| 332 |
submitInfo.pCommandBuffers = commandBuffers; |
| 333 |
submitInfo.signalSemaphoreCount = signalCount; |
| 334 |
submitInfo.pSignalSemaphores = signalSemaphores; |
| 335 |
|
| 336 |
VkResult result; |
| 337 |
VULKAN_CALL_RESULT(sharedContext, result, QueueSubmit(queue, 1, &submitInfo, fence)); |
| 338 |
return result; |
| 339 |
} |
| 340 |
|
| 341 |
bool VulkanCommandBuffer::submit(VkQueue queue, const SubmitInfo& submitInfo) { |
| 342 |
this->end(); |
| 343 |
|
| 344 |
auto device = fSharedContext->device(); |
| 345 |
VkResult err; |
| 346 |
|
| 347 |
if (fSubmitFence == VK_NULL_HANDLE) { |
| 348 |
VkFenceCreateInfo fenceInfo = {}; |
| 349 |
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 350 |
VULKAN_CALL_RESULT( |
| 351 |
fSharedContext, err, CreateFence(device, &fenceInfo, nullptr, &fSubmitFence)); |
| 352 |
if (err) { |
| 353 |
fSubmitFence = VK_NULL_HANDLE; |
| 354 |
return false; |
| 355 |
} |
| 356 |
} else { |
| 357 |
// This cannot return DEVICE_LOST so we assert we succeeded. |
| 358 |
VULKAN_CALL_RESULT(fSharedContext, err, ResetFences(device, 1, &fSubmitFence)); |
| 359 |
SkASSERT(err == VK_SUCCESS); |
| 360 |
} |
| 361 |
|
| 362 |
SkASSERT(fSubmitFence != VK_NULL_HANDLE); |
| 363 |
int waitCount = fWaitSemaphores.size(); |
| 364 |
TArray<VkPipelineStageFlags> vkWaitStages(waitCount); |
| 365 |
for (int i = 0; i < waitCount; ++i) { |
| 366 |
vkWaitStages.push_back(VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | |
| 367 |
VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 368 |
} |
| 369 |
|
| 370 |
VkResult submitResult = submit_to_queue(fSharedContext, |
| 371 |
queue, |
| 372 |
fSubmitFence, |
| 373 |
waitCount, |
| 374 |
fWaitSemaphores.data(), |
| 375 |
vkWaitStages.data(), |
| 376 |
/*commandBufferCount*/ 1, |
| 377 |
&fPrimaryCommandBuffer, |
| 378 |
fSignalSemaphores.size(), |
| 379 |
fSignalSemaphores.data(), |
| 380 |
this->isProtected(), |
| 381 |
submitInfo.fMarkBoundary, |
| 382 |
submitInfo.fFrameID); |
| 383 |
fWaitSemaphores.clear(); |
| 384 |
fSignalSemaphores.clear(); |
| 385 |
if (submitResult != VK_SUCCESS) { |
| 386 |
// If we failed to submit because of a device lost, we still need to wait for the fence to |
| 387 |
// signal before deleting. However, there is an ARM bug (b/359822580) where the driver early |
| 388 |
// outs on the fence wait if in a device lost state and thus we can't wait on it. Instead, |
| 389 |
// we just wait on the queue to finish. We're already in a state that's going to cause us to |
| 390 |
// restart the whole device, so waiting on the queue shouldn't have any performance impact. |
| 391 |
if (submitResult == VK_ERROR_DEVICE_LOST) { |
| 392 |
VULKAN_CALL(fSharedContext->interface(), QueueWaitIdle(queue)); |
| 393 |
} else { |
| 394 |
SkASSERT(submitResult == VK_ERROR_OUT_OF_HOST_MEMORY || |
| 395 |
submitResult == VK_ERROR_OUT_OF_DEVICE_MEMORY); |
| 396 |
} |
| 397 |
|
| 398 |
VULKAN_CALL(fSharedContext->interface(), DestroyFence(device, fSubmitFence, nullptr)); |
| 399 |
fSubmitFence = VK_NULL_HANDLE; |
| 400 |
return false; |
| 401 |
} |
| 402 |
return true; |
| 403 |
} |
| 404 |
|
| 405 |
bool VulkanCommandBuffer::isFinished() { |
| 406 |
SkASSERT(!fActive); |
| 407 |
if (VK_NULL_HANDLE == fSubmitFence) { |
| 408 |
return true; |
| 409 |
} |
| 410 |
|
| 411 |
VkResult err; |
| 412 |
VULKAN_CALL_RESULT_NOCHECK(fSharedContext->interface(), err, |
| 413 |
GetFenceStatus(fSharedContext->device(), fSubmitFence)); |
| 414 |
switch (err) { |
| 415 |
case VK_SUCCESS: |
| 416 |
case VK_ERROR_DEVICE_LOST: |
| 417 |
return true; |
| 418 |
|
| 419 |
case VK_NOT_READY: |
| 420 |
return false; |
| 421 |
|
| 422 |
default: |
| 423 |
SKGPU_LOG_F("Error calling vkGetFenceStatus. Error: %d", err); |
| 424 |
SK_ABORT("Got an invalid fence status"); |
| 425 |
return false; |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
void VulkanCommandBuffer::waitUntilFinished() { |
| 430 |
if (fSubmitFence == VK_NULL_HANDLE) { |
| 431 |
return; |
| 432 |
} |
| 433 |
VULKAN_CALL_ERRCHECK(fSharedContext, |
| 434 |
WaitForFences(fSharedContext->device(), |
| 435 |
1, |
| 436 |
&fSubmitFence, |
| 437 |
/*waitAll=*/true, |
| 438 |
/*timeout=*/UINT64_MAX)); |
| 439 |
} |
| 440 |
|
| 441 |
void VulkanCommandBuffer::pushConstants(const PushConstantInfo& pushConstantInfo, |
| 442 |
VkPipelineLayout compatibleLayout) { |
| 443 |
// size must be within limits. Vulkan spec dictates each device supports at least 128 bytes |
| 444 |
SkASSERT(pushConstantInfo.fSize < 128); |
| 445 |
// offset and size must be a multiple of 4 |
| 446 |
SkASSERT(!SkToBool(pushConstantInfo.fOffset & 0x3)); |
| 447 |
SkASSERT(!SkToBool(pushConstantInfo.fSize & 0x3)); |
| 448 |
|
| 449 |
VULKAN_CALL(fSharedContext->interface(), |
| 450 |
CmdPushConstants(fPrimaryCommandBuffer, |
| 451 |
compatibleLayout, |
| 452 |
pushConstantInfo.fShaderStageFlagBits, |
| 453 |
pushConstantInfo.fOffset, |
| 454 |
pushConstantInfo.fSize, |
| 455 |
pushConstantInfo.fValues)); |
| 456 |
} |
| 457 |
|
| 458 |
bool VulkanCommandBuffer::onAddRenderPass(const RenderPassDesc& rpDesc, |
| 459 |
SkIRect renderPassBounds, |
| 460 |
const Texture* colorTexture, |
| 461 |
const Texture* resolveTexture, |
| 462 |
const Texture* depthStencilTexture, |
| 463 |
SkIPoint resolveOffset, |
| 464 |
SkIRect viewport, |
| 465 |
const DrawPassList& drawPasses) { |
| 466 |
SkASSERT(resolveOffset.isZero()); |
| 467 |
for (const auto& drawPass : drawPasses) { |
| 468 |
// Track resources now since the barriers will reference them. |
| 469 |
if (!drawPass->addResourceRefs(fResourceProvider, this)) SK_UNLIKELY { |
| 470 |
return false; |
| 471 |
} |
| 472 |
|
| 473 |
// Our current implementation of setting texture image layouts does not allow layout changes |
| 474 |
// once we have already begun a render pass, so prior to any other commands, set the layout |
| 475 |
// of all sampled textures from the drawpass so they can be sampled from the shader. |
| 476 |
for (const sk_sp<TextureProxy>& textureProxy : drawPass->sampledTextures()) { |
| 477 |
VulkanTexture* vulkanTexture = const_cast<VulkanTexture*>( |
| 478 |
static_cast<const VulkanTexture*>( |
| 479 |
textureProxy->texture())); |
| 480 |
vulkanTexture->setImageLayout(this, |
| 481 |
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, |
| 482 |
VK_ACCESS_SHADER_READ_BIT, |
| 483 |
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); |
| 484 |
} |
| 485 |
} |
| 486 |
if (fDstCopy.first) { |
| 487 |
VulkanTexture* vulkanTexture = |
| 488 |
const_cast<VulkanTexture*>(static_cast<const VulkanTexture*>(fDstCopy.first)); |
| 489 |
vulkanTexture->setImageLayout(this, |
| 490 |
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, |
| 491 |
VK_ACCESS_SHADER_READ_BIT, |
| 492 |
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); |
| 493 |
} |
| 494 |
this->setViewport(viewport); |
| 495 |
|
| 496 |
if (!this->beginRenderPass( |
| 497 |
rpDesc, renderPassBounds, colorTexture, resolveTexture, depthStencilTexture)) { |
| 498 |
return false; |
| 499 |
} |
| 500 |
|
| 501 |
// After loading msaa from resolve (if needed), perform any updates that only need to occur |
| 502 |
// once per renderpass. |
| 503 |
this->performOncePerRPUpdates( |
| 504 |
viewport, rpDesc.fDstReadStrategy == DstReadStrategy::kReadFromInput); |
| 505 |
|
| 506 |
for (const auto& drawPass : drawPasses) { |
| 507 |
this->addDrawPass(drawPass.get()); |
| 508 |
} |
| 509 |
|
| 510 |
this->endRenderPass(); |
| 511 |
return true; |
| 512 |
} |
| 513 |
|
| 514 |
void VulkanCommandBuffer::performOncePerRPUpdates(SkIRect viewport, bool bindDstAsInputAttachment) { |
| 515 |
// Updating push constant values and - if any draw within the RP reads from the dst as an |
| 516 |
// input attachment - binding the dst texture as an input attachment only need to occur once per |
| 517 |
// RP. This is because intrinsic constant vlues (dst copy bounds and rtAdjust) and the render |
| 518 |
// target do not change throughout the course of a RenderPass. Even if no pipeline is bound yet, |
| 519 |
// we can use a compatible mock pipeline layout to perform these operations. |
| 520 |
|
| 521 |
// TODO(b/374997389): Somehow convey & enforce Layout::kStd430 for push constants. |
| 522 |
UniformManager intrinsicValues{Layout::kStd140}; |
| 523 |
CollectIntrinsicUniforms(fSharedContext->caps(), viewport, fDstReadBounds, &intrinsicValues); |
| 524 |
SkSpan<const char> bytes = intrinsicValues.finish(); |
| 525 |
SkASSERT(bytes.size_bytes() == VulkanResourceProvider::kIntrinsicConstantSize); |
| 526 |
|
| 527 |
PushConstantInfo pushConstantInfo; |
| 528 |
pushConstantInfo.fOffset = 0; |
| 529 |
pushConstantInfo.fSize = VulkanResourceProvider::kIntrinsicConstantSize; |
| 530 |
pushConstantInfo.fShaderStageFlagBits = VulkanResourceProvider::kIntrinsicConstantStageFlags; |
| 531 |
pushConstantInfo.fValues = bytes.data(); |
| 532 |
this->pushConstants(pushConstantInfo, fResourceProvider->mockPipelineLayout()); |
| 533 |
|
| 534 |
if (bindDstAsInputAttachment) { |
| 535 |
this->updateAndBindInputAttachment(*fTargetTexture, |
| 536 |
VulkanGraphicsPipeline::kDstAsInputDescSetIndex, |
| 537 |
fResourceProvider->mockPipelineLayout()); |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
bool VulkanCommandBuffer::updateAndBindInputAttachment(const VulkanTexture& texture, |
| 542 |
const int setIdx, |
| 543 |
VkPipelineLayout piplineLayout) { |
| 544 |
// Fetch a descriptor set that contains one input attachment (we do not support using more than |
| 545 |
// one per set at this time). |
| 546 |
STArray<1, DescriptorData> inputDesc = {VulkanGraphicsPipeline::kInputAttachmentDescriptor}; |
| 547 |
sk_sp<VulkanDescriptorSet> set = fResourceProvider->findOrCreateDescriptorSet( |
| 548 |
{&inputDesc.front(), (size_t)inputDesc.size()}); |
| 549 |
if (!set) { |
| 550 |
return false; |
| 551 |
} |
| 552 |
|
| 553 |
// Update and write to the descriptor given the provided texture, binding it afterwards. |
| 554 |
VkDescriptorImageInfo textureInfo = {}; |
| 555 |
textureInfo.sampler = VK_NULL_HANDLE; |
| 556 |
textureInfo.imageView = |
| 557 |
texture.getImageView(VulkanImageView::Usage::kAttachment)->imageView(); |
| 558 |
// Even though the image is in the VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, the subpass |
| 559 |
// is configured to implicitly use VK_IMAGE_LAYOUT_GENERAL in VkAttachmentReference::layout as |
| 560 |
// part of VkSubpassDescription. |
| 561 |
textureInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL; |
| 562 |
|
| 563 |
VkWriteDescriptorSet writeInfo = {}; |
| 564 |
writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 565 |
writeInfo.dstSet = *set->descriptorSet(); |
| 566 |
writeInfo.dstBinding = 0; |
| 567 |
writeInfo.dstArrayElement = 0; |
| 568 |
writeInfo.descriptorCount = 1; |
| 569 |
writeInfo.descriptorType = DsTypeEnumToVkDs(DescriptorType::kInputAttachment); |
| 570 |
writeInfo.pImageInfo = &textureInfo; |
| 571 |
|
| 572 |
VULKAN_CALL(fSharedContext->interface(), |
| 573 |
UpdateDescriptorSets(fSharedContext->device(), |
| 574 |
/*descriptorWriteCount=*/1, |
| 575 |
&writeInfo, |
| 576 |
/*descriptorCopyCount=*/0, |
| 577 |
/*pDescriptorCopies=*/nullptr)); |
| 578 |
|
| 579 |
VULKAN_CALL(fSharedContext->interface(), |
| 580 |
CmdBindDescriptorSets(fPrimaryCommandBuffer, |
| 581 |
VK_PIPELINE_BIND_POINT_GRAPHICS, |
| 582 |
piplineLayout, |
| 583 |
setIdx, |
| 584 |
/*setCount=*/1, |
| 585 |
set->descriptorSet(), |
| 586 |
/*dynamicOffsetCount=*/0, |
| 587 |
/*dynamicOffsets=*/nullptr)); |
| 588 |
|
| 589 |
this->trackCommandBufferResource(std::move(set)); |
| 590 |
return true; |
| 591 |
} |
| 592 |
|
| 593 |
bool VulkanCommandBuffer::loadMSAAFromResolve(const RenderPassDesc& rpDesc, |
| 594 |
VulkanTexture& resolveTexture, |
| 595 |
SkISize dstDimensions, |
| 596 |
const SkIRect nativeDrawBounds) { |
| 597 |
sk_sp<VulkanGraphicsPipeline> loadPipeline = |
| 598 |
fResourceProvider->findOrCreateLoadMSAAPipeline(rpDesc); |
| 599 |
if (!loadPipeline) { |
| 600 |
SKGPU_LOG_E("Unable to create pipeline to load resolve texture into MSAA attachment"); |
| 601 |
return false; |
| 602 |
} |
| 603 |
|
| 604 |
// Update and bind uniform descriptor set |
| 605 |
int w = nativeDrawBounds.width(); |
| 606 |
int h = nativeDrawBounds.height(); |
| 607 |
|
| 608 |
// dst rect edges in NDC (-1 to 1) |
| 609 |
int dw = dstDimensions.width(); |
| 610 |
int dh = dstDimensions.height(); |
| 611 |
float dx0 = 2.f * nativeDrawBounds.fLeft / dw - 1.f; |
| 612 |
float dx1 = 2.f * (nativeDrawBounds.fLeft + w) / dw - 1.f; |
| 613 |
float dy0 = 2.f * nativeDrawBounds.fTop / dh - 1.f; |
| 614 |
float dy1 = 2.f * (nativeDrawBounds.fTop + h) / dh - 1.f; |
| 615 |
float uniData[] = {dx1 - dx0, dy1 - dy0, dx0, dy0}; // posXform |
| 616 |
SkASSERT(sizeof(uniData) == VulkanResourceProvider::kLoadMSAAPushConstantSize); |
| 617 |
|
| 618 |
this->bindGraphicsPipeline(loadPipeline.get()); |
| 619 |
|
| 620 |
PushConstantInfo loadMsaaPushConstantInfo; |
| 621 |
loadMsaaPushConstantInfo.fOffset = 0; |
| 622 |
loadMsaaPushConstantInfo.fSize = VulkanResourceProvider::kLoadMSAAPushConstantSize; |
| 623 |
loadMsaaPushConstantInfo.fShaderStageFlagBits = |
| 624 |
VulkanResourceProvider::kLoadMSAAPushConstantStageFlags; |
| 625 |
loadMsaaPushConstantInfo.fValues = uniData; |
| 626 |
this->pushConstants(loadMsaaPushConstantInfo, loadPipeline->layout()); |
| 627 |
|
| 628 |
// Make sure we do not attempt to bind uniform or texture/sampler descriptors because we do |
| 629 |
// not use them for loading MSAA from resolve. |
| 630 |
fBindUniformBuffers = false; |
| 631 |
fBindTextureSamplers = false; |
| 632 |
|
| 633 |
this->setScissor(SkIRect::MakeXYWH(0, 0, dstDimensions.width(), dstDimensions.height())); |
| 634 |
|
| 635 |
if (!this->updateAndBindInputAttachment( |
| 636 |
resolveTexture, |
| 637 |
VulkanGraphicsPipeline::kLoadMsaaFromResolveInputDescSetIndex, |
| 638 |
fActiveGraphicsPipeline->layout())) { |
| 639 |
SKGPU_LOG_E("Unable to update and bind an input attachment descriptor for loading MSAA " |
| 640 |
"from resolve"); |
| 641 |
return false; |
| 642 |
} |
| 643 |
|
| 644 |
this->draw(PrimitiveType::kTriangleStrip, /*baseVertex=*/0, /*vertexCount=*/4); |
| 645 |
|
| 646 |
// After loading the resolve attachment, proceed to the next subpass. |
| 647 |
this->nextSubpass(); |
| 648 |
// While transitioning to the next subpass, the layout of the resolve texture gets changed |
| 649 |
// internally to accommodate its usage within the following subpass. Thus, we need to update |
| 650 |
// our tracking of the layout to match the new/final layout. We do not need to use a general |
| 651 |
// layout because we do not expect to later treat the resolve texture as a dst to read from. |
| 652 |
resolveTexture.updateImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
| 653 |
|
| 654 |
// After using a distinct descriptor set layout for loading MSAA from resolve, we will need to |
| 655 |
// (re-)bind any descriptor sets. |
| 656 |
fBindUniformBuffers = true; |
| 657 |
fBindTextureSamplers = true; |
| 658 |
return true; |
| 659 |
} |
| 660 |
|
| 661 |
namespace { |
| 662 |
// Helpers for determining + updating texture layouts. |
| 663 |
void assign_color_texture_layout(VulkanCommandBuffer* cmdBuf, |
| 664 |
VulkanTexture* colorTexture, |
| 665 |
bool rpReadsDstAsInput) { |
| 666 |
VkAccessFlags access = |
| 667 |
VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 668 |
VkPipelineStageFlags stageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 669 |
VkImageLayout layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
| 670 |
|
| 671 |
// If any draws within a render pass read from the dst color texture as an input attachment, |
| 672 |
// we must add additional pipeline stage + access flags. |
| 673 |
if (rpReadsDstAsInput) { |
| 674 |
stageFlags |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; |
| 675 |
access |= VK_ACCESS_INPUT_ATTACHMENT_READ_BIT; |
| 676 |
} |
| 677 |
|
| 678 |
colorTexture->setImageLayout(cmdBuf, layout, access, stageFlags); |
| 679 |
} |
| 680 |
|
| 681 |
void assign_resolve_texture_layout(VulkanCommandBuffer* cmdBuf, |
| 682 |
VulkanTexture* resolveTexture, |
| 683 |
bool loadMSAAFromResolve) { |
| 684 |
VkPipelineStageFlags stageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 685 |
VkAccessFlags access = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT; |
| 686 |
// The resolve image uses the color attachment layout. If loadMSAAFromResolve is true, the |
| 687 |
// additional subpass will set the appropriate layout in VkAttachmentReference::layout, and |
| 688 |
// layout transitions are performed automatically between subpasses. |
| 689 |
VkImageLayout layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
| 690 |
|
| 691 |
// If loading MSAA from resolve, then the resolve texture is used in the first subpass |
| 692 |
// as an input attachment and is referenced within the fragment shader. Add to the access and |
| 693 |
// pipeline stage flags accordingly. |
| 694 |
if (loadMSAAFromResolve) { |
| 695 |
access |= VK_ACCESS_INPUT_ATTACHMENT_READ_BIT; |
| 696 |
stageFlags |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; |
| 697 |
} else { |
| 698 |
// Otherwise, add write access. |
| 699 |
access |= VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 700 |
} |
| 701 |
|
| 702 |
resolveTexture->setImageLayout(cmdBuf, layout, access, stageFlags); |
| 703 |
} |
| 704 |
|
| 705 |
void setup_texture_layouts(VulkanCommandBuffer* cmdBuf, |
| 706 |
VulkanTexture* colorTexture, |
| 707 |
VulkanTexture* resolveTexture, |
| 708 |
VulkanTexture* depthStencilTexture, |
| 709 |
bool loadMSAAFromResolve, |
| 710 |
bool rpReadsDstAsInput) { |
| 711 |
if (colorTexture) { |
| 712 |
assign_color_texture_layout(cmdBuf, colorTexture, rpReadsDstAsInput); |
| 713 |
if (resolveTexture) { |
| 714 |
// rpReadsDstAsInput does not matter here given that we do not anticipate reading |
| 715 |
// from the resolve texture as a dst input attachment. |
| 716 |
assign_resolve_texture_layout(cmdBuf, resolveTexture, loadMSAAFromResolve); |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
if (depthStencilTexture) { |
| 721 |
depthStencilTexture->setImageLayout(cmdBuf, |
| 722 |
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 723 |
VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | |
| 724 |
VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, |
| 725 |
VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT); |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
static constexpr int kMaxNumAttachments = 3; |
| 730 |
void gather_clear_values(const RenderPassDesc& rpDesc, |
| 731 |
STArray<kMaxNumAttachments, VkClearValue>* clearValues) { |
| 732 |
// NOTE: This must stay in sync with the attachment order defined in VulkanRenderPass.cpp, in |
| 733 |
// populate_attachment_refs(). |
| 734 |
if (rpDesc.fColorAttachment.fFormat != TextureFormat::kUnsupported) { |
| 735 |
VkClearValue& colorAttachmentClear = clearValues->push_back(); |
| 736 |
colorAttachmentClear.color = {{rpDesc.fClearColor[0], |
| 737 |
rpDesc.fClearColor[1], |
| 738 |
rpDesc.fClearColor[2], |
| 739 |
rpDesc.fClearColor[3]}}; |
| 740 |
} |
| 741 |
// The resolve attachment (if defined) should never be cleared, but add a value to keep the |
| 742 |
// attachment indices in sync. |
| 743 |
if (rpDesc.fColorResolveAttachment.fFormat != TextureFormat::kUnsupported) { |
| 744 |
SkASSERT(rpDesc.fColorResolveAttachment.fLoadOp != LoadOp::kClear); |
| 745 |
clearValues->push_back({}); |
| 746 |
} |
| 747 |
|
| 748 |
// Vulkan takes the clear depth and clear stencil regardless of whether or not the DS attachment |
| 749 |
// only has a single aspect or both. |
| 750 |
if (rpDesc.fDepthStencilAttachment.fFormat != TextureFormat::kUnsupported) { |
| 751 |
VkClearValue& depthStencilAttachmentClear = clearValues->push_back(); |
| 752 |
depthStencilAttachmentClear.depthStencil = {rpDesc.fClearDepth, rpDesc.fClearStencil}; |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple |
| 757 |
// of the granularity. The width must also be a multiple of the granularity or equal to the width |
| 758 |
// of the entire attachment. Similar requirements apply to the y and height components. |
| 759 |
VkRect2D get_render_area(const SkIRect& srcBounds, |
| 760 |
const VkExtent2D& granularity, |
| 761 |
int maxWidth, |
| 762 |
int maxHeight) { |
| 763 |
SkIRect dstBounds; |
| 764 |
// Adjust Width |
| 765 |
if (granularity.width == 0 || granularity.width == 1) { |
| 766 |
dstBounds.fLeft = srcBounds.fLeft; |
| 767 |
dstBounds.fRight = srcBounds.fRight; |
| 768 |
} else { |
| 769 |
// Start with the right side of rect so we know if we end up going past the maxWidth. |
| 770 |
int rightAdj = srcBounds.fRight % granularity.width; |
| 771 |
if (rightAdj != 0) { |
| 772 |
rightAdj = granularity.width - rightAdj; |
| 773 |
} |
| 774 |
dstBounds.fRight = srcBounds.fRight + rightAdj; |
| 775 |
if (dstBounds.fRight > maxWidth) { |
| 776 |
dstBounds.fRight = maxWidth; |
| 777 |
dstBounds.fLeft = 0; |
| 778 |
} else { |
| 779 |
dstBounds.fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width; |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
if (granularity.height == 0 || granularity.height == 1) { |
| 784 |
dstBounds.fTop = srcBounds.fTop; |
| 785 |
dstBounds.fBottom = srcBounds.fBottom; |
| 786 |
} else { |
| 787 |
// Start with the bottom side of rect so we know if we end up going past the maxHeight. |
| 788 |
int bottomAdj = srcBounds.fBottom % granularity.height; |
| 789 |
if (bottomAdj != 0) { |
| 790 |
bottomAdj = granularity.height - bottomAdj; |
| 791 |
} |
| 792 |
dstBounds.fBottom = srcBounds.fBottom + bottomAdj; |
| 793 |
if (dstBounds.fBottom > maxHeight) { |
| 794 |
dstBounds.fBottom = maxHeight; |
| 795 |
dstBounds.fTop = 0; |
| 796 |
} else { |
| 797 |
dstBounds.fTop = srcBounds.fTop - srcBounds.fTop % granularity.height; |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
VkRect2D renderArea; |
| 802 |
renderArea.offset = { dstBounds.fLeft , dstBounds.fTop }; |
| 803 |
renderArea.extent = { (uint32_t)dstBounds.width(), (uint32_t)dstBounds.height() }; |
| 804 |
return renderArea; |
| 805 |
} |
| 806 |
|
| 807 |
void populate_write_info(VulkanDescriptorSet* set, |
| 808 |
TArray<VkDescriptorImageInfo>& descriptorImageInfos, |
| 809 |
TArray<VkWriteDescriptorSet>& writeDescriptorSets, |
| 810 |
const VulkanTexture* texture, |
| 811 |
const VulkanSampler* sampler, |
| 812 |
int32_t binding) { |
| 813 |
SkASSERT(set); |
| 814 |
VkDescriptorImageInfo& textureInfo = descriptorImageInfos.push_back(); |
| 815 |
textureInfo = {}; |
| 816 |
textureInfo.sampler = sampler ? sampler->vkSampler() : VK_NULL_HANDLE; |
| 817 |
textureInfo.imageView = |
| 818 |
texture->getImageView(VulkanImageView::Usage::kShaderInput)->imageView(); |
| 819 |
textureInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 820 |
|
| 821 |
VkWriteDescriptorSet& writeInfo = writeDescriptorSets.push_back(); |
| 822 |
writeInfo = {}; |
| 823 |
writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 824 |
writeInfo.dstSet = *set->descriptorSet(); |
| 825 |
writeInfo.dstBinding = binding; |
| 826 |
writeInfo.dstArrayElement = 0; |
| 827 |
writeInfo.descriptorCount = 1; |
| 828 |
writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 829 |
writeInfo.pImageInfo = &textureInfo; |
| 830 |
} |
| 831 |
} // anonymous namespace |
| 832 |
|
| 833 |
bool VulkanCommandBuffer::beginRenderPass(const RenderPassDesc& rpDesc, |
| 834 |
SkIRect renderPassBounds, |
| 835 |
const Texture* colorTexture, |
| 836 |
const Texture* resolveTexture, |
| 837 |
const Texture* depthStencilTexture) { |
| 838 |
// Validate attachment descs and textures |
| 839 |
SkDEBUGCODE(const auto& colorInfo = rpDesc.fColorAttachment;) |
| 840 |
SkDEBUGCODE(const auto& resolveInfo = rpDesc.fColorResolveAttachment;) |
| 841 |
SkDEBUGCODE(const auto& depthStencilInfo = rpDesc.fDepthStencilAttachment;) |
| 842 |
SkASSERT(colorTexture ? colorInfo.isCompatible(colorTexture->textureInfo()) |
| 843 |
: colorInfo.fFormat == TextureFormat::kUnsupported); |
| 844 |
SkASSERT(resolveTexture ? resolveInfo.isCompatible(resolveTexture->textureInfo()) |
| 845 |
: resolveInfo.fFormat == TextureFormat::kUnsupported); |
| 846 |
SkASSERT(depthStencilTexture ? depthStencilInfo.isCompatible(depthStencilTexture->textureInfo()) |
| 847 |
: depthStencilInfo.fFormat == TextureFormat::kUnsupported); |
| 848 |
|
| 849 |
fTargetTexture = |
| 850 |
const_cast<VulkanTexture*>(static_cast<const VulkanTexture*>(colorTexture)); |
| 851 |
VulkanTexture* vulkanResolveTexture = |
| 852 |
const_cast<VulkanTexture*>(static_cast<const VulkanTexture*>(resolveTexture)); |
| 853 |
VulkanTexture* vulkanDepthStencilTexture = |
| 854 |
const_cast<VulkanTexture*>(static_cast<const VulkanTexture*>(depthStencilTexture)); |
| 855 |
|
| 856 |
// Determine if we need to load MSAA from resolve, and if so, make certain that key conditions |
| 857 |
// are met before proceeding. |
| 858 |
const bool loadMSAAFromResolve = RenderPassDescWillLoadMSAAFromResolve(rpDesc); |
| 859 |
if (loadMSAAFromResolve && (!vulkanResolveTexture || !fTargetTexture || |
| 860 |
!vulkanResolveTexture->supportsInputAttachmentUsage())) { |
| 861 |
SKGPU_LOG_E("Cannot begin render pass. In order to load MSAA from resolve, the color " |
| 862 |
"attachment must have input attachment usage and both the color and resolve " |
| 863 |
"attachments must be valid."); |
| 864 |
return false; |
| 865 |
} |
| 866 |
|
| 867 |
// Before beginning a renderpass, set all textures to the appropriate image layout. Whether a RP |
| 868 |
// must support reading from the dst as an input attachment affects some layout selections. |
| 869 |
setup_texture_layouts( |
| 870 |
this, |
| 871 |
fTargetTexture, |
| 872 |
vulkanResolveTexture, |
| 873 |
vulkanDepthStencilTexture, |
| 874 |
loadMSAAFromResolve, |
| 875 |
/*rpReadsDstAsInput=*/rpDesc.fDstReadStrategy == DstReadStrategy::kReadFromInput); |
| 876 |
|
| 877 |
// Gather clear values needed for RenderPassBeginInfo. Indexed by attachment number. |
| 878 |
STArray<kMaxNumAttachments, VkClearValue> clearValues; |
| 879 |
gather_clear_values(rpDesc, &clearValues); |
| 880 |
|
| 881 |
sk_sp<VulkanRenderPass> vulkanRenderPass = |
| 882 |
fResourceProvider->findOrCreateRenderPass(rpDesc, /*compatibleOnly=*/false); |
| 883 |
if (!vulkanRenderPass) { |
| 884 |
SKGPU_LOG_W("Could not create Vulkan RenderPass"); |
| 885 |
return false; |
| 886 |
} |
| 887 |
this->submitPipelineBarriers(); |
| 888 |
this->trackCommandBufferResource(vulkanRenderPass); |
| 889 |
|
| 890 |
int frameBufferWidth = 0; |
| 891 |
int frameBufferHeight = 0; |
| 892 |
if (colorTexture) { |
| 893 |
frameBufferWidth = colorTexture->dimensions().width(); |
| 894 |
frameBufferHeight = colorTexture->dimensions().height(); |
| 895 |
} else if (depthStencilTexture) { |
| 896 |
frameBufferWidth = depthStencilTexture->dimensions().width(); |
| 897 |
frameBufferHeight = depthStencilTexture->dimensions().height(); |
| 898 |
} |
| 899 |
sk_sp<VulkanFramebuffer> framebuffer = |
| 900 |
fResourceProvider->findOrCreateFramebuffer(fSharedContext, |
| 901 |
fTargetTexture, |
| 902 |
vulkanResolveTexture, |
| 903 |
vulkanDepthStencilTexture, |
| 904 |
rpDesc, |
| 905 |
*vulkanRenderPass, |
| 906 |
frameBufferWidth, |
| 907 |
frameBufferHeight); |
| 908 |
if (!framebuffer) { |
| 909 |
SKGPU_LOG_W("Could not find or create Vulkan Framebuffer"); |
| 910 |
return false; |
| 911 |
} |
| 912 |
|
| 913 |
bool useFullBounds = loadMSAAFromResolve && |
| 914 |
fSharedContext->vulkanCaps().mustLoadFullImageForMSAA(); |
| 915 |
|
| 916 |
VkRect2D renderArea = get_render_area(useFullBounds ? SkIRect::MakeWH(frameBufferWidth, |
| 917 |
frameBufferHeight) |
| 918 |
: renderPassBounds, |
| 919 |
vulkanRenderPass->granularity(), |
| 920 |
frameBufferWidth, |
| 921 |
frameBufferHeight); |
| 922 |
|
| 923 |
VkRenderPassBeginInfo beginInfo = {}; |
| 924 |
beginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; |
| 925 |
beginInfo.renderPass = vulkanRenderPass->renderPass(); |
| 926 |
beginInfo.framebuffer = framebuffer->framebuffer(); |
| 927 |
beginInfo.renderArea = renderArea; |
| 928 |
beginInfo.clearValueCount = clearValues.size(); |
| 929 |
beginInfo.pClearValues = clearValues.begin(); |
| 930 |
|
| 931 |
// Submit pipeline barriers to ensure any image layout transitions are recorded prior to |
| 932 |
// beginning the render pass. |
| 933 |
this->submitPipelineBarriers(); |
| 934 |
// TODO: If we add support for secondary command buffers, dynamically determine subpass contents |
| 935 |
VULKAN_CALL(fSharedContext->interface(), |
| 936 |
CmdBeginRenderPass(fPrimaryCommandBuffer, |
| 937 |
&beginInfo, |
| 938 |
VK_SUBPASS_CONTENTS_INLINE)); |
| 939 |
fActiveRenderPass = true; |
| 940 |
|
| 941 |
SkIRect nativeBounds = SkIRect::MakeXYWH(renderArea.offset.x, |
| 942 |
renderArea.offset.y, |
| 943 |
renderArea.extent.width, |
| 944 |
renderArea.extent.height); |
| 945 |
|
| 946 |
if (loadMSAAFromResolve && !this->loadMSAAFromResolve(rpDesc, |
| 947 |
*vulkanResolveTexture, |
| 948 |
fTargetTexture->dimensions(), |
| 949 |
nativeBounds)) { |
| 950 |
SKGPU_LOG_E("Failed to load MSAA from resolve"); |
| 951 |
this->endRenderPass(); |
| 952 |
return false; |
| 953 |
} |
| 954 |
|
| 955 |
// Once we have an active render pass, the command buffer should hold on to a frame buffer ref. |
| 956 |
this->trackCommandBufferResource(std::move(framebuffer)); |
| 957 |
return true; |
| 958 |
} |
| 959 |
|
| 960 |
void VulkanCommandBuffer::endRenderPass() { |
| 961 |
SkASSERT(fActive); |
| 962 |
VULKAN_CALL(fSharedContext->interface(), CmdEndRenderPass(fPrimaryCommandBuffer)); |
| 963 |
fActiveRenderPass = false; |
| 964 |
fTargetTexture = nullptr; |
| 965 |
} |
| 966 |
|
| 967 |
void VulkanCommandBuffer::addDrawPass(DrawPass* drawPass) { |
| 968 |
// If there is gradient data to bind, it must be done prior to draws. |
| 969 |
if (drawPass->floatStorageManager()->hasData()) { |
| 970 |
this->recordBufferBindingInfo(drawPass->floatStorageManager()->getBufferInfo(), |
| 971 |
UniformSlot::kGradient); |
| 972 |
} |
| 973 |
|
| 974 |
for (auto [type, cmdPtr] : drawPass->commands()) { |
| 975 |
switch (type) { |
| 976 |
case DrawPassCommands::Type::kBindGraphicsPipeline: { |
| 977 |
auto bgp = static_cast<DrawPassCommands::BindGraphicsPipeline*>(cmdPtr); |
| 978 |
this->bindGraphicsPipeline(drawPass->getPipeline(bgp->fPipelineIndex)); |
| 979 |
break; |
| 980 |
} |
| 981 |
case DrawPassCommands::Type::kSetBlendConstants: { |
| 982 |
auto sbc = static_cast<DrawPassCommands::SetBlendConstants*>(cmdPtr); |
| 983 |
this->setBlendConstants(sbc->fBlendConstants); |
| 984 |
break; |
| 985 |
} |
| 986 |
case DrawPassCommands::Type::kBindUniformBuffer: { |
| 987 |
auto bub = static_cast<DrawPassCommands::BindUniformBuffer*>(cmdPtr); |
| 988 |
this->recordBufferBindingInfo(bub->fInfo, bub->fSlot); |
| 989 |
break; |
| 990 |
} |
| 991 |
case DrawPassCommands::Type::kBindStaticDataBuffer: { |
| 992 |
auto bdb = static_cast<DrawPassCommands::BindStaticDataBuffer*>(cmdPtr); |
| 993 |
this->bindInputBuffer(bdb->fStaticData.fBuffer, bdb->fStaticData.fOffset, |
| 994 |
VulkanGraphicsPipeline::kStaticDataBufferIndex); |
| 995 |
break; |
| 996 |
} |
| 997 |
case DrawPassCommands::Type::kBindAppendDataBuffer: { |
| 998 |
auto bdb = static_cast<DrawPassCommands::BindAppendDataBuffer*>(cmdPtr); |
| 999 |
this->bindInputBuffer(bdb->fAppendData.fBuffer, bdb->fAppendData.fOffset, |
| 1000 |
VulkanGraphicsPipeline::kAppendDataBufferIndex); |
| 1001 |
break; |
| 1002 |
} |
| 1003 |
case DrawPassCommands::Type::kBindIndexBuffer: { |
| 1004 |
auto bdb = static_cast<DrawPassCommands::BindIndexBuffer*>(cmdPtr); |
| 1005 |
this->bindIndexBuffer( |
| 1006 |
bdb->fIndices.fBuffer, bdb->fIndices.fOffset); |
| 1007 |
break; |
| 1008 |
} |
| 1009 |
case DrawPassCommands::Type::kBindIndirectBuffer: { |
| 1010 |
auto bdb = static_cast<DrawPassCommands::BindIndirectBuffer*>(cmdPtr); |
| 1011 |
this->bindIndirectBuffer( |
| 1012 |
bdb->fIndirect.fBuffer, bdb->fIndirect.fOffset); |
| 1013 |
break; |
| 1014 |
} |
| 1015 |
case DrawPassCommands::Type::kBindTexturesAndSamplers: { |
| 1016 |
auto bts = static_cast<DrawPassCommands::BindTexturesAndSamplers*>(cmdPtr); |
| 1017 |
this->recordTextureAndSamplerDescSet(drawPass, bts); |
| 1018 |
break; |
| 1019 |
} |
| 1020 |
case DrawPassCommands::Type::kSetScissor: { |
| 1021 |
auto ss = static_cast<DrawPassCommands::SetScissor*>(cmdPtr); |
| 1022 |
this->setScissor(ss->fScissor); |
| 1023 |
break; |
| 1024 |
} |
| 1025 |
case DrawPassCommands::Type::kDraw: { |
| 1026 |
auto draw = static_cast<DrawPassCommands::Draw*>(cmdPtr); |
| 1027 |
this->draw(draw->fType, draw->fBaseVertex, draw->fVertexCount); |
| 1028 |
break; |
| 1029 |
} |
| 1030 |
case DrawPassCommands::Type::kDrawIndexed: { |
| 1031 |
auto draw = static_cast<DrawPassCommands::DrawIndexed*>(cmdPtr); |
| 1032 |
this->drawIndexed( |
| 1033 |
draw->fType, draw->fBaseIndex, draw->fIndexCount, draw->fBaseVertex); |
| 1034 |
break; |
| 1035 |
} |
| 1036 |
case DrawPassCommands::Type::kDrawInstanced: { |
| 1037 |
auto draw = static_cast<DrawPassCommands::DrawInstanced*>(cmdPtr); |
| 1038 |
this->drawInstanced(draw->fType, |
| 1039 |
draw->fBaseVertex, |
| 1040 |
draw->fVertexCount, |
| 1041 |
draw->fBaseInstance, |
| 1042 |
draw->fInstanceCount); |
| 1043 |
break; |
| 1044 |
} |
| 1045 |
case DrawPassCommands::Type::kDrawIndexedInstanced: { |
| 1046 |
auto draw = static_cast<DrawPassCommands::DrawIndexedInstanced*>(cmdPtr); |
| 1047 |
this->drawIndexedInstanced(draw->fType, |
| 1048 |
draw->fBaseIndex, |
| 1049 |
draw->fIndexCount, |
| 1050 |
draw->fBaseVertex, |
| 1051 |
draw->fBaseInstance, |
| 1052 |
draw->fInstanceCount); |
| 1053 |
break; |
| 1054 |
} |
| 1055 |
case DrawPassCommands::Type::kDrawIndirect: { |
| 1056 |
auto draw = static_cast<DrawPassCommands::DrawIndirect*>(cmdPtr); |
| 1057 |
this->drawIndirect(draw->fType); |
| 1058 |
break; |
| 1059 |
} |
| 1060 |
case DrawPassCommands::Type::kDrawIndexedIndirect: { |
| 1061 |
auto draw = static_cast<DrawPassCommands::DrawIndexedIndirect*>(cmdPtr); |
| 1062 |
this->drawIndexedIndirect(draw->fType); |
| 1063 |
break; |
| 1064 |
} |
| 1065 |
case DrawPassCommands::Type::kAddBarrier: { |
| 1066 |
auto barrierCmd = static_cast<DrawPassCommands::AddBarrier*>(cmdPtr); |
| 1067 |
this->addBarrier(barrierCmd->fType); |
| 1068 |
break; |
| 1069 |
} |
| 1070 |
} |
| 1071 |
} |
| 1072 |
} |
| 1073 |
|
| 1074 |
void VulkanCommandBuffer::bindGraphicsPipeline(const GraphicsPipeline* graphicsPipeline) { |
| 1075 |
SkASSERT(fActiveRenderPass); |
| 1076 |
// TODO(b/414645289): Once the front-end is made aware of dynamic state, it could recognize when |
| 1077 |
// only dynamic state has changed. In that case, since the pipeline doesn't change, this call |
| 1078 |
// can be avoided. The logic after this would then have to move to another place; for example |
| 1079 |
// setting dynamic states should move to a separate VulkanCommandBuffer call. |
| 1080 |
const auto* previousGraphicsPipeline = fActiveGraphicsPipeline; |
| 1081 |
fActiveGraphicsPipeline = static_cast<const VulkanGraphicsPipeline*>(graphicsPipeline); |
| 1082 |
VULKAN_CALL(fSharedContext->interface(), CmdBindPipeline(fPrimaryCommandBuffer, |
| 1083 |
VK_PIPELINE_BIND_POINT_GRAPHICS, |
| 1084 |
fActiveGraphicsPipeline->pipeline())); |
| 1085 |
// TODO(b/293924877): Compare pipeline layouts. If 2 pipelines have the same pipeline layout, |
| 1086 |
// then descriptor sets do not need to be re-bound. For now, simply force a re-binding of |
| 1087 |
// descriptor sets with any new bindGraphicsPipeline DrawPassCommand. |
| 1088 |
fBindUniformBuffers = true; |
| 1089 |
|
| 1090 |
fActiveGraphicsPipeline->updateDynamicState( |
| 1091 |
fSharedContext, fPrimaryCommandBuffer, previousGraphicsPipeline); |
| 1092 |
} |
| 1093 |
|
| 1094 |
void VulkanCommandBuffer::setBlendConstants(std::array<float, 4> blendConstants) { |
| 1095 |
SkASSERT(fActive); |
| 1096 |
if (fCachedBlendConstant != blendConstants) { |
| 1097 |
VULKAN_CALL(fSharedContext->interface(), |
| 1098 |
CmdSetBlendConstants(fPrimaryCommandBuffer, blendConstants.data())); |
| 1099 |
fCachedBlendConstant = blendConstants; |
| 1100 |
} |
| 1101 |
} |
| 1102 |
|
| 1103 |
void VulkanCommandBuffer::addBarrier(BarrierType type) { |
| 1104 |
SkASSERT(fTargetTexture); |
| 1105 |
|
| 1106 |
VkPipelineStageFlags dstStage; |
| 1107 |
VkAccessFlags dstAccess; |
| 1108 |
if (type == BarrierType::kAdvancedNoncoherentBlend) { |
| 1109 |
dstStage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 1110 |
dstAccess = VK_ACCESS_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT; |
| 1111 |
} else { |
| 1112 |
// If input reads are coherent, no barrier is needed |
| 1113 |
if (fSharedContext->vulkanCaps().isInputAttachmentReadCoherent()) { |
| 1114 |
return; |
| 1115 |
} |
| 1116 |
|
| 1117 |
SkASSERT(type == BarrierType::kReadDstFromInput); |
| 1118 |
dstStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; |
| 1119 |
dstAccess = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT; |
| 1120 |
} |
| 1121 |
VkImageMemoryBarrier imageMemoryBarrier = { |
| 1122 |
VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, |
| 1123 |
/*pNext=*/nullptr, |
| 1124 |
/*srcAccessMask=*/VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, |
| 1125 |
dstAccess, |
| 1126 |
/*oldLayout=*/VK_IMAGE_LAYOUT_GENERAL, |
| 1127 |
/*newLayout=*/VK_IMAGE_LAYOUT_GENERAL, |
| 1128 |
/*srcQueueFamilyIndex=*/VK_QUEUE_FAMILY_IGNORED, |
| 1129 |
/*dstQueueFamilyIndex=*/VK_QUEUE_FAMILY_IGNORED, |
| 1130 |
fTargetTexture->vkImage(), |
| 1131 |
/*subresourceRange=*/{ VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }}; |
| 1132 |
this->addImageMemoryBarrier(fTargetTexture, |
| 1133 |
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, |
| 1134 |
dstStage, |
| 1135 |
/*byRegion=*/true, |
| 1136 |
&imageMemoryBarrier); |
| 1137 |
} |
| 1138 |
|
| 1139 |
void VulkanCommandBuffer::recordBufferBindingInfo(const BindBufferInfo& info, UniformSlot slot) { |
| 1140 |
unsigned int bufferIndex = 0; |
| 1141 |
switch (slot) { |
| 1142 |
case UniformSlot::kCombinedUniforms: |
| 1143 |
bufferIndex = VulkanGraphicsPipeline::kCombinedUniformIndex; |
| 1144 |
break; |
| 1145 |
case UniformSlot::kGradient: |
| 1146 |
bufferIndex = VulkanGraphicsPipeline::kGradientBufferIndex; |
| 1147 |
break; |
| 1148 |
default: |
| 1149 |
SkASSERT(false); |
| 1150 |
} |
| 1151 |
|
| 1152 |
fUniformBuffersToBind[bufferIndex] = info; |
| 1153 |
fBindUniformBuffers = true; |
| 1154 |
} |
| 1155 |
|
| 1156 |
void VulkanCommandBuffer::syncDescriptorSets() { |
| 1157 |
if (fBindUniformBuffers) { |
| 1158 |
this->bindUniformBuffers(); |
| 1159 |
// Changes to descriptor sets in lower slot numbers disrupt later set bindings. Currently, |
| 1160 |
// the descriptor set which houses uniform buffers is at a lower slot than the texture / |
| 1161 |
// sampler set, so rebinding uniform buffers necessitates re-binding any texture/samplers. |
| 1162 |
fBindTextureSamplers = true; |
| 1163 |
} |
| 1164 |
if (fBindTextureSamplers) { |
| 1165 |
this->bindTextureSamplers(); |
| 1166 |
} |
| 1167 |
} |
| 1168 |
|
| 1169 |
void VulkanCommandBuffer::bindUniformBuffers() { |
| 1170 |
fBindUniformBuffers = false; |
| 1171 |
|
| 1172 |
// Define a container with size reserved for up to kNumUniformBuffers descriptors. Only add |
| 1173 |
// DescriptorData for uniforms that actually are used and need to be bound. |
| 1174 |
STArray<VulkanGraphicsPipeline::kNumUniformBuffers, DescriptorData> descriptors; |
| 1175 |
|
| 1176 |
// Up to kNumUniformBuffers can be used and require rebinding depending upon render pass info. |
| 1177 |
DescriptorType uniformBufferType = |
| 1178 |
fSharedContext->caps()->storageBufferSupport() ? DescriptorType::kStorageBuffer |
| 1179 |
: DescriptorType::kUniformBuffer; |
| 1180 |
if (fActiveGraphicsPipeline->hasCombinedUniforms() && |
| 1181 |
fUniformBuffersToBind[VulkanGraphicsPipeline::kCombinedUniformIndex].fBuffer) { |
| 1182 |
descriptors.push_back({ |
| 1183 |
uniformBufferType, |
| 1184 |
/*count=*/1, |
| 1185 |
VulkanGraphicsPipeline::kCombinedUniformIndex, |
| 1186 |
PipelineStageFlags::kVertexShader | PipelineStageFlags::kFragmentShader }); |
| 1187 |
} |
| 1188 |
|
| 1189 |
if (fActiveGraphicsPipeline->hasGradientBuffer() && |
| 1190 |
fUniformBuffersToBind[VulkanGraphicsPipeline::kGradientBufferIndex].fBuffer) { |
| 1191 |
SkASSERT(fSharedContext->caps()->gradientBufferSupport() && |
| 1192 |
fSharedContext->caps()->storageBufferSupport()); |
| 1193 |
descriptors.push_back({ DescriptorType::kStorageBuffer, /*count=*/1, |
| 1194 |
VulkanGraphicsPipeline::kGradientBufferIndex, |
| 1195 |
PipelineStageFlags::kFragmentShader }); |
| 1196 |
} |
| 1197 |
|
| 1198 |
// If no uniforms are used, we can go ahead and return since no descriptors need to be bound. |
| 1199 |
if (descriptors.empty()) { |
| 1200 |
return; |
| 1201 |
} |
| 1202 |
|
| 1203 |
skia_private::AutoSTMalloc<VulkanGraphicsPipeline::kNumUniformBuffers, uint32_t> |
| 1204 |
dynamicOffsets(descriptors.size()); |
| 1205 |
for (int i = 0; i < descriptors.size(); i++) { |
| 1206 |
int descriptorBindingIndex = descriptors[i].fBindingIndex; |
| 1207 |
SkASSERT(static_cast<unsigned long>(descriptorBindingIndex) < fUniformBuffersToBind.size()); |
| 1208 |
const auto& bindInfo = fUniformBuffersToBind[descriptorBindingIndex]; |
| 1209 |
#ifdef SK_DEBUG |
| 1210 |
if (descriptors[i].fPipelineStageFlags & PipelineStageFlags::kVertexShader) { |
| 1211 |
SkASSERT(bindInfo.fBuffer->isProtected() == Protected::kNo); |
| 1212 |
} |
| 1213 |
#endif |
| 1214 |
dynamicOffsets[i] = bindInfo.fOffset; |
| 1215 |
} |
| 1216 |
|
| 1217 |
sk_sp<VulkanDescriptorSet> descSet = fResourceProvider->findOrCreateUniformBuffersDescriptorSet( |
| 1218 |
descriptors, fUniformBuffersToBind); |
| 1219 |
if (!descSet) { |
| 1220 |
SKGPU_LOG_E("Unable to find or create uniform descriptor set"); |
| 1221 |
return; |
| 1222 |
} |
| 1223 |
|
| 1224 |
VULKAN_CALL(fSharedContext->interface(), |
| 1225 |
CmdBindDescriptorSets(fPrimaryCommandBuffer, |
| 1226 |
VK_PIPELINE_BIND_POINT_GRAPHICS, |
| 1227 |
fActiveGraphicsPipeline->layout(), |
| 1228 |
VulkanGraphicsPipeline::kUniformBufferDescSetIndex, |
| 1229 |
/*setCount=*/1, |
| 1230 |
descSet->descriptorSet(), |
| 1231 |
descriptors.size(), |
| 1232 |
dynamicOffsets.get())); |
| 1233 |
this->trackCommandBufferResource(std::move(descSet)); |
| 1234 |
} |
| 1235 |
|
| 1236 |
void VulkanCommandBuffer::bindInputBuffer(const Buffer* inputBuffer, VkDeviceSize offset, |
| 1237 |
uint32_t binding) { |
| 1238 |
if (inputBuffer) { |
| 1239 |
SkASSERT(inputBuffer->isProtected() == Protected::kNo); |
| 1240 |
VkBuffer vkBuffer = static_cast<const VulkanBuffer*>(inputBuffer)->vkBuffer(); |
| 1241 |
SkASSERT(vkBuffer != VK_NULL_HANDLE); |
| 1242 |
VULKAN_CALL(fSharedContext->interface(), CmdBindVertexBuffers(fPrimaryCommandBuffer, |
| 1243 |
binding, |
| 1244 |
/*bindingCount=*/1, |
| 1245 |
&vkBuffer, |
| 1246 |
&offset)); |
| 1247 |
} |
| 1248 |
} |
| 1249 |
|
| 1250 |
void VulkanCommandBuffer::bindIndexBuffer(const Buffer* indexBuffer, size_t offset) { |
| 1251 |
if (indexBuffer) { |
| 1252 |
SkASSERT(indexBuffer->isProtected() == Protected::kNo); |
| 1253 |
VkBuffer vkBuffer = static_cast<const VulkanBuffer*>(indexBuffer)->vkBuffer(); |
| 1254 |
SkASSERT(vkBuffer != VK_NULL_HANDLE); |
| 1255 |
VULKAN_CALL(fSharedContext->interface(), CmdBindIndexBuffer(fPrimaryCommandBuffer, |
| 1256 |
vkBuffer, |
| 1257 |
offset, |
| 1258 |
VK_INDEX_TYPE_UINT16)); |
| 1259 |
} |
| 1260 |
} |
| 1261 |
|
| 1262 |
void VulkanCommandBuffer::bindIndirectBuffer(const Buffer* indirectBuffer, size_t offset) { |
| 1263 |
// Indirect buffers are not bound via the command buffer, but specified in the draw cmd. |
| 1264 |
if (indirectBuffer) { |
| 1265 |
SkASSERT(indirectBuffer->isProtected() == Protected::kNo); |
| 1266 |
fBoundIndirectBuffer = static_cast<const VulkanBuffer*>(indirectBuffer)->vkBuffer(); |
| 1267 |
fBoundIndirectBufferOffset = offset; |
| 1268 |
} else { |
| 1269 |
fBoundIndirectBuffer = VK_NULL_HANDLE; |
| 1270 |
fBoundIndirectBufferOffset = 0; |
| 1271 |
} |
| 1272 |
} |
| 1273 |
|
| 1274 |
void VulkanCommandBuffer::recordTextureAndSamplerDescSet( |
| 1275 |
const DrawPass* drawPass, const DrawPassCommands::BindTexturesAndSamplers* command) { |
| 1276 |
SkASSERT(SkToBool(drawPass) == SkToBool(command)); |
| 1277 |
SkASSERT(fActiveGraphicsPipeline); |
| 1278 |
auto resetTextureAndSamplerState = [&]() { |
| 1279 |
fNumTextureSamplers = 0; |
| 1280 |
fTextureSamplerDescSetToBind = VK_NULL_HANDLE; |
| 1281 |
fBindTextureSamplers = false; |
| 1282 |
}; |
| 1283 |
|
| 1284 |
bool hasDstCopy = fActiveGraphicsPipeline->dstReadStrategy() == DstReadStrategy::kTextureCopy; |
| 1285 |
int numTexSamplers = (command ? command->fNumTexSamplers : 0) + hasDstCopy; |
| 1286 |
if (numTexSamplers == 0) { |
| 1287 |
resetTextureAndSamplerState(); |
| 1288 |
return; |
| 1289 |
} |
| 1290 |
|
| 1291 |
sk_sp<VulkanDescriptorSet> set; |
| 1292 |
const VulkanTexture* singleTexture = nullptr; |
| 1293 |
sk_sp<Sampler> singleSampler = nullptr; |
| 1294 |
if (numTexSamplers == 1) { |
| 1295 |
SkASSERT(hasDstCopy || command); |
| 1296 |
singleTexture = static_cast<const VulkanTexture*>( |
| 1297 |
hasDstCopy ? fDstCopy.first : command->fTextures[0]->texture()); |
| 1298 |
singleSampler = sk_ref_sp<Sampler>(hasDstCopy ? fDstCopy.second : getSampler(command, 0)); |
| 1299 |
SkASSERT(singleTexture && singleSampler); |
| 1300 |
set = singleTexture->getCachedSingleTextureDescriptorSet(singleSampler.get()); |
| 1301 |
} |
| 1302 |
|
| 1303 |
if (!set) { |
| 1304 |
TArray<DescriptorData> descriptors(numTexSamplers); |
| 1305 |
if (command) { |
| 1306 |
for (int i = 0; i < command->fNumTexSamplers; i++) { |
| 1307 |
// Embed immutable samplers into the descriptor set directly, which are held on the |
| 1308 |
// active graphics pipeline and can be indexed directly with `i`. |
| 1309 |
const Sampler* immutableSampler = fActiveGraphicsPipeline->immutableSampler(i); |
| 1310 |
SkASSERT(SkToBool(immutableSampler) == command->fSamplers[i].isImmutable()); |
| 1311 |
descriptors.push_back({DescriptorType::kCombinedTextureSampler, |
| 1312 |
/*count=*/1, |
| 1313 |
/*bindingIdx=*/i, |
| 1314 |
PipelineStageFlags::kFragmentShader, |
| 1315 |
immutableSampler}); |
| 1316 |
} |
| 1317 |
} |
| 1318 |
// If required the dst copy texture+sampler is the last one in the descriptor set |
| 1319 |
if (hasDstCopy) { |
| 1320 |
descriptors.push_back({DescriptorType::kCombinedTextureSampler, |
| 1321 |
/*count=*/1, |
| 1322 |
/*bindingIdx=*/numTexSamplers - 1, |
| 1323 |
PipelineStageFlags::kFragmentShader, |
| 1324 |
/*immutableSampler=*/nullptr}); |
| 1325 |
} |
| 1326 |
SkASSERT(descriptors.size() == numTexSamplers); |
| 1327 |
// Query resource provider to obtain a descriptor set for the texture/samplers |
| 1328 |
set = fResourceProvider->findOrCreateDescriptorSet( |
| 1329 |
{&descriptors.front(), (size_t)descriptors.size()}); |
| 1330 |
if (!set) { |
| 1331 |
SKGPU_LOG_E("Unable to find or create descriptor set"); |
| 1332 |
resetTextureAndSamplerState(); |
| 1333 |
return; |
| 1334 |
} |
| 1335 |
|
| 1336 |
TArray<VkWriteDescriptorSet> writeDescriptorSets(numTexSamplers); |
| 1337 |
TArray<VkDescriptorImageInfo> descriptorImageInfos(numTexSamplers); |
| 1338 |
if (command) { |
| 1339 |
for (int i = 0; i < command->fNumTexSamplers; ++i) { |
| 1340 |
auto texture = static_cast<const VulkanTexture*>(command->fTextures[i]->texture()); |
| 1341 |
// TODO(b/294198324): Investigate the root cause for null texture or samplers on |
| 1342 |
// Ubuntu QuadP400 GPU |
| 1343 |
if (!texture) { |
| 1344 |
SKGPU_LOG_E("Invalid texture in BindTexturesAndSamplers command."); |
| 1345 |
resetTextureAndSamplerState(); |
| 1346 |
return; |
| 1347 |
} |
| 1348 |
|
| 1349 |
if (command->fSamplers[i].isImmutable()) { |
| 1350 |
populate_write_info(set.get(), descriptorImageInfos, writeDescriptorSets, |
| 1351 |
texture, /*sampler=*/nullptr, i); |
| 1352 |
} else { |
| 1353 |
auto sampler = static_cast<const VulkanSampler*>( |
| 1354 |
fSharedContext->globalCache()->getDynamicSampler( |
| 1355 |
command->fSamplers[i])); |
| 1356 |
// b/294198324, see above |
| 1357 |
if (!sampler) { |
| 1358 |
SKGPU_LOG_E("Invalid dynamic sampler."); |
| 1359 |
resetTextureAndSamplerState(); |
| 1360 |
return; |
| 1361 |
} |
| 1362 |
populate_write_info(set.get(), descriptorImageInfos, writeDescriptorSets, |
| 1363 |
texture, sampler, i); |
| 1364 |
} |
| 1365 |
} |
| 1366 |
} |
| 1367 |
|
| 1368 |
if (fActiveGraphicsPipeline->dstReadStrategy() == DstReadStrategy::kTextureCopy) { |
| 1369 |
auto texture = static_cast<const VulkanTexture*>(fDstCopy.first); |
| 1370 |
auto sampler = static_cast<const VulkanSampler*>(fDstCopy.second); |
| 1371 |
// b/294198324, see above |
| 1372 |
if (!texture || !sampler) { |
| 1373 |
SKGPU_LOG_E("Invalid texture or sampler for dst-copy path."); |
| 1374 |
resetTextureAndSamplerState(); |
| 1375 |
return; |
| 1376 |
} |
| 1377 |
populate_write_info(set.get(), descriptorImageInfos, writeDescriptorSets, |
| 1378 |
texture, sampler, numTexSamplers - 1); |
| 1379 |
} |
| 1380 |
|
| 1381 |
SkASSERT(writeDescriptorSets.size() == numTexSamplers); |
| 1382 |
VULKAN_CALL(fSharedContext->interface(), |
| 1383 |
UpdateDescriptorSets(fSharedContext->device(), |
| 1384 |
writeDescriptorSets.size(), |
| 1385 |
writeDescriptorSets.begin(), |
| 1386 |
/*descriptorCopyCount=*/0, |
| 1387 |
/*pDescriptorCopies=*/nullptr)); |
| 1388 |
|
| 1389 |
if (numTexSamplers == 1) { |
| 1390 |
SkASSERT(singleTexture && singleSampler); |
| 1391 |
singleTexture->addCachedSingleTextureDescriptorSet(set, singleSampler); |
| 1392 |
} |
| 1393 |
} |
| 1394 |
|
| 1395 |
// Store the updated descriptor set to be actually bound later on. This avoids binding and |
| 1396 |
// potentially having to re-bind in cases where earlier descriptor sets change while going |
| 1397 |
// through drawpass commands. |
| 1398 |
fTextureSamplerDescSetToBind = *set->descriptorSet(); |
| 1399 |
fBindTextureSamplers = true; |
| 1400 |
fNumTextureSamplers = numTexSamplers; |
| 1401 |
this->trackCommandBufferResource(std::move(set)); |
| 1402 |
} |
| 1403 |
|
| 1404 |
void VulkanCommandBuffer::bindTextureSamplers() { |
| 1405 |
fBindTextureSamplers = false; |
| 1406 |
if (fTextureSamplerDescSetToBind != VK_NULL_HANDLE && |
| 1407 |
fActiveGraphicsPipeline->numFragTexturesAndSamplers() == fNumTextureSamplers) { |
| 1408 |
VULKAN_CALL(fSharedContext->interface(), |
| 1409 |
CmdBindDescriptorSets(fPrimaryCommandBuffer, |
| 1410 |
VK_PIPELINE_BIND_POINT_GRAPHICS, |
| 1411 |
fActiveGraphicsPipeline->layout(), |
| 1412 |
VulkanGraphicsPipeline::kTextureBindDescSetIndex, |
| 1413 |
/*setCount=*/1, |
| 1414 |
&fTextureSamplerDescSetToBind, |
| 1415 |
/*dynamicOffsetCount=*/0, |
| 1416 |
/*dynamicOffsets=*/nullptr)); |
| 1417 |
} |
| 1418 |
} |
| 1419 |
|
| 1420 |
void VulkanCommandBuffer::setScissor(const Scissor& scissor) { |
| 1421 |
this->setScissor(scissor.getRect(fReplayTranslation, fRenderPassBounds)); |
| 1422 |
} |
| 1423 |
|
| 1424 |
void VulkanCommandBuffer::setScissor(const SkIRect& rect) { |
| 1425 |
VkRect2D scissor = { |
| 1426 |
{rect.x(), rect.y()}, |
| 1427 |
{static_cast<unsigned int>(rect.width()), static_cast<unsigned int>(rect.height())}}; |
| 1428 |
VULKAN_CALL(fSharedContext->interface(), |
| 1429 |
CmdSetScissor(fPrimaryCommandBuffer, |
| 1430 |
/*firstScissor=*/0, |
| 1431 |
/*scissorCount=*/1, |
| 1432 |
&scissor)); |
| 1433 |
} |
| 1434 |
|
| 1435 |
void VulkanCommandBuffer::draw(PrimitiveType, |
| 1436 |
unsigned int baseVertex, |
| 1437 |
unsigned int vertexCount) { |
| 1438 |
SkASSERT(fActiveRenderPass); |
| 1439 |
this->syncDescriptorSets(); |
| 1440 |
// TODO: set primitive type via dynamic state if available |
| 1441 |
VULKAN_CALL(fSharedContext->interface(), |
| 1442 |
CmdDraw(fPrimaryCommandBuffer, |
| 1443 |
vertexCount, |
| 1444 |
/*instanceCount=*/1, |
| 1445 |
baseVertex, |
| 1446 |
/*firstInstance=*/0)); |
| 1447 |
} |
| 1448 |
|
| 1449 |
void VulkanCommandBuffer::drawIndexed(PrimitiveType, |
| 1450 |
unsigned int baseIndex, |
| 1451 |
unsigned int indexCount, |
| 1452 |
unsigned int baseVertex) { |
| 1453 |
SkASSERT(fActiveRenderPass); |
| 1454 |
this->syncDescriptorSets(); |
| 1455 |
// TODO: set primitive type via dynamic state if available |
| 1456 |
VULKAN_CALL(fSharedContext->interface(), |
| 1457 |
CmdDrawIndexed(fPrimaryCommandBuffer, |
| 1458 |
indexCount, |
| 1459 |
/*instanceCount=*/1, |
| 1460 |
baseIndex, |
| 1461 |
baseVertex, |
| 1462 |
/*firstInstance=*/0)); |
| 1463 |
} |
| 1464 |
|
| 1465 |
void VulkanCommandBuffer::drawInstanced(PrimitiveType, |
| 1466 |
unsigned int baseVertex, |
| 1467 |
unsigned int vertexCount, |
| 1468 |
unsigned int baseInstance, |
| 1469 |
unsigned int instanceCount) { |
| 1470 |
SkASSERT(fActiveRenderPass); |
| 1471 |
this->syncDescriptorSets(); |
| 1472 |
// TODO: set primitive type via dynamic state if available |
| 1473 |
VULKAN_CALL(fSharedContext->interface(), |
| 1474 |
CmdDraw(fPrimaryCommandBuffer, |
| 1475 |
vertexCount, |
| 1476 |
instanceCount, |
| 1477 |
baseVertex, |
| 1478 |
baseInstance)); |
| 1479 |
} |
| 1480 |
|
| 1481 |
void VulkanCommandBuffer::drawIndexedInstanced(PrimitiveType, |
| 1482 |
unsigned int baseIndex, |
| 1483 |
unsigned int indexCount, |
| 1484 |
unsigned int baseVertex, |
| 1485 |
unsigned int baseInstance, |
| 1486 |
unsigned int instanceCount) { |
| 1487 |
SkASSERT(fActiveRenderPass); |
| 1488 |
this->syncDescriptorSets(); |
| 1489 |
// TODO: set primitive type via dynamic state if available |
| 1490 |
VULKAN_CALL(fSharedContext->interface(), |
| 1491 |
CmdDrawIndexed(fPrimaryCommandBuffer, |
| 1492 |
indexCount, |
| 1493 |
instanceCount, |
| 1494 |
baseIndex, |
| 1495 |
baseVertex, |
| 1496 |
baseInstance)); |
| 1497 |
} |
| 1498 |
|
| 1499 |
void VulkanCommandBuffer::drawIndirect(PrimitiveType) { |
| 1500 |
SkASSERT(fActiveRenderPass); |
| 1501 |
this->syncDescriptorSets(); |
| 1502 |
// TODO: set primitive type via dynamic state if available |
| 1503 |
// Currently we can only support doing one indirect draw operation at a time, |
| 1504 |
// so stride is irrelevant. |
| 1505 |
VULKAN_CALL(fSharedContext->interface(), |
| 1506 |
CmdDrawIndirect(fPrimaryCommandBuffer, |
| 1507 |
fBoundIndirectBuffer, |
| 1508 |
fBoundIndirectBufferOffset, |
| 1509 |
/*drawCount=*/1, |
| 1510 |
/*stride=*/0)); |
| 1511 |
} |
| 1512 |
|
| 1513 |
void VulkanCommandBuffer::drawIndexedIndirect(PrimitiveType) { |
| 1514 |
SkASSERT(fActiveRenderPass); |
| 1515 |
this->syncDescriptorSets(); |
| 1516 |
// TODO: set primitive type via dynamic state if available |
| 1517 |
// Currently we can only support doing one indirect draw operation at a time, |
| 1518 |
// so stride is irrelevant. |
| 1519 |
VULKAN_CALL(fSharedContext->interface(), |
| 1520 |
CmdDrawIndexedIndirect(fPrimaryCommandBuffer, |
| 1521 |
fBoundIndirectBuffer, |
| 1522 |
fBoundIndirectBufferOffset, |
| 1523 |
/*drawCount=*/1, |
| 1524 |
/*stride=*/0)); |
| 1525 |
} |
| 1526 |
|
| 1527 |
bool VulkanCommandBuffer::onAddComputePass(DispatchGroupSpan) { return false; } |
| 1528 |
|
| 1529 |
bool VulkanCommandBuffer::onCopyBufferToBuffer(const Buffer* srcBuffer, |
| 1530 |
size_t srcOffset, |
| 1531 |
const Buffer* dstBuffer, |
| 1532 |
size_t dstOffset, |
| 1533 |
size_t size) { |
| 1534 |
auto vkSrcBuffer = static_cast<const VulkanBuffer*>(srcBuffer); |
| 1535 |
auto vkDstBuffer = static_cast<const VulkanBuffer*>(dstBuffer); |
| 1536 |
|
| 1537 |
SkASSERT(vkSrcBuffer->bufferUsageFlags() & VK_BUFFER_USAGE_TRANSFER_SRC_BIT); |
| 1538 |
SkASSERT(vkDstBuffer->bufferUsageFlags() & VK_BUFFER_USAGE_TRANSFER_DST_BIT); |
| 1539 |
|
| 1540 |
vkSrcBuffer->setBufferAccess(this, VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 1541 |
vkDstBuffer->setBufferAccess( |
| 1542 |
this, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 1543 |
|
| 1544 |
VkBufferCopy region = {}; |
| 1545 |
region.srcOffset = srcOffset; |
| 1546 |
region.dstOffset = dstOffset; |
| 1547 |
region.size = size; |
| 1548 |
|
| 1549 |
this->submitPipelineBarriers(); |
| 1550 |
|
| 1551 |
VULKAN_CALL(fSharedContext->interface(), |
| 1552 |
CmdCopyBuffer(fPrimaryCommandBuffer, |
| 1553 |
vkSrcBuffer->vkBuffer(), |
| 1554 |
vkDstBuffer->vkBuffer(), |
| 1555 |
/*regionCount=*/1, |
| 1556 |
®ion)); |
| 1557 |
|
| 1558 |
// TODO (b/394121386): We don't currently have a list of tracked buffers that are used on a |
| 1559 |
// RenderPass in order to put in any needed barriers (like we do for textures). If we did have |
| 1560 |
// one, then we would add the needed barriers for the buffers at the start of a render pass. |
| 1561 |
// Until we have such a system, we need to do some hackyness here to put in a barrier with the |
| 1562 |
// assumption that the buffer will be read after this write from the copy. The only buffer types |
| 1563 |
// we allow to be used as the dst of a transfer are vertex and index buffers. So we check the |
| 1564 |
// buffers usages for either of those and then set the corresponding access flag. |
| 1565 |
VkAccessFlags dstAccess = 0; |
| 1566 |
VkPipelineStageFlags dstStageMask = 0; |
| 1567 |
VkBufferUsageFlags bufferUsageFlags = vkDstBuffer->bufferUsageFlags(); |
| 1568 |
if (bufferUsageFlags & VK_BUFFER_USAGE_VERTEX_BUFFER_BIT) { |
| 1569 |
dstAccess = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT; |
| 1570 |
dstStageMask = VK_PIPELINE_STAGE_VERTEX_INPUT_BIT; |
| 1571 |
} else if (bufferUsageFlags & VK_BUFFER_USAGE_INDEX_BUFFER_BIT) { |
| 1572 |
dstAccess = VK_ACCESS_INDEX_READ_BIT; |
| 1573 |
dstStageMask = VK_PIPELINE_STAGE_VERTEX_INPUT_BIT; |
| 1574 |
} else if (vkDstBuffer->bufferUsedForCpuRead()) { |
| 1575 |
dstAccess = VK_ACCESS_HOST_READ_BIT; |
| 1576 |
dstStageMask = VK_PIPELINE_STAGE_HOST_BIT; |
| 1577 |
} else { |
| 1578 |
SkDEBUGFAIL("Unhandled type of buffer to buffer copy\n"); |
| 1579 |
return false; |
| 1580 |
} |
| 1581 |
SkASSERT(dstAccess); |
| 1582 |
vkDstBuffer->setBufferAccess(this, dstAccess, dstStageMask); |
| 1583 |
|
| 1584 |
return true; |
| 1585 |
} |
| 1586 |
|
| 1587 |
bool VulkanCommandBuffer::onCopyTextureToBuffer(const Texture* texture, |
| 1588 |
SkIRect srcRect, |
| 1589 |
const Buffer* buffer, |
| 1590 |
size_t bufferOffset, |
| 1591 |
size_t bufferRowBytes) { |
| 1592 |
const VulkanTexture* srcTexture = static_cast<const VulkanTexture*>(texture); |
| 1593 |
auto dstBuffer = static_cast<const VulkanBuffer*>(buffer); |
| 1594 |
SkASSERT(dstBuffer->bufferUsageFlags() & VK_BUFFER_USAGE_TRANSFER_DST_BIT); |
| 1595 |
|
| 1596 |
size_t bytesPerBlock = VkFormatBytesPerBlock(srcTexture->vulkanTextureInfo().fFormat); |
| 1597 |
|
| 1598 |
// Set up copy region |
| 1599 |
VkBufferImageCopy region = {}; |
| 1600 |
region.bufferOffset = bufferOffset; |
| 1601 |
// Vulkan expects bufferRowLength in texels, not bytes. |
| 1602 |
region.bufferRowLength = (uint32_t)(bufferRowBytes/bytesPerBlock); |
| 1603 |
region.bufferImageHeight = 0; // Tightly packed |
| 1604 |
region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, /*mipLevel=*/0, 0, 1 }; |
| 1605 |
region.imageOffset = { srcRect.left(), srcRect.top(), /*z=*/0 }; |
| 1606 |
region.imageExtent = { (uint32_t)srcRect.width(), (uint32_t)srcRect.height(), /*depth=*/1 }; |
| 1607 |
|
| 1608 |
// Enable editing of the source texture so we can change its layout so it can be copied from. |
| 1609 |
const_cast<VulkanTexture*>(srcTexture)->setImageLayout(this, |
| 1610 |
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1611 |
VK_ACCESS_TRANSFER_READ_BIT, |
| 1612 |
VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 1613 |
// Set current access mask for buffer |
| 1614 |
const_cast<VulkanBuffer*>(dstBuffer)->setBufferAccess(this, |
| 1615 |
VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1616 |
VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 1617 |
|
| 1618 |
this->submitPipelineBarriers(); |
| 1619 |
|
| 1620 |
VULKAN_CALL(fSharedContext->interface(), |
| 1621 |
CmdCopyImageToBuffer(fPrimaryCommandBuffer, |
| 1622 |
srcTexture->vkImage(), |
| 1623 |
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1624 |
dstBuffer->vkBuffer(), |
| 1625 |
/*regionCount=*/1, |
| 1626 |
®ion)); |
| 1627 |
return true; |
| 1628 |
} |
| 1629 |
|
| 1630 |
bool VulkanCommandBuffer::onCopyBufferToTexture(const Buffer* buffer, |
| 1631 |
const Texture* texture, |
| 1632 |
const BufferTextureCopyData* copyData, |
| 1633 |
int count) { |
| 1634 |
auto srcBuffer = static_cast<const VulkanBuffer*>(buffer); |
| 1635 |
SkASSERT(srcBuffer->bufferUsageFlags() & VK_BUFFER_USAGE_TRANSFER_SRC_BIT); |
| 1636 |
const VulkanTexture* dstTexture = static_cast<const VulkanTexture*>(texture); |
| 1637 |
|
| 1638 |
TextureFormat format = TextureInfoPriv::ViewFormat(dstTexture->textureInfo()); |
| 1639 |
size_t bytesPerBlock = TextureFormatBytesPerBlock(format); |
| 1640 |
SkISize oneBlockDims = CompressedDimensions(TextureFormatCompressionType(format), {1, 1}); |
| 1641 |
|
| 1642 |
// Set up copy regions. |
| 1643 |
TArray<VkBufferImageCopy> regions(count); |
| 1644 |
for (int i = 0; i < count; ++i) { |
| 1645 |
VkBufferImageCopy& region = regions.push_back(); |
| 1646 |
region = {}; |
| 1647 |
region.bufferOffset = copyData[i].fBufferOffset; |
| 1648 |
// copyData provides row length in bytes, but Vulkan expects bufferRowLength in texels. |
| 1649 |
// For compressed this is the number of logical pixels not the number of blocks. |
| 1650 |
region.bufferRowLength = |
| 1651 |
(uint32_t)((copyData[i].fBufferRowBytes/bytesPerBlock) * oneBlockDims.fWidth); |
| 1652 |
region.bufferImageHeight = 0; // Tightly packed |
| 1653 |
region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, copyData[i].fMipLevel, 0, 1 }; |
| 1654 |
region.imageOffset = { copyData[i].fRect.left(), |
| 1655 |
copyData[i].fRect.top(), |
| 1656 |
/*z=*/0 }; |
| 1657 |
region.imageExtent = { (uint32_t)copyData[i].fRect.width(), |
| 1658 |
(uint32_t)copyData[i].fRect.height(), |
| 1659 |
/*depth=*/1 }; |
| 1660 |
} |
| 1661 |
|
| 1662 |
// Enable editing of the destination texture so we can change its layout so it can be copied to. |
| 1663 |
const_cast<VulkanTexture*>(dstTexture)->setImageLayout(this, |
| 1664 |
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1665 |
VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1666 |
VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 1667 |
|
| 1668 |
this->submitPipelineBarriers(); |
| 1669 |
|
| 1670 |
VULKAN_CALL(fSharedContext->interface(), |
| 1671 |
CmdCopyBufferToImage(fPrimaryCommandBuffer, |
| 1672 |
srcBuffer->vkBuffer(), |
| 1673 |
dstTexture->vkImage(), |
| 1674 |
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1675 |
regions.size(), |
| 1676 |
regions.begin())); |
| 1677 |
return true; |
| 1678 |
} |
| 1679 |
|
| 1680 |
bool VulkanCommandBuffer::onCopyTextureToTexture(const Texture* src, |
| 1681 |
SkIRect srcRect, |
| 1682 |
const Texture* dst, |
| 1683 |
SkIPoint dstPoint, |
| 1684 |
int mipLevel) { |
| 1685 |
const VulkanTexture* srcTexture = static_cast<const VulkanTexture*>(src); |
| 1686 |
const VulkanTexture* dstTexture = static_cast<const VulkanTexture*>(dst); |
| 1687 |
|
| 1688 |
VkImageCopy copyRegion = {}; |
| 1689 |
copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1690 |
copyRegion.srcOffset = { srcRect.fLeft, srcRect.fTop, 0 }; |
| 1691 |
copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, (uint32_t)mipLevel, 0, 1 }; |
| 1692 |
copyRegion.dstOffset = { dstPoint.fX, dstPoint.fY, 0 }; |
| 1693 |
copyRegion.extent = { (uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1 }; |
| 1694 |
|
| 1695 |
// Enable editing of the src texture so we can change its layout so it can be copied from. |
| 1696 |
const_cast<VulkanTexture*>(srcTexture)->setImageLayout(this, |
| 1697 |
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1698 |
VK_ACCESS_TRANSFER_READ_BIT, |
| 1699 |
VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 1700 |
// Enable editing of the destination texture so we can change its layout so it can be copied to. |
| 1701 |
const_cast<VulkanTexture*>(dstTexture)->setImageLayout(this, |
| 1702 |
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1703 |
VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1704 |
VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 1705 |
|
| 1706 |
this->submitPipelineBarriers(); |
| 1707 |
|
| 1708 |
VULKAN_CALL(fSharedContext->interface(), |
| 1709 |
CmdCopyImage(fPrimaryCommandBuffer, |
| 1710 |
srcTexture->vkImage(), |
| 1711 |
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1712 |
dstTexture->vkImage(), |
| 1713 |
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1714 |
/*regionCount=*/1, |
| 1715 |
©Region)); |
| 1716 |
|
| 1717 |
return true; |
| 1718 |
} |
| 1719 |
|
| 1720 |
bool VulkanCommandBuffer::onSynchronizeBufferToCpu(const Buffer* buffer, bool* outDidResultInWork) { |
| 1721 |
static_cast<const VulkanBuffer*>(buffer)->setBufferAccess(this, |
| 1722 |
VK_ACCESS_HOST_READ_BIT, |
| 1723 |
VK_PIPELINE_STAGE_HOST_BIT); |
| 1724 |
|
| 1725 |
*outDidResultInWork = true; |
| 1726 |
return true; |
| 1727 |
} |
| 1728 |
|
| 1729 |
bool VulkanCommandBuffer::onClearBuffer(const Buffer*, size_t offset, size_t size) { |
| 1730 |
return false; |
| 1731 |
} |
| 1732 |
|
| 1733 |
void VulkanCommandBuffer::addBufferMemoryBarrier(const Resource* resource, |
| 1734 |
VkPipelineStageFlags srcStageMask, |
| 1735 |
VkPipelineStageFlags dstStageMask, |
| 1736 |
VkBufferMemoryBarrier* barrier) { |
| 1737 |
SkASSERT(resource); |
| 1738 |
this->pipelineBarrier(resource, |
| 1739 |
srcStageMask, |
| 1740 |
dstStageMask, |
| 1741 |
/*byRegion=*/false, |
| 1742 |
kBufferMemory_BarrierType, |
| 1743 |
barrier); |
| 1744 |
} |
| 1745 |
|
| 1746 |
void VulkanCommandBuffer::addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 1747 |
VkPipelineStageFlags dstStageMask, |
| 1748 |
VkBufferMemoryBarrier* barrier) { |
| 1749 |
// We don't pass in a resource here to the command buffer. The command buffer only is using it |
| 1750 |
// to hold a ref, but every place where we add a buffer memory barrier we are doing some other |
| 1751 |
// command with the buffer on the command buffer. Thus those other commands will already cause |
| 1752 |
// the command buffer to be holding a ref to the buffer. |
| 1753 |
this->pipelineBarrier(/*resource=*/nullptr, |
| 1754 |
srcStageMask, |
| 1755 |
dstStageMask, |
| 1756 |
/*byRegion=*/false, |
| 1757 |
kBufferMemory_BarrierType, |
| 1758 |
barrier); |
| 1759 |
} |
| 1760 |
|
| 1761 |
void VulkanCommandBuffer::addImageMemoryBarrier(const Resource* resource, |
| 1762 |
VkPipelineStageFlags srcStageMask, |
| 1763 |
VkPipelineStageFlags dstStageMask, |
| 1764 |
bool byRegion, |
| 1765 |
VkImageMemoryBarrier* barrier) { |
| 1766 |
SkASSERT(resource); |
| 1767 |
this->pipelineBarrier(resource, |
| 1768 |
srcStageMask, |
| 1769 |
dstStageMask, |
| 1770 |
byRegion, |
| 1771 |
kImageMemory_BarrierType, |
| 1772 |
barrier); |
| 1773 |
} |
| 1774 |
|
| 1775 |
void VulkanCommandBuffer::pipelineBarrier(const Resource* resource, |
| 1776 |
VkPipelineStageFlags srcStageMask, |
| 1777 |
VkPipelineStageFlags dstStageMask, |
| 1778 |
bool byRegion, |
| 1779 |
PipelineBarrierType barrierType, |
| 1780 |
void* barrier) { |
| 1781 |
// TODO: Do we need to handle wrapped command buffers? |
| 1782 |
// SkASSERT(!this->isWrapped()); |
| 1783 |
SkASSERT(fActive); |
| 1784 |
#ifdef SK_DEBUG |
| 1785 |
// For images we can have barriers inside of render passes but they require us to add more |
| 1786 |
// support in subpasses which need self dependencies to have barriers inside them. Also, we can |
| 1787 |
// never have buffer barriers inside of a render pass. For now we will just assert that we are |
| 1788 |
// not in a render pass. |
| 1789 |
bool isValidSubpassBarrier = false; |
| 1790 |
if (barrierType == kImageMemory_BarrierType) { |
| 1791 |
VkImageMemoryBarrier* imgBarrier = static_cast<VkImageMemoryBarrier*>(barrier); |
| 1792 |
isValidSubpassBarrier = (imgBarrier->newLayout == imgBarrier->oldLayout) && |
| 1793 |
(imgBarrier->srcQueueFamilyIndex == VK_QUEUE_FAMILY_IGNORED) && |
| 1794 |
(imgBarrier->dstQueueFamilyIndex == VK_QUEUE_FAMILY_IGNORED) && |
| 1795 |
byRegion; |
| 1796 |
} |
| 1797 |
SkASSERT(!fActiveRenderPass || isValidSubpassBarrier); |
| 1798 |
#endif |
| 1799 |
|
| 1800 |
if (barrierType == kBufferMemory_BarrierType) { |
| 1801 |
const VkBufferMemoryBarrier* barrierPtr = static_cast<VkBufferMemoryBarrier*>(barrier); |
| 1802 |
fBufferBarriers.push_back(*barrierPtr); |
| 1803 |
} else { |
| 1804 |
SkASSERT(barrierType == kImageMemory_BarrierType); |
| 1805 |
const VkImageMemoryBarrier* barrierPtr = static_cast<VkImageMemoryBarrier*>(barrier); |
| 1806 |
// We need to check if we are adding a pipeline barrier that covers part of the same |
| 1807 |
// subresource range as a barrier that is already in current batch. If it does, then we must |
| 1808 |
// submit the first batch because the vulkan spec does not define a specific ordering for |
| 1809 |
// barriers submitted in the same batch. |
| 1810 |
// TODO: Look if we can gain anything by merging barriers together instead of submitting |
| 1811 |
// the old ones. |
| 1812 |
for (int i = 0; i < fImageBarriers.size(); ++i) { |
| 1813 |
VkImageMemoryBarrier& currentBarrier = fImageBarriers[i]; |
| 1814 |
if (barrierPtr->image == currentBarrier.image) { |
| 1815 |
const VkImageSubresourceRange newRange = barrierPtr->subresourceRange; |
| 1816 |
const VkImageSubresourceRange oldRange = currentBarrier.subresourceRange; |
| 1817 |
SkASSERT(newRange.aspectMask == oldRange.aspectMask); |
| 1818 |
SkASSERT(newRange.baseArrayLayer == oldRange.baseArrayLayer); |
| 1819 |
SkASSERT(newRange.layerCount == oldRange.layerCount); |
| 1820 |
uint32_t newStart = newRange.baseMipLevel; |
| 1821 |
uint32_t newEnd = newRange.baseMipLevel + newRange.levelCount - 1; |
| 1822 |
uint32_t oldStart = oldRange.baseMipLevel; |
| 1823 |
uint32_t oldEnd = oldRange.baseMipLevel + oldRange.levelCount - 1; |
| 1824 |
if (std::max(newStart, oldStart) <= std::min(newEnd, oldEnd)) { |
| 1825 |
this->submitPipelineBarriers(); |
| 1826 |
break; |
| 1827 |
} |
| 1828 |
} |
| 1829 |
} |
| 1830 |
fImageBarriers.push_back(*barrierPtr); |
| 1831 |
} |
| 1832 |
fBarriersByRegion |= byRegion; |
| 1833 |
fSrcStageMask = fSrcStageMask | srcStageMask; |
| 1834 |
fDstStageMask = fDstStageMask | dstStageMask; |
| 1835 |
|
| 1836 |
if (fActiveRenderPass) { |
| 1837 |
this->submitPipelineBarriers(true); |
| 1838 |
} |
| 1839 |
} |
| 1840 |
|
| 1841 |
void VulkanCommandBuffer::submitPipelineBarriers(bool forSelfDependency) { |
| 1842 |
SkASSERT(fActive); |
| 1843 |
|
| 1844 |
// TODO: Do we need to handle SecondaryCommandBuffers as well? |
| 1845 |
|
| 1846 |
// Currently we never submit a pipeline barrier without at least one buffer or image barrier. |
| 1847 |
if (!fBufferBarriers.empty() || !fImageBarriers.empty()) { |
| 1848 |
// For images we can have barriers inside of render passes but they require us to add more |
| 1849 |
// support in subpasses which need self dependencies to have barriers inside them. Also, we |
| 1850 |
// can never have buffer barriers inside of a render pass. For now we will just assert that |
| 1851 |
// we are not in a render pass. |
| 1852 |
SkASSERT(!fActiveRenderPass || forSelfDependency); |
| 1853 |
// TODO: Do we need to handle wrapped CommandBuffers? |
| 1854 |
// SkASSERT(!this->isWrapped()); |
| 1855 |
SkASSERT(fSrcStageMask && fDstStageMask); |
| 1856 |
|
| 1857 |
VkDependencyFlags dependencyFlags = fBarriersByRegion ? VK_DEPENDENCY_BY_REGION_BIT : 0; |
| 1858 |
VULKAN_CALL(fSharedContext->interface(), |
| 1859 |
CmdPipelineBarrier(fPrimaryCommandBuffer, fSrcStageMask, fDstStageMask, |
| 1860 |
dependencyFlags, |
| 1861 |
/*memoryBarrierCount=*/0, /*pMemoryBarrier=*/nullptr, |
| 1862 |
fBufferBarriers.size(), fBufferBarriers.begin(), |
| 1863 |
fImageBarriers.size(), fImageBarriers.begin())); |
| 1864 |
fBufferBarriers.clear(); |
| 1865 |
fImageBarriers.clear(); |
| 1866 |
fBarriersByRegion = false; |
| 1867 |
fSrcStageMask = 0; |
| 1868 |
fDstStageMask = 0; |
| 1869 |
} |
| 1870 |
SkASSERT(fBufferBarriers.empty()); |
| 1871 |
SkASSERT(fImageBarriers.empty()); |
| 1872 |
SkASSERT(!fBarriersByRegion); |
| 1873 |
SkASSERT(!fSrcStageMask); |
| 1874 |
SkASSERT(!fDstStageMask); |
| 1875 |
} |
| 1876 |
|
| 1877 |
void VulkanCommandBuffer::nextSubpass() { |
| 1878 |
// TODO: Use VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS if we add secondary cmd buffers |
| 1879 |
VULKAN_CALL(fSharedContext->interface(), |
| 1880 |
CmdNextSubpass(fPrimaryCommandBuffer, VK_SUBPASS_CONTENTS_INLINE)); |
| 1881 |
} |
| 1882 |
|
| 1883 |
void VulkanCommandBuffer::setViewport(SkIRect viewport) { |
| 1884 |
VkViewport vkViewport = { |
| 1885 |
(float) viewport.fLeft, |
| 1886 |
(float) viewport.fTop, |
| 1887 |
(float) viewport.width(), |
| 1888 |
(float) viewport.height(), |
| 1889 |
0.0f, // minDepth |
| 1890 |
1.0f, // maxDepth |
| 1891 |
}; |
| 1892 |
VULKAN_CALL(fSharedContext->interface(), |
| 1893 |
CmdSetViewport(fPrimaryCommandBuffer, |
| 1894 |
/*firstViewport=*/0, |
| 1895 |
/*viewportCount=*/1, |
| 1896 |
&vkViewport)); |
| 1897 |
} |
| 1898 |
|
| 1899 |
} // namespace skgpu::graphite |