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