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