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