Hash :
c759b8b4
Author :
Date :
2019-01-03T15:16:50
Vulkan: More Vertex Array optimizations. Inlines a number of Vulkan vertex array methods. Also changes the way vertex buffers are bound. Note that Vulkan doesn't support NULL buffer bindings. Thus we create an emulated NULL buffer to work around the problem of having gaps in the bound vertex buffers. This allows us to use a single bind call for ranges of vertex buffers even when there are gaps. Also changes how vertex array dirty bits are reset. Instead of calling memset to clear the affected buffers we pass a mutable pointer to the Vertex Array sync state. This allows us to only reset the dirty bits that we sync. This saves on the memory clearing time. Improves perf by about 10% in the Vulkan VBO state change test. Bug: angleproject:3014 Change-Id: Ib7b742dff7897fc891606a652ea0b64255a24c86 Reviewed-on: https://chromium-review.googlesource.com/c/1390360 Commit-Queue: Jamie Madill <jmadill@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
//
// Copyright 2017 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// CommandGraph:
// Deferred work constructed by GL calls, that will later be flushed to Vulkan.
//
#include "libANGLE/renderer/vulkan/CommandGraph.h"
#include <iostream>
#include "libANGLE/renderer/vulkan/RenderTargetVk.h"
#include "libANGLE/renderer/vulkan/RendererVk.h"
#include "libANGLE/renderer/vulkan/vk_format_utils.h"
#include "libANGLE/renderer/vulkan/vk_helpers.h"
#include "third_party/trace_event/trace_event.h"
namespace rx
{
namespace vk
{
namespace
{
angle::Result InitAndBeginCommandBuffer(vk::Context *context,
const CommandPool &commandPool,
const VkCommandBufferInheritanceInfo &inheritanceInfo,
VkCommandBufferUsageFlags flags,
CommandBuffer *commandBuffer)
{
ASSERT(!commandBuffer->valid());
VkCommandBufferAllocateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
createInfo.commandPool = commandPool.getHandle();
createInfo.level = VK_COMMAND_BUFFER_LEVEL_SECONDARY;
createInfo.commandBufferCount = 1;
ANGLE_VK_TRY(context, commandBuffer->init(context->getDevice(), createInfo));
VkCommandBufferBeginInfo beginInfo = {};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = flags | VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
beginInfo.pInheritanceInfo = &inheritanceInfo;
ANGLE_VK_TRY(context, commandBuffer->begin(beginInfo));
return angle::Result::Continue;
}
const char *GetResourceTypeName(CommandGraphResourceType resourceType,
CommandGraphNodeFunction function)
{
switch (resourceType)
{
case CommandGraphResourceType::Buffer:
return "Buffer";
case CommandGraphResourceType::Framebuffer:
return "Framebuffer";
case CommandGraphResourceType::Image:
return "Image";
case CommandGraphResourceType::Query:
switch (function)
{
case CommandGraphNodeFunction::BeginQuery:
return "BeginQuery";
case CommandGraphNodeFunction::EndQuery:
return "EndQuery";
case CommandGraphNodeFunction::WriteTimestamp:
return "WriteTimestamp";
default:
UNREACHABLE();
return "Query";
}
default:
UNREACHABLE();
return "";
}
}
} // anonymous namespace
// CommandGraphResource implementation.
CommandGraphResource::CommandGraphResource(CommandGraphResourceType resourceType)
: mResourceType(resourceType), mCurrentWritingNode(nullptr)
{}
CommandGraphResource::~CommandGraphResource() = default;
bool CommandGraphResource::isResourceInUse(RendererVk *renderer) const
{
return renderer->isSerialInUse(mStoredQueueSerial);
}
bool CommandGraphResource::hasPendingWork(RendererVk *renderer) const
{
// If the renderer has a queue serial higher than the stored one, the command buffers recorded
// by this resource have already been submitted, so there is no pending work.
return mStoredQueueSerial == renderer->getCurrentQueueSerial();
}
// RecordableGraphResource implementation.
RecordableGraphResource::RecordableGraphResource(CommandGraphResourceType resourceType)
: CommandGraphResource(resourceType)
{}
RecordableGraphResource::~RecordableGraphResource() = default;
angle::Result RecordableGraphResource::recordCommands(Context *context,
CommandBuffer **commandBufferOut)
{
updateQueueSerial(context->getRenderer()->getCurrentQueueSerial());
if (!hasChildlessWritingNode() || hasStartedRenderPass())
{
startNewCommands(context->getRenderer());
return mCurrentWritingNode->beginOutsideRenderPassRecording(
context, context->getRenderer()->getCommandPool(), commandBufferOut);
}
CommandBuffer *outsideRenderPassCommands = mCurrentWritingNode->getOutsideRenderPassCommands();
if (!outsideRenderPassCommands->valid())
{
ANGLE_TRY(mCurrentWritingNode->beginOutsideRenderPassRecording(
context, context->getRenderer()->getCommandPool(), commandBufferOut));
}
else
{
*commandBufferOut = outsideRenderPassCommands;
}
return angle::Result::Continue;
}
const gl::Rectangle &RecordableGraphResource::getRenderPassRenderArea() const
{
ASSERT(hasStartedRenderPass());
return mCurrentWritingNode->getRenderPassRenderArea();
}
angle::Result RecordableGraphResource::beginRenderPass(Context *context,
const Framebuffer &framebuffer,
const gl::Rectangle &renderArea,
const RenderPassDesc &renderPassDesc,
const std::vector<VkClearValue> &clearValues,
CommandBuffer **commandBufferOut)
{
// If a barrier has been inserted in the meantime, stop the command buffer.
if (!hasChildlessWritingNode())
{
startNewCommands(context->getRenderer());
}
// Hard-code RenderPass to clear the first render target to the current clear value.
// TODO(jmadill): Proper clear value implementation. http://anglebug.com/2361
mCurrentWritingNode->storeRenderPassInfo(framebuffer, renderArea, renderPassDesc, clearValues);
return mCurrentWritingNode->beginInsideRenderPassRecording(context, commandBufferOut);
}
void RecordableGraphResource::addWriteDependency(RecordableGraphResource *writingResource)
{
CommandGraphNode *writingNode = writingResource->mCurrentWritingNode;
ASSERT(writingNode);
onWriteImpl(writingNode, writingResource->getStoredQueueSerial());
}
void RecordableGraphResource::addReadDependency(RecordableGraphResource *readingResource)
{
updateQueueSerial(readingResource->getStoredQueueSerial());
CommandGraphNode *readingNode = readingResource->mCurrentWritingNode;
ASSERT(readingNode);
if (mCurrentWritingNode)
{
// Ensure 'readingNode' happens after the current writing node.
CommandGraphNode::SetHappensBeforeDependency(mCurrentWritingNode, readingNode);
}
// Add the read node to the list of nodes currently reading this resource.
mCurrentReadingNodes.push_back(readingNode);
}
void RecordableGraphResource::finishCurrentCommands(RendererVk *renderer)
{
startNewCommands(renderer);
}
void RecordableGraphResource::startNewCommands(RendererVk *renderer)
{
CommandGraphNode *newCommands =
renderer->getCommandGraph()->allocateNode(CommandGraphNodeFunction::Generic);
newCommands->setDiagnosticInfo(mResourceType, reinterpret_cast<uintptr_t>(this));
onWriteImpl(newCommands, renderer->getCurrentQueueSerial());
}
void RecordableGraphResource::onWriteImpl(CommandGraphNode *writingNode, Serial currentSerial)
{
updateQueueSerial(currentSerial);
// Make sure any open reads and writes finish before we execute 'writingNode'.
if (!mCurrentReadingNodes.empty())
{
CommandGraphNode::SetHappensBeforeDependencies(mCurrentReadingNodes.data(),
mCurrentReadingNodes.size(), writingNode);
mCurrentReadingNodes.clear();
}
if (mCurrentWritingNode && mCurrentWritingNode != writingNode)
{
CommandGraphNode::SetHappensBeforeDependency(mCurrentWritingNode, writingNode);
}
mCurrentWritingNode = writingNode;
}
// QueryGraphResource implementation.
QueryGraphResource::QueryGraphResource() : CommandGraphResource(CommandGraphResourceType::Query) {}
QueryGraphResource::~QueryGraphResource() = default;
void QueryGraphResource::beginQuery(Context *context,
const QueryPool *queryPool,
uint32_t queryIndex)
{
startNewCommands(context->getRenderer(), CommandGraphNodeFunction::BeginQuery);
mCurrentWritingNode->setQueryPool(queryPool, queryIndex);
}
void QueryGraphResource::endQuery(Context *context, const QueryPool *queryPool, uint32_t queryIndex)
{
startNewCommands(context->getRenderer(), CommandGraphNodeFunction::EndQuery);
mCurrentWritingNode->setQueryPool(queryPool, queryIndex);
}
void QueryGraphResource::writeTimestamp(Context *context,
const QueryPool *queryPool,
uint32_t queryIndex)
{
startNewCommands(context->getRenderer(), CommandGraphNodeFunction::WriteTimestamp);
mCurrentWritingNode->setQueryPool(queryPool, queryIndex);
}
void QueryGraphResource::startNewCommands(RendererVk *renderer, CommandGraphNodeFunction function)
{
CommandGraph *commandGraph = renderer->getCommandGraph();
CommandGraphNode *newNode = commandGraph->allocateNode(function);
newNode->setDiagnosticInfo(mResourceType, reinterpret_cast<uintptr_t>(this));
commandGraph->setNewBarrier(newNode);
mStoredQueueSerial = renderer->getCurrentQueueSerial();
mCurrentWritingNode = newNode;
}
// CommandGraphNode implementation.
CommandGraphNode::CommandGraphNode(CommandGraphNodeFunction function)
: mRenderPassClearValues{},
mFunction(function),
mQueryPool(VK_NULL_HANDLE),
mQueryIndex(0),
mHasChildren(false),
mVisitedState(VisitedState::Unvisited),
mGlobalMemoryBarrierSrcAccess(0),
mGlobalMemoryBarrierDstAccess(0)
{}
CommandGraphNode::~CommandGraphNode()
{
mRenderPassFramebuffer.setHandle(VK_NULL_HANDLE);
// Command buffers are managed by the command pool, so don't need to be freed.
mOutsideRenderPassCommands.releaseHandle();
mInsideRenderPassCommands.releaseHandle();
}
CommandBuffer *CommandGraphNode::getOutsideRenderPassCommands()
{
ASSERT(!mHasChildren);
return &mOutsideRenderPassCommands;
}
angle::Result CommandGraphNode::beginOutsideRenderPassRecording(Context *context,
const CommandPool &commandPool,
CommandBuffer **commandsOut)
{
ASSERT(!mHasChildren);
VkCommandBufferInheritanceInfo inheritanceInfo = {};
inheritanceInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO;
inheritanceInfo.renderPass = VK_NULL_HANDLE;
inheritanceInfo.subpass = 0;
inheritanceInfo.framebuffer = VK_NULL_HANDLE;
inheritanceInfo.occlusionQueryEnable =
context->getRenderer()->getPhysicalDeviceFeatures().inheritedQueries;
inheritanceInfo.queryFlags = 0;
inheritanceInfo.pipelineStatistics = 0;
ANGLE_TRY(InitAndBeginCommandBuffer(context, commandPool, inheritanceInfo, 0,
&mOutsideRenderPassCommands));
*commandsOut = &mOutsideRenderPassCommands;
return angle::Result::Continue;
}
angle::Result CommandGraphNode::beginInsideRenderPassRecording(Context *context,
CommandBuffer **commandsOut)
{
ASSERT(!mHasChildren);
// Get a compatible RenderPass from the cache so we can initialize the inheritance info.
// TODO(jmadill): Support query for compatible/conformant render pass. http://anglebug.com/2361
RenderPass *compatibleRenderPass;
ANGLE_TRY(context->getRenderer()->getCompatibleRenderPass(context, mRenderPassDesc,
&compatibleRenderPass));
VkCommandBufferInheritanceInfo inheritanceInfo = {};
inheritanceInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO;
inheritanceInfo.renderPass = compatibleRenderPass->getHandle();
inheritanceInfo.subpass = 0;
inheritanceInfo.framebuffer = mRenderPassFramebuffer.getHandle();
inheritanceInfo.occlusionQueryEnable =
context->getRenderer()->getPhysicalDeviceFeatures().inheritedQueries;
inheritanceInfo.queryFlags = 0;
inheritanceInfo.pipelineStatistics = 0;
ANGLE_TRY(InitAndBeginCommandBuffer(
context, context->getRenderer()->getCommandPool(), inheritanceInfo,
VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT, &mInsideRenderPassCommands));
*commandsOut = &mInsideRenderPassCommands;
return angle::Result::Continue;
}
void CommandGraphNode::storeRenderPassInfo(const Framebuffer &framebuffer,
const gl::Rectangle renderArea,
const vk::RenderPassDesc &renderPassDesc,
const std::vector<VkClearValue> &clearValues)
{
mRenderPassDesc = renderPassDesc;
mRenderPassFramebuffer.setHandle(framebuffer.getHandle());
mRenderPassRenderArea = renderArea;
std::copy(clearValues.begin(), clearValues.end(), mRenderPassClearValues.begin());
}
// static
void CommandGraphNode::SetHappensBeforeDependencies(CommandGraphNode **beforeNodes,
size_t beforeNodesCount,
CommandGraphNode *afterNode)
{
afterNode->mParents.insert(afterNode->mParents.end(), beforeNodes,
beforeNodes + beforeNodesCount);
// TODO(jmadill): is there a faster way to do this?
for (size_t i = 0; i < beforeNodesCount; ++i)
{
beforeNodes[i]->setHasChildren();
ASSERT(beforeNodes[i] != afterNode && !beforeNodes[i]->isChildOf(afterNode));
}
}
void CommandGraphNode::SetHappensBeforeDependencies(CommandGraphNode *beforeNode,
CommandGraphNode **afterNodes,
size_t afterNodesCount)
{
for (size_t i = 0; i < afterNodesCount; ++i)
{
SetHappensBeforeDependency(beforeNode, afterNodes[i]);
}
}
bool CommandGraphNode::hasParents() const
{
return !mParents.empty();
}
void CommandGraphNode::setQueryPool(const QueryPool *queryPool, uint32_t queryIndex)
{
ASSERT(mFunction == CommandGraphNodeFunction::BeginQuery ||
mFunction == CommandGraphNodeFunction::EndQuery ||
mFunction == CommandGraphNodeFunction::WriteTimestamp);
mQueryPool = queryPool->getHandle();
mQueryIndex = queryIndex;
}
// Do not call this in anything but testing code, since it's slow.
bool CommandGraphNode::isChildOf(CommandGraphNode *parent)
{
std::set<CommandGraphNode *> visitedList;
std::vector<CommandGraphNode *> openList;
openList.insert(openList.begin(), mParents.begin(), mParents.end());
while (!openList.empty())
{
CommandGraphNode *current = openList.back();
openList.pop_back();
if (visitedList.count(current) == 0)
{
if (current == parent)
{
return true;
}
visitedList.insert(current);
openList.insert(openList.end(), current->mParents.begin(), current->mParents.end());
}
}
return false;
}
VisitedState CommandGraphNode::visitedState() const
{
return mVisitedState;
}
void CommandGraphNode::visitParents(std::vector<CommandGraphNode *> *stack)
{
ASSERT(mVisitedState == VisitedState::Unvisited);
stack->insert(stack->end(), mParents.begin(), mParents.end());
mVisitedState = VisitedState::Ready;
}
angle::Result CommandGraphNode::visitAndExecute(vk::Context *context,
Serial serial,
RenderPassCache *renderPassCache,
CommandBuffer *primaryCommandBuffer)
{
switch (mFunction)
{
case CommandGraphNodeFunction::Generic:
ASSERT(mQueryPool == VK_NULL_HANDLE);
// Record the deferred pipeline barrier if necessary.
ASSERT((mGlobalMemoryBarrierDstAccess == 0) == (mGlobalMemoryBarrierSrcAccess == 0));
if (mGlobalMemoryBarrierSrcAccess)
{
VkMemoryBarrier memoryBarrier = {};
memoryBarrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
memoryBarrier.srcAccessMask = mGlobalMemoryBarrierSrcAccess;
memoryBarrier.dstAccessMask = mGlobalMemoryBarrierDstAccess;
// Use the all pipe stage to keep the state management simple.
primaryCommandBuffer->pipelineBarrier(VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 1,
&memoryBarrier, 0, nullptr, 0, nullptr);
}
if (mOutsideRenderPassCommands.valid())
{
ANGLE_VK_TRY(context, mOutsideRenderPassCommands.end());
primaryCommandBuffer->executeCommands(1, &mOutsideRenderPassCommands);
}
if (mInsideRenderPassCommands.valid())
{
// Pull a compatible RenderPass from the cache.
// TODO(jmadill): Insert real ops and layout transitions.
RenderPass *renderPass = nullptr;
ANGLE_TRY(renderPassCache->getCompatibleRenderPass(context, serial, mRenderPassDesc,
&renderPass));
ANGLE_VK_TRY(context, mInsideRenderPassCommands.end());
VkRenderPassBeginInfo beginInfo = {};
beginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
beginInfo.renderPass = renderPass->getHandle();
beginInfo.framebuffer = mRenderPassFramebuffer.getHandle();
beginInfo.renderArea.offset.x = static_cast<uint32_t>(mRenderPassRenderArea.x);
beginInfo.renderArea.offset.y = static_cast<uint32_t>(mRenderPassRenderArea.y);
beginInfo.renderArea.extent.width =
static_cast<uint32_t>(mRenderPassRenderArea.width);
beginInfo.renderArea.extent.height =
static_cast<uint32_t>(mRenderPassRenderArea.height);
beginInfo.clearValueCount = mRenderPassDesc.attachmentCount();
beginInfo.pClearValues = mRenderPassClearValues.data();
primaryCommandBuffer->beginRenderPass(
beginInfo, VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS);
primaryCommandBuffer->executeCommands(1, &mInsideRenderPassCommands);
primaryCommandBuffer->endRenderPass();
}
break;
case CommandGraphNodeFunction::BeginQuery:
ASSERT(!mOutsideRenderPassCommands.valid() && !mInsideRenderPassCommands.valid());
ASSERT(mQueryPool != VK_NULL_HANDLE);
primaryCommandBuffer->resetQueryPool(mQueryPool, mQueryIndex, 1);
primaryCommandBuffer->beginQuery(mQueryPool, mQueryIndex, 0);
break;
case CommandGraphNodeFunction::EndQuery:
ASSERT(!mOutsideRenderPassCommands.valid() && !mInsideRenderPassCommands.valid());
ASSERT(mQueryPool != VK_NULL_HANDLE);
primaryCommandBuffer->endQuery(mQueryPool, mQueryIndex);
break;
case CommandGraphNodeFunction::WriteTimestamp:
ASSERT(!mOutsideRenderPassCommands.valid() && !mInsideRenderPassCommands.valid());
ASSERT(mQueryPool != VK_NULL_HANDLE);
primaryCommandBuffer->resetQueryPool(mQueryPool, mQueryIndex, 1);
primaryCommandBuffer->writeTimestamp(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, mQueryPool,
mQueryIndex);
break;
default:
UNREACHABLE();
}
mVisitedState = VisitedState::Visited;
return angle::Result::Continue;
}
const std::vector<CommandGraphNode *> &CommandGraphNode::getParentsForDiagnostics() const
{
return mParents;
}
void CommandGraphNode::setDiagnosticInfo(CommandGraphResourceType resourceType,
uintptr_t resourceID)
{
mResourceType = resourceType;
mResourceID = resourceID;
}
const gl::Rectangle &CommandGraphNode::getRenderPassRenderArea() const
{
return mRenderPassRenderArea;
}
// CommandGraph implementation.
CommandGraph::CommandGraph(bool enableGraphDiagnostics)
: mEnableGraphDiagnostics(enableGraphDiagnostics), mLastBarrierIndex(kInvalidNodeIndex)
{}
CommandGraph::~CommandGraph()
{
ASSERT(empty());
}
CommandGraphNode *CommandGraph::allocateNode(CommandGraphNodeFunction function)
{
// TODO(jmadill): Use a pool allocator for the CPU node allocations.
CommandGraphNode *newCommands = new CommandGraphNode(function);
mNodes.emplace_back(newCommands);
return newCommands;
}
void CommandGraph::setNewBarrier(CommandGraphNode *newBarrier)
{
size_t previousBarrierIndex = 0;
CommandGraphNode *previousBarrier = getLastBarrierNode(&previousBarrierIndex);
// Add a dependency from previousBarrier to all nodes in (previousBarrier, newBarrier].
if (previousBarrier && previousBarrierIndex + 1 < mNodes.size())
{
size_t afterNodesCount = mNodes.size() - (previousBarrierIndex + 1);
CommandGraphNode::SetHappensBeforeDependencies(
previousBarrier, &mNodes[previousBarrierIndex + 1], afterNodesCount);
}
// Add a dependency from all nodes in (previousBarrier, newBarrier) to newBarrier.
addDependenciesToNextBarrier(previousBarrierIndex + 1, mNodes.size() - 1, newBarrier);
mLastBarrierIndex = mNodes.size() - 1;
}
angle::Result CommandGraph::submitCommands(Context *context,
Serial serial,
RenderPassCache *renderPassCache,
CommandPool *commandPool,
CommandBuffer *primaryCommandBufferOut)
{
// There is no point in submitting an empty command buffer, so make sure not to call this
// function if there's nothing to do.
ASSERT(!mNodes.empty());
size_t previousBarrierIndex = 0;
CommandGraphNode *previousBarrier = getLastBarrierNode(&previousBarrierIndex);
// Add a dependency from previousBarrier to all nodes in (previousBarrier, end].
if (previousBarrier && previousBarrierIndex + 1 < mNodes.size())
{
size_t afterNodesCount = mNodes.size() - (previousBarrierIndex + 1);
CommandGraphNode::SetHappensBeforeDependencies(
previousBarrier, &mNodes[previousBarrierIndex + 1], afterNodesCount);
}
VkCommandBufferAllocateInfo primaryInfo = {};
primaryInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
primaryInfo.commandPool = commandPool->getHandle();
primaryInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
primaryInfo.commandBufferCount = 1;
ANGLE_VK_TRY(context, primaryCommandBufferOut->init(context->getDevice(), primaryInfo));
if (mEnableGraphDiagnostics)
{
dumpGraphDotFile(std::cout);
}
std::vector<CommandGraphNode *> nodeStack;
VkCommandBufferBeginInfo beginInfo = {};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
beginInfo.pInheritanceInfo = nullptr;
ANGLE_VK_TRY(context, primaryCommandBufferOut->begin(beginInfo));
ANGLE_TRY(context->getRenderer()->traceGpuEvent(
context, primaryCommandBufferOut, TRACE_EVENT_PHASE_BEGIN, "Primary Command Buffer"));
for (CommandGraphNode *topLevelNode : mNodes)
{
// Only process commands that don't have child commands. The others will be pulled in
// automatically. Also skip commands that have already been visited.
if (topLevelNode->hasChildren() || topLevelNode->visitedState() != VisitedState::Unvisited)
continue;
nodeStack.push_back(topLevelNode);
while (!nodeStack.empty())
{
CommandGraphNode *node = nodeStack.back();
switch (node->visitedState())
{
case VisitedState::Unvisited:
node->visitParents(&nodeStack);
break;
case VisitedState::Ready:
ANGLE_TRY(node->visitAndExecute(context, serial, renderPassCache,
primaryCommandBufferOut));
nodeStack.pop_back();
break;
case VisitedState::Visited:
nodeStack.pop_back();
break;
default:
UNREACHABLE();
break;
}
}
}
ANGLE_TRY(context->getRenderer()->traceGpuEvent(
context, primaryCommandBufferOut, TRACE_EVENT_PHASE_END, "Primary Command Buffer"));
ANGLE_VK_TRY(context, primaryCommandBufferOut->end());
clear();
return angle::Result::Continue;
}
bool CommandGraph::empty() const
{
return mNodes.empty();
}
void CommandGraph::clear()
{
mLastBarrierIndex = kInvalidNodeIndex;
// TODO(jmadill): Use pool allocator for performance. http://anglebug.com/2951
for (CommandGraphNode *node : mNodes)
{
delete node;
}
mNodes.clear();
}
// Dumps the command graph into a dot file that works with graphviz.
void CommandGraph::dumpGraphDotFile(std::ostream &out) const
{
// This ID maps a node pointer to a monatonic ID. It allows us to look up parent node IDs.
std::map<const CommandGraphNode *, int> nodeIDMap;
std::map<uintptr_t, int> objectIDMap;
// Map nodes to ids.
for (size_t nodeIndex = 0; nodeIndex < mNodes.size(); ++nodeIndex)
{
const CommandGraphNode *node = mNodes[nodeIndex];
nodeIDMap[node] = static_cast<int>(nodeIndex) + 1;
}
int bufferIDCounter = 1;
int framebufferIDCounter = 1;
int imageIDCounter = 1;
int queryIDCounter = 1;
out << "digraph {" << std::endl;
for (const CommandGraphNode *node : mNodes)
{
int nodeID = nodeIDMap[node];
std::stringstream strstr;
strstr << GetResourceTypeName(node->getResourceTypeForDiagnostics(), node->getFunction());
strstr << " ";
auto it = objectIDMap.find(node->getResourceIDForDiagnostics());
if (it != objectIDMap.end())
{
strstr << it->second;
}
else
{
int id = 0;
switch (node->getResourceTypeForDiagnostics())
{
case CommandGraphResourceType::Buffer:
id = bufferIDCounter++;
break;
case CommandGraphResourceType::Framebuffer:
id = framebufferIDCounter++;
break;
case CommandGraphResourceType::Image:
id = imageIDCounter++;
break;
case CommandGraphResourceType::Query:
id = queryIDCounter++;
break;
default:
UNREACHABLE();
break;
}
objectIDMap[node->getResourceIDForDiagnostics()] = id;
strstr << id;
}
const std::string &label = strstr.str();
out << " " << nodeID << "[label =<" << label << "<BR/> <FONT POINT-SIZE=\"10\">Node ID "
<< nodeID << "</FONT>>];" << std::endl;
}
for (const CommandGraphNode *node : mNodes)
{
int nodeID = nodeIDMap[node];
for (const CommandGraphNode *parent : node->getParentsForDiagnostics())
{
int parentID = nodeIDMap[parent];
out << " " << parentID << " -> " << nodeID << ";" << std::endl;
}
}
out << "}" << std::endl;
}
CommandGraphNode *CommandGraph::getLastBarrierNode(size_t *indexOut)
{
*indexOut = mLastBarrierIndex == kInvalidNodeIndex ? 0 : mLastBarrierIndex;
return mLastBarrierIndex == kInvalidNodeIndex ? nullptr : mNodes[mLastBarrierIndex];
}
void CommandGraph::addDependenciesToNextBarrier(size_t begin,
size_t end,
CommandGraphNode *nextBarrier)
{
for (size_t i = begin; i < end; ++i)
{
// As a small optimization, only add edges to childless nodes. The others have an
// indirect dependency.
if (!mNodes[i]->hasChildren())
{
CommandGraphNode::SetHappensBeforeDependency(mNodes[i], nextBarrier);
}
}
}
} // namespace vk
} // namespace rx